-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtransformersEngine.ts
More file actions
270 lines (253 loc) · 8.36 KB
/
transformersEngine.ts
File metadata and controls
270 lines (253 loc) · 8.36 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2025 Flower Labs GmbH. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// =============================================================================
import type { ProgressInfo, TextGenerationConfig } from '@huggingface/transformers';
import {
InterruptableStoppingCriteria,
pipeline,
StoppingCriteriaList,
Tensor,
TextGenerationPipeline,
TextStreamer,
} from '@huggingface/transformers';
import { getAvailableRAM } from '../env';
import {
ChatResponseResult,
FailureCode,
Message,
Progress,
ResponseFormat,
Result,
} from '../typing';
import { getEngineModelConfig } from './common/model';
import { BaseEngine } from './engine';
const stoppingCriteria = new InterruptableStoppingCriteria();
const choice = 0;
const textGenerationPipeline = pipeline as (
task: 'text-generation',
model: string,
options?: { dtype?: DTYPE }
) => Promise<TextGenerationPipeline>;
export class TransformersEngine extends BaseEngine {
private generationPipelines: Record<string, TextGenerationPipeline> = {};
async chat(
messages: Message[],
model: string,
temperature?: number,
topP?: number,
maxCompletionTokens?: number,
_responseFormat?: ResponseFormat,
stream?: boolean,
onStreamEvent?: (event: { chunk: string }) => void
): Promise<ChatResponseResult> {
const modelConfigRes = await getEngineModelConfig(model, 'onnx');
if (!modelConfigRes.ok) {
return {
ok: false,
failure: {
code: FailureCode.UnsupportedModelError,
description: `The model ${model} is not supported on the Transformers.js engine.`,
},
};
}
try {
if (!(model in this.generationPipelines)) {
const modelElems = modelConfigRes.value.name.split('|');
const modelId = modelElems[0];
const pipelineOptions: { dtype?: DTYPE } = {};
if (modelElems.length > 1) {
pipelineOptions.dtype = modelElems[1] as DTYPE;
}
this.generationPipelines.model = await textGenerationPipeline(
'text-generation',
modelId,
pipelineOptions
);
}
const tokenizer = this.generationPipelines.model.tokenizer;
const modelInstance = this.generationPipelines.model.model;
const inputs = tokenizer.apply_chat_template(messages, {
add_generation_prompt: true,
return_dict: true,
}) as {
input_ids: Tensor | number[] | number[][];
attention_mask: Tensor | number[] | number[][];
token_type_ids?: Tensor | number[] | number[][] | undefined;
};
let streamer = undefined;
if (stream && onStreamEvent) {
streamer = new TextStreamer(tokenizer, {
skip_prompt: true,
callback_function: (output: string) => {
let formattedOutput = output;
for (const str of tokenizer.special_tokens as string[]) {
formattedOutput = formattedOutput.replace(str, '');
}
onStreamEvent({ chunk: formattedOutput });
},
});
}
stoppingCriteria.reset();
const stoppingCriteriaList = new StoppingCriteriaList();
stoppingCriteriaList.push(stoppingCriteria);
const { past_key_values: _, sequences } = (await modelInstance.generate({
...inputs,
generation_config: {
do_sample: false,
max_new_tokens: maxCompletionTokens ?? 1024,
temperature: temperature ?? 1,
return_dict_in_generate: true,
top_p: topP ?? 1,
} as TextGenerationConfig,
stopping_criteria: stoppingCriteriaList,
...(streamer && { streamer }),
})) as { past_key_values: object; sequences: Tensor };
const decoded = tokenizer.batch_decode(sequences, {
skip_special_tokens: true,
});
let promptLengths: number[] | undefined;
const inputIds = inputs.input_ids as Tensor;
const inputDim = inputIds.dims[inputIds.dims.length - 1];
if (typeof inputDim === 'number' && inputDim > 0) {
promptLengths = tokenizer
.batch_decode(inputIds, { skip_special_tokens: true })
.map((x) => x.length);
}
if (promptLengths) {
for (let i = 0; i < decoded.length; ++i) {
decoded[i] = decoded[i].slice(promptLengths[i]);
}
}
return {
ok: true,
message: {
role: 'assistant',
content: decoded[choice],
},
};
} catch (error) {
return {
ok: false,
failure: {
code: FailureCode.LocalEngineChatError,
description: `Transformers.js engine failed with: ${String(error)}`,
},
};
}
}
async fetchModel(model: string, callback: (progress: Progress) => void): Promise<Result<void>> {
const modelConfigRes = await getEngineModelConfig(model, 'onnx');
if (!modelConfigRes.ok) {
return {
ok: false,
failure: {
code: FailureCode.UnsupportedModelError,
description: `The model ${model} is not supported on the Transformers.js engine.`,
},
};
}
try {
if (!(model in this.generationPipelines)) {
const modelElems = modelConfigRes.value.name.split('|');
const modelId = modelElems[0];
const pipelineOptions: {
dtype?: DTYPE;
progress_callback: (progressInfo: ProgressInfo) => void;
} = {
progress_callback: (progressInfo: ProgressInfo) => {
let percentage = 0;
let total = 0;
let loaded = 0;
let description = progressInfo.status as string;
if (progressInfo.status == 'progress') {
percentage = progressInfo.progress;
total = progressInfo.total;
loaded = progressInfo.loaded;
description = progressInfo.file;
} else if (progressInfo.status === 'done') {
percentage = 100;
description = progressInfo.status;
}
callback({
totalBytes: total,
loadedBytes: loaded,
percentage,
description,
});
},
};
if (modelElems.length > 1) {
pipelineOptions.dtype = modelElems[1] as DTYPE;
}
this.generationPipelines.model = await textGenerationPipeline(
'text-generation',
modelId,
pipelineOptions
);
}
return { ok: true, value: undefined };
} catch (error) {
return {
ok: false,
failure: { code: FailureCode.LocalEngineFetchError, description: String(error) },
};
}
}
async isSupported(model: string): Promise<Result<void>> {
const modelConfigRes = await getEngineModelConfig(model, 'onnx');
if (modelConfigRes.ok) {
if (modelConfigRes.value.vram) {
const availableRamRes = await getAvailableRAM();
if (availableRamRes.ok) {
if (modelConfigRes.value.vram < availableRamRes.value) {
return {
ok: true,
value: undefined,
};
} else {
return {
ok: false,
failure: {
code: FailureCode.InsufficientRAMError,
description: `Model ${model} requires at least ${String(modelConfigRes.value.vram)} MB to be loaded, but on ${String(availableRamRes.value)} MB are currently available.`,
},
};
}
}
}
return {
ok: true,
value: undefined,
};
}
return {
ok: false,
failure: {
code: FailureCode.UnsupportedModelError,
description: `Model ${model} is unavailable for local inference.`,
},
};
}
}
type DTYPE =
| 'auto'
| 'fp32'
| 'fp16'
| 'q8'
| 'int8'
| 'uint8'
| 'q4'
| 'bnb4'
| 'q4f16'
| Record<string, 'auto' | 'fp32' | 'fp16' | 'q8' | 'int8' | 'uint8' | 'q4' | 'bnb4' | 'q4f16'>;