[PR #208] [CLOSED] server: optional idle unload, /unload and /load, a free-VRAM guard (share the GPU with other programs) #2507

Closed
opened 2026-11-04 02:00:48 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/208
Author: @bytethecookie
Created: 9/30/2026
Status: ❌ Closed

Base: main ← Head: idle-unload


📝 Commits (3)

  • ae40c71 server: optional idle unload, POST /unload and /load, a free-VRAM guard and a before_load hook
  • a5f49b5 server: the image encoder unloads and loads with the engine; docs for sharing the GPU
  • a57f1f8 server: review fixes - load() counts as activity, unload() asks the engine to QUIT first, docs: the VRAM check looks at the first GPU

📊 Changes

3 files changed (+393 additions, -14 deletions)

View changed files

📝 docs/DETAILS.md (+21 -0)
📝 serve/server.py (+199 -13)
📝 serve/test_server.py (+173 -1)

📄 Description

What

Optional support for sharing the GPU with other programs: Strata can unload the model when it is idle and load it again on the next request. Everything is off by default — without the new options the server behaves exactly as before.

Option Config key What it does
--idle-unload SECONDS idle_unload_s unload after that long without requests; the next request loads it again
--min-free-vram-mib N min_free_vram_mib load an unloaded model only when that much VRAM is free (waits up to 15 s), else 503 "the GPU is in use by another program"
--before-load CMD before_load (string or list) a command run before the model is loaded again, e.g. to unload another server's model

Plus POST /unload (409 while a request is running) and POST /load; /health gains "loaded", /v1/models lists the model with status unloaded (like llama.cpp's router, building on #122's endpoints), /props sets is_sleeping, and /metrics reports the state unloaded.

Why

Strata fills all free VRAM and holds it until the window is closed. On a PC that also games, runs ComfyUI or another model server, that means stopping and starting Strata by hand. With these options it can stay up permanently next to them.

How

  • Unloading ends the engine process (and the image encoder, when images are on), so VRAM and pinned RAM go straight back. Loading reuses the existing restart() path from #27 — the "engine died, start it again" check in run() now goes through one ensure_loaded() that also handles a deliberate unload.
  • The image encoder is started again before the engine, as at a normal start, so a GPU encoder takes its VRAM before the engine sizes its expert cache. Its cache of encoded images is kept.
  • Requests load the model before the answer starts, so a busy GPU is a clean JSON 503 rather than an error mid-stream.
  • Free VRAM is read through the existing _Nvml class in telemetry.py; if it can't be read, nothing is refused.
  • Unload only happens between requests (it takes the request FIFO non-blocking and checks busy/queued).

Measured

RTX 5060 Ti 16 GB, i5-12400F, 48 GB DDR4, Linux (Docker), Q2_0 with --mmap-experts, files in the OS cache:

  • POST /unload returns after ~0.33 s with the VRAM free (15.7 GB -> 0.76 GB used)
  • a text request to an unloaded model answers after 4.6 s
  • a request with a picture (encoder on the CPU) answers after 14.7 s
  • with 8 GB of VRAM held by another process and --min-free-vram-mib 11000: 503 after the 15 s wait, model stays unloaded

I run it this way next to llama.cpp's router and ComfyUI: --idle-unload 120 --min-free-vram-mib 11000 --before-load "<script that unloads llama-server's models>".

Tests

python -m unittest serve.test_server: the 39 existing tests pass unchanged, plus 9 new ones in SharingTheGpu (unload then reload on the next request, /load, refused while busy, idle unload, the VRAM guard refusing and then loading, unreadable VRAM never refusing, before_load running first, the encoder unloading and starting first, off by default).

Notes for review

  • POST /unload and /load follow the API-key check like the other POST routes but not the _own_page check that /settings has, since other programs on the machine are the intended callers. The worst a cross-site request could do is unload an idle model.
  • Not tested on Windows or with the GPU image encoder or multi-GPU — I only have the setup above. terminate() on Windows is a hard kill, which should be fine for a process between requests, but it is untested.
  • Docs: a short section in docs/DETAILS.md. Setup does not ask about any of this; it seemed better left as an advanced option.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/208 **Author:** [@bytethecookie](https://github.com/bytethecookie) **Created:** 9/30/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `idle-unload` --- ### 📝 Commits (3) - [`ae40c71`](https://github.com/Niko1221/Strata/commit/ae40c71d502e730953dad380aa604fe2ec5c75ac) server: optional idle unload, POST /unload and /load, a free-VRAM guard and a before_load hook - [`a5f49b5`](https://github.com/Niko1221/Strata/commit/a5f49b5c03e8846b40da18d31a94f4aac9acb6c2) server: the image encoder unloads and loads with the engine; docs for sharing the GPU - [`a57f1f8`](https://github.com/Niko1221/Strata/commit/a57f1f844f3ce1891aa541d1e73753c8c9fa30c9) server: review fixes - load() counts as activity, unload() asks the engine to QUIT first, docs: the VRAM check looks at the first GPU ### 📊 Changes **3 files changed** (+393 additions, -14 deletions) <details> <summary>View changed files</summary> 📝 `docs/DETAILS.md` (+21 -0) 📝 `serve/server.py` (+199 -13) 📝 `serve/test_server.py` (+173 -1) </details> ### 📄 Description ## What Optional support for sharing the GPU with other programs: Strata can unload the model when it is idle and load it again on the next request. Everything is **off by default** — without the new options the server behaves exactly as before. | Option | Config key | What it does | | --- | --- | --- | | `--idle-unload SECONDS` | `idle_unload_s` | unload after that long without requests; the next request loads it again | | `--min-free-vram-mib N` | `min_free_vram_mib` | load an unloaded model only when that much VRAM is free (waits up to 15 s), else `503` "the GPU is in use by another program" | | `--before-load CMD` | `before_load` (string or list) | a command run before the model is loaded again, e.g. to unload another server's model | Plus `POST /unload` (`409` while a request is running) and `POST /load`; `/health` gains `"loaded"`, `/v1/models` lists the model with status `unloaded` (like llama.cpp's router, building on #122's endpoints), `/props` sets `is_sleeping`, and `/metrics` reports the state `unloaded`. ## Why Strata fills all free VRAM and holds it until the window is closed. On a PC that also games, runs ComfyUI or another model server, that means stopping and starting Strata by hand. With these options it can stay up permanently next to them. ## How - Unloading ends the engine process (and the image encoder, when images are on), so VRAM and pinned RAM go straight back. Loading reuses the existing `restart()` path from #27 — the "engine died, start it again" check in `run()` now goes through one `ensure_loaded()` that also handles a deliberate unload. - The image encoder is started again **before** the engine, as at a normal start, so a GPU encoder takes its VRAM before the engine sizes its expert cache. Its cache of encoded images is kept. - Requests load the model before the answer starts, so a busy GPU is a clean JSON `503` rather than an error mid-stream. - Free VRAM is read through the existing `_Nvml` class in `telemetry.py`; if it can't be read, nothing is refused. - Unload only happens between requests (it takes the request FIFO non-blocking and checks `busy`/`queued`). ## Measured RTX 5060 Ti 16 GB, i5-12400F, 48 GB DDR4, Linux (Docker), Q2_0 with `--mmap-experts`, files in the OS cache: - `POST /unload` returns after ~0.33 s with the VRAM free (15.7 GB -> 0.76 GB used) - a text request to an unloaded model answers after 4.6 s - a request with a picture (encoder on the CPU) answers after 14.7 s - with 8 GB of VRAM held by another process and `--min-free-vram-mib 11000`: `503` after the 15 s wait, model stays unloaded I run it this way next to llama.cpp's router and ComfyUI: `--idle-unload 120 --min-free-vram-mib 11000 --before-load "<script that unloads llama-server's models>"`. ## Tests `python -m unittest serve.test_server`: the 39 existing tests pass unchanged, plus 9 new ones in `SharingTheGpu` (unload then reload on the next request, `/load`, refused while busy, idle unload, the VRAM guard refusing and then loading, unreadable VRAM never refusing, `before_load` running first, the encoder unloading and starting first, off by default). ## Notes for review - `POST /unload` and `/load` follow the API-key check like the other POST routes but not the `_own_page` check that `/settings` has, since other programs on the machine are the intended callers. The worst a cross-site request could do is unload an idle model. - Not tested on Windows or with the GPU image encoder or multi-GPU — I only have the setup above. `terminate()` on Windows is a hard kill, which should be fine for a process between requests, but it is untested. - Docs: a short section in `docs/DETAILS.md`. Setup does not ask about any of this; it seemed better left as an advanced option. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-04 02:00:48 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#2507
No description provided.