@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
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { attentionMask as e } from "../ops/attentionMask.js";
|
|
2
|
+
import { t } from "../random_normal-DUIT8kyC.js";
|
|
3
|
+
import { n } from "../picomatch--TW6gyBJ.js";
|
|
4
|
+
import r from "./BaseLayer.js";
|
|
5
|
+
import { t as i } from "../rope-JjBVufF_.js";
|
|
6
|
+
import { appendCache as a } from "../ops/appendCache.js";
|
|
7
|
+
import { W as o, Y as s } from "../dist-CMMLi-qO.js";
|
|
8
|
+
import { softmax16 as c } from "../ops/softmax16.js";
|
|
9
|
+
import { t as l } from "../matMul16-tE26fnhf.js";
|
|
10
|
+
import { t as u } from "../pack16-BHO4t1-M.js";
|
|
11
|
+
import { transpose16 as d } from "../ops/transpose16.js";
|
|
12
|
+
import { dot16 as f } from "../ops/dot16.js";
|
|
13
|
+
import { reshape16 as p } from "../ops/reshape16.js";
|
|
14
|
+
import { isPackedTensor as m } from "../utilities/packed.js";
|
|
15
|
+
import { qkv as h } from "../ops/qkv.js";
|
|
16
|
+
import { t as g } from "../dropout16-CA31Ud5a.js";
|
|
17
|
+
import _ from "./RMSNorm.js";
|
|
18
|
+
// lib/layers/GQA.ts
|
|
19
|
+
var v = class extends r {
|
|
20
|
+
attentionConfig;
|
|
21
|
+
divisor;
|
|
22
|
+
index;
|
|
23
|
+
units;
|
|
24
|
+
projUnits;
|
|
25
|
+
ATTN;
|
|
26
|
+
PROJ;
|
|
27
|
+
qNorm = null;
|
|
28
|
+
kNorm = null;
|
|
29
|
+
constructor(e, t, n, r) {
|
|
30
|
+
super(t, r), this.attentionConfig = n, this.index = e, this.units = t.nEmbed * 3, this.projUnits = t.nEmbed, this.ATTN = `block_${this.index}_cAttn`, this.PROJ = `block_${this.index}_cProj`, this.addVariable(this.ATTN), this.addVariable(this.PROJ), this.divisor = 1 / Math.sqrt(t.nEmbed / t.nHead), (this.attentionConfig.useQKNorm ?? !1) && (this.qNorm = new _(t, n, `block_${this.index}_qNorm`, this), this.kNorm = new _(t, n, `block_${this.index}_kNorm`, this));
|
|
31
|
+
}
|
|
32
|
+
build() {
|
|
33
|
+
this.hasVariable(this.ATTN) === !1 && this.setVariable(this.ATTN, n(t([this.config.nEmbed, this.units], 0, .02), !0, this.ATTN)), this.hasVariable(this.PROJ) === !1 && this.setVariable(this.PROJ, n(t([this.projUnits, this.config.nEmbed], 0, .02), !0, this.PROJ));
|
|
34
|
+
}
|
|
35
|
+
getAttentionScores(t, n, r) {
|
|
36
|
+
let i = e(t, n, this.divisor, r), a = c(i);
|
|
37
|
+
return i.dispose(), a;
|
|
38
|
+
}
|
|
39
|
+
getQKV(e) {
|
|
40
|
+
let t = m(e) ? u(this.getVariable(this.ATTN)) : this.getVariable(this.ATTN), n = h(e, t, this.config.nHead);
|
|
41
|
+
return m(e) && t.dispose(), n;
|
|
42
|
+
}
|
|
43
|
+
getOutputProjection(e) {
|
|
44
|
+
let t = e.shape[0], n = e.shape[2], r = this.config.nEmbed, i = m(e), a = d(e, [
|
|
45
|
+
0,
|
|
46
|
+
2,
|
|
47
|
+
1,
|
|
48
|
+
3
|
|
49
|
+
]), o = p(a, [
|
|
50
|
+
t,
|
|
51
|
+
n,
|
|
52
|
+
i ? r / 2 : r
|
|
53
|
+
]);
|
|
54
|
+
a.dispose();
|
|
55
|
+
let s = i ? u(this.getVariable(this.PROJ)) : this.getVariable(this.PROJ), c = f(o, s);
|
|
56
|
+
return i && s.dispose(), o.dispose(), c;
|
|
57
|
+
}
|
|
58
|
+
updateCache(e, t, n) {
|
|
59
|
+
let r = this.config.blockSize, i = e.shape[2], s = n.length || 0, c = a(e, r, s, n.k);
|
|
60
|
+
e.dispose(), n.k && n.k.dispose();
|
|
61
|
+
let l = a(t, r, s, n.v);
|
|
62
|
+
t.dispose(), n.v && n.v.dispose();
|
|
63
|
+
let u = Math.min(s + i, r), d = n.cumulativeLength + i;
|
|
64
|
+
n.length = u, n.cumulativeLength = d, n.k = o(c), n.v = o(l);
|
|
65
|
+
}
|
|
66
|
+
forward(e, t) {
|
|
67
|
+
return s(() => {
|
|
68
|
+
this.startMemory();
|
|
69
|
+
let [n, r, a] = this.getQKV(t), s = e.pastKV ? e.pastKV.cumulativeLength : e.ropePositionOffset || 0, c = e.ropeCache, u = this.attentionConfig.ropeInterleaved ?? !0, d = c ? i(n, c, s, !1, u) : n, f = c ? i(r, c, s, !1, u) : r, p = this.qNorm ? this.qNorm.call(e, d) : d;
|
|
70
|
+
this.qNorm && d.dispose();
|
|
71
|
+
let m = this.kNorm ? this.kNorm.call(e, f) : f;
|
|
72
|
+
this.kNorm && f.dispose(), c && (n.dispose(), r.dispose());
|
|
73
|
+
let h = e.pastKV ? e.pastKV.length : 0;
|
|
74
|
+
e.pastKV && !e.training && this.updateCache(m, a, e.pastKV);
|
|
75
|
+
let _ = e.pastKV?.k ? e.pastKV.k : m, v = e.pastKV?.v ? e.pastKV.v : a, y;
|
|
76
|
+
y = h > 0 ? this.getAttentionScores(p, _, h) : this.getAttentionScores(p, _), p.dispose(), e.pastKV || _.dispose();
|
|
77
|
+
let b = l(y, v), x = e.attentionScores !== void 0 && e.attentionScores.attentionOut !== void 0;
|
|
78
|
+
x || y.dispose(), e.pastKV || v.dispose();
|
|
79
|
+
let S = this.getOutputProjection(b);
|
|
80
|
+
if (b.dispose(), x && e.attentionScores && e.attentionScores.attentionOut !== void 0) {
|
|
81
|
+
let t = y.shape[1], n = y.shape[2];
|
|
82
|
+
e.attentionScores.attentionOut?.push(o(y.slice([
|
|
83
|
+
0,
|
|
84
|
+
0,
|
|
85
|
+
0,
|
|
86
|
+
0
|
|
87
|
+
], [
|
|
88
|
+
1,
|
|
89
|
+
-1,
|
|
90
|
+
-1,
|
|
91
|
+
-1
|
|
92
|
+
]).reshape([
|
|
93
|
+
t,
|
|
94
|
+
n,
|
|
95
|
+
-1
|
|
96
|
+
])));
|
|
97
|
+
}
|
|
98
|
+
return this.endMemory("CausalSelfAttention"), e.dropout && e.dropout > 0 ? g(S, e.dropout) : S;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
export { v as default };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Tensor } from '@tensorflow/tfjs-core';
|
|
2
|
+
import { default as BaseLayer, ForwardAttributes } from './BaseLayer';
|
|
3
|
+
import { GPTConfig } from '../../models/config';
|
|
4
|
+
export default class HeadLayer extends BaseLayer {
|
|
5
|
+
private vocabSize;
|
|
6
|
+
private embedDim;
|
|
7
|
+
private initializer;
|
|
8
|
+
private WEIGHTS;
|
|
9
|
+
constructor(config: GPTConfig, name: string, parent?: BaseLayer);
|
|
10
|
+
project(inputs: Tensor): Tensor;
|
|
11
|
+
forward(_: ForwardAttributes, x: Tensor): Tensor;
|
|
12
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { n as e } from "../dist-DyL7qSLJ.js";
|
|
2
|
+
import "../dist-CMMLi-qO.js";
|
|
3
|
+
import { n as t } from "../picomatch--TW6gyBJ.js";
|
|
4
|
+
import n from "./BaseLayer.js";
|
|
5
|
+
import { dot16 as r } from "../ops/dot16.js";
|
|
6
|
+
import { isPackedTensor as i } from "../utilities/packed.js";
|
|
7
|
+
import { t as a } from "../pack16-BHO4t1-M.js";
|
|
8
|
+
// lib/layers/HeadLayer.ts
|
|
9
|
+
var o = class extends n {
|
|
10
|
+
vocabSize;
|
|
11
|
+
embedDim;
|
|
12
|
+
initializer;
|
|
13
|
+
WEIGHTS;
|
|
14
|
+
constructor(n, r, i) {
|
|
15
|
+
super(n, i), this.WEIGHTS = r, this.vocabSize = n.vocabSize, this.embedDim = n.nEmbed, this.initializer = e({
|
|
16
|
+
mean: 0,
|
|
17
|
+
stddev: .02
|
|
18
|
+
}), this.addVariable(this.WEIGHTS, t(this.initializer.apply([this.embedDim, this.vocabSize]), !0, r));
|
|
19
|
+
}
|
|
20
|
+
project(e) {
|
|
21
|
+
let t = i(e) ? a(this.getVariable(this.WEIGHTS), void 0, 32) : this.getVariable(this.WEIGHTS), n = r(e, t);
|
|
22
|
+
return i(e) && t.dispose(), n;
|
|
23
|
+
}
|
|
24
|
+
forward(e, t) {
|
|
25
|
+
return this.project(t);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
export { o as default };
|
package/dist/layers/LoRA.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { i as e } from "../
|
|
2
|
-
import {
|
|
3
|
-
import { t as
|
|
4
|
-
|
|
1
|
+
import { i as e } from "../rolldown-runtime-CwxnZyOB.js";
|
|
2
|
+
import { Y as t, b as n } from "../dist-CMMLi-qO.js";
|
|
3
|
+
import { t as r } from "../zeros-hxKGkRfl.js";
|
|
4
|
+
import { t as i } from "../random_normal-DUIT8kyC.js";
|
|
5
|
+
import { n as a, t as o } from "../picomatch--TW6gyBJ.js";
|
|
6
|
+
// lib/layers/LoRA.ts
|
|
5
7
|
var s = /* @__PURE__ */ e(o(), 1), c = class {
|
|
6
8
|
weightStore;
|
|
7
9
|
alpha;
|
|
@@ -9,29 +11,25 @@ var s = /* @__PURE__ */ e(o(), 1), c = class {
|
|
|
9
11
|
variables;
|
|
10
12
|
scale;
|
|
11
13
|
name;
|
|
12
|
-
constructor(e,
|
|
13
|
-
this.name = e, this.weightStore =
|
|
14
|
-
let u = (0, s.default)(l), d =
|
|
14
|
+
constructor(e, t, o, c, l) {
|
|
15
|
+
this.name = e, this.weightStore = t, this.alpha = o, this.rank = c;
|
|
16
|
+
let u = (0, s.default)(l), d = t.variableNames.filter((e) => u(e) && !e.endsWith("_loraA") && !e.endsWith("_loraB"));
|
|
15
17
|
this.variables = new Set(d), this.scale = n(o / c), this.variables.forEach((e) => {
|
|
16
|
-
let
|
|
17
|
-
|
|
18
|
-
console.warn(`LoRA currently only supports 2D weight matrices. Variable ${e} has shape ${n.shape}`), this.variables.delete(e);
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
this.weightStore.hasVariable(s) || this.weightStore.hasVariable(c) || (this.weightStore.addVariable(s, t(a([r, this.rank], 0, .02), !0, s)), this.weightStore.addVariable(c, t(i([this.rank, o]), !0, c)));
|
|
18
|
+
let t = this.weightStore.getRawVariable(e), [n, o] = t.shape, s = `${e}_${this.name}_loraA`, c = `${e}_${this.name}_loraB`;
|
|
19
|
+
t.shape.length === 2 ? this.weightStore.hasVariable(s) || this.weightStore.hasVariable(c) || (this.weightStore.addVariable(s, a(i([n, this.rank], 0, .02), !0, s)), this.weightStore.addVariable(c, a(r([this.rank, o]), !0, c))) : (console.warn(`LoRA currently only supports 2D weight matrices. Variable ${e} has shape ${t.shape}`), this.variables.delete(e));
|
|
22
20
|
});
|
|
23
21
|
}
|
|
24
22
|
attach() {
|
|
25
23
|
if (this.weightStore.onWeightRead) throw Error("LoRA cannot be applied to a WeightStore that already has a onWeightRead hook.");
|
|
26
|
-
this.weightStore.onWeightRead = (e,
|
|
27
|
-
let
|
|
28
|
-
return
|
|
29
|
-
}) :
|
|
24
|
+
this.weightStore.onWeightRead = (e, n) => this.variables.has(e) ? t(() => {
|
|
25
|
+
let t = this.weightStore.getRawVariable(`${e}_${this.name}_loraA`), r = this.weightStore.getRawVariable(`${e}_${this.name}_loraB`);
|
|
26
|
+
return n.add(t.matMul(r).mul(this.scale));
|
|
27
|
+
}) : n, this.weightStore.setTrainable([`*_${this.name}_loraA`, `*_${this.name}_loraB`]);
|
|
30
28
|
}
|
|
31
29
|
merge() {
|
|
32
30
|
this.variables.forEach((e) => {
|
|
33
|
-
let
|
|
34
|
-
|
|
31
|
+
let n = this.weightStore.getRawVariable(e), r = this.weightStore.getRawVariable(`${e}_${this.name}_loraA`), i = this.weightStore.getRawVariable(`${e}_${this.name}_loraB`), a = t(() => n.add(r.matMul(i).mul(this.scale)));
|
|
32
|
+
n.assign(a), a.dispose();
|
|
35
33
|
});
|
|
36
34
|
}
|
|
37
35
|
detach() {
|
|
@@ -44,5 +42,4 @@ var s = /* @__PURE__ */ e(o(), 1), c = class {
|
|
|
44
42
|
}), this.variables.clear();
|
|
45
43
|
}
|
|
46
44
|
};
|
|
47
|
-
//#endregion
|
|
48
45
|
export { c as default };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { default as BaseLayer, ForwardAttributes } from './BaseLayer';
|
|
2
|
+
import { Tensor } from '@tensorflow/tfjs-core';
|
|
3
|
+
import { GPTConfig } from '../../models/config';
|
|
4
|
+
import { RMSNormConfig } from './RMSNorm';
|
|
5
|
+
export interface KVCache {
|
|
6
|
+
k?: Tensor;
|
|
7
|
+
v?: Tensor;
|
|
8
|
+
length: number;
|
|
9
|
+
cumulativeLength: number;
|
|
10
|
+
}
|
|
11
|
+
export interface AttentionScores {
|
|
12
|
+
meanOfHeads?: boolean;
|
|
13
|
+
attentionOut?: Tensor[];
|
|
14
|
+
}
|
|
15
|
+
export interface AttentionForwardAttributes extends ForwardAttributes {
|
|
16
|
+
attentionScores?: AttentionScores;
|
|
17
|
+
pastKV?: KVCache;
|
|
18
|
+
seed?: number;
|
|
19
|
+
ropePositionOffset?: number;
|
|
20
|
+
mask?: Tensor;
|
|
21
|
+
}
|
|
22
|
+
export interface MHAConfig extends RMSNormConfig {
|
|
23
|
+
useQKNorm?: boolean;
|
|
24
|
+
ropeInterleaved?: boolean;
|
|
25
|
+
}
|
|
26
|
+
export default class MHA extends BaseLayer<AttentionForwardAttributes> {
|
|
27
|
+
private readonly attentionConfig;
|
|
28
|
+
private divisor;
|
|
29
|
+
private index;
|
|
30
|
+
private units;
|
|
31
|
+
private projUnits;
|
|
32
|
+
private ATTN;
|
|
33
|
+
private PROJ;
|
|
34
|
+
private qNorm;
|
|
35
|
+
private kNorm;
|
|
36
|
+
constructor(index: number, config: GPTConfig, attentionConfig: MHAConfig, parent?: BaseLayer);
|
|
37
|
+
protected build(): void;
|
|
38
|
+
private getAttentionScores;
|
|
39
|
+
private getQKV;
|
|
40
|
+
private getOutputProjection;
|
|
41
|
+
private updateCache;
|
|
42
|
+
forward(attr: AttentionForwardAttributes, x: Tensor): Tensor;
|
|
43
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { attentionMask as e } from "../ops/attentionMask.js";
|
|
2
|
+
import { t } from "../random_normal-DUIT8kyC.js";
|
|
3
|
+
import { n } from "../picomatch--TW6gyBJ.js";
|
|
4
|
+
import r from "./BaseLayer.js";
|
|
5
|
+
import { t as i } from "../rope-JjBVufF_.js";
|
|
6
|
+
import { appendCache as a } from "../ops/appendCache.js";
|
|
7
|
+
import { W as o, Y as s } from "../dist-CMMLi-qO.js";
|
|
8
|
+
import { softmax16 as c } from "../ops/softmax16.js";
|
|
9
|
+
import { t as l } from "../matMul16-tE26fnhf.js";
|
|
10
|
+
import { t as u } from "../pack16-BHO4t1-M.js";
|
|
11
|
+
import { transpose16 as d } from "../ops/transpose16.js";
|
|
12
|
+
import { dot16 as f } from "../ops/dot16.js";
|
|
13
|
+
import { reshape16 as p } from "../ops/reshape16.js";
|
|
14
|
+
import { isPackedTensor as m } from "../utilities/packed.js";
|
|
15
|
+
import { qkv as h } from "../ops/qkv.js";
|
|
16
|
+
import { t as g } from "../dropout16-CA31Ud5a.js";
|
|
17
|
+
import _ from "./RMSNorm.js";
|
|
18
|
+
// lib/layers/MHA.ts
|
|
19
|
+
var v = class extends r {
|
|
20
|
+
attentionConfig;
|
|
21
|
+
divisor;
|
|
22
|
+
index;
|
|
23
|
+
units;
|
|
24
|
+
projUnits;
|
|
25
|
+
ATTN;
|
|
26
|
+
PROJ;
|
|
27
|
+
qNorm = null;
|
|
28
|
+
kNorm = null;
|
|
29
|
+
constructor(e, t, n, r) {
|
|
30
|
+
super(t, r), this.attentionConfig = n, this.index = e, this.units = t.nEmbed * 3, this.projUnits = t.nEmbed, this.ATTN = `block_${this.index}_cAttn`, this.PROJ = `block_${this.index}_cProj`, this.addVariable(this.ATTN), this.addVariable(this.PROJ), this.divisor = 1 / Math.sqrt(t.nEmbed / t.nHead), (this.attentionConfig.useQKNorm ?? !1) && (this.qNorm = new _(t, n, `block_${this.index}_qNorm`, this), this.kNorm = new _(t, n, `block_${this.index}_kNorm`, this));
|
|
31
|
+
}
|
|
32
|
+
build() {
|
|
33
|
+
this.hasVariable(this.ATTN) === !1 && this.setVariable(this.ATTN, n(t([this.config.nEmbed, this.units], 0, .02), !0, this.ATTN)), this.hasVariable(this.PROJ) === !1 && this.setVariable(this.PROJ, n(t([this.projUnits, this.config.nEmbed], 0, .02), !0, this.PROJ));
|
|
34
|
+
}
|
|
35
|
+
getAttentionScores(t, n, r, i) {
|
|
36
|
+
let a = e(t, n, this.divisor, r, i), o = c(a);
|
|
37
|
+
return a.dispose(), o;
|
|
38
|
+
}
|
|
39
|
+
getQKV(e) {
|
|
40
|
+
let t = m(e) ? u(this.getVariable(this.ATTN)) : this.getVariable(this.ATTN), n = h(e, t, this.config.nHead);
|
|
41
|
+
return m(e) && t.dispose(), n;
|
|
42
|
+
}
|
|
43
|
+
getOutputProjection(e) {
|
|
44
|
+
let t = e.shape[0], n = e.shape[2], r = this.config.nEmbed, i = m(e), a = d(e, [
|
|
45
|
+
0,
|
|
46
|
+
2,
|
|
47
|
+
1,
|
|
48
|
+
3
|
|
49
|
+
]), o = p(a, [
|
|
50
|
+
t,
|
|
51
|
+
n,
|
|
52
|
+
i ? r / 2 : r
|
|
53
|
+
]);
|
|
54
|
+
a.dispose();
|
|
55
|
+
let s = i ? u(this.getVariable(this.PROJ)) : this.getVariable(this.PROJ), c = f(o, s);
|
|
56
|
+
return i && s.dispose(), o.dispose(), c;
|
|
57
|
+
}
|
|
58
|
+
updateCache(e, t, n) {
|
|
59
|
+
let r = this.config.blockSize, i = e.shape[2], s = n.length || 0, c = a(e, r, s, n.k);
|
|
60
|
+
e.dispose(), n.k && n.k.dispose();
|
|
61
|
+
let l = a(t, r, s, n.v);
|
|
62
|
+
t.dispose(), n.v && n.v.dispose();
|
|
63
|
+
let u = Math.min(s + i, r), d = n.cumulativeLength + i;
|
|
64
|
+
n.length = u, n.cumulativeLength = d, n.k = o(c), n.v = o(l);
|
|
65
|
+
}
|
|
66
|
+
forward(e, t) {
|
|
67
|
+
return s(() => {
|
|
68
|
+
this.startMemory();
|
|
69
|
+
let [n, r, a] = this.getQKV(t), s = e.pastKV ? e.pastKV.cumulativeLength : e.ropePositionOffset || 0, c = e.ropeCache, u = this.attentionConfig.ropeInterleaved ?? !0, d = c ? i(n, c, s, !1, u) : n, f = c ? i(r, c, s, !1, u) : r, p = this.qNorm ? this.qNorm.call(e, d) : d;
|
|
70
|
+
this.qNorm && d.dispose();
|
|
71
|
+
let m = this.kNorm ? this.kNorm.call(e, f) : f;
|
|
72
|
+
this.kNorm && f.dispose(), c && (n.dispose(), r.dispose());
|
|
73
|
+
let h = e.pastKV ? e.pastKV.length : 0;
|
|
74
|
+
e.pastKV && !e.training && this.updateCache(m, a, e.pastKV);
|
|
75
|
+
let _ = e.pastKV?.k ? e.pastKV.k : m, v = e.pastKV?.v ? e.pastKV.v : a, y;
|
|
76
|
+
y = h > 0 ? this.getAttentionScores(p, _, h, e.mask) : this.getAttentionScores(p, _, void 0, e.mask), p.dispose(), e.pastKV || _.dispose();
|
|
77
|
+
let b = l(y, v), x = e.attentionScores !== void 0 && e.attentionScores.attentionOut !== void 0;
|
|
78
|
+
x || y.dispose(), e.pastKV || v.dispose();
|
|
79
|
+
let S = this.getOutputProjection(b);
|
|
80
|
+
if (b.dispose(), x && e.attentionScores && e.attentionScores.attentionOut !== void 0) {
|
|
81
|
+
let t = y.shape[1], n = y.shape[2];
|
|
82
|
+
e.attentionScores.attentionOut?.push(o(y.slice([
|
|
83
|
+
0,
|
|
84
|
+
0,
|
|
85
|
+
0,
|
|
86
|
+
0
|
|
87
|
+
], [
|
|
88
|
+
1,
|
|
89
|
+
-1,
|
|
90
|
+
-1,
|
|
91
|
+
-1
|
|
92
|
+
]).reshape([
|
|
93
|
+
t,
|
|
94
|
+
n,
|
|
95
|
+
-1
|
|
96
|
+
])));
|
|
97
|
+
}
|
|
98
|
+
return this.endMemory("CausalSelfAttention"), e.dropout && e.dropout > 0 ? g(S, e.dropout) : S;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
export { v as default };
|
package/dist/layers/MLP.d.ts
CHANGED
|
@@ -2,14 +2,18 @@ import { Tensor } from '@tensorflow/tfjs-core';
|
|
|
2
2
|
import { default as BaseLayer, ForwardAttributes } from './BaseLayer';
|
|
3
3
|
import { GPTConfig } from '../../models/config';
|
|
4
4
|
export interface MLPConfig {
|
|
5
|
-
activation?: 'gelu' | 'relu2';
|
|
5
|
+
activation?: 'gelu' | 'relu2' | 'swiglu';
|
|
6
6
|
hiddenFactor?: number;
|
|
7
|
+
hiddenSize?: number;
|
|
7
8
|
}
|
|
9
|
+
export declare function swigluHidden(C: number, factor?: number, multipleOf?: number): number;
|
|
8
10
|
export default class MLP extends BaseLayer {
|
|
9
11
|
private index;
|
|
10
12
|
private hiddenUnits;
|
|
11
13
|
private MLPHIDDEN;
|
|
12
14
|
private MLPOUT;
|
|
15
|
+
private MLPGATE;
|
|
16
|
+
private MLPUP;
|
|
13
17
|
private mlpConfig;
|
|
14
18
|
constructor(index: number, config: GPTConfig, mlpConfig: MLPConfig, parent?: BaseLayer);
|
|
15
19
|
protected build(): void;
|
package/dist/layers/MLP.js
CHANGED
|
@@ -1,34 +1,47 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { Y as e } from "../dist-CMMLi-qO.js";
|
|
2
|
+
import { t } from "../random_normal-DUIT8kyC.js";
|
|
3
|
+
import { n } from "../picomatch--TW6gyBJ.js";
|
|
4
|
+
import r from "./BaseLayer.js";
|
|
5
|
+
import { t as i } from "../matMul16-tE26fnhf.js";
|
|
6
|
+
import { reshape16 as a } from "../ops/reshape16.js";
|
|
7
|
+
import { t as o } from "../dropout16-CA31Ud5a.js";
|
|
8
|
+
import { t as s } from "../mul16-C7sqgqik.js";
|
|
9
|
+
// lib/layers/MLP.ts
|
|
10
|
+
function c(e, t = 8 / 3, n = 32) {
|
|
11
|
+
let r = Math.round(t * e);
|
|
12
|
+
return Math.max(n, Math.round(r / n) * n);
|
|
13
|
+
}
|
|
14
|
+
var l = class extends r {
|
|
8
15
|
index;
|
|
9
16
|
hiddenUnits;
|
|
10
17
|
MLPHIDDEN;
|
|
11
18
|
MLPOUT;
|
|
19
|
+
MLPGATE;
|
|
20
|
+
MLPUP;
|
|
12
21
|
mlpConfig;
|
|
13
22
|
constructor(e, t, n, r) {
|
|
14
|
-
super(t, r), this.index = e, this.mlpConfig = n, this.hiddenUnits = (n.hiddenFactor ?? t.mlpFactor)
|
|
23
|
+
super(t, r), this.index = e, this.mlpConfig = n, this.hiddenUnits = n.hiddenSize ?? c(t.nEmbed, n.hiddenFactor ?? t.mlpFactor), this.MLPHIDDEN = `block_${this.index}_mlpHidden`, this.MLPOUT = `block_${this.index}_mlpOut`, this.addVariable(this.MLPOUT), this.MLPGATE = `block_${this.index}_mlpGate`, this.MLPUP = `block_${this.index}_mlpUp`, this.mlpConfig.activation === "swiglu" ? (this.addVariable(this.MLPGATE), this.addVariable(this.MLPUP)) : this.addVariable(this.MLPHIDDEN);
|
|
15
24
|
}
|
|
16
25
|
build() {
|
|
17
|
-
this.hasVariable(this.MLPHIDDEN) === !1 && this.setVariable(this.MLPHIDDEN,
|
|
26
|
+
this.mlpConfig.activation === "swiglu" ? (this.hasVariable(this.MLPGATE) === !1 && this.setVariable(this.MLPGATE, n(t([this.config.nEmbed, this.hiddenUnits], 0, .02), !0, this.MLPGATE)), this.hasVariable(this.MLPUP) === !1 && this.setVariable(this.MLPUP, n(t([this.config.nEmbed, this.hiddenUnits], 0, .02), !0, this.MLPUP))) : this.hasVariable(this.MLPHIDDEN) === !1 && this.setVariable(this.MLPHIDDEN, n(t([this.config.nEmbed, this.hiddenUnits], 0, .02), !0, this.MLPHIDDEN)), this.hasVariable(this.MLPOUT) === !1 && this.setVariable(this.MLPOUT, n(t([this.hiddenUnits, this.config.nEmbed], 0, .02 / Math.sqrt(2 * this.config.nLayer)), !0, this.MLPOUT));
|
|
18
27
|
}
|
|
19
|
-
forward(
|
|
20
|
-
return
|
|
28
|
+
forward(t, n) {
|
|
29
|
+
return e(() => {
|
|
21
30
|
this.startMemory();
|
|
22
|
-
let [
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
t,
|
|
26
|
-
|
|
27
|
-
|
|
31
|
+
let [e, r, c] = n.shape, l = a(n, [e * r, c]), u;
|
|
32
|
+
if (this.mlpConfig.activation === "swiglu") {
|
|
33
|
+
let e = i(l, this.getVariable(this.MLPGATE), !1, !1, { activation: "silu" }), t = i(l, this.getVariable(this.MLPUP));
|
|
34
|
+
u = s(e, t), e.dispose(), t.dispose();
|
|
35
|
+
} else u = i(l, this.getVariable(this.MLPHIDDEN), !1, !1, { activation: this.mlpConfig.activation ?? "gelu" });
|
|
36
|
+
let d = i(u, this.getVariable(this.MLPOUT));
|
|
37
|
+
u.dispose();
|
|
38
|
+
let f = a(d, [
|
|
39
|
+
e,
|
|
40
|
+
r,
|
|
41
|
+
c
|
|
28
42
|
]);
|
|
29
|
-
return this.endMemory("MLP"),
|
|
43
|
+
return this.endMemory("MLP"), t.dropout && t.dropout > 0 ? o(f, t.dropout) : f;
|
|
30
44
|
});
|
|
31
45
|
}
|
|
32
46
|
};
|
|
33
|
-
|
|
34
|
-
export { s as default };
|
|
47
|
+
export { l as default, c as swigluHidden };
|
|
@@ -1,27 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { Y as e, b as t, it as n } from "../dist-CMMLi-qO.js";
|
|
2
|
+
import r from "./BaseLayer.js";
|
|
3
|
+
import { t as i } from "../ops-Be5ACU5R.js";
|
|
4
|
+
import { t as a } from "../range-CYvQC3fW.js";
|
|
5
|
+
import { n as o, t as s } from "../dist-DyL7qSLJ.js";
|
|
6
|
+
// lib/layers/PositionEmbedding.ts
|
|
7
|
+
var c = class extends r {
|
|
6
8
|
wpe;
|
|
7
9
|
constructor(e, t = "", n) {
|
|
8
|
-
super(e, n), this.wpe =
|
|
10
|
+
super(e, n), this.wpe = s({
|
|
9
11
|
inputDim: this.config.blockSize,
|
|
10
12
|
outputDim: this.config.nEmbed,
|
|
11
13
|
name: t,
|
|
12
|
-
embeddingsInitializer:
|
|
14
|
+
embeddingsInitializer: o({
|
|
13
15
|
mean: 0,
|
|
14
16
|
stddev: .02
|
|
15
17
|
})
|
|
16
18
|
});
|
|
17
19
|
}
|
|
18
|
-
forward(
|
|
19
|
-
let s =
|
|
20
|
-
return
|
|
21
|
-
let [,
|
|
22
|
-
return o.add(
|
|
20
|
+
forward(r, o) {
|
|
21
|
+
let s = r.cache?.[0]?.length ?? 0;
|
|
22
|
+
return e(() => {
|
|
23
|
+
let [, e] = o.shape, r = this.config.blockSize, c = a(0, e, 1, "int32"), l = i(n(c, t(s, "int32")), t(r, "int32")), u = this.wpe.apply(l);
|
|
24
|
+
return o.add(u);
|
|
23
25
|
});
|
|
24
26
|
}
|
|
25
27
|
};
|
|
26
|
-
//#endregion
|
|
27
28
|
export { c as default };
|
package/dist/layers/RMSNorm.d.ts
CHANGED
|
@@ -3,10 +3,12 @@ import { default as BaseLayer, ForwardAttributes } from './BaseLayer';
|
|
|
3
3
|
import { GPTConfig } from '../../models/config';
|
|
4
4
|
export interface RMSNormConfig {
|
|
5
5
|
useGamma?: boolean;
|
|
6
|
+
epsilon?: number;
|
|
6
7
|
}
|
|
7
8
|
export default class RMSNorm extends BaseLayer {
|
|
8
9
|
private GAMMA;
|
|
9
10
|
private rmsConfig;
|
|
10
11
|
constructor(config: GPTConfig, rmsConfig: RMSNormConfig, name?: string, parent?: BaseLayer);
|
|
12
|
+
protected buildGamma(size: number): void;
|
|
11
13
|
forward(_: ForwardAttributes, x: Tensor): Tensor;
|
|
12
14
|
}
|
package/dist/layers/RMSNorm.js
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Y as e } from "../dist-CMMLi-qO.js";
|
|
2
|
+
import { t } from "../ones-DpPXWa6A.js";
|
|
3
|
+
import { n } from "../picomatch--TW6gyBJ.js";
|
|
2
4
|
import r from "./BaseLayer.js";
|
|
3
5
|
import { normRMS as i } from "../ops/normRMS.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
+
import { isPackedTensor as a } from "../utilities/packed.js";
|
|
7
|
+
// lib/layers/RMSNorm.ts
|
|
8
|
+
var o = class extends r {
|
|
6
9
|
GAMMA;
|
|
7
10
|
rmsConfig;
|
|
8
|
-
constructor(
|
|
9
|
-
super(
|
|
11
|
+
constructor(e, t, n = "", r) {
|
|
12
|
+
super(e, r), this.GAMMA = n, this.rmsConfig = t, (this.rmsConfig.useGamma ?? !0) && this.addVariable(this.GAMMA);
|
|
10
13
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
buildGamma(e) {
|
|
15
|
+
(this.rmsConfig.useGamma ?? !0) && this.hasVariable(this.GAMMA) === !1 && this.setVariable(this.GAMMA, n(t([e]), !0, this.GAMMA, "float32"));
|
|
16
|
+
}
|
|
17
|
+
forward(t, n) {
|
|
18
|
+
return e(() => {
|
|
19
|
+
this.startMemory(), this.buildGamma(a(n) ? n.shape[n.shape.length - 1] * 2 : n.shape[n.shape.length - 1]);
|
|
20
|
+
let e = i(n, this.rmsConfig.useGamma ?? !0 ? this.getVariable(this.GAMMA) : void 0, this.rmsConfig.epsilon);
|
|
15
21
|
return this.endMemory("RMSNorm"), e;
|
|
16
22
|
});
|
|
17
23
|
}
|
|
18
24
|
};
|
|
19
|
-
|
|
20
|
-
export { a as default };
|
|
25
|
+
export { o as default };
|