Skip to content

[diffusion] Skip warmup preferred preload when it would OOM - #40761

Merged
mickqian merged 14 commits into
sgl-project:mainfrom
cipheraxat:fix/warmup-component-offload-oom
Oct 5, 2026
Merged

mickqian merged 14 commits into
sgl-project:mainfrom
cipheraxat:fix/warmup-component-offload-oom

Conversation

@cipheraxat

@cipheraxat cipheraxat commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

With dit / text_encoder / vae component-offload, synthetic warmup finalization
can OOM while reloading preferred components via
prepare_for_use → module.to(device), then leave the allocator full so smaller
warmup retries also fail.

ComponentOffloadStrategy.finish_request(..., preferred=True) always tried to keep
preferred components resident after a warmup request. After VAE release, that
optional H2D can still exceed free device memory:

  • non-memory_intensive releases leave reserved allocator blocks that
    mem_get_info still reports as used
  • module.to(device) then fails with CUDA OOM (or partially moves tensors)
  • the failed preload leaves little free memory for the next warmup attempt

Separately, Wan2.2 A14B synthetic warmup only runs the high-noise transformer,
but DenoisingStage → finish_active_use(prefetch_next=True) still prefetched the
co-declared low-noise transformer_2 (~26 GiB) and left it resident through VAE
decode.

Tracked in #40562.

Reproduced with:

sglang serve --model-path Wan-AI/Wan2.2-T2V-A14B-Diffusers \
  --component-residency dit=component-offload \
  text_encoder=component-offload \
  vae=component-offload

Modifications

Warmup preferred path in
component_residency_strategies.py / ComponentOffloadStrategy.finish_request:

  • Keep an already-ready component resident without querying the memory budget
    or clearing the allocator cache. Otherwise, query free memory first; only
    call empty_cache and recheck if the optional preload would not fit.
  • Estimate how much device memory the preload needs (_cpu_module_nbytes),
    counting weights at target_dtype since module.to casts them during the
    copy. If that doesn't fit in free memory minus 1 GiB headroom, skip the copy
    and leave the component offloaded; the next request loads it on demand.
  • If the copy still OOMs, catch torch.OutOfMemoryError / "out of memory",
    call finish_use (moves any partial device tensors back to CPU), and
    empty_cache so startup can continue.
  • Recovery checks all parameters and buffers, including children already on
    the device when a parent parameter remains on CPU after a failed copy.
  • Non-OOM errors still propagate. Non-warmup preferred release behavior is
    unchanged.

Same-stage prefetch cleanup in component_manager.py:

  • finish_active_use prefetches starting at the next stage. Stages call it
    when they are done with their components (e.g. DenoisingStage at the end of
    the loop), so a later same-stage declaration such as transformer_2 on a
    warmup that only ran the high-noise expert is never H2D'd.
  • end_stage releases same-stage prefetches that were never begun (strategies
    that can prefetch while another use is active), including when no use is
    active anymore. A component that is still the active use is left alone.
  • Mid-stage end_use prefetch is unchanged.

Deliberately unchanged:

  • Non-warmup residency policy (partial-copy cleanup also applies to finish_use)
  • Which components are marked preferred_ready_after_request
  • Shared-pool next-stage headroom (SHARED_POOL_NEXT_STAGE_HEADROOM_BYTES)
  • Dual-expert requests that begin_use both transformers inside the stage

Tests

Validated on one NVIDIA H200, Python 3.12, PyTorch 2.13.0+cu130:

python3 -m pytest -q \
  python/sglang/multimodal_gen/test/unit/test_component_residency.py \
  python/sglang/multimodal_gen/test/unit/test_auto_residency.py \
  python/sglang/multimodal_gen/test/unit/test_component_residency_report.py \
  python/sglang/multimodal_gen/test/unit/test_pipeline_residency_manager_install.py

121 passed, including component and snapshot offload, budget skips, cache
reclamation, target-dtype sizing, non-OOM propagation, and same-stage prefetch
cleanup. CUDA recovery tests exercise real Module.to copies with an injected
mid-copy OOM, check all recovered weights and buffers, and run the next forward.

The expanded warmup tests against the earlier PR implementation (1dde33a4)
reported 10 failures and 11 passes, exposing unnecessary cache clears and the
parent-CPU/child-CUDA rollback gap. They all pass with the follow-up fix.

Full Wan2.2-T2V-A14B startup/inference on L20 and startup latency benchmarks
have not been rerun. The injected-OOM tests do not exhaust physical GPU memory.

Accuracy Tests

N/A: residency / memory-management only; no model output change when preload
succeeds. When preload is skipped or recovered, the next real request loads the
component on demand (same as a cold start for that component).

Speed Tests and Profiling

N/A for correctness. Warmup may be slightly slower when preload is skipped
(on-demand H2D on the first real request). Successful preloads keep today's
behavior. Avoiding the unused transformer_2 prefetch reduces warmup device
traffic on dual-DiT pipelines.

Checklist

  • Format your code according to the Format code with pre-commit.
  • Add unit tests according to the Run and add unit tests.
  • Update documentation: no user-facing docs; behavior is documented in code comments.
  • Provide accuracy and speed benchmark results: N/A (see above).
  • Follow the SGLang code style guidance.

CI States

Latest PR Test (Base): ✅ Run #37272621265
Latest PR Test (Extra): ❌ Run #37272621075
Latest PR Test (AMD ROCm 10): ❌ Run #37272621171

@cipheraxat

Copy link
Copy Markdown
Contributor Author

/tag-and-rerun-ci

@yummyaa

yummyaa commented Sep 24, 2026

Copy link
Copy Markdown

@cipheraxat Thanks for working on this. From reading the patch, I think it addresses the immediate failure reported in #40562: a preferred warmup preload is now skipped when its weights do not fit, and a partial move is rolled back if the copy still OOMs.

I still have two concerns before considering the original problem fully resolved.

1. The preferred preload policy is still not user-configurable

The patch deliberately keeps the existing preferred_ready_after_request selection unchanged. These flags are currently hard-coded in the stage ComponentUse declarations, so users cannot explicitly choose which components should remain ready after warmup.

The budget check also uses a fixed _WARMUP_PRELOAD_MARGIN_BYTES. This verifies that the component weights fit at warmup finalization, but it does not guarantee that enough memory remains for activations, attention workspaces, and other allocations during the first real request. A component may therefore pass the preload check but still cause the following inference to OOM.

Would it be possible to expose a user-facing policy that can:

  • disable all warmup preferred preloads;
  • explicitly select the components to preload; and
  • optionally configure the required memory headroom?

For example, --warmup-preload-components auto|none|text_encoder,dit,vae could preserve the current behavior under auto, while allowing memory-constrained deployments to select a safe set explicitly.

2. The unused transformer_2 prefetch path is still present

Wan2.2 A14B synthetic warmup only executes the high-noise transformer. The low-noise transformer_2 is not used by the warmup request.

However, when denoising finishes, finish_active_use(prefetch_next=True) calls _prefetch_next_memory_intensive_use() and moves transformer_2 to the device. Since _prefetch_use() does not create an active use, this component is never consumed or released by a matching begin_use() / finish_use() pair. It remains resident across VAE decoding until the request-end sweep.

This PR only changes the preferred branch in ComponentOffloadStrategy.finish_request(), so it may prevent an additional preferred preload from causing OOM, but it does not prevent the unnecessary 26.62 GiB transformer_2 transfer and residency.

Could the manager either:

  • avoid prefetching a component when there is no remaining use of it in the current request;
  • release a prefetched component if no begin_use() consumes it before the next stage; or
  • expose a component-level prefetch allowlist/disable option?

In this warmup case, I believe transformer_2 should not be loaded at all. If it is prefetched, it should at least be released as soon as the manager determines that the request will not use it.

Would you prefer these two points to be handled in this PR, or tracked in a separate follow-up issue?

@cipheraxat

cipheraxat commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor Author

@yummyaa Thanks for the detailed review, agreed on both points.

Point 2 (unused transformer_2 prefetch): fixed in this PR. DenoisingStage calls finish_active_use() at the end of its loop, which prefetched the next memory-intensive use in the same stage, so Wan2.2 synthetic warmup (high-noise transformer only) still copied transformer_2 to the device. Now:

  • finish_active_use prefetches from the next stage, so an unused co-declared expert is never loaded.
  • end_stage releases same-stage prefetches that were never begun (for strategies that can prefetch while another use is active).

The new tests follow the real call order (begin_use → finish_active_use → end_stage) and fail on the previous code.

Point 1 (user-configurable preload policy / headroom): tracked as a follow-up in #41126, since it adds a new server arg.

Does that split work for you?

@yummyaa

yummyaa commented Sep 28, 2026

Copy link
Copy Markdown

@cipheraxat Thanks for the quick and thoughtful follow-up. Yes, this split works well for me.

The updated handling for the unused transformer_2 prefetch matches the expected behavior: a same-stage component that will not run in the current request should not be loaded, and an unconsumed prefetch should be released at stage exit.

Tracking the user-configurable preferred preload policy and memory headroom separately in #41126 also makes sense, since that introduces new user-facing configuration rather than only fixing the current failure path.

I really appreciate how thoroughly you addressed both concerns and incorporated the feedback. I will continue following both the PR and the follow-up issue and report any additional findings.

@mickqian

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Sep 29, 2026
cipheraxat and others added 10 commits September 29, 2026 22:11
Warmup finalization may optionally reload preferred components; if they
cannot fit, leave them offloaded and recover instead of crashing startup.
Fixes sgl-project#40562.
Drop dtype scaling and storage dedup. The reported Wan path does not
cast on preload, and a plain CPU byte sum is enough to skip a copy
that cannot fit.
VAE release at request end is not memory_intensive, so reserved
allocator blocks can still look used to mem_get_info. Free them before
deciding whether the optional preferred preload fits.
Stage exit was advancing into later co-declared experts (e.g. transformer_2
during Wan2.2 synthetic warmup) via finish_active_use's prefetch. Skip the
remaining same-stage uses and release any prefetched-but-never-begun copies.
… use

DenoisingStage calls finish_active_use() inside the stage, so the earlier
end_stage-only fix never ran on the Wan2.2 warmup path and transformer_2
was still prefetched. Prefetch from the next stage when finishing an active
use, and release unconsumed same-stage prefetches at end_stage even when no
use is active.
Fold the budget-skip and OOM paths into one fallback, filter same-stage
prefetch by stage name instead of index math, and consolidate the tests.
No behavior change.
Some backends expose mem_get_info without implementing it; catch RuntimeError
so the warmup budget check falls back to attempting the optional preload.
module.to(device, dtype=...) widens floating-point weights during the H2D,
so counting host nbytes alone underestimates the copy when target_dtype is
wider than the stored dtype.
@cipheraxat
cipheraxat force-pushed the fix/warmup-component-offload-oom branch from 3a3fb15 to a3d95da Compare September 29, 2026 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diffusion SGLang Diffusion run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants