@genai-fi/nanogpt 1.4.1 → 1.6.0
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/{DatasetBuilder-DU1G1OKX.js → DatasetBuilder-DRDW8tcM.js} +85 -86
- package/dist/Generator-BcR_14Cj.js +316 -0
- package/dist/{RealDiv-CSnvtN2E.js → RealDiv-Cl8k3YTx.js} +54 -62
- package/dist/{Reshape-BlylqwWy.js → Reshape-CKxKPKqi.js} +9 -12
- package/dist/Reshape-H5zZRgr9.js +16 -0
- package/dist/RoPECache-dYscFINl.js +722 -0
- package/dist/TeachableLLM.js +29 -34
- package/dist/api/beamer.d.ts +1 -0
- package/dist/api/beamer.js +19 -10
- package/dist/api/responses.js +11 -12
- package/dist/api/training.js +12 -14
- package/dist/axis_util-rF9RnwKE.js +37 -0
- package/dist/backend.js +5 -6
- package/dist/{backend_util-DAV0I34P.js → backend_util-Cpp0Gwlp.js} +180 -185
- package/dist/{binary_op_util-CrYk9LXL.js → binary_op_util-WY9YIl0q.js} +3 -7
- package/dist/broadcast_to-CGvLNiQ0.js +20 -0
- package/dist/checks/appendCache.js +7 -7
- package/dist/checks/attentionMask.js +9 -8
- package/dist/checks/check.js +1 -2
- package/dist/checks/gelu.js +6 -6
- package/dist/checks/index.js +24 -25
- package/dist/checks/matMulGelu.js +7 -7
- package/dist/checks/normRMS.js +14 -12
- package/dist/checks/normRMSGrad.js +9 -8
- package/dist/checks/packUnpack.js +7 -7
- package/dist/checks/qkv.js +1 -34
- package/dist/checks/rope.js +6 -6
- package/dist/checks/weights.js +1 -2
- package/dist/clip_by_value-D-UX9am5.js +13 -0
- package/dist/complex-Cl904TqN.js +13 -0
- package/dist/{complex_util-CkazZsaH.js → complex_util-BBlPAm-s.js} +1 -2
- package/dist/concat-Oip5acVY.js +14 -0
- package/dist/{concat_util-CWzvQQlK.js → concat_util-CY45Hf4F.js} +2 -3
- package/dist/data/docx.js +3 -4
- package/dist/data/pdf.js +11 -14
- package/dist/data/stream.js +1 -2
- package/dist/data/textLoader.js +16 -15
- package/dist/data-CV_i_H6B.js +11 -0
- package/dist/data.js +5 -12
- package/dist/dist-CMMLi-qO.js +2675 -0
- package/dist/{dist-BGVm64AY.js → dist-DeVH1nGn.js} +1465 -1573
- package/dist/{dist-CwK5S7Ls.js → dist-DyL7qSLJ.js} +2664 -2400
- package/dist/dropout16-CA31Ud5a.js +20 -0
- package/dist/dropout_util-SiFMbFXz.js +22 -0
- package/dist/{eventemitter3-D_qV3Lof.js → eventemitter3-wmfmOP_f.js} +2 -3
- package/dist/expand_dims-Dh3Fulyn.js +10 -0
- package/dist/floor-BwjmUb7X.js +8 -0
- package/dist/gather-B5Z1RFt5.js +14 -0
- package/dist/gatherSub-CRUfUzBa.js +22 -0
- package/dist/gelu-Xx9-ohH9.js +25 -0
- package/dist/{gpgpu_math-20tPK8LM.js → gpgpu_math-48m8q3jI.js} +265 -286
- package/dist/inference/Beamer.d.ts +14 -2
- package/dist/inference/Beamer.js +170 -105
- package/dist/inference/Generator.js +1 -259
- package/dist/inference/tokenisePrompt.d.ts +2 -1
- package/dist/inference/tokenisePrompt.js +15 -11
- package/dist/inference/types.d.ts +7 -0
- package/dist/inference/utilities.js +1 -2
- package/dist/{jszip.min-DKa1Rjyn.js → jszip.min-Btv48m44.js} +68 -64
- package/dist/{kernel_funcs_utils-ql8Y8qPn.js → kernel_funcs_utils-BYRpSfV_.js} +39 -42
- package/dist/layers/BaseLayer.js +9 -10
- package/dist/layers/{CausalSelfAttention.d.ts → GQA.d.ts} +8 -3
- package/dist/layers/GQA.js +102 -0
- package/dist/layers/HeadLayer.d.ts +12 -0
- package/dist/layers/HeadLayer.js +28 -0
- package/dist/layers/LoRA.js +17 -20
- package/dist/layers/MHA.d.ts +43 -0
- package/dist/layers/MHA.js +102 -0
- package/dist/layers/MLP.d.ts +5 -1
- package/dist/layers/MLP.js +33 -20
- package/dist/layers/PositionEmbedding.js +14 -13
- package/dist/layers/RMSNorm.d.ts +2 -0
- package/dist/layers/RMSNorm.js +16 -11
- package/dist/layers/RoPECache.js +1 -337
- package/dist/layers/TiedEmbedding.js +15 -14
- package/dist/layers/TransformerBlock.d.ts +4 -10
- package/dist/layers/TransformerBlock.js +29 -30
- package/dist/layers/WeightStore.js +4 -5
- package/dist/layers-CFqEip3s.js +13 -0
- package/dist/layers.d.ts +1 -1
- package/dist/layers.js +4 -12
- package/dist/loader/load.js +27 -24
- package/dist/loader/loadHF.js +3 -3
- package/dist/loader/loadTransformers.js +22 -7
- package/dist/loader/loadZipMeta.js +1 -2
- package/dist/loader/newZipLoad.js +1 -2
- package/dist/loader/oldZipLoad.js +8 -9
- package/dist/loader/save.js +31 -17
- package/dist/loader/types.d.ts +8 -2
- package/dist/main.js +3281 -3463
- package/dist/matMul16-tE26fnhf.js +87 -0
- package/dist/{matMulGelu-CBoqTZM7.js → matMulGelu-ASUWlKqu.js} +33 -33
- package/dist/mat_mul-R9_DWmIT.js +16 -0
- package/dist/models/Llama.d.ts +16 -0
- package/dist/models/Llama.js +88 -0
- package/dist/models/NanoGPTV1.js +53 -54
- package/dist/models/NanoGPTV2.js +48 -49
- package/dist/models/Qwen3.d.ts +16 -0
- package/dist/models/Qwen3.js +88 -0
- package/dist/models/config.d.ts +18 -1
- package/dist/models/config.js +3 -8
- package/dist/models/factory.js +14 -11
- package/dist/models/model.d.ts +5 -7
- package/dist/models/model.js +1 -2
- package/dist/models-DyBpJJwM.js +13 -0
- package/dist/models.js +6 -14
- package/dist/mul16-C7sqgqik.js +41 -0
- package/dist/non_max_suppression_impl-B0H318rb.js +72 -0
- package/dist/not_equal-Bu3O8rh_.js +67 -0
- package/dist/one_hot-TEMPumJj.js +14 -0
- package/dist/ones-DpPXWa6A.js +13 -0
- package/dist/ops/adamAdjust.js +2 -3
- package/dist/ops/adamMoments.js +2 -3
- package/dist/ops/add16.js +2 -3
- package/dist/ops/appendCache.js +9 -8
- package/dist/ops/attentionMask.d.ts +1 -1
- package/dist/ops/attentionMask.js +8 -5
- package/dist/ops/concat16.js +5 -5
- package/dist/ops/cpu/adamAdjust.js +7 -8
- package/dist/ops/cpu/adamMoments.js +5 -5
- package/dist/ops/cpu/appendCache.js +3 -3
- package/dist/ops/cpu/attentionMask.js +12 -8
- package/dist/ops/cpu/fusedSoftmax.js +3 -3
- package/dist/ops/cpu/gatherSub.js +1 -12
- package/dist/ops/cpu/gelu.js +12 -13
- package/dist/ops/cpu/matMul16.js +7 -7
- package/dist/ops/cpu/matMulGelu.js +21 -18
- package/dist/ops/cpu/matMulMul.js +8 -9
- package/dist/ops/cpu/mulDropout.js +8 -9
- package/dist/ops/cpu/normRMS.js +12 -13
- package/dist/ops/cpu/qkv.js +14 -13
- package/dist/ops/cpu/rope.js +11 -8
- package/dist/ops/cpu/scatterSub.js +1 -12
- package/dist/ops/dot16.js +16 -17
- package/dist/ops/dropout.js +9 -8
- package/dist/ops/dropout16.js +1 -22
- package/dist/ops/gatherSub.js +3 -4
- package/dist/ops/gelu.js +1 -2
- package/dist/ops/globalNorm.js +14 -14
- package/dist/ops/grads/add16.js +16 -17
- package/dist/ops/grads/attentionMask.js +6 -7
- package/dist/ops/grads/dropout16.js +1 -1
- package/dist/ops/grads/gelu.js +1 -2
- package/dist/ops/grads/matMul16.js +1 -2
- package/dist/ops/grads/matMulGelu.js +6 -7
- package/dist/ops/grads/mul16.js +1 -1
- package/dist/ops/grads/normRMS.js +8 -9
- package/dist/ops/grads/pack16.js +1 -2
- package/dist/ops/grads/qkv.js +10 -10
- package/dist/ops/grads/rope.js +1 -2
- package/dist/ops/grads/silu.d.ts +2 -0
- package/dist/ops/grads/silu.js +1 -0
- package/dist/ops/grads/softmax16.js +6 -7
- package/dist/ops/grads/unpack16.js +1 -2
- package/dist/ops/grads/utils.js +1 -2
- package/dist/ops/matMul16.d.ts +2 -2
- package/dist/ops/matMul16.js +1 -2
- package/dist/ops/matMulGelu.js +3 -4
- package/dist/ops/matMulMul.js +2 -3
- package/dist/ops/mul16.js +1 -43
- package/dist/ops/mulDrop.js +2 -3
- package/dist/ops/normRMS.d.ts +1 -1
- package/dist/ops/normRMS.js +10 -11
- package/dist/ops/pack16.js +1 -2
- package/dist/ops/qkv.js +2 -3
- package/dist/ops/reshape16.js +11 -11
- package/dist/ops/rope.d.ts +1 -1
- package/dist/ops/rope.js +1 -2
- package/dist/ops/scatterSub.js +3 -4
- package/dist/ops/silu.d.ts +3 -0
- package/dist/ops/silu.js +1 -0
- package/dist/ops/slice16.js +5 -5
- package/dist/ops/softmax16.js +3 -4
- package/dist/ops/sub16.js +3 -4
- package/dist/ops/sum16.js +3 -3
- package/dist/ops/transpose16.js +16 -15
- package/dist/ops/unpack16.js +1 -2
- package/dist/ops/webgl/adamAdjust.js +8 -9
- package/dist/ops/webgl/adamMoments.js +2 -3
- package/dist/ops/webgl/appendCache.js +2 -3
- package/dist/ops/webgl/attentionMask.js +2 -3
- package/dist/ops/webgl/dropout16.js +2 -3
- package/dist/ops/webgl/fusedSoftmax.js +13 -13
- package/dist/ops/webgl/gatherSub.js +2 -3
- package/dist/ops/webgl/gelu.js +11 -12
- package/dist/ops/webgl/log.js +10 -10
- package/dist/ops/webgl/matMul16.js +31 -24
- package/dist/ops/webgl/matMulGelu.js +1 -2
- package/dist/ops/webgl/matMulMul.js +3 -4
- package/dist/ops/webgl/mulDropout.js +2 -3
- package/dist/ops/webgl/normRMS.js +11 -10
- package/dist/ops/webgl/qkv.js +2 -3
- package/dist/ops/webgl/rope.js +10 -9
- package/dist/ops/webgl/scatterSub.js +2 -3
- package/dist/ops/webgpu/adamAdjust.js +13 -14
- package/dist/ops/webgpu/adamMoments.js +13 -14
- package/dist/ops/webgpu/add16.js +3 -4
- package/dist/ops/webgpu/appendCache.js +20 -21
- package/dist/ops/webgpu/attentionMask.js +18 -19
- package/dist/ops/webgpu/attentionMask32_program.js +7 -8
- package/dist/ops/webgpu/clipScale.js +11 -12
- package/dist/ops/webgpu/concat16.js +17 -18
- package/dist/ops/webgpu/dropout16.js +6 -7
- package/dist/ops/webgpu/gatherSub.js +11 -12
- package/dist/ops/webgpu/gelu.js +10 -11
- package/dist/ops/webgpu/index.js +17 -16
- package/dist/ops/webgpu/matMul16.js +74 -57
- package/dist/ops/webgpu/matMul16_program.d.ts +4 -3
- package/dist/ops/webgpu/matMul16_program.js +30 -22
- package/dist/ops/webgpu/mul16.js +3 -4
- package/dist/ops/webgpu/norm2.js +9 -10
- package/dist/ops/webgpu/normRMS.js +13 -14
- package/dist/ops/webgpu/normRMS16_program.d.ts +2 -1
- package/dist/ops/webgpu/normRMS16_program.js +5 -5
- package/dist/ops/webgpu/normRMS32_program.d.ts +2 -1
- package/dist/ops/webgpu/normRMS32_program.js +5 -5
- package/dist/ops/webgpu/normRMSGrad.js +30 -30
- package/dist/ops/webgpu/pack16.js +2 -3
- package/dist/ops/webgpu/pack16_program.js +3 -4
- package/dist/ops/webgpu/qkv.js +10 -11
- package/dist/ops/webgpu/rope.js +113 -49
- package/dist/ops/webgpu/scatterSub.js +12 -13
- package/dist/ops/webgpu/silu.d.ts +14 -0
- package/dist/ops/webgpu/silu.js +117 -0
- package/dist/ops/webgpu/slice16.js +17 -18
- package/dist/ops/webgpu/softmax16.js +9 -9
- package/dist/ops/webgpu/softmax16_program.js +9 -10
- package/dist/ops/webgpu/softmax16_subgroup_program.js +11 -12
- package/dist/ops/webgpu/softmax16grad.js +8 -9
- package/dist/ops/webgpu/sub16.js +3 -4
- package/dist/ops/webgpu/sum16.js +16 -15
- package/dist/ops/webgpu/transpose16.js +13 -12
- package/dist/ops/webgpu/transpose16_program.js +3 -4
- package/dist/ops/webgpu/transpose16_shared_program.js +17 -18
- package/dist/ops/webgpu/unpack16.js +8 -9
- package/dist/ops/webgpu/utils/binary_op.js +20 -21
- package/dist/ops/webgpu/utils/deviceInfo.js +1 -2
- package/dist/ops/webgpu/utils/reductions.js +21 -20
- package/dist/ops-Be5ACU5R.js +13 -0
- package/dist/ops-C1UpdEYM.js +509 -0
- package/dist/{pack16-BhuXNUS7.js → pack16-BHO4t1-M.js} +14 -17
- package/dist/pad-Dy-1IP3A.js +13 -0
- package/dist/patches/webgpu_backend.js +18 -19
- package/dist/patches/webgpu_base.js +2 -3
- package/dist/patches/webgpu_program.js +21 -22
- package/dist/{pdf-Bzuk68Vy.js → pdf-CbnQ75VG.js} +380 -701
- package/dist/{picomatch-BO4HbNfo.js → picomatch--TW6gyBJ.js} +229 -291
- package/dist/qkv-BQfNPucX.js +42 -0
- package/dist/rand_util-_vCY93Uw.js +44 -0
- package/dist/random_normal-DUIT8kyC.js +11 -0
- package/dist/range-CYvQC3fW.js +13 -0
- package/dist/relu-C4XULT8A.js +8 -0
- package/dist/reshape-DJb1jgMt.js +8 -0
- package/dist/resize_nearest_neighbor-DAqcTYUe.js +188 -0
- package/dist/{chunk-CWhphoD1.js → rolldown-runtime-CwxnZyOB.js} +4 -5
- package/dist/rope-JjBVufF_.js +24 -0
- package/dist/scatterSub-BOnqk-uY.js +27 -0
- package/dist/scatter_nd_util-BepMCpLY.js +34 -0
- package/dist/seedrandom-SHEGFK6H.js +282 -0
- package/dist/selu_util-DeOTf-sc.js +27 -0
- package/dist/shared-BIDMUBCs.js +1864 -0
- package/dist/{shared-CfTzpULd.js → shared-CX_iPI5K.js} +2 -3
- package/dist/silu-DsAE2cJ5.js +23 -0
- package/dist/slice-Dlb1FlzG.js +13 -0
- package/dist/{slice_util-Dve4TkDn.js → slice_util-BVgPRoij.js} +3 -4
- package/dist/softmax-Dpav_dWw.js +10 -0
- package/dist/split-DYnT8chz.js +11 -0
- package/dist/squeeze-B3XYGAlg.js +9 -0
- package/dist/stack-Dbbt9mIk.js +10 -0
- package/dist/step-DA-5DNu9.js +348 -0
- package/dist/{stream-BpAwcvHz.js → stream-BBe2apn4.js} +68 -88
- package/dist/sum-C8qNbFNp.js +13 -0
- package/dist/tensor-ALxWZfmz.js +7 -0
- package/dist/tensor1d-BHgkkhTE.js +9 -0
- package/dist/tensor2d-ARItvqVn.js +10 -0
- package/dist/tensor4d-D1zRBipJ.js +10 -0
- package/dist/tfjs_backend-CsXOum0h.js +685 -0
- package/dist/tile-DfyQ0KX-.js +10 -0
- package/dist/tokenise-DbS2Ea-T.js +14 -0
- package/dist/tokenise.js +6 -15
- package/dist/tokeniser/BaseTokeniser.js +5 -9
- package/dist/tokeniser/CharTokeniser.js +1 -2
- package/dist/tokeniser/bpe.js +19 -20
- package/dist/topk-BrU2pkrR.js +19 -0
- package/dist/training/AdamW.js +35 -31
- package/dist/training/BasicTrainer.js +56 -56
- package/dist/training/DatasetBuilder.js +1 -2
- package/dist/training/Evaluator.js +8 -5
- package/dist/training/LRScheduler.js +1 -2
- package/dist/training/PreTrainer.js +7 -8
- package/dist/training/SFTTrainer.js +7 -8
- package/dist/training/configure.js +16 -15
- package/dist/training/factory.js +1 -2
- package/dist/training/loss.js +1 -2
- package/dist/training/orthoGrad.js +2 -3
- package/dist/training/prepareData.js +18 -17
- package/dist/training/sparseCrossEntropy.js +38 -32
- package/dist/training/tasks/TokenStore.js +21 -22
- package/dist/training/tasks/tokenStream.js +1 -2
- package/dist/training/validateOptions.js +1 -2
- package/dist/training/validation.js +1 -2
- package/dist/transpose-BC-M_OoT.js +34 -0
- package/dist/unsorted_segment_sum-BelC7R-q.js +408 -0
- package/dist/utilities/arrayClose.js +5 -4
- package/dist/utilities/arrayShape.js +1 -2
- package/dist/utilities/datasetID.js +1 -2
- package/dist/utilities/dummy.js +15 -15
- package/dist/utilities/float16.js +10 -10
- package/dist/utilities/maxHeap.d.ts +16 -0
- package/dist/utilities/maxHeap.js +54 -0
- package/dist/utilities/multinomialCPU.d.ts +1 -2
- package/dist/utilities/multinomialCPU.js +6 -8
- package/dist/utilities/packed.js +2 -3
- package/dist/utilities/parameters.js +34 -19
- package/dist/utilities/performance.js +2 -3
- package/dist/utilities/profile.js +3 -4
- package/dist/utilities/random.js +1 -2
- package/dist/utilities/safetensors.js +14 -14
- package/dist/utilities/sentences.js +16 -13
- package/dist/utilities/tokenParse.js +1 -2
- package/dist/utilities/tokens.js +5 -6
- package/dist/utilities/topP.d.ts +1 -0
- package/dist/utilities/topP.js +20 -13
- package/dist/utilities/waitForModel.js +1 -2
- package/dist/utilities/weights.js +4 -4
- package/dist/utilities/yielder.js +1 -2
- package/dist/{v4-BK7K-jy_.js → v4-YCkfo8OR.js} +4 -7
- package/dist/{webgpu-IAn9VotJ.js → webgpu-BHX-uBym.js} +156 -163
- package/dist/{webgpu_program-B0PokuRJ.js → webgpu_program-DvkFqRlR.js} +28 -29
- package/dist/{webgpu_util-DfODg6XI.js → webgpu_util-5QTyovaf.js} +2 -3
- package/dist/zeros-hxKGkRfl.js +12 -0
- package/package.json +3 -3
- package/dist/Reshape-CwxdaU8n.js +0 -17
- package/dist/dist-Da20xy8E.js +0 -6375
- package/dist/gelu-B6637MKa.js +0 -27
- package/dist/layers/CausalSelfAttention.js +0 -99
- package/dist/matMul16-DGtlziMn.js +0 -81
- package/dist/rope-C8EAiSGY.js +0 -25
- package/dist/selu_util-B_AFEfoz.js +0 -24
- package/dist/shared-24EoeHJl.js +0 -1314
- package/dist/tfjs_backend-h5weiy1O.js +0 -614
|
@@ -19,19 +19,20 @@ export default class MatMul16ProgramGeneric implements WebGPUProgram {
|
|
|
19
19
|
scale: boolean;
|
|
20
20
|
scaleA: boolean;
|
|
21
21
|
scaleB: boolean;
|
|
22
|
-
activation?: 'gelu' | 'relu2' | 'relu';
|
|
22
|
+
activation?: 'gelu' | 'relu2' | 'relu' | 'silu';
|
|
23
23
|
causalMask: boolean;
|
|
24
|
+
hasMask: boolean;
|
|
24
25
|
outputComponent?: number | undefined;
|
|
25
26
|
variableComponents?: number[];
|
|
26
27
|
outputIndexSnippet?: string;
|
|
27
28
|
outputStrideSnippet?: string;
|
|
28
|
-
constructor(batch: number, O1: number, O2: number, I1: number, I2: number, transposeA?: boolean, transposeB?: boolean);
|
|
29
|
+
constructor(batch: number, O1: number, O2: number, I1: number, I2: number, transposeA?: boolean, transposeB?: boolean, hasMask?: boolean);
|
|
29
30
|
private addUniform;
|
|
30
31
|
setOutputShape(shape: number[], perm?: number[]): void;
|
|
31
32
|
useScale(): void;
|
|
32
33
|
useScaleA(): void;
|
|
33
34
|
useScaleB(): void;
|
|
34
|
-
useActivation(activation: 'gelu' | 'relu2' | 'relu'): void;
|
|
35
|
+
useActivation(activation: 'gelu' | 'relu2' | 'relu' | 'silu'): void;
|
|
35
36
|
useCausalMask(): void;
|
|
36
37
|
private activationSnippet;
|
|
37
38
|
private readASnippet;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { s as t } from "../../webgpu_program-
|
|
3
|
-
|
|
1
|
+
import { qi as e } from "../../dist-CMMLi-qO.js";
|
|
2
|
+
import { s as t } from "../../webgpu_program-DvkFqRlR.js";
|
|
3
|
+
// lib/ops/webgpu/matMul16_program.ts
|
|
4
4
|
var n = class {
|
|
5
5
|
variableNames = ["A", "B"];
|
|
6
6
|
outputShape;
|
|
@@ -23,12 +23,13 @@ var n = class {
|
|
|
23
23
|
scaleB = !1;
|
|
24
24
|
activation;
|
|
25
25
|
causalMask = !1;
|
|
26
|
+
hasMask = !1;
|
|
26
27
|
outputComponent;
|
|
27
28
|
variableComponents;
|
|
28
29
|
outputIndexSnippet;
|
|
29
30
|
outputStrideSnippet;
|
|
30
|
-
constructor(e, t, n, r, i, a = !1, o = !1) {
|
|
31
|
-
if (this.transposeA = a, this.transposeB = o, this.variableComponents = [2, 2], this.outputComponent = 2, this.shaderKey = `MatMul16TB_${t}_${n}_${r}_${i}_${a ? "TA" : ""}${o ? "TB" : ""}`, a) {
|
|
31
|
+
constructor(e, t, n, r, i, a = !1, o = !1, s = !1) {
|
|
32
|
+
if (this.hasMask = s, this.transposeA = a, this.transposeB = o, this.variableComponents = [2, 2], this.outputComponent = 2, this.shaderKey = `MatMul16TB_${t}_${n}_${r}_${i}_${a ? "TA" : ""}${o ? "TB" : ""}${s ? "HM" : ""}`, a) {
|
|
32
33
|
if (this.outputShape = [
|
|
33
34
|
e,
|
|
34
35
|
r,
|
|
@@ -56,7 +57,7 @@ var n = class {
|
|
|
56
57
|
this.outputShape[0]
|
|
57
58
|
], i % 32 != 0 || r % 32 != 0) throw Error("Head size must be even for MatMul16 transposeB");
|
|
58
59
|
if (t % 32 != 0 || n % 32 != 0) throw Error("Sequence length must be multiple of 32 for MatMul16 transposeB");
|
|
59
|
-
this.outputIndexSnippet = "var idx0 = getOutputIndexFromCoords(vec3<i32>(batch, gRow, gColPacked));", this.outputStrideSnippet = "idx0 = idx0 + uniforms.outShapeStrides[1]; // Next row";
|
|
60
|
+
this.outputIndexSnippet = "var idx0 = getOutputIndexFromCoords(vec3<i32>(batch, gRow, gColPacked));", this.outputStrideSnippet = "idx0 = idx0 + uniforms.outShapeStrides[1]; // Next row", this.hasMask && (this.variableComponents.push(1), this.variableNames.push("mask"));
|
|
60
61
|
}
|
|
61
62
|
addUniform(e) {
|
|
62
63
|
this.uniforms ? this.uniforms += `, ${e}` : this.uniforms = e;
|
|
@@ -68,18 +69,19 @@ var n = class {
|
|
|
68
69
|
return [`${e} / ${t}`, `${e} % ${t}`];
|
|
69
70
|
}
|
|
70
71
|
let o = this.outputShape, s = [];
|
|
71
|
-
if (t.length === o.length + 1)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
72
|
+
if (t.length === o.length + 1) {
|
|
73
|
+
if (t[0] * t[1] === o[0]) s = [
|
|
74
|
+
...a("batch", t[1]),
|
|
75
|
+
"gRow",
|
|
76
|
+
"gColPacked"
|
|
77
|
+
], this.shaderKey += `_batchSplit_${t[1]}`;
|
|
78
|
+
else if (t[t.length - 2] * t[t.length - 1] === o[o.length - 1]) s = [
|
|
79
|
+
"batch",
|
|
80
|
+
"gRow",
|
|
81
|
+
...a("gColPacked", t[t.length - 1])
|
|
82
|
+
], this.shaderKey += `_colSplit_${t[t.length - 1]}`;
|
|
83
|
+
else throw Error("Unsupported output shape split");
|
|
84
|
+
} else if (t.length === o.length) s = [
|
|
83
85
|
"batch",
|
|
84
86
|
"gRow",
|
|
85
87
|
"gColPacked"
|
|
@@ -92,7 +94,7 @@ var n = class {
|
|
|
92
94
|
c = n.map((e) => s[e]), this.shaderKey += `_perm_${n.join("")}`;
|
|
93
95
|
} else c = s;
|
|
94
96
|
let l = c.findIndex((e) => e === "gRow"), u = `vec${t.length}<i32>(${c.join(", ")})`;
|
|
95
|
-
this.outputIndexSnippet = `var idx0: i32 = getOutputIndexFromCoords(${u});`, this.outputStrideSnippet = `idx0 = idx0 + uniforms.outShapeStrides${l === 0 ? "" : `[${l}]`}; `,
|
|
97
|
+
this.outputIndexSnippet = `var idx0: i32 = getOutputIndexFromCoords(${u});`, this.outputStrideSnippet = `idx0 = idx0 + uniforms.outShapeStrides${l === 0 ? "" : `[${l}]`}; `, this.outputShape = n ? n.map((e) => t[e]) : t;
|
|
96
98
|
}
|
|
97
99
|
useScale() {
|
|
98
100
|
this.addUniform("scale: f32"), this.scale = !0, this.shaderKey += "_scaled";
|
|
@@ -110,7 +112,7 @@ var n = class {
|
|
|
110
112
|
this.causalMask = !0, this.addUniform("pastLen: i32"), this.shaderKey += "_causalMask";
|
|
111
113
|
}
|
|
112
114
|
activationSnippet() {
|
|
113
|
-
return this.activation === "gelu" ? "\n // TODO: revisit after https://github.com/gpuweb/gpuweb/issues/4458 is resolved\n fn tanhComplete(x: vec4<f32>) -> vec4<f32> {\n return vec4<f32>(\n select(tanh(x.x), sign(x.x), abs(x.x) > 15.0f),\n select(tanh(x.y), sign(x.y), abs(x.y) > 15.0f),\n select(tanh(x.z), sign(x.z), abs(x.z) > 15.0f),\n select(tanh(x.w), sign(x.w), abs(x.w) > 15.0f),\n );\n }\n fn activation(x : vec4<f32>) -> vec4<f32> {\n let x3 = x * x * x;\n var inner = fma(vec4<f32>(0.044715f), x3, x);\n inner = 0.7978845608028654f * inner;\n inner = tanhComplete(inner);\n inner = 0.5f * (1.0f + inner);\n return x * inner;\n }\n " : this.activation === "relu2" ? "\n fn activation(x : vec4<f32>) -> vec4<f32> {\n let y = max(x, vec4<f32>(0.0));\n return y * y;\n }\n " : this.activation === "relu" ? "\n fn activation(x : vec4<f32>) -> vec4<f32> {\n return max(x, vec4<f32>(0.0));\n }\n " : "";
|
|
115
|
+
return this.activation === "gelu" ? "\n // TODO: revisit after https://github.com/gpuweb/gpuweb/issues/4458 is resolved\n fn tanhComplete(x: vec4<f32>) -> vec4<f32> {\n return vec4<f32>(\n select(tanh(x.x), sign(x.x), abs(x.x) > 15.0f),\n select(tanh(x.y), sign(x.y), abs(x.y) > 15.0f),\n select(tanh(x.z), sign(x.z), abs(x.z) > 15.0f),\n select(tanh(x.w), sign(x.w), abs(x.w) > 15.0f),\n );\n }\n fn activation(x : vec4<f32>) -> vec4<f32> {\n let x3 = x * x * x;\n var inner = fma(vec4<f32>(0.044715f), x3, x);\n inner = 0.7978845608028654f * inner;\n inner = tanhComplete(inner);\n inner = 0.5f * (1.0f + inner);\n return x * inner;\n }\n " : this.activation === "relu2" ? "\n fn activation(x : vec4<f32>) -> vec4<f32> {\n let y = max(x, vec4<f32>(0.0));\n return y * y;\n }\n " : this.activation === "relu" ? "\n fn activation(x : vec4<f32>) -> vec4<f32> {\n return max(x, vec4<f32>(0.0));\n }\n " : this.activation === "silu" ? "\n fn activation(x : vec4<f32>) -> vec4<f32> {\n return x * (1.0 / (1.0 + exp(-x)));\n }\n " : "";
|
|
114
116
|
}
|
|
115
117
|
readASnippet() {
|
|
116
118
|
let e = `
|
|
@@ -284,7 +286,14 @@ var n = class {
|
|
|
284
286
|
|
|
285
287
|
${this.outputIndexSnippet}
|
|
286
288
|
for (var i = 0; i < 4; i = i + 1) {
|
|
287
|
-
${this.causalMask ?
|
|
289
|
+
${this.causalMask ? `
|
|
290
|
+
// Causal Masking: mask if col > row + pastLen
|
|
291
|
+
let r = gRow + i;
|
|
292
|
+
let cBase = gColPacked * 2;
|
|
293
|
+
let cVec = vec4<i32>(cBase, cBase + 1, cBase + 2, cBase + 3);
|
|
294
|
+
let positionMask = cVec > vec4<i32>(r + uniforms.pastLen);
|
|
295
|
+
${this.hasMask ? "\n let additionalMask = vec4<bool>(\n positionMask.x || mask[cVec.x] == 1,\n positionMask.y || mask[cVec.y] == 1,\n positionMask.z || mask[cVec.z] == 1,\n positionMask.w || mask[cVec.w] == 1\n );\n acc[i] = select(acc[i], vec4<f32>(-uniforms.INFINITY), additionalMask);\n " : "\n acc[i] = select(acc[i], vec4<f32>(-uniforms.INFINITY), positionMask);\n "}
|
|
296
|
+
` : ""}
|
|
288
297
|
|
|
289
298
|
${this.activation ? "acc[i] = activation(acc[i]);" : ""}
|
|
290
299
|
${this.scale ? "acc[i] = acc[i] * uniforms.scale;" : ""}
|
|
@@ -299,5 +308,4 @@ var n = class {
|
|
|
299
308
|
`;
|
|
300
309
|
}
|
|
301
310
|
};
|
|
302
|
-
//#endregion
|
|
303
311
|
export { n as default };
|
package/dist/ops/webgpu/mul16.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t } from "../../binary_op_util-
|
|
1
|
+
import { xt as e } from "../../dist-CMMLi-qO.js";
|
|
2
|
+
import { t } from "../../binary_op_util-WY9YIl0q.js";
|
|
3
3
|
import { BinaryOpProgram as n, BinaryOpScalarProgram as r } from "./utils/binary_op.js";
|
|
4
|
-
|
|
4
|
+
// lib/ops/webgpu/mul16.ts
|
|
5
5
|
function i(e) {
|
|
6
6
|
let { a: i, b: a } = e.inputs, o = e.backend, s = a.shape.length === 0 ? new r(t.MUL, i.shape) : new n(t.MUL, i.shape, a.shape);
|
|
7
7
|
return o.runWebGPUProgram(s, [i, a], "packedF16");
|
|
@@ -11,4 +11,3 @@ e({
|
|
|
11
11
|
backendName: "webgpu",
|
|
12
12
|
kernelFunc: i
|
|
13
13
|
});
|
|
14
|
-
//#endregion
|
package/dist/ops/webgpu/norm2.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ReduceProgram as e, reduce as t } from "./utils/reductions.js";
|
|
2
|
+
import { qi as n, xt as r } from "../../dist-CMMLi-qO.js";
|
|
3
3
|
import i from "./utils/deviceInfo.js";
|
|
4
|
-
|
|
5
|
-
var a = class extends
|
|
4
|
+
// lib/ops/webgpu/norm2.ts
|
|
5
|
+
var a = class extends e {
|
|
6
6
|
shaderKey = "norm2";
|
|
7
7
|
atomic = !0;
|
|
8
8
|
utilityFunctions = "\n fn atomicAddF32(sum: ptr<storage, atomic<i32>, read_write>, value: f32) -> f32 {\n var old = atomicLoad(sum);\n loop {\n let new_value = value + bitcast<f32>(old);\n let exchange_result = atomicCompareExchangeWeak(sum, old, bitcast<i32>(new_value));\n if (exchange_result.exchanged) {\n return new_value;\n }\n old = exchange_result.old_value;\n }\n }\n ";
|
|
@@ -21,26 +21,25 @@ var a = class extends n {
|
|
|
21
21
|
}
|
|
22
22
|
};
|
|
23
23
|
function o(e) {
|
|
24
|
-
let { x:
|
|
24
|
+
let { x: r, output: o } = e.inputs, { invLossScaling: s, index: c } = e.attrs, l = e.backend, u = [], d = i(l), f = Math.min(512, l.device.limits.maxComputeWorkgroupSizeX), p = n(r.shape);
|
|
25
25
|
for (; p % (f * 4) != 0 && f > 1;) f /= 2;
|
|
26
26
|
if (f === 1) throw Error(`Cannot find suitable workgroup size for Norm2Program with reduce size ${p}`);
|
|
27
|
-
let m =
|
|
27
|
+
let m = new a(d, {
|
|
28
28
|
inSize: f * 4,
|
|
29
29
|
outSize: 1,
|
|
30
30
|
batchSize: p / (f * 4),
|
|
31
31
|
windowSize: f
|
|
32
|
-
}, f), [
|
|
32
|
+
}, f), h = t(m, [r], l, [{
|
|
33
33
|
type: "float32",
|
|
34
34
|
data: [s]
|
|
35
35
|
}, {
|
|
36
36
|
type: "int32",
|
|
37
37
|
data: [c]
|
|
38
38
|
}], o);
|
|
39
|
-
return u.forEach((e) => e.dispose()),
|
|
39
|
+
return u.forEach((e) => e.dispose()), h;
|
|
40
40
|
}
|
|
41
|
-
|
|
41
|
+
r({
|
|
42
42
|
kernelName: "Norm2",
|
|
43
43
|
backendName: "webgpu",
|
|
44
44
|
kernelFunc: o
|
|
45
45
|
});
|
|
46
|
-
//#endregion
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import o from "./
|
|
1
|
+
import { Si as e, xt as t } from "../../dist-CMMLi-qO.js";
|
|
2
|
+
import { createReduceInfo as n, reduce as r } from "./utils/reductions.js";
|
|
3
|
+
import { isPackedTensor as i } from "../../utilities/packed.js";
|
|
4
|
+
import { t as a } from "../../pack16-BHO4t1-M.js";
|
|
5
|
+
import o from "./normRMS16_program.js";
|
|
6
6
|
import s from "./normRMS32_program.js";
|
|
7
|
-
import c from "./
|
|
8
|
-
|
|
7
|
+
import c from "./utils/deviceInfo.js";
|
|
8
|
+
// lib/ops/webgpu/normRMS.ts
|
|
9
9
|
function l(t) {
|
|
10
|
-
let { x: l, gamma: u } = t.inputs, d = t.backend, f =
|
|
11
|
-
if (
|
|
12
|
-
if (
|
|
13
|
-
if (
|
|
14
|
-
let
|
|
15
|
-
return
|
|
10
|
+
let { x: l, gamma: u } = t.inputs, d = t.backend, f = t.attrs?.epsilon ?? 1e-8, p = c(d), m = i(l), h = u ? i(u) : !1, g = m || h, _ = !g || m ? l : a(l), v = u && (!g || h ? u : a(u)), y = v ? [_, v] : [_], b = n(y, -1), x = g ? new o(p, b, !!u, f) : new s(p, b, !!u, f);
|
|
11
|
+
if (v && e(v.shape, [_.shape[_.shape.length - 1]], "Error in RMSNorm: "), l.shape.length !== 3) throw Error(`rmsNormGPU: input rank ${l.shape.length} not supported, only rank 3 is supported`);
|
|
12
|
+
if (b.inSize !== _.shape[_.shape.length - 1]) throw Error(`rmsNormGPU: reduction size ${b.inSize} does not match expected size ${_.shape[_.shape.length - 1]}`);
|
|
13
|
+
if (b.batchSize !== l.shape[0] * l.shape[1]) throw Error(`rmsNormGPU: batch size ${b.batchSize} does not match expected size ${l.shape[0] * l.shape[1]}`);
|
|
14
|
+
let S = r(x, y, d);
|
|
15
|
+
return g && !m && _.dispose(), g && !h && v && v.dispose(), S;
|
|
16
16
|
}
|
|
17
17
|
t({
|
|
18
18
|
kernelName: "RMSNorm",
|
|
@@ -23,4 +23,3 @@ t({
|
|
|
23
23
|
backendName: "webgpu",
|
|
24
24
|
kernelFunc: l
|
|
25
25
|
});
|
|
26
|
-
//#endregion
|
|
@@ -2,8 +2,9 @@ import { backend_util } from '@tensorflow/tfjs-core';
|
|
|
2
2
|
import { ReduceProgram } from './utils/reductions';
|
|
3
3
|
import { DeviceInformation } from './utils/deviceInfo';
|
|
4
4
|
export default class RMSProgram16 extends ReduceProgram {
|
|
5
|
+
private epsilon;
|
|
5
6
|
private hasGamma;
|
|
6
|
-
constructor(deviceInfo: DeviceInformation, reduceInfo: backend_util.ReduceInfo, hasGamma?: boolean);
|
|
7
|
+
constructor(deviceInfo: DeviceInformation, reduceInfo: backend_util.ReduceInfo, hasGamma?: boolean, epsilon?: number);
|
|
7
8
|
getPreprocessSnippet(): string;
|
|
8
9
|
getPostprocessSnippet(): string;
|
|
9
10
|
getWriteSnippet(): string;
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import { ReduceProgram as e } from "./utils/reductions.js";
|
|
2
|
-
|
|
2
|
+
// lib/ops/webgpu/normRMS16_program.ts
|
|
3
3
|
var t = class extends e {
|
|
4
|
+
epsilon;
|
|
4
5
|
hasGamma;
|
|
5
|
-
constructor(e, t, n = !1) {
|
|
6
|
+
constructor(e, t, n = !1, r = 1e-8) {
|
|
6
7
|
super(e, t, {
|
|
7
8
|
reductionOp: "mean",
|
|
8
9
|
elementwise: !0
|
|
9
|
-
}, !0), this.shaderKey = "
|
|
10
|
+
}, !0), this.epsilon = r, this.shaderKey = `RMSNorm16_${n ? "Gamma" : "NoGamma"}_${this.epsilon}`, n && this.variableNames.push("gamma"), this.variableComponents = [1, 1], this.hasGamma = n;
|
|
10
11
|
}
|
|
11
12
|
getPreprocessSnippet() {
|
|
12
13
|
return "candidate = candidate * candidate;";
|
|
13
14
|
}
|
|
14
15
|
getPostprocessSnippet() {
|
|
15
|
-
return
|
|
16
|
+
return `bestValue = inverseSqrt(bestValue + ${this.epsilon});`;
|
|
16
17
|
}
|
|
17
18
|
getWriteSnippet() {
|
|
18
19
|
return `
|
|
@@ -24,5 +25,4 @@ var t = class extends e {
|
|
|
24
25
|
`;
|
|
25
26
|
}
|
|
26
27
|
};
|
|
27
|
-
//#endregion
|
|
28
28
|
export { t as default };
|
|
@@ -2,8 +2,9 @@ import { backend_util } from '@tensorflow/tfjs-core';
|
|
|
2
2
|
import { ReduceProgram } from './utils/reductions';
|
|
3
3
|
import { DeviceInformation } from './utils/deviceInfo';
|
|
4
4
|
export default class RMSProgram32 extends ReduceProgram {
|
|
5
|
+
private epsilon;
|
|
5
6
|
private hasGamma;
|
|
6
|
-
constructor(deviceInfo: DeviceInformation, reduceInfo: backend_util.ReduceInfo, hasGamma?: boolean);
|
|
7
|
+
constructor(deviceInfo: DeviceInformation, reduceInfo: backend_util.ReduceInfo, hasGamma?: boolean, epsilon?: number);
|
|
7
8
|
protected getPreprocessSnippet(): string;
|
|
8
9
|
protected getPostprocessSnippet(): string;
|
|
9
10
|
protected getWriteSnippet(): string;
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import { ReduceProgram as e } from "./utils/reductions.js";
|
|
2
|
-
|
|
2
|
+
// lib/ops/webgpu/normRMS32_program.ts
|
|
3
3
|
var t = class extends e {
|
|
4
|
+
epsilon;
|
|
4
5
|
hasGamma;
|
|
5
|
-
constructor(e, t, n = !1) {
|
|
6
|
+
constructor(e, t, n = !1, r = 1e-8) {
|
|
6
7
|
super(e, t, {
|
|
7
8
|
reductionOp: "mean",
|
|
8
9
|
elementwise: !0
|
|
9
|
-
}, !1), this.shaderKey = "
|
|
10
|
+
}, !1), this.epsilon = r, this.shaderKey = `RMSNorm32_${n ? "Gamma" : "NoGamma"}_${this.epsilon}`, n && this.variableNames.push("gamma"), this.variableComponents = [1, 1], this.hasGamma = n;
|
|
10
11
|
}
|
|
11
12
|
getPreprocessSnippet() {
|
|
12
13
|
return "candidate = candidate * candidate;";
|
|
13
14
|
}
|
|
14
15
|
getPostprocessSnippet() {
|
|
15
|
-
return
|
|
16
|
+
return `bestValue = inverseSqrt(bestValue + ${this.epsilon});`;
|
|
16
17
|
}
|
|
17
18
|
getWriteSnippet() {
|
|
18
19
|
return `
|
|
@@ -24,5 +25,4 @@ var t = class extends e {
|
|
|
24
25
|
`;
|
|
25
26
|
}
|
|
26
27
|
};
|
|
27
|
-
//#endregion
|
|
28
28
|
export { t as default };
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { r as o, t as s } from "../../pack16-
|
|
6
|
-
import { c } from "../../
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
1
|
+
import { H as e, Si as t, xt as n } from "../../dist-CMMLi-qO.js";
|
|
2
|
+
import { createReduceInfo as r } from "./utils/reductions.js";
|
|
3
|
+
import { c as i } from "../../webgpu_util-5QTyovaf.js";
|
|
4
|
+
import { s as a } from "../../webgpu_program-DvkFqRlR.js";
|
|
5
|
+
import { r as o, t as s } from "../../pack16-BHO4t1-M.js";
|
|
6
|
+
import { isPackedTensor as c } from "../../utilities/packed.js";
|
|
7
|
+
import { reshape16 as l } from "../reshape16.js";
|
|
8
|
+
import { sum16 as u } from "../sum16.js";
|
|
9
|
+
import { slice16 as d } from "../slice16.js";
|
|
10
|
+
// lib/ops/webgpu/normRMSGrad.ts
|
|
11
11
|
var f = class {
|
|
12
12
|
outputShape;
|
|
13
13
|
shaderKey = "RMSNormGrad";
|
|
@@ -30,7 +30,7 @@ var f = class {
|
|
|
30
30
|
packed = !1;
|
|
31
31
|
outputComponent;
|
|
32
32
|
constructor(e, t = 4, n = !1) {
|
|
33
|
-
if (this.packed = n, this.shaderKey = `RMSNormGrad_${t}`, this.rowsPerWorkgroup = t, this.inputShape = [e.batchSize, e.inSize], this.outputShape = [e.batchSize + e.batchSize / this.rowsPerWorkgroup, e.inSize], this.dispatchLayout =
|
|
33
|
+
if (this.packed = n, this.shaderKey = `RMSNormGrad_${t}`, this.rowsPerWorkgroup = t, this.inputShape = [e.batchSize, e.inSize], this.outputShape = [e.batchSize + e.batchSize / this.rowsPerWorkgroup, e.inSize], this.dispatchLayout = i(this.outputShape), this.dispatch = [
|
|
34
34
|
e.batchSize / this.rowsPerWorkgroup,
|
|
35
35
|
1,
|
|
36
36
|
1
|
|
@@ -41,7 +41,7 @@ var f = class {
|
|
|
41
41
|
let e = this.workgroupSize[0], t = this.rowsPerWorkgroup, n = `
|
|
42
42
|
var<workgroup> partials : array<vec2<f32>, ${e}>;
|
|
43
43
|
var<workgroup> accumulation: array<${this.packed ? "vec2<f32>" : "f32"}, 1024>;
|
|
44
|
-
`, r = this.packed ? "\n let X = unpack2x16float(u32(x[offset + k]));\n let DY = unpack2x16float(u32(dy[offset + k]));\n let G = unpack2x16float(u32(gamma[k]));\n sum_x2 = fma(X.x, X.x, sum_x2);\n sum_x2 = fma(X.y, X.y, sum_x2);\n sum_dygx = fma(DY.x * G.x, X.x, sum_dygx);\n sum_dygx = fma(DY.y * G.y, X.y, sum_dygx);\n " : "\n let X = f32(x[offset + k]);\n let DY = f32(dy[offset + k]);\n let G = f32(gamma[k]);\n sum_x2 = fma(X, X, sum_x2);\n sum_dygx = fma(DY * G, X, sum_dygx);\n ", i = this.packed ? "\n let X = unpack2x16float(u32(x[offset + k]));\n let DY = unpack2x16float(u32(dy[offset + k]));\n let G = unpack2x16float(u32(gamma[k]));\n\n let dyGamma = DY * G;\n let dx = vec2<f32>(\n fma(dyGamma.x, invRMS, -X.x * scale),\n fma(dyGamma.y, invRMS, -X.y * scale)\n );\n\n result[offset + k] = i32(pack2x16float(dx));\n\n // dGamma\n accumulation[k] = fma(DY, X * invRMS, accumulation[k]);\n " : "\n let X = f32(x[offset + k]);\n let DY = f32(dy[offset + k]);\n let G = f32(gamma[k]);\n\n let dyGamma = DY * G;\n let dx = fma(dyGamma, invRMS, -X * scale);\n\n result[offset + k] = dx;\n\n // dGamma\n accumulation[k] = fma(DY, X * invRMS, accumulation[k]);\n ",
|
|
44
|
+
`, r = this.packed ? "\n let X = unpack2x16float(u32(x[offset + k]));\n let DY = unpack2x16float(u32(dy[offset + k]));\n let G = unpack2x16float(u32(gamma[k]));\n sum_x2 = fma(X.x, X.x, sum_x2);\n sum_x2 = fma(X.y, X.y, sum_x2);\n sum_dygx = fma(DY.x * G.x, X.x, sum_dygx);\n sum_dygx = fma(DY.y * G.y, X.y, sum_dygx);\n " : "\n let X = f32(x[offset + k]);\n let DY = f32(dy[offset + k]);\n let G = f32(gamma[k]);\n sum_x2 = fma(X, X, sum_x2);\n sum_dygx = fma(DY * G, X, sum_dygx);\n ", i = this.packed ? "\n let X = unpack2x16float(u32(x[offset + k]));\n let DY = unpack2x16float(u32(dy[offset + k]));\n let G = unpack2x16float(u32(gamma[k]));\n\n let dyGamma = DY * G;\n let dx = vec2<f32>(\n fma(dyGamma.x, invRMS, -X.x * scale),\n fma(dyGamma.y, invRMS, -X.y * scale)\n );\n\n result[offset + k] = i32(pack2x16float(dx));\n\n // dGamma\n accumulation[k] = fma(DY, X * invRMS, accumulation[k]);\n " : "\n let X = f32(x[offset + k]);\n let DY = f32(dy[offset + k]);\n let G = f32(gamma[k]);\n\n let dyGamma = DY * G;\n let dx = fma(dyGamma, invRMS, -X * scale);\n\n result[offset + k] = dx;\n\n // dGamma\n accumulation[k] = fma(DY, X * invRMS, accumulation[k]);\n ", o = this.packed ? "\n result[outDgBase + k] = i32(pack2x16float(accumulation[k]));\n " : "\n result[outDgBase + k] = accumulation[k];\n ";
|
|
45
45
|
return `
|
|
46
46
|
fn DIV_CEIL(a : u32, b : u32) -> u32 {
|
|
47
47
|
return ((a - 1u) / b + 1u);
|
|
@@ -49,7 +49,7 @@ var f = class {
|
|
|
49
49
|
|
|
50
50
|
${n}
|
|
51
51
|
|
|
52
|
-
${
|
|
52
|
+
${a("index")} {
|
|
53
53
|
// One workgroup per row (batch).
|
|
54
54
|
let Length = uniforms.reduceSize;
|
|
55
55
|
let BatchSize = uniforms.batchSize;
|
|
@@ -99,7 +99,7 @@ var f = class {
|
|
|
99
99
|
// Write out the partially accumulated dGamma
|
|
100
100
|
let outDgBase = BatchSize * Length + i32(workgroupId.x) * Length;
|
|
101
101
|
for (var k = i32(localId.x); k < Length; k = k + ${e}) {
|
|
102
|
-
${
|
|
102
|
+
${o}
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
105
|
`;
|
|
@@ -121,7 +121,7 @@ var f = class {
|
|
|
121
121
|
packed = !1;
|
|
122
122
|
outputComponent;
|
|
123
123
|
constructor(e, t = !1) {
|
|
124
|
-
this.packed = t, this.shaderKey = "RMSNormGrad_NoGamma", this.inputShape = [e.batchSize, e.inSize], this.outputShape = [e.batchSize, e.inSize], this.dispatchLayout =
|
|
124
|
+
this.packed = t, this.shaderKey = "RMSNormGrad_NoGamma", this.inputShape = [e.batchSize, e.inSize], this.outputShape = [e.batchSize, e.inSize], this.dispatchLayout = i(this.outputShape), this.dispatch = [
|
|
125
125
|
e.batchSize,
|
|
126
126
|
1,
|
|
127
127
|
1
|
|
@@ -138,7 +138,7 @@ var f = class {
|
|
|
138
138
|
|
|
139
139
|
${t}
|
|
140
140
|
|
|
141
|
-
${
|
|
141
|
+
${a("index")} {
|
|
142
142
|
// One workgroup per row (batch).
|
|
143
143
|
let Length = uniforms.reduceSize;
|
|
144
144
|
let BatchSize = uniforms.batchSize;
|
|
@@ -181,12 +181,12 @@ var f = class {
|
|
|
181
181
|
`;
|
|
182
182
|
}
|
|
183
183
|
};
|
|
184
|
-
function m(
|
|
185
|
-
let { dy:
|
|
186
|
-
|
|
187
|
-
let h =
|
|
188
|
-
b &&
|
|
189
|
-
let S =
|
|
184
|
+
function m(n) {
|
|
185
|
+
let { dy: i, x: a, gamma: m } = n.inputs;
|
|
186
|
+
t(a.shape, i.shape, "Error in RMSNormGrad dy: ");
|
|
187
|
+
let h = c(a), g = m ? c(m) : !1, _ = c(i), v = h || g || _, y = !v || h ? a : s(a), b = !v || g || !m ? m : s(m), x = !v || _ ? i : s(i);
|
|
188
|
+
b && t(b.shape, [y.shape[y.shape.length - 1]], "Error in RMSNormGrad gamma: ");
|
|
189
|
+
let S = n.backend, C = r(b ? [
|
|
190
190
|
y,
|
|
191
191
|
b,
|
|
192
192
|
x
|
|
@@ -204,22 +204,22 @@ function m(t) {
|
|
|
204
204
|
x
|
|
205
205
|
] : [y, x], v ? "packedF16" : "float32", T);
|
|
206
206
|
v && !h && y.dispose(), v && !g && b && b.dispose(), v && !_ && x.dispose();
|
|
207
|
-
let D =
|
|
207
|
+
let D = e().makeTensorFromTensorInfo(E);
|
|
208
208
|
if (m) {
|
|
209
|
-
let e =
|
|
209
|
+
let e = d(D, [0, 0], [C.batchSize, C.inSize]), t = d(D, [C.batchSize, 0], [C.batchSize / 4, C.inSize]);
|
|
210
210
|
D.dispose();
|
|
211
|
-
let n =
|
|
211
|
+
let n = l(e, a.shape);
|
|
212
212
|
e.dispose();
|
|
213
|
-
let r =
|
|
213
|
+
let r = u(t, [0]);
|
|
214
214
|
return t.dispose(), [n, !v || g ? r : o(r)];
|
|
215
|
-
}
|
|
216
|
-
|
|
215
|
+
}
|
|
216
|
+
{
|
|
217
|
+
let e = l(D, a.shape);
|
|
217
218
|
return D.dispose(), [e];
|
|
218
219
|
}
|
|
219
220
|
}
|
|
220
|
-
|
|
221
|
+
n({
|
|
221
222
|
kernelName: "RMSNormGrad",
|
|
222
223
|
backendName: "webgpu",
|
|
223
224
|
kernelFunc: m
|
|
224
225
|
});
|
|
225
|
-
//#endregion
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { xt as e } from "../../dist-CMMLi-qO.js";
|
|
2
2
|
import t from "./pack16_program.js";
|
|
3
|
-
|
|
3
|
+
// lib/ops/webgpu/pack16.ts
|
|
4
4
|
function n(e) {
|
|
5
5
|
let { x: n } = e.inputs, { scaling: r, padding: i } = e.attrs, a = e.backend;
|
|
6
6
|
if (n.shape[n.shape.length - 1] % 2 != 0) throw Error("Last dimension of input tensor must be even to use Pack16.");
|
|
@@ -18,4 +18,3 @@ e({
|
|
|
18
18
|
backendName: "webgpu",
|
|
19
19
|
kernelFunc: n
|
|
20
20
|
});
|
|
21
|
-
//#endregion
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { c as e, i as t } from "../../webgpu_util-
|
|
2
|
-
import { s as n } from "../../webgpu_program-
|
|
3
|
-
|
|
1
|
+
import { c as e, i as t } from "../../webgpu_util-5QTyovaf.js";
|
|
2
|
+
import { s as n } from "../../webgpu_program-DvkFqRlR.js";
|
|
3
|
+
// lib/ops/webgpu/pack16_program.ts
|
|
4
4
|
var r = class {
|
|
5
5
|
outputShape;
|
|
6
6
|
shaderKey = "Pack16";
|
|
@@ -89,5 +89,4 @@ var r = class {
|
|
|
89
89
|
}`;
|
|
90
90
|
}
|
|
91
91
|
};
|
|
92
|
-
//#endregion
|
|
93
92
|
export { r as default };
|
package/dist/ops/webgpu/qkv.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
1
|
+
import { Si as e, xt as t } from "../../dist-CMMLi-qO.js";
|
|
2
|
+
import { t as n } from "../../matMul16-tE26fnhf.js";
|
|
3
|
+
import { slice16 as r } from "../slice16.js";
|
|
4
|
+
import { isPackedTensor as i } from "../../utilities/packed.js";
|
|
5
|
+
// lib/ops/webgpu/qkv.ts
|
|
6
6
|
function a(t) {
|
|
7
|
-
let { x: a, kernel: o } = t.inputs, { heads: s } = t.attrs, c = a.shape[0], l = a.shape[1], u = a.shape[2], d =
|
|
7
|
+
let { x: a, kernel: o } = t.inputs, { heads: s } = t.attrs, c = a.shape[0], l = a.shape[1], u = a.shape[2], d = i(a);
|
|
8
8
|
if (e(o.shape, [d ? u * 2 : u, 3 * u], "Error in QKV: "), u % s !== 0) throw Error(`Channel dimension ${u} must be divisible by number of heads ${s} in QKV.`);
|
|
9
|
-
let f =
|
|
9
|
+
let f = n(a, o, !1, !1, {
|
|
10
10
|
forceOutputShape: [
|
|
11
11
|
c,
|
|
12
12
|
l,
|
|
@@ -20,7 +20,7 @@ function a(t) {
|
|
|
20
20
|
3
|
|
21
21
|
]
|
|
22
22
|
}), p = [
|
|
23
|
-
|
|
23
|
+
r(f, [
|
|
24
24
|
0,
|
|
25
25
|
0,
|
|
26
26
|
0,
|
|
@@ -31,7 +31,7 @@ function a(t) {
|
|
|
31
31
|
l,
|
|
32
32
|
u / s
|
|
33
33
|
]),
|
|
34
|
-
|
|
34
|
+
r(f, [
|
|
35
35
|
0,
|
|
36
36
|
s,
|
|
37
37
|
0,
|
|
@@ -42,7 +42,7 @@ function a(t) {
|
|
|
42
42
|
l,
|
|
43
43
|
u / s
|
|
44
44
|
]),
|
|
45
|
-
|
|
45
|
+
r(f, [
|
|
46
46
|
0,
|
|
47
47
|
2 * s,
|
|
48
48
|
0,
|
|
@@ -61,4 +61,3 @@ t({
|
|
|
61
61
|
backendName: "webgpu",
|
|
62
62
|
kernelFunc: a
|
|
63
63
|
});
|
|
64
|
-
//#endregion
|