@genai-fi/nanogpt 0.23.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.
Files changed (109) hide show
  1. package/README.md +78 -281
  2. package/dist/{DatasetBuilder-C0iJT29K.js → DatasetBuilder-DU1G1OKX.js} +20 -23
  3. package/dist/{RealDiv-CNsvC4AU.js → RealDiv-CSnvtN2E.js} +20 -20
  4. package/dist/{Reshape-dnm9bO3B.js → Reshape-BlylqwWy.js} +12 -12
  5. package/dist/TeachableLLM.d.ts +10 -15
  6. package/dist/TeachableLLM.js +201 -2
  7. package/dist/api/responses.d.ts +81 -0
  8. package/dist/api/responses.js +169 -0
  9. package/dist/api/training.d.ts +70 -0
  10. package/dist/api/training.js +205 -0
  11. package/dist/data/docx.js +9 -3036
  12. package/dist/data/stream.d.ts +8 -8
  13. package/dist/data/stream.js +1 -1
  14. package/dist/data/textLoader.d.ts +1 -1
  15. package/dist/data/textLoader.js +2 -2
  16. package/dist/data.d.ts +3 -0
  17. package/dist/data.js +12 -0
  18. package/dist/{dist-BqAU9-yi.js → dist-CwK5S7Ls.js} +2168 -2168
  19. package/dist/{gpgpu_math-DBYEAAdI.js → gpgpu_math-20tPK8LM.js} +458 -458
  20. package/dist/{Generator.d.ts → inference/Generator.d.ts} +16 -44
  21. package/dist/inference/Generator.js +271 -0
  22. package/dist/inference/tokenisePrompt.d.ts +4 -0
  23. package/dist/inference/tokenisePrompt.js +13 -0
  24. package/dist/inference/types.d.ts +44 -8
  25. package/dist/inference/utilities.d.ts +9 -0
  26. package/dist/inference/utilities.js +20 -0
  27. package/dist/jszip.min-DKa1Rjyn.js +3033 -0
  28. package/dist/{kernel_funcs_utils-D-mATnGR.js → kernel_funcs_utils-ql8Y8qPn.js} +96 -93
  29. package/dist/layers/MLP.d.ts +1 -1
  30. package/dist/layers/PositionEmbedding.d.ts +2 -1
  31. package/dist/layers/PositionEmbedding.js +1 -1
  32. package/dist/layers/RMSNorm.d.ts +1 -1
  33. package/dist/layers/TiedEmbedding.js +1 -1
  34. package/dist/layers.d.ts +4 -0
  35. package/dist/layers.js +14 -0
  36. package/dist/loader/load.js +58 -2
  37. package/dist/loader/loadHF.d.ts +1 -1
  38. package/dist/loader/loadHF.js +17 -2
  39. package/dist/loader/loadTransformers.js +46 -2
  40. package/dist/loader/newZipLoad.js +25 -2
  41. package/dist/loader/oldZipLoad.d.ts +1 -1
  42. package/dist/loader/oldZipLoad.js +37 -2
  43. package/dist/loader/save.js +75 -2
  44. package/dist/loader/types.d.ts +3 -3
  45. package/dist/main.d.ts +34 -43
  46. package/dist/main.js +12327 -20
  47. package/dist/{matMulGelu-BAIgQaRx.js → matMulGelu-CBoqTZM7.js} +2 -2
  48. package/dist/models/NanoGPTV1.js +95 -2
  49. package/dist/models/NanoGPTV2.js +86 -2
  50. package/dist/models/factory.js +13 -2
  51. package/dist/models/model.js +76 -2
  52. package/dist/models.d.ts +4 -0
  53. package/dist/models.js +14 -0
  54. package/dist/ops/dot16.js +1 -1
  55. package/dist/ops/matMulGelu.js +1 -1
  56. package/dist/ops/webgl/adamAdjust.js +1 -1
  57. package/dist/ops/webgl/fusedSoftmax.js +2 -2
  58. package/dist/ops/webgl/gelu.js +2 -2
  59. package/dist/ops/webgl/log.js +5 -5
  60. package/dist/ops/webgl/matMulGelu.js +1 -1
  61. package/dist/ops/webgl/matMulMul.js +1 -1
  62. package/dist/{stream-BjdpSNqB.js → stream-BpAwcvHz.js} +565 -561
  63. package/dist/{tfjs_backend-CydPRQTc.js → tfjs_backend-h5weiy1O.js} +36 -36
  64. package/dist/tokenise.d.ts +4 -0
  65. package/dist/tokenise.js +15 -0
  66. package/dist/tokeniser/CharTokeniser.js +18 -20
  67. package/dist/tokeniser/bpe.js +18 -22
  68. package/dist/training/BasicTrainer.d.ts +5 -10
  69. package/dist/training/BasicTrainer.js +80 -88
  70. package/dist/training/DatasetBuilder.d.ts +4 -4
  71. package/dist/training/DatasetBuilder.js +1 -1
  72. package/dist/training/PreTrainer.js +1 -1
  73. package/dist/training/SFTTrainer.js +1 -1
  74. package/dist/training/configure.d.ts +3 -0
  75. package/dist/training/configure.js +32 -0
  76. package/dist/training/factory.d.ts +6 -0
  77. package/dist/training/factory.js +8 -0
  78. package/dist/training/prepareData.d.ts +22 -0
  79. package/dist/training/prepareData.js +49 -0
  80. package/dist/training/tasks/TokenStore.d.ts +2 -1
  81. package/dist/training/tasks/TokenStore.js +8 -5
  82. package/dist/training/tasks/tokenStream.d.ts +17 -0
  83. package/dist/training/tasks/tokenStream.js +46 -0
  84. package/dist/training/types.d.ts +14 -1
  85. package/dist/training/validateOptions.d.ts +2 -0
  86. package/dist/training/validateOptions.js +19 -0
  87. package/dist/training/validation.js +4 -2
  88. package/dist/utilities/arrayShape.d.ts +1 -0
  89. package/dist/utilities/arrayShape.js +8 -0
  90. package/dist/utilities/random.d.ts +1 -0
  91. package/dist/utilities/random.js +19 -0
  92. package/dist/utilities/waitForModel.d.ts +1 -1
  93. package/dist/v4-BK7K-jy_.js +30 -0
  94. package/package.json +8 -2
  95. package/dist/Generator.js +0 -2
  96. package/dist/Trainer-DBsyWJ4s.js +0 -228
  97. package/dist/Trainer.d.ts +0 -45
  98. package/dist/Trainer.js +0 -2
  99. package/dist/main-BSaDGH7I.js +0 -13274
  100. package/dist/training/tasks/ConversationTask.d.ts +0 -17
  101. package/dist/training/tasks/ConversationTask.js +0 -29
  102. package/dist/training/tasks/PretrainingTask.d.ts +0 -17
  103. package/dist/training/tasks/PretrainingTask.js +0 -42
  104. package/dist/training/tasks/StartSentenceTask.d.ts +0 -18
  105. package/dist/training/tasks/StartSentenceTask.js +0 -45
  106. package/dist/training/tasks/Task.d.ts +0 -29
  107. package/dist/training/tasks/Task.js +0 -50
  108. package/dist/training/tasks/splitter.d.ts +0 -5
  109. package/dist/training/tasks/splitter.js +0 -18
