brsh 3.3.1 → 3.5.0
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/CHANGELOG.md +14 -0
- package/Readme.md +2 -2
- package/dist/wbt.es.js +384 -242
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +3 -3
- package/dist/wbt.min.js.map +1 -1
- package/lib/wbt/packages/wllama.js +433 -125
- package/package.json +1 -1
- package/test/wbt.test.js +38 -3
|
@@ -4,7 +4,29 @@ const Command = require('../../Command');
|
|
|
4
4
|
const ReplCommand = require('../../ReplCommand');
|
|
5
5
|
const { readBytesFromFs, writeBytesToFs, loadModule } = require('../utils');
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Pre-configured friendly aliases for lightweight instruct models compatible with Wllama in-browser runtime.
|
|
9
|
+
*/
|
|
10
|
+
const MODEL_ALIASES = {
|
|
11
|
+
smol: 'https://huggingface.co/bartowski/SmolLM2-135M-Instruct-GGUF/resolve/main/SmolLM2-135M-Instruct-Q4_K_M.gguf',
|
|
12
|
+
smollm: 'https://huggingface.co/bartowski/SmolLM2-135M-Instruct-GGUF/resolve/main/SmolLM2-135M-Instruct-Q4_K_M.gguf',
|
|
13
|
+
smollm2: 'https://huggingface.co/bartowski/SmolLM2-135M-Instruct-GGUF/resolve/main/SmolLM2-135M-Instruct-Q4_K_M.gguf',
|
|
14
|
+
'smol-135m': 'https://huggingface.co/bartowski/SmolLM2-135M-Instruct-GGUF/resolve/main/SmolLM2-135M-Instruct-Q4_K_M.gguf',
|
|
15
|
+
'smol-360m': 'https://huggingface.co/bartowski/SmolLM2-360M-Instruct-GGUF/resolve/main/SmolLM2-360M-Instruct-Q4_K_M.gguf',
|
|
16
|
+
'smollm-360m': 'https://huggingface.co/bartowski/SmolLM2-360M-Instruct-GGUF/resolve/main/SmolLM2-360M-Instruct-Q4_K_M.gguf',
|
|
17
|
+
'smollm2-360m': 'https://huggingface.co/bartowski/SmolLM2-360M-Instruct-GGUF/resolve/main/SmolLM2-360M-Instruct-Q4_K_M.gguf',
|
|
18
|
+
'smol-1.7b': 'https://huggingface.co/bartowski/SmolLM2-1.7B-Instruct-GGUF/resolve/main/SmolLM2-1.7B-Instruct-Q4_K_M.gguf',
|
|
19
|
+
qwen: 'https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/main/qwen2.5-0.5b-instruct-q4_k_m.gguf',
|
|
20
|
+
'qwen2.5': 'https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/main/qwen2.5-0.5b-instruct-q4_k_m.gguf',
|
|
21
|
+
'qwen-0.5b': 'https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/main/qwen2.5-0.5b-instruct-q4_k_m.gguf',
|
|
22
|
+
'qwen2.5-0.5b': 'https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/main/qwen2.5-0.5b-instruct-q4_k_m.gguf',
|
|
23
|
+
'qwen-1.5b': 'https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct-GGUF/resolve/main/qwen2.5-1.5b-instruct-q4_k_m.gguf',
|
|
24
|
+
'qwen2.5-1.5b': 'https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct-GGUF/resolve/main/qwen2.5-1.5b-instruct-q4_k_m.gguf',
|
|
25
|
+
stories: 'https://huggingface.co/ggml-org/models/resolve/main/tinyllamas/stories260K.gguf',
|
|
26
|
+
stories15m: 'https://huggingface.co/ggml-org/models/resolve/main/tinyllamas/stories15M.gguf'
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const DEFAULT_MODEL_URL = MODEL_ALIASES.smol;
|
|
8
30
|
const DEFAULT_WASM_URL = 'https://cdn.jsdelivr.net/npm/@wllama/wllama@3.8.1/src/wasm/wllama.wasm';
|
|
9
31
|
const DEFAULT_PERSIST_DIR = '/var/cache/wllama';
|
|
10
32
|
|
|
@@ -31,15 +53,18 @@ function getDefaultPersistPath(modelSpec) {
|
|
|
31
53
|
return `${DEFAULT_PERSIST_DIR}/${fileName}`;
|
|
32
54
|
}
|
|
33
55
|
|
|
56
|
+
function formatSize(bytes) {
|
|
57
|
+
if (!bytes || bytes <= 0) return '0 B';
|
|
58
|
+
if (bytes >= 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`;
|
|
59
|
+
if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
60
|
+
if (bytes >= 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
61
|
+
return `${bytes} B`;
|
|
62
|
+
}
|
|
63
|
+
|
|
34
64
|
/**
|
|
35
65
|
* Formats an in-place carriage-return (\r) download progress bar line.
|
|
36
66
|
*/
|
|
37
67
|
function formatDownloadProgress(label, loaded, total, width = 20) {
|
|
38
|
-
const formatSize = (bytes) => {
|
|
39
|
-
if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
40
|
-
if (bytes >= 1024) return `${(bytes / (1024 * 1024 * 1024 >= 1 ? 1024 * 1024 : 1024)).toFixed(1)} KB`;
|
|
41
|
-
return `${bytes} B`;
|
|
42
|
-
};
|
|
43
68
|
const shortLabel = (label || 'model.gguf').split('/').pop().split('?')[0] || label;
|
|
44
69
|
if (total > 0) {
|
|
45
70
|
const ratio = Math.max(0, Math.min(1, loaded / total));
|
|
@@ -51,6 +76,123 @@ function formatDownloadProgress(label, loaded, total, width = 20) {
|
|
|
51
76
|
return `\rPulling ${shortLabel}: ${formatSize(loaded)} downloaded...`;
|
|
52
77
|
}
|
|
53
78
|
|
|
79
|
+
/**
|
|
80
|
+
* Animated intermediate & determinate progress tracker for model weights loading,
|
|
81
|
+
* compilation, and inference execution.
|
|
82
|
+
*/
|
|
83
|
+
class ProgressTracker {
|
|
84
|
+
constructor({ onRender = null, width = 20, intervalMs = 80, enabled = true } = {}) {
|
|
85
|
+
this.onRender = onRender;
|
|
86
|
+
this.width = width;
|
|
87
|
+
this.intervalMs = intervalMs;
|
|
88
|
+
this.enabled = enabled;
|
|
89
|
+
this.label = '';
|
|
90
|
+
this.total = 0;
|
|
91
|
+
this.loaded = 0;
|
|
92
|
+
this.hint = '';
|
|
93
|
+
this.startTime = 0;
|
|
94
|
+
this.step = 0;
|
|
95
|
+
this.timer = null;
|
|
96
|
+
this.active = false;
|
|
97
|
+
this.isDeterminate = false;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
start(label, { total = 0, loaded = 0, hint = '', determinate = null } = {}) {
|
|
101
|
+
if (!this.enabled) return;
|
|
102
|
+
this.stopTimer();
|
|
103
|
+
this.label = label || 'Loading';
|
|
104
|
+
this.total = total || 0;
|
|
105
|
+
this.loaded = loaded || 0;
|
|
106
|
+
this.hint = hint || '';
|
|
107
|
+
this.startTime = Date.now();
|
|
108
|
+
this.step = 0;
|
|
109
|
+
this.active = true;
|
|
110
|
+
this.isDeterminate = determinate !== null ? determinate : (this.total > 0 && this.loaded > 0);
|
|
111
|
+
this.render();
|
|
112
|
+
this.timer = setInterval(() => {
|
|
113
|
+
this.step++;
|
|
114
|
+
this.render();
|
|
115
|
+
}, this.intervalMs);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
update({ loaded, total, hint } = {}) {
|
|
119
|
+
if (!this.enabled || !this.active) return;
|
|
120
|
+
if (typeof total === 'number' && total > 0) {
|
|
121
|
+
this.total = total;
|
|
122
|
+
this.isDeterminate = true;
|
|
123
|
+
}
|
|
124
|
+
if (typeof loaded === 'number' && loaded >= 0) {
|
|
125
|
+
this.loaded = loaded;
|
|
126
|
+
if (this.total > 0) {
|
|
127
|
+
this.isDeterminate = true;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (typeof hint === 'string') {
|
|
131
|
+
this.hint = hint;
|
|
132
|
+
}
|
|
133
|
+
this.render();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
formatLine() {
|
|
137
|
+
const elapsedSec = ((Date.now() - this.startTime) / 1000).toFixed(1) + 's';
|
|
138
|
+
if (this.isDeterminate && this.total > 0) {
|
|
139
|
+
const ratio = Math.max(0, Math.min(1, this.loaded / this.total));
|
|
140
|
+
const pct = Math.round(ratio * 100);
|
|
141
|
+
const filled = Math.round(ratio * this.width);
|
|
142
|
+
const bar = '█'.repeat(filled) + '░'.repeat(Math.max(0, this.width - filled));
|
|
143
|
+
const sizeStr = `${formatSize(this.loaded)} / ${formatSize(this.total)}`;
|
|
144
|
+
const hintStr = this.hint ? ` (${this.hint})` : '';
|
|
145
|
+
return `${this.label}: [${bar}] ${String(pct).padStart(3)}% (${sizeStr}, ${elapsedSec})${hintStr}`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Bouncing intermediate bar animation
|
|
149
|
+
const blockWidth = 5;
|
|
150
|
+
const maxPos = Math.max(1, this.width - blockWidth);
|
|
151
|
+
const cycle = maxPos * 2;
|
|
152
|
+
const posInCycle = this.step % cycle;
|
|
153
|
+
const offset = posInCycle < maxPos ? posInCycle : cycle - posInCycle;
|
|
154
|
+
const left = '░'.repeat(offset);
|
|
155
|
+
const middle = '█'.repeat(blockWidth);
|
|
156
|
+
const right = '░'.repeat(Math.max(0, this.width - offset - blockWidth));
|
|
157
|
+
const bar = `[${left}${middle}${right}]`;
|
|
158
|
+
const hintStr = this.hint ? ` (${this.hint})` : '';
|
|
159
|
+
return `${this.label}: ${bar} ${elapsedSec}${hintStr}`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
render() {
|
|
163
|
+
if (!this.enabled || !this.active || typeof this.onRender !== 'function') return;
|
|
164
|
+
this.onRender(this.formatLine());
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
stopTimer() {
|
|
168
|
+
if (this.timer !== null) {
|
|
169
|
+
clearInterval(this.timer);
|
|
170
|
+
this.timer = null;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
finish(msg = 'Done') {
|
|
175
|
+
if (!this.enabled || !this.active) return;
|
|
176
|
+
this.stopTimer();
|
|
177
|
+
const elapsedSec = ((Date.now() - this.startTime) / 1000).toFixed(1) + 's';
|
|
178
|
+
const bar = '█'.repeat(this.width);
|
|
179
|
+
const sizeStr = this.total > 0 ? `, ${formatSize(this.total)}` : '';
|
|
180
|
+
const line = `${this.label}: [${bar}] 100% (${elapsedSec}${sizeStr}) — ${msg}`;
|
|
181
|
+
if (typeof this.onRender === 'function') {
|
|
182
|
+
this.onRender(line);
|
|
183
|
+
}
|
|
184
|
+
this.active = false;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
clear() {
|
|
188
|
+
this.stopTimer();
|
|
189
|
+
this.active = false;
|
|
190
|
+
if (this.enabled && typeof this.onRender === 'function') {
|
|
191
|
+
this.onRender('');
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
54
196
|
|
|
55
197
|
/**
|
|
56
198
|
* Resolves a model spec (`hf:owner/repo/file.gguf`, `owner/repo:Q4_K_M`, URL, or filesystem path)
|
|
@@ -77,7 +219,7 @@ function resolveModelSpec(rawSpec, hfRepo = null, hfFile = null, hfQuant = null)
|
|
|
77
219
|
};
|
|
78
220
|
}
|
|
79
221
|
|
|
80
|
-
|
|
222
|
+
let spec = (rawSpec || '').trim();
|
|
81
223
|
if (!spec) {
|
|
82
224
|
return {
|
|
83
225
|
kind: 'url',
|
|
@@ -86,6 +228,11 @@ function resolveModelSpec(rawSpec, hfRepo = null, hfFile = null, hfQuant = null)
|
|
|
86
228
|
};
|
|
87
229
|
}
|
|
88
230
|
|
|
231
|
+
const lower = spec.toLowerCase();
|
|
232
|
+
if (MODEL_ALIASES[lower]) {
|
|
233
|
+
spec = MODEL_ALIASES[lower];
|
|
234
|
+
}
|
|
235
|
+
|
|
89
236
|
if (/^hf:\/\//i.test(spec) || /^hf:/i.test(spec)) {
|
|
90
237
|
const stripped = spec.replace(/^hf:(\/\/)?/i, '');
|
|
91
238
|
// Support `hf:owner/repo:Q4_K_M`
|
|
@@ -231,6 +378,9 @@ async function getOrInitWllamaInstance(opts = {}) {
|
|
|
231
378
|
loadModel = true,
|
|
232
379
|
persist = false,
|
|
233
380
|
persistPath = null,
|
|
381
|
+
tracker = null,
|
|
382
|
+
shortLabel = 'model.gguf',
|
|
383
|
+
isPullCommand = false,
|
|
234
384
|
onStatus = null
|
|
235
385
|
} = opts;
|
|
236
386
|
|
|
@@ -298,6 +448,9 @@ async function getOrInitWllamaInstance(opts = {}) {
|
|
|
298
448
|
if (typeof onStatus === 'function') {
|
|
299
449
|
onStatus('Loaded model from cache.');
|
|
300
450
|
}
|
|
451
|
+
if (tracker) {
|
|
452
|
+
tracker.finish('Loaded model from cache');
|
|
453
|
+
}
|
|
301
454
|
let persisted = null;
|
|
302
455
|
if ((persist || persistPath) && fs && resolvedPersistPath) {
|
|
303
456
|
if (fs.exists(resolvedPersistPath)) {
|
|
@@ -322,6 +475,20 @@ async function getOrInitWllamaInstance(opts = {}) {
|
|
|
322
475
|
let downloadedBlob = null;
|
|
323
476
|
let loadedFromPersistedFs = null;
|
|
324
477
|
|
|
478
|
+
const makeLoadParams = (hint = 'loading tensors') => {
|
|
479
|
+
return {
|
|
480
|
+
...loadParams,
|
|
481
|
+
progressCallback: ({ loaded, total }) => {
|
|
482
|
+
if (tracker) {
|
|
483
|
+
tracker.update({ loaded, total, hint });
|
|
484
|
+
}
|
|
485
|
+
if (typeof loadParams.progressCallback === 'function') {
|
|
486
|
+
loadParams.progressCallback({ loaded, total });
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
};
|
|
491
|
+
|
|
325
492
|
// If a remote model was previously persisted into the brsh filesystem, load directly from fs
|
|
326
493
|
if (modelSpec.kind !== 'fs' && fs) {
|
|
327
494
|
const candidatePath = (resolvedPersistPath && fs.exists(resolvedPersistPath))
|
|
@@ -333,10 +500,19 @@ async function getOrInitWllamaInstance(opts = {}) {
|
|
|
333
500
|
if (typeof onStatus === 'function') {
|
|
334
501
|
onStatus(`Loading model from ${candidatePath}...`);
|
|
335
502
|
}
|
|
503
|
+
if (tracker) {
|
|
504
|
+
tracker.start(`Loading model (${shortLabel}) weights`, {
|
|
505
|
+
total: cachedBytes.byteLength,
|
|
506
|
+
hint: 'parsing tensors & compiling WebGPU shaders'
|
|
507
|
+
});
|
|
508
|
+
}
|
|
336
509
|
const cachedBlob = new Blob([cachedBytes], { type: 'application/octet-stream' });
|
|
337
510
|
cachedBlob.name = candidatePath.split('/').pop() || 'model.gguf';
|
|
338
|
-
await wllama.loadModel([cachedBlob],
|
|
511
|
+
await wllama.loadModel([cachedBlob], makeLoadParams());
|
|
339
512
|
loadedFromPersistedFs = { path: candidatePath, bytes: cachedBytes };
|
|
513
|
+
if (tracker) {
|
|
514
|
+
tracker.finish('Model weights loaded');
|
|
515
|
+
}
|
|
340
516
|
}
|
|
341
517
|
}
|
|
342
518
|
}
|
|
@@ -349,6 +525,9 @@ async function getOrInitWllamaInstance(opts = {}) {
|
|
|
349
525
|
if (typeof onStatus === 'function') {
|
|
350
526
|
onStatus(`Loading model from ${modelSpec.path}...`);
|
|
351
527
|
}
|
|
528
|
+
if (tracker) {
|
|
529
|
+
tracker.start(`Loading model (${shortLabel}) weights`, { hint: 'reading file' });
|
|
530
|
+
}
|
|
352
531
|
const bytes = readBytesFromFs(fs, modelSpec.path);
|
|
353
532
|
if (!bytes) {
|
|
354
533
|
throw new Error(`${modelSpec.path}: No such file or directory`);
|
|
@@ -370,7 +549,17 @@ async function getOrInitWllamaInstance(opts = {}) {
|
|
|
370
549
|
if (typeof onStatus === 'function') {
|
|
371
550
|
onStatus(`Loading model ${modelBlob.name} into memory...`);
|
|
372
551
|
}
|
|
373
|
-
|
|
552
|
+
if (tracker) {
|
|
553
|
+
tracker.start(`Loading model (${shortLabel}) weights`, {
|
|
554
|
+
total: bytes.byteLength,
|
|
555
|
+
loaded: bytes.byteLength,
|
|
556
|
+
hint: 'parsing tensors & compiling WebGPU shaders'
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
await wllama.loadModel(blobs, makeLoadParams());
|
|
560
|
+
if (tracker) {
|
|
561
|
+
tracker.finish('Model weights loaded');
|
|
562
|
+
}
|
|
374
563
|
if (resolvedPersistPath && resolvedPersistPath !== modelSpec.path) {
|
|
375
564
|
writeBytesToFs(fs, resolvedPersistPath, bytes, 'application/octet-stream');
|
|
376
565
|
}
|
|
@@ -378,25 +567,46 @@ async function getOrInitWllamaInstance(opts = {}) {
|
|
|
378
567
|
if (typeof onStatus === 'function') {
|
|
379
568
|
onStatus(`Pulling model ${modelSpec.repo} (${modelSpec.quant})...`);
|
|
380
569
|
}
|
|
570
|
+
if (tracker) {
|
|
571
|
+
tracker.start(`Pulling ${shortLabel}`, { hint: 'connecting to Hugging Face' });
|
|
572
|
+
}
|
|
381
573
|
try {
|
|
382
574
|
await wllama.loadModelFromHF(
|
|
383
575
|
{
|
|
384
576
|
repo: modelSpec.repo,
|
|
385
577
|
quant: modelSpec.quant
|
|
386
578
|
},
|
|
387
|
-
|
|
579
|
+
makeLoadParams('downloading')
|
|
388
580
|
);
|
|
581
|
+
if (tracker) {
|
|
582
|
+
tracker.finish('Model weights loaded');
|
|
583
|
+
}
|
|
389
584
|
} catch (err) {
|
|
390
585
|
if (typeof mod.getHFModelSource === 'function') {
|
|
391
586
|
const src = await mod.getHFModelSource({ repo: modelSpec.repo, quant: modelSpec.quant });
|
|
392
587
|
if (typeof onStatus === 'function') {
|
|
393
588
|
onStatus(`Pulling model ${modelSpec.repo} (${modelSpec.quant})...`);
|
|
394
589
|
}
|
|
395
|
-
|
|
590
|
+
if (tracker) {
|
|
591
|
+
tracker.start(`Pulling ${shortLabel}`, { hint: 'downloading' });
|
|
592
|
+
}
|
|
593
|
+
downloadedBlob = await fetchUrlAsGgufBlob(src.url || src, (p) => {
|
|
594
|
+
if (tracker) tracker.update({ loaded: p.loaded, total: p.total, hint: 'downloading' });
|
|
595
|
+
if (typeof loadParams.progressCallback === 'function') loadParams.progressCallback(p);
|
|
596
|
+
});
|
|
396
597
|
if (typeof onStatus === 'function') {
|
|
397
598
|
onStatus(`Loading model into memory...`);
|
|
398
599
|
}
|
|
399
|
-
|
|
600
|
+
if (tracker) {
|
|
601
|
+
tracker.start(`Loading model (${shortLabel}) weights`, {
|
|
602
|
+
total: downloadedBlob.size,
|
|
603
|
+
hint: 'parsing tensors & compiling WebGPU shaders'
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
await wllama.loadModel([downloadedBlob], makeLoadParams());
|
|
607
|
+
if (tracker) {
|
|
608
|
+
tracker.finish('Model weights loaded');
|
|
609
|
+
}
|
|
400
610
|
} else {
|
|
401
611
|
throw err;
|
|
402
612
|
}
|
|
@@ -405,38 +615,77 @@ async function getOrInitWllamaInstance(opts = {}) {
|
|
|
405
615
|
if (typeof onStatus === 'function') {
|
|
406
616
|
onStatus(`Pulling model ${modelSpec.repo}/${modelSpec.file}...`);
|
|
407
617
|
}
|
|
618
|
+
if (tracker) {
|
|
619
|
+
tracker.start(`Pulling ${shortLabel}`, { hint: 'connecting to Hugging Face' });
|
|
620
|
+
}
|
|
408
621
|
try {
|
|
409
622
|
await wllama.loadModelFromHF(
|
|
410
623
|
{
|
|
411
624
|
repo: modelSpec.repo,
|
|
412
625
|
file: modelSpec.file
|
|
413
626
|
},
|
|
414
|
-
|
|
627
|
+
makeLoadParams('downloading')
|
|
415
628
|
);
|
|
629
|
+
if (tracker) {
|
|
630
|
+
tracker.finish('Model weights loaded');
|
|
631
|
+
}
|
|
416
632
|
} catch {
|
|
417
|
-
|
|
633
|
+
if (tracker) {
|
|
634
|
+
tracker.start(`Pulling ${shortLabel}`, { hint: 'downloading' });
|
|
635
|
+
}
|
|
636
|
+
downloadedBlob = await fetchUrlAsGgufBlob(modelSpec.url, (p) => {
|
|
637
|
+
if (tracker) tracker.update({ loaded: p.loaded, total: p.total, hint: 'downloading' });
|
|
638
|
+
if (typeof loadParams.progressCallback === 'function') loadParams.progressCallback(p);
|
|
639
|
+
});
|
|
418
640
|
if (typeof onStatus === 'function') {
|
|
419
641
|
onStatus(`Loading model into memory...`);
|
|
420
642
|
}
|
|
421
|
-
|
|
643
|
+
if (tracker) {
|
|
644
|
+
tracker.start(`Loading model (${shortLabel}) weights`, {
|
|
645
|
+
total: downloadedBlob.size,
|
|
646
|
+
hint: 'parsing tensors & compiling WebGPU shaders'
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
await wllama.loadModel([downloadedBlob], makeLoadParams());
|
|
650
|
+
if (tracker) {
|
|
651
|
+
tracker.finish('Model weights loaded');
|
|
652
|
+
}
|
|
422
653
|
}
|
|
423
654
|
} else {
|
|
424
655
|
const targetUrl = modelSpec.url || DEFAULT_MODEL_URL;
|
|
425
656
|
if (typeof onStatus === 'function') {
|
|
426
657
|
onStatus(`Pulling model from ${targetUrl.split('/').pop().split('?')[0] || targetUrl}...`);
|
|
427
658
|
}
|
|
659
|
+
if (tracker) {
|
|
660
|
+
tracker.start(`Pulling ${shortLabel}`, { hint: 'downloading' });
|
|
661
|
+
}
|
|
428
662
|
try {
|
|
429
663
|
if (mmprojSpec && /^https?:\/\//i.test(mmprojSpec)) {
|
|
430
|
-
await wllama.loadModelFromUrl({ url: targetUrl, mmprojUrl: mmprojSpec },
|
|
664
|
+
await wllama.loadModelFromUrl({ url: targetUrl, mmprojUrl: mmprojSpec }, makeLoadParams('downloading'));
|
|
431
665
|
} else {
|
|
432
|
-
await wllama.loadModelFromUrl(targetUrl,
|
|
666
|
+
await wllama.loadModelFromUrl(targetUrl, makeLoadParams('downloading'));
|
|
667
|
+
}
|
|
668
|
+
if (tracker) {
|
|
669
|
+
tracker.finish('Model weights loaded');
|
|
433
670
|
}
|
|
434
671
|
} catch {
|
|
435
|
-
downloadedBlob = await fetchUrlAsGgufBlob(targetUrl,
|
|
672
|
+
downloadedBlob = await fetchUrlAsGgufBlob(targetUrl, (p) => {
|
|
673
|
+
if (tracker) tracker.update({ loaded: p.loaded, total: p.total, hint: 'downloading' });
|
|
674
|
+
if (typeof loadParams.progressCallback === 'function') loadParams.progressCallback(p);
|
|
675
|
+
});
|
|
436
676
|
if (typeof onStatus === 'function') {
|
|
437
677
|
onStatus(`Loading model into memory...`);
|
|
438
678
|
}
|
|
439
|
-
|
|
679
|
+
if (tracker) {
|
|
680
|
+
tracker.start(`Loading model (${shortLabel}) weights`, {
|
|
681
|
+
total: downloadedBlob.size,
|
|
682
|
+
hint: 'parsing tensors & compiling WebGPU shaders'
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
await wllama.loadModel([downloadedBlob], makeLoadParams());
|
|
686
|
+
if (tracker) {
|
|
687
|
+
tracker.finish('Model weights loaded');
|
|
688
|
+
}
|
|
440
689
|
}
|
|
441
690
|
}
|
|
442
691
|
|
|
@@ -649,8 +898,8 @@ function createWllamaCommand(pkgMeta = {}, wbtConfig = {}, binName = 'wllama') {
|
|
|
649
898
|
}
|
|
650
899
|
|
|
651
900
|
if (positional.length > 0) {
|
|
652
|
-
// If no `-m` model was provided and first positional token looks like
|
|
653
|
-
if (!model && !hfRepo && (subcommand === 'pull' || /\.gguf(\?.*)?$/i.test(positional[0]) || /^hf:/i.test(positional[0]) || /^https?:\/\//i.test(positional[0]))) {
|
|
901
|
+
// If no `-m` model was provided and first positional token looks like an alias, .gguf path, URL, or hf spec (or subcommand is pull)
|
|
902
|
+
if (!model && !hfRepo && (subcommand === 'pull' || MODEL_ALIASES[positional[0].toLowerCase()] || /\.gguf(\?.*)?$/i.test(positional[0]) || /^hf:/i.test(positional[0]) || /^https?:\/\//i.test(positional[0]))) {
|
|
654
903
|
model = positional.shift();
|
|
655
904
|
}
|
|
656
905
|
if (subcommand === 'pull' && positional.length > 0 && !persistPath) {
|
|
@@ -754,6 +1003,27 @@ function createWllamaCommand(pkgMeta = {}, wbtConfig = {}, binName = 'wllama') {
|
|
|
754
1003
|
}
|
|
755
1004
|
}
|
|
756
1005
|
|
|
1006
|
+
_createTracker(defaultLabel = 'Loading') {
|
|
1007
|
+
const enabled = Boolean(this._parsed.showProgress && !this._parsed.jsonOutput);
|
|
1008
|
+
return new ProgressTracker({
|
|
1009
|
+
enabled,
|
|
1010
|
+
width: 20,
|
|
1011
|
+
intervalMs: 80,
|
|
1012
|
+
onRender: (line) => {
|
|
1013
|
+
if (this.interactive) {
|
|
1014
|
+
this._replStatus = line;
|
|
1015
|
+
this._renderRepl();
|
|
1016
|
+
} else {
|
|
1017
|
+
if (line) {
|
|
1018
|
+
this._renderStatus(line);
|
|
1019
|
+
} else {
|
|
1020
|
+
this._clearStatus();
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
|
|
757
1027
|
_attachProgressCallback() {
|
|
758
1028
|
if (!this._parsed.showProgress || this._parsed.modelSpec.kind === 'fs') return;
|
|
759
1029
|
const label = this._parsed.model || this._parsed.modelSpec.file || this._parsed.modelSpec.repo || this._parsed.modelSpec.url || 'model.gguf';
|
|
@@ -782,6 +1052,10 @@ function createWllamaCommand(pkgMeta = {}, wbtConfig = {}, binName = 'wllama') {
|
|
|
782
1052
|
this._attachProgressCallback();
|
|
783
1053
|
}
|
|
784
1054
|
|
|
1055
|
+
const rawLabel = this._parsed.model || this._parsed.modelSpec.file || this._parsed.modelSpec.repo || this._parsed.modelSpec.url || 'model.gguf';
|
|
1056
|
+
const shortLabel = rawLabel.split('/').pop().split('?')[0] || rawLabel;
|
|
1057
|
+
const tracker = this._createTracker();
|
|
1058
|
+
|
|
785
1059
|
const onStatus = (msg) => {
|
|
786
1060
|
if (this.interactive) {
|
|
787
1061
|
this._replStatus = msg;
|
|
@@ -791,23 +1065,32 @@ function createWllamaCommand(pkgMeta = {}, wbtConfig = {}, binName = 'wllama') {
|
|
|
791
1065
|
}
|
|
792
1066
|
};
|
|
793
1067
|
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
this.
|
|
1068
|
+
try {
|
|
1069
|
+
const { wllama, Wllama, persisted } = await getOrInitWllamaInstance({
|
|
1070
|
+
modelSpec: this._parsed.modelSpec,
|
|
1071
|
+
mmprojSpec: this._parsed.mmproj,
|
|
1072
|
+
loadParams: this._parsed.loadParams,
|
|
1073
|
+
fs: this.fs,
|
|
1074
|
+
pkgMeta,
|
|
1075
|
+
wbtConfig,
|
|
1076
|
+
loadModel,
|
|
1077
|
+
persist: this._parsed.persist,
|
|
1078
|
+
persistPath: this._parsed.persistPath,
|
|
1079
|
+
tracker,
|
|
1080
|
+
shortLabel,
|
|
1081
|
+
isPullCommand: this._parsed.subcommand === 'pull',
|
|
1082
|
+
onStatus
|
|
1083
|
+
});
|
|
1084
|
+
this._wllama = wllama;
|
|
1085
|
+
this._WllamaClass = Wllama;
|
|
1086
|
+
this._persisted = persisted;
|
|
1087
|
+
} finally {
|
|
1088
|
+
if (tracker) {
|
|
1089
|
+
tracker.clear();
|
|
1090
|
+
}
|
|
1091
|
+
if (!this.interactive) {
|
|
1092
|
+
this._clearStatus();
|
|
1093
|
+
}
|
|
811
1094
|
}
|
|
812
1095
|
}
|
|
813
1096
|
|
|
@@ -891,24 +1174,75 @@ function createWllamaCommand(pkgMeta = {}, wbtConfig = {}, binName = 'wllama') {
|
|
|
891
1174
|
if (this._parsed.topK !== undefined) opts.top_k = this._parsed.topK;
|
|
892
1175
|
if (this._parsed.topP !== undefined) opts.top_p = this._parsed.topP;
|
|
893
1176
|
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
this.
|
|
899
|
-
|
|
1177
|
+
const tracker = this._createTracker();
|
|
1178
|
+
tracker.start('Running inference', { hint: 'evaluating prompt & preparing KV cache' });
|
|
1179
|
+
|
|
1180
|
+
try {
|
|
1181
|
+
if (typeof this._wllama.createChatCompletion === 'function') {
|
|
1182
|
+
const res = await this._wllama.createChatCompletion(opts);
|
|
900
1183
|
|
|
901
|
-
|
|
902
|
-
|
|
1184
|
+
if (res && typeof res[Symbol.asyncIterator] === 'function') {
|
|
1185
|
+
let fullText = '';
|
|
1186
|
+
let firstToken = false;
|
|
1187
|
+
for await (const chunk of res) {
|
|
1188
|
+
const delta = chunk?.choices?.[0]?.delta?.content ?? chunk?.choices?.[0]?.text ?? '';
|
|
1189
|
+
if (delta) {
|
|
1190
|
+
if (!firstToken) {
|
|
1191
|
+
firstToken = true;
|
|
1192
|
+
tracker.clear();
|
|
1193
|
+
if (this.interactive) {
|
|
1194
|
+
this._replStatus = 'Streaming tokens...';
|
|
1195
|
+
this._renderRepl();
|
|
1196
|
+
} else {
|
|
1197
|
+
this._clearStatus();
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
fullText += delta;
|
|
1201
|
+
if (typeof onToken === 'function') {
|
|
1202
|
+
onToken(delta);
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
tracker.clear();
|
|
1207
|
+
if (!this.interactive) {
|
|
1208
|
+
this._clearStatus();
|
|
1209
|
+
}
|
|
1210
|
+
return fullText;
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
tracker.clear();
|
|
1214
|
+
if (!this.interactive) this._clearStatus();
|
|
1215
|
+
if (this._parsed.jsonOutput) {
|
|
1216
|
+
return JSON.stringify(res);
|
|
1217
|
+
}
|
|
1218
|
+
const text = extractCompletionText(res);
|
|
1219
|
+
if (typeof onToken === 'function' && text) {
|
|
1220
|
+
onToken(text);
|
|
1221
|
+
}
|
|
1222
|
+
return text;
|
|
1223
|
+
}
|
|
903
1224
|
|
|
1225
|
+
// Fallback if mock/custom instance only implements createCompletion
|
|
1226
|
+
const lastMsg = messages[messages.length - 1]?.content || '';
|
|
1227
|
+
const compOpts = {
|
|
1228
|
+
prompt: lastMsg,
|
|
1229
|
+
max_tokens: this._parsed.maxTokens,
|
|
1230
|
+
temperature: this._parsed.temperature,
|
|
1231
|
+
stream: shouldStream
|
|
1232
|
+
};
|
|
1233
|
+
if (this._parsed.topK !== undefined) compOpts.top_k = this._parsed.topK;
|
|
1234
|
+
if (this._parsed.topP !== undefined) compOpts.top_p = this._parsed.topP;
|
|
1235
|
+
|
|
1236
|
+
const res = await this._wllama.createCompletion(compOpts);
|
|
904
1237
|
if (res && typeof res[Symbol.asyncIterator] === 'function') {
|
|
905
1238
|
let fullText = '';
|
|
906
1239
|
let firstToken = false;
|
|
907
1240
|
for await (const chunk of res) {
|
|
908
|
-
const delta = chunk?.choices?.[0]?.
|
|
1241
|
+
const delta = chunk?.choices?.[0]?.text ?? chunk?.choices?.[0]?.delta?.content ?? '';
|
|
909
1242
|
if (delta) {
|
|
910
1243
|
if (!firstToken) {
|
|
911
1244
|
firstToken = true;
|
|
1245
|
+
tracker.clear();
|
|
912
1246
|
if (this.interactive) {
|
|
913
1247
|
this._replStatus = 'Streaming tokens...';
|
|
914
1248
|
this._renderRepl();
|
|
@@ -922,74 +1256,29 @@ function createWllamaCommand(pkgMeta = {}, wbtConfig = {}, binName = 'wllama') {
|
|
|
922
1256
|
}
|
|
923
1257
|
}
|
|
924
1258
|
}
|
|
1259
|
+
tracker.clear();
|
|
925
1260
|
if (!this.interactive) {
|
|
926
1261
|
this._clearStatus();
|
|
927
1262
|
}
|
|
928
1263
|
return fullText;
|
|
929
1264
|
}
|
|
930
1265
|
|
|
931
|
-
|
|
932
|
-
if (!this.interactive) this._clearStatus();
|
|
933
|
-
return JSON.stringify(res);
|
|
934
|
-
}
|
|
935
|
-
const text = extractCompletionText(res);
|
|
1266
|
+
tracker.clear();
|
|
936
1267
|
if (!this.interactive) {
|
|
937
1268
|
this._clearStatus();
|
|
938
1269
|
}
|
|
1270
|
+
if (this._parsed.jsonOutput) return JSON.stringify(res);
|
|
1271
|
+
const text = extractCompletionText(res);
|
|
939
1272
|
if (typeof onToken === 'function' && text) {
|
|
940
1273
|
onToken(text);
|
|
941
1274
|
}
|
|
942
1275
|
return text;
|
|
943
|
-
}
|
|
944
|
-
|
|
945
|
-
// Fallback if mock/custom instance only implements createCompletion
|
|
946
|
-
const lastMsg = messages[messages.length - 1]?.content || '';
|
|
947
|
-
const compOpts = {
|
|
948
|
-
prompt: lastMsg,
|
|
949
|
-
max_tokens: this._parsed.maxTokens,
|
|
950
|
-
temperature: this._parsed.temperature,
|
|
951
|
-
stream: shouldStream
|
|
952
|
-
};
|
|
953
|
-
if (this._parsed.topK !== undefined) compOpts.top_k = this._parsed.topK;
|
|
954
|
-
if (this._parsed.topP !== undefined) compOpts.top_p = this._parsed.topP;
|
|
955
|
-
|
|
956
|
-
const res = await this._wllama.createCompletion(compOpts);
|
|
957
|
-
if (res && typeof res[Symbol.asyncIterator] === 'function') {
|
|
958
|
-
let fullText = '';
|
|
959
|
-
let firstToken = false;
|
|
960
|
-
for await (const chunk of res) {
|
|
961
|
-
const delta = chunk?.choices?.[0]?.text ?? chunk?.choices?.[0]?.delta?.content ?? '';
|
|
962
|
-
if (delta) {
|
|
963
|
-
if (!firstToken) {
|
|
964
|
-
firstToken = true;
|
|
965
|
-
if (this.interactive) {
|
|
966
|
-
this._replStatus = 'Streaming tokens...';
|
|
967
|
-
this._renderRepl();
|
|
968
|
-
} else {
|
|
969
|
-
this._clearStatus();
|
|
970
|
-
}
|
|
971
|
-
}
|
|
972
|
-
fullText += delta;
|
|
973
|
-
if (typeof onToken === 'function') {
|
|
974
|
-
onToken(delta);
|
|
975
|
-
}
|
|
976
|
-
}
|
|
977
|
-
}
|
|
1276
|
+
} finally {
|
|
1277
|
+
tracker.clear();
|
|
978
1278
|
if (!this.interactive) {
|
|
979
1279
|
this._clearStatus();
|
|
980
1280
|
}
|
|
981
|
-
return fullText;
|
|
982
|
-
}
|
|
983
|
-
|
|
984
|
-
if (!this.interactive) {
|
|
985
|
-
this._clearStatus();
|
|
986
1281
|
}
|
|
987
|
-
if (this._parsed.jsonOutput) return JSON.stringify(res);
|
|
988
|
-
const text = extractCompletionText(res);
|
|
989
|
-
if (typeof onToken === 'function' && text) {
|
|
990
|
-
onToken(text);
|
|
991
|
-
}
|
|
992
|
-
return text;
|
|
993
1282
|
}
|
|
994
1283
|
|
|
995
1284
|
_formatModelInfo() {
|
|
@@ -1117,7 +1406,7 @@ function createWllamaCommand(pkgMeta = {}, wbtConfig = {}, binName = 'wllama') {
|
|
|
1117
1406
|
' wllama bench Run ggml WebGPU/CPU backend ops test',
|
|
1118
1407
|
'',
|
|
1119
1408
|
'Options:',
|
|
1120
|
-
' -m, --model=MODEL Path in brsh
|
|
1409
|
+
' -m, --model=MODEL Path in brsh fs, URL, HF spec, or alias (default: smol [SmolLM2-135M]; also: qwen)',
|
|
1121
1410
|
' --hf=REPO Hugging Face model repository (e.g. Qwen/Qwen2.5-0.5B-Instruct-GGUF)',
|
|
1122
1411
|
' --quant=QUANT Hugging Face GGUF quantization (default: Q4_K_M)',
|
|
1123
1412
|
' --persist[=PATH] Persist pulled remote model into brsh filesystem (default: /var/cache/wllama/)',
|
|
@@ -1177,7 +1466,15 @@ function createWllamaCommand(pkgMeta = {}, wbtConfig = {}, binName = 'wllama') {
|
|
|
1177
1466
|
}
|
|
1178
1467
|
|
|
1179
1468
|
if (this._parsed.embeddings || this._parsed.subcommand === 'embed') {
|
|
1180
|
-
const
|
|
1469
|
+
const tracker = this._createTracker();
|
|
1470
|
+
tracker.start('Computing embeddings', { hint: 'evaluating prompt' });
|
|
1471
|
+
let embRes;
|
|
1472
|
+
try {
|
|
1473
|
+
embRes = await this._wllama.createEmbedding({ input: inputText });
|
|
1474
|
+
} finally {
|
|
1475
|
+
tracker.clear();
|
|
1476
|
+
this._clearStatus();
|
|
1477
|
+
}
|
|
1181
1478
|
if (this._parsed.jsonOutput) {
|
|
1182
1479
|
this.stdOut = JSON.stringify(embRes);
|
|
1183
1480
|
} else {
|
|
@@ -1198,36 +1495,45 @@ function createWllamaCommand(pkgMeta = {}, wbtConfig = {}, binName = 'wllama') {
|
|
|
1198
1495
|
if (this._parsed.topK !== undefined) compOpts.top_k = this._parsed.topK;
|
|
1199
1496
|
if (this._parsed.topP !== undefined) compOpts.top_p = this._parsed.topP;
|
|
1200
1497
|
|
|
1201
|
-
this.
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
let
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1498
|
+
const tracker = this._createTracker();
|
|
1499
|
+
tracker.start('Running inference', { hint: 'evaluating prompt & preparing KV cache' });
|
|
1500
|
+
try {
|
|
1501
|
+
const res = await this._wllama.createCompletion(compOpts);
|
|
1502
|
+
let outText = '';
|
|
1503
|
+
if (res && typeof res[Symbol.asyncIterator] === 'function') {
|
|
1504
|
+
let streamedAny = false;
|
|
1505
|
+
for await (const chunk of res) {
|
|
1506
|
+
const delta = chunk?.choices?.[0]?.text ?? chunk?.choices?.[0]?.delta?.content ?? '';
|
|
1507
|
+
if (delta) {
|
|
1508
|
+
if (!streamedAny) {
|
|
1509
|
+
streamedAny = true;
|
|
1510
|
+
this._streamedStdout = true;
|
|
1511
|
+
tracker.clear();
|
|
1512
|
+
this._clearStatus();
|
|
1513
|
+
}
|
|
1514
|
+
outText += delta;
|
|
1515
|
+
if (this.stdout && typeof this.stdout.write === 'function') {
|
|
1516
|
+
this.stdout.write(delta);
|
|
1517
|
+
}
|
|
1518
|
+
this.emit('token', delta);
|
|
1217
1519
|
}
|
|
1218
|
-
this.emit('token', delta);
|
|
1219
1520
|
}
|
|
1521
|
+
tracker.clear();
|
|
1522
|
+
this._clearStatus();
|
|
1523
|
+
if (streamedAny && outText.length > 0 && !outText.endsWith('\n') && this.stdout && typeof this.stdout.write === 'function') {
|
|
1524
|
+
this.stdout.write('\r\n');
|
|
1525
|
+
}
|
|
1526
|
+
} else {
|
|
1527
|
+
tracker.clear();
|
|
1528
|
+
this._clearStatus();
|
|
1529
|
+
outText = this._parsed.jsonOutput ? JSON.stringify(res) : extractCompletionText(res);
|
|
1220
1530
|
}
|
|
1531
|
+
outText.split(/\r?\n/).forEach(l => { this.stdOut = l; });
|
|
1532
|
+
return;
|
|
1533
|
+
} finally {
|
|
1534
|
+
tracker.clear();
|
|
1221
1535
|
this._clearStatus();
|
|
1222
|
-
if (streamedAny && outText.length > 0 && !outText.endsWith('\n') && this.stdout && typeof this.stdout.write === 'function') {
|
|
1223
|
-
this.stdout.write('\r\n');
|
|
1224
|
-
}
|
|
1225
|
-
} else {
|
|
1226
|
-
this._clearStatus();
|
|
1227
|
-
outText = this._parsed.jsonOutput ? JSON.stringify(res) : extractCompletionText(res);
|
|
1228
1536
|
}
|
|
1229
|
-
outText.split(/\r?\n/).forEach(l => { this.stdOut = l; });
|
|
1230
|
-
return;
|
|
1231
1537
|
}
|
|
1232
1538
|
|
|
1233
1539
|
const messages = [];
|
|
@@ -1284,3 +1590,5 @@ module.exports.getDefaultPersistPath = getDefaultPersistPath;
|
|
|
1284
1590
|
module.exports.DEFAULT_PERSIST_DIR = DEFAULT_PERSIST_DIR;
|
|
1285
1591
|
module.exports.resolveModelSpec = resolveModelSpec;
|
|
1286
1592
|
module.exports.formatDownloadProgress = formatDownloadProgress;
|
|
1593
|
+
module.exports.DEFAULT_MODEL_URL = DEFAULT_MODEL_URL;
|
|
1594
|
+
module.exports.MODEL_ALIASES = MODEL_ALIASES;
|