@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
@@ -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 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
+ class l {
4
5
  variableNames = ["labels", "logits", "values"];
5
6
  outputShape;
6
7
  shaderKey = "GatherSub";
@@ -9,29 +10,30 @@ 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 = h(this.outputShape), this.dispatch = p(this.dispatchLayout, this.outputShape, this.workgroupSize);
13
14
  }
14
15
  getUserCode() {
15
16
  return `
16
- ${c("index")} {
17
+ ${u("index")} {
17
18
  if (index < uniforms.size) {
18
- let coords = getCoordsFromIndex(index);
19
- let idx = i32(getLabelsByOutputIndex(index));
20
- let val = getValuesByOutputIndex(index);
21
- let logit = getLogits(coords, idx);
19
+ let idx = i32(labels[index]);
20
+ let val = values[index];
21
+ let logit = logits[index * uniforms.logitsShape[1] + idx];
22
22
  setOutputAtIndex(index, val - logit);
23
23
  }
24
24
  }
25
25
  `;
26
26
  }
27
27
  }
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");
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");
31
33
  }
32
- const h = {
34
+ const f = {
33
35
  kernelName: "EfficientGatherSub",
34
36
  backendName: "webgpu",
35
- kernelFunc: l
37
+ kernelFunc: d
36
38
  };
37
- p(h);
39
+ c(f);
@@ -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-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";
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,49 @@
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 n } from "../../webgpu_util-D____QpY.js";
2
+ import { f as p, a2 as h } from "../../index-BoWRt-10.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 = n(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(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 z = {
44
+ const l = {
37
45
  kernelName: "RMSNorm",
38
46
  backendName: "webgpu",
39
- kernelFunc: b
47
+ kernelFunc: S
40
48
  };
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);
49
+ p(l);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,133 @@
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
+ 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 = x(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
+ ${z("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(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 = [
112
+ { type: "int32", data: [s.inputShape[1]] },
113
+ // Reduce size
114
+ { type: "int32", data: [s.inputShape[0]] }
115
+ // Batch size
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]);
122
+ n.dispose();
123
+ const h = m.reshape(e.shape);
124
+ m.dispose();
125
+ const S = w(u, [0]);
126
+ return u.dispose(), [h, S];
127
+ }
128
+ const b = {
129
+ kernelName: "RMSNormGrad",
130
+ backendName: "webgpu",
131
+ kernelFunc: v
132
+ };
133
+ f(b);
@@ -1,5 +1,6 @@
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";
1
+ import { g as h } from "../../webgpu_program-PFzf1hAQ.js";
2
+ import { f as c, c as d } from "../../webgpu_util-D____QpY.js";
3
+ import { f as p, a2 as m } from "../../index-BoWRt-10.js";
3
4
  class l {
4
5
  variableNames = ["x", "kernel"];
5
6
  outputShape;
@@ -10,8 +11,8 @@ class l {
10
11
  workgroupSize = [64, 1, 1];
11
12
  size = !0;
12
13
  constructor(t, e, o, s) {
13
- 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);
14
+ const n = s / e;
15
+ this.shaderKey = `QKV_${e}_${n}`, this.outputShape = [t, e, o, n], this.dispatchLayout = c(this.outputShape), this.dispatch = d(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;
@@ -25,13 +26,14 @@ class l {
25
26
  let d = coords[3];
26
27
 
27
28
  // Compute output channel index in fused kernel
28
- let out_offset = uniforms.mode * ${t} * ${e} + h * ${e} + d;
29
+ let out_offset = uniforms.mode * ${o} + h * ${e} + d;
29
30
 
30
31
  var sum = 0.0;
32
+ let baseX = b * uniforms.xShape[1] * uniforms.xShape[2] + t * uniforms.xShape[2];
31
33
  for (var c = 0; c < ${o}; c += 1) {
32
- let xval = getX(b, t, c); // fetch from x
33
- let kval = getKernel(c, out_offset); // fetch from kernel
34
- sum += xval * kval;
34
+ let xval = x[baseX + c];
35
+ let kval = getKernel(c, out_offset);
36
+ sum = fma(xval, kval, sum);
35
37
  }
36
38
 
37
39
  setOutputAtIndex(index, sum);
@@ -40,17 +42,20 @@ class l {
40
42
  `;
41
43
  }
42
44
  }
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);
45
+ function f(r) {
46
+ const { x: t, kernel: e } = r.inputs, { heads: o } = r.attrs, s = r.backend, n = t.shape[0], u = t.shape[1], a = t.shape[2];
47
+ if (m(e.shape, [a, 3 * a], "Error in QKV: "), a % o !== 0)
48
+ throw new Error(`Channel dimension ${a} must be divisible by number of heads ${o} in QKV.`);
49
+ const i = new l(n, o, u, a);
45
50
  return [
46
- s.runWebGPUProgram(n, [t, e], "float32", [{ type: "int32", data: [0] }]),
47
- s.runWebGPUProgram(n, [t, e], "float32", [{ type: "int32", data: [1] }]),
48
- s.runWebGPUProgram(n, [t, e], "float32", [{ type: "int32", data: [2] }])
51
+ s.runWebGPUProgram(i, [t, e], "float32", [{ type: "int32", data: [0] }]),
52
+ s.runWebGPUProgram(i, [t, e], "float32", [{ type: "int32", data: [1] }]),
53
+ s.runWebGPUProgram(i, [t, e], "float32", [{ type: "int32", data: [2] }])
49
54
  ];
50
55
  }
51
- const f = {
56
+ const b = {
52
57
  kernelName: "QKV",
53
58
  backendName: "webgpu",
54
- kernelFunc: m
59
+ kernelFunc: f
55
60
  };
56
- p(f);
61
+ p(b);
@@ -1,6 +1,7 @@
1
- import { f as h, c as l, g as m } from "../../webgpu_util-g13LvDIv.js";
2
- import { f as g } from "../../index-C0dhsYom.js";
3
- class f {
1
+ import { g as c } from "../../webgpu_program-PFzf1hAQ.js";
2
+ import { f as m, c as l } from "../../webgpu_util-D____QpY.js";
3
+ import { f as x, a2 as f } from "../../index-BoWRt-10.js";
4
+ class S {
4
5
  variableNames = ["x", "sin", "cos"];
5
6
  outputShape;
6
7
  shaderKey = "Rope";
@@ -9,13 +10,13 @@ class f {
9
10
  workgroupSize = [64, 1, 1];
10
11
  size = !0;
11
12
  uniforms = "pastLen: i32";
12
- constructor(t, o, s, n) {
13
- this.outputShape = [t, o, s, n], this.dispatchLayout = h(this.outputShape), this.dispatch = l(this.dispatchLayout, this.outputShape, this.workgroupSize);
13
+ constructor(t, e, a, s) {
14
+ this.shaderKey = `Rope_${s}`, this.outputShape = [t, e, a, s], this.dispatchLayout = m(this.outputShape), this.dispatch = l(this.dispatchLayout, this.outputShape, this.workgroupSize);
14
15
  }
15
16
  getUserCode() {
16
17
  const t = this.outputShape[3];
17
18
  return `
18
- ${m("index")} {
19
+ ${c("index")} {
19
20
  if (index < uniforms.size) {
20
21
  let coords = getCoordsFromIndex(index); // [b, h, t, d]
21
22
  let b = coords[0];
@@ -27,27 +28,31 @@ class f {
27
28
 
28
29
  var outVal = 0.0;
29
30
 
31
+ let xIdx = b * uniforms.outShapeStrides[0] +
32
+ h * uniforms.outShapeStrides[1] +
33
+ t * uniforms.outShapeStrides[2] +
34
+ d;
35
+
30
36
  if (d < rotaryDim) {
31
- let pairIdx = d / 2;
32
- let cos = getCos(t + uniforms.pastLen, pairIdx, 0);
33
- let sin = getSin(t + uniforms.pastLen, pairIdx, 0);
37
+ let idx = (t + uniforms.pastLen) * uniforms.cosShape[1] + d / 2;
38
+ let cos = cos[idx];
39
+ let sin = sin[idx];
34
40
 
35
- let ownX = getX(b, h, t, d) * cos;
41
+ let ownX = x[xIdx] * cos;
42
+ var evenOdd = 0.0;
36
43
 
37
44
  if (d % 2 == 0) {
38
45
  // even index
39
- let even = ownX;
40
- let odd = getX(b, h, t, d + 1);
41
- outVal = even - odd * sin;
46
+ evenOdd = -x[xIdx + 1];
42
47
  } else {
43
48
  // odd index
44
- let even = getX(b, h, t, d - 1);
45
- let odd = ownX;
46
- outVal = even * sin + odd;
49
+ evenOdd = x[xIdx - 1];
47
50
  }
51
+
52
+ outVal = fma(evenOdd, sin, ownX);
48
53
  } else {
49
54
  // pass through for non-rotary dims
50
- outVal = getX(b, h, t, d);
55
+ outVal = x[xIdx];
51
56
  }
52
57
 
53
58
  setOutputAtIndex(index, outVal);
@@ -56,13 +61,22 @@ class f {
56
61
  `;
57
62
  }
58
63
  }
59
- function b(e) {
60
- const { x: t, sin: o, cos: s } = e.inputs, { pastLen: n } = e.attrs, a = e.backend, i = t.shape[0], r = t.shape[1], d = t.shape[2], p = t.shape[3], u = new f(i, r, d, p), c = [{ type: "int32", data: [n] }];
61
- return a.runWebGPUProgram(u, [t, o, s], "float32", c);
64
+ function g(o) {
65
+ const { x: t, sin: e, cos: a } = o.inputs, { pastLen: s } = o.attrs, i = o.backend, p = t.shape[0], d = t.shape[1], n = t.shape[2], r = t.shape[3];
66
+ if (f(e.shape, a.shape, "Error in Rope: "), e.shape[0] < n + s)
67
+ throw new Error(
68
+ `Sin tensor shape ${e.shape} is not compatible with seqLength ${n} and pastLen ${s}.`
69
+ );
70
+ if (e.shape[1] * 2 < r)
71
+ throw new Error(`Sin tensor shape ${e.shape} is not compatible with feature dimension ${r}.`);
72
+ if (e.shape.length !== 3)
73
+ throw new Error(`Sin tensor must be 3-dimensional, but got shape ${e.shape}.`);
74
+ const h = new S(p, d, n, r), u = [{ type: "int32", data: [s] }];
75
+ return i.runWebGPUProgram(h, [t, e, a], "float32", u);
62
76
  }
63
- const x = {
77
+ const b = {
64
78
  kernelName: "Rope",
65
79
  backendName: "webgpu",
66
- kernelFunc: b
80
+ kernelFunc: g
67
81
  };
68
- g(x);
82
+ x(b);
@@ -1,6 +1,7 @@
1
- import { f as c, c as u, g as d } from "../../webgpu_util-g13LvDIv.js";
2
- import { f as p } from "../../index-C0dhsYom.js";
3
- class h {
1
+ import { g as p } from "../../webgpu_program-PFzf1hAQ.js";
2
+ import { f as u, c as d } from "../../webgpu_util-D____QpY.js";
3
+ import { f as h, a2 as o } from "../../index-BoWRt-10.js";
4
+ class b {
4
5
  variableNames = ["labels", "softmaxProbs", "dy"];
5
6
  outputShape;
6
7
  shaderKey = "ScatterSub";
@@ -9,29 +10,31 @@ class h {
9
10
  workgroupSize = [64, 1, 1];
10
11
  size = !0;
11
12
  constructor(t, e) {
12
- this.outputShape = [t, e], this.dispatchLayout = c(this.outputShape), this.dispatch = u(this.dispatchLayout, this.outputShape, this.workgroupSize);
13
+ this.outputShape = [t, e], this.dispatchLayout = u(this.outputShape), this.dispatch = d(this.dispatchLayout, this.outputShape, this.workgroupSize);
13
14
  }
14
15
  getUserCode() {
15
16
  return `
16
- ${d("index")} {
17
+ ${p("index")} {
17
18
  if (index < uniforms.size) {
18
19
  let coords = getCoordsFromIndex(index); // [batch, depth]
19
- let idx = i32(getLabels(coords[0]));
20
- let prob = getSoftmaxProbsByOutputIndex(index);
21
- let dy = getDy(coords[0]);
20
+ let idx = i32(labels[coords[0]]);
21
+ let prob = softmaxProbs[index];
22
+ let dy = dy[coords[0]];
22
23
  setOutputAtIndex(index, select(prob, prob - 1.0, idx == coords[1]) * dy);
23
24
  }
24
25
  }
25
26
  `;
26
27
  }
27
28
  }
28
- function b(o) {
29
- const { logits: t, labels: e, dy: s } = o.inputs, a = o.backend, r = e.shape[0], i = t.shape[1], n = new h(r, i);
30
- return a.runWebGPUProgram(n, [e, t, s], "float32");
29
+ function f(a) {
30
+ const { logits: t, labels: e, dy: s } = a.inputs, c = a.backend, r = e.shape[0], i = t.shape[1];
31
+ o(s.shape, [r], "Error in EfficientScatterSub dy: "), o(t.shape, [r, i], "Error in EfficientScatterSub logits: "), o(e.shape, [r], "Error in EfficientScatterSub labels: ");
32
+ const n = new b(r, i);
33
+ return c.runWebGPUProgram(n, [e, t, s], "float32");
31
34
  }
32
35
  const l = {
33
36
  kernelName: "EfficientScatterSub",
34
37
  backendName: "webgpu",
35
- kernelFunc: b
38
+ kernelFunc: f
36
39
  };
37
- p(l);
40
+ h(l);
@@ -0,0 +1,9 @@
1
+ import { backend_util, TensorInfo } from '@tensorflow/tfjs-core';
2
+ import { WebGPUProgram } from '@tensorflow/tfjs-backend-webgpu/dist/webgpu_program';
3
+ import { WebGPUBackend } from '@tensorflow/tfjs-backend-webgpu';
4
+ export interface ReduceWebGPUProgram extends WebGPUProgram {
5
+ inputShape: number[];
6
+ }
7
+ export declare function createReductionShader(workgroupSizeX: number, reductionOp: 'mean' | 'sum', inputSnippet: string, reducedSnippet: string, outputSnippet: string): string;
8
+ export declare function createReduceInfo(inputs: TensorInfo[], axis: number | number[]): backend_util.ReduceInfo;
9
+ export declare function reduce(program: ReduceWebGPUProgram, inputs: TensorInfo[], backend: WebGPUBackend): TensorInfo;