@@ -1,8 +1,25 @@
1
1
  import { Ci as e, Di as t, In as n, Ms as r, Ya as i, ca as a, ko as o, oc as s, to as c } from "./dist-Da20xy8E.js";
2
2
  import { r as l } from "./backend_util-DAV0I34P.js";
3
- import { a as u, o as d } from "./gpgpu_math-DBYEAAdI.js";
3
+ import { Z as u, a as d } from "./gpgpu_math-20tPK8LM.js";
4
+ //#region node_modules/@tensorflow/tfjs-backend-webgl/dist/binaryop_gpu.js
5
+ var f = "\n if (isnan(a)) return a;\n if (isnan(b)) return b;\n", p = class {
6
+ constructor(e, t, r) {
7
+ this.variableNames = ["A", "B"], this.outputShape = n(t, r), this.enableShapeUniforms = d(this.outputShape.length), this.userCode = `
8
+ float binaryOperation(float a, float b) {
9
+ ${e}
10
+ }
11
+
12
+ void main() {
13
+ float a = getAAtOutCoords();
14
+ float b = getBAtOutCoords();
15
+ setOutput(binaryOperation(a, b));
16
+ }
17
+ `;
18
+ }
19
+ };
20
+ //#endregion
4
21
  //#region node_modules/@tensorflow/tfjs-backend-webgl/dist/packing_util.js
5
- function f(e, t) {
22
+ function m(e, t) {
6
23
  return [
7
24
  "x",
8
25
  "y",
@@ -12,77 +29,33 @@ function f(e, t) {
12
29
  "v"
13
30
  ].slice(0, t).map((t) => `${e}.${t}`);
14
31
  }
15
- function p(e, t) {
16
- return t === 1 ? [e] : f(e, t);
32
+ function h(e, t) {
33
+ return t === 1 ? [e] : m(e, t);
17
34
  }
18
- function m(e, t) {
35
+ function g(e, t) {
19
36
  if (e === 1) return "rc";
20
37
  let n = "";
21
38
  for (let r = 0; r < e; r++) n += t[r], r < e - 1 && (n += ",");
22
39
  return n;
23
40
  }
24
41
  //#endregion
25
- //#region node_modules/@tensorflow/tfjs-backend-webgl/dist/unaryop_gpu.js
26
- var h = class {
27
- constructor(e, t) {
28
- this.variableNames = ["A"], this.outputShape = e, this.enableShapeUniforms = u(this.outputShape.length), this.userCode = `
29
- float unaryOperation(float x) {
30
- ${t}
31
- }
32
-
33
- void main() {
34
- float x = getAAtOutCoords();
35
- float y = unaryOperation(x);
36
-
37
- setOutput(y);
38
- }
39
- `;
40
- }
41
- }, g = "if (isnan(x)) return x;", _ = "return x;", v = "return abs(x);", y = "return (x >= 0.0) ? x : (exp(x) - 1.0);", b = g + "\n return (x < 0.0) ? 0.0 : x;\n", x = g + "\n return (x < 0.0) ? 0.0 : min(6.0, x);\n", S = "return x;", C = "return 1.0 / (1.0 + exp(-1.0 * x));", w = "return x;", T = "\n vec4 result;\n\n result.r = (x.r >= 0.0) ? x.r : (exp(x.r) - 1.0);\n result.g = (x.g >= 0.0) ? x.g : (exp(x.g) - 1.0);\n result.b = (x.b >= 0.0) ? x.b : (exp(x.b) - 1.0);\n result.a = (x.a >= 0.0) ? x.a : (exp(x.a) - 1.0);\n\n return result;\n", E = "\n vec4 result = x * vec4(greaterThanEqual(x, vec4(0.0)));\n bvec4 isNaN = isnan(x);\n\n result.r = isNaN.r ? x.r : result.r;\n result.g = isNaN.g ? x.g : result.g;\n result.b = isNaN.b ? x.b : result.b;\n result.a = isNaN.a ? x.a : result.a;\n\n return result;\n", D = "\n vec4 result = min(x, vec4(6.)) * vec4(greaterThanEqual(x, vec4(0.0)));\n bvec4 isNaN = isnan(x);\n\n result.r = isNaN.r ? x.r : result.r;\n result.g = isNaN.g ? x.g : result.g;\n result.b = isNaN.b ? x.b : result.b;\n result.a = isNaN.a ? x.a : result.a;\n\n return result;\n", O = "return 1.0 / (1.0 + exp(-1.0 * x));", k = class {
42
- constructor(e, t) {
43
- this.variableNames = ["A"], this.packedInputs = !0, this.packedOutput = !0, this.outputShape = e, this.enableShapeUniforms = u(this.outputShape.length), this.userCode = `
44
- vec4 unaryOperation(vec4 x) {
45
- ${t}
46
- }
47
-
48
- void main() {
49
- vec4 x = getAAtOutCoords();
50
- vec4 y = unaryOperation(x);
51
-
52
- setOutput(y);
53
- }
54
- `;
55
- }
56
- }, A = "\n if (isnan(a)) return a;\n if (isnan(b)) return b;\n", j = class {
57
- constructor(e, t, r) {
58
- this.variableNames = ["A", "B"], this.outputShape = n(t, r), this.enableShapeUniforms = u(this.outputShape.length), this.userCode = `
59
- float binaryOperation(float a, float b) {
60
- ${e}
61
- }
62
-
63
- void main() {
64
- float a = getAAtOutCoords();
65
- float b = getBAtOutCoords();
66
- setOutput(binaryOperation(a, b));
67
- }
68
- `;
69
- }
70
- }, M = "\n result.r = isNaN.r ? NAN : result.r;\n result.g = isNaN.g ? NAN : result.g;\n result.b = isNaN.b ? NAN : result.b;\n result.a = isNaN.a ? NAN : result.a;\n", N = class {
42
+ //#region node_modules/@tensorflow/tfjs-backend-webgl/dist/binaryop_packed_gpu.js
43
+ var _ = "\n result.r = isNaN.r ? NAN : result.r;\n result.g = isNaN.g ? NAN : result.g;\n result.b = isNaN.b ? NAN : result.b;\n result.a = isNaN.a ? NAN : result.a;\n", v = class {
71
44
  constructor(e, t, r, i = !1) {
72
45
  this.variableNames = ["A", "B"], this.supportsBroadcasting = !0, this.packedInputs = !0, this.packedOutput = !0, this.outputShape = n(t, r);
73
46
  let a = this.outputShape.length;
74
- this.enableShapeUniforms = u(a);
47
+ this.enableShapeUniforms = d(a);
75
48
  let o = "";
76
49
  if (i) if (a === 0 || s(this.outputShape) === 1) o = "\n result.y = 0.;\n result.z = 0.;\n result.w = 0.;\n ";
77
50
  else if (o = `
78
- ${d(a)} coords = getOutputCoords();
51
+ ${u(a)} coords = getOutputCoords();
79
52
  `, a === 1) this.enableShapeUniforms ? o += "\n result.y = (coords + 1) >= outShape ? 0. : result.y;\n result.z = 0.;\n result.w = 0.;\n " : o += `
80
53
  result.y = (coords + 1) >= ${this.outputShape[0]} ? 0. : result.y;
81
54
  result.z = 0.;
82
55
  result.w = 0.;
83
56
  `;
84
57
  else {
85
- let e = p("coords", a);
58
+ let e = h("coords", a);
86
59
  this.enableShapeUniforms ? o += `
87
60
  bool nextRowOutOfBounds =
88
61
  (${e[a - 2]} + 1) >= outShape[${a} - 2];
@@ -120,7 +93,7 @@ var h = class {
120
93
  };
121
94
  //#endregion
122
95
  //#region node_modules/@tensorflow/tfjs-backend-webgl/dist/kernels/Identity.js
123
- function P(e) {
96
+ function y(e) {
124
97
  let { inputs: t, backend: n } = e, { x: r } = t;
125
98
  return n.incRef(r.dataId), {
126
99
  dataId: r.dataId,
@@ -128,48 +101,78 @@ function P(e) {
128
101
  dtype: r.dtype
129
102
  };
130
103
  }
131
- var F = {
104
+ var b = {
132
105
  kernelName: i,
133
106
  backendName: "webgl",
134
- kernelFunc: P
107
+ kernelFunc: y
135
108
  };
136
109
  //#endregion
137
110
  //#region node_modules/@tensorflow/tfjs-backend-webgl/dist/kernels/Complex.js
138
- function I(e) {
111
+ function x(e) {
139
112
  let { inputs: t, backend: n } = e, { real: r, imag: i } = t, a = n.makeTensorInfo(r.shape, "complex64"), o = n.texData.get(a.dataId);
140
113
  return o.complexTensorInfos = {
141
- real: P({
114
+ real: y({
142
115
  inputs: { x: r },
143
116
  backend: n
144
117
  }),
145
- imag: P({
118
+ imag: y({
146
119
  inputs: { x: i },
147
120
  backend: n
148
121
  })
149
122
  }, a;
150
123
  }
151
- var L = {
124
+ var S = {
152
125
  kernelName: a,
153
126
  backendName: "webgl",
154
- kernelFunc: I
155
- }, R = "return (a < 0.) ? b * a : a;", z = "\n vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));\n return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);\n";
156
- function B(e) {
157
- let { inputs: n, backend: i, attrs: a } = e, { x: o } = n, { alpha: s } = a, c = i.makeTensorInfo([], "float32", t(s, "float32")), l = r().getBool("WEBGL_PACK_BINARY_OPERATIONS") ? new N(z, o.shape, c.shape) : new j(R, o.shape, c.shape), u = i.runWebGLProgram(l, [o, c], "float32");
127
+ kernelFunc: x
128
+ }, C = "return (a < 0.) ? b * a : a;", w = "\n vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));\n return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);\n";
129
+ function T(e) {
130
+ let { inputs: n, backend: i, attrs: a } = e, { x: o } = n, { alpha: s } = a, c = i.makeTensorInfo([], "float32", t(s, "float32")), l = r().getBool("WEBGL_PACK_BINARY_OPERATIONS") ? new v(w, o.shape, c.shape) : new p(C, o.shape, c.shape), u = i.runWebGLProgram(l, [o, c], "float32");
158
131
  return i.disposeIntermediateTensorInfo(c), u;
159
132
  }
160
- var V = {
133
+ var E = {
161
134
  kernelName: c,
162
135
  backendName: "webgl",
163
- kernelFunc: B
164
- }, H = "return (a < 0.) ? b * a : a;", U = "\n vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));\n return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);\n";
165
- function W(e) {
166
- let { inputs: t, backend: n } = e, { x: i, alpha: a } = t, o = r().getBool("WEBGL_PACK_BINARY_OPERATIONS") ? new N(U, i.shape, a.shape) : new j(H, i.shape, a.shape);
136
+ kernelFunc: T
137
+ }, D = "return (a < 0.) ? b * a : a;", O = "\n vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));\n return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);\n";
138
+ function k(e) {
139
+ let { inputs: t, backend: n } = e, { x: i, alpha: a } = t, o = r().getBool("WEBGL_PACK_BINARY_OPERATIONS") ? new v(O, i.shape, a.shape) : new p(D, i.shape, a.shape);
167
140
  return n.runWebGLProgram(o, [i, a], "float32");
168
141
  }
169
- var G = {
142
+ var A = {
170
143
  kernelName: o,
171
144
  backendName: "webgl",
172
- kernelFunc: W
145
+ kernelFunc: k
146
+ }, j = class {
147
+ constructor(e, t) {
148
+ this.variableNames = ["A"], this.outputShape = e, this.enableShapeUniforms = d(this.outputShape.length), this.userCode = `
149
+ float unaryOperation(float x) {
150
+ ${t}
151
+ }
152
+
153
+ void main() {
154
+ float x = getAAtOutCoords();
155
+ float y = unaryOperation(x);
156
+
157
+ setOutput(y);
158
+ }
159
+ `;
160
+ }
161
+ }, M = "if (isnan(x)) return x;", N = "return x;", P = "return abs(x);", F = "return (x >= 0.0) ? x : (exp(x) - 1.0);", I = M + "\n return (x < 0.0) ? 0.0 : x;\n", L = M + "\n return (x < 0.0) ? 0.0 : min(6.0, x);\n", R = "return x;", z = "return 1.0 / (1.0 + exp(-1.0 * x));", B = "return x;", V = "\n vec4 result;\n\n result.r = (x.r >= 0.0) ? x.r : (exp(x.r) - 1.0);\n result.g = (x.g >= 0.0) ? x.g : (exp(x.g) - 1.0);\n result.b = (x.b >= 0.0) ? x.b : (exp(x.b) - 1.0);\n result.a = (x.a >= 0.0) ? x.a : (exp(x.a) - 1.0);\n\n return result;\n", H = "\n vec4 result = x * vec4(greaterThanEqual(x, vec4(0.0)));\n bvec4 isNaN = isnan(x);\n\n result.r = isNaN.r ? x.r : result.r;\n result.g = isNaN.g ? x.g : result.g;\n result.b = isNaN.b ? x.b : result.b;\n result.a = isNaN.a ? x.a : result.a;\n\n return result;\n", U = "\n vec4 result = min(x, vec4(6.)) * vec4(greaterThanEqual(x, vec4(0.0)));\n bvec4 isNaN = isnan(x);\n\n result.r = isNaN.r ? x.r : result.r;\n result.g = isNaN.g ? x.g : result.g;\n result.b = isNaN.b ? x.b : result.b;\n result.a = isNaN.a ? x.a : result.a;\n\n return result;\n", W = "return 1.0 / (1.0 + exp(-1.0 * x));", G = class {
162
+ constructor(e, t) {
163
+ this.variableNames = ["A"], this.packedInputs = !0, this.packedOutput = !0, this.outputShape = e, this.enableShapeUniforms = d(this.outputShape.length), this.userCode = `
164
+ vec4 unaryOperation(vec4 x) {
165
+ ${t}
166
+ }
167
+
168
+ void main() {
169
+ vec4 x = getAAtOutCoords();
170
+ vec4 y = unaryOperation(x);
171
+
172
+ setOutput(y);
173
+ }
174
+ `;
175
+ }
173
176
  }, K = "if (isnan(x)) return x;";
174
177
  function q({ opSnippet: e, packedOpSnippet: t, cpuKernelImpl: n, dtype: i }) {
175
178
  return ({ inputs: a, backend: o }) => {
@@ -179,14 +182,14 @@ function q({ opSnippet: e, packedOpSnippet: t, cpuKernelImpl: n, dtype: i }) {
179
182
  return c.makeTensorInfo(s.shape, l, e);
180
183
  }
181
184
  let u = r().getBool("WEBGL_PACK_UNARY_OPERATIONS") && t != null, d;
182
- return d = u ? new k(s.shape, t) : new h(s.shape, e), c.runWebGLProgram(d, [s], l);
185
+ return d = u ? new G(s.shape, t) : new j(s.shape, e), c.runWebGLProgram(d, [s], l);
183
186
  };
184
187
  }
185
188
  function J({ opSnippet: t, packedOpSnippet: n, checkOutOfBounds: i = !1, supportsComplex: a = !1, cpuKernelImpl: o, dtype: s }) {
186
189
  return ({ inputs: c, backend: u }) => {
187
- let { a: d, b: f } = c, p = u;
190
+ let { a: d, b: f } = c, m = u;
188
191
  if (a && d.dtype === "complex64") {
189
- let n = p.texData.get(d.dataId), r = p.texData.get(f.dataId), [i, a] = [[n.complexTensorInfos.real, r.complexTensorInfos.real], [n.complexTensorInfos.imag, r.complexTensorInfos.imag]].map((n) => {
192
+ let n = m.texData.get(d.dataId), r = m.texData.get(f.dataId), [i, a] = [[n.complexTensorInfos.real, r.complexTensorInfos.real], [n.complexTensorInfos.imag, r.complexTensorInfos.imag]].map((n) => {
190
193
  let [r, i] = n, a = {
191
194
  dataId: r.dataId,
192
195
  dtype: r.dtype,
@@ -195,35 +198,35 @@ function J({ opSnippet: t, packedOpSnippet: n, checkOutOfBounds: i = !1, support
195
198
  dataId: i.dataId,
196
199
  dtype: i.dtype,
197
200
  shape: f.shape
198
- }, s = new j(t, d.shape, f.shape);
199
- return p.runWebGLProgram(s, [a, o], e(r.dtype, i.dtype));
200
- }), o = I({
201
+ }, s = new p(t, d.shape, f.shape);
202
+ return m.runWebGLProgram(s, [a, o], e(r.dtype, i.dtype));
203
+ }), o = x({
201
204
  inputs: {
202
205
  real: i,
203
206
  imag: a
204
207
  },
205
- backend: p
208
+ backend: m
206
209
  });
207
- return p.disposeIntermediateTensorInfo(i), p.disposeIntermediateTensorInfo(a), o;
210
+ return m.disposeIntermediateTensorInfo(i), m.disposeIntermediateTensorInfo(a), o;
208
211
  }
209
- let m = s || e(d.dtype, f.dtype);
210
- if ((d.dtype === "string" || f.dtype === "string" || p.shouldExecuteOnCPU([d, f])) && o != null) {
211
- let e = p.texData.get(d.dataId).values, t = p.texData.get(f.dataId).values, n = d.dtype === "string" ? l(e) : e, r = d.dtype === "string" ? l(t) : t, [i, a] = o(d.shape, f.shape, n, r, m), s = p.makeTensorInfo(a, m), c = p.texData.get(s.dataId);
212
+ let h = s || e(d.dtype, f.dtype);
213
+ if ((d.dtype === "string" || f.dtype === "string" || m.shouldExecuteOnCPU([d, f])) && o != null) {
214
+ let e = m.texData.get(d.dataId).values, t = m.texData.get(f.dataId).values, n = d.dtype === "string" ? l(e) : e, r = d.dtype === "string" ? l(t) : t, [i, a] = o(d.shape, f.shape, n, r, h), s = m.makeTensorInfo(a, h), c = m.texData.get(s.dataId);
212
215
  return c.values = i, s;
213
216
  }
214
- let h = r().getBool("WEBGL_PACK_BINARY_OPERATIONS") && n != null, g;
215
- return g = h ? new N(n, d.shape, f.shape, i) : new j(t, d.shape, f.shape), p.runWebGLProgram(g, [d, f], m);
217
+ let g = r().getBool("WEBGL_PACK_BINARY_OPERATIONS") && n != null, _;
218
+ return _ = g ? new v(n, d.shape, f.shape, i) : new p(t, d.shape, f.shape), m.runWebGLProgram(_, [d, f], h);
216
219
  };
217
220
  }
218
221
  function Y(e, t = !1) {
219
- if (e === "linear") return t ? w : _;
220
- if (e === "relu") return t ? E : b;
221
- if (e === "elu") return t ? T : y;
222
- if (e === "relu6") return t ? D : x;
223
- if (e === "prelu") return t ? U : H;
224
- if (e === "leakyrelu") return t ? z : R;
225
- if (e === "sigmoid") return t ? O : C;
222
+ if (e === "linear") return t ? B : N;
223
+ if (e === "relu") return t ? H : I;
224
+ if (e === "elu") return t ? V : F;
225
+ if (e === "relu6") return t ? U : L;
226
+ if (e === "prelu") return t ? O : D;
227
+ if (e === "leakyrelu") return t ? w : C;
228
+ if (e === "sigmoid") return t ? W : z;
226
229
  throw Error(`Activation ${e} has not been implemented for the WebGL backend.`);
227
230
  }
228
231
  //#endregion
229
- export { f as S, g as _, G as a, p as b, L as c, N as d, M as f, v as g, k as h, q as i, P as l, A as m, J as n, V as o, j as p, Y as r, I as s, K as t, F as u, S as v, m as x, h as y };
232
+ export { f as S, _, G as a, m as b, R as c, E as d, x as f, v as g, b as h, q as i, j as l, y as m, J as n, P as o, S as p, Y as r, M as s, K as t, A as u, h as v, p as x, g as y };
@@ -1,6 +1,6 @@
1
1
  import { Tensor } from '@tensorflow/tfjs-core';
2
2
  import { default as BaseLayer, ForwardAttributes } from './BaseLayer';
3
- import { GPTConfig } from '../../main';
3
+ import { GPTConfig } from '../../models/config';
4
4
  export interface MLPConfig {
5
5
  activation?: 'gelu' | 'relu2';
6
6
  hiddenFactor?: number;
@@ -1,6 +1,7 @@
1
1
  import { Tensor } from '@tensorflow/tfjs-core';
2
2
  import { default as BaseLayer } from './BaseLayer';
3
- import { GPTConfig, ModelForwardAttributes } from '../../main';
3
+ import { GPTConfig } from '../../models/config';
4
+ import { ModelForwardAttributes } from '../../models/model';
4
5
  export default class PositionEmbedding extends BaseLayer {
5
6
  private wpe?;
6
7
  constructor(config: GPTConfig, name?: string, parent?: BaseLayer);
@@ -1,5 +1,5 @@
1
1
  import { Tt as e, _n as t, di as n, dt as r, vi as i } from "../dist-Da20xy8E.js";
2
- import { n as a, t as o } from "../dist-BqAU9-yi.js";
2
+ import { n as a, t as o } from "../dist-CwK5S7Ls.js";
3
3
  import s from "./BaseLayer.js";
4
4
  //#region lib/layers/PositionEmbedding.ts
5
5
  var c = class extends s {
@@ -1,6 +1,6 @@
1
1
  import { Tensor } from '@tensorflow/tfjs-core';
2
2
  import { default as BaseLayer, ForwardAttributes } from './BaseLayer';
3
- import { GPTConfig } from '../../main';
3
+ import { GPTConfig } from '../../models/config';
4
4
  export interface RMSNormConfig {
5
5
  useGamma?: boolean;
6
6
  }
@@ -1,5 +1,5 @@
1
1
  import { T as e, an as t } from "../dist-Da20xy8E.js";
2
- import { n } from "../dist-BqAU9-yi.js";
2
+ import { n } from "../dist-CwK5S7Ls.js";
3
3
  import { isPackedTensor as r } from "../utilities/packed.js";
4
4
  import { transpose16 as i } from "../ops/transpose16.js";
5
5
  import { t as a } from "../pack16-BhuXNUS7.js";
@@ -0,0 +1,4 @@
1
+ export { default as CausalSelfAttention } from './layers/CausalSelfAttention';
2
+ export { default as MLP } from './layers/MLP';
3
+ export { default as TransformerBlock } from './layers/TransformerBlock';
4
+ export { default as RoPECache } from './layers/RoPECache';
package/dist/layers.js ADDED
@@ -0,0 +1,14 @@
1
+ import { n as e } from "./chunk-CWhphoD1.js";
2
+ import t from "./layers/CausalSelfAttention.js";
3
+ import n from "./layers/MLP.js";
4
+ import r from "./layers/TransformerBlock.js";
5
+ import i from "./layers/RoPECache.js";
6
+ //#region lib/layers.ts
7
+ var a = /* @__PURE__ */ e({
8
+ CausalSelfAttention: () => t,
9
+ MLP: () => n,
10
+ RoPECache: () => i,
11
+ TransformerBlock: () => r
12
+ });
13
+ //#endregion
14
+ export { t as CausalSelfAttention, n as MLP, i as RoPECache, r as TransformerBlock, a as t };
@@ -1,2 +1,58 @@
1
- import { c as e, l as t } from "../main-BSaDGH7I.js";
2
- export { e as VERSION, t as loadModel };
1
+ import { i as e } from "../chunk-CWhphoD1.js";
2
+ import { t } from "../jszip.min-DKa1Rjyn.js";
3
+ import { load_safetensors as n } from "../utilities/safetensors.js";
4
+ import r from "./oldZipLoad.js";
5
+ import i from "./newZipLoad.js";
6
+ import a from "./loadHF.js";
7
+ import { loadZipMeta as o } from "./loadZipMeta.js";
8
+ //#region lib/loader/load.ts
9
+ var s = /* @__PURE__ */ e(t(), 1), c = 2;
10
+ async function l(e) {
11
+ let t = await fetch(e);
12
+ if (!t.ok) throw Error(`Failed to fetch ${e}: ${t.statusText}`);
13
+ return t.arrayBuffer();
14
+ }
15
+ async function u(e, t, r) {
16
+ let i = e.file("model.safetensors");
17
+ if (!i) return;
18
+ let a = await n(await i.async("arraybuffer")), o = /* @__PURE__ */ new Map();
19
+ for (let [e, t] of Object.entries(a)) o.set(e, [t]);
20
+ t.weightStore.loadWeights(o, r);
21
+ }
22
+ async function d(e, t) {
23
+ let n = e.file("config.json");
24
+ if (!n) return;
25
+ let r = await n.async("string"), i = JSON.parse(r);
26
+ if (i.loraName) {
27
+ if (t.hasLoRA()) throw Error("Model already has LoRA attached");
28
+ t.attachLoRA(i.loraName);
29
+ }
30
+ }
31
+ async function f(e, t) {
32
+ if (t.reference) {
33
+ let n = await p(t.reference);
34
+ return await u(e, n.model, !!t.url), await d(e, n.model), n.model.config.loraName && n.model.attachLoRA(n.model.config.loraName), {
35
+ ...n,
36
+ metaData: {
37
+ ...n.metaData,
38
+ ...t
39
+ }
40
+ };
41
+ } else if (e.file("manifest.json")) return r(e, t);
42
+ else {
43
+ let n = await i(e, t);
44
+ return n.model.config.loraName && n.model.attachLoRA(n.model.config.loraName), n;
45
+ }
46
+ }
47
+ async function p(e, t) {
48
+ if (typeof e == "string") if (e.startsWith("http://") || e.startsWith("https://")) {
49
+ let t = await l(e), n = await s.default.loadAsync(t), r = await o(n);
50
+ return r.url = e, f(n, r);
51
+ } else return a(e);
52
+ else {
53
+ let n = await s.default.loadAsync(e), r = await o(n);
54
+ return r.url = t?.sourceURL || void 0, f(n, r);
55
+ }
56
+ }
57
+ //#endregion
58
+ export { c as VERSION, p as loadModel };
@@ -1,4 +1,4 @@
1
- import { ITokeniser } from '../../main';
1
+ import { ITokeniser } from '../../tokeniser/type';
2
2
  import { default as Model, ModelForwardAttributes } from '../../models/model';
3
3
  import { TransformersMetadata } from './types';
4
4
  export default function loadHuggingFace(name: string): Promise<{
@@ -1,2 +1,17 @@
1
- import { u as e } from "../main-BSaDGH7I.js";
2
- export { e as default };
1
+ import e from "./loadTransformers.js";
2
+ //#region lib/loader/loadHF.ts
3
+ async function t(t) {
4
+ let n = `https://huggingface.co/${t}/resolve/main/config.json`, r = `https://huggingface.co/${t}/resolve/main/tokeniser.json`, i = `https://huggingface.co/${t}/resolve/main/meta.json`, a = `https://huggingface.co/${t}/resolve/main/model.safetensors`, [o, s, c, l] = await Promise.all([
5
+ fetch(n),
6
+ fetch(r),
7
+ fetch(i),
8
+ fetch(a)
9
+ ]);
10
+ if (!o.ok) throw Error(`Failed to fetch config from ${n}: ${o.statusText}`);
11
+ if (!s.ok) throw Error(`Failed to fetch tokeniser from ${r}: ${s.statusText}`);
12
+ if (!c.ok) throw Error(`Failed to fetch meta from ${i}: ${c.statusText}`);
13
+ if (!l.ok) throw Error(`Failed to fetch weights from ${a}: ${l.statusText}`);
14
+ return e(await o.json(), await s.json(), await c.json(), await l.arrayBuffer());
15
+ }
16
+ //#endregion
17
+ export { t as default };
@@ -1,2 +1,46 @@
1
- import { f as e, p as t } from "../main-BSaDGH7I.js";
2
- export { e as default, t as mapTransformersConfigToGPTConfig };
1
+ import e from "../tokeniser/CharTokeniser.js";
2
+ import t from "../tokeniser/bpe.js";
3
+ import { ri as n } from "../dist-Da20xy8E.js";
4
+ import { load_safetensors as r } from "../utilities/safetensors.js";
5
+ import { dummyPassAsync as i } from "../utilities/dummy.js";
6
+ import a from "../models/factory.js";
7
+ //#region lib/loader/loadTransformers.ts
8
+ function o(e) {
9
+ let t;
10
+ return t = e.model_type === "GenAI_NanoGPT_v1" ? {
11
+ modelType: "GenAI_NanoGPT_v1",
12
+ vocabSize: e.vocab_size,
13
+ blockSize: e.block_size,
14
+ nLayer: e.num_hidden_layers,
15
+ nHead: e.num_attention_heads,
16
+ nEmbed: e.hidden_size,
17
+ mlpFactor: e.mlpFactor,
18
+ useRope: e.useRope
19
+ } : {
20
+ modelType: "GenAI_NanoGPT_v2",
21
+ vocabSize: e.vocab_size,
22
+ blockSize: e.block_size,
23
+ nLayer: e.num_hidden_layers,
24
+ nHead: e.num_attention_heads,
25
+ nEmbed: e.hidden_size,
26
+ mlpFactor: e.mlpFactor,
27
+ loraConfig: e.loraConfig ? new Map(Object.entries(e.loraConfig)) : void 0,
28
+ loraName: e.loraName,
29
+ windowSize: e.windowSize
30
+ }, t;
31
+ }
32
+ async function s(s, c, l, u) {
33
+ let d = o(s), f = (c.type ?? "char") === "char" ? new e(c.vocab) : new t(c.vocab, c.merges);
34
+ c.datasetID && (f.datasetID = c.datasetID), c.id ? f.id = c.id : f.generateID();
35
+ let p = await r(u), m = /* @__PURE__ */ new Map();
36
+ for (let [e, t] of Object.entries(p)) m.set(e, [t]);
37
+ n();
38
+ let h = a(d);
39
+ return h.metaData = l, await i(h), h.weightStore.loadWeights(m, !!l.url), {
40
+ model: h,
41
+ tokeniser: f,
42
+ metaData: l
43
+ };
44
+ }
45
+ //#endregion
46
+ export { s as default, o as mapTransformersConfigToGPTConfig };
@@ -1,2 +1,25 @@
1
- import { d as e } from "../main-BSaDGH7I.js";
2
- export { e as default };
1
+ import e from "./loadTransformers.js";
2
+ import { AdamWOptimizer as t } from "../training/AdamW.js";
3
+ //#region lib/loader/newZipLoad.ts
4
+ async function n(n, r) {
5
+ let i = await n.file("config.json")?.async("string");
6
+ if (!i) throw Error("Config file not found in the zip archive");
7
+ let a = JSON.parse(i), o = await n.file("tokeniser.json")?.async("string");
8
+ if (!o) throw Error("Tokeniser file not found in the zip archive");
9
+ let s = JSON.parse(o), c = await n.file("model.safetensors")?.async("arraybuffer");
10
+ if (!c) throw Error("Model weights not found in the zip archive");
11
+ let l = await n.file("optimizer_config.json")?.async("string"), u;
12
+ if (l) {
13
+ let e = JSON.parse(l), r = await n.file("optimizer.safetensors")?.async("arraybuffer");
14
+ if (!r) throw Error("Optimizer weights not found in the zip archive");
15
+ u = new t(e), await u.loadMoments(r);
16
+ }
17
+ let d = await n.file("training_log.json")?.async("string"), f;
18
+ return d && (f = JSON.parse(d)), {
19
+ ...await e(a, s, r, c),
20
+ optimizer: u,
21
+ log: f
22
+ };
23
+ }
24
+ //#endregion
25
+ export { n as default };
@@ -1,5 +1,5 @@
1
1
  import { default as zip } from 'jszip';
2
- import { ITokeniser } from '../../main';
2
+ import { ITokeniser } from '../../tokeniser/type';
3
3
  import { default as Model, ModelForwardAttributes } from '../../models/model';
4
4
  import { TransformersMetadata } from './types';
5
5
  export default function loadOldModel(zipFile: zip, metaData: TransformersMetadata): Promise<{
@@ -1,2 +1,37 @@
1
- import { m as e } from "../main-BSaDGH7I.js";
2
- export { e as default };
1
+ import e from "../tokeniser/CharTokeniser.js";
2
+ import t from "../tokeniser/bpe.js";
3
+ import { ri as n } from "../dist-Da20xy8E.js";
4
+ import { importWeights as r } from "../utilities/weights.js";
5
+ import { dummyPassAsync as i } from "../utilities/dummy.js";
6
+ import a from "../models/factory.js";
7
+ //#region lib/loader/oldZipLoad.ts
8
+ async function o(o, s) {
9
+ let c = /* @__PURE__ */ new Map(), l = await o.file("manifest.json")?.async("string");
10
+ if (!l) throw Error("Manifest file not found in the zip archive");
11
+ let u = JSON.parse(l);
12
+ for (let [e, t] of Object.entries(u.weightSpec)) c.set(e, {
13
+ spec: t,
14
+ data: new Float32Array()
15
+ });
16
+ let d = await o.file("tokeniser.json")?.async("string");
17
+ if (!d) throw Error("Tokeniser file not found in the zip archive");
18
+ let f = JSON.parse(d), p = (f.type ?? "char") === "char" ? new e(f.vocab) : new t(f.vocab, f.merges), m = /* @__PURE__ */ new Map();
19
+ for (let e of Object.keys(o.files)) if (e.endsWith(".bin")) {
20
+ let t = e.replace(".bin", ""), n = await o.file(e).async("arraybuffer"), i = new Float32Array(n), a = c.get(t) || {
21
+ spec: [],
22
+ data: new Float32Array()
23
+ };
24
+ a.data = i, c.set(t, a);
25
+ let s = await r(a);
26
+ m.set(t, s);
27
+ }
28
+ n();
29
+ let h = a(u.config);
30
+ return h.metaData = s, await i(h), h.weightStore.loadWeights(m, !!s.url), {
31
+ model: h,
32
+ tokeniser: p,
33
+ metaData: s
34
+ };
35
+ }
36
+ //#endregion
37
+ export { o as default };
@@ -1,2 +1,75 @@
1
- import { s as e } from "../main-BSaDGH7I.js";
2
- export { e as saveModel };
1
+ import { i as e } from "../chunk-CWhphoD1.js";
2
+ import t from "../tokeniser/CharTokeniser.js";
3
+ import { t as n } from "../jszip.min-DKa1Rjyn.js";
4
+ import { save_safetensors as r } from "../utilities/safetensors.js";
5
+ import "./load.js";
6
+ //#region lib/loader/save.ts
7
+ var i = /* @__PURE__ */ e(n(), 1);
8
+ function a(e) {
9
+ if (e.length > 1e3) {
10
+ let t = Math.ceil(e.length / 1e3);
11
+ return e.filter((n, r) => r % t === 0 || r === e.length - 1);
12
+ }
13
+ return e;
14
+ }
15
+ async function o(e, n, o, s) {
16
+ let c = /* @__PURE__ */ new Map();
17
+ e.weightStore.saveWeights(c);
18
+ let l = new i.default();
19
+ if (s?.optimizer) {
20
+ let e = await s.optimizer.saveMoments();
21
+ l.file("optimizer.safetensors", e, { binary: !0 }), l.file("optimizer_config.json", JSON.stringify(s.optimizer.serializeConfig()), { binary: !1 });
22
+ }
23
+ s?.trainingLog && l.file("training_log.json", JSON.stringify(a(s.trainingLog), void 0, 4), { binary: !1 });
24
+ let u = {};
25
+ c.forEach((e, t) => {
26
+ e.length === 1 && (u[t] = e[0]);
27
+ });
28
+ let d = await r(u, o?.quantize);
29
+ l.file("model.safetensors", d, { binary: !0 });
30
+ let f = e.config.modelType, p;
31
+ p = f === "GenAI_NanoGPT_v1" ? {
32
+ model_type: "GenAI_NanoGPT_v1",
33
+ vocab_size: n.getVocab().length,
34
+ hidden_size: e.config.nEmbed,
35
+ num_hidden_layers: e.config.nLayer,
36
+ num_attention_heads: e.config.nHead,
37
+ block_size: e.config.blockSize,
38
+ mlpFactor: e.config.mlpFactor,
39
+ useRope: e.config.useRope
40
+ } : {
41
+ model_type: "GenAI_NanoGPT_v2",
42
+ vocab_size: n.getVocab().length,
43
+ hidden_size: e.config.nEmbed,
44
+ num_hidden_layers: e.config.nLayer,
45
+ num_attention_heads: e.config.nHead,
46
+ block_size: e.config.blockSize,
47
+ mlpFactor: e.config.mlpFactor,
48
+ loraConfig: e.config.loraConfig ? Object.fromEntries(e.config.loraConfig) : void 0,
49
+ loraName: e.config.loraName,
50
+ windowSize: e.config.windowSize
51
+ }, l.file("config.json", JSON.stringify(p, void 0, 4), { binary: !1 });
52
+ let m = {
53
+ version: 2,
54
+ application: "@genai-fi/nanogpt",
55
+ meta: o?.metadata,
56
+ name: o?.name,
57
+ training: e.metaData?.training || void 0,
58
+ reference: e.metaData?.url || void 0,
59
+ mode: e.metaData?.mode || void 0,
60
+ pretrainingData: e.metaData?.pretrainingData || void 0,
61
+ pretrainingSettings: e.metaData?.pretrainingSettings || void 0,
62
+ generationSettings: e.metaData?.generationSettings || void 0,
63
+ actionLog: e.metaData?.actionLog || void 0
64
+ };
65
+ if (l.file("meta.json", JSON.stringify(m, void 0, 4), { binary: !1 }), l.file("tokeniser.json", JSON.stringify({
66
+ type: n instanceof t ? "char" : "bpe",
67
+ vocab: n.getVocab(),
68
+ merges: n.getMerges(),
69
+ datasetID: n.datasetID,
70
+ id: n.id
71
+ }), { binary: !1 }), o?.files) for (let [e, t] of Object.entries(o.files)) l.file(e, JSON.stringify(t), { binary: !1 });
72
+ return l.generateAsync({ type: "blob" });
73
+ }
74
+ //#endregion
75
+ export { o as saveModel };