@genai-fi/nanogpt 0.7.1 → 0.7.3

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 (138) hide show
  1. package/dist/Generator.d.ts +11 -2
  2. package/dist/Generator.js +81 -68
  3. package/dist/NanoGPTModel.js +8 -8
  4. package/dist/{RealDiv-CVYNbZxu.js → RealDiv-Dy0p8Bvo.js} +7 -7
  5. package/dist/{Reshape-CEsEp0AI.js → Reshape-DH5srBP0.js} +2 -2
  6. package/dist/{Reshape-Do18N3gO.js → Reshape-DvudQDvJ.js} +1 -1
  7. package/dist/TeachableLLM.js +33 -32
  8. package/dist/{TiedEmbedding-ccLBFiZi.js → TiedEmbedding-BxOerUmB.js} +4 -4
  9. package/dist/Trainer.d.ts +6 -1
  10. package/dist/Trainer.js +53 -19
  11. package/dist/{axis_util-5DTW2tFV.js → axis_util-BzbKo31C.js} +1 -1
  12. package/dist/backend.js +2 -2
  13. package/dist/{backend_util-C9Ut8n0Q.js → backend_util-TE7aTPhZ.js} +4 -4
  14. package/dist/{broadcast_to-Ba9h_8DO.js → broadcast_to-CdbwV-Dj.js} +2 -2
  15. package/dist/{concat-CbXTetof.js → concat-CsxrgovM.js} +1 -1
  16. package/dist/{dataset-U3PrjwgU.js → dataset-CtdBYwjo.js} +3 -3
  17. package/dist/{dropout-DPfPgWWe.js → dropout-DYs5QFGQ.js} +1 -1
  18. package/dist/{gather-Bbh8DHhM.js → gather-CMMy2KEG.js} +1 -1
  19. package/dist/{gelu-BFwVnd1r.js → gelu-C-dPj6Ku.js} +1 -1
  20. package/dist/{gpgpu_math-DffelNS-.js → gpgpu_math-DGNLNL4I.js} +2 -2
  21. package/dist/{index-UdZhlibC.js → index-BoWRt-10.js} +4 -4
  22. package/dist/{index-DYD_yPa-.js → index-CLthM0TO.js} +10 -10
  23. package/dist/{kernel_funcs_utils-CXDy3EN7.js → kernel_funcs_utils-BYKWV8Aa.js} +3 -3
  24. package/dist/layers/BaseLayer.js +2 -2
  25. package/dist/layers/CausalSelfAttention.js +6 -6
  26. package/dist/layers/MLP.js +5 -5
  27. package/dist/layers/RMSNorm.js +3 -3
  28. package/dist/layers/RoPECache.js +4 -4
  29. package/dist/layers/TiedEmbedding.js +5 -5
  30. package/dist/layers/TransformerBlock.js +1 -1
  31. package/dist/loader/loadTransformers.js +1 -1
  32. package/dist/loader/oldZipLoad.js +5 -5
  33. package/dist/{log_sum_exp-BnmCkHWl.js → log_sum_exp-DbjkV734.js} +5 -5
  34. package/dist/main.js +5 -5
  35. package/dist/{mat_mul-dwmZz69e.js → mat_mul-8m8pfdcx.js} +1 -1
  36. package/dist/{max-ByjEGoFx.js → max-Ddnnb5xe.js} +1 -1
  37. package/dist/{mulmat_packed_gpu-IGPBp6h9.js → mulmat_packed_gpu-VSekgsNv.js} +1 -1
  38. package/dist/{ones-C8Mfln6-.js → ones-Dj0SDhHf.js} +2 -2
  39. package/dist/ops/adamAdjust.js +1 -1
  40. package/dist/ops/adamMoments.js +1 -1
  41. package/dist/ops/appendCache.js +3 -3
  42. package/dist/ops/attentionMask.js +1 -1
  43. package/dist/ops/cpu/adamAdjust.js +1 -1
  44. package/dist/ops/cpu/adamMoments.js +2 -2
  45. package/dist/ops/cpu/appendCache.js +2 -2
  46. package/dist/ops/cpu/attentionMask.js +5 -5
  47. package/dist/ops/cpu/fusedSoftmax.js +2 -2
  48. package/dist/ops/cpu/gatherSub.js +3 -3
  49. package/dist/ops/cpu/gelu.js +1 -1
  50. package/dist/ops/cpu/matMulGelu.js +2 -2
  51. package/dist/ops/cpu/matMulMul.js +1 -1
  52. package/dist/ops/cpu/mulDropout.js +1 -1
  53. package/dist/ops/cpu/normRMS.js +1 -1
  54. package/dist/ops/cpu/qkv.js +3 -3
  55. package/dist/ops/cpu/rope.js +5 -5
  56. package/dist/ops/cpu/scatterSub.js +5 -5
  57. package/dist/ops/fusedSoftmax.js +1 -1
  58. package/dist/ops/gatherSub.js +1 -1
  59. package/dist/ops/gelu.js +2 -2
  60. package/dist/ops/grads/attentionMask.js +1 -1
  61. package/dist/ops/grads/fusedSoftmax.js +2 -2
  62. package/dist/ops/grads/gelu.js +2 -2
  63. package/dist/ops/grads/matMulGelu.js +1 -1
  64. package/dist/ops/grads/normRMS.js +1 -1
  65. package/dist/ops/grads/qkv.js +1 -1
  66. package/dist/ops/grads/rope.js +1 -1
  67. package/dist/ops/matMulGelu.js +1 -1
  68. package/dist/ops/matMulMul.js +1 -1
  69. package/dist/ops/mulDrop.js +1 -1
  70. package/dist/ops/normRMS.js +1 -1
  71. package/dist/ops/qkv.js +1 -1
  72. package/dist/ops/rope.js +4 -4
  73. package/dist/ops/scatterSub.js +1 -1
  74. package/dist/ops/webgl/adamAdjust.js +2 -2
  75. package/dist/ops/webgl/adamMoments.js +7 -5
  76. package/dist/ops/webgl/appendCache.js +1 -1
  77. package/dist/ops/webgl/attentionMask.js +1 -1
  78. package/dist/ops/webgl/fusedSoftmax.js +4 -4
  79. package/dist/ops/webgl/gatherSub.js +1 -1
  80. package/dist/ops/webgl/gelu.js +2 -2
  81. package/dist/ops/webgl/log.js +3 -3
  82. package/dist/ops/webgl/matMulGelu.js +4 -4
  83. package/dist/ops/webgl/matMulMul.js +1 -1
  84. package/dist/ops/webgl/mulDropout.js +1 -1
  85. package/dist/ops/webgl/normRMS.js +2 -2
  86. package/dist/ops/webgl/qkv.js +1 -1
  87. package/dist/ops/webgl/rope.js +1 -1
  88. package/dist/ops/webgl/scatterSub.js +1 -1
  89. package/dist/ops/webgpu/adamAdjust.js +15 -13
  90. package/dist/ops/webgpu/adamMoments.js +18 -11
  91. package/dist/ops/webgpu/appendCache.js +18 -15
  92. package/dist/ops/webgpu/attentionMask.js +24 -18
  93. package/dist/ops/webgpu/gatherSub.js +17 -30
  94. package/dist/ops/webgpu/gelu.js +3 -3
  95. package/dist/ops/webgpu/normRMS.js +16 -8
  96. package/dist/ops/webgpu/normRMSGrad.js +25 -20
  97. package/dist/ops/webgpu/qkv.js +23 -19
  98. package/dist/ops/webgpu/rope.js +37 -24
  99. package/dist/ops/webgpu/scatterSub.js +16 -14
  100. package/dist/ops/webgpu/utils/reductions.js +4 -4
  101. package/dist/{ops-aRTXR2Sr.js → ops-BFGCx8Ri.js} +15 -15
  102. package/dist/{random_width-DbSpgl4o.js → random_width-sZORGo5k.js} +22 -22
  103. package/dist/{range-D9CZhVlR.js → range-CRuAh-gd.js} +1 -1
  104. package/dist/{reciprocal-CGB48wZB.js → reciprocal-BvGAyKyu.js} +1 -1
  105. package/dist/{register_all_kernels-DnbAyBXt.js → register_all_kernels-BwDSRN-f.js} +30 -30
  106. package/dist/{reshape-BR0eoLYN.js → reshape-CdBq1WJ6.js} +1 -1
  107. package/dist/{scatter_nd_util-OjyAxku2.js → scatter_nd_util-DUstGbU1.js} +1 -1
  108. package/dist/{selu_util-Ce6pu9IM.js → selu_util-BJEXVvjX.js} +3 -3
  109. package/dist/{shared-Czipaeb6.js → shared-B8ztnyEk.js} +6 -6
  110. package/dist/{shared-DS5waSIY.js → shared-wS99K7_n.js} +1 -1
  111. package/dist/{sin-CiBxrDqX.js → sin-BeA3tsEd.js} +1 -1
  112. package/dist/{slice-BHbDHObE.js → slice-BiOsknYS.js} +1 -1
  113. package/dist/{softmax-JMEIUo2J.js → softmax-Bv_6lyMX.js} +1 -1
  114. package/dist/{split-CRU0PjVV.js → split-B-dikLRw.js} +1 -1
  115. package/dist/{stack-ikk2Y8_P.js → stack-B17UN2nn.js} +1 -1
  116. package/dist/{sum-NLYbiDag.js → sum-66ew2byf.js} +1 -1
  117. package/dist/{tensor-Do9PKbIE.js → tensor-JwS7ZYY6.js} +1 -1
  118. package/dist/{tensor2d-CWHxHpLh.js → tensor2d-wxPAnDQy.js} +1 -1
  119. package/dist/training/Adam.js +2 -2
  120. package/dist/training/AdamExt.js +1 -1
  121. package/dist/training/DatasetBuilder.js +35 -32
  122. package/dist/training/FullTrainer.d.ts +15 -2
  123. package/dist/training/FullTrainer.js +97 -51
  124. package/dist/training/Trainer.d.ts +10 -0
  125. package/dist/training/Trainer.js +2 -2
  126. package/dist/training/sparseCrossEntropy.js +4 -4
  127. package/dist/utilities/dummy.js +2 -2
  128. package/dist/utilities/generate.js +3 -3
  129. package/dist/utilities/multinomialCPU.js +2 -2
  130. package/dist/utilities/performance.js +1 -1
  131. package/dist/utilities/profile.js +1 -1
  132. package/dist/utilities/safetensors.js +2 -2
  133. package/dist/utilities/weights.js +2 -2
  134. package/dist/{variable-BTBkayv_.js → variable-BuddVFLa.js} +1 -1
  135. package/dist/{webgpu_program-WaoMq-WD.js → webgpu_program-PFzf1hAQ.js} +1 -1
  136. package/dist/{webgpu_util-DhSeP4b6.js → webgpu_util-D____QpY.js} +1 -1
  137. package/dist/{zeros-DnPT2nD4.js → zeros--BdLQ3oG.js} +1 -1
  138. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- import { e as i } from "../index-UdZhlibC.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) {
