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.

Serving: compile the chat template once per process (GLM + Qwen servers), ~24 ms off every chat request, output bitwise equal

YanissAmzcd0d92a2d4bc
diff --git a/tools/glm/serve.py b/tools/glm/serve.py
index 817835c..07c0be6 100644
--- a/tools/glm/serve.py
+++ b/tools/glm/serve.py
@@ -371,16 +371,33 @@ def template_kwargs(body: dict[str, Any]) -> dict[str, Any]:
return kw
-def render_prompt(template: str, messages: list[dict[str, Any]], tools: list[dict[str, Any]] | None,
- **kwargs: Any) -> str:
- """Render the model's own Jinja chat template."""
- # Same environment as transformers' apply_chat_template.
+def _template_env() -> Any:
+ """One Jinja environment per process (same settings as transformers' apply_chat_template)."""
env = ImmutableSandboxedEnvironment(trim_blocks=True, lstrip_blocks=True,
extensions=["jinja2.ext.loopcontrols"])
env.filters["tojson"] = lambda value, ensure_ascii=False, indent=None, separators=None, sort_keys=False: \
json.dumps(value, ensure_ascii=ensure_ascii, indent=indent, separators=separators, sort_keys=sort_keys)
def raise_exception(msg): raise ValueError(msg)
env.globals["raise_exception"] = raise_exception
+ return env
+
+
+_COMPILED_TEMPLATES: dict[str, Any] = {} # template source -> compiled Template (render is thread-safe)
+
+
+def _compiled_template(template: str) -> Any:
+ """The compiled chat template, cached: compiling it per request costs ~24 ms (8.6 KB GLM template)."""
+ hit = _COMPILED_TEMPLATES.get(template)
+ if hit is None:
+ hit = _template_env().from_string(template)
+ _COMPILED_TEMPLATES[template] = hit
+ return hit
+
+
+def render_prompt(template: str, messages: list[dict[str, Any]], tools: list[dict[str, Any]] | None,
+ **kwargs: Any) -> str:
+ """Render the model's own Jinja chat template."""
+ # Same environment as transformers' apply_chat_template.
# OpenAI sends tool-call arguments as a JSON string; the GLM template iterates them as a dict.
fixed = []
for m in messages:
@@ -392,7 +409,7 @@ def render_prompt(template: str, messages: list[dict[str, Any]], tools: list[dic
if isinstance(c.get("function", {}).get("arguments"), str) else c
for c in m["tool_calls"]])
fixed.append(m)
- return env.from_string(template).render(
+ return _compiled_template(template).render(
messages=fixed, tools=tools or [], add_generation_prompt=True, **kwargs)
diff --git a/tools/mimo/serve.py b/tools/mimo/serve.py
index ce93af2..99a8e76 100644
--- a/tools/mimo/serve.py
+++ b/tools/mimo/serve.py
@@ -139,9 +139,8 @@ TOOL_CLOSE = "</tool_call>"
# ---------------------------------------------------------------------------- template
-def render_prompt(template: str, messages: list[dict[str, Any]], tools: list[dict[str, Any]] | None,
- **kwargs: Any) -> str:
- """Render the model's own Jinja chat template (MiMo chat_template.jinja)."""
+def _template_env() -> Any:
+ """One Jinja environment per process."""
env = ImmutableSandboxedEnvironment(trim_blocks=True, lstrip_blocks=True,
extensions=["jinja2.ext.loopcontrols"])
env.filters["tojson"] = lambda value, ensure_ascii=False, indent=None, separators=None, \
@@ -149,6 +148,24 @@ def render_prompt(template: str, messages: list[dict[str, Any]], tools: list[dic
separators=separators, sort_keys=sort_keys)
def raise_exception(msg): raise ValueError(msg)
env.globals["raise_exception"] = raise_exception
+ return env
+
+
+_COMPILED_TEMPLATES: dict[str, Any] = {} # template source -> compiled Template (render is thread-safe)
+
+
+def _compiled_template(template: str) -> Any:
+ """The compiled chat template, cached: compiling it per request costs ~24 ms."""
+ hit = _COMPILED_TEMPLATES.get(template)
+ if hit is None:
+ hit = _template_env().from_string(template)
+ _COMPILED_TEMPLATES[template] = hit
+ return hit
+
+
+def render_prompt(template: str, messages: list[dict[str, Any]], tools: list[dict[str, Any]] | None,
+ **kwargs: Any) -> str:
+ """Render the model's own Jinja chat template (MiMo chat_template.jinja)."""
# OpenAI sends tool-call arguments as a JSON string; the MiMo template accepts a dict or a
# string, but normalizing keeps `tool_call.function.arguments` round-tripping exact.
fixed = []
@@ -161,7 +178,7 @@ def render_prompt(template: str, messages: list[dict[str, Any]], tools: list[dic
if isinstance(c.get("function", {}).get("arguments"), str) else c
for c in m["tool_calls"]])
fixed.append(m)
- return env.from_string(template).render(
+ return _compiled_template(template).render(
messages=fixed, tools=tools or [], add_generation_prompt=True, **kwargs)