@genai-fi/nanogpt 0.7.0 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/dist/Generator.js +13 -9
  2. package/dist/NanoGPTModel.js +10 -10
  3. package/dist/{RealDiv-C4hOvYOZ.js → RealDiv-Dy0p8Bvo.js} +11 -11
  4. package/dist/{Reshape-BLijOA8h.js → Reshape-DH5srBP0.js} +2 -2
  5. package/dist/Reshape-DvudQDvJ.js +30 -0
  6. package/dist/TeachableLLM.js +9 -5
  7. package/dist/{TiedEmbedding-BLltddza.js → TiedEmbedding-BxOerUmB.js} +4 -4
  8. package/dist/{axis_util-DaAl5MER.js → axis_util-BzbKo31C.js} +1 -1
  9. package/dist/backend.js +2 -2
  10. package/dist/{backend_util-DWiwsi2N.js → backend_util-TE7aTPhZ.js} +40 -40
  11. package/dist/{broadcast_to-C4v-j9yA.js → broadcast_to-CdbwV-Dj.js} +2 -2
  12. package/dist/{concat-CsHeR4zV.js → concat-CsxrgovM.js} +1 -1
  13. package/dist/{dataset-JDyjG3QR.js → dataset-CtdBYwjo.js} +7 -7
  14. package/dist/{dropout-hpDwECTe.js → dropout-DYs5QFGQ.js} +11 -11
  15. package/dist/{gather-D0_gPiBz.js → gather-CMMy2KEG.js} +4 -4
  16. package/dist/{gelu-uyHP1x1f.js → gelu-C-dPj6Ku.js} +1 -1
  17. package/dist/{gpgpu_math-DJm3ZTAf.js → gpgpu_math-DGNLNL4I.js} +2 -2
  18. package/dist/{index-C0dhsYom.js → index-BoWRt-10.js} +126 -126
  19. package/dist/{index-BPPzKVdR.js → index-CLthM0TO.js} +1083 -1106
  20. package/dist/{kernel_funcs_utils-CwRTFqrc.js → kernel_funcs_utils-BYKWV8Aa.js} +3 -3
  21. package/dist/layers/BaseLayer.js +2 -2
  22. package/dist/layers/CausalSelfAttention.js +8 -8
  23. package/dist/layers/MLP.js +5 -5
  24. package/dist/layers/RMSNorm.js +3 -3
  25. package/dist/layers/RoPECache.js +4 -4
  26. package/dist/layers/TiedEmbedding.js +5 -5
  27. package/dist/layers/TransformerBlock.js +1 -1
  28. package/dist/loader/loadTransformers.js +1 -1
  29. package/dist/loader/oldZipLoad.js +11 -7
  30. package/dist/{log_sum_exp-D086OgZJ.js → log_sum_exp-DbjkV734.js} +8 -8
  31. package/dist/main.d.ts +11 -0
  32. package/dist/main.js +44 -27
  33. package/dist/{mat_mul-1nwdPkQ_.js → mat_mul-8m8pfdcx.js} +1 -1
  34. package/dist/{max-BQc2Aj-I.js → max-Ddnnb5xe.js} +3 -3
  35. package/dist/{mulmat_packed_gpu-Gzf3I9UV.js → mulmat_packed_gpu-VSekgsNv.js} +1 -1
  36. package/dist/{ones-D63HpSF_.js → ones-Dj0SDhHf.js} +2 -2
  37. package/dist/ops/adamAdjust.d.ts +2 -0
  38. package/dist/ops/adamAdjust.js +9 -0
  39. package/dist/ops/adamMoments.d.ts +2 -0
  40. package/dist/ops/adamMoments.js +9 -0
  41. package/dist/ops/appendCache.js +3 -3
  42. package/dist/ops/attentionMask.js +1 -1
  43. package/dist/ops/cpu/adamAdjust.d.ts +1 -0
  44. package/dist/ops/cpu/adamAdjust.js +18 -0
  45. package/dist/ops/cpu/adamMoments.d.ts +1 -0
  46. package/dist/ops/cpu/adamMoments.js +16 -0
  47. package/dist/ops/cpu/appendCache.js +2 -2
  48. package/dist/ops/cpu/attentionMask.js +5 -5
  49. package/dist/ops/cpu/fusedSoftmax.js +2 -2
  50. package/dist/ops/cpu/gatherSub.js +3 -3
  51. package/dist/ops/cpu/gelu.js +1 -1
  52. package/dist/ops/cpu/matMulGelu.js +2 -2
  53. package/dist/ops/cpu/matMulMul.js +1 -1
  54. package/dist/ops/cpu/mulDropout.js +1 -1
  55. package/dist/ops/cpu/normRMS.js +1 -1
  56. package/dist/ops/cpu/qkv.js +3 -3
  57. package/dist/ops/cpu/rope.js +5 -5
  58. package/dist/ops/cpu/scatterSub.js +11 -11
  59. package/dist/ops/fusedSoftmax.js +1 -1
  60. package/dist/ops/gatherSub.js +1 -1
  61. package/dist/ops/gelu.js +2 -2
  62. package/dist/ops/grads/attentionMask.js +1 -1
  63. package/dist/ops/grads/fusedSoftmax.js +2 -2
  64. package/dist/ops/grads/gelu.js +2 -2
  65. package/dist/ops/grads/matMulGelu.js +1 -1
  66. package/dist/ops/grads/normRMS.js +1 -1
  67. package/dist/ops/grads/qkv.js +1 -1
  68. package/dist/ops/grads/rope.js +1 -1
  69. package/dist/ops/matMulGelu.js +1 -1
  70. package/dist/ops/matMulMul.js +1 -1
  71. package/dist/ops/mulDrop.js +1 -1
  72. package/dist/ops/normRMS.js +1 -1
  73. package/dist/ops/qkv.js +1 -1
  74. package/dist/ops/rope.js +4 -4
  75. package/dist/ops/scatterSub.js +1 -1
  76. package/dist/ops/webgl/adamAdjust.d.ts +1 -0
  77. package/dist/ops/webgl/adamAdjust.js +50 -0
  78. package/dist/ops/webgl/adamMoments.d.ts +1 -0
  79. package/dist/ops/webgl/adamMoments.js +40 -0
  80. package/dist/ops/webgl/appendCache.js +1 -1
  81. package/dist/ops/webgl/attentionMask.js +1 -1
  82. package/dist/ops/webgl/fusedSoftmax.js +4 -4
  83. package/dist/ops/webgl/gatherSub.js +8 -8
  84. package/dist/ops/webgl/gelu.js +2 -2
  85. package/dist/ops/webgl/log.js +3 -3
  86. package/dist/ops/webgl/matMulGelu.js +4 -4
  87. package/dist/ops/webgl/matMulMul.js +1 -1
  88. package/dist/ops/webgl/mulDropout.js +1 -1
  89. package/dist/ops/webgl/normRMS.js +2 -2
  90. package/dist/ops/webgl/qkv.js +1 -1
  91. package/dist/ops/webgl/rope.js +1 -1
  92. package/dist/ops/webgl/scatterSub.js +1 -1
  93. package/dist/ops/webgpu/adamAdjust.d.ts +1 -0
  94. package/dist/ops/webgpu/adamAdjust.js +54 -0
  95. package/dist/ops/webgpu/adamMoments.d.ts +1 -0
  96. package/dist/ops/webgpu/adamMoments.js +58 -0
  97. package/dist/ops/webgpu/appendCache.js +22 -18
  98. package/dist/ops/webgpu/attentionMask.js +24 -17
  99. package/dist/ops/webgpu/gatherSub.js +17 -15
  100. package/dist/ops/webgpu/gelu.js +7 -6
  101. package/dist/ops/webgpu/index.js +3 -0
  102. package/dist/ops/webgpu/normRMS.js +35 -101
  103. package/dist/ops/webgpu/normRMSGrad.d.ts +1 -0
  104. package/dist/ops/webgpu/normRMSGrad.js +133 -0
  105. package/dist/ops/webgpu/qkv.js +21 -16
  106. package/dist/ops/webgpu/rope.js +37 -23
  107. package/dist/ops/webgpu/scatterSub.js +16 -13
  108. package/dist/ops/webgpu/utils/reductions.d.ts +9 -0
  109. package/dist/ops/webgpu/utils/reductions.js +68 -0
  110. package/dist/{ops-CIQLNshk.js → ops-BFGCx8Ri.js} +195 -219
  111. package/dist/{random_width-DkYP8W8N.js → random_width-sZORGo5k.js} +22 -21
  112. package/dist/{range-CYzpQY53.js → range-CRuAh-gd.js} +1 -1
  113. package/dist/{reciprocal-_A9yv27J.js → reciprocal-BvGAyKyu.js} +1 -1
  114. package/dist/{register_all_kernels-guvSxp7M.js → register_all_kernels-BwDSRN-f.js} +30 -29
  115. package/dist/{reshape-BMUzc1UY.js → reshape-CdBq1WJ6.js} +3 -3
  116. package/dist/{scatter_nd_util-IRBqKz_b.js → scatter_nd_util-DUstGbU1.js} +1 -1
  117. package/dist/{selu_util-Dt_iuXaq.js → selu_util-BJEXVvjX.js} +41 -41
  118. package/dist/{shared-CDu9S76h.js → shared-B8ztnyEk.js} +6 -6
  119. package/dist/{shared-BNa2q6jD.js → shared-wS99K7_n.js} +1 -1
  120. package/dist/{sin-Cocju-BY.js → sin-BeA3tsEd.js} +6 -6
  121. package/dist/slice-BiOsknYS.js +28 -0
  122. package/dist/{softmax-GPNK3o-U.js → softmax-Bv_6lyMX.js} +3 -3
  123. package/dist/{split-CHzJjxDv.js → split-B-dikLRw.js} +1 -1
  124. package/dist/{stack-Dpgg_1W1.js → stack-B17UN2nn.js} +1 -1
  125. package/dist/{sum-B8wEpKsg.js → sum-66ew2byf.js} +3 -3
  126. package/dist/{tensor-RvZVNmg0.js → tensor-JwS7ZYY6.js} +1 -1
  127. package/dist/{tensor2d-B_kyod7_.js → tensor2d-wxPAnDQy.js} +1 -1
  128. package/dist/training/Adam.d.ts +22 -0
  129. package/dist/training/Adam.js +93 -0
  130. package/dist/training/AdamExt.d.ts +1 -1
  131. package/dist/training/AdamExt.js +13 -12
  132. package/dist/training/DatasetBuilder.js +35 -32
  133. package/dist/training/FullTrainer.js +22 -22
  134. package/dist/training/Trainer.d.ts +1 -1
  135. package/dist/training/Trainer.js +32 -32
  136. package/dist/training/sparseCrossEntropy.d.ts +0 -4
  137. package/dist/training/sparseCrossEntropy.js +7 -7
  138. package/dist/utilities/arrayClose.d.ts +1 -0
  139. package/dist/utilities/arrayClose.js +11 -0
  140. package/dist/utilities/dummy.js +2 -2
  141. package/dist/utilities/generate.js +3 -3
  142. package/dist/utilities/multinomialCPU.js +2 -2
  143. package/dist/utilities/performance.d.ts +1 -1
  144. package/dist/utilities/performance.js +11 -11
  145. package/dist/utilities/profile.js +1 -1
  146. package/dist/utilities/safetensors.js +2 -2
  147. package/dist/utilities/weights.js +2 -2
  148. package/dist/{variable-DXEUOwew.js → variable-BuddVFLa.js} +1 -1
  149. package/dist/{webgpu_util-g13LvDIv.js → webgpu_program-PFzf1hAQ.js} +138 -215
  150. package/dist/webgpu_util-D____QpY.js +80 -0
  151. package/dist/{zeros-DCPCdFGq.js → zeros--BdLQ3oG.js} +4 -4
  152. package/package.json +1 -1
