unsloth/studio/backend
Daniel Han 1dcb9ea8cd Fix ~20% SSE streaming overhead in Studio inference
Benchmarking showed llama-server through Studio's web layer runs at
284 TPS vs 357 TPS direct (~20% overhead, +0.72ms per token). The
generator code itself has zero overhead -- the bottleneck is entirely
in how the route handler and middleware pipeline yield SSE chunks.

Three structural changes:

1. Convert LoggingMiddleware from BaseHTTPMiddleware to pure ASGI
   middleware. BaseHTTPMiddleware routes every response body chunk
   through an anyio memory channel (two coroutine context switches
   per chunk). The pure ASGI version wraps the send callable directly.

2. Replace per-token asyncio.to_thread / run_in_executor with a
   single background thread feeding an asyncio.Queue via
   call_soon_threadsafe (~2us vs ~50us per token). All three
   streaming loops (gguf_stream_chunks, gguf_tool_stream,
   stream_chunks) now use the shared _run_gen_to_queue helper.

3. Pre-format JSON for content-token chunks (the 99% hot path)
   instead of constructing 3 Pydantic objects and calling
   model_dump_json per token. Pydantic is still used for the
   first chunk (role), final chunk (finish_reason), and usage
   chunk where correctness matters more than speed.

Additionally, request.is_disconnected() is now checked every 20
tokens instead of every token. The cancel_event mechanism still
provides prompt cancellation since the watcher in llama_cpp.py
closes the httpx response on cancel.
2026-03-26 10:28:34 +00:00
..
assets studio: improve onboarding UX, tooltips, and training defaults (#4355) 2026-03-17 07:46:07 -07:00
auth fix: remove old comments (#4292) 2026-03-14 16:50:13 +04:00
core feat(studio): editable context length with Apply/Reset for GGUF settings (#4592) 2026-03-25 08:32:38 -07:00
loggers Fix ~20% SSE streaming overhead in Studio inference 2026-03-26 10:28:34 +00:00
models feat(studio): editable context length with Apply/Reset for GGUF settings (#4592) 2026-03-25 08:32:38 -07:00
plugins Bump Data Designer to 0.5.4 (removes litellm dependency) (#4569) 2026-03-25 02:01:43 -07:00
requirements Bump Data Designer to 0.5.4 (removes litellm dependency) (#4569) 2026-03-25 02:01:43 -07:00
routes Fix ~20% SSE streaming overhead in Studio inference 2026-03-26 10:28:34 +00:00
state Final cleanup 2026-03-12 18:28:04 +00:00
storage feat(studio): training history persistence and past runs viewer (#4501) 2026-03-25 00:58:55 -07:00
tests feat: support GGUF export for non-PEFT models + fix venv_t5 switching for local checkpoints (#4455) 2026-03-20 12:13:18 +04:00
utils feat: multi-source model discovery (HF default, legacy cache, LM Studio) (#4591) 2026-03-25 07:48:04 -07:00
__init__.py Final cleanup 2026-03-12 18:28:04 +00:00
_platform_compat.py Fix Studio crash on Anaconda/conda-forge Python (#4484) 2026-03-22 05:36:55 -07:00
colab.py Fix Colab huggingface-hub conflict, ensurepip fallback, bump to 2026.3.14 (#4603) 2026-03-25 09:38:02 -07:00
main.py feat(studio): training history persistence and past runs viewer (#4501) 2026-03-25 00:58:55 -07:00
run.py Add PID file tracking and unsloth studio stop command (#4598) 2026-03-25 08:27:27 -07:00