Files
composable_kernel/dispatcher/examples/fmha/python/11_bf16_fmha.py
Vidyasagar Ananthan b20458e19e [rocm-libraries] ROCm/rocm-libraries#5260 (commit a1834d2)
[CK] [CK_Tile] Add FMHA scaffolding to CK kernel dispatcher (#5260)

## Motivation

The CK Tile dispatcher currently supports GEMM and Grouped Convolution
but has no support for Fused Multi-Head Attention (FMHA). The
example/ck_tile/01_fmha folder contains a comprehensive FMHA
implementation with forward, backward, split-KV, paged-KV, append-KV,
and batch-prefill kernels across multiple GPU architectures — but there
is no unified dispatch layer for it. This PR ports the FMHA stack into
the dispatcher, following the same architectural patterns established by
GEMM and Grouped Convolution, enabling runtime kernel selection, JIT
compilation from Python, and a declarative C++ example flow. Autotuning
heuristics to follow.

## Technical Details

This PR adds FMHA scaffolding to the CK dispatcher framework, mirroring
GEMM's layered architecture. Seven new C++ runtime headers provide type
definitions (coexisting with upstream headers via __has_include,
requiring zero modifications to example/ck_tile/01_fmha/), a problem
builder with 18+ setters, Signature + Algorithm kernel key matching, a
virtual kernel instance, a DECL_FMHA_KERNEL_SET macro with wildcard
support and named tile/wave/warp setters, arch-aware registry with JSON
export, and a dispatcher with seqtune-aware selection, configurable
timing, and multi-stage execution plans for split-KV (two-stage) and
backward (three-stage). The codegen pipeline is driven by a
fmha_arch_specs.json capturing per-arch tile tables and pipeline
constraints for five architectures (gfx90a/942/950/1100/1201), migrated
from hardcoded logic in 01_fmha/codegen/, with supporting modules for
C++ symbol mappings, validation rules, and named receipt profiles
(ck_default, flash, pytorch, aiter, fp32, fp8). Python integration
(fmha_utils.py) mirrors the C++ layer with JIT compilation, parallel
multi-kernel builds, HIP memory management via ctypes, tolerance-based
validation, and a NumPy CPU reference with GQA support. Twenty-seven C++
and thirty-two Python examples cover the full feature surface — forward,
split-KV, masks, bias, dropout, GQA, backward, append-KV, batch prefill,
fp8, logits soft cap, sink tokens, and parameter sweeps — all
JIT-compiled on the fly.

## Test Plan

Seven test files cover the runtime types, codegen, and end-to-end
correctness. C++ unit tests validate the problem builder, dispatcher
planning (single-stage for forward/paged-KV/append-KV; multi-stage for
split-KV and backward), registry operations, and the kernel-set
declaration macro. Python unit tests verify codegen emission, profile
filtering, and 15 validation rules for masks, hdim constraints, and
pipeline requirements. GPU execution validation in 01_basic_fmha
--validate reports zero errors across 65,536 elements with max absolute
error of 7.29e-05. A gold-standard parity suite (test_fmha_parity.py)
runs 14 configurations through both the upstream tile_example_fmha_fwd
and the dispatcher, comparing exit codes to confirm behavioral parity —
all 14 match.

## Test Result

The C++ smoke test builds and passes all 9 compiled examples, and a
Python JIT sweep (29_sweep_seqlen.py) passes 7/7 configurations reaching
up to 375 TFLOPS at seqlen 2048.

## Submission Checklist

- [x] Look over the contributing guidelines at
https://github.com/ROCm/ROCm/blob/develop/CONTRIBUTING.md#pull-requests.

---------

Co-authored-by: Yaswanth Raparti <113389104+yraparti@users.noreply.github.com>
Co-authored-by: Mohsen Saffari <mohsen.saffari@amd.com>
Co-authored-by: Maksim (Max) Podkorytov <Maksim.Podkorytov@amd.com>
Co-authored-by: yashagar <yashagar@amd.com>
2026-05-17 00:29:40 -07:00

189 lines
6.1 KiB
Python

#!/usr/bin/env python3
# Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
# SPDX-License-Identifier: MIT
"""
Example 11: BF16 Forward Attention
Demonstrates:
1. BF16 data generation and handling
2. GPU execution attempt with prebuilt kernel (fp16-only)
3. CPU reference computation in float32
4. BF16-specific tolerance validation (atol=1e-2)
The prebuilt library contains only fp16 kernels. This example shows the API
pattern for bf16 and gracefully falls back to CPU reference when the GPU
kernel does not support bf16.
Usage:
python3 11_bf16_fmha.py
python3 11_bf16_fmha.py --batch 4 --seqlen 256
python3 11_bf16_fmha.py --arch gfx942
"""
import sys
import argparse
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent / "python"))
import numpy as np
from fmha_utils import (
FmhaProblem,
FmhaKernelConfig,
FmhaValidator,
cpu_attention_fwd,
detect_gpu_arch,
setup_fmha_dispatcher,
)
def to_bf16(arr: np.ndarray) -> np.ndarray:
"""Convert float32 array to bfloat16 (stored as uint16 with bf16 bit pattern)."""
f32 = arr.astype(np.float32)
u32 = f32.view(np.uint32)
return (u32 >> 16).astype(np.uint16)
def bf16_to_f32(arr_u16: np.ndarray) -> np.ndarray:
"""Convert bfloat16 (uint16) back to float32."""
u32 = arr_u16.astype(np.uint32) << 16
return u32.view(np.float32)
def main():
parser = argparse.ArgumentParser(description="BF16 Forward Attention")
parser.add_argument("--arch", default=detect_gpu_arch())
parser.add_argument("--batch", type=int, default=2)
parser.add_argument("--nhead", type=int, default=8)
parser.add_argument("--seqlen", type=int, default=128)
parser.add_argument("--hdim", type=int, default=128)
args = parser.parse_args()
print("=" * 70)
print("Example 11: BF16 Forward Attention")
print("=" * 70)
prob = FmhaProblem(
batch=args.batch,
nhead_q=args.nhead,
nhead_k=args.nhead,
seqlen_q=args.seqlen,
seqlen_k=args.seqlen,
hdim_q=args.hdim,
hdim_v=args.hdim,
)
print(
f"\n Problem: B={prob.batch} H={prob.nhead_q} S={prob.seqlen_q} D={prob.hdim_q}"
)
print(" Dtype: bfloat16")
print(f" Arch: {args.arch}")
print(f" Scale: {prob.scale:.6f}")
# --- Generate bf16 data ---
np.random.seed(42)
Q_f32 = (np.random.randn(*prob.q_shape()) * 0.1).astype(np.float32)
K_f32 = (np.random.randn(*prob.k_shape()) * 0.1).astype(np.float32)
V_f32 = (np.random.randn(*prob.v_shape()) * 0.1).astype(np.float32)
Q_bf16 = to_bf16(Q_f32)
K_bf16 = to_bf16(K_f32)
V_bf16 = to_bf16(V_f32)
Q_bf16_f32 = bf16_to_f32(Q_bf16)
K_bf16_f32 = bf16_to_f32(K_bf16)
V_bf16_f32 = bf16_to_f32(V_bf16)
print(f"\n Q bf16 range: [{Q_bf16_f32.min():.4f}, {Q_bf16_f32.max():.4f}]")
print(f" K bf16 range: [{K_bf16_f32.min():.4f}, {K_bf16_f32.max():.4f}]")
print(f" V bf16 range: [{V_bf16_f32.min():.4f}, {V_bf16_f32.max():.4f}]")
bf16_quant_err = np.abs(Q_f32 - Q_bf16_f32).max()
print(f" BF16 quantization error: {bf16_quant_err:.2e}")
# --- GPU execution attempt ---
print("\n--- GPU Execution ---")
gpu_output = None
gpu_time = None
config = FmhaKernelConfig(
data_type="fp16",
hdim_q=args.hdim,
hdim_v=args.hdim,
gfx_arch=args.arch,
)
setup = setup_fmha_dispatcher(config)
if not setup.success:
print(f" JIT build failed: {setup.error}")
else:
runner = setup.runner
print(f" JIT build: {setup.build_time_s:.1f}s")
Q_fp16 = Q_bf16_f32.astype(np.float16)
K_fp16 = K_bf16_f32.astype(np.float16)
V_fp16 = V_bf16_f32.astype(np.float16)
result = runner.run(Q_fp16, K_fp16, V_fp16, prob)
if result.success:
gpu_output = result.output
gpu_time = result.time_ms
print(f" GPU: {result.time_ms:.4f} ms, {result.tflops:.2f} TFLOPS")
print(" Note: Ran as fp16 (JIT kernel); native bf16 kernel not compiled")
else:
print(" GPU: Kernel does not support bf16 (expected)")
# --- CPU reference (always computed) ---
print("\n--- CPU Reference (float32 with bf16-quantized inputs) ---")
O_ref = cpu_attention_fwd(Q_bf16_f32, K_bf16_f32, V_bf16_f32, prob.scale)
print(f" Output range: [{O_ref.min():.4f}, {O_ref.max():.4f}]")
print(f" Output shape: {O_ref.shape}")
# --- Validation ---
print("\n--- Validation ---")
validator = FmhaValidator(rtol=1e-2, atol=1e-2)
print(f"\n {'Check':<30} {'MaxAbs':>10} {'MaxRel':>10} {'Status':>8}")
print(" " + "-" * 62)
if gpu_output is not None:
ok, max_abs, max_rel = validator.check(gpu_output, O_ref)
tag = "PASS" if ok else "FAIL"
print(
f" {'GPU vs CPU (bf16 tol)':<30} {max_abs:>10.2e} {max_rel:>10.2e} {tag:>8}"
)
else:
print(f" {'GPU vs CPU (bf16 tol)':<30} {'N/A':>10} {'N/A':>10} {'SKIP':>8}")
strict_val = FmhaValidator(rtol=1e-5, atol=1e-5)
ok_strict, ma_strict, mr_strict = strict_val.check(
O_ref.astype(np.float16),
O_ref,
)
print(
f" {'fp16(ref) vs f32(ref)':<30} {ma_strict:>10.2e} {mr_strict:>10.2e} {'PASS' if ok_strict else 'INFO':>8}"
)
O_ref_from_f32 = cpu_attention_fwd(Q_f32, K_f32, V_f32, prob.scale)
bf16_impact = float(np.abs(O_ref - O_ref_from_f32).max())
print(
f" {'bf16 vs f32 input impact':<30} {bf16_impact:>10.2e} {'':>10} {'INFO':>8}"
)
# --- Summary ---
print("\n" + "=" * 70)
print(" Dtype: bfloat16 (7-bit mantissa vs fp16's 10-bit)")
print(" Tolerance: atol=1e-2 (relaxed for bf16 precision)")
print(
f" GPU: {'%.4f ms' % gpu_time if gpu_time else 'N/A (bf16 kernel not in prebuilt)'}"
)
print(" CPU ref: Computed with bf16-quantized inputs")
print(" BF16 range: Larger exponent range (±3.4e38) vs fp16 (±65504)")
status = "PASS" if gpu_output is not None else "DEMO"
print(f" Status: {status}")
print("=" * 70)
return 0
if __name__ == "__main__":
sys.exit(main())