From d8395aeb778a1b29f6ed5f4996675c9f1baebc94 Mon Sep 17 00:00:00 2001 From: usernames122 <88596366+usernames122@users.noreply.github.com> Date: Sat, 4 Apr 2026 19:47:14 +0200 Subject: [PATCH] Add attribute check for sampler in close method This solves a bug I uncovered, that causes an AttributeError if constantly re-initializing a model in a loop and Python garbage collects it, such as testing the highest GPU layer count you can go before CUDA OOMs. --- llama_cpp/_internals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llama_cpp/_internals.py b/llama_cpp/_internals.py index cde52c8c8..35abfa537 100644 --- a/llama_cpp/_internals.py +++ b/llama_cpp/_internals.py @@ -76,7 +76,7 @@ def free_model(): self._exit_stack.callback(free_model) def close(self): - if self.sampler is not None: + if hasattr(self, "sampler") and self.sampler is not None: # NOTE: Must remove custom samplers before free or llama.cpp will try to free them for i, _ in reversed(self.custom_samplers): llama_cpp.llama_sampler_chain_remove(self.sampler, i)