@genai-fi/nanogpt 0.2.12 → 0.3.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.js +30 -25
- package/dist/NanoGPTModel.d.ts +13 -14
- package/dist/NanoGPTModel.js +167 -85
- package/dist/TeachableLLM.d.ts +3 -5
- package/dist/TeachableLLM.js +47 -35
- package/dist/Trainer.js +8 -8
- package/dist/concat-BIZS_td9.js +33 -0
- package/dist/data/parquet.js +1 -1
- package/dist/exports_layers-7idKoYqh.js +25 -0
- package/dist/{sum-D7fu15XL.js → gather-BPGW8RsB.js} +6 -8
- package/dist/index-C4L8Cm77.js +349 -0
- package/dist/{index-YPKosni4.js → index-pWA4_lUh.js} +1020 -782
- package/dist/layers/CausalSelfAttention.d.ts +11 -11
- package/dist/layers/CausalSelfAttention.js +71 -63
- package/dist/layers/MLP.d.ts +6 -7
- package/dist/layers/MLP.js +18 -16
- package/dist/layers/RMSNorm.d.ts +6 -7
- package/dist/layers/RMSNorm.js +15 -13
- package/dist/layers/RoPECache.d.ts +4 -5
- package/dist/layers/RoPECache.js +36 -12
- package/dist/layers/TiedEmbedding.d.ts +7 -8
- package/dist/layers/TiedEmbedding.js +16 -418
- package/dist/layers/TransformerBlock.d.ts +8 -9
- package/dist/layers/TransformerBlock.js +12 -12
- package/dist/main.d.ts +1 -0
- package/dist/main.js +35 -21
- package/dist/{mat_mul-Bu7bhLms.js → mat_mul-D7_a4KJn.js} +5 -5
- package/dist/moments-DfcpfwKi.js +132 -0
- package/dist/ones-Cog-G2ag.js +29 -0
- package/dist/ops/appendCache.d.ts +2 -0
- package/dist/ops/appendCache.js +9 -0
- package/dist/ops/attentionMask.d.ts +1 -1
- package/dist/ops/attentionMask.js +7 -85
- package/dist/ops/cpu/appendCache.d.ts +2 -0
- package/dist/ops/cpu/appendCache.js +28 -0
- package/dist/ops/cpu/attentionMask.js +18 -0
- package/dist/ops/cpu/gatherSub.d.ts +1 -0
- package/dist/ops/cpu/gatherSub.js +34 -0
- package/dist/ops/cpu/qkv.d.ts +5 -0
- package/dist/ops/cpu/qkv.js +38 -0
- package/dist/ops/cpu/rope.d.ts +6 -0
- package/dist/ops/cpu/rope.js +38 -0
- package/dist/ops/cpu/scatterSub.d.ts +1 -0
- package/dist/ops/cpu/scatterSub.js +70 -0
- package/dist/ops/gatherSub.d.ts +1 -1
- package/dist/ops/gatherSub.js +6 -63
- package/dist/ops/grads/attentionMask.d.ts +1 -0
- package/dist/ops/grads/attentionMask.js +21 -0
- package/dist/ops/grads/qkv.d.ts +1 -0
- package/dist/ops/grads/qkv.js +20 -0
- package/dist/ops/grads/rope.d.ts +1 -0
- package/dist/ops/grads/rope.js +14 -0
- package/dist/ops/node/sparseCrossEntropy.js +1 -1
- package/dist/ops/qkv.d.ts +1 -6
- package/dist/ops/qkv.js +7 -124
- package/dist/ops/rope.d.ts +0 -5
- package/dist/ops/rope.js +7 -151
- package/dist/ops/scatterSub.d.ts +1 -1
- package/dist/ops/scatterSub.js +6 -147
- package/dist/ops/webgl/appendCache.d.ts +1 -0
- package/dist/ops/webgl/appendCache.js +43 -0
- package/dist/ops/webgl/attentionMask.d.ts +1 -0
- package/dist/ops/webgl/attentionMask.js +43 -0
- package/dist/ops/webgl/gatherSub.d.ts +1 -0
- package/dist/ops/webgl/gatherSub.js +27 -0
- package/dist/ops/webgl/qkv.d.ts +1 -0
- package/dist/ops/webgl/qkv.js +46 -0
- package/dist/ops/webgl/rope.d.ts +1 -0
- package/dist/ops/webgl/rope.js +56 -0
- package/dist/ops/webgl/scatterSub.d.ts +1 -0
- package/dist/ops/webgl/scatterSub.js +27 -0
- package/dist/{parquet-BRl5lE_I.js → parquet-C0Tlmv9c.js} +3045 -3048
- package/dist/random_width-PbCt7RXv.js +15489 -0
- package/dist/range-CcDl05lo.js +26 -0
- package/dist/{reshape-DmnmKT6r.js → reshape-C8CR_Bad.js} +3 -3
- package/dist/sin-BJIrfnj7.js +47 -0
- package/dist/softmax-Be_lsqUc.js +105 -0
- package/dist/{complex-CJ-qCcLB.js → split-DZbvruEP.js} +6 -8
- package/dist/stack-BMm-efee.js +27 -0
- package/dist/sum-C7Mgy9Bw.js +104 -0
- package/dist/tensor-DJVbYhh1.js +24 -0
- package/dist/tensor2d-ZuQSh2D-.js +30 -0
- package/dist/tokeniser/bpe.d.ts +17 -6
- package/dist/tokeniser/bpe.js +88 -60
- package/dist/training/AdamExt.js +1 -1
- package/dist/training/DatasetBuilder.d.ts +6 -6
- package/dist/training/DatasetBuilder.js +1262 -17
- package/dist/training/Evaluator.d.ts +3 -2
- package/dist/training/FullTrainer.d.ts +9 -8
- package/dist/training/FullTrainer.js +26 -25
- package/dist/training/LayerTrainer.d.ts +9 -8
- package/dist/training/LayerTrainer.js +34 -33
- package/dist/training/Trainer.d.ts +22 -21
- package/dist/training/Trainer.js +21 -18
- package/dist/training/sparseCrossEntropy.js +22 -166
- package/dist/utilities/dummy.js +10 -8
- package/dist/utilities/generate.js +14 -11
- package/dist/utilities/load.d.ts +1 -2
- package/dist/utilities/load.js +37 -35
- package/dist/utilities/profile.js +1 -1
- package/dist/utilities/save.js +14 -9
- package/dist/utilities/tokenParse.d.ts +1 -1
- package/dist/utilities/tokenParse.js +7 -61
- package/dist/utilities/weights.d.ts +3 -3
- package/dist/utilities/weights.js +21 -19
- package/dist/variable-Dl_ub3pk.js +23 -0
- package/dist/{stack-BtKpB0Ry.js → zeros-CCy9C3uU.js} +18 -16
- package/package.json +2 -1
- package/dist/assets/worker-BYeSPNkq.js +0 -1
- package/dist/tokeniser/NodeTokeniser.d.ts +0 -20
- package/dist/tokeniser/NodeTokeniser.js +0 -46
- package/dist/tokeniser/WebTokeniser.d.ts +0 -18
- package/dist/tokeniser/WebTokeniser.js +0 -96
- package/dist/tokeniser/worker.js +0 -53
- /package/dist/{tokeniser/worker.d.ts → ops/cpu/attentionMask.d.ts} +0 -0
package/dist/utilities/dummy.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import "../index-pWA4_lUh.js";
|
|
2
|
+
import { z as n } from "../zeros-CCy9C3uU.js";
|
|
3
|
+
async function c(s) {
|
|
4
|
+
const o = n([1, s.config.blockSize], "int32"), { logits: t, loss: i } = s.forward(o, void 0, !1);
|
|
5
|
+
await t.data(), t.dispose(), i && i.dispose(), o.dispose();
|
|
4
6
|
}
|
|
5
|
-
function
|
|
6
|
-
const o =
|
|
7
|
-
|
|
7
|
+
function f(s) {
|
|
8
|
+
const o = n([1, s.config.blockSize], "int32"), { logits: t, loss: i } = s.forward(o, void 0, !1);
|
|
9
|
+
t.dispose(), i && i.dispose(), o.dispose();
|
|
8
10
|
}
|
|
9
11
|
export {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
f as dummyPass,
|
|
13
|
+
c as dummyPassAsync
|
|
12
14
|
};
|
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
import { t as y } from "../index-pWA4_lUh.js";
|
|
2
|
+
import { t as x } from "../tensor2d-ZuQSh2D-.js";
|
|
3
|
+
import { c as g } from "../concat-BIZS_td9.js";
|
|
4
|
+
async function A(o, r, a, c, T) {
|
|
2
5
|
if (c <= 0)
|
|
3
6
|
throw new Error("Length must be a positive integer");
|
|
4
7
|
if (a.length === 0)
|
|
5
8
|
throw new Error("Prompt cannot be an empty string");
|
|
6
|
-
const
|
|
7
|
-
let e =
|
|
9
|
+
const d = await o.tokenise([a], !0), s = r.config.useRope ? new Array(r.config.nLayer).fill(void 0) : void 0, p = y(() => {
|
|
10
|
+
let e = x(d, [1, d[0].length], "int32"), t = e;
|
|
8
11
|
for (let f = 0; f < c; f++) {
|
|
9
|
-
const { output:
|
|
10
|
-
|
|
12
|
+
const { output: n } = r.generate(e, s, T), l = e, m = t;
|
|
13
|
+
t = g([t, n], 1), e = s ? n : g([e, n], 1), l.dispose(), m.dispose(), s || n.dispose();
|
|
11
14
|
}
|
|
12
|
-
return
|
|
13
|
-
}),
|
|
14
|
-
|
|
15
|
-
const i =
|
|
16
|
-
return
|
|
15
|
+
return t;
|
|
16
|
+
}), w = await p.array();
|
|
17
|
+
p.dispose();
|
|
18
|
+
const i = w[0], u = i.indexOf(o.eosToken);
|
|
19
|
+
return u !== -1 && i.splice(u), await o.decode(i);
|
|
17
20
|
}
|
|
18
21
|
export {
|
|
19
|
-
|
|
22
|
+
A as generateText
|
|
20
23
|
};
|
package/dist/utilities/load.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { default as TF } from '@tensorflow/tfjs';
|
|
2
1
|
import { default as NanoGPT } from '../NanoGPTModel';
|
|
3
2
|
import { ITokeniser } from '../tokeniser/type';
|
|
4
|
-
export declare function loadModel(
|
|
3
|
+
export declare function loadModel(data: Blob | Buffer | string): Promise<{
|
|
5
4
|
model: NanoGPT;
|
|
6
5
|
tokeniser: ITokeniser;
|
|
7
6
|
}>;
|
package/dist/utilities/load.js
CHANGED
|
@@ -1,45 +1,47 @@
|
|
|
1
|
-
import { j as
|
|
2
|
-
import { importWeights as
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import { dummyPassAsync as
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { j as k } from "../jszip.min-CjP2V1VV.js";
|
|
2
|
+
import { importWeights as b } from "./weights.js";
|
|
3
|
+
import u from "../tokeniser/CharTokeniser.js";
|
|
4
|
+
import F from "../NanoGPTModel.js";
|
|
5
|
+
import { dummyPassAsync as j } from "./dummy.js";
|
|
6
|
+
import { d as T } from "../index-pWA4_lUh.js";
|
|
7
|
+
import E from "../tokeniser/bpe.js";
|
|
8
|
+
async function A(t) {
|
|
9
|
+
const o = await fetch(t);
|
|
10
|
+
if (!o.ok)
|
|
11
|
+
throw new Error(`Failed to fetch ${t}: ${o.statusText}`);
|
|
12
|
+
return o.arrayBuffer();
|
|
11
13
|
}
|
|
12
|
-
async function
|
|
13
|
-
const
|
|
14
|
-
if (!
|
|
14
|
+
async function P(t) {
|
|
15
|
+
const o = typeof t == "string" ? await A(t) : t, n = await k.loadAsync(o), i = /* @__PURE__ */ new Map(), f = await n.file("manifest.json")?.async("string");
|
|
16
|
+
if (!f)
|
|
15
17
|
throw new Error("Manifest file not found in the zip archive");
|
|
16
|
-
const
|
|
17
|
-
for (const [
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
if (!
|
|
18
|
+
const l = JSON.parse(f);
|
|
19
|
+
for (const [e, r] of Object.entries(l.weightSpec))
|
|
20
|
+
i.set(e, { spec: r, data: new Float32Array() });
|
|
21
|
+
const p = await n.file("tokeniser.json")?.async("string");
|
|
22
|
+
if (!p)
|
|
21
23
|
throw new Error("Tokeniser file not found in the zip archive");
|
|
22
|
-
const
|
|
23
|
-
for (const
|
|
24
|
-
if (
|
|
25
|
-
const r =
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
|
|
24
|
+
const s = JSON.parse(p), y = (s.type ?? "char") === "char" ? new u(s.vocab) : new E(s.vocab, s.merges), w = /* @__PURE__ */ new Map();
|
|
25
|
+
for (const e of Object.keys(n.files))
|
|
26
|
+
if (e.endsWith(".bin")) {
|
|
27
|
+
const r = e.replace(".bin", ""), g = await n.file(e).async("arraybuffer"), h = new Float32Array(g), c = i.get(r) || { spec: [], data: new Float32Array() };
|
|
28
|
+
c.data = h, i.set(r, c);
|
|
29
|
+
const d = await b(c);
|
|
30
|
+
w.set(r, d);
|
|
29
31
|
}
|
|
30
|
-
|
|
31
|
-
const a = new
|
|
32
|
-
await
|
|
33
|
-
const
|
|
34
|
-
if (
|
|
32
|
+
T();
|
|
33
|
+
const a = new F(l.config);
|
|
34
|
+
await j(a), a.loadWeights(w);
|
|
35
|
+
const m = await n.file("log.json")?.async("string");
|
|
36
|
+
if (m)
|
|
35
37
|
try {
|
|
36
|
-
const
|
|
37
|
-
a.log =
|
|
38
|
-
} catch (
|
|
39
|
-
throw console.error("Error parsing training log:",
|
|
38
|
+
const e = JSON.parse(m);
|
|
39
|
+
a.log = e;
|
|
40
|
+
} catch (e) {
|
|
41
|
+
throw console.error("Error parsing training log:", e), new Error(`Failed to parse training log: ${e}`);
|
|
40
42
|
}
|
|
41
43
|
return { model: a, tokeniser: y };
|
|
42
44
|
}
|
|
43
45
|
export {
|
|
44
|
-
|
|
46
|
+
P as loadModel
|
|
45
47
|
};
|
package/dist/utilities/save.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { j as g } from "../jszip.min-CjP2V1VV.js";
|
|
2
2
|
import { exportWeights as l } from "./weights.js";
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
import b from "../tokeniser/CharTokeniser.js";
|
|
4
|
+
const y = "1.0.0";
|
|
5
|
+
async function h(t, a, i) {
|
|
5
6
|
const o = i?.includeLog ?? !0, c = t.saveWeights(), e = new g(), f = {};
|
|
6
|
-
for (const [n,
|
|
7
|
-
const r = await l(
|
|
7
|
+
for (const [n, s] of c) {
|
|
8
|
+
const r = await l(s);
|
|
8
9
|
f[n] = r.spec, e.file(`${n}.bin`, r.data.buffer, { binary: !0 });
|
|
9
10
|
}
|
|
10
11
|
if (e.file(
|
|
@@ -12,7 +13,7 @@ async function p(t, s, i) {
|
|
|
12
13
|
JSON.stringify({
|
|
13
14
|
weightSpec: f,
|
|
14
15
|
config: t.config,
|
|
15
|
-
version:
|
|
16
|
+
version: y,
|
|
16
17
|
application: "@genai-fi/nanogpt",
|
|
17
18
|
meta: i?.metadata,
|
|
18
19
|
name: i?.name
|
|
@@ -22,15 +23,19 @@ async function p(t, s, i) {
|
|
|
22
23
|
}
|
|
23
24
|
), e.file(
|
|
24
25
|
"tokeniser.json",
|
|
25
|
-
JSON.stringify({
|
|
26
|
+
JSON.stringify({
|
|
27
|
+
type: a instanceof b ? "char" : "bpe",
|
|
28
|
+
vocab: a.getVocab(),
|
|
29
|
+
merges: await a.getMerges()
|
|
30
|
+
}),
|
|
26
31
|
{
|
|
27
32
|
binary: !1
|
|
28
33
|
}
|
|
29
34
|
), o && e.file("log.json", JSON.stringify(t.log), { binary: !1 }), i?.files)
|
|
30
|
-
for (const [n,
|
|
31
|
-
e.file(n, JSON.stringify(
|
|
35
|
+
for (const [n, s] of Object.entries(i.files))
|
|
36
|
+
e.file(n, JSON.stringify(s), { binary: !1 });
|
|
32
37
|
return e.generateAsync({ type: "blob" });
|
|
33
38
|
}
|
|
34
39
|
export {
|
|
35
|
-
|
|
40
|
+
h as saveModel
|
|
36
41
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export default function parseTokens(text: string
|
|
1
|
+
export default function parseTokens(text: string): string[];
|
|
@@ -1,66 +1,12 @@
|
|
|
1
|
-
function o
|
|
2
|
-
|
|
3
|
-
}
|
|
4
|
-
function l(a, c) {
|
|
5
|
-
const r = c ? a : a.toLocaleLowerCase(), s = [];
|
|
1
|
+
function h(o) {
|
|
2
|
+
const r = Array.from(o), t = [], l = new RegExp("(\\p{P}|\\p{S}|\\s)", "gu");
|
|
6
3
|
let e = "";
|
|
7
|
-
for (let
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
case "0":
|
|
11
|
-
case "1":
|
|
12
|
-
case "2":
|
|
13
|
-
case "3":
|
|
14
|
-
case "4":
|
|
15
|
-
case "5":
|
|
16
|
-
case "6":
|
|
17
|
-
case "7":
|
|
18
|
-
case "8":
|
|
19
|
-
case "9":
|
|
20
|
-
case ":":
|
|
21
|
-
case ";":
|
|
22
|
-
case ",":
|
|
23
|
-
case ".":
|
|
24
|
-
case "?":
|
|
25
|
-
case "!":
|
|
26
|
-
case '"':
|
|
27
|
-
case "'":
|
|
28
|
-
case "`":
|
|
29
|
-
case "(":
|
|
30
|
-
case ")":
|
|
31
|
-
case "[":
|
|
32
|
-
case "]":
|
|
33
|
-
case "{":
|
|
34
|
-
case "}":
|
|
35
|
-
case "-":
|
|
36
|
-
case "_":
|
|
37
|
-
case "/":
|
|
38
|
-
case "\\":
|
|
39
|
-
case "%":
|
|
40
|
-
case "<":
|
|
41
|
-
case ">":
|
|
42
|
-
case "=":
|
|
43
|
-
case "+":
|
|
44
|
-
case "*":
|
|
45
|
-
case "&":
|
|
46
|
-
case "^":
|
|
47
|
-
case "|":
|
|
48
|
-
case "~":
|
|
49
|
-
case "@":
|
|
50
|
-
case "#":
|
|
51
|
-
case "$":
|
|
52
|
-
c ? s.push(e) : o(e, s), s.push(n), e = "";
|
|
53
|
-
break;
|
|
54
|
-
case " ":
|
|
55
|
-
c ? s.push(e) : o(e, s), e = n;
|
|
56
|
-
break;
|
|
57
|
-
default:
|
|
58
|
-
e += n;
|
|
59
|
-
break;
|
|
60
|
-
}
|
|
4
|
+
for (let n = 0; n < r.length; n++) {
|
|
5
|
+
const s = r[n];
|
|
6
|
+
s === " " ? (t.push(e), e = s) : s.match(l) ? (t.push(e), t.push(s), e = "") : e += s;
|
|
61
7
|
}
|
|
62
|
-
return e.length > 0 &&
|
|
8
|
+
return e.length > 0 && t.push(e), t.filter((n) => n.length > 0);
|
|
63
9
|
}
|
|
64
10
|
export {
|
|
65
|
-
|
|
11
|
+
h as default
|
|
66
12
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Tensor } from '@tensorflow/tfjs-core';
|
|
2
2
|
export interface ITensorSpec {
|
|
3
3
|
shape: number[];
|
|
4
4
|
min?: number;
|
|
@@ -8,5 +8,5 @@ export interface IWeightManifest {
|
|
|
8
8
|
spec: ITensorSpec[];
|
|
9
9
|
data: Float32Array;
|
|
10
10
|
}
|
|
11
|
-
export declare function exportWeights(weights:
|
|
12
|
-
export declare function importWeights(manifest: IWeightManifest
|
|
11
|
+
export declare function exportWeights(weights: Tensor[]): Promise<IWeightManifest>;
|
|
12
|
+
export declare function importWeights(manifest: IWeightManifest): Promise<Tensor[]>;
|
|
@@ -1,22 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import "../index-pWA4_lUh.js";
|
|
2
|
+
import { t as p } from "../tensor-DJVbYhh1.js";
|
|
3
|
+
function h(n) {
|
|
4
|
+
const e = n.reduce((s, o) => s + o.length, 0), a = new Float32Array(e);
|
|
3
5
|
let t = 0;
|
|
4
|
-
for (const s of
|
|
6
|
+
for (const s of n)
|
|
5
7
|
a.set(s, t), t += s.length;
|
|
6
8
|
return a;
|
|
7
9
|
}
|
|
8
|
-
async function
|
|
9
|
-
const
|
|
10
|
+
async function d(n) {
|
|
11
|
+
const e = {
|
|
10
12
|
spec: [],
|
|
11
13
|
data: new Float32Array()
|
|
12
14
|
}, a = [];
|
|
13
|
-
for (const t of
|
|
15
|
+
for (const t of n) {
|
|
14
16
|
if (!t || !Array.isArray(t.shape) || t.shape.length === 0) {
|
|
15
17
|
console.warn("Skipping weight with invalid shape:", t);
|
|
16
18
|
continue;
|
|
17
19
|
}
|
|
18
20
|
const s = t.min(), o = t.max(), c = (await s.data())[0], r = (await o.data())[0] - c;
|
|
19
|
-
|
|
21
|
+
e.spec.push({
|
|
20
22
|
shape: t.shape,
|
|
21
23
|
min: c,
|
|
22
24
|
scale: r
|
|
@@ -24,20 +26,20 @@ async function f(e) {
|
|
|
24
26
|
const i = await t.data();
|
|
25
27
|
a.push(i);
|
|
26
28
|
}
|
|
27
|
-
return
|
|
29
|
+
return e.data = h(a), e;
|
|
28
30
|
}
|
|
29
|
-
async function
|
|
30
|
-
const
|
|
31
|
-
let
|
|
32
|
-
for (const
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
31
|
+
async function u(n) {
|
|
32
|
+
const e = [];
|
|
33
|
+
let a = 0;
|
|
34
|
+
for (const t of n.spec) {
|
|
35
|
+
const s = t.shape.reduce((r, i) => r * i, 1), o = n.data.slice(a, a + s);
|
|
36
|
+
a += s;
|
|
37
|
+
const c = p(o, t.shape, "float32");
|
|
38
|
+
e.push(c);
|
|
37
39
|
}
|
|
38
|
-
return
|
|
40
|
+
return e;
|
|
39
41
|
}
|
|
40
42
|
export {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
+
d as exportWeights,
|
|
44
|
+
u as importWeights
|
|
43
45
|
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { E as i } from "./index-pWA4_lUh.js";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2018 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 m(r, a = !0, e, t) {
|
|
19
|
+
return i.makeVariable(r, a, e, t);
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
m as v
|
|
23
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { o as m, h as r, M as l, E as c, N as i, k as p, O as u, n as f } from "./index-pWA4_lUh.js";
|
|
2
2
|
/**
|
|
3
3
|
* @license
|
|
4
|
-
* Copyright
|
|
4
|
+
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
5
5
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
6
|
* you may not use this file except in compliance with the License.
|
|
7
7
|
* You may obtain a copy of the License at
|
|
@@ -15,15 +15,16 @@ import { E as e, j as c, o as f, k as u, l as a, P as i } from "./index-YPKosni4
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
* =============================================================================
|
|
17
17
|
*/
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
return
|
|
18
|
+
function x(a, e) {
|
|
19
|
+
const o = r(a, "real", "complex"), s = r(e, "imag", "complex");
|
|
20
|
+
l(o.shape, s.shape, `real and imag shapes, ${o.shape} and ${s.shape}, must match in call to tf.complex().`);
|
|
21
|
+
const n = { real: o, imag: s };
|
|
22
|
+
return c.runKernel(i, n);
|
|
23
23
|
}
|
|
24
|
+
const g = /* @__PURE__ */ m({ complex_: x });
|
|
24
25
|
/**
|
|
25
26
|
* @license
|
|
26
|
-
* Copyright
|
|
27
|
+
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
27
28
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
28
29
|
* you may not use this file except in compliance with the License.
|
|
29
30
|
* You may obtain a copy of the License at
|
|
@@ -37,14 +38,15 @@ function g(n, s, t = 1, r = "float32") {
|
|
|
37
38
|
* limitations under the License.
|
|
38
39
|
* =============================================================================
|
|
39
40
|
*/
|
|
40
|
-
function
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
function t(a, e = "float32") {
|
|
42
|
+
if (p(a), e === "complex64") {
|
|
43
|
+
const s = t(a, "float32"), n = t(a, "float32");
|
|
44
|
+
return g(s, n);
|
|
45
|
+
}
|
|
46
|
+
const o = u(f(a), e);
|
|
47
|
+
return c.makeTensor(o, a, e);
|
|
45
48
|
}
|
|
46
|
-
const h = /* @__PURE__ */ f({ stack_: k });
|
|
47
49
|
export {
|
|
48
|
-
g as
|
|
49
|
-
|
|
50
|
+
g as c,
|
|
51
|
+
t as z
|
|
50
52
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@genai-fi/nanogpt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/main.js",
|
|
6
6
|
"types": "dist/main.d.ts",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"preview": "vite preview",
|
|
22
22
|
"lint": "eslint",
|
|
23
23
|
"test": "vitest",
|
|
24
|
+
"test:gl": "playwright test",
|
|
24
25
|
"ci:test": "vitest --coverage --reporter=junit --outputFile=junit.xml",
|
|
25
26
|
"coverage": "vitest run --coverage",
|
|
26
27
|
"train": "tsx scripts/train.ts",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
(function(){"use strict";function u(n,t){const e=n,s=[];let a="";for(let o=0;o<e.length;o++){const c=e[o];switch(c){case"0":case"1":case"2":case"3":case"4":case"5":case"6":case"7":case"8":case"9":case":":case";":case",":case".":case"?":case"!":case'"':case"'":case"`":case"(":case")":case"[":case"]":case"{":case"}":case"-":case"_":case"/":case"\\":case"%":case"<":case">":case"=":case"+":case"*":case"&":case"^":case"|":case"~":case"@":case"#":case"$":s.push(a),s.push(c),a="";break;case" ":s.push(a),a=c;break;default:a+=c;break}}return a.length>0&&s.push(a),s}function k(n){const t=new Map;for(let e=0;e<n.length;e++){const s=n[e];for(let a=0;a<s.length-1;a++){const o=`${s[a]}${s[a+1]}`,c=t.get(o)||{a:s[a],b:s[a+1],count:0,instances:new Set};c.count+=1,c.instances.add(e),t.set(o,c)}}return{pairs:t,tokens:n}}function p(n,t,e,s,a){const o=`${t}${e}`;if(n.pairs.has(o)){const c=n.pairs.get(o);c.count+=a,c.instances.add(s)}else n.pairs.set(o,{a:t,b:e,count:a,instances:new Set([s])})}function b(n){let t=null,e=0;for(const s of n.pairs.values())s.count>e&&(e=s.count,t=s);return t}function m(n,t){return n.map(e=>{const s=[];for(let a=0;a<e.length;a++)a<e.length-1&&e[a]===t[0]&&e[a+1]===t[1]?(s.push(t[0]+t[1]),a++):s.push(e[a]);return s})}function y(n,t){t.instances.forEach(e=>{const s=n.tokens[e],a=[];for(let o=0;o<s.length;o++)if(o<s.length-1&&s[o]===t.a&&s[o+1]===t.b){const c=t.a+t.b;a.push(c),o>0&&(p(n,s[o-1],t.a,e,-1),p(n,s[o-1],c,e,1)),o++,o<s.length-1&&(p(n,t.b,s[o+1],e,-1),p(n,c,s[o+1],e,1))}else a.push(s[o]);n.tokens[e]=a}),n.pairs.delete(`${t.a}${t.b}`)}class l{vocab=new Set;vocabIndex=new Map;merges=[];pretokenMap=new Map;constructor(t,e){t&&t.forEach((s,a)=>{this.vocab.add(s),this.vocabIndex.set(s,a)}),e&&(this.merges=e)}train(t,e,s){const a=t.map(i=>u(i)).flat(1),o=new Set(a);this.vocab=new Set,this.pretokenMap.clear(),this.merges=[],this.vocab.add("<eos>");const c=Array.from(o),d=c.map(i=>i.split("").map(h=>(this.vocab.add(h),h))),f=k(d);for(;this.vocab.size<e&&this.merges.length<e;){const i=b(f);if(!i)break;this.merges.push([i.a,i.b]),this.vocab.add(i.a+i.b),y(f,i),s&&this.vocab.size%100===0&&s(this.vocab.size/e,this.vocab.size)}c.forEach((i,g)=>{const h=d[g];this.pretokenMap.set(i,h)}),this.vocabIndex.clear();let M=0;for(const i of this.vocab.keys())this.vocabIndex.set(i,M++)}getVocab(){return Array.from(this.vocab)}getMerges(){return this.merges}tokeniseWord(t){let e=t.split("");return this.merges.forEach(s=>{e=m([e],s)[0]}),this.pretokenMap.set(t,e),e}tokeniseStrings(t){return t.map(e=>u(e).map(o=>this.pretokenMap.has(o)?this.pretokenMap.get(o):this.tokeniseWord(o)).flat(1))}tokenise(t,e){const s=this.tokeniseStrings(t);return e?s.map(a=>a.map(o=>this.vocabIndex.get(o)??-1)):s}}let r=new l;onmessage=async n=>{if(n.data.type==="tokenise")if(n.data.numeric){const t=r.tokenise(n.data.text,!0),e={type:"tokeniseResponse",id:n.data.id,tokens:t,numeric:!0};postMessage(e)}else{const t=r.tokenise(n.data.text),e={type:"tokeniseResponse",id:n.data.id,tokens:t,numeric:!1};postMessage(e)}else if(n.data.type==="detokenise"){const t=r.getVocab(),e=n.data.tokens.map(a=>a.map(o=>t[o]).join("")),s={type:"detokeniseResponse",id:n.data.id,text:e};postMessage(s)}else if(n.data.type==="train"){r=new l,r.train(n.data.text,n.data.vocabSize??100,(e,s)=>{const a={type:"trainStatus",id:n.data.id,progress:e,vocabSize:s};postMessage(a)});const t={type:"trainResponse",id:n.data.id,vocabSize:r.getVocab().length};postMessage(t)}else if(n.data.type==="tokens"){const t=r.getVocab(),e={type:"tokensResponse",id:n.data.id,tokens:t};postMessage(e)}}})();
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { default as EE } from 'eventemitter3';
|
|
2
|
-
import { ITokeniser } from './type';
|
|
3
|
-
export default class NodeTokeniser extends EE<'trainStatus'> implements ITokeniser {
|
|
4
|
-
vocabSize: number;
|
|
5
|
-
eosToken: number;
|
|
6
|
-
private bpe;
|
|
7
|
-
constructor(vocabSize: number);
|
|
8
|
-
constructor(vocab: string[], merges: [string, string][]);
|
|
9
|
-
get trained(): boolean;
|
|
10
|
-
destroy(): void;
|
|
11
|
-
train(text: string[]): Promise<number>;
|
|
12
|
-
tokenise(text: string[], numeric: true): Promise<number[][]>;
|
|
13
|
-
tokenise(text: string[]): Promise<string[][]>;
|
|
14
|
-
detokenise(tokens: number[][]): Promise<string[]>;
|
|
15
|
-
encode(text: string): Promise<number[]>;
|
|
16
|
-
decode(tokens: number[]): Promise<string>;
|
|
17
|
-
getVocab(): string[];
|
|
18
|
-
getMerges(): Promise<[string, string][]>;
|
|
19
|
-
createTrainingData(text: string[], windowSize?: number): Promise<[number[], number[]]>;
|
|
20
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { E as a } from "../index-Dwqa6Zy2.js";
|
|
2
|
-
import o from "./bpe.js";
|
|
3
|
-
class p extends a {
|
|
4
|
-
vocabSize = 0;
|
|
5
|
-
eosToken = 0;
|
|
6
|
-
bpe = new o();
|
|
7
|
-
constructor(e, t) {
|
|
8
|
-
super(), Array.isArray(e) ? (this.bpe = new o(e, t), this.vocabSize = e.length) : this.vocabSize = e;
|
|
9
|
-
}
|
|
10
|
-
get trained() {
|
|
11
|
-
return this.vocabSize > 0;
|
|
12
|
-
}
|
|
13
|
-
destroy() {
|
|
14
|
-
}
|
|
15
|
-
async train(e) {
|
|
16
|
-
return this.bpe.train(e, this.vocabSize), this.vocabSize = this.bpe.getVocab().length, this.vocabSize;
|
|
17
|
-
}
|
|
18
|
-
async tokenise(e, t) {
|
|
19
|
-
return t ? this.bpe.tokenise(e, !0) : this.bpe.tokenise(e);
|
|
20
|
-
}
|
|
21
|
-
async detokenise(e) {
|
|
22
|
-
const t = this.bpe.getVocab();
|
|
23
|
-
return e.map((n) => n.map((i) => t[i]).join(""));
|
|
24
|
-
}
|
|
25
|
-
async encode(e) {
|
|
26
|
-
return (await this.tokenise([e], !0))[0];
|
|
27
|
-
}
|
|
28
|
-
async decode(e) {
|
|
29
|
-
return (await this.detokenise([e]))[0];
|
|
30
|
-
}
|
|
31
|
-
getVocab() {
|
|
32
|
-
return this.bpe.getVocab();
|
|
33
|
-
}
|
|
34
|
-
async getMerges() {
|
|
35
|
-
return this.bpe.getMerges();
|
|
36
|
-
}
|
|
37
|
-
async createTrainingData(e, t = 5) {
|
|
38
|
-
const s = this.bpe.tokenise(e, !0), n = [], i = [];
|
|
39
|
-
for (let r = 0; r < s.length - t; r++)
|
|
40
|
-
n.push(...s[r].slice(0, t)), i.push(s[r + 1][0]);
|
|
41
|
-
return [n, i];
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
export {
|
|
45
|
-
p as default
|
|
46
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { default as EE } from 'eventemitter3';
|
|
2
|
-
import { ITokeniser } from './type';
|
|
3
|
-
export default class WebTokeniser extends EE<'trainStatus'> implements ITokeniser {
|
|
4
|
-
private id;
|
|
5
|
-
vocabSize: number;
|
|
6
|
-
private handler?;
|
|
7
|
-
constructor();
|
|
8
|
-
destroy(): void;
|
|
9
|
-
private post;
|
|
10
|
-
train(text: string[], vocabSize: number): Promise<number>;
|
|
11
|
-
tokenise(text: string[], numeric: true): Promise<number[][]>;
|
|
12
|
-
tokenise(text: string[]): Promise<string[][]>;
|
|
13
|
-
detokenise(tokens: number[][]): Promise<string[]>;
|
|
14
|
-
encode(text: string): Promise<number[]>;
|
|
15
|
-
decode(tokens: number[]): Promise<string>;
|
|
16
|
-
getVocab(): Promise<string[]>;
|
|
17
|
-
createTrainingData(text: string[], windowSize?: number): Promise<[number[], number[]]>;
|
|
18
|
-
}
|