-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtransformers.js
214 lines (198 loc) · 8.55 KB
/
transformers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
class PretrainedModel {
static async loadSession(modelSource) {
console.log('Loading session from', modelSource);
const response = await fetch(modelSource, { cache: 'force-cache' });
const modelBuffer = await response.arrayBuffer();
const session = await ort.InferenceSession.create(modelBuffer, { executionProviders: ["wasm"] });
console.log('Session loaded from', modelSource);
return session;
}
}
class AutoModelForSeq2SeqLM extends PretrainedModel {
constructor(encoderSession, initDecoderSession, decoderSession) {
super();
this.encoderSession = encoderSession;
this.initDecoderSession = initDecoderSession;
this.decoderSession = decoderSession;
}
static async fromPretrained(modelId, modelsPath, progressAsyncCallback) {
// TODO: This should load different model types. Right now it's hardcoded to T5.
const modelIdParts = modelId.split('/');
const modelName = modelIdParts[modelIdParts.length - 1];
const suffix = "-quantized";
const encoderUrl = `${modelsPath}/${modelName}-encoder${suffix}.onnx`;
const initDecoderUrl = `${modelsPath}/${modelName}-init-decoder${suffix}.onnx`;
const decoderUrl = `${modelsPath}/${modelName}-decoder${suffix}.onnx`;
const progressMax = 4;
let progress = 0;
async function incrementProgress() {
progress++;
const p = progress / progressMax;
console.log(`Loading model ${modelId}... ${p * 100}%`);
if (progressAsyncCallback) {
await progressAsyncCallback(p);
}
}
await incrementProgress();
const encoderSessionPromise = this.loadSession(encoderUrl);
const initDecoderSessionPromise = this.loadSession(initDecoderUrl);
const decoderSessionPromise = this.loadSession(decoderUrl);
const encoderSession = await encoderSessionPromise;
await incrementProgress();
const initDecoderSession = await initDecoderSessionPromise;
await incrementProgress();
const decoderSession = await decoderSessionPromise;
await incrementProgress();
return new T5ForConditionalGeneration(encoderSession, initDecoderSession, decoderSession);
}
/**
* Generate a sequence of tokens.
*
* @param {Array} inputTokenIds
* @param {Object} options Properties:
* `maxLength` for the maximum generated sequence length,
* `topK` for the number of logits to consider when sampling.
* @param {Promise} progressAsyncCallback
* @returns The generated sequence of tokens.
*/
async generate(inputTokenIds, options, progressAsyncCallback) {
const maxLength = options.maxLength || 100;
const topK = options.topK || 0;
const topP = options.topP || 0;
const numBeams = options.numBeams || 0;
// attention_mask=token['attention_mask'], num_beams=2
const startOfDecoderTokenId = 0;
const endOfDecoderTokenId = 1;
let encoderOutputs = null;
let pastKeyValues = null;
let outputTokenIds = [startOfDecoderTokenId];
let numOutputTokens = 1;
let shouldContinue = true;
const maxOutputTokens = numOutputTokens + maxLength;
async function progress() {
if (progressAsyncCallback) {
shouldContinue = await progressAsyncCallback(outputTokenIds, inputTokenIds);
}
}
let sampler = x => this.sampleLogitsGreedily(x);
if (topK > 0) {
sampler = x => this.sampleLogitsTopK(x, topK);
}
while (shouldContinue && numOutputTokens < maxOutputTokens) {
let output = await this.forward(inputTokenIds, outputTokenIds, encoderOutputs, pastKeyValues);
pastKeyValues = output.pastKeyValues;
encoderOutputs = output.encoderOutputs;
let newTokenId = sampler(output.logits);
outputTokenIds.push(newTokenId);
numOutputTokens++;
await progress(outputTokenIds);
if (newTokenId === endOfDecoderTokenId) {
break;
}
}
return outputTokenIds;
}
sampleLogitsGreedily(logits) {
let shape = logits.dims;
let [batchSize, seqLength, vocabSize] = shape;
let n = batchSize * seqLength * vocabSize;
let startIndex = n - vocabSize;
let argmaxi = 0;
let argmax = logits.data[startIndex + argmaxi];
for (let i = 1; i < vocabSize; i++) {
let l = logits.data[startIndex + i];
if (l > argmax) {
argmaxi = i;
argmax = l;
}
}
return argmaxi;
}
sampleLogitsTopK(logits, k) {
let shape = logits.dims;
let [batchSize, seqLength, vocabSize] = shape;
let n = batchSize * seqLength * vocabSize;
let startIndex = n - vocabSize;
let logs = logits.data.slice(startIndex);
k = Math.min(k, vocabSize);
let logitAndId = Array.from(logs).map((x, i) => [x, i])
.sort((a, b) => b[0] - a[0]);
const sMin = Math.exp(-100.0);
let sumS = 0.0;
for (let i = 0; i < logitAndId.length; i++) {
const s = i < k ? Math.exp(logitAndId[i][0]) : sMin;
sumS += s;
logitAndId[i][0] = s;
}
let r = Math.random() * sumS;
for (let i = 0; i < logitAndId.length; i++) {
r -= logitAndId[i][0];
if (r <= 0) {
return logitAndId[i][1];
}
}
return logitAndId[0][1];
}
}
class T5ForConditionalGeneration extends AutoModelForSeq2SeqLM {
constructor(encoderSession, initDecoderSession, decoderSession) {
super(encoderSession, initDecoderSession, decoderSession);
}
async forward(inputIds, decoderInputIds, encoderOutputs, pastKeyValues) {
const inputIdsTensor = new ort.Tensor("int64", new BigInt64Array(inputIds.map(x => BigInt(x))), [1, inputIds.length]);
const encoderAttentionMaskTensor = new ort.Tensor("int64", new BigInt64Array(inputIds.length).fill(1n), [1, inputIds.length]);
if (encoderOutputs === null) {
// console.log("Encoding...");
const encoderFeeds = {
"input_ids": inputIdsTensor,
"attention_mask": encoderAttentionMaskTensor,
}
const encoderResults = await this.encoderSession.run(encoderFeeds);
const encoderHiddenStates = encoderResults.hidden_states;
encoderOutputs = encoderHiddenStates;
// console.log("Encoding done.", encoderOutputs);
}
const decoderInputIdsTensor = new ort.Tensor("int64", new BigInt64Array(decoderInputIds.map(x => BigInt(x))), [1, decoderInputIds.length]);
// const decoderAttentionMaskTensor = new ort.Tensor("int64", new BigInt64Array(decoderInputIds.length).fill(1n), [1, decoderInputIds.length]);
const decoderFeeds = {
"input_ids": decoderInputIdsTensor,
"encoder_attention_mask": encoderAttentionMaskTensor,
"encoder_hidden_states": encoderOutputs,
};
let logits = null;
if (pastKeyValues === null) {
// console.log("Init Decoding...");
const initDecoderResults = await this.initDecoderSession.run(decoderFeeds);
logits = initDecoderResults.logits;
pastKeyValues = this.getPastKeyValues(this.initDecoderSession.outputNames.slice(1), initDecoderResults);
// console.log("Init Decoding done.", logits, pastKeyValues);
}
else {
// console.log("Decoding...");
for (const [k, v] of pastKeyValues) {
decoderFeeds[k] = v;
}
const decoderResults = await this.decoderSession.run(decoderFeeds);
logits = decoderResults.logits;
pastKeyValues = this.getPastKeyValues(this.decoderSession.outputNames.slice(1), decoderResults);
// console.log("Decoding done.", logits, pastKeyValues);
}
return new Seq2SeqLMOutput(logits, pastKeyValues, encoderOutputs);
}
getPastKeyValues(pkvNames, decoderResults) {
const pkvs = [];
for (const i in pkvNames) {
const k = pkvNames[i];
const v = decoderResults[k];
pkvs.push([`pkv_${i}`, v]);
}
return pkvs;
}
}
class Seq2SeqLMOutput {
constructor(logits, pastKeyValues, encoderOutputs) {
this.logits = logits;
this.pastKeyValues = pastKeyValues;
this.encoderOutputs = encoderOutputs;
}
}