-
Notifications
You must be signed in to change notification settings - Fork 877
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Engine] Allow manually aborting reload, fix unexpected deviceLostErr…
…or (#525) ### Manually aborting reload This PR updates the engine `reload()` and `unload()` methods to allow users to abort an uncompleted `reload()` by either: - call `unload()` any time before `reload()` completed - call `reload()` again before the previous `reload()` completed ### Note on unload() and unexpected device lost error Previously, we had an issue where a device lost error is reported when we simply switch a model intentionally (i.e. calling `reload()`). This is because `unload()` sets `deviceLostIsError` back to true immediately after calling `this.pipeline.dispose()`, which destroys the WebGPU device internally. However, WebGPU is asynchronous and may not finish after `dispose()` returns. This PR also fixes this issue by making `unload()` wait until the device is actually destroyed by introducing `LLMChatPipeline.sync()`. --------- Co-authored-by: Charlie Ruan <[email protected]>
- Loading branch information
1 parent
7690707
commit ddac6d1
Showing
6 changed files
with
139 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# WebLLM Get Started App | ||
|
||
This folder provides a demo for cancelling model fetching after calling `engine.reload()`. | ||
|
||
```bash | ||
npm install | ||
npm start | ||
``` | ||
|
||
Note if you would like to hack WebLLM core package. | ||
You can change web-llm dependencies as `"file:../.."`, and follow the build from source | ||
instruction in the project to build webllm locally. This option is only recommended | ||
if you would like to hack WebLLM core package. |
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,20 @@ | ||
{ | ||
"name": "get-started", | ||
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "parcel src/get_started.html --port 8887", | ||
"build": "parcel build src/get_started.html --dist-dir lib" | ||
}, | ||
"devDependencies": { | ||
"buffer": "^5.7.1", | ||
"parcel": "^2.8.3", | ||
"process": "^0.11.10", | ||
"tslib": "^2.3.1", | ||
"typescript": "^4.9.5", | ||
"url": "^0.11.3" | ||
}, | ||
"dependencies": { | ||
"@mlc-ai/web-llm": "file:../../lib" | ||
} | ||
} |
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,23 @@ | ||
<!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="./get_started.js"></script> | ||
</body> | ||
</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,32 @@ | ||
import * as webllm from "@mlc-ai/web-llm"; | ||
import { error } from "loglevel"; | ||
|
||
let engine; | ||
|
||
function setLabel(id, text) { | ||
const label = document.getElementById(id); | ||
if (label == null) { | ||
throw Error("Cannot find label " + id); | ||
} | ||
label.innerText = text; | ||
} | ||
|
||
async function main() { | ||
const initProgressCallback = (report) => { | ||
console.log(report.text); | ||
setLabel("init-label", report.text); | ||
}; | ||
// Option 1: If we do not specify appConfig, we use `prebuiltAppConfig` defined in `config.ts` | ||
const selectedModel = "Llama-3.1-8B-Instruct-q4f32_1-MLC"; | ||
engine = new webllm.MLCEngine({ | ||
initProgressCallback, | ||
}); | ||
engine.reload(selectedModel); | ||
} | ||
main(); | ||
setTimeout(() => { | ||
console.log("calling unload"); | ||
engine.unload().catch((err) => { | ||
console.log(err); | ||
}); | ||
}, 5000); |
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