Skip to content

Commit 11aeacd

Browse files
committed
fix: bind multimodal context cleanup to model
1 parent 5090bff commit 11aeacd

3 files changed

Lines changed: 3 additions & 15 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.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -463,16 +463,6 @@ def free_lora_adapter():
463463

464464
self.chat_format = chat_format
465465
self.chat_handler = chat_handler
466-
# A vision chat handler builds its mtmd/clip context from this model, so
467-
# that context must be released before the model itself is freed. The
468-
# handler object can outlive the Llama (callers commonly reuse a single
469-
# handler across loads), and it skips re-initialization while mtmd_ctx
470-
# is set -- leaving a context bound to an already-freed model behind.
471-
# `_stack` unwinds LIFO, so this runs before the model teardown that was
472-
# registered earlier in __init__.
473-
handler_stack = getattr(chat_handler, "_exit_stack", None)
474-
if handler_stack is not None:
475-
self._stack.callback(handler_stack.close)
476466
self._chat_handlers: Dict[
477467
str, llama_chat_format.LlamaChatCompletionHandler
478468
] = {}

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)