@genai-fi/nanogpt 0.2.11 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/dist/Generator.js +30 -25
  2. package/dist/NanoGPTModel.d.ts +13 -14
  3. package/dist/NanoGPTModel.js +167 -85
  4. package/dist/TeachableLLM.d.ts +3 -5
  5. package/dist/TeachableLLM.js +47 -35
  6. package/dist/Trainer.js +8 -8
  7. package/dist/concat-BIZS_td9.js +33 -0
  8. package/dist/data/parquet.js +1 -1
  9. package/dist/exports_layers-7idKoYqh.js +25 -0
  10. package/dist/{sum-D7fu15XL.js → gather-BPGW8RsB.js} +6 -8
  11. package/dist/index-C4L8Cm77.js +349 -0
  12. package/dist/{index-YPKosni4.js → index-pWA4_lUh.js} +1020 -782
  13. package/dist/layers/CausalSelfAttention.d.ts +11 -11
  14. package/dist/layers/CausalSelfAttention.js +71 -63
  15. package/dist/layers/MLP.d.ts +6 -7
  16. package/dist/layers/MLP.js +18 -16
  17. package/dist/layers/RMSNorm.d.ts +6 -7
  18. package/dist/layers/RMSNorm.js +15 -13
  19. package/dist/layers/RoPECache.d.ts +4 -6
  20. package/dist/layers/RoPECache.js +36 -23
  21. package/dist/layers/TiedEmbedding.d.ts +7 -8
  22. package/dist/layers/TiedEmbedding.js +16 -418
  23. package/dist/layers/TransformerBlock.d.ts +8 -9
  24. package/dist/layers/TransformerBlock.js +12 -12
  25. package/dist/main.d.ts +1 -0
  26. package/dist/main.js +35 -21
  27. package/dist/{mat_mul-Bu7bhLms.js → mat_mul-D7_a4KJn.js} +5 -5
  28. package/dist/moments-DfcpfwKi.js +132 -0
  29. package/dist/ones-Cog-G2ag.js +29 -0
  30. package/dist/ops/appendCache.d.ts +2 -0
  31. package/dist/ops/appendCache.js +9 -0
  32. package/dist/ops/attentionMask.d.ts +1 -1
  33. package/dist/ops/attentionMask.js +7 -85
  34. package/dist/ops/cpu/appendCache.d.ts +2 -0
  35. package/dist/ops/cpu/appendCache.js +28 -0
  36. package/dist/ops/cpu/attentionMask.js +18 -0
  37. package/dist/ops/cpu/gatherSub.d.ts +1 -0
  38. package/dist/ops/cpu/gatherSub.js +34 -0
  39. package/dist/ops/cpu/qkv.d.ts +5 -0
  40. package/dist/ops/cpu/qkv.js +38 -0
  41. package/dist/ops/cpu/rope.d.ts +6 -0
  42. package/dist/ops/cpu/rope.js +38 -0
  43. package/dist/ops/cpu/scatterSub.d.ts +1 -0
  44. package/dist/ops/cpu/scatterSub.js +70 -0
  45. package/dist/ops/gatherSub.d.ts +1 -1
  46. package/dist/ops/gatherSub.js +6 -63
  47. package/dist/ops/grads/attentionMask.d.ts +1 -0
  48. package/dist/ops/grads/attentionMask.js +21 -0
  49. package/dist/ops/grads/qkv.d.ts +1 -0
  50. package/dist/ops/grads/qkv.js +20 -0
  51. package/dist/ops/grads/rope.d.ts +1 -0
  52. package/dist/ops/grads/rope.js +14 -0
  53. package/dist/ops/node/sparseCrossEntropy.js +1 -1
  54. package/dist/ops/qkv.d.ts +1 -6
  55. package/dist/ops/qkv.js +7 -124
  56. package/dist/ops/rope.d.ts +0 -5
  57. package/dist/ops/rope.js +7 -150
  58. package/dist/ops/scatterSub.d.ts +1 -1
  59. package/dist/ops/scatterSub.js +6 -147
  60. package/dist/ops/webgl/appendCache.d.ts +1 -0
  61. package/dist/ops/webgl/appendCache.js +43 -0
  62. package/dist/ops/webgl/attentionMask.d.ts +1 -0
  63. package/dist/ops/webgl/attentionMask.js +43 -0
  64. package/dist/ops/webgl/gatherSub.d.ts +1 -0
  65. package/dist/ops/webgl/gatherSub.js +27 -0
  66. package/dist/ops/webgl/qkv.d.ts +1 -0
  67. package/dist/ops/webgl/qkv.js +46 -0
  68. package/dist/ops/webgl/rope.d.ts +1 -0
  69. package/dist/ops/webgl/rope.js +56 -0
  70. package/dist/ops/webgl/scatterSub.d.ts +1 -0
  71. package/dist/ops/webgl/scatterSub.js +27 -0
  72. package/dist/{parquet-BRl5lE_I.js → parquet-C0Tlmv9c.js} +3045 -3048
  73. package/dist/random_width-PbCt7RXv.js +15489 -0
  74. package/dist/range-CcDl05lo.js +26 -0
  75. package/dist/{reshape-DmnmKT6r.js → reshape-C8CR_Bad.js} +3 -3
  76. package/dist/sin-BJIrfnj7.js +47 -0
  77. package/dist/softmax-Be_lsqUc.js +105 -0
  78. package/dist/{complex-CJ-qCcLB.js → split-DZbvruEP.js} +6 -8
  79. package/dist/stack-BMm-efee.js +27 -0
  80. package/dist/sum-C7Mgy9Bw.js +104 -0
  81. package/dist/tensor-DJVbYhh1.js +24 -0
  82. package/dist/tensor2d-ZuQSh2D-.js +30 -0
  83. package/dist/tokeniser/bpe.d.ts +17 -6
  84. package/dist/tokeniser/bpe.js +88 -60
  85. package/dist/training/AdamExt.js +1 -1
  86. package/dist/training/DatasetBuilder.d.ts +6 -6
  87. package/dist/training/DatasetBuilder.js +1262 -17
  88. package/dist/training/Evaluator.d.ts +3 -2
  89. package/dist/training/FullTrainer.d.ts +9 -8
  90. package/dist/training/FullTrainer.js +26 -25
  91. package/dist/training/LayerTrainer.d.ts +9 -8
  92. package/dist/training/LayerTrainer.js +34 -33
  93. package/dist/training/Trainer.d.ts +22 -21
  94. package/dist/training/Trainer.js +21 -18
  95. package/dist/training/sparseCrossEntropy.js +22 -166
  96. package/dist/utilities/dummy.js +10 -8
  97. package/dist/utilities/generate.js +14 -11
  98. package/dist/utilities/load.d.ts +1 -2
  99. package/dist/utilities/load.js +37 -35
  100. package/dist/utilities/profile.js +1 -1
  101. package/dist/utilities/save.js +14 -9
  102. package/dist/utilities/tokenParse.d.ts +1 -1
  103. package/dist/utilities/tokenParse.js +7 -61
  104. package/dist/utilities/weights.d.ts +3 -3
  105. package/dist/utilities/weights.js +21 -19
  106. package/dist/variable-Dl_ub3pk.js +23 -0
  107. package/dist/{stack-BtKpB0Ry.js → zeros-CCy9C3uU.js} +18 -16
  108. package/package.json +2 -1
  109. package/dist/assets/worker-BYeSPNkq.js +0 -1
  110. package/dist/tokeniser/NodeTokeniser.d.ts +0 -20
  111. package/dist/tokeniser/NodeTokeniser.js +0 -46
  112. package/dist/tokeniser/WebTokeniser.d.ts +0 -18
  113. package/dist/tokeniser/WebTokeniser.js +0 -96
  114. package/dist/tokeniser/worker.js +0 -53
  115. /package/dist/{tokeniser/worker.d.ts → ops/cpu/attentionMask.d.ts} +0 -0
