Skip to content

Studio: run large LTX-2.x VAE decoder Conv3d as per-tap Conv2d on Windows ROCm - #18965

Merged
danielhanchen merged 2 commits into
mainfrom
studio-win-rocm-vae-pertap-conv
Nov 23, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-win-rocm-vae-pertap-conv

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

On Windows ROCm, a video VAE decoder Conv3d whose input or output tensor is larger than 2 GiB runs about 10x slower per element than the same conv just below that size. This PR sends only those calls through an equivalent decomposition: one Conv2d per temporal tap, with time folded into the batch, taps summed in fp32, and the bias added once. Every other call, and every other platform, keeps the stock Conv3d.

The cliff was measured on gfx1151 (Radeon 8060S), torch 2.11.0+rocm7.14.0, on the LTX-2.3 decoder. It shows up across 6 conv shape classes and 5 clip sizes, and the 2 GiB tensor size predicts it in every case. For example, the Cin=128 3x3x3 conv at 272x480 takes 352-372 ms at 852-887M elements but 3.8-4.1 s at 1387-1504M. Linux ROCm 7.13 on the same card has no cliff, and there the decomposition is slower, so the patch is Windows only.

What changes

  • New studio/backend/core/inference/video_vae_rocm_pertap.py:
    • install(vae) patches the Conv3d modules in the decoder of an AutoencoderKLLTX2Video (LTX-2 / 2.3). It installs only when sys.platform == "win32" and rocm_bf16.is_rocm_torch(torch) are both true. That covers AMD SDK wheels, which carry the ROCm tag only in __version__.
    • Each patched call re-checks the gate and takes the per-tap path only if all of these hold:
      • the tensor is on the GPU;
      • it is outside a torch.compile trace;
      • it is not tracking gradients;
      • its input or output is over 2 GiB.
        Otherwise it calls the stock Conv3d.forward.
    • Convs whose forward something else already replaced are left alone.
    • Non-zeros padding modes are pre-padded the same way the stock conv does it.
    • Output frames are processed in chunks, so the fp32 accumulator stays under 256 MiB. The result is a contiguous tensor in the stock layout.
    • UNSLOTH_VIDEO_VAE_ROCM_PERTAP=0 turns it off. It is read at install and on every call.
  • video.py installs it right after the frame pad fix, records vae_rocm_pertap_conv in speed_optims, and logs a warning on failure. Linux ROCm, CUDA, MPS, XPU and CPU never install it.
  • Wan and HunyuanVideo decoders are not covered because they were not measured.

Results (gfx1151 Windows, torch 2.11.0+rocm7.14.0, LTX-2.3 distilled VAE, bf16)

Setup:

  • Studio's own decode route: MIOpen immediate mode, wide tiles, untiled when it fits.
  • One saved N(0,1) latent per clip.
  • Every arm runs in a fresh process.
  • Warm time is the median of 3 decodes after the first.

Warm and first times are in seconds. A/A is the same arm again in a new process. "Kill" is head with UNSLOTH_VIDEO_VAE_ROCM_PERTAP=0.

clip base warm (A/A) head warm (A/A) kill warm first, base / head peak GiB, base / head convs routed per decode PSNR vs CPU fp32, base / head head vs base
512x288x97 1.754 / 1.759 1.752 / 1.749 1.750 4.12 / 3.92 2.85 / 2.85 0 48.46 / 48.46 dB bit-identical
1280x704x49 5.850 / 5.881 5.853 / 5.854 5.880 7.95 / 7.97 5.97 / 5.97 0 48.22 / 48.22 dB bit-identical
1920x1088x121 90.65 / 83.68 44.59 / 44.19 84.00 94.5 / 46.6 27.45 / 20.64 10 48.25 / 48.59 dB 61.0 dB
  • Large clip: decode is 1.9-2.0x faster, peak memory is 6.8 GiB lower, and the output is slightly closer to the CPU fp32 decode. The 10 routed convs are the eight resnet convs at the top decoder level, its upsampler and conv_out.
  • Small and medium clips: no call crosses 2 GiB. Head output is bit-identical to base, and timing is within the A/A spread.
  • Kill switch: with it set, nothing is installed and the output is bit-identical to base.
  • Single conv: the Cin=128 3x3x3 conv over 123 frames at 272x480 (4.1 GB input) takes 5.8-9.9 s per call stock and 2.32-2.36 s per tap (62.8 dB vs stock).
  • Numerics: with the per-tap path forced on GPU for LTX conv geometries, its error vs an fp64 reference is no larger than the stock conv's in fp32 (at most 1.4e-6) and in bf16.

On the Linux gfx1151 control (torch 2.11.0+rocm7.13.0), platform_applies is False and no convs are installed or routed:

clip base warm (A/A) head warm (A/A) head vs base
1920x1088x121 39.41 / 39.28 s 39.35 / 39.63 s bit-identical
512x288x97 1.712 / 1.745 s 1.741 / 1.772 s bit-identical

Tests

  • tests/test_video_vae_rocm_pertap.py (CPU), which covers:
    • the per-tap path vs F.conv3d in fp32 (rtol/atol 1e-5) and bf16 (no worse than the stock bf16 conv vs an fp64 reference), across the geometries the LTX decoder uses plus temporal/spatial stride, dilation, groups, batch 2 and multi-chunk;
    • reflect / replicate / circular padding;
    • the real LTX2VideoCausalConv3d, causal and non-causal;
    • the input and output size gate, and bytes rather than elements;
    • the platform gate: Windows ROCm, an AMD SDK wheel, Linux ROCm, Windows CUDA and CPU, macOS, Linux CUDA;
    • the target device;
    • the kill switch, at install and per call;
    • idempotence, and skipping convs that already have a forward override;
    • a torch.compile(fullgraph=True) trace with error_on_recompile;
    • a tiny AutoencoderKLLTX2Video decode, per-tap vs stock;
    • the video.py wiring.
  • tests/test_video_backend.py, tests/test_diffusion_rocm_tag_detect.py, tests/test_video_ltx2_vae_tiles.py and tests/test_video_vae_untiled.py pass.

…dows ROCm

On Windows ROCm a decoder Conv3d whose input or output tensor exceeds 2 GiB runs
about 10x slower per element than below that size. Route only those calls to one
Conv2d per temporal tap (time folded into batch, fp32 accumulation, bias once).
Windows ROCm only, size gated, UNSLOTH_VIDEO_VAE_ROCM_PERTAP=0 turns it off.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 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-23T02:38:23.932011Z da012c1 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. Already looking forward to the next diff.

Reviewed commit: da012c14b5

