Files
composable_kernel/dispatcher/examples/fmha/python/36_bwd_benchmark_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

235 lines
7.3 KiB
Python

#!/usr/bin/env python3
# Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
# SPDX-License-Identifier: MIT
"""
Example 36: Backward Pass Benchmark
Benchmarks the FMHA backward pass across problem sizes. The backward
pass is approximately 4x the forward FLOPS because it computes dQ, dK,
and dV through two matrix multiplications each (plus the dot_do_o stage).
Backward FLOPS estimate:
FWD: 2 * B * H * Sq * Sk * (Dq + Dv)
BWD: ~4 * FWD_FLOPS
= 2 * B * H * Sq * Sk * Dq (dP = dO @ V^T, part of dS computation)
+ 2 * B * H * Sq * Sk * Dq (dQ = dS @ K)
+ 2 * B * H * Sq * Sk * Dq (dK = dS^T @ Q)
+ 2 * B * H * Sq * Sk * Dv (dV = P^T @ dO)
When GPU JIT is unavailable, benchmarks CPU reference instead.
Usage:
python3 36_bwd_benchmark_fmha.py
python3 36_bwd_benchmark_fmha.py --repeat 5
python3 36_bwd_benchmark_fmha.py --arch gfx942
"""
import sys
import time
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 (
FmhaKernelConfig,
FmhaProblem,
setup_fmha_dispatcher,
detect_gpu_arch,
cpu_attention_fwd_with_intermediates,
cpu_attention_bwd,
)
cpu_fwd_with_intermediates = cpu_attention_fwd_with_intermediates
def bwd_flops(prob: FmhaProblem) -> int:
"""Estimate backward FLOPS (~4x forward)."""
B, Hq, Sq, Sk = prob.batch, prob.nhead_q, prob.seqlen_q, prob.seqlen_k
Dq, Dv = prob.hdim_q, prob.hdim_v
fwd = 2 * B * Hq * Sq * Sk * (Dq + Dv)
return 4 * fwd
def main():
parser = argparse.ArgumentParser(description="Backward Pass Benchmark")
parser.add_argument("--arch", default=detect_gpu_arch())
parser.add_argument("--repeat", type=int, default=3, help="Benchmark iterations")
parser.add_argument("--nhead", type=int, default=8)
parser.add_argument("--hdim", type=int, default=128)
args = parser.parse_args()
print("=" * 70)
print("Example 36: Backward Pass Benchmark")
print("=" * 70)
print(f"\n Arch: {args.arch}")
print(f" nhead: {args.nhead}")
print(f" hdim: {args.hdim}")
print(f" Repeat: {args.repeat}")
# --- JIT compile a basic fp16 h128 fwd kernel ---
print("\n--- JIT Compilation ---")
config = FmhaKernelConfig(
data_type="fp16",
hdim_q=args.hdim,
hdim_v=args.hdim,
gfx_arch=args.arch,
)
setup = setup_fmha_dispatcher(config)
if setup.success:
print(f" Fwd kernel compiled: {setup.build_time_s:.1f}s")
print(" Backward GPU kernel: Not available (bwd JIT tile structure issue)")
print(" Benchmarking CPU backward reference instead")
else:
print(f" JIT build: {setup.error}")
print(" Benchmarking CPU backward reference")
# --- Benchmark configs ---
bench_configs = [
(1, 64),
(1, 128),
(1, 256),
(1, 512),
(1, 1024),
(2, 64),
(2, 128),
(2, 256),
(2, 512),
(4, 64),
(4, 128),
(4, 256),
(8, 64),
(8, 128),
]
# --- FLOPS estimate table ---
print("\n--- FLOPS Estimates (BWD ~4x FWD) ---")
print(
f"\n {'Batch':>5} {'SeqLen':>7} | {'FWD FLOPS':>14} {'BWD FLOPS':>14} {'Ratio':>6}"
)
print(" " + "-" * 52)
for batch, seqlen in [(1, 128), (1, 1024), (4, 256), (8, 128)]:
prob = FmhaProblem(
batch=batch,
nhead_q=args.nhead,
nhead_k=args.nhead,
seqlen_q=seqlen,
seqlen_k=seqlen,
hdim_q=args.hdim,
hdim_v=args.hdim,
)
fwd_ops = prob.num_ops
bwd_ops = bwd_flops(prob)
print(
f" {batch:>5} {seqlen:>7} | {fwd_ops:>14,} {bwd_ops:>14,} {bwd_ops / fwd_ops:>5.1f}x"
)
# --- CPU backward benchmark ---
print("\n--- CPU Backward Benchmark ---")
print(
f"\n {'Batch':>5} {'SeqLen':>7} | {'Time(ms)':>10} {'TFLOPS':>10}"
f" | {'dQ range':>22} {'Finite':>6}"
)
print(" " + "-" * 76)
all_tflops = []
for batch, seqlen in bench_configs:
prob = FmhaProblem(
batch=batch,
nhead_q=args.nhead,
nhead_k=args.nhead,
seqlen_q=seqlen,
seqlen_k=seqlen,
hdim_q=args.hdim,
hdim_v=args.hdim,
)
ops = bwd_flops(prob)
np.random.seed(42)
Q = (np.random.randn(*prob.q_shape()) * 0.1).astype(np.float32)
K = (np.random.randn(*prob.k_shape()) * 0.1).astype(np.float32)
V = (np.random.randn(*prob.v_shape()) * 0.1).astype(np.float32)
dO = (np.random.randn(*prob.o_shape()) * 0.1).astype(np.float32)
out, P = cpu_fwd_with_intermediates(Q, K, V, prob.scale)
times = []
dQ = dK = dV = None
for _ in range(args.repeat):
t0 = time.perf_counter()
dQ, dK, dV = cpu_attention_bwd(Q, K, V, out, dO, P, prob.scale)
t1 = time.perf_counter()
times.append((t1 - t0) * 1000.0)
avg_ms = sum(times) / len(times)
tflops = ops / (avg_ms * 1e-3) / 1e12 if avg_ms > 0 else 0.0
all_tflops.append(tflops)
is_finite = bool(np.all(np.isfinite(dQ)))
dq_range = f"[{dQ.min():.4e}, {dQ.max():.4e}]"
print(
f" {batch:>5} {seqlen:>7} | {avg_ms:>10.4f} {tflops:>10.4f}"
f" | {dq_range:>22} {'OK' if is_finite else 'NaN!':>6}"
)
# --- Scaling analysis ---
print("\n--- Scaling Analysis ---")
print(" Backward time should scale as O(B * H * Sq * Sk * D).")
print(" Doubling seqlen -> ~4x time (quadratic in sequence length).\n")
ref_configs = [(1, 128), (1, 256), (1, 512)]
ref_times = {}
for batch, seqlen in ref_configs:
prob = FmhaProblem(
batch=batch,
nhead_q=args.nhead,
nhead_k=args.nhead,
seqlen_q=seqlen,
seqlen_k=seqlen,
hdim_q=args.hdim,
hdim_v=args.hdim,
)
np.random.seed(42)
Q = (np.random.randn(*prob.q_shape()) * 0.1).astype(np.float32)
K = (np.random.randn(*prob.k_shape()) * 0.1).astype(np.float32)
V = (np.random.randn(*prob.v_shape()) * 0.1).astype(np.float32)
dO = (np.random.randn(*prob.o_shape()) * 0.1).astype(np.float32)
out, P = cpu_fwd_with_intermediates(Q, K, V, prob.scale)
t0 = time.perf_counter()
cpu_attention_bwd(Q, K, V, out, dO, P, prob.scale)
ref_times[seqlen] = (time.perf_counter() - t0) * 1000.0
if 128 in ref_times and ref_times[128] > 0:
base = ref_times[128]
print(f" {'SeqLen':>7} {'Time(ms)':>10} {'vs S=128':>10}")
print(" " + "-" * 30)
for sl in sorted(ref_times):
ratio = ref_times[sl] / base
print(f" {sl:>7} {ref_times[sl]:>10.4f} {ratio:>9.1f}x")
# --- Summary ---
print("\n" + "=" * 70)
print(f" Configs tested: {len(bench_configs)}")
print(" BWD FLOPS: ~4x forward FLOPS")
if all_tflops:
print(f" CPU avg: {sum(all_tflops) / len(all_tflops):.4f} TFLOPS")
print(f" CPU peak: {max(all_tflops):.4f} TFLOPS")
print(" GPU: Requires bwd-family JIT kernel")
print(" Status: DEMO")
print("=" * 70)
return 0
if __name__ == "__main__":
sys.exit(main())