class Rdna3MxFp4LinearKernel(MxFp4LinearKernel):
"""Weight-only MXFP4 GEMM for RDNA3 (gfx1100) via the native HIP kernel
`_rocm_C.mxfp4_gemm_rdna3` (scalar GEMV for decode, WMMA for prefill).
RDNA3 has no native FP4 matmul, so E2M1 weights are dequantized on the fly
into the f16/bf16 pipeline (W4A16).
"""
@classmethod
def is_supported(
cls, compute_capability: int | None = None
) -> tuple[bool, str | None]:
if not current_platform.is_rocm():
return False, "not running on ROCm"
from vllm.platforms.rocm import on_gfx1100
if not on_gfx1100():
return False, "RDNA3 gfx1100 WMMA required"
return True, None
@classmethod
def can_implement(cls, config: MxFp4LinearLayerConfig) -> tuple[bool, str | None]:
if config.activation_quant_key not in (None, kMxfp4Dynamic):
return False, "only supports MXFP4 dynamic or unquantized activations"
if config.activation_quant_key is not None:
logger.warning_once(
"Rdna3MxFp4LinearKernel is a weight-only (A16) kernel; "
"the requested activation quantization (%s) is ignored.",
config.activation_quant_key,
)
return True, None
def process_weights_after_loading(self, layer: torch.nn.Module) -> None:
# layer.weight: [N, K/2] uint8, two E2M1 codes per byte (low nibble at
# the even K index). layer.weight_scale: [N, K/32] uint8 E8M0.
w = layer.weight.data
scale = layer.weight_scale.data
N, k_half = w.shape
K = k_half * 2
if N % 16 != 0 or K % _MXFP4_GROUP_SIZE != 0:
raise ValueError(
f"RDNA3 MXFP4 WMMA kernel needs N%16==0 and K%32==0, got N={N}, K={K}"
)
# [N, K/2] uint8 reinterpreted little-endian as int32 is already the
# [K/8, N] word layout the kernel reads, so repack = view + transpose.
b_q = w.contiguous().view(torch.int32).t().contiguous() # [K/8, N]
b_scale = scale.t().contiguous() # E8M0 [K/32, N]
layer.weight = Parameter(b_q, requires_grad=False)
layer.weight_scale = Parameter(b_scale, requires_grad=False)
def apply_weights(
self,
layer: torch.nn.Module,
x: torch.Tensor,
bias: torch.Tensor | None = None,
) -> torch.Tensor:
out_shape = x.shape[:-1] + (layer.output_size_per_partition,)
x_2d = x.reshape(-1, x.shape[-1])
if not x_2d.is_contiguous():
x_2d = x_2d.contiguous()
out = ops.mxfp4_gemm_rdna3(x_2d, layer.weight, layer.weight_scale)
if bias is not None:
out = out + bias
return out.view(out_shape)