You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(server): add idle TTLs for model weights - #2406
Add --weight-staging-ttl to release idle GPU copies of RAM-backed weights while retaining the originals. This covers explicit CPU placement and auto-fit. The next request uploads the weights from RAM.
Add --weight-residency-ttl to unload cached model components from RAM and VRAM. The shared DGL context keeps its configuration and file-access callbacks so later requests can reload components on demand.
Both options use -1 to disable release by default, 0 for immediate release when idle, and positive integers for idle seconds. Running and queued jobs defer release. Status and progress polling leave the deadlines intact. When both deadlines are due, the server unloads the model once.
If this PR adds or changes model support, I have added or updated its usage guide and relevant model index/README entries following the model documentation requirements.
Summary by CodeRabbit
New Features
Added controls for automatically releasing idle GPU staging memory and unloading cached model components after configurable periods. Both settings are disabled by default.
Added API options to manually release idle staging memory or unload cached models. Models reload as needed; these operations return a busy status during generation or script execution.
Documented the new server settings, timing behavior, and API options.
No actionable comments were generated in the recent review.
Walkthrough
The DGL context API adds operations to release idle GPU staging and unload cached models. The server adds TTL options and schedules these operations after jobs complete, while deferring release when jobs are active.
AsyncJobManager records deadlines after context jobs complete and processes due releases while idle. Synchronous jobs defer release. The README documents the options and timing rules.
Adds opt-in idle weight release options for the server, both disabled by default, so existing deployments are unaffected. No concrete merge-blocking risk was found.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title "feat(server): add idle TTLs for model weights" matches the main change. The PR adds --weight-staging-ttl and --weight-residency-ttl to sd-server, which release idle weights after a configur…
Description check
The description follows the repository template. It has Summary, Related Issue / Discussion, Additional Information, and Checklist sections. The Summary explains both options, their defaults (-1, 0, p…
Linked Issues check
Directly linked issue #2401 asks for either (a) an option that releases offloaded GPU weight copies and per-run caches when a job ends, or (b) making that behavior the default for --offload-to-cpu i…
Out of Scope Changes check
All changes connect to the TTL release feature requested in #2401. The new sd_dgl_ctx_release_staging and sd_dgl_ctx_unload_models C API functions (include/stable-diffusion.h, src/dgl/dgl.cpp) pro…
Full details: Title check
Explanation
The title "feat(server): add idle TTLs for model weights" matches the main change. The PR adds --weight-staging-ttl and --weight-residency-ttl to sd-server, which release idle weights after a configurable idle time. The title is concise and uses a conventional commit prefix. It does not name the two distinct TTL options, but it accurately summarizes the primary change.
Full details: Description check
Explanation
The description follows the repository template. It has Summary, Related Issue / Discussion, Additional Information, and Checklist sections. The Summary explains both options, their defaults (-1, 0, positive seconds), deferral rules for running and queued jobs, and the single unload when both deadlines are due. It links issue #2401. Additional Information is filled with N/A. Both checklist items are checked. The model-documentation checklist item does not clearly apply to this server change, but the template includes it, so this is not a gap. The description does not mention the docs/guides/c_api.md changes or the new C API functions, but those are secondary to the server feature.
Full details: Linked Issues check
Explanation
Directly linked issue #2401 asks for either (a) an option that releases offloaded GPU weight copies and per-run caches when a job ends, or (b) making that behavior the default for --offload-to-cpu in sd-server. The PR implements option (a). --weight-staging-ttl releases idle GPU copies of RAM-backed weights while keeping the RAM originals. A value of 0 releases them immediately when idle, and running or queued jobs defer release. --weight-residency-ttl unloads cached model components from RAM and VRAM, which covers the per-run cache request. The default is -1 (disabled), so alternative (b) is not met. The issue allows either alternative, so this does not fail the check. The Qwen Image 2.1 slowdown is a reported symptom with an unconfirmed explanation (prefix cache). The objectives state it is not a confirmed requirement, so it is not assessed. Evidence is the PR summary and raw summary of changes (runtime.cpp option parsing, async_jobs.cpp deadline handling, model_manager.cpp release_idle_gpu_staging, dgl.cpp and stable-diffusion.h C API additions). I did not inspect the diff directly, so the exact release behavior rests on the summary.
Full details: Out of Scope Changes check
Explanation
All changes connect to the TTL release feature requested in #2401. The new sd_dgl_ctx_release_staging and sd_dgl_ctx_unload_models C API functions (include/stable-diffusion.h, src/dgl/dgl.cpp) provide the server's release hooks. Context::unload_models (src/dgl/context.cpp/.h) is a refactor that the residency unload uses. The protect_prefetched_states and release_idle_gpu_staging changes (src/model_manager.cpp/.h) implement the staging release. The SynchronousContextJob (routes_models.cpp) and AsyncJobManager deadline changes (async_jobs.cpp/.h) implement job-based deferral. The runtime.cpp and runtime.h changes add the options. The documentation changes (docs/guides/c_api.md, examples/server/README.md) describe these options and functions. The whitespace-only edits in model_catalog.cpp, api.cpp, and generation.h do not change behavior. They are minor, incidental edits and do not indicate unrelated work. No unrelated feature is evident in the summary.
Finishing Touches
Generate unit tests (beta)
Commit to this branch
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: ee0ee6ba-7198-40e4-9272-12ab2dcd7c4b
Commits
Reviewing files that changed from the base of the PR and between 8187e47 and 1f71578.
Files selected for processing: 16
docs/guides/c_api.md
examples/server/README.md
examples/server/async_jobs.cpp
examples/server/async_jobs.h
examples/server/model_catalog.cpp
examples/server/routes_models.cpp
examples/server/runtime.cpp
examples/server/runtime.h
include/stable-diffusion.h
src/api/api.cpp
src/api/generation.h
src/dgl/context.cpp
src/dgl/context.h
src/dgl/dgl.cpp
src/model_manager.cpp
src/model_manager.h
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
--weight-staging-ttlto release idle GPU copies of RAM-backed weights while retaining the originals. This covers explicit CPU placement and auto-fit. The next request uploads the weights from RAM.--weight-residency-ttlto unload cached model components from RAM and VRAM. The shared DGL context keeps its configuration and file-access callbacks so later requests can reload components on demand.-1to disable release by default,0for immediate release when idle, and positive integers for idle seconds. Running and queued jobs defer release. Status and progress polling leave the deadlines intact. When both deadlines are due, the server unloads the model once.Related Issue / Discussion
Fix #2401.
Additional Information
N/A
Checklist
Summary by CodeRabbit