Skip to content

[ROCm][DLight] Support bf16 in the RDNA WMMA intrinsics and dlight Matmul - #21064

Draft
raviguptaamd wants to merge 3 commits into
apache:mainfrom
raviguptaamd:rocm-rdna-wmma-bf16
Draft

raviguptaamd wants to merge 3 commits into
apache:mainfrom
raviguptaamd:rocm-rdna-wmma-bf16

Conversation

@raviguptaamd

Copy link
Copy Markdown

Stacked PR. It depends on:

[ROCm][DLight] Support bf16 in the RDNA WMMA intrinsics and dlight Matmul

Add bf16 x bf16 -> fp32 WMMA tensor intrinsics (llvm.amdgcn.wmma.f32.16x16x16.bf16;
on gfx11 the operands are passed as <16 x i16> bit patterns, so the bf16 fragments are
reinterpreted as int16) and let RocmWmmaMatmul pick the fp16 or bf16 intrinsics by the
input dtype.

Requires the tvm-tirx fix that keeps bit-reinterpreted bf16 tensors in bf16 storage
during BF16ComputeLegalize; without it LLVM rejects the fragment bitcast.

Tests: bf16 selection (static and dynamic M) and numeric checks on an RDNA GPU (M = 512
and a ragged dynamic M = 300) against numpy; all 18 tests in the file pass on gfx1151.

TVM has tensor intrinsics for CDNA MFMA (wave64) but none for the WMMA
instructions of RDNA GPUs (gfx11, wave32), so tensorized GEMMs cannot use the
matrix cores of Radeon RX 7000 / Radeon 8060S (Ryzen AI Max) class GPUs.

This adds fill, load (A, B and transposed B from shared memory), multiply
(f16 x f16 -> f32, llvm.amdgcn.wmma.f32.16x16x16.f16) and store (global and
shared) intrinsics in tensor_intrin/rocm_wmma.py, next to the MFMA ones in
rocm.py. On RDNA, lanes 16-31 of a wave hold a copy of the A and B fragments of
lanes 0-15, so the A/B descriptors describe the 16 distinct lanes while the
implementations run on all 32; the accumulator uses 32 lanes x 8 values (rows
2 * i + lane // 16 of column lane % 16).

mfma_schedule in s_tir/testing/tir.py takes a warp_size argument (default 64,
unchanged for MFMA) so it can drive the wave32 intrinsics.

A numeric test tensorizes a 512x512x512 fp16 GEMM with B in [K, N] and in
[N, K] layout and compares with numpy. It is skipped unless an RDNA device with
WMMA is present, and passes on gfx1151 (Radeon 8060S).
dlight Matmul schedules every ROCm GEMM with SIMT FMAs, so RDNA GPUs (gfx11,
gfx12) leave their WMMA matrix instructions unused.

Add a RocmWmmaMatmul rule (dlight/gpu/rocm_wmma_matmul.py) built on the RDNA
WMMA tensor intrinsics, and try it from Matmul.apply for rocm targets with an
RDNA mcpu and thread_warp_size 32. It handles fp16 x fp16 -> fp32 GEMMs with
static or dynamic M, including the forms Relax produces:
- prologues (padding for dynamic M, weight dequantization such as int4 ->
  fp16) are computed while filling LDS, once per block tile;
- epilogues (cast, bias, other elementwise ops) are inlined into the output
  copy kernel.
If any block would be left outside a GPU kernel the rule returns None and
Matmul falls back to its SIMT schedule. Small static M (< 64, decode GEMVs)
also stays on the SIMT path. CUDA, Metal and CDNA targets are unchanged.

Tests: selection for plain, cast+bias and int4-dequant GEMMs with static and
dynamic M (CPU only), no WMMA for M = 8 or gfx942, and numeric checks of
those GEMMs (M = 512 and a ragged dynamic M = 300) on an RDNA GPU.

On an idle gfx1151 (Radeon 8060S, ROCm 7.2.1), N = 2048, K = 1536, warm cache:

  GEMM                         WMMA       SIMT (before)   speedup
  static M=512, plain/cast+bias 0.19-0.21 ms 0.83-0.94 ms   4.0-4.4x
  static M=512, int4 dequant   0.20 ms    0.66 ms         3.2x
  dynamic M=512                0.26-0.27 ms 0.68-0.90 ms  2.5-3.3x
  dynamic M=300                0.20 ms    0.38-0.45 ms    1.9-2.2x
…tmul

Add bf16 x bf16 -> fp32 WMMA tensor intrinsics (llvm.amdgcn.wmma.f32.16x16x16.bf16;
on gfx11 the operands are passed as <16 x i16> bit patterns, so the bf16 fragments are
reinterpreted as int16) and let RocmWmmaMatmul pick the fp16 or bf16 intrinsics by the
input dtype.

Requires the tvm-tirx fix that keeps bit-reinterpreted bf16 tensors in bf16 storage
during BF16ComputeLegalize; without it LLVM rejects the fragment bitcast.

Tests: bf16 selection (static and dynamic M) and numeric checks on an RDNA GPU (M = 512
and a ragged dynamic M = 300) against numpy; all 18 tests in the file pass on gfx1151.

This branch has not been deployed

No deployments
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