@genai-fi/nanogpt 0.2.11 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/dist/Generator.js +30 -25
  2. package/dist/NanoGPTModel.d.ts +13 -14
  3. package/dist/NanoGPTModel.js +167 -85
  4. package/dist/TeachableLLM.d.ts +3 -5
  5. package/dist/TeachableLLM.js +47 -35
  6. package/dist/Trainer.js +8 -8
  7. package/dist/concat-BIZS_td9.js +33 -0
  8. package/dist/data/parquet.js +1 -1
  9. package/dist/exports_layers-7idKoYqh.js +25 -0
  10. package/dist/{sum-D7fu15XL.js → gather-BPGW8RsB.js} +6 -8
  11. package/dist/index-C4L8Cm77.js +349 -0
  12. package/dist/{index-YPKosni4.js → index-pWA4_lUh.js} +1020 -782
  13. package/dist/layers/CausalSelfAttention.d.ts +11 -11
  14. package/dist/layers/CausalSelfAttention.js +71 -63
  15. package/dist/layers/MLP.d.ts +6 -7
  16. package/dist/layers/MLP.js +18 -16
  17. package/dist/layers/RMSNorm.d.ts +6 -7
  18. package/dist/layers/RMSNorm.js +15 -13
  19. package/dist/layers/RoPECache.d.ts +4 -6
  20. package/dist/layers/RoPECache.js +36 -23
  21. package/dist/layers/TiedEmbedding.d.ts +7 -8
  22. package/dist/layers/TiedEmbedding.js +16 -418
  23. package/dist/layers/TransformerBlock.d.ts +8 -9
  24. package/dist/layers/TransformerBlock.js +12 -12
  25. package/dist/main.d.ts +1 -0
  26. package/dist/main.js +35 -21
  27. package/dist/{mat_mul-Bu7bhLms.js → mat_mul-D7_a4KJn.js} +5 -5
  28. package/dist/moments-DfcpfwKi.js +132 -0
  29. package/dist/ones-Cog-G2ag.js +29 -0
  30. package/dist/ops/appendCache.d.ts +2 -0
  31. package/dist/ops/appendCache.js +9 -0
  32. package/dist/ops/attentionMask.d.ts +1 -1
  33. package/dist/ops/attentionMask.js +7 -85
  34. package/dist/ops/cpu/appendCache.d.ts +2 -0
  35. package/dist/ops/cpu/appendCache.js +28 -0
  36. package/dist/ops/cpu/attentionMask.js +18 -0
  37. package/dist/ops/cpu/gatherSub.d.ts +1 -0
  38. package/dist/ops/cpu/gatherSub.js +34 -0
  39. package/dist/ops/cpu/qkv.d.ts +5 -0
  40. package/dist/ops/cpu/qkv.js +38 -0
  41. package/dist/ops/cpu/rope.d.ts +6 -0
  42. package/dist/ops/cpu/rope.js +38 -0
  43. package/dist/ops/cpu/scatterSub.d.ts +1 -0
  44. package/dist/ops/cpu/scatterSub.js +70 -0
  45. package/dist/ops/gatherSub.d.ts +1 -1
  46. package/dist/ops/gatherSub.js +6 -63
  47. package/dist/ops/grads/attentionMask.d.ts +1 -0
  48. package/dist/ops/grads/attentionMask.js +21 -0
  49. package/dist/ops/grads/qkv.d.ts +1 -0
  50. package/dist/ops/grads/qkv.js +20 -0
  51. package/dist/ops/grads/rope.d.ts +1 -0
  52. package/dist/ops/grads/rope.js +14 -0
  53. package/dist/ops/node/sparseCrossEntropy.js +1 -1
  54. package/dist/ops/qkv.d.ts +1 -6
  55. package/dist/ops/qkv.js +7 -124
  56. package/dist/ops/rope.d.ts +0 -5
  57. package/dist/ops/rope.js +7 -150
  58. package/dist/ops/scatterSub.d.ts +1 -1
  59. package/dist/ops/scatterSub.js +6 -147
  60. package/dist/ops/webgl/appendCache.d.ts +1 -0
  61. package/dist/ops/webgl/appendCache.js +43 -0
  62. package/dist/ops/webgl/attentionMask.d.ts +1 -0
  63. package/dist/ops/webgl/attentionMask.js +43 -0
  64. package/dist/ops/webgl/gatherSub.d.ts +1 -0
  65. package/dist/ops/webgl/gatherSub.js +27 -0
  66. package/dist/ops/webgl/qkv.d.ts +1 -0
  67. package/dist/ops/webgl/qkv.js +46 -0
  68. package/dist/ops/webgl/rope.d.ts +1 -0
  69. package/dist/ops/webgl/rope.js +56 -0
  70. package/dist/ops/webgl/scatterSub.d.ts +1 -0
  71. package/dist/ops/webgl/scatterSub.js +27 -0
  72. package/dist/{parquet-BRl5lE_I.js → parquet-C0Tlmv9c.js} +3045 -3048
  73. package/dist/random_width-PbCt7RXv.js +15489 -0
  74. package/dist/range-CcDl05lo.js +26 -0
  75. package/dist/{reshape-DmnmKT6r.js → reshape-C8CR_Bad.js} +3 -3
  76. package/dist/sin-BJIrfnj7.js +47 -0
  77. package/dist/softmax-Be_lsqUc.js +105 -0
  78. package/dist/{complex-CJ-qCcLB.js → split-DZbvruEP.js} +6 -8
  79. package/dist/stack-BMm-efee.js +27 -0
  80. package/dist/sum-C7Mgy9Bw.js +104 -0
  81. package/dist/tensor-DJVbYhh1.js +24 -0
  82. package/dist/tensor2d-ZuQSh2D-.js +30 -0
  83. package/dist/tokeniser/bpe.d.ts +17 -6
  84. package/dist/tokeniser/bpe.js +88 -60
  85. package/dist/training/AdamExt.js +1 -1
  86. package/dist/training/DatasetBuilder.d.ts +6 -6
  87. package/dist/training/DatasetBuilder.js +1262 -17
  88. package/dist/training/Evaluator.d.ts +3 -2
  89. package/dist/training/FullTrainer.d.ts +9 -8
  90. package/dist/training/FullTrainer.js +26 -25
  91. package/dist/training/LayerTrainer.d.ts +9 -8
  92. package/dist/training/LayerTrainer.js +34 -33
  93. package/dist/training/Trainer.d.ts +22 -21
  94. package/dist/training/Trainer.js +21 -18
  95. package/dist/training/sparseCrossEntropy.js +22 -166
  96. package/dist/utilities/dummy.js +10 -8
  97. package/dist/utilities/generate.js +14 -11
  98. package/dist/utilities/load.d.ts +1 -2
  99. package/dist/utilities/load.js +37 -35
  100. package/dist/utilities/profile.js +1 -1
  101. package/dist/utilities/save.js +14 -9
  102. package/dist/utilities/tokenParse.d.ts +1 -1
  103. package/dist/utilities/tokenParse.js +7 -61
  104. package/dist/utilities/weights.d.ts +3 -3
  105. package/dist/utilities/weights.js +21 -19
  106. package/dist/variable-Dl_ub3pk.js +23 -0
  107. package/dist/{stack-BtKpB0Ry.js → zeros-CCy9C3uU.js} +18 -16
  108. package/package.json +2 -1
  109. package/dist/assets/worker-BYeSPNkq.js +0 -1
  110. package/dist/tokeniser/NodeTokeniser.d.ts +0 -20
  111. package/dist/tokeniser/NodeTokeniser.js +0 -46
  112. package/dist/tokeniser/WebTokeniser.d.ts +0 -18
  113. package/dist/tokeniser/WebTokeniser.js +0 -96
  114. package/dist/tokeniser/worker.js +0 -53
  115. /package/dist/{tokeniser/worker.d.ts → ops/cpu/attentionMask.d.ts} +0 -0
