@genai-fi/nanogpt 0.6.0 → 0.6.2
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.
- package/dist/Generator.js +7 -7
- package/dist/NanoGPTModel.js +70 -121
- package/dist/RealDiv-BYViZwhN.js +540 -0
- package/dist/Reshape-t7Kcikjk.js +127 -0
- package/dist/TeachableLLM.d.ts +2 -0
- package/dist/TeachableLLM.js +34 -27
- package/dist/{TiedEmbedding-BhxWO8QR.js → TiedEmbedding-9WeDwvjO.js} +12 -13
- package/dist/{axis_util-D17qZRQm.js → axis_util-Bu4h7XWV.js} +14 -12
- package/dist/{broadcast_to-BMQLjvt_.js → broadcast_to-DARN-DBD.js} +2 -2
- package/dist/{concat-DhZfF1GY.js → concat-5aPGqw3Z.js} +3 -3
- package/dist/{dataset-oilnemHf.js → dataset-pgqp-YfL.js} +3 -3
- package/dist/{dropout-CrMQPCeG.js → dropout-Bciw46HT.js} +7 -7
- package/dist/{gather-DZCMHZuN.js → gather-DjyCjmOD.js} +1 -1
- package/dist/gpgpu_math-CNslybmD.js +3115 -0
- package/dist/{index-bMBtI-WR.js → index-BAzbokzv.js} +846 -649
- package/dist/{kernel_funcs_utils-CNmjLWnB.js → kernel_funcs_utils-CUxJCg0g.js} +232 -138
- package/dist/layers/BaseLayer.js +2 -2
- package/dist/layers/CausalSelfAttention.js +6 -6
- package/dist/layers/MLP.js +5 -5
- package/dist/layers/RMSNorm.js +3 -3
- package/dist/layers/RoPECache.js +13 -33
- package/dist/layers/TiedEmbedding.js +6 -7
- package/dist/layers/TransformerBlock.js +1 -1
- package/dist/loader/load.d.ts +13 -0
- package/dist/loader/load.js +27 -0
- package/dist/loader/loadHF.d.ts +7 -0
- package/dist/loader/loadHF.js +22 -0
- package/dist/{utilities/load.d.ts → loader/loadTransformers.d.ts} +11 -11
- package/dist/loader/loadTransformers.js +28 -0
- package/dist/loader/newZipLoad.d.ts +8 -0
- package/dist/loader/newZipLoad.js +21 -0
- package/dist/loader/oldZipLoad.d.ts +7 -0
- package/dist/loader/oldZipLoad.js +76 -0
- package/dist/{log_sum_exp-BHdkCb4s.js → log_sum_exp-YEo2h3gb.js} +14 -14
- package/dist/main.js +23 -20
- package/dist/{mat_mul-BsrLfy81.js → mat_mul-7121rsJk.js} +1 -1
- package/dist/{max-DechV4Bc.js → max-DtlIuVeW.js} +1 -1
- package/dist/mulmat_packed_gpu-D4nKF7Je.js +71 -0
- package/dist/{norm-B9hWHZH1.js → norm-CzltS9Fz.js} +16 -16
- package/dist/{ones-g0K8jVwm.js → ones-BBlSRqn1.js} +2 -2
- package/dist/ops/appendCache.js +3 -3
- package/dist/ops/attentionMask.js +1 -1
- package/dist/ops/cpu/appendCache.js +2 -2
- package/dist/ops/cpu/attentionMask.js +6 -6
- package/dist/ops/cpu/fusedSoftmax.js +2 -2
- package/dist/ops/cpu/gatherSub.js +9 -9
- package/dist/ops/cpu/gelu.js +1 -1
- package/dist/ops/cpu/matMulGelu.js +1 -1
- package/dist/ops/cpu/matMulMul.js +1 -1
- package/dist/ops/cpu/mulDropout.js +1 -1
- package/dist/ops/cpu/normRMS.js +1 -1
- package/dist/ops/cpu/qkv.js +3 -3
- package/dist/ops/cpu/rope.js +5 -5
- package/dist/ops/cpu/scatterSub.js +17 -48
- package/dist/ops/fusedSoftmax.js +1 -1
- package/dist/ops/gatherSub.js +1 -1
- package/dist/ops/gelu.js +1 -1
- package/dist/ops/grads/attentionMask.js +1 -1
- package/dist/ops/grads/fusedSoftmax.js +4 -4
- package/dist/ops/grads/gelu.js +1 -1
- package/dist/ops/grads/matMulGelu.js +1 -1
- package/dist/ops/grads/normRMS.js +1 -1
- package/dist/ops/grads/qkv.js +1 -1
- package/dist/ops/grads/rope.js +1 -1
- package/dist/ops/matMulGelu.js +1 -1
- package/dist/ops/matMulMul.js +1 -1
- package/dist/ops/mulDrop.js +1 -1
- package/dist/ops/node/sparseCrossEntropy.js +1 -1
- package/dist/ops/normRMS.js +1 -1
- package/dist/ops/qkv.js +1 -1
- package/dist/ops/rope.js +8 -4
- package/dist/ops/scatterSub.js +1 -1
- package/dist/ops/webgl/appendCache.js +1 -1
- package/dist/ops/webgl/attentionMask.js +1 -1
- package/dist/ops/webgl/fusedSoftmax.js +29 -560
- package/dist/ops/webgl/gatherSub.js +1 -1
- package/dist/ops/webgl/gelu.js +2 -2
- package/dist/ops/webgl/log.js +3 -3
- package/dist/ops/webgl/matMulGelu.js +46 -113
- package/dist/ops/webgl/matMulMul.js +1 -1
- package/dist/ops/webgl/mulDropout.js +1 -1
- package/dist/ops/webgl/normRMS.js +2 -2
- package/dist/ops/webgl/qkv.js +1 -1
- package/dist/ops/webgl/rope.js +1 -1
- package/dist/ops/webgl/scatterSub.js +1 -1
- package/dist/{ops-Mv7Ta72x.js → ops-C0sQEcPw.js} +117 -109
- package/dist/{random_width-BBAWzDym.js → random_width-DWzaOgrn.js} +6925 -6291
- package/dist/{range-DMaG9A3G.js → range-DYsrnfiy.js} +1 -1
- package/dist/{gpgpu_math-Ctc31slO.js → reciprocal-CJQeasVa.js} +7 -5
- package/dist/register_all_kernels-BfFCQAqs.js +21397 -0
- package/dist/{reshape-T4yDEqoF.js → reshape-krWGKraP.js} +1 -1
- package/dist/scatter_nd_util-93ln7Hut.js +46 -0
- package/dist/selu_util-sntGesxr.js +740 -0
- package/dist/{shared-XNAoXhOa.js → shared-Ca6iDobD.js} +1462 -1089
- package/dist/{sin-EEhbrRO_.js → sin-D_h-qCSx.js} +1 -1
- package/dist/{softmax-B2_IKPDR.js → softmax-fsdtf6JC.js} +1 -1
- package/dist/{split-dcks18H1.js → split-eiktj-6L.js} +1 -1
- package/dist/{stack-lpJ5kYvE.js → stack-dfEEz2OY.js} +2 -2
- package/dist/{sum-CutF5lj2.js → sum-BE_Irnim.js} +1 -1
- package/dist/{tensor-C15NA2LA.js → tensor-Xyi595sG.js} +1 -1
- package/dist/{tensor2d-DZ_e5eKM.js → tensor2d-CPEkynbH.js} +1 -1
- package/dist/training/AdamExt.js +1 -1
- package/dist/training/DatasetBuilder.js +2 -2
- package/dist/training/FullTrainer.js +1 -1
- package/dist/training/Trainer.js +3 -3
- package/dist/training/sparseCrossEntropy.js +5 -5
- package/dist/utilities/dummy.d.ts +6 -0
- package/dist/utilities/dummy.js +31 -10
- package/dist/utilities/generate.js +3 -3
- package/dist/utilities/profile.d.ts +5 -0
- package/dist/utilities/profile.js +10 -7
- package/dist/utilities/safetensors.js +2 -2
- package/dist/utilities/save.js +1 -1
- package/dist/utilities/weights.js +2 -2
- package/dist/{variable-CdRKKp8x.js → variable-wSS22xj5.js} +1 -1
- package/dist/{zeros-CAbHfODe.js → zeros-YJDE7oRb.js} +4 -4
- package/package.json +2 -8
- package/dist/Reshape-CLOrdpve.js +0 -212
- package/dist/slice_util-Ddk0uxGJ.js +0 -49
- package/dist/tfjs_backend-BDb8r9qx.js +0 -1010
- package/dist/utilities/load.js +0 -99
|
@@ -0,0 +1,540 @@
|
|
|
1
|
+
import { ao as T, ac as E, p as O, g as V, aw as B, N as F, M as j, ax as K } from "./index-BAzbokzv.js";
|
|
2
|
+
import { r as $ } from "./Reshape-t7Kcikjk.js";
|
|
3
|
+
import { g as A, a as k, b as C, c as N, e as R } from "./axis_util-Bu4h7XWV.js";
|
|
4
|
+
import { t as U, m as W } from "./shared-Ca6iDobD.js";
|
|
5
|
+
import { j as _, f as y } from "./gpgpu_math-CNslybmD.js";
|
|
6
|
+
import { g as G, b as L } from "./kernel_funcs_utils-CUxJCg0g.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 { g as c, aa as C, i as f, D as R } from "./index-BAzbokzv.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-CNslybmD.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
|
+
};
|
package/dist/TeachableLLM.d.ts
CHANGED
|
@@ -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;
|