@genai-fi/nanogpt 0.5.6 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Generator.js +10 -9
- package/dist/NanoGPTModel.js +70 -121
- package/dist/RealDiv-7xu-pkZN.js +540 -0
- package/dist/Reshape-BYC1oUku.js +127 -0
- package/dist/TeachableLLM.d.ts +2 -0
- package/dist/TeachableLLM.js +42 -34
- package/dist/{TiedEmbedding-8S8xn8e6.js → TiedEmbedding-C1HBot-5.js} +12 -13
- package/dist/{axis_util-BczFISHz.js → axis_util-CCNL7jea.js} +14 -12
- package/dist/{broadcast_to-B7NGsBSh.js → broadcast_to-CddAF879.js} +2 -2
- package/dist/{concat-DdKPyAtw.js → concat-XOK9ANZu.js} +7 -7
- package/dist/{dataset-iqT4Otvb.js → dataset-BFFipD1c.js} +5 -5
- package/dist/{dropout-B09InSJS.js → dropout-xlKRoJyU.js} +9 -9
- package/dist/{gather-D6MsdXqc.js → gather-DKtUaTtA.js} +1 -1
- package/dist/gpgpu_math-B_ycgZ4W.js +3115 -0
- package/dist/{index-Du-bmOP8.js → index-CamYe_M8.js} +844 -647
- package/dist/{kernel_funcs_utils-DShm7-0k.js → kernel_funcs_utils-D5MS0JFg.js} +232 -136
- package/dist/layers/BaseLayer.js +2 -2
- package/dist/layers/CausalSelfAttention.js +6 -6
- package/dist/layers/MLP.js +5 -5
- package/dist/layers/RMSNorm.js +3 -3
- package/dist/layers/RoPECache.js +13 -33
- package/dist/layers/TiedEmbedding.js +6 -7
- package/dist/layers/TransformerBlock.js +1 -1
- package/dist/{log_sum_exp-CxfBtUaG.js → log_sum_exp-CV_5-TTu.js} +15 -15
- package/dist/main.js +24 -20
- package/dist/{mat_mul-CbiqIe2d.js → mat_mul-CAbRFWUj.js} +4 -4
- package/dist/{max-0Xnlpv8k.js → max-JBBv7aUf.js} +3 -3
- package/dist/mulmat_packed_gpu-DW4doKL_.js +71 -0
- package/dist/{norm-01kY9I2B.js → norm-B9dQTFYn.js} +12 -12
- package/dist/{ones-CrutWGas.js → ones-CMHNqMr6.js} +2 -2
- package/dist/ops/appendCache.js +3 -3
- package/dist/ops/attentionMask.js +1 -1
- 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 +5 -5
- package/dist/ops/cpu/gelu.js +1 -1
- package/dist/ops/cpu/matMulGelu.js +1 -1
- 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 +18 -49
- package/dist/ops/fusedSoftmax.js +1 -1
- package/dist/ops/gatherSub.js +1 -1
- package/dist/ops/gelu.js +1 -1
- package/dist/ops/grads/attentionMask.js +15 -11
- package/dist/ops/grads/fusedSoftmax.js +12 -10
- package/dist/ops/grads/gelu.js +1 -1
- 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/log.d.ts +0 -0
- package/dist/ops/log.js +1 -0
- package/dist/ops/matMulGelu.js +1 -1
- package/dist/ops/matMulMul.js +1 -1
- package/dist/ops/mulDrop.js +1 -1
- package/dist/ops/node/sparseCrossEntropy.js +1 -1
- package/dist/ops/normRMS.js +1 -1
- package/dist/ops/qkv.js +1 -1
- package/dist/ops/rope.js +8 -4
- package/dist/ops/scatterSub.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 +31 -3379
- package/dist/ops/webgl/gatherSub.js +1 -1
- package/dist/ops/webgl/gelu.js +2 -2
- package/dist/{gpgpu_math-BFbOyvk4.js → ops/webgl/log.d.ts} +2 -8
- package/dist/ops/webgl/log.js +39 -0
- package/dist/ops/webgl/matMulGelu.js +48 -115
- 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-CJNniCAV.js → ops-DqtYemmV.js} +143 -135
- package/dist/{random_width-C-v-35bY.js → random_width-CLMQG5Jn.js} +6925 -6291
- package/dist/{range-Bvs1hidm.js → range-DqYjKnuG.js} +1 -1
- package/dist/reciprocal-z49filta.js +25 -0
- package/dist/register_all_kernels-COt6wLD0.js +21397 -0
- package/dist/{reshape-BH7eBpwq.js → reshape-C45vIIRU.js} +1 -1
- package/dist/scatter_nd_util-qgtnviTE.js +46 -0
- package/dist/selu_util-4QV_GXTB.js +740 -0
- package/dist/shared-ByfrGA97.js +3199 -0
- package/dist/{sin-CPAZXNjH.js → sin-9JBrfVaB.js} +1 -1
- package/dist/{softmax-DhWoBa7r.js → softmax-DvMvui-_.js} +1 -1
- package/dist/{split-BCUhuU7B.js → split-DxrHrPFK.js} +4 -4
- package/dist/{stack-BV1v7l3S.js → stack-DgaoDmnF.js} +1 -1
- package/dist/{sum-Cvq06317.js → sum-BpcpxNEh.js} +3 -3
- package/dist/{tensor-DgTOPY6h.js → tensor-CDz5x1mP.js} +1 -1
- package/dist/{tensor2d-CRWjDyUe.js → tensor2d-jO8JY5Jd.js} +1 -1
- package/dist/training/AdamExt.js +1 -1
- package/dist/training/DatasetBuilder.js +2 -2
- package/dist/training/FullTrainer.js +1 -1
- package/dist/training/Trainer.js +3 -3
- package/dist/training/sparseCrossEntropy.js +4 -4
- package/dist/utilities/dummy.d.ts +6 -0
- package/dist/utilities/dummy.js +31 -10
- package/dist/utilities/generate.js +3 -3
- package/dist/utilities/load.d.ts +25 -0
- package/dist/utilities/load.js +89 -37
- package/dist/utilities/profile.d.ts +5 -0
- package/dist/utilities/profile.js +12 -9
- package/dist/utilities/safetensors.d.ts +3 -0
- package/dist/utilities/safetensors.js +83 -0
- package/dist/utilities/save.js +47 -29
- package/dist/utilities/weights.js +2 -2
- package/dist/{variable-DZ3fF0R2.js → variable-CLVXjN7F.js} +1 -1
- package/dist/{zeros-BaHhQTWf.js → zeros-DUkkVccu.js} +8 -8
- package/package.json +3 -9
- package/dist/Reshape-Biok_3X1.js +0 -212
- package/dist/slice_util-DskXqRZa.js +0 -49
- package/dist/tfjs_backend-D9Ytje0G.js +0 -1010
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { q as p, w as i, E as a, H as c } from "./index-CamYe_M8.js";
|
|
2
2
|
/**
|
|
3
3
|
* @license
|
|
4
4
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
@@ -15,9 +15,9 @@ import { o as p, j as i, E as a, x as c } from "./index-Du-bmOP8.js";
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
* =============================================================================
|
|
17
17
|
*/
|
|
18
|
-
function e(t, s,
|
|
19
|
-
const
|
|
20
|
-
return a.runKernel(c,
|
|
18
|
+
function e(t, s, n = 0) {
|
|
19
|
+
const o = { x: i(t, "x", "split") }, r = { numOrSizeSplits: s, axis: n };
|
|
20
|
+
return a.runKernel(c, o, r);
|
|
21
21
|
}
|
|
22
22
|
const u = /* @__PURE__ */ p({ split_: e });
|
|
23
23
|
export {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { q as e, w as u, N as c, E as l, O as m } from "./index-CamYe_M8.js";
|
|
2
2
|
/**
|
|
3
3
|
* @license
|
|
4
4
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
@@ -15,10 +15,10 @@ import { o as e, j as u, D as c, E as l, F as m } from "./index-Du-bmOP8.js";
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
* =============================================================================
|
|
17
17
|
*/
|
|
18
|
-
function i(t,
|
|
18
|
+
function i(t, n = null, o = !1) {
|
|
19
19
|
let s = u(t, "x", "sum");
|
|
20
20
|
s.dtype === "bool" && (s = c(s, "int32"));
|
|
21
|
-
const r = { x: s }, a = { axis:
|
|
21
|
+
const r = { x: s }, a = { axis: n, keepDims: o };
|
|
22
22
|
return l.runKernel(m, r, a);
|
|
23
23
|
}
|
|
24
24
|
const f = /* @__PURE__ */ e({ sum_: i });
|
package/dist/training/AdamExt.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { A as r, b as c, f as h, s as g, e as o } from "../index-
|
|
1
|
+
import { A as r, b as c, f as h, s as g, e as o } from "../index-CamYe_M8.js";
|
|
2
2
|
class u extends r {
|
|
3
3
|
constructor(t, e, s, a, i) {
|
|
4
4
|
super(t, e, s, a), this.config = i, this.startLearningRate = t;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { generateText as T } from "../utilities/generate.js";
|
|
2
2
|
import L from "./Trainer.js";
|
|
3
3
|
import x from "./Evaluator.js";
|
|
4
|
-
import { a as h } from "../index-
|
|
4
|
+
import { a as h } from "../index-CamYe_M8.js";
|
|
5
5
|
import y from "../utilities/profile.js";
|
|
6
6
|
const D = {
|
|
7
7
|
desiredLoss: 0.01,
|
package/dist/training/Trainer.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { DatasetBuilder as g, flattenTokens as m, PAGE_FACTOR as u } from "./DatasetBuilder.js";
|
|
2
2
|
import f from "./AdamExt.js";
|
|
3
|
-
import { t as y, v as z, a as c } from "../index-
|
|
4
|
-
import { n as S } from "../norm-
|
|
5
|
-
import { z as p } from "../zeros-
|
|
3
|
+
import { t as y, v as z, a as c } from "../index-CamYe_M8.js";
|
|
4
|
+
import { n as S } from "../norm-B9dQTFYn.js";
|
|
5
|
+
import { z as p } from "../zeros-DUkkVccu.js";
|
|
6
6
|
class R {
|
|
7
7
|
constructor(t, e, s = 1e-3) {
|
|
8
8
|
this.tokenizer = e, this.model = t, this.learningRate = s, this.resetOptimizer(), this.datasetBuilder = new g(e, t.config.gpt.blockSize);
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { gatherSub as L } from "../ops/gatherSub.js";
|
|
2
2
|
import { scatterSub as y } from "../ops/scatterSub.js";
|
|
3
|
-
import { e as u, c as i, z as S, t as f, s as G } from "../index-
|
|
4
|
-
import { s as v } from "../softmax-
|
|
5
|
-
import { m as z } from "../max-
|
|
6
|
-
import { l as k } from "../log_sum_exp-
|
|
3
|
+
import { e as u, c as i, z as S, t as f, s as G } from "../index-CamYe_M8.js";
|
|
4
|
+
import { s as v } from "../softmax-DvMvui-_.js";
|
|
5
|
+
import { m as z } from "../max-JBBv7aUf.js";
|
|
6
|
+
import { l as k } from "../log_sum_exp-CV_5-TTu.js";
|
|
7
7
|
function F(a, s) {
|
|
8
8
|
return f(() => {
|
|
9
9
|
const e = a.shape[a.shape.length - 1], o = a.shape.slice(0, -1).reduce((d, c) => d * c, 1), p = a.shape.length > 2 ? a.reshape([o, e]) : a, n = s.shape.length > 1 ? s.reshape([o]).cast("int32") : s.cast("int32"), t = z(p, -1, !0), r = G(p, t), h = k(r, -1);
|
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
import { default as NanoGPT } from '../NanoGPTModel';
|
|
2
2
|
export declare function dummyPassAsync(model: NanoGPT): Promise<void>;
|
|
3
|
+
export interface MemoryRequirements {
|
|
4
|
+
perBatch: number;
|
|
5
|
+
tapeSize: number;
|
|
6
|
+
gradients: number;
|
|
7
|
+
}
|
|
8
|
+
export declare function dummyPassTrainAsync(model: NanoGPT): Promise<MemoryRequirements>;
|
|
3
9
|
export declare function dummyPass(model: NanoGPT): void;
|
package/dist/utilities/dummy.js
CHANGED
|
@@ -1,14 +1,35 @@
|
|
|
1
|
-
import "../index-
|
|
2
|
-
import { z as
|
|
3
|
-
async function
|
|
4
|
-
const
|
|
5
|
-
await
|
|
1
|
+
import { m as f, v as S, e as w } from "../index-CamYe_M8.js";
|
|
2
|
+
import { z as i } from "../zeros-DUkkVccu.js";
|
|
3
|
+
async function P(s) {
|
|
4
|
+
const t = i([1, s.config.gpt.blockSize], "int32"), [e, n] = s.forward({ training: !1 }, t);
|
|
5
|
+
await e.data(), e.dispose(), n && n.dispose(), t.dispose();
|
|
6
6
|
}
|
|
7
|
-
function
|
|
8
|
-
const
|
|
9
|
-
|
|
7
|
+
async function v(s) {
|
|
8
|
+
const t = f(), e = t.numBytesInGPUAllocated ?? t.numBytes;
|
|
9
|
+
await P(s);
|
|
10
|
+
const n = i([1, s.config.gpt.blockSize], "int32"), r = i([1, s.config.gpt.blockSize], "int32"), o = {
|
|
11
|
+
perBatch: 0,
|
|
12
|
+
tapeSize: 0,
|
|
13
|
+
gradients: s.getNumParams() * 4
|
|
14
|
+
}, g = () => {
|
|
15
|
+
const [a, l] = s.forward({ training: !0 }, n, r), d = w().state.activeTape;
|
|
16
|
+
let u = 0;
|
|
17
|
+
if (d)
|
|
18
|
+
for (const z of d)
|
|
19
|
+
u += z.saved?.reduce((B, I) => B + I.size * 4, 0) || 0;
|
|
20
|
+
return o.tapeSize = u, a.dispose(), l;
|
|
21
|
+
}, { value: c, grads: m } = S(g), p = f(), y = p.numBytesInGPUAllocated ?? p.numBytes;
|
|
22
|
+
o.perBatch = y - e - o.gradients, console.log("Dummy training memory requirements:", o), await c.data(), c.dispose();
|
|
23
|
+
for (const a in m)
|
|
24
|
+
m[a].dispose();
|
|
25
|
+
return n.dispose(), r.dispose(), o;
|
|
26
|
+
}
|
|
27
|
+
function A(s) {
|
|
28
|
+
const t = i([1, s.config.gpt.blockSize], "int32"), [e, n] = s.forward({ training: !1 }, t);
|
|
29
|
+
e.dispose(), n && n.dispose(), t.dispose();
|
|
10
30
|
}
|
|
11
31
|
export {
|
|
12
|
-
|
|
13
|
-
|
|
32
|
+
A as dummyPass,
|
|
33
|
+
P as dummyPassAsync,
|
|
34
|
+
v as dummyPassTrainAsync
|
|
14
35
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { t as y } from "../index-
|
|
2
|
-
import { t as x } from "../tensor2d-
|
|
3
|
-
import { c as f } from "../concat-
|
|
1
|
+
import { t as y } from "../index-CamYe_M8.js";
|
|
2
|
+
import { t as x } from "../tensor2d-jO8JY5Jd.js";
|
|
3
|
+
import { c as f } from "../concat-XOK9ANZu.js";
|
|
4
4
|
async function A(o, r, a, c, T) {
|
|
5
5
|
if (c <= 0)
|
|
6
6
|
throw new Error("Length must be a positive integer");
|
package/dist/utilities/load.d.ts
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
|
+
import { default as zip } from 'jszip';
|
|
1
2
|
import { default as NanoGPT } from '../NanoGPTModel';
|
|
2
3
|
import { ITokeniser } from '../tokeniser/type';
|
|
4
|
+
export declare const VERSION = 2;
|
|
5
|
+
export interface TransformersConfig {
|
|
6
|
+
model_type: string;
|
|
7
|
+
vocab_size: number;
|
|
8
|
+
hidden_size: number;
|
|
9
|
+
num_hidden_layers: number;
|
|
10
|
+
num_attention_heads: number;
|
|
11
|
+
block_size: number;
|
|
12
|
+
dropout: number;
|
|
13
|
+
biasInLinear: boolean;
|
|
14
|
+
biasInLayerNorm: boolean;
|
|
15
|
+
mlpFactor: number;
|
|
16
|
+
useRope: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface Metadata {
|
|
19
|
+
version: string;
|
|
20
|
+
application: string;
|
|
21
|
+
name?: string;
|
|
22
|
+
}
|
|
23
|
+
export declare function loadOldModel(zipFile: zip): Promise<{
|
|
24
|
+
model: NanoGPT;
|
|
25
|
+
tokeniser: ITokeniser;
|
|
26
|
+
}>;
|
|
3
27
|
export declare function loadModel(data: Blob | Buffer | string): Promise<{
|
|
4
28
|
model: NanoGPT;
|
|
5
29
|
tokeniser: ITokeniser;
|
|
30
|
+
name?: string;
|
|
6
31
|
}>;
|
package/dist/utilities/load.js
CHANGED
|
@@ -1,47 +1,99 @@
|
|
|
1
|
-
import { j as
|
|
2
|
-
import { importWeights as
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import { dummyPassAsync as
|
|
6
|
-
import { d as
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import { j as v } from "../jszip.min-CjP2V1VV.js";
|
|
2
|
+
import { importWeights as F } from "./weights.js";
|
|
3
|
+
import h from "../tokeniser/CharTokeniser.js";
|
|
4
|
+
import b from "../NanoGPTModel.js";
|
|
5
|
+
import { dummyPassAsync as u } from "./dummy.js";
|
|
6
|
+
import { d as k } from "../index-CamYe_M8.js";
|
|
7
|
+
import j from "../tokeniser/bpe.js";
|
|
8
|
+
import { load_safetensors as N } from "./safetensors.js";
|
|
9
|
+
const I = 2;
|
|
10
|
+
async function O(t) {
|
|
11
|
+
const s = await fetch(t);
|
|
12
|
+
if (!s.ok)
|
|
13
|
+
throw new Error(`Failed to fetch ${t}: ${s.statusText}`);
|
|
14
|
+
return s.arrayBuffer();
|
|
13
15
|
}
|
|
14
|
-
async function
|
|
15
|
-
const
|
|
16
|
-
if (!
|
|
16
|
+
async function S(t) {
|
|
17
|
+
const s = /* @__PURE__ */ new Map(), r = await t.file("manifest.json")?.async("string");
|
|
18
|
+
if (!r)
|
|
17
19
|
throw new Error("Manifest file not found in the zip archive");
|
|
18
|
-
const
|
|
19
|
-
for (const [
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
if (!
|
|
20
|
+
const p = JSON.parse(r);
|
|
21
|
+
for (const [o, a] of Object.entries(p.weightSpec))
|
|
22
|
+
s.set(o, { spec: a, data: new Float32Array() });
|
|
23
|
+
const e = await t.file("tokeniser.json")?.async("string");
|
|
24
|
+
if (!e)
|
|
23
25
|
throw new Error("Tokeniser file not found in the zip archive");
|
|
24
|
-
const
|
|
25
|
-
for (const
|
|
26
|
-
if (
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
26
|
+
const i = JSON.parse(e), c = (i.type ?? "char") === "char" ? new h(i.vocab) : new j(i.vocab, i.merges), d = /* @__PURE__ */ new Map();
|
|
27
|
+
for (const o of Object.keys(t.files))
|
|
28
|
+
if (o.endsWith(".bin")) {
|
|
29
|
+
const a = o.replace(".bin", ""), w = await t.file(o).async("arraybuffer"), g = new Float32Array(w), l = s.get(a) || { spec: [], data: new Float32Array() };
|
|
30
|
+
l.data = g, s.set(a, l);
|
|
31
|
+
const n = await F(l);
|
|
32
|
+
d.set(a, n);
|
|
31
33
|
}
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
await
|
|
35
|
-
const m = await
|
|
34
|
+
k();
|
|
35
|
+
const f = new b(p.config);
|
|
36
|
+
await u(f), f.loadWeights(d);
|
|
37
|
+
const m = await t.file("log.json")?.async("string");
|
|
36
38
|
if (m)
|
|
37
39
|
try {
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
} catch (
|
|
41
|
-
throw console.error("Error parsing training log:",
|
|
40
|
+
const o = JSON.parse(m);
|
|
41
|
+
f.log = o;
|
|
42
|
+
} catch (o) {
|
|
43
|
+
throw console.error("Error parsing training log:", o), new Error(`Failed to parse training log: ${o}`);
|
|
42
44
|
}
|
|
43
|
-
return { model:
|
|
45
|
+
return { model: f, tokeniser: c };
|
|
46
|
+
}
|
|
47
|
+
async function R(t) {
|
|
48
|
+
const s = typeof t == "string" ? await O(t) : t, r = await v.loadAsync(s);
|
|
49
|
+
if (r.file("manifest.json"))
|
|
50
|
+
return S(r);
|
|
51
|
+
{
|
|
52
|
+
const p = await r.file("config.json")?.async("string");
|
|
53
|
+
if (!p)
|
|
54
|
+
throw new Error("Config file not found in the zip archive");
|
|
55
|
+
const e = JSON.parse(p), i = {
|
|
56
|
+
vocabSize: e.vocab_size,
|
|
57
|
+
blockSize: e.block_size,
|
|
58
|
+
nLayer: e.num_hidden_layers,
|
|
59
|
+
nHead: e.num_attention_heads,
|
|
60
|
+
nEmbed: e.hidden_size,
|
|
61
|
+
dropout: e.dropout,
|
|
62
|
+
biasInLinear: e.biasInLinear,
|
|
63
|
+
biasInLayerNorm: e.biasInLayerNorm,
|
|
64
|
+
mlpFactor: e.mlpFactor,
|
|
65
|
+
useRope: e.useRope
|
|
66
|
+
}, y = await r.file("tokeniser.json")?.async("string");
|
|
67
|
+
if (!y)
|
|
68
|
+
throw new Error("Tokeniser file not found in the zip archive");
|
|
69
|
+
const c = JSON.parse(y), f = (c.type ?? "char") === "char" ? new h(c.vocab) : new j(c.vocab, c.merges), m = await N(await r.file("model.safetensors").async("arraybuffer")), o = /* @__PURE__ */ new Map();
|
|
70
|
+
for (const [n, E] of Object.entries(m))
|
|
71
|
+
o.set(n, [E]);
|
|
72
|
+
k();
|
|
73
|
+
const a = new b(i);
|
|
74
|
+
await u(a), a.loadWeights(o);
|
|
75
|
+
const w = await r.file("meta.json")?.async("string");
|
|
76
|
+
let g;
|
|
77
|
+
if (w)
|
|
78
|
+
try {
|
|
79
|
+
const n = JSON.parse(w);
|
|
80
|
+
n.name && (g = n.name);
|
|
81
|
+
} catch (n) {
|
|
82
|
+
console.error("Error parsing meta file:", n);
|
|
83
|
+
}
|
|
84
|
+
const l = await r.file("log.json")?.async("string");
|
|
85
|
+
if (l)
|
|
86
|
+
try {
|
|
87
|
+
const n = JSON.parse(l);
|
|
88
|
+
a.log = n;
|
|
89
|
+
} catch (n) {
|
|
90
|
+
throw console.error("Error parsing training log:", n), new Error(`Failed to parse training log: ${n}`);
|
|
91
|
+
}
|
|
92
|
+
return { model: a, tokeniser: f, name: g };
|
|
93
|
+
}
|
|
44
94
|
}
|
|
45
95
|
export {
|
|
46
|
-
|
|
96
|
+
I as VERSION,
|
|
97
|
+
R as loadModel,
|
|
98
|
+
S as loadOldModel
|
|
47
99
|
};
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { MemoryInfo } from '@tensorflow/tfjs-core';
|
|
2
|
+
export interface ExtendedMemoryInfo extends MemoryInfo {
|
|
3
|
+
numBytesInGPUAllocated?: number;
|
|
4
|
+
}
|
|
1
5
|
export default class MemoryProfiler {
|
|
2
6
|
private log;
|
|
3
7
|
private maxMemory;
|
|
@@ -6,6 +10,7 @@ export default class MemoryProfiler {
|
|
|
6
10
|
private peakMemory;
|
|
7
11
|
startMemory(): void;
|
|
8
12
|
getPeakMemory(): number;
|
|
13
|
+
getMaxMemory(): number;
|
|
9
14
|
endMemory(label: string): void;
|
|
10
15
|
printSummary(): void;
|
|
11
16
|
}
|
|
@@ -1,32 +1,35 @@
|
|
|
1
|
-
import { m as
|
|
1
|
+
import { m as a } from "../index-CamYe_M8.js";
|
|
2
2
|
const m = 1024 * 1024;
|
|
3
|
-
class
|
|
3
|
+
class l {
|
|
4
4
|
log = /* @__PURE__ */ new Map();
|
|
5
5
|
maxMemory = 0;
|
|
6
6
|
maxLabel;
|
|
7
7
|
lastMemInfo = [];
|
|
8
8
|
peakMemory = 0;
|
|
9
9
|
startMemory() {
|
|
10
|
-
this.lastMemInfo.push(
|
|
10
|
+
this.lastMemInfo.push(a());
|
|
11
11
|
}
|
|
12
12
|
getPeakMemory() {
|
|
13
13
|
return this.peakMemory;
|
|
14
14
|
}
|
|
15
|
-
|
|
15
|
+
getMaxMemory() {
|
|
16
|
+
return this.maxMemory;
|
|
17
|
+
}
|
|
18
|
+
endMemory(o) {
|
|
16
19
|
if (this.lastMemInfo.length === 0) {
|
|
17
20
|
console.warn("MemoryProfiler: endMemory called without matching startMemory");
|
|
18
21
|
return;
|
|
19
22
|
}
|
|
20
|
-
const
|
|
21
|
-
this.log.set(
|
|
23
|
+
const e = a(), s = this.lastMemInfo.pop(), t = (e.numBytesInGPUAllocated ?? e.numBytes) - (s?.numBytes ?? s?.numBytesInGPUAllocated ?? 0);
|
|
24
|
+
this.log.set(o, Math.max(this.log.get(o) || 0, t)), t > this.maxMemory && (this.maxMemory = t, this.maxLabel = o), this.peakMemory = Math.max(this.peakMemory, e.numBytesInGPUAllocated ?? e.numBytes);
|
|
22
25
|
}
|
|
23
26
|
printSummary() {
|
|
24
27
|
console.log("Memory Usage Summary:");
|
|
25
|
-
for (const [
|
|
26
|
-
console.log(`- ${
|
|
28
|
+
for (const [o, e] of this.log.entries())
|
|
29
|
+
console.log(`- ${o}: ${(e / m).toFixed(2)} MB`);
|
|
27
30
|
this.maxLabel && console.log(`Peak Memory Usage: ${(this.maxMemory / m).toFixed(2)} MB at "${this.maxLabel}"`), console.log(`Overall Peak Memory Usage: ${(this.peakMemory / m).toFixed(2)} MB`);
|
|
28
31
|
}
|
|
29
32
|
}
|
|
30
33
|
export {
|
|
31
|
-
|
|
34
|
+
l as default
|
|
32
35
|
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import "../index-CamYe_M8.js";
|
|
2
|
+
import { t as y } from "../tensor-CDz5x1mP.js";
|
|
3
|
+
function l(t) {
|
|
4
|
+
if (t === "float32") return "F32";
|
|
5
|
+
if (t === "int32") return "I32";
|
|
6
|
+
throw new Error(`Unsupported dtype: ${t}`);
|
|
7
|
+
}
|
|
8
|
+
function h(t) {
|
|
9
|
+
if (t === "F32") return "float32";
|
|
10
|
+
if (t === "I32") return "int32";
|
|
11
|
+
throw new Error(`Unsupported dtype: ${t}`);
|
|
12
|
+
}
|
|
13
|
+
async function _(t) {
|
|
14
|
+
const c = {};
|
|
15
|
+
let a = 0;
|
|
16
|
+
for (const [n, s] of Object.entries(t))
|
|
17
|
+
c[n] = {
|
|
18
|
+
dtype: l(s.dtype),
|
|
19
|
+
shape: s.shape,
|
|
20
|
+
data_offsets: [a, a + s.size * 4]
|
|
21
|
+
}, a += s.size * 4;
|
|
22
|
+
const p = JSON.stringify(c);
|
|
23
|
+
let r = new TextEncoder().encode(p);
|
|
24
|
+
if (r.length % 4 !== 0) {
|
|
25
|
+
const n = 4 - r.length % 4, s = new Uint8Array(r.length + n);
|
|
26
|
+
s.set(r);
|
|
27
|
+
for (let w = r.length; w < s.length; w++)
|
|
28
|
+
s[w] = 32;
|
|
29
|
+
r = s;
|
|
30
|
+
}
|
|
31
|
+
const o = r.length, f = 8 + o + a, e = new ArrayBuffer(f);
|
|
32
|
+
new DataView(e).setUint32(0, o, !0), new Uint8Array(e, 8, o).set(r);
|
|
33
|
+
let d = 8 + o;
|
|
34
|
+
for (const n of Object.values(t)) {
|
|
35
|
+
if (n.size === 0) continue;
|
|
36
|
+
const s = await n.data();
|
|
37
|
+
if (n.dtype === "float32")
|
|
38
|
+
new Float32Array(e, d, n.size).set(s), d += n.size * 4;
|
|
39
|
+
else if (n.dtype === "int32")
|
|
40
|
+
new Int32Array(e, d, n.size).set(s), d += n.size * 4;
|
|
41
|
+
else
|
|
42
|
+
throw new Error(`Unsupported dtype: ${n.dtype}`);
|
|
43
|
+
}
|
|
44
|
+
return e;
|
|
45
|
+
}
|
|
46
|
+
async function U(t) {
|
|
47
|
+
const a = new DataView(t).getUint32(0, !0), p = new Uint8Array(t, 8, a), r = JSON.parse(new TextDecoder().decode(p)), o = {};
|
|
48
|
+
for (const [f, e] of Object.entries(r)) {
|
|
49
|
+
if (e.data_offsets[0] === e.data_offsets[1]) {
|
|
50
|
+
o[f] = y([], e.shape, h(e.dtype));
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (e.dtype === "F32") {
|
|
54
|
+
const i = y(
|
|
55
|
+
new Float32Array(
|
|
56
|
+
t,
|
|
57
|
+
e.data_offsets[0] + 8 + a,
|
|
58
|
+
(e.data_offsets[1] - e.data_offsets[0]) / 4
|
|
59
|
+
),
|
|
60
|
+
e.shape,
|
|
61
|
+
h(e.dtype)
|
|
62
|
+
);
|
|
63
|
+
o[f] = i;
|
|
64
|
+
} else if (e.dtype === "I32") {
|
|
65
|
+
const i = y(
|
|
66
|
+
new Int32Array(
|
|
67
|
+
t,
|
|
68
|
+
e.data_offsets[0] + 8 + a,
|
|
69
|
+
(e.data_offsets[1] - e.data_offsets[0]) / 4
|
|
70
|
+
),
|
|
71
|
+
e.shape,
|
|
72
|
+
h(e.dtype)
|
|
73
|
+
);
|
|
74
|
+
o[f] = i;
|
|
75
|
+
} else
|
|
76
|
+
throw new Error(`Unsupported dtype: ${e.dtype}`);
|
|
77
|
+
}
|
|
78
|
+
return o;
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
U as load_safetensors,
|
|
82
|
+
_ as save_safetensors
|
|
83
|
+
};
|
package/dist/utilities/save.js
CHANGED
|
@@ -1,43 +1,61 @@
|
|
|
1
|
-
import { j as
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
async function
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
import { j as p } from "../jszip.min-CjP2V1VV.js";
|
|
2
|
+
import b from "../tokeniser/CharTokeniser.js";
|
|
3
|
+
import { save_safetensors as l } from "./safetensors.js";
|
|
4
|
+
import { VERSION as y } from "./load.js";
|
|
5
|
+
async function N(e, a, n) {
|
|
6
|
+
const f = n?.includeLog ?? !0, s = /* @__PURE__ */ new Map();
|
|
7
|
+
e.saveWeights(s);
|
|
8
|
+
const i = new p(), r = {};
|
|
9
|
+
s.forEach((t, o) => {
|
|
10
|
+
t.length === 1 && (r[o] = t[0]);
|
|
11
|
+
});
|
|
12
|
+
const g = await l(r);
|
|
13
|
+
i.file("model.safetensors", g, { binary: !0 });
|
|
14
|
+
const c = {
|
|
15
|
+
model_type: "GenAI_NanoGPT_1",
|
|
16
|
+
vocab_size: a.getVocab().length,
|
|
17
|
+
hidden_size: e.config.gpt.nEmbed,
|
|
18
|
+
num_hidden_layers: e.config.gpt.nLayer,
|
|
19
|
+
num_attention_heads: e.config.gpt.nHead,
|
|
20
|
+
block_size: e.config.gpt.blockSize,
|
|
21
|
+
dropout: e.config.gpt.dropout,
|
|
22
|
+
biasInLinear: e.config.gpt.biasInLinear,
|
|
23
|
+
biasInLayerNorm: e.config.gpt.biasInLayerNorm,
|
|
24
|
+
mlpFactor: e.config.gpt.mlpFactor,
|
|
25
|
+
useRope: e.config.gpt.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: n?.metadata,
|
|
36
|
+
name: n?.name
|
|
37
|
+
},
|
|
38
|
+
void 0,
|
|
39
|
+
4
|
|
40
|
+
),
|
|
23
41
|
{
|
|
24
42
|
binary: !1
|
|
25
43
|
}
|
|
26
|
-
),
|
|
44
|
+
), i.file(
|
|
27
45
|
"tokeniser.json",
|
|
28
46
|
JSON.stringify({
|
|
29
|
-
type: a instanceof
|
|
47
|
+
type: a instanceof b ? "char" : "bpe",
|
|
30
48
|
vocab: a.getVocab(),
|
|
31
49
|
merges: await a.getMerges()
|
|
32
50
|
}),
|
|
33
51
|
{
|
|
34
52
|
binary: !1
|
|
35
53
|
}
|
|
36
|
-
),
|
|
37
|
-
for (const [
|
|
38
|
-
|
|
39
|
-
return
|
|
54
|
+
), f && i.file("log.json", JSON.stringify(e.log), { binary: !1 }), n?.files)
|
|
55
|
+
for (const [t, o] of Object.entries(n.files))
|
|
56
|
+
i.file(t, JSON.stringify(o), { binary: !1 });
|
|
57
|
+
return i.generateAsync({ type: "blob" });
|
|
40
58
|
}
|
|
41
59
|
export {
|
|
42
|
-
|
|
60
|
+
N as saveModel
|
|
43
61
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import "../index-
|
|
2
|
-
import { t as p } from "../tensor-
|
|
1
|
+
import "../index-CamYe_M8.js";
|
|
2
|
+
import { t as p } from "../tensor-CDz5x1mP.js";
|
|
3
3
|
function h(n) {
|
|
4
4
|
const e = n.reduce((s, o) => s + o.length, 0), a = new Float32Array(e);
|
|
5
5
|
let t = 0;
|