package/dist/ops/gelu.js CHANGED
@@ -1,7 +1,7 @@
1
- import "../index-C0dhsYom.js";
1
+ import "../index-BoWRt-10.js";
2
2
  import "./cpu/gelu.js";
3
3
  import "./webgl/gelu.js";
4
- import { d as e, g as i } from "../gelu-uyHP1x1f.js";
4
+ import { d as e, g as i } from "../gelu-C-dPj6Ku.js";
5
5
  export {
6
6
  e as dGelu,
7
7
  i as gelu
@@ -1,4 +1,4 @@
1
- import { i as m, b as i } from "../../index-C0dhsYom.js";
1
+ import { i as m, b as i } from "../../index-BoWRt-10.js";
2
2
  import { matMulMul as u } from "../matMulMul.js";
3
3
  const p = {
4
4
  kernelName: "AttentionMask",
@@ -1,6 +1,6 @@
1
- import { i as f, a as i, c as l } from "../../index-C0dhsYom.js";
1
+ import { i as f, a as i, c as l } from "../../index-BoWRt-10.js";
2
2
  import { mulDrop as g } from "../mulDrop.js";
3
- import { s as T } from "../../sum-B8wEpKsg.js";
3
+ import { s as T } from "../../sum-66ew2byf.js";
4
4
  const Y = {
5
5
  kernelName: "FusedSoftmax",
6
6
  outputsToSave: [!0],
@@ -1,5 +1,5 @@
1
- import "../../index-C0dhsYom.js";
2
- import { a as e } from "../../gelu-uyHP1x1f.js";
1
+ import "../../index-BoWRt-10.js";
2
+ import { a as e } from "../../gelu-C-dPj6Ku.js";
3
3
  export {
4
4
  e as geluGradConfig
5
5
  };
@@ -1,4 +1,4 @@
1
- import { i as a, e as o } from "../../index-C0dhsYom.js";
1
+ import { i as a, e as o } from "../../index-BoWRt-10.js";
2
2
  function i(e, n, r) {
3
3
  return o().runKernel("MatMulGeluGrad", { dy: e, x: n, kernel: r });
4
4
  }
@@ -1,4 +1,4 @@
1
- import { i as t, e as g } from "../../index-C0dhsYom.js";
1
+ import { i as t, e as g } from "../../index-BoWRt-10.js";
2
2
  function i(r, a, n) {
3
3
  return g().runKernel("RMSNormGrad", { dy: r, x: a, gamma: n });
4
4
  }
@@ -1,4 +1,4 @@
1
- import { i as Q } from "../../index-C0dhsYom.js";
1
+ import { i as Q } from "../../index-BoWRt-10.js";
2
2
  const V = {
3
3
  kernelName: "QKV",
4
4
  inputsToSave: ["x", "kernel"],
@@ -1,4 +1,4 @@
1
- import { i, e as a } from "../../index-C0dhsYom.js";
1
+ import { i, e as a } from "../../index-BoWRt-10.js";
2
2
  function p(n, e, s, o) {
3
3
  return a().runKernel("Rope", { x: n, sin: e, cos: s }, { pastLen: o });
4
4
  }
@@ -1,4 +1,4 @@
1
- import { e as u } from "../index-C0dhsYom.js";
1
+ import { e as u } from "../index-BoWRt-10.js";
2
2
  import "./cpu/matMulGelu.js";
3
3
  import "./webgl/matMulGelu.js";
4
4
  import "./grads/matMulGelu.js";
@@ -1,4 +1,4 @@
1
- import { e as u } from "../index-C0dhsYom.js";
1
+ import { e as u } from "../index-BoWRt-10.js";
2
2
  import "./cpu/matMulMul.js";
3
3
  import "./webgl/matMulMul.js";
4
4
  function m(e, r, t, l = !1, n = !1) {
@@ -1,4 +1,4 @@
1
- import { e as t } from "../index-C0dhsYom.js";
1
+ import { e as t } from "../index-BoWRt-10.js";
2
2
  import "./cpu/mulDropout.js";
3
3
  import "./webgl/mulDropout.js";
4
4
  function m(r, o, e, n) {
@@ -1,4 +1,4 @@
1
- import { e as n } from "../index-C0dhsYom.js";
1
+ import { e as n } from "../index-BoWRt-10.js";
2
2
  import "./cpu/normRMS.js";
3
3
  import "./webgl/normRMS.js";
4
4
  import "./grads/normRMS.js";
package/dist/ops/qkv.js CHANGED
@@ -1,4 +1,4 @@
1
- import { e as o } from "../index-C0dhsYom.js";
1
+ import { e as o } from "../index-BoWRt-10.js";
2
2
  import "./cpu/qkv.js";
3
3
  import "./webgl/qkv.js";
4
4
  import "./grads/qkv.js";
package/dist/ops/rope.js CHANGED
@@ -1,8 +1,8 @@
1
- import { e as p } from "../index-C0dhsYom.js";
2
- import "../random_width-DkYP8W8N.js";
3
- import "../register_all_kernels-guvSxp7M.js";
1
+ import { e as p } from "../index-BoWRt-10.js";
2
+ import "../random_width-sZORGo5k.js";
3
+ import "../register_all_kernels-BwDSRN-f.js";
4
4
  import "../index-Tf7vU29b.js";
5
- import "../dataset-JDyjG3QR.js";
5
+ import "../dataset-CtdBYwjo.js";
6
6
  import "./cpu/rope.js";
7
7
  import "./webgl/rope.js";
8
8
  import "./grads/rope.js";
@@ -1,4 +1,4 @@
1
- import { e as i } from "../index-C0dhsYom.js";
1
+ import { e as i } from "../index-BoWRt-10.js";
2
2
  import "./cpu/scatterSub.js";
3
3
  import "./webgl/scatterSub.js";
4
4
  function c(t, r, e) {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,50 @@
1
+ import { r as n } from "../../Reshape-DH5srBP0.js";
2
+ import { f } from "../../index-BoWRt-10.js";
3
+ class v {
4
+ variableNames = ["moments", "value"];
5
+ outputShape;
6
+ userCode;
7
+ customUniforms = [
8
+ { name: "invBeta1", type: "float" },
9
+ { name: "invBeta2", type: "float" },
10
+ { name: "learningRate", type: "float" },
11
+ { name: "epsilon", type: "float" }
12
+ ];
13
+ constructor(a) {
14
+ this.outputShape = a, this.userCode = `
15
+ void main() {
16
+ float v = getValueAtOutCoords();
17
+ int coords = getOutputCoords();
18
+ coords *= 2;
19
+ float m1 = getMoments(coords);
20
+ float m2 = getMoments(coords + 1);
21
+
22
+ float m1Hat = m1 * invBeta1;
23
+ float m2Hat = m2 * invBeta2;
24
+
25
+ float invSqrt = inversesqrt(max(m2Hat, 1e-30));
26
+ float invDenom = invSqrt / (1.0 + epsilon * invSqrt);
27
+ float adjustedValue = -learningRate * m1Hat * invDenom + v;
28
+
29
+ setOutput(adjustedValue);
30
+ }
31
+ `;
32
+ }
33
+ }
34
+ function h(t) {
35
+ const { moments: a, value: s } = t.inputs, { beta1: i, beta2: l, learningRate: u, epsilon: p } = t.attrs, e = t.backend, r = n({ inputs: { x: a }, backend: e, attrs: { shape: [-1] } }), o = n({ inputs: { x: s }, backend: e, attrs: { shape: [-1] } }), d = new v(o.shape), m = e.runWebGLProgram(d, [r, o], "float32", [
36
+ [1 / i],
37
+ [1 / l],
38
+ [u],
39
+ [p]
40
+ ]);
41
+ e.disposeIntermediateTensorInfo(r), e.disposeIntermediateTensorInfo(o);
42
+ const c = n({ inputs: { x: m }, backend: e, attrs: { shape: s.shape } });
43
+ return e.disposeIntermediateTensorInfo(m), c;
44
+ }
45
+ const g = {
46
+ kernelName: "AdamAdjust",
47
+ backendName: "webgl",
48
+ kernelFunc: h
49
+ };
50
+ f(g);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,40 @@
1
+ import { f as m } from "../../index-BoWRt-10.js";
2
+ class i {
3
+ variableNames = ["moments", "gradient"];
4
+ outputShape;
5
+ userCode;
6
+ customUniforms = [
7
+ { name: "beta1", type: "float" },
8
+ { name: "beta2", type: "float" }
9
+ ];
10
+ constructor(e) {
11
+ this.outputShape = e;
12
+ const t = e.length, n = t === 1 ? "int" : `ivec${Math.min(t, 4)}`, a = t === 1 ? "coords" : `coords[${t - 1}]`, r = t === 1 ? "coords" : e.slice(0, -1).map((s, c) => `coords[${c}]`).join(", ");
13
+ this.userCode = `
14
+ void main() {
15
+ float m = getMomentsAtOutCoords();
16
+ ${n} coords = getOutputCoords();
17
+
18
+ // Add gradient clipping here
19
+ float g = clamp(getGradient(${r}), -1.0, 1.0);
20
+ int which = ${a};
21
+
22
+ float beta = which == 0 ? beta1 : beta2;
23
+ float gg = which == 0 ? g : g * g;
24
+
25
+ float newM = m * beta + gg * (1.0 - beta);
26
+ setOutput(newM);
27
+ }
28
+ `;
29
+ }
30
+ }
31
+ function d(o) {
32
+ const { moments: e, gradient: t } = o.inputs, { beta1: n, beta2: a } = o.attrs, r = o.backend, s = new i(e.shape);
33
+ return r.runWebGLProgram(s, [e, t], "float32", [[n], [a]]);
34
+ }
35
+ const g = {
36
+ kernelName: "AdamMoments",
37
+ backendName: "webgl",
38
+ kernelFunc: d
39
+ };
40
+ m(g);
@@ -1,4 +1,4 @@
1
- import { f as p } from "../../index-C0dhsYom.js";
1
+ import { f as p } from "../../index-BoWRt-10.js";
2
2
  class m {
3
3
  variableNames = ["cache", "item"];
4
4
  outputShape;
@@ -1,4 +1,4 @@
1
- import { f as m } from "../../index-C0dhsYom.js";
1
+ import { f as m } from "../../index-BoWRt-10.js";
2
2
  class h {
3
3
  variableNames = ["q", "k"];
4
4
  outputShape;
@@ -1,7 +1,7 @@
1
- import { m as b, s as I, r as k } from "../../RealDiv-C4hOvYOZ.js";
2
- import { r as v } from "../../Reshape-BLijOA8h.js";
3
- import { f as w, p as P } from "../../index-C0dhsYom.js";
4
- import { e as S } from "../../axis_util-DaAl5MER.js";
1
+ import { m as b, s as I, r as k } from "../../RealDiv-Dy0p8Bvo.js";
2
+ import { r as v } from "../../Reshape-DH5srBP0.js";
3
+ import { f as w, p as P } from "../../index-BoWRt-10.js";
4
+ import { e as S } from "../../axis_util-BzbKo31C.js";
5
5
  class T {
6
6
  variableNames = ["logits", "maxLogits"];
7
7
  outputShape;
@@ -1,17 +1,17 @@
1
- import { f as l } from "../../index-C0dhsYom.js";
1
+ import { f as l } from "../../index-BoWRt-10.js";
2
2
  class u {
3
3
  variableNames = ["labels", "logits", "values"];
4
4
  outputShape;
5
5
  userCode;
6
6
  constructor(e) {
7
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
- }
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
15
  `;
16
16
  }
17
17
  }
@@ -1,5 +1,5 @@
1
- import { f as a } from "../../index-C0dhsYom.js";
2
- import { u as s, C as x } from "../../kernel_funcs_utils-CwRTFqrc.js";
1
+ import { f as a } from "../../index-BoWRt-10.js";
2
+ import { u as s, C as x } from "../../kernel_funcs_utils-BYKWV8Aa.js";
3
3
  const t = 0.7978845608028654, r = 0.044715, c = x + `
4
4
  float x3 = x * x * x;
5
5
  float inner = x + ${r} * x3;
@@ -1,6 +1,6 @@
1
- import { f as e, a8 as r } from "../../index-C0dhsYom.js";
2
- import { u as s, l as N } from "../../kernel_funcs_utils-CwRTFqrc.js";
3
- import { y as l } from "../../shared-BNa2q6jD.js";
1
+ import { f as e, af as r } from "../../index-BoWRt-10.js";
2
+ import { u as s, l as N } from "../../kernel_funcs_utils-BYKWV8Aa.js";
3
+ import { y as l } from "../../shared-wS99K7_n.js";
4
4
  /**
5
5
  * @license
6
6
  * Copyright 2020 Google LLC. All Rights Reserved.
@@ -1,7 +1,7 @@
1
- import { f as _, t as R, e as C, j as A, k as N, l as H, u as O } from "../../index-C0dhsYom.js";
2
- import { r as f } from "../../Reshape-BLijOA8h.js";
3
- import { M as U } from "../../mulmat_packed_gpu-Gzf3I9UV.js";
4
- import { m as E } from "../../mat_mul-1nwdPkQ_.js";
1
+ import { f as _, t as R, e as C, j as A, k as N, l as H, u as O } from "../../index-BoWRt-10.js";
2
+ import { r as f } from "../../Reshape-DH5srBP0.js";
3
+ import { M as U } from "../../mulmat_packed_gpu-VSekgsNv.js";
4
+ import { m as E } from "../../mat_mul-8m8pfdcx.js";
5
5
  const M = 0.7978845608028654, x = 0.044715, j = `
6
6
  vec4 x3 = x * x * x;
7
7
  vec4 inner = x + ${x} * x3;
@@ -1,4 +1,4 @@
1
- import { f as u } from "../../index-C0dhsYom.js";
1
+ import { f as u } from "../../index-BoWRt-10.js";
2
2
  import { batchMatMulGeluImpl as c } from "./matMulGelu.js";
3
3
  const M = `
4
4
  return a * b;
@@ -1,4 +1,4 @@
1
- import { f as m } from "../../index-C0dhsYom.js";
1
+ import { f as m } from "../../index-BoWRt-10.js";
2
2
  class f {
3
3
  variableNames = ["a", "b"];
4
4
  outputShape;
@@ -1,5 +1,5 @@
1
- import { f as p, e as G } from "../../index-C0dhsYom.js";
2
- import { s as x } from "../../sum-B8wEpKsg.js";
1
+ import { f as p, e as G } from "../../index-BoWRt-10.js";
2
+ import { s as x } from "../../sum-66ew2byf.js";
3
3
  class y {
4
4
  variableNames = ["x", "meanSquare", "gamma"];
5
5
  outputShape;
@@ -1,4 +1,4 @@
1
- import { f as i } from "../../index-C0dhsYom.js";
1
+ import { f as i } from "../../index-BoWRt-10.js";
2
2
  class l {
3
3
  variableNames = ["x", "kernel"];
4
4
  outputShape;
@@ -1,4 +1,4 @@
1
- import { f as u } from "../../index-C0dhsYom.js";
1
+ import { f as u } from "../../index-BoWRt-10.js";
2
2
  class l {
3
3
  variableNames = ["x", "sin", "cos"];
4
4
  outputShape;
@@ -1,4 +1,4 @@
1
- import { f as i } from "../../index-C0dhsYom.js";
1
+ import { f as i } from "../../index-BoWRt-10.js";
2
2
  class u {
3
3
  variableNames = ["labels", "softmaxProbs", "dy"];
4
4
  outputShape;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,54 @@
1
+ import { g as p } from "../../webgpu_program-PFzf1hAQ.js";
2
+ import { f as d, c as l } from "../../webgpu_util-D____QpY.js";
3
+ import { f, a2 as c } from "../../index-BoWRt-10.js";
4
+ class h {
5
+ variableNames = ["moments", "value"];
6
+ outputShape;
7
+ shaderKey = "AdamAdjust";
8
+ dispatchLayout;
9
+ dispatch;
10
+ workgroupSize = [64, 1, 1];
11
+ size = !0;
12
+ uniforms = "invbeta1: f32, invbeta2: f32, learningRate: f32, epsilon: f32";
13
+ outputComponent = 1;
14
+ variableComponents = [2, 1];
15
+ constructor(e) {
16
+ this.outputShape = e, this.dispatchLayout = d(this.outputShape), this.dispatch = l(this.dispatchLayout, this.outputShape, this.workgroupSize);
17
+ }
18
+ getUserCode() {
19
+ return `
20
+ ${p("index")} {
21
+ if (index < uniforms.size) {
22
+ let moments: vec2<f32> = moments[index];
23
+ let value: f32 = value[index];
24
+
25
+ let m1Hat = moments.x * uniforms.invbeta1;
26
+ let m2Hat = moments.y * uniforms.invbeta2;
27
+
28
+ let invSqrt = inverseSqrt(max(m2Hat, 1e-30));
29
+ let invDenom = invSqrt / fma(uniforms.epsilon, invSqrt, 1.0);
30
+ let adjustedValue = fma(-uniforms.learningRate * m1Hat, invDenom, value);
31
+
32
+ setOutputAtIndex(index, adjustedValue);
33
+ }
34
+ }
35
+ `;
36
+ }
37
+ }
38
+ function v(t) {
39
+ const { moments: e, value: a } = t.inputs, { beta1: n, beta2: s, learningRate: o, epsilon: i } = t.attrs, r = t.backend;
40
+ c(e.shape, [...a.shape, 2], "Error in AdamAdjust: ");
41
+ const u = new h(a.shape), m = [
42
+ { type: "float32", data: [1 / n] },
43
+ { type: "float32", data: [1 / s] },
44
+ { type: "float32", data: [o] },
45
+ { type: "float32", data: [i] }
46
+ ];
47
+ return r.runWebGPUProgram(u, [e, a], "float32", m);
48
+ }
49
+ const b = {
50
+ kernelName: "AdamAdjust",
51
+ backendName: "webgpu",
52
+ kernelFunc: v
53
+ };
54
+ f(b);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,58 @@
1
+ import { g as m } from "../../webgpu_program-PFzf1hAQ.js";
2
+ import { f as u, c as p } from "../../webgpu_util-D____QpY.js";
3
+ import { f as d, a2 as c } from "../../index-BoWRt-10.js";
4
+ class h {
5
+ variableNames = ["moments", "gradient"];
6
+ outputShape;
7
+ shaderKey = "AdamMoments";
8
+ dispatchLayout;
9
+ dispatch;
10
+ workgroupSize = [64, 1, 1];
11
+ size = !0;
12
+ uniforms = "beta1: f32, beta2: f32";
13
+ outputComponent = 2;
14
+ variableComponents = [2, 1];
15
+ constructor(e) {
16
+ this.outputShape = e, this.dispatchLayout = u(this.outputShape.slice(0, -1)), this.dispatch = p(
17
+ this.dispatchLayout,
18
+ this.outputShape.slice(0, -1),
19
+ this.workgroupSize,
20
+ [1, 1, 1]
21
+ );
22
+ }
23
+ getUserCode() {
24
+ return `
25
+ ${m("index")} {
26
+ if (index < uniforms.size) {
27
+ let m: vec2<f32> = moments[index];
28
+
29
+ // Add gradient clipping here
30
+ let g: f32 = clamp(gradient[index], -1.0, 1.0);
31
+
32
+ let newM1 = fma(m.x, uniforms.beta1, g * (1.0 - uniforms.beta1));
33
+ let newM2 = fma(m.y, uniforms.beta2, g * g * (1.0 - uniforms.beta2));
34
+
35
+ setOutputAtIndex(index, vec2<f32>(newM1, newM2));
36
+ }
37
+ }
38
+ `;
39
+ }
40
+ }
41
+ function f(t) {
42
+ const { moments: e, gradient: o } = t.inputs, { beta1: a, beta2: n } = t.attrs, r = t.backend;
43
+ if (c(e.shape, [...o.shape, 2], "Error in AdamMoments: "), a < 0 || a >= 1)
44
+ throw new Error(`Invalid beta1 value: ${a}. Must be in the range [0, 1).`);
45
+ if (n < 0 || n >= 1)
46
+ throw new Error(`Invalid beta2 value: ${n}. Must be in the range [0, 1).`);
47
+ const s = new h(e.shape), i = [
48
+ { type: "float32", data: [a] },
49
+ { type: "float32", data: [n] }
50
+ ];
51
+ return r.runWebGPUProgram(s, [e, o], "float32", i);
52
+ }
53
+ const l = {
54
+ kernelName: "AdamMoments",
55
+ backendName: "webgpu",
56
+ kernelFunc: f
57
+ };
58
+ d(l);
@@ -1,6 +1,7 @@
1
- import { f as u, c as d, g as l } from "../../webgpu_util-g13LvDIv.js";
2
- import { f as m } from "../../index-C0dhsYom.js";
3
- class f {
1
+ import { g as u } from "../../webgpu_program-PFzf1hAQ.js";
2
+ import { f as d, c as m } from "../../webgpu_util-D____QpY.js";
3
+ import { f as l, a2 as f } from "../../index-BoWRt-10.js";
4
+ class g {
4
5
  variableNames = ["cache", "item"];
5
6
  outputShape;
6
7
  shaderKey = "AppendCache";
@@ -9,14 +10,14 @@ class f {
9
10
  workgroupSize = [64, 1, 1];
10
11
  size = !0;
11
12
  uniforms = "cacheT: i32";
12
- constructor(t, a, s, o, c) {
13
- const i = Math.min(s + 1, c);
14
- this.outputShape = [t, a, i, o], this.dispatchLayout = u(this.outputShape), this.dispatch = d(this.dispatchLayout, this.outputShape, this.workgroupSize);
13
+ constructor(e, t, s, a, c) {
14
+ const o = Math.min(s + 1, c);
15
+ this.shaderKey = `AppendCache_${o}`, this.outputShape = [e, t, o, a], this.dispatchLayout = d(this.outputShape), this.dispatch = m(this.dispatchLayout, this.outputShape, this.workgroupSize);
15
16
  }
16
17
  getUserCode() {
17
- const t = this.outputShape[2];
18
+ const e = this.outputShape[2];
18
19
  return `
19
- ${l("index")} {
20
+ ${u("index")} {
20
21
  if (index < uniforms.size) {
21
22
  let coords = getCoordsFromIndex(index); // [b, h, t, d]
22
23
  let b = coords[0];
@@ -25,7 +26,7 @@ class f {
25
26
  let d = coords[3];
26
27
 
27
28
  let itemT = 1;
28
- let maxSize = ${t};
29
+ let maxSize = ${e};
29
30
  let totalT = uniforms.cacheT + itemT;
30
31
  let start = select(0, 1, totalT >= maxSize);
31
32
 
@@ -33,24 +34,27 @@ class f {
33
34
  var val = 0.0;
34
35
  if (srcT < uniforms.cacheT) {
35
36
  val = getCache(b, h, srcT, d);
36
- } else if (srcT == uniforms.cacheT) {
37
+ }
38
+ if (srcT == uniforms.cacheT) {
37
39
  val = getItem(b, h, 0, d);
38
- } else {
39
- val = 0.0;
40
40
  }
41
+
41
42
  setOutputAtIndex(index, val);
42
43
  }
43
44
  }
44
45
  `;
45
46
  }
46
47
  }
47
- function T(e) {
48
- const { cache: t, item: a } = e.inputs, { maxSize: s, pastLen: o } = e.attrs, c = e.backend, i = t.shape[0], r = t.shape[2], n = t.shape[1], h = new f(i, n, r, a.shape[3], s), p = [{ type: "int32", data: [o] }];
49
- return c.runWebGPUProgram(h, [t, a], "float32", p);
48
+ function S(r) {
49
+ const { cache: e, item: t } = r.inputs, { maxSize: s, pastLen: a } = r.attrs, c = r.backend, o = e.shape[0], n = e.shape[2], i = e.shape[1];
50
+ if (f(t.shape, [o, i, 1, t.shape[3]], "Error in AppendCache: "), a < 0 || a > s)
51
+ throw new Error(`Invalid pastLen value: ${a}. Must be in the range [0, ${s}].`);
52
+ const h = new g(o, i, n, t.shape[3], s), p = [{ type: "int32", data: [a] }];
53
+ return c.runWebGPUProgram(h, [e, t], "float32", p);
50
54
  }
51
- const g = {
55
+ const T = {
52
56
  kernelName: "AppendCache",
53
57
  backendName: "webgpu",
54
- kernelFunc: T
58
+ kernelFunc: S
55
59
  };
56
- m(g);
60
+ l(T);
@@ -1,5 +1,6 @@
1
- import { f } from "../../index-C0dhsYom.js";
2
- import { f as m, c as k, g as l } from "../../webgpu_util-g13LvDIv.js";
1
+ import { f, a2 as m } from "../../index-BoWRt-10.js";
2
+ import { g as k } from "../../webgpu_program-PFzf1hAQ.js";
3
+ import { f as l, c as v } from "../../webgpu_util-D____QpY.js";
3
4
  class g {
4
5
  variableNames = ["q", "k"];
5
6
  outputShape;
@@ -13,13 +14,13 @@ class g {
13
14
  nh;
14
15
  T1;
15
16
  T2;
16
- constructor(t, e, o, i, a) {
17
- if (this.outputShape = [t, e, o, i], this.hs = a, this.nh = e, this.T1 = o, this.T2 = i, this.dispatchLayout = m(this.outputShape), this.dispatch = k(this.dispatchLayout, this.outputShape, this.workgroupSize), a % 4 !== 0)
17
+ constructor(t, e, s, o, i) {
18
+ if (this.shaderKey = `AttentionMask_${i}`, this.outputShape = [t, e, s, o], this.hs = i, this.nh = e, this.T1 = s, this.T2 = o, this.dispatchLayout = l(this.outputShape), this.dispatch = v(this.dispatchLayout, this.outputShape, this.workgroupSize), i % 4 !== 0)
18
19
  throw new Error("Head size must be a multiple of 4 for AttentionMaskProgram");
19
20
  }
20
21
  getUserCode() {
21
22
  return `
22
- ${l("index")} {
23
+ ${k("index")} {
23
24
 
24
25
  let coords = getCoordsFromIndex(index);
25
26
  let b = coords[0];
@@ -32,13 +33,14 @@ class g {
32
33
  setOutputAtIndex(index, uniforms.inf);
33
34
  return;
34
35
  }
36
+
37
+ let q0 = getIndexFromCoords4D(vec4<i32>(b, h, t1, 0), uniforms.qShape);
38
+ let k0 = getIndexFromCoords4D(vec4<i32>(b, h, t2, 0), uniforms.kShape);
35
39
 
36
40
  var sum: f32 = 0.0;
37
41
  for (var i: i32 = 0; i < ${this.hs}; i = i + 4) {
38
- let q0 = getIndexFromCoords4D(vec4<i32>(b, h, t1, i), uniforms.qShape);
39
- let qv = vec4<f32>(q[q0], q[q0 + 1], q[q0 + 2], q[q0 + 3]);
40
- let k0 = getIndexFromCoords4D(vec4<i32>(b, h, t2, i), uniforms.kShape);
41
- let kv = vec4<f32>(k[k0], k[k0 + 1], k[k0 + 2], k[k0 + 3]);
42
+ let qv = vec4<f32>(q[q0 + i], q[q0 + i + 1], q[q0 + i + 2], q[q0 + i + 3]);
43
+ let kv = vec4<f32>(k[k0 + i], k[k0 + i + 1], k[k0 + i + 2], k[k0 + i + 3]);
42
44
  sum = sum + dot(qv, kv);
43
45
  }
44
46
  let scaled = sum * uniforms.divisor;
@@ -48,17 +50,22 @@ class g {
48
50
  `;
49
51
  }
50
52
  }
51
- function q(s) {
52
- const { q: t, k: e } = s.inputs, { divisor: o, pastLen: i } = s.attrs, a = s.backend, n = t.shape[0], r = t.shape[2], u = e.shape[2], c = t.shape[1], d = t.shape[3], h = new g(n, c, r, u, d), p = [
53
- { type: "float32", data: [o] },
54
- { type: "int32", data: [i] },
53
+ function b(n) {
54
+ const { q: t, k: e } = n.inputs, { divisor: s, pastLen: o } = n.attrs, i = n.backend, r = t.shape[0], p = t.shape[2], a = e.shape[2], u = t.shape[1], h = t.shape[3];
55
+ if (m(e.shape, [r, u, a, h], "Error in AttentionMask: "), s === 0)
56
+ throw new Error("Divisor must be non-zero in AttentionMask");
57
+ if (o < 0)
58
+ throw new Error("pastLen must be non-negative in AttentionMask");
59
+ const c = new g(r, u, p, a, h), d = [
60
+ { type: "float32", data: [s] },
61
+ { type: "int32", data: [o] },
55
62
  { type: "float32", data: [Number.NEGATIVE_INFINITY] }
56
63
  ];
57
- return a.runWebGPUProgram(h, [t, e], "float32", p);
64
+ return i.runWebGPUProgram(c, [t, e], "float32", d);
58
65
  }
59
- const v = {
66
+ const q = {
60
67
  kernelName: "AttentionMask",
61
68
  backendName: "webgpu",
62
- kernelFunc: q
69
+ kernelFunc: b
63
70
  };
64
- f(v);
71
+ f(q);