@@ -0,0 +1,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);
@@ -1,4 +1,4 @@
1
- import { r as o } from "../../index-YPKosni4.js";
1
+ import { r as o } from "../../index-pWA4_lUh.js";
2
2
  function r(e) {
3
3
  const { logits: t, labels: n } = e.inputs;
4
4
  return e.backend.executeMultipleOutputs("SparseSoftmaxCrossEntropyWithLogits", [], [t, n], 2);
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 { engine as C } from "@tensorflow/tfjs";
2
- import { o as N, d as V, E as Q, i as T, r as b, c as G } from "../index-YPKosni4.js";
3
- import { r as x } from "../reshape-DmnmKT6r.js";
4
- /**
5
- * @license
6
- * Copyright 2020 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(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
- y as qkv,
126
- q as qkvCPU
9
+ u as qkv
127
10
  };
@@ -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,153 +1,10 @@
1
- import { engine as D } from "@tensorflow/tfjs";
2
- import { o as G, l as F, k as _, n as z, E as K, p as O, d as T, q as U, r as g, c as A } from "../index-YPKosni4.js";
3
- import { r as $, s as B } from "../stack-BtKpB0Ry.js";
4
- /**
5
- * @license
6
- * Copyright 2020 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 W(e, t = 0) {
21
- F(e.length >= 1, () => "Pass at least one tensor to concat");
22
- const o = _(e, "tensors", "concat", "string_or_numeric");
23
- if (o[0].dtype === "complex64" && o.forEach((s) => {
24
- if (s.dtype !== "complex64")
25
- throw new Error(`Cannot concatenate complex64 tensors with a tensor
26
- with dtype ${s.dtype}. `);
27
- }), o.length === 1)
28
- return z(o[0]);
29
- const n = o, r = { axis: t };
30
- return K.runKernel(O, n, r);
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(e, t, o = 0, n = 0) {
50
- const r = T(e, "x", "gather"), s = T(t, "indices", "gather", "int32"), c = { x: r, indices: s }, a = { axis: o, batchDims: n };
51
- return K.runKernel(U, 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
- constructor(t, o, n, r, s) {
60
- this.outputShape = [t, o, n, r], this.userCode = `
61
- void main() {
62
- ivec4 coords = getOutputCoords(); // [b, h, t, d]
63
- int b = coords.x;
64
- int h = coords.y;
65
- int t = coords.z;
66
- int d = coords.w;
67
-
68
- int rotaryDim = ${r};
69
-
70
- float outVal = 0.0;
71
-
72
- if (d < rotaryDim) {
73
- int pairIdx = d / 2;
74
- float cos = getCos(t + ${s}, pairIdx, 0);
75
- float sin = getSin(t + ${s}, pairIdx, 0);
76
-
77
- if (d % 2 == 0) {
78
- // even index
79
- float even = getX(b, h, t, d);
80
- float odd = getX(b, h, t, d + 1);
81
- outVal = even * cos - odd * sin;
82
- } else {
83
- // odd index
84
- float even = getX(b, h, t, d - 1);
85
- float odd = getX(b, h, t, d);
86
- outVal = even * sin + odd * cos;
87
- }
88
- } else {
89
- // pass through for non-rotary dims
90
- outVal = getX(b, h, t, d);
91
- }
92
-
93
- setOutput(outVal);
94
- }
95
- `;
96
- }
97
- }
98
- function M(e) {
99
- const { x: t, sin: o, cos: n } = e.inputs, { pastLen: r } = e.attrs, s = e.backend, c = t.shape[0], a = t.shape[1], i = t.shape[2], d = t.shape[3], p = new J(c, a, i, d, r);
100
- return s.runWebGLProgram(p, [t, o, n], "float32");
101
- }
102
- const Q = {
103
- kernelName: "Rope",
104
- backendName: "webgl",
105
- kernelFunc: M
106
- };
107
- g(Q);
108
- function V(e, t, o, n, r) {
109
- const s = n.shape[3], c = o;
110
- if (c > s) return n;
111
- const a = n.shape[2], i = c / 2, d = t.slice([r, 0, 0], [a, i, 1]).reshape([1, 1, a, i]), p = e.slice([r, 0, 0], [a, i, 1]).reshape([1, 1, a, i]), u = n.shape[0], l = n.shape[1], m = $(0, c, 2, "int32"), x = $(1, c, 2, "int32"), X = ((b) => {
112
- const v = b.slice([0, 0, 0, 0], [u, l, a, c]), k = c < s ? b.slice([0, 0, 0, c], [u, l, a, s - c]) : null, h = E(v, m, 3), f = E(v, x, 3), C = h.mul(d), y = f.mul(p), R = C.sub(y), N = f.mul(d), S = h.mul(p), w = N.add(S);
113
- h.dispose(), f.dispose(), d.dispose(), p.dispose(), C.dispose(), y.dispose(), N.dispose(), S.dispose();
114
- const P = B([R, w], -1);
115
- R.dispose(), w.dispose();
116
- const I = P.reshape([u, l, a, c]);
117
- return P.dispose(), k ? j([I, k], 3) : I;
118
- })(n);
119
- return m.dispose(), x.dispose(), X;
120
- }
121
- function L(e) {
122
- const { x: t, sin: o, cos: n } = e.inputs, { pastLen: r } = e.attrs, s = t.shape[3];
123
- return V(o, n, s, t, r);
124
- }
125
- const Y = {
126
- kernelName: "Rope",
127
- backendName: "cpu",
128
- kernelFunc: L
129
- };
130
- g(Y);
131
- const Z = {
132
- kernelName: "Rope",
133
- backendName: "tensorflow",
134
- kernelFunc: L
135
- };
136
- g(Z);
137
- function st(e, t, o) {
138
- return t.ensureRopeCache(e.shape[1]), D().runKernel("Rope", { x: e, sin: t.getSin(), cos: t.getCos() }, { pastLen: o });
139
- }
140
- const q = {
141
- kernelName: "Rope",
142
- inputsToSave: ["x", "sin", "cos"],
143
- outputsToSave: [],
144
- gradFunc: (e, t) => {
145
- const [o, n, r] = t, s = n.neg(), c = o.shape[3], i = V(s, r, c, e, 0);
146
- return s.dispose(), { x: () => i };
147
- }
148
- };
149
- A(q);
150
8
  export {
151
- st as rope,
152
- L as ropeCPU
9
+ s as rope
153
10
  };
@@ -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;
@@ -1,150 +1,9 @@
1
- import { engine as $ } from "@tensorflow/tfjs";
2
- import { t as u, u as S, v as l, E as f, w as E, o as N, d as h, x as y, r as p, b as x, a as D } from "../index-YPKosni4.js";
3
- import { c as d } from "../complex-CJ-qCcLB.js";
4
- import { r as v, s as T } from "../stack-BtKpB0Ry.js";
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
- R as scatterSub
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 {};