@@ -1,5 +1,5 @@
1
- import { r as n } from "../../Reshape-CEsEp0AI.js";
2
- import { f } from "../../index-UdZhlibC.js";
1
+ import { r as n } from "../../Reshape-DH5srBP0.js";
2
+ import { f } from "../../index-BoWRt-10.js";
3
3
  class v {
4
4
  variableNames = ["moments", "value"];
5
5
  outputShape;
@@ -1,4 +1,4 @@
1
- import { f as m } from "../../index-UdZhlibC.js";
1
+ import { f as m } from "../../index-BoWRt-10.js";
2
2
  class i {
3
3
  variableNames = ["moments", "gradient"];
4
4
  outputShape;
@@ -9,12 +9,14 @@ class i {
9
9
  ];
10
10
  constructor(e) {
11
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}]`, s = t === 1 ? "coords" : e.slice(0, -1).map((r, c) => `coords[${c}]`).join(", ");
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
13
  this.userCode = `
14
14
  void main() {
15
15
  float m = getMomentsAtOutCoords();
16
16
  ${n} coords = getOutputCoords();
17
- float g = getGradient(${s});
17
+
18
+ // Add gradient clipping here
19
+ float g = clamp(getGradient(${r}), -1.0, 1.0);
18
20
  int which = ${a};
19
21
 
20
22
  float beta = which == 0 ? beta1 : beta2;
@@ -27,8 +29,8 @@ class i {
27
29
  }
28
30
  }
29
31
  function d(o) {
30
- const { moments: e, gradient: t } = o.inputs, { beta1: n, beta2: a } = o.attrs, s = o.backend, r = new i(e.shape);
31
- return s.runWebGLProgram(r, [e, t], "float32", [[n], [a]]);
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]]);
32
34
  }
