Skip to content

Studio sd.cpp: let the VAE keep its own direct-conv choice on Vulkan (decode 5-6x faster on gfx1151, -21% per image) - #18303

Merged
danielhanchen merged 2 commits into
mainfrom
studio-sdcpp-vulkan-vae-conv-direct
Nov 18, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-sdcpp-vulkan-vae-conv-direct

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

On the native sd.cpp route with a Vulkan denoiser, Studio sent a global --conv-direct off. That also turned off the fork's per-device direct-conv choice for the VAE. Studio now sends --conv-direct off,vae=auto, so the denoiser's 3D/2D convs stay off and the VAE decides for itself.

AMD CI, gfx1151 (Strix Halo), Z-Image Turbo at 1024 px, 3 repeats per arm:

OS / driver VAE decode, main to this PR Whole image, main to this PR Peak memory, main to this PR
Linux RADV 12.81 s to 2.04 s 51.9 s to 40.8 s (-21%) 5.4 GB to 1.7 GB
Windows AMD driver 16.92 s to 3.51 s 60.1 s to 46.7 s (-22%) 21.7 GB to 13.6 GB
  • Images: PSNR against main's output is about 55.6 dB on both.
  • Engagement: this PR's log shows the VAE taking direct conv after its output check (conv2d direct auto: Vulkan0 -> on (output check passed)), and 3D conv stays off.
  • Forced on: forcing it with vae=on gives byte-identical images at the same speed.

Changes

  • When the denoiser runs on Vulkan and the VAE runs on Vulkan or CPU, the argument is now off,vae=auto. The pinned fork binary accepts this syntax; it shipped with the --conv-direct flag itself.
  • ROCm/HIP, Metal and the kill switch still send off.
  • Tests updated in test_sd_cpp_conv_direct_off.py: 22 passed.

Kill switch: UNSLOTH_SD_CPP_CONV_DIRECT=0.

Not covered

  • ROCm/HIP sd.cpp builds: the pin only publishes Vulkan ones.
  • A video VAE on the native route.
  • NVIDIA Vulkan.

…onv-direct off,vae=auto)

When Studio's policy rejects direct conv (Vulkan, ROCm / HIP), builds that list --conv-direct were sent a global
off, which also turned off the fork's VAE-only path: on Vulkan it runs direct conv2d in the VAE only after an output
check against im2col passes (3D convs stay im2col), and decodes faster in far less memory on RADV and AMD's Windows
driver. A Vulkan denoiser with its VAE on Vulkan or CPU now gets off,vae=auto (denoiser and ControlNet stay off);
HIP keeps the global off (its auto picks implicit GEMM, never faster on gfx1151), as do Metal and
UNSLOTH_SD_CPP_CONV_DIRECT=0 (kill switch). The module syntax shipped with --conv-direct itself, so the existing help
check covers it.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 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-18T10:55:23.288058Z 986968c 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 986968ca5d

ℹ️ 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 that referenced this pull request Nov 18, 2026
#18324)

## Multi-LoRA packing: load the num_proc policy without importing
unsloth


`studio/backend/tests/test_dataset_map_num_proc.py::test_the_batched_packing_pool_asks_the_same_veto`
fails in `(Python 3.13, a-f)` on every PR since #17701, for example
#18015, #18108, #18142 and #18303:

```
core/training/multi_lora_trainer.py:686: in _pack_num_proc
    from unsloth_zoo.dataset_num_proc import get_dataset_num_proc
E   ModuleNotFoundError: No module named 'unsloth_zoo'
core/training/multi_lora_trainer.py:688: in _pack_num_proc
    from unsloth.dataset_num_proc import get_dataset_num_proc
unsloth/_gpu_init.py:241: in <module>
E   ImportError: Unsloth: Please install unsloth_zoo via `pip install unsloth_zoo` then retry!
```

### Cause

`_pack_num_proc`, the multi-LoRA packing pool's worker count from
#15733, looks up the shared `dataset_num_proc` policy in two steps:

1. it imports `unsloth_zoo`;
2. if that fails, it falls back to `from unsloth.dataset_num_proc import
...`.

That fallback runs the whole `unsloth` package `__init__`, which needs
`unsloth_zoo` anyway and patches torch on the way.

The rest of the backend already avoids this.
`utils.hardware.hardware._shared_policy()` tries `unsloth_zoo` first and
otherwise loads the stdlib-only `dataset_num_proc.py` straight off disk,
precisely so that no caller runs `import unsloth`.

#17701 added the first test that reaches `_pack_num_proc`, and the
backend shard runs without `unsloth_zoo`, so the fallback fails there.

### Fix

`_pack_num_proc` now gets the policy from `_shared_policy()`, the same
loader the single-job tokenize pool uses.

- **With a policy**, which is every real install: the call is unchanged,
`get_dataset_num_proc(count)` on the same module.
- **With no policy at all:** it keeps that function's contract, so a
serial count of 1 still means `None`.

### Verification

- Faithful repro of the CI state: `core.training.trainer` already
imported, then `unsloth_zoo` made unimportable, then `_pack_num_proc`
called with a tight host-memory snapshot.
  - `main`: `ModuleNotFoundError: No module named 'unsloth_zoo'`.
  - This PR: returns `None`, as the test expects.
- With `unsloth_zoo` installed: `tests/test_dataset_map_num_proc.py` and
the multi-LoRA tests pass (88 passed).
- ruff is clean.
@danielhanchen
danielhanchen merged commit 4d92b71 into main Nov 18, 2026
@danielhanchen
danielhanchen deleted the studio-sdcpp-vulkan-vae-conv-direct branch November 18, 2026 21:01
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