Skip to content

Studio: serve the Jev API locally with Laya - #11603

Merged
danielhanchen merged 36 commits into
unslothai:mainfrom
NilayYadav:studio-decision-api
Sep 25, 2026
Merged

danielhanchen merged 36 commits into
unslothai:mainfrom
NilayYadav:studio-decision-api

Conversation

@NilayYadav

@NilayYadav NilayYadav commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator

Adds a Decision API to Studio, backed by Laya running locally.

POST /v1/systemone takes text plus yes/no, pick-one or score questions and returns a probability for each answer. The request format matches TypeSafe's Jev, so the TypeSafe SDK works against Studio as is.

Off by default. The owner turns it on from a new Decision API section in Settings > API, which downloads the model. The model loads into memory on the first request. The same section has the model picker, the CPU or GPU choice, and an unload button.

Before / after

Settings > API on the merge base (left) and on this branch with the Decision API switched on (right). The new section shows the Serve requests switch, the model picker (Multilingual, not downloaded, 678 MB, with a Download button), and the CPU / GPU choice. GET /api/settings/systemone returns 404 on the base and 200 here, and it is off by default.

Decision API settings, before and after

@NilayYadav

NilayYadav commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator Author

Before/after UI evidence

This compares merge base be072ee2ff0334c95d300cd5c522fd9df3b1023f with 67bc36ff5e846d2a4fe71e761cd3bed89dbe2fcc. Each side ran a real Studio installed with install.sh --local from its own ref, with its own home, HF cache and port, in Chromium at 1280×1000 on Modal (CPU). Nothing was mocked: the AFTER side downloaded the real convaiinnovations/laya checkpoint and answered a real request. The head is now 82a5898. Since then, main was merged in (one trivial conflict in the hub export list) and small status fixes landed. None of them change what this flow shows.

Steps: open Settings › API as the owner, turn on Serve requests, wait for the download, send the example below to POST /v1/systemone, then read the section again and press Unload.

Measured Before After
Decision API section in Settings › API absent present, off by default
GET /api/settings/systemone 404 200
POST /v1/systemone while off 405 404, "The Decision API is off. The Studio owner can turn it on in Settings > API."
Download after switching on n/a laya-multilingual, 678,201,636 bytes, 5 files
POST /v1/systemone after the download n/a 200 (12.1 s including the load, 0.12 s warm, repeat identical)
Section after that request n/a "Loaded on CPU" + Unload; Unload clears loaded_model

Real answers for "I was charged twice this month." (model: jev-latest):

  • billing (noul): 0.9165
  • team (choice billing/technical): billing, probabilities 1.0 / 0.0
  • urgency (score, 3 levels): 1.238, probabilities 0.0754 / 0.6113 / 0.3133
  • usage.input_tokens: 107

Settings › API

Settings API tab: no Decision API section before, section loaded on CPU after one request

The section through the flow: off, downloaded, first request served

Decision API section off, then downloaded, then loaded on CPU

Checks

  • studio/backend/tests/test_systemone.py passed in the same container using the real checkpoint (SYSTEMONE_TEST_LAYA set): 58 passed, 0 skipped.
  • Codex review of 82a5898: "Didn't find any major issues."
  • Upstream CI on 82a5898 was still running when this was updated. On aee6a4b, every red is one main shows on its own: Backend CI has failed on main's last six commits (legacy-upgrade sim and chat, font-scale and update-banner source contracts), Frontend bundle sanity has failed on main since 76846f4, and Windows Chat UI fails on main at 9fce748. On 477e235 (two small backend commits earlier), 24 jobs passed. Two failures came from this PR, and both are fixed in 67bc36f: the desktop-auth test did not stub the new router, and the laya install subprocess had no encoding. The other reds on that run are not from this PR:
    • The studiobench parity job reported chat-thread differences only. The base-against-itself control shows the same drift.
    • Composer on Safari failed with a Selenium session timeout.
    • Local Agent Guides connection (dsh) failed; this PR changes nothing it runs.
  • Fork CI on current upstream main 0065fad plus aee6a4b: 18 workflows passed. They include Unsloth API, GGUF and Tauri, Windows API/GGUF/Update, MLX on Mac M1, Lint, Security audit and Wheel. The reds are not caused by this PR:
    • Frontend bundle sanity: main at 0065fad is already over the startup budget on its own (5,601.9 KB raw against 5,595.7 KB, measured on the same build). This PR adds 2.1 KB raw and no new startup chunk.
    • The Update, Chat UI and studio source-assertion jobs fail on files this PR does not change: --ui-font-scale, the update banner, chat component source checks and the prebuilt marker. The staged branch is exactly main plus this PR's 31 files.

Review fixes on this branch (each with a regression test that fails before and passes after):

  • A System One load and a Hub download of the same repo now refuse each other, and the registry check runs outside the runtime lock, so the two locks are never taken in opposite orders.
  • Switching to another checkpoint downloads it before unloading the resident model, so a long or failed download no longer takes the API down.
  • /v1/systemone keeps Jev's error shape (422 validation errors, {"detail": {"error_type", "message"}}) instead of Studio's OpenAI envelope.
  • Each question is capped at 20,000 characters before the model lock; the state limit counts characters, not JSON \u escapes.
  • Turning the feature on is refused on Python < 3.10 or without PyTorch, and the route module no longer uses X | Y at import, so Studio still starts on Python 3.9.
  • The section shows "Loaded on" only for the selected model, reports only that model's load errors, still renders when its first settings request fails, and clears a stale install error once laya is present.
  • The section reports a load failure only until its retry window passes, and shows "Loading…" only for the selected model.
  • CI: the desktop-auth test stubs the new router, and the laya install decodes its output as UTF-8.

Lyxot and others added 2 commits September 25, 2026 17:12
The stack gate accepts mlx below 0.24.1, where only mx.metal.clear_cache exists, so calling mx.clear_cache unconditionally broke every Decision API load and unload there.
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 25, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 25, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Review record from the review rounds on the mirror (danielhanchen/unsloth-staging-review#104). All fixes were pushed to this branch directly; the head was approved after round 3.

Round Finding Verdict Where
1 Unload leaves the model's CUDA/XPU/MPS memory reserved in the caching allocator Fixed caefafb (1304 MiB reserved after unload -> 32 MiB on a B200)
1 Keyless callers can name any checkpoint and trigger downloads or swaps Fixed caefafb (403 unless the configured model, same rule as the OpenAI routes)
1 Settings save and model-load admission are not atomic Not changed needs a request inside one settings write; the wider claimed-but-not-started gap is closed in e137681
1 Load errors return raw paths to API clients Not changed only reaches callers the owner authorised; hardening, not correctness
2 CPU fallback after GPU OOM leaves the GPU cache reserved Fixed 6e76479
2 Register GPU loads with the shared GPU arbiter Not changed GPU is an explicit opt-in; RAG embeddings and native audio do not register either
3 No major issues Approved 6e76479

Also on this branch: e137681 (refuse unload while a load is claimed but its thread has not started), 517fc71 (merge of main), and two comment-only passes.

@danielhanchen
danielhanchen merged commit a838acf into unslothai:main Sep 25, 2026
73 of 79 checks passed
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
* Studio: serve the Jev API locally with Laya

* Studio: keep Decision API loads off managed downloads and serving during switches

* Studio: probe downloads outside the Decision API state lock and gate it on Python 3.10

* Studio: simplify Decision API settings and explain the missing laya package

* Studio: install laya automatically for the Decision API

* Studio: tighten the Decision API model row and move the section below the usage examples

* Studio: cap Decision API question text before it reaches the model lock

* Studio: refuse the Decision API without torch and only show the selected model as loaded

* Studio: keep the Decision API route importable on Python 3.9 and scope load errors to their model

* Studio: show the Decision API section with its error when settings fail to load

* Studio: stub the systemone router in desktop auth tests and decode the laya install as utf-8

* Studio: clear a stale laya install error once the package is present

* Studio: drop expired Decision API load errors and show Loading only for the selected model

* Studio: tokenize the Decision API state once per request

* Studio: rerank document search with the Decision API model

* Studio: document measured rerank latency

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

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

* Studio: move Laya to CPU when the GPU runs out of memory, like laya's own predict

* Studio: run the Decision API on MLX when GPU is chosen on Apple Silicon

With the device set to GPU on Apple Silicon, laya_runtime now loads the checkpoint through unsloth_zoo.mlx.decision instead of torch MPS, reusing laya's tokenizer, prompt building and calibration so answers match laya on CPU. An unsloth-zoo without the module keeps the MPS path. Evicting or unloading also clears the MLX allocator cache so the memory is returned.

* Studio: wait for the retry load in the hand-install Decision API test

* Studio: move the MLX Decision API model to CPU when it runs out of memory

The MLX path returned before the GPU out-of-memory fallback, so on Apple Silicon an MLX allocation failure became a 500 on every retry where MPS used to move to CPU. The resident agent now releases the MLX model and its buffer cache, loads laya on CPU and continues in place; a failed CPU load drops the agent so the next request reloads it. Also read rl_agent_config.json as UTF-8.

* Studio: clear the MLX cache under its older mx.metal name too

The stack gate accepts mlx below 0.24.1, where only mx.metal.clear_cache exists, so calling mx.clear_cache unconditionally broke every Decision API load and unload there.

* Studio: reject Decision API requests that truncate state

* Studio: center the Decision API device dropdown on its row

* Studio: drop Studio from the Decision API device description

* Studio: allow MLX 0.31+ precision in the MLX Decision API test

* Studio: remove Laya document search reranking

* Studio: trim Decision API comments

* Studio: refuse Decision API unload while a load is claimed but not yet started

* Studio: free cached device memory on Decision API unload and keep keyless callers on the configured model

* Studio: free cached device memory after the Decision API falls back to CPU

* Studio: trim Decision API comments after review

* Studio: let the Decision API placement test pass without a comment between the sections

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Lyxot <[email protected]>
Co-authored-by: wasimysaid <[email protected]>
Co-authored-by: danielhanchen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants