Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

Keep loaded objects out of the garbage collector in the Qwen and GLM servers

YanissAmzb161cc53a8ba
diff --git a/tools/glm/serve.py b/tools/glm/serve.py
index ef36211..866d974 100644
--- a/tools/glm/serve.py
+++ b/tools/glm/serve.py
@@ -387,6 +387,13 @@ class ResidentEngine:
self.supports_vision = self.vision is not None
self.vocab_size = int(self.config.vocab_size or self.tokenizer.actual_vocab_size) # rows of the embedding table
self.Generator = Generator
+ # The loaded model and tokenizer are far more Python objects than a request ever creates, and every later gc.collect()
+ # walked them all: the Generator rebuild of a cache_salt change paid for it inside time to first token. Keep them out
+ # of the collector, before the first Generator exists (a frozen Generator would never be freed). 0 = off.
+ if os.environ.get("EXL3_GC_FREEZE", "1") != "0":
+ import gc
+ gc.collect()
+ gc.freeze()
self.greedy_generator = self._make_generator()
# n1f2: one drafted token, fused catch-up variant 2. One Generator only: a second one over the
# same Cache takes ownership and breaks the first. Sampling also goes through MTP: a draft is
diff --git a/tools/qwen/serve.py b/tools/qwen/serve.py
index 51fedb9..0a8f924 100644
--- a/tools/qwen/serve.py
+++ b/tools/qwen/serve.py
@@ -685,6 +685,12 @@ class QwenEngine:
self.spec_on = self.draft_model is not None
self.slot_store = None
self.last_stats: dict[str, Any] = {}
+ # The loaded model and tokenizer are about 1.9 M Python objects, and every later gc.collect() walked them all: the Generator
+ # rebuild of a cache_salt change or cache_prompt=false spent 0.5 s there, inside time to first token (0.53 -> 0.03 s).
+ # Keep them out of the collector, before the first Generator exists (a frozen Generator would never be freed). 0 = off.
+ if os.environ.get("EXL3_GC_FREEZE", "1") != "0":
+ gc.collect()
+ gc.freeze()
self._build(self.spec_on)
def _ngram_resident(self) -> None: