@genai-fi/nanogpt 0.1.3 → 0.1.4

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.
@@ -33,6 +33,7 @@ export default class NanoGPT {
33
33
  set trainable(value: boolean);
34
34
  private validateInput;
35
35
  private calculateLoss;
36
+ private computeAttentionRollout;
36
37
  forward(idx: TF.Tensor, targets?: TF.Tensor, training?: boolean, includeAttention?: boolean): {
37
38
  logits: TF.Tensor;
38
39
  loss?: TF.Tensor;
@@ -54,8 +54,8 @@ class $ {
54
54
  }
55
55
  inputPhase(t, e = !1) {
56
56
  return this.tf.tidy(() => {
57
- const [, s] = t.shape, n = this.wte.embed(t), i = this.tf.range(0, s, 1, "int32"), o = this.wpe.apply(i), h = n.add(o);
58
- return this.drop.apply(h, { training: e });
57
+ const [, s] = t.shape, i = this.wte.embed(t), n = this.tf.range(0, s, 1, "int32"), h = this.wpe.apply(n), o = i.add(h);
58
+ return this.drop.apply(o, { training: e });
59
59
  });
60
60
  }
61
61
  setSkipMask(t) {
@@ -90,44 +90,60 @@ class $ {
90
90
  throw console.error("Error computing loss:", s), new Error(`Loss computation failed: ${s}`);
91
91
  }
92
92
  }
93
- forward(t, e, s = !1, n = !1) {
93
+ // Attention rollout per Abnar & Zuidema (2020)
94
+ // Expects list of (B, T, T) attention matrices already averaged over heads.
95
+ computeAttentionRollout(t) {
96
+ return this.tf.tidy(() => {
97
+ if (t.length === 0)
98
+ throw new Error("No attentions for rollout");
99
+ const e = t[0].shape[0], s = t[0].shape[1], i = this.tf.eye(s, s).expandDims(0);
100
+ let n = i.tile([e, 1, 1]);
101
+ for (const h of t) {
102
+ let o = h.add(i);
103
+ o = o.div(o.sum(-1, !0)), n = o.matMul(n);
104
+ }
105
+ return n;
106
+ });
107
+ }
108
+ forward(t, e, s = !1, i = !1) {
94
109
  return this.validateInput(t), this.tf.tidy(() => {
95
- let i = this.inputPhase(t, s), o;
96
- n && (o = this.tf.zeros([i.shape[0], i.shape[1], i.shape[1]]));
97
- for (const l of this.blocks) {
98
- const { output: r, attention: f } = l.call(i, s, n);
99
- i = r, f && o && (o = o.add(f));
110
+ let n = this.inputPhase(t, s);
111
+ const h = [];
112
+ for (const c of this.blocks) {
113
+ const { output: p, attention: a } = c.call(n, s, i);
114
+ n = p, i && a && h.push(a);
100
115
  }
101
- o && (o = o.div(this.blocks.length)), i = this.lnF.apply(i);
102
- const h = this.wte.project(i);
103
- let a;
104
- return e && (a = this.calculateLoss(h, e)), { logits: h, loss: a, attention: n ? o : void 0 };
116
+ let o;
117
+ i && h.length > 0 && (o = this.computeAttentionRollout(h)), n = this.lnF.apply(n);
118
+ const l = this.wte.project(n);
119
+ let r;
120
+ return e && (r = this.calculateLoss(l, e)), { logits: l, loss: r, attention: i ? o : void 0 };
105
121
  });
106
122
  }
107
123
  generate(t, e) {
108
- const s = e?.temperature ?? 1, n = e?.topK, i = e?.usePadding ?? !1, o = e?.includeAttention ?? !1;
124
+ const s = e?.temperature ?? 1, i = e?.topK, n = e?.usePadding ?? !1, h = e?.includeAttention ?? !1;
109
125
  return this.tf.tidy(() => {
110
- const h = t, a = h.shape[1], l = a <= this.config.blockSize ? h : h.slice(
111
- [0, a - this.config.blockSize],
112
- [h.shape[0], this.config.blockSize]
113
- ), r = i ? this.config.blockSize - l.shape[1] : 0, f = r > 0 ? this.tf.pad(l, [
126
+ const o = t, l = o.shape[1], r = l <= this.config.blockSize ? o : o.slice(
127
+ [0, l - this.config.blockSize],
128
+ [o.shape[0], this.config.blockSize]
129
+ ), c = n ? this.config.blockSize - r.shape[1] : 0, p = c > 0 ? this.tf.pad(r, [
114
130
  [0, 0],
115
- [0, r]
116
- ]) : l, { logits: g, attention: p } = this.forward(f, void 0, !1, o), d = g.shape[1] - 1 - r, m = g.slice([0, d, 0], [g.shape[0], 1, g.shape[2]]), u = p ? p.slice([0, d, 0], [p.shape[0], 1, p.shape[2]]) : void 0, b = m.div(s);
117
- let c;
118
- if (n) {
119
- const { values: k, indices: w } = this.tf.topk(b, n), E = this.tf.multinomial(k.squeeze([1]), 1);
120
- c = this.tf.gather(w.squeeze([1]), E, 1);
131
+ [0, c]
132
+ ]) : r, { logits: a, attention: g } = this.forward(p, void 0, !1, h), d = a.shape[1] - 1 - c, m = a.slice([0, d, 0], [a.shape[0], 1, a.shape[2]]), u = g ? g.slice([0, d, 0], [g.shape[0], 1, g.shape[2]]) : void 0, b = m.div(s);
133
+ let f;
134
+ if (i) {
135
+ const { values: k, indices: w } = this.tf.topk(b, i), E = this.tf.multinomial(k.squeeze([1]), 1);
136
+ f = this.tf.gather(w.squeeze([1]), E, 1);
121
137
  } else
122
- c = this.tf.multinomial(b.squeeze([1]), 1);
123
- return c = c.reshape([1, 1]), { output: c, attention: u?.squeeze([1]) };
138
+ f = this.tf.multinomial(b.squeeze([1]), 1);
139
+ return f = f.reshape([1, 1]), { output: f, attention: u?.squeeze([1]) };
124
140
  });
125
141
  }
126
142
  getNumParams() {
127
143
  const t = this.config.vocabSize * this.config.nEmbed + this.config.blockSize * this.config.nEmbed, e = this.config.nLayer * (4 * this.config.nEmbed * this.config.nEmbed + // qkv + proj
128
144
  2 * this.config.nEmbed), s = this.config.nLayer * (4 * this.config.nEmbed * this.config.nEmbed + // fc
129
- this.config.nEmbed * 4 * this.config.nEmbed), n = this.config.nEmbed + this.config.vocabSize * this.config.nEmbed;
130
- return t + e + s + n;
145
+ this.config.nEmbed * 4 * this.config.nEmbed), i = this.config.nEmbed + this.config.vocabSize * this.config.nEmbed;
146
+ return t + e + s + i;
131
147
  }
132
148
  }
133
149
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@genai-fi/nanogpt",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "main": "dist/main.js",
6
6
  "types": "dist/main.d.ts",