@genai-fi/nanogpt 0.5.6 → 0.6.1

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 (116) hide show
  1. package/dist/Generator.js +10 -9
  2. package/dist/NanoGPTModel.js +70 -121
  3. package/dist/RealDiv-7xu-pkZN.js +540 -0
  4. package/dist/Reshape-BYC1oUku.js +127 -0
  5. package/dist/TeachableLLM.d.ts +2 -0
  6. package/dist/TeachableLLM.js +42 -34
  7. package/dist/{TiedEmbedding-8S8xn8e6.js → TiedEmbedding-C1HBot-5.js} +12 -13
  8. package/dist/{axis_util-BczFISHz.js → axis_util-CCNL7jea.js} +14 -12
  9. package/dist/{broadcast_to-B7NGsBSh.js → broadcast_to-CddAF879.js} +2 -2
  10. package/dist/{concat-DdKPyAtw.js → concat-XOK9ANZu.js} +7 -7
  11. package/dist/{dataset-iqT4Otvb.js → dataset-BFFipD1c.js} +5 -5
  12. package/dist/{dropout-B09InSJS.js → dropout-xlKRoJyU.js} +9 -9
  13. package/dist/{gather-D6MsdXqc.js → gather-DKtUaTtA.js} +1 -1
  14. package/dist/gpgpu_math-B_ycgZ4W.js +3115 -0
  15. package/dist/{index-Du-bmOP8.js → index-CamYe_M8.js} +844 -647
  16. package/dist/{kernel_funcs_utils-DShm7-0k.js → kernel_funcs_utils-D5MS0JFg.js} +232 -136
  17. package/dist/layers/BaseLayer.js +2 -2
  18. package/dist/layers/CausalSelfAttention.js +6 -6
  19. package/dist/layers/MLP.js +5 -5
  20. package/dist/layers/RMSNorm.js +3 -3
  21. package/dist/layers/RoPECache.js +13 -33
  22. package/dist/layers/TiedEmbedding.js +6 -7
  23. package/dist/layers/TransformerBlock.js +1 -1
  24. package/dist/{log_sum_exp-CxfBtUaG.js → log_sum_exp-CV_5-TTu.js} +15 -15
  25. package/dist/main.js +24 -20
  26. package/dist/{mat_mul-CbiqIe2d.js → mat_mul-CAbRFWUj.js} +4 -4
  27. package/dist/{max-0Xnlpv8k.js → max-JBBv7aUf.js} +3 -3
  28. package/dist/mulmat_packed_gpu-DW4doKL_.js +71 -0
  29. package/dist/{norm-01kY9I2B.js → norm-B9dQTFYn.js} +12 -12
  30. package/dist/{ones-CrutWGas.js → ones-CMHNqMr6.js} +2 -2
  31. package/dist/ops/appendCache.js +3 -3
  32. package/dist/ops/attentionMask.js +1 -1
  33. package/dist/ops/cpu/appendCache.js +2 -2
  34. package/dist/ops/cpu/attentionMask.js +5 -5
  35. package/dist/ops/cpu/fusedSoftmax.js +2 -2
  36. package/dist/ops/cpu/gatherSub.js +5 -5
  37. package/dist/ops/cpu/gelu.js +1 -1
  38. package/dist/ops/cpu/matMulGelu.js +1 -1
  39. package/dist/ops/cpu/matMulMul.js +1 -1
  40. package/dist/ops/cpu/mulDropout.js +1 -1
  41. package/dist/ops/cpu/normRMS.js +1 -1
  42. package/dist/ops/cpu/qkv.js +3 -3
  43. package/dist/ops/cpu/rope.js +5 -5
  44. package/dist/ops/cpu/scatterSub.js +18 -49
  45. package/dist/ops/fusedSoftmax.js +1 -1
  46. package/dist/ops/gatherSub.js +1 -1
  47. package/dist/ops/gelu.js +1 -1
  48. package/dist/ops/grads/attentionMask.js +15 -11
  49. package/dist/ops/grads/fusedSoftmax.js +12 -10
  50. package/dist/ops/grads/gelu.js +1 -1
  51. package/dist/ops/grads/matMulGelu.js +1 -1
  52. package/dist/ops/grads/normRMS.js +1 -1
  53. package/dist/ops/grads/qkv.js +1 -1
  54. package/dist/ops/grads/rope.js +1 -1
  55. package/dist/ops/log.d.ts +0 -0
  56. package/dist/ops/log.js +1 -0
  57. package/dist/ops/matMulGelu.js +1 -1
  58. package/dist/ops/matMulMul.js +1 -1
  59. package/dist/ops/mulDrop.js +1 -1
  60. package/dist/ops/node/sparseCrossEntropy.js +1 -1
  61. package/dist/ops/normRMS.js +1 -1
  62. package/dist/ops/qkv.js +1 -1
  63. package/dist/ops/rope.js +8 -4
  64. package/dist/ops/scatterSub.js +1 -1
  65. package/dist/ops/webgl/appendCache.js +1 -1
  66. package/dist/ops/webgl/attentionMask.js +1 -1
  67. package/dist/ops/webgl/fusedSoftmax.js +31 -3379
  68. package/dist/ops/webgl/gatherSub.js +1 -1
  69. package/dist/ops/webgl/gelu.js +2 -2
  70. package/dist/{gpgpu_math-BFbOyvk4.js → ops/webgl/log.d.ts} +2 -8
  71. package/dist/ops/webgl/log.js +39 -0
  72. package/dist/ops/webgl/matMulGelu.js +48 -115
  73. package/dist/ops/webgl/matMulMul.js +1 -1
  74. package/dist/ops/webgl/mulDropout.js +1 -1
  75. package/dist/ops/webgl/normRMS.js +2 -2
  76. package/dist/ops/webgl/qkv.js +1 -1
  77. package/dist/ops/webgl/rope.js +1 -1
  78. package/dist/ops/webgl/scatterSub.js +1 -1
  79. package/dist/{ops-CJNniCAV.js → ops-DqtYemmV.js} +143 -135
  80. package/dist/{random_width-C-v-35bY.js → random_width-CLMQG5Jn.js} +6925 -6291
  81. package/dist/{range-Bvs1hidm.js → range-DqYjKnuG.js} +1 -1
  82. package/dist/reciprocal-z49filta.js +25 -0
  83. package/dist/register_all_kernels-COt6wLD0.js +21397 -0
  84. package/dist/{reshape-BH7eBpwq.js → reshape-C45vIIRU.js} +1 -1
  85. package/dist/scatter_nd_util-qgtnviTE.js +46 -0
  86. package/dist/selu_util-4QV_GXTB.js +740 -0
  87. package/dist/shared-ByfrGA97.js +3199 -0
  88. package/dist/{sin-CPAZXNjH.js → sin-9JBrfVaB.js} +1 -1
  89. package/dist/{softmax-DhWoBa7r.js → softmax-DvMvui-_.js} +1 -1
  90. package/dist/{split-BCUhuU7B.js → split-DxrHrPFK.js} +4 -4
  91. package/dist/{stack-BV1v7l3S.js → stack-DgaoDmnF.js} +1 -1
  92. package/dist/{sum-Cvq06317.js → sum-BpcpxNEh.js} +3 -3
  93. package/dist/{tensor-DgTOPY6h.js → tensor-CDz5x1mP.js} +1 -1
  94. package/dist/{tensor2d-CRWjDyUe.js → tensor2d-jO8JY5Jd.js} +1 -1
  95. package/dist/training/AdamExt.js +1 -1
  96. package/dist/training/DatasetBuilder.js +2 -2
  97. package/dist/training/FullTrainer.js +1 -1
  98. package/dist/training/Trainer.js +3 -3
  99. package/dist/training/sparseCrossEntropy.js +4 -4
  100. package/dist/utilities/dummy.d.ts +6 -0
  101. package/dist/utilities/dummy.js +31 -10
  102. package/dist/utilities/generate.js +3 -3
  103. package/dist/utilities/load.d.ts +25 -0
  104. package/dist/utilities/load.js +89 -37
  105. package/dist/utilities/profile.d.ts +5 -0
  106. package/dist/utilities/profile.js +12 -9
  107. package/dist/utilities/safetensors.d.ts +3 -0
  108. package/dist/utilities/safetensors.js +83 -0
  109. package/dist/utilities/save.js +47 -29
  110. package/dist/utilities/weights.js +2 -2
  111. package/dist/{variable-DZ3fF0R2.js → variable-CLVXjN7F.js} +1 -1
  112. package/dist/{zeros-BaHhQTWf.js → zeros-DUkkVccu.js} +8 -8
  113. package/package.json +3 -9
  114. package/dist/Reshape-Biok_3X1.js +0 -212
  115. package/dist/slice_util-DskXqRZa.js +0 -49
  116. package/dist/tfjs_backend-D9Ytje0G.js +0 -1010