@@ -0,0 +1,26 @@
1
+ import { E as e, R as f } from "./index-pWA4_lUh.js";
2
+ /**
3
+ * @license
4
+ * Copyright 2018 Google LLC. All Rights Reserved.
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ * =============================================================================
17
+ */
18
+ function E(n, o, r = 1, a = "float32") {
19
+ if (r === 0)
20
+ throw new Error("Cannot have a step of zero");
21
+ const t = { start: n, stop: o, step: r, dtype: a };
22
+ return e.runKernel(f, {}, t);
23
+ }
24
+ export {
25
+ E as r
26
+ };
@@ -1,4 +1,4 @@
1
- import { o, d as t, E as a, R as p } from "./index-YPKosni4.js";
1
+ import { o, h as t, E as a, q as p } from "./index-pWA4_lUh.js";
2
2
  /**
3
3
  * @license
4
4
  * Copyright 2020 Google LLC. All Rights Reserved.
@@ -19,7 +19,7 @@ function c(r, s) {
19
19
  const e = { x: t(r, "x", "reshape", "string_or_numeric") }, n = { shape: s };
20
20
  return a.runKernel(p, e, n);
21
21
  }
22
- const x = /* @__PURE__ */ o({ reshape_: c });
22
+ const u = /* @__PURE__ */ o({ reshape_: c });
23
23
  export {
24
- x as r
24
+ u as r
25
25
  };
