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.
266 lines
8.1 KiB
Python
266 lines
8.1 KiB
Python
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
"""
|
|
Example 10: Advanced Benchmarking with Full Control
|
|
|
|
This example demonstrates all available benchmark parameters:
|
|
- warmup: Number of warmup iterations (default: 5)
|
|
- repeat: Number of benchmark iterations (default: 20)
|
|
- flush_cache: Flush GPU cache between iterations (default: False)
|
|
- timer: Timer type - "gpu" (default) or "cpu"
|
|
- init: Initialization method - "random", "linear", "constant"
|
|
|
|
Usage:
|
|
python3 10_advanced_benchmark.py
|
|
python3 10_advanced_benchmark.py --warmup 10 --repeat 100
|
|
python3 10_advanced_benchmark.py --init linear
|
|
"""
|
|
|
|
import argparse
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Add paths for imports
|
|
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent / "python"))
|
|
|
|
import numpy as np
|
|
|
|
from ctypes_utils import (
|
|
KernelConfig,
|
|
setup_gemm_dispatcher,
|
|
cleanup_gemm,
|
|
reset_for_example,
|
|
detect_gpu_arch,
|
|
)
|
|
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser(
|
|
description="Advanced GEMM benchmarking with full parameter control"
|
|
)
|
|
|
|
# Problem size
|
|
parser.add_argument("-m", type=int, default=2048, help="M dimension")
|
|
parser.add_argument("-n", type=int, default=2048, help="N dimension")
|
|
parser.add_argument("-k", type=int, default=2048, help="K dimension")
|
|
|
|
# Benchmark parameters
|
|
parser.add_argument(
|
|
"--warmup", type=int, default=5, help="Number of warmup iterations"
|
|
)
|
|
parser.add_argument(
|
|
"--repeat", type=int, default=20, help="Number of benchmark iterations"
|
|
)
|
|
parser.add_argument(
|
|
"--flush-cache", action="store_true", help="Flush GPU cache between iterations"
|
|
)
|
|
parser.add_argument(
|
|
"--timer", choices=["gpu", "cpu"], default="gpu", help="Timer type (gpu or cpu)"
|
|
)
|
|
parser.add_argument(
|
|
"--init",
|
|
choices=["random", "linear", "constant"],
|
|
default="random",
|
|
help="Initialization method",
|
|
)
|
|
|
|
# Kernel configuration
|
|
parser.add_argument("--dtype", default="fp16", help="Data type")
|
|
parser.add_argument("--pipeline", default="compv4", help="Pipeline type")
|
|
parser.add_argument(
|
|
"--arch",
|
|
default=detect_gpu_arch(),
|
|
help="GPU architecture (auto-detected from rocminfo)",
|
|
)
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
def initialize_matrix(shape, method, dtype):
|
|
"""Initialize matrix with specified method"""
|
|
if method == "random":
|
|
return np.random.randn(*shape).astype(dtype) * 0.5
|
|
elif method == "linear":
|
|
total = np.prod(shape)
|
|
return np.arange(total).reshape(shape).astype(dtype) / total
|
|
elif method == "constant":
|
|
return np.ones(shape, dtype=dtype)
|
|
else:
|
|
return np.random.randn(*shape).astype(dtype)
|
|
|
|
|
|
def main():
|
|
args = parse_args()
|
|
|
|
reset_for_example()
|
|
|
|
print("=" * 70)
|
|
print("Example 10: Advanced GEMM Benchmarking")
|
|
print("=" * 70)
|
|
|
|
# Show benchmark configuration
|
|
print("\nBenchmark Configuration:")
|
|
print(f" Problem Size: {args.m} x {args.n} x {args.k}")
|
|
print(f" Warmup: {args.warmup} iterations")
|
|
print(f" Repeat: {args.repeat} iterations")
|
|
print(f" Flush Cache: {args.flush_cache}")
|
|
print(f" Timer: {args.timer}")
|
|
print(f" Init Method: {args.init}")
|
|
print(f" Data Type: {args.dtype}")
|
|
print(f" Pipeline: {args.pipeline}")
|
|
print(f" Architecture: {args.arch}")
|
|
print()
|
|
|
|
# Map dtype
|
|
np_dtype = np.float16 if args.dtype in ["fp16", "bf16"] else np.float32
|
|
|
|
# Initialize matrices
|
|
print("Step 1: Initialize matrices...")
|
|
A = initialize_matrix((args.m, args.k), args.init, np_dtype)
|
|
B = initialize_matrix((args.k, args.n), args.init, np_dtype)
|
|
print(f" A: {A.shape} ({args.init})")
|
|
print(f" B: {B.shape} ({args.init})")
|
|
|
|
# Create kernel config (does not include M/N/K - those are problem size)
|
|
print("\nStep 2: Create kernel configuration...")
|
|
kernel_config = KernelConfig(
|
|
dtype_a=args.dtype,
|
|
dtype_b=args.dtype,
|
|
dtype_c=args.dtype,
|
|
dtype_acc="fp32",
|
|
layout_a="row",
|
|
layout_b="col", # B is column-major for optimal performance
|
|
layout_c="row",
|
|
tile_m=128,
|
|
tile_n=128,
|
|
tile_k=32,
|
|
wave_m=2,
|
|
wave_n=2,
|
|
wave_k=1,
|
|
warp_m=32,
|
|
warp_n=32,
|
|
warp_k=16,
|
|
pipeline=args.pipeline,
|
|
scheduler="intrawave",
|
|
epilogue="cshuffle",
|
|
gfx_arch=args.arch,
|
|
)
|
|
print(f" Config: {args.dtype}, tile=128x128x32, {args.pipeline}")
|
|
|
|
# Setup dispatcher
|
|
print("\nStep 3: Setup dispatcher...")
|
|
setup = setup_gemm_dispatcher(
|
|
config=kernel_config,
|
|
registry_name="benchmark_gemm",
|
|
verbose=False,
|
|
auto_rebuild=True,
|
|
)
|
|
|
|
if not setup.success:
|
|
print(f" ERROR: {setup.error}")
|
|
return 1
|
|
|
|
dispatcher = setup.dispatcher
|
|
print(f" Library: {setup.lib.path if setup.lib else 'N/A'}")
|
|
print(f" Kernel: {setup.lib.get_kernel_name() if setup.lib else 'N/A'}")
|
|
|
|
# Run benchmark with multiple iterations
|
|
print("\nStep 4: Run benchmark...")
|
|
print(f" Running {args.warmup} warmup + {args.repeat} benchmark iterations...")
|
|
|
|
# Warmup
|
|
for _ in range(args.warmup):
|
|
_ = dispatcher.run(A, B, args.m, args.n, args.k)
|
|
|
|
# Benchmark
|
|
times = []
|
|
for _ in range(args.repeat):
|
|
result = dispatcher.run(A, B, args.m, args.n, args.k)
|
|
if result.success:
|
|
times.append(result.time_ms)
|
|
|
|
if times:
|
|
avg_time = sum(times) / len(times)
|
|
min_time = min(times)
|
|
max_time = max(times)
|
|
|
|
# Calculate TFLOPS
|
|
flops = 2 * args.m * args.n * args.k
|
|
avg_tflops = (flops / 1e12) / (avg_time / 1000) if avg_time > 0 else 0
|
|
max_tflops = (flops / 1e12) / (min_time / 1000) if min_time > 0 else 0
|
|
|
|
# Calculate bandwidth (C has same dtype as A and B)
|
|
C_bytes = args.m * args.n * np.dtype(np_dtype).itemsize
|
|
bandwidth_gb = (
|
|
(A.nbytes + B.nbytes + C_bytes) / 1e9 / (avg_time / 1000)
|
|
if avg_time > 0
|
|
else 0
|
|
)
|
|
|
|
print(f"\n *** BENCHMARK RESULTS ({args.repeat} iterations) ***")
|
|
print(f" Average Time: {avg_time:.4f} ms")
|
|
print(f" Min Time: {min_time:.4f} ms")
|
|
print(f" Max Time: {max_time:.4f} ms")
|
|
print(f" Avg TFLOPS: {avg_tflops:.2f}")
|
|
print(f" Peak TFLOPS: {max_tflops:.2f}")
|
|
print(f" Bandwidth: {bandwidth_gb:.2f} GB/s")
|
|
else:
|
|
print(" FAILED: No successful runs")
|
|
return 1
|
|
|
|
# Summary
|
|
print("\n" + "=" * 70)
|
|
print("BENCHMARK PARAMETERS REFERENCE")
|
|
print("=" * 70)
|
|
print("""
|
|
Available parameters for GEMM benchmarking:
|
|
|
|
--warmup N Number of warmup iterations (discard results)
|
|
Higher = more stable results, longer run time
|
|
Default: 5
|
|
|
|
--repeat N Number of benchmark iterations
|
|
Higher = more accurate average, longer run time
|
|
Default: 20
|
|
|
|
--flush-cache Flush GPU L2 cache between iterations
|
|
Use for memory-bound benchmarks
|
|
Default: off
|
|
|
|
--timer {gpu,cpu} Timer type
|
|
gpu = HIP events (more accurate for GPU)
|
|
cpu = std::chrono (includes kernel launch overhead)
|
|
Default: gpu
|
|
|
|
--init METHOD Matrix initialization
|
|
random = uniform random [-0.5, 0.5]
|
|
linear = sequential values
|
|
constant = all ones
|
|
Default: random
|
|
|
|
Note: For C++ examples, these parameters are passed to stream_config:
|
|
|
|
ck_tile::stream_config cfg{
|
|
nullptr, // stream_id
|
|
true, // time_kernel
|
|
1, // log_level
|
|
5, // cold_niters (warmup)
|
|
20, // nrepeat
|
|
true, // is_gpu_timer
|
|
false, // flush_cache
|
|
1 // rotating_count
|
|
};
|
|
""")
|
|
|
|
# Cleanup
|
|
cleanup_gemm()
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|