@genai-fi/nanogpt 1.2.2 → 1.2.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/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;
|
|
@@ -100,33 +101,49 @@ var f = class {
|
|
|
100
101
|
});
|
|
101
102
|
}
|
|
102
103
|
}
|
|
103
|
-
let c = o.filter((e) => e.terminated).slice();
|
|
104
|
+
let c = o.filter((e) => e.terminated), f = c.slice();
|
|
104
105
|
a.sort((e, t) => t.score - e.score);
|
|
105
106
|
for (let e of a) {
|
|
106
|
-
if (
|
|
107
|
+
if (f.length >= r) break;
|
|
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
|
+
}), f.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()),
|
|
121
|
-
o =
|
|
135
|
+
if (u += a.length - f.length + c.length, o.forEach((e) => e.context.dispose()), f.length === 0) break;
|
|
136
|
+
o = f, 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
|
|
@@ -2,8 +2,8 @@ import { I as e } from "../dist-Da20xy8E.js";
|
|
|
2
2
|
//#region lib/inference/tokenisePrompt.ts
|
|
3
3
|
async function t(t, n, r, i) {
|
|
4
4
|
if (r) {
|
|
5
|
-
let a = r.length > 0 && r[r.length - 1].role === "text", o;
|
|
6
|
-
return
|
|
5
|
+
let a = r.length > 0 && (r[r.length - 1].role === "text" || r[r.length - 1].role === "assistant"), o;
|
|
6
|
+
return i?.nonConversational ? o = a && i?.continuation ? [t.bosToken, ...t.encode(r[r.length - 1].content)] : t.encodeAsSequence(r, !0) : a && i?.continuation ? (o = t.encodeConversation(r, !0), o[o.length - 1] === t.getSpecialTokenIndex("<|assistant_start|>") && o[o.length - 2] === t.getSpecialTokenIndex("<|assistant_end|>") && (o.pop(), o.pop())) : o = t.encodeConversation(r, !0), o.length > n && (o = o.slice(-n)), e([o], [1, o.length], "int32");
|
|
7
7
|
} else {
|
|
8
8
|
let n = i?.nonConversational ? void 0 : t.getSpecialTokenIndex("<|assistant_start|>"), r = n === void 0 ? [t.bosToken] : [t.bosToken, n];
|
|
9
9
|
return e([r], [1, r.length], "int32");
|
|
@@ -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
|
}
|