@@ -0,0 +1,47 @@
1
+ import { o, h as t, E as c, K as a, L as e } from "./index-pWA4_lUh.js";
2
+ /**
3
+ * @license
4
+ * Copyright 2018 Google LLC. All Rights Reserved.
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ * =============================================================================
17
+ */
18
+ function i(n) {
19
+ const s = { x: t(n, "x", "cos", "float32") };
20
+ return c.runKernel(a, s);
21
+ }
22
+ const f = /* @__PURE__ */ o({ cos_: i });
23
+ /**
24
+ * @license
25
+ * Copyright 2018 Google LLC. All Rights Reserved.
26
+ * Licensed under the Apache License, Version 2.0 (the "License");
27
+ * you may not use this file except in compliance with the License.
28
+ * You may obtain a copy of the License at
29
+ *
30
+ * http://www.apache.org/licenses/LICENSE-2.0
31
+ *
32
+ * Unless required by applicable law or agreed to in writing, software
33
+ * distributed under the License is distributed on an "AS IS" BASIS,
34
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35
+ * See the License for the specific language governing permissions and
36
+ * limitations under the License.
37
+ * =============================================================================
38
+ */
39
+ function x(n) {
40
+ const s = { x: t(n, "x", "sin", "float32") };
41
+ return c.runKernel(e, s);
42
+ }
43
+ const p = /* @__PURE__ */ o({ sin_: x });
44
+ export {
45
+ f as c,
46
+ p as s
47
+ };
@@ -0,0 +1,105 @@
1
+ import { o as r, h as e, E as c, _ as $, $ as h, a0 as E, s as S, Z as _, a1 as d } from "./index-pWA4_lUh.js";
2
+ import { m as k, s as w, e as K } from "./sum-C7Mgy9Bw.js";
3
+ import { r as l } from "./reshape-C8CR_Bad.js";
4
+ /**
5
+ * @license
6
+ * Copyright 2018 Google LLC. All Rights Reserved.
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ * =============================================================================
19
+ */
20
+ function T(n) {
21
+ const s = { x: e(n, "x", "exp") };
22
+ return c.runKernel($, s);
23
+ }
24
+ const b = /* @__PURE__ */ r({ exp_: T });
25
+ /**
26
+ * @license
27
+ * Copyright 2018 Google LLC. All Rights Reserved.
28
+ * Licensed under the Apache License, Version 2.0 (the "License");
29
+ * you may not use this file except in compliance with the License.
30
+ * You may obtain a copy of the License at
31
+ *
32
+ * http://www.apache.org/licenses/LICENSE-2.0
33
+ *
34
+ * Unless required by applicable law or agreed to in writing, software
35
+ * distributed under the License is distributed on an "AS IS" BASIS,
36
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37
+ * See the License for the specific language governing permissions and
38
+ * limitations under the License.
39
+ * =============================================================================
40
+ */
41
+ function L(n) {
42
+ const s = { x: e(n, "x", "log", "float32") };
43
+ return c.runKernel(h, s);
44
+ }
45
+ const N = /* @__PURE__ */ r({ log_: L });
46
+ /**
47
+ * @license
48
+ * Copyright 2020 Google LLC. All Rights Reserved.
49
+ * Licensed under the Apache License, Version 2.0 (the "License");
50
+ * you may not use this file except in compliance with the License.
51
+ * You may obtain a copy of the License at
52
+ *
53
+ * http://www.apache.org/licenses/LICENSE-2.0
54
+ *
55
+ * Unless required by applicable law or agreed to in writing, software
56
+ * distributed under the License is distributed on an "AS IS" BASIS,
57
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
58
+ * See the License for the specific language governing permissions and
59
+ * limitations under the License.
60
+ * =============================================================================
61
+ */
62
+ function v(n, o = null, s = !1) {
63
+ const t = e(n, "x", "logSumExp"), a = E(o, t.shape), p = k(
64
+ t,
65
+ a,
66
+ !0
67
+ /* keepDims */
68
+ ), i = S(t, p), f = b(i), m = w(f, a), u = N(m), x = _(l(p, u.shape), u);
69
+ if (s) {
70
+ const g = K(x.shape, a);
71
+ return l(x, g);
72
+ }
73
+ return x;
74
+ }
75
+ const I = /* @__PURE__ */ r({ logSumExp_: v });
76
+ /**
77
+ * @license
78
+ * Copyright 2018 Google LLC. All Rights Reserved.
79
+ * Licensed under the Apache License, Version 2.0 (the "License");
80
+ * you may not use this file except in compliance with the License.
81
+ * You may obtain a copy of the License at
82
+ *
83
+ * http://www.apache.org/licenses/LICENSE-2.0
84
+ *
85
+ * Unless required by applicable law or agreed to in writing, software
86
+ * distributed under the License is distributed on an "AS IS" BASIS,
87
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
88
+ * See the License for the specific language governing permissions and
89
+ * limitations under the License.
90
+ * =============================================================================
91
+ */
92
+ function y(n, o = -1) {
93
+ const s = e(n, "logits", "softmax", "float32");
94
+ if (o === -1 && (o = s.rank - 1), o !== s.rank - 1)
95
+ throw Error(`Softmax along a non-last dimension is not yet supported. Logits was rank ${s.rank} and dim was ${o}`);
96
+ const t = { logits: s }, a = { dim: o };
97
+ return c.runKernel(d, t, a);
98
+ }
99
+ const M = /* @__PURE__ */ r({ softmax_: y });
100
+ export {
101
+ N as a,
102
+ b as e,
103
+ I as l,
104
+ M as s
105
+ };
@@ -1,4 +1,4 @@
1
- import { o as c, d as s, g as n, E as m, C as r } from "./index-YPKosni4.js";
1
+ import { o as p, h as i, E as a, S as c } from "./index-pWA4_lUh.js";
2
2
  /**
3
3
  * @license
4
4
  * Copyright 2020 Google LLC. All Rights Reserved.
@@ -15,13 +15,11 @@ import { o as c, d as s, g as n, E as m, C as r } from "./index-YPKosni4.js";
15
15
  * limitations under the License.
16
16
  * =============================================================================
17
17
  */
