Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: scale the token count timeout so long chats can compact (#15900)
oobabooga60dc72a6ba39
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.pyindex bd8b5cb203e..12603b59895 100644--- a/studio/backend/core/inference/llama_cpp.py+++ b/studio/backend/core/inference/llama_cpp.py@@ -7924,6 +7924,19 @@ class CountAborted(Exception):__slots__ = ()+# Single-threaded render + tokenize ran ~600K chars/s on the #15800 reporter's CPU; a sixth of that still catches a hang.+_COUNT_TIMEOUT_FLOOR_S = 10.0+_COUNT_TIMEOUT_CHARS_PER_S = 100_000+++def _count_timeout(messages, system_text, tools) -> float:+ try:+ chars = len(json.dumps([system_text, messages, tools], ensure_ascii = False, default = str))+ except Exception: # noqa: BLE001 -- an unmeasurable prompt keeps the floor+ chars = 0+ return _COUNT_TIMEOUT_FLOOR_S + chars / _COUNT_TIMEOUT_CHARS_PER_S++# Module level so tests can point the orphan sweep at a fixture tree._PROC_ROOT = "/proc"@@ -40742,7 +40755,9 @@ class LlamaCppBackend:try:with httpx.Client(- timeout = 10,+ timeout = httpx.Timeout(+ _COUNT_TIMEOUT_FLOOR_S, read = _count_timeout(messages, system_text, tools)+ ),headers = self._auth_headers,trust_env = False,verify = _local_ssl_context(),@@ -40824,6 +40839,11 @@ class LlamaCppBackend:except CountAborted:# Not a template failure. Must precede the generic except.raise+ except httpx.TimeoutException:+ if strict:+ raise RuntimeError(+ "llama-server timed out counting the prompt"+ ) from Noneexcept Exception:passreturn None, Falsediff --git a/studio/backend/tests/test_count_chat_tokens_timeout.py b/studio/backend/tests/test_count_chat_tokens_timeout.pynew file mode 100644index 00000000000..1428f9eb8d2--- /dev/null+++ b/studio/backend/tests/test_count_chat_tokens_timeout.py@@ -0,0 +1,176 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""A long transcript's token count gets time to finish, so compaction can run (#15800)."""++from __future__ import annotations++import httpx+import pytest++import core.inference.llama_cpp as llama_cpp+from core.inference.checkpoint import fit_checkpoint_context+from core.inference.context_window import fit_rolling_context++# The reporter's llama-server: 11.1 s to tokenize a 1.68M-token prompt.+SERVER_CHARS_PER_S = 600_000+++class _Backend(llama_cpp.LlamaCppBackend):+ is_loaded = True+ base_url = "http://127.0.0.1:8080"+ _auth_headers: dict = {}+++def _slow_llama_server(clients: list):+ """Renders and tokenizes at SERVER_CHARS_PER_S; times out when the client's read timeout is shorter."""++ class _Resp:+ status_code = 200++ def __init__(self, payload):+ self._payload = payload++ def json(self):+ return self._payload++ class _Client:+ def __init__(+ self,+ *_args,+ timeout = None,+ **_kwargs,+ ):+ self.timeout = timeout+ clients.append(self)++ def __enter__(self):+ return self++ def __exit__(self, *_exc):+ return False++ def post(+ self,+ url,+ json = None,+ **_kwargs,+ ):+ body = json or {}+ if url.endswith("/apply-template"):+ text = "|".join(str(m.get("content") or "") for m in body.get("messages", []))+ payload = {"prompt": text}+ else:+ text = body.get("content", "")+ payload = {"tokens": [0] * (len(text) // 4)}+ if len(text) / SERVER_CHARS_PER_S > self.timeout.read:+ raise httpx.ReadTimeout("timed out")+ return _Resp(payload)++ return _Client+++def _transcript(turns: int) -> list[dict]:+ messages = [{"role": "system", "content": "you are helpful"}]+ for index in range(turns):+ messages.append({"role": "user", "content": f"question {index} " + "word " * 400})+ messages.append({"role": "assistant", "content": f"answer {index} " + "word " * 2000})+ messages.append({"role": "user", "content": "What is 2+2?"})+ return messages+++# About 7.2M characters: 12 s for the stand-in, past the old fixed 10 s.+LONG = _transcript(600)+++def backend(monkeypatch):+ clients: list = []+ monkeypatch.setattr(llama_cpp.httpx, "Client", _slow_llama_server(clients))+ instance = _Backend.__new__(_Backend)+ instance.clients = clients+ return instance+++def _strict_count(backend):+ return lambda messages: backend.count_chat_tokens(messages, strict = True)+++def test_the_timeout_keeps_the_floor_for_a_short_prompt():+ assert llama_cpp._count_timeout([{"role": "user", "content": "hi"}], "", None) < 10.1+++def test_the_timeout_leaves_a_slow_cpu_room_to_count_a_long_transcript():+ chars = sum(len(m["content"]) for m in LONG)+ assert chars / SERVER_CHARS_PER_S > 10+ assert llama_cpp._count_timeout(LONG, "", None) > 5 * chars / SERVER_CHARS_PER_S+++def test_a_long_transcript_is_counted_rather_than_timing_out(backend):+ counted = backend.count_chat_tokens(LONG, strict = True)+ assert counted > 1_000_000+ timeout = backend.clients[-1].timeout+ assert timeout.read > 10+ assert (timeout.connect, timeout.write, timeout.pool) == (10, 10, 10)+++def test_rolling_compacts_a_transcript_whose_count_outlasts_ten_seconds(backend):+ fitted, truncation = fit_rolling_context(+ LONG, context_length = 16384, max_tokens = 64, count_tokens = _strict_count(backend)+ )+ assert truncation["fits"] is True+ assert truncation["dropped_messages"] > 0+ assert fitted[-1]["content"] == "What is 2+2?"++[email protected]("force_reset", (False, True))+def test_checkpoint_compacts_a_transcript_whose_count_outlasts_ten_seconds(backend, force_reset):+ fitted, truncation = fit_checkpoint_context(+ LONG,+ context_length = 16384,+ max_tokens = 64,+ count_tokens = _strict_count(backend),+ can_reset = True,+ searchable = True,+ force_reset = force_reset,+ )+ assert truncation["fits"] is True+ assert truncation["checkpoint_started"] is True+ assert fitted[-1]["content"] == "What is 2+2?"+++def _timing_out_client():+ class _Resp:+ status_code = 200++ def json(self):+ return {"prompt": "rendered"}++ class _Client:+ def __init__(self, *_args, **_kwargs):+ pass++ def __enter__(self):+ return self++ def __exit__(self, *_exc):+ return False++ def post(self, url, **_kwargs):+ if url.endswith("/apply-template"):+ return _Resp()+ raise httpx.ReadTimeout("timed out")++ return _Client++[email protected]("strict", (True, False))+def test_a_count_timeout_is_not_reported_as_a_template_refusal(monkeypatch, strict):+ monkeypatch.setattr(llama_cpp.httpx, "Client", _timing_out_client())+ backend = _Backend.__new__(_Backend)+ messages = [{"role": "user", "content": "hello"}]+ if strict:+ with pytest.raises(RuntimeError, match = "timed out counting the prompt"):+ backend.count_chat_tokens(messages, strict = True)+ else:+ assert backend.count_chat_tokens(messages) == 0