Files
composable_kernel/tile_engine/ops/fmha/run_full_sweep.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

176 lines
5.0 KiB
Python

#!/usr/bin/env python3
# Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
# SPDX-License-Identifier: MIT
"""
Full FMHA benchmark sweep, organized by variant and dtype.
Compiles all kernels per variant (shared build dir for caching),
benchmarks against all smoke shapes, then splits results into:
<output_dir>/
fwd/fp16/results.csv
fwd/bf16/results.csv
splitkv/fp16/results.csv
...
bwd_dot_do_o/fp16/results.csv
bwd_dq_dk_dv/fp16/results.csv
bwd_convert_dq/fp16/results.csv
Usage:
python run_full_sweep.py --workers 256
python run_full_sweep.py --workers 256 --category full --output /tmp/fmha_sweep
"""
import argparse
import csv
import os
import subprocess
import sys
import time
from collections import defaultdict
from pathlib import Path
_THIS_DIR = Path(__file__).resolve().parent
VARIANTS = ["fwd", "splitkv", "pagedkv", "appendkv", "batch_prefill", "bwd"]
BWD_FAMILIES = ["bwd_dot_do_o", "bwd_dq_dk_dv", "bwd_convert_dq"]
def run_variant(variant, category, workers, build_dir, raw_csv, shape_timeout=600):
"""Run fmha_full_benchmark.py for one variant, return path to raw CSV."""
cmd = [
sys.executable,
str(_THIS_DIR / "fmha_full_benchmark.py"),
"--category",
category,
"--variant",
variant,
"--workers",
str(workers),
"--build-dir",
str(build_dir),
"--csv",
str(raw_csv),
"--json",
str(raw_csv.with_suffix(".json")),
"--shape-timeout",
str(shape_timeout),
]
print(f"\n{'=' * 80}")
print(f" Variant: {variant}")
print(f" Command: {' '.join(cmd)}")
print(f"{'=' * 80}", flush=True)
env = os.environ.copy()
env["PYTHONUNBUFFERED"] = "1"
proc = subprocess.run(cmd, env=env)
return proc.returncode
def split_csv(raw_csv, output_dir):
"""Split a raw CSV into per-family per-dtype subdirectories."""
if not raw_csv.exists():
return {}
counts = defaultdict(int)
writers = {}
files = {}
with open(raw_csv, newline="") as f:
reader = csv.DictReader(f)
fieldnames = reader.fieldnames
for row in reader:
family = row.get("family", "unknown")
dtype = row.get("dtype", "unknown")
key = (family, dtype)
if key not in writers:
d = output_dir / family / dtype
d.mkdir(parents=True, exist_ok=True)
fh = open(d / "results.csv", "w", newline="")
w = csv.DictWriter(fh, fieldnames=fieldnames)
w.writeheader()
writers[key] = w
files[key] = fh
writers[key].writerow(row)
counts[key] += 1
for fh in files.values():
fh.close()
return counts
def main():
p = argparse.ArgumentParser(
description="Full FMHA Sweep (organized by variant/dtype)"
)
p.add_argument("--workers", type=int, default=256)
p.add_argument("--category", default="smoke", choices=["smoke", "full", "nightly"])
p.add_argument("--output", default="/tmp/fmha_sweep")
p.add_argument("--build-dir", default="/tmp/fmha_sweep_build")
p.add_argument(
"--variants",
nargs="+",
default=VARIANTS,
choices=VARIANTS,
help="Which variants to run",
)
p.add_argument(
"--shape-timeout", type=int, default=600, help="Per-shape timeout in seconds"
)
args = p.parse_args()
output_dir = Path(args.output)
build_dir = Path(args.build_dir)
output_dir.mkdir(parents=True, exist_ok=True)
build_dir.mkdir(parents=True, exist_ok=True)
t0 = time.perf_counter()
grand_total = defaultdict(int)
for variant in args.variants:
raw_csv = output_dir / f"_raw_{variant}.csv"
rc = run_variant(
variant, args.category, args.workers, build_dir, raw_csv, args.shape_timeout
)
if rc != 0:
print(f"\n WARNING: {variant} exited with code {rc}", flush=True)
counts = split_csv(raw_csv, output_dir)
for key, n in counts.items():
grand_total[key] += n
family, dtype = key
print(f" {family}/{dtype}: {n} measurements")
elapsed = time.perf_counter() - t0
print(f"\n{'=' * 80}")
print("SWEEP COMPLETE")
print(f"{'=' * 80}")
print(f" Total time: {elapsed / 60:.1f} min")
print(f" Output dir: {output_dir}")
print()
print(f" {'Family':<25} {'Dtype':<10} {'Measurements':>12}")
print(f" {'-' * 25} {'-' * 10} {'-' * 12}")
total = 0
for (family, dtype), n in sorted(grand_total.items()):
print(f" {family:<25} {dtype:<10} {n:>12,}")
total += n
print(f" {'-' * 25} {'-' * 10} {'-' * 12}")
print(f" {'TOTAL':<25} {'':<10} {total:>12,}")
print("\n Directory structure:")
for d in sorted(output_dir.rglob("results.csv")):
rel = d.relative_to(output_dir)
print(f" {rel}")
if __name__ == "__main__":
main()