18
- function l(o, p) {
19
- const a = s(o, "real", "complex"), e = s(p, "imag", "complex");
20
- n(a.shape, e.shape, `real and imag shapes, ${a.shape} and ${e.shape}, must match in call to tf.complex().`);
21
- const t = { real: a, imag: e };
22
- return m.runKernel(r, t);
18
+ function e(t, s, o = 0) {
19
+ const n = { x: i(t, "x", "split") }, r = { numOrSizeSplits: s, axis: o };
20
+ return a.runKernel(c, n, r);
23
21
  }
24
- const i = /* @__PURE__ */ c({ complex_: l });
22
+ const u = /* @__PURE__ */ p({ split_: e });
25
23
  export {
26
- i as c
24
+ u as s
27
25
  };
@@ -0,0 +1,27 @@
1
+ import { o as e, i as c, j as n, E as i, P as k } from "./index-pWA4_lUh.js";
2
+ /**
3
+ * @license
4
+ * Copyright 2020 Google LLC. All Rights Reserved.
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ * =============================================================================
17
+ */
18
+ function u(r, t = 0) {
19
+ const s = c(r, "tensors", "stack", "string_or_numeric");
20
+ n(s.length >= 1, () => "Pass at least one tensor to tf.stack"), s.length > 0 && n(t <= s[0].rank, () => "Axis must be <= rank of the tensor");
21
+ const o = s, a = { axis: t };
22
+ return i.runKernel(k, o, a);
23
+ }
24
+ const l = /* @__PURE__ */ e({ stack_: u });
25
+ export {
26
+ l as s
27
+ };
@@ -0,0 +1,104 @@
1
+ import { o as c, h as i, E as a, T as f, U as m, V as p } from "./index-pWA4_lUh.js";
2
+ /**
3
+ * @license
4
+ * Copyright 2017 Google LLC. All Rights Reserved.
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ * =============================================================================
17
+ */
18
+ function d(e, n) {
19
+ for (let t = 0; t < e.length; ++t)
20
+ if (e[e.length - t - 1] !== n - 1 - t)
21
+ return !1;
22
+ return !0;
23
+ }
24
+ function x(e, n, t) {
25
+ const s = e.length + n.length, r = [];
26
+ let o = 0, l = 0;
27
+ for (let u = 0; u < s; u++)
28
+ t.indexOf(u) === -1 ? r.push(e[o++]) : r.push(n[l++]);
29
+ return r;
30
+ }
31
+ function S(e, n) {
32
+ const t = [], s = e.length;
33
+ for (let o = 0; o < s; o++)
34
+ n.indexOf(o) === -1 && t.push(e[o]);
35
+ const r = n.map((o) => e[o]);
36
+ return [t, r];
37
+ }
38
+ function A(e, n) {
39
+ const t = n.map((s) => 1);
40
+ return x(e, t, n);
41
+ }
42
+ function E(e, n) {
43
+ if (d(e, n))
44
+ return null;
45
+ const t = [];
46
+ for (let s = 0; s < n; ++s)
47
+ e.indexOf(s) === -1 && t.push(s);
48
+ return e.forEach((s) => t.push(s)), t;
49
+ }
50
+ function O(e) {
51
+ return e.map((n, t) => [t, n]).sort((n, t) => n[1] - t[1]).map((n) => n[0]);
52
+ }
53
+ /**
54
+ * @license
55
+ * Copyright 2020 Google LLC. All Rights Reserved.
56
+ * Licensed under the Apache License, Version 2.0 (the "License");
57
+ * you may not use this file except in compliance with the License.
58
+ * You may obtain a copy of the License at
59
+ *
60
+ * http://www.apache.org/licenses/LICENSE-2.0
61
+ *
62
+ * Unless required by applicable law or agreed to in writing, software
63
+ * distributed under the License is distributed on an "AS IS" BASIS,
64
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
65
+ * See the License for the specific language governing permissions and
66
+ * limitations under the License.
67
+ * =============================================================================
68
+ */
69
+ function h(e, n = null, t = !1) {
70
+ const r = { x: i(e, "x", "max") }, o = { reductionIndices: n, keepDims: t };
71
+ return a.runKernel(f, r, o);
72
+ }
73
+ const T = /* @__PURE__ */ c({ max_: h });
74
+ /**
75
+ * @license
76
+ * Copyright 2018 Google LLC. All Rights Reserved.
77
+ * Licensed under the Apache License, Version 2.0 (the "License");
78
+ * you may not use this file except in compliance with the License.
79
+ * You may obtain a copy of the License at
80
+ *
81
+ * http://www.apache.org/licenses/LICENSE-2.0
82
+ *
83
+ * Unless required by applicable law or agreed to in writing, software
84
+ * distributed under the License is distributed on an "AS IS" BASIS,
85
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
86
+ * See the License for the specific language governing permissions and
87
+ * limitations under the License.
88
+ * =============================================================================
89
+ */
90
+ function g(e, n = null, t = !1) {
91
+ let s = i(e, "x", "sum");
92
+ s.dtype === "bool" && (s = m(s, "int32"));
93
+ const r = { x: s }, o = { axis: n, keepDims: t };
94
+ return a.runKernel(p, r, o);
95
+ }
96
+ const b = /* @__PURE__ */ c({ sum_: g });
97
+ export {
98
+ O as a,
99
+ S as c,
100
+ A as e,
101
+ E as g,
102
+ T as m,
103
+ b as s
104
+ };
@@ -0,0 +1,24 @@
1
+ import { D as t, F as a } from "./index-pWA4_lUh.js";
2
+ /**
3
+ * @license
4
+ * Copyright 2018 Google LLC. All Rights Reserved.
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ * =============================================================================
17
+ */
18
+ function f(r, n, e) {
19
+ const o = t(r, e);
20
+ return a(r, n, o, e);
21
+ }
22
+ export {
23
+ f as t
24
+ };
@@ -0,0 +1,30 @@
1
+ import { y as t, D as s, F as a } from "./index-pWA4_lUh.js";
2
+ /**
3
+ * @license
4
+ * Copyright 2018 Google LLC. All Rights Reserved.
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ * =============================================================================
17
+ */
18
+ function i(n, r, o) {
19
+ if (t(n), r != null && r.length !== 2)
20
+ throw new Error("tensor2d() requires shape to have two numbers");
21
+ const e = s(n, o);
22
+ if (e.length !== 2 && e.length !== 1)
23
+ throw new Error("tensor2d() requires values to be number[][] or flat/TypedArray");
24
+ if (e.length === 1 && r == null)
25
+ throw new Error("tensor2d() requires shape to be provided when `values` are a flat/TypedArray");
26
+ return a(n, r, e, o);
27
+ }
28
+ export {
29
+ i as t
30
+ };
@@ -1,14 +1,25 @@
1
- export default class BPE {
1
+ import { default as EE } from 'eventemitter3';
2
+ import { ITokeniser } from './type';
3
+ export default class BPETokeniser extends EE<'trainStatus'> implements ITokeniser {
4
+ private targetSize;
2
5
  private vocab;
3
6
  private vocabIndex;
4
7
  private merges;
5
8
  private pretokenMap;
6
- constructor(vocab?: string[], merges?: [string, string][]);
7
- train(text: string[], vocabSize: number, onUpdate?: (progress: number, vocabSize: number) => void): void;
9
+ constructor(vocabSize: number);
10
+ constructor(vocab: string[], merges?: [string, string][]);
11
+ destroy(): void;
12
+ get trained(): boolean;
13
+ get vocabSize(): number;
14
+ get eosToken(): number;
15
+ train(text: string[]): Promise<number>;
8
16
  getVocab(): string[];
9
- getMerges(): [string, string][];
17
+ getMerges(): Promise<[string, string][]>;
10
18
  private tokeniseWord;
11
19
  private tokeniseStrings;
12
- tokenise(text: string[], numeric: true): number[][];
13
- tokenise(text: string[]): string[][];
20
+ tokenise(text: string[], numeric: true): Promise<number[][]>;
21
+ tokenise(text: string[]): Promise<string[][]>;
22
+ detokenise(tokens: number[][]): Promise<string[]>;
23
+ encode(text: string): Promise<number[]>;
24
+ decode(tokens: number[]): Promise<string>;
14
25
  }
@@ -1,100 +1,128 @@
1
- import f from "../utilities/tokenParse.js";
2
- function b(r) {
3
- const s = /* @__PURE__ */ new Map();
4
- for (let e = 0; e < r.length; e++) {
5
- const t = r[e];
1
+ import p from "../utilities/tokenParse.js";
2
+ import { E as g } from "../index-Dwqa6Zy2.js";
3
+ function u(o, e) {
4
+ return `${o}-::-${e}`;
5
+ }
6
+ function k(o) {
7
+ const e = /* @__PURE__ */ new Map();
8
+ for (let s = 0; s < o.length; s++) {
9
+ const t = o[s];
6
10
  for (let n = 0; n < t.length - 1; n++) {
7
- const o = `${t[n]}${t[n + 1]}`, i = s.get(o) || {
11
+ const r = u(t[n], t[n + 1]), a = e.get(r) || {
8
12
  a: t[n],
9
13
  b: t[n + 1],
10
14
  count: 0,
11
15
  instances: /* @__PURE__ */ new Set()
12
16
  };
13
- i.count += 1, i.instances.add(e), s.set(o, i);
17
+ a.count += 1, a.instances.add(s), e.set(r, a);
14
18
  }
15
19
  }
16
- return { pairs: s, tokens: r };
20
+ return { pairs: e, tokens: o };
17
21
  }
18
- function h(r, s, e, t, n) {
19
- const o = `${s}${e}`;
20
- if (r.pairs.has(o)) {
21
- const i = r.pairs.get(o);
22
- i.count += n, i.instances.add(t);
22
+ function h(o, e, s, t, n) {
23
+ const r = u(e, s);
24
+ if (o.pairs.has(r)) {
25
+ const a = o.pairs.get(r);
26
+ a.count += n, n > 0 ? a.instances.add(t) : a.count <= 0 ? o.pairs.delete(r) : a.instances.delete(t);
23
27
  } else
24
- r.pairs.set(o, { a: s, b: e, count: n, instances: /* @__PURE__ */ new Set([t]) });
28
+ o.pairs.set(r, { a: e, b: s, count: n, instances: /* @__PURE__ */ new Set([t]) });
25
29
  }
26
- function g(r) {
27
- let s = null, e = 0;
28
- for (const t of r.pairs.values())
29
- t.count > e && (e = t.count, s = t);
30
- return s;
30
+ function b(o) {
31
+ let e = null, s = 0;
32
+ for (const t of o.pairs.values())
33
+ t.count > s && (s = t.count, e = t);
34
+ return e;
31
35
  }
32
- function m(r, s) {
33
- return r.map((e) => {
36
+ function d(o, e) {
37
+ return o.map((s) => {
34
38
  const t = [];
35
- for (let n = 0; n < e.length; n++)
36
- n < e.length - 1 && e[n] === s[0] && e[n + 1] === s[1] ? (t.push(s[0] + s[1]), n++) : t.push(e[n]);
39
+ for (let n = 0; n < s.length; n++)
40
+ n < s.length - 1 && s[n] === e[0] && s[n + 1] === e[1] ? (t.push(e[0] + e[1]), n++) : t.push(s[n]);
37
41
  return t;
38
42
  });
39
43
  }
40
- function d(r, s) {
41
- s.instances.forEach((e) => {
42
- const t = r.tokens[e], n = [];
43
- for (let o = 0; o < t.length; o++)
44
- if (o < t.length - 1 && t[o] === s.a && t[o + 1] === s.b) {
45
- const i = s.a + s.b;
46
- n.push(i), o > 0 && (h(r, t[o - 1], s.a, e, -1), h(r, t[o - 1], i, e, 1)), o++, o < t.length - 1 && (h(r, s.b, t[o + 1], e, -1), h(r, i, t[o + 1], e, 1));
44
+ function m(o, e) {
45
+ e.instances.forEach((s) => {
46
+ const t = o.tokens[s], n = [];
47
+ for (let r = 0; r < t.length; r++)
48
+ if (r < t.length - 1 && t[r] === e.a && t[r + 1] === e.b) {
49
+ const a = e.a + e.b;
50
+ n.push(a), r > 0 && (h(o, t[r - 1], e.a, s, -1), h(o, t[r - 1], a, s, 1)), r++, r < t.length - 1 && (h(o, e.b, t[r + 1], s, -1), h(o, a, t[r + 1], s, 1));
47
51
  } else
48
- n.push(t[o]);
49
- r.tokens[e] = n;
50
- }), r.pairs.delete(`${s.a}${s.b}`);
52
+ n.push(t[r]);
53
+ o.tokens[s] = n;
54
+ }), o.pairs.delete(u(e.a, e.b));
51
55
  }
52
- class w {
56
+ class w extends g {
57
+ targetSize;
53
58
  vocab = /* @__PURE__ */ new Set();
54
59
  vocabIndex = /* @__PURE__ */ new Map();
55
60
  merges = [];
56
61
  pretokenMap = /* @__PURE__ */ new Map();
57
- constructor(s, e) {
58
- s && s.forEach((t, n) => {
62
+ constructor(e, s) {
63
+ super(), Array.isArray(e) ? (e.forEach((t, n) => {
59
64
  this.vocab.add(t), this.vocabIndex.set(t, n);
60
- }), e && (this.merges = e);
65
+ }), s && (this.merges = s), this.targetSize = e.length) : (this.vocab.add("<eos>"), this.vocab.add("<unk>"), this.targetSize = e);
66
+ }
67
+ destroy() {
68
+ this.vocab.clear(), this.vocabIndex.clear(), this.merges = [], this.pretokenMap.clear();
69
+ }
70
+ get trained() {
71
+ return this.vocab.size === this.targetSize && this.merges.length > 0;
72
+ }
73
+ get vocabSize() {
74
+ return this.vocab.size;
61
75
  }
62
- train(s, e, t) {
63
- const n = s.map((a) => f(a, !0)).flat(1), o = new Set(n);
76
+ get eosToken() {
77
+ return this.vocabIndex.get("<eos>") ?? 0;
78
+ }
79
+ async train(e) {
80
+ const s = e.map((i) => p(i)).flat(1), t = new Set(s);
64
81
  this.vocab = /* @__PURE__ */ new Set(), this.pretokenMap.clear(), this.merges = [], this.vocab.add("<eos>");
65
- const i = Array.from(o), u = i.map((a) => a.split("").map((c) => (this.vocab.add(c), c))), p = b(u);
66
- for (; this.vocab.size < e && this.merges.length < e; ) {
67
- const a = g(p);
68
- if (!a)
82
+ const n = Array.from(t), r = n.map((i) => Array.from(i).map((c) => (this.vocab.add(c), c))), a = k(r);
83
+ for (; this.vocab.size < this.targetSize && this.merges.length < this.targetSize; ) {
84
+ const i = b(a);
85
+ if (!i)
69
86
  break;
70
- this.merges.push([a.a, a.b]), this.vocab.add(a.a + a.b), d(p, a), t && this.vocab.size % 100 === 0 && t(this.vocab.size / e, this.vocab.size);
87
+ this.merges.push([i.a, i.b]), this.vocab.add(i.a + i.b), m(a, i);
71
88
  }
72
- i.forEach((a, l) => {
73
- const c = u[l];
74
- this.pretokenMap.set(a, c);
89
+ n.forEach((i, l) => {
90
+ const c = r[l];
91
+ this.pretokenMap.set(i, c);
75
92
  }), this.vocabIndex.clear();
76
- let k = 0;
77
- for (const a of this.vocab.keys())
78
- this.vocabIndex.set(a, k++);
93
+ let f = 0;
94
+ for (const i of this.vocab.keys())
95
+ this.vocabIndex.set(i, f++);
96
+ return this.vocab.size;
79
97
  }
80
98
  getVocab() {
81
99
  return Array.from(this.vocab);
82
100
  }
83
- getMerges() {
101
+ async getMerges() {
84
102
  return this.merges;
85
103
  }
86
- tokeniseWord(s) {
87
- let e = s.split("");
104
+ tokeniseWord(e) {
105
+ let s = Array.from(e);
88
106
  return this.merges.forEach((t) => {
89
- e = m([e], t)[0];
90
- }), this.pretokenMap.set(s, e), e;
107
+ s = d([s], t)[0];
108
+ }), this.pretokenMap.set(e, s), s;
109
+ }
110
+ tokeniseStrings(e) {
111
+ return e.map((s) => p(s).map((r) => this.pretokenMap.has(r) ? this.pretokenMap.get(r) : this.tokeniseWord(r)).flat(1));
112
+ }
113
+ async tokenise(e, s) {
114
+ const t = this.tokeniseStrings(e);
115
+ return s ? t.map((n) => n.map((r) => this.vocabIndex.get(r) ?? -1)) : t;
116
+ }
117
+ async detokenise(e) {
118
+ const s = this.getVocab();
119
+ return e.map((n) => n.map((r) => s[r]).join(""));
91
120
  }
92
- tokeniseStrings(s) {
93
- return s.map((e) => f(e, !0).map((o) => this.pretokenMap.has(o) ? this.pretokenMap.get(o) : this.tokeniseWord(o)).flat(1));
121
+ async encode(e) {
122
+ return (await this.tokenise([e], !0))[0];
94
123
  }
95
- tokenise(s, e) {
96
- const t = this.tokeniseStrings(s);
97
- return e ? t.map((n) => n.map((o) => this.vocabIndex.get(o) ?? -1)) : t;
124
+ async decode(e) {
125
+ return (await this.detokenise([e]))[0];
98
126
  }
99
127
  }
100
128
  export {
@@ -1,4 +1,4 @@
1
- import { A as r, a as c, s as h, b as g, e as o } from "../index-YPKosni4.js";
1
+ import { A as r, b as c, f as h, s as g, e as o } from "../index-pWA4_lUh.js";
2
2
  class u extends r {
3
3
  constructor(t, e, s, a, i) {
4
4
  super(t, e, s, a), this.config = i, this.startLearningRate = t;
@@ -1,12 +1,12 @@
1
+ import { Tensor } from '@tensorflow/tfjs-core';
1
2
  import { ITokeniser } from '../tokeniser/type';
2
- import { default as TF } from '@tensorflow/tfjs';
3
+ import { Dataset } from '@tensorflow/tfjs-data';
3
4
  export declare class DatasetBuilder {
4
5
  tokenizer: ITokeniser;
5
6
  blockSize: number;
6
- private tf;
7
- constructor(tf: typeof TF, tokenizer: ITokeniser, blockSize?: number);
8
- createTextDataset(textData: string[], batchSize?: number, start?: number, end?: number): Promise<TF.data.Dataset<{
9
- xs: TF.Tensor;
10
- ys: TF.Tensor;
7
+ constructor(tokenizer: ITokeniser, blockSize?: number);
8
+ createTextDataset(textData: string[], batchSize?: number, start?: number, end?: number): Promise<Dataset<{
9
+ xs: Tensor;
10
+ ys: Tensor;
11
11
  }>>;
12
12
  }