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

95 lines
3.1 KiB
CMake

# Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
# SPDX-License-Identifier: MIT
# FMHA Tile Engine -- Pure Python benchmarking via the CK dispatcher.
# No C++ per-kernel targets; all compilation is JIT via the dispatcher.
set(FMHA_TE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(FMHA_TE_CONFIGS ${FMHA_TE_DIR}/configs)
include(ProcessorCount)
ProcessorCount(NPROC)
if(NPROC EQUAL 0)
set(NPROC 8)
endif()
# Use first arch from SUPPORTED_GPU_TARGETS, or fallback to gfx950
set(FMHA_BENCH_ARCH "gfx950")
if(SUPPORTED_GPU_TARGETS)
list(GET SUPPORTED_GPU_TARGETS 0 FMHA_BENCH_ARCH)
endif()
# Main benchmark target (runs forward sweep by default)
add_custom_target(benchmark_fmha
COMMAND ${Python3_EXECUTABLE} ${FMHA_TE_DIR}/fmha_benchmark.py
${FMHA_TE_CONFIGS}/fwd.json
--arch ${FMHA_BENCH_ARCH}
--workers ${NPROC}
--best
--json ${CMAKE_CURRENT_BINARY_DIR}/fmha_fwd_results.json
WORKING_DIRECTORY ${FMHA_TE_DIR}
COMMENT "FMHA tile engine benchmark (forward)"
)
if(TARGET ck_tile_dispatcher)
add_dependencies(benchmark_fmha ck_tile_dispatcher)
endif()
# Per-variant convenience targets
foreach(variant fwd bwd splitkv appendkv pagedkv batch_prefill)
if(EXISTS ${FMHA_TE_CONFIGS}/${variant}.json)
add_custom_target(benchmark_fmha_${variant}
COMMAND ${Python3_EXECUTABLE} ${FMHA_TE_DIR}/fmha_benchmark.py
${FMHA_TE_CONFIGS}/${variant}.json
--arch ${FMHA_BENCH_ARCH}
--workers ${NPROC}
--best
--json ${CMAKE_CURRENT_BINARY_DIR}/fmha_${variant}_results.json
WORKING_DIRECTORY ${FMHA_TE_DIR}
COMMENT "FMHA tile engine benchmark (${variant})"
)
if(TARGET ck_tile_dispatcher)
add_dependencies(benchmark_fmha_${variant} ck_tile_dispatcher)
endif()
endif()
endforeach()
# CI target (minimal sweep for quick validation)
if(EXISTS ${FMHA_TE_CONFIGS}/fwd_ci.json)
add_custom_target(benchmark_fmha_ci
COMMAND ${Python3_EXECUTABLE} ${FMHA_TE_DIR}/fmha_benchmark.py
${FMHA_TE_CONFIGS}/fwd_ci.json
--arch ${FMHA_BENCH_ARCH}
--workers 8
--verify
WORKING_DIRECTORY ${FMHA_TE_DIR}
COMMENT "FMHA tile engine CI benchmark"
)
if(TARGET ck_tile_dispatcher)
add_dependencies(benchmark_fmha_ci ck_tile_dispatcher)
endif()
endif()
# All-variants target
set(FMHA_ALL_CONFIGS "")
foreach(cfg fwd bwd splitkv appendkv pagedkv batch_prefill)
if(EXISTS ${FMHA_TE_CONFIGS}/${cfg}.json)
list(APPEND FMHA_ALL_CONFIGS ${FMHA_TE_CONFIGS}/${cfg}.json)
endif()
endforeach()
add_custom_target(benchmark_fmha_all
COMMAND ${Python3_EXECUTABLE} ${FMHA_TE_DIR}/fmha_benchmark.py
${FMHA_ALL_CONFIGS}
--arch ${FMHA_BENCH_ARCH}
--workers ${NPROC}
--best
--json ${CMAKE_CURRENT_BINARY_DIR}/fmha_all_results.json
WORKING_DIRECTORY ${FMHA_TE_DIR}
COMMENT "FMHA tile engine benchmark (all variants)"
)
if(TARGET ck_tile_dispatcher)
add_dependencies(benchmark_fmha_all ck_tile_dispatcher)
endif()