ℹ️ 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 bae720b into main Nov 23, 2026
@danielhanchen
danielhanchen deleted the studio-win-rocm-vae-pertap-conv branch November 23, 2026 03:37
danielhanchen added a commit that referenced this pull request Nov 23, 2026
…cess, INT8 attention and MPS guard changes (#19005)

## Summary

Follow-ups to #18965, #18968, #18979 and #18981 from a recheck of the
merged kernel and compile changes. There is one commit per PR. Each fix
is small and backwards compatible. The default bf16 / fp32 paths produce
the same outputs as before.

### #18965: Windows ROCm per-tap Conv3d
- Under CPU bf16 autocast, fp32 inputs came back as fp32 instead of the
autocast dtype. Autocast calls now stay on stock `Conv3d`; bf16, fp16
and fp32 calls outside autocast keep the per-tap path. fp16 stays on it
on purpose: on Windows gfx1151 the real LTX-2.3 fp16 decode is finite
there, and stock `Conv3d` was 2.4x slower (35.7 s vs 87.0 s) with a 7.1
GiB higher peak.
- `torch.compiler.is_compiling()` is now checked before the env switch.
Before this, flipping `UNSLOTH_VIDEO_VAE_ROCM_PERTAP` inside a compiled
region added an env guard and forced a recompile.

### #18968: Lanczos reference preprocess
- The int32 guard bounds a single image. The identity-resize and
vertical-first normalize launches run flat over the whole NHWC batch, so
a `[24, 8192, 8192, 3]` batch wrapped the int32 index. `_normalize_rgb`
now widens the program offset to int64 before the multiply. The TTIR now
shows an `i64` multiply and `i64` pointer offsets.
- If the load-time probe hit a transient OOM, GPU reference
preprocessing stayed off on that device until restart. Now an OOM takes
the CPU path for that call only, and the next request probes again.
Other probe failures are still cached.

### #18981: INT8 attention kernel switches
- Flipping the residual-fold, sink-order or native-P switch built a new
launch plan. The plan's `decision` key left the switches out, so the new
kernel reused the old kernel's accuracy and timing verdict without
racing. The decision key now carries the same switch tuple as the plan
key.

### #18979: MPS large-attention guard
- The prefill check parsed `torch.__version__` with `re.match`. On a
cold regex cache, Dynamo traced into the pattern compiler and failed
`fullgraph=True`. The pattern is now compiled at import.

## Tests

Each new test fails on `origin/main` and passes here:
- `test_video_vae_rocm_pertap.py`:
`test_autocast_keeps_stock_output_dtype`,
`test_compiled_kill_switch_flip_does_not_recompile` (plus
`test_fp16_outside_autocast_still_takes_pertap`)
- `test_diffusion_ref_preprocess.py`:
`test_batched_normalize_uses_64_bit_offsets`,
`test_probe_oom_is_not_cached`
- `test_diffusion_int8_attention_resid_fold.py`:
`test_a_flipped_switch_races_its_own_kernel`
- `test_mps_large_attention_guard.py`:
`test_cold_prefill_check_traces_under_fullgraph`

The touched suites and their neighbours (per-tap VAE, reference
preprocess, MPS guard and families, ROCm contiguous q/k/v and k/v,
CUTLASS race key, attention install, every INT8 attention suite) give
657 passed, 175 skipped (GPU / Mac only) on Linux CPU, torch 2.12.1,
Triton 3.7.1.

## Not changed

- #18966: the 2,560-token q/k/v gate splits a dynamic range into two
graphs. That is bounded: 8 lengths from 1,024 to 4,096 under
`fullgraph=True, dynamic=True` compiled 2 graphs. Studio's trainer never
marks a dynamic range across the threshold.
- #18979: over-cap query lengths still specialize the chunk loop under
compile. Studio never compiles on MPS (training compiles on CUDA only,
and the MPS target defaults to eager).


## GPU, Windows ROCm and Mac checks

Same host and job for both arms, base = the merge base.

| platform | check | base | this PR |
|---|---|---|---|
| A100 40 GB | Lanczos normalize of a `[24, 8192, 8192, 3]` uint8 batch
| illegal memory access | 24 of 24 images equal to the torch reference |
| A100 40 GB | real OOM in the load-time probe, then memory freed | GPU
path off for the process | probes again, GPU path on |
| A100 40 GB | GPU Pillow parity, 6 sizes; INT8 `-m gpu` tests | 6/6; 6
passed | 6/6; 6 passed |
| L4 | flip the residual-fold switch in one loaded process | 1 race,
same decision key | 2 races, keys differ |
| Windows gfx1151 | bf16 1920x1088x121 LTX-2.3 decode | 10 per-tap calls
| 10 per-tap calls, bit-identical, time within the base A/A spread |
| macos-15 | MPS guard suites with `UNSLOTH_MPS_LARGE_ATTENTION=1` | 4
failed, 90 passed | same 4 failed (stock attention past the threshold,
the bug the guard exists for), 92 passed |

On Metal, a V batch wider than Q and K does not broaden the attention,
so the guard keeps counting Q and K only.
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.

1 participant