-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add subgroup wasm gating example #797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ksgr5566
wants to merge
1
commit into
mlc-ai:main
Choose a base branch
from
ksgr5566:webgpu-subgroups
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,19 @@ | ||
| # WebLLM Wasm Gating App | ||
|
|
||
| This folder provides a minimum demo to show capability-based routing between | ||
| baseline and subgroup WebGPU WASM builds in a webapp setting. | ||
| To try it out, you can do the following steps under this folder | ||
|
|
||
| ```bash | ||
| npm install | ||
| npm start | ||
| ``` | ||
|
|
||
| Edit `src/wasm_gating.ts` if you would like to point the example at your own | ||
| model path and baseline `model_lib`. The example will switch to | ||
| `-subgroups.wasm` when the adapter reports subgroup support. | ||
|
|
||
| Note if you would like to hack WebLLM core package. | ||
| You can change the WebLLM dependency to `"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 hidden or 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 @@ | ||
| { | ||
| "name": "wasm-gating", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "scripts": { | ||
| "start": "parcel src/wasm_gating.html --port 8888", | ||
| "build": "parcel build src/wasm_gating.html --dist-dir lib" | ||
| }, | ||
| "devDependencies": { | ||
| "buffer": "^5.7.1", | ||
| "crypto-browserify": "^3.12.1", | ||
| "events": "^3.3.0", | ||
| "parcel": "^2.8.3", | ||
| "process": "^0.11.10", | ||
| "stream-browserify": "^3.0.0", | ||
| "string_decoder": "^1.3.0", | ||
| "tslib": "^2.3.1", | ||
| "typescript": "^4.9.5", | ||
| "url": "^0.11.3", | ||
| "vm-browserify": "^1.1.2" | ||
| }, | ||
| "dependencies": { | ||
| "@mlc-ai/web-llm": "^0.2.82" | ||
| } | ||
| } |
This file contains hidden or 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,26 @@ | ||
| <!doctype html> | ||
| <html> | ||
| <script> | ||
| webLLMGlobal = {}; | ||
| </script> | ||
| <body> | ||
| <h2>WebLLM Test Page</h2> | ||
| Open console to see output | ||
| <br /> | ||
| <br /> | ||
| <label id="init-label"> </label> | ||
| <br /> | ||
| <br /> | ||
| <h3>Prompt</h3> | ||
| <label id="prompt-label"> </label> | ||
| <br /> | ||
| <br /> | ||
| <h3>Response</h3> | ||
| <label id="generate-label"> </label> | ||
| <br /> | ||
| <br /> | ||
| <label id="stats-label"> </label> | ||
|
|
||
| <script type="module" src="./wasm_gating.ts"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or 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,111 @@ | ||
| 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() { | ||
| const initProgressCallback = (report: webllm.InitProgressReport) => { | ||
| setLabel("init-label", report.text); | ||
| }; | ||
|
|
||
| const selectedModel = "Llama-3.2-1B-Instruct-q4f16_1-MLC"; | ||
| const adapter = await (navigator as any).gpu?.requestAdapter({ | ||
| powerPreference: "high-performance", | ||
| }); | ||
| if (adapter == null) { | ||
| throw Error("Unable to request a WebGPU adapter."); | ||
| } | ||
| const supportsSubgroups = adapter.features.has("subgroups"); | ||
| // Option 1: If we do not specify appConfig, we use `prebuiltAppConfig` defined in `config.ts` | ||
| const modelRecord = webllm.prebuiltAppConfig.model_list.find( | ||
| (entry) => entry.model_id === selectedModel, | ||
| ); | ||
| const appConfig = | ||
| supportsSubgroups && modelRecord !== undefined | ||
| ? { | ||
| model_list: [ | ||
| { | ||
| ...modelRecord, | ||
| model_lib: modelRecord.model_lib.replace( | ||
| /\.wasm$/, | ||
| "-subgroups.wasm", | ||
| ), | ||
| }, | ||
| ], | ||
| } | ||
| : undefined; | ||
| const engine: webllm.MLCEngineInterface = await webllm.CreateMLCEngine( | ||
| selectedModel, | ||
| { | ||
| appConfig: appConfig, | ||
| initProgressCallback: initProgressCallback, | ||
| logLevel: "INFO", // specify the log level | ||
| }, | ||
| // customize kv cache, use either context_window_size or sliding_window_size (with attention sink) | ||
| { | ||
| context_window_size: 2048, | ||
| // sliding_window_size: 1024, | ||
| // attention_sink_size: 4, | ||
| }, | ||
| ); | ||
|
|
||
| // Option 2: Specify your own model other than the prebuilt ones | ||
| // const appConfig: webllm.AppConfig = { | ||
| // model_list: [ | ||
| // { | ||
| // model: "http://127.0.0.1:8000/models/Llama-3.2-1B-Instruct-q4f16_1-MLC/", | ||
| // model_id: "Llama-3.2-1B-Instruct-q4f16_1-MLC", | ||
| // model_lib: "http://127.0.0.1:8000/libs/Llama-3.2-1B-Instruct-q4f16_1-webgpu.wasm", | ||
| // overrides: { | ||
| // context_window_size: 2048, | ||
| // }, | ||
| // }, | ||
| // ], | ||
| // }; | ||
| // if (supportsSubgroups) { | ||
| // appConfig.model_list[0].model_lib = appConfig.model_list[0].model_lib.replace( | ||
| // /\.wasm$/, | ||
| // "-subgroups.wasm", | ||
| // ); | ||
| // } | ||
| // const engine: webllm.MLCEngineInterface = await webllm.CreateMLCEngine( | ||
| // selectedModel, | ||
| // { appConfig: appConfig, initProgressCallback: initProgressCallback }, | ||
| // ); | ||
|
|
||
| // Option 3: Instantiate MLCEngine() and call reload() separately | ||
| // const engine: webllm.MLCEngineInterface = new webllm.MLCEngine({ | ||
| // appConfig: appConfig, // if do not specify, we use webllm.prebuiltAppConfig | ||
| // initProgressCallback: initProgressCallback, | ||
| // }); | ||
| // await engine.reload(selectedModel); | ||
|
|
||
| const reply0 = await engine.chat.completions.create({ | ||
| messages: [{ role: "user", content: "List three US states." }], | ||
| // below configurations are all optional | ||
| n: 3, | ||
| temperature: 1.5, | ||
| max_tokens: 256, | ||
| // 46510 and 7188 are "California", and 8421 and 51325 are "Texas" in Llama-3.1-8B-Instruct | ||
| // So we would have a higher chance of seeing the latter two, but never the first in the answer | ||
| logit_bias: { | ||
| "46510": -100, | ||
| "7188": -100, | ||
| "8421": 5, | ||
| "51325": 5, | ||
| }, | ||
| logprobs: true, | ||
| top_logprobs: 2, | ||
| }); | ||
| console.log(reply0); | ||
| console.log(reply0.usage); | ||
|
|
||
| // To change model, either create a new engine via `CreateMLCEngine()`, or call `engine.reload(modelId)` | ||
| } | ||
|
|
||
| main(); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comments explaining the specific token IDs for "California" and "Texas" are highly model-dependent (
Llama-3.1-8B-Instruct). This makes the example less portable and the comments could quickly become outdated or misleading if the model or tokenizer changes. Consider making these comments more generic about the purpose oflogit_biasrather than detailing specific token values, or moving such model-specific details to external documentation if necessary.