Skip to content

Commit 6b4fa87

Browse files
[Fix][Grammar] Detach token table to prevent disposing it (#571)
Prior to this PR, when reusing the same engine but for different schema will run into error "Module has already been disposed". An example to reproduce this is included in #560. This is because `this.tokenTable` is a `tvmjs.TVMObject` and will be disposed after the scope ends. We fix this by wrapping the call with `this.tvm.detachFromCurrentScope()`, and only dispose `this.tokenTable` when we dispose the entire `LLMChatPipeline`. Co-authored-by: SMarioMan <[email protected]>
1 parent 2f6762d commit 6b4fa87

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/llm_chat.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ export class LLMChatPipeline {
298298
this.logitsOnCPU?.dispose();
299299
this.tvm.dispose();
300300
this.tokenizer.dispose();
301+
this.tokenTable?.dispose();
301302
}
302303

303304
/**
@@ -557,9 +558,11 @@ export class LLMChatPipeline {
557558
if (this.tokenTable === undefined) {
558559
const rawTokenTable = getTokenTableFromTokenizer(this.tokenizer);
559560
// Post process entire table
560-
this.tokenTable = this.fpostProcessTokenTable(
561-
rawTokenTable,
562-
this.token_postproc_method,
561+
this.tokenTable = this.tvm.detachFromCurrentScope(
562+
this.fpostProcessTokenTable(
563+
rawTokenTable,
564+
this.token_postproc_method,
565+
),
563566
);
564567
}
565568
const grammar: BNFGrammar =

0 commit comments

Comments
 (0)