Skip to content

feat(server): add idle TTLs for model weights - #2406

Merged
leejet merged 1 commit into
masterfrom
feat/server-weight-ttl
Nov 23, 2026
Merged

leejet merged 1 commit into
masterfrom
feat/server-weight-ttl

Conversation

@leejet

@leejet leejet commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • 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.

Related Issue / Discussion

Fix #2401.

Additional Information

N/A

Checklist

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.

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

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.

Changes: Idle release

Layer / File(s) Summary
DGL release operations
include/stable-diffusion.h, src/dgl/*, src/model_manager.*, docs/guides/c_api.md
The DGL API adds idle staging release and model unloading. ModelManager releases eligible staging blocks, and Context unloads cached model resources.
Server TTL options
examples/server/runtime.*, examples/server/model_catalog.cpp
The server adds --weight-staging-ttl and --weight-residency-ttl. Both default to -1; the parser accepts values of -1 or greater.
Idle release scheduling
examples/server/async_jobs.*, examples/server/routes_models.cpp, examples/server/README.md
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.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Feature · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant RuntimeRoutes
  participant AsyncJobManager
  participant DGLContext
  participant ModelManager
  RuntimeRoutes->>AsyncJobManager: Record synchronous job completion
  AsyncJobManager->>DGLContext: Request staging release or model unload at deadline
  DGLContext->>ModelManager: Release eligible GPU staging blocks
Loading

Possibly related issues

Possibly related PRs

Merge Risk: Minimal · up to 1f715

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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 Passed 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 Passed 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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • 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.

@leejet
leejet merged commit 3b12781 into master Nov 23, 2026
11 checks passed
@leejet
leejet deleted the feat/server-weight-ttl branch November 23, 2026 17:19
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.

[Bug] sd-server keeps offloaded weights on the GPU between jobs since 1117 (#2321), and Qwen Image 2.1 edits run ~40% slower

1 participant