@@ -0,0 +1,540 @@
1
+ import { ao as T, ac as E, p as O, i as V, aw as B, O as F, M as j, ax as K } from "./index-CamYe_M8.js";
2
+ import { r as $ } from "./Reshape-BYC1oUku.js";
3
+ import { g as A, a as k, b as C, c as N, e as R } from "./axis_util-CCNL7jea.js";
4
+ import { t as U, m as W } from "./shared-ByfrGA97.js";
5
+ import { j as _, f as y } from "./gpgpu_math-B_ycgZ4W.js";
6
+ import { g as G, b as L } from "./kernel_funcs_utils-D5MS0JFg.js";
7
+ /**
8
+ * @license
9
+ * Copyright 2020 Google LLC. All Rights Reserved.
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ * =============================================================================
22
+ */
23
+ class w {
24
+ constructor(s, e) {
25
+ this.variableNames = ["x"];
26
+ const { windowSize: t, batchSize: n, inSize: l, outSize: r } = s;
27
+ this.outputShape = [n, r];
28
+ const i = Math.floor(t / 4) * 4, c = t % 4;
29
+ let o = "sumValue += dot(values, ones);";
30
+ if (e != null) {
31
+ const p = 1 / e;
32
+ o = `sumValue += dot(values * ${T(p) ? p.toPrecision(2) : p}, ones);`;
33
+ }
34
+ let u = "";
35
+ l % t > 0 && (u = `
36
+ if (inIdx < 0 || inIdx >= ${l}) {
37
+ return 0.0;
38
+ }
39
+ `), this.userCode = `
40
+ const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
41
+
42
+ float getValue(int batch, int inIdx) {
43
+ ${u}
44
+ return getX(batch, inIdx);
45
+ }
46
+
47
+ void main() {
48
+ ivec2 coords = getOutputCoords();
49
+ int batch = coords[0];
50
+ int outIdx = coords[1];
51
+ int inOffset = outIdx * ${t};
52
+
53
+ float sumValue = 0.0;
54
+
55
+ for (int i = 0; i < ${i}; i += 4) {
56
+ int inIdx = inOffset + i;
57
+ vec4 values = vec4(
58
+ getValue(batch, inIdx),
59
+ getValue(batch, inIdx + 1),
60
+ getValue(batch, inIdx + 2),
61
+ getValue(batch, inIdx + 3)
62
+ );
63
+
64
+ ${o}
65
+ }
66
+
67
+ int inIdx = inOffset + ${i};
68
+ if (${c === 1}) {
69
+ vec4 values = vec4(getValue(batch, inIdx), 0.0, 0.0, 0.0);
70
+
71
+ ${o}
72
+ } else if (${c === 2}) {
73
+ vec4 values = vec4(
74
+ getValue(batch, inIdx),
75
+ getValue(batch, inIdx + 1), 0.0, 0.0);
76
+
77
+ ${o}
78
+ } else if (${c === 3}) {
79
+ vec4 values = vec4(
80
+ getValue(batch, inIdx),
81
+ getValue(batch, inIdx + 1),
82
+ getValue(batch, inIdx + 2), 0.0);
83
+
84
+ ${o}
85
+ }
86
+ setOutput(sumValue);
87
+ }
88
+ `;
89
+ }
90
+ }
91
+ /**
92
+ * @license
93
+ * Copyright 2017 Google LLC. All Rights Reserved.
94
+ * Licensed under the Apache License, Version 2.0 (the "License");
95
+ * you may not use this file except in compliance with the License.
96
+ * You may obtain a copy of the License at
97
+ *
98
+ * http://www.apache.org/licenses/LICENSE-2.0
99
+ *
100
+ * Unless required by applicable law or agreed to in writing, software
101
+ * distributed under the License is distributed on an "AS IS" BASIS,
102
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
103
+ * See the License for the specific language governing permissions and
104
+ * limitations under the License.
105
+ * =============================================================================
106
+ */
107
+ class X {
108
+ constructor(s, e) {
109
+ this.variableNames = ["x"];
110
+ const { windowSize: t, batchSize: n, inSize: l, outSize: r } = s;
111
+ this.outputShape = [n, r];
112
+ let i = "0.0", c = "";
113
+ e === "prod" ? i = "1.0" : e === "min" ? (i = "1.0 / 1e-20", c = "min") : e === "max" && (i = "-1.0 / 1e-20", c = "max");
114
+ let o = `${e}(${e}(${e}(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])`;
115
+ e === "sum" ? o = "sumValue" : e === "prod" ? o = "prodValue" : e === "all" ? o = "allValue" : e === "any" && (o = "anyValue");
116
+ const u = Math.floor(t / 4) * 4, p = t % 4;
117
+ let h = `
118
+ if (${e === "sum"}) {
119
+ sumValue += dot(values, ones);
120
+ } else if (${e === "prod"}) {
121
+ vec2 tmp = vec2(values[0], values[1]) * vec2(values[2], values[3]);
122
+ prodValue *= tmp[0] * tmp[1];
123
+ } else {
124
+ minMaxValue = ${c}(values, minMaxValue);
125
+ if (${e === "min"} || ${e === "max"}) {
126
+ minMaxValue = ${c}(values, minMaxValue);
127
+ bvec4 isNaN = isnan(values);
128
+ if (isNaN.r || isNaN.g || isNaN.b || isNaN.a) {
129
+ minMaxValue = vec4(NAN);
130
+ }
131
+ }
132
+ }
133
+ `, d = "vec4";
134
+ e === "all" ? (i = "1.0", h = `
135
+ bool reducedAllValue = all(values);
136
+ float floatedReducedAllValue = float(reducedAllValue);
137
+ allValue = float(allValue >= 1.0 && floatedReducedAllValue >= 1.0);
138
+ `, d = "bvec4") : e === "any" && (i = "0.0", h = `
139
+ bool reducedAnyValue = any(values);
140
+ float floatedReducedAnyValue = float(reducedAnyValue);
141
+ anyValue = float(anyValue >= 1.0 || floatedReducedAnyValue >= 1.0);
142
+ `, d = "bvec4");
143
+ let f = "";
144
+ l % t > 0 && (f = `
145
+ if (inIdx < 0 || inIdx >= ${l}) {
146
+ return initializationValue;
147
+ }
148
+ `), this.userCode = `
149
+ const float initializationValue = ${i};
150
+ const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
151
+
152
+ float getValue(int batch, int inIdx) {
153
+ ${f}
154
+ return getX(batch, inIdx);
155
+ }
156
+
157
+ void main() {
158
+ ivec2 coords = getOutputCoords();
159
+ int batch = coords[0];
160
+ int outIdx = coords[1];
161
+ int inOffset = outIdx * ${t};
162
+
163
+ vec4 minMaxValue = vec4(${i});
164
+ float prodValue = 1.0;
165
+ float sumValue = 0.0;
166
+ float allValue = 1.0;
167
+ float anyValue = 0.0;
168
+
169
+ for (int i = 0; i < ${u}; i += 4) {
170
+ int inIdx = inOffset + i;
171
+ ${d} values = ${d}(
172
+ getValue(batch, inIdx),
173
+ getValue(batch, inIdx + 1),
174
+ getValue(batch, inIdx + 2),
175
+ getValue(batch, inIdx + 3)
176
+ );
177
+
178
+ ${h}
179
+ }
180
+
181
+ int inIdx = inOffset + ${u};
182
+ if (${p === 1}) {
183
+ ${d} values = ${d}(
184
+ getValue(batch, inIdx),
185
+ initializationValue,
186
+ initializationValue,
187
+ initializationValue
188
+ );
189
+
190
+ ${h}
191
+ } else if (${p === 2}) {
192
+ ${d} values = ${d}(
193
+ getValue(batch, inIdx),
194
+ getValue(batch, inIdx + 1),
195
+ initializationValue,
196
+ initializationValue
197
+ );
198
+
199
+ ${h}
200
+ } else if (${p === 3}) {
201
+ ${d} values = ${d}(
202
+ getValue(batch, inIdx),
203
+ getValue(batch, inIdx + 1),
204
+ getValue(batch, inIdx + 2),
205
+ initializationValue
206
+ );
207
+
208
+ ${h}
209
+ }
210
+ setOutput(${o});
211
+ }
212
+ `;
213
+ }
214
+ }
215
+ /**
216
+ * @license
217
+ * Copyright 2020 Google LLC. All Rights Reserved.
218
+ * Licensed under the Apache License, Version 2.0 (the "License");
219
+ * you may not use this file except in compliance with the License.
220
+ * You may obtain a copy of the License at
221
+ *
222
+ * http://www.apache.org/licenses/LICENSE-2.0
223
+ *
224
+ * Unless required by applicable law or agreed to in writing, software
225
+ * distributed under the License is distributed on an "AS IS" BASIS,
226
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
227
+ * See the License for the specific language governing permissions and
228
+ * limitations under the License.
229
+ * =============================================================================
230
+ */
231
+ function q(a) {
232
+ const s = [];
233
+ for (; s.length === 0 || s[s.length - 1].outSize !== 1; ) {
234
+ const e = s.length ? s[s.length - 1].outSize : a[1], t = _(e);
235
+ s.push({
236
+ inSize: e,
237
+ windowSize: t,
238
+ outSize: Math.ceil(e / t)
239
+ });
240
+ }
241
+ return s;
242
+ }
243
+ function M(a, s, e, t) {
244
+ const n = q(a.shape);
245
+ let l = a;
246
+ for (let r = 0; r < n.length; r++) {
247
+ const { inSize: i, windowSize: c, outSize: o } = n[r];
248
+ let u, p;
249
+ e === "mean" ? u = r === 0 ? new w({ windowSize: c, inSize: i, batchSize: a.shape[0], outSize: o }, i) : new w({ windowSize: c, inSize: i, batchSize: a.shape[0], outSize: o }) : u = new X({ windowSize: c, inSize: i, batchSize: a.shape[0], outSize: o }, e), p = l, l = t.runWebGLProgram(u, [l], s), p.dataId !== a.dataId && t.disposeIntermediateTensorInfo(p);
250
+ }
251
+ return l;
252
+ }
253
+ /**
254
+ * @license
255
+ * Copyright 2017 Google LLC. All Rights Reserved.
256
+ * Licensed under the Apache License, Version 2.0 (the "License");
257
+ * you may not use this file except in compliance with the License.
258
+ * You may obtain a copy of the License at
259
+ *
260
+ * http://www.apache.org/licenses/LICENSE-2.0
261
+ *
262
+ * Unless required by applicable law or agreed to in writing, software
263
+ * distributed under the License is distributed on an "AS IS" BASIS,
264
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
265
+ * See the License for the specific language governing permissions and
266
+ * limitations under the License.
267
+ * =============================================================================
268
+ */
269
+ class Y {
270
+ constructor(s, e) {
271
+ this.variableNames = ["A"];
272
+ const t = new Array(s.length);
273
+ for (let r = 0; r < t.length; r++)
274
+ t[r] = s[e[r]];
275
+ this.outputShape = t, this.rank = t.length;
276
+ const n = y(this.rank), l = H(e);
277
+ this.userCode = `
278
+ void main() {
279
+ ${n} resRC = getOutputCoords();
280
+ setOutput(getA(${l}));
281
+ }
282
+ `;
283
+ }
284
+ }
285
+ function H(a) {
286
+ const s = a.length;
287
+ if (s > 6)
288
+ throw Error(`Transpose for rank ${s} is not yet supported`);
289
+ const e = ["resRC.x", "resRC.y", "resRC.z", "resRC.w", "resRC.u", "resRC.v"], t = new Array(s);
290
+ for (let n = 0; n < a.length; n++)
291
+ t[a[n]] = e[n];
292
+ return t.join();
293
+ }
294
+ /**
295
+ * @license
296
+ * Copyright 2019 Google LLC. All Rights Reserved.
297
+ * Licensed under the Apache License, Version 2.0 (the "License");
298
+ * you may not use this file except in compliance with the License.
299
+ * You may obtain a copy of the License at
300
+ *
301
+ * http://www.apache.org/licenses/LICENSE-2.0
302
+ *
303
+ * Unless required by applicable law or agreed to in writing, software
304
+ * distributed under the License is distributed on an "AS IS" BASIS,
305
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
306
+ * See the License for the specific language governing permissions and
307
+ * limitations under the License.
308
+ * =============================================================================
309
+ */
310
+ class J {
311
+ constructor(s, e) {
312
+ this.variableNames = ["A"], this.packedInputs = !0, this.packedOutput = !0;
313
+ const t = new Array(s.length);
314
+ for (let u = 0; u < t.length; u++)
315
+ t[u] = s[e[u]];
316
+ if (this.outputShape = t, this.rank = t.length, this.rank > 6)
317
+ throw Error(`Packed transpose for rank ${this.rank} is not yet supported.`);
318
+ const n = y(this.rank), l = G("rc", this.rank), r = new Array(this.rank);
319
+ for (let u = 0; u < e.length; u++)
320
+ r[e[u]] = l[u];
321
+ const i = `vec2(${r.slice(-2).join()})`, c = `++${l[this.rank - 1]} < ${t[this.rank - 1]}`, o = `getChannel(getA(${r.join()}), ${i})`;
322
+ this.userCode = `
323
+ void main() {
324
+ ${n} rc = getOutputCoords();
325
+ vec4 result = vec4(0.);
326
+ result[0] = ${o};
327
+ if(${c}) {
328
+ result[1] = ${o};
329
+ }
330
+ --${l[this.rank - 1]};
331
+ if(++${l[this.rank - 2]} < ${t[this.rank - 2]}) {
332
+ result[2] = ${o};
333
+ if(${c}) {
334
+ result[3] = ${o};
335
+ }
336
+ }
337
+ setOutput(result);
338
+ }
339
+ `;
340
+ }
341
+ }
342
+ /**
343
+ * @license
344
+ * Copyright 2020 Google LLC. All Rights Reserved.
345
+ * Licensed under the Apache License, Version 2.0 (the "License");
346
+ * you may not use this file except in compliance with the License.
347
+ * You may obtain a copy of the License at
348
+ *
349
+ * http://www.apache.org/licenses/LICENSE-2.0
350
+ *
351
+ * Unless required by applicable law or agreed to in writing, software
352
+ * distributed under the License is distributed on an "AS IS" BASIS,
353
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
354
+ * See the License for the specific language governing permissions and
355
+ * limitations under the License.
356
+ * =============================================================================
357
+ */
358
+ function P(a, s, e) {
359
+ const t = E().getBool("WEBGL_PACK_ARRAY_OPERATIONS") ? new J(a.shape, s) : new Y(a.shape, s);
360
+ return e.runWebGLProgram(t, [a], a.dtype);
361
+ }
362
+ /**
363
+ * @license
364
+ * Copyright 2020 Google LLC. All Rights Reserved.
365
+ * Licensed under the Apache License, Version 2.0 (the "License");
366
+ * you may not use this file except in compliance with the License.
367
+ * You may obtain a copy of the License at
368
+ *
369
+ * http://www.apache.org/licenses/LICENSE-2.0
370
+ *
371
+ * Unless required by applicable law or agreed to in writing, software
372
+ * distributed under the License is distributed on an "AS IS" BASIS,
373
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
374
+ * See the License for the specific language governing permissions and
375
+ * limitations under the License.
376
+ * =============================================================================
377
+ */
378
+ function Q(a, s, e, t) {
379
+ const n = s, l = a.shape.length, r = O(n, a.shape);
380
+ let i = r;
381
+ const c = A(i, l), o = c != null;
382
+ let u = a;
383
+ o && (u = P(a, c, t), i = k(i.length, l)), C("sum", i, l);
384
+ const [p, h] = N(u.shape, i);
385
+ let d = p;
386
+ e && (d = R(p, r));
387
+ const f = V(h), g = V(a.shape) / f, x = $({ inputs: { x: u }, attrs: { shape: [g, f] }, backend: t }), b = B(a.dtype), I = M(x, b, "sum", t), m = $({ inputs: { x: I }, attrs: { shape: d }, backend: t });
388
+ return t.disposeIntermediateTensorInfo(x), t.disposeIntermediateTensorInfo(I), o && t.disposeIntermediateTensorInfo(u), m;
389
+ }
390
+ /**
391
+ * @license
392
+ * Copyright 2020 Google LLC. All Rights Reserved.
393
+ * Licensed under the Apache License, Version 2.0 (the "License");
394
+ * you may not use this file except in compliance with the License.
395
+ * You may obtain a copy of the License at
396
+ *
397
+ * http://www.apache.org/licenses/LICENSE-2.0
398
+ *
399
+ * Unless required by applicable law or agreed to in writing, software
400
+ * distributed under the License is distributed on an "AS IS" BASIS,
401
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
402
+ * See the License for the specific language governing permissions and
403
+ * limitations under the License.
404
+ * =============================================================================
405
+ */
406
+ function Z(a) {
407
+ const { inputs: s, backend: e, attrs: t } = a, { x: n } = s, { axis: l, keepDims: r } = t;
408
+ return Q(n, l, r, e);
409
+ }
410
+ const de = {
411
+ kernelName: F,
412
+ backendName: "webgl",
413
+ kernelFunc: Z
414
+ };
415
+ /**
416
+ * @license
417
+ * Copyright 2020 Google LLC. All Rights Reserved.
418
+ * Licensed under the Apache License, Version 2.0 (the "License");
419
+ * you may not use this file except in compliance with the License.
420
+ * You may obtain a copy of the License at
421
+ *
422
+ * http://www.apache.org/licenses/LICENSE-2.0
423
+ *
424
+ * Unless required by applicable law or agreed to in writing, software
425
+ * distributed under the License is distributed on an "AS IS" BASIS,
426
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
427
+ * See the License for the specific language governing permissions and
428
+ * limitations under the License.
429
+ * =============================================================================
430
+ */
431
+ function ee(a, s, e, t) {
432
+ const n = V(s), r = V(a.shape) / n, i = $({ inputs: { x: a }, attrs: { shape: [r, n] }, backend: t }), c = M(i, a.dtype, "max", t), o = $({ inputs: { x: c }, attrs: { shape: e }, backend: t });
433
+ return t.disposeIntermediateTensorInfo(i), t.disposeIntermediateTensorInfo(c), o;
434
+ }
435
+ /**
436
+ * @license
437
+ * Copyright 2020 Google LLC. All Rights Reserved.
438
+ * Licensed under the Apache License, Version 2.0 (the "License");
439
+ * you may not use this file except in compliance with the License.
440
+ * You may obtain a copy of the License at
441
+ *
442
+ * http://www.apache.org/licenses/LICENSE-2.0
443
+ *
444
+ * Unless required by applicable law or agreed to in writing, software
445
+ * distributed under the License is distributed on an "AS IS" BASIS,
446
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
447
+ * See the License for the specific language governing permissions and
448
+ * limitations under the License.
449
+ * =============================================================================
450
+ */
451
+ function te(a) {
452
+ const { inputs: s, backend: e, attrs: t } = a, { x: n } = s, { reductionIndices: l, keepDims: r } = t, i = n.shape.length, c = O(l, n.shape);
453
+ let o = c;
454
+ const u = A(o, i), p = u != null, h = e.shouldExecuteOnCPU([n]);
455
+ let d = n;
456
+ if (p) {
457
+ if (h) {
458
+ const I = e.texData.get(d.dataId).values, m = new Array(i);
459
+ for (let v = 0; v < m.length; v++)
460
+ m[v] = n.shape[u[v]];
461
+ const z = U(I, n.shape, n.dtype, u, m);
462
+ d = e.makeTensorInfo(m, n.dtype);
463
+ const D = e.texData.get(d.dataId);
464
+ D.values = z;
465
+ } else
466
+ d = P(n, u, e);
467
+ o = k(o.length, i);
468
+ }
469
+ C("max", o, i);
470
+ const [f, S] = N(d.shape, o);
471
+ let g = f;
472
+ r && (g = R(f, c));
473
+ let x;
474
+ if (h) {
475
+ const I = e.texData.get(d.dataId).values, m = W(I, V(S), g, n.dtype);
476
+ x = e.makeTensorInfo(g, n.dtype);
477
+ const z = e.texData.get(x.dataId);
478
+ z.values = m;
479
+ } else
480
+ x = ee(d, S, g, e);
481
+ return p && e.disposeIntermediateTensorInfo(d), x;
482
+ }
483
+ const pe = {
484
+ kernelName: j,
485
+ backendName: "webgl",
486
+ kernelFunc: te
487
+ };
488
+ /**
489
+ * @license
490
+ * Copyright 2020 Google LLC. All Rights Reserved.
491
+ * Licensed under the Apache License, Version 2.0 (the "License");
492
+ * you may not use this file except in compliance with the License.
493
+ * You may obtain a copy of the License at
494
+ *
495
+ * http://www.apache.org/licenses/LICENSE-2.0
496
+ *
497
+ * Unless required by applicable law or agreed to in writing, software
498
+ * distributed under the License is distributed on an "AS IS" BASIS,
499
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
500
+ * See the License for the specific language governing permissions and
501
+ * limitations under the License.
502
+ * =============================================================================
503
+ */
504
+ const ae = `
505
+ if (a == b) {
506
+ return 1.0;
507
+ };
508
+ return a / b;`, se = `
509
+ // vec4 one = vec4(equal(a, b));
510
+ // return one + (vec4(1.0) - one) * a / b;
511
+ vec4 result = a / b;
512
+ if(a.x == b.x) {
513
+ result.x = 1.;
514
+ }
515
+ if(a.y == b.y) {
516
+ result.y = 1.;
517
+ }
518
+ if(a.z == b.z) {
519
+ result.z = 1.;
520
+ }
521
+ if(a.w == b.w) {
522
+ result.w = 1.;
523
+ }
524
+
525
+ return result;
526
+ `, ne = L({ opSnippet: ae, packedOpSnippet: se, checkOutOfBounds: !0 }), he = {
527
+ kernelName: K,
528
+ backendName: "webgl",
529
+ kernelFunc: ne
530
+ };
531
+ export {
532
+ M as a,
533
+ pe as b,
534
+ he as c,
535
+ de as d,
536
+ te as m,
537
+ ne as r,
538
+ Z as s,
539
+ P as t
540
+ };
@@ -0,0 +1,127 @@
1
+ import { i as c, aa as C, k as f, F as R } from "./index-CamYe_M8.js";
2
+ import { u as g, g as I, a as x, b as F, c as $, d as u, e as m, i as l } from "./gpgpu_math-B_ycgZ4W.js";
3
+ /**
4
+ * @license
5
+ * Copyright 2018 Google LLC. All Rights Reserved.
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ * =============================================================================
18
+ */
19
+ class S {
20
+ constructor(t, i) {
21
+ this.variableNames = ["A"], this.packedInputs = !0, this.packedOutput = !0, this.customUniforms = [{ name: "inputShape", type: "ivec3" }], this.outputShape = t, this.enableShapeUniforms = g(this.outputShape.length);
22
+ let a = "";
23
+ for (let e = 0; e < 4; e++) {
24
+ let o = "thisRC = rc;";
25
+ e % 2 === 1 && (o += "thisRC.z += 1;"), e > 1 && (o += "thisRC.y += 1;"), a += `
26
+ ${o}
27
+ ${e > 0 ? "if(thisRC.y < rows && thisRC.z < cols){" : ""}
28
+ int flatIndex = getFlatIndex(thisRC);
29
+
30
+ ivec3 inputRC = inputCoordsFromReshapedOutCoords(flatIndex);
31
+ vec2 inputRCInnerDims = vec2(float(inputRC.y),float(inputRC.z));
32
+
33
+ result[${e}] =
34
+ getChannel(getA(inputRC.x, inputRC.y, inputRC.z), inputRCInnerDims);
35
+ ${e > 0 ? "}" : ""}
36
+ `;
37
+ }
38
+ this.userCode = `
39
+ ${v(i, this.enableShapeUniforms)}
40
+ ${this.enableShapeUniforms ? I() : x(t)}
41
+
42
+ void main() {
43
+ ivec3 rc = getOutputCoords();
44
+
45
+ vec4 result = vec4(0.);
46
+
47
+ ivec3 thisRC;
48
+ int rows = ${this.enableShapeUniforms ? "outShape[1]" : t[1]};
49
+ int cols = ${this.enableShapeUniforms ? "outShape[2]" : t[2]};
50
+
51
+ ${a}
52
+
53
+ setOutput(result);
54
+ }
55
+ `;
56
+ }
57
+ }
58
+ function v(s, t) {
59
+ return `
60
+ ivec3 inputCoordsFromReshapedOutCoords(int index) {
61
+ ${t ? F(["r", "c", "d"], "inputShape") : $(["r", "c", "d"], s)}
62
+ return ivec3(r, c, d);
63
+ }
64
+ `;
65
+ }
66
+ /**
67
+ * @license
68
+ * Copyright 2020 Google LLC. All Rights Reserved.
69
+ * Licensed under the Apache License, Version 2.0 (the "License");
70
+ * you may not use this file except in compliance with the License.
71
+ * You may obtain a copy of the License at
72
+ *
73
+ * http://www.apache.org/licenses/LICENSE-2.0
74
+ *
75
+ * Unless required by applicable law or agreed to in writing, software
76
+ * distributed under the License is distributed on an "AS IS" BASIS,
77
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
78
+ * See the License for the specific language governing permissions and
79
+ * limitations under the License.
80
+ * =============================================================================
81
+ */
82
+ function b(s, t, i) {
83
+ const a = [
84
+ u(s.shape),
85
+ ...m(s.shape)
86
+ ], e = {
87
+ dtype: s.dtype,
88
+ shape: a,
89
+ dataId: s.dataId
90
+ }, o = [
91
+ u(t),
92
+ ...m(t)
93
+ ], r = new S(o, a), p = !0, n = [a], h = i.runWebGLProgram(r, [e], s.dtype, n, p);
94
+ return { dataId: h.dataId, shape: t, dtype: h.dtype };
95
+ }
96
+ /**
97
+ * @license
98
+ * Copyright 2020 Google LLC. All Rights Reserved.
99
+ * Licensed under the Apache License, Version 2.0 (the "License");
100
+ * you may not use this file except in compliance with the License.
101
+ * You may obtain a copy of the License at
102
+ *
103
+ * http://www.apache.org/licenses/LICENSE-2.0
104
+ *
105
+ * Unless required by applicable law or agreed to in writing, software
106
+ * distributed under the License is distributed on an "AS IS" BASIS,
107
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
108
+ * See the License for the specific language governing permissions and
109
+ * limitations under the License.
110
+ * =============================================================================
111
+ */
112
+ function y(s) {
113
+ const { inputs: t, backend: i, attrs: a } = s, { x: e } = t, { shape: o } = a, r = i, p = c(e.shape), n = C(o, p), h = c(n);
114
+ f(p === h, () => `The new shape (${n}) has ${h} elements and the old shape (${e.shape}) has ${p} elements. The new shape and old shape must have the same number of elements.`);
115
+ const d = r.texData.get(e.dataId);
116
+ return d.isPacked && !l(e.shape, n) && !(d.texture !== null && l(d.shape, n)) ? b(e, n, r) : (r.incRef(e.dataId), { dataId: e.dataId, shape: n, dtype: e.dtype });
117
+ }
118
+ const U = {
119
+ kernelName: R,
120
+ backendName: "webgl",
121
+ kernelFunc: y
122
+ };
123
+ export {
124
+ S as R,
125
+ U as a,
126
+ y as r
127
+ };
@@ -18,6 +18,7 @@ export default class TeachableLLM {
18
18
  private _model?;
19
19
  private _tokeniser?;
20
20
  private _status;
21
+ private _memoryRequirements?;
21
22
  meta: TeachableLLMMeta;
22
23
  constructor(tokeniser?: ITokeniser, model?: NanoGPT);
23
24
  get vocab(): string[];
@@ -27,6 +28,7 @@ export default class TeachableLLM {
27
28
  get tokeniser(): ITokeniser;
28
29
  get status(): TeachableLLMStatus;
29
30
  get ready(): boolean;
31
+ estimateTrainingMemoryUsage(batchSize: number): number;
30
32
  private setStatus;
31
33
  saveModel(options?: SaveOptions): Promise<Blob>;
32
34
  static loadModel(data: Blob | Buffer | string): TeachableLLM;