Skip to content

About

ComfyUI node: full-speed Linear layers on AMD RDNA 4 (gfx12) under ROCm, bit-identical output

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

ComfyUI-AMD-RDNA4-Speedup

On AMD RDNA 4 GPUs (gfx12) under ROCm, hipBLASLt runs the bf16/fp16 GEMM behind nn.Linear at about half speed when in_features is a multiple of 4096. This node computes those layers as x @ W.T with a cached contiguous copy of W.T, which takes a different GEMM path that runs at full speed. The output is bit-identical.

Upstream report: ROCm/rocm-libraries#17180.

Results

RX 9060 XT (gfx1200), Windows 11, ROCm 10.1.0, PyTorch 2.14.

GEMM throughput, M = 8192 tokens, N = 2048 outputs, bf16 (TFLOPS):

in_features F.linear this node
3072 62.8 (not patched)
4096 32.5 65.4
8192 26.4 57.0
12288 33.2 59.9
16384 24.9 56.5

Anima (Cosmos-Predict2 2B DiT, whose MLP down-projections are 8192 → 2048), 1024×1024, mean sampling time over 2 runs:

without with speedup
25 steps, euler_ancestral, CFG 4, one LoRA (4 prompts) 25.3–26.2 s 21.7–22.0 s 1.15–1.20×
30 steps, euler, CFG 4, no LoRA 30.2 s 26.2 s 1.15×

Every image was bit-identical with and without the node. On SDXL, which has no such layers, the node changes nothing. At 1152×1536 an earlier version of the same code gave the same 1.17×.

Other architectures with such layers (Flux and Qwen-Image MLPs have in_features 12288, for example) should benefit, but I have only measured Anima end to end.

Install

From ComfyUI-Manager, search for AMD RDNA 4 Speedup, or with comfy-cli: comfy node install amd-rdna4-speedup. Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/Mexes-GM/ComfyUI-AMD-RDNA4-Speedup

No extra dependencies.

Usage

Put gfx12 Linear Fix (RDNA 4) (category advanced/model) between the model loader and the sampler. LoRA loaders can go before or after it.

With only_on_gfx12 on (the default) the node returns the model unchanged unless it runs on a ROCm gfx12 GPU, so a workflow that uses it still works on other hardware.

To patch every model without editing workflows, start ComfyUI with GFX12_LINEAR_FIX=1. Models loaded by UNETLoader (Load Diffusion Model) and CheckpointLoaderSimple are then patched on load, only on gfx12. Using the node as well does no harm.

What it changes

  • Every Linear in the diffusion model whose in_features is a multiple of 4096 gets its forward replaced through ModelPatcher.add_object_patch. The module tree, parameter names and state dict do not change, and unloading the model restores the original forward.
  • The weight is obtained through ComfyUI's own cast path, so LoRAs, weight casts and offloading behave as before.
  • W.T buffers are allocated before sampling starts and freed when the model is unpatched. They cost VRAM equal to the patched weights: 944 MiB for Anima's 34 layers in bf16 (the 28 MLP down-projections and 6 small layers in its text adapter).
  • If a layer's weight changes on every call (a LoRA applied on the fly, for example), the cached copy is useless; after two misses in a row that layer goes back to F.linear and a warning is logged once.
  • Not patched: quantized weights (fp8, int8, GGUF and other tensor subclasses), layers with their own forward, training (autograd), and text encoders and VAEs.

Tests

python -m pytest

The tests run on the CPU and do not import ComfyUI.

License

MIT

About

ComfyUI node: full-speed Linear layers on AMD RDNA 4 (gfx12) under ROCm, bit-identical output

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages