@genai-fi/nanogpt 0.3.2 → 0.4.1

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.
Files changed (98) hide show
  1. package/dist/Generator.js +22 -22
  2. package/dist/MLP-KHhikThU.js +83 -0
  3. package/dist/NanoGPTModel.d.ts +2 -3
  4. package/dist/NanoGPTModel.js +79 -79
  5. package/dist/TeachableLLM.js +16 -13
  6. package/dist/axis_util-DeydwOoC.js +69 -0
  7. package/dist/{concat-BIZS_td9.js → concat-DS_qH7MI.js} +5 -5
  8. package/dist/config.js +7 -8
  9. package/dist/{gather-BPGW8RsB.js → gather-BUmJIS8n.js} +1 -1
  10. package/dist/{index-pWA4_lUh.js → index-XjBAhiFO.js} +1272 -1174
  11. package/dist/layers/BaseLayer.d.ts +14 -2
  12. package/dist/layers/BaseLayer.js +9 -9
  13. package/dist/layers/CausalSelfAttention.d.ts +4 -8
  14. package/dist/layers/CausalSelfAttention.js +106 -80
  15. package/dist/layers/MLP.d.ts +2 -3
  16. package/dist/layers/MLP.js +5 -62
  17. package/dist/layers/RMSNorm.d.ts +2 -2
  18. package/dist/layers/RMSNorm.js +11 -11
  19. package/dist/layers/RoPECache.js +3 -3
  20. package/dist/layers/TiedEmbedding.js +7 -6
  21. package/dist/layers/TransformerBlock.d.ts +2 -6
  22. package/dist/layers/TransformerBlock.js +9 -12
  23. package/dist/{sum-C7Mgy9Bw.js → log_sum_exp-DJPkVZZn.js} +32 -54
  24. package/dist/main.js +22 -19
  25. package/dist/{mat_mul-D7_a4KJn.js → mat_mul-CKwFEV1Q.js} +1 -1
  26. package/dist/max-DJvEiCAJ.js +25 -0
  27. package/dist/moments-CrWRPcR3.js +53 -0
  28. package/dist/norm-BzY929B_.js +86 -0
  29. package/dist/{ones-Cog-G2ag.js → ones-BO01zpJG.js} +2 -2
  30. package/dist/ops/appendCache.js +1 -1
  31. package/dist/ops/attentionMask.js +1 -1
  32. package/dist/ops/cpu/appendCache.js +2 -2
  33. package/dist/ops/cpu/attentionMask.js +2 -2
  34. package/dist/ops/cpu/fusedSoftmax.d.ts +9 -0
  35. package/dist/ops/cpu/fusedSoftmax.js +23 -0
  36. package/dist/ops/cpu/gatherSub.js +3 -3
  37. package/dist/ops/cpu/mulDropout.d.ts +1 -0
  38. package/dist/ops/cpu/mulDropout.js +17 -0
  39. package/dist/ops/cpu/qkv.js +3 -3
  40. package/dist/ops/cpu/rope.js +5 -5
  41. package/dist/ops/cpu/scatterSub.js +27 -27
  42. package/dist/ops/fusedSoftmax.d.ts +2 -0
  43. package/dist/ops/fusedSoftmax.js +10 -0
  44. package/dist/ops/gatherSub.js +1 -1
  45. package/dist/ops/grads/attentionMask.js +1 -1
  46. package/dist/ops/grads/fusedSoftmax.d.ts +2 -0
  47. package/dist/ops/grads/fusedSoftmax.js +17 -0
  48. package/dist/ops/grads/qkv.js +1 -1
  49. package/dist/ops/grads/rope.js +1 -1
  50. package/dist/ops/mulDrop.d.ts +2 -0
  51. package/dist/ops/mulDrop.js +9 -0
  52. package/dist/ops/node/sparseCrossEntropy.js +1 -1
  53. package/dist/ops/qkv.js +1 -1
  54. package/dist/ops/scatterSub.js +1 -1
  55. package/dist/ops/webgl/appendCache.js +1 -1
  56. package/dist/ops/webgl/attentionMask.js +1 -1
  57. package/dist/ops/webgl/fusedSoftmax.d.ts +11 -0
  58. package/dist/ops/webgl/fusedSoftmax.js +3930 -0
  59. package/dist/ops/webgl/gatherSub.js +1 -1
  60. package/dist/ops/webgl/mulDropout.d.ts +1 -0
  61. package/dist/ops/webgl/mulDropout.js +41 -0
  62. package/dist/ops/webgl/qkv.js +1 -1
  63. package/dist/ops/webgl/rope.js +1 -1
  64. package/dist/ops/webgl/scatterSub.js +1 -1
  65. package/dist/{random_width-oeUIlUZj.js → random_width-CMHmdbSu.js} +4212 -6630
  66. package/dist/{range-CcDl05lo.js → range-DQMNzBWs.js} +1 -1
  67. package/dist/{reshape-C8CR_Bad.js → reshape-DFzh97Sc.js} +1 -1
  68. package/dist/{sin-BJIrfnj7.js → sin-BYM-U4Ut.js} +1 -1
  69. package/dist/slice_util-CnVNPQI-.js +90 -0
  70. package/dist/softmax-4DOn6cPq.js +28 -0
  71. package/dist/{split-DZbvruEP.js → split-CkbeVdF8.js} +3 -3
  72. package/dist/{stack-BMm-efee.js → stack-DaIMO5iX.js} +1 -1
  73. package/dist/sum-C6u3xMi3.js +27 -0
  74. package/dist/{tensor-DJVbYhh1.js → tensor-Cu1fU7H7.js} +1 -1
  75. package/dist/{tensor2d-ZuQSh2D-.js → tensor2d-D0CKdG6B.js} +1 -1
  76. package/dist/tfjs_backend-Bzl2SrRo.js +2460 -0
  77. package/dist/training/AdamExt.js +1 -1
  78. package/dist/training/DatasetBuilder.js +3 -3
  79. package/dist/training/FullTrainer.js +1 -1
  80. package/dist/training/Trainer.js +13 -12
  81. package/dist/training/sparseCrossEntropy.js +12 -11
  82. package/dist/utilities/dummy.js +8 -8
  83. package/dist/utilities/generate.js +11 -11
  84. package/dist/utilities/load.js +1 -1
  85. package/dist/utilities/profile.js +1 -1
  86. package/dist/utilities/weights.js +2 -2
  87. package/dist/{variable-Dl_ub3pk.js → variable-BS4AKqNU.js} +1 -1
  88. package/dist/{zeros-CCy9C3uU.js → zeros-CmJFiC84.js} +1 -1
  89. package/package.json +1 -1
  90. package/dist/exports_layers-tbTBcwMM.js +0 -25
  91. package/dist/layers/LayerNorm.d.ts +0 -13
  92. package/dist/layers/LayerNorm.js +0 -33
  93. package/dist/moments-DfcpfwKi.js +0 -132
  94. package/dist/softmax-Be_lsqUc.js +0 -105
  95. package/dist/training/LayerTrainer.d.ts +0 -29
  96. package/dist/training/LayerTrainer.js +0 -95
  97. package/dist/training/lwSchedule.d.ts +0 -7
  98. package/dist/training/lwSchedule.js +0 -162
@@ -1,4 +1,4 @@
1
- import { A as r, b as c, f as h, s as g, e as o } from "../index-pWA4_lUh.js";
1
+ import { A as r, b as c, f as h, s as g, e as o } from "../index-XjBAhiFO.js";
2
2
  class u extends r {
3
3
  constructor(t, e, s, a, i) {
4
4
  super(t, e, s, a), this.config = i, this.startLearningRate = t;
@@ -1,7 +1,7 @@
1
- import { a7 as $, a8 as m, a9 as M, a as R, aa as f, ab as v, ac as z, j as _, t as x } from "../index-pWA4_lUh.js";
1
+ import { a9 as $, aa as m, ab as M, a as R, ac as f, ad as v, ae as z, j as _, t as x } from "../index-XjBAhiFO.js";
2
2
  import { s as E } from "../index-C4L8Cm77.js";
3
- import { s as P } from "../stack-BMm-efee.js";
4
- import { t as D } from "../tensor-DJVbYhh1.js";
3
+ import { s as P } from "../stack-DaIMO5iX.js";
4
+ import { t as D } from "../tensor-Cu1fU7H7.js";
5
5
  import "../index-Tf7vU29b.js";
6
6
  /**
7
7
  * @license
@@ -1,7 +1,7 @@
1
1
  import { generateText as v } from "../utilities/generate.js";
2
2
  import L from "./Trainer.js";
3
3
  import x from "./Evaluator.js";
4
- import { a as h } from "../index-pWA4_lUh.js";
4
+ import { a as h } from "../index-XjBAhiFO.js";
5
5
  const D = {
6
6
  desiredLoss: 0.01,
7
7
  logInterval: 1,
@@ -1,12 +1,13 @@
1
- import { DatasetBuilder as p } from "./DatasetBuilder.js";
1
+ import { DatasetBuilder as d } from "./DatasetBuilder.js";
2
2
  import h from "./AdamExt.js";
3
- import { t as g, v as u, a as o } from "../index-pWA4_lUh.js";
4
- import { m as y, a as f, b as S, n as z } from "../moments-DfcpfwKi.js";
5
- import { m as b } from "../sum-C7Mgy9Bw.js";
6
- import { z as n } from "../zeros-CCy9C3uU.js";
7
- class M {
3
+ import { t as g, v as u, a as o } from "../index-XjBAhiFO.js";
4
+ import { m as y, n as f } from "../norm-BzY929B_.js";
5
+ import { m as S, a as z } from "../moments-CrWRPcR3.js";
6
+ import { m as b } from "../max-DJvEiCAJ.js";
7
+ import { z as n } from "../zeros-CmJFiC84.js";
8
+ class G {
8
9
  constructor(t, s, e = 1e-3) {
9
- this.tokenizer = s, this.model = t, this.learningRate = e, this.resetOptimizer(), this.datasetBuilder = new p(s, t.config.blockSize);
10
+ this.tokenizer = s, this.model = t, this.learningRate = e, this.resetOptimizer(), this.datasetBuilder = new d(s, t.config.gpt.blockSize);
10
11
  }
11
12
  model;
12
13
  optimizer;
@@ -45,21 +46,21 @@ class M {
45
46
  printGradients(t) {
46
47
  Object.keys(t).forEach((s) => {
47
48
  const e = t[s];
48
- console.log(`${s}:`), console.log(` Shape: ${e.shape}`), console.log(` Mean: ${y(e).dataSync()[0]}`), console.log(` Std: ${f(e).variance.sqrt().dataSync()[0]}`), console.log(` Min: ${S(e).dataSync()[0]}`), console.log(` Max: ${b(e).dataSync()[0]}`), console.log(` Norm: ${z(e).dataSync()[0]}`);
49
+ console.log(`${s}:`), console.log(` Shape: ${e.shape}`), console.log(` Mean: ${S(e).dataSync()[0]}`), console.log(` Std: ${z(e).variance.sqrt().dataSync()[0]}`), console.log(` Min: ${y(e).dataSync()[0]}`), console.log(` Max: ${b(e).dataSync()[0]}`), console.log(` Norm: ${f(e).dataSync()[0]}`);
49
50
  });
50
51
  }
51
52
  trainStep(t, s = !1, e = !1) {
52
53
  return g(() => {
53
54
  this.model.getProfiler()?.startMemory();
54
55
  const { xs: a, ys: r } = t, l = () => {
55
- const { loss: d, logits: m } = this.model.forward(a, r, !0);
56
- return m.dispose(), d;
56
+ const { loss: m, logits: p } = this.model.forward(a, r, !0);
57
+ return p.dispose(), m;
57
58
  }, { value: c, grads: i } = u(l);
58
59
  return s ? this.model.getProfiler()?.endMemory("Training") : (e && (console.log("-------"), this.printGradients(i), console.log("-------")), this.optimizer.applyGradients(i), this.model.getProfiler()?.endMemory("Training"), o(i)), c;
59
60
  });
60
61
  }
61
62
  dummyPass() {
62
- const t = n([1, this.model.config.blockSize], "int32"), s = n([1, this.model.config.blockSize], "int32");
63
+ const t = n([1, this.model.config.gpt.blockSize], "int32"), s = n([1, this.model.config.gpt.blockSize], "int32");
63
64
  try {
64
65
  const e = this.trainStep({ xs: t, ys: s }, !0);
65
66
  e.dataSync(), e.dispose();
@@ -94,5 +95,5 @@ class M {
94
95
  }
95
96
  }
96
97
  export {
97
- M as default
98
+ G as default
98
99
  };
@@ -1,26 +1,27 @@
1
1
  import { gatherSub as L } from "../ops/gatherSub.js";
2
2
  import { scatterSub as y } from "../ops/scatterSub.js";
3
- import { e as u, c as i, z as S, t as f, s as G } from "../index-pWA4_lUh.js";
4
- import { s as v, l as z } from "../softmax-Be_lsqUc.js";
5
- import { m as k } from "../sum-C7Mgy9Bw.js";
3
+ import { e as u, c as i, z as S, t as f, s as G } from "../index-XjBAhiFO.js";
4
+ import { s as v } from "../softmax-4DOn6cPq.js";
5
+ import { m as z } from "../max-DJvEiCAJ.js";
6
+ import { l as k } from "../log_sum_exp-DJPkVZZn.js";
6
7
  function F(a, s) {
7
8
  return f(() => {
8
- const e = a.shape[a.shape.length - 1], n = a.shape.slice(0, -1).reduce((d, c) => d * c, 1), p = a.shape.length > 2 ? a.reshape([n, e]) : a, o = s.shape.length > 1 ? s.reshape([n]).cast("int32") : s.cast("int32"), t = k(p, -1, !0), r = G(p, t), h = z(r, -1);
9
- return L(h, o, r);
9
+ const e = a.shape[a.shape.length - 1], o = a.shape.slice(0, -1).reduce((d, c) => d * c, 1), p = a.shape.length > 2 ? a.reshape([o, e]) : a, n = s.shape.length > 1 ? s.reshape([o]).cast("int32") : s.cast("int32"), t = z(p, -1, !0), r = G(p, t), h = k(r, -1);
10
+ return L(h, n, r);
10
11
  });
11
12
  }
12
- function $() {
13
+ function j() {
13
14
  return u().backendName === "tensorflow" ? i((s, e, m) => {
14
- const n = s.shape.length > 2 ? s.reshape([-1, s.shape[s.shape.length - 1]]) : s, p = e.shape.length > 1 ? e.reshape([-1]).cast("int32") : e.cast("int32"), [o, t] = u().runKernel(
15
+ const o = s.shape.length > 2 ? s.reshape([-1, s.shape[s.shape.length - 1]]) : s, p = e.shape.length > 1 ? e.reshape([-1]).cast("int32") : e.cast("int32"), [n, t] = u().runKernel(
15
16
  "NativeSparseSoftmaxCrossEntropy",
16
- { logits: n, labels: p },
17
+ { logits: o, labels: p },
17
18
  {}
18
19
  );
19
- return m([t.reshape(s.shape)]), { value: o, gradFunc: (r, h) => [h[0], S(e)] };
20
+ return m([t.reshape(s.shape)]), { value: n, gradFunc: (r, h) => [h[0], S(e)] };
20
21
  }) : i(
21
22
  // @ts-expect-error Invalid params
22
23
  (s, e, m) => {
23
- const n = s.shape[s.shape.length - 1], o = s.shape.slice(0, -1).reduce((d, c) => d * c, 1), t = s.reshape([o, n]), r = e.reshape([o]).cast("int32"), h = F(t, r);
24
+ const o = s.shape[s.shape.length - 1], n = s.shape.slice(0, -1).reduce((d, c) => d * c, 1), t = s.reshape([n, o]), r = e.reshape([n]).cast("int32"), h = F(t, r);
24
25
  return m([t, r]), t.dispose(), r.dispose(), { value: h, gradFunc: (d, c) => f(() => {
25
26
  const l = c[0], b = c[1], x = v(l), C = y(x, b, d), E = S(e);
26
27
  return [C.reshape(s.shape), E];
@@ -29,6 +30,6 @@ function $() {
29
30
  );
30
31
  }
31
32
  export {
32
- $ as createSoftmaxCrossEntropyWithGrad,
33
+ j as createSoftmaxCrossEntropyWithGrad,
33
34
  F as sparseSoftmaxCrossEntropy
34
35
  };
@@ -1,14 +1,14 @@
1
- import "../index-pWA4_lUh.js";
2
- import { z as n } from "../zeros-CCy9C3uU.js";
3
- async function c(s) {
4
- const o = n([1, s.config.blockSize], "int32"), { logits: t, loss: i } = s.forward(o, void 0, !1);
1
+ import "../index-XjBAhiFO.js";
2
+ import { z as n } from "../zeros-CmJFiC84.js";
3
+ async function a(s) {
4
+ const o = n([1, s.config.gpt.blockSize], "int32"), { logits: t, loss: i } = s.forward(o, void 0, !1);
5
5
  await t.data(), t.dispose(), i && i.dispose(), o.dispose();
6
6
  }
7
- function f(s) {
8
- const o = n([1, s.config.blockSize], "int32"), { logits: t, loss: i } = s.forward(o, void 0, !1);
7
+ function c(s) {
8
+ const o = n([1, s.config.gpt.blockSize], "int32"), { logits: t, loss: i } = s.forward(o, void 0, !1);
9
9
  t.dispose(), i && i.dispose(), o.dispose();
10
10
  }
11
11
  export {
12
- f as dummyPass,
13
- c as dummyPassAsync
12
+ c as dummyPass,
13
+ a as dummyPassAsync
14
14
  };
@@ -1,20 +1,20 @@
1
- import { t as y } from "../index-pWA4_lUh.js";
2
- import { t as x } from "../tensor2d-ZuQSh2D-.js";
3
- import { c as g } from "../concat-BIZS_td9.js";
1
+ import { t as y } from "../index-XjBAhiFO.js";
2
+ import { t as x } from "../tensor2d-D0CKdG6B.js";
3
+ import { c as f } from "../concat-DS_qH7MI.js";
4
4
  async function A(o, r, a, c, T) {
5
5
  if (c <= 0)
6
6
  throw new Error("Length must be a positive integer");
7
7
  if (a.length === 0)
8
8
  throw new Error("Prompt cannot be an empty string");
9
- const d = await o.tokenise([a], !0), s = r.config.useRope ? new Array(r.config.nLayer).fill(void 0) : void 0, p = y(() => {
10
- let e = x(d, [1, d[0].length], "int32"), t = e;
11
- for (let f = 0; f < c; f++) {
12
- const { output: n } = r.generate(e, s, T), l = e, m = t;
13
- t = g([t, n], 1), e = s ? n : g([e, n], 1), l.dispose(), m.dispose(), s || n.dispose();
9
+ const p = await o.tokenise([a], !0), s = r.config.gpt.useRope ? new Array(r.config.gpt.nLayer).fill(void 0) : void 0, d = y(() => {
10
+ let t = x(p, [1, p[0].length], "int32"), e = t;
11
+ for (let g = 0; g < c; g++) {
12
+ const { output: n } = r.generate(t, s, T), l = t, m = e;
13
+ e = f([e, n], 1), t = s ? n : f([t, n], 1), l.dispose(), m.dispose(), s || n.dispose();
14
14
  }
15
- return t;
16
- }), w = await p.array();
17
- p.dispose();
15
+ return e;
16
+ }), w = await d.array();
17
+ d.dispose();
18
18
  const i = w[0], u = i.indexOf(o.eosToken);
19
19
  return u !== -1 && i.splice(u), await o.decode(i);
20
20
  }
@@ -3,7 +3,7 @@ import { importWeights as b } from "./weights.js";
3
3
  import u from "../tokeniser/CharTokeniser.js";
4
4
  import F from "../NanoGPTModel.js";
5
5
  import { dummyPassAsync as j } from "./dummy.js";
6
- import { d as T } from "../index-pWA4_lUh.js";
6
+ import { d as T } from "../index-XjBAhiFO.js";
7
7
  import E from "../tokeniser/bpe.js";
8
8
  async function A(t) {
9
9
  const o = await fetch(t);
@@ -1,4 +1,4 @@
1
- import { m as s } from "../index-pWA4_lUh.js";
1
+ import { m as s } from "../index-XjBAhiFO.js";
2
2
  const m = 1024 * 1024;
3
3
  class i {
4
4
  log = /* @__PURE__ */ new Map();
@@ -1,5 +1,5 @@
1
- import "../index-pWA4_lUh.js";
2
- import { t as p } from "../tensor-DJVbYhh1.js";
1
+ import "../index-XjBAhiFO.js";
2
+ import { t as p } from "../tensor-Cu1fU7H7.js";
3
3
  function h(n) {
4
4
  const e = n.reduce((s, o) => s + o.length, 0), a = new Float32Array(e);
5
5
  let t = 0;
@@ -1,4 +1,4 @@
1
- import { E as i } from "./index-pWA4_lUh.js";
1
+ import { E as i } from "./index-XjBAhiFO.js";
2
2
  /**
3
3
  * @license
4
4
  * Copyright 2018 Google LLC. All Rights Reserved.
@@ -1,4 +1,4 @@
1
- import { o as m, h as r, M as l, E as c, N as i, k as p, O as u, n as f } from "./index-pWA4_lUh.js";
1
+ import { o as m, h as r, U as l, E as c, V as i, k as p, W as u, n as f } from "./index-XjBAhiFO.js";
2
2
  /**
3
3
  * @license
4
4
  * Copyright 2020 Google LLC. All Rights Reserved.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@genai-fi/nanogpt",
3
- "version": "0.3.2",
3
+ "version": "0.4.1",
4
4
  "type": "module",
5
5
  "main": "dist/main.js",
6
6
  "types": "dist/main.d.ts",
@@ -1,25 +0,0 @@
1
- import { E as e, D as r, a as o } from "./random_width-oeUIlUZj.js";
2
- import "./index-pWA4_lUh.js";
3
- /**
4
- * @license
5
- * Copyright 2018 Google LLC
6
- *
7
- * Use of this source code is governed by an MIT-style
8
- * license that can be found in the LICENSE file or at
9
- * https://opensource.org/licenses/MIT.
10
- * =============================================================================
11
- */
12
- function s(n) {
13
- return new o(n);
14
- }
15
- function u(n) {
16
- return new r(n);
17
- }
18
- function d(n) {
19
- return new e(n);
20
- }
21
- export {
22
- u as a,
23
- s as d,
24
- d as e
25
- };
@@ -1,13 +0,0 @@
1
- import { default as TF } from '@tensorflow/tfjs';
2
- export default class LayerNorm {
3
- private gamma;
4
- private epsilon;
5
- private tf;
6
- constructor(tf: typeof TF, shape: number[], epsilon?: number, name?: string);
7
- get trainableWeights(): TF.Variable[];
8
- set trainable(value: boolean);
9
- getWeights(): TF.Tensor[];
10
- setWeights(weights: TF.Tensor[]): void;
11
- apply(x: TF.Tensor): TF.Tensor;
12
- dispose(): void;
13
- }
@@ -1,33 +0,0 @@
1
- class h {
2
- gamma;
3
- //private beta: TF.Variable;
4
- epsilon;
5
- tf;
6
- constructor(a, s, t = 1e-5, e = "") {
7
- this.tf = a, this.epsilon = t, this.gamma = a.variable(a.ones(s), !0, `${e}_gamma`, "float32");
8
- }
9
- get trainableWeights() {
10
- return [this.gamma];
11
- }
12
- set trainable(a) {
13
- this.gamma.trainable = a;
14
- }
15
- getWeights() {
16
- return [this.gamma];
17
- }
18
- setWeights(a) {
19
- this.gamma.assign(a[0]);
20
- }
21
- apply(a) {
22
- return this.tf.tidy(() => {
23
- const s = a.mean(-1, !0), t = a.sub(s), i = t.square().mean(-1, !0).add(this.epsilon).rsqrt();
24
- return t.mul(i).mul(this.gamma);
25
- });
26
- }
27
- dispose() {
28
- this.gamma.dispose();
29
- }
30
- }
31
- export {
32
- h as default
33
- };
@@ -1,132 +0,0 @@
1
- import { o as i, h as m, E as y, a2 as A, a0 as E, a3 as o, a4 as h, J as b, f as S, a5 as v, a6 as _, s as k, U as q } from "./index-pWA4_lUh.js";
2
- import { e as w, s as u, m as l } from "./sum-C7Mgy9Bw.js";
3
- import { r as c } from "./reshape-C8CR_Bad.js";
4
- /**
5
- * @license
6
- * Copyright 2020 Google Inc. All Rights Reserved.
7
- * Licensed under the Apache License, Version 2.0 (the "License");
8
- * you may not use this file except in compliance with the License.
9
- * You may obtain a copy of the License at
10
- *
11
- * http://www.apache.org/licenses/LICENSE-2.0
12
- *
13
- * Unless required by applicable law or agreed to in writing, software
14
- * distributed under the License is distributed on an "AS IS" BASIS,
15
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- * See the License for the specific language governing permissions and
17
- * limitations under the License.
18
- * =============================================================================
19
- */
20
- function K(r, e = null, n = !1) {
21
- const t = { x: m(r, "x", "min") }, a = { axis: e, keepDims: n };
22
- return y.runKernel(A, t, a);
23
- }
24
- const d = /* @__PURE__ */ i({ min_: K });
25
- /**
26
- * @license
27
- * Copyright 2018 Google LLC. All Rights Reserved.
28
- * Licensed under the Apache License, Version 2.0 (the "License");
29
- * you may not use this file except in compliance with the License.
30
- * You may obtain a copy of the License at
31
- *
32
- * http://www.apache.org/licenses/LICENSE-2.0
33
- *
34
- * Unless required by applicable law or agreed to in writing, software
35
- * distributed under the License is distributed on an "AS IS" BASIS,
36
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37
- * See the License for the specific language governing permissions and
38
- * limitations under the License.
39
- * =============================================================================
40
- */
41
- function M(r, e = "euclidean", n = null, s = !1) {
42
- r = m(r, "x", "norm");
43
- const t = $(r, e, n);
44
- let a = t.shape;
45
- if (s) {
46
- const f = E(n, r.shape);
47
- a = w(t.shape, f);
48
- }
49
- return c(t, a);
50
- }
51
- function $(r, e, n = null) {
52
- if (r.rank === 0)
53
- return o(r);
54
- if (r.rank !== 1 && n === null)
55
- return $(c(r, [-1]), e, n);
56
- if (r.rank === 1 || typeof n == "number" || Array.isArray(n) && n.length === 1) {
57
- if (e === 1)
58
- return u(o(r), n);
59
- if (e === 1 / 0)
60
- return l(o(r), n);
61
- if (e === -1 / 0)
62
- return d(o(r), n);
63
- if (e === "euclidean" || e === 2)
64
- return h(u(b(o(r), S(2, "int32")), n));
65
- throw new Error(`Error in norm: invalid ord value: ${e}`);
66
- }
67
- if (Array.isArray(n) && n.length === 2) {
68
- if (e === 1)
69
- return l(u(o(r), n[0]), n[1] - 1);
70
- if (e === 1 / 0)
71
- return l(u(o(r), n[1]), n[0]);
72
- if (e === -1 / 0)
73
- return d(u(o(r), n[1]), n[0]);
74
- if (e === "fro" || e === "euclidean")
75
- return h(u(v(r), n));
76
- throw new Error(`Error in norm: invalid ord value: ${e}`);
77
- }
78
- throw new Error(`Error in norm: invalid axis: ${n}`);
79
- }
80
- const J = /* @__PURE__ */ i({ norm_: M });
81
- /**
82
- * @license
83
- * Copyright 2020 Google Inc. All Rights Reserved.
84
- * Licensed under the Apache License, Version 2.0 (the "License");
85
- * you may not use this file except in compliance with the License.
86
- * You may obtain a copy of the License at
87
- *
88
- * http://www.apache.org/licenses/LICENSE-2.0
89
- *
90
- * Unless required by applicable law or agreed to in writing, software
91
- * distributed under the License is distributed on an "AS IS" BASIS,
92
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
93
- * See the License for the specific language governing permissions and
94
- * limitations under the License.
95
- * =============================================================================
96
- */
97
- function T(r, e = null, n = !1) {
98
- const t = { x: m(r, "x", "mean") }, a = { axis: e, keepDims: n };
99
- return y.runKernel(_, t, a);
100
- }
101
- const p = /* @__PURE__ */ i({ mean_: T });
102
- /**
103
- * @license
104
- * Copyright 2020 Google LLC. All Rights Reserved.
105
- * Licensed under the Apache License, Version 2.0 (the "License");
106
- * you may not use this file except in compliance with the License.
107
- * You may obtain a copy of the License at
108
- *
109
- * http://www.apache.org/licenses/LICENSE-2.0
110
- *
111
- * Unless required by applicable law or agreed to in writing, software
112
- * distributed under the License is distributed on an "AS IS" BASIS,
113
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
114
- * See the License for the specific language governing permissions and
115
- * limitations under the License.
116
- * =============================================================================
117
- */
118
- function g(r, e = null, n = !1) {
119
- r = m(r, "x", "moments");
120
- const s = E(e, r.shape), t = p(r, s, n);
121
- let a = t.shape;
122
- n || (a = w(t.shape, s));
123
- const f = v(k(q(r, "float32"), c(t, a))), I = p(f, s, n);
124
- return { mean: t, variance: I };
125
- }
126
- const P = /* @__PURE__ */ i({ moments_: g });
127
- export {
128
- P as a,
129
- d as b,
130
- p as m,
131
- J as n
132
- };
@@ -1,105 +0,0 @@
1
- import { o as r, h as e, E as c, _ as $, $ as h, a0 as E, s as S, Z as _, a1 as d } from "./index-pWA4_lUh.js";
2
- import { m as k, s as w, e as K } from "./sum-C7Mgy9Bw.js";
3
- import { r as l } from "./reshape-C8CR_Bad.js";
4
- /**
5
- * @license
6
- * Copyright 2018 Google LLC. All Rights Reserved.
7
- * Licensed under the Apache License, Version 2.0 (the "License");
8
- * you may not use this file except in compliance with the License.
9
- * You may obtain a copy of the License at
10
- *
11
- * http://www.apache.org/licenses/LICENSE-2.0
12
- *
13
- * Unless required by applicable law or agreed to in writing, software
14
- * distributed under the License is distributed on an "AS IS" BASIS,
15
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- * See the License for the specific language governing permissions and
17
- * limitations under the License.
18
- * =============================================================================
19
- */
20
- function T(n) {
21
- const s = { x: e(n, "x", "exp") };
22
- return c.runKernel($, s);
23
- }
24
- const b = /* @__PURE__ */ r({ exp_: T });
25
- /**
26
- * @license
27
- * Copyright 2018 Google LLC. All Rights Reserved.
28
- * Licensed under the Apache License, Version 2.0 (the "License");
29
- * you may not use this file except in compliance with the License.
30
- * You may obtain a copy of the License at
31
- *
32
- * http://www.apache.org/licenses/LICENSE-2.0
33
- *
34
- * Unless required by applicable law or agreed to in writing, software
35
- * distributed under the License is distributed on an "AS IS" BASIS,
36
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37
- * See the License for the specific language governing permissions and
38
- * limitations under the License.
39
- * =============================================================================
40
- */
41
- function L(n) {
42
- const s = { x: e(n, "x", "log", "float32") };
43
- return c.runKernel(h, s);
44
- }
45
- const N = /* @__PURE__ */ r({ log_: L });
46
- /**
47
- * @license
48
- * Copyright 2020 Google LLC. All Rights Reserved.
49
- * Licensed under the Apache License, Version 2.0 (the "License");
50
- * you may not use this file except in compliance with the License.
51
- * You may obtain a copy of the License at
52
- *
53
- * http://www.apache.org/licenses/LICENSE-2.0
54
- *
55
- * Unless required by applicable law or agreed to in writing, software
56
- * distributed under the License is distributed on an "AS IS" BASIS,
57
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
58
- * See the License for the specific language governing permissions and
59
- * limitations under the License.
60
- * =============================================================================
61
- */
62
- function v(n, o = null, s = !1) {
63
- const t = e(n, "x", "logSumExp"), a = E(o, t.shape), p = k(
64
- t,
65
- a,
66
- !0
67
- /* keepDims */
68
- ), i = S(t, p), f = b(i), m = w(f, a), u = N(m), x = _(l(p, u.shape), u);
69
- if (s) {
70
- const g = K(x.shape, a);
71
- return l(x, g);
72
- }
73
- return x;
74
- }
75
- const I = /* @__PURE__ */ r({ logSumExp_: v });
76
- /**
77
- * @license
78
- * Copyright 2018 Google LLC. All Rights Reserved.
79
- * Licensed under the Apache License, Version 2.0 (the "License");
80
- * you may not use this file except in compliance with the License.
81
- * You may obtain a copy of the License at
82
- *
83
- * http://www.apache.org/licenses/LICENSE-2.0
84
- *
85
- * Unless required by applicable law or agreed to in writing, software
86
- * distributed under the License is distributed on an "AS IS" BASIS,
87
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
88
- * See the License for the specific language governing permissions and
89
- * limitations under the License.
90
- * =============================================================================
91
- */
92
- function y(n, o = -1) {
93
- const s = e(n, "logits", "softmax", "float32");
94
- if (o === -1 && (o = s.rank - 1), o !== s.rank - 1)
95
- throw Error(`Softmax along a non-last dimension is not yet supported. Logits was rank ${s.rank} and dim was ${o}`);
96
- const t = { logits: s }, a = { dim: o };
97
- return c.runKernel(d, t, a);
98
- }
99
- const M = /* @__PURE__ */ r({ softmax_: y });
100
- export {
101
- N as a,
102
- b as e,
103
- I as l,
104
- M as s
105
- };
@@ -1,29 +0,0 @@
1
- import { ITokeniser } from '../tokeniser/type';
2
- import { default as NanoGPT } from '../NanoGPTModel';
3
- import { default as GPTTrainer, TrainingOptions } from './Trainer';
4
- import { Tensor } from '@tensorflow/tfjs-core';
5
- import { Dataset } from '@tensorflow/tfjs-data';
6
- interface LayerTrainingOptions extends TrainingOptions {
7
- stepsPerLayer: number;
8
- maxPasses: number;
9
- onLayerChange?: (layer: number, pass: number, valLoss?: number) => Promise<void> | void;
10
- onPassComplete?: (pass: number) => Promise<void> | void;
11
- }
12
- export default class LayerTrainer extends GPTTrainer {
13
- private trainingPattern;
14
- private startPass;
15
- private startLayer;
16
- constructor(model: NanoGPT, tokenizer: ITokeniser, learningRate?: number);
17
- private applyTrainingPattern;
18
- trainOnDataset(dataset: Dataset<{
19
- xs: Tensor;
20
- ys: Tensor;
21
- }>, options: Partial<LayerTrainingOptions>, validationDataset?: Dataset<{
22
- xs: Tensor;
23
- ys: Tensor;
24
- }>): Promise<{
25
- losses: number[];
26
- validationLosses: number[];
27
- }>;
28
- }
29
- export {};