@genai-fi/nanogpt 1.2.4 → 1.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.
@@ -64,7 +64,7 @@ export default class Training {
64
64
  getJob(id: string): ITrainingJob | null;
65
65
  /** Resume a paused job and optionally change some options. */
66
66
  resume(id: string): Promise<void>;
67
- cancel(id: string): void;
67
+ cancel(id?: string): void;
68
68
  addBreak(jobid: string): number | undefined;
69
69
  deleteBreak(jobid: string, breakId: number): void;
70
70
  getPretrainingJob(): ITrainingJob | null;
@@ -108,7 +108,7 @@ var o = class {
108
108
  return s.trainer.log = t, s.trainer.resumeFromLog(t[t.length - 1]), this._jobs.set(s.id, s), s;
109
109
  }
110
110
  launchJob(e) {
111
- e.trainDataset && (this.setState(e, "running"), e.trainer.trainOnDataset(e.trainDataset, e.options, e.validationDataset, (t) => {
111
+ e.trainDataset && (this.setState(e, "running"), e.trainer.prepareToTrain(e.options), e.trainer.trainOnDataset(e.trainDataset, e.options, e.validationDataset, (t) => {
112
112
  e.history = e.trainer.log, e.progress = t.totalTokens / e.totalTokens, e.remaining = Math.max(0, (e.totalTokens - t.totalTokens) / t.totalTokens * t.duration), e.breakOnLog.size > 0 && e.state === "running" && (this.setState(e, "pausing"), e.trainer.stop()), this.ee.emit("progress", e);
113
113
  }).then(() => {
114
114
  e.state === "running" ? this.setState(e, "completed") : e.state === "pausing" ? this.setState(e, "paused") : e.state === "cancelling" && this.setState(e, "cancelled");
@@ -180,12 +180,15 @@ var o = class {
180
180
  t && t.state === "paused" && (t.resumeCounter++, !(t.resumeCounter < t.breakOnLog.size) && (t.resumeCounter = 0, this.setState(t, "pending"), this.launchJob(t)));
181
181
  }
182
182
  cancel(e) {
183
- this.assertValidJobId(e);
184
- let t = this.getJob(e);
185
- if (t) if (t.state === "paused") {
186
- this.setState(t, "cancelled");
187
- return;
188
- } else (t.state === "running" || t.state === "pausing") && (this.setState(t, "cancelling"), t.trainer.stop());
183
+ if (e) {
184
+ this.assertValidJobId(e);
185
+ let t = this.getJob(e);
186
+ if (!t) return;
187
+ if (t.state === "paused") {
188
+ this.setState(t, "cancelled");
189
+ return;
190
+ } else (t.state === "running" || t.state === "pausing") && (this.setState(t, "cancelling"), t.trainer.stop());
191
+ } else for (let e of this._jobs.values()) e.state === "paused" ? this.setState(e, "cancelled") : (e.state === "running" || e.state === "pausing") && (this.setState(e, "cancelling"), e.trainer.stop());
189
192
  }
190
193
  addBreak(e) {
191
194
  let t = this.getJob(e);
@@ -45,6 +45,13 @@ export default class BasicTrainer {
45
45
  dispose(): void;
46
46
  private createEmptyState;
47
47
  private performLogging;
48
+ /** A sync function to ensure the trainer is prepared and the state is initialized before training.
49
+ * It should be called before invoking trainOnDataset.
50
+ */
51
+ prepareToTrain(options: Partial<TrainingOptions>): {
52
+ losses: number[];
53
+ validationLosses: number[];
54
+ } | undefined;
48
55
  trainOnDataset(dataset: Dataset<{
49
56
  xs: Tensor;
50
57
  ys: Tensor;
@@ -211,22 +211,31 @@ var p = {
211
211
  }
212
212
  this.log.push(p), r && r(p), a.logStartTime = Date.now();
213
213
  }
214
- async trainOnDataset(e, t, n, i) {
215
- let { logInterval: a = 40, maxEpochs: o = Infinity } = {
214
+ prepareToTrain(e) {
215
+ let { maxEpochs: t = Infinity } = {
216
216
  ...p,
217
- ...t
217
+ ...e
218
218
  };
219
219
  this.log.length > 0 && this.resumeFromLog(this.log[this.log.length - 1]);
220
- let c = o * (t?.epochSteps || 1e3);
221
- t.metrics && this.setMetrics(t.metrics);
222
- let l = this.createEmptyState();
223
- if (this.lastState = l, l.step >= c) return {
224
- losses: l.losses,
225
- validationLosses: l.validationLosses
220
+ let n = t * (e?.epochSteps || 1e3);
221
+ e.metrics && this.setMetrics(e.metrics);
222
+ let r = this.createEmptyState();
223
+ if (this.lastState = r, r.step >= n) return {
224
+ losses: r.losses,
225
+ validationLosses: r.validationLosses
226
226
  };
227
+ this.running = !0;
228
+ }
229
+ async trainOnDataset(e, t, n, i) {
230
+ let { logInterval: a = 40, maxEpochs: o = Infinity } = {
231
+ ...p,
232
+ ...t
233
+ }, c = o * (t?.epochSteps || 1e3);
227
234
  await this.dummyPass(), t?.metrics?.includes("memoryUsage") && (this.model.getProfiler() || this.model.setProfiler(new s()));
235
+ let l = this.lastState;
236
+ if (!l) throw Error("Trainer state has not been initialized. Call prepareToTrain() first.");
228
237
  let d = Date.now();
229
- this.running = !0, l.logStartTime = d;
238
+ l.logStartTime = d;
230
239
  let f = d, m = n ? new u(this.model, n, this.maskedLoss) : void 0, h = await e.iterator(), g = h.next();
231
240
  try {
232
241
  for (; this.running;) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@genai-fi/nanogpt",
3
- "version": "1.2.4",
3
+ "version": "1.3.0",
4
4
  "type": "module",
5
5
  "main": "dist/main.js",
6
6
  "types": "dist/main.d.ts",