mirror of
https://github.com/ROCm/composable_kernel.git
synced 2026-07-03 05:37:34 +00:00
[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>
184 lines
7.9 KiB
C++
184 lines
7.9 KiB
C++
// Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
|
|
// SPDX-License-Identifier: MIT
|
|
//
|
|
// Example 34: FMHA Backward with GQA (Grouped Query Attention)
|
|
//
|
|
// Demonstrates backward with nhead_q=8, nhead_k=2 (4:1 ratio). GQA is a
|
|
// runtime property: each KV head is shared by multiple Q heads. Backward
|
|
// handles head indexing via nhead_stride_dk/dv so dK/dV are reduced across
|
|
// the Q-head group. Planning only.
|
|
|
|
#include <iostream>
|
|
|
|
#include "ck_tile/dispatcher.hpp"
|
|
#include "ck_tile/dispatcher/example_args.hpp"
|
|
|
|
using namespace ck_tile::dispatcher;
|
|
using namespace ck_tile::dispatcher::utils;
|
|
|
|
DECL_FMHA_KERNEL_SET(bwd_gqa_fmha_kernels,
|
|
.add(FmhaSignature()
|
|
.family("fwd")
|
|
.dtype("fp16")
|
|
.mode("batch")
|
|
.vlayout("r")
|
|
.hdim(128)
|
|
.mask("top_left")
|
|
.bias("no")
|
|
.lse(true)
|
|
.dropout(false)
|
|
.qscale("no"),
|
|
FmhaAlgorithm()
|
|
.tile_m0(128)
|
|
.tile_n0(128)
|
|
.tile_k0(32)
|
|
.tile_n1(128)
|
|
.tile_k1(32)
|
|
.tile_k0max(128)
|
|
.wave_m0(4)
|
|
.wave_n0(1)
|
|
.wave_k0(1)
|
|
.wave_m1(4)
|
|
.wave_n1(1)
|
|
.wave_k1(1)
|
|
.warp_m0(32)
|
|
.warp_n0(32)
|
|
.warp_k0(16)
|
|
.warp_m1(32)
|
|
.warp_n1(32)
|
|
.warp_k1(16)
|
|
.pipeline("qr_async")
|
|
.padding(true, true, true, true)
|
|
.alignments(128, 128)
|
|
.selection_rank(0),
|
|
"gfx950")
|
|
.add(FmhaSignature()
|
|
.family("bwd_dot_do_o")
|
|
.dtype("fp16")
|
|
.mode("batch")
|
|
.hdim(128)
|
|
.mask("top_left")
|
|
.bias("no")
|
|
.dropout(false)
|
|
.dbias(false)
|
|
.store_randval(false)
|
|
.deterministic(false),
|
|
FmhaAlgorithm()
|
|
.tile_m0(64)
|
|
.tile_n0(128)
|
|
.tile_k0(32)
|
|
.tile_n1(0)
|
|
.tile_k1(0)
|
|
.tile_k0max(0)
|
|
.padding(true, true, true, true)
|
|
.selection_rank(0),
|
|
"gfx950")
|
|
.add(FmhaSignature()
|
|
.family("bwd_dq_dk_dv")
|
|
.dtype("fp16")
|
|
.mode("batch")
|
|
.hdim(128)
|
|
.mask("top_left")
|
|
.bias("no")
|
|
.dropout(false)
|
|
.dbias(false)
|
|
.store_randval(false)
|
|
.deterministic(false),
|
|
FmhaAlgorithm()
|
|
.tile_m0(16)
|
|
.tile_n0(128)
|
|
.tile_k0(128)
|
|
.tile_n1(16)
|
|
.tile_k1(128)
|
|
.tile_k0max(32)
|
|
.wave(1, 4, 1, 4, 1, 1, 1, 4, 1)
|
|
.warp(16, 16, 32, 16, 16, 16, 16, 16, 16)
|
|
.padding(true, true, true, true)
|
|
.max_seq_len_q(0)
|
|
.selection_rank(0),
|
|
"gfx950")
|
|
.add(FmhaSignature()
|
|
.family("bwd_convert_dq")
|
|
.dtype("fp16")
|
|
.mode("batch")
|
|
.hdim(128)
|
|
.mask("top_left")
|
|
.bias("no")
|
|
.dropout(false)
|
|
.dbias(false)
|
|
.store_randval(false)
|
|
.deterministic(false),
|
|
FmhaAlgorithm()
|
|
.tile_m0(64)
|
|
.tile_n0(128)
|
|
.tile_k0(0)
|
|
.tile_n1(0)
|
|
.tile_k1(0)
|
|
.tile_k0max(0)
|
|
.padding(true, true, true, true)
|
|
.selection_rank(0),
|
|
"gfx950"));
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
ExampleArgs args("Example 34: FMHA Backward GQA", "nhead_q=8, nhead_k=2 (4:1 ratio)");
|
|
args.add_option("--arch", "gfx950", "GPU architecture");
|
|
args.add_option("--batch", "2", "Batch size");
|
|
args.add_option("--nhead_q", "8", "Query heads");
|
|
args.add_option("--nhead_k", "2", "KV heads (GQA ratio = nhead_q/nhead_k)");
|
|
args.add_option("--seqlen", "128", "Sequence length");
|
|
args.add_option("--hdim", "128", "Head dimension");
|
|
if(!args.parse(argc, argv))
|
|
return 0;
|
|
|
|
const std::string gfx_arch = args.get("--arch", "gfx950");
|
|
const int batch = args.get_int("--batch", 2);
|
|
const int nhead_q = args.get_int("--nhead_q", 8);
|
|
const int nhead_k = args.get_int("--nhead_k", 2);
|
|
const int seqlen = args.get_int("--seqlen", 128);
|
|
const int hdim = args.get_int("--hdim", 128);
|
|
|
|
print_header("Example 34: FMHA Backward GQA");
|
|
|
|
std::cout << "\nStep 1: Register Kernels\n";
|
|
FmhaRegistry registry;
|
|
registry.set_name("bwd_gqa_fmha");
|
|
REGISTER_GENERATED_KERNELS(registry, gfx_arch);
|
|
std::cout << " Registered " << registry.size() << " kernel(s)\n";
|
|
|
|
std::cout << "\nStep 2: Plan (GQA nhead_q=" << nhead_q << ", nhead_k=" << nhead_k << ")\n";
|
|
FmhaDispatcher dispatcher(®istry);
|
|
fmha_bwd_traits traits{};
|
|
traits.hdim_q = hdim;
|
|
traits.hdim_v = hdim;
|
|
traits.data_type = "fp16";
|
|
traits.is_group_mode = false;
|
|
traits.mask_type = mask_enum::mask_top_left;
|
|
traits.bias_type = bias_enum::no_bias;
|
|
traits.has_dbias = false;
|
|
traits.has_dropout = false;
|
|
traits.is_store_randval = false;
|
|
traits.is_deterministic = false;
|
|
|
|
fmha_bwd_args bwd_args{};
|
|
bwd_args.batch = batch;
|
|
bwd_args.seqlen_q = seqlen;
|
|
bwd_args.seqlen_k = seqlen;
|
|
bwd_args.hdim_q = hdim;
|
|
bwd_args.hdim_v = hdim;
|
|
bwd_args.nhead_q = nhead_q;
|
|
bwd_args.nhead_k = nhead_k;
|
|
|
|
auto plan = dispatcher.plan(
|
|
FmhaProblem::from_invocation(FmhaInvocation::make(traits, bwd_args), gfx_arch));
|
|
std::cout << " Plan valid: " << (plan.is_valid() ? "yes" : "no") << "\n";
|
|
|
|
std::cout << "\nStep 3: GQA Backward Head Indexing\n";
|
|
std::cout << " Q heads " << nhead_q << ", KV heads " << nhead_k
|
|
<< " -> each KV head shared by " << (nhead_q / nhead_k) << " Q heads.\n";
|
|
std::cout << " dK/dV reduced across Q-head group via nhead_stride.\n";
|
|
|
|
print_separator();
|
|
return 0;
|
|
}
|