@genai-fi/nanogpt 0.7.2 → 0.8.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/Generator.d.ts +36 -4
- package/dist/Generator.js +183 -69
- package/dist/{RealDiv-Dy0p8Bvo.js → RealDiv-N8TpOMYv.js} +14 -14
- package/dist/{Reshape-DvudQDvJ.js → Reshape-B-lWQRnF.js} +1 -1
- package/dist/{Reshape-DH5srBP0.js → Reshape-Bo8HzP8V.js} +5 -5
- package/dist/TeachableLLM.d.ts +6 -6
- package/dist/TeachableLLM.js +51 -50
- package/dist/Trainer.d.ts +19 -3
- package/dist/Trainer.js +71 -28
- package/dist/{axis_util-BzbKo31C.js → axis_util-DubwyOhW.js} +3 -3
- package/dist/backend.js +2 -2
- package/dist/{backend_util-TE7aTPhZ.js → backend_util-BJ-_jSeK.js} +46 -46
- package/dist/{broadcast_to-CdbwV-Dj.js → broadcast_to-BYfCp5iL.js} +2 -2
- package/dist/{concat-CsxrgovM.js → concat-BmDqqFsa.js} +1 -1
- package/dist/{dataset-CtdBYwjo.js → dataset-CJmEGu6D.js} +5 -5
- package/dist/{dropout-DYs5QFGQ.js → dropout-sx0sjVAT.js} +8 -8
- package/dist/exports_initializers-DAKM8UO9.js +16 -0
- package/dist/{gather-CMMy2KEG.js → gather-C1siEkdp.js} +1 -1
- package/dist/{gelu-C-dPj6Ku.js → gelu-Bd3UBBxg.js} +1 -1
- package/dist/{gpgpu_math-DGNLNL4I.js → gpgpu_math-TFLxaLkw.js} +26 -26
- package/dist/{index-CLthM0TO.js → index-BaPo_0H8.js} +185 -185
- package/dist/{index-BoWRt-10.js → index-CUQrfsw_.js} +266 -265
- package/dist/{kernel_funcs_utils-BYKWV8Aa.js → kernel_funcs_utils-P9aFa232.js} +9 -9
- package/dist/layers/BaseLayer.d.ts +8 -13
- package/dist/layers/BaseLayer.js +25 -13
- package/dist/layers/CausalSelfAttention.d.ts +3 -2
- package/dist/layers/CausalSelfAttention.js +28 -28
- package/dist/layers/MLP.d.ts +3 -2
- package/dist/layers/MLP.js +16 -20
- package/dist/layers/PositionEmbedding.d.ts +9 -0
- package/dist/layers/PositionEmbedding.js +45 -0
- package/dist/layers/RMSNorm.d.ts +3 -2
- package/dist/layers/RMSNorm.js +6 -6
- package/dist/layers/RoPECache.d.ts +1 -1
- package/dist/layers/RoPECache.js +4 -4
- package/dist/layers/TiedEmbedding.d.ts +3 -2
- package/dist/layers/TiedEmbedding.js +29 -7
- package/dist/layers/TransformerBlock.d.ts +3 -2
- package/dist/layers/TransformerBlock.js +1 -1
- package/dist/loader/load.d.ts +2 -2
- package/dist/loader/loadHF.d.ts +2 -2
- package/dist/loader/loadTransformers.d.ts +4 -2
- package/dist/loader/loadTransformers.js +10 -9
- package/dist/loader/newZipLoad.d.ts +2 -2
- package/dist/loader/oldZipLoad.d.ts +2 -2
- package/dist/loader/oldZipLoad.js +42 -51
- package/dist/loader/save.d.ts +8 -0
- package/dist/loader/save.js +62 -0
- package/dist/{log_sum_exp-DbjkV734.js → log_sum_exp-C142qZqY.js} +14 -14
- package/dist/main.d.ts +5 -4
- package/dist/main.js +22 -18
- package/dist/{mat_mul-8m8pfdcx.js → mat_mul-DMkduNJu.js} +1 -1
- package/dist/{max-Ddnnb5xe.js → max-B3JOcNGb.js} +1 -1
- package/dist/mod-uUuj4gSb.js +27 -0
- package/dist/models/NanoGPTV1.d.ts +15 -0
- package/dist/models/NanoGPTV1.js +71 -0
- package/dist/{config.d.ts → models/config.d.ts} +1 -0
- package/dist/{config.js → models/config.js} +1 -0
- package/dist/models/factory.d.ts +3 -0
- package/dist/models/factory.js +14 -0
- package/dist/models/model.d.ts +26 -0
- package/dist/models/model.js +68 -0
- package/dist/{mulmat_packed_gpu-VSekgsNv.js → mulmat_packed_gpu-Cm2gw-c8.js} +1 -1
- package/dist/{ones-Dj0SDhHf.js → ones-ZdgQGBCP.js} +2 -2
- package/dist/ops/adamAdjust.js +1 -1
- package/dist/ops/adamMoments.js +1 -1
- package/dist/ops/appendCache.js +3 -3
- package/dist/ops/attentionMask.js +1 -1
- package/dist/ops/cpu/adamAdjust.js +9 -9
- package/dist/ops/cpu/adamMoments.js +2 -2
- package/dist/ops/cpu/appendCache.js +2 -2
- package/dist/ops/cpu/attentionMask.js +5 -5
- package/dist/ops/cpu/fusedSoftmax.js +2 -2
- package/dist/ops/cpu/gatherSub.js +3 -3
- package/dist/ops/cpu/gelu.js +1 -1
- package/dist/ops/cpu/matMulGelu.js +2 -2
- package/dist/ops/cpu/matMulMul.js +1 -1
- package/dist/ops/cpu/mulDropout.js +1 -1
- package/dist/ops/cpu/normRMS.js +1 -1
- package/dist/ops/cpu/qkv.js +3 -3
- package/dist/ops/cpu/rope.js +5 -5
- package/dist/ops/cpu/scatterSub.js +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.js +2 -2
- package/dist/ops/webgl/adamMoments.js +1 -1
- package/dist/ops/webgl/appendCache.js +1 -1
- package/dist/ops/webgl/attentionMask.js +1 -1
- package/dist/ops/webgl/fusedSoftmax.js +4 -4
- package/dist/ops/webgl/gatherSub.js +1 -1
- package/dist/ops/webgl/gelu.js +2 -2
- package/dist/ops/webgl/log.js +3 -3
- package/dist/ops/webgl/matMulGelu.js +10 -10
- package/dist/ops/webgl/matMulMul.js +1 -1
- package/dist/ops/webgl/mulDropout.js +1 -1
- package/dist/ops/webgl/normRMS.js +2 -2
- package/dist/ops/webgl/qkv.js +1 -1
- package/dist/ops/webgl/rope.js +1 -1
- package/dist/ops/webgl/scatterSub.js +1 -1
- package/dist/ops/webgpu/adamAdjust.js +3 -3
- package/dist/ops/webgpu/adamMoments.js +3 -3
- package/dist/ops/webgpu/appendCache.js +3 -3
- package/dist/ops/webgpu/attentionMask.js +3 -3
- package/dist/ops/webgpu/gatherSub.js +3 -3
- package/dist/ops/webgpu/gelu.js +3 -3
- package/dist/ops/webgpu/normRMS.js +2 -2
- package/dist/ops/webgpu/normRMSGrad.js +5 -5
- package/dist/ops/webgpu/qkv.js +3 -3
- package/dist/ops/webgpu/rope.js +3 -3
- package/dist/ops/webgpu/scatterSub.js +3 -3
- package/dist/ops/webgpu/utils/reductions.js +4 -4
- package/dist/{ops-BFGCx8Ri.js → ops-C_1K_-35.js} +103 -103
- package/dist/{random_width-sZORGo5k.js → random_width-D8Pwy_na.js} +136 -136
- package/dist/{range-CRuAh-gd.js → range-LVHrSLdi.js} +1 -1
- package/dist/{reciprocal-BvGAyKyu.js → reciprocal-CaR9e67G.js} +1 -1
- package/dist/{register_all_kernels-BwDSRN-f.js → register_all_kernels-DUshvVWP.js} +2026 -2049
- package/dist/{reshape-CdBq1WJ6.js → reshape-DEfQGSin.js} +1 -1
- package/dist/{scatter_nd_util-DUstGbU1.js → scatter_nd_util-CUPPNLaA.js} +1 -1
- package/dist/{selu_util-BJEXVvjX.js → selu_util-8vv5JxQV.js} +3 -3
- package/dist/{shared-B8ztnyEk.js → shared-CkNorDcU.js} +83 -83
- package/dist/{shared-wS99K7_n.js → shared-D1elLckx.js} +1 -1
- package/dist/{sin-BeA3tsEd.js → sin-D2CKKmyR.js} +1 -1
- package/dist/{slice-BiOsknYS.js → slice-BnyE-M_7.js} +1 -1
- package/dist/{softmax-Bv_6lyMX.js → softmax-DLoZWYBx.js} +1 -1
- package/dist/{split-B-dikLRw.js → split-By_n4TKP.js} +1 -1
- package/dist/{stack-B17UN2nn.js → stack-DkdFLq37.js} +1 -1
- package/dist/{sum-66ew2byf.js → sum-l_0SqM4h.js} +3 -3
- package/dist/{tensor-JwS7ZYY6.js → tensor-BAQdLqoU.js} +1 -1
- package/dist/{tensor2d-wxPAnDQy.js → tensor2d-BHy261cI.js} +1 -1
- package/dist/training/Adam.js +2 -2
- package/dist/training/AdamExt.js +1 -1
- package/dist/training/DatasetBuilder.js +2 -2
- package/dist/training/Evaluator.d.ts +2 -2
- package/dist/training/FullTrainer.d.ts +16 -3
- package/dist/training/FullTrainer.js +91 -53
- package/dist/training/Trainer.d.ts +25 -3
- package/dist/training/Trainer.js +39 -47
- package/dist/training/sparseCrossEntropy.js +9 -9
- package/dist/utilities/dummy.d.ts +4 -4
- package/dist/utilities/dummy.js +13 -13
- package/dist/utilities/multinomialCPU.js +2 -2
- package/dist/utilities/parameters.d.ts +1 -1
- package/dist/utilities/performance.js +1 -1
- package/dist/utilities/profile.js +1 -1
- package/dist/utilities/safetensors.js +2 -2
- package/dist/utilities/weights.js +2 -2
- package/dist/{variable-BuddVFLa.js → variable-C9hihzDB.js} +1 -1
- package/dist/{webgpu_program-PFzf1hAQ.js → webgpu_program-dFEVbDPL.js} +1 -1
- package/dist/{webgpu_util-D____QpY.js → webgpu_util-DLImlSc6.js} +27 -27
- package/dist/{zeros--BdLQ3oG.js → zeros-VZ72lWXM.js} +1 -1
- package/package.json +2 -3
- package/dist/NanoGPTModel.d.ts +0 -52
- package/dist/NanoGPTModel.js +0 -203
- package/dist/TiedEmbedding-BxOerUmB.js +0 -43
- package/dist/utilities/generate.d.ts +0 -3
- package/dist/utilities/generate.js +0 -22
- package/dist/utilities/save.d.ts +0 -9
- package/dist/utilities/save.js +0 -61
|
@@ -1,30 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import "../jszip.min-CjP2V1VV.js";
|
|
3
|
-
import h from "../tokeniser/CharTokeniser.js";
|
|
4
|
-
import { a0 as k } from "../index-BoWRt-10.js";
|
|
5
|
-
import b from "../tokeniser/bpe.js";
|
|
6
|
-
import { dummyPassAsync as u } from "../utilities/dummy.js";
|
|
7
|
-
import "../Generator.js";
|
|
8
|
-
import "../index-Dwqa6Zy2.js";
|
|
9
|
-
import "../dataset-CtdBYwjo.js";
|
|
10
|
-
import "../index-Tf7vU29b.js";
|
|
11
|
-
import "../ops/cpu/adamAdjust.js";
|
|
12
|
-
import "../ops/webgl/adamAdjust.js";
|
|
13
|
-
import "../ops/cpu/adamMoments.js";
|
|
14
|
-
import "../ops/webgl/adamMoments.js";
|
|
15
|
-
import "../papaparse.min-C8l2Kvo1.js";
|
|
16
|
-
import "../ops/cpu/scatterSub.js";
|
|
17
|
-
import "../ops/webgl/scatterSub.js";
|
|
18
|
-
import "../ops/cpu/gatherSub.js";
|
|
19
|
-
import "../ops/webgl/gatherSub.js";
|
|
1
|
+
import { a1 as y } from "../index-CUQrfsw_.js";
|
|
20
2
|
import "../ops/cpu/attentionMask.js";
|
|
21
3
|
import "../ops/webgl/attentionMask.js";
|
|
22
4
|
import "../ops/grads/attentionMask.js";
|
|
23
5
|
import "../ops/cpu/qkv.js";
|
|
24
6
|
import "../ops/webgl/qkv.js";
|
|
25
7
|
import "../ops/grads/qkv.js";
|
|
26
|
-
import "../random_width-
|
|
27
|
-
import "../register_all_kernels-
|
|
8
|
+
import "../random_width-D8Pwy_na.js";
|
|
9
|
+
import "../register_all_kernels-DUshvVWP.js";
|
|
10
|
+
import "../index-Tf7vU29b.js";
|
|
11
|
+
import "../dataset-CJmEGu6D.js";
|
|
28
12
|
import "../ops/cpu/rope.js";
|
|
29
13
|
import "../ops/webgl/rope.js";
|
|
30
14
|
import "../ops/grads/rope.js";
|
|
@@ -36,45 +20,52 @@ import "../ops/grads/fusedSoftmax.js";
|
|
|
36
20
|
import "../ops/cpu/matMulGelu.js";
|
|
37
21
|
import "../ops/webgl/matMulGelu.js";
|
|
38
22
|
import "../ops/grads/matMulGelu.js";
|
|
39
|
-
import "../ops/cpu/gelu.js";
|
|
40
|
-
import "../ops/webgl/gelu.js";
|
|
41
|
-
import "../gelu-C-dPj6Ku.js";
|
|
42
23
|
import "../ops/cpu/normRMS.js";
|
|
43
24
|
import "../ops/webgl/normRMS.js";
|
|
44
25
|
import "../ops/grads/normRMS.js";
|
|
26
|
+
import "../ops/cpu/gatherSub.js";
|
|
27
|
+
import "../ops/webgl/gatherSub.js";
|
|
28
|
+
import "../ops/cpu/scatterSub.js";
|
|
29
|
+
import "../ops/webgl/scatterSub.js";
|
|
30
|
+
import "../jszip.min-CjP2V1VV.js";
|
|
31
|
+
import h from "../tokeniser/CharTokeniser.js";
|
|
32
|
+
import k from "../tokeniser/bpe.js";
|
|
33
|
+
import { dummyPassAsync as g } from "../utilities/dummy.js";
|
|
34
|
+
import b from "../models/factory.js";
|
|
35
|
+
import "../Generator.js";
|
|
36
|
+
import "../index-Dwqa6Zy2.js";
|
|
37
|
+
import "../ops/cpu/adamAdjust.js";
|
|
38
|
+
import "../ops/webgl/adamAdjust.js";
|
|
39
|
+
import "../ops/cpu/adamMoments.js";
|
|
40
|
+
import "../ops/webgl/adamMoments.js";
|
|
41
|
+
import "../papaparse.min-C8l2Kvo1.js";
|
|
42
|
+
import "../ops/cpu/gelu.js";
|
|
43
|
+
import "../ops/webgl/gelu.js";
|
|
44
|
+
import "../gelu-Bd3UBBxg.js";
|
|
45
45
|
import "../ops/webgl/log.js";
|
|
46
|
-
import { importWeights as
|
|
47
|
-
async function
|
|
48
|
-
const
|
|
49
|
-
if (!
|
|
46
|
+
import { importWeights as u } from "../utilities/weights.js";
|
|
47
|
+
async function dt(r) {
|
|
48
|
+
const e = /* @__PURE__ */ new Map(), a = await r.file("manifest.json")?.async("string");
|
|
49
|
+
if (!a)
|
|
50
50
|
throw new Error("Manifest file not found in the zip archive");
|
|
51
|
-
const m = JSON.parse(
|
|
52
|
-
for (const [t,
|
|
53
|
-
|
|
54
|
-
const p = await
|
|
51
|
+
const m = JSON.parse(a);
|
|
52
|
+
for (const [t, o] of Object.entries(m.weightSpec))
|
|
53
|
+
e.set(t, { spec: o, data: new Float32Array() });
|
|
54
|
+
const p = await r.file("tokeniser.json")?.async("string");
|
|
55
55
|
if (!p)
|
|
56
56
|
throw new Error("Tokeniser file not found in the zip archive");
|
|
57
|
-
const i = JSON.parse(p),
|
|
58
|
-
for (const t of Object.keys(
|
|
57
|
+
const i = JSON.parse(p), f = (i.type ?? "char") === "char" ? new h(i.vocab) : new k(i.vocab, i.merges), c = /* @__PURE__ */ new Map();
|
|
58
|
+
for (const t of Object.keys(r.files))
|
|
59
59
|
if (t.endsWith(".bin")) {
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
c.set(
|
|
64
|
-
}
|
|
65
|
-
k();
|
|
66
|
-
const e = new d(m.config);
|
|
67
|
-
await u(e), e.loadWeights(c);
|
|
68
|
-
const f = await o.file("log.json")?.async("string");
|
|
69
|
-
if (f)
|
|
70
|
-
try {
|
|
71
|
-
const t = JSON.parse(f);
|
|
72
|
-
e.log = t;
|
|
73
|
-
} catch (t) {
|
|
74
|
-
throw console.error("Error parsing training log:", t), new Error(`Failed to parse training log: ${t}`);
|
|
60
|
+
const o = t.replace(".bin", ""), l = await r.file(t).async("arraybuffer"), w = new Float32Array(l), s = e.get(o) || { spec: [], data: new Float32Array() };
|
|
61
|
+
s.data = w, e.set(o, s);
|
|
62
|
+
const d = await u(s);
|
|
63
|
+
c.set(o, d);
|
|
75
64
|
}
|
|
76
|
-
|
|
65
|
+
y();
|
|
66
|
+
const n = b(m.config);
|
|
67
|
+
return await g(n), n.loadWeights(c), { model: n, tokeniser: f };
|
|
77
68
|
}
|
|
78
69
|
export {
|
|
79
|
-
|
|
70
|
+
dt as default
|
|
80
71
|
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ITokeniser } from '../tokeniser/type';
|
|
2
|
+
import { default as Model, ModelForwardAttributes } from '../models/model';
|
|
3
|
+
export interface SaveOptions {
|
|
4
|
+
name?: string;
|
|
5
|
+
metadata?: Record<string, unknown>;
|
|
6
|
+
files?: Record<string, unknown>;
|
|
7
|
+
}
|
|
8
|
+
export declare function saveModel(model: Model<ModelForwardAttributes>, tokeniser: ITokeniser, options?: SaveOptions): Promise<Blob>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { j as g } from "../jszip.min-CjP2V1VV.js";
|
|
2
|
+
import b from "../tokeniser/CharTokeniser.js";
|
|
3
|
+
import { save_safetensors as p } from "../utilities/safetensors.js";
|
|
4
|
+
import { VERSION as y } from "./load.js";
|
|
5
|
+
async function v(e, n, a) {
|
|
6
|
+
const o = /* @__PURE__ */ new Map();
|
|
7
|
+
e.saveWeights(o);
|
|
8
|
+
const i = new g(), r = {};
|
|
9
|
+
o.forEach((t, s) => {
|
|
10
|
+
t.length === 1 && (r[s] = t[0]);
|
|
11
|
+
});
|
|
12
|
+
const f = await p(r);
|
|
13
|
+
i.file("model.safetensors", f, { binary: !0 });
|
|
14
|
+
const c = {
|
|
15
|
+
model_type: e.config.modelType || e.getClassName(),
|
|
16
|
+
vocab_size: n.getVocab().length,
|
|
17
|
+
hidden_size: e.config.nEmbed,
|
|
18
|
+
num_hidden_layers: e.config.nLayer,
|
|
19
|
+
num_attention_heads: e.config.nHead,
|
|
20
|
+
block_size: e.config.blockSize,
|
|
21
|
+
dropout: e.config.dropout,
|
|
22
|
+
biasInLinear: e.config.biasInLinear,
|
|
23
|
+
biasInLayerNorm: e.config.biasInLayerNorm,
|
|
24
|
+
mlpFactor: e.config.mlpFactor,
|
|
25
|
+
useRope: e.config.useRope
|
|
26
|
+
};
|
|
27
|
+
if (i.file("config.json", JSON.stringify(c, void 0, 4), {
|
|
28
|
+
binary: !1
|
|
29
|
+
}), i.file(
|
|
30
|
+
"meta.json",
|
|
31
|
+
JSON.stringify(
|
|
32
|
+
{
|
|
33
|
+
version: y,
|
|
34
|
+
application: "@genai-fi/nanogpt",
|
|
35
|
+
meta: a?.metadata,
|
|
36
|
+
name: a?.name,
|
|
37
|
+
training: e.trainingState || void 0
|
|
38
|
+
},
|
|
39
|
+
void 0,
|
|
40
|
+
4
|
|
41
|
+
),
|
|
42
|
+
{
|
|
43
|
+
binary: !1
|
|
44
|
+
}
|
|
45
|
+
), i.file(
|
|
46
|
+
"tokeniser.json",
|
|
47
|
+
JSON.stringify({
|
|
48
|
+
type: n instanceof b ? "char" : "bpe",
|
|
49
|
+
vocab: n.getVocab(),
|
|
50
|
+
merges: await n.getMerges()
|
|
51
|
+
}),
|
|
52
|
+
{
|
|
53
|
+
binary: !1
|
|
54
|
+
}
|
|
55
|
+
), a?.files)
|
|
56
|
+
for (const [t, s] of Object.entries(a.files))
|
|
57
|
+
i.file(t, JSON.stringify(s), { binary: !1 });
|
|
58
|
+
return i.generateAsync({ type: "blob" });
|
|
59
|
+
}
|
|
60
|
+
export {
|
|
61
|
+
v as saveModel
|
|
62
|
+
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { B as r, C as p, E as u,
|
|
2
|
-
import { e as K } from "./axis_util-
|
|
3
|
-
import { m as T } from "./max-
|
|
4
|
-
import { r as m } from "./reshape-
|
|
5
|
-
import { s as _ } from "./sum-
|
|
1
|
+
import { B as r, C as p, E as u, ag as h, ah as E, p as S, c as $, q as d } from "./index-CUQrfsw_.js";
|
|
2
|
+
import { e as K } from "./axis_util-DubwyOhW.js";
|
|
3
|
+
import { m as T } from "./max-B3JOcNGb.js";
|
|
4
|
+
import { r as m } from "./reshape-DEfQGSin.js";
|
|
5
|
+
import { s as _ } from "./sum-l_0SqM4h.js";
|
|
6
6
|
/**
|
|
7
7
|
* @license
|
|
8
8
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
@@ -21,7 +21,7 @@ import { s as _ } from "./sum-66ew2byf.js";
|
|
|
21
21
|
*/
|
|
22
22
|
function b(s) {
|
|
23
23
|
const o = { x: p(s, "x", "exp") };
|
|
24
|
-
return u.runKernel(
|
|
24
|
+
return u.runKernel(h, o);
|
|
25
25
|
}
|
|
26
26
|
const N = /* @__PURE__ */ r({ exp_: b });
|
|
27
27
|
/**
|
|
@@ -40,11 +40,11 @@ const N = /* @__PURE__ */ r({ exp_: b });
|
|
|
40
40
|
* limitations under the License.
|
|
41
41
|
* =============================================================================
|
|
42
42
|
*/
|
|
43
|
-
function
|
|
43
|
+
function q(s) {
|
|
44
44
|
const o = { x: p(s, "x", "log", "float32") };
|
|
45
|
-
return u.runKernel(
|
|
45
|
+
return u.runKernel(E, o);
|
|
46
46
|
}
|
|
47
|
-
const
|
|
47
|
+
const v = /* @__PURE__ */ r({ log_: q });
|
|
48
48
|
/**
|
|
49
49
|
* @license
|
|
50
50
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
@@ -61,22 +61,22 @@ const w = /* @__PURE__ */ r({ log_: v });
|
|
|
61
61
|
* limitations under the License.
|
|
62
62
|
* =============================================================================
|
|
63
63
|
*/
|
|
64
|
-
function
|
|
64
|
+
function w(s, n = null, o = !1) {
|
|
65
65
|
const a = p(s, "x", "logSumExp"), t = S(n, a.shape), x = T(
|
|
66
66
|
a,
|
|
67
67
|
t,
|
|
68
68
|
!0
|
|
69
69
|
/* keepDims */
|
|
70
|
-
), i = $(a, x), l = N(i), f = _(l, t), c =
|
|
70
|
+
), i = $(a, x), l = N(i), f = _(l, t), c = v(f), e = d(m(x, c.shape), c);
|
|
71
71
|
if (o) {
|
|
72
72
|
const g = K(e.shape, t);
|
|
73
73
|
return m(e, g);
|
|
74
74
|
}
|
|
75
75
|
return e;
|
|
76
76
|
}
|
|
77
|
-
const
|
|
77
|
+
const I = /* @__PURE__ */ r({ logSumExp_: w });
|
|
78
78
|
export {
|
|
79
|
-
|
|
79
|
+
v as a,
|
|
80
80
|
N as e,
|
|
81
|
-
|
|
81
|
+
I as l
|
|
82
82
|
};
|
package/dist/main.d.ts
CHANGED
|
@@ -2,18 +2,19 @@ import { default as CausalSelfAttention } from './layers/CausalSelfAttention';
|
|
|
2
2
|
import { default as MLP } from './layers/MLP';
|
|
3
3
|
import { default as TransformerBlock } from './layers/TransformerBlock';
|
|
4
4
|
import { default as RoPECache } from './layers/RoPECache';
|
|
5
|
-
export { default as NanoGPT } from './
|
|
5
|
+
export { default as NanoGPT } from './models/NanoGPTV1';
|
|
6
6
|
export { default as TeachableLLM } from './TeachableLLM';
|
|
7
7
|
export { default as CharTokeniser } from './tokeniser/CharTokeniser';
|
|
8
8
|
export { default as BPETokeniser } from './tokeniser/bpe';
|
|
9
9
|
export { default as waitForModel } from './utilities/waitForModel';
|
|
10
10
|
export { default as loadTextData } from './data/textLoader';
|
|
11
|
+
export { default as Generator } from './Generator';
|
|
11
12
|
export type { ITrainerOptions } from './Trainer';
|
|
12
13
|
export type { IGenerateOptions } from './Generator';
|
|
13
|
-
export type
|
|
14
|
+
export { type ModelForwardAttributes, default as Model } from './models/model';
|
|
14
15
|
export type { ITokeniser } from './tokeniser/type';
|
|
15
|
-
export type { TrainingProgress } from './training/Trainer';
|
|
16
|
-
export type { GPTConfig } from './config';
|
|
16
|
+
export type { TrainingProgress, TrainingLogEntry } from './training/Trainer';
|
|
17
|
+
export type { GPTConfig } from './models/config';
|
|
17
18
|
export { estimateParameterCount, estimateMemoryUsage, estimateTrainingMemoryUsage, estimateResources, validateConfig, } from './utilities/parameters';
|
|
18
19
|
export { selectBackend } from './backend';
|
|
19
20
|
export { default as performanceTest } from './utilities/performance';
|
package/dist/main.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { default as W } from "./
|
|
1
|
+
import { default as W } from "./models/NanoGPTV1.js";
|
|
2
2
|
import { default as Y } from "./TeachableLLM.js";
|
|
3
3
|
import { default as _ } from "./tokeniser/CharTokeniser.js";
|
|
4
4
|
import { default as oo } from "./tokeniser/bpe.js";
|
|
5
5
|
import { default as to } from "./utilities/waitForModel.js";
|
|
6
6
|
import { default as eo } from "./data/textLoader.js";
|
|
7
|
-
import {
|
|
8
|
-
import "./
|
|
7
|
+
import { default as io } from "./Generator.js";
|
|
8
|
+
import { default as fo } from "./models/model.js";
|
|
9
|
+
import { estimateMemoryUsage as lo, estimateParameterCount as xo, estimateResources as no, estimateTrainingMemoryUsage as uo, validateConfig as co } from "./utilities/parameters.js";
|
|
10
|
+
import "./index-CUQrfsw_.js";
|
|
9
11
|
import "./ops/cpu/scatterSub.js";
|
|
10
12
|
import "./ops/webgl/scatterSub.js";
|
|
11
13
|
import "./ops/cpu/gatherSub.js";
|
|
@@ -16,10 +18,10 @@ import "./ops/grads/attentionMask.js";
|
|
|
16
18
|
import "./ops/cpu/qkv.js";
|
|
17
19
|
import "./ops/webgl/qkv.js";
|
|
18
20
|
import "./ops/grads/qkv.js";
|
|
19
|
-
import "./random_width-
|
|
20
|
-
import "./register_all_kernels-
|
|
21
|
+
import "./random_width-D8Pwy_na.js";
|
|
22
|
+
import "./register_all_kernels-DUshvVWP.js";
|
|
21
23
|
import "./index-Tf7vU29b.js";
|
|
22
|
-
import "./dataset-
|
|
24
|
+
import "./dataset-CJmEGu6D.js";
|
|
23
25
|
import "./ops/cpu/rope.js";
|
|
24
26
|
import "./ops/webgl/rope.js";
|
|
25
27
|
import "./ops/grads/rope.js";
|
|
@@ -33,7 +35,7 @@ import "./ops/webgl/matMulGelu.js";
|
|
|
33
35
|
import "./ops/grads/matMulGelu.js";
|
|
34
36
|
import "./ops/cpu/gelu.js";
|
|
35
37
|
import "./ops/webgl/gelu.js";
|
|
36
|
-
import "./gelu-
|
|
38
|
+
import "./gelu-Bd3UBBxg.js";
|
|
37
39
|
import "./ops/cpu/normRMS.js";
|
|
38
40
|
import "./ops/webgl/normRMS.js";
|
|
39
41
|
import "./ops/grads/normRMS.js";
|
|
@@ -42,13 +44,13 @@ import "./ops/cpu/adamMoments.js";
|
|
|
42
44
|
import "./ops/webgl/adamMoments.js";
|
|
43
45
|
import "./ops/cpu/adamAdjust.js";
|
|
44
46
|
import "./ops/webgl/adamAdjust.js";
|
|
45
|
-
import { selectBackend as
|
|
46
|
-
import { default as
|
|
47
|
+
import { selectBackend as Mo } from "./backend.js";
|
|
48
|
+
import { default as Co } from "./utilities/performance.js";
|
|
47
49
|
import o from "./layers/CausalSelfAttention.js";
|
|
48
50
|
import r from "./layers/MLP.js";
|
|
49
51
|
import t from "./layers/TransformerBlock.js";
|
|
50
52
|
import m from "./layers/RoPECache.js";
|
|
51
|
-
import { default as
|
|
53
|
+
import { default as go } from "./training/AdamExt.js";
|
|
52
54
|
const O = {
|
|
53
55
|
CausalSelfAttention: o,
|
|
54
56
|
MLP: r,
|
|
@@ -56,19 +58,21 @@ const O = {
|
|
|
56
58
|
RoPECache: m
|
|
57
59
|
};
|
|
58
60
|
export {
|
|
59
|
-
|
|
61
|
+
go as AdamExt,
|
|
60
62
|
oo as BPETokeniser,
|
|
61
63
|
_ as CharTokeniser,
|
|
64
|
+
io as Generator,
|
|
65
|
+
fo as Model,
|
|
62
66
|
W as NanoGPT,
|
|
63
67
|
Y as TeachableLLM,
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
+
lo as estimateMemoryUsage,
|
|
69
|
+
xo as estimateParameterCount,
|
|
70
|
+
no as estimateResources,
|
|
71
|
+
uo as estimateTrainingMemoryUsage,
|
|
68
72
|
O as layers,
|
|
69
73
|
eo as loadTextData,
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
74
|
+
Co as performanceTest,
|
|
75
|
+
Mo as selectBackend,
|
|
76
|
+
co as validateConfig,
|
|
73
77
|
to as waitForModel
|
|
74
78
|
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { B as r, C as s, N as m, E as c, Y as d } from "./index-CUQrfsw_.js";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
* =============================================================================
|
|
17
|
+
*/
|
|
18
|
+
function p(t, e) {
|
|
19
|
+
let o = s(t, "a", "mod"), a = s(e, "b", "mod");
|
|
20
|
+
[o, a] = m(o, a);
|
|
21
|
+
const n = { a: o, b: a };
|
|
22
|
+
return c.runKernel(d, n);
|
|
23
|
+
}
|
|
24
|
+
const b = /* @__PURE__ */ r({ mod_: p });
|
|
25
|
+
export {
|
|
26
|
+
b as m
|
|
27
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { GPTConfig } from './config';
|
|
2
|
+
import { Tensor } from '@tensorflow/tfjs-core';
|
|
3
|
+
import { default as Model, ModelForwardAttributes } from './model';
|
|
4
|
+
export default class NanoGPT extends Model<ModelForwardAttributes> {
|
|
5
|
+
private wte;
|
|
6
|
+
private wpe?;
|
|
7
|
+
private blocks;
|
|
8
|
+
private lnF;
|
|
9
|
+
private ropeCache?;
|
|
10
|
+
constructor(config?: Partial<GPTConfig>);
|
|
11
|
+
getClassName(): string;
|
|
12
|
+
private inputPhase;
|
|
13
|
+
forward(attrs: ModelForwardAttributes, idx: Tensor, targets?: Tensor): Tensor[];
|
|
14
|
+
dispose(): void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { defaultConfig as m } from "./config.js";
|
|
2
|
+
import f from "../layers/TransformerBlock.js";
|
|
3
|
+
import u from "../layers/TiedEmbedding.js";
|
|
4
|
+
import g from "../layers/RoPECache.js";
|
|
5
|
+
import b from "../layers/RMSNorm.js";
|
|
6
|
+
import { t as l, k as p } from "../index-CUQrfsw_.js";
|
|
7
|
+
import w from "./model.js";
|
|
8
|
+
import k from "../layers/PositionEmbedding.js";
|
|
9
|
+
class R extends w {
|
|
10
|
+
wte;
|
|
11
|
+
// Token embeddings
|
|
12
|
+
wpe;
|
|
13
|
+
// Position embeddings
|
|
14
|
+
// private drop: layers.Layer; // Dropout
|
|
15
|
+
blocks;
|
|
16
|
+
lnF;
|
|
17
|
+
// Final layer norm
|
|
18
|
+
ropeCache;
|
|
19
|
+
constructor(e = {}) {
|
|
20
|
+
super({ ...m, ...e }), this.wte = new u(this.config, "token_embedding", this), this.config.useRope === !1 ? this.wpe = new k(this.config, "positional_embedding", this) : this.ropeCache = new g(this.config), this.blocks = [];
|
|
21
|
+
for (let i = 0; i < this.config.nLayer; i++)
|
|
22
|
+
this.blocks.push(new f(i, this.config, this));
|
|
23
|
+
this.lnF = new b(this.config, "final_rms_norm", this);
|
|
24
|
+
}
|
|
25
|
+
getClassName() {
|
|
26
|
+
return "GenAI_NanoGPT_v1";
|
|
27
|
+
}
|
|
28
|
+
inputPhase(e, i) {
|
|
29
|
+
return l(() => {
|
|
30
|
+
const s = this.wte.embed(e);
|
|
31
|
+
if (this.config.useRope === !1) {
|
|
32
|
+
const o = this.wpe.call(i, s);
|
|
33
|
+
if (Array.isArray(o))
|
|
34
|
+
throw new Error("PositionEmbedding output should not be an array");
|
|
35
|
+
return o;
|
|
36
|
+
}
|
|
37
|
+
return s;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
forward(e, i, s) {
|
|
41
|
+
return this.validateInput(i), e.ropeCache = this.ropeCache, e.outputEmbeddings && (e.embeddings = []), l(() => {
|
|
42
|
+
this.startMemory();
|
|
43
|
+
let o = this.inputPhase(i, e);
|
|
44
|
+
if (e.cache && e.cache.length !== this.blocks.length)
|
|
45
|
+
throw console.error("Cache", e.cache), new Error(
|
|
46
|
+
`Cache length ${e.cache.length} does not match number of blocks ${this.blocks.length}`
|
|
47
|
+
);
|
|
48
|
+
for (let t = 0; t < this.blocks.length; t++) {
|
|
49
|
+
const c = this.blocks[t], d = Math.random() * 1e9, r = {
|
|
50
|
+
ropeCache: e.ropeCache,
|
|
51
|
+
training: e.training,
|
|
52
|
+
seed: d,
|
|
53
|
+
attentionScores: e.attentionScores,
|
|
54
|
+
pastKV: e.cache ? e.cache[t] : void 0
|
|
55
|
+
}, a = e.checkpointing && e.training ? c.callCheckpoint(r, o) : c.call(r, o);
|
|
56
|
+
e.outputEmbeddings ? (p(o), e.embeddings.push(o)) : o.dispose(), o = a;
|
|
57
|
+
}
|
|
58
|
+
o = this.lnF.call(e, o);
|
|
59
|
+
const n = this.wte.project(o);
|
|
60
|
+
e.outputEmbeddings ? (p(o), e.embeddings.push(o)) : o.dispose();
|
|
61
|
+
let h;
|
|
62
|
+
return s && (h = this.calculateLoss(n, s)), this.endMemory("Forward"), h ? [n, h] : [n];
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
dispose() {
|
|
66
|
+
this.wte.dispose(), this.wpe && this.wpe.dispose(), this.blocks.forEach((e) => e.dispose()), this.lnF.dispose();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export {
|
|
70
|
+
R as default
|
|
71
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import o from "./NanoGPTV1.js";
|
|
2
|
+
function a(e) {
|
|
3
|
+
const n = e.modelType || "GenAI_NanoGPT_v1";
|
|
4
|
+
switch (n) {
|
|
5
|
+
case "GenAI_NanoGPT_1":
|
|
6
|
+
case "GenAI_NanoGPT_v1":
|
|
7
|
+
return new o(e);
|
|
8
|
+
default:
|
|
9
|
+
throw new Error(`Unsupported model type: ${n}`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export {
|
|
13
|
+
a as default
|
|
14
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Tensor } from '@tensorflow/tfjs-core';
|
|
2
|
+
import { ForwardAttributes, default as BaseLayer } from '../layers/BaseLayer';
|
|
3
|
+
import { AttentionScores, KVCache } from '../layers/CausalSelfAttention';
|
|
4
|
+
export interface ModelForwardAttributes extends ForwardAttributes {
|
|
5
|
+
cache?: KVCache[];
|
|
6
|
+
attentionScores?: AttentionScores;
|
|
7
|
+
seed?: number;
|
|
8
|
+
outputEmbeddings?: boolean;
|
|
9
|
+
embeddings?: Tensor[];
|
|
10
|
+
}
|
|
11
|
+
interface TrainingState {
|
|
12
|
+
steps: number;
|
|
13
|
+
learningRate: number;
|
|
14
|
+
batchSize: number;
|
|
15
|
+
loss: number;
|
|
16
|
+
}
|
|
17
|
+
export default abstract class Model<T extends ModelForwardAttributes> extends BaseLayer<T> {
|
|
18
|
+
trainingState: TrainingState | null;
|
|
19
|
+
abstract getClassName(): string;
|
|
20
|
+
abstract forward(attrs: T, idx: Tensor, targets?: Tensor): Tensor[];
|
|
21
|
+
abstract dispose(): void;
|
|
22
|
+
getNumParams(): number;
|
|
23
|
+
protected validateInput(idx: Tensor): void;
|
|
24
|
+
protected calculateLoss(logits: Tensor, targets: Tensor): Tensor;
|
|
25
|
+
}
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import i from "../layers/BaseLayer.js";
|
|
2
|
+
import "../index-CUQrfsw_.js";
|
|
3
|
+
import "../ops/cpu/attentionMask.js";
|
|
4
|
+
import "../ops/webgl/attentionMask.js";
|
|
5
|
+
import "../ops/grads/attentionMask.js";
|
|
6
|
+
import "../ops/cpu/qkv.js";
|
|
7
|
+
import "../ops/webgl/qkv.js";
|
|
8
|
+
import "../ops/grads/qkv.js";
|
|
9
|
+
import "../random_width-D8Pwy_na.js";
|
|
10
|
+
import "../register_all_kernels-DUshvVWP.js";
|
|
11
|
+
import "../index-Tf7vU29b.js";
|
|
12
|
+
import "../dataset-CJmEGu6D.js";
|
|
13
|
+
import "../ops/cpu/rope.js";
|
|
14
|
+
import "../ops/webgl/rope.js";
|
|
15
|
+
import "../ops/grads/rope.js";
|
|
16
|
+
import "../ops/cpu/appendCache.js";
|
|
17
|
+
import "../ops/webgl/appendCache.js";
|
|
18
|
+
import "../ops/cpu/fusedSoftmax.js";
|
|
19
|
+
import "../ops/webgl/fusedSoftmax.js";
|
|
20
|
+
import "../ops/grads/fusedSoftmax.js";
|
|
21
|
+
import "../ops/cpu/matMulGelu.js";
|
|
22
|
+
import "../ops/webgl/matMulGelu.js";
|
|
23
|
+
import "../ops/grads/matMulGelu.js";
|
|
24
|
+
import "../ops/cpu/normRMS.js";
|
|
25
|
+
import "../ops/webgl/normRMS.js";
|
|
26
|
+
import "../ops/grads/normRMS.js";
|
|
27
|
+
import "../jszip.min-CjP2V1VV.js";
|
|
28
|
+
import "../index-Dwqa6Zy2.js";
|
|
29
|
+
import "../Generator.js";
|
|
30
|
+
import "../ops/cpu/adamAdjust.js";
|
|
31
|
+
import "../ops/webgl/adamAdjust.js";
|
|
32
|
+
import "../ops/cpu/adamMoments.js";
|
|
33
|
+
import "../ops/webgl/adamMoments.js";
|
|
34
|
+
import "../papaparse.min-C8l2Kvo1.js";
|
|
35
|
+
import { estimateParameterCount as p } from "../utilities/parameters.js";
|
|
36
|
+
import "../ops/cpu/scatterSub.js";
|
|
37
|
+
import "../ops/webgl/scatterSub.js";
|
|
38
|
+
import "../ops/cpu/gatherSub.js";
|
|
39
|
+
import "../ops/webgl/gatherSub.js";
|
|
40
|
+
import "../ops/cpu/gelu.js";
|
|
41
|
+
import "../ops/webgl/gelu.js";
|
|
42
|
+
import "../gelu-Bd3UBBxg.js";
|
|
43
|
+
import "../ops/webgl/log.js";
|
|
44
|
+
import { createSoftmaxCrossEntropyWithGrad as m } from "../training/sparseCrossEntropy.js";
|
|
45
|
+
class Y extends i {
|
|
46
|
+
trainingState = null;
|
|
47
|
+
getNumParams() {
|
|
48
|
+
return p(this.config);
|
|
49
|
+
}
|
|
50
|
+
validateInput(t) {
|
|
51
|
+
if (t.shape.length !== 2)
|
|
52
|
+
throw new Error(`Invalid input shape: expected [batch_size, sequence_length], got ${t.shape}`);
|
|
53
|
+
if (t.shape[1] > this.config.blockSize)
|
|
54
|
+
throw new Error(`Input sequence length ${t.shape[1]} isn't block size ${this.config.blockSize}`);
|
|
55
|
+
if (t.dtype !== "int32")
|
|
56
|
+
throw new Error(`Input tensor must be of type int32, got ${t.dtype}`);
|
|
57
|
+
}
|
|
58
|
+
calculateLoss(t, o) {
|
|
59
|
+
try {
|
|
60
|
+
return m()(t, o).mean();
|
|
61
|
+
} catch (r) {
|
|
62
|
+
throw console.error("Error computing loss:", r), new Error(`Loss computation failed: ${r}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
Y as default
|
|
68
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { z as c, c as f } from "./zeros
|
|
1
|
+
import { L as n, M as t, j as m, E as i } from "./index-CUQrfsw_.js";
|
|
2
|
+
import { z as c, c as f } from "./zeros-VZ72lWXM.js";
|
|
3
3
|
/**
|
|
4
4
|
* @license
|
|
5
5
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
package/dist/ops/adamAdjust.js
CHANGED