@genai-fi/nanogpt 0.24.0 → 1.0.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/README.md +78 -281
- package/dist/{RealDiv-CNsvC4AU.js → RealDiv-CSnvtN2E.js} +20 -20
- package/dist/{Reshape-dnm9bO3B.js → Reshape-BlylqwWy.js} +12 -12
- package/dist/TeachableLLM.d.ts +10 -14
- package/dist/TeachableLLM.js +201 -2
- package/dist/api/responses.d.ts +81 -0
- package/dist/api/responses.js +169 -0
- package/dist/api/training.d.ts +70 -0
- package/dist/api/training.js +205 -0
- package/dist/data/docx.js +9 -3036
- package/dist/data/stream.d.ts +2 -2
- package/dist/data/textLoader.d.ts +1 -1
- package/dist/data/textLoader.js +1 -1
- package/dist/data.d.ts +3 -0
- package/dist/data.js +12 -0
- package/dist/{dist-BqAU9-yi.js → dist-CwK5S7Ls.js} +2168 -2168
- package/dist/{gpgpu_math-DBYEAAdI.js → gpgpu_math-20tPK8LM.js} +458 -458
- package/dist/{Generator.d.ts → inference/Generator.d.ts} +16 -44
- package/dist/inference/Generator.js +271 -0
- package/dist/inference/tokenisePrompt.d.ts +4 -0
- package/dist/inference/tokenisePrompt.js +13 -0
- package/dist/inference/types.d.ts +44 -8
- package/dist/inference/utilities.d.ts +9 -0
- package/dist/inference/utilities.js +20 -0
- package/dist/jszip.min-DKa1Rjyn.js +3033 -0
- package/dist/{kernel_funcs_utils-D-mATnGR.js → kernel_funcs_utils-ql8Y8qPn.js} +96 -93
- package/dist/layers/MLP.d.ts +1 -1
- package/dist/layers/PositionEmbedding.d.ts +2 -1
- package/dist/layers/PositionEmbedding.js +1 -1
- package/dist/layers/RMSNorm.d.ts +1 -1
- package/dist/layers/TiedEmbedding.js +1 -1
- package/dist/layers.d.ts +4 -0
- package/dist/layers.js +14 -0
- package/dist/loader/load.js +58 -2
- package/dist/loader/loadHF.d.ts +1 -1
- package/dist/loader/loadHF.js +17 -2
- package/dist/loader/loadTransformers.js +46 -2
- package/dist/loader/newZipLoad.js +25 -2
- package/dist/loader/oldZipLoad.d.ts +1 -1
- package/dist/loader/oldZipLoad.js +37 -2
- package/dist/loader/save.js +75 -2
- package/dist/loader/types.d.ts +3 -3
- package/dist/main.d.ts +34 -35
- package/dist/main.js +12327 -20
- package/dist/{matMulGelu-BAIgQaRx.js → matMulGelu-CBoqTZM7.js} +2 -2
- package/dist/models/NanoGPTV1.js +95 -2
- package/dist/models/NanoGPTV2.js +86 -2
- package/dist/models/factory.js +13 -2
- package/dist/models/model.js +76 -2
- package/dist/models.d.ts +4 -0
- package/dist/models.js +14 -0
- package/dist/ops/dot16.js +1 -1
- package/dist/ops/matMulGelu.js +1 -1
- package/dist/ops/webgl/adamAdjust.js +1 -1
- package/dist/ops/webgl/fusedSoftmax.js +2 -2
- package/dist/ops/webgl/gelu.js +2 -2
- package/dist/ops/webgl/log.js +5 -5
- package/dist/ops/webgl/matMulGelu.js +1 -1
- package/dist/ops/webgl/matMulMul.js +1 -1
- package/dist/{tfjs_backend-CydPRQTc.js → tfjs_backend-h5weiy1O.js} +36 -36
- package/dist/tokenise.d.ts +4 -0
- package/dist/tokenise.js +15 -0
- package/dist/training/BasicTrainer.d.ts +5 -10
- package/dist/training/BasicTrainer.js +80 -88
- package/dist/training/configure.d.ts +3 -0
- package/dist/training/configure.js +32 -0
- package/dist/training/factory.d.ts +6 -0
- package/dist/training/factory.js +8 -0
- package/dist/training/prepareData.d.ts +22 -0
- package/dist/training/prepareData.js +49 -0
- package/dist/training/tasks/tokenStream.d.ts +2 -1
- package/dist/training/types.d.ts +14 -1
- package/dist/training/validateOptions.d.ts +2 -0
- package/dist/training/validateOptions.js +19 -0
- package/dist/utilities/arrayShape.d.ts +1 -0
- package/dist/utilities/arrayShape.js +8 -0
- package/dist/utilities/waitForModel.d.ts +1 -1
- package/dist/v4-BK7K-jy_.js +30 -0
- package/package.json +8 -2
- package/dist/Generator.js +0 -2
- package/dist/Trainer-Cr7csbTD.js +0 -228
- package/dist/Trainer.d.ts +0 -45
- package/dist/Trainer.js +0 -2
- package/dist/main-Dz72vadm.js +0 -13267
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { t as e } from "../eventemitter3-D_qV3Lof.js";
|
|
2
|
+
import { TokenStore as t } from "../training/tasks/TokenStore.js";
|
|
3
|
+
import { t as n } from "../v4-BK7K-jy_.js";
|
|
4
|
+
import r from "../training/factory.js";
|
|
5
|
+
import i from "../training/prepareData.js";
|
|
6
|
+
import a from "../utilities/datasetID.js";
|
|
7
|
+
//#region lib/api/training.ts
|
|
8
|
+
var o = class {
|
|
9
|
+
ee;
|
|
10
|
+
_model;
|
|
11
|
+
_tokeniser;
|
|
12
|
+
_jobs = /* @__PURE__ */ new Map();
|
|
13
|
+
constructor(t, n) {
|
|
14
|
+
this.ee = new e(), this._model = t, this._tokeniser = n;
|
|
15
|
+
}
|
|
16
|
+
setState(e, t, n) {
|
|
17
|
+
let r = () => {
|
|
18
|
+
throw Error(`invalid_state_transition_from_${e.state}_to_${t}`);
|
|
19
|
+
};
|
|
20
|
+
switch (t) {
|
|
21
|
+
case "pending":
|
|
22
|
+
e.state !== "completed" && e.state !== "paused" && e.state !== "cancelled" && e.state !== "pending" && r();
|
|
23
|
+
break;
|
|
24
|
+
case "running":
|
|
25
|
+
e.state !== "pending" && e.state !== "paused" && r(), this.ee.emit("running", e.id);
|
|
26
|
+
break;
|
|
27
|
+
case "paused":
|
|
28
|
+
e.state !== "pausing" && r(), this.ee.emit("paused", e.id);
|
|
29
|
+
break;
|
|
30
|
+
case "completed":
|
|
31
|
+
e.state !== "running" && r(), this.ee.emit("completed", e.id);
|
|
32
|
+
break;
|
|
33
|
+
case "pausing":
|
|
34
|
+
e.state !== "running" && r(), this.ee.emit("pausing", e.id);
|
|
35
|
+
break;
|
|
36
|
+
case "cancelling":
|
|
37
|
+
e.state !== "running" && e.state !== "paused" && e.state !== "pausing" && r(), this.ee.emit("cancelling", e.id);
|
|
38
|
+
break;
|
|
39
|
+
case "cancelled":
|
|
40
|
+
e.state !== "paused" && e.state !== "cancelling" && r(), this.ee.emit("cancelled", e.id);
|
|
41
|
+
break;
|
|
42
|
+
case "error":
|
|
43
|
+
n && this.ee.emit("error", e.id, n);
|
|
44
|
+
break;
|
|
45
|
+
default: throw Error("invalid_state");
|
|
46
|
+
}
|
|
47
|
+
e.state = t;
|
|
48
|
+
}
|
|
49
|
+
get activeJobs() {
|
|
50
|
+
return this._jobs.values().reduce((e, t) => t.state === "running" ? e + 1 : e, 0);
|
|
51
|
+
}
|
|
52
|
+
get training() {
|
|
53
|
+
return this.activeJobs > 0;
|
|
54
|
+
}
|
|
55
|
+
assertValidJobId(e) {
|
|
56
|
+
if (typeof e != "string" || e.trim().length === 0) throw Error("invalid_job_id");
|
|
57
|
+
}
|
|
58
|
+
assertValidOptions(e) {
|
|
59
|
+
if (!Number.isInteger(e.batchSize) || e.batchSize <= 0) throw Error("invalid_batch_size");
|
|
60
|
+
if (e.maxEpochs !== void 0 && (!Number.isFinite(e.maxEpochs) || e.maxEpochs <= 0)) throw Error("invalid_max_epochs");
|
|
61
|
+
if (e.logInterval !== void 0 && (!Number.isInteger(e.logInterval) || e.logInterval <= 0)) throw Error("invalid_log_interval");
|
|
62
|
+
if (e.learningRate !== void 0 && (!Number.isFinite(e.learningRate) || e.learningRate <= 0)) throw Error("invalid_learning_rate");
|
|
63
|
+
if (e.validationSplit !== void 0 && (!Number.isFinite(e.validationSplit) || e.validationSplit <= 0 || e.validationSplit >= 1)) throw Error("invalid_validation_split");
|
|
64
|
+
if (e.dropout !== void 0 && (!Number.isFinite(e.dropout) || e.dropout < 0 || e.dropout > 1)) throw Error("invalid_dropout");
|
|
65
|
+
if (e.layerDrop !== void 0 && (!Number.isFinite(e.layerDrop) || e.layerDrop < 0 || e.layerDrop > 1)) throw Error("invalid_layer_drop");
|
|
66
|
+
if (e.labelSmoothing !== void 0 && (!Number.isFinite(e.labelSmoothing) || e.labelSmoothing < 0 || e.labelSmoothing > 1)) throw Error("invalid_label_smoothing");
|
|
67
|
+
}
|
|
68
|
+
assertValidDatasets(e) {
|
|
69
|
+
if (e.length === 0) throw Error("invalid_datasets");
|
|
70
|
+
let t = /* @__PURE__ */ new Set();
|
|
71
|
+
for (let n of e) {
|
|
72
|
+
if (!n.id.trim()) throw Error("invalid_dataset_id");
|
|
73
|
+
if (!n.name.trim()) throw Error("invalid_dataset_name");
|
|
74
|
+
if (t.has(n.id)) throw Error("duplicate_dataset_id");
|
|
75
|
+
t.add(n.id);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
assertValidDataInput(e) {
|
|
79
|
+
if (e instanceof t) {
|
|
80
|
+
if (e.tokeniserId !== this._tokeniser.id) throw Error("tokeniser_mismatch");
|
|
81
|
+
if (e.getTokenCount() === 0) throw Error("empty_training_data");
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (e.length === 0) throw Error("empty_training_data");
|
|
85
|
+
}
|
|
86
|
+
on(e, t) {
|
|
87
|
+
this.ee.on(e, t);
|
|
88
|
+
}
|
|
89
|
+
off(e, t) {
|
|
90
|
+
this.ee.off(e, t);
|
|
91
|
+
}
|
|
92
|
+
restore(e, t, i, o) {
|
|
93
|
+
if (this.assertValidOptions(e), this.assertValidDatasets(o), t.length === 0) throw Error("invalid_log");
|
|
94
|
+
let s = {
|
|
95
|
+
id: n(),
|
|
96
|
+
state: "completed",
|
|
97
|
+
trainer: r(this._model, this._tokeniser, e, i),
|
|
98
|
+
history: t,
|
|
99
|
+
progress: t[t.length - 1]?.totalTokens / t[t.length - 1]?.duration || 0,
|
|
100
|
+
remaining: 0,
|
|
101
|
+
options: e,
|
|
102
|
+
totalTokens: t[t.length - 1]?.totalTokens || 0,
|
|
103
|
+
datasets: o,
|
|
104
|
+
datasetId: a(o),
|
|
105
|
+
breakOnLog: !1
|
|
106
|
+
};
|
|
107
|
+
return s.trainer.log = t, s.trainer.resumeFromLog(t[t.length - 1]), this._jobs.set(s.id, s), s;
|
|
108
|
+
}
|
|
109
|
+
launchJob(e) {
|
|
110
|
+
e.trainDataset && (this.setState(e, "running"), e.trainer.trainOnDataset(e.trainDataset, e.options, e.validationDataset, (t) => {
|
|
111
|
+
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 && e.state === "running" && (this.setState(e, "pausing"), e.trainer.stop()), this.ee.emit("progress", e);
|
|
112
|
+
}).then(() => {
|
|
113
|
+
e.state === "running" ? this.setState(e, "completed") : e.state === "pausing" ? this.setState(e, "paused") : e.state === "cancelling" && this.setState(e, "cancelled");
|
|
114
|
+
}).catch((t) => {
|
|
115
|
+
this.setState(e, "error", t);
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
async job(e, a = [], o, s) {
|
|
119
|
+
if (this.assertValidOptions(e), this.assertValidDataInput(a), this.assertValidDatasets(o), s && s instanceof t && s.tokeniserId !== this._tokeniser.id) throw Error("tokeniser_mismatch");
|
|
120
|
+
let c = Array.from(this._jobs.keys());
|
|
121
|
+
for (let t of c) {
|
|
122
|
+
let n = this._jobs.get(t);
|
|
123
|
+
if (n && n.id !== e.previous_job_id) {
|
|
124
|
+
if (n.options.method.type === e.method.type && n.state !== "running") n.trainer.dispose(), this._jobs.delete(n.id);
|
|
125
|
+
else if (n.options.method.type === e.method.type && n.state === "running") throw Error("training_in_progress");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
let l = e.previous_job_id ? this.getJob(e.previous_job_id) : {
|
|
129
|
+
id: n(),
|
|
130
|
+
state: "pending",
|
|
131
|
+
trainer: r(this._model, this._tokeniser, e),
|
|
132
|
+
history: [],
|
|
133
|
+
progress: 0,
|
|
134
|
+
remaining: 0,
|
|
135
|
+
options: e,
|
|
136
|
+
totalTokens: 0,
|
|
137
|
+
datasets: o,
|
|
138
|
+
breakOnLog: !1
|
|
139
|
+
};
|
|
140
|
+
if (!l) throw Error("invalid_previous_job_id");
|
|
141
|
+
if (this._jobs.set(l.id, l), this.setState(l, "pending"), a instanceof t) {
|
|
142
|
+
if (l.datasetId && a.datasetId !== l.datasetId) {
|
|
143
|
+
let e = /* @__PURE__ */ Error("dataset_mismatch");
|
|
144
|
+
throw this.setState(l, "error", e), e;
|
|
145
|
+
}
|
|
146
|
+
if (a.tokeniserId !== this._tokeniser.id) {
|
|
147
|
+
let e = /* @__PURE__ */ Error("tokeniser_mismatch");
|
|
148
|
+
throw this.setState(l, "error", e), e;
|
|
149
|
+
}
|
|
150
|
+
l.datasetId = a.datasetId;
|
|
151
|
+
}
|
|
152
|
+
if (!l.trainDataset) try {
|
|
153
|
+
let t = await i(e, this._model, this._tokeniser, a, s, o);
|
|
154
|
+
l.trainDataset = t.trainDataset, l.validationDataset = t.validationDataset, l.totalTokens = t.totalTokens;
|
|
155
|
+
} catch (e) {
|
|
156
|
+
throw this.setState(l, "error", /* @__PURE__ */ Error("prepare_data_failed")), l.trainer.dispose(), this._jobs.delete(l.id), e;
|
|
157
|
+
}
|
|
158
|
+
if (e.previous_job_id) {
|
|
159
|
+
this.assertValidOptions(e), l.options = e;
|
|
160
|
+
try {
|
|
161
|
+
l.trainer.configure(e);
|
|
162
|
+
} catch (e) {
|
|
163
|
+
throw this.setState(l, "error", /* @__PURE__ */ Error("trainer_configuration_failed")), l.trainer.dispose(), this._jobs.delete(l.id), e;
|
|
164
|
+
}
|
|
165
|
+
} else try {
|
|
166
|
+
l.trainer.configure(e);
|
|
167
|
+
} catch (e) {
|
|
168
|
+
throw this.setState(l, "error", /* @__PURE__ */ Error("trainer_configuration_failed")), l.trainer.dispose(), this._jobs.delete(l.id), e;
|
|
169
|
+
}
|
|
170
|
+
return this.launchJob(l), l;
|
|
171
|
+
}
|
|
172
|
+
getJob(e) {
|
|
173
|
+
return this.assertValidJobId(e), this._jobs.get(e) ?? null;
|
|
174
|
+
}
|
|
175
|
+
async resume(e) {
|
|
176
|
+
this.assertValidJobId(e);
|
|
177
|
+
let t = this.getJob(e);
|
|
178
|
+
t && t.state === "paused" && (this.setState(t, "pending"), this.launchJob(t));
|
|
179
|
+
}
|
|
180
|
+
cancel(e) {
|
|
181
|
+
this.assertValidJobId(e);
|
|
182
|
+
let t = this.getJob(e);
|
|
183
|
+
if (t) if (t.state === "paused") {
|
|
184
|
+
this.setState(t, "cancelled");
|
|
185
|
+
return;
|
|
186
|
+
} else (t.state === "running" || t.state === "pausing") && (this.setState(t, "cancelling"), t.trainer.stop());
|
|
187
|
+
}
|
|
188
|
+
breakpoints(e, t) {
|
|
189
|
+
let n = this.getJob(e);
|
|
190
|
+
n && (n.breakOnLog = t);
|
|
191
|
+
}
|
|
192
|
+
getPretrainingJob() {
|
|
193
|
+
for (let e of this._jobs.values()) if (e.options.method.type === "pretraining") return e;
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
dispose() {
|
|
197
|
+
for (let e of this._jobs.values()) {
|
|
198
|
+
if (e.state === "running") throw Error(`Cannot dispose training while job ${e.id} is still running`);
|
|
199
|
+
e.trainer.dispose();
|
|
200
|
+
}
|
|
201
|
+
this._jobs.clear(), this.ee.removeAllListeners();
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
//#endregion
|
|
205
|
+
export { o as default };
|