@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.
- package/dist/NanoGPTModel.d.ts +1 -0
- package/dist/NanoGPTModel.js +43 -27
- package/package.json +1 -1
package/dist/NanoGPTModel.d.ts
CHANGED
|
@@ -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;
|
package/dist/NanoGPTModel.js
CHANGED
|
@@ -54,8 +54,8 @@ class $ {
|
|
|
54
54
|
}
|
|
55
55
|
inputPhase(t, e = !1) {
|
|
56
56
|
return this.tf.tidy(() => {
|
|
57
|
-
const [, s] = t.shape,
|
|
58
|
-
return this.drop.apply(
|
|
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
|
-
|
|
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
|
|
96
|
-
|
|
97
|
-
for (const
|
|
98
|
-
const { output:
|
|
99
|
-
|
|
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
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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,
|
|
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
|
|
111
|
-
[0,
|
|
112
|
-
[
|
|
113
|
-
),
|
|
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,
|
|
116
|
-
]) :
|
|
117
|
-
let
|
|
118
|
-
if (
|
|
119
|
-
const { values: k, indices: w } = this.tf.topk(b,
|
|
120
|
-
|
|
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
|
-
|
|
123
|
-
return
|
|
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),
|
|
130
|
-
return t + e + s +
|
|
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 {
|