Skip to content
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

Fix interruptGenerate causing the engine to get stuck when using synchronous completion calls #596

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,8 @@ export class MLCEngine implements MLCEngineInterface {
async chatCompletion(
request: ChatCompletionRequest,
): Promise<AsyncIterable<ChatCompletionChunk> | ChatCompletion> {
const initialInterruptSignal = this.interruptSignal;

// 0. Check model loaded and preprocess inputs
const [selectedModelId, selectedPipeline, selectedChatConfig] =
this.getLLMStates("ChatCompletionRequest", request.model);
Expand All @@ -771,6 +773,10 @@ export class MLCEngine implements MLCEngineInterface {
// 0.5 Block wait until this pipeline finishes all previous requests
const lock = this.loadedModelIdToLock.get(selectedModelId)!;
await lock.acquire();
// If interruptGenerate called during the wait for lock, respect that. But reset if locked from a previous call to interruptGenerate
if (initialInterruptSignal && this.interruptSignal) {
this.interruptSignal = false;
}

// 1. If request is streaming, return an AsyncIterable (an iterable version of `_generate()`)
if (request.stream) {
Expand Down Expand Up @@ -901,6 +907,8 @@ export class MLCEngine implements MLCEngineInterface {
async completion(
request: CompletionCreateParams,
): Promise<AsyncIterable<Completion> | Completion> {
const initialInterruptSignal = this.interruptSignal;

// 0. Check model loaded and preprocess inputs
const [selectedModelId, selectedPipeline, selectedChatConfig] =
this.getLLMStates("CompletionCreateParams", request.model);
Expand All @@ -920,6 +928,10 @@ export class MLCEngine implements MLCEngineInterface {
// 0.5 Block wait until this pipeline finishes all previous requests
const lock = this.loadedModelIdToLock.get(selectedModelId)!;
await lock.acquire();
// If interruptGenerate called during the wait for lock, respect that. But reset if locked from a previous call to interruptGenerate
if (initialInterruptSignal && this.interruptSignal) {
this.interruptSignal = false;
}

// 1. If request is streaming, return an AsyncIterable (an iterable version of `_generate()`)
if (request.stream) {
Expand Down
Loading