Skip to content

[ROCm] Run Matrix-Game-4 inference on AMD GPUs - #100

Open
jeffdaily wants to merge 3 commits into
SkyworkAI:mainfrom
AMD-Ecosystem:moat-port
Open

jeffdaily wants to merge 3 commits into
SkyworkAI:mainfrom
AMD-Ecosystem:moat-port

Conversation

@jeffdaily

Copy link
Copy Markdown

This lets Matrix-Game-4 inference run on AMD GPUs with ROCm. Matrix-Game-3 is not changed (#67 covers it).

Problem

On a ROCm build of PyTorch, the bf16 presets already run, but the rtx5090, rtx5080 and rtx5070ti presets do not:

  • The FP8 presets quantize to float8_e4m3fn. MI300-series GPUs (gfx94x) only multiply the FNUZ encoding (float8_e4m3fnuz, largest finite value 240), so their FP8 GEMMs refuse the tensors.
  • The rtx5070ti MXFP8 role uses NVIDIA's swizzled block-scale layout and a bias in the GEMM. PyTorch's ROCm MX GEMM (hipBLASLt, gfx950 only) accepts only unswizzled row-major [M, K/32] scales, M a multiple of 32, no bias and BF16 output.
  • The rtx5080 epilogue calls tl.extra.cuda.libdevice.tanh, which Triton's AMD backend refuses.

Change

  • rtx5090/fp8_linear.py: pick the e4m3 encoding once per process (FNUZ and 240 on gfx94x, float8_e4m3fn and 448 everywhere else). The Triton quantizers take the maximum as a constexpr and cast to the output tensor's dtype, so the CUDA build compiles the same code as before.
  • rtx5070ti/linear.py: on ROCm, write row-major scales, pad the activation rows to a multiple of 32 and add the bias after the GEMM; AMD GPUs without MX GEMMs (all but gfx950) run that one role in BF16. The NVIDIA path is unchanged.
  • rtx5080/linears.py: use triton.language.extra.libdevice.tanh, which dispatches correctly on every backend (the rtx5070ti epilogue already does this).
  • README and docs/installation.md: an AMD ROCm install section (the ROCm build of PyTorch replaces the CUDA one; nothing else changes), what each preset needs from the GPU, and HIP_VISIBLE_DEVICES.

Testing

Unit tests (python -m unittest discover -s tests -p 'test_*.py') and 120-frame renders of the same clip and seed with matrix-game render --scene ancient-egypt ... --prompt-mode fixed --seed 42 --preset <p>, compared frame by frame with the bf16 native runtime on the same GPU (the release check in docs/testing.md):

GPU realtime rtx5090 rtx5080 rtx5070ti
Instinct MI355X (gfx950) 120/120 120/120 120/120 120/120 (MXFP8)
Instinct MI300X (gfx942) 120/120 120/120 (FP8 FNUZ) 120/120 (FP8 FNUZ) 120/120 (MXFP8 role in BF16)
Radeon PRO W7800 (gfx1100) 120/120 not supported (no FP8 GEMM) not supported (no FP8 GEMM) 120/120 (MXFP8 role in BF16)

Each converted linear layer was also checked against its BF16 layer (relative error about 0.04 for FP8 and MXFP8). On the MI300X, the FP8 GEMM output matches an exact FNUZ emulation to BF16 rounding. Nothing was run on an NVIDIA GPU. With the CUDA build's Triton (3.7.0), the five changed kernel instances compile to byte-identical PTX, cubin and SASS for sm_90, sm_100 and sm_120 before and after this change.

Notes:

  • The -sage presets need SageAttention, which has no ROCm build.
  • On MI300X, PyTorch's default convolution solver for one TAEHV decode convolution is not run-to-run deterministic. torch.backends.cudnn.deterministic = True makes renders repeat bit for bit at the same speed. cuDNN makes the same promise, so this PR does not change it.
  • Windows was not tested.

My agent came up with this analysis, but I've read and approved it. The changes were prepared with the help of an AI coding agent.


This pull request was prepared with the help of an AI assistant acting as a coding agent and was read and approved by a person before it was opened. It comes from an ongoing effort to add AMD GPU support to widely used CUDA projects, one repository at a time: https://github.com/AMD-Ecosystem/moat -- that repository describes how the work is done and what a person checks before anything is submitted.

If you would rather not receive pull requests from this effort, say so here or open an issue at https://github.com/AMD-Ecosystem/moat/issues/new/choose and we will close this and stop. That can cover this repository alone or everything you own, whichever you prefer.

The transposed feed-forward epilogue called tl.extra.cuda.libdevice.tanh. Triton's AMD backend refuses the CUDA-namespaced libdevice functions ("Implicit conversion of CUDA __nv_tanhf device function has been dropped"), so the rtx5080 preset failed to compile its epilogue on ROCm. triton.language.extra.libdevice dispatches to the right library on every backend and is what the rtx5070ti INT8 GEMM epilogue already uses, so behaviour on NVIDIA GPUs is unchanged.

This change was prepared with the help of an AI coding agent.

Test Plan:

```
python -m unittest discover -s tests -p 'test_*.py'
matrix-game render --scene ancient-egypt --resources resources --input clip.mp4 --preset rtx5080 --prompt-mode fixed --output outputs/rtx5080
```

Both pass on AMD Instinct MI355X (PyTorch 2.12.0, ROCm build); the render produces every input frame.
The rtx5090, rtx5080 and rtx5070ti presets quantize to FP8 and MXFP8 in layouts and encodings that only NVIDIA's libraries accept, so they could not run on ROCm.

FP8 (rtx5090, rtx5080, the fp8 roles of rtx5070ti): the e4m3 encoding is now chosen once per process. MI300-series (gfx94x) GPUs only multiply the FNUZ variant (float8_e4m3fnuz, largest finite value 240), which is how PyTorch's own tests pick their e4m3 type; every other GPU keeps float8_e4m3fn and 448. The Triton activation quantizers take the largest finite value as a constexpr and cast to the output tensor's element type, so the CUDA build compiles exactly the code it did before (448, float8e4nv).

MXFP8 (the cross_q role of rtx5070ti): PyTorch's ROCm MX GEMM (hipBLASLt on gfx950) refuses SWIZZLE_32_4_4 and accepts only NO_SWIZZLE, with row-major [M, K/32] e8m0 scales, M a multiple of 32, no bias and BF16 output. On ROCm the quantizer therefore writes row-major scales, pads the activation rows with zeros to a multiple of 32, and the bias is added after the GEMM. AMD GPUs without MX GEMMs (all but gfx950) run that role in BF16. The NVIDIA path is unchanged.

This change was prepared with the help of an AI coding agent.

Test Plan:

```
python -m unittest discover -s tests -p 'test_*.py'
for p in rtx5090 rtx5080 rtx5070ti realtime; do
  matrix-game render --scene ancient-egypt --resources resources --input clip.mp4 --preset $p --prompt-mode fixed --seed 42 --output outputs/$p
done
matrix-game render --scene ancient-egypt --resources resources --input clip.mp4 --preset realtime --dit-runtime native --no-vae-graph --prompt-mode fixed --seed 42 --output outputs/native
```

On AMD Instinct MI355X (gfx950, PyTorch 2.12.0 ROCm build) the unit tests pass and each preset renders all 120 frames of a 120-frame clip. Per-frame PSNR against the native runtime: rtx5090 19.3 dB, rtx5080 20.0 dB, rtx5070ti 18.9 dB mean, where the bf16 realtime preset gives 22.2 dB. Each converted linear was also checked against its BF16 layer (relative error 0.037 for FP8 and MXFP8, 0.015 to 0.05 for INT8), and an MXFP8 layer replays bit-identically from a CUDA graph.
Add an AMD ROCm subsection to the v4 install guide: the ROCm build of PyTorch 2.12.0 replaces the CUDA one and nothing else changes. It lists the presets that were run on AMD Instinct MI355X, what the FP8 and MXFP8 presets need from the GPU, that the -sage presets need SageAttention (which has no ROCm build), and that HIP_VISIBLE_DEVICES selects the GPU. docs/installation.md points to it.

This change was prepared with the help of an AI coding agent.

Test Plan:

Documentation only. On an MI355X host with a ROCm build of PyTorch 2.12.0 (an AMD build against ROCm 10.2 rather than the download.pytorch.org rocm7.2 wheel) and the package installed as described, the unit tests and the realtime, quality, baseline, rtx5090, rtx5080 and rtx5070ti renders ran:

```
python -m unittest discover -s tests -p 'test_*.py'
matrix-game render --scene ancient-egypt --resources resources --input clip.mp4 --preset quality --prompt-mode fixed --output outputs/quality
```
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