@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.
- package/dist/Generator.js +13 -9
- package/dist/NanoGPTModel.js +10 -10
- package/dist/{RealDiv-C4hOvYOZ.js → RealDiv-Dy0p8Bvo.js} +11 -11
- package/dist/{Reshape-BLijOA8h.js → Reshape-DH5srBP0.js} +2 -2
- package/dist/Reshape-DvudQDvJ.js +30 -0
- package/dist/TeachableLLM.js +9 -5
- package/dist/{TiedEmbedding-BLltddza.js → TiedEmbedding-BxOerUmB.js} +4 -4
- package/dist/{axis_util-DaAl5MER.js → axis_util-BzbKo31C.js} +1 -1
- package/dist/backend.js +2 -2
- package/dist/{backend_util-DWiwsi2N.js → backend_util-TE7aTPhZ.js} +40 -40
- package/dist/{broadcast_to-C4v-j9yA.js → broadcast_to-CdbwV-Dj.js} +2 -2
- package/dist/{concat-CsHeR4zV.js → concat-CsxrgovM.js} +1 -1
- package/dist/{dataset-JDyjG3QR.js → dataset-CtdBYwjo.js} +7 -7
- package/dist/{dropout-hpDwECTe.js → dropout-DYs5QFGQ.js} +11 -11
- package/dist/{gather-D0_gPiBz.js → gather-CMMy2KEG.js} +4 -4
- package/dist/{gelu-uyHP1x1f.js → gelu-C-dPj6Ku.js} +1 -1
- package/dist/{gpgpu_math-DJm3ZTAf.js → gpgpu_math-DGNLNL4I.js} +2 -2
- package/dist/{index-C0dhsYom.js → index-BoWRt-10.js} +126 -126
- package/dist/{index-BPPzKVdR.js → index-CLthM0TO.js} +1083 -1106
- package/dist/{kernel_funcs_utils-CwRTFqrc.js → kernel_funcs_utils-BYKWV8Aa.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-DbjkV734.js} +8 -8
- package/dist/main.d.ts +11 -0
- package/dist/main.js +44 -27
- package/dist/{mat_mul-1nwdPkQ_.js → mat_mul-8m8pfdcx.js} +1 -1
- package/dist/{max-BQc2Aj-I.js → max-Ddnnb5xe.js} +3 -3
- package/dist/{mulmat_packed_gpu-Gzf3I9UV.js → mulmat_packed_gpu-VSekgsNv.js} +1 -1
- package/dist/{ones-D63HpSF_.js → ones-Dj0SDhHf.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 +40 -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 +54 -0
- package/dist/ops/webgpu/adamMoments.d.ts +1 -0
- package/dist/ops/webgpu/adamMoments.js +58 -0
- package/dist/ops/webgpu/appendCache.js +22 -18
- package/dist/ops/webgpu/attentionMask.js +24 -17
- package/dist/ops/webgpu/gatherSub.js +17 -15
- package/dist/ops/webgpu/gelu.js +7 -6
- package/dist/ops/webgpu/index.js +3 -0
- package/dist/ops/webgpu/normRMS.js +35 -101
- package/dist/ops/webgpu/normRMSGrad.d.ts +1 -0
- package/dist/ops/webgpu/normRMSGrad.js +133 -0
- package/dist/ops/webgpu/qkv.js +21 -16
- package/dist/ops/webgpu/rope.js +37 -23
- package/dist/ops/webgpu/scatterSub.js +16 -13
- 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-BFGCx8Ri.js} +195 -219
- package/dist/{random_width-DkYP8W8N.js → random_width-sZORGo5k.js} +22 -21
- package/dist/{range-CYzpQY53.js → range-CRuAh-gd.js} +1 -1
- package/dist/{reciprocal-_A9yv27J.js → reciprocal-BvGAyKyu.js} +1 -1
- package/dist/{register_all_kernels-guvSxp7M.js → register_all_kernels-BwDSRN-f.js} +30 -29
- package/dist/{reshape-BMUzc1UY.js → reshape-CdBq1WJ6.js} +3 -3
- package/dist/{scatter_nd_util-IRBqKz_b.js → scatter_nd_util-DUstGbU1.js} +1 -1
- package/dist/{selu_util-Dt_iuXaq.js → selu_util-BJEXVvjX.js} +41 -41
- package/dist/{shared-CDu9S76h.js → shared-B8ztnyEk.js} +6 -6
- package/dist/{shared-BNa2q6jD.js → shared-wS99K7_n.js} +1 -1
- package/dist/{sin-Cocju-BY.js → sin-BeA3tsEd.js} +6 -6
- package/dist/slice-BiOsknYS.js +28 -0
- package/dist/{softmax-GPNK3o-U.js → softmax-Bv_6lyMX.js} +3 -3
- package/dist/{split-CHzJjxDv.js → split-B-dikLRw.js} +1 -1
- package/dist/{stack-Dpgg_1W1.js → stack-B17UN2nn.js} +1 -1
- package/dist/{sum-B8wEpKsg.js → sum-66ew2byf.js} +3 -3
- package/dist/{tensor-RvZVNmg0.js → tensor-JwS7ZYY6.js} +1 -1
- package/dist/{tensor2d-B_kyod7_.js → tensor2d-wxPAnDQy.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 +35 -32
- 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-BuddVFLa.js} +1 -1
- package/dist/{webgpu_util-g13LvDIv.js → webgpu_program-PFzf1hAQ.js} +138 -215
- package/dist/webgpu_util-D____QpY.js +80 -0
- package/dist/{zeros-DCPCdFGq.js → zeros--BdLQ3oG.js} +4 -4
- package/package.json +1 -1
package/dist/ops/gelu.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import "../index-
|
|
1
|
+
import "../index-BoWRt-10.js";
|
|
2
2
|
import "./cpu/gelu.js";
|
|
3
3
|
import "./webgl/gelu.js";
|
|
4
|
-
import { d as e, g as i } from "../gelu-
|
|
4
|
+
import { d as e, g as i } from "../gelu-C-dPj6Ku.js";
|
|
5
5
|
export {
|
|
6
6
|
e as dGelu,
|
|
7
7
|
i as gelu
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { i as f, a as i, c as l } from "../../index-
|
|
1
|
+
import { i as f, a as i, c as l } from "../../index-BoWRt-10.js";
|
|
2
2
|
import { mulDrop as g } from "../mulDrop.js";
|
|
3
|
-
import { s as T } from "../../sum-
|
|
3
|
+
import { s as T } from "../../sum-66ew2byf.js";
|
|
4
4
|
const Y = {
|
|
5
5
|
kernelName: "FusedSoftmax",
|
|
6
6
|
outputsToSave: [!0],
|
package/dist/ops/grads/gelu.js
CHANGED
package/dist/ops/grads/qkv.js
CHANGED
package/dist/ops/grads/rope.js
CHANGED
package/dist/ops/matMulGelu.js
CHANGED
package/dist/ops/matMulMul.js
CHANGED
package/dist/ops/mulDrop.js
CHANGED
package/dist/ops/normRMS.js
CHANGED
package/dist/ops/qkv.js
CHANGED
package/dist/ops/rope.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { e as p } from "../index-
|
|
2
|
-
import "../random_width-
|
|
3
|
-
import "../register_all_kernels-
|
|
1
|
+
import { e as p } from "../index-BoWRt-10.js";
|
|
2
|
+
import "../random_width-sZORGo5k.js";
|
|
3
|
+
import "../register_all_kernels-BwDSRN-f.js";
|
|
4
4
|
import "../index-Tf7vU29b.js";
|
|
5
|
-
import "../dataset-
|
|
5
|
+
import "../dataset-CtdBYwjo.js";
|
|
6
6
|
import "./cpu/rope.js";
|
|
7
7
|
import "./webgl/rope.js";
|
|
8
8
|
import "./grads/rope.js";
|
package/dist/ops/scatterSub.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { r as n } from "../../Reshape-DH5srBP0.js";
|
|
2
|
+
import { f } from "../../index-BoWRt-10.js";
|
|
3
|
+
class v {
|
|
4
|
+
variableNames = ["moments", "value"];
|
|
5
|
+
outputShape;
|
|
6
|
+
userCode;
|
|
7
|
+
customUniforms = [
|
|
8
|
+
{ name: "invBeta1", type: "float" },
|
|
9
|
+
{ name: "invBeta2", type: "float" },
|
|
10
|
+
{ name: "learningRate", type: "float" },
|
|
11
|
+
{ name: "epsilon", type: "float" }
|
|
12
|
+
];
|
|
13
|
+
constructor(a) {
|
|
14
|
+
this.outputShape = a, this.userCode = `
|
|
15
|
+
void main() {
|
|
16
|
+
float v = getValueAtOutCoords();
|
|
17
|
+
int coords = getOutputCoords();
|
|
18
|
+
coords *= 2;
|
|
19
|
+
float m1 = getMoments(coords);
|
|
20
|
+
float m2 = getMoments(coords + 1);
|
|
21
|
+
|
|
22
|
+
float m1Hat = m1 * invBeta1;
|
|
23
|
+
float m2Hat = m2 * invBeta2;
|
|
24
|
+
|
|
25
|
+
float invSqrt = inversesqrt(max(m2Hat, 1e-30));
|
|
26
|
+
float invDenom = invSqrt / (1.0 + epsilon * invSqrt);
|
|
27
|
+
float adjustedValue = -learningRate * m1Hat * invDenom + v;
|
|
28
|
+
|
|
29
|
+
setOutput(adjustedValue);
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function h(t) {
|
|
35
|
+
const { moments: a, value: s } = t.inputs, { beta1: i, beta2: l, learningRate: u, epsilon: p } = t.attrs, e = t.backend, r = n({ inputs: { x: a }, backend: e, attrs: { shape: [-1] } }), o = n({ inputs: { x: s }, backend: e, attrs: { shape: [-1] } }), d = new v(o.shape), m = e.runWebGLProgram(d, [r, o], "float32", [
|
|
36
|
+
[1 / i],
|
|
37
|
+
[1 / l],
|
|
38
|
+
[u],
|
|
39
|
+
[p]
|
|
40
|
+
]);
|
|
41
|
+
e.disposeIntermediateTensorInfo(r), e.disposeIntermediateTensorInfo(o);
|
|
42
|
+
const c = n({ inputs: { x: m }, backend: e, attrs: { shape: s.shape } });
|
|
43
|
+
return e.disposeIntermediateTensorInfo(m), c;
|
|
44
|
+
}
|
|
45
|
+
const g = {
|
|
46
|
+
kernelName: "AdamAdjust",
|
|
47
|
+
backendName: "webgl",
|
|
48
|
+
kernelFunc: h
|
|
49
|
+
};
|
|
50
|
+
f(g);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { f as m } from "../../index-BoWRt-10.js";
|
|
2
|
+
class i {
|
|
3
|
+
variableNames = ["moments", "gradient"];
|
|
4
|
+
outputShape;
|
|
5
|
+
userCode;
|
|
6
|
+
customUniforms = [
|
|
7
|
+
{ name: "beta1", type: "float" },
|
|
8
|
+
{ name: "beta2", type: "float" }
|
|
9
|
+
];
|
|
10
|
+
constructor(e) {
|
|
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}]`, r = t === 1 ? "coords" : e.slice(0, -1).map((s, c) => `coords[${c}]`).join(", ");
|
|
13
|
+
this.userCode = `
|
|
14
|
+
void main() {
|
|
15
|
+
float m = getMomentsAtOutCoords();
|
|
16
|
+
${n} coords = getOutputCoords();
|
|
17
|
+
|
|
18
|
+
// Add gradient clipping here
|
|
19
|
+
float g = clamp(getGradient(${r}), -1.0, 1.0);
|
|
20
|
+
int which = ${a};
|
|
21
|
+
|
|
22
|
+
float beta = which == 0 ? beta1 : beta2;
|
|
23
|
+
float gg = which == 0 ? g : g * g;
|
|
24
|
+
|
|
25
|
+
float newM = m * beta + gg * (1.0 - beta);
|
|
26
|
+
setOutput(newM);
|
|
27
|
+
}
|
|
28
|
+
`;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function d(o) {
|
|
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]]);
|
|
34
|
+
}
|
|
35
|
+
const g = {
|
|
36
|
+
kernelName: "AdamMoments",
|
|
37
|
+
backendName: "webgl",
|
|
38
|
+
kernelFunc: d
|
|
39
|
+
};
|
|
40
|
+
m(g);
|
|
@@ -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;
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { f as l } from "../../index-
|
|
1
|
+
import { f as l } from "../../index-BoWRt-10.js";
|
|
2
2
|
class u {
|
|
3
3
|
variableNames = ["labels", "logits", "values"];
|
|
4
4
|
outputShape;
|
|
5
5
|
userCode;
|
|
6
6
|
constructor(e) {
|
|
7
7
|
this.outputShape = [e], this.userCode = `
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
void main() {
|
|
9
|
+
int coords = getOutputCoords();
|
|
10
|
+
int index = int(getLabelsAtOutCoords());
|
|
11
|
+
float val = getValuesAtOutCoords();
|
|
12
|
+
float logit = getLogits(coords, index);
|
|
13
|
+
setOutput(val - logit);
|
|
14
|
+
}
|
|
15
15
|
`;
|
|
16
16
|
}
|
|
17
17
|
}
|
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,
|
|
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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
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 / fma(uniforms.epsilon, invSqrt, 1.0);
|
|
30
|
+
let adjustedValue = fma(-uniforms.learningRate * m1Hat, invDenom, value);
|
|
31
|
+
|
|
32
|
+
setOutputAtIndex(index, adjustedValue);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
`;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
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 = [
|
|
42
|
+
{ type: "float32", data: [1 / n] },
|
|
43
|
+
{ type: "float32", data: [1 / s] },
|
|
44
|
+
{ type: "float32", data: [o] },
|
|
45
|
+
{ type: "float32", data: [i] }
|
|
46
|
+
];
|
|
47
|
+
return r.runWebGPUProgram(u, [e, a], "float32", m);
|
|
48
|
+
}
|
|
49
|
+
const b = {
|
|
50
|
+
kernelName: "AdamAdjust",
|
|
51
|
+
backendName: "webgpu",
|
|
52
|
+
kernelFunc: v
|
|
53
|
+
};
|
|
54
|
+
f(b);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
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
|
+
|
|
29
|
+
// Add gradient clipping here
|
|
30
|
+
let g: f32 = clamp(gradient[index], -1.0, 1.0);
|
|
31
|
+
|
|
32
|
+
let newM1 = fma(m.x, uniforms.beta1, g * (1.0 - uniforms.beta1));
|
|
33
|
+
let newM2 = fma(m.y, uniforms.beta2, g * g * (1.0 - uniforms.beta2));
|
|
34
|
+
|
|
35
|
+
setOutputAtIndex(index, vec2<f32>(newM1, newM2));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
`;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function f(t) {
|
|
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] }
|
|
50
|
+
];
|
|
51
|
+
return r.runWebGPUProgram(s, [e, o], "float32", i);
|
|
52
|
+
}
|
|
53
|
+
const l = {
|
|
54
|
+
kernelName: "AdamMoments",
|
|
55
|
+
backendName: "webgpu",
|
|
56
|
+
kernelFunc: f
|
|
57
|
+
};
|
|
58
|
+
d(l);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { f as m } from "../../
|
|
3
|
-
|
|
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 {
|
|
4
5
|
variableNames = ["cache", "item"];
|
|
5
6
|
outputShape;
|
|
6
7
|
shaderKey = "AppendCache";
|
|
@@ -9,14 +10,14 @@ class f {
|
|
|
9
10
|
workgroupSize = [64, 1, 1];
|
|
10
11
|
size = !0;
|
|
11
12
|
uniforms = "cacheT: i32";
|
|
12
|
-
constructor(
|
|
13
|
-
const
|
|
14
|
-
this.outputShape = [
|
|
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);
|
|
15
16
|
}
|
|
16
17
|
getUserCode() {
|
|
17
|
-
const
|
|
18
|
+
const e = 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];
|
|
@@ -25,7 +26,7 @@ class f {
|
|
|
25
26
|
let d = coords[3];
|
|
26
27
|
|
|
27
28
|
let itemT = 1;
|
|
28
|
-
let maxSize = ${
|
|
29
|
+
let maxSize = ${e};
|
|
29
30
|
let totalT = uniforms.cacheT + itemT;
|
|
30
31
|
let start = select(0, 1, totalT >= maxSize);
|
|
31
32
|
|
|
@@ -33,24 +34,27 @@ 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
|
}
|
|
44
45
|
`;
|
|
45
46
|
}
|
|
46
47
|
}
|
|
47
|
-
function
|
|
48
|
-
const { cache:
|
|
49
|
-
|
|
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);
|
|
50
54
|
}
|
|
51
|
-
const
|
|
55
|
+
const T = {
|
|
52
56
|
kernelName: "AppendCache",
|
|
53
57
|
backendName: "webgpu",
|
|
54
|
-
kernelFunc:
|
|
58
|
+
kernelFunc: S
|
|
55
59
|
};
|
|
56
|
-
|
|
60
|
+
l(T);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { f } from "../../index-
|
|
2
|
-
import {
|
|
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";
|
|
3
4
|
class g {
|
|
4
5
|
variableNames = ["q", "k"];
|
|
5
6
|
outputShape;
|
|
@@ -13,13 +14,13 @@ class g {
|
|
|
13
14
|
nh;
|
|
14
15
|
T1;
|
|
15
16
|
T2;
|
|
16
|
-
constructor(t, e, o, i
|
|
17
|
-
if (this.outputShape = [t, e,
|
|
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)
|
|
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
|
+
${k("index")} {
|
|
23
24
|
|
|
24
25
|
let coords = getCoordsFromIndex(index);
|
|
25
26
|
let b = coords[0];
|
|
@@ -32,13 +33,14 @@ class g {
|
|
|
32
33
|
setOutputAtIndex(index, uniforms.inf);
|
|
33
34
|
return;
|
|
34
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);
|
|
35
39
|
|
|
36
40
|
var sum: f32 = 0.0;
|
|
37
41
|
for (var i: i32 = 0; i < ${this.hs}; i = i + 4) {
|
|
38
|
-
let
|
|
39
|
-
let
|
|
40
|
-
let k0 = getIndexFromCoords4D(vec4<i32>(b, h, t2, i), uniforms.kShape);
|
|
41
|
-
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]);
|
|
42
44
|
sum = sum + dot(qv, kv);
|
|
43
45
|
}
|
|
44
46
|
let scaled = sum * uniforms.divisor;
|
|
@@ -48,17 +50,22 @@ class g {
|
|
|
48
50
|
`;
|
|
49
51
|
}
|
|
50
52
|
}
|
|
51
|
-
function
|
|
52
|
-
const { q: t, k: e } =
|
|
53
|
-
|
|
54
|
-
|
|
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] },
|
|
55
62
|
{ type: "float32", data: [Number.NEGATIVE_INFINITY] }
|
|
56
63
|
];
|
|
57
|
-
return
|
|
64
|
+
return i.runWebGPUProgram(c, [t, e], "float32", d);
|
|
58
65
|
}
|
|
59
|
-
const
|
|
66
|
+
const q = {
|
|
60
67
|
kernelName: "AttentionMask",
|
|
61
68
|
backendName: "webgpu",
|
|
62
|
-
kernelFunc:
|
|
69
|
+
kernelFunc: b
|
|
63
70
|
};
|
|
64
|
-
f(
|
|
71
|
+
f(q);
|