Skip to content

Studio: cut polled-probe cost and release memory sooner - #14595

Merged
danielhanchen merged 9 commits into
mainfrom
studio-backend-perf-memory
Oct 22, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
studio-backend-perf-memory

Conversation

@shimmyshimmer

Copy link
Copy Markdown
Member

What changes for users

Backend-only. Less work per /api/system poll and per training poll, fewer stalls of the server's event loop, steadier readings, and memory freed sooner.

Lag and CPU

  • Vulkan hosts no longer start a Python process on every system poll. On AMD or Intel machines running the Vulkan llama.cpp build, /api/system re-ran the Vulkan device probe (a new interpreter plus a VkInstance) about every 10 s while the Live Monitor or Settings > Resources was open, alongside generation on the same GPU. The inventory is now cached for 30 s for display reads only. Fit, placement, refresh_memory, model loads and GPU memory events still read fresh, and an empty result is never cached.
  • amd-smi is cached for 3 s on display paths, the same rule nvidia-smi already follows, and concurrent pollers share one run. Fit reports stay uncached.
  • Training history reads no longer block the event loop. The runs list (polled every 5 s while a run is active), run detail and metrics hit SQLite directly inside async handlers; they now run in a worker thread, so a busy database can't freeze chat streaming.
  • macOS reads the AGX stats once per refresh instead of twice (a 1.5 s reuse window for display reads only).

More accurate readings

  • CPU %: psutil.cpu_percent(None) measures since the previous call from anyone, so it read 0.0 right after start and jumped around when several pollers interleaved. It is now primed at startup and sampled at most once per second.
  • Apple Silicon GPU telemetry: the Live Monitor's GPU utilization, temperature and power were hard-coded to empty on Macs, though the data was already being read. Now filled, best effort, None on failure. On an M3 Max: utilization 0-9%, temperature ~54 C, power 0-0.2 W at idle.
  • Process memory: new additive field memory.process_tree_used_mb that also counts child processes (llama-server, the training worker). process_used_mb is unchanged.

Memory

  • Unloading the RAG embedder now releases its GPU memory. Dropping the SentenceTransformer left its blocks reserved in the torch allocator, so the space still looked taken to the next model load. It now clears the GPU cache after a real release.
  • Uploads stop reading at the limit. Data Recipe document uploads and video reference uploads were read fully into RAM before the size check. They now read at most limit + 1 bytes; error messages are unchanged.

Smaller

  • A broken torch install (installed but failing to import) is retried and logged once per detection pass instead of on every poll.
  • Thread safety: a lock around the Apple SMC/IOReport readers. gpu_power_w releases its previous sample, and with /api/system now reading it too, two threads could free it twice.

How it was verified

  • Tests: new tests/test_polled_probe_costs.py (14 tests) covers each change with mocked subprocess, psutil and time, plus new cases in test_training_history_delete.py and test_data_recipe_seed.py. 222 pass across the new and closest existing files. ruff check is clean.
  • Live: a backend from this branch on an M3 Max serves the Apple telemetry and the new field. Normal polls take about 9 ms; refresh_memory=true takes about 108 ms.
  • Pre-existing failures: the wider run shows failures in test_gpu_selection, test_memory_estimate, test_video_backend and test_startup_defers_torch. They fail identically on an unmodified checkout on macOS and are unrelated to this change.

Not tested here

  • No Vulkan, AMD or NVIDIA hardware on this machine. Those paths are covered by mocked tests only; staging CI covers Linux and Windows.

shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 22, 2026
@shimmyshimmer

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T11:49:49.580351Z 023ea01 Manual request
🔒 Security Review ✅ Completed 2026-10-22T06:24:23.981502Z e4450f3 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: e4450f31b2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@shimmyshimmer

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: e4450f31b2

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 22, 2026
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 22, 2026
@danielhanchen danielhanchen self-assigned this Oct 22, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 023ea01b46

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit that referenced this pull request Nov 22, 2026
… test (order-dependent red) (#18867)

## Summary


`studio/backend/tests/test_warm_window_review_fixes.py::test_an_absent_torch_is_still_just_absent`
fails whenever torch has not been imported earlier in the process. It
failed on the staging cross-platform leg for #18727 (ubuntu-latest), and
it fails on `main` when the file runs on its own:

```
AssertionError: a --no-torch install would be reported as a detection failure
assert 'stale' is None
 +  where 'stale' = <module 'utils.hardware.hardware'>.TORCH_IMPORT_ERROR
```

`_has_torch()` (since #14595) remembers a failed import for the current
detection epoch: while `TORCH_IMPORT_ERROR` is set,
`_TORCH_IMPORT_FAILED_EPOCH == DETECTION_EPOCH` and torch is not in
`sys.modules`, it returns `False` without trying again. The code is
right. The tests are wrong:
`test_a_broken_torch_install_is_not_reported_as_a_host_without_a_gpu`,
just above, records a failure (setting both globals) and then restores
only `TORCH_IMPORT_ERROR`. The epoch it leaves behind makes the next
`_has_torch()` short-circuit, so the absent-torch test never reaches the
import it checks. In the full CI run some earlier test has usually
imported torch, which disables the short-circuit, so this has been order
dependent rather than always red.

## Fix

An autouse fixture in that file sets `TORCH_IMPORT_ERROR` and
`_TORCH_IMPORT_FAILED_EPOCH` to `None` through `monkeypatch`, so every
test starts with no cached failure and both are restored afterwards. No
production code changes.

## Testing

- `studio/backend/tests/test_warm_window_review_fixes.py` on its own:
100 passed (`main`: 1 failed, the test above).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants