@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.
- package/dist/Generator.js +13 -9
- package/dist/NanoGPTModel.js +10 -10
- package/dist/{RealDiv-C4hOvYOZ.js → RealDiv-CVYNbZxu.js} +11 -11
- package/dist/{Reshape-BLijOA8h.js → Reshape-CEsEp0AI.js} +2 -2
- package/dist/Reshape-Do18N3gO.js +30 -0
- package/dist/TeachableLLM.js +9 -5
- package/dist/{TiedEmbedding-BLltddza.js → TiedEmbedding-ccLBFiZi.js} +4 -4
- package/dist/{axis_util-DaAl5MER.js → axis_util-5DTW2tFV.js} +1 -1
- package/dist/backend.js +2 -2
- package/dist/{backend_util-DWiwsi2N.js → backend_util-C9Ut8n0Q.js} +40 -40
- package/dist/{broadcast_to-C4v-j9yA.js → broadcast_to-Ba9h_8DO.js} +2 -2
- package/dist/{concat-CsHeR4zV.js → concat-CbXTetof.js} +1 -1
- package/dist/{dataset-JDyjG3QR.js → dataset-U3PrjwgU.js} +7 -7
- package/dist/{dropout-hpDwECTe.js → dropout-DPfPgWWe.js} +11 -11
- package/dist/{gather-D0_gPiBz.js → gather-Bbh8DHhM.js} +4 -4
- package/dist/{gelu-uyHP1x1f.js → gelu-BFwVnd1r.js} +1 -1
- package/dist/{gpgpu_math-DJm3ZTAf.js → gpgpu_math-DffelNS-.js} +2 -2
- package/dist/{index-BPPzKVdR.js → index-DYD_yPa-.js} +1083 -1106
- package/dist/{index-C0dhsYom.js → index-UdZhlibC.js} +126 -126
- package/dist/{kernel_funcs_utils-CwRTFqrc.js → kernel_funcs_utils-CXDy3EN7.js} +3 -3
- package/dist/layers/BaseLayer.js +2 -2
- package/dist/layers/CausalSelfAttention.js +8 -8
- 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 +11 -7
- package/dist/{log_sum_exp-D086OgZJ.js → log_sum_exp-BnmCkHWl.js} +8 -8
- package/dist/main.d.ts +11 -0
- package/dist/main.js +44 -27
- package/dist/{mat_mul-1nwdPkQ_.js → mat_mul-dwmZz69e.js} +1 -1
- package/dist/{max-BQc2Aj-I.js → max-ByjEGoFx.js} +3 -3
- package/dist/{mulmat_packed_gpu-Gzf3I9UV.js → mulmat_packed_gpu-IGPBp6h9.js} +1 -1
- package/dist/{ones-D63HpSF_.js → ones-C8Mfln6-.js} +2 -2
- package/dist/ops/adamAdjust.d.ts +2 -0
- package/dist/ops/adamAdjust.js +9 -0
- package/dist/ops/adamMoments.d.ts +2 -0
- package/dist/ops/adamMoments.js +9 -0
- package/dist/ops/appendCache.js +3 -3
- package/dist/ops/attentionMask.js +1 -1
- package/dist/ops/cpu/adamAdjust.d.ts +1 -0
- package/dist/ops/cpu/adamAdjust.js +18 -0
- package/dist/ops/cpu/adamMoments.d.ts +1 -0
- package/dist/ops/cpu/adamMoments.js +16 -0
- 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 +11 -11
- 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.d.ts +1 -0
- package/dist/ops/webgl/adamAdjust.js +50 -0
- package/dist/ops/webgl/adamMoments.d.ts +1 -0
- package/dist/ops/webgl/adamMoments.js +38 -0
- 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 +8 -8
- 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.d.ts +1 -0
- package/dist/ops/webgpu/adamAdjust.js +52 -0
- package/dist/ops/webgpu/adamMoments.d.ts +1 -0
- package/dist/ops/webgpu/adamMoments.js +51 -0
- package/dist/ops/webgpu/appendCache.js +13 -12
- package/dist/ops/webgpu/attentionMask.js +11 -10
- package/dist/ops/webgpu/gatherSub.js +26 -11
- package/dist/ops/webgpu/gelu.js +7 -6
- package/dist/ops/webgpu/index.js +3 -0
- package/dist/ops/webgpu/normRMS.js +27 -101
- package/dist/ops/webgpu/normRMSGrad.d.ts +1 -0
- package/dist/ops/webgpu/normRMSGrad.js +128 -0
- package/dist/ops/webgpu/qkv.js +9 -8
- package/dist/ops/webgpu/rope.js +8 -7
- package/dist/ops/webgpu/scatterSub.js +8 -7
- package/dist/ops/webgpu/utils/reductions.d.ts +9 -0
- package/dist/ops/webgpu/utils/reductions.js +68 -0
- package/dist/{ops-CIQLNshk.js → ops-aRTXR2Sr.js} +195 -219
- package/dist/{random_width-DkYP8W8N.js → random_width-DbSpgl4o.js} +22 -21
- package/dist/{range-CYzpQY53.js → range-D9CZhVlR.js} +1 -1
- package/dist/{reciprocal-_A9yv27J.js → reciprocal-CGB48wZB.js} +1 -1
- package/dist/{register_all_kernels-guvSxp7M.js → register_all_kernels-DnbAyBXt.js} +30 -29
- package/dist/{reshape-BMUzc1UY.js → reshape-BR0eoLYN.js} +3 -3
- package/dist/{scatter_nd_util-IRBqKz_b.js → scatter_nd_util-OjyAxku2.js} +1 -1
- package/dist/{selu_util-Dt_iuXaq.js → selu_util-Ce6pu9IM.js} +41 -41
- package/dist/{shared-CDu9S76h.js → shared-Czipaeb6.js} +6 -6
- package/dist/{shared-BNa2q6jD.js → shared-DS5waSIY.js} +1 -1
- package/dist/{sin-Cocju-BY.js → sin-CiBxrDqX.js} +6 -6
- package/dist/slice-BHbDHObE.js +28 -0
- package/dist/{softmax-GPNK3o-U.js → softmax-JMEIUo2J.js} +3 -3
- package/dist/{split-CHzJjxDv.js → split-CRU0PjVV.js} +1 -1
- package/dist/{stack-Dpgg_1W1.js → stack-ikk2Y8_P.js} +1 -1
- package/dist/{sum-B8wEpKsg.js → sum-NLYbiDag.js} +3 -3
- package/dist/{tensor-RvZVNmg0.js → tensor-Do9PKbIE.js} +1 -1
- package/dist/{tensor2d-B_kyod7_.js → tensor2d-CWHxHpLh.js} +1 -1
- package/dist/training/Adam.d.ts +22 -0
- package/dist/training/Adam.js +93 -0
- package/dist/training/AdamExt.d.ts +1 -1
- package/dist/training/AdamExt.js +13 -12
- package/dist/training/DatasetBuilder.js +2 -2
- package/dist/training/FullTrainer.js +22 -22
- package/dist/training/Trainer.d.ts +1 -1
- package/dist/training/Trainer.js +32 -32
- package/dist/training/sparseCrossEntropy.d.ts +0 -4
- package/dist/training/sparseCrossEntropy.js +7 -7
- package/dist/utilities/arrayClose.d.ts +1 -0
- package/dist/utilities/arrayClose.js +11 -0
- 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.d.ts +1 -1
- package/dist/utilities/performance.js +11 -11
- package/dist/utilities/profile.js +1 -1
- package/dist/utilities/safetensors.js +2 -2
- package/dist/utilities/weights.js +2 -2
- package/dist/{variable-DXEUOwew.js → variable-BTBkayv_.js} +1 -1
- package/dist/{webgpu_util-g13LvDIv.js → webgpu_program-WaoMq-WD.js} +138 -215
- package/dist/webgpu_util-DhSeP4b6.js +80 -0
- package/dist/{zeros-DCPCdFGq.js → zeros-DnPT2nD4.js} +4 -4
- 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 {
|
|
2
|
-
import { f as m } from "../../
|
|
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,
|
|
13
|
-
const
|
|
14
|
-
this.outputShape = [t, a,
|
|
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
|
-
${
|
|
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
|
-
}
|
|
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:
|
|
49
|
-
return
|
|
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
|
-
|
|
57
|
+
l(g);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { f } from "../../index-
|
|
2
|
-
import {
|
|
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,
|
|
17
|
-
if (this.outputShape = [t, e,
|
|
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
|
-
${
|
|
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(
|
|
52
|
-
const { q: t, k: e } =
|
|
53
|
-
{ type: "float32", data: [
|
|
54
|
-
{ type: "int32", data: [
|
|
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
|
|
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 {
|
|
2
|
-
import { f as
|
|
3
|
-
|
|
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 =
|
|
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
|
-
${
|
|
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
|
-
|
|
22
|
-
|
|
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
|
|
29
|
-
const { logits: e, labels:
|
|
30
|
-
return
|
|
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:
|
|
50
|
+
kernelFunc: c
|
|
36
51
|
};
|
|
37
52
|
p(h);
|
package/dist/ops/webgpu/gelu.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { f as i } from "../../index-
|
|
2
|
-
import {
|
|
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 =
|
|
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
|
-
${
|
|
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 =
|
|
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
|
-
${
|
|
59
|
+
${o("index")} {
|
|
59
60
|
if (index < uniforms.size) {
|
|
60
61
|
let X = getXByOutputIndex(index);
|
|
61
62
|
let x2 = X * X;
|
package/dist/ops/webgpu/index.js
CHANGED
|
@@ -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
|
|
2
|
-
import { f as
|
|
3
|
-
import {
|
|
4
|
-
class
|
|
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(
|
|
13
|
-
this.
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
|
33
|
-
const { x:
|
|
34
|
-
return
|
|
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
|
|
36
|
+
const h = {
|
|
37
37
|
kernelName: "RMSNorm",
|
|
38
38
|
backendName: "webgpu",
|
|
39
|
-
kernelFunc:
|
|
39
|
+
kernelFunc: S
|
|
40
40
|
};
|
|
41
|
-
|
|
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);
|
package/dist/ops/webgpu/qkv.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { f as
|
|
3
|
-
|
|
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 =
|
|
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
|
-
${
|
|
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
|
|
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
|
|
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:
|
|
55
|
+
kernelFunc: l
|
|
55
56
|
};
|
|
56
57
|
p(f);
|