@genai-fi/nanogpt 1.2.1 → 1.2.3
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/inference/Beamer.js
CHANGED
|
@@ -79,8 +79,9 @@ var f = class {
|
|
|
79
79
|
text: "",
|
|
80
80
|
terminated: !1,
|
|
81
81
|
context: a.context,
|
|
82
|
-
contextLength: a.contextLength
|
|
83
|
-
|
|
82
|
+
contextLength: a.contextLength,
|
|
83
|
+
candidates: 0
|
|
84
|
+
}], s = t.maxBeamLength, c = t.endOnWhiteSpace === !0 ? Math.max(s, t.maxLength ?? s + this.model.config.blockSize) : s, l = Date.now(), u = 0;
|
|
84
85
|
for (let e = 0; e < c && this.active; e++) {
|
|
85
86
|
let e = o.filter((e) => !e.terminated);
|
|
86
87
|
if (e.length === 0) break;
|
|
@@ -88,7 +89,7 @@ var f = class {
|
|
|
88
89
|
for (let n = 0; n < e.length; n++) {
|
|
89
90
|
let o = e[n], c = i[n], l = d(c, t.topK ? Math.min(t.topK, r) : Math.max(1, r));
|
|
90
91
|
for (let e of l) {
|
|
91
|
-
let n = this.actualTokeniser.decode([e.token]), r = this.shouldTerminate(t.allowSpecial ?? !1, e.token), i = o.tokens.length + 1
|
|
92
|
+
let n = this.actualTokeniser.decode([e.token]), r = this.shouldTerminate(t.allowSpecial ?? !1, e.token), i = o.tokens.length + 1 > s, c = t.endOnWhiteSpace === !0 && i && /\s/.test(n), l = c && o.tokens.length > 0 && /^\s/.test(n);
|
|
92
93
|
a.push({
|
|
93
94
|
parent: o,
|
|
94
95
|
token: e.token,
|
|
@@ -107,26 +108,42 @@ var f = class {
|
|
|
107
108
|
let t = e.terminatedByToken || e.terminatedByWhitespace, n = !e.dropLeadingWhitespaceTerminator, i = n ? e.parent.tokens.concat(e.token) : e.parent.tokens.slice(), a = e.terminatedByToken || e.dropLeadingWhitespaceTerminator ? e.parent.text : e.parent.text + e.tokenText, o = n ? this.appendTokenToContext(e.parent.context, e.parent.contextLength, e.token) : {
|
|
108
109
|
context: e.parent.context.clone(),
|
|
109
110
|
contextLength: e.parent.contextLength
|
|
110
|
-
};
|
|
111
|
-
|
|
111
|
+
}, s = e.parent._output?.slice() ?? [];
|
|
112
|
+
n && s.push({
|
|
113
|
+
token: e.token,
|
|
114
|
+
score: e.score,
|
|
115
|
+
text: e.tokenText,
|
|
116
|
+
terminated: t,
|
|
117
|
+
confidence: null,
|
|
118
|
+
logits: null,
|
|
119
|
+
scores: null,
|
|
120
|
+
hiddenStates: null,
|
|
121
|
+
attention: null,
|
|
122
|
+
loss: null,
|
|
123
|
+
multinomialRand: null
|
|
124
|
+
}), c.push({
|
|
112
125
|
tokens: i,
|
|
113
126
|
score: e.score,
|
|
114
127
|
text: a,
|
|
115
128
|
terminated: t,
|
|
116
129
|
context: o.context,
|
|
117
|
-
contextLength: o.contextLength
|
|
130
|
+
contextLength: o.contextLength,
|
|
131
|
+
_output: s,
|
|
132
|
+
candidates: u
|
|
118
133
|
});
|
|
119
134
|
}
|
|
120
|
-
if (o.forEach((e) => e.context.dispose()), c.length === 0) break;
|
|
135
|
+
if (u += a.length - c.length, o.forEach((e) => e.context.dispose()), c.length === 0) break;
|
|
121
136
|
o = c, n && Date.now() - l >= 40 && (n(o.slice(0, t.beams)), l = Date.now());
|
|
122
137
|
}
|
|
123
|
-
this.active = !1;
|
|
124
|
-
let
|
|
125
|
-
for (let e of
|
|
138
|
+
this.active = !1, u += o.length;
|
|
139
|
+
let f = o.slice().sort((e, t) => t.score - e.score), p = [], m = /* @__PURE__ */ new Set();
|
|
140
|
+
for (let e of f) {
|
|
126
141
|
let n = e.text.trim();
|
|
127
|
-
if (
|
|
142
|
+
if (m.has(n) || (m.add(n), p.push(e)), p.length >= t.beams) break;
|
|
128
143
|
}
|
|
129
|
-
return o.forEach((e) =>
|
|
144
|
+
return o.forEach((e) => {
|
|
145
|
+
e.context.dispose(), e.candidates = u;
|
|
146
|
+
}), p;
|
|
130
147
|
}
|
|
131
148
|
};
|
|
132
149
|
//#endregion
|
|
@@ -152,7 +152,10 @@ var y = class extends e {
|
|
|
152
152
|
content: "",
|
|
153
153
|
_timestamp: Date.now()
|
|
154
154
|
}), n = !0, this.resetCache(!e?.noCache)) : (this.lastToken < 0 || t) && this.resetCache(!e?.noCache);
|
|
155
|
-
let r = this.lastToken >= 0 && this.cache ? i([this.lastToken], [1, 1], "int32") : await m(this.actualTokeniser, this.model.config.blockSize, t ? n ? this.outputConversation.slice(0, -1) : this.outputConversation : void 0,
|
|
155
|
+
let r = this.lastToken >= 0 && this.cache ? i([this.lastToken], [1, 1], "int32") : await m(this.actualTokeniser, this.model.config.blockSize, t ? n ? this.outputConversation.slice(0, -1) : this.outputConversation : void 0, n ? e : {
|
|
156
|
+
...e,
|
|
157
|
+
continuation: !0
|
|
158
|
+
}), a = e?.maxLength ?? 1e3;
|
|
156
159
|
for (let t = 0; t < a && this.active; t++) {
|
|
157
160
|
let n = await this._generateToken(r, this.cache ? this.cache : void 0, {
|
|
158
161
|
...e,
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Conversation } from '../tokeniser/type';
|
|
2
2
|
import { Tensor } from '@tensorflow/tfjs-core';
|
|
3
3
|
export interface IGeneratorOutput {
|
|
4
|
-
outputTensor: Tensor;
|
|
5
4
|
token: number;
|
|
6
5
|
text: string;
|
|
7
6
|
confidence: number | null;
|
|
@@ -14,6 +13,9 @@ export interface IGeneratorOutput {
|
|
|
14
13
|
multinomialRand: number | null;
|
|
15
14
|
terminated: boolean;
|
|
16
15
|
}
|
|
16
|
+
export interface IGeneratorOutputInternal extends IGeneratorOutput {
|
|
17
|
+
outputTensor: Tensor;
|
|
18
|
+
}
|
|
17
19
|
export interface GeneratorConversation extends Conversation {
|
|
18
20
|
_completed?: boolean;
|
|
19
21
|
_timestamp?: number;
|
|
@@ -59,4 +61,6 @@ export interface IBeam {
|
|
|
59
61
|
tokens: number[];
|
|
60
62
|
score: number;
|
|
61
63
|
text: string;
|
|
64
|
+
candidates: number;
|
|
65
|
+
_output?: IGeneratorOutput[];
|
|
62
66
|
}
|
|
@@ -89,6 +89,9 @@ var t = [
|
|
|
89
89
|
case "system":
|
|
90
90
|
r.push([a[2]]), e = !0;
|
|
91
91
|
break;
|
|
92
|
+
case "text":
|
|
93
|
+
r[r.length - 1][0] !== this.bosToken && r.push([this.bosToken]);
|
|
94
|
+
break;
|
|
92
95
|
}
|
|
93
96
|
switch (r.push(s), t.role) {
|
|
94
97
|
case "user":
|
|
@@ -100,11 +103,14 @@ var t = [
|
|
|
100
103
|
case "system":
|
|
101
104
|
r.push([o[2]]);
|
|
102
105
|
break;
|
|
106
|
+
case "text":
|
|
107
|
+
r.push([this.eosToken]);
|
|
108
|
+
break;
|
|
103
109
|
}
|
|
104
110
|
n && i && e ? (i.push([!1]), i.push(s.map(() => !1)), i.push([!1])) : n && i && (i.push([!1]), i.push(s.map(() => !0)), i.push([!0]));
|
|
105
111
|
}
|
|
106
112
|
let s = r.flat();
|
|
107
|
-
return t ? (s.push(a[1]), n && i && i.push([!1])) : (s.push(this.eosToken), n && i && i.push([!0])), n && i ? {
|
|
113
|
+
return t ? (s.push(a[1]), n && i && i.push([!1])) : (s[s.length - 1] !== this.eosToken && s.push(this.eosToken), n && i && i.push([!0])), n && i ? {
|
|
108
114
|
tokens: s,
|
|
109
115
|
mask: i.flat()
|
|
110
116
|
} : s;
|