Skip to content

Vulkan: keep CPU-offloaded weights in pageable RAM (upstream #2450) - #106

Merged
danielhanchen merged 1 commit into
masterfrom
r13-sdcpp-2450-pageable
Nov 28, 2026
Merged

danielhanchen merged 1 commit into
masterfrom
r13-sdcpp-2450-pageable

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Vulkan: keep CPU-offloaded weights in pageable RAM (upstream leejet#2450)

Cherry-pick of upstream leejet/stable-diffusion.cpp PR leejet#2450 (merged as 58d1126, MIT) onto master.

Problem

With --offload-to-cpu, ModelManager::params_buffer_type_for places the CPU-side copy of every offloaded weight in ggml_backend_dev_host_buffer_type(compute_dev). On Vulkan that buffer type is a driver allocation. On AMD radv it is GTT, which the kernel caps with TTM pages_limit, so the CPU copies of the weights compete with the GPU replicas for the same pool. A MiniMax-H3 Q4_K render at 864x480x124 pins about 31 GiB of GTT this way (about 15.5 GiB with the text encoder on the CPU). When the pool runs out, the run ends in device lost even though the device copies have already been evicted.

Change

src/model_manager.cpp, params_buffer_type_for: do not use the device host buffer type when the compute backend is Vulkan. The offloaded weights then use the default CPU buffer type, which is ordinary pageable memory. CUDA, ROCm and the other backends are unchanged.

if (compute_dev != nullptr && !sd_backend_is(state.compute_backend, "Vulkan")) {
    params_buft = ggml_backend_dev_host_buffer_type(compute_dev);
}

Only the Vulkan guard is taken from upstream. The surrounding condition on master is older than the upstream one (it lacks the sd_backend_is_cpu(state.params_backend) clause from a different upstream change), and that part is left alone so this PR stays a single behaviour change. The docs line from upstream is added to docs/performance.md.

Validation

gfx1151 (Radeon 8060S, Linux, Vulkan/radv). Both sd-cli builds come from one source tree with the same toolchain and flags. The H3 run is --mode vid_gen with MiniMax-H3 fl2va_pruned-Q4_K and the Q4_K_M encoder at 864x480, 124 frames, 8 steps, seed 1, using the same Auto flags the app uses (--offload-to-cpu --diffusion-fa --max-vram -1), and writes a raw AVI so the frames can be hashed. Runs alternate between the two builds, two each.

# build peak GTT GiB peak RSS GiB s/it decode s wall s frame MD5
0 before 40.99 1.32 93.23 284.5 1037.0 d1b5e8588ea6617f513a5d4cf548c42d
1 after 15.92 32.58 92.86 282.8 1031.1 d1b5e8588ea6617f513a5d4cf548c42d
2 before 40.99 1.22 92.91 292.8 1042.3 d1b5e8588ea6617f513a5d4cf548c42d
3 after 15.93 32.70 92.71 284.9 1031.9 d1b5e8588ea6617f513a5d4cf548c42d

Baseline GTT before each run was 0.02 GiB. Peak GTT drops by 25.07 GiB (median of peaks), and the weights move into ordinary process memory (RSS rises by about 31 GiB). Sampling speed is unchanged: s/it after/before is 0.997, and the spread between repeats of the same build is 0.35% (before) and 0.17% (after). The decoded frames are bit-identical.

Follow-up

After a prebuilt that carries this change is pinned, the app's MiniMax-H3 memory fit can stop charging the offloaded weights against free GTT for Vulkan on that tag. Until then the existing charge stays, because older binaries still pin GTT.

Cherry-picked from upstream leejet/stable-diffusion.cpp PR leejet#2450
(MIT). Only the Vulkan guard is taken; the fork's params_buffer_type_for
condition is otherwise unchanged.

(cherry picked from commit 58d1126)
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-28T17:30:57.337638Z c19ff8d 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: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: c19ff8d9a4

ℹ️ 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
danielhanchen merged commit 6bc0f39 into master Nov 28, 2026
7 checks passed
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.

1 participant