Skip to content

Studio: pin the kept offload weights on first onload when host RAM allows - #11766

Merged
danielhanchen merged 26 commits into
mainfrom
studio-offload-pin
Sep 25, 2026
Merged

danielhanchen merged 26 commits into
mainfrom
studio-offload-pin

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Stacked on #11764 (base branch studio-offload-keep-cpu).

Summary

With #11764 the whole-model offload no longer copies weights back from the GPU, and the remaining cost per render is the onload. That onload reads pageable host memory, which the GPU copies at 6.6 GiB/s on the B200 box, against 52 GiB/s from page-locked memory.

This PR pins the kept host weights once per component, on the component's first onload, when host RAM allows.

How

_pin_host_weights runs from the wrapped pre_forward, once per module:

  • Chunked: the module's kept weights are packed into page-locked chunks (1 GiB, or the largest weight rounded up to a power of two), so the host allocator's power-of-two rounding does not inflate them. Pinning weight by weight instead reached 50 GiB RSS for FLUX's 31 GiB of weights; chunked it is 35 GiB, the same as stock offload.
  • Swapped in place: each parameter's data and its kept host tensor are pointed at a view into the chunk. The bytes are identical, so output is unchanged.
  • RAM gate: the chunk layout is planned first, and auto (the default) pins only when available host RAM stays above max(4 GiB, 15% of total) after allocating those chunks (their real sizes, not just the weight bytes), so low-RAM machines keep today's behaviour. UNSLOTH_DIFFUSION_OFFLOAD_PIN=0/1 forces it off or on.
  • Skipped weights: tensor subclasses and non-contiguous weights (e.g. channels_last convs) are skipped, so layouts and kernel choices do not change.
  • Failure: a pinning failure (e.g. a WSL pinned-memory cap) keeps the pageable weights, and the chunks allocated before it are released straight away.
  • Host cache: the post-generate host reclaim also empties torch's host allocator cache, so an unloaded model's pinned blocks are returned.

Results (B200, memory_mode="low_vram", speed default, steady renders after the first)

model stock #11764 this PR RSS after, stock vs this PR
FLUX.1-schnell 1024, 4 steps 42.5 s 9.7 s 1.9 to 2.5 s 33.8 vs 35.0 GiB
Z-Image-Turbo 1024, 8 steps 18.5 s 5.6 s 1.7 to 2.0 s 21.5 vs 22.5 GiB
Qwen-Image-2.1 1024, 25 steps 27.4 s 8.5 to 9.0 s 4.8 to 5.7 s 25.2 vs 26.3 GiB
Wan2.2-TI2V-5B 832x480x49, 20 steps 36.9 to 42.4 s 20.2 to 27.9 s (median 25.2) 13.2 to 19.2 s (median 16.6) not measured

Tests

tests/test_diffusion_offload_keep_cpu.py, GPU-gated:

  • weights are pinned on the first onload into shared chunks, with bit-identical output against stock offload;
  • pinning stays off with the switch and when psutil reports too little RAM;
  • a non-contiguous weight keeps its layout and is not pinned;
  • the RAM gate counts the chunks really allocated, so weights that fit the budget are refused when their chunks do not;
  • a failed allocation part way through hands the earlier chunks back.

Results: 14 passed with a GPU; without one, the offload and memory suites pass (107 passed, 5 skipped).

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-25T15:00:26.628065Z c7384b5 Manual request
🔒 Security Review ✅ Completed 2026-09-25T15:02:08.885036Z c7384b5 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review 🔄 Running since 2026-09-24T03:30:31.140183Z aebe844 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 4135f82686

ℹ️ 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".

pull Bot pushed a commit to ShinnChow/unsloth that referenced this pull request Sep 24, 2026
…i#11777)

_free_port bound port 0, closed the socket and passed the number to the
server process to bind later. Another xdist worker could take the port in
between, and the server died with address already in use (seen on the l-r
shard of unslothai#11766). Worse, the readiness probe could then connect to whichever
process now held the port. The server now binds port 0 itself, keeps the
socket and reports the port before serving.

The new test makes every port this process is handed by the OS one that is
already listening; it fails on the old harness and passes here.
…udio-offload-pin

# Conflicts:
#	studio/backend/core/inference/diffusion_memory.py
@unslothai unslothai deleted a comment from danielhanchen Sep 25, 2026
@unslothai unslothai deleted a comment from danielhanchen Sep 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 25, 2026
@unslothai unslothai deleted a comment from danielhanchen Sep 25, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 25, 2026
@unslothai unslothai deleted a comment from danielhanchen Sep 25, 2026
@unslothai unslothai deleted a comment from danielhanchen Sep 25, 2026
@unslothai unslothai deleted a comment from danielhanchen Sep 25, 2026
@unslothai unslothai deleted a comment from danielhanchen Sep 25, 2026
@unslothai unslothai deleted a comment from danielhanchen Sep 25, 2026
@unslothai unslothai deleted a comment from danielhanchen Sep 25, 2026
@shimmyshimmer

Copy link
Copy Markdown
Member

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: c7384b5459

ℹ️ 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".

danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Sep 25, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: c7384b5459

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 25, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 25, 2026
Base automatically changed from studio-offload-keep-cpu to main September 25, 2026 17:54
# Conflicts:
#	studio/backend/core/inference/diffusion.py
#	studio/backend/core/inference/diffusion_memory.py
#	studio/backend/core/inference/video.py
#	studio/backend/tests/test_diffusion_backend.py
#	studio/backend/tests/test_diffusion_offload_keep_cpu.py
#	studio/backend/tests/test_video_backend.py
@danielhanchen
danielhanchen merged commit b0af0ea into main Sep 25, 2026
34 of 37 checks passed
@danielhanchen
danielhanchen deleted the studio-offload-pin branch September 25, 2026 18:00
shimmyshimmer pushed a commit that referenced this pull request Sep 25, 2026
Size the streamed-tier pin reserve from the container like _pin_host_weights (#11766) and reuse its constants.
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…i#11777)

_free_port bound port 0, closed the socket and passed the number to the
server process to bind later. Another xdist worker could take the port in
between, and the server died with address already in use (seen on the l-r
shard of unslothai#11766). Worse, the readiness probe could then connect to whichever
process now held the port. The server now binds port 0 itself, keeps the
socket and reports the port before serving.

The new test makes every port this process is handed by the OS one that is
already listening; it fails on the old harness and passes here.
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…lows (unslothai#11766)

* Studio: whole-model offload points weights back at their host tensors instead of copying them

* Studio: pin the kept offload weights on first onload when host RAM allows

* Match the parameter object, not only its version, before reusing the host copy

* Forget a kept host tensor when its parameter is replaced or re-pointed while offloaded

* Take the stock offload copy when a weight's device storage was swapped through .data

* Merge studio-offload-keep-cpu; drain pinned host memory even when GPU cleanup raises

* Trim comments

* Trim comments

* Size the offload pin reserve from the container, not the host

psutil reads host-wide memory, so inside a 64 GiB container on a 512 GiB host
the RAM gate could pin more than the container has left. Cap the available
figure by the cgroup remainder and size the reserve from the cgroup limit,
through the same helpers the rest of diffusion_memory uses. Hosts without an
enforcing cgroup (and Windows / macOS) are unchanged.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Trim comments in keep-cpu offload

* Trim comments in offload pinning

* Skip the keep-cpu rescan on modules with nothing kept

* Drop a comment the owner check already states

* Tighten offload pinning comments

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants