unsloth/studio/frontend/src/features/chat
Daniel Han a32b871f0e
studio: add speculative decoding support (ngram-mod, on by default) (#4836)
* studio: add speculative decoding support (ngram-mod, on by default)

Enable n-gram speculative decoding for GGUF models in Unsloth Studio.
Uses llama.cpp's ngram-mod mode which gives 10-40% faster generation
with zero VRAM cost via a 4MB fixed hash table that auto-resets on
low acceptance rates.

Backend:
- Add speculative_type field to LoadRequest, LoadResponse, and
  InferenceStatusResponse pydantic models
- Add speculative_type parameter to LlamaCppBackend.load_model()
  with allowlist validation (ngram-simple, ngram-mod)
- Pass --spec-type, --spec-ngram-size-n 16, --draft-max 24 flags
  to llama-server when ngram-mod is active
- Default to ngram-mod for non-vision GGUF models server-side
- Silently skip speculative decoding for vision models (unsupported
  in llama.cpp server-context.cpp)

Frontend:
- Add speculative_type to TS API types
- Add speculativeType/loadedSpeculativeType to chat runtime store
  with default value of "ngram-mod"
- Add On/Off toggle in Model settings section (GGUF only, hidden
  for vision models), included in dirty check for Apply/Reset
- Wire speculative_type through model load request and response
- Restore speculative type state on page refresh/reconnect

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: remove server-side speculative decoding override

The backend was overriding speculative_type=None to "ngram-mod" for
non-vision GGUF models, which prevented users from disabling spec
decoding via the UI toggle. The frontend store already defaults to
"ngram-mod", so the backend fallback was redundant and blocked the
explicit "Off" setting.

* fix: use recommended ngram-mod params from llama.cpp docs

Update speculative decoding params to match the recommended values
from llama.cpp docs (docs/speculative.md):
  --spec-ngram-size-n 24 (was 16, docs say small n not recommended)
  --draft-min 48 (was 0)
  --draft-max 64 (was 24, docs note MoEs need long drafts)

Also fix comment: ngram-mod uses ~16 MB (4M entries * 4 bytes),
not 4 MB.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add benchmark table and references to speculative decoding comment

Include speedup numbers from llama.cpp PRs #18471 and #19164 as an
inline comment so future readers understand the expected gains.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-04-03 13:56:59 -07:00
..
api fix(studio): ensure first chat tool call starts in session sandbox (#4810) 2026-04-03 11:44:22 -07:00
components feat(chat): server-side timings, context display & source hover cards (#4467) 2026-03-20 23:42:01 -07:00
hooks studio: add speculative decoding support (ngram-mod, on by default) (#4836) 2026-04-03 13:56:59 -07:00
lib Update license headers 2026-03-12 17:23:10 +00:00
stores studio: add speculative decoding support (ngram-mod, on by default) (#4836) 2026-04-03 13:56:59 -07:00
tour studio: improve onboarding UX, tooltips, and training defaults (#4355) 2026-03-17 07:46:07 -07:00
types studio: add speculative decoding support (ngram-mod, on by default) (#4836) 2026-04-03 13:56:59 -07:00
utils Update license headers 2026-03-12 17:23:10 +00:00
chat-page.tsx fix(studio): ensure first chat tool call starts in session sandbox (#4810) 2026-04-03 11:44:22 -07:00
chat-settings-sheet.tsx studio: add speculative decoding support (ngram-mod, on by default) (#4836) 2026-04-03 13:56:59 -07:00
db.ts studio: fix chat CPU spike (#4632) 2026-03-27 06:20:26 +00:00
index.ts Update license headers 2026-03-12 17:23:10 +00:00
runtime-provider.tsx fix(studio): ensure first chat tool call starts in session sandbox (#4810) 2026-04-03 11:44:22 -07:00
shared-composer.tsx studio: align composer/code, unify fonts, and remove tool collapse jitter (#4763) 2026-04-01 19:57:10 -07:00
thread-sidebar.tsx feat(studio): editable context length with Apply/Reset for GGUF settings (#4592) 2026-03-25 08:32:38 -07:00
types.ts feat(studio): display images from Python tool execution in chat UI (#4778) 2026-04-02 05:08:16 -07:00