-
Notifications
You must be signed in to change notification settings - Fork 886
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce builtin WebWorker support (#118)
This PR introduces webworker support to WebLLM package.
- Loading branch information
Showing
18 changed files
with
527 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Serve the chat workload through web worker | ||
import { ChatWorkerHandler, ChatModule } from "@mlc-ai/web-llm"; | ||
|
||
const chat = new ChatModule(); | ||
const handler = new ChatWorkerHandler(chat); | ||
self.onmessage = (msg: MessageEvent) => { | ||
handler.onmessage(msg); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# WebLLM Get Started with WebWorker | ||
|
||
This folder provides a minimum demo to show WebLLM API using | ||
[WebWorker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers). | ||
The main benefit of web worker is that all ML workloads runs on a separate thread as a result | ||
will less likely block the UI. | ||
|
||
To try it out, you can do the following steps | ||
|
||
- Modify [package.json](package.json) to make sure either | ||
- `@mlc-ai/web-llm` points to a valid npm version e.g. | ||
```js | ||
"dependencies": { | ||
"@mlc-ai/web-llm": "^0.2.0" | ||
} | ||
``` | ||
Try this option if you would like to use WebLLM without building it yourself. | ||
- Or keep the dependencies as `"file:../.."`, and follow the build from source | ||
instruction in the project to build webllm locally. This option is more useful | ||
for developers who would like to hack WebLLM core package. | ||
- Run the following command | ||
```bash | ||
npm install | ||
npm start | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "get-started-web-worker", | ||
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "parcel src/get_started.html --port 8888", | ||
"build": "parcel build src/get_started.html --dist-dir lib" | ||
}, | ||
"devDependencies": { | ||
"parcel": "^2.8.3", | ||
"typescript": "^4.9.5", | ||
"tslib": "^2.3.1" | ||
}, | ||
"dependencies": { | ||
"@mlc-ai/web-llm": "file:../.." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<script> | ||
webLLMGlobal = {} | ||
</script> | ||
<body> | ||
<h2>WebLLM Test Page</h2> | ||
Open console to see output | ||
</br> | ||
</br> | ||
<label id="init-label"> </label> | ||
|
||
<h3>Prompt</h3> | ||
<label id="prompt-label"> </label> | ||
|
||
<h3>Response</h3> | ||
<label id="generate-label"> </label> | ||
</br> | ||
<label id="stats-label"> </label> | ||
|
||
<script type="module" src="./main.ts"></script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as webllm from "@mlc-ai/web-llm"; | ||
|
||
function setLabel(id: string, text: string) { | ||
const label = document.getElementById(id); | ||
if (label == null) { | ||
throw Error("Cannot find label " + id); | ||
} | ||
label.innerText = text; | ||
} | ||
|
||
async function main() { | ||
// Use a chat worker client instead of ChatModule here | ||
const chat = new webllm.ChatWorkerClient(new Worker( | ||
new URL('./worker.ts', import.meta.url), | ||
{type: 'module'} | ||
)); | ||
|
||
chat.setInitProgressCallback((report: webllm.InitProgressReport) => { | ||
setLabel("init-label", report.text); | ||
}); | ||
|
||
await chat.reload("vicuna-v1-7b-q4f32_0"); | ||
|
||
const generateProgressCallback = (_step: number, message: string) => { | ||
setLabel("generate-label", message); | ||
}; | ||
|
||
const prompt0 = "What is the capital of Canada?"; | ||
setLabel("prompt-label", prompt0); | ||
const reply0 = await chat.generate(prompt0, generateProgressCallback); | ||
console.log(reply0); | ||
|
||
const prompt1 = "Can you write a poem about it?"; | ||
setLabel("prompt-label", prompt1); | ||
const reply1 = await chat.generate(prompt1, generateProgressCallback); | ||
console.log(reply1); | ||
|
||
console.log(await chat.runtimeStatsText()); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ChatWorkerHandler, ChatModule } from "@mlc-ai/web-llm"; | ||
|
||
// Hookup a chat module to a worker handler | ||
const chat = new ChatModule(); | ||
const handler = new ChatWorkerHandler(chat); | ||
self.onmessage = (msg: MessageEvent) => { | ||
handler.onmessage(msg); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.