Repository navigation
perf: reduce CUDA launch overhead in H3 VAE encoding - #2369
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
…art 1) The last step towards upstream master comes in three parts. This first one takes the commits before the Diffusion Graph Language rewrite: - two unused ggml compat functions removed, so the library links against a shared libggml 1.0.0 (leejet#2363); - asym_w4a8_int8 defaults to group size 16, as in ComfyUI (leejet#2388); - batched spatial padding in the Hunyuan causal convolutions (leejet#2374) and fewer CUDA launches in the H3 VAE encoder (leejet#2369); - documentation: Windows ROCm 0xC0000135, Off Grid AI, the server's browser notification (leejet#2356, leejet#2372, leejet#2318). The merge is clean; no fork code changes. Verified with a CPU build: the library, sd-cli and sd-server build, and all nine unit-test programs pass. Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
Summary
H3 VAE encoding spends much of its time launching CUDA kernels for spatial reflection padding. CONCAT launches once per channel with the current tensor layout.
Fold frames and channels into one dimension while padding width and height in both CausalConv3d and Downsample3D, then restore the original shape before the existing temporal padding and convolution. This lets CONCAT process all channels in one launch. The reflected edge values are unchanged.
Related Issue / Discussion
N/A
Additional Information
Tested MiniMax-H3-Image reference-image editing on Windows with an RTX 5070 Ti Laptop GPU (12 GB) and CUDA 13.4, against
532d2c15c430a785a712b2d94281f04e733008a4.Settings: INT8 ConvRot diffusion and P-H3 text encoder, FP16 H3 Image VAE, 1024 × 1152 reference, 1024 × 1024 output, 6 Euler steps, CFG 1, seed 42, CPU offload, 8 CPU threads, VAE tiles 256 with 25% overlap.
Mean of two runs per version, each in a fresh process without profiling:
Encoding is 6.3× faster; total time drops by 22%. Both optimized runs produce the same pixels as the baseline. A separate Nsight Systems capture shows encoder CONCAT launches dropping from 1,375,080 to 72,240.
Also checked CausalConv3d with one- and five-frame inputs on CPU and CUDA. On each backend, the original and modified code produce exactly the same values. The CUDA Release build, changed-line formatting check, and
git diff --checkpass.The change is in the shared H3 encoder, also used for video and by Sol-H3-VAE. Full video generation, Metal, and Vulkan were not tested.
Downsampling padding
The initial change above optimizes CausalConv3d. Downsample3D also pads the right and bottom edges, so this PR now applies the same layout change there.
Measured separately against
3fdcce8, which already includes the CausalConv3d optimization, using the same model, reference image, output size, steps, CFG, and seed listed above, with CPU RNG and a 7.5 GiB VRAM budget. Two runs per version were interleaved in separate processes:This further reduces encoding time by 27.9%, saving 0.82 s. All four generated images are pixel-identical. These are separate measurements from the initial comparison above. End-to-end time varied with offload and memory pressure, so no additional end-to-end speedup is claimed.
Downsample3D checks with single- and multi-frame inputs, including non-contiguous inputs, produce exactly the same values before and after the change on each backend separately (CPU and CUDA). The CUDA Release build, changed-line formatting checks, and
git diff --checkpass.Checklist
Summary by CodeRabbit