Skip to content

Commit 86591de

Browse files
[rocm-libraries] ROCm/rocm-libraries#5260 (commit a1834d2)
[CK] [CK_Tile] Add FMHA scaffolding to CK kernel dispatcher (#5260) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 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.
1 parent 61b019f commit 86591de

148 files changed

Lines changed: 41248 additions & 85 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎dispatcher/CMakeLists.txt‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ endif()
2121
add_library(ck_tile_dispatcher
2222
src/registry.cpp
2323
src/dispatcher.cpp
24+
src/fmha_registry.cpp
25+
src/fmha_dispatcher.cpp
2426
)
2527

2628
# Enable PIC for Python bindings
@@ -34,13 +36,21 @@ target_include_directories(ck_tile_dispatcher
3436
$<INSTALL_INTERFACE:include>
3537
)
3638

37-
# Link against CK Tile headers (header-only)
39+
# CK Tile core headers (ck_tile/core, ck_tile/ops, etc.)
3840
target_include_directories(ck_tile_dispatcher
3941
PUBLIC
4042
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
4143
$<INSTALL_INTERFACE:include>
4244
)
4345

46+
# CK project root -- needed only for FMHA generated wrappers that include
47+
# "example/ck_tile/01_fmha/fmha_fwd.hpp". PRIVATE to avoid exposing the
48+
# entire project tree to downstream consumers.
49+
target_include_directories(ck_tile_dispatcher
50+
PRIVATE
51+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
52+
)
53+
4454
# Link against HIP headers if available
4555
if(hip_FOUND)
4656
target_link_libraries(ck_tile_dispatcher PUBLIC hip::host)

‎dispatcher/README.md‎

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,12 @@ python3 examples/grouped_conv/python/03_bwd_data.py # Backward data +
394394
python3 examples/grouped_conv/python/04_bwd_weight.py # Backward weight + CPU ref
395395
python3 examples/grouped_conv/python/05_benchmark.py # Multi-problem benchmark
396396
python3 examples/grouped_conv/python/06_registry_json.py # Heuristic selection + JSON
397+
398+
# FMHA Examples (JIT-compiled on the fly)
399+
python3 examples/fmha/python/01_basic_fmha.py # Basic forward attention
400+
python3 examples/fmha/python/12_masks_fmha.py # Causal masks
401+
python3 examples/fmha/python/18_backward_fmha.py # Backward pass
402+
python3 examples/fmha/python/16_splitkv_fmha.py # Split-KV for long sequences
397403
```
398404

399405
### Example Output
@@ -716,7 +722,7 @@ This matrix shows all CK Tile operations with per-data-type, per-layout, and per
716722
| GEMM | streamk_gemm<br>example: `40_streamk_gemm/` | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
717723
| Reduce | multi_reduce2d<br>example: `05_reduce/` | ❌ | | ❌ | | | | | | | | | ❌ | ❌ | ❌ | ❌ |
718724
| Reduce | reduce2d<br>example: `05_reduce/` | ❌ | | ❌ | | | | | | | | | ❌ | ❌ | ❌ | ❌ |
719-
| Attention | fmha<br>example: `01_fmha/` | ❌ | ❌ | ❌ | ❌ | ❌ | | | | | | | ❌ | ❌ | ❌ | ❌ |
725+
| Attention | fmha<br>example: `01_fmha/` | ✅ | ✅ | ✅ | ✅ | ❌ | | | | | | | ✅ | ✅ | ✅ | ❌ |
720726
| Attention | sparse_attn<br>example: `50_sparse_attn/` | ❌ | | ❌ | | ❌ | | | | | | | ❌ | ❌ | ❌ | ❌ |
721727
| Activation | softmax | ❌ | | ❌ | | | | | | | | | ❌ | ❌ | ❌ | ❌ |
722728
| Activation | topk_softmax<br>example: `09_topk_softmax/` | ❌ | ❌ | ❌ | | | | | | | | | ❌ | ❌ | ❌ | ❌ |
@@ -871,36 +877,52 @@ dispatcher/
871877
| |---- grouped_conv_problem.hpp # Grouped conv problem (with builder)
872878
| |---- grouped_conv_kernel_decl.hpp # Grouped conv kernel declarations
873879
| |---- grouped_conv_registry.hpp # Grouped conv registry (thread-safe)
874-
| +---- grouped_conv_utils.hpp # Grouped conv utilities
880+
| |---- grouped_conv_utils.hpp # Grouped conv utilities
881+
| |---- fmha_types.hpp # FMHA fwd/bwd args and traits structs
882+
| |---- fmha_problem.hpp # FmhaProblem, FmhaProblemBuilder
883+
| |---- fmha_kernel_key.hpp # FmhaKernelKey (Signature + Algorithm)
884+
| |---- fmha_kernel_instance.hpp # FmhaKernelInstance virtual interface
885+
| |---- fmha_kernel_decl.hpp # Declarative FmhaSignature/FmhaAlgorithm
886+
| |---- fmha_registry.hpp # FmhaRegistry (thread-safe)
887+
| +---- fmha_dispatcher.hpp # FmhaDispatcher (plan, select, run)
875888
|
876889
|---- src/ # C++ implementation
877890
|
878891
|---- codegen/ # Kernel generation
879892
| |---- codegen_common.py # Shared: TileConfig, TraitConfigBase, type mappings
880893
| |---- unified_gemm_codegen.py # GEMM kernel generator
881894
| |---- unified_grouped_conv_codegen.py # Grouped conv kernel generator
895+
| |---- unified_fmha_codegen.py # FMHA kernel generator
896+
| |---- fmha_arch_specs.json # FMHA per-arch tile/pipeline specs
897+
| |---- fmha_rules.py # FMHA validation rules
898+
| |---- fmha_profiles.py # FMHA named profiles/receipts
882899
| +---- arch_specs.json # GPU specifications
883900
|
884901
|---- python/ # Python utilities
885902
| |---- dispatcher_common.py # Shared: paths, validation, Colors, phased output
886903
| |---- ctypes_utils.py # GEMM ctypes utilities
887-
| +---- grouped_conv_utils.py # Grouped conv utilities
904+
| |---- grouped_conv_utils.py # Grouped conv utilities
905+
| +---- fmha_utils.py # FMHA: JIT compile, FmhaRunner, FmhaKernelConfig
888906
|
889907
|---- scripts/ # Build scripts
890908
| |---- compile_gemm_examples.py # GEMM build script
891909
| +---- compile_grouped_conv_examples.py # Grouped conv build script
892910
|
893911
|---- bindings/ctypes/ # Python ctypes interface
894912
| |---- gemm_ctypes_lib.cpp # GEMM Python library
895-
| +---- conv_ctypes_lib.cpp # Grouped conv Python library
913+
| |---- conv_ctypes_lib.cpp # Grouped conv Python library
914+
| +---- fmha_ctypes_lib.cpp # FMHA Python library
896915
|
897916
|---- examples/ # Examples
898917
| |---- gemm/
899918
| | |---- cpp/ # C++ GEMM examples (01-07)
900919
| | +---- python/ # Python GEMM examples (01-11)
901-
| +---- grouped_conv/
902-
| |---- cpp/ # C++ Grouped Conv examples (01-07)
903-
| +---- python/ # Python Grouped Conv examples (01-06)
920+
| |---- grouped_conv/
921+
| | |---- cpp/ # C++ Grouped Conv examples (01-07)
922+
| | +---- python/ # Python Grouped Conv examples (01-06)
923+
| +---- fmha/
924+
| |---- cpp/ # C++ FMHA examples (01-35)
925+
| +---- python/ # Python FMHA examples (01-38)
904926
|
905927
+---- tests/ # Unit tests (C++ and Python)
906928
```
@@ -913,6 +935,8 @@ dispatcher/
913935
|-----------|--------|
914936
| GEMM C++ | [examples/gemm/cpp/README.md](examples/gemm/cpp/README.md) |
915937
| GEMM Python | [examples/gemm/python/README.md](examples/gemm/python/README.md) |
938+
| FMHA C++ | examples/fmha/cpp/ (35 examples covering all FMHA variants) |
939+
| FMHA Python | examples/fmha/python/ (38 examples with JIT compilation) |
916940
| Codegen | [codegen/README.md](codegen/README.md) |
917941
| Python Utils | [python/README.md](python/README.md) |
918942
| C++ Headers | [include/ck_tile/dispatcher/README.md](include/ck_tile/dispatcher/README.md) |

‎dispatcher/bindings/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ bindings/
1010
| |---- gemm_ctypes_lib.cpp # GEMM dispatcher C API
1111
| |---- conv_ctypes_lib.cpp # Grouped conv dispatcher C API (fwd + bwd_data)
1212
| |---- conv_bwdw_ctypes_lib.cpp # Grouped conv backward weight C API (separate library)
13+
| |---- fmha_ctypes_lib.cpp # FMHA dispatcher C API (fwd + bwd)
1314
| |---- gpu_helper.cpp # CLI helper for Python
1415
| +---- CMakeLists.txt
1516
+---- README.md

0 commit comments

Comments
 (0)