-
Notifications
You must be signed in to change notification settings - Fork 934
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WorkerHandler][Breaking] Create MLCEngine in worker handler internal…
…ly (#472) This PR applies to all `WebWorkerMLCEngine`, `ServiceWorkerMLCEngine`, and `ExtensionServiceWorkerMLCEngine`. Prior to this PR, the worker thread script looked like the following: ```typescript import { MLCEngineServiceWorkerHandler, MLCEngine, } from "@mlc-ai/web-llm"; const engine = new MLCEngine(); let handler: MLCEngineServiceWorkerHandler; self.addEventListener("activate", function (event) { handler = new MLCEngineServiceWorkerHandler(engine); console.log("Service Worker is ready"); }); ``` This may confuse users because they need to instantiate an `MLCEngine` in the backend, and an `ServiceWorkerMLCEngine` in the frontend. After this PR, the script looks like the following: ```typescript import { ServiceWorkerMLCEngineHandler } from "@mlc-ai/web-llm"; let handler: ServiceWorkerMLCEngineHandler; self.addEventListener("activate", function (event) { handler = new ServiceWorkerMLCEngineHandler(); console.log("Service Worker is ready"); }); ``` That is, `WorkerHandler` does not take any constructor (except `port` for ExtensionServiceWorker), and we will instantiate `MLCEngine` internally, making the flow more intuitive. For logit processor usage, we add `setLogitProcessor()` to the handler (see examples for the change). Besides, we rename: - `MLCEngineWorkerHandler` --> `WebWorkerMLCEngineHandler` - `MLCEngineServiceWorkerHandler` --> `ServiceWorkerMLCEngineHandler` - `MLCEngineExtensionServiceWorkerHandler` --> `ExtensionServiceWorkerMLCEngineHandler`
- Loading branch information
1 parent
e9b83b6
commit 7e11cf3
Showing
11 changed files
with
62 additions
and
81 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
8 changes: 2 additions & 6 deletions
8
examples/chrome-extension-webgpu-service-worker/src/background.ts
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 |
---|---|---|
@@ -1,8 +1,7 @@ | ||
import { MLCEngineWorkerHandler, MLCEngine } from "@mlc-ai/web-llm"; | ||
import { WebWorkerMLCEngineHandler } from "@mlc-ai/web-llm"; | ||
|
||
// Hookup an engine to a worker handler | ||
const engine = new MLCEngine(); | ||
const handler = new MLCEngineWorkerHandler(engine); | ||
const handler = new WebWorkerMLCEngineHandler(); | ||
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
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 |
---|---|---|
@@ -1,13 +1,8 @@ | ||
import { | ||
MLCEngineServiceWorkerHandler, | ||
MLCEngineInterface, | ||
MLCEngine, | ||
} from "@mlc-ai/web-llm"; | ||
import { ServiceWorkerMLCEngineHandler } from "@mlc-ai/web-llm"; | ||
|
||
const engine: MLCEngineInterface = new MLCEngine(); | ||
let handler: MLCEngineServiceWorkerHandler; | ||
let handler: ServiceWorkerMLCEngineHandler; | ||
|
||
self.addEventListener("activate", function (event) { | ||
handler = new MLCEngineServiceWorkerHandler(engine); | ||
handler = new ServiceWorkerMLCEngineHandler(); | ||
console.log("Web-LLM Service Worker Activated"); | ||
}); |
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 |
---|---|---|
@@ -1,8 +1,7 @@ | ||
// Serve the engine workload through web worker | ||
import { MLCEngineWorkerHandler, MLCEngine } from "@mlc-ai/web-llm"; | ||
import { WebWorkerMLCEngineHandler } from "@mlc-ai/web-llm"; | ||
|
||
const engine = new MLCEngine(); | ||
const handler = new MLCEngineWorkerHandler(engine); | ||
const handler = new WebWorkerMLCEngineHandler(); | ||
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 |
---|---|---|
@@ -1,8 +1,7 @@ | ||
// Serve the engine workload through web worker | ||
import { MLCEngineWorkerHandler, MLCEngine } from "@mlc-ai/web-llm"; | ||
import { WebWorkerMLCEngineHandler } from "@mlc-ai/web-llm"; | ||
|
||
const engine = new MLCEngine(); | ||
const handler = new MLCEngineWorkerHandler(engine); | ||
const handler = new WebWorkerMLCEngineHandler(); | ||
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
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.