33
35
  const g = {
34
36
  kernelName: "AdamMoments",
@@ -1,4 +1,4 @@
1
- import { f as p } from "../../index-UdZhlibC.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-UdZhlibC.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-CVYNbZxu.js";
2
- import { r as v } from "../../Reshape-CEsEp0AI.js";
3
- import { f as w, p as P } from "../../index-UdZhlibC.js";
4
- import { e as S } from "../../axis_util-5DTW2tFV.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,4 +1,4 @@
1
- import { f as l } from "../../index-UdZhlibC.js";
1
+ import { f as l } from "../../index-BoWRt-10.js";
2
2
  class u {
3
3
  variableNames = ["labels", "logits", "values"];
4
4
  outputShape;
@@ -1,5 +1,5 @@
1
- import { f as a } from "../../index-UdZhlibC.js";
2
- import { u as s, C as x } from "../../kernel_funcs_utils-CXDy3EN7.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, af as r } from "../../index-UdZhlibC.js";
2
- import { u as s, l as N } from "../../kernel_funcs_utils-CXDy3EN7.js";
3
- import { y as l } from "../../shared-DS5waSIY.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-UdZhlibC.js";
2
- import { r as f } from "../../Reshape-CEsEp0AI.js";
3
- import { M as U } from "../../mulmat_packed_gpu-IGPBp6h9.js";
4
- import { m as E } from "../../mat_mul-dwmZz69e.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-UdZhlibC.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-UdZhlibC.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-UdZhlibC.js";
2
- import { s as x } from "../../sum-NLYbiDag.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-UdZhlibC.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-UdZhlibC.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-UdZhlibC.js";
1
+ import { f as i } from "../../index-BoWRt-10.js";
2
2
  class u {
3
3
  variableNames = ["labels", "softmaxProbs", "dy"];
4
4
  outputShape;
@@ -1,7 +1,7 @@
1
- import { g as p } from "../../webgpu_program-WaoMq-WD.js";
2
- import { f as d, c as l } from "../../webgpu_util-DhSeP4b6.js";
3
- import { f } from "../../index-UdZhlibC.js";
4
- class c {
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
5
  variableNames = ["moments", "value"];
6
6
  outputShape;
7
7
  shaderKey = "AdamAdjust";
@@ -26,7 +26,7 @@ class c {
26
26
  let m2Hat = moments.y * uniforms.invbeta2;
27
27
 
28
28
  let invSqrt = inverseSqrt(max(m2Hat, 1e-30));
29
- let invDenom = invSqrt / (1.0 + uniforms.epsilon * invSqrt);
29
+ let invDenom = invSqrt / fma(uniforms.epsilon, invSqrt, 1.0);
30
30
  let adjustedValue = fma(-uniforms.learningRate * m1Hat, invDenom, value);
31
31
 
32
32
  setOutputAtIndex(index, adjustedValue);
@@ -35,18 +35,20 @@ class c {
35
35
  `;
36
36
  }
37
37
  }
38
- function h(t) {
39
- const { moments: e, value: a } = t.inputs, { beta1: n, beta2: o, learningRate: i, epsilon: s } = t.attrs, r = t.backend, u = new c(a.shape), m = [
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 = [
40
42
  { type: "float32", data: [1 / n] },
41
- { type: "float32", data: [1 / o] },
42
- { type: "float32", data: [i] },
43
- { type: "float32", data: [s] }
43
+ { type: "float32", data: [1 / s] },
44
+ { type: "float32", data: [o] },
45
+ { type: "float32", data: [i] }
44
46
  ];
45
47
  return r.runWebGPUProgram(u, [e, a], "float32", m);
46
48
  }
47
- const v = {
49
+ const b = {
48
50
  kernelName: "AdamAdjust",
49
51
  backendName: "webgpu",
50
- kernelFunc: h
52
+ kernelFunc: v
51
53
  };
52
- f(v);
54
+ f(b);
@@ -1,7 +1,7 @@
1
- import { g as m } from "../../webgpu_program-WaoMq-WD.js";
2
- import { f as u, c as p } from "../../webgpu_util-DhSeP4b6.js";
3
- import { f as c } from "../../index-UdZhlibC.js";
4
- class d {
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
5
  variableNames = ["moments", "gradient"];
6
6
  outputShape;
7
7
  shaderKey = "AdamMoments";
@@ -25,7 +25,9 @@ class d {
25
25
  ${m("index")} {
26
26
  if (index < uniforms.size) {
27
27
  let m: vec2<f32> = moments[index];
28
- let g: f32 = gradient[index];
28
+
29
+ // Add gradient clipping here
30
+ let g: f32 = clamp(gradient[index], -1.0, 1.0);
29
31
 
30
32
  let newM1 = fma(m.x, uniforms.beta1, g * (1.0 - uniforms.beta1));
31
33
  let newM2 = fma(m.y, uniforms.beta2, g * g * (1.0 - uniforms.beta2));
@@ -37,15 +39,20 @@ class d {
37
39
  }
38
40
  }
39
41
  function f(t) {
40
- const { moments: e, gradient: a } = t.inputs, { beta1: n, beta2: o } = t.attrs, s = t.backend, i = new d(e.shape), r = [
41
- { type: "float32", data: [n] },
42
- { type: "float32", data: [o] }
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] }
43
50
  ];
44
- return s.runWebGPUProgram(i, [e, a], "float32", r);
51
+ return r.runWebGPUProgram(s, [e, o], "float32", i);
45
52
  }
46
- const h = {
53
+ const l = {
47
54
  kernelName: "AdamMoments",
48
55
  backendName: "webgpu",
49
56
  kernelFunc: f
50
57
  };
51
- c(h);
58
+ d(l);
@@ -1,7 +1,7 @@
1
- import { g as u } from "../../webgpu_program-WaoMq-WD.js";
2
- import { f as d, c as m } from "../../webgpu_util-DhSeP4b6.js";
3
- import { f as l } from "../../index-UdZhlibC.js";
4
- 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 {
5
5
  variableNames = ["cache", "item"];
6
6
  outputShape;
7
7
  shaderKey = "AppendCache";
@@ -10,12 +10,12 @@ class f {
10
10
  workgroupSize = [64, 1, 1];
11
11
  size = !0;
12
12
  uniforms = "cacheT: i32";
13
- constructor(t, a, o, c, i) {
14
- const s = Math.min(o + 1, i);
15
- this.shaderKey = `AppendCache_${s}`, this.outputShape = [t, a, s, c], this.dispatchLayout = d(this.outputShape), this.dispatch = m(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);
16
16
  }
17
17
  getUserCode() {
18
- const t = this.outputShape[2];
18
+ const e = this.outputShape[2];
19
19
  return `
20
20
  ${u("index")} {
21
21
  if (index < uniforms.size) {
@@ -26,7 +26,7 @@ class f {
26
26
  let d = coords[3];
27
27
 
28
28
  let itemT = 1;
29
- let maxSize = ${t};
29
+ let maxSize = ${e};
30
30
  let totalT = uniforms.cacheT + itemT;
31
31
  let start = select(0, 1, totalT >= maxSize);
32
32
 
@@ -45,13 +45,16 @@ class f {
45
45
  `;
46
46
  }
47
47
  }
48
- function T(e) {
49
- const { cache: t, item: a } = e.inputs, { maxSize: o, pastLen: c } = e.attrs, i = e.backend, s = t.shape[0], r = t.shape[2], n = t.shape[1], h = new f(s, n, r, a.shape[3], o), p = [{ type: "int32", data: [c] }];
50
- return i.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);
51
54
  }
52
- const g = {
55
+ const T = {
53
56
  kernelName: "AppendCache",
54
57
  backendName: "webgpu",
55
- kernelFunc: T
58
+ kernelFunc: S
56
59
  };
57
- l(g);
60
+ l(T);
@@ -1,6 +1,6 @@
1
- import { f } from "../../index-UdZhlibC.js";
2
- import { g as m } from "../../webgpu_program-WaoMq-WD.js";
3
- import { f as k, c as l } from "../../webgpu_util-DhSeP4b6.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";
4
4
  class g {
5
5
  variableNames = ["q", "k"];
6
6
  outputShape;
@@ -14,13 +14,13 @@ class g {
14
14
  nh;
15
15
  T1;
16
16
  T2;
17
- constructor(t, e, i, r, s) {
18
- if (this.shaderKey = `AttentionMask_${s}`, this.outputShape = [t, e, i, r], this.hs = s, this.nh = e, this.T1 = i, this.T2 = r, this.dispatchLayout = k(this.outputShape), this.dispatch = l(this.dispatchLayout, this.outputShape, this.workgroupSize), s % 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)
19
19
  throw new Error("Head size must be a multiple of 4 for AttentionMaskProgram");
20
20
  }
21
21
  getUserCode() {
22
22
  return `
23
- ${m("index")} {
23
+ ${k("index")} {
24
24
 
25
25
  let coords = getCoordsFromIndex(index);
26
26
  let b = coords[0];
@@ -33,13 +33,14 @@ class g {
33
33
  setOutputAtIndex(index, uniforms.inf);
34
34
  return;
35
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);
36
39
 
37
40
  var sum: f32 = 0.0;
38
41
  for (var i: i32 = 0; i < ${this.hs}; i = i + 4) {
39
- let q0 = getIndexFromCoords4D(vec4<i32>(b, h, t1, i), uniforms.qShape);
40
- let qv = vec4<f32>(q[q0], q[q0 + 1], q[q0 + 2], q[q0 + 3]);
41
- let k0 = getIndexFromCoords4D(vec4<i32>(b, h, t2, i), uniforms.kShape);
42
- 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]);
43
44
  sum = sum + dot(qv, kv);
44
45
  }
45
46
  let scaled = sum * uniforms.divisor;
@@ -49,17 +50,22 @@ class g {
49
50
  `;
50
51
  }
51
52
  }
52
- function q(o) {
53
- const { q: t, k: e } = o.inputs, { divisor: i, pastLen: r } = o.attrs, s = o.backend, a = t.shape[0], n = t.shape[2], u = e.shape[2], d = t.shape[1], h = t.shape[3], c = new g(a, d, n, u, h), p = [
54
- { type: "float32", data: [i] },
55
- { type: "int32", data: [r] },
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] },
56
62
  { type: "float32", data: [Number.NEGATIVE_INFINITY] }
57
63
  ];
58
- return s.runWebGPUProgram(c, [t, e], "float32", p);
64
+ return i.runWebGPUProgram(c, [t, e], "float32", d);
59
65
  }
60
- const v = {
66
+ const q = {
61
67
  kernelName: "AttentionMask",
62
68
  backendName: "webgpu",
63
- kernelFunc: q
69
+ kernelFunc: b
64
70
  };
65
- f(v);
71
+ f(q);
@@ -1,6 +1,6 @@
1
- import { g as r } from "../../webgpu_program-WaoMq-WD.js";
2
- import { f as u, c as d } from "../../webgpu_util-DhSeP4b6.js";
3
- import { f as p } from "../../index-UdZhlibC.js";
1
+ import { g as u } from "../../webgpu_program-PFzf1hAQ.js";
2
+ import { f as h, c as p } from "../../webgpu_util-D____QpY.js";
3
+ import { f as c, a2 as r } from "../../index-BoWRt-10.js";
4
4
  class l {
5
5
  variableNames = ["labels", "logits", "values"];
6
6
  outputShape;
@@ -10,43 +10,30 @@ class l {
10
10
  workgroupSize = [64, 1, 1];
11
11
  size = !0;
12
12
  constructor(e) {
13
- this.outputShape = [e], this.dispatchLayout = u(this.outputShape), this.dispatch = d(this.dispatchLayout, this.outputShape, this.workgroupSize);
13
+ this.outputShape = [e], this.dispatchLayout = h(this.outputShape), this.dispatch = p(this.dispatchLayout, this.outputShape, this.workgroupSize);
14
14
  }
15
15
  getUserCode() {
16
16
  return `
17
- ${r("index")} {
17
+ ${u("index")} {
18
18
  if (index < uniforms.size) {
19
- let coords = getCoordsFromIndex(index);
20
- let idx = i32(getLabelsByOutputIndex(index));
21
- let val = getValuesByOutputIndex(index);
22
-
23
- if (isnan(val)) {
24
- setOutputAtIndex(index, 0.0);
25
- return;
26
- }
27
-
28
- if (idx < uniforms.logitsShape[1] && idx >= 0) {
29
- let logit = getLogits(coords, idx);
30
- if (isnan(logit)) {
31
- setOutputAtIndex(index, 0.0);
32
- return;
33
- }
34
- setOutputAtIndex(index, val - logit);
35
- } else {
36
- setOutputAtIndex(index, 0.0);
37
- }
19
+ let idx = i32(labels[index]);
20
+ let val = values[index];
21
+ let logit = logits[index * uniforms.logitsShape[1] + idx];
22
+ setOutputAtIndex(index, val - logit);
38
23
  }
39
24
  }
40
25
  `;
41
26
  }
42
27
  }
43
- function c(t) {
44
- const { logits: e, labels: i, values: s } = t.inputs, a = t.backend, n = i.shape[0], o = new l(n);
45
- return a.runWebGPUProgram(o, [i, e, s], "float32");
28
+ function d(t) {
29
+ const { logits: e, labels: a, values: s } = t.inputs, o = t.backend, i = a.shape[0];
30
+ r(s.shape, [i], "Error in EfficientGatherSub: "), r(a.shape, [i], "Error in EfficientGatherSub: ");
31
+ const n = new l(i);
32
+ return o.runWebGPUProgram(n, [a, e, s], "float32");
46
33
  }
47
- const h = {
34
+ const f = {
48
35
  kernelName: "EfficientGatherSub",
49
36
  backendName: "webgpu",
50
- kernelFunc: c
37
+ kernelFunc: d
51
38
  };
52
- p(h);
39
+ c(f);
@@ -1,6 +1,6 @@
1
- import { f as i } from "../../index-UdZhlibC.js";
2
- import { g as o } from "../../webgpu_program-WaoMq-WD.js";
3
- import { f as s, c as p } from "../../webgpu_util-DhSeP4b6.js";
1
+ import { f as i } from "../../index-BoWRt-10.js";
2
+ import { g as o } from "../../webgpu_program-PFzf1hAQ.js";
3
+ import { f as s, c as p } from "../../webgpu_util-D____QpY.js";
4
4
  const u = 0.7978845608028654, a = 0.044715;
5
5
  class c {
6
6
  outputShape;
@@ -1,5 +1,5 @@
1
- import { f as p } from "../../webgpu_util-DhSeP4b6.js";
2
- import { f as s } from "../../index-UdZhlibC.js";
1
+ import { f as n } from "../../webgpu_util-D____QpY.js";
2
+ import { f as p, a2 as h } from "../../index-BoWRt-10.js";
3
3
  import { createReduceInfo as u, reduce as c, createReductionShader as m } from "./utils/reductions.js";
4
4
  class d {
5
5
  outputShape;
@@ -12,7 +12,7 @@ class d {
12
12
  inputShape;
13
13
  size = !0;
14
14
  constructor(e) {
15
- this.inputShape = [e.batchSize, e.inSize], this.outputShape = this.inputShape, this.dispatchLayout = p(this.outputShape), this.dispatch = [e.batchSize, 1, 1];
15
+ this.inputShape = [e.batchSize, e.inSize], this.outputShape = this.inputShape, this.dispatchLayout = n(this.outputShape), this.dispatch = [e.batchSize, 1, 1];
16
16
  }
17
17
  getUserCode() {
18
18
  const e = this.workgroupSize[0];
@@ -29,13 +29,21 @@ class d {
29
29
  `);
30
30
  }
31
31
  }
32
- function S(t) {
33
- const { x: e, gamma: i } = t.inputs, n = t.backend, a = [e, i], o = u(a, -1), r = new d(o);
34
- return c(r, a, n);
32
+ function S(a) {
33
+ const { x: e, gamma: r } = a.inputs, o = a.backend, s = [e, r], t = u(s, -1), i = new d(t);
34
+ if (h(r.shape, [e.shape[e.shape.length - 1]], "Error in RMSNorm: "), e.shape.length !== 3)
35
+ throw new Error(`rmsNormGPU: input rank ${e.shape.length} not supported, only rank 3 is supported`);
36
+ if (t.inSize !== e.shape[2])
37
+ throw new Error(`rmsNormGPU: reduction size ${t.inSize} does not match expected size ${e.shape[2]}`);
38
+ if (t.batchSize !== e.shape[0] * e.shape[1])
39
+ throw new Error(
40
+ `rmsNormGPU: batch size ${t.batchSize} does not match expected size ${e.shape[0] * e.shape[1]}`
41
+ );
42
+ return c(i, s, o);
35
43
  }
36
- const h = {
44
+ const l = {
37
45
  kernelName: "RMSNorm",
38
46
  backendName: "webgpu",
39
47
  kernelFunc: S
40
48
  };
41
- s(h);
49
+ p(l);
@@ -1,10 +1,10 @@
1
- import { f as h, e as S } from "../../index-UdZhlibC.js";
2
- import { createReduceInfo as g } from "./utils/reductions.js";
3
- import { f as k } from "../../webgpu_util-DhSeP4b6.js";
4
- import { g as x } from "../../webgpu_program-WaoMq-WD.js";
5
- import { s as c } from "../../slice-BHbDHObE.js";
6
- import { s as z } from "../../sum-NLYbiDag.js";
7
- class w {
1
+ import { f, a2 as c, e as g } from "../../index-BoWRt-10.js";
2
+ import { createReduceInfo as k } from "./utils/reductions.js";
3
+ import { f as x } from "../../webgpu_util-D____QpY.js";
4
+ import { g as z } from "../../webgpu_program-PFzf1hAQ.js";
5
+ import { s as d } from "../../slice-BiOsknYS.js";
6
+ import { s as w } from "../../sum-66ew2byf.js";
7
+ class y {
8
8
  outputShape;
9
9
  shaderKey = "RMSNormGrad";
10
10
  dispatchLayout;
@@ -16,7 +16,7 @@ class w {
16
16
  size = !1;
17
17
  rowsPerWorkgroup;
18
18
  constructor(r, e = 4) {
19
- if (this.shaderKey = `RMSNormGrad_${e}`, this.rowsPerWorkgroup = e, this.inputShape = [r.batchSize, r.inSize], this.outputShape = [r.batchSize + r.batchSize / this.rowsPerWorkgroup, r.inSize], this.dispatchLayout = k(this.outputShape), this.dispatch = [r.batchSize / this.rowsPerWorkgroup, 1, 1], r.batchSize % this.rowsPerWorkgroup !== 0)
19
+ if (this.shaderKey = `RMSNormGrad_${e}`, this.rowsPerWorkgroup = e, this.inputShape = [r.batchSize, r.inSize], this.outputShape = [r.batchSize + r.batchSize / this.rowsPerWorkgroup, r.inSize], this.dispatchLayout = x(this.outputShape), this.dispatch = [r.batchSize / this.rowsPerWorkgroup, 1, 1], r.batchSize % this.rowsPerWorkgroup !== 0)
20
20
  throw new Error(
21
21
  `RMSNormGradProgram: batch size ${r.batchSize} must be divisible by rowsPerWorkgroup ${this.rowsPerWorkgroup}`
22
22
  );
@@ -35,7 +35,7 @@ class w {
35
35
  var<workgroup> accumulation: array<f32, 1024>;
36
36
  `}
37
37
 
38
- ${x("index")} {
38
+ ${z("index")} {
39
39
  // One workgroup per row (batch).
40
40
  let Length = uniforms.reduceSize;
41
41
  let BatchSize = uniforms.batchSize;
@@ -105,24 +105,29 @@ class w {
105
105
  `;
106
106
  }
107
107
  }
108
- function v(t) {
109
- const { dy: r, x: e, gamma: o } = t.inputs, i = 4, l = t.backend, a = g([e, o, r], -1), s = new w(a, i), d = [
108
+ function v(o) {
109
+ const { dy: r, x: e, gamma: t } = o.inputs, i = 4;
110
+ c(e.shape, r.shape, "Error in RMSNormGrad dy: "), c(t.shape, [e.shape[e.shape.length - 1]], "Error in RMSNormGrad gamma: ");
111
+ const l = o.backend, a = k([e, t, r], -1), s = new y(a, i), p = [
110
112
  { type: "int32", data: [s.inputShape[1]] },
111
113
  // Reduce size
112
114
  { type: "int32", data: [s.inputShape[0]] }
113
115
  // Batch size
114
- ], n = S().makeTensorFromTensorInfo(
115
- l.runWebGPUProgram(s, [e, o, r], "float32", d)
116
- ), u = c(n, [0, 0], [a.batchSize, a.inSize]), m = c(n, [a.batchSize, 0], [a.batchSize / i, a.inSize]);
116
+ ];
117
+ if (a.inSize > 1024)
118
+ throw new Error(`rmsNormGradGPU: inSize ${a.inSize} exceeds max of 1024`);
119
+ const n = g().makeTensorFromTensorInfo(
120
+ l.runWebGPUProgram(s, [e, t, r], "float32", p)
121
+ ), m = d(n, [0, 0], [a.batchSize, a.inSize]), u = d(n, [a.batchSize, 0], [a.batchSize / i, a.inSize]);
117
122
  n.dispose();
118
- const p = u.reshape(e.shape);
119
- u.dispose();
120
- const f = z(m, [0]);
121
- return m.dispose(), [p, f];
123
+ const h = m.reshape(e.shape);
124
+ m.dispose();
125
+ const S = w(u, [0]);
126
+ return u.dispose(), [h, S];
122
127
  }
123
- const y = {
128
+ const b = {
124
129
  kernelName: "RMSNormGrad",
125
130
  backendName: "webgpu",
126
131
  kernelFunc: v
127
132
  };
128
- h(y);
133
+ f(b);