Repository navigation
Studio: overlap streamed block onloads and stop copying a streamed DiT's top-level weights back every forward - #12389
danielhanchen wants to merge 8 commits into
Conversation
The streaming offload tier applied diffusers group offloading with low_cpu_mem_usage=True and record_stream=False. An unpinned host copy is pinned again on the CPU at every onload, and record_stream=False makes every group's offload synchronize the compute stream, so the next block's pin ran with the GPU idle. Pin the streamed DiTs (then the encoders) within the host budget the group tier already uses, off the load path when the caller requested background pins, and keep record_stream on. UNSLOTH_DIFFUSION_STREAMING_PREFETCH=0 restores the previous kwargs.
diffusers block-level offload keeps the weights outside the block lists (embedders, norm_out, proj_out) in a top-level group with no stream: every forward re-uploads them from pageable host memory and every offload copies them back into a fresh host buffer, which inference never needs. Keep one pinned host copy: the onload becomes an async H2D on the compute stream and the offload only re-points the tensors. VRAM is unchanged. Skipped for torchao weights and when the copy does not fit the pinnable host RAM. UNSLOTH_DIFFUSION_PIN_TOP_GROUP=0 turns it off.
…aded A .to() conversion or an adapter fused on the host swaps the tensor the top-level group points at while it sits offloaded. The next onload now pins and uploads what is there instead of the stale copy.
…he pin override Round each tensor up to a power of two when checking the host budget, as _module_host_mib does, and skip the pinned top-level copy when UNSLOTH_DIFFUSION_GROUP_OFFLOAD_PIN=0 asks for no pinning.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4a12384086
ℹ️ 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".
| if use_stream and "record_stream" in params: | ||
| kwargs["record_stream"] = False | ||
| kwargs["record_stream"] = prefetch |
There was a problem hiding this comment.
Gate record-stream prefetch on the available VRAM
On low-VRAM CUDA systems where refine_memory_plan_for_components selects OFFLOAD_STREAMING, this now enables record_stream by default even though the planner only verifies that non-streamed resident components fit; it does not reserve space for the previous group's recorded allocation while later groups are prefetched. A model with sufficiently large blocks can therefore fit the former synchronized streaming path but OOM under this path, defeating the fallback specifically intended for components larger than the device budget. Please gate this optimization on an estimated overlapping-group peak or retain record_stream=False when that headroom cannot be established.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not reproducible. A block held by record_stream is only deferred: on a failed allocation the CUDA caching allocator runs release_cached_blocks, which calls synchronize_and_free_events before declaring OOM (c10/cuda/CUDACachingAllocator.cpp, malloc fallback chain and release_cached_blocks in v2.11.0), so pressure costs a sync, not an OOM. Measured on FLUX.1-schnell 1024px streaming with set_per_process_memory_fraction: at a 2.0 GiB cap both the old synchronized path and this path render (peak reserved 2.00 vs 1.99 GiB, bit-identical latents); at 1.9 GiB both OOM on the same 52 MiB request with 1.81 GiB allocated. The OOM point is unchanged.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4a12384086
ℹ️ 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".
| def offload_() -> None: | ||
| for tensor, pinned in host.items(): | ||
| tensor.data = pinned |
There was a problem hiding this comment.
Preserve mutations to top-level buffers during offload
For a transformer whose forward updates a top-level buffer, this replacement offload_ discards the GPU-side update by repointing the buffer at the original pinned snapshot. Diffusers' original streamless offload copies tensors back to CPU, while this helper includes both parameters and buffers in host; consequently counters, running statistics, or inference caches reset after every forward and later outputs can be incorrect. Restrict the no-copy-back optimization to immutable parameters or otherwise preserve updated buffers.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
No reachable case. Inference never mutates a registered top-level buffer in place: across diffusers' transformers, embeddings and normalization modules (27 register_buffer sites) no forward writes one with copy_/fill_/add_ or index assignment; the forward-time caches that do exist (Wan Animate rope_freqs_cache / block_masks, GLM-Image k_caches) are plain dicts and lists, not buffers, so they are never in the pinned map. A buffer reassigned to a new tensor is likewise not in the map and stays where the forward put it. Studio's int8 weight buffers are read-only.
…iT's top-level group
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9d31daaaf3
ℹ️ 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".
| if use_stream and offload_type == "block_level": | ||
| _pin_top_level_group(module, logger, top_reserved_mib) |
There was a problem hiding this comment.
Track aggregate top-level pins across multiple DiTs
In a dual-DiT pipeline, such as one exposing transformer_2 or unconditional_transformer, this call runs once per block-level module with the same top_reserved_mib; a successful pin for the first DiT is never added to the reservation before evaluating the next. Consequently, when the combined DiTs do not qualify for planned pinning, two top groups that each fit individually can collectively exceed the host pin budget and cause swapping or allocation failure. The encoder-reservation fix does not cover this case; use a shared committed-pin total and update it after each successful top-group pin.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Already tracked. Each top-group pin is made immediately (pin_memory on the load path, never deferred), and _pin_top_level_group re-reads the budget from live available host memory (_pin_budget_mib -> _available_system_memory_mib) on every call, so the second DiT's check already sees the first DiT's pinned pages. reserved_mib only covers pins that have not happened yet (the encoders, applied after the DiTs). Measured here: releasing a 4096 MiB pinned tensor returns 3175 to 4228 MiB of budget across three runs.
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Problem
When a diffusion model streams its DiT from host RAM, the copy of block i+1 is meant to overlap block i's compute on a side stream. I profiled this on Colab L4 and A100 with the VRAM budget capped so the planner picks the streamed tiers. The group tier does hide the copy, but two things waste time:
low_cpu_mem_usage=True, so every onload re-pins the block on the CPU.record_stream=False, so each group's offload synchronizes the compute stream, and that pinning runs with the GPU idle.norm_out,proj_out) into one top-level group with no stream.Changes (
diffusion_memory.py)_streamed_pin_plan).record_streamis on, so an offload no longer drains the compute stream.UNSLOTH_DIFFUSION_STREAMING_PREFETCH=0._pin_top_level_group:UNSLOTH_DIFFUSION_GROUP_OFFLOAD_PIN=0asks for no pinning.UNSLOTH_DIFFUSION_PIN_TOP_GROUP=0.Results
Colab L4 and A100-SXM4-40GB, torch 2.11.0, diffusers 0.41.0.dev0, transformers 5.5.0, torchao 0.17.0. Pinned host-to-device copies measured 12.35 GB/s on both. Each arm runs in a fresh process, interleaved on the same card. The cap patches the memory the planner and the allocator see.
From the profiler traces:
Costs
record_streamon, the CPU runs further ahead, so peak reserved VRAM grows (Wan2.2-5B under a 12 GiB cap: 5.7 to 9.3 GiB). Allocated memory is unchanged, and the group tier already behaves this way. No OOM was seen in the capped runs.Tests
test_diffusion_memory.pyadds or changes 13 tests, some of which run on a GPU. They cover:record_streamon;mainand pass here.Not covered