@genai-fi/nanogpt 0.2.12 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Generator.js +30 -25
- package/dist/NanoGPTModel.d.ts +13 -14
- package/dist/NanoGPTModel.js +167 -85
- package/dist/TeachableLLM.d.ts +3 -5
- package/dist/TeachableLLM.js +47 -35
- 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-7idKoYqh.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 +1 -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-PbCt7RXv.js +15489 -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 +88 -60
- 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,14 @@
|
|
|
1
|
+
import { g as a, e as i } from "../../index-pWA4_lUh.js";
|
|
2
|
+
function p(n, e, s, o) {
|
|
3
|
+
return i().runKernel("Rope", { x: n, sin: e, cos: s }, { pastLen: o });
|
|
4
|
+
}
|
|
5
|
+
const c = {
|
|
6
|
+
kernelName: "Rope",
|
|
7
|
+
inputsToSave: ["sin", "cos"],
|
|
8
|
+
outputsToSave: [],
|
|
9
|
+
gradFunc: (n, e) => {
|
|
10
|
+
const [s, o] = e, t = s.neg(), r = p(n, t, o, 0);
|
|
11
|
+
return t.dispose(), { x: () => r };
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
a(c);
|
package/dist/ops/qkv.d.ts
CHANGED
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
import { Tensor } from '@tensorflow/tfjs';
|
|
2
|
-
import { TensorInfo, NamedTensorInfoMap, NamedAttrMap } from '@tensorflow/tfjs-core';
|
|
3
|
-
export declare function qkvCPU(args: {
|
|
4
|
-
inputs: NamedTensorInfoMap;
|
|
5
|
-
attrs?: NamedAttrMap;
|
|
6
|
-
}): TensorInfo[];
|
|
1
|
+
import { Tensor } from '@tensorflow/tfjs-core';
|
|
7
2
|
export declare function qkv(x: Tensor, kernel: Tensor, heads: number): Tensor[];
|
package/dist/ops/qkv.js
CHANGED
|
@@ -1,127 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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(r, e, o = 0) {
|
|
21
|
-
const t = { x: V(r, "x", "split") }, s = { numOrSizeSplits: e, axis: o };
|
|
22
|
-
return Q.runKernel(T, t, s);
|
|
1
|
+
import { e as o } from "../index-pWA4_lUh.js";
|
|
2
|
+
import "./cpu/qkv.js";
|
|
3
|
+
import "./webgl/qkv.js";
|
|
4
|
+
import "./grads/qkv.js";
|
|
5
|
+
function u(r, e, n) {
|
|
6
|
+
return o().runKernel("QKV", { x: r, kernel: e }, { heads: n });
|
|
23
7
|
}
|
|
24
|
-
const P = /* @__PURE__ */ N({ split_: M });
|
|
25
|
-
class S {
|
|
26
|
-
variableNames = ["x", "kernel"];
|
|
27
|
-
outputShape;
|
|
28
|
-
userCode;
|
|
29
|
-
// enableShapeUniforms = true;
|
|
30
|
-
customUniforms = [{ name: "mode", type: "int" }];
|
|
31
|
-
constructor(e, o, a, t) {
|
|
32
|
-
const s = t / o;
|
|
33
|
-
this.outputShape = [e, o, a, s], this.userCode = `
|
|
34
|
-
void main() {
|
|
35
|
-
ivec4 coords = getOutputCoords(); // [b, h, t, d]
|
|
36
|
-
int b = coords.x;
|
|
37
|
-
int h = coords.y;
|
|
38
|
-
int t = coords.z;
|
|
39
|
-
int d = coords.w;
|
|
40
|
-
|
|
41
|
-
// Compute output channel index in fused kernel
|
|
42
|
-
int out_offset = mode * ${o} * ${s} + h * ${s} + d;
|
|
43
|
-
|
|
44
|
-
float sum = 0.0;
|
|
45
|
-
for (int c = 0; c < ${t}; ++c) {
|
|
46
|
-
float xval = getX(b, t, c); // fetch from x
|
|
47
|
-
float kval = getKernel(c, out_offset); // fetch from kernel
|
|
48
|
-
sum += xval * kval;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
setOutput(sum);
|
|
52
|
-
}
|
|
53
|
-
`;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
function $(r) {
|
|
57
|
-
const { x: e, kernel: o } = r.inputs, { heads: a } = r.attrs, t = r.backend, s = e.shape[0], l = e.shape[1], d = e.shape[2], c = new S(s, a, l, d);
|
|
58
|
-
return [
|
|
59
|
-
t.runWebGLProgram(c, [e, o], "float32", [[0]]),
|
|
60
|
-
t.runWebGLProgram(c, [e, o], "float32", [[1]]),
|
|
61
|
-
t.runWebGLProgram(c, [e, o], "float32", [[2]])
|
|
62
|
-
];
|
|
63
|
-
}
|
|
64
|
-
const w = {
|
|
65
|
-
kernelName: "QKV",
|
|
66
|
-
backendName: "webgl",
|
|
67
|
-
kernelFunc: $
|
|
68
|
-
};
|
|
69
|
-
b(w);
|
|
70
|
-
function q(r) {
|
|
71
|
-
const { x: e, kernel: o } = r.inputs, { heads: a } = r.attrs, [t, s, l] = e.shape, d = x(e, [t * s, l]), c = d.dot(o);
|
|
72
|
-
d.dispose();
|
|
73
|
-
const n = x(c, [t, s, 3 * l]);
|
|
74
|
-
c.dispose();
|
|
75
|
-
const [p, m, k] = P(n, 3, -1);
|
|
76
|
-
n.dispose();
|
|
77
|
-
const f = l / a, v = x(p, [t, s, a, f]);
|
|
78
|
-
p.dispose();
|
|
79
|
-
const g = v.transpose([0, 2, 1, 3]);
|
|
80
|
-
v.dispose();
|
|
81
|
-
const u = x(m, [t, s, a, f]);
|
|
82
|
-
m.dispose();
|
|
83
|
-
const h = u.transpose([0, 2, 1, 3]);
|
|
84
|
-
u.dispose();
|
|
85
|
-
const i = x(k, [t, s, a, f]);
|
|
86
|
-
k.dispose();
|
|
87
|
-
const K = i.transpose([0, 2, 1, 3]);
|
|
88
|
-
return i.dispose(), [g, h, K];
|
|
89
|
-
}
|
|
90
|
-
const F = {
|
|
91
|
-
kernelName: "QKV",
|
|
92
|
-
backendName: "cpu",
|
|
93
|
-
kernelFunc: q
|
|
94
|
-
};
|
|
95
|
-
b(F);
|
|
96
|
-
const L = {
|
|
97
|
-
kernelName: "QKV",
|
|
98
|
-
backendName: "tensorflow",
|
|
99
|
-
kernelFunc: q
|
|
100
|
-
};
|
|
101
|
-
b(L);
|
|
102
|
-
function y(r, e, o) {
|
|
103
|
-
return C().runKernel("QKV", { x: r, kernel: e }, { heads: o });
|
|
104
|
-
}
|
|
105
|
-
const _ = {
|
|
106
|
-
kernelName: "QKV",
|
|
107
|
-
inputsToSave: ["x", "kernel"],
|
|
108
|
-
outputsToSave: [],
|
|
109
|
-
gradFunc: (r, e) => {
|
|
110
|
-
const [o, a, t] = r, [s, l] = e, [d, c, n] = s.shape, p = o.transpose([0, 2, 1, 3]).reshape([d * c, n]), m = a.transpose([0, 2, 1, 3]).reshape([d * c, n]), k = t.transpose([0, 2, 1, 3]).reshape([d * c, n]), f = l.slice([0, 0], [n, n]), v = l.slice([0, n], [n, n]), g = l.slice([0, 2 * n], [n, n]);
|
|
111
|
-
return {
|
|
112
|
-
x: () => {
|
|
113
|
-
const u = p.matMul(f, !1, !0), h = m.matMul(v, !1, !0), i = k.matMul(g, !1, !0);
|
|
114
|
-
return u.add(h).add(i).reshape([d, c, n]);
|
|
115
|
-
},
|
|
116
|
-
kernel: () => {
|
|
117
|
-
const u = s.reshape([d * c, n]), h = u.matMul(p, !0, !1), i = u.matMul(m, !0, !1), K = u.matMul(k, !0, !1);
|
|
118
|
-
return h.concat(i, 1).concat(K, 1);
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
G(_);
|
|
124
8
|
export {
|
|
125
|
-
|
|
126
|
-
q as qkvCPU
|
|
9
|
+
u as qkv
|
|
127
10
|
};
|
package/dist/ops/rope.d.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
1
|
import { default as RoPECache } from '../layers/RoPECache';
|
|
2
2
|
import { Tensor } from '@tensorflow/tfjs';
|
|
3
|
-
import { TensorInfo, NamedTensorInfoMap, NamedAttrMap } from '@tensorflow/tfjs-core';
|
|
4
|
-
export declare function ropeCPU(args: {
|
|
5
|
-
inputs: NamedTensorInfoMap;
|
|
6
|
-
attrs?: NamedAttrMap;
|
|
7
|
-
}): TensorInfo;
|
|
8
3
|
export declare function rope(x: Tensor, cache: RoPECache, pastLength: number): Tensor;
|
package/dist/ops/rope.js
CHANGED
|
@@ -1,154 +1,10 @@
|
|
|
1
|
-
import { engine as
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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 W(o, t = 0) {
|
|
21
|
-
F(o.length >= 1, () => "Pass at least one tensor to concat");
|
|
22
|
-
const e = _(o, "tensors", "concat", "string_or_numeric");
|
|
23
|
-
if (e[0].dtype === "complex64" && e.forEach((r) => {
|
|
24
|
-
if (r.dtype !== "complex64")
|
|
25
|
-
throw new Error(`Cannot concatenate complex64 tensors with a tensor
|
|
26
|
-
with dtype ${r.dtype}. `);
|
|
27
|
-
}), e.length === 1)
|
|
28
|
-
return U(e[0]);
|
|
29
|
-
const n = e, s = { axis: t };
|
|
30
|
-
return K.runKernel(z, n, s);
|
|
1
|
+
import { engine as n } from "@tensorflow/tfjs";
|
|
2
|
+
import "./cpu/rope.js";
|
|
3
|
+
import "./webgl/rope.js";
|
|
4
|
+
import "./grads/rope.js";
|
|
5
|
+
function s(o, e, r) {
|
|
6
|
+
return e.ensureRopeCache(o.shape[1] + r), n().runKernel("Rope", { x: o, sin: e.getSin(), cos: e.getCos() }, { pastLen: r });
|
|
31
7
|
}
|
|
32
|
-
const j = /* @__PURE__ */ G({ concat_: W });
|
|
33
|
-
/**
|
|
34
|
-
* @license
|
|
35
|
-
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
36
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
37
|
-
* you may not use this file except in compliance with the License.
|
|
38
|
-
* You may obtain a copy of the License at
|
|
39
|
-
*
|
|
40
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
41
|
-
*
|
|
42
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
43
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
44
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
45
|
-
* See the License for the specific language governing permissions and
|
|
46
|
-
* limitations under the License.
|
|
47
|
-
* =============================================================================
|
|
48
|
-
*/
|
|
49
|
-
function H(o, t, e = 0, n = 0) {
|
|
50
|
-
const s = I(o, "x", "gather"), r = I(t, "indices", "gather", "int32"), c = { x: s, indices: r }, a = { axis: e, batchDims: n };
|
|
51
|
-
return K.runKernel(O, c, a);
|
|
52
|
-
}
|
|
53
|
-
const E = /* @__PURE__ */ G({ gather_: H });
|
|
54
|
-
class J {
|
|
55
|
-
variableNames = ["x", "sin", "cos"];
|
|
56
|
-
outputShape;
|
|
57
|
-
userCode;
|
|
58
|
-
// enableShapeUniforms = true;
|
|
59
|
-
customUniforms = [{ name: "pastLen", type: "int" }];
|
|
60
|
-
constructor(t, e, n, s) {
|
|
61
|
-
this.outputShape = [t, e, n, s], this.userCode = `
|
|
62
|
-
void main() {
|
|
63
|
-
ivec4 coords = getOutputCoords(); // [b, h, t, d]
|
|
64
|
-
int b = coords.x;
|
|
65
|
-
int h = coords.y;
|
|
66
|
-
int t = coords.z;
|
|
67
|
-
int d = coords.w;
|
|
68
|
-
|
|
69
|
-
int rotaryDim = ${s};
|
|
70
|
-
|
|
71
|
-
float outVal = 0.0;
|
|
72
|
-
|
|
73
|
-
if (d < rotaryDim) {
|
|
74
|
-
int pairIdx = d / 2;
|
|
75
|
-
float cos = getCos(t + pastLen, pairIdx, 0);
|
|
76
|
-
float sin = getSin(t + pastLen, pairIdx, 0);
|
|
77
|
-
|
|
78
|
-
if (d % 2 == 0) {
|
|
79
|
-
// even index
|
|
80
|
-
float even = getX(b, h, t, d);
|
|
81
|
-
float odd = getX(b, h, t, d + 1);
|
|
82
|
-
outVal = even * cos - odd * sin;
|
|
83
|
-
} else {
|
|
84
|
-
// odd index
|
|
85
|
-
float even = getX(b, h, t, d - 1);
|
|
86
|
-
float odd = getX(b, h, t, d);
|
|
87
|
-
outVal = even * sin + odd * cos;
|
|
88
|
-
}
|
|
89
|
-
} else {
|
|
90
|
-
// pass through for non-rotary dims
|
|
91
|
-
outVal = getX(b, h, t, d);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
setOutput(outVal);
|
|
95
|
-
}
|
|
96
|
-
`;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
function M(o) {
|
|
100
|
-
const { x: t, sin: e, cos: n } = o.inputs, { pastLen: s } = o.attrs, r = o.backend, c = t.shape[0], a = t.shape[1], i = t.shape[2], d = t.shape[3], p = new J(c, a, i, d);
|
|
101
|
-
return r.runWebGLProgram(p, [t, e, n], "float32", [[s]]);
|
|
102
|
-
}
|
|
103
|
-
const Q = {
|
|
104
|
-
kernelName: "Rope",
|
|
105
|
-
backendName: "webgl",
|
|
106
|
-
kernelFunc: M
|
|
107
|
-
};
|
|
108
|
-
f(Q);
|
|
109
|
-
function V(o, t, e, n, s) {
|
|
110
|
-
const r = n.shape[3], c = e;
|
|
111
|
-
if (c > r) return n;
|
|
112
|
-
const a = n.shape[2], i = c / 2, d = t.slice([s, 0, 0], [a, i, 1]).reshape([1, 1, a, i]), p = o.slice([s, 0, 0], [a, i, 1]).reshape([1, 1, a, i]), u = n.shape[0], l = n.shape[1], g = T(0, c, 2, "int32"), x = T(1, c, 2, "int32"), $ = ((b) => {
|
|
113
|
-
const v = b.slice([0, 0, 0, 0], [u, l, a, c]), k = c < r ? b.slice([0, 0, 0, c], [u, l, a, r - c]) : null, h = E(v, g, 3), m = E(v, x, 3), C = h.mul(d), y = m.mul(p), R = C.sub(y), N = m.mul(d), S = h.mul(p), w = N.add(S);
|
|
114
|
-
h.dispose(), m.dispose(), d.dispose(), p.dispose(), C.dispose(), y.dispose(), N.dispose(), S.dispose();
|
|
115
|
-
const L = B([R, w], -1);
|
|
116
|
-
R.dispose(), w.dispose();
|
|
117
|
-
const P = L.reshape([u, l, a, c]);
|
|
118
|
-
return L.dispose(), k ? j([P, k], 3) : P;
|
|
119
|
-
})(n);
|
|
120
|
-
return g.dispose(), x.dispose(), $;
|
|
121
|
-
}
|
|
122
|
-
function X(o) {
|
|
123
|
-
const { x: t, sin: e, cos: n } = o.inputs, { pastLen: s } = o.attrs, r = t.shape[3];
|
|
124
|
-
return V(e, n, r, t, s);
|
|
125
|
-
}
|
|
126
|
-
const Y = {
|
|
127
|
-
kernelName: "Rope",
|
|
128
|
-
backendName: "cpu",
|
|
129
|
-
kernelFunc: X
|
|
130
|
-
};
|
|
131
|
-
f(Y);
|
|
132
|
-
const Z = {
|
|
133
|
-
kernelName: "Rope",
|
|
134
|
-
backendName: "tensorflow",
|
|
135
|
-
kernelFunc: X
|
|
136
|
-
};
|
|
137
|
-
f(Z);
|
|
138
|
-
function st(o, t, e) {
|
|
139
|
-
return t.ensureRopeCache(o.shape[1] + e), D().runKernel("Rope", { x: o, sin: t.getSin(), cos: t.getCos() }, { pastLen: e });
|
|
140
|
-
}
|
|
141
|
-
const q = {
|
|
142
|
-
kernelName: "Rope",
|
|
143
|
-
inputsToSave: ["x", "sin", "cos"],
|
|
144
|
-
outputsToSave: [],
|
|
145
|
-
gradFunc: (o, t) => {
|
|
146
|
-
const [e, n, s] = t, r = n.neg(), c = e.shape[3], i = V(r, s, c, o, 0);
|
|
147
|
-
return r.dispose(), { x: () => i };
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
A(q);
|
|
151
8
|
export {
|
|
152
|
-
|
|
153
|
-
X as ropeCPU
|
|
9
|
+
s as rope
|
|
154
10
|
};
|
package/dist/ops/scatterSub.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 scatterSub(probabilities: Tensor, labels: Tensor, scale: Tensor): Tensor;
|
package/dist/ops/scatterSub.js
CHANGED
|
@@ -1,150 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* @license
|
|
7
|
-
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
8
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9
|
-
* you may not use this file except in compliance with the License.
|
|
10
|
-
* You may obtain a copy of the License at
|
|
11
|
-
*
|
|
12
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
-
*
|
|
14
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
15
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17
|
-
* See the License for the specific language governing permissions and
|
|
18
|
-
* limitations under the License.
|
|
19
|
-
* =============================================================================
|
|
20
|
-
*/
|
|
21
|
-
function i(e, t = "float32") {
|
|
22
|
-
if (u(e), t === "complex64") {
|
|
23
|
-
const a = i(e, "float32"), o = i(e, "float32");
|
|
24
|
-
return d(a, o);
|
|
25
|
-
}
|
|
26
|
-
const r = S(l(e), t);
|
|
27
|
-
return f.makeTensor(r, e, t);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* @license
|
|
31
|
-
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
32
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
33
|
-
* you may not use this file except in compliance with the License.
|
|
34
|
-
* You may obtain a copy of the License at
|
|
35
|
-
*
|
|
36
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
37
|
-
*
|
|
38
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
39
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
40
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
41
|
-
* See the License for the specific language governing permissions and
|
|
42
|
-
* limitations under the License.
|
|
43
|
-
* =============================================================================
|
|
44
|
-
*/
|
|
45
|
-
function m(e, t = "float32") {
|
|
46
|
-
if (u(e), t === "complex64") {
|
|
47
|
-
const a = m(e, "float32"), o = i(e, "float32");
|
|
48
|
-
return d(a, o);
|
|
49
|
-
}
|
|
50
|
-
const r = E(l(e), t);
|
|
51
|
-
return f.makeTensor(r, e, t);
|
|
52
|
-
}
|
|
53
|
-
function C(e, t, r) {
|
|
54
|
-
const a = t.rank > 1 ? t.shape[t.rank - 1] : 1, o = t.rank > 1 ? t.rank - 1 : 1, s = `Must have updates.shape = indices.shape[:batchDim] + shape[sliceDim:], got updates.shape: ${r.shape}, indices.shape: ${t.shape}, shape: ${e}, sliceDim: ${a}, and batchDim: ${o}.`;
|
|
55
|
-
if (r.rank < o)
|
|
56
|
-
throw new Error(s + ` update.rank < ${o}. `);
|
|
57
|
-
if (e.length < a + (r.rank - o))
|
|
58
|
-
throw new Error(s + ` Output shape length < ${a + (r.rank - o)}`);
|
|
59
|
-
if (r.rank !== o + e.length - a)
|
|
60
|
-
throw new Error(s + ` update.rank != ${o + e.length - a}`);
|
|
61
|
-
for (let n = 0; n < o; ++n)
|
|
62
|
-
if (r.shape[n] !== t.shape[n])
|
|
63
|
-
throw new Error(s + ` updates.shape[${n}] (${r.shape[n]}) != indices.shape[${n}] (${t.shape[n]}).`);
|
|
64
|
-
for (let n = 0; n < r.rank - o; ++n)
|
|
65
|
-
if (r.shape[n + o] !== e[n + a])
|
|
66
|
-
throw new Error(s + ` updates.shape[${n + o}] (${r.shape[n + o]}) != shape[${n + o}] (${e[n + o]})`);
|
|
67
|
-
}
|
|
68
|
-
function O(e, t, r) {
|
|
69
|
-
if (t.rank < 1)
|
|
70
|
-
throw new Error(`tf.scatterND() expects the indices to be rank 1 or higher, but the rank was ${t.rank}.`);
|
|
71
|
-
if (e.rank < 1)
|
|
72
|
-
throw new Error(`tf.scatterND() expects the updates to be rank 1 or higher, but the rank was ${e.rank}.`);
|
|
73
|
-
if (t.dtype !== "int32")
|
|
74
|
-
throw new Error(`The dtype of 'indices' should be int32, but got dtype: ${t.dtype}`);
|
|
75
|
-
if (r.length < 1)
|
|
76
|
-
throw new Error(`Output rank must be greater or equal to 1, but got shape: ${r}`);
|
|
77
|
-
if (r.length === 0) {
|
|
78
|
-
if (t.size === 0)
|
|
79
|
-
throw new Error(`Indices specified for empty output. indices shape: ${t.shape}`);
|
|
80
|
-
if (e.size === 0)
|
|
81
|
-
throw new Error(`Updates specified for empty output. updates shape: ${e.shape}`);
|
|
82
|
-
}
|
|
83
|
-
C(r, t, e);
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* @license
|
|
87
|
-
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
88
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
89
|
-
* you may not use this file except in compliance with the License.
|
|
90
|
-
* You may obtain a copy of the License at
|
|
91
|
-
*
|
|
92
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
93
|
-
*
|
|
94
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
95
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
96
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
97
|
-
* See the License for the specific language governing permissions and
|
|
98
|
-
* limitations under the License.
|
|
99
|
-
* =============================================================================
|
|
100
|
-
*/
|
|
101
|
-
function z(e, t, r) {
|
|
102
|
-
u(r);
|
|
103
|
-
const a = h(e, "indices", "scatterND", "int32"), o = h(t, "updates", "scatterND");
|
|
104
|
-
O(o, a, r);
|
|
105
|
-
const s = { indices: a, updates: o }, n = { shape: r };
|
|
106
|
-
return f.runKernel(y, s, n);
|
|
107
|
-
}
|
|
108
|
-
const I = /* @__PURE__ */ N({ scatterND_: z });
|
|
109
|
-
class L {
|
|
110
|
-
variableNames = ["labels", "softmaxProbs", "dy"];
|
|
111
|
-
outputShape;
|
|
112
|
-
userCode;
|
|
113
|
-
constructor(t, r) {
|
|
114
|
-
this.outputShape = [t, r], this.userCode = `
|
|
115
|
-
void main() {
|
|
116
|
-
ivec2 coords = getOutputCoords();
|
|
117
|
-
int index = int(getLabels(coords.x));
|
|
118
|
-
float prob = getSoftmaxProbsAtOutCoords();
|
|
119
|
-
float dy = getDy(coords.x);
|
|
120
|
-
setOutput((index == coords.y ? prob - 1.0 : prob) * dy);
|
|
121
|
-
}
|
|
122
|
-
`;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
function P(e) {
|
|
126
|
-
const { logits: t, labels: r, dy: a } = e.inputs, o = e.backend, s = r.shape[0], n = t.shape[1], c = new L(s, n);
|
|
127
|
-
return o.runWebGLProgram(c, [r, t, a], "float32");
|
|
128
|
-
}
|
|
129
|
-
const K = {
|
|
130
|
-
kernelName: "EfficientScatterSub",
|
|
131
|
-
backendName: "webgl",
|
|
132
|
-
kernelFunc: P
|
|
133
|
-
};
|
|
134
|
-
p(K);
|
|
135
|
-
function A(e) {
|
|
136
|
-
const { logits: t, labels: r, dy: a } = e.inputs, o = r.shape[0], s = t.shape[1], n = v(0, o, 1, "int32"), c = T([n, r], 1), b = m([o]), g = I(c, b, [o, s]), k = x(t, g), w = a.reshape([o, 1]);
|
|
137
|
-
return D(k, w);
|
|
138
|
-
}
|
|
139
|
-
const F = {
|
|
140
|
-
kernelName: "EfficientScatterSub",
|
|
141
|
-
backendName: "cpu",
|
|
142
|
-
kernelFunc: A
|
|
143
|
-
};
|
|
144
|
-
p(F);
|
|
145
|
-
function R(e, t, r) {
|
|
146
|
-
return $().runKernel("EfficientScatterSub", { logits: e, labels: t, dy: r }, {});
|
|
1
|
+
import { e as i } from "../index-pWA4_lUh.js";
|
|
2
|
+
import "./cpu/scatterSub.js";
|
|
3
|
+
import "./webgl/scatterSub.js";
|
|
4
|
+
function c(t, r, e) {
|
|
5
|
+
return i().runKernel("EfficientScatterSub", { logits: t, labels: r, dy: e }, {});
|
|
147
6
|
}
|
|
148
7
|
export {
|
|
149
|
-
|
|
8
|
+
c as scatterSub
|
|
150
9
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { r as h } from "../../index-pWA4_lUh.js";
|
|
2
|
+
class m {
|
|
3
|
+
variableNames = ["cache", "item"];
|
|
4
|
+
outputShape;
|
|
5
|
+
userCode;
|
|
6
|
+
customUniforms = [{ name: "cacheT", type: "int" }];
|
|
7
|
+
constructor(t, a, o, s, n) {
|
|
8
|
+
const c = Math.min(o + 1, n);
|
|
9
|
+
this.outputShape = [t, a, c, s], this.userCode = `
|
|
10
|
+
void main() {
|
|
11
|
+
ivec4 coords = getOutputCoords(); // [b, h, t, d]
|
|
12
|
+
int b = coords.x;
|
|
13
|
+
int h = coords.y;
|
|
14
|
+
int t = coords.z;
|
|
15
|
+
int d = coords.w;
|
|
16
|
+
|
|
17
|
+
int itemT = 1;
|
|
18
|
+
int maxSize = ${n};
|
|
19
|
+
int totalT = cacheT + itemT;
|
|
20
|
+
int start = totalT >= maxSize ? 1 : 0;
|
|
21
|
+
|
|
22
|
+
int srcT = t + start;
|
|
23
|
+
float val = 0.0;
|
|
24
|
+
if (srcT < cacheT) {
|
|
25
|
+
val = getCache(b, h, srcT, d);
|
|
26
|
+
} else {
|
|
27
|
+
val = getItem(b, h, 0, d);
|
|
28
|
+
}
|
|
29
|
+
setOutput(val);
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function p(e) {
|
|
35
|
+
const { cache: t, item: a } = e.inputs, { maxSize: o } = e.attrs, s = e.backend, n = t.shape[0], c = t.shape[2], r = t.shape[1], i = new m(n, r, c, a.shape[3], o);
|
|
36
|
+
return s.runWebGLProgram(i, [t, a], "float32", [[c]]);
|
|
37
|
+
}
|
|
38
|
+
const d = {
|
|
39
|
+
kernelName: "AppendCache",
|
|
40
|
+
backendName: "webgl",
|
|
41
|
+
kernelFunc: p
|
|
42
|
+
};
|
|
43
|
+
h(d);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { r as m } from "../../index-pWA4_lUh.js";
|
|
2
|
+
class k {
|
|
3
|
+
variableNames = ["q", "k", "mask"];
|
|
4
|
+
outputShape;
|
|
5
|
+
userCode;
|
|
6
|
+
customUniforms = [{ name: "divisor", type: "float" }];
|
|
7
|
+
constructor(t, a, o, e) {
|
|
8
|
+
this.outputShape = [t, a, o, o], this.userCode = `
|
|
9
|
+
void main() {
|
|
10
|
+
ivec4 coords = getOutputCoords(); // [batch, nh, t1, t2]
|
|
11
|
+
int b = coords.x;
|
|
12
|
+
int h = coords.y;
|
|
13
|
+
int t1 = coords.z;
|
|
14
|
+
int t2 = coords.w;
|
|
15
|
+
|
|
16
|
+
float sum = 0.0;
|
|
17
|
+
for (int i = 0; i < ${e}; ++i) {
|
|
18
|
+
float qv = getQ(b, h, t1, i);
|
|
19
|
+
float kv = getK(b, h, t2, i); // k is transposed on last two dims
|
|
20
|
+
sum += qv * kv;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Scale by divisor
|
|
24
|
+
float scaled = sum * divisor;
|
|
25
|
+
|
|
26
|
+
// Add mask
|
|
27
|
+
float maskVal = getMask(t1, t2); // mask is [T,T]
|
|
28
|
+
|
|
29
|
+
setOutput(scaled + maskVal);
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function l(s) {
|
|
35
|
+
const { q: t, k: a, mask: o } = s.inputs, { divisor: e } = s.attrs, n = s.backend, i = t.shape[0], r = t.shape[2], c = t.shape[1], d = new k(i, c, r, t.shape[3]);
|
|
36
|
+
return n.runWebGLProgram(d, [t, a, o], "float32", [[e]]);
|
|
37
|
+
}
|
|
38
|
+
const u = {
|
|
39
|
+
kernelName: "AttentionMask",
|
|
40
|
+
backendName: "webgl",
|
|
41
|
+
kernelFunc: l
|
|
42
|
+
};
|
|
43
|
+
m(u);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { r as l } from "../../index-pWA4_lUh.js";
|
|
2
|
+
class u {
|
|
3
|
+
variableNames = ["labels", "logits", "values"];
|
|
4
|
+
outputShape;
|
|
5
|
+
userCode;
|
|
6
|
+
constructor(e) {
|
|
7
|
+
this.outputShape = [e], this.userCode = `
|
|
8
|
+
void main() {
|
|
9
|
+
int coords = getOutputCoords();
|
|
10
|
+
int index = int(getLabelsAtOutCoords());
|
|
11
|
+
float val = getValuesAtOutCoords();
|
|
12
|
+
float logit = getLogits(coords, index);
|
|
13
|
+
setOutput(val - logit);
|
|
14
|
+
}
|
|
15
|
+
`;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function i(t) {
|
|
19
|
+
const { logits: e, labels: o, values: s } = t.inputs, a = t.backend, r = o.shape[0], n = new u(r);
|
|
20
|
+
return a.runWebGLProgram(n, [o, e, s], "float32");
|
|
21
|
+
}
|
|
22
|
+
const c = {
|
|
23
|
+
kernelName: "EfficientGatherSub",
|
|
24
|
+
backendName: "webgl",
|
|
25
|
+
kernelFunc: i
|
|
26
|
+
};
|
|
27
|
+
l(c);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { r as i } from "../../index-pWA4_lUh.js";
|
|
2
|
+
class l {
|
|
3
|
+
variableNames = ["x", "kernel"];
|
|
4
|
+
outputShape;
|
|
5
|
+
userCode;
|
|
6
|
+
// enableShapeUniforms = true;
|
|
7
|
+
customUniforms = [{ name: "mode", type: "int" }];
|
|
8
|
+
constructor(e, t, s, o) {
|
|
9
|
+
const n = o / t;
|
|
10
|
+
this.outputShape = [e, t, s, n], this.userCode = `
|
|
11
|
+
void main() {
|
|
12
|
+
ivec4 coords = getOutputCoords(); // [b, h, t, d]
|
|
13
|
+
int b = coords.x;
|
|
14
|
+
int h = coords.y;
|
|
15
|
+
int t = coords.z;
|
|
16
|
+
int d = coords.w;
|
|
17
|
+
|
|
18
|
+
// Compute output channel index in fused kernel
|
|
19
|
+
int out_offset = mode * ${t} * ${n} + h * ${n} + d;
|
|
20
|
+
|
|
21
|
+
float sum = 0.0;
|
|
22
|
+
for (int c = 0; c < ${o}; ++c) {
|
|
23
|
+
float xval = getX(b, t, c); // fetch from x
|
|
24
|
+
float kval = getKernel(c, out_offset); // fetch from kernel
|
|
25
|
+
sum += xval * kval;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
setOutput(sum);
|
|
29
|
+
}
|
|
30
|
+
`;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function m(r) {
|
|
34
|
+
const { x: e, kernel: t } = r.inputs, { heads: s } = r.attrs, o = r.backend, n = e.shape[0], c = e.shape[1], u = e.shape[2], a = new l(n, s, c, u);
|
|
35
|
+
return [
|
|
36
|
+
o.runWebGLProgram(a, [e, t], "float32", [[0]]),
|
|
37
|
+
o.runWebGLProgram(a, [e, t], "float32", [[1]]),
|
|
38
|
+
o.runWebGLProgram(a, [e, t], "float32", [[2]])
|
|
39
|
+
];
|
|
40
|
+
}
|
|
41
|
+
const d = {
|
|
42
|
+
kernelName: "QKV",
|
|
43
|
+
backendName: "webgl",
|
|
44
|
+
kernelFunc: m
|
|
45
|
+
};
|
|
46
|
+
i(d);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|