@genai-fi/nanogpt 0.1.8 → 0.1.9
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/Trainer.d.ts +2 -0
- package/dist/Trainer.js +10 -5
- package/dist/training/FullTrainer.js +27 -29
- package/dist/training/Trainer.d.ts +2 -0
- package/dist/training/Trainer.js +31 -27
- package/package.json +1 -1
package/dist/Trainer.d.ts
CHANGED
|
@@ -12,7 +12,9 @@ export interface ITrainerOptions {
|
|
|
12
12
|
}
|
|
13
13
|
export default class Trainer extends EE<'start' | 'stop' | 'log'> {
|
|
14
14
|
private trainer;
|
|
15
|
+
private hasTrained;
|
|
15
16
|
constructor(model: NanoGPT, tokeniser: ITokeniser);
|
|
16
17
|
stop(): void;
|
|
18
|
+
reset(): void;
|
|
17
19
|
train(text: string[], options?: ITrainerOptions): Promise<void>;
|
|
18
20
|
}
|
package/dist/Trainer.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { E as l } from "./index-SOhdqzHq.js";
|
|
2
|
-
import
|
|
3
|
-
class
|
|
2
|
+
import h from "./training/FullTrainer.js";
|
|
3
|
+
class m extends l {
|
|
4
4
|
trainer;
|
|
5
|
+
hasTrained = !1;
|
|
5
6
|
constructor(a, t) {
|
|
6
|
-
super(), this.trainer = new
|
|
7
|
+
super(), this.trainer = new h(a.tf, a, t, 1e-3);
|
|
7
8
|
}
|
|
8
9
|
stop() {
|
|
10
|
+
this.trainer.stop();
|
|
11
|
+
}
|
|
12
|
+
reset() {
|
|
13
|
+
this.hasTrained = !1, this.trainer.reset();
|
|
9
14
|
}
|
|
10
15
|
async train(a, t) {
|
|
11
16
|
const { trainDataset: e, validationDataset: r } = await this.trainer.createTrainValidationSplit(
|
|
@@ -13,7 +18,7 @@ class d extends l {
|
|
|
13
18
|
t?.batchSize || 32,
|
|
14
19
|
t?.validationSplit || 0.1
|
|
15
20
|
);
|
|
16
|
-
this.trainer.setLearningRate(t?.learningRate || 1e-3), this.emit("start"), await this.trainer.trainOnDataset(
|
|
21
|
+
this.hasTrained || this.trainer.setLearningRate(t?.learningRate || 1e-3), this.hasTrained = !0, this.emit("start"), await this.trainer.trainOnDataset(
|
|
17
22
|
e,
|
|
18
23
|
{
|
|
19
24
|
prompt: t?.prompt,
|
|
@@ -31,5 +36,5 @@ class d extends l {
|
|
|
31
36
|
}
|
|
32
37
|
}
|
|
33
38
|
export {
|
|
34
|
-
|
|
39
|
+
m as default
|
|
35
40
|
};
|
|
@@ -1,70 +1,68 @@
|
|
|
1
1
|
import { generateText as L } from "../utilities/generate.js";
|
|
2
2
|
import w from "./Trainer.js";
|
|
3
|
-
import
|
|
4
|
-
const
|
|
3
|
+
import x from "./Evaluator.js";
|
|
4
|
+
const g = {
|
|
5
5
|
desiredLoss: 0.01,
|
|
6
6
|
logInterval: 1,
|
|
7
7
|
maxSteps: 1e3
|
|
8
8
|
};
|
|
9
|
-
class
|
|
9
|
+
class P extends w {
|
|
10
10
|
constructor(r, i, o, n = 3e-4) {
|
|
11
11
|
super(r, i, o, n);
|
|
12
12
|
}
|
|
13
13
|
// Train for multiple epochs using Dataset API - FIXED memory leaks
|
|
14
14
|
async trainOnDataset(r, i, o) {
|
|
15
|
-
const { desiredLoss: n, logInterval:
|
|
16
|
-
...
|
|
15
|
+
const { desiredLoss: n, logInterval: m, onStep: l, prompt: c, maxSteps: d } = {
|
|
16
|
+
...g,
|
|
17
17
|
...i
|
|
18
|
-
},
|
|
19
|
-
pass: 0,
|
|
20
|
-
depth: 1,
|
|
18
|
+
}, t = {
|
|
21
19
|
step: 0,
|
|
22
|
-
stepSinceDepthChange: 0,
|
|
23
20
|
lastLoss: 1e6,
|
|
24
21
|
totalSteps: 0,
|
|
25
22
|
losses: [],
|
|
26
|
-
validationLosses: []
|
|
23
|
+
validationLosses: [],
|
|
24
|
+
...this.lastState || {}
|
|
27
25
|
};
|
|
28
|
-
this.dummyPass(), this.model.trainable = !0;
|
|
26
|
+
this.lastState = t, this.dummyPass(), this.model.trainable = !0;
|
|
29
27
|
const u = Date.now();
|
|
30
28
|
this.running = !0;
|
|
31
|
-
const
|
|
29
|
+
const h = o ? new x(this.model, o) : void 0, f = await r.iterator();
|
|
32
30
|
try {
|
|
33
|
-
for (; this.running && !(
|
|
31
|
+
for (; this.running && !(t.lastLoss < n); ) {
|
|
34
32
|
const e = await f.next();
|
|
35
33
|
if (e.done) break;
|
|
36
|
-
const
|
|
37
|
-
loss:
|
|
38
|
-
step:
|
|
34
|
+
const p = e.value, v = this.trainBatch(t, p), a = {
|
|
35
|
+
loss: t.lastLoss,
|
|
36
|
+
step: t.step,
|
|
39
37
|
time: Date.now() - u,
|
|
40
|
-
batchSize:
|
|
38
|
+
batchSize: p.xs.shape[0]
|
|
41
39
|
};
|
|
42
|
-
if (this.model.log.push(a),
|
|
43
|
-
if (await v,
|
|
40
|
+
if (this.model.log.push(a), t.step % m === 0) {
|
|
41
|
+
if (await v, h)
|
|
44
42
|
try {
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
} catch (
|
|
48
|
-
console.error("Validation error:",
|
|
43
|
+
const s = await h.evaluate(5);
|
|
44
|
+
t.validationLosses.push(s), a.valLoss = s;
|
|
45
|
+
} catch (s) {
|
|
46
|
+
console.error("Validation error:", s);
|
|
49
47
|
}
|
|
50
48
|
if (l) {
|
|
51
|
-
if (
|
|
52
|
-
const
|
|
49
|
+
if (c) {
|
|
50
|
+
const s = await L(this.tokenizer, this.model, c, 100, {
|
|
53
51
|
temperature: 0.8
|
|
54
52
|
});
|
|
55
|
-
a.example =
|
|
53
|
+
a.example = s;
|
|
56
54
|
}
|
|
57
55
|
await l(a);
|
|
58
56
|
}
|
|
59
57
|
}
|
|
60
|
-
|
|
58
|
+
t.step >= d && this.stop();
|
|
61
59
|
}
|
|
62
60
|
} catch (e) {
|
|
63
61
|
throw console.error("Training error:", e), this.tf.dispose(), e;
|
|
64
62
|
}
|
|
65
|
-
return this.tf.dispose(), this.running = !1, { losses:
|
|
63
|
+
return this.tf.dispose(), this.running = !1, { losses: t.losses, validationLosses: t.validationLosses };
|
|
66
64
|
}
|
|
67
65
|
}
|
|
68
66
|
export {
|
|
69
|
-
|
|
67
|
+
P as default
|
|
70
68
|
};
|
|
@@ -31,8 +31,10 @@ export default abstract class GPTTrainer {
|
|
|
31
31
|
protected tf: typeof TF;
|
|
32
32
|
protected learningRate: number;
|
|
33
33
|
protected running: boolean;
|
|
34
|
+
protected lastState?: TrainingState;
|
|
34
35
|
constructor(tf: typeof TF, model: NanoGPT, tokenizer: ITokeniser, learningRate?: number);
|
|
35
36
|
setLearningRate(learningRate: number): void;
|
|
37
|
+
reset(): void;
|
|
36
38
|
stop(): void;
|
|
37
39
|
getOptimizer(): AdamExt;
|
|
38
40
|
resetOptimizer(config?: AdamConfig): void;
|
package/dist/training/Trainer.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { DatasetBuilder as d } from "./DatasetBuilder.js";
|
|
2
|
-
import
|
|
2
|
+
import h from "./AdamExt.js";
|
|
3
3
|
class u {
|
|
4
|
-
constructor(t,
|
|
5
|
-
this.tokenizer =
|
|
4
|
+
constructor(t, e, s, i = 1e-3) {
|
|
5
|
+
this.tokenizer = s, this.tf = t, this.model = e, this.learningRate = i, this.resetOptimizer(), this.datasetBuilder = new d(this.tf, s, e.config.blockSize);
|
|
6
6
|
}
|
|
7
7
|
model;
|
|
8
8
|
optimizer;
|
|
@@ -10,9 +10,13 @@ class u {
|
|
|
10
10
|
tf;
|
|
11
11
|
learningRate;
|
|
12
12
|
running = !1;
|
|
13
|
+
lastState;
|
|
13
14
|
setLearningRate(t) {
|
|
14
15
|
this.learningRate = t, this.resetOptimizer({ learningRateFactor: 1, beta1: 0.9, beta2: 0.99, epsilon: 1e-8 });
|
|
15
16
|
}
|
|
17
|
+
reset() {
|
|
18
|
+
this.lastState = void 0, this.running = !1;
|
|
19
|
+
}
|
|
16
20
|
stop() {
|
|
17
21
|
this.running = !1;
|
|
18
22
|
}
|
|
@@ -21,7 +25,7 @@ class u {
|
|
|
21
25
|
}
|
|
22
26
|
resetOptimizer(t = { learningRateFactor: 1, beta1: 0.9, beta2: 0.99, epsilon: 1e-8 }) {
|
|
23
27
|
this.optimizer && this.optimizer.dispose();
|
|
24
|
-
const
|
|
28
|
+
const e = new h(
|
|
25
29
|
t.learningRateFactor * this.learningRate,
|
|
26
30
|
t.beta1,
|
|
27
31
|
t.beta2,
|
|
@@ -33,53 +37,53 @@ class u {
|
|
|
33
37
|
weightDecay: 0
|
|
34
38
|
}
|
|
35
39
|
);
|
|
36
|
-
this.optimizer =
|
|
40
|
+
this.optimizer = e;
|
|
37
41
|
}
|
|
38
42
|
printGradients(t) {
|
|
39
|
-
Object.keys(t).forEach((
|
|
40
|
-
const
|
|
41
|
-
console.log(`${
|
|
43
|
+
Object.keys(t).forEach((e) => {
|
|
44
|
+
const s = t[e];
|
|
45
|
+
console.log(`${e}:`), console.log(` Shape: ${s.shape}`), console.log(` Mean: ${this.tf.mean(s).dataSync()[0]}`), console.log(` Std: ${this.tf.moments(s).variance.sqrt().dataSync()[0]}`), console.log(` Min: ${this.tf.min(s).dataSync()[0]}`), console.log(` Max: ${this.tf.max(s).dataSync()[0]}`), console.log(` Norm: ${this.tf.norm(s).dataSync()[0]}`);
|
|
42
46
|
});
|
|
43
47
|
}
|
|
44
|
-
trainStep(t,
|
|
48
|
+
trainStep(t, e = !1, s = !1) {
|
|
45
49
|
return this.tf.tidy(() => {
|
|
46
50
|
const { xs: i, ys: a } = t, o = () => {
|
|
47
51
|
const { loss: l, logits: c } = this.model.forward(i, a, !0);
|
|
48
52
|
return c.dispose(), l;
|
|
49
53
|
}, { value: n, grads: r } = this.tf.variableGrads(o);
|
|
50
|
-
return
|
|
54
|
+
return e || (s && (console.log("-------"), this.printGradients(r), console.log("-------")), this.optimizer.applyGradients(r), this.tf.dispose(r)), n;
|
|
51
55
|
});
|
|
52
56
|
}
|
|
53
57
|
dummyPass() {
|
|
54
|
-
const t = this.tf.zeros([1, this.model.config.blockSize], "int32"),
|
|
58
|
+
const t = this.tf.zeros([1, this.model.config.blockSize], "int32"), e = this.tf.zeros([1, this.model.config.blockSize, this.model.config.vocabSize]);
|
|
55
59
|
try {
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
} catch (
|
|
59
|
-
console.error("Error during dummy pass:",
|
|
60
|
+
const s = this.trainStep({ xs: t, ys: e }, !0);
|
|
61
|
+
s.dataSync(), s.dispose();
|
|
62
|
+
} catch (s) {
|
|
63
|
+
console.error("Error during dummy pass:", s);
|
|
60
64
|
} finally {
|
|
61
|
-
t.dispose(),
|
|
65
|
+
t.dispose(), e.dispose();
|
|
62
66
|
}
|
|
63
67
|
}
|
|
64
|
-
async trainBatch(t,
|
|
68
|
+
async trainBatch(t, e) {
|
|
65
69
|
try {
|
|
66
|
-
const
|
|
67
|
-
return
|
|
68
|
-
} catch (
|
|
69
|
-
throw console.error(`Error processing batch at step ${t.step}:`,
|
|
70
|
+
const s = this.trainStep(e, !1, !1);
|
|
71
|
+
return e.xs.dispose(), e.ys.dispose(), t.step++, t.totalSteps++, s.array().then((i) => (t.lastLoss = i, t.losses.push(t.lastLoss), s.dispose(), t.lastLoss));
|
|
72
|
+
} catch (s) {
|
|
73
|
+
throw console.error(`Error processing batch at step ${t.step}:`, s), this.tf.dispose(), s;
|
|
70
74
|
}
|
|
71
75
|
}
|
|
72
|
-
async createTrainValidationSplit(t,
|
|
73
|
-
const i = await this.datasetBuilder.createTextDataset(t,
|
|
76
|
+
async createTrainValidationSplit(t, e = 32, s = 0.1) {
|
|
77
|
+
const i = await this.datasetBuilder.createTextDataset(t, e, 0, 1 - s), a = await this.datasetBuilder.createTextDataset(
|
|
74
78
|
t,
|
|
75
|
-
|
|
76
|
-
1 -
|
|
79
|
+
e,
|
|
80
|
+
1 - s,
|
|
77
81
|
1
|
|
78
82
|
);
|
|
79
83
|
return { trainDataset: i, validationDataset: a };
|
|
80
84
|
}
|
|
81
|
-
async createDataset(t,
|
|
82
|
-
return await this.datasetBuilder.createTextDataset(t,
|
|
85
|
+
async createDataset(t, e = 32) {
|
|
86
|
+
return await this.datasetBuilder.createTextDataset(t, e);
|
|
83
87
|
}
|
|
84
88
|
dispose() {
|
|
85
89
|
this.optimizer && this.optimizer.dispose();
|