@genai-fi/nanogpt 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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-CVYNbZxu.js} +11 -11
  4. package/dist/{Reshape-BLijOA8h.js → Reshape-CEsEp0AI.js} +2 -2
  5. package/dist/Reshape-Do18N3gO.js +30 -0
  6. package/dist/TeachableLLM.js +9 -5
  7. package/dist/{TiedEmbedding-BLltddza.js → TiedEmbedding-ccLBFiZi.js} +4 -4
  8. package/dist/{axis_util-DaAl5MER.js → axis_util-5DTW2tFV.js} +1 -1
  9. package/dist/backend.js +2 -2
  10. package/dist/{backend_util-DWiwsi2N.js → backend_util-C9Ut8n0Q.js} +40 -40
  11. package/dist/{broadcast_to-C4v-j9yA.js → broadcast_to-Ba9h_8DO.js} +2 -2
  12. package/dist/{concat-CsHeR4zV.js → concat-CbXTetof.js} +1 -1
  13. package/dist/{dataset-JDyjG3QR.js → dataset-U3PrjwgU.js} +7 -7
  14. package/dist/{dropout-hpDwECTe.js → dropout-DPfPgWWe.js} +11 -11
  15. package/dist/{gather-D0_gPiBz.js → gather-Bbh8DHhM.js} +4 -4
  16. package/dist/{gelu-uyHP1x1f.js → gelu-BFwVnd1r.js} +1 -1
  17. package/dist/{gpgpu_math-DJm3ZTAf.js → gpgpu_math-DffelNS-.js} +2 -2
  18. package/dist/{index-BPPzKVdR.js → index-DYD_yPa-.js} +1083 -1106
  19. package/dist/{index-C0dhsYom.js → index-UdZhlibC.js} +126 -126
  20. package/dist/{kernel_funcs_utils-CwRTFqrc.js → kernel_funcs_utils-CXDy3EN7.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-BnmCkHWl.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-dwmZz69e.js} +1 -1
  34. package/dist/{max-BQc2Aj-I.js → max-ByjEGoFx.js} +3 -3
  35. package/dist/{mulmat_packed_gpu-Gzf3I9UV.js → mulmat_packed_gpu-IGPBp6h9.js} +1 -1
  36. package/dist/{ones-D63HpSF_.js → ones-C8Mfln6-.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 +38 -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 +52 -0
  95. package/dist/ops/webgpu/adamMoments.d.ts +1 -0
  96. package/dist/ops/webgpu/adamMoments.js +51 -0
  97. package/dist/ops/webgpu/appendCache.js +13 -12
  98. package/dist/ops/webgpu/attentionMask.js +11 -10
  99. package/dist/ops/webgpu/gatherSub.js +26 -11
  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 +27 -101
  103. package/dist/ops/webgpu/normRMSGrad.d.ts +1 -0
  104. package/dist/ops/webgpu/normRMSGrad.js +128 -0
  105. package/dist/ops/webgpu/qkv.js +9 -8
  106. package/dist/ops/webgpu/rope.js +8 -7
  107. package/dist/ops/webgpu/scatterSub.js +8 -7
  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-aRTXR2Sr.js} +195 -219
  111. package/dist/{random_width-DkYP8W8N.js → random_width-DbSpgl4o.js} +22 -21
  112. package/dist/{range-CYzpQY53.js → range-D9CZhVlR.js} +1 -1
  113. package/dist/{reciprocal-_A9yv27J.js → reciprocal-CGB48wZB.js} +1 -1
  114. package/dist/{register_all_kernels-guvSxp7M.js → register_all_kernels-DnbAyBXt.js} +30 -29
  115. package/dist/{reshape-BMUzc1UY.js → reshape-BR0eoLYN.js} +3 -3
  116. package/dist/{scatter_nd_util-IRBqKz_b.js → scatter_nd_util-OjyAxku2.js} +1 -1
  117. package/dist/{selu_util-Dt_iuXaq.js → selu_util-Ce6pu9IM.js} +41 -41
  118. package/dist/{shared-CDu9S76h.js → shared-Czipaeb6.js} +6 -6
  119. package/dist/{shared-BNa2q6jD.js → shared-DS5waSIY.js} +1 -1
  120. package/dist/{sin-Cocju-BY.js → sin-CiBxrDqX.js} +6 -6
  121. package/dist/slice-BHbDHObE.js +28 -0
  122. package/dist/{softmax-GPNK3o-U.js → softmax-JMEIUo2J.js} +3 -3
  123. package/dist/{split-CHzJjxDv.js → split-CRU0PjVV.js} +1 -1
  124. package/dist/{stack-Dpgg_1W1.js → stack-ikk2Y8_P.js} +1 -1
  125. package/dist/{sum-B8wEpKsg.js → sum-NLYbiDag.js} +3 -3
  126. package/dist/{tensor-RvZVNmg0.js → tensor-Do9PKbIE.js} +1 -1
  127. package/dist/{tensor2d-B_kyod7_.js → tensor2d-CWHxHpLh.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 +2 -2
  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-BTBkayv_.js} +1 -1
  149. package/dist/{webgpu_util-g13LvDIv.js → webgpu_program-WaoMq-WD.js} +138 -215
  150. package/dist/webgpu_util-DhSeP4b6.js +80 -0
  151. package/dist/{zeros-DCPCdFGq.js → zeros-DnPT2nD4.js} +4 -4
  152. package/package.json +1 -1
