[Fix] Allow concurrent inference for multi model in WebWorker #546
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.
This is a follow-up to #542. Update
examples/multi-model
to use web worker, and to also show case generating responses from two models concurrently from the same engine. This is already supported forMLCEngine
prior to this PR, butWebWorkerMLCEngine
needed a patch. Specifically:WebWorkerMLCEngineHandler
maintains a singleasyncGenreator
, assuming there is only one model.this.asyncGenerator
withthis.loadedModelIdToAsyncGenerator
, which maps from a model id to its dedicatedasyncGenerator
selectedModelId
, hence the updates for the message sending and handling ofchatCompletionStreamInit
,completionStreamInit
,completionStreamNextChunk
.next()
oncompletion()
andchatCompletion()
ofWebWorkerMLCEngine
will callgetModelIdToUse()
, which prior to this PR delays till the underlyingMLCEngine
this.loadedModelIdToAsyncGenerator
may not be cleaned properly when one asyncGenerator finishes. We only call clear atunload()
, which may not be called upon reload(). However, service_worker may skipreload()
. Will leave it as is for now.Tested with WebLLMChat, also tests WebLLMChat terminating service worker manually.