From 4c1d74c0aecb5fe263674cc761573654b16d4c20 Mon Sep 17 00:00:00 2001 From: Andrei Betlen Date: Wed, 19 Jun 2024 10:07:20 -0400 Subject: [PATCH] fix: Make destructor to automatically call .close() method on Llama class. --- llama_cpp/_internals.py | 9 +++++++++ llama_cpp/llama.py | 3 +++ 2 files changed, 12 insertions(+) diff --git a/llama_cpp/_internals.py b/llama_cpp/_internals.py index ee990d4..423c1b5 100644 --- a/llama_cpp/_internals.py +++ b/llama_cpp/_internals.py @@ -64,6 +64,9 @@ class _LlamaModel: def close(self): self._exit_stack.close() + def __del__(self): + self.close() + def vocab_type(self) -> int: assert self.model is not None return llama_cpp.llama_vocab_type(self.model) @@ -292,6 +295,9 @@ class _LlamaContext: def close(self): self._exit_stack.close() + def __del__(self): + self.close() + def n_ctx(self) -> int: assert self.ctx is not None return llama_cpp.llama_n_ctx(self.ctx) @@ -531,6 +537,9 @@ class _LlamaBatch: def close(self): self._exit_stack.close() + def __del__(self): + self.close() + def n_tokens(self) -> int: assert self.batch is not None return self.batch.n_tokens diff --git a/llama_cpp/llama.py b/llama_cpp/llama.py index 459b29f..5f53966 100644 --- a/llama_cpp/llama.py +++ b/llama_cpp/llama.py @@ -1968,6 +1968,9 @@ class Llama: """Explicitly free the model from memory.""" self._stack.close() + def __del__(self) -> None: + self.close() + @staticmethod def logits_to_logprobs( logits: Union[npt.NDArray[np.single], List], axis: int = -1