Files
composable_kernel/dispatcher/tests/test_grouped_conv_kernel_decl.cpp
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

142 lines
4.4 KiB
C++

// Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
/// Unit tests for GroupedConvKernelDecl using assert() and std::cout
#include "ck_tile/dispatcher/grouped_conv_kernel_decl.hpp"
#include <cassert>
#include <iostream>
using namespace ck_tile::dispatcher;
using namespace ck_tile::dispatcher::grouped_conv_decl;
void test_grouped_conv_signature_builder()
{
std::cout << " test_grouped_conv_signature_builder... ";
GroupedConvSignature sig;
sig.dtype("fp16").layout("nhwc").conv_type("forward").dims(2).groups(4);
assert(sig.dtype_in_ == "fp16");
assert(sig.dtype_wei_ == "fp16");
assert(sig.dtype_out_ == "fp16");
assert(sig.layout_ == "nhwc");
assert(sig.conv_op_ == "forward");
assert(sig.num_dims_ == 2);
assert(sig.groups_ == 4);
assert(sig.op_str() == "fwd");
sig.conv_type("bwd_data");
assert(sig.op_str() == "bwd_data");
sig.conv_type("bwd_weight");
assert(sig.op_str() == "bwd_weight");
std::cout << "PASSED\n";
}
void test_grouped_conv_algorithm_builder()
{
std::cout << " test_grouped_conv_algorithm_builder... ";
GroupedConvAlgorithm algo;
algo.tile(128, 128, 64)
.wave(2, 2, 1)
.warp(32, 32, 16)
.pipeline("compv4")
.scheduler("intrawave");
assert(algo.tile_m_ == 128);
assert(algo.tile_n_ == 128);
assert(algo.tile_k_ == 64);
assert(algo.wave_m_ == 2);
assert(algo.wave_n_ == 2);
assert(algo.warp_m_ == 32);
assert(algo.warp_n_ == 32);
assert(algo.pipeline_ == "compv4");
assert(algo.scheduler_ == "intrawave");
assert(!algo.needs_expansion());
algo.wave_m_ = ANY_INT;
assert(algo.needs_wave_expansion());
std::cout << "PASSED\n";
}
void test_grouped_conv_kernel_decl()
{
std::cout << " test_grouped_conv_kernel_decl... ";
GroupedConvSignature sig;
sig.dtype("fp16").layout("nhwc").conv_type("forward").dims(2);
GroupedConvAlgorithm algo;
algo.tile(128, 128, 64).wave(2, 2, 1).warp(32, 32, 16);
GroupedConvKernelDecl decl(sig, algo, "gfx942");
std::string name = decl.name();
assert(!name.empty());
assert(name.find("grouped_conv_") != std::string::npos);
assert(name.find("fwd") != std::string::npos);
assert(name.find("fp16") != std::string::npos);
assert(name.find("128x128x64") != std::string::npos);
assert(!decl.has_wildcards());
std::cout << "PASSED\n";
}
void test_grouped_conv_kernel_set()
{
std::cout << " test_grouped_conv_kernel_set... ";
GroupedConvKernelSet set;
set.add("fp16", "nhwc", "forward", 128, 128);
assert(set.size() == 1);
set.add("fp16", "nhwc", "forward", 256, 256);
assert(set.size() == 2);
const auto& decls = set.declarations();
assert(decls[0].algorithm.tile_n_ == 128);
assert(decls[0].algorithm.tile_k_ == 128);
assert(decls[1].algorithm.tile_n_ == 256);
assert(decls[1].algorithm.tile_k_ == 256);
set.tag("test_set");
assert(set.tag() == "test_set");
std::cout << "PASSED\n";
}
void test_grouped_conv_kernel_set_merge()
{
std::cout << " test_grouped_conv_kernel_set_merge... ";
GroupedConvKernelSet set1;
set1.add("fp16", "nhwc", "forward", 128, 128);
GroupedConvKernelSet set2;
set2.add("fp16", "nhwc", "forward", 256, 256);
set1.merge(set2);
assert(set1.size() == 2);
assert(set1.declarations()[0].algorithm.tile_n_ == 128);
assert(set1.declarations()[1].algorithm.tile_n_ == 256);
std::cout << "PASSED\n";
}
void test_grouped_conv_kernel_set_registry()
{
std::cout << " test_grouped_conv_kernel_set_registry... ";
auto& reg = GroupedConvKernelSetRegistry::instance();
reg.clear();
GroupedConvKernelSet set;
set.add("fp16", "nhwc", "forward", 128, 128);
reg.register_set("gconv_test", set);
assert(reg.has("gconv_test"));
assert(reg.size() >= 1);
const auto& retrieved = reg.get("gconv_test");
assert(retrieved.size() == 1);
const auto& empty = reg.get("nonexistent");
assert(empty.size() == 0);
reg.clear();
assert(!reg.has("gconv_test"));
std::cout << "PASSED\n";
}
int main()
{
std::cout << "\n=== Test Grouped Conv Kernel Decl ===\n\n";
test_grouped_conv_signature_builder();
test_grouped_conv_algorithm_builder();
test_grouped_conv_kernel_decl();
test_grouped_conv_kernel_set();
test_grouped_conv_kernel_set_merge();
test_grouped_conv_kernel_set_registry();
std::cout << "\n=== All Tests Passed! ===\n\n";
return 0;
}