@genai-fi/nanogpt 1.4.0 → 1.5.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 (78) hide show
  1. package/dist/TeachableLLM.js +2 -0
  2. package/dist/api/beamer.d.ts +1 -0
  3. package/dist/api/beamer.js +15 -5
  4. package/dist/api/training.d.ts +1 -0
  5. package/dist/api/training.js +2 -2
  6. package/dist/inference/Beamer.d.ts +14 -2
  7. package/dist/inference/Beamer.js +147 -89
  8. package/dist/inference/Generator.js +36 -22
  9. package/dist/inference/tokenisePrompt.d.ts +2 -1
  10. package/dist/inference/tokenisePrompt.js +10 -7
  11. package/dist/inference/types.d.ts +1 -0
  12. package/dist/layers/GQA.d.ts +44 -0
  13. package/dist/layers/{CausalSelfAttention.js → GQA.js} +21 -19
  14. package/dist/layers/HeadLayer.d.ts +12 -0
  15. package/dist/layers/HeadLayer.js +28 -0
  16. package/dist/layers/{CausalSelfAttention.d.ts → MHA.d.ts} +7 -3
  17. package/dist/layers/MHA.js +101 -0
  18. package/dist/layers/MLP.d.ts +5 -1
  19. package/dist/layers/MLP.js +25 -13
  20. package/dist/layers/RMSNorm.d.ts +2 -0
  21. package/dist/layers/RMSNorm.js +12 -8
  22. package/dist/layers/RoPECache.js +4 -3
  23. package/dist/layers/TransformerBlock.d.ts +2 -2
  24. package/dist/layers/TransformerBlock.js +4 -4
  25. package/dist/layers.d.ts +1 -1
  26. package/dist/layers.js +1 -1
  27. package/dist/loader/loadTransformers.js +17 -1
  28. package/dist/loader/save.js +18 -3
  29. package/dist/loader/types.d.ts +8 -2
  30. package/dist/main.js +2 -2
  31. package/dist/matMul16-CO1QhsRt.js +85 -0
  32. package/dist/models/Llama.d.ts +16 -0
  33. package/dist/models/Llama.js +89 -0
  34. package/dist/models/Qwen3.d.ts +16 -0
  35. package/dist/models/Qwen3.js +89 -0
  36. package/dist/models/config.d.ts +18 -1
  37. package/dist/models/config.js +1 -1
  38. package/dist/models/factory.js +11 -7
  39. package/dist/models/model.d.ts +1 -1
  40. package/dist/ops/dot16.js +1 -1
  41. package/dist/ops/globalNorm.js +12 -12
  42. package/dist/ops/grads/attentionMask.js +1 -1
  43. package/dist/ops/grads/matMul16.js +1 -1
  44. package/dist/ops/grads/qkv.js +1 -1
  45. package/dist/ops/grads/rope.js +1 -1
  46. package/dist/ops/grads/silu.d.ts +2 -0
  47. package/dist/ops/grads/silu.js +2 -0
  48. package/dist/ops/matMul16.d.ts +1 -1
  49. package/dist/ops/matMul16.js +1 -1
  50. package/dist/ops/normRMS.d.ts +1 -1
  51. package/dist/ops/normRMS.js +8 -8
  52. package/dist/ops/rope.d.ts +1 -1
  53. package/dist/ops/rope.js +1 -1
  54. package/dist/ops/silu.d.ts +3 -0
  55. package/dist/ops/silu.js +2 -0
  56. package/dist/ops/webgl/rope.js +6 -4
  57. package/dist/ops/webgpu/attentionMask.js +1 -1
  58. package/dist/ops/webgpu/index.js +1 -0
  59. package/dist/ops/webgpu/matMul16.js +50 -44
  60. package/dist/ops/webgpu/matMul16_program.d.ts +2 -2
  61. package/dist/ops/webgpu/matMul16_program.js +1 -1
  62. package/dist/ops/webgpu/normRMS.js +6 -6
  63. package/dist/ops/webgpu/normRMS16_program.d.ts +2 -1
  64. package/dist/ops/webgpu/normRMS16_program.js +4 -3
  65. package/dist/ops/webgpu/normRMS32_program.d.ts +2 -1
  66. package/dist/ops/webgpu/normRMS32_program.js +4 -3
  67. package/dist/ops/webgpu/qkv.js +1 -1
  68. package/dist/ops/webgpu/rope.js +99 -34
  69. package/dist/ops/webgpu/silu.d.ts +14 -0
  70. package/dist/ops/webgpu/silu.js +118 -0
  71. package/dist/{rope-C8EAiSGY.js → rope-BPu_uooz.js} +5 -4
  72. package/dist/silu-B6bvCgJ7.js +25 -0
  73. package/dist/training/Evaluator.js +4 -1
  74. package/dist/utilities/maxHeap.d.ts +16 -0
  75. package/dist/utilities/maxHeap.js +55 -0
  76. package/dist/utilities/parameters.js +33 -17
  77. package/package.json +1 -1
  78. package/dist/matMul16-DGtlziMn.js +0 -81
@@ -2,18 +2,18 @@ import { T as e, di as t, oi as n, pt as r } from "../dist-Da20xy8E.js";
2
2
  import { isPackedTensor as i } from "../utilities/packed.js";
3
3
  import { transpose16 as a } from "../ops/transpose16.js";
4
4
  import { reshape16 as o } from "../ops/reshape16.js";
5
- import { t as s } from "../matMul16-DGtlziMn.js";
5
+ import { t as s } from "../matMul16-CO1QhsRt.js";
6
6
  import { t as c } from "../pack16-BhuXNUS7.js";
7
7
  import { attentionMask as l } from "../ops/attentionMask.js";
8
8
  import u from "./BaseLayer.js";
9
- import { t as d } from "../rope-C8EAiSGY.js";
9
+ import { t as d } from "../rope-BPu_uooz.js";
10
10
  import { appendCache as f } from "../ops/appendCache.js";
11
11
  import { softmax16 as p } from "../ops/softmax16.js";
12
12
  import { dot16 as m } from "../ops/dot16.js";
13
13
  import { qkv as h } from "../ops/qkv.js";
14
- import { normRMS as g } from "../ops/normRMS.js";
15
- import { dropout16 as _ } from "../ops/dropout16.js";
16
- //#region lib/layers/CausalSelfAttention.ts
14
+ import { dropout16 as g } from "../ops/dropout16.js";
15
+ import _ from "./RMSNorm.js";
16
+ //#region lib/layers/GQA.ts
17
17
  var v = class extends u {
18
18
  attentionConfig;
19
19
  divisor;
@@ -22,8 +22,10 @@ var v = class extends u {
22
22
  projUnits;
23
23
  ATTN;
24
24
  PROJ;
25
+ qNorm = null;
26
+ kNorm = null;
25
27
  constructor(e, t, n, r) {
26
- super(t, r), this.attentionConfig = n, this.index = e, this.units = t.nEmbed * 3, this.projUnits = t.nEmbed, this.ATTN = `block_${this.index}_cAttn`, this.PROJ = `block_${this.index}_cProj`, this.addVariable(this.ATTN), this.addVariable(this.PROJ), this.divisor = 1 / Math.sqrt(t.nEmbed / t.nHead);
28
+ super(t, r), this.attentionConfig = n, this.index = e, this.units = t.nEmbed * 3, this.projUnits = t.nEmbed, this.ATTN = `block_${this.index}_cAttn`, this.PROJ = `block_${this.index}_cProj`, this.addVariable(this.ATTN), this.addVariable(this.PROJ), this.divisor = 1 / Math.sqrt(t.nEmbed / t.nHead), (this.attentionConfig.useQKNorm ?? !1) && (this.qNorm = new _(t, n, `block_${this.index}_qNorm`, this), this.kNorm = new _(t, n, `block_${this.index}_kNorm`, this));
27
29
  }
28
30
  build() {
29
31
  this.hasVariable(this.ATTN) === !1 && this.setVariable(this.ATTN, e(r([this.config.nEmbed, this.units], 0, .02), !0, this.ATTN)), this.hasVariable(this.PROJ) === !1 && this.setVariable(this.PROJ, e(r([this.projUnits, this.config.nEmbed], 0, .02), !0, this.PROJ));
@@ -62,20 +64,20 @@ var v = class extends u {
62
64
  forward(e, r) {
63
65
  return t(() => {
64
66
  this.startMemory();
65
- let [t, i, a] = this.getQKV(r), o = e.pastKV ? e.pastKV.cumulativeLength : e.ropePositionOffset || 0, c = e.ropeCache, l = c ? d(t, c, o) : t, u = c ? d(i, c, o) : i, f = this.attentionConfig.useQKNorm ?? !1, p = f ? g(l) : l;
66
- f && l.dispose();
67
- let m = f ? g(u) : u;
68
- f && u.dispose(), c && (t.dispose(), i.dispose());
67
+ let [t, i, a] = this.getQKV(r), o = e.pastKV ? e.pastKV.cumulativeLength : e.ropePositionOffset || 0, c = e.ropeCache, l = this.attentionConfig.ropeInterleaved ?? !0, u = c ? d(t, c, o, !1, l) : t, f = c ? d(i, c, o, !1, l) : i, p = this.qNorm ? this.qNorm.call(e, u) : u;
68
+ this.qNorm && u.dispose();
69
+ let m = this.kNorm ? this.kNorm.call(e, f) : f;
70
+ this.kNorm && f.dispose(), c && (t.dispose(), i.dispose());
69
71
  let h = e.pastKV ? e.pastKV.length : 0;
70
72
  e.pastKV && !e.training && this.updateCache(m, a, e.pastKV);
71
- let v = e.pastKV?.k ? e.pastKV.k : m, y = e.pastKV?.v ? e.pastKV.v : a, b;
72
- b = h > 0 ? this.getAttentionScores(p, v, h) : this.getAttentionScores(p, v), p.dispose(), e.pastKV || v.dispose();
73
- let x = s(b, y), S = e.attentionScores !== void 0 && e.attentionScores.attentionOut !== void 0;
74
- S || b.dispose(), e.pastKV || y.dispose();
75
- let C = this.getOutputProjection(x);
76
- if (x.dispose(), S && e.attentionScores && e.attentionScores.attentionOut !== void 0) {
77
- let t = b.shape[1], r = b.shape[2];
78
- e.attentionScores.attentionOut?.push(n(b.slice([
73
+ let _ = e.pastKV?.k ? e.pastKV.k : m, v = e.pastKV?.v ? e.pastKV.v : a, y;
74
+ y = h > 0 ? this.getAttentionScores(p, _, h) : this.getAttentionScores(p, _), p.dispose(), e.pastKV || _.dispose();
75
+ let b = s(y, v), x = e.attentionScores !== void 0 && e.attentionScores.attentionOut !== void 0;
76
+ x || y.dispose(), e.pastKV || v.dispose();
77
+ let S = this.getOutputProjection(b);
78
+ if (b.dispose(), x && e.attentionScores && e.attentionScores.attentionOut !== void 0) {
79
+ let t = y.shape[1], r = y.shape[2];
80
+ e.attentionScores.attentionOut?.push(n(y.slice([
79
81
  0,
80
82
  0,
81
83
  0,
@@ -91,7 +93,7 @@ var v = class extends u {
91
93
  -1
92
94
  ])));
93
95
  }
94
- return this.endMemory("CausalSelfAttention"), e.dropout && e.dropout > 0 ? _(C, e.dropout) : C;
96
+ return this.endMemory("CausalSelfAttention"), e.dropout && e.dropout > 0 ? g(S, e.dropout) : S;
95
97
  });
96
98
  }
97
99
  };
@@ -0,0 +1,12 @@
1
+ import { Tensor } from '@tensorflow/tfjs-core';
2
+ import { default as BaseLayer, ForwardAttributes } from './BaseLayer';
3
+ import { GPTConfig } from '../../models/config';
4
+ export default class HeadLayer extends BaseLayer {
5
+ private vocabSize;
6
+ private embedDim;
7
+ private initializer;
8
+ private WEIGHTS;
9
+ constructor(config: GPTConfig, name: string, parent?: BaseLayer);
10
+ project(inputs: Tensor): Tensor;
11
+ forward(_: ForwardAttributes, x: Tensor): Tensor;
12
+ }
@@ -0,0 +1,28 @@
1
+ import { T as e } from "../dist-Da20xy8E.js";
2
+ import { n as t } from "../dist-CwK5S7Ls.js";
3
+ import { isPackedTensor as n } from "../utilities/packed.js";
4
+ import { t as r } from "../pack16-BhuXNUS7.js";
5
+ import i from "./BaseLayer.js";
6
+ import { dot16 as a } from "../ops/dot16.js";
7
+ //#region lib/layers/HeadLayer.ts
8
+ var o = class extends i {
9
+ vocabSize;
10
+ embedDim;
11
+ initializer;
12
+ WEIGHTS;
13
+ constructor(n, r, i) {
14
+ super(n, i), this.WEIGHTS = r, this.vocabSize = n.vocabSize, this.embedDim = n.nEmbed, this.initializer = t({
15
+ mean: 0,
16
+ stddev: .02
17
+ }), this.addVariable(this.WEIGHTS, e(this.initializer.apply([this.embedDim, this.vocabSize]), !0, r));
18
+ }
19
+ project(e) {
20
+ let t = n(e) ? r(this.getVariable(this.WEIGHTS), void 0, 32) : this.getVariable(this.WEIGHTS), i = a(e, t);
21
+ return n(e) && t.dispose(), i;
22
+ }
23
+ forward(e, t) {
24
+ return this.project(t);
25
+ }
26
+ };
27
+ //#endregion
28
+ export { o as default };
@@ -1,6 +1,7 @@
1
1
  import { default as BaseLayer, ForwardAttributes } from './BaseLayer';
2
2
  import { Tensor } from '@tensorflow/tfjs-core';
3
3
  import { GPTConfig } from '../../models/config';
4
+ import { RMSNormConfig } from './RMSNorm';
4
5
  export interface KVCache {
5
6
  k?: Tensor;
6
7
  v?: Tensor;
@@ -17,10 +18,11 @@ interface AttentionForwardAttributes extends ForwardAttributes {
17
18
  seed?: number;
18
19
  ropePositionOffset?: number;
19
20
  }
20
- export interface CausalSelfAttentionConfig {
21
+ export interface MHAConfig extends RMSNormConfig {
21
22
  useQKNorm?: boolean;
23
+ ropeInterleaved?: boolean;
22
24
  }
23
- export default class CausalSelfAttention extends BaseLayer<AttentionForwardAttributes> {
25
+ export default class MHA extends BaseLayer<AttentionForwardAttributes> {
24
26
  private readonly attentionConfig;
25
27
  private divisor;
26
28
  private index;
@@ -28,7 +30,9 @@ export default class CausalSelfAttention extends BaseLayer<AttentionForwardAttri
28
30
  private projUnits;
29
31
  private ATTN;
30
32
  private PROJ;
31
- constructor(index: number, config: GPTConfig, attentionConfig: CausalSelfAttentionConfig, parent?: BaseLayer);
33
+ private qNorm;
34
+ private kNorm;
35
+ constructor(index: number, config: GPTConfig, attentionConfig: MHAConfig, parent?: BaseLayer);
32
36
  protected build(): void;
33
37
  private getAttentionScores;
34
38
  private getQKV;
@@ -0,0 +1,101 @@
1
+ import { T as e, di as t, oi as n, pt as r } from "../dist-Da20xy8E.js";
2
+ import { isPackedTensor as i } from "../utilities/packed.js";
3
+ import { transpose16 as a } from "../ops/transpose16.js";
4
+ import { reshape16 as o } from "../ops/reshape16.js";
5
+ import { t as s } from "../matMul16-CO1QhsRt.js";
6
+ import { t as c } from "../pack16-BhuXNUS7.js";
7
+ import { attentionMask as l } from "../ops/attentionMask.js";
8
+ import u from "./BaseLayer.js";
9
+ import { t as d } from "../rope-BPu_uooz.js";
10
+ import { appendCache as f } from "../ops/appendCache.js";
11
+ import { softmax16 as p } from "../ops/softmax16.js";
12
+ import { dot16 as m } from "../ops/dot16.js";
13
+ import { qkv as h } from "../ops/qkv.js";
14
+ import { dropout16 as g } from "../ops/dropout16.js";
15
+ import _ from "./RMSNorm.js";
16
+ //#region lib/layers/MHA.ts
17
+ var v = class extends u {
18
+ attentionConfig;
19
+ divisor;
20
+ index;
21
+ units;
22
+ projUnits;
23
+ ATTN;
24
+ PROJ;
25
+ qNorm = null;
26
+ kNorm = null;
27
+ constructor(e, t, n, r) {
28
+ super(t, r), this.attentionConfig = n, this.index = e, this.units = t.nEmbed * 3, this.projUnits = t.nEmbed, this.ATTN = `block_${this.index}_cAttn`, this.PROJ = `block_${this.index}_cProj`, this.addVariable(this.ATTN), this.addVariable(this.PROJ), this.divisor = 1 / Math.sqrt(t.nEmbed / t.nHead), (this.attentionConfig.useQKNorm ?? !1) && (this.qNorm = new _(t, n, `block_${this.index}_qNorm`, this), this.kNorm = new _(t, n, `block_${this.index}_kNorm`, this));
29
+ }
30
+ build() {
31
+ this.hasVariable(this.ATTN) === !1 && this.setVariable(this.ATTN, e(r([this.config.nEmbed, this.units], 0, .02), !0, this.ATTN)), this.hasVariable(this.PROJ) === !1 && this.setVariable(this.PROJ, e(r([this.projUnits, this.config.nEmbed], 0, .02), !0, this.PROJ));
32
+ }
33
+ getAttentionScores(e, t, n) {
34
+ let r = l(e, t, this.divisor, n), i = p(r);
35
+ return r.dispose(), i;
36
+ }
37
+ getQKV(e) {
38
+ let t = i(e) ? c(this.getVariable(this.ATTN)) : this.getVariable(this.ATTN), n = h(e, t, this.config.nHead);
39
+ return i(e) && t.dispose(), n;
40
+ }
41
+ getOutputProjection(e) {
42
+ let t = e.shape[0], n = e.shape[2], r = this.config.nEmbed, s = i(e), l = a(e, [
43
+ 0,
44
+ 2,
45
+ 1,
46
+ 3
47
+ ]), u = o(l, [
48
+ t,
49
+ n,
50
+ s ? r / 2 : r
51
+ ]);
52
+ l.dispose();
53
+ let d = s ? c(this.getVariable(this.PROJ)) : this.getVariable(this.PROJ), f = m(u, d);
54
+ return s && d.dispose(), u.dispose(), f;
55
+ }
56
+ updateCache(e, t, r) {
57
+ let i = this.config.blockSize, a = e.shape[2], o = r.length || 0, s = f(e, i, o, r.k);
58
+ e.dispose(), r.k && r.k.dispose();
59
+ let c = f(t, i, o, r.v);
60
+ t.dispose(), r.v && r.v.dispose();
61
+ let l = Math.min(o + a, i), u = r.cumulativeLength + a;
62
+ r.length = l, r.cumulativeLength = u, r.k = n(s), r.v = n(c);
63
+ }
64
+ forward(e, r) {
65
+ return t(() => {
66
+ this.startMemory();
67
+ let [t, i, a] = this.getQKV(r), o = e.pastKV ? e.pastKV.cumulativeLength : e.ropePositionOffset || 0, c = e.ropeCache, l = this.attentionConfig.ropeInterleaved ?? !0, u = c ? d(t, c, o, !1, l) : t, f = c ? d(i, c, o, !1, l) : i, p = this.qNorm ? this.qNorm.call(e, u) : u;
68
+ this.qNorm && u.dispose();
69
+ let m = this.kNorm ? this.kNorm.call(e, f) : f;
70
+ this.kNorm && f.dispose(), c && (t.dispose(), i.dispose());
71
+ let h = e.pastKV ? e.pastKV.length : 0;
72
+ e.pastKV && !e.training && this.updateCache(m, a, e.pastKV);
73
+ let _ = e.pastKV?.k ? e.pastKV.k : m, v = e.pastKV?.v ? e.pastKV.v : a, y;
74
+ y = h > 0 ? this.getAttentionScores(p, _, h) : this.getAttentionScores(p, _), p.dispose(), e.pastKV || _.dispose();
75
+ let b = s(y, v), x = e.attentionScores !== void 0 && e.attentionScores.attentionOut !== void 0;
76
+ x || y.dispose(), e.pastKV || v.dispose();
77
+ let S = this.getOutputProjection(b);
78
+ if (b.dispose(), x && e.attentionScores && e.attentionScores.attentionOut !== void 0) {
79
+ let t = y.shape[1], r = y.shape[2];
80
+ e.attentionScores.attentionOut?.push(n(y.slice([
81
+ 0,
82
+ 0,
83
+ 0,
84
+ 0
85
+ ], [
86
+ 1,
87
+ -1,
88
+ -1,
89
+ -1
90
+ ]).reshape([
91
+ t,
92
+ r,
93
+ -1
94
+ ])));
95
+ }
96
+ return this.endMemory("CausalSelfAttention"), e.dropout && e.dropout > 0 ? g(S, e.dropout) : S;
97
+ });
98
+ }
99
+ };
100
+ //#endregion
101
+ export { v as default };
@@ -2,14 +2,18 @@ import { Tensor } from '@tensorflow/tfjs-core';
2
2
  import { default as BaseLayer, ForwardAttributes } from './BaseLayer';
3
3
  import { GPTConfig } from '../../models/config';
4
4
  export interface MLPConfig {
5
- activation?: 'gelu' | 'relu2';
5
+ activation?: 'gelu' | 'relu2' | 'swiglu';
6
6
  hiddenFactor?: number;
7
+ hiddenSize?: number;
7
8
  }
9
+ export declare function swigluHidden(C: number, factor?: number, multipleOf?: number): number;
8
10
  export default class MLP extends BaseLayer {
9
11
  private index;
10
12
  private hiddenUnits;
11
13
  private MLPHIDDEN;
12
14
  private MLPOUT;
15
+ private MLPGATE;
16
+ private MLPUP;
13
17
  private mlpConfig;
14
18
  constructor(index: number, config: GPTConfig, mlpConfig: MLPConfig, parent?: BaseLayer);
15
19
  protected build(): void;
@@ -1,34 +1,46 @@
1
1
  import { T as e, di as t, pt as n } from "../dist-Da20xy8E.js";
2
2
  import { reshape16 as r } from "../ops/reshape16.js";
3
- import { t as i } from "../matMul16-DGtlziMn.js";
4
- import a from "./BaseLayer.js";
5
- import { dropout16 as o } from "../ops/dropout16.js";
3
+ import { mul16 as i } from "../ops/mul16.js";
4
+ import { t as a } from "../matMul16-CO1QhsRt.js";
5
+ import o from "./BaseLayer.js";
6
+ import { dropout16 as s } from "../ops/dropout16.js";
6
7
  //#region lib/layers/MLP.ts
7
- var s = class extends a {
8
+ function c(e, t = 8 / 3, n = 32) {
9
+ let r = Math.round(t * e);
10
+ return Math.max(n, Math.round(r / n) * n);
11
+ }
12
+ var l = class extends o {
8
13
  index;
9
14
  hiddenUnits;
10
15
  MLPHIDDEN;
11
16
  MLPOUT;
17
+ MLPGATE;
18
+ MLPUP;
12
19
  mlpConfig;
13
20
  constructor(e, t, n, r) {
14
- super(t, r), this.index = e, this.mlpConfig = n, this.hiddenUnits = (n.hiddenFactor ?? t.mlpFactor) * t.nEmbed, this.MLPHIDDEN = `block_${this.index}_mlpHidden`, this.MLPOUT = `block_${this.index}_mlpOut`, this.addVariable(this.MLPHIDDEN), this.addVariable(this.MLPOUT);
21
+ super(t, r), this.index = e, this.mlpConfig = n, this.hiddenUnits = n.hiddenSize ?? c(t.nEmbed, n.hiddenFactor ?? t.mlpFactor), this.MLPHIDDEN = `block_${this.index}_mlpHidden`, this.MLPOUT = `block_${this.index}_mlpOut`, this.addVariable(this.MLPOUT), this.MLPGATE = `block_${this.index}_mlpGate`, this.MLPUP = `block_${this.index}_mlpUp`, this.mlpConfig.activation === "swiglu" ? (this.addVariable(this.MLPGATE), this.addVariable(this.MLPUP)) : this.addVariable(this.MLPHIDDEN);
15
22
  }
16
23
  build() {
17
- this.hasVariable(this.MLPHIDDEN) === !1 && this.setVariable(this.MLPHIDDEN, e(n([this.config.nEmbed, this.hiddenUnits], 0, .02), !0, this.MLPHIDDEN)), this.hasVariable(this.MLPOUT) === !1 && this.setVariable(this.MLPOUT, e(n([this.hiddenUnits, this.config.nEmbed], 0, .02 / Math.sqrt(2 * this.config.nLayer)), !0, this.MLPOUT));
24
+ this.mlpConfig.activation === "swiglu" ? (this.hasVariable(this.MLPGATE) === !1 && this.setVariable(this.MLPGATE, e(n([this.config.nEmbed, this.hiddenUnits], 0, .02), !0, this.MLPGATE)), this.hasVariable(this.MLPUP) === !1 && this.setVariable(this.MLPUP, e(n([this.config.nEmbed, this.hiddenUnits], 0, .02), !0, this.MLPUP))) : this.hasVariable(this.MLPHIDDEN) === !1 && this.setVariable(this.MLPHIDDEN, e(n([this.config.nEmbed, this.hiddenUnits], 0, .02), !0, this.MLPHIDDEN)), this.hasVariable(this.MLPOUT) === !1 && this.setVariable(this.MLPOUT, e(n([this.hiddenUnits, this.config.nEmbed], 0, .02 / Math.sqrt(2 * this.config.nLayer)), !0, this.MLPOUT));
18
25
  }
19
26
  forward(e, n) {
20
27
  return t(() => {
21
28
  this.startMemory();
22
- let [t, a, s] = n.shape, c = i(r(n, [t * a, s]), this.getVariable(this.MLPHIDDEN), !1, !1, { activation: this.mlpConfig.activation ?? "gelu" }), l = i(c, this.getVariable(this.MLPOUT));
23
- c.dispose();
24
- let u = r(l, [
29
+ let [t, o, c] = n.shape, l = r(n, [t * o, c]), u;
30
+ if (this.mlpConfig.activation === "swiglu") {
31
+ let e = a(l, this.getVariable(this.MLPGATE), !1, !1, { activation: "silu" }), t = a(l, this.getVariable(this.MLPUP));
32
+ u = i(e, t), e.dispose(), t.dispose();
33
+ } else u = a(l, this.getVariable(this.MLPHIDDEN), !1, !1, { activation: this.mlpConfig.activation ?? "gelu" });
34
+ let d = a(u, this.getVariable(this.MLPOUT));
35
+ u.dispose();
36
+ let f = r(d, [
25
37
  t,
26
- a,
27
- s
38
+ o,
39
+ c
28
40
  ]);
29
- return this.endMemory("MLP"), e.dropout && e.dropout > 0 ? o(u, e.dropout) : u;
41
+ return this.endMemory("MLP"), e.dropout && e.dropout > 0 ? s(f, e.dropout) : f;
30
42
  });
31
43
  }
32
44
  };
33
45
  //#endregion
34
- export { s as default };
46
+ export { l as default, c as swigluHidden };
@@ -3,10 +3,12 @@ import { default as BaseLayer, ForwardAttributes } from './BaseLayer';
3
3
  import { GPTConfig } from '../../models/config';
4
4
  export interface RMSNormConfig {
5
5
  useGamma?: boolean;
6
+ epsilon?: number;
6
7
  }
7
8
  export default class RMSNorm extends BaseLayer {
8
9
  private GAMMA;
9
10
  private rmsConfig;
10
11
  constructor(config: GPTConfig, rmsConfig: RMSNormConfig, name?: string, parent?: BaseLayer);
12
+ protected buildGamma(size: number): void;
11
13
  forward(_: ForwardAttributes, x: Tensor): Tensor;
12
14
  }
@@ -1,20 +1,24 @@
1
1
  import { Ot as e, T as t, di as n } from "../dist-Da20xy8E.js";
2
- import r from "./BaseLayer.js";
3
- import { normRMS as i } from "../ops/normRMS.js";
2
+ import { isPackedTensor as r } from "../utilities/packed.js";
3
+ import i from "./BaseLayer.js";
4
+ import { normRMS as a } from "../ops/normRMS.js";
4
5
  //#region lib/layers/RMSNorm.ts
5
- var a = class extends r {
6
+ var o = class extends i {
6
7
  GAMMA;
7
8
  rmsConfig;
8
- constructor(n, r, i = "", a) {
9
- super(n, a), this.GAMMA = i, this.rmsConfig = r, (this.rmsConfig.useGamma ?? !0) && this.addVariable(this.GAMMA, t(e([n.nEmbed]), !0, this.GAMMA, "float32"));
9
+ constructor(e, t, n = "", r) {
10
+ super(e, r), this.GAMMA = n, this.rmsConfig = t, (this.rmsConfig.useGamma ?? !0) && this.addVariable(this.GAMMA);
11
+ }
12
+ buildGamma(n) {
13
+ (this.rmsConfig.useGamma ?? !0) && this.hasVariable(this.GAMMA) === !1 && this.setVariable(this.GAMMA, t(e([n]), !0, this.GAMMA, "float32"));
10
14
  }
11
15
  forward(e, t) {
12
16
  return n(() => {
13
- this.startMemory();
14
- let e = i(t, this.rmsConfig.useGamma ?? !0 ? this.getVariable(this.GAMMA) : void 0);
17
+ this.startMemory(), this.buildGamma(r(t) ? t.shape[t.shape.length - 1] * 2 : t.shape[t.shape.length - 1]);
18
+ let e = a(t, this.rmsConfig.useGamma ?? !0 ? this.getVariable(this.GAMMA) : void 0, this.rmsConfig.epsilon);
15
19
  return this.endMemory("RMSNorm"), e;
16
20
  });
17
21
  }
18
22
  };
19
23
  //#endregion
20
- export { a as default };
24
+ export { o as default };
@@ -306,9 +306,10 @@ var wt = class {
306
306
  this.config = e;
307
307
  let t = this.config.nEmbed / this.config.nHead;
308
308
  if (this.rotaryDim = t, this.rotaryDim % 2 != 0) throw Error("rotaryDim must be even");
309
- this.ropeBase = 1e4;
310
- let n = J(0, this.rotaryDim, 2, "float32"), r = u(n, W(this.rotaryDim, "float32")), i = Q(W(this.ropeBase, "float32"), r);
311
- this.ropeInvFreq = Z(i), r.dispose(), i.dispose(), n.dispose(), (this.config.modelType === "GenAI_NanoGPT_v1" ? this.config.useRope ?? !0 : !0) === !1 ? (this.ropeCos = null, this.ropeSin = null, this.ropeNegSin = null, this.ropeCacheLen = 0) : K(() => {
309
+ let n = this.config.ropeTheta ?? 1e4;
310
+ this.ropeBase = n;
311
+ let r = J(0, this.rotaryDim, 2, "float32"), i = u(r, W(this.rotaryDim, "float32")), a = Q(W(this.ropeBase, "float32"), i);
312
+ this.ropeInvFreq = Z(a), i.dispose(), a.dispose(), r.dispose(), (this.config.modelType === "GenAI_NanoGPT_v1" ? this.config.useRope ?? !0 : !0) === !1 ? (this.ropeCos = null, this.ropeSin = null, this.ropeNegSin = null, this.ropeCacheLen = 0) : K(() => {
312
313
  this.ensureRopeCache(this.config.blockSize * 4);
313
314
  });
314
315
  }
@@ -1,4 +1,4 @@
1
- import { AttentionScores, CausalSelfAttentionConfig, KVCache } from './CausalSelfAttention';
1
+ import { AttentionScores, MHAConfig, KVCache } from './MHA';
2
2
  import { MLPConfig } from './MLP';
3
3
  import { RMSNormConfig } from './RMSNorm';
4
4
  import { default as BaseLayer, ForwardAttributes } from './BaseLayer';
@@ -10,7 +10,7 @@ interface BlockAttributes extends ForwardAttributes {
10
10
  attentionScores?: AttentionScores;
11
11
  ropePositionOffset?: number;
12
12
  }
13
- export type TransformerBlockConfig = MLPConfig & RMSNormConfig & CausalSelfAttentionConfig;
13
+ export type TransformerBlockConfig = MLPConfig & RMSNormConfig & MHAConfig;
14
14
  export default class Block extends BaseLayer<BlockAttributes> {
15
15
  private ln1;
16
16
  private attn;
@@ -1,8 +1,8 @@
1
1
  import { di as e, oi as t } from "../dist-Da20xy8E.js";
2
2
  import n from "./BaseLayer.js";
3
- import r from "./CausalSelfAttention.js";
4
- import i from "./MLP.js";
5
- import a from "./RMSNorm.js";
3
+ import r from "./RMSNorm.js";
4
+ import i from "./MHA.js";
5
+ import a from "./MLP.js";
6
6
  import { add16 as o } from "../ops/add16.js";
7
7
  //#region lib/layers/TransformerBlock.ts
8
8
  var s = class extends n {
@@ -14,7 +14,7 @@ var s = class extends n {
14
14
  skipped = !1;
15
15
  blockConfig;
16
16
  constructor(e, t, n, o) {
17
- super(t, o), this.index = e, this.blockConfig = n, this.ln1 = new a(t, this.blockConfig, `block_${this.index}_rms1`, this), this.attn = new r(this.index, t, this.blockConfig, this), this.ln2 = new a(t, this.blockConfig, `block_${this.index}_rms2`, this), this.mlp = new i(this.index, t, this.blockConfig, this);
17
+ super(t, o), this.index = e, this.blockConfig = n, this.ln1 = new r(t, this.blockConfig, `block_${this.index}_rms1`, this), this.attn = new i(this.index, t, this.blockConfig, this), this.ln2 = new r(t, this.blockConfig, `block_${this.index}_rms2`, this), this.mlp = new a(this.index, t, this.blockConfig, this);
18
18
  }
19
19
  getMLPOutput(e, n) {
20
20
  let r = this.ln2.call({ training: n.training }, e), i = this.mlp.call(n, r);
package/dist/layers.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { default as CausalSelfAttention } from './layers/CausalSelfAttention';
1
+ export { default as CausalSelfAttention } from './layers/MHA';
2
2
  export { default as MLP } from './layers/MLP';
3
3
  export { default as TransformerBlock } from './layers/TransformerBlock';
4
4
  export { default as RoPECache } from './layers/RoPECache';
package/dist/layers.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { n as e } from "./chunk-CWhphoD1.js";
2
- import t from "./layers/CausalSelfAttention.js";
2
+ import t from "./layers/MHA.js";
3
3
  import n from "./layers/MLP.js";
4
4
  import r from "./layers/TransformerBlock.js";
5
5
  import i from "./layers/RoPECache.js";
@@ -16,7 +16,7 @@ function o(e) {
16
16
  nEmbed: e.hidden_size,
17
17
  mlpFactor: e.mlpFactor,
18
18
  useRope: e.useRope
19
- } : {
19
+ } : e.model_type === "GenAI_NanoGPT_v2" ? {
20
20
  modelType: "GenAI_NanoGPT_v2",
21
21
  vocabSize: e.vocab_size,
22
22
  blockSize: e.block_size,
@@ -27,6 +27,22 @@ function o(e) {
27
27
  loraConfig: e.loraConfig ? new Map(Object.entries(e.loraConfig)) : void 0,
28
28
  loraName: e.loraName,
29
29
  windowSize: e.windowSize
30
+ } : e.model_type === "qwen3" ? {
31
+ modelType: "qwen3",
32
+ vocabSize: e.vocab_size,
33
+ blockSize: e.block_size,
34
+ nLayer: e.num_hidden_layers,
35
+ nHead: e.num_attention_heads,
36
+ nEmbed: e.hidden_size,
37
+ mlpFactor: e.mlpFactor
38
+ } : {
39
+ modelType: "llama",
40
+ vocabSize: e.vocab_size,
41
+ blockSize: e.block_size,
42
+ nLayer: e.num_hidden_layers,
43
+ nHead: e.num_attention_heads,
44
+ nEmbed: e.hidden_size,
45
+ mlpFactor: e.mlpFactor
30
46
  }, t;
31
47
  }
32
48
  async function s(s, c, l, u) {
@@ -37,7 +37,7 @@ async function o(e, n, o, s) {
37
37
  block_size: e.config.blockSize,
38
38
  mlpFactor: e.config.mlpFactor,
39
39
  useRope: e.config.useRope
40
- } : {
40
+ } : f === "GenAI_NanoGPT_v2" ? {
41
41
  model_type: "GenAI_NanoGPT_v2",
42
42
  vocab_size: n.getVocab().length,
43
43
  hidden_size: e.config.nEmbed,
@@ -46,8 +46,23 @@ async function o(e, n, o, s) {
46
46
  block_size: e.config.blockSize,
47
47
  mlpFactor: e.config.mlpFactor,
48
48
  loraConfig: e.config.loraConfig ? Object.fromEntries(e.config.loraConfig) : void 0,
49
- loraName: e.config.loraName,
50
- windowSize: e.config.windowSize
49
+ loraName: e.config.loraName
50
+ } : f === "qwen3" ? {
51
+ model_type: "qwen3",
52
+ vocab_size: n.getVocab().length,
53
+ hidden_size: e.config.nEmbed,
54
+ num_hidden_layers: e.config.nLayer,
55
+ num_attention_heads: e.config.nHead,
56
+ block_size: e.config.blockSize,
57
+ mlpFactor: e.config.mlpFactor
58
+ } : {
59
+ model_type: "llama",
60
+ vocab_size: n.getVocab().length,
61
+ hidden_size: e.config.nEmbed,
62
+ num_hidden_layers: e.config.nLayer,
63
+ num_attention_heads: e.config.nHead,
64
+ block_size: e.config.blockSize,
65
+ mlpFactor: e.config.mlpFactor
51
66
  }, l.file("config.json", JSON.stringify(p, void 0, 4), { binary: !1 });
52
67
  let m = {
53
68
  version: 2,
@@ -5,7 +5,7 @@ import { ITokeniser } from '../../tokeniser/type';
5
5
  import { AdamWOptimizer } from '../../training/AdamW';
6
6
  import { TrainingLogEntry, TrainingOptions } from '../../training/types';
7
7
  export interface TransformersConfigBase {
8
- model_type: 'GenAI_NanoGPT_v1' | 'GenAI_NanoGPT_v2';
8
+ model_type: 'GenAI_NanoGPT_v1' | 'GenAI_NanoGPT_v2' | 'qwen3' | 'llama';
9
9
  vocab_size: number;
10
10
  hidden_size: number;
11
11
  num_hidden_layers: number;
@@ -23,7 +23,13 @@ export interface TransformersConfigV2 extends TransformersConfigBase {
23
23
  model_type: 'GenAI_NanoGPT_v2';
24
24
  windowSize?: string;
25
25
  }
26
- export type TransformersConfig = TransformersConfigV1 | TransformersConfigV2;
26
+ export interface TransformersConfigQwen3 extends TransformersConfigBase {
27
+ model_type: 'qwen3';
28
+ }
29
+ export interface TransformersConfigLlama extends TransformersConfigBase {
30
+ model_type: 'llama';
31
+ }
32
+ export type TransformersConfig = TransformersConfigV1 | TransformersConfigV2 | TransformersConfigQwen3 | TransformersConfigLlama;
27
33
  export interface TransformersTokeniser {
28
34
  type: 'char' | 'bpe';
29
35
  vocab: string[];
package/dist/main.js CHANGED
@@ -11,13 +11,13 @@ import { $ as ii, A as ai, B as oi, C as si, D as ci, E as li, F as ui, G as di,
11
11
  import { S as ia, _ as aa, a as oa, c as sa, d as ca, f as la, g as ua, h as da, i as G, l as fa, m as pa, n as K, o as ma, p as ha, r as ga, s as _a, t as va, u as ya, v as q, x as ba, y as xa } from "./kernel_funcs_utils-ql8Y8qPn.js";
12
12
  import "./gelu-B6637MKa.js";
13
13
  import "./ops/transpose16.js";
14
- import "./matMul16-DGtlziMn.js";
14
+ import "./matMul16-CO1QhsRt.js";
15
15
  import { n as Sa, r as Ca, t as J } from "./Reshape-BlylqwWy.js";
16
16
  import { i as wa } from "./matMulGelu-CBoqTZM7.js";
17
17
  import "./ops/matMulGelu.js";
18
18
  import { r as Ta, t as Ea } from "./pack16-BhuXNUS7.js";
19
19
  import "./ops/attentionMask.js";
20
- import "./rope-C8EAiSGY.js";
20
+ import "./rope-BPu_uooz.js";
21
21
  import "./ops/appendCache.js";
22
22
  import "./ops/softmax16.js";
23
23
  import "./ops/qkv.js";
@@ -0,0 +1,85 @@
1
+ import { Fi as e, ii as t } from "./dist-Da20xy8E.js";
2
+ import { t as n } from "./gelu-B6637MKa.js";
3
+ import { isPackedTensor as r } from "./utilities/packed.js";
4
+ import { transpose16 as i } from "./ops/transpose16.js";
5
+ import { reshape16 as a } from "./ops/reshape16.js";
6
+ import { mul16 as o } from "./ops/mul16.js";
7
+ import { t as s } from "./silu-B6bvCgJ7.js";
8
+ import "./ops/webgl/matMul16.js";
9
+ import "./ops/cpu/matMul16.js";
10
+ import { t as c } from "./pack16-BhuXNUS7.js";
11
+ //#region lib/ops/grads/matMul16.ts
12
+ var l = {
13
+ kernelName: "MatMul16",
14
+ inputsToSave: ["A", "B"],
15
+ outputsToSave: [],
16
+ gradFunc: (e, t, r) => {
17
+ let [c, l] = t;
18
+ if (Array.isArray(e)) throw Error("Expected dy to be a single Tensor");
19
+ let d = e, { transposeA: m, transposeB: h, scale: g, activation: _, originalShape: v, perm: y } = r;
20
+ if (y && v) {
21
+ let e = Array(y.length);
22
+ for (let t = 0; t < y.length; ++t) e[y[t]] = t;
23
+ let t = d;
24
+ d = i(d, e), t.dispose();
25
+ }
26
+ if (v) {
27
+ let e = d;
28
+ d = a(d, v), e.dispose();
29
+ }
30
+ if (_ === "gelu") {
31
+ let e = d, t = u(c, l, m, h);
32
+ d = n(e, t), e.dispose(), t.dispose();
33
+ } else if (_ === "relu2") {
34
+ let e = d, t = u(c, l, m, h, {
35
+ activation: "relu",
36
+ scale: 2
37
+ });
38
+ d = o(e, t), e.dispose(), t.dispose();
39
+ } else if (_ === "silu") {
40
+ let e = d, t = u(c, l, m, h);
41
+ d = s(e, t), e.dispose(), t.dispose();
42
+ }
43
+ if (!m && !h) return {
44
+ A: () => g === void 0 ? u(d, l, !1, !0) : f(d, l, g, !1, !0),
45
+ B: () => g === void 0 ? u(c, d, !0, !1) : p(c, d, g, !0, !1)
46
+ };
47
+ if (!m && h) return {
48
+ A: () => g === void 0 ? u(d, l, !1, !1) : f(d, l, g, !1, !1),
49
+ B: () => g === void 0 ? u(c, d, !0, !1) : p(c, d, g, !0, !1)
50
+ };
51
+ if (m && !h) return {
52
+ A: () => g === void 0 ? u(l, d, !1, !0) : p(l, d, g, !1, !0),
53
+ B: () => g === void 0 ? u(c, d, !1, !1) : p(c, d, g, !1, !1)
54
+ };
55
+ throw Error("Gradient for transposeA=true and transposeB=true is not supported yet.");
56
+ }
57
+ };
58
+ e(l);
59
+ //#endregion
60
+ //#region lib/ops/matMul16.ts
61
+ function u(e, n, i = !1, a = !1, o = {}) {
62
+ let s = r(e), l = r(n), u = s || l, d = !u || s ? e : c(e), f = !u || l ? n : c(n), p = t().runKernel("MatMul16", {
63
+ A: d,
64
+ B: f
65
+ }, {
66
+ transposeA: i,
67
+ transposeB: a,
68
+ ...o
69
+ });
70
+ return u && !s && d.dispose(), u && !l && f.dispose(), p;
71
+ }
72
+ function d(e, t, n, r = !1, i = !1) {
73
+ return u(e, t, r, i, { scale: n });
74
+ }
75
+ function f(e, t, n, r = !1, i = !1) {
76
+ return u(e, t, r, i, { scaleA: n });
77
+ }
78
+ function p(e, t, n, r = !1, i = !1) {
79
+ return u(e, t, r, i, { scaleB: n });
80
+ }
81
+ function m(e, t, n = !1, r = !1) {
82
+ return u(e, t, n, r, { activation: "gelu" });
83
+ }
84
+ //#endregion
85
+ export { d as a, p as i, m as n, l as o, f as r, u as t };