@genai-fi/nanogpt 0.0.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/LICENSE +7 -0
- package/README.md +20 -0
- package/dist/Generator.d.ts +14 -0
- package/dist/Generator.js +39 -0
- package/dist/NanoGPTModel.d.ts +35 -0
- package/dist/NanoGPTModel.js +129 -0
- package/dist/TeachableLLM.d.ts +21 -0
- package/dist/TeachableLLM.js +47 -0
- package/dist/Trainer.d.ts +19 -0
- package/dist/Trainer.js +34 -0
- package/dist/_commonjsHelpers-DaMA6jEr.js +8 -0
- package/dist/assets/worker-BYeSPNkq.js +1 -0
- package/dist/config.d.ts +11 -0
- package/dist/config.js +19 -0
- package/dist/index-B8nyc6IR.js +3899 -0
- package/dist/index-SOhdqzHq.js +113 -0
- package/dist/jszip.min-BLbRbbKt.js +2324 -0
- package/dist/layers/CausalSelfAttention.d.ts +22 -0
- package/dist/layers/CausalSelfAttention.js +75 -0
- package/dist/layers/LayerNorm.d.ts +12 -0
- package/dist/layers/LayerNorm.js +30 -0
- package/dist/layers/MLP.d.ts +17 -0
- package/dist/layers/MLP.js +57 -0
- package/dist/layers/TiedEmbedding.d.ts +22 -0
- package/dist/layers/TiedEmbedding.js +532 -0
- package/dist/layers/TransformerBlock.d.ts +19 -0
- package/dist/layers/TransformerBlock.js +47 -0
- package/dist/main.d.ts +6 -0
- package/dist/main.js +8 -0
- package/dist/tokeniser/CharTokeniser.d.ts +20 -0
- package/dist/tokeniser/CharTokeniser.js +52 -0
- package/dist/tokeniser/NodeTokeniser.d.ts +19 -0
- package/dist/tokeniser/NodeTokeniser.js +46 -0
- package/dist/tokeniser/WebTokeniser.d.ts +18 -0
- package/dist/tokeniser/WebTokeniser.js +96 -0
- package/dist/tokeniser/bpe.d.ts +14 -0
- package/dist/tokeniser/bpe.js +102 -0
- package/dist/tokeniser/messages.d.ts +61 -0
- package/dist/tokeniser/messages.js +1 -0
- package/dist/tokeniser/type.d.ts +14 -0
- package/dist/tokeniser/type.js +1 -0
- package/dist/tokeniser/worker.d.ts +1 -0
- package/dist/tokeniser/worker.js +53 -0
- package/dist/training/AdamExt.d.ts +23 -0
- package/dist/training/AdamExt.js +43 -0
- package/dist/training/DatasetBuilder.d.ts +12 -0
- package/dist/training/DatasetBuilder.js +27 -0
- package/dist/training/FullTrainer.d.ts +17 -0
- package/dist/training/FullTrainer.js +75 -0
- package/dist/training/LayerTrainer.d.ts +28 -0
- package/dist/training/LayerTrainer.js +108 -0
- package/dist/training/Trainer.d.ts +73 -0
- package/dist/training/Trainer.js +87 -0
- package/dist/training/lwSchedule.d.ts +7 -0
- package/dist/training/lwSchedule.js +162 -0
- package/dist/utilities/generate.d.ts +3 -0
- package/dist/utilities/generate.js +22 -0
- package/dist/utilities/load.d.ts +7 -0
- package/dist/utilities/load.js +47 -0
- package/dist/utilities/save.d.ts +3 -0
- package/dist/utilities/save.js +21 -0
- package/dist/utilities/textLoader.d.ts +1 -0
- package/dist/utilities/textLoader.js +438 -0
- package/dist/utilities/tokenParse.d.ts +1 -0
- package/dist/utilities/tokenParse.js +66 -0
- package/dist/utilities/weights.d.ts +12 -0
- package/dist/utilities/weights.js +43 -0
- package/package.json +59 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { default as TF } from '@tensorflow/tfjs';
|
|
2
|
+
import { GPTConfig } from '../config';
|
|
3
|
+
export default class CausalSelfAttention {
|
|
4
|
+
private config;
|
|
5
|
+
private cAttn;
|
|
6
|
+
private cProj;
|
|
7
|
+
private attnDropout;
|
|
8
|
+
private residDropout;
|
|
9
|
+
private bias;
|
|
10
|
+
private maskInf;
|
|
11
|
+
private tf;
|
|
12
|
+
private divisor;
|
|
13
|
+
private index;
|
|
14
|
+
private _trainable;
|
|
15
|
+
constructor(tf: typeof TF, index: number, config: GPTConfig);
|
|
16
|
+
get variables(): TF.Variable[];
|
|
17
|
+
get trainable(): boolean;
|
|
18
|
+
set trainable(value: boolean);
|
|
19
|
+
saveWeights(map: Map<string, TF.Tensor[]>): void;
|
|
20
|
+
loadWeights(weights: Map<string, TF.Tensor[]>): void;
|
|
21
|
+
call(x: TF.Tensor, training?: boolean): TF.Tensor;
|
|
22
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
class g {
|
|
2
|
+
config;
|
|
3
|
+
cAttn;
|
|
4
|
+
cProj;
|
|
5
|
+
attnDropout;
|
|
6
|
+
residDropout;
|
|
7
|
+
bias;
|
|
8
|
+
maskInf;
|
|
9
|
+
tf;
|
|
10
|
+
divisor;
|
|
11
|
+
index;
|
|
12
|
+
_trainable = !0;
|
|
13
|
+
constructor(s, e, t) {
|
|
14
|
+
this.config = t, this.tf = s, this.index = e, this.cAttn = this.tf.layers.dense({
|
|
15
|
+
units: 3 * t.nEmbed,
|
|
16
|
+
useBias: t.biasInLinear,
|
|
17
|
+
name: `block_${e}_attn_cAttn`,
|
|
18
|
+
kernelInitializer: this.tf.initializers.randomNormal({
|
|
19
|
+
mean: 0,
|
|
20
|
+
stddev: 0.02
|
|
21
|
+
}),
|
|
22
|
+
biasInitializer: "zeros"
|
|
23
|
+
}), this.cProj = this.tf.layers.dense({
|
|
24
|
+
units: t.nEmbed,
|
|
25
|
+
useBias: t.biasInLinear,
|
|
26
|
+
name: `block_${e}_attn_cProj`,
|
|
27
|
+
kernelInitializer: this.tf.initializers.randomNormal({
|
|
28
|
+
mean: 0,
|
|
29
|
+
stddev: 0.02 / Math.sqrt(2 * t.nLayer)
|
|
30
|
+
}),
|
|
31
|
+
biasInitializer: "zeros"
|
|
32
|
+
}), this.attnDropout = this.tf.layers.dropout({ rate: t.dropout }), this.residDropout = this.tf.layers.dropout({ rate: t.dropout }), this.bias = this.tf.linalg.bandPart(this.tf.ones([t.blockSize, t.blockSize]), -1, 0).cast("bool"), this.divisor = this.tf.scalar(1 / Math.sqrt(t.nEmbed / t.nHead)), this.maskInf = this.tf.zeros([t.blockSize, t.blockSize]).where(this.bias, -1 / 0);
|
|
33
|
+
}
|
|
34
|
+
get variables() {
|
|
35
|
+
return [
|
|
36
|
+
...this.cAttn.trainableWeights.map((s) => s.read()),
|
|
37
|
+
...this.cProj.trainableWeights.map((s) => s.read())
|
|
38
|
+
];
|
|
39
|
+
}
|
|
40
|
+
get trainable() {
|
|
41
|
+
return this._trainable;
|
|
42
|
+
}
|
|
43
|
+
set trainable(s) {
|
|
44
|
+
this._trainable = s, this.cAttn.trainable = s, this.cProj.trainable = s;
|
|
45
|
+
}
|
|
46
|
+
saveWeights(s) {
|
|
47
|
+
s.set(`block_${this.index}_cAttn`, this.cAttn.getWeights()), s.set(`block_${this.index}_cProj`, this.cProj.getWeights());
|
|
48
|
+
}
|
|
49
|
+
loadWeights(s) {
|
|
50
|
+
this.cAttn.setWeights(s.get(`block_${this.index}_cAttn`) || []), this.cProj.setWeights(s.get(`block_${this.index}_cProj`) || []);
|
|
51
|
+
}
|
|
52
|
+
call(s, e = !1) {
|
|
53
|
+
return this.tf.tidy(() => {
|
|
54
|
+
const [t, i, n] = s.shape, r = this.cAttn.apply(s), [o, h, l] = this.tf.split(r, 3, -1);
|
|
55
|
+
r.dispose();
|
|
56
|
+
const a = n / this.config.nHead, d = this.tf.reshape(o, [t, i, this.config.nHead, a]);
|
|
57
|
+
o.dispose();
|
|
58
|
+
const b = d.transpose([0, 2, 1, 3]);
|
|
59
|
+
d.dispose();
|
|
60
|
+
const c = this.tf.reshape(h, [t, i, this.config.nHead, a]);
|
|
61
|
+
h.dispose();
|
|
62
|
+
const u = c.transpose([0, 2, 1, 3]);
|
|
63
|
+
c.dispose();
|
|
64
|
+
const p = this.tf.reshape(l, [t, i, this.config.nHead, a]);
|
|
65
|
+
l.dispose();
|
|
66
|
+
const f = p.transpose([0, 2, 1, 3]);
|
|
67
|
+
p.dispose();
|
|
68
|
+
const m = this.tf.matMul(b, u, !1, !0).mul(this.divisor), k = this.maskInf.slice([0, 0], [i, i]), _ = m.add(k), y = this.tf.softmax(_, -1), z = this.attnDropout.apply(y, { training: e }), A = this.tf.matMul(z, f).transpose([0, 2, 1, 3]), P = this.tf.reshape(A, [t, i, n]), j = this.cProj.apply(P);
|
|
69
|
+
return this.residDropout.apply(j, { training: e });
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
export {
|
|
74
|
+
g as default
|
|
75
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { default as TF } from '@tensorflow/tfjs';
|
|
2
|
+
export default class LayerNorm {
|
|
3
|
+
private gamma;
|
|
4
|
+
private epsilon;
|
|
5
|
+
private tf;
|
|
6
|
+
constructor(tf: typeof TF, shape: number[], epsilon?: number, name?: string);
|
|
7
|
+
get trainableWeights(): TF.Variable[];
|
|
8
|
+
set trainable(value: boolean);
|
|
9
|
+
getWeights(): TF.Tensor[];
|
|
10
|
+
setWeights(weights: TF.Tensor[]): void;
|
|
11
|
+
apply(x: TF.Tensor): TF.Tensor;
|
|
12
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
class u {
|
|
2
|
+
gamma;
|
|
3
|
+
//private beta: TF.Variable;
|
|
4
|
+
epsilon;
|
|
5
|
+
tf;
|
|
6
|
+
constructor(a, s, t = 1e-5, e = "") {
|
|
7
|
+
this.tf = a, this.epsilon = t, this.gamma = a.variable(a.ones(s), !0, `${e}_gamma`, "float32");
|
|
8
|
+
}
|
|
9
|
+
get trainableWeights() {
|
|
10
|
+
return [this.gamma];
|
|
11
|
+
}
|
|
12
|
+
set trainable(a) {
|
|
13
|
+
this.gamma.trainable = a;
|
|
14
|
+
}
|
|
15
|
+
getWeights() {
|
|
16
|
+
return [this.gamma];
|
|
17
|
+
}
|
|
18
|
+
setWeights(a) {
|
|
19
|
+
this.gamma.assign(a[0]);
|
|
20
|
+
}
|
|
21
|
+
apply(a) {
|
|
22
|
+
return this.tf.tidy(() => {
|
|
23
|
+
const s = a.mean(-1, !0), t = a.sub(s), n = t.square().mean(-1, !0).add(this.epsilon).rsqrt();
|
|
24
|
+
return t.mul(n).mul(this.gamma);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export {
|
|
29
|
+
u as default
|
|
30
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { default as TF } from '@tensorflow/tfjs';
|
|
2
|
+
import { GPTConfig } from '../config';
|
|
3
|
+
export default class MLP {
|
|
4
|
+
private cFc;
|
|
5
|
+
private cProj;
|
|
6
|
+
private dropout;
|
|
7
|
+
private tf;
|
|
8
|
+
private index;
|
|
9
|
+
private _trainable;
|
|
10
|
+
constructor(tf: typeof TF, index: number, config: GPTConfig);
|
|
11
|
+
get variables(): TF.Variable[];
|
|
12
|
+
get trainable(): boolean;
|
|
13
|
+
set trainable(value: boolean);
|
|
14
|
+
saveWeights(map: Map<string, TF.Tensor[]>): void;
|
|
15
|
+
loadWeights(weights: Map<string, TF.Tensor[]>): void;
|
|
16
|
+
call(x: TF.Tensor, training?: boolean): TF.Tensor;
|
|
17
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
class n {
|
|
2
|
+
cFc;
|
|
3
|
+
cProj;
|
|
4
|
+
dropout;
|
|
5
|
+
tf;
|
|
6
|
+
index;
|
|
7
|
+
_trainable = !0;
|
|
8
|
+
constructor(t, i, e) {
|
|
9
|
+
this.tf = t, this.index = i, this.cFc = this.tf.layers.dense({
|
|
10
|
+
units: 4 * e.nEmbed,
|
|
11
|
+
activation: "gelu",
|
|
12
|
+
useBias: e.biasInLinear,
|
|
13
|
+
kernelInitializer: this.tf.initializers.randomNormal({
|
|
14
|
+
mean: 0,
|
|
15
|
+
stddev: 0.02
|
|
16
|
+
}),
|
|
17
|
+
biasInitializer: "zeros",
|
|
18
|
+
name: `block_${i}_mlp_cFc`
|
|
19
|
+
}), this.cProj = this.tf.layers.dense({
|
|
20
|
+
units: e.nEmbed,
|
|
21
|
+
useBias: e.biasInLinear,
|
|
22
|
+
kernelInitializer: this.tf.initializers.randomNormal({
|
|
23
|
+
mean: 0,
|
|
24
|
+
stddev: 0.02 / Math.sqrt(2 * e.nLayer)
|
|
25
|
+
}),
|
|
26
|
+
biasInitializer: "zeros",
|
|
27
|
+
name: `block_${i}_mlp_cProj`
|
|
28
|
+
}), this.dropout = this.tf.layers.dropout({ rate: e.dropout });
|
|
29
|
+
}
|
|
30
|
+
get variables() {
|
|
31
|
+
return [
|
|
32
|
+
...this.cFc.trainableWeights.map((t) => t.read()),
|
|
33
|
+
...this.cProj.trainableWeights.map((t) => t.read())
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
get trainable() {
|
|
37
|
+
return this._trainable;
|
|
38
|
+
}
|
|
39
|
+
set trainable(t) {
|
|
40
|
+
this._trainable = t, this.cFc.trainable = t, this.cProj.trainable = t;
|
|
41
|
+
}
|
|
42
|
+
saveWeights(t) {
|
|
43
|
+
t.set(`block_${this.index}_mlpHidden`, this.cFc.getWeights()), t.set(`block_${this.index}_mlpOut`, this.cProj.getWeights());
|
|
44
|
+
}
|
|
45
|
+
loadWeights(t) {
|
|
46
|
+
this.cFc.setWeights(t.get(`block_${this.index}_mlpHidden`) || []), this.cProj.setWeights(t.get(`block_${this.index}_mlpOut`) || []);
|
|
47
|
+
}
|
|
48
|
+
call(t, i = !1) {
|
|
49
|
+
return this.tf.tidy(() => {
|
|
50
|
+
const e = this.cFc.apply(t), s = this.cProj.apply(e);
|
|
51
|
+
return this.dropout.apply(s, { training: i });
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export {
|
|
56
|
+
n as default
|
|
57
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { default as TF } from '@tensorflow/tfjs';
|
|
2
|
+
export default class TiedEmbeddingOutputLayer {
|
|
3
|
+
private vocabSize;
|
|
4
|
+
private embedDim;
|
|
5
|
+
private tf;
|
|
6
|
+
private tiedWeights;
|
|
7
|
+
private initializer;
|
|
8
|
+
constructor(tf: typeof TF, config: {
|
|
9
|
+
vocabSize: number;
|
|
10
|
+
embedDim: number;
|
|
11
|
+
name?: string;
|
|
12
|
+
}, name?: string);
|
|
13
|
+
get variables(): TF.Variable[];
|
|
14
|
+
embed(inputs: TF.Tensor): TF.Tensor;
|
|
15
|
+
project(inputs: TF.Tensor): TF.Tensor;
|
|
16
|
+
getWeights(): TF.Tensor[];
|
|
17
|
+
setWeights(weights: TF.Tensor[]): void;
|
|
18
|
+
getConfig(): {
|
|
19
|
+
vocabSize: number;
|
|
20
|
+
embedDim: number;
|
|
21
|
+
};
|
|
22
|
+
}
|