@genai-fi/nanogpt 0.2.12 → 0.3.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.
- package/dist/Generator.js +30 -25
- package/dist/NanoGPTModel.d.ts +13 -14
- package/dist/NanoGPTModel.js +142 -70
- package/dist/TeachableLLM.d.ts +16 -7
- package/dist/TeachableLLM.js +81 -44
- package/dist/Trainer.js +8 -8
- package/dist/concat-BIZS_td9.js +33 -0
- package/dist/data/parquet.js +1 -1
- package/dist/exports_layers-tbTBcwMM.js +25 -0
- package/dist/{sum-D7fu15XL.js → gather-BPGW8RsB.js} +6 -8
- package/dist/index-C4L8Cm77.js +349 -0
- package/dist/{index-YPKosni4.js → index-pWA4_lUh.js} +1020 -782
- package/dist/layers/CausalSelfAttention.d.ts +11 -11
- package/dist/layers/CausalSelfAttention.js +71 -63
- package/dist/layers/MLP.d.ts +6 -7
- package/dist/layers/MLP.js +18 -16
- package/dist/layers/RMSNorm.d.ts +6 -7
- package/dist/layers/RMSNorm.js +15 -13
- package/dist/layers/RoPECache.d.ts +4 -5
- package/dist/layers/RoPECache.js +36 -12
- package/dist/layers/TiedEmbedding.d.ts +7 -8
- package/dist/layers/TiedEmbedding.js +16 -418
- package/dist/layers/TransformerBlock.d.ts +8 -9
- package/dist/layers/TransformerBlock.js +12 -12
- package/dist/main.d.ts +2 -0
- package/dist/main.js +35 -21
- package/dist/{mat_mul-Bu7bhLms.js → mat_mul-D7_a4KJn.js} +5 -5
- package/dist/moments-DfcpfwKi.js +132 -0
- package/dist/ones-Cog-G2ag.js +29 -0
- package/dist/ops/appendCache.d.ts +2 -0
- package/dist/ops/appendCache.js +9 -0
- package/dist/ops/attentionMask.d.ts +1 -1
- package/dist/ops/attentionMask.js +7 -85
- package/dist/ops/cpu/appendCache.d.ts +2 -0
- package/dist/ops/cpu/appendCache.js +28 -0
- package/dist/ops/cpu/attentionMask.js +18 -0
- package/dist/ops/cpu/gatherSub.d.ts +1 -0
- package/dist/ops/cpu/gatherSub.js +34 -0
- package/dist/ops/cpu/qkv.d.ts +5 -0
- package/dist/ops/cpu/qkv.js +38 -0
- package/dist/ops/cpu/rope.d.ts +6 -0
- package/dist/ops/cpu/rope.js +38 -0
- package/dist/ops/cpu/scatterSub.d.ts +1 -0
- package/dist/ops/cpu/scatterSub.js +70 -0
- package/dist/ops/gatherSub.d.ts +1 -1
- package/dist/ops/gatherSub.js +6 -63
- package/dist/ops/grads/attentionMask.d.ts +1 -0
- package/dist/ops/grads/attentionMask.js +21 -0
- package/dist/ops/grads/qkv.d.ts +1 -0
- package/dist/ops/grads/qkv.js +20 -0
- package/dist/ops/grads/rope.d.ts +1 -0
- package/dist/ops/grads/rope.js +14 -0
- package/dist/ops/node/sparseCrossEntropy.js +1 -1
- package/dist/ops/qkv.d.ts +1 -6
- package/dist/ops/qkv.js +7 -124
- package/dist/ops/rope.d.ts +0 -5
- package/dist/ops/rope.js +7 -151
- package/dist/ops/scatterSub.d.ts +1 -1
- package/dist/ops/scatterSub.js +6 -147
- package/dist/ops/webgl/appendCache.d.ts +1 -0
- package/dist/ops/webgl/appendCache.js +43 -0
- package/dist/ops/webgl/attentionMask.d.ts +1 -0
- package/dist/ops/webgl/attentionMask.js +43 -0
- package/dist/ops/webgl/gatherSub.d.ts +1 -0
- package/dist/ops/webgl/gatherSub.js +27 -0
- package/dist/ops/webgl/qkv.d.ts +1 -0
- package/dist/ops/webgl/qkv.js +46 -0
- package/dist/ops/webgl/rope.d.ts +1 -0
- package/dist/ops/webgl/rope.js +56 -0
- package/dist/ops/webgl/scatterSub.d.ts +1 -0
- package/dist/ops/webgl/scatterSub.js +27 -0
- package/dist/{parquet-BRl5lE_I.js → parquet-C0Tlmv9c.js} +3045 -3048
- package/dist/random_width-oeUIlUZj.js +15487 -0
- package/dist/range-CcDl05lo.js +26 -0
- package/dist/{reshape-DmnmKT6r.js → reshape-C8CR_Bad.js} +3 -3
- package/dist/sin-BJIrfnj7.js +47 -0
- package/dist/softmax-Be_lsqUc.js +105 -0
- package/dist/{complex-CJ-qCcLB.js → split-DZbvruEP.js} +6 -8
- package/dist/stack-BMm-efee.js +27 -0
- package/dist/sum-C7Mgy9Bw.js +104 -0
- package/dist/tensor-DJVbYhh1.js +24 -0
- package/dist/tensor2d-ZuQSh2D-.js +30 -0
- package/dist/tokeniser/bpe.d.ts +17 -6
- package/dist/tokeniser/bpe.js +89 -61
- package/dist/training/AdamExt.js +1 -1
- package/dist/training/DatasetBuilder.d.ts +6 -6
- package/dist/training/DatasetBuilder.js +1262 -17
- package/dist/training/Evaluator.d.ts +3 -2
- package/dist/training/FullTrainer.d.ts +9 -8
- package/dist/training/FullTrainer.js +26 -25
- package/dist/training/LayerTrainer.d.ts +9 -8
- package/dist/training/LayerTrainer.js +34 -33
- package/dist/training/Trainer.d.ts +22 -21
- package/dist/training/Trainer.js +21 -18
- package/dist/training/sparseCrossEntropy.js +22 -166
- package/dist/utilities/dummy.js +10 -8
- package/dist/utilities/generate.js +14 -11
- package/dist/utilities/load.d.ts +1 -2
- package/dist/utilities/load.js +37 -35
- package/dist/utilities/profile.js +1 -1
- package/dist/utilities/save.js +14 -9
- package/dist/utilities/tokenParse.d.ts +1 -1
- package/dist/utilities/tokenParse.js +7 -61
- package/dist/utilities/weights.d.ts +3 -3
- package/dist/utilities/weights.js +21 -19
- package/dist/variable-Dl_ub3pk.js +23 -0
- package/dist/{stack-BtKpB0Ry.js → zeros-CCy9C3uU.js} +18 -16
- package/package.json +2 -1
- package/dist/assets/worker-BYeSPNkq.js +0 -1
- package/dist/tokeniser/NodeTokeniser.d.ts +0 -20
- package/dist/tokeniser/NodeTokeniser.js +0 -46
- package/dist/tokeniser/WebTokeniser.d.ts +0 -18
- package/dist/tokeniser/WebTokeniser.js +0 -96
- package/dist/tokeniser/worker.js +0 -53
- /package/dist/{tokeniser/worker.d.ts → ops/cpu/attentionMask.d.ts} +0 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { o as i, h as m, E as y, a2 as A, a0 as E, a3 as o, a4 as h, J as b, f as S, a5 as v, a6 as _, s as k, U as q } from "./index-pWA4_lUh.js";
|
|
2
|
+
import { e as w, s as u, m as l } from "./sum-C7Mgy9Bw.js";
|
|
3
|
+
import { r as c } from "./reshape-C8CR_Bad.js";
|
|
4
|
+
/**
|
|
5
|
+
* @license
|
|
6
|
+
* Copyright 2020 Google Inc. 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 K(r, e = null, n = !1) {
|
|
21
|
+
const t = { x: m(r, "x", "min") }, a = { axis: e, keepDims: n };
|
|
22
|
+
return y.runKernel(A, t, a);
|
|
23
|
+
}
|
|
24
|
+
const d = /* @__PURE__ */ i({ min_: K });
|
|
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 M(r, e = "euclidean", n = null, s = !1) {
|
|
42
|
+
r = m(r, "x", "norm");
|
|
43
|
+
const t = $(r, e, n);
|
|
44
|
+
let a = t.shape;
|
|
45
|
+
if (s) {
|
|
46
|
+
const f = E(n, r.shape);
|
|
47
|
+
a = w(t.shape, f);
|
|
48
|
+
}
|
|
49
|
+
return c(t, a);
|
|
50
|
+
}
|
|
51
|
+
function $(r, e, n = null) {
|
|
52
|
+
if (r.rank === 0)
|
|
53
|
+
return o(r);
|
|
54
|
+
if (r.rank !== 1 && n === null)
|
|
55
|
+
return $(c(r, [-1]), e, n);
|
|
56
|
+
if (r.rank === 1 || typeof n == "number" || Array.isArray(n) && n.length === 1) {
|
|
57
|
+
if (e === 1)
|
|
58
|
+
return u(o(r), n);
|
|
59
|
+
if (e === 1 / 0)
|
|
60
|
+
return l(o(r), n);
|
|
61
|
+
if (e === -1 / 0)
|
|
62
|
+
return d(o(r), n);
|
|
63
|
+
if (e === "euclidean" || e === 2)
|
|
64
|
+
return h(u(b(o(r), S(2, "int32")), n));
|
|
65
|
+
throw new Error(`Error in norm: invalid ord value: ${e}`);
|
|
66
|
+
}
|
|
67
|
+
if (Array.isArray(n) && n.length === 2) {
|
|
68
|
+
if (e === 1)
|
|
69
|
+
return l(u(o(r), n[0]), n[1] - 1);
|
|
70
|
+
if (e === 1 / 0)
|
|
71
|
+
return l(u(o(r), n[1]), n[0]);
|
|
72
|
+
if (e === -1 / 0)
|
|
73
|
+
return d(u(o(r), n[1]), n[0]);
|
|
74
|
+
if (e === "fro" || e === "euclidean")
|
|
75
|
+
return h(u(v(r), n));
|
|
76
|
+
throw new Error(`Error in norm: invalid ord value: ${e}`);
|
|
77
|
+
}
|
|
78
|
+
throw new Error(`Error in norm: invalid axis: ${n}`);
|
|
79
|
+
}
|
|
80
|
+
const J = /* @__PURE__ */ i({ norm_: M });
|
|
81
|
+
/**
|
|
82
|
+
* @license
|
|
83
|
+
* Copyright 2020 Google Inc. All Rights Reserved.
|
|
84
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
85
|
+
* you may not use this file except in compliance with the License.
|
|
86
|
+
* You may obtain a copy of the License at
|
|
87
|
+
*
|
|
88
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
89
|
+
*
|
|
90
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
91
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
92
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
93
|
+
* See the License for the specific language governing permissions and
|
|
94
|
+
* limitations under the License.
|
|
95
|
+
* =============================================================================
|
|
96
|
+
*/
|
|
97
|
+
function T(r, e = null, n = !1) {
|
|
98
|
+
const t = { x: m(r, "x", "mean") }, a = { axis: e, keepDims: n };
|
|
99
|
+
return y.runKernel(_, t, a);
|
|
100
|
+
}
|
|
101
|
+
const p = /* @__PURE__ */ i({ mean_: T });
|
|
102
|
+
/**
|
|
103
|
+
* @license
|
|
104
|
+
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
105
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
* you may not use this file except in compliance with the License.
|
|
107
|
+
* You may obtain a copy of the License at
|
|
108
|
+
*
|
|
109
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
*
|
|
111
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
* See the License for the specific language governing permissions and
|
|
115
|
+
* limitations under the License.
|
|
116
|
+
* =============================================================================
|
|
117
|
+
*/
|
|
118
|
+
function g(r, e = null, n = !1) {
|
|
119
|
+
r = m(r, "x", "moments");
|
|
120
|
+
const s = E(e, r.shape), t = p(r, s, n);
|
|
121
|
+
let a = t.shape;
|
|
122
|
+
n || (a = w(t.shape, s));
|
|
123
|
+
const f = v(k(q(r, "float32"), c(t, a))), I = p(f, s, n);
|
|
124
|
+
return { mean: t, variance: I };
|
|
125
|
+
}
|
|
126
|
+
const P = /* @__PURE__ */ i({ moments_: g });
|
|
127
|
+
export {
|
|
128
|
+
P as a,
|
|
129
|
+
d as b,
|
|
130
|
+
p as m,
|
|
131
|
+
J as n
|
|
132
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { k as n, l as t, n as m, E as i } from "./index-pWA4_lUh.js";
|
|
2
|
+
import { z as l, c } from "./zeros-CCy9C3uU.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
|
+
function f(o, r = "float32") {
|
|
20
|
+
if (n(o), r === "complex64") {
|
|
21
|
+
const s = f(o, "float32"), a = l(o, "float32");
|
|
22
|
+
return c(s, a);
|
|
23
|
+
}
|
|
24
|
+
const e = t(m(o), r);
|
|
25
|
+
return i.makeTensor(e, o, r);
|
|
26
|
+
}
|
|
27
|
+
export {
|
|
28
|
+
f as o
|
|
29
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Tensor } from '@tensorflow/tfjs';
|
|
1
|
+
import { Tensor } from '@tensorflow/tfjs-core';
|
|
2
2
|
export declare function attentionMask(q: Tensor, k: Tensor, mask: Tensor, divisor: number): Tensor;
|
|
@@ -1,88 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
userCode;
|
|
8
|
-
// enableShapeUniforms = true;
|
|
9
|
-
customUniforms = [{ name: "divisor", type: "float" }];
|
|
10
|
-
constructor(e, n, s, a) {
|
|
11
|
-
this.outputShape = [e, n, s, s], this.userCode = `
|
|
12
|
-
void main() {
|
|
13
|
-
ivec4 coords = getOutputCoords(); // [batch, nh, t1, t2]
|
|
14
|
-
int b = coords.x;
|
|
15
|
-
int h = coords.y;
|
|
16
|
-
int t1 = coords.z;
|
|
17
|
-
int t2 = coords.w;
|
|
18
|
-
|
|
19
|
-
float sum = 0.0;
|
|
20
|
-
for (int i = 0; i < ${a}; ++i) {
|
|
21
|
-
float qv = getQ(b, h, t1, i);
|
|
22
|
-
float kv = getK(b, h, t2, i); // k is transposed on last two dims
|
|
23
|
-
sum += qv * kv;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// Scale by divisor
|
|
27
|
-
float scaled = sum * divisor;
|
|
28
|
-
|
|
29
|
-
// Add mask
|
|
30
|
-
float maskVal = getMask(t1, t2); // mask is [T,T]
|
|
31
|
-
|
|
32
|
-
setOutput(scaled + maskVal);
|
|
33
|
-
}
|
|
34
|
-
`;
|
|
35
|
-
}
|
|
1
|
+
import { e as o } from "../index-pWA4_lUh.js";
|
|
2
|
+
import "./cpu/attentionMask.js";
|
|
3
|
+
import "./webgl/attentionMask.js";
|
|
4
|
+
import "./grads/attentionMask.js";
|
|
5
|
+
function s(t, n, r, e) {
|
|
6
|
+
return o().runKernel("AttentionMask", { q: t, k: n, mask: r }, { divisor: e });
|
|
36
7
|
}
|
|
37
|
-
function v(t) {
|
|
38
|
-
const { q: e, k: n, mask: s } = t.inputs, { divisor: a } = t.attrs, o = t.backend, r = e.shape[0], i = e.shape[2], c = e.shape[1], u = new h(r, c, i, e.shape[3]);
|
|
39
|
-
return o.runWebGLProgram(u, [e, n, s], "float32", [[a]]);
|
|
40
|
-
}
|
|
41
|
-
const b = {
|
|
42
|
-
kernelName: "AttentionMask",
|
|
43
|
-
backendName: "webgl",
|
|
44
|
-
kernelFunc: v
|
|
45
|
-
};
|
|
46
|
-
m(b);
|
|
47
|
-
function l(t) {
|
|
48
|
-
const { q: e, k: n, mask: s } = t.inputs, { divisor: a } = t.attrs, o = e.shape[2], i = f(e, n, !1, !0).mul(p(a)), c = s.slice([0, 0], [o, o]).expandDims(0).expandDims(0);
|
|
49
|
-
return i.add(c);
|
|
50
|
-
}
|
|
51
|
-
const M = {
|
|
52
|
-
kernelName: "AttentionMask",
|
|
53
|
-
backendName: "cpu",
|
|
54
|
-
kernelFunc: l
|
|
55
|
-
};
|
|
56
|
-
m(M);
|
|
57
|
-
const g = {
|
|
58
|
-
kernelName: "AttentionMask",
|
|
59
|
-
backendName: "tensorflow",
|
|
60
|
-
kernelFunc: l
|
|
61
|
-
};
|
|
62
|
-
m(g);
|
|
63
|
-
function N(t, e, n, s) {
|
|
64
|
-
return k().runKernel("AttentionMask", { q: t, k: e, mask: n }, { divisor: s });
|
|
65
|
-
}
|
|
66
|
-
const A = {
|
|
67
|
-
kernelName: "AttentionMask",
|
|
68
|
-
inputsToSave: ["q", "k"],
|
|
69
|
-
outputsToSave: [],
|
|
70
|
-
gradFunc: (t, e, n) => {
|
|
71
|
-
if (Array.isArray(t))
|
|
72
|
-
throw new Error("Expected dy to be a single Tensor");
|
|
73
|
-
const [s, a] = e, { divisor: o } = n;
|
|
74
|
-
return {
|
|
75
|
-
q: () => t.matMul(a).mul(o),
|
|
76
|
-
k: () => s.transpose([0, 1, 3, 2]).matMul(t).mul(o).transpose([0, 1, 3, 2]),
|
|
77
|
-
mask: () => t,
|
|
78
|
-
divisor: () => {
|
|
79
|
-
const r = s.matMul(a, !1, !0);
|
|
80
|
-
return t.mul(r).sum();
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
d(A);
|
|
86
8
|
export {
|
|
87
|
-
|
|
9
|
+
s as attentionMask
|
|
88
10
|
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { r as a, e as m } from "../../index-pWA4_lUh.js";
|
|
2
|
+
import { c as d } from "../../concat-BIZS_td9.js";
|
|
3
|
+
function r(n) {
|
|
4
|
+
const { cache: c, item: t } = n.inputs, { maxSize: o } = n.attrs, e = d([c, t], 2), s = e.shape[2];
|
|
5
|
+
if (s > o) {
|
|
6
|
+
const p = s - o, i = e.shape[0], l = e.shape[1], h = e.shape[3], u = e.slice([0, 0, p, 0], [i, l, o, h]);
|
|
7
|
+
return e.dispose(), u;
|
|
8
|
+
}
|
|
9
|
+
return e;
|
|
10
|
+
}
|
|
11
|
+
const f = {
|
|
12
|
+
kernelName: "AppendCache",
|
|
13
|
+
backendName: "cpu",
|
|
14
|
+
kernelFunc: r
|
|
15
|
+
};
|
|
16
|
+
a(f);
|
|
17
|
+
const C = {
|
|
18
|
+
kernelName: "AppendCache",
|
|
19
|
+
backendName: "tensorflow",
|
|
20
|
+
kernelFunc: r
|
|
21
|
+
};
|
|
22
|
+
a(C);
|
|
23
|
+
function N(n, c, t) {
|
|
24
|
+
return m().runKernel("AppendCache", { cache: n, item: c }, { maxSize: t });
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
N as appendCache
|
|
28
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { r as s, f as i } from "../../index-pWA4_lUh.js";
|
|
2
|
+
import { m as k } from "../../mat_mul-D7_a4KJn.js";
|
|
3
|
+
function a(t) {
|
|
4
|
+
const { q: e, k: o, mask: r } = t.inputs, { divisor: c } = t.attrs, n = e.shape[2], m = k(e, o, !1, !0).mul(i(c)), l = r.slice([0, 0], [n, n]).expandDims(0).expandDims(0);
|
|
5
|
+
return m.add(l);
|
|
6
|
+
}
|
|
7
|
+
const d = {
|
|
8
|
+
kernelName: "AttentionMask",
|
|
9
|
+
backendName: "cpu",
|
|
10
|
+
kernelFunc: a
|
|
11
|
+
};
|
|
12
|
+
s(d);
|
|
13
|
+
const u = {
|
|
14
|
+
kernelName: "AttentionMask",
|
|
15
|
+
backendName: "tensorflow",
|
|
16
|
+
kernelFunc: a
|
|
17
|
+
};
|
|
18
|
+
s(u);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { o as u, h as c, E as g, H as h, r as m, s as p } from "../../index-pWA4_lUh.js";
|
|
2
|
+
import { r as l } from "../../range-CcDl05lo.js";
|
|
3
|
+
import { s as N } from "../../stack-BMm-efee.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 f(e, s) {
|
|
21
|
+
const n = c(s, "indices", "gatherND", "int32"), t = { params: c(e, "x", "gatherND", "string_or_numeric"), indices: n };
|
|
22
|
+
return g.runKernel(h, t);
|
|
23
|
+
}
|
|
24
|
+
const b = /* @__PURE__ */ u({ gatherND_: f });
|
|
25
|
+
function d(e) {
|
|
26
|
+
const { values: s, labels: n, logits: r } = e.inputs, t = n.shape[0], o = l(0, t, 1, "int32"), a = N([o, n], 1), i = b(r, a);
|
|
27
|
+
return p(s, i);
|
|
28
|
+
}
|
|
29
|
+
const k = {
|
|
30
|
+
kernelName: "EfficientGatherSub",
|
|
31
|
+
backendName: "cpu",
|
|
32
|
+
kernelFunc: d
|
|
33
|
+
};
|
|
34
|
+
m(k);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { r as q } from "../../index-pWA4_lUh.js";
|
|
2
|
+
import { r as o } from "../../reshape-C8CR_Bad.js";
|
|
3
|
+
import { s as x } from "../../split-DZbvruEP.js";
|
|
4
|
+
function v(p) {
|
|
5
|
+
const { x: c, kernel: K } = p.inputs, { heads: n } = p.attrs, [s, e, t] = c.shape, a = o(c, [s * e, t]), i = a.dot(K);
|
|
6
|
+
a.dispose();
|
|
7
|
+
const d = o(i, [s, e, 3 * t]);
|
|
8
|
+
i.dispose();
|
|
9
|
+
const [k, l, m] = x(d, 3, -1);
|
|
10
|
+
d.dispose();
|
|
11
|
+
const r = t / n, f = o(k, [s, e, n, r]);
|
|
12
|
+
k.dispose();
|
|
13
|
+
const C = f.transpose([0, 2, 1, 3]);
|
|
14
|
+
f.dispose();
|
|
15
|
+
const h = o(l, [s, e, n, r]);
|
|
16
|
+
l.dispose();
|
|
17
|
+
const N = h.transpose([0, 2, 1, 3]);
|
|
18
|
+
h.dispose();
|
|
19
|
+
const u = o(m, [s, e, n, r]);
|
|
20
|
+
m.dispose();
|
|
21
|
+
const T = u.transpose([0, 2, 1, 3]);
|
|
22
|
+
return u.dispose(), [C, N, T];
|
|
23
|
+
}
|
|
24
|
+
const F = {
|
|
25
|
+
kernelName: "QKV",
|
|
26
|
+
backendName: "cpu",
|
|
27
|
+
kernelFunc: v
|
|
28
|
+
};
|
|
29
|
+
q(F);
|
|
30
|
+
const R = {
|
|
31
|
+
kernelName: "QKV",
|
|
32
|
+
backendName: "tensorflow",
|
|
33
|
+
kernelFunc: v
|
|
34
|
+
};
|
|
35
|
+
q(R);
|
|
36
|
+
export {
|
|
37
|
+
v as qkvCPU
|
|
38
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { NamedAttrMap, NamedTensorInfoMap, Tensor, TensorInfo } from '@tensorflow/tfjs-core';
|
|
2
|
+
export declare function applyRoPE(sinCache: Tensor, cosCache: Tensor, rotaryDim: number, q: Tensor, pastLen: number): Tensor;
|
|
3
|
+
export declare function ropeCPU(args: {
|
|
4
|
+
inputs: NamedTensorInfoMap;
|
|
5
|
+
attrs?: NamedAttrMap;
|
|
6
|
+
}): TensorInfo;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { r as S } from "../../index-pWA4_lUh.js";
|
|
2
|
+
import { r as F } from "../../range-CcDl05lo.js";
|
|
3
|
+
import { g as I } from "../../gather-BPGW8RsB.js";
|
|
4
|
+
import { s as E } from "../../stack-BMm-efee.js";
|
|
5
|
+
import { c as T } from "../../concat-BIZS_td9.js";
|
|
6
|
+
function U(t, c, p, o, r) {
|
|
7
|
+
const n = o.shape[3], s = p;
|
|
8
|
+
if (s > n) return o;
|
|
9
|
+
const e = o.shape[2], i = s / 2, a = c.slice([r, 0, 0], [e, i, 1]).reshape([1, 1, e, i]), d = t.slice([r, 0, 0], [e, i, 1]).reshape([1, 1, e, i]), l = o.shape[0], m = o.shape[1], h = F(0, s, 2, "int32"), k = F(1, s, 2, "int32"), D = ((R) => {
|
|
10
|
+
const g = R.slice([0, 0, 0, 0], [l, m, e, s]), v = s < n ? R.slice([0, 0, 0, s], [l, m, e, n - s]) : null, u = I(g, h, 3), f = I(g, k, 3), C = u.mul(a), N = f.mul(d), P = C.sub(N), b = f.mul(a), x = u.mul(d), K = b.add(x);
|
|
11
|
+
u.dispose(), f.dispose(), a.dispose(), d.dispose(), C.dispose(), N.dispose(), b.dispose(), x.dispose();
|
|
12
|
+
const w = E([P, K], -1);
|
|
13
|
+
P.dispose(), K.dispose();
|
|
14
|
+
const y = w.reshape([l, m, e, s]);
|
|
15
|
+
return w.dispose(), v ? T([y, v], 3) : y;
|
|
16
|
+
})(o);
|
|
17
|
+
return h.dispose(), k.dispose(), D;
|
|
18
|
+
}
|
|
19
|
+
function B(t) {
|
|
20
|
+
const { x: c, sin: p, cos: o } = t.inputs, { pastLen: r } = t.attrs, n = c.shape[3];
|
|
21
|
+
return U(p, o, n, c, r);
|
|
22
|
+
}
|
|
23
|
+
const j = {
|
|
24
|
+
kernelName: "Rope",
|
|
25
|
+
backendName: "cpu",
|
|
26
|
+
kernelFunc: B
|
|
27
|
+
};
|
|
28
|
+
S(j);
|
|
29
|
+
const z = {
|
|
30
|
+
kernelName: "Rope",
|
|
31
|
+
backendName: "tensorflow",
|
|
32
|
+
kernelFunc: B
|
|
33
|
+
};
|
|
34
|
+
S(z);
|
|
35
|
+
export {
|
|
36
|
+
U as applyRoPE,
|
|
37
|
+
B as ropeCPU
|
|
38
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { o as l, k, h, E as g, Q as w, r as $, s as d, b as m } from "../../index-pWA4_lUh.js";
|
|
2
|
+
import { r as b } from "../../range-CcDl05lo.js";
|
|
3
|
+
import { s as E } from "../../stack-BMm-efee.js";
|
|
4
|
+
import { o as D } from "../../ones-Cog-G2ag.js";
|
|
5
|
+
function N(a, r, t) {
|
|
6
|
+
const s = r.rank > 1 ? r.shape[r.rank - 1] : 1, e = r.rank > 1 ? r.rank - 1 : 1, o = `Must have updates.shape = indices.shape[:batchDim] + shape[sliceDim:], got updates.shape: ${t.shape}, indices.shape: ${r.shape}, shape: ${a}, sliceDim: ${s}, and batchDim: ${e}.`;
|
|
7
|
+
if (t.rank < e)
|
|
8
|
+
throw new Error(o + ` update.rank < ${e}. `);
|
|
9
|
+
if (a.length < s + (t.rank - e))
|
|
10
|
+
throw new Error(o + ` Output shape length < ${s + (t.rank - e)}`);
|
|
11
|
+
if (t.rank !== e + a.length - s)
|
|
12
|
+
throw new Error(o + ` update.rank != ${e + a.length - s}`);
|
|
13
|
+
for (let n = 0; n < e; ++n)
|
|
14
|
+
if (t.shape[n] !== r.shape[n])
|
|
15
|
+
throw new Error(o + ` updates.shape[${n}] (${t.shape[n]}) != indices.shape[${n}] (${r.shape[n]}).`);
|
|
16
|
+
for (let n = 0; n < t.rank - e; ++n)
|
|
17
|
+
if (t.shape[n + e] !== a[n + s])
|
|
18
|
+
throw new Error(o + ` updates.shape[${n + e}] (${t.shape[n + e]}) != shape[${n + e}] (${a[n + e]})`);
|
|
19
|
+
}
|
|
20
|
+
function S(a, r, t) {
|
|
21
|
+
if (r.rank < 1)
|
|
22
|
+
throw new Error(`tf.scatterND() expects the indices to be rank 1 or higher, but the rank was ${r.rank}.`);
|
|
23
|
+
if (a.rank < 1)
|
|
24
|
+
throw new Error(`tf.scatterND() expects the updates to be rank 1 or higher, but the rank was ${a.rank}.`);
|
|
25
|
+
if (r.dtype !== "int32")
|
|
26
|
+
throw new Error(`The dtype of 'indices' should be int32, but got dtype: ${r.dtype}`);
|
|
27
|
+
if (t.length < 1)
|
|
28
|
+
throw new Error(`Output rank must be greater or equal to 1, but got shape: ${t}`);
|
|
29
|
+
if (t.length === 0) {
|
|
30
|
+
if (r.size === 0)
|
|
31
|
+
throw new Error(`Indices specified for empty output. indices shape: ${r.shape}`);
|
|
32
|
+
if (a.size === 0)
|
|
33
|
+
throw new Error(`Updates specified for empty output. updates shape: ${a.shape}`);
|
|
34
|
+
}
|
|
35
|
+
N(t, r, a);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* @license
|
|
39
|
+
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
40
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
41
|
+
* you may not use this file except in compliance with the License.
|
|
42
|
+
* You may obtain a copy of the License at
|
|
43
|
+
*
|
|
44
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
45
|
+
*
|
|
46
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
47
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
48
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
49
|
+
* See the License for the specific language governing permissions and
|
|
50
|
+
* limitations under the License.
|
|
51
|
+
* =============================================================================
|
|
52
|
+
*/
|
|
53
|
+
function y(a, r, t) {
|
|
54
|
+
k(t);
|
|
55
|
+
const s = h(a, "indices", "scatterND", "int32"), e = h(r, "updates", "scatterND");
|
|
56
|
+
S(e, s, t);
|
|
57
|
+
const o = { indices: s, updates: e }, n = { shape: t };
|
|
58
|
+
return g.runKernel(w, o, n);
|
|
59
|
+
}
|
|
60
|
+
const v = /* @__PURE__ */ l({ scatterND_: y });
|
|
61
|
+
function I(a) {
|
|
62
|
+
const { logits: r, labels: t, dy: s } = a.inputs, e = t.shape[0], o = r.shape[1], n = b(0, e, 1, "int32"), i = E([n, t], 1), c = D([e]), p = v(i, c, [e, o]), f = d(r, p), u = s.reshape([e, 1]);
|
|
63
|
+
return m(f, u);
|
|
64
|
+
}
|
|
65
|
+
const T = {
|
|
66
|
+
kernelName: "EfficientScatterSub",
|
|
67
|
+
backendName: "cpu",
|
|
68
|
+
kernelFunc: I
|
|
69
|
+
};
|
|
70
|
+
$(T);
|
package/dist/ops/gatherSub.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Tensor } from '@tensorflow/tfjs';
|
|
1
|
+
import { Tensor } from '@tensorflow/tfjs-core';
|
|
2
2
|
export declare function gatherSub(values: Tensor, labels: Tensor, logits: Tensor): Tensor;
|
package/dist/ops/gatherSub.js
CHANGED
|
@@ -1,66 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
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 m(e, t) {
|
|
21
|
-
const n = i(t, "indices", "gatherND", "int32"), r = { params: i(e, "x", "gatherND", "string_or_numeric"), indices: n };
|
|
22
|
-
return b.runKernel(d, r);
|
|
23
|
-
}
|
|
24
|
-
const N = /* @__PURE__ */ g({ gatherND_: m });
|
|
25
|
-
class S {
|
|
26
|
-
variableNames = ["labels", "logits", "values"];
|
|
27
|
-
outputShape;
|
|
28
|
-
userCode;
|
|
29
|
-
constructor(t) {
|
|
30
|
-
this.outputShape = [t], this.userCode = `
|
|
31
|
-
void main() {
|
|
32
|
-
int coords = getOutputCoords();
|
|
33
|
-
int index = int(getLabelsAtOutCoords());
|
|
34
|
-
float val = getValuesAtOutCoords();
|
|
35
|
-
float logit = getLogits(coords, index);
|
|
36
|
-
setOutput(val - logit);
|
|
37
|
-
}
|
|
38
|
-
`;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
function k(e) {
|
|
42
|
-
const { logits: t, labels: n, values: s } = e.inputs, r = e.backend, o = n.shape[0], a = new S(o);
|
|
43
|
-
return r.runWebGLProgram(a, [n, t, s], "float32");
|
|
44
|
-
}
|
|
45
|
-
const G = {
|
|
46
|
-
kernelName: "EfficientGatherSub",
|
|
47
|
-
backendName: "webgl",
|
|
48
|
-
kernelFunc: k
|
|
49
|
-
};
|
|
50
|
-
c(G);
|
|
51
|
-
function v(e) {
|
|
52
|
-
const { values: t, labels: n, logits: s } = e.inputs, r = n.shape[0], o = p(0, r, 1, "int32"), a = f([o, n], 1), u = N(s, a);
|
|
53
|
-
return h(t, u);
|
|
54
|
-
}
|
|
55
|
-
const C = {
|
|
56
|
-
kernelName: "EfficientGatherSub",
|
|
57
|
-
backendName: "cpu",
|
|
58
|
-
kernelFunc: v
|
|
59
|
-
};
|
|
60
|
-
c(C);
|
|
61
|
-
function K(e, t, n) {
|
|
62
|
-
return l().runKernel("EfficientGatherSub", { logits: n, labels: t, values: e }, {});
|
|
1
|
+
import { e as n } from "../index-pWA4_lUh.js";
|
|
2
|
+
import "./cpu/gatherSub.js";
|
|
3
|
+
import "./webgl/gatherSub.js";
|
|
4
|
+
function f(r, e, t) {
|
|
5
|
+
return n().runKernel("EfficientGatherSub", { logits: t, labels: e, values: r }, {});
|
|
63
6
|
}
|
|
64
7
|
export {
|
|
65
|
-
|
|
8
|
+
f as gatherSub
|
|
66
9
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { g as i } from "../../index-pWA4_lUh.js";
|
|
2
|
+
const u = {
|
|
3
|
+
kernelName: "AttentionMask",
|
|
4
|
+
inputsToSave: ["q", "k"],
|
|
5
|
+
outputsToSave: [],
|
|
6
|
+
gradFunc: (t, a, n) => {
|
|
7
|
+
if (Array.isArray(t))
|
|
8
|
+
throw new Error("Expected dy to be a single Tensor");
|
|
9
|
+
const [r, e] = a, { divisor: s } = n;
|
|
10
|
+
return {
|
|
11
|
+
q: () => t.matMul(e).mul(s),
|
|
12
|
+
k: () => r.transpose([0, 1, 3, 2]).matMul(t).mul(s).transpose([0, 1, 3, 2]),
|
|
13
|
+
mask: () => t,
|
|
14
|
+
divisor: () => {
|
|
15
|
+
const o = r.matMul(e, !1, !0);
|
|
16
|
+
return t.mul(o).sum();
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
i(u);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { g as v } from "../../index-pWA4_lUh.js";
|
|
2
|
+
const g = {
|
|
3
|
+
kernelName: "QKV",
|
|
4
|
+
inputsToSave: ["x", "kernel"],
|
|
5
|
+
outputsToSave: [],
|
|
6
|
+
gradFunc: (k, m) => {
|
|
7
|
+
const [x, K, f] = k, [c, r] = m, [t, s, e] = c.shape, d = x.transpose([0, 2, 1, 3]).reshape([t * s, e]), l = K.transpose([0, 2, 1, 3]).reshape([t * s, e]), u = f.transpose([0, 2, 1, 3]).reshape([t * s, e]), i = r.slice([0, 0], [e, e]), h = r.slice([0, e], [e, e]), M = r.slice([0, 2 * e], [e, e]);
|
|
8
|
+
return {
|
|
9
|
+
x: () => {
|
|
10
|
+
const n = d.matMul(i, !1, !0), a = l.matMul(h, !1, !0), o = u.matMul(M, !1, !0);
|
|
11
|
+
return n.add(a).add(o).reshape([t, s, e]);
|
|
12
|
+
},
|
|
13
|
+
kernel: () => {
|
|
14
|
+
const n = c.reshape([t * s, e]), a = n.matMul(d, !0, !1), o = n.matMul(l, !0, !1), p = n.matMul(u, !0, !1);
|
|
15
|
+
return a.concat(o, 1).concat(p, 1);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
v(g);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|