Skip to content

GLM-5.3-Flash EXL3 on 2x DGX Spark with TensorFold

Python
2.6 KB
Raw Blame
# PR #23: earlier turns keep their reasoning in the prompt (clear_thinking=False unless the request sets it);
# TF_GLM_CLEAR_THINKING=1 restores the old rendering. Contributed by @kky42
# (MiaAI-Lab/GLM-5.3-Flash-EXL3-2x-DGX-Sparks-TensorFold#23), applied unchanged. Apache-2.0.

diff --git tensorfold/families/glm5_next/cuda/app.py tensorfold/families/glm5_next/cuda/app.py
index 161f218..5a540cf 100644
--- tensorfold/families/glm5_next/cuda/app.py
+++ tensorfold/families/glm5_next/cuda/app.py
@@ -2,6 +2,7 @@
 
 from __future__ import annotations
 
+import os
 from pathlib import Path
 from typing import Any, Callable
 
@@ -17,14 +18,17 @@ class ThinkingOffTemplate:
     (``prompts.agent_history``), with the reasoning of tool-calling steps the client dropped put back (``kept``,
     ``kept_reasoning.KeptReasoning``; None: off)."""
 
-    def __init__(self, inner, kept: KeptReasoning | None = None) -> None:
+    def __init__(self, inner, kept: KeptReasoning | None = None, clear_thinking: bool = False) -> None:
         self.inner = inner
         self.efforts = getattr(inner, "efforts", frozenset())
         self.kept = kept
+        self.clear_thinking = clear_thinking
 
     def render(self, messages, *, tools, enable_thinking, extra=None, allow_images: bool = False) -> str:
         if self.kept is not None:
             messages = self.kept.restore(messages)
+        # earlier turns keep their reasoning unless the request says otherwise, as in zai-org's current template
+        extra = {"clear_thinking": self.clear_thinking, **(extra or {})}
         text = self.inner.render(agent_history(messages), tools=tools, enable_thinking=enable_thinking, extra=extra,
                                  allow_images=allow_images)
         return text if enable_thinking else thinking_off(text)
@@ -60,7 +64,10 @@ class GlmApp(App):
         super().__init__(engine, model_dir, served, **kwargs)
         if self.vision is not None:
             media_template(self.template, Path(model_dir))
-        self.template = ThinkingOffTemplate(self.template, KeptReasoning.from_env())
+        clear = os.environ.get("TF_GLM_CLEAR_THINKING", "0").strip() or "0"
+        if clear not in ("0", "1"):
+            raise ValueError(f"TF_GLM_CLEAR_THINKING={clear!r}: expected 0 or 1")
+        self.template = ThinkingOffTemplate(self.template, KeptReasoning.from_env(), clear_thinking=clear == "1")
 
     def check(self, body: dict[str, Any], *, prepared: PreparedRequest | None = None) -> str | None:
         """Validate the rendered prompt plus max_tokens against the engine context limit before streaming."""