Files
composable_kernel/dispatcher/scripts/generate_conv_dispatch_header.py
Vidyasagar Ananthan 920acd2c12 [rocm-libraries] ROCm/rocm-libraries#5168 (commit 8b5afcb)
[CK] [CK_Tile] Add GroupConv to Kernel Dispatcher

## Motivation

This PR adds CK Tile group convolution (forward, backward-data,
backward-weight) support to the kernel dispatcher, matching and unifying
with the existing dispatcher GEMM infrastructure in architecture and
usability. The dispatcher provides a unified kernel dispatch system with
both C++ and Python frontends, and until now only supported GEMM
operations. This PR enables framework integrators to use the same
declarative kernel workflow for convolutions as they do for GEMM:
declare kernels, build a registry JIT, select kernels within the
registry at runtime, and dispatch to GPU. Future PRs will include
runtime kernel selection heuristics for autotuning of kernel parameters
based on (problem, hardware arch).

## Technical Details

Grouped convolution support has been added to the CK Tile Dispatcher
with generated_conv_backend.hpp enabling dispatcher.run(in, wei, out,
problem) for all 6 conv variants (fwd/bwdd/bwdw x 2D/3D), runtime
heuristic kernel selection, and GroupedConvKernelKey with full
ConvConfigBase fields. Python side adds parallel JIT via
registry.build(max_workers) and heuristic registry.select(). Includes 7
C++ and 6 Python examples covering all directions with CPU reference
validation, and shared infrastructure improvements (BaseRegistry CRTP,
structured exceptions). As a sanity check, JIT compile times for a
single kernel remains the same and for multiple kernels there is better
parallelism:
Kernels | 1 worker | 8 workers
1 | 7.7 s | 7.7 s
2 | 15.9 s | 8.2 s
4 | 33.4 s | 9.7 s
6 | 52.3 s | 10.2 s

## Test Plan

145 ephemeral unit tests have been added to test basic functionality.
All 30 examples/integration tests run end-to-end on gfx950 (MI350): 7
C++ conv, 7 C++ GEMM, 6 Python conv, 10 Python GEMM. CPU reference
validation for forward, backward-data, and backward-weight (2D) in both
C++ and Python examples pass.

## Test Result

30 examples pass. Peak performance: 132 TFLOPS (Batch-32 forward 56x56),
53 TFLOPS (pointwise 1x1). CPU reference accuracy: max_abs_diff < 0.002
for all directions (fp16 vs fp32 reference).

## Submission Checklist

- [x] Look over the contributing guidelines at
https://github.com/ROCm/ROCm/blob/develop/CONTRIBUTING.md#pull-requests.
2026-04-09 17:39:35 +00:00

108 lines
3.2 KiB
Python

#!/usr/bin/env python3
# Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
# SPDX-License-Identifier: MIT
"""Generate the conv_python_dispatch.hpp header for the Python conv library.
Reads the include_all headers to find available kernels and creates dispatch
aliases for 2D/3D x fwd/bwd_data/bwd_weight.
"""
import argparse
import re
from pathlib import Path
def find_3d_launcher(include_all_path: Path, variant_prefix: str) -> str:
"""Find first 3D launcher name from an include_all header."""
text = include_all_path.read_text()
pattern = rf"(grouped_conv_{variant_prefix}_\w+_3d_\w+)\.hpp"
match = re.search(pattern, text)
if match:
return match.group(1) + "_Launcher"
return ""
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--kernel-dir", required=True)
parser.add_argument("--output", required=True)
args = parser.parse_args()
kdir = Path(args.kernel_dir)
fwd_3d = find_3d_launcher(kdir / "include_all_grouped_conv_fwd_kernels.hpp", "fwd")
bwd_data_3d = find_3d_launcher(
kdir / "include_all_grouped_conv_bwd_data_kernels.hpp", "bwd_data"
)
bwd_weight_3d = find_3d_launcher(
kdir / "include_all_grouped_conv_bwd_weight_kernels.hpp", "bwd_weight"
)
lines = [
"// Auto-generated dispatch header for Python conv library",
"#pragma once",
"",
"// Forward kernels",
'#include "include_all_grouped_conv_fwd_kernels.hpp"',
"#define CONV_FWD_2D_AVAILABLE 1",
]
if fwd_3d:
lines += [
"#define CONV_FWD_3D_AVAILABLE 1",
f"using ConvFwd3dLauncher = {fwd_3d};",
]
lines += [
"",
"// Backward data kernels",
'#include "include_all_grouped_conv_bwd_data_kernels.hpp"',
"#define CONV_BWD_DATA_2D_AVAILABLE 1",
]
if bwd_data_3d:
lines += [
"#define CONV_BWD_DATA_3D_AVAILABLE 1",
f"using ConvBwdData3dLauncher = {bwd_data_3d};",
]
lines += [
"",
"// Backward weight kernels",
'#include "include_all_grouped_conv_bwd_weight_kernels.hpp"',
"#define CONV_BWD_WEIGHT_2D_AVAILABLE 1",
]
if bwd_weight_3d:
lines += [
"#define CONV_BWD_WEIGHT_3D_AVAILABLE 1",
f"using ConvBwdWeight3dLauncher = {bwd_weight_3d};",
]
# Kernel name table for Python introspection
names = []
if True: # fwd 2D always present
names.append('"fwd_2d"')
if fwd_3d:
names.append('"fwd_3d"')
if True: # bwd_data 2D
names.append('"bwd_data_2d"')
if bwd_data_3d:
names.append('"bwd_data_3d"')
if True: # bwd_weight 2D
names.append('"bwd_weight_2d"')
if bwd_weight_3d:
names.append('"bwd_weight_3d"')
lines += [
"",
"// Kernel inventory for Python",
f"static const char* CONV_KERNEL_NAMES[] = {{{', '.join(names)}}};",
f"static const int CONV_KERNEL_COUNT = {len(names)};",
"",
]
Path(args.output).write_text("\n".join(lines) + "\n")
print(f"Generated dispatch header: {args.output} ({len(names)} kernels)")
if __name__ == "__main__":
main()