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