-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathllama2c-worker.js
97 lines (88 loc) · 2.8 KB
/
llama2c-worker.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
import { WASI } from "/vendor/wasi.js";
var context;
var result;
// Initialize WASM memory.
var wasmMemory = new WebAssembly.Memory({initial:32, maximum: 10000});
var fileRequest = await fetch(self.location.origin + '/' + 'llama2.c/tokenizer.bin');
var fileContent = await fileRequest.arrayBuffer();
var modelURL = 'https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin';
if (isLocalhost()) { modelURL = self.location.origin + '/' + 'models/stories15M.bin'; }
var modelFileRequest = await fetch(modelURL);
var contentLength = modelFileRequest.headers.get('Content-Length');
var responseSize = 0;
let chunksAll = new Uint8Array(contentLength); // (4.1)
for await (var chunk of streamAsyncIterable(modelFileRequest.body)) {
chunksAll.set(chunk, responseSize); // (4.2)
responseSize += chunk.length;
self.postMessage({
eventType: "MODELDOWNLOADPROGRESS",
eventData: responseSize / contentLength
});
}
async function* streamAsyncIterable(stream) {
const reader = stream.getReader()
try {
while (true) {
const { done, value } = await reader.read()
if (done) return
yield value
}
} finally {
reader.releaseLock()
}
}
var output = '';
context = new WASI({
args: ['run', 'model.bin', '-i', 'Once upon a time'],
stdout: function (out) {
output += out;
self.postMessage({
eventType: "STDOUT",
eventData: out
});
},
stderr: function (err) {
self.postMessage({
eventType: "STDERR",
eventData: err
});
console.error('stderr', err);
},
stdin: () => prompt('stdin:'),
fs: {
'/model.bin': {
path: modelFileRequest.name,
timestamps: {
change: new Date(modelFileRequest.headers.get('Last-Modified')),
access: new Date(modelFileRequest.headers.get('Last-Modified')),
modification: new Date(modelFileRequest.headers.get('Last-Modified')),
},
mode: 'binary',
content: new Uint8Array(chunksAll),
},
'/tokenizer.bin': {
path: 'tokenizer.bin',
timestamps: {
change: new Date(fileRequest.headers.get('Last-Modified')),
access: new Date(fileRequest.headers.get('Last-Modified')),
modification: new Date(fileRequest.headers.get('Last-Modified')),
},
mode: 'binary',
content: new Uint8Array(fileContent),
}
}
});
var memory = new WebAssembly.Memory({ initial: 32, maximum: 10000 });
var wasm = await WebAssembly.instantiateStreaming(
fetch('llama2c.wasm'),
{
...context.getImportObject(),
env: {memory: memory}
}
);
result = context.start(wasm, {memory: memory});
function isLocalhost() {
var url = self.location.origin;
return url.indexOf('127.0.0.1') !== -1 || url.indexOf('localhost') !== -1;
}
//result = await WASIJS.start(fetch('llama2c.wasm'), context);