Skip to content

Commit 9dd3950

Browse files
Anai-GuoAnai Guoabetlen
authored
fix(llama): release a vision chat handler's mtmd context on close (#2342) (#2343)
* fix(llama): release a vision chat handler's mtmd context on close (#2342) `Llama.close()` tore down the model and its context but left the chat handler's `_exit_stack` untouched, so the mtmd/clip context it built from that model was never freed. Handlers routinely outlive the `Llama` that initialized them -- callers construct one handler and reuse it across loads -- and `_init_mtmd_context()` returns early while `mtmd_ctx` is set. After the first `close()` the handler therefore kept a context bound to an already-freed model and handed it back on the next load, where it surfaces as a null `mtmd_ctx` on the C++ side. Register the handler's exit stack on the Llama's `_stack`. It unwinds LIFO, so the mtmd context is released ahead of the model teardown registered earlier in `__init__`, and `mtmd_free` resets `mtmd_ctx` to `None` so the next load re-initializes cleanly. Duck-typed on `_exit_stack`, which both `Llava15ChatHandler` and `MTMDChatHandler` (and every subclass) expose. * fix: bind multimodal context cleanup to model --------- Co-authored-by: Anai Guo <antai12232931@anaiguo.com> Co-authored-by: abetlen <abetlen@gmail.com>
1 parent d8a4a1e commit 9dd3950

2 files changed

Lines changed: 3 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- fix(example): retain recurrent state for server MTP rollback
1212
- feat: update llama.cpp to ggml-org/llama.cpp@adb55e514
1313
- fix(server): show falsey defaults in CLI help by @cupkk in #2355
14+
- fix: release multimodal contexts when closing Llama by @Anai-Guo in #2343
1415

1516
## [0.3.34]
1617

‎llama_cpp/llama_chat_format.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import string
1010

1111
from datetime import datetime
12-
from contextlib import ExitStack
1312
from typing import (
1413
Any,
1514
Dict,
@@ -2779,7 +2778,6 @@ def __init__(self, clip_model_path: str, verbose: bool = True):
27792778
self.clip_model_path = clip_model_path
27802779
self.verbose = verbose
27812780
self._mtmd_cpp = mtmd_cpp
2782-
self._exit_stack = ExitStack()
27832781
self.mtmd_ctx: Optional[mtmd_cpp.mtmd_context_p] = None
27842782

27852783
if not os.path.exists(clip_model_path):
@@ -2825,7 +2823,7 @@ def mtmd_free():
28252823
self._mtmd_cpp.mtmd_free(self.mtmd_ctx)
28262824
self.mtmd_ctx = None
28272825

2828-
self._exit_stack.callback(mtmd_free)
2826+
llama_model._stack.callback(mtmd_free)
28292827

28302828
def load_image(self, image_url: str) -> bytes:
28312829
return self._load_image(image_url)
@@ -3278,7 +3276,6 @@ def __init__(
32783276
self.verbose = verbose
32793277
self.use_gpu = use_gpu
32803278
self._mtmd_cpp = mtmd_cpp
3281-
self._exit_stack = ExitStack()
32823279
self.mtmd_ctx: Optional[mtmd_cpp.mtmd_context_p] = None
32833280

32843281
if not os.path.exists(clip_model_path):
@@ -3321,7 +3318,7 @@ def mtmd_free():
33213318
self._mtmd_cpp.mtmd_free(self.mtmd_ctx)
33223319
self.mtmd_ctx = None
33233320

3324-
self._exit_stack.callback(mtmd_free)
3321+
llama_model._stack.callback(mtmd_free)
33253322

33263323
def load_image(self, image_url: str) -> bytes:
33273324
return self._load_image(image_url)

0 commit comments

Comments
 (0)