@genai-fi/nanogpt 0.1.5 → 0.1.6

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.
@@ -5,7 +5,7 @@ export declare class DatasetBuilder {
5
5
  blockSize: number;
6
6
  private tf;
7
7
  constructor(tf: typeof TF, tokenizer: ITokeniser, blockSize?: number);
8
- createTextDataset(textData: string[], batchSize?: number): Promise<TF.data.Dataset<{
8
+ createTextDataset(textData: string[], batchSize?: number, start?: number, end?: number): Promise<TF.data.Dataset<{
9
9
  xs: TF.Tensor;
10
10
  ys: TF.Tensor;
11
11
  }>>;
@@ -1,19 +1,22 @@
1
- class l {
1
+ class z {
2
2
  tokenizer;
3
3
  blockSize;
4
4
  tf;
5
- constructor(s, i, o = 128) {
6
- this.tokenizer = i, this.blockSize = o, this.tf = s;
5
+ constructor(s, o, i = 128) {
6
+ this.tokenizer = o, this.blockSize = i, this.tf = s;
7
7
  }
8
8
  // Create dataset from text files
9
- async createTextDataset(s, i = 32) {
10
- const o = await Promise.all(s.map((t) => this.tokenizer.encode(t))), a = this.tokenizer.eosToken >= 0, n = o.map((t) => a ? [...t, this.tokenizer.eosToken] : t).flat(), c = (function* () {
9
+ async createTextDataset(s, o = 32, i = 0, c = 1) {
10
+ const n = await Promise.all(s.map((t) => this.tokenizer.encode(t))), h = this.tokenizer.eosToken >= 0, a = n.map((t) => h ? [...t, this.tokenizer.eosToken] : t).flat().slice(
11
+ Math.floor(i * n.length),
12
+ c === 1 ? void 0 : Math.floor(c * n.length)
13
+ ), r = (function* () {
11
14
  for (; ; ) {
12
- const t = Math.floor(Math.random() * (n.length - this.blockSize - 1)), e = n.slice(t, t + this.blockSize), r = n.slice(t + 1, t + this.blockSize + 1);
13
- yield { xs: e, ys: r };
15
+ const t = Math.floor(Math.random() * (a.length - this.blockSize - 1)), e = a.slice(t, t + this.blockSize), l = a.slice(t + 1, t + this.blockSize + 1);
16
+ yield { xs: e, ys: l };
14
17
  }
15
18
  }).bind(this);
16
- return this.tf.data.generator(c).batch(i).map((t) => {
19
+ return this.tf.data.generator(r).batch(o).map((t) => {
17
20
  const e = t;
18
21
  return this.tf.tidy(() => ({
19
22
  xs: e.xs.cast("int32"),
@@ -23,5 +26,5 @@ class l {
23
26
  }
24
27
  }
25
28
  export {
26
- l as DatasetBuilder
29
+ z as DatasetBuilder
27
30
  };
@@ -1,8 +1,8 @@
1
1
  import { DatasetBuilder as d } from "./DatasetBuilder.js";
2
2
  import p from "./AdamExt.js";
3
- class g {
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);
3
+ class u {
4
+ constructor(t, s, e, i = 1e-3) {
5
+ this.tokenizer = e, this.tf = t, this.model = s, this.learningRate = i, this.resetOptimizer(), this.datasetBuilder = new d(this.tf, e, s.config.blockSize);
6
6
  }
7
7
  model;
8
8
  optimizer;
@@ -21,7 +21,7 @@ class g {
21
21
  }
22
22
  resetOptimizer(t = { learningRateFactor: 1, beta1: 0.9, beta2: 0.99, epsilon: 1e-8 }) {
23
23
  this.optimizer && this.optimizer.dispose();
24
- const e = new p(
24
+ const s = new p(
25
25
  t.learningRateFactor * this.learningRate,
26
26
  t.beta1,
27
27
  t.beta2,
@@ -33,53 +33,58 @@ class g {
33
33
  weightDecay: 0
34
34
  }
35
35
  );
36
- this.optimizer = e;
36
+ this.optimizer = s;
37
37
  }
38
38
  printGradients(t) {
39
- Object.keys(t).forEach((e) => {
40
- const s = t[e];
41
- 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]}`);
39
+ Object.keys(t).forEach((s) => {
40
+ const e = t[s];
41
+ console.log(`${s}:`), console.log(` Shape: ${e.shape}`), console.log(` Mean: ${this.tf.mean(e).dataSync()[0]}`), console.log(` Std: ${this.tf.moments(e).variance.sqrt().dataSync()[0]}`), console.log(` Min: ${this.tf.min(e).dataSync()[0]}`), console.log(` Max: ${this.tf.max(e).dataSync()[0]}`), console.log(` Norm: ${this.tf.norm(e).dataSync()[0]}`);
42
42
  });
43
43
  }
44
- trainStep(t, e = !1, s = !1) {
44
+ trainStep(t, s = !1, e = !1) {
45
45
  return this.tf.tidy(() => {
46
- const { xs: i, ys: r } = t, o = () => {
47
- const { loss: l, logits: c } = this.model.forward(i, r, !0);
46
+ const { xs: i, ys: a } = t, o = () => {
47
+ const { loss: l, logits: c } = this.model.forward(i, a, !0);
48
48
  return c.dispose(), l;
49
- }, { value: n, grads: a } = this.tf.variableGrads(o);
50
- return e || (s && (console.log("-------"), this.printGradients(a), console.log("-------")), this.optimizer.applyGradients(a), this.tf.dispose(a)), n;
49
+ }, { value: n, grads: r } = this.tf.variableGrads(o);
50
+ return s || (e && (console.log("-------"), this.printGradients(r), console.log("-------")), this.optimizer.applyGradients(r), this.tf.dispose(r)), n;
51
51
  });
52
52
  }
53
53
  dummyPass() {
54
- const t = this.tf.zeros([1, this.model.config.blockSize], "int32"), e = this.tf.zeros([1, this.model.config.blockSize, this.model.config.vocabSize]);
54
+ const t = this.tf.zeros([1, this.model.config.blockSize], "int32"), s = this.tf.zeros([1, this.model.config.blockSize, this.model.config.vocabSize]);
55
55
  try {
56
- const s = this.trainStep({ xs: t, ys: e }, !0);
57
- s.dataSync(), s.dispose();
58
- } catch (s) {
59
- console.error("Error during dummy pass:", s);
56
+ const e = this.trainStep({ xs: t, ys: s }, !0);
57
+ e.dataSync(), e.dispose();
58
+ } catch (e) {
59
+ console.error("Error during dummy pass:", e);
60
60
  } finally {
61
- t.dispose(), e.dispose();
61
+ t.dispose(), s.dispose();
62
62
  }
63
63
  }
64
- async trainBatch(t, e) {
64
+ async trainBatch(t, s) {
65
65
  try {
66
- const s = this.trainStep(e, !1, !1);
67
- 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));
68
- } catch (s) {
69
- throw console.error(`Error processing batch at step ${t.step}:`, s), this.tf.dispose(), s;
66
+ const e = this.trainStep(s, !1, !1);
67
+ return s.xs.dispose(), s.ys.dispose(), t.step++, t.totalSteps++, e.array().then((i) => (t.lastLoss = i, t.losses.push(t.lastLoss), e.dispose(), t.lastLoss));
68
+ } catch (e) {
69
+ throw console.error(`Error processing batch at step ${t.step}:`, e), this.tf.dispose(), e;
70
70
  }
71
71
  }
72
- async createTrainValidationSplit(t, e = 32, s = 0.1) {
73
- const i = Math.floor(t.length * (1 - s)), r = t.slice(0, i), o = t.slice(i), n = await this.datasetBuilder.createTextDataset(r, e), a = await this.datasetBuilder.createTextDataset(o, e);
74
- return { trainDataset: n, validationDataset: a };
72
+ async createTrainValidationSplit(t, s = 32, e = 0.1) {
73
+ const i = await this.datasetBuilder.createTextDataset(t, s, 0, 1 - e), a = await this.datasetBuilder.createTextDataset(
74
+ t,
75
+ s,
76
+ 1 - e,
77
+ 1
78
+ );
79
+ return { trainDataset: i, validationDataset: a };
75
80
  }
76
- async createDataset(t, e = 32) {
77
- return await this.datasetBuilder.createTextDataset(t, e);
81
+ async createDataset(t, s = 32) {
82
+ return await this.datasetBuilder.createTextDataset(t, s);
78
83
  }
79
84
  dispose() {
80
85
  this.optimizer && this.optimizer.dispose();
81
86
  }
82
87
  }
83
88
  export {
84
- g as default
89
+ u as default
85
90
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@genai-fi/nanogpt",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "main": "dist/main.js",
6
6
  "types": "dist/main.d.ts",