mirror of
https://github.com/ROCm/composable_kernel.git
synced 2026-05-12 09:16:52 +00:00
[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.
147 lines
3.9 KiB
Python
147 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
"""
|
|
Example 02: Batch GEMM
|
|
|
|
Runs multiple GEMM operations with different sizes using JIT compilation.
|
|
|
|
Usage:
|
|
python3 02_batch_gemm.py
|
|
python3 02_batch_gemm.py --help
|
|
python3 02_batch_gemm.py --dtype bf16
|
|
"""
|
|
|
|
import sys
|
|
import argparse
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent / "python"))
|
|
import numpy as np
|
|
|
|
from ctypes_utils import (
|
|
KernelConfig,
|
|
Registry,
|
|
detect_gpu_arch,
|
|
)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
description="Batch GEMM Example - runs multiple sizes",
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
epilog="""
|
|
Examples:
|
|
python3 02_batch_gemm.py # Default FP16
|
|
python3 02_batch_gemm.py --dtype bf16 # BF16 GEMM
|
|
python3 02_batch_gemm.py --max-size 2048 # Limit max size
|
|
""",
|
|
)
|
|
parser.add_argument(
|
|
"--dtype",
|
|
default="fp16",
|
|
choices=["fp16", "bf16", "fp32"],
|
|
help="Data type (default: fp16)",
|
|
)
|
|
parser.add_argument(
|
|
"--max-size",
|
|
type=int,
|
|
default=4096,
|
|
help="Maximum problem size (default: 4096)",
|
|
)
|
|
parser.add_argument(
|
|
"--arch",
|
|
default=detect_gpu_arch(),
|
|
help="Target architecture (auto-detected from rocminfo)",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
print("=" * 60)
|
|
print("Example 02: Batch GEMM")
|
|
print("=" * 60)
|
|
|
|
# =========================================================================
|
|
# Step 1: JIT build dispatcher
|
|
# =========================================================================
|
|
print("\nStep 1: JIT Build Dispatcher")
|
|
|
|
config = KernelConfig(
|
|
dtype_a=args.dtype,
|
|
dtype_b=args.dtype,
|
|
dtype_c=args.dtype,
|
|
tile_m=128,
|
|
tile_n=128,
|
|
tile_k=32,
|
|
gfx_arch=args.arch,
|
|
)
|
|
|
|
reg = Registry(name="batch_gemm")
|
|
reg.register_kernel(config)
|
|
|
|
setups = reg.build(verbose=True)
|
|
if not setups or not setups[0].success:
|
|
error = setups[0].error if setups else "No kernels built"
|
|
print(f" ERROR: {error}")
|
|
return 1
|
|
|
|
dispatcher = setups[0].dispatcher
|
|
|
|
# =========================================================================
|
|
# Step 2: Run batch of different sizes
|
|
# =========================================================================
|
|
print("\nStep 2: Run Batch")
|
|
|
|
all_sizes = [
|
|
(256, 256, 256),
|
|
(512, 512, 512),
|
|
(1024, 1024, 1024),
|
|
(2048, 2048, 2048),
|
|
(4096, 4096, 4096),
|
|
]
|
|
sizes = [(m, n, k) for m, n, k in all_sizes if max(m, n, k) <= args.max_size]
|
|
|
|
np_dtype = np.float16 if args.dtype in ["fp16", "bf16"] else np.float32
|
|
|
|
print(f"\n {'Size':<20} | {'Time (ms)':>12} | {'TFLOPS':>10} | {'Status':>8}")
|
|
print(" " + "-" * 60)
|
|
|
|
total_ops = 0
|
|
total_time = 0
|
|
|
|
for M, N, K in sizes:
|
|
if not dispatcher.is_supported(M, N, K):
|
|
print(f" {M:>4}x{N:>4}x{K:<4} | {'N/A':>12} | {'N/A':>10} | Skipped")
|
|
continue
|
|
|
|
A = np.random.randn(M, K).astype(np_dtype) * 0.1
|
|
B = np.random.randn(K, N).astype(np_dtype) * 0.1
|
|
|
|
result = dispatcher.run(A, B, M, N, K)
|
|
|
|
if result.success:
|
|
total_ops += 2 * M * N * K
|
|
total_time += result.time_ms
|
|
print(
|
|
f" {M:>4}x{N:>4}x{K:<4} | {result.time_ms:>12.4f} | {result.tflops:>10.2f} | OK"
|
|
)
|
|
else:
|
|
print(f" {M:>4}x{N:>4}x{K:<4} | {'N/A':>12} | {'N/A':>10} | Error")
|
|
|
|
print(" " + "-" * 60)
|
|
|
|
if total_time > 0:
|
|
avg_tflops = (total_ops / 1e12) / (total_time / 1000)
|
|
print(f"\n Total: {total_time:.2f} ms, Average: {avg_tflops:.2f} TFLOPS")
|
|
|
|
print("\n" + "=" * 60)
|
|
print("Batch GEMM complete!")
|
|
print("=" * 60)
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|