@@ -0,0 +1,52 @@
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 {
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 / (1.0 + uniforms.epsilon * invSqrt);
30
+ let adjustedValue = fma(-uniforms.learningRate * m1Hat, invDenom, value);
31
+
32
+ setOutputAtIndex(index, adjustedValue);
33
+ }
34
+ }
35
+ `;
36
+ }
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 = [
40
+ { type: "float32", data: [1 / n] },
41
+ { type: "float32", data: [1 / o] },
42
+ { type: "float32", data: [i] },
43
+ { type: "float32", data: [s] }
44
+ ];
45
+ return r.runWebGPUProgram(u, [e, a], "float32", m);
46
+ }
47
+ const v = {
48
+ kernelName: "AdamAdjust",
49
+ backendName: "webgpu",
50
+ kernelFunc: h
51
+ };
52
+ f(v);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,51 @@
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 {
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
+ let g: f32 = gradient[index];
29
+
30
+ let newM1 = fma(m.x, uniforms.beta1, g * (1.0 - uniforms.beta1));
31
+ let newM2 = fma(m.y, uniforms.beta2, g * g * (1.0 - uniforms.beta2));
32
+
33
+ setOutputAtIndex(index, vec2<f32>(newM1, newM2));
34
+ }
35
+ }
36
+ `;
37
+ }
38
+ }
39
+ 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] }
43
+ ];
44
+ return s.runWebGPUProgram(i, [e, a], "float32", r);
45
+ }
46
+ const h = {
47
+ kernelName: "AdamMoments",
48
+ backendName: "webgpu",
49
+ kernelFunc: f
50
+ };
51
+ c(h);
@@ -1,5 +1,6 @@
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";
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";
3
4
  class f {
4
5
  variableNames = ["cache", "item"];
5
6
  outputShape;
@@ -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(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);
15
16
  }
16
17
  getUserCode() {
17
18
  const t = 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];
@@ -33,11 +34,11 @@ 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
  }
@@ -45,12 +46,12 @@ class f {
45
46
  }
46
47
  }
47
48
  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);
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);
50
51
  }
51
52
  const g = {
52
53
  kernelName: "AppendCache",
53
54
  backendName: "webgpu",
54
55
  kernelFunc: T
55
56
  };
56
- m(g);
57
+ l(g);
@@ -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 } 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";
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, 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)
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
+ ${m("index")} {
23
24
 
24
25
  let coords = getCoordsFromIndex(index);
25
26
  let b = coords[0];
@@ -48,13 +49,13 @@ class g {
48
49
  `;
49
50
  }
50
51
  }
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] },
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] },
55
56
  { type: "float32", data: [Number.NEGATIVE_INFINITY] }
56
57
  ];
57
- return a.runWebGPUProgram(h, [t, e], "float32", p);
58
+ return s.runWebGPUProgram(c, [t, e], "float32", p);
58
59
  }
59
60
  const v = {
60
61
  kernelName: "AttentionMask",
@@ -1,6 +1,7 @@
1
- import { f as u, c as n, g as c } from "../../webgpu_util-g13LvDIv.js";
2
- import { f as p } from "../../index-C0dhsYom.js";
3
- class d {
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";
4
+ class l {
4
5
  variableNames = ["labels", "logits", "values"];
5
6
  outputShape;
6
7
  shaderKey = "GatherSub";
@@ -9,29 +10,43 @@ class d {
9
10
  workgroupSize = [64, 1, 1];
10
11
  size = !0;
11
12
  constructor(e) {
12
- this.outputShape = [e], this.dispatchLayout = u(this.outputShape), this.dispatch = n(this.dispatchLayout, this.outputShape, this.workgroupSize);
13
+ this.outputShape = [e], this.dispatchLayout = u(this.outputShape), this.dispatch = d(this.dispatchLayout, this.outputShape, this.workgroupSize);
13
14
  }
14
15
  getUserCode() {
15
16
  return `
16
- ${c("index")} {
17
+ ${r("index")} {
17
18
  if (index < uniforms.size) {
18
19
  let coords = getCoordsFromIndex(index);
19
20
  let idx = i32(getLabelsByOutputIndex(index));
20
21
  let val = getValuesByOutputIndex(index);
21
- let logit = getLogits(coords, idx);
22
- setOutputAtIndex(index, val - logit);
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
+ }
23
38
  }
24
39
  }
25
40
  `;
26
41
  }
27
42
  }
28
- function l(t) {
29
- const { logits: e, labels: a, values: s } = t.inputs, i = t.backend, o = a.shape[0], r = new d(o);
30
- return i.runWebGPUProgram(r, [a, e, s], "float32");
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");
31
46
  }
32
47
  const h = {
33
48
  kernelName: "EfficientGatherSub",
34
49
  backendName: "webgpu",
35
- kernelFunc: l
50
+ kernelFunc: c
36
51
  };
37
52
  p(h);
@@ -1,5 +1,6 @@
1
- import { f as i } from "../../index-C0dhsYom.js";
2
- import { f as o, c as s, g as p } from "../../webgpu_util-g13LvDIv.js";
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";
3
4
  const u = 0.7978845608028654, a = 0.044715;
4
5
  class c {
5
6
  outputShape;
@@ -10,7 +11,7 @@ class c {
10
11
  workgroupSize;
11
12
  size = !0;
12
13
  constructor(e) {
13
- this.workgroupSize = [128, 1, 1], this.outputShape = e, this.dispatchLayout = o(this.outputShape), this.dispatch = s(this.dispatchLayout, this.outputShape, this.workgroupSize), this.shaderKey = "unary_gelu";
14
+ this.workgroupSize = [128, 1, 1], this.outputShape = e, this.dispatchLayout = s(this.outputShape), this.dispatch = p(this.dispatchLayout, this.outputShape, this.workgroupSize), this.shaderKey = "unary_gelu";
14
15
  }
15
16
  getUserCode() {
16
17
  return `
@@ -22,7 +23,7 @@ class c {
22
23
  inner = 0.5 * (1.0 + inner);
23
24
  return x * inner;
24
25
  }
25
- ${p("index")} {
26
+ ${o("index")} {
26
27
  if (index < uniforms.size) {
27
28
  let a = getAByOutputIndex(index);
28
29
  setOutputAtIndex(index, unaryOperation(a));
@@ -51,11 +52,11 @@ class x {
51
52
  workgroupSize = [128, 1, 1];
52
53
  size = !0;
53
54
  constructor(e) {
54
- this.outputShape = e, this.dispatchLayout = o(this.outputShape), this.dispatch = s(this.dispatchLayout, this.outputShape, this.workgroupSize);
55
+ this.outputShape = e, this.dispatchLayout = s(this.outputShape), this.dispatch = p(this.dispatchLayout, this.outputShape, this.workgroupSize);
55
56
  }
56
57
  getUserCode() {
57
58
  return `
58
- ${p("index")} {
59
+ ${o("index")} {
59
60
  if (index < uniforms.size) {
60
61
  let X = getXByOutputIndex(index);
61
62
  let x2 = X * X;
@@ -1,8 +1,11 @@
1
1
  import "./attentionMask.js";
2
2
  import "./normRMS.js";
3
+ import "./normRMSGrad.js";
3
4
  import "./rope.js";
4
5
  import "./appendCache.js";
5
6
  import "./scatterSub.js";
6
7
  import "./gatherSub.js";
7
8
  import "./qkv.js";
8
9
  import "./gelu.js";
10
+ import "./adamMoments.js";
11
+ import "./adamAdjust.js";
@@ -1,115 +1,41 @@
1
- import { f as m, c as p, g as c } from "../../webgpu_util-g13LvDIv.js";
2
- import { f as l, e as k } from "../../index-C0dhsYom.js";
3
- import { s as M } from "../../sum-B8wEpKsg.js";
4
- class N {
5
- variableNames = ["x", "meanSquare", "gamma"];
1
+ import { f as p } from "../../webgpu_util-DhSeP4b6.js";
2
+ import { f as s } from "../../index-UdZhlibC.js";
3
+ import { createReduceInfo as u, reduce as c, createReductionShader as m } from "./utils/reductions.js";
4
+ class d {
6
5
  outputShape;
7
6
  shaderKey = "RMSNorm";
8
7
  dispatchLayout;
9
8
  dispatch;
10
9
  workgroupSize = [64, 1, 1];
10
+ variableNames = ["x", "gamma"];
11
+ uniforms = "reduceSize : i32,";
12
+ inputShape;
11
13
  size = !0;
12
- constructor(t, e, a) {
13
- this.outputShape = [t, e, a], this.dispatchLayout = m(this.outputShape), this.dispatch = p(this.dispatchLayout, this.outputShape, this.workgroupSize);
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];
14
16
  }
15
17
  getUserCode() {
16
- return `
17
- ${c("index")} {
18
- if (index < uniforms.size) {
19
- let coords = getCoordsFromIndex(index);
20
- let x = getXByOutputIndex(index);
21
- let meanSquare = getMeanSquare(coords[0], coords[1], 0);
22
- let gamma = getGammaByOutputIndex(index);
23
- let invRms = inverseSqrt(meanSquare + 1e-8);
24
- let normalized = x * invRms;
25
- let outVal = normalized * gamma;
26
- setOutputAtIndex(index, outVal);
27
- }
28
- }
29
- `;
18
+ const e = this.workgroupSize[0];
19
+ return m(e, "mean", `
20
+ candidate = candidate * candidate;
21
+ `, `
22
+ bestValue = inverseSqrt(bestValue + 1e-8);
23
+ `, `
24
+ let X = f32(x[offset + k]);
25
+ let gamma = gamma[k];
26
+ let normalized = X * bestValue;
27
+ let outVal = normalized * gamma;
28
+ result[offset + k] = f32(outVal);
29
+ `);
30
30
  }
31
31
  }
32
- function b(s) {
33
- const { x: t, gamma: e } = s.inputs, a = s.backend, o = t.shape[0], n = t.shape[1], i = t.shape[2], u = t.square().mean(-1, !0), r = new N(o, n, i);
34
- return a.runWebGPUProgram(r, [t, u, e], "float32");
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);
35
35
  }
36
- const z = {
36
+ const h = {
37
37
  kernelName: "RMSNorm",
38
38
  backendName: "webgpu",
39
- kernelFunc: b
39
+ kernelFunc: S
40
40
  };
41
- l(z);
42
- class R {
43
- variableNames = ["x", "meanSquare", "dyGamma", "dyXMean"];
44
- outputShape;
45
- shaderKey = "RMSNormGradX";
46
- dispatchLayout;
47
- dispatch;
48
- workgroupSize = [64, 1, 1];
49
- size = !0;
50
- C;
51
- constructor(t, e, a) {
52
- this.outputShape = [t, e, a], this.dispatchLayout = m(this.outputShape), this.dispatch = p(this.dispatchLayout, this.outputShape, this.workgroupSize), this.C = a;
53
- }
54
- getUserCode() {
55
- return `
56
- ${c("index")} {
57
- if (index < uniforms.size) {
58
- let coords = getCoordsFromIndex(index);
59
- let x = getXByOutputIndex(index);
60
- let meanSquare = getMeanSquare(coords[0], coords[1], 0) + 1e-8;
61
- let dyGamma = getDyGammaByOutputIndex(index);
62
- let dyXMean = getDyXMean(coords[0], coords[1], 0) / ${this.C}.0;
63
- let invRms = inverseSqrt(meanSquare);
64
- let dx = dyGamma * invRms - x * dyXMean * invRms / meanSquare;
65
- setOutputAtIndex(index, dx);
66
- }
67
- }
68
- `;
69
- }
70
- }
71
- class v {
72
- variableNames = ["x", "meanSquare", "dy"];
73
- outputShape;
74
- shaderKey = "RMSNormGradGamma";
75
- dispatchLayout;
76
- dispatch;
77
- workgroupSize = [64, 1, 1];
78
- size = !0;
79
- constructor(t, e, a) {
80
- this.outputShape = [t, e, a], this.dispatchLayout = m(this.outputShape), this.dispatch = p(this.dispatchLayout, this.outputShape, this.workgroupSize);
81
- }
82
- getUserCode() {
83
- return `
84
- ${c("index")} {
85
- if (index < uniforms.size) {
86
- let coords = getCoordsFromIndex(index);
87
- let x = getXByOutputIndex(index);
88
- let meanSquare = getMeanSquare(coords[0], coords[1], 0) + 1e-8;
89
- let dy = getDyByOutputIndex(index);
90
- let invRms = inverseSqrt(meanSquare);
91
- let dGamma = dy * (x * invRms);
92
- setOutputAtIndex(index,dGamma);
93
- }
94
- }
95
- `;
96
- }
97
- }
98
- function I(s) {
99
- const { dy: t, x: e, gamma: a } = s.inputs, o = s.backend, n = e.shape[0], i = e.shape[1], u = e.shape[2], r = t.mul(a), h = r.mul(e), g = h.sum(-1, !0);
100
- h.dispose();
101
- const S = e.square(), d = S.mean(-1, !0);
102
- S.dispose();
103
- const y = new R(n, i, u), G = o.runWebGPUProgram(y, [e, d, r, g], "float32");
104
- r.dispose(), g.dispose();
105
- const q = new v(n, i, u), x = o.runWebGPUProgram(q, [e, d, t], "float32");
106
- d.dispose();
107
- const f = M(k().makeTensorFromTensorInfo(x), [0, 1]);
108
- return o.disposeData(x), [G, f];
109
- }
110
- const P = {
111
- kernelName: "RMSNormGrad",
112
- backendName: "webgpu",
113
- kernelFunc: I
114
- };
115
- l(P);
41
+ s(h);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,128 @@
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 {
8
+ outputShape;
9
+ shaderKey = "RMSNormGrad";
10
+ dispatchLayout;
11
+ dispatch;
12
+ workgroupSize = [64, 1, 1];
13
+ variableNames = ["x", "gamma", "dy"];
14
+ uniforms = "reduceSize : i32, batchSize: i32";
15
+ inputShape;
16
+ size = !1;
17
+ rowsPerWorkgroup;
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)
20
+ throw new Error(
21
+ `RMSNormGradProgram: batch size ${r.batchSize} must be divisible by rowsPerWorkgroup ${this.rowsPerWorkgroup}`
22
+ );
23
+ if (r.inSize > 1024)
24
+ throw new Error(`RMSNormGradProgram: inSize ${r.inSize} exceeds max of 1024`);
25
+ }
26
+ getUserCode() {
27
+ const r = this.workgroupSize[0], e = this.rowsPerWorkgroup;
28
+ return `
29
+ fn DIV_CEIL(a : u32, b : u32) -> u32 {
30
+ return ((a - 1u) / b + 1u);
31
+ }
32
+
33
+ ${`
34
+ var<workgroup> partials : array<vec2<f32>, ${r}>;
35
+ var<workgroup> accumulation: array<f32, 1024>;
36
+ `}
37
+
38
+ ${x("index")} {
39
+ // One workgroup per row (batch).
40
+ let Length = uniforms.reduceSize;
41
+ let BatchSize = uniforms.batchSize;
42
+ for (var k = i32(localId.x); k < Length; k = k + ${r}) {
43
+ accumulation[k] = 0.0;
44
+ }
45
+
46
+ for (var rowOff = 0; rowOff < ${e}; rowOff = rowOff + 1) {
47
+ let row = i32(workgroupId.x) * ${e} + rowOff;
48
+ let offset = row * Length;
49
+
50
+ var sum_x2 = 0.0;
51
+ var sum_dygx = 0.0;
52
+
53
+ for (var k = i32(localId.x); k < Length; k = k + ${r}) {
54
+ let X = f32(x[offset + k]);
55
+ let DY = f32(dy[offset + k]);
56
+ let G = f32(gamma[k]);
57
+ sum_x2 = fma(X, X, sum_x2);
58
+ sum_dygx = fma(DY * G, X, sum_dygx);
59
+ }
60
+
61
+ partials[localId.x] = vec2<f32>(sum_x2, sum_dygx);
62
+ workgroupBarrier();
63
+
64
+ var reduceSize = min(u32(Length), ${r}u);
65
+ for (var currentSize = reduceSize / 2u; reduceSize > 1u; currentSize = reduceSize / 2u) {
66
+ let interval = DIV_CEIL(reduceSize, 2u);
67
+ if (localId.x < currentSize) {
68
+ partials[localId.x] = partials[localId.x] + partials[localId.x + interval];
69
+ }
70
+ reduceSize = interval;
71
+ workgroupBarrier();
72
+ }
73
+
74
+ let invN = 1.0 / f32(Length);
75
+ let mean_x2 = fma(partials[0].x, invN, 1e-8);
76
+ let mean_dygx = partials[0].y * invN;
77
+
78
+ let invRMS = inverseSqrt(mean_x2);
79
+ let scale = (mean_dygx / (mean_x2)) * invRMS;
80
+
81
+ // write dx and dGamma.
82
+ for (var k = i32(localId.x); k < Length; k = k + ${r}) {
83
+ let X = f32(x[offset + k]);
84
+ let DY = f32(dy[offset + k]);
85
+ let G = f32(gamma[k]);
86
+
87
+ let dyGamma = DY * G;
88
+ let dx = fma(dyGamma, invRMS, -X * scale);
89
+
90
+ result[offset + k] = dx;
91
+
92
+ // dGamma
93
+ accumulation[k] = fma(DY, X * invRMS, accumulation[k]);
94
+ }
95
+
96
+ workgroupBarrier();
97
+ }
98
+
99
+ // Write out the partially accumulated dGamma
100
+ let outDgBase = BatchSize * Length + i32(workgroupId.x) * Length;
101
+ for (var k = i32(localId.x); k < Length; k = k + ${r}) {
102
+ result[outDgBase + k] = accumulation[k];
103
+ }
104
+ }
105
+ `;
106
+ }
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 = [
110
+ { type: "int32", data: [s.inputShape[1]] },
111
+ // Reduce size
112
+ { type: "int32", data: [s.inputShape[0]] }
113
+ // 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]);
117
+ n.dispose();
118
+ const p = u.reshape(e.shape);
119
+ u.dispose();
120
+ const f = z(m, [0]);
121
+ return m.dispose(), [p, f];
122
+ }
123
+ const y = {
124
+ kernelName: "RMSNormGrad",
125
+ backendName: "webgpu",
126
+ kernelFunc: v
127
+ };
128
+ h(y);
@@ -1,6 +1,7 @@
1
- import { f as c, c as d, g as h } from "../../webgpu_util-g13LvDIv.js";
2
- import { f as p } from "../../index-C0dhsYom.js";
3
- class l {
1
+ import { g as c } from "../../webgpu_program-WaoMq-WD.js";
2
+ import { f as d, c as h } from "../../webgpu_util-DhSeP4b6.js";
3
+ import { f as p } from "../../index-UdZhlibC.js";
4
+ class m {
4
5
  variableNames = ["x", "kernel"];
5
6
  outputShape;
6
7
  shaderKey = "QKV";
@@ -11,12 +12,12 @@ class l {
11
12
  size = !0;
12
13
  constructor(t, e, o, s) {
13
14
  const r = s / e;
14
- this.outputShape = [t, e, o, r], this.dispatchLayout = c(this.outputShape), this.dispatch = d(this.dispatchLayout, this.outputShape, this.workgroupSize);
15
+ this.shaderKey = `QKV_${e}_${r}`, this.outputShape = [t, e, o, r], this.dispatchLayout = d(this.outputShape), this.dispatch = h(this.dispatchLayout, this.outputShape, this.workgroupSize);
15
16
  }
16
17
  getUserCode() {
17
18
  const t = this.outputShape[1], e = this.outputShape[3], o = t * e;
18
19
  return `
19
- ${h("index")} {
20
+ ${c("index")} {
20
21
  if (index < uniforms.size) {
21
22
  let coords = getCoordsFromIndex(index); // [b, h, t, d]
22
23
  let b = coords[0];
@@ -40,8 +41,8 @@ class l {
40
41
  `;
41
42
  }
42
43
  }
43
- function m(a) {
44
- const { x: t, kernel: e } = a.inputs, { heads: o } = a.attrs, s = a.backend, r = t.shape[0], i = t.shape[1], u = t.shape[2], n = new l(r, o, i, u);
44
+ function l(a) {
45
+ const { x: t, kernel: e } = a.inputs, { heads: o } = a.attrs, s = a.backend, r = t.shape[0], i = t.shape[1], u = t.shape[2], n = new m(r, o, i, u);
45
46
  return [
46
47
  s.runWebGPUProgram(n, [t, e], "float32", [{ type: "int32", data: [0] }]),
47
48
  s.runWebGPUProgram(n, [t, e], "float32", [{ type: "int32", data: [1] }]),
@@ -51,6 +52,6 @@ function m(a) {
51
52
  const f = {
52
53
  kernelName: "QKV",
53
54
  backendName: "webgpu",
54
- kernelFunc: m
55
+ kernelFunc: l
55
56
  };
56
57
  p(f);