@genai-fi/nanogpt 0.1.1 → 0.1.2

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.
@@ -1,7 +1,6 @@
1
1
  import { default as TF } from '@tensorflow/tfjs';
2
2
  import { GPTConfig } from './config';
3
3
  export interface TrainingLogEntry {
4
- epoch: number;
5
4
  loss: number;
6
5
  valLoss?: number;
7
6
  step: number;
@@ -6,7 +6,7 @@ import { default as Generator, IGenerateOptions } from './Generator';
6
6
  import { default as Trainer, ITrainerOptions } from './Trainer';
7
7
  import { default as EE } from 'eventemitter3';
8
8
  type TeachableLLMStatus = 'warmup' | 'ready' | 'training' | 'loading' | 'busy' | 'error';
9
- export default class TeachableLLM extends EE<'status' | 'error'> {
9
+ export default class TeachableLLM extends EE<'status' | 'error' | 'trainStep'> {
10
10
  private _config?;
11
11
  private _model?;
12
12
  readonly tf: typeof TF;
@@ -1,13 +1,13 @@
1
- import a from "./NanoGPTModel.js";
2
- import { defaultConfig as h } from "./config.js";
3
- import { saveModel as d } from "./utilities/save.js";
4
- import { loadModel as m } from "./utilities/load.js";
5
- import l from "./Generator.js";
6
- import u from "./Trainer.js";
7
- import { E as _ } from "./index-SOhdqzHq.js";
8
- import { dummyPassAsync as f } from "./utilities/dummy.js";
9
- import c from "./tokeniser/CharTokeniser.js";
10
- class s extends _ {
1
+ import m from "./NanoGPTModel.js";
2
+ import { defaultConfig as d } from "./config.js";
3
+ import { saveModel as l } from "./utilities/save.js";
4
+ import { loadModel as u } from "./utilities/load.js";
5
+ import _ from "./Generator.js";
6
+ import c from "./Trainer.js";
7
+ import { E as f } from "./index-SOhdqzHq.js";
8
+ import { dummyPassAsync as a } from "./utilities/dummy.js";
9
+ import g from "./tokeniser/CharTokeniser.js";
10
+ class n extends f {
11
11
  _config;
12
12
  _model;
13
13
  tf;
@@ -43,23 +43,27 @@ class s extends _ {
43
43
  saveModel() {
44
44
  if (!this._model || !this._tokeniser)
45
45
  throw new Error("Model or tokeniser is not initialized.");
46
- return d(this._model, this._tokeniser);
46
+ return l(this._model, this._tokeniser);
47
47
  }
48
48
  static loadModel(t, r) {
49
- const e = new s(t);
50
- return m(t, r).then(({ model: i, tokeniser: o }) => {
51
- e._model = i, e._tokeniser = o, e._config = i.config, e.setStatus("warmup"), f(i).then(() => {
49
+ const e = new n(t);
50
+ return u(t, r).then(({ model: i, tokeniser: s }) => {
51
+ e._model = i, e._tokeniser = s, e._config = i.config, e.setStatus("warmup"), a(i).then(() => {
52
52
  e.setStatus("ready");
53
- }).catch((n) => {
54
- e.setStatus("error"), e.emit("error", n);
53
+ }).catch((o) => {
54
+ e.setStatus("error"), e.emit("error", o);
55
55
  });
56
56
  }).catch((i) => {
57
57
  e.setStatus("error"), e.emit("error", i);
58
58
  }), e;
59
59
  }
60
60
  static create(t, r = {}) {
61
- const e = { ...h, ...r }, i = new c(e.vocabSize), o = new a(t, e);
62
- return new s(t, i, o);
61
+ const e = { ...d, ...r }, i = new g(e.vocabSize), s = new m(t, e), o = new n(t, i, s);
62
+ return o.setStatus("warmup"), a(s).then(() => {
63
+ o.setStatus("ready");
64
+ }).catch((h) => {
65
+ o.setStatus("error"), o.emit("error", h);
66
+ }), o;
63
67
  }
64
68
  getNumParams() {
65
69
  if (!this._model)
@@ -69,8 +73,12 @@ class s extends _ {
69
73
  trainer() {
70
74
  if (!this._model || !this._tokeniser)
71
75
  throw new Error("Model or tokeniser is not initialized.");
72
- const t = new u(this._model, this._tokeniser);
73
- return t.on("start", () => this.setStatus("training")), t.on("stop", () => this.setStatus("ready")), t;
76
+ const t = new c(this._model, this._tokeniser);
77
+ return t.on("start", () => this.setStatus("training")), t.on("stop", () => this.setStatus("ready")), t.on("log", async (r) => {
78
+ const e = this.listeners("trainStep");
79
+ for (const i of e)
80
+ await i(r);
81
+ }), t;
74
82
  }
75
83
  train(t, r) {
76
84
  return this.trainer().train(t, r);
@@ -78,7 +86,7 @@ class s extends _ {
78
86
  generator() {
79
87
  if (!this._model || !this._tokeniser)
80
88
  throw new Error("Model or tokeniser is not initialized.");
81
- const t = new l(this._model, this._tokeniser);
89
+ const t = new _(this._model, this._tokeniser);
82
90
  return t.on("start", () => this.setStatus("busy")), t.on("stop", () => this.setStatus("ready")), t;
83
91
  }
84
92
  generateText(t, r) {
@@ -86,5 +94,5 @@ class s extends _ {
86
94
  }
87
95
  }
88
96
  export {
89
- s as default
97
+ n as default
90
98
  };
package/dist/Trainer.d.ts CHANGED
@@ -2,7 +2,6 @@ import { default as NanoGPT } from './NanoGPTModel';
2
2
  import { ITokeniser } from './tokeniser/type';
3
3
  import { default as EE } from 'eventemitter3';
4
4
  export interface ITrainerOptions {
5
- epochs?: number;
6
5
  batchSize?: number;
7
6
  learningRate?: number;
8
7
  maxSteps?: number;
package/dist/Trainer.js CHANGED
@@ -1,34 +1,34 @@
1
- import { E as s } from "./index-SOhdqzHq.js";
2
- import n from "./training/FullTrainer.js";
3
- class o extends s {
1
+ import { E as l } from "./index-SOhdqzHq.js";
2
+ import o from "./training/FullTrainer.js";
3
+ class m extends l {
4
4
  trainer;
5
5
  constructor(a, t) {
6
- super(), this.trainer = new n(a.tf, a, t, 1e-3);
6
+ super(), this.trainer = new o(a.tf, a, t, 1e-3);
7
7
  }
8
8
  stop() {
9
9
  }
10
10
  async train(a, t) {
11
- const { trainDataset: e, validationDataset: r } = await this.trainer.createTrainValidationSplit(
11
+ const { trainDataset: r, validationDataset: e } = await this.trainer.createTrainValidationSplit(
12
12
  a,
13
13
  t?.batchSize || 32,
14
14
  t?.validationSplit || 0.1
15
15
  );
16
16
  this.emit("start"), await this.trainer.trainOnDataset(
17
- e,
17
+ r,
18
18
  {
19
- epochs: t?.epochs || 2,
20
19
  prompt: t?.prompt,
21
- stepsPerEpoch: t?.maxSteps || 100,
22
20
  logInterval: t?.logInterval || 10,
23
21
  desiredLoss: t?.desiredLoss || 0.01,
24
22
  onStep: async (i) => {
25
- this.emit("log", i);
23
+ const s = this.listeners("log");
24
+ for (const n of s)
25
+ await n(i);
26
26
  }
27
27
  },
28
- r
28
+ e
29
29
  ), this.emit("stop");
30
30
  }
31
31
  }
32
32
  export {
33
- o as default
33
+ m as default
34
34
  };
@@ -1,77 +1,65 @@
1
- import { generateText as g } from "../utilities/generate.js";
2
- import T from "./Trainer.js";
3
- const b = {
4
- epochs: 1,
5
- stepsPerEpoch: 1e6,
1
+ import { generateText as L } from "../utilities/generate.js";
2
+ import f from "./Trainer.js";
3
+ const w = {
6
4
  desiredLoss: 0.01,
7
5
  logInterval: 1
8
6
  };
9
- class S extends T {
10
- constructor(a, r, t, i = 3e-4) {
11
- super(a, r, t, i);
7
+ class g extends f {
8
+ constructor(r, i, o, n = 3e-4) {
9
+ super(r, i, o, n);
12
10
  }
13
11
  // Train for multiple epochs using Dataset API - FIXED memory leaks
14
- async trainOnDataset(a, r, t) {
15
- const { epochs: i, stepsPerEpoch: n, desiredLoss: c, logInterval: L, onStep: h, onEpoch: o, prompt: l } = {
16
- ...b,
17
- ...r
12
+ async trainOnDataset(r, i, o) {
13
+ const { desiredLoss: n, logInterval: h, onStep: l, prompt: c } = {
14
+ ...w,
15
+ ...i
18
16
  }, s = {
19
- epoch: 0,
20
17
  pass: 0,
21
18
  depth: 1,
22
19
  step: 0,
23
20
  stepSinceDepthChange: 0,
24
21
  lastLoss: 1e6,
25
- epochLoss: 0,
26
22
  totalSteps: 0,
27
23
  losses: [],
28
24
  validationLosses: []
29
25
  };
30
26
  this.dummyPass(), this.model.trainable = !0;
31
- const m = Date.now();
32
- for (s.epoch = 0; s.epoch < i; s.epoch++) {
33
- s.step = 0, s.epochLoss = 0, s.pass = 0, s.depth = 1, s.stepSinceDepthChange = 0;
34
- const u = await a.iterator();
35
- try {
36
- for (; !(n && s.step >= n || s.lastLoss < c); ) {
37
- const e = await u.next();
38
- if (e.done) break;
39
- const f = e.value, w = this.trainBatch(s, f), p = {
40
- epoch: s.epoch,
41
- loss: s.lastLoss,
42
- step: s.step,
43
- time: Date.now() - m,
44
- batchSize: f.xs.shape[0]
45
- };
46
- if (this.model.log.push(p), s.step % L === 0 && (await w, h)) {
47
- if (l) {
48
- const v = await g(this.tokenizer, this.model, l, 100, {
27
+ const d = Date.now(), m = await r.iterator();
28
+ try {
29
+ for (; !(s.lastLoss < n); ) {
30
+ const e = await m.next();
31
+ if (e.done) break;
32
+ const p = e.value, u = this.trainBatch(s, p), a = {
33
+ loss: s.lastLoss,
34
+ step: s.step,
35
+ time: Date.now() - d,
36
+ batchSize: p.xs.shape[0]
37
+ };
38
+ if (this.model.log.push(a), s.step % h === 0) {
39
+ if (await u, o)
40
+ try {
41
+ const t = await this.evaluateOnDataset(o, 5);
42
+ s.validationLosses.push(t), a.valLoss = t;
43
+ } catch (t) {
44
+ console.error("Validation error:", t);
45
+ }
46
+ if (l) {
47
+ if (c) {
48
+ const t = await L(this.tokenizer, this.model, c, 100, {
49
49
  temperature: 0.8
50
50
  });
51
- p.example = v;
51
+ a.example = t;
52
52
  }
53
- await h(p);
53
+ await l(a);
54
54
  }
55
55
  }
56
- } catch (e) {
57
- throw console.error("Training error:", e), this.tf.dispose(), e;
58
56
  }
59
- const d = s.epochLoss / s.step;
60
- if (t)
61
- try {
62
- const e = await this.evaluateOnDataset(t, 5);
63
- s.validationLosses.push(e), o && await o(s.epoch, d, e);
64
- } catch (e) {
65
- console.error("Validation error:", e);
66
- }
67
- else
68
- o && o(s.epoch, d);
69
- if (this.tf.dispose(), s.lastLoss < c)
70
- break;
57
+ } catch (e) {
58
+ throw console.error("Training error:", e), this.tf.dispose(), e;
71
59
  }
72
- return { losses: s.losses, validationLosses: s.validationLosses };
60
+ return this.tf.dispose(), { losses: s.losses, validationLosses: s.validationLosses };
73
61
  }
74
62
  }
75
63
  export {
76
- S as default
64
+ g as default
77
65
  };
@@ -1,111 +1,90 @@
1
- import { generateText as v } from "../utilities/generate.js";
2
- import T from "./Trainer.js";
3
- import { schedule as k } from "./lwSchedule.js";
4
- const x = {
5
- epochs: 1,
6
- stepsPerEpoch: 1e6,
1
+ import { generateText as d } from "../utilities/generate.js";
2
+ import S from "./Trainer.js";
3
+ import { schedule as u } from "./lwSchedule.js";
4
+ const w = {
7
5
  desiredLoss: 0.01,
8
6
  logInterval: 1,
9
7
  stepsPerLayer: 400,
10
8
  maxPasses: 3
11
9
  };
12
- class D extends T {
10
+ class b extends S {
13
11
  trainingPattern = [];
14
12
  startPass = 0;
15
13
  startLayer = 0;
16
- constructor(o, e, t, h = 3e-4) {
17
- if (super(o, e, t, h), this.trainingPattern = k[e.config.nLayer - 1] || [], e.log.length > 0) {
18
- const r = e.log[e.log.length - 1];
19
- r.pass !== void 0 && r.layer !== void 0 && (this.startPass = r.pass, this.startLayer = r.layer, console.log(`Resuming training from pass ${this.startPass}, layer ${this.startLayer}`));
14
+ constructor(r, a, e, p = 3e-4) {
15
+ if (super(r, a, e, p), this.trainingPattern = u[a.config.nLayer - 1] || [], a.log.length > 0) {
16
+ const i = a.log[a.log.length - 1];
17
+ i.pass !== void 0 && i.layer !== void 0 && (this.startPass = i.pass, this.startLayer = i.layer, console.log(`Resuming training from pass ${this.startPass}, layer ${this.startLayer}`));
20
18
  }
21
19
  }
22
- applyTrainingPattern(o) {
23
- const e = o < this.trainingPattern.length ? o : this.trainingPattern.length - 1, t = this.trainingPattern[e];
24
- this.model.setSkipMask(t.skip), this.model.setTrainableMask(t.trainable), this.resetOptimizer(t.adam), console.log("Applied training pattern:", e, t);
20
+ applyTrainingPattern(r) {
21
+ const a = r < this.trainingPattern.length ? r : this.trainingPattern.length - 1, e = this.trainingPattern[a];
22
+ this.model.setSkipMask(e.skip), this.model.setTrainableMask(e.trainable), this.resetOptimizer(e.adam), console.log("Applied training pattern:", a, e);
25
23
  }
26
24
  // Train for multiple epochs using Dataset API - FIXED memory leaks
27
- async trainOnDataset(o, e, t) {
28
- const {
29
- epochs: h,
30
- stepsPerEpoch: r,
31
- desiredLoss: c,
32
- logInterval: P,
33
- stepsPerLayer: d,
34
- onLayerChange: n,
35
- onPassComplete: g,
36
- onStep: y,
37
- onEpoch: p,
38
- prompt: L
39
- } = {
40
- ...x,
41
- ...e
42
- }, s = {
43
- epoch: 0,
25
+ async trainOnDataset(r, a, e) {
26
+ const { desiredLoss: p, logInterval: i, stepsPerLayer: L, onLayerChange: l, onPassComplete: h, onStep: c, prompt: g } = {
27
+ ...w,
28
+ ...a
29
+ }, t = {
44
30
  pass: 0,
45
31
  layerStep: 0,
46
32
  step: 0,
47
33
  stepSinceLayerChange: 0,
48
34
  lastLoss: 1e6,
49
- epochLoss: 0,
50
35
  totalSteps: 0,
51
36
  losses: [],
52
37
  validationLosses: []
53
38
  };
54
39
  this.dummyPass();
55
- const S = Date.now();
56
- for (s.epoch = 0; s.epoch < h; s.epoch++) {
57
- s.step = 0, s.epochLoss = 0, s.pass = this.startPass, s.layerStep = this.startLayer + this.startPass * this.model.config.nLayer, s.stepSinceLayerChange = 0, this.startPass = 0, this.startLayer = 0;
58
- const u = await o.iterator();
59
- this.applyTrainingPattern(s.layerStep % this.trainingPattern.length);
60
- try {
61
- for (; !(r && s.step >= r || s.lastLoss < c); ) {
62
- const a = await u.next();
63
- if (a.done) break;
64
- const m = a.value, w = this.trainBatch(s, m);
65
- s.stepSinceLayerChange++;
66
- const l = {
67
- epoch: s.epoch,
68
- loss: s.lastLoss,
69
- step: s.step,
70
- time: Date.now() - S,
71
- batchSize: m.xs.shape[0],
72
- pass: s.pass,
73
- layer: s.layerStep % this.model.config.nLayer
74
- };
75
- if (this.model.log.push(l), s.step % P === 0 && (await w, y)) {
76
- if (L) {
77
- const i = await v(this.tokenizer, this.model, L, 100, {
40
+ const f = Date.now();
41
+ this.startPass = 0, this.startLayer = 0;
42
+ const m = await r.iterator();
43
+ this.applyTrainingPattern(t.layerStep % this.trainingPattern.length);
44
+ try {
45
+ for (; !(t.lastLoss < p); ) {
46
+ const n = await m.next();
47
+ if (n.done) break;
48
+ const y = n.value, P = this.trainBatch(t, y);
49
+ t.stepSinceLayerChange++;
50
+ const o = {
51
+ loss: t.lastLoss,
52
+ step: t.step,
53
+ time: Date.now() - f,
54
+ batchSize: y.xs.shape[0],
55
+ pass: t.pass,
56
+ layer: t.layerStep % this.model.config.nLayer
57
+ };
58
+ if (this.model.log.push(o), t.step % i === 0) {
59
+ if (await P, e)
60
+ try {
61
+ const s = await this.evaluateOnDataset(e, 5);
62
+ t.validationLosses.push(s), o.valLoss = s;
63
+ } catch (s) {
64
+ console.error("Validation error:", s);
65
+ }
66
+ if (c) {
67
+ if (g) {
68
+ const s = await d(this.tokenizer, this.model, g, 100, {
78
69
  temperature: 0.8,
79
70
  topK: 10
80
71
  });
81
- l.example = i;
72
+ o.example = s;
82
73
  }
83
- await y(l);
84
- }
85
- if (s.stepSinceLayerChange >= d) {
86
- let i;
87
- t && (i = await this.evaluateOnDataset(t, 5), s.validationLosses.push(i), l.valLoss = i), s.layerStep++, s.layerStep % this.model.config.nLayer === 0 ? (n && await n(s.layerStep, s.pass, i), g && await g(s.pass), s.pass++) : n && await n(s.layerStep, s.pass, i), s.stepSinceLayerChange = 0, this.applyTrainingPattern(s.layerStep % this.trainingPattern.length);
74
+ await c(o);
88
75
  }
89
76
  }
90
- } catch (a) {
91
- throw console.error("Training error:", a), this.tf.dispose(), a;
92
- }
93
- const f = s.epochLoss / s.step;
94
- if (t)
95
- try {
96
- const a = await this.evaluateOnDataset(t, 5);
97
- s.validationLosses.push(a), p && await p(s.epoch, f, a);
98
- } catch (a) {
99
- console.error("Validation error:", a);
77
+ if (t.stepSinceLayerChange >= L) {
78
+ let s;
79
+ e && (s = await this.evaluateOnDataset(e, 5), t.validationLosses.push(s), o.valLoss = s), t.layerStep++, t.layerStep % this.model.config.nLayer === 0 ? (l && await l(t.layerStep, t.pass, s), h && await h(t.pass), t.pass++) : l && await l(t.layerStep, t.pass, s), t.stepSinceLayerChange = 0, this.applyTrainingPattern(t.layerStep % this.trainingPattern.length);
100
80
  }
101
- else
102
- p && p(s.epoch, f);
103
- if (this.tf.dispose(), s.lastLoss < c)
104
- break;
81
+ }
82
+ } catch (n) {
83
+ throw console.error("Training error:", n), this.tf.dispose(), n;
105
84
  }
106
- return { losses: s.losses, validationLosses: s.validationLosses };
85
+ return this.tf.dispose(), { losses: t.losses, validationLosses: t.validationLosses };
107
86
  }
108
87
  }
109
88
  export {
110
- D as default
89
+ b as default
111
90
  };
@@ -4,10 +4,8 @@ import { default as NanoGPT, TrainingLogEntry } from '../NanoGPTModel';
4
4
  import { default as TF } from '@tensorflow/tfjs';
5
5
  import { default as AdamExt } from './AdamExt';
6
6
  export interface TrainingState {
7
- epoch: number;
8
7
  step: number;
9
8
  lastLoss: number;
10
- epochLoss: number;
11
9
  totalSteps: number;
12
10
  losses: number[];
13
11
  validationLosses: number[];
@@ -19,12 +17,9 @@ export interface AdamConfig {
19
17
  epsilon: number;
20
18
  }
21
19
  export interface TrainingOptions {
22
- epochs: number;
23
- stepsPerEpoch: number;
24
20
  desiredLoss: number;
25
21
  logInterval: number;
26
22
  prompt?: string;
27
- onEpoch?: (e: number, loss: number, valLoss?: number) => Promise<void> | void;
28
23
  onStep?: (log: TrainingLogEntry) => Promise<void> | void;
29
24
  }
30
25
  export default abstract class GPTTrainer {
@@ -57,7 +57,7 @@ class y {
57
57
  async trainBatch(t, e) {
58
58
  try {
59
59
  const s = this.trainStep(e, !1, !1);
60
- return e.xs.dispose(), e.ys.dispose(), t.step++, t.totalSteps++, s.array().then((a) => (t.lastLoss = a, t.losses.push(t.lastLoss), t.epochLoss += t.lastLoss, s.dispose(), t.lastLoss));
60
+ return e.xs.dispose(), e.ys.dispose(), t.step++, t.totalSteps++, s.array().then((a) => (t.lastLoss = a, t.losses.push(t.lastLoss), s.dispose(), t.lastLoss));
61
61
  } catch (s) {
62
62
  throw console.error(`Error processing batch at step ${t.step}:`, s), this.tf.dispose(), s;
63
63
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@genai-fi/nanogpt",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "main": "dist/main.js",
6
6
  "types": "dist/main.d.ts",
@@ -23,7 +23,7 @@
23
23
  "test": "vitest",
24
24
  "ci:test": "vitest --coverage --reporter=junit --outputFile=junit.xml",
25
25
  "coverage": "vitest run --coverage",
26
- "train": "tsx scripts/train.ts --epochs 2 --batch 64",
26
+ "train": "tsx scripts/train.ts --batch 64",
27
27
  "generate": "tsx scripts/generate.ts",
28
28
  "evaluate": "tsx scripts/evaluate.ts",
29
29
  "debug": "tsx scripts/debug.ts"