@genai-fi/nanogpt 0.5.6 → 0.6.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 (116) hide show
  1. package/dist/Generator.js +10 -9
  2. package/dist/NanoGPTModel.js +70 -121
  3. package/dist/RealDiv-7xu-pkZN.js +540 -0
  4. package/dist/Reshape-BYC1oUku.js +127 -0
  5. package/dist/TeachableLLM.d.ts +2 -0
  6. package/dist/TeachableLLM.js +42 -34
  7. package/dist/{TiedEmbedding-8S8xn8e6.js → TiedEmbedding-C1HBot-5.js} +12 -13
  8. package/dist/{axis_util-BczFISHz.js → axis_util-CCNL7jea.js} +14 -12
  9. package/dist/{broadcast_to-B7NGsBSh.js → broadcast_to-CddAF879.js} +2 -2
  10. package/dist/{concat-DdKPyAtw.js → concat-XOK9ANZu.js} +7 -7
  11. package/dist/{dataset-iqT4Otvb.js → dataset-BFFipD1c.js} +5 -5
  12. package/dist/{dropout-B09InSJS.js → dropout-xlKRoJyU.js} +9 -9
  13. package/dist/{gather-D6MsdXqc.js → gather-DKtUaTtA.js} +1 -1
  14. package/dist/gpgpu_math-B_ycgZ4W.js +3115 -0
  15. package/dist/{index-Du-bmOP8.js → index-CamYe_M8.js} +844 -647
  16. package/dist/{kernel_funcs_utils-DShm7-0k.js → kernel_funcs_utils-D5MS0JFg.js} +232 -136
  17. package/dist/layers/BaseLayer.js +2 -2
  18. package/dist/layers/CausalSelfAttention.js +6 -6
  19. package/dist/layers/MLP.js +5 -5
  20. package/dist/layers/RMSNorm.js +3 -3
  21. package/dist/layers/RoPECache.js +13 -33
  22. package/dist/layers/TiedEmbedding.js +6 -7
  23. package/dist/layers/TransformerBlock.js +1 -1
  24. package/dist/{log_sum_exp-CxfBtUaG.js → log_sum_exp-CV_5-TTu.js} +15 -15
  25. package/dist/main.js +24 -20
  26. package/dist/{mat_mul-CbiqIe2d.js → mat_mul-CAbRFWUj.js} +4 -4
  27. package/dist/{max-0Xnlpv8k.js → max-JBBv7aUf.js} +3 -3
  28. package/dist/mulmat_packed_gpu-DW4doKL_.js +71 -0
  29. package/dist/{norm-01kY9I2B.js → norm-B9dQTFYn.js} +12 -12
  30. package/dist/{ones-CrutWGas.js → ones-CMHNqMr6.js} +2 -2
  31. package/dist/ops/appendCache.js +3 -3
  32. package/dist/ops/attentionMask.js +1 -1
  33. package/dist/ops/cpu/appendCache.js +2 -2
  34. package/dist/ops/cpu/attentionMask.js +5 -5
  35. package/dist/ops/cpu/fusedSoftmax.js +2 -2
  36. package/dist/ops/cpu/gatherSub.js +5 -5
  37. package/dist/ops/cpu/gelu.js +1 -1
  38. package/dist/ops/cpu/matMulGelu.js +1 -1
  39. package/dist/ops/cpu/matMulMul.js +1 -1
  40. package/dist/ops/cpu/mulDropout.js +1 -1
  41. package/dist/ops/cpu/normRMS.js +1 -1
  42. package/dist/ops/cpu/qkv.js +3 -3
  43. package/dist/ops/cpu/rope.js +5 -5
  44. package/dist/ops/cpu/scatterSub.js +18 -49
  45. package/dist/ops/fusedSoftmax.js +1 -1
  46. package/dist/ops/gatherSub.js +1 -1
  47. package/dist/ops/gelu.js +1 -1
  48. package/dist/ops/grads/attentionMask.js +15 -11
  49. package/dist/ops/grads/fusedSoftmax.js +12 -10
  50. package/dist/ops/grads/gelu.js +1 -1
  51. package/dist/ops/grads/matMulGelu.js +1 -1
  52. package/dist/ops/grads/normRMS.js +1 -1
  53. package/dist/ops/grads/qkv.js +1 -1
  54. package/dist/ops/grads/rope.js +1 -1
  55. package/dist/ops/log.d.ts +0 -0
  56. package/dist/ops/log.js +1 -0
  57. package/dist/ops/matMulGelu.js +1 -1
  58. package/dist/ops/matMulMul.js +1 -1
  59. package/dist/ops/mulDrop.js +1 -1
  60. package/dist/ops/node/sparseCrossEntropy.js +1 -1
  61. package/dist/ops/normRMS.js +1 -1
  62. package/dist/ops/qkv.js +1 -1
  63. package/dist/ops/rope.js +8 -4
  64. package/dist/ops/scatterSub.js +1 -1
  65. package/dist/ops/webgl/appendCache.js +1 -1
  66. package/dist/ops/webgl/attentionMask.js +1 -1
  67. package/dist/ops/webgl/fusedSoftmax.js +31 -3379
  68. package/dist/ops/webgl/gatherSub.js +1 -1
  69. package/dist/ops/webgl/gelu.js +2 -2
  70. package/dist/{gpgpu_math-BFbOyvk4.js → ops/webgl/log.d.ts} +2 -8
  71. package/dist/ops/webgl/log.js +39 -0
  72. package/dist/ops/webgl/matMulGelu.js +48 -115
  73. package/dist/ops/webgl/matMulMul.js +1 -1
  74. package/dist/ops/webgl/mulDropout.js +1 -1
  75. package/dist/ops/webgl/normRMS.js +2 -2
  76. package/dist/ops/webgl/qkv.js +1 -1
  77. package/dist/ops/webgl/rope.js +1 -1
  78. package/dist/ops/webgl/scatterSub.js +1 -1
  79. package/dist/{ops-CJNniCAV.js → ops-DqtYemmV.js} +143 -135
  80. package/dist/{random_width-C-v-35bY.js → random_width-CLMQG5Jn.js} +6925 -6291
  81. package/dist/{range-Bvs1hidm.js → range-DqYjKnuG.js} +1 -1
  82. package/dist/reciprocal-z49filta.js +25 -0
  83. package/dist/register_all_kernels-COt6wLD0.js +21397 -0
  84. package/dist/{reshape-BH7eBpwq.js → reshape-C45vIIRU.js} +1 -1
  85. package/dist/scatter_nd_util-qgtnviTE.js +46 -0
  86. package/dist/selu_util-4QV_GXTB.js +740 -0
  87. package/dist/shared-ByfrGA97.js +3199 -0
  88. package/dist/{sin-CPAZXNjH.js → sin-9JBrfVaB.js} +1 -1
  89. package/dist/{softmax-DhWoBa7r.js → softmax-DvMvui-_.js} +1 -1
  90. package/dist/{split-BCUhuU7B.js → split-DxrHrPFK.js} +4 -4
  91. package/dist/{stack-BV1v7l3S.js → stack-DgaoDmnF.js} +1 -1
  92. package/dist/{sum-Cvq06317.js → sum-BpcpxNEh.js} +3 -3
  93. package/dist/{tensor-DgTOPY6h.js → tensor-CDz5x1mP.js} +1 -1
  94. package/dist/{tensor2d-CRWjDyUe.js → tensor2d-jO8JY5Jd.js} +1 -1
  95. package/dist/training/AdamExt.js +1 -1
  96. package/dist/training/DatasetBuilder.js +2 -2
  97. package/dist/training/FullTrainer.js +1 -1
  98. package/dist/training/Trainer.js +3 -3
  99. package/dist/training/sparseCrossEntropy.js +4 -4
  100. package/dist/utilities/dummy.d.ts +6 -0
  101. package/dist/utilities/dummy.js +31 -10
  102. package/dist/utilities/generate.js +3 -3
  103. package/dist/utilities/load.d.ts +25 -0
  104. package/dist/utilities/load.js +89 -37
  105. package/dist/utilities/profile.d.ts +5 -0
  106. package/dist/utilities/profile.js +12 -9
  107. package/dist/utilities/safetensors.d.ts +3 -0
  108. package/dist/utilities/safetensors.js +83 -0
  109. package/dist/utilities/save.js +47 -29
  110. package/dist/utilities/weights.js +2 -2
  111. package/dist/{variable-DZ3fF0R2.js → variable-CLVXjN7F.js} +1 -1
  112. package/dist/{zeros-BaHhQTWf.js → zeros-DUkkVccu.js} +8 -8
  113. package/package.json +3 -9
  114. package/dist/Reshape-Biok_3X1.js +0 -212
  115. package/dist/slice_util-DskXqRZa.js +0 -49
  116. package/dist/tfjs_backend-D9Ytje0G.js +0 -1010
@@ -1,4 +1,4 @@
1
- import { o, j as t, E as c, _ as a, $ as e } from "./index-Du-bmOP8.js";
1
+ import { q as o, w as t, E as c, a0 as a, a1 as e } from "./index-CamYe_M8.js";
2
2
  /**
3
3
  * @license
4
4
  * Copyright 2018 Google LLC. All Rights Reserved.
@@ -1,4 +1,4 @@
1
- import { o as r, j as f, E as e, S as i } from "./index-Du-bmOP8.js";
1
+ import { q as r, w as f, E as e, S as i } from "./index-CamYe_M8.js";
2
2
  /**
3
3
  * @license
4
4
  * Copyright 2018 Google LLC. All Rights Reserved.
@@ -1,4 +1,4 @@
1
- import { o as p, j as i, E as a, x as c } from "./index-Du-bmOP8.js";
1
+ import { q as p, w as i, E as a, H as c } from "./index-CamYe_M8.js";
2
2
  /**
3
3
  * @license
4
4
  * Copyright 2020 Google LLC. All Rights Reserved.
@@ -15,9 +15,9 @@ import { o as p, j as i, E as a, x as c } from "./index-Du-bmOP8.js";
15
15
  * limitations under the License.
16
16
  * =============================================================================
17
17
  */
18
- function e(t, s, o = 0) {
19
- const n = { x: i(t, "x", "split") }, r = { numOrSizeSplits: s, axis: o };
20
- return a.runKernel(c, n, r);
18
+ function e(t, s, n = 0) {
19
+ const o = { x: i(t, "x", "split") }, r = { numOrSizeSplits: s, axis: n };
20
+ return a.runKernel(c, o, r);
21
21
  }
22
22
  const u = /* @__PURE__ */ p({ split_: e });
23
23
  export {
@@ -1,4 +1,4 @@
1
- import { o as e, k as c, l as n, E as k, P as i } from "./index-Du-bmOP8.js";
1
+ import { q as e, x as c, k as n, E as k, P as i } from "./index-CamYe_M8.js";
2
2
  /**
3
3
  * @license
4
4
  * Copyright 2020 Google LLC. All Rights Reserved.
@@ -1,4 +1,4 @@
1
- import { o as e, j as u, D as c, E as l, F as m } from "./index-Du-bmOP8.js";
1
+ import { q as e, w as u, N as c, E as l, O as m } from "./index-CamYe_M8.js";
2
2
  /**
3
3
  * @license
4
4
  * Copyright 2018 Google LLC. All Rights Reserved.
@@ -15,10 +15,10 @@ import { o as e, j as u, D as c, E as l, F as m } from "./index-Du-bmOP8.js";
15
15
  * limitations under the License.
16
16
  * =============================================================================
17
17
  */
18
- function i(t, o = null, n = !1) {
18
+ function i(t, n = null, o = !1) {
19
19
  let s = u(t, "x", "sum");
20
20
  s.dtype === "bool" && (s = c(s, "int32"));
21
- const r = { x: s }, a = { axis: o, keepDims: n };
21
+ const r = { x: s }, a = { axis: n, keepDims: o };
22
22
  return l.runKernel(m, r, a);
23
23
  }
24
24
  const f = /* @__PURE__ */ e({ sum_: i });
@@ -1,4 +1,4 @@
1
- import { J as t, K as a } from "./index-Du-bmOP8.js";
1
+ import { Q as t, U as a } from "./index-CamYe_M8.js";
2
2
  /**
3
3
  * @license
4
4
  * Copyright 2018 Google LLC. All Rights Reserved.
@@ -1,4 +1,4 @@
1
- import { I as t, J as s, K as a } from "./index-Du-bmOP8.js";
1
+ import { V as t, Q as s, U as a } from "./index-CamYe_M8.js";
2
2
  /**
3
3
  * @license
4
4
  * Copyright 2018 Google LLC. All Rights Reserved.
@@ -1,4 +1,4 @@
1
- import { A as r, b as c, f as h, s as g, e as o } from "../index-Du-bmOP8.js";
1
+ import { A as r, b as c, f as h, s as g, e as o } from "../index-CamYe_M8.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,5 +1,5 @@
1
- import { t as u } from "../index-Du-bmOP8.js";
2
- import { d as z, i as f } from "../dataset-iqT4Otvb.js";
1
+ import { t as u } from "../index-CamYe_M8.js";
2
+ import { d as z, i as f } from "../dataset-BFFipD1c.js";
3
3
  import "../index-Tf7vU29b.js";
4
4
  /**
5
5
  * @license
@@ -1,7 +1,7 @@
1
1
  import { generateText as T } 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-Du-bmOP8.js";
4
+ import { a as h } from "../index-CamYe_M8.js";
5
5
  import y from "../utilities/profile.js";
6
6
  const D = {
7
7
  desiredLoss: 0.01,
@@ -1,8 +1,8 @@
1
1
  import { DatasetBuilder as g, flattenTokens as m, PAGE_FACTOR as u } from "./DatasetBuilder.js";
2
2
  import f from "./AdamExt.js";
3
- import { t as y, v as z, a as c } from "../index-Du-bmOP8.js";
4
- import { n as S } from "../norm-01kY9I2B.js";
5
- import { z as p } from "../zeros-BaHhQTWf.js";
3
+ import { t as y, v as z, a as c } from "../index-CamYe_M8.js";
4
+ import { n as S } from "../norm-B9dQTFYn.js";
5
+ import { z as p } from "../zeros-DUkkVccu.js";
6
6
  class R {
7
7
  constructor(t, e, s = 1e-3) {
8
8
  this.tokenizer = e, this.model = t, this.learningRate = s, this.resetOptimizer(), this.datasetBuilder = new g(e, t.config.gpt.blockSize);
@@ -1,9 +1,9 @@
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-Du-bmOP8.js";
4
- import { s as v } from "../softmax-DhWoBa7r.js";
5
- import { m as z } from "../max-0Xnlpv8k.js";
6
- import { l as k } from "../log_sum_exp-CxfBtUaG.js";
3
+ import { e as u, c as i, z as S, t as f, s as G } from "../index-CamYe_M8.js";
4
+ import { s as v } from "../softmax-DvMvui-_.js";
5
+ import { m as z } from "../max-JBBv7aUf.js";
6
+ import { l as k } from "../log_sum_exp-CV_5-TTu.js";
7
7
  function F(a, s) {
8
8
  return f(() => {
9
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);
@@ -1,3 +1,9 @@
1
1
  import { default as NanoGPT } from '../NanoGPTModel';
2
2
  export declare function dummyPassAsync(model: NanoGPT): Promise<void>;
3
+ export interface MemoryRequirements {
4
+ perBatch: number;
5
+ tapeSize: number;
6
+ gradients: number;
7
+ }
8
+ export declare function dummyPassTrainAsync(model: NanoGPT): Promise<MemoryRequirements>;
3
9
  export declare function dummyPass(model: NanoGPT): void;
@@ -1,14 +1,35 @@
1
- import "../index-Du-bmOP8.js";
2
- import { z as n } from "../zeros-BaHhQTWf.js";
3
- async function c(s) {
4
- const i = n([1, s.config.gpt.blockSize], "int32"), [t, o] = s.forward({ training: !1 }, i);
5
- await t.data(), t.dispose(), o && o.dispose(), i.dispose();
1
+ import { m as f, v as S, e as w } from "../index-CamYe_M8.js";
2
+ import { z as i } from "../zeros-DUkkVccu.js";
3
+ async function P(s) {
4
+ const t = i([1, s.config.gpt.blockSize], "int32"), [e, n] = s.forward({ training: !1 }, t);
5
+ await e.data(), e.dispose(), n && n.dispose(), t.dispose();
6
6
  }
7
- function d(s) {
8
- const i = n([1, s.config.gpt.blockSize], "int32"), [t, o] = s.forward({ training: !1 }, i);
9
- t.dispose(), o && o.dispose(), i.dispose();
7
+ async function v(s) {
8
+ const t = f(), e = t.numBytesInGPUAllocated ?? t.numBytes;
9
+ await P(s);
10
+ const n = i([1, s.config.gpt.blockSize], "int32"), r = i([1, s.config.gpt.blockSize], "int32"), o = {
11
+ perBatch: 0,
12
+ tapeSize: 0,
13
+ gradients: s.getNumParams() * 4
14
+ }, g = () => {
15
+ const [a, l] = s.forward({ training: !0 }, n, r), d = w().state.activeTape;
16
+ let u = 0;
17
+ if (d)
18
+ for (const z of d)
19
+ u += z.saved?.reduce((B, I) => B + I.size * 4, 0) || 0;
20
+ return o.tapeSize = u, a.dispose(), l;
21
+ }, { value: c, grads: m } = S(g), p = f(), y = p.numBytesInGPUAllocated ?? p.numBytes;
22
+ o.perBatch = y - e - o.gradients, console.log("Dummy training memory requirements:", o), await c.data(), c.dispose();
23
+ for (const a in m)
24
+ m[a].dispose();
25
+ return n.dispose(), r.dispose(), o;
26
+ }
27
+ function A(s) {
28
+ const t = i([1, s.config.gpt.blockSize], "int32"), [e, n] = s.forward({ training: !1 }, t);
29
+ e.dispose(), n && n.dispose(), t.dispose();
10
30
  }
11
31
  export {
12
- d as dummyPass,
13
- c as dummyPassAsync
32
+ A as dummyPass,
33
+ P as dummyPassAsync,
34
+ v as dummyPassTrainAsync
14
35
  };
@@ -1,6 +1,6 @@
1
- import { t as y } from "../index-Du-bmOP8.js";
2
- import { t as x } from "../tensor2d-CRWjDyUe.js";
3
- import { c as f } from "../concat-DdKPyAtw.js";
1
+ import { t as y } from "../index-CamYe_M8.js";
2
+ import { t as x } from "../tensor2d-jO8JY5Jd.js";
3
+ import { c as f } from "../concat-XOK9ANZu.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");
@@ -1,6 +1,31 @@
1
+ import { default as zip } from 'jszip';
1
2
  import { default as NanoGPT } from '../NanoGPTModel';
2
3
  import { ITokeniser } from '../tokeniser/type';
4
+ export declare const VERSION = 2;
5
+ export interface TransformersConfig {
6
+ model_type: string;
7
+ vocab_size: number;
8
+ hidden_size: number;
9
+ num_hidden_layers: number;
10
+ num_attention_heads: number;
11
+ block_size: number;
12
+ dropout: number;
13
+ biasInLinear: boolean;
14
+ biasInLayerNorm: boolean;
15
+ mlpFactor: number;
16
+ useRope: boolean;
17
+ }
18
+ export interface Metadata {
19
+ version: string;
20
+ application: string;
21
+ name?: string;
22
+ }
23
+ export declare function loadOldModel(zipFile: zip): Promise<{
24
+ model: NanoGPT;
25
+ tokeniser: ITokeniser;
26
+ }>;
3
27
  export declare function loadModel(data: Blob | Buffer | string): Promise<{
4
28
  model: NanoGPT;
5
29
  tokeniser: ITokeniser;
30
+ name?: string;
6
31
  }>;
@@ -1,47 +1,99 @@
1
- import { j as k } from "../jszip.min-CjP2V1VV.js";
2
- import { importWeights as b } from "./weights.js";
3
- import u from "../tokeniser/CharTokeniser.js";
4
- import F from "../NanoGPTModel.js";
5
- import { dummyPassAsync as j } from "./dummy.js";
6
- import { d as T } from "../index-Du-bmOP8.js";
7
- import E from "../tokeniser/bpe.js";
8
- async function A(t) {
9
- const o = await fetch(t);
10
- if (!o.ok)
11
- throw new Error(`Failed to fetch ${t}: ${o.statusText}`);
12
- return o.arrayBuffer();
1
+ import { j as v } from "../jszip.min-CjP2V1VV.js";
2
+ import { importWeights as F } from "./weights.js";
3
+ import h from "../tokeniser/CharTokeniser.js";
4
+ import b from "../NanoGPTModel.js";
5
+ import { dummyPassAsync as u } from "./dummy.js";
6
+ import { d as k } from "../index-CamYe_M8.js";
7
+ import j from "../tokeniser/bpe.js";
8
+ import { load_safetensors as N } from "./safetensors.js";
9
+ const I = 2;
10
+ async function O(t) {
11
+ const s = await fetch(t);
12
+ if (!s.ok)
13
+ throw new Error(`Failed to fetch ${t}: ${s.statusText}`);
14
+ return s.arrayBuffer();
13
15
  }
14
- async function P(t) {
15
- const o = typeof t == "string" ? await A(t) : t, n = await k.loadAsync(o), i = /* @__PURE__ */ new Map(), f = await n.file("manifest.json")?.async("string");
16
- if (!f)
16
+ async function S(t) {
17
+ const s = /* @__PURE__ */ new Map(), r = await t.file("manifest.json")?.async("string");
18
+ if (!r)
17
19
  throw new Error("Manifest file not found in the zip archive");
18
- const l = JSON.parse(f);
19
- for (const [e, r] of Object.entries(l.weightSpec))
20
- i.set(e, { spec: r, data: new Float32Array() });
21
- const p = await n.file("tokeniser.json")?.async("string");
22
- if (!p)
20
+ const p = JSON.parse(r);
21
+ for (const [o, a] of Object.entries(p.weightSpec))
22
+ s.set(o, { spec: a, data: new Float32Array() });
23
+ const e = await t.file("tokeniser.json")?.async("string");
24
+ if (!e)
23
25
  throw new Error("Tokeniser file not found in the zip archive");
24
- const s = JSON.parse(p), y = (s.type ?? "char") === "char" ? new u(s.vocab) : new E(s.vocab, s.merges), w = /* @__PURE__ */ new Map();
25
- for (const e of Object.keys(n.files))
26
- if (e.endsWith(".bin")) {
27
- const r = e.replace(".bin", ""), g = await n.file(e).async("arraybuffer"), h = new Float32Array(g), c = i.get(r) || { spec: [], data: new Float32Array() };
28
- c.data = h, i.set(r, c);
29
- const d = await b(c);
30
- w.set(r, d);
26
+ const i = JSON.parse(e), c = (i.type ?? "char") === "char" ? new h(i.vocab) : new j(i.vocab, i.merges), d = /* @__PURE__ */ new Map();
27
+ for (const o of Object.keys(t.files))
28
+ if (o.endsWith(".bin")) {
29
+ const a = o.replace(".bin", ""), w = await t.file(o).async("arraybuffer"), g = new Float32Array(w), l = s.get(a) || { spec: [], data: new Float32Array() };
30
+ l.data = g, s.set(a, l);
31
+ const n = await F(l);
32
+ d.set(a, n);
31
33
  }
32
- T();
33
- const a = new F(l.config);
34
- await j(a), a.loadWeights(w);
35
- const m = await n.file("log.json")?.async("string");
34
+ k();
35
+ const f = new b(p.config);
36
+ await u(f), f.loadWeights(d);
37
+ const m = await t.file("log.json")?.async("string");
36
38
  if (m)
37
39
  try {
38
- const e = JSON.parse(m);
39
- a.log = e;
40
- } catch (e) {
41
- throw console.error("Error parsing training log:", e), new Error(`Failed to parse training log: ${e}`);
40
+ const o = JSON.parse(m);
41
+ f.log = o;
42
+ } catch (o) {
43
+ throw console.error("Error parsing training log:", o), new Error(`Failed to parse training log: ${o}`);
42
44
  }
43
- return { model: a, tokeniser: y };
45
+ return { model: f, tokeniser: c };
46
+ }
47
+ async function R(t) {
48
+ const s = typeof t == "string" ? await O(t) : t, r = await v.loadAsync(s);
49
+ if (r.file("manifest.json"))
50
+ return S(r);
51
+ {
52
+ const p = await r.file("config.json")?.async("string");
53
+ if (!p)
54
+ throw new Error("Config file not found in the zip archive");
55
+ const e = JSON.parse(p), i = {
56
+ vocabSize: e.vocab_size,
57
+ blockSize: e.block_size,
58
+ nLayer: e.num_hidden_layers,
59
+ nHead: e.num_attention_heads,
60
+ nEmbed: e.hidden_size,
61
+ dropout: e.dropout,
62
+ biasInLinear: e.biasInLinear,
63
+ biasInLayerNorm: e.biasInLayerNorm,
64
+ mlpFactor: e.mlpFactor,
65
+ useRope: e.useRope
66
+ }, y = await r.file("tokeniser.json")?.async("string");
67
+ if (!y)
68
+ throw new Error("Tokeniser file not found in the zip archive");
69
+ const c = JSON.parse(y), f = (c.type ?? "char") === "char" ? new h(c.vocab) : new j(c.vocab, c.merges), m = await N(await r.file("model.safetensors").async("arraybuffer")), o = /* @__PURE__ */ new Map();
70
+ for (const [n, E] of Object.entries(m))
71
+ o.set(n, [E]);
72
+ k();
73
+ const a = new b(i);
74
+ await u(a), a.loadWeights(o);
75
+ const w = await r.file("meta.json")?.async("string");
76
+ let g;
77
+ if (w)
78
+ try {
79
+ const n = JSON.parse(w);
80
+ n.name && (g = n.name);
81
+ } catch (n) {
82
+ console.error("Error parsing meta file:", n);
83
+ }
84
+ const l = await r.file("log.json")?.async("string");
85
+ if (l)
86
+ try {
87
+ const n = JSON.parse(l);
88
+ a.log = n;
89
+ } catch (n) {
90
+ throw console.error("Error parsing training log:", n), new Error(`Failed to parse training log: ${n}`);
91
+ }
92
+ return { model: a, tokeniser: f, name: g };
93
+ }
44
94
  }
45
95
  export {
46
- P as loadModel
96
+ I as VERSION,
97
+ R as loadModel,
98
+ S as loadOldModel
47
99
  };
@@ -1,3 +1,7 @@
1
+ import { MemoryInfo } from '@tensorflow/tfjs-core';
2
+ export interface ExtendedMemoryInfo extends MemoryInfo {
3
+ numBytesInGPUAllocated?: number;
4
+ }
1
5
  export default class MemoryProfiler {
2
6
  private log;
3
7
  private maxMemory;
@@ -6,6 +10,7 @@ export default class MemoryProfiler {
6
10
  private peakMemory;
7
11
  startMemory(): void;
8
12
  getPeakMemory(): number;
13
+ getMaxMemory(): number;
9
14
  endMemory(label: string): void;
10
15
  printSummary(): void;
11
16
  }
@@ -1,32 +1,35 @@
1
- import { m as s } from "../index-Du-bmOP8.js";
1
+ import { m as a } from "../index-CamYe_M8.js";
2
2
  const m = 1024 * 1024;
3
- class M {
3
+ class l {
4
4
  log = /* @__PURE__ */ new Map();
5
5
  maxMemory = 0;
6
6
  maxLabel;
7
7
  lastMemInfo = [];
8
8
  peakMemory = 0;
9
9
  startMemory() {
10
- this.lastMemInfo.push(s());
10
+ this.lastMemInfo.push(a());
11
11
  }
12
12
  getPeakMemory() {
13
13
  return this.peakMemory;
14
14
  }
15
- endMemory(e) {
15
+ getMaxMemory() {
16
+ return this.maxMemory;
17
+ }
18
+ endMemory(o) {
16
19
  if (this.lastMemInfo.length === 0) {
17
20
  console.warn("MemoryProfiler: endMemory called without matching startMemory");
18
21
  return;
19
22
  }
20
- const o = s(), t = o.numBytes - (this.lastMemInfo.pop()?.numBytes || 0);
21
- this.log.set(e, Math.max(this.log.get(e) || 0, t)), t > this.maxMemory && (this.maxMemory = t, this.maxLabel = e), this.peakMemory = Math.max(this.peakMemory, o.numBytes);
23
+ const e = a(), s = this.lastMemInfo.pop(), t = (e.numBytesInGPUAllocated ?? e.numBytes) - (s?.numBytes ?? s?.numBytesInGPUAllocated ?? 0);
24
+ this.log.set(o, Math.max(this.log.get(o) || 0, t)), t > this.maxMemory && (this.maxMemory = t, this.maxLabel = o), this.peakMemory = Math.max(this.peakMemory, e.numBytesInGPUAllocated ?? e.numBytes);
22
25
  }
23
26
  printSummary() {
24
27
  console.log("Memory Usage Summary:");
25
- for (const [e, o] of this.log.entries())
26
- console.log(`- ${e}: ${(o / m).toFixed(2)} MB`);
28
+ for (const [o, e] of this.log.entries())
29
+ console.log(`- ${o}: ${(e / m).toFixed(2)} MB`);
27
30
  this.maxLabel && console.log(`Peak Memory Usage: ${(this.maxMemory / m).toFixed(2)} MB at "${this.maxLabel}"`), console.log(`Overall Peak Memory Usage: ${(this.peakMemory / m).toFixed(2)} MB`);
28
31
  }
29
32
  }
30
33
  export {
31
- M as default
34
+ l as default
32
35
  };
@@ -0,0 +1,3 @@
1
+ import { Tensor } from '@tensorflow/tfjs-core';
2
+ export declare function save_safetensors(tensors: Record<string, Tensor>): Promise<ArrayBuffer>;
3
+ export declare function load_safetensors(buffer: ArrayBuffer): Promise<Record<string, Tensor>>;
@@ -0,0 +1,83 @@
1
+ import "../index-CamYe_M8.js";
2
+ import { t as y } from "../tensor-CDz5x1mP.js";
3
+ function l(t) {
4
+ if (t === "float32") return "F32";
5
+ if (t === "int32") return "I32";
6
+ throw new Error(`Unsupported dtype: ${t}`);
7
+ }
8
+ function h(t) {
9
+ if (t === "F32") return "float32";
10
+ if (t === "I32") return "int32";
11
+ throw new Error(`Unsupported dtype: ${t}`);
12
+ }
13
+ async function _(t) {
14
+ const c = {};
15
+ let a = 0;
16
+ for (const [n, s] of Object.entries(t))
17
+ c[n] = {
18
+ dtype: l(s.dtype),
19
+ shape: s.shape,
20
+ data_offsets: [a, a + s.size * 4]
21
+ }, a += s.size * 4;
22
+ const p = JSON.stringify(c);
23
+ let r = new TextEncoder().encode(p);
24
+ if (r.length % 4 !== 0) {
25
+ const n = 4 - r.length % 4, s = new Uint8Array(r.length + n);
26
+ s.set(r);
27
+ for (let w = r.length; w < s.length; w++)
28
+ s[w] = 32;
29
+ r = s;
30
+ }
31
+ const o = r.length, f = 8 + o + a, e = new ArrayBuffer(f);
32
+ new DataView(e).setUint32(0, o, !0), new Uint8Array(e, 8, o).set(r);
33
+ let d = 8 + o;
34
+ for (const n of Object.values(t)) {
35
+ if (n.size === 0) continue;
36
+ const s = await n.data();
37
+ if (n.dtype === "float32")
38
+ new Float32Array(e, d, n.size).set(s), d += n.size * 4;
39
+ else if (n.dtype === "int32")
40
+ new Int32Array(e, d, n.size).set(s), d += n.size * 4;
41
+ else
42
+ throw new Error(`Unsupported dtype: ${n.dtype}`);
43
+ }
44
+ return e;
45
+ }
46
+ async function U(t) {
47
+ const a = new DataView(t).getUint32(0, !0), p = new Uint8Array(t, 8, a), r = JSON.parse(new TextDecoder().decode(p)), o = {};
48
+ for (const [f, e] of Object.entries(r)) {
49
+ if (e.data_offsets[0] === e.data_offsets[1]) {
50
+ o[f] = y([], e.shape, h(e.dtype));
51
+ continue;
52
+ }
53
+ if (e.dtype === "F32") {
54
+ const i = y(
55
+ new Float32Array(
56
+ t,
57
+ e.data_offsets[0] + 8 + a,
58
+ (e.data_offsets[1] - e.data_offsets[0]) / 4
59
+ ),
60
+ e.shape,
61
+ h(e.dtype)
62
+ );
63
+ o[f] = i;
64
+ } else if (e.dtype === "I32") {
65
+ const i = y(
66
+ new Int32Array(
67
+ t,
68
+ e.data_offsets[0] + 8 + a,
69
+ (e.data_offsets[1] - e.data_offsets[0]) / 4
70
+ ),
71
+ e.shape,
72
+ h(e.dtype)
73
+ );
74
+ o[f] = i;
75
+ } else
76
+ throw new Error(`Unsupported dtype: ${e.dtype}`);
77
+ }
78
+ return o;
79
+ }
80
+ export {
81
+ U as load_safetensors,
82
+ _ as save_safetensors
83
+ };
@@ -1,43 +1,61 @@
1
- import { j as g } from "../jszip.min-CjP2V1VV.js";
2
- import { exportWeights as l } from "./weights.js";
3
- import p from "../tokeniser/CharTokeniser.js";
4
- const b = "1.0.0";
5
- async function h(t, a, i) {
6
- const c = i?.includeLog ?? !0, f = /* @__PURE__ */ new Map();
7
- t.saveWeights(f);
8
- const e = new g(), r = {};
9
- for (const [n, s] of f) {
10
- const o = await l(s);
11
- r[n] = o.spec, e.file(`${n}.bin`, o.data.buffer, { binary: !0 });
12
- }
13
- if (e.file(
14
- "manifest.json",
15
- JSON.stringify({
16
- weightSpec: r,
17
- config: t.config.gpt,
18
- version: b,
19
- application: "@genai-fi/nanogpt",
20
- meta: i?.metadata,
21
- name: i?.name
22
- }),
1
+ import { j as p } from "../jszip.min-CjP2V1VV.js";
2
+ import b from "../tokeniser/CharTokeniser.js";
3
+ import { save_safetensors as l } from "./safetensors.js";
4
+ import { VERSION as y } from "./load.js";
5
+ async function N(e, a, n) {
6
+ const f = n?.includeLog ?? !0, s = /* @__PURE__ */ new Map();
7
+ e.saveWeights(s);
8
+ const i = new p(), r = {};
9
+ s.forEach((t, o) => {
10
+ t.length === 1 && (r[o] = t[0]);
11
+ });
12
+ const g = await l(r);
13
+ i.file("model.safetensors", g, { binary: !0 });
14
+ const c = {
15
+ model_type: "GenAI_NanoGPT_1",
16
+ vocab_size: a.getVocab().length,
17
+ hidden_size: e.config.gpt.nEmbed,
18
+ num_hidden_layers: e.config.gpt.nLayer,
19
+ num_attention_heads: e.config.gpt.nHead,
20
+ block_size: e.config.gpt.blockSize,
21
+ dropout: e.config.gpt.dropout,
22
+ biasInLinear: e.config.gpt.biasInLinear,
23
+ biasInLayerNorm: e.config.gpt.biasInLayerNorm,
24
+ mlpFactor: e.config.gpt.mlpFactor,
25
+ useRope: e.config.gpt.useRope
26
+ };
27
+ if (i.file("config.json", JSON.stringify(c, void 0, 4), {
28
+ binary: !1
29
+ }), i.file(
30
+ "meta.json",
31
+ JSON.stringify(
32
+ {
33
+ version: y,
34
+ application: "@genai-fi/nanogpt",
35
+ meta: n?.metadata,
36
+ name: n?.name
37
+ },
38
+ void 0,
39
+ 4
40
+ ),
23
41
  {
24
42
  binary: !1
25
43
  }
26
- ), e.file(
44
+ ), i.file(
27
45
  "tokeniser.json",
28
46
  JSON.stringify({
29
- type: a instanceof p ? "char" : "bpe",
47
+ type: a instanceof b ? "char" : "bpe",
30
48
  vocab: a.getVocab(),
31
49
  merges: await a.getMerges()
32
50
  }),
33
51
  {
34
52
  binary: !1
35
53
  }
36
- ), c && e.file("log.json", JSON.stringify(t.log), { binary: !1 }), i?.files)
37
- for (const [n, s] of Object.entries(i.files))
38
- e.file(n, JSON.stringify(s), { binary: !1 });
39
- return e.generateAsync({ type: "blob" });
54
+ ), f && i.file("log.json", JSON.stringify(e.log), { binary: !1 }), n?.files)
55
+ for (const [t, o] of Object.entries(n.files))
56
+ i.file(t, JSON.stringify(o), { binary: !1 });
57
+ return i.generateAsync({ type: "blob" });
40
58
  }
41
59
  export {
42
- h as saveModel
60
+ N as saveModel
43
61
  };
@@ -1,5 +1,5 @@
1
- import "../index-Du-bmOP8.js";
2
- import { t as p } from "../tensor-DgTOPY6h.js";
1
+ import "../index-CamYe_M8.js";
2
+ import { t as p } from "../tensor-CDz5x1mP.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-Du-bmOP8.js";
1
+ import { E as i } from "./index-CamYe_M8.js";
2
2
  /**
3
3
  * @license
4
4
  * Copyright 2018 Google LLC. All Rights Reserved.