@genai-fi/nanogpt 0.4.0 → 0.4.2

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 (87) hide show
  1. package/dist/Generator.js +3 -3
  2. package/dist/NanoGPTModel.js +83 -70
  3. package/dist/TeachableLLM.js +1 -1
  4. package/dist/{random_width-CMHmdbSu.js → TiedEmbedding-CnJ1bx4q.js} +760 -719
  5. package/dist/{axis_util-DeydwOoC.js → axis_util-BgTGy5w8.js} +1 -1
  6. package/dist/{concat-DS_qH7MI.js → concat-CuRsVY-K.js} +1 -1
  7. package/dist/dropout-DfDdklfL.js +193 -0
  8. package/dist/{gather-BUmJIS8n.js → gather-ZYRWhmXR.js} +1 -1
  9. package/dist/gelu-CnCt17Lk.js +26 -0
  10. package/dist/{index-XjBAhiFO.js → index-C4JCoBvj.js} +61 -61
  11. package/dist/kernel_funcs_utils-CAd1h9X1.js +388 -0
  12. package/dist/layers/CausalSelfAttention.js +73 -72
  13. package/dist/layers/MLP.d.ts +3 -1
  14. package/dist/layers/MLP.js +93 -5
  15. package/dist/layers/RMSNorm.js +3 -3
  16. package/dist/layers/RoPECache.js +3 -3
  17. package/dist/layers/TiedEmbedding.js +6 -46
  18. package/dist/layers/TransformerBlock.js +2 -2
  19. package/dist/{log_sum_exp-DJPkVZZn.js → log_sum_exp-BswFnwOb.js} +5 -5
  20. package/dist/main.js +1 -1
  21. package/dist/{mat_mul-CKwFEV1Q.js → mat_mul-415y5Qn2.js} +1 -1
  22. package/dist/{max-DJvEiCAJ.js → max-CP_9O2Yd.js} +1 -1
  23. package/dist/{moments-CrWRPcR3.js → moments-CjeIaVdp.js} +3 -3
  24. package/dist/{norm-BzY929B_.js → norm-CZM380I3.js} +5 -5
  25. package/dist/{ones-BO01zpJG.js → ones-Bf3YR48P.js} +2 -2
  26. package/dist/ops/appendCache.js +1 -1
  27. package/dist/ops/attentionMask.d.ts +1 -1
  28. package/dist/ops/attentionMask.js +4 -4
  29. package/dist/ops/cpu/appendCache.js +2 -2
  30. package/dist/ops/cpu/attentionMask.js +13 -9
  31. package/dist/ops/cpu/fusedSoftmax.js +2 -2
  32. package/dist/ops/cpu/gatherSub.js +3 -3
  33. package/dist/ops/cpu/gelu.d.ts +1 -0
  34. package/dist/ops/cpu/gelu.js +40 -0
  35. package/dist/ops/cpu/mulDropout.js +1 -1
  36. package/dist/ops/cpu/qkv.js +3 -3
  37. package/dist/ops/cpu/rope.js +5 -5
  38. package/dist/ops/cpu/scatterSub.js +4 -4
  39. package/dist/ops/fusedSoftmax.js +1 -1
  40. package/dist/ops/gatherSub.js +1 -1
  41. package/dist/ops/gelu.d.ts +3 -0
  42. package/dist/ops/gelu.js +8 -0
  43. package/dist/ops/grads/attentionMask.js +1 -1
  44. package/dist/ops/grads/fusedSoftmax.js +2 -2
  45. package/dist/ops/grads/gelu.d.ts +2 -0
  46. package/dist/ops/grads/gelu.js +5 -0
  47. package/dist/ops/grads/qkv.js +1 -1
  48. package/dist/ops/grads/rope.js +1 -1
  49. package/dist/ops/mulDrop.js +1 -1
  50. package/dist/ops/node/sparseCrossEntropy.js +1 -1
  51. package/dist/ops/qkv.js +1 -1
  52. package/dist/ops/scatterSub.js +1 -1
  53. package/dist/ops/webgl/appendCache.js +1 -1
  54. package/dist/ops/webgl/attentionMask.js +19 -18
  55. package/dist/ops/webgl/fusedSoftmax.js +489 -788
  56. package/dist/ops/webgl/gatherSub.js +1 -1
  57. package/dist/ops/webgl/gelu.d.ts +2 -0
  58. package/dist/ops/webgl/gelu.js +50 -0
  59. package/dist/ops/webgl/mulDropout.js +1 -1
  60. package/dist/ops/webgl/qkv.js +1 -1
  61. package/dist/ops/webgl/rope.js +1 -1
  62. package/dist/ops/webgl/scatterSub.js +1 -1
  63. package/dist/{range-DQMNzBWs.js → range-9AzeApCc.js} +1 -1
  64. package/dist/{reshape-DFzh97Sc.js → reshape-Boe4DuIO.js} +1 -1
  65. package/dist/{sin-BYM-U4Ut.js → sin-KmhiDuMa.js} +1 -1
  66. package/dist/{slice_util-CnVNPQI-.js → slice_util-19zDNNSn.js} +2 -2
  67. package/dist/{softmax-4DOn6cPq.js → softmax-Cujsg4ay.js} +1 -1
  68. package/dist/{split-CkbeVdF8.js → split-DbcNm1-i.js} +1 -1
  69. package/dist/{stack-DaIMO5iX.js → stack-D1YjmgKN.js} +1 -1
  70. package/dist/{sum-C6u3xMi3.js → sum-R28pucR5.js} +1 -1
  71. package/dist/{tensor-Cu1fU7H7.js → tensor-BVeHdl7V.js} +1 -1
  72. package/dist/{tensor2d-D0CKdG6B.js → tensor2d-DqFGNs_K.js} +1 -1
  73. package/dist/{tfjs_backend-Bzl2SrRo.js → tfjs_backend-Cug-PH75.js} +826 -1015
  74. package/dist/training/AdamExt.js +1 -1
  75. package/dist/training/DatasetBuilder.js +3 -3
  76. package/dist/training/FullTrainer.js +1 -1
  77. package/dist/training/Trainer.js +5 -5
  78. package/dist/training/sparseCrossEntropy.js +4 -4
  79. package/dist/utilities/dummy.js +2 -2
  80. package/dist/utilities/generate.js +3 -3
  81. package/dist/utilities/load.js +1 -1
  82. package/dist/utilities/profile.js +1 -1
  83. package/dist/utilities/weights.js +2 -2
  84. package/dist/{variable-BS4AKqNU.js → variable-LJT9Ld63.js} +1 -1
  85. package/dist/{zeros-CmJFiC84.js → zeros-dnQxFgAD.js} +1 -1
  86. package/package.json +1 -1
  87. package/dist/MLP-KHhikThU.js +0 -83
@@ -1,7 +1,95 @@
1
- import "../index-XjBAhiFO.js";
2
- import "./BaseLayer.js";
3
- import "../random_width-CMHmdbSu.js";
4
- import { M as i } from "../MLP-KHhikThU.js";
1
+ import { t as _, c as M, e as d, H as v } from "../index-C4JCoBvj.js";
2
+ import x from "./BaseLayer.js";
3
+ import { g as L } from "../gelu-CnCt17Lk.js";
4
+ import { v as n } from "../variable-LJT9Ld63.js";
5
+ import { r as p, d as u } from "../dropout-DfDdklfL.js";
6
+ import { r as l } from "../reshape-Boe4DuIO.js";
7
+ import { m as f } from "../mat_mul-415y5Qn2.js";
8
+ class V extends x {
9
+ cFc = null;
10
+ cProj = null;
11
+ index;
12
+ _trainable = !0;
13
+ hiddenUnits;
14
+ constructor(t, s) {
15
+ super(s), this.index = t, this.hiddenUnits = s.gpt.mlpFactor * s.gpt.nEmbed;
16
+ }
17
+ build() {
18
+ this.cFc === null && (this.cFc = n(
19
+ p([this.config.gpt.nEmbed, this.hiddenUnits], 0, 0.02),
20
+ !0
21
+ //`block_${this.index}_attn_cAttn_kernel`
22
+ )), this.cProj === null && (this.cProj = n(
23
+ p(
24
+ [this.hiddenUnits, this.config.gpt.nEmbed],
25
+ 0,
26
+ 0.02 / Math.sqrt(2 * this.config.gpt.nLayer)
27
+ ),
28
+ !0
29
+ //`block_${this.index}_attn_cProj_kernel`
30
+ ));
31
+ }
32
+ get variables() {
33
+ return [this.cFc, this.cProj];
34
+ }
35
+ get trainable() {
36
+ return this._trainable;
37
+ }
38
+ set trainable(t) {
39
+ this._trainable = t, this.cFc && (this.cFc.trainable = t), this.cProj && (this.cProj.trainable = t);
40
+ }
41
+ saveWeights(t) {
42
+ t.set(`block_${this.index}_mlpHidden`, this.cFc ? [this.cFc.clone()] : []), t.set(`block_${this.index}_mlpOut`, this.cProj ? [this.cProj.clone()] : []);
43
+ }
44
+ loadWeights(t) {
45
+ const s = t.get(`block_${this.index}_mlpOut`)?.[0], i = t.get(`block_${this.index}_mlpHidden`)?.[0];
46
+ if (!s || !i)
47
+ throw new Error(`Weights for block ${this.index} not found`);
48
+ this.cFc ? this.cFc.assign(i) : this.cFc = n(i, !0), this.cProj ? this.cProj.assign(s) : this.cProj = n(s, !0);
49
+ }
50
+ forward(t) {
51
+ return _(() => {
52
+ this.startMemory();
53
+ const [s, i, o] = t.shape, r = l(t, [s * i, o]), e = f(r, this.cFc), c = L(e);
54
+ e.dispose();
55
+ const a = f(c, this.cProj);
56
+ c.dispose();
57
+ const h = l(a, [s, i, o]);
58
+ return this.endMemory("MLP"), h;
59
+ });
60
+ }
61
+ call(t, s = !1) {
62
+ if (this.build(), s && this.config.layerConfig.checkpointMLP) {
63
+ const o = M(
64
+ // @ts-expect-error Invalid params
65
+ (r, e, c, a) => {
66
+ const h = this.forward(r);
67
+ return a([r]), { value: h, gradFunc: (g, m) => {
68
+ const [b] = m, P = d().state.activeTape;
69
+ d().state.activeTape = [];
70
+ const j = v((F, w, T) => this.forward(F))([b, e, c], g);
71
+ return d().state.activeTape = P, j;
72
+ } };
73
+ }
74
+ )(t, this.cFc, this.cProj);
75
+ if (this.config.gpt.dropout > 0) {
76
+ const r = u(o, this.config.gpt.dropout);
77
+ return o.dispose(), r;
78
+ }
79
+ return o;
80
+ } else {
81
+ const i = this.forward(t);
82
+ if (s && this.config.gpt.dropout > 0) {
83
+ const o = u(i, this.config.gpt.dropout);
84
+ return i.dispose(), o;
85
+ }
86
+ return i;
87
+ }
88
+ }
89
+ dispose() {
90
+ this.cFc?.dispose(), this.cProj?.dispose();
91
+ }
92
+ }
5
93
  export {
6
- i as default
94
+ V as default
7
95
  };
@@ -1,7 +1,7 @@
1
- import { t as r } from "../index-XjBAhiFO.js";
1
+ import { t as r } from "../index-C4JCoBvj.js";
2
2
  import m from "./BaseLayer.js";
3
- import { v as i } from "../variable-BS4AKqNU.js";
4
- import { o } from "../ones-BO01zpJG.js";
3
+ import { v as i } from "../variable-LJT9Ld63.js";
4
+ import { o } from "../ones-Bf3YR48P.js";
5
5
  class d extends m {
6
6
  gamma;
7
7
  epsilon;
@@ -1,6 +1,6 @@
1
- import { o as h, h as c, E as f, N as l, f as n, O as m, t as u, F as p } from "../index-XjBAhiFO.js";
2
- import { c as d, s as C } from "../sin-BYM-U4Ut.js";
3
- import { r as a } from "../range-DQMNzBWs.js";
1
+ import { o as h, h as c, E as f, N as l, f as n, O as m, t as u, F as p } from "../index-C4JCoBvj.js";
2
+ import { c as d, s as C } from "../sin-KmhiDuMa.js";
3
+ import { r as a } from "../range-9AzeApCc.js";
4
4
  /**
5
5
  * @license
6
6
  * Copyright 2018 Google LLC. All Rights Reserved.
@@ -1,48 +1,8 @@
1
- import { r as t } from "../random_width-CMHmdbSu.js";
2
- import "../index-XjBAhiFO.js";
3
- import { d as s } from "../tfjs_backend-Bzl2SrRo.js";
4
- import { v as r } from "../variable-BS4AKqNU.js";
5
- import { g as d } from "../gather-BUmJIS8n.js";
6
- class n {
7
- vocabSize;
8
- embedDim;
9
- tiedWeights;
10
- initializer;
11
- constructor(i, e) {
12
- this.vocabSize = i.vocabSize, this.embedDim = i.embedDim, this.initializer = t({
13
- mean: 0,
14
- stddev: 0.02
15
- }), this.tiedWeights = r(
16
- this.initializer.apply([this.vocabSize, this.embedDim]),
17
- !0,
18
- e || "tied_embedding"
19
- );
20
- }
21
- get variables() {
22
- return [this.tiedWeights];
23
- }
24
- embed(i) {
25
- return d(this.tiedWeights, i, 0);
26
- }
27
- project(i) {
28
- return s(i, this.tiedWeights.transpose());
29
- }
30
- getWeights() {
31
- return [this.tiedWeights];
32
- }
33
- setWeights(i) {
34
- this.tiedWeights.assign(i[0]);
35
- }
36
- getConfig() {
37
- return {
38
- vocabSize: this.vocabSize,
39
- embedDim: this.embedDim
40
- };
41
- }
42
- dispose() {
43
- this.tiedWeights.dispose();
44
- }
45
- }
1
+ import { T as a } from "../TiedEmbedding-CnJ1bx4q.js";
2
+ import "../index-C4JCoBvj.js";
3
+ import "../tfjs_backend-Cug-PH75.js";
4
+ import "../variable-LJT9Ld63.js";
5
+ import "../gather-ZYRWhmXR.js";
46
6
  export {
47
- n as default
7
+ a as default
48
8
  };
@@ -1,8 +1,8 @@
1
1
  import h from "./CausalSelfAttention.js";
2
- import { M as o } from "../MLP-KHhikThU.js";
2
+ import o from "./MLP.js";
3
3
  import a from "./RMSNorm.js";
4
4
  import p from "./BaseLayer.js";
5
- import { t as d } from "../index-XjBAhiFO.js";
5
+ import { t as d } from "../index-C4JCoBvj.js";
6
6
  class W extends p {
7
7
  ln1;
8
8
  attn;
@@ -1,8 +1,8 @@
1
- import { o as r, h as p, E as u, a3 as h, a4 as E, Y as S, s as $, a5 as d } from "./index-XjBAhiFO.js";
2
- import { e as K } from "./axis_util-DeydwOoC.js";
3
- import { m as T } from "./max-DJvEiCAJ.js";
4
- import { r as m } from "./reshape-DFzh97Sc.js";
5
- import { s as _ } from "./sum-C6u3xMi3.js";
1
+ import { o as r, h as p, E as u, a3 as h, a4 as E, Y as S, s as $, a5 as d } from "./index-C4JCoBvj.js";
2
+ import { e as K } from "./axis_util-BgTGy5w8.js";
3
+ import { m as T } from "./max-CP_9O2Yd.js";
4
+ import { r as m } from "./reshape-Boe4DuIO.js";
5
+ import { s as _ } from "./sum-R28pucR5.js";
6
6
  /**
7
7
  * @license
8
8
  * Copyright 2018 Google LLC. All Rights Reserved.
package/dist/main.js CHANGED
@@ -5,7 +5,7 @@ import { default as w } from "./tokeniser/bpe.js";
5
5
  import { default as D } from "./utilities/waitForModel.js";
6
6
  import { default as F } from "./data/textLoader.js";
7
7
  import { estimateMemoryUsage as N, estimateParameterCount as R, estimateResources as j, estimateTrainingMemoryUsage as q, validateConfig as z } from "./utilities/parameters.js";
8
- import "./index-XjBAhiFO.js";
8
+ import "./index-C4JCoBvj.js";
9
9
  import "./ops/cpu/scatterSub.js";
10
10
  import "./ops/webgl/scatterSub.js";
11
11
  import "./ops/cpu/gatherSub.js";
@@ -1,4 +1,4 @@
1
- import { o as m, h as s, p as c, E as M, B as p } from "./index-XjBAhiFO.js";
1
+ import { o as m, h as s, p as c, E as M, B as p } from "./index-C4JCoBvj.js";
2
2
  /**
3
3
  * @license
4
4
  * Copyright 2020 Google LLC. All Rights Reserved.
@@ -1,4 +1,4 @@
1
- import { o as r, h as e, E as x, M as c } from "./index-XjBAhiFO.js";
1
+ import { o as r, h as e, E as x, M as c } from "./index-C4JCoBvj.js";
2
2
  /**
3
3
  * @license
4
4
  * Copyright 2020 Google LLC. All Rights Reserved.
@@ -1,6 +1,6 @@
1
- import { o as m, h as c, E as f, X as i, Y as l, Z as h, s as x, x as d } from "./index-XjBAhiFO.js";
2
- import { e as v } from "./axis_util-DeydwOoC.js";
3
- import { r as E } from "./reshape-DFzh97Sc.js";
1
+ import { o as m, h as c, E as f, X as i, Y as l, Z as h, s as x, x as d } from "./index-C4JCoBvj.js";
2
+ import { e as v } from "./axis_util-BgTGy5w8.js";
3
+ import { r as E } from "./reshape-Boe4DuIO.js";
4
4
  /**
5
5
  * @license
6
6
  * Copyright 2020 Google Inc. All Rights Reserved.
@@ -1,8 +1,8 @@
1
- import { o as l, h as c, E as y, _ as E, Y as w, $ as o, a0 as u, O as v, f as I, Z as $ } from "./index-XjBAhiFO.js";
2
- import { e as A } from "./axis_util-DeydwOoC.js";
3
- import { m as f } from "./max-DJvEiCAJ.js";
4
- import { r as h } from "./reshape-DFzh97Sc.js";
5
- import { s as t } from "./sum-C6u3xMi3.js";
1
+ import { o as l, h as c, E as y, _ as E, Y as w, $ as o, a0 as u, O as v, f as I, Z as $ } from "./index-C4JCoBvj.js";
2
+ import { e as A } from "./axis_util-BgTGy5w8.js";
3
+ import { m as f } from "./max-CP_9O2Yd.js";
4
+ import { r as h } from "./reshape-Boe4DuIO.js";
5
+ import { s as t } from "./sum-R28pucR5.js";
6
6
  /**
7
7
  * @license
8
8
  * Copyright 2020 Google Inc. All Rights Reserved.
@@ -1,5 +1,5 @@
1
- import { k as n, l as t, n as m, E as i } from "./index-XjBAhiFO.js";
2
- import { z as l, c } from "./zeros-CmJFiC84.js";
1
+ import { k as n, l as t, n as m, E as i } from "./index-C4JCoBvj.js";
2
+ import { z as l, c } from "./zeros-dnQxFgAD.js";
3
3
  /**
4
4
  * @license
5
5
  * Copyright 2018 Google LLC. All Rights Reserved.
@@ -1,4 +1,4 @@
1
- import { e as p } from "../index-XjBAhiFO.js";
1
+ import { e as p } from "../index-C4JCoBvj.js";
2
2
  import "./cpu/appendCache.js";
3
3
  import "./webgl/appendCache.js";
4
4
  function a(e, n, r) {
@@ -1,2 +1,2 @@
1
1
  import { Tensor } from '@tensorflow/tfjs-core';
2
- export declare function attentionMask(q: Tensor, k: Tensor, mask: Tensor, divisor: number): Tensor;
2
+ export declare function attentionMask(q: Tensor, k: Tensor, divisor: number, mask?: Tensor, pastLen?: number): Tensor;
@@ -1,10 +1,10 @@
1
- import { e as o } from "../index-XjBAhiFO.js";
1
+ import { e as i } from "../index-C4JCoBvj.js";
2
2
  import "./cpu/attentionMask.js";
3
3
  import "./webgl/attentionMask.js";
4
4
  import "./grads/attentionMask.js";
5
- function s(t, n, r, e) {
6
- return o().runKernel("AttentionMask", { q: t, k: n, mask: r }, { divisor: e });
5
+ function f(t, n, e, r, o) {
6
+ return r ? i().runKernel("AttentionMask", { q: t, k: n, mask: r }, { divisor: e, pastLen: o || 0 }) : i().runKernel("AttentionMask", { q: t, k: n }, { divisor: e, pastLen: o || 0 });
7
7
  }
8
8
  export {
9
- s as attentionMask
9
+ f as attentionMask
10
10
  };
@@ -1,5 +1,5 @@
1
- import { r as a, e as m } from "../../index-XjBAhiFO.js";
2
- import { c as d } from "../../concat-DS_qH7MI.js";
1
+ import { r as a, e as m } from "../../index-C4JCoBvj.js";
2
+ import { c as d } from "../../concat-CuRsVY-K.js";
3
3
  function r(n) {
4
4
  const { cache: c, item: t } = n.inputs, { maxSize: o } = n.attrs, e = d([c, t], 2), s = e.shape[2];
5
5
  if (s > o) {
@@ -1,18 +1,22 @@
1
- import { r as s, f as i } from "../../index-XjBAhiFO.js";
2
- import { m as k } from "../../mat_mul-CKwFEV1Q.js";
3
- function a(t) {
4
- const { q: e, k: o, mask: r } = t.inputs, { divisor: c } = t.attrs, n = e.shape[2], m = k(e, o, !1, !0).mul(i(c)), l = r.slice([0, 0], [n, n]).expandDims(0).expandDims(0);
5
- return m.add(l);
1
+ import { r as o, f as l } from "../../index-C4JCoBvj.js";
2
+ import { m as k } from "../../mat_mul-415y5Qn2.js";
3
+ function r(t) {
4
+ const { q: e, k: c, mask: n } = t.inputs, { divisor: m } = t.attrs, s = e.shape[2], a = k(e, c, !1, !0).mul(l(m));
5
+ if (n) {
6
+ const i = n.slice([0, 0], [s, s]).expandDims(0).expandDims(0);
7
+ return a.add(i);
8
+ }
9
+ return a;
6
10
  }
7
11
  const d = {
8
12
  kernelName: "AttentionMask",
9
13
  backendName: "cpu",
10
- kernelFunc: a
14
+ kernelFunc: r
11
15
  };
12
- s(d);
16
+ o(d);
13
17
  const u = {
14
18
  kernelName: "AttentionMask",
15
19
  backendName: "tensorflow",
16
- kernelFunc: a
20
+ kernelFunc: r
17
21
  };
18
- s(u);
22
+ o(u);
@@ -1,5 +1,5 @@
1
- import { r as n } from "../../index-XjBAhiFO.js";
2
- import { s as f } from "../../softmax-4DOn6cPq.js";
1
+ import { r as n } from "../../index-C4JCoBvj.js";
2
+ import { s as f } from "../../softmax-Cujsg4ay.js";
3
3
  function r(t) {
4
4
  const { inputs: s, attrs: i } = t, { logits: o } = s, { dim: a, dropoutRate: e } = i;
5
5
  if (!o)
@@ -1,6 +1,6 @@
1
- import { o as u, h as c, E as g, L as h, r as m, s as p } from "../../index-XjBAhiFO.js";
2
- import { r as l } from "../../range-DQMNzBWs.js";
3
- import { s as N } from "../../stack-DaIMO5iX.js";
1
+ import { o as u, h as c, E as g, L as h, r as m, s as p } from "../../index-C4JCoBvj.js";
2
+ import { r as l } from "../../range-9AzeApCc.js";
3
+ import { s as N } from "../../stack-D1YjmgKN.js";
4
4
  /**
5
5
  * @license
6
6
  * Copyright 2018 Google LLC. All Rights Reserved.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,40 @@
1
+ import { r as t, t as d } from "../../index-C4JCoBvj.js";
2
+ const o = 0.7978845608028654, c = 0.044715;
3
+ function m(u) {
4
+ const { inputs: l } = u, { x: e } = l, n = e;
5
+ return d(() => {
6
+ const r = n.mul(n).mul(n), s = n.add(r.mul(c)).mul(o).tanh().add(1).mul(0.5);
7
+ return n.mul(s);
8
+ });
9
+ }
10
+ const K = {
11
+ kernelName: "Gelu",
12
+ backendName: "cpu",
13
+ kernelFunc: m
14
+ };
15
+ t(K);
16
+ const p = {
17
+ kernelName: "Gelu",
18
+ backendName: "tensorflow",
19
+ kernelFunc: m
20
+ };
21
+ t(p);
22
+ function i(u) {
23
+ const { dy: l, x: e } = u.inputs;
24
+ return d(() => {
25
+ const n = e.square(), r = n.mul(e), a = e.add(r.mul(c)).mul(o).tanh(), f = a.square().neg().add(1), k = n.mul(3 * c).add(1), g = a.add(1).mul(0.5), G = e.mul(f).mul(o).mul(k).mul(0.5), N = g.add(G);
26
+ return l.mul(N);
27
+ });
28
+ }
29
+ const x = {
30
+ kernelName: "GeluGrad",
31
+ backendName: "cpu",
32
+ kernelFunc: i
33
+ };
34
+ t(x);
35
+ const h = {
36
+ kernelName: "GeluGrad",
37
+ backendName: "tensorflow",
38
+ kernelFunc: i
39
+ };
40
+ t(h);
@@ -1,4 +1,4 @@
1
- import { r as e, b as u } from "../../index-XjBAhiFO.js";
1
+ import { r as e, b as u } from "../../index-C4JCoBvj.js";
2
2
  function n(o) {
3
3
  const { inputs: r } = o, { a: l, b: t } = r;
4
4
  return console.warn("Using fallback mulDrop implementation without dropout."), u(l, t);
@@ -1,6 +1,6 @@
1
- import { r as q } from "../../index-XjBAhiFO.js";
2
- import { r as o } from "../../reshape-DFzh97Sc.js";
3
- import { s as x } from "../../split-CkbeVdF8.js";
1
+ import { r as q } from "../../index-C4JCoBvj.js";
2
+ import { r as o } from "../../reshape-Boe4DuIO.js";
3
+ import { s as x } from "../../split-DbcNm1-i.js";
4
4
  function v(p) {
5
5
  const { x: c, kernel: K } = p.inputs, { heads: n } = p.attrs, [s, e, t] = c.shape, a = o(c, [s * e, t]), i = a.dot(K);
6
6
  a.dispose();
@@ -1,8 +1,8 @@
1
- import { r as S } from "../../index-XjBAhiFO.js";
2
- import { r as F } from "../../range-DQMNzBWs.js";
3
- import { g as I } from "../../gather-BUmJIS8n.js";
4
- import { s as E } from "../../stack-DaIMO5iX.js";
5
- import { c as T } from "../../concat-DS_qH7MI.js";
1
+ import { r as S } from "../../index-C4JCoBvj.js";
2
+ import { r as F } from "../../range-9AzeApCc.js";
3
+ import { g as I } from "../../gather-ZYRWhmXR.js";
4
+ import { s as E } from "../../stack-D1YjmgKN.js";
5
+ import { c as T } from "../../concat-CuRsVY-K.js";
6
6
  function U(t, c, p, o, r) {
7
7
  const n = o.shape[3], s = p;
8
8
  if (s > n) return o;
@@ -1,7 +1,7 @@
1
- import { o as l, k, h, E as g, a2 as w, r as $, s as d, b as m } from "../../index-XjBAhiFO.js";
2
- import { r as b } from "../../range-DQMNzBWs.js";
3
- import { s as E } from "../../stack-DaIMO5iX.js";
4
- import { o as D } from "../../ones-BO01zpJG.js";
1
+ import { o as l, k, h, E as g, a2 as w, r as $, s as d, b as m } from "../../index-C4JCoBvj.js";
2
+ import { r as b } from "../../range-9AzeApCc.js";
3
+ import { s as E } from "../../stack-D1YjmgKN.js";
4
+ import { o as D } from "../../ones-Bf3YR48P.js";
5
5
  function N(n, r, t) {
6
6
  const s = r.rank > 1 ? r.shape[r.rank - 1] : 1, e = r.rank > 1 ? r.rank - 1 : 1, o = `Must have updates.shape = indices.shape[:batchDim] + shape[sliceDim:], got updates.shape: ${t.shape}, indices.shape: ${r.shape}, shape: ${n}, sliceDim: ${s}, and batchDim: ${e}.`;
7
7
  if (t.rank < e)
@@ -1,4 +1,4 @@
1
- import { e as t } from "../index-XjBAhiFO.js";
1
+ import { e as t } from "../index-C4JCoBvj.js";
2
2
  import "./cpu/fusedSoftmax.js";
3
3
  import "./webgl/fusedSoftmax.js";
4
4
  import "./grads/fusedSoftmax.js";
@@ -1,4 +1,4 @@
1
- import { e as n } from "../index-XjBAhiFO.js";
1
+ import { e as n } from "../index-C4JCoBvj.js";
2
2
  import "./cpu/gatherSub.js";
3
3
  import "./webgl/gatherSub.js";
4
4
  function f(r, e, t) {
@@ -0,0 +1,3 @@
1
+ import { Tensor } from '@tensorflow/tfjs-core';
2
+ export declare function gelu(x: Tensor): Tensor;
3
+ export declare function dGelu(dy: Tensor, x: Tensor): Tensor;
@@ -0,0 +1,8 @@
1
+ import "../index-C4JCoBvj.js";
2
+ import "./cpu/gelu.js";
3
+ import "./webgl/gelu.js";
4
+ import { d as e, g as i } from "../gelu-CnCt17Lk.js";
5
+ export {
6
+ e as dGelu,
7
+ i as gelu
8
+ };
@@ -1,4 +1,4 @@
1
- import { g as i } from "../../index-XjBAhiFO.js";
1
+ import { g as i } from "../../index-C4JCoBvj.js";
2
2
  const u = {
3
3
  kernelName: "AttentionMask",
4
4
  inputsToSave: ["q", "k"],
@@ -1,6 +1,6 @@
1
- import { g as p, b as m, s as d } from "../../index-XjBAhiFO.js";
1
+ import { g as p, b as m, s as d } from "../../index-C4JCoBvj.js";
2
2
  import { mulDrop as c } from "../mulDrop.js";
3
- import { s as f } from "../../sum-C6u3xMi3.js";
3
+ import { s as f } from "../../sum-R28pucR5.js";
4
4
  const g = {
5
5
  kernelName: "FusedSoftmax",
6
6
  outputsToSave: [!0],
@@ -0,0 +1,2 @@
1
+ import { GradConfig } from '@tensorflow/tfjs-core';
2
+ export declare const geluGradConfig: GradConfig;
@@ -0,0 +1,5 @@
1
+ import "../../index-C4JCoBvj.js";
2
+ import { a as e } from "../../gelu-CnCt17Lk.js";
3
+ export {
4
+ e as geluGradConfig
5
+ };
@@ -1,4 +1,4 @@
1
- import { g as v } from "../../index-XjBAhiFO.js";
1
+ import { g as v } from "../../index-C4JCoBvj.js";
2
2
  const g = {
3
3
  kernelName: "QKV",
4
4
  inputsToSave: ["x", "kernel"],
@@ -1,4 +1,4 @@
1
- import { g as a, e as i } from "../../index-XjBAhiFO.js";
1
+ import { g as a, e as i } from "../../index-C4JCoBvj.js";
2
2
  function p(n, e, s, o) {
3
3
  return i().runKernel("Rope", { x: n, sin: e, cos: s }, { pastLen: o });
4
4
  }
@@ -1,4 +1,4 @@
1
- import { e as t } from "../index-XjBAhiFO.js";
1
+ import { e as t } from "../index-C4JCoBvj.js";
2
2
  import "./cpu/mulDropout.js";
3
3
  import "./webgl/mulDropout.js";
4
4
  function m(r, o, e, n) {
@@ -1,4 +1,4 @@
1
- import { r as o } from "../../index-XjBAhiFO.js";
1
+ import { r as o } from "../../index-C4JCoBvj.js";
2
2
  function r(e) {
3
3
  const { logits: t, labels: n } = e.inputs;
4
4
  return e.backend.executeMultipleOutputs("SparseSoftmaxCrossEntropyWithLogits", [], [t, n], 2);
package/dist/ops/qkv.js CHANGED
@@ -1,4 +1,4 @@
1
- import { e as o } from "../index-XjBAhiFO.js";
1
+ import { e as o } from "../index-C4JCoBvj.js";
2
2
  import "./cpu/qkv.js";
3
3
  import "./webgl/qkv.js";
4
4
  import "./grads/qkv.js";
@@ -1,4 +1,4 @@
1
- import { e as i } from "../index-XjBAhiFO.js";
1
+ import { e as i } from "../index-C4JCoBvj.js";
2
2
  import "./cpu/scatterSub.js";
3
3
  import "./webgl/scatterSub.js";
4
4
  function c(t, r, e) {
@@ -1,4 +1,4 @@
1
- import { r as h } from "../../index-XjBAhiFO.js";
1
+ import { r as h } from "../../index-C4JCoBvj.js";
2
2
  class m {
3
3
  variableNames = ["cache", "item"];
4
4
  outputShape;
@@ -1,11 +1,14 @@
1
- import { r as m } from "../../index-XjBAhiFO.js";
2
- class k {
3
- variableNames = ["q", "k", "mask"];
1
+ import { r as h } from "../../index-C4JCoBvj.js";
2
+ class l {
3
+ variableNames = ["q", "k"];
4
4
  outputShape;
5
5
  userCode;
6
- customUniforms = [{ name: "divisor", type: "float" }];
7
- constructor(t, a, o, e) {
8
- this.outputShape = [t, a, o, o], this.userCode = `
6
+ customUniforms = [
7
+ { name: "divisor", type: "float" },
8
+ { name: "pastLen", type: "int" }
9
+ ];
10
+ constructor(t, s, e, n, a) {
11
+ this.outputShape = [t, s, e, n], this.userCode = `
9
12
  void main() {
10
13
  ivec4 coords = getOutputCoords(); // [batch, nh, t1, t2]
11
14
  int b = coords.x;
@@ -14,30 +17,28 @@ class k {
14
17
  int t2 = coords.w;
15
18
 
16
19
  float sum = 0.0;
17
- for (int i = 0; i < ${e}; ++i) {
20
+ for (int i = 0; i < ${a}; ++i) {
18
21
  float qv = getQ(b, h, t1, i);
19
- float kv = getK(b, h, t2, i); // k is transposed on last two dims
22
+ float kv = getK(b, h, t2, i);
20
23
  sum += qv * kv;
21
24
  }
22
25
 
23
26
  // Scale by divisor
24
27
  float scaled = sum * divisor;
25
28
 
26
- // Add mask
27
- float maskVal = getMask(t1, t2); // mask is [T,T]
28
-
29
- setOutput(scaled + maskVal);
29
+ // Mask out future positions
30
+ setOutput((t2 > t1 + pastLen) ? -1.0/0.0 : scaled);
30
31
  }
31
32
  `;
32
33
  }
33
34
  }
34
- function l(s) {
35
- const { q: t, k: a, mask: o } = s.inputs, { divisor: e } = s.attrs, n = s.backend, i = t.shape[0], r = t.shape[2], c = t.shape[1], d = new k(i, c, r, t.shape[3]);
36
- return n.runWebGLProgram(d, [t, a, o], "float32", [[e]]);
35
+ function m(o) {
36
+ const { q: t, k: s } = o.inputs, { divisor: e, pastLen: n } = o.attrs, a = o.backend, i = t.shape[0], r = t.shape[2], c = s.shape[2], u = t.shape[1], p = t.shape[3], d = new l(i, u, r, c, p);
37
+ return a.runWebGLProgram(d, [t, s], "float32", [[e], [n]]);
37
38
  }
38
- const u = {
39
+ const k = {
39
40
  kernelName: "AttentionMask",
40
41
  backendName: "webgl",
41
- kernelFunc: l
42
+ kernelFunc: m
42
43
  };
43
- m(u);
44
+ h(k);