@genai-fi/nanogpt 1.2.5 → 1.4.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/api/responses.js +13 -10
- package/dist/api/training.js +1 -1
- package/dist/inference/Generator.js +3 -1
- package/dist/inference/tokenisePrompt.js +1 -1
- package/dist/inference/types.d.ts +2 -0
- package/dist/training/BasicTrainer.d.ts +7 -0
- package/dist/training/BasicTrainer.js +19 -10
- package/package.json +1 -1
package/dist/api/responses.js
CHANGED
|
@@ -29,13 +29,10 @@ var r = class {
|
|
|
29
29
|
}
|
|
30
30
|
async _runForRecord(e, t, n) {
|
|
31
31
|
let r = t.previous_response_id ? this._responses.get(t.previous_response_id) : void 0;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
content: t.input
|
|
37
|
-
}] : t.input = e;
|
|
38
|
-
}
|
|
32
|
+
r && (t.input && Array.isArray(t.input) || t.input && typeof t.input == "string" && (t.input = [{
|
|
33
|
+
role: t.nonConversational ? "text" : "user",
|
|
34
|
+
content: t.input
|
|
35
|
+
}]));
|
|
39
36
|
let i = r ? r.generator : this._responses.get(e).generator, a = n ? () => n({
|
|
40
37
|
output: i.getConversation(),
|
|
41
38
|
id: e,
|
|
@@ -63,11 +60,17 @@ var r = class {
|
|
|
63
60
|
done: !1
|
|
64
61
|
};
|
|
65
62
|
return c.then((n) => {
|
|
66
|
-
o.done = !0, t.done = !0, t.output = n, this._hookedResponses.delete(e), this._resumeWaiters.delete(e)
|
|
63
|
+
o.done = !0, t.done = !0, t.output = n, this._hookedResponses.delete(e), this._resumeWaiters.delete(e);
|
|
64
|
+
let r = n[0]?._id || e;
|
|
65
|
+
n.forEach((e) => {
|
|
66
|
+
e._id === void 0 && (e._id = r);
|
|
67
|
+
}), a && i.off("tokens", a), this.ee.emit("done", e), this._processNextJob();
|
|
67
68
|
}), t;
|
|
68
69
|
}
|
|
69
|
-
let l = await c;
|
|
70
|
-
return
|
|
70
|
+
let l = await c, u = l[0]?._id || e;
|
|
71
|
+
return l.forEach((e) => {
|
|
72
|
+
e._id === void 0 && (e._id = u);
|
|
73
|
+
}), a && i.off("tokens", a), o.done = !0, this._hookedResponses.delete(e), this._resumeWaiters.delete(e), this.ee.emit("done", e), this._processNextJob(), {
|
|
71
74
|
output: l,
|
|
72
75
|
id: e,
|
|
73
76
|
done: !0
|
package/dist/api/training.js
CHANGED
|
@@ -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");
|
|
@@ -187,7 +187,9 @@ var y = class extends e {
|
|
|
187
187
|
this.reset();
|
|
188
188
|
}
|
|
189
189
|
initialise(e, n) {
|
|
190
|
-
if (this.cache && n?.noCache && this.reset(), this.initialPrompt = e || null, this.lastToken === -1 ? this.outputConversation =
|
|
190
|
+
if (this.cache && n?.noCache && this.reset(), this.initialPrompt = e || null, this.lastToken === -1 && this.outputConversation.length === 0 ? this.outputConversation = this.initialPrompt || [] : e && e.length > 0 && (e.forEach((e) => {
|
|
191
|
+
this.outputConversation.push(e);
|
|
192
|
+
}), this.resetCache()), !this.cache && !n?.noCache && (this.model.config.modelType !== "GenAI_NanoGPT_v1" || this.model.config.useRope)) {
|
|
191
193
|
let e = Array(this.model.config.nLayer);
|
|
192
194
|
for (let t = 0; t < this.model.config.nLayer; t++) e[t] = {
|
|
193
195
|
k: void 0,
|
|
@@ -3,7 +3,7 @@ import { I as e } from "../dist-Da20xy8E.js";
|
|
|
3
3
|
async function t(t, n, r, i) {
|
|
4
4
|
if (r) {
|
|
5
5
|
let a = r.length > 0 && (r[r.length - 1].role === "text" || r[r.length - 1].role === "assistant"), o;
|
|
6
|
-
return i?.nonConversational ? o = a && i?.continuation ? [t.bosToken, ...t.encode(r[r.length - 1].content)] : t.encodeAsSequence(r, !0) : a && i?.continuation ? (o = t.encodeConversation(r, !0), o[o.length - 1] === t.getSpecialTokenIndex("<|assistant_start|>") && o[o.length - 2] === t.getSpecialTokenIndex("<|assistant_end|>") && (o.pop(), o.pop())) : o = t.encodeConversation(r, !0), o.length > n && (o = o.slice(-n)), e([o], [1, o.length], "int32");
|
|
6
|
+
return i?.nonConversational ? o = a && i?.continuation ? [t.bosToken, ...t.encode(r[r.length - 1].content)] : t.encodeAsSequence(r, !0) : a && i?.continuation ? (o = t.encodeConversation(r, !0), o[o.length - 1] === t.getSpecialTokenIndex("<|assistant_start|>") && o[o.length - 2] === t.getSpecialTokenIndex("<|assistant_end|>") && (o.pop(), o.pop())) : o = t.encodeConversation(r, !0), o.length > n && (o = o.slice(-n)), i?.debug && console.log("Tokenised prompt:", t.decode(o)), e([o], [1, o.length], "int32");
|
|
7
7
|
} else {
|
|
8
8
|
let n = i?.nonConversational ? void 0 : t.getSpecialTokenIndex("<|assistant_start|>"), r = n === void 0 ? [t.bosToken] : [t.bosToken, n];
|
|
9
9
|
return e([r], [1, r.length], "int32");
|
|
@@ -17,6 +17,7 @@ export interface IGeneratorOutputInternal extends IGeneratorOutput {
|
|
|
17
17
|
outputTensor: Tensor;
|
|
18
18
|
}
|
|
19
19
|
export interface GeneratorConversation extends Conversation {
|
|
20
|
+
_id?: string;
|
|
20
21
|
_completed?: boolean;
|
|
21
22
|
_timestamp?: number;
|
|
22
23
|
_output?: IGeneratorOutput[];
|
|
@@ -45,6 +46,7 @@ export interface IGenerateOptions {
|
|
|
45
46
|
continuation?: boolean; /** Continue the previous response without adding a new turn. */
|
|
46
47
|
chunkSize?: number; /** Number of tokens to generate before returning a partial response. */
|
|
47
48
|
background?: boolean; /** Run the generation in the background and return immediately. */
|
|
49
|
+
debug?: boolean;
|
|
48
50
|
_onChunk?: (output: IGeneratorOutput) => void | Promise<void>;
|
|
49
51
|
}
|
|
50
52
|
export interface IGeneratorResponse {
|
|
@@ -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
|
-
|
|
215
|
-
let {
|
|
214
|
+
prepareToTrain(e) {
|
|
215
|
+
let { maxEpochs: t = Infinity } = {
|
|
216
216
|
...p,
|
|
217
|
-
...
|
|
217
|
+
...e
|
|
218
218
|
};
|
|
219
219
|
this.log.length > 0 && this.resumeFromLog(this.log[this.log.length - 1]);
|
|
220
|
-
let
|
|
221
|
-
|
|
222
|
-
let
|
|
223
|
-
if (this.lastState =
|
|
224
|
-
losses:
|
|
225
|
-
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
|
-
|
|
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;) {
|