-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
215 lines (182 loc) · 5.2 KB
/
worker.js
File metadata and controls
215 lines (182 loc) · 5.2 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
import {
AutoTokenizer,
AutoModelForCausalLM,
TextStreamer,
InterruptableStoppingCriteria,
} from '@huggingface/transformers';
/**
* Helper function to perform feature detection for WebGPU
*/
async function check() {
try {
const adapter = await navigator.gpu.requestAdapter();
if (!adapter) {
throw new Error('WebGPU is not supported (no adapter found)');
}
} catch (e) {
self.postMessage({
status: 'error',
data: e.toString(),
});
}
}
/**
* This class uses the Singleton pattern to enable lazy-loading of the pipeline
*/
class TextGenerationPipeline {
static model_id = 'onnx-community/Qwen3-4B-ONNX';
static tokenizer = null;
static model = null;
static setModelId(modelId) {
// If model changes, reset the cached instances
if (this.model_id !== modelId) {
this.model_id = modelId;
this.tokenizer = null;
this.model = null;
}
}
static async getInstance(progress_callback = null) {
this.tokenizer ??= AutoTokenizer.from_pretrained(this.model_id, {
progress_callback,
});
this.model ??= AutoModelForCausalLM.from_pretrained(this.model_id, {
dtype: 'q4f16',
device: 'webgpu',
progress_callback,
});
return Promise.all([this.tokenizer, this.model]);
}
}
const stopping_criteria = new InterruptableStoppingCriteria();
let currentSystemPrompt = null;
async function generate(messages) {
// Retrieve the text-generation pipeline.
const [tokenizer, model] = await TextGenerationPipeline.getInstance();
const platformPrompt = `
---
When writing any mathematical expression, equation, or formula, always use LaTeX syntax inside math delimiters:
Inline math: $...$
Display math: $$...$$
Use proper LaTeX commands for symbols, operators, and formatting.
Do not output plain text approximations of math (e.g., "sqrt(x)" or "x^2").
Assume all environments support LaTeX rendering via KaTeX.`;
// Use the system prompt content, with fallback to default only if no system prompt is provided at all
let basePrompt = 'Be a helpful assistant'; // Default fallback
if (currentSystemPrompt && currentSystemPrompt.content) {
basePrompt = currentSystemPrompt.content;
}
const system_prompt = basePrompt + platformPrompt;
messages.unshift({
role: 'system',
content: system_prompt,
});
const inputs = tokenizer.apply_chat_template(messages, {
add_generation_prompt: true,
return_dict: true,
});
const [START_THINKING_TOKEN_ID, END_THINKING_TOKEN_ID] = tokenizer.encode(
'<think></think>',
{ add_special_tokens: false },
);
let state = 'thinking'; // 'thinking' or 'answering'
let startTime;
let numTokens = 0;
let tps;
const token_callback_function = (tokens) => {
startTime ??= performance.now();
if (numTokens++ > 0) {
tps = (numTokens / (performance.now() - startTime)) * 1000;
}
if (parseInt(tokens[0]) === END_THINKING_TOKEN_ID) {
state = 'answering';
}
};
const callback_function = (output) => {
self.postMessage({
status: 'update',
output,
tps,
numTokens,
state,
});
};
const streamer = new TextStreamer(tokenizer, {
skip_prompt: true,
skip_special_tokens: true,
callback_function,
token_callback_function,
});
// Tell the main thread we are starting
self.postMessage({ status: 'start' });
const { past_key_values, sequences } = await model.generate({
...inputs,
// Sampling
do_sample: true,
repetition_penalty: 1.2,
top_k: 20,
top_p: 0.8,
min_p: 0,
temperature: 0.7,
max_new_tokens: 2048,
streamer,
stopping_criteria,
return_dict_in_generate: true,
});
const decoded = tokenizer.batch_decode(sequences, {
skip_special_tokens: true,
});
// Send the output back to the main thread
self.postMessage({
status: 'complete',
output: decoded,
});
}
async function load(modelId = null) {
if (modelId) {
TextGenerationPipeline.setModelId(modelId);
}
self.postMessage({
status: 'loading',
data: `Loading model ${TextGenerationPipeline.model_id}...`,
});
// Load the pipeline and save it for future use.
const [tokenizer, model] = await TextGenerationPipeline.getInstance((x) => {
// We also add a progress callback to the pipeline so that we can
// track model loading.
self.postMessage(x);
});
self.postMessage({
status: 'loading',
data: 'Compiling shaders and warming up model...',
});
// Run model with dummy input to compile shaders
const inputs = tokenizer('a');
await model.generate({ ...inputs, max_new_tokens: 1 });
self.postMessage({ status: 'ready' });
}
// Listen for messages from the main thread
self.addEventListener('message', async (e) => {
const { type, data, systemPrompt } = e.data;
switch (type) {
case 'check':
check();
break;
case 'load':
// Store the system prompt if provided
if (systemPrompt) {
currentSystemPrompt = systemPrompt;
}
load(data);
break;
case 'generate':
stopping_criteria.reset();
generate(data);
break;
case 'interrupt':
stopping_criteria.interrupt();
break;
case 'reset':
stopping_criteria.reset();
break;
}
});