@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.
- package/dist/Generator.d.ts +11 -2
- package/dist/Generator.js +81 -68
- package/dist/NanoGPTModel.js +8 -8
- package/dist/{RealDiv-CVYNbZxu.js → RealDiv-Dy0p8Bvo.js} +7 -7
- package/dist/{Reshape-CEsEp0AI.js → Reshape-DH5srBP0.js} +2 -2
- package/dist/{Reshape-Do18N3gO.js → Reshape-DvudQDvJ.js} +1 -1
- package/dist/TeachableLLM.js +33 -32
- package/dist/{TiedEmbedding-ccLBFiZi.js → TiedEmbedding-BxOerUmB.js} +4 -4
- package/dist/Trainer.d.ts +6 -1
- package/dist/Trainer.js +53 -19
- package/dist/{axis_util-5DTW2tFV.js → axis_util-BzbKo31C.js} +1 -1
- package/dist/backend.js +2 -2
- package/dist/{backend_util-C9Ut8n0Q.js → backend_util-TE7aTPhZ.js} +4 -4
- package/dist/{broadcast_to-Ba9h_8DO.js → broadcast_to-CdbwV-Dj.js} +2 -2
- package/dist/{concat-CbXTetof.js → concat-CsxrgovM.js} +1 -1
- package/dist/{dataset-U3PrjwgU.js → dataset-CtdBYwjo.js} +3 -3
- package/dist/{dropout-DPfPgWWe.js → dropout-DYs5QFGQ.js} +1 -1
- package/dist/{gather-Bbh8DHhM.js → gather-CMMy2KEG.js} +1 -1
- package/dist/{gelu-BFwVnd1r.js → gelu-C-dPj6Ku.js} +1 -1
- package/dist/{gpgpu_math-DffelNS-.js → gpgpu_math-DGNLNL4I.js} +2 -2
- package/dist/{index-UdZhlibC.js → index-BoWRt-10.js} +4 -4
- package/dist/{index-DYD_yPa-.js → index-CLthM0TO.js} +10 -10
- package/dist/{kernel_funcs_utils-CXDy3EN7.js → kernel_funcs_utils-BYKWV8Aa.js} +3 -3
- package/dist/layers/BaseLayer.js +2 -2
- package/dist/layers/CausalSelfAttention.js +6 -6
- package/dist/layers/MLP.js +5 -5
- package/dist/layers/RMSNorm.js +3 -3
- package/dist/layers/RoPECache.js +4 -4
- package/dist/layers/TiedEmbedding.js +5 -5
- package/dist/layers/TransformerBlock.js +1 -1
- package/dist/loader/loadTransformers.js +1 -1
- package/dist/loader/oldZipLoad.js +5 -5
- package/dist/{log_sum_exp-BnmCkHWl.js → log_sum_exp-DbjkV734.js} +5 -5
- package/dist/main.js +5 -5
- package/dist/{mat_mul-dwmZz69e.js → mat_mul-8m8pfdcx.js} +1 -1
- package/dist/{max-ByjEGoFx.js → max-Ddnnb5xe.js} +1 -1
- package/dist/{mulmat_packed_gpu-IGPBp6h9.js → mulmat_packed_gpu-VSekgsNv.js} +1 -1
- package/dist/{ones-C8Mfln6-.js → ones-Dj0SDhHf.js} +2 -2
- package/dist/ops/adamAdjust.js +1 -1
- package/dist/ops/adamMoments.js +1 -1
- package/dist/ops/appendCache.js +3 -3
- package/dist/ops/attentionMask.js +1 -1
- package/dist/ops/cpu/adamAdjust.js +1 -1
- package/dist/ops/cpu/adamMoments.js +2 -2
- package/dist/ops/cpu/appendCache.js +2 -2
- package/dist/ops/cpu/attentionMask.js +5 -5
- package/dist/ops/cpu/fusedSoftmax.js +2 -2
- package/dist/ops/cpu/gatherSub.js +3 -3
- package/dist/ops/cpu/gelu.js +1 -1
- package/dist/ops/cpu/matMulGelu.js +2 -2
- package/dist/ops/cpu/matMulMul.js +1 -1
- package/dist/ops/cpu/mulDropout.js +1 -1
- package/dist/ops/cpu/normRMS.js +1 -1
- package/dist/ops/cpu/qkv.js +3 -3
- package/dist/ops/cpu/rope.js +5 -5
- package/dist/ops/cpu/scatterSub.js +5 -5
- package/dist/ops/fusedSoftmax.js +1 -1
- package/dist/ops/gatherSub.js +1 -1
- package/dist/ops/gelu.js +2 -2
- package/dist/ops/grads/attentionMask.js +1 -1
- package/dist/ops/grads/fusedSoftmax.js +2 -2
- package/dist/ops/grads/gelu.js +2 -2
- package/dist/ops/grads/matMulGelu.js +1 -1
- package/dist/ops/grads/normRMS.js +1 -1
- package/dist/ops/grads/qkv.js +1 -1
- package/dist/ops/grads/rope.js +1 -1
- package/dist/ops/matMulGelu.js +1 -1
- package/dist/ops/matMulMul.js +1 -1
- package/dist/ops/mulDrop.js +1 -1
- package/dist/ops/normRMS.js +1 -1
- package/dist/ops/qkv.js +1 -1
- package/dist/ops/rope.js +4 -4
- package/dist/ops/scatterSub.js +1 -1
- package/dist/ops/webgl/adamAdjust.js +2 -2
- package/dist/ops/webgl/adamMoments.js +7 -5
- package/dist/ops/webgl/appendCache.js +1 -1
- package/dist/ops/webgl/attentionMask.js +1 -1
- package/dist/ops/webgl/fusedSoftmax.js +4 -4
- package/dist/ops/webgl/gatherSub.js +1 -1
- package/dist/ops/webgl/gelu.js +2 -2
- package/dist/ops/webgl/log.js +3 -3
- package/dist/ops/webgl/matMulGelu.js +4 -4
- package/dist/ops/webgl/matMulMul.js +1 -1
- package/dist/ops/webgl/mulDropout.js +1 -1
- package/dist/ops/webgl/normRMS.js +2 -2
- package/dist/ops/webgl/qkv.js +1 -1
- package/dist/ops/webgl/rope.js +1 -1
- package/dist/ops/webgl/scatterSub.js +1 -1
- package/dist/ops/webgpu/adamAdjust.js +15 -13
- package/dist/ops/webgpu/adamMoments.js +18 -11
- package/dist/ops/webgpu/appendCache.js +18 -15
- package/dist/ops/webgpu/attentionMask.js +24 -18
- package/dist/ops/webgpu/gatherSub.js +17 -30
- package/dist/ops/webgpu/gelu.js +3 -3
- package/dist/ops/webgpu/normRMS.js +16 -8
- package/dist/ops/webgpu/normRMSGrad.js +25 -20
- package/dist/ops/webgpu/qkv.js +23 -19
- package/dist/ops/webgpu/rope.js +37 -24
- package/dist/ops/webgpu/scatterSub.js +16 -14
- package/dist/ops/webgpu/utils/reductions.js +4 -4
- package/dist/{ops-aRTXR2Sr.js → ops-BFGCx8Ri.js} +15 -15
- package/dist/{random_width-DbSpgl4o.js → random_width-sZORGo5k.js} +22 -22
- package/dist/{range-D9CZhVlR.js → range-CRuAh-gd.js} +1 -1
- package/dist/{reciprocal-CGB48wZB.js → reciprocal-BvGAyKyu.js} +1 -1
- package/dist/{register_all_kernels-DnbAyBXt.js → register_all_kernels-BwDSRN-f.js} +30 -30
- package/dist/{reshape-BR0eoLYN.js → reshape-CdBq1WJ6.js} +1 -1
- package/dist/{scatter_nd_util-OjyAxku2.js → scatter_nd_util-DUstGbU1.js} +1 -1
- package/dist/{selu_util-Ce6pu9IM.js → selu_util-BJEXVvjX.js} +3 -3
- package/dist/{shared-Czipaeb6.js → shared-B8ztnyEk.js} +6 -6
- package/dist/{shared-DS5waSIY.js → shared-wS99K7_n.js} +1 -1
- package/dist/{sin-CiBxrDqX.js → sin-BeA3tsEd.js} +1 -1
- package/dist/{slice-BHbDHObE.js → slice-BiOsknYS.js} +1 -1
- package/dist/{softmax-JMEIUo2J.js → softmax-Bv_6lyMX.js} +1 -1
- package/dist/{split-CRU0PjVV.js → split-B-dikLRw.js} +1 -1
- package/dist/{stack-ikk2Y8_P.js → stack-B17UN2nn.js} +1 -1
- package/dist/{sum-NLYbiDag.js → sum-66ew2byf.js} +1 -1
- package/dist/{tensor-Do9PKbIE.js → tensor-JwS7ZYY6.js} +1 -1
- package/dist/{tensor2d-CWHxHpLh.js → tensor2d-wxPAnDQy.js} +1 -1
- package/dist/training/Adam.js +2 -2
- package/dist/training/AdamExt.js +1 -1
- package/dist/training/DatasetBuilder.js +35 -32
- package/dist/training/FullTrainer.d.ts +15 -2
- package/dist/training/FullTrainer.js +97 -51
- package/dist/training/Trainer.d.ts +10 -0
- package/dist/training/Trainer.js +2 -2
- package/dist/training/sparseCrossEntropy.js +4 -4
- package/dist/utilities/dummy.js +2 -2
- package/dist/utilities/generate.js +3 -3
- package/dist/utilities/multinomialCPU.js +2 -2
- package/dist/utilities/performance.js +1 -1
- package/dist/utilities/profile.js +1 -1
- package/dist/utilities/safetensors.js +2 -2
- package/dist/utilities/weights.js +2 -2
- package/dist/{variable-BTBkayv_.js → variable-BuddVFLa.js} +1 -1
- package/dist/{webgpu_program-WaoMq-WD.js → webgpu_program-PFzf1hAQ.js} +1 -1
- package/dist/{webgpu_util-DhSeP4b6.js → webgpu_util-D____QpY.js} +1 -1
- package/dist/{zeros-DnPT2nD4.js → zeros--BdLQ3oG.js} +1 -1
- package/package.json +1 -1
package/dist/ops/scatterSub.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { f as m } from "../../index-
|
|
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}]`,
|
|
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
|
-
|
|
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,
|
|
31
|
-
return
|
|
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,7 +1,7 @@
|
|
|
1
|
-
import { m as b, s as I, r as k } from "../../RealDiv-
|
|
2
|
-
import { r as v } from "../../Reshape-
|
|
3
|
-
import { f as w, p as P } from "../../index-
|
|
4
|
-
import { e as S } from "../../axis_util-
|
|
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;
|
package/dist/ops/webgl/gelu.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { f as a } from "../../index-
|
|
2
|
-
import { u as s, C as x } from "../../kernel_funcs_utils-
|
|
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;
|
package/dist/ops/webgl/log.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { f as e, af as r } from "../../index-
|
|
2
|
-
import { u as s, l as N } from "../../kernel_funcs_utils-
|
|
3
|
-
import { y as l } from "../../shared-
|
|
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-
|
|
2
|
-
import { r as f } from "../../Reshape-
|
|
3
|
-
import { M as U } from "../../mulmat_packed_gpu-
|
|
4
|
-
import { m as E } from "../../mat_mul-
|
|
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,5 +1,5 @@
|
|
|
1
|
-
import { f as p, e as G } from "../../index-
|
|
2
|
-
import { s as x } from "../../sum-
|
|
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;
|
package/dist/ops/webgl/qkv.js
CHANGED
package/dist/ops/webgl/rope.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { g as p } from "../../webgpu_program-
|
|
2
|
-
import { f as d, c as l } from "../../webgpu_util-
|
|
3
|
-
import { f } from "../../index-
|
|
4
|
-
class
|
|
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 / (
|
|
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
|
|
39
|
-
const { moments: e, value: a } = t.inputs, { beta1: n, beta2:
|
|
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 /
|
|
42
|
-
{ type: "float32", data: [
|
|
43
|
-
{ type: "float32", data: [
|
|
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
|
|
49
|
+
const b = {
|
|
48
50
|
kernelName: "AdamAdjust",
|
|
49
51
|
backendName: "webgpu",
|
|
50
|
-
kernelFunc:
|
|
52
|
+
kernelFunc: v
|
|
51
53
|
};
|
|
52
|
-
f(
|
|
54
|
+
f(b);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { g as m } from "../../webgpu_program-
|
|
2
|
-
import { f as u, c as p } from "../../webgpu_util-
|
|
3
|
-
import { f as c } from "../../index-
|
|
4
|
-
class
|
|
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
|
-
|
|
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:
|
|
41
|
-
|
|
42
|
-
|
|
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
|
|
51
|
+
return r.runWebGPUProgram(s, [e, o], "float32", i);
|
|
45
52
|
}
|
|
46
|
-
const
|
|
53
|
+
const l = {
|
|
47
54
|
kernelName: "AdamMoments",
|
|
48
55
|
backendName: "webgpu",
|
|
49
56
|
kernelFunc: f
|
|
50
57
|
};
|
|
51
|
-
|
|
58
|
+
d(l);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { g as u } from "../../webgpu_program-
|
|
2
|
-
import { f as d, c as m } from "../../webgpu_util-
|
|
3
|
-
import { f as l } from "../../index-
|
|
4
|
-
class
|
|
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,
|
|
14
|
-
const
|
|
15
|
-
this.shaderKey = `AppendCache_${
|
|
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
|
|
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 = ${
|
|
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
|
|
49
|
-
const { cache:
|
|
50
|
-
|
|
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
|
|
55
|
+
const T = {
|
|
53
56
|
kernelName: "AppendCache",
|
|
54
57
|
backendName: "webgpu",
|
|
55
|
-
kernelFunc:
|
|
58
|
+
kernelFunc: S
|
|
56
59
|
};
|
|
57
|
-
l(
|
|
60
|
+
l(T);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { f } from "../../index-
|
|
2
|
-
import { g as
|
|
3
|
-
import { f as
|
|
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,
|
|
18
|
-
if (this.shaderKey = `AttentionMask_${
|
|
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
|
-
${
|
|
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
|
|
40
|
-
let
|
|
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
|
|
53
|
-
const { q: t, k: e } =
|
|
54
|
-
|
|
55
|
-
|
|
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
|
|
64
|
+
return i.runWebGPUProgram(c, [t, e], "float32", d);
|
|
59
65
|
}
|
|
60
|
-
const
|
|
66
|
+
const q = {
|
|
61
67
|
kernelName: "AttentionMask",
|
|
62
68
|
backendName: "webgpu",
|
|
63
|
-
kernelFunc:
|
|
69
|
+
kernelFunc: b
|
|
64
70
|
};
|
|
65
|
-
f(
|
|
71
|
+
f(q);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { g as
|
|
2
|
-
import { f as
|
|
3
|
-
import { f as
|
|
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 =
|
|
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
|
-
${
|
|
17
|
+
${u("index")} {
|
|
18
18
|
if (index < uniforms.size) {
|
|
19
|
-
let
|
|
20
|
-
let
|
|
21
|
-
let
|
|
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
|
|
44
|
-
const { logits: e, labels:
|
|
45
|
-
|
|
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
|
|
34
|
+
const f = {
|
|
48
35
|
kernelName: "EfficientGatherSub",
|
|
49
36
|
backendName: "webgpu",
|
|
50
|
-
kernelFunc:
|
|
37
|
+
kernelFunc: d
|
|
51
38
|
};
|
|
52
|
-
|
|
39
|
+
c(f);
|
package/dist/ops/webgpu/gelu.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { f as i } from "../../index-
|
|
2
|
-
import { g as o } from "../../webgpu_program-
|
|
3
|
-
import { f as s, c as p } from "../../webgpu_util-
|
|
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
|
|
2
|
-
import { f as
|
|
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 =
|
|
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(
|
|
33
|
-
const { x: e, gamma:
|
|
34
|
-
|
|
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
|
|
44
|
+
const l = {
|
|
37
45
|
kernelName: "RMSNorm",
|
|
38
46
|
backendName: "webgpu",
|
|
39
47
|
kernelFunc: S
|
|
40
48
|
};
|
|
41
|
-
|
|
49
|
+
p(l);
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { f as
|
|
2
|
-
import { createReduceInfo as
|
|
3
|
-
import { f as
|
|
4
|
-
import { g as
|
|
5
|
-
import { s as
|
|
6
|
-
import { s as
|
|
7
|
-
class
|
|
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 =
|
|
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
|
-
${
|
|
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(
|
|
109
|
-
const { dy: r, x: e, gamma:
|
|
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
|
-
]
|
|
115
|
-
|
|
116
|
-
|
|
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
|
|
119
|
-
|
|
120
|
-
const
|
|
121
|
-
return
|
|
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
|
|
128
|
+
const b = {
|
|
124
129
|
kernelName: "RMSNormGrad",
|
|
125
130
|
backendName: "webgpu",
|
|
126
131
|
kernelFunc: v
|
|
127
132
|
};
|
|
128
|
-
|
|
133
|
+
f(b);
|