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

231 lines
6.5 KiB
C++

// Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
/// Unit tests for GroupedConvRegistry and GroupedConvDispatcher using assert() and std::cout
#include "ck_tile/dispatcher/grouped_conv_utils.hpp"
#include <cassert>
#include <iostream>
#include <thread>
#include <atomic>
using namespace ck_tile::dispatcher;
using namespace ck_tile::dispatcher::grouped_conv_decl;
void test_grouped_conv_registry_basic()
{
std::cout << " test_grouped_conv_registry_basic... ";
GroupedConvRegistry& reg = GroupedConvRegistry::instance();
reg.clear();
reg.set_name("test_registry");
assert(reg.name() == "test_registry");
assert(reg.size() == 0);
assert(reg.empty());
reg.clear();
std::cout << "PASSED\n";
}
void test_grouped_conv_registry_register_set()
{
std::cout << " test_grouped_conv_registry_register_set... ";
GroupedConvRegistry& reg = GroupedConvRegistry::instance();
reg.clear();
GroupedConvKernelSet set;
set.add("fp16", "nhwc", "forward", 128, 128);
set.add("fp16", "nhwc", "forward", 256, 256);
bool ok = reg.register_set(set);
assert(ok);
assert(reg.size() == 2);
assert(!reg.empty());
reg.clear();
std::cout << "PASSED\n";
}
void test_grouped_conv_registry_all_kernels()
{
std::cout << " test_grouped_conv_registry_all_kernels... ";
GroupedConvRegistry& reg = GroupedConvRegistry::instance();
reg.clear();
GroupedConvKernelSet set;
set.add("fp16", "nhwc", "forward", 128, 128);
reg.register_set(set);
auto all = reg.all_kernels();
assert(all.size() == 1);
assert(all[0]->name().find("grouped_conv_") != std::string::npos);
reg.clear();
std::cout << "PASSED\n";
}
void test_grouped_conv_registry_clear()
{
std::cout << " test_grouped_conv_registry_clear... ";
GroupedConvRegistry& reg = GroupedConvRegistry::instance();
reg.clear();
GroupedConvKernelSet set;
set.add("fp16", "nhwc", "forward", 128, 128);
reg.register_set(set);
assert(reg.size() == 1);
reg.clear();
assert(reg.size() == 0);
assert(reg.empty());
reg.clear();
std::cout << "PASSED\n";
}
void test_grouped_conv_registry_thread_safe()
{
std::cout << " test_grouped_conv_registry_thread_safe... ";
GroupedConvRegistry& reg = GroupedConvRegistry::instance();
reg.clear();
const int num_threads = 4;
const int sets_per_thread = 10;
std::vector<std::thread> threads;
std::atomic<int> success_count{0};
for(int t = 0; t < num_threads; t++)
{
threads.emplace_back([t, &reg, &success_count]() {
for(int k = 0; k < sets_per_thread; k++)
{
GroupedConvKernelSet set;
set.add("fp16", "nhwc", "forward", 128 + t * 32 + k, 128);
if(reg.register_set(set))
{
success_count++;
}
}
});
}
for(auto& th : threads)
th.join();
assert(reg.size() == num_threads * sets_per_thread);
assert(success_count.load() == num_threads * sets_per_thread);
reg.clear();
std::cout << "PASSED\n";
}
void test_grouped_conv_registry_export_json()
{
std::cout << " test_grouped_conv_registry_export_json... ";
GroupedConvRegistry& reg = GroupedConvRegistry::instance();
reg.clear();
GroupedConvKernelSet set;
set.add("fp16", "nhwc", "forward", 128, 128);
reg.register_set(set);
std::string json = reg.export_json(false);
assert(!json.empty());
assert(json.find("\"kernels\"") != std::string::npos);
assert(json.find("\"metadata\"") != std::string::npos);
assert(json.find("grouped_conv_") != std::string::npos);
std::string json_stats = reg.export_json(true);
assert(json_stats.find("\"statistics\"") != std::string::npos);
reg.clear();
std::cout << "PASSED\n";
}
void test_grouped_conv_registry_filter()
{
std::cout << " test_grouped_conv_registry_filter... ";
GroupedConvRegistry& reg = GroupedConvRegistry::instance();
reg.clear();
GroupedConvKernelSet set;
set.add("fp16", "nhwc", "forward", 128, 128);
set.add("fp16", "nhwc", "forward", 256, 256);
set.add("bf16", "nhwc", "forward", 128, 128);
reg.register_set(set);
auto fp16_only =
reg.filter([](const GroupedConvKernelInstance& k) { return k.key().dtype_in == "fp16"; });
assert(fp16_only.size() == 2);
auto large_tile = reg.filter([](const GroupedConvKernelInstance& k) {
return k.key().tile_m >= 256 || k.key().tile_n >= 256;
});
assert(large_tile.size() >= 1);
reg.clear();
std::cout << "PASSED\n";
}
void test_grouped_conv_dispatcher_basic()
{
std::cout << " test_grouped_conv_dispatcher_basic... ";
GroupedConvRegistry& reg = GroupedConvRegistry::instance();
reg.clear();
GroupedConvKernelSet set;
set.add("fp16", "nhwc", "forward", 128, 128);
reg.register_set(set);
GroupedConvDispatcher dispatcher(&reg);
GroupedConvProblem problem = grouped_conv_utils::create_grouped_conv2d_problem(
4, 64, 128, 28, 28, 3, 3, 1, 1, GroupedConvOp::Forward);
float time = dispatcher.run(problem, nullptr);
assert(time >= 0.0f);
reg.clear();
std::cout << "PASSED\n";
}
void test_grouped_conv_dispatcher_select()
{
std::cout << " test_grouped_conv_dispatcher_select... ";
GroupedConvRegistry& reg = GroupedConvRegistry::instance();
reg.clear();
GroupedConvKernelSet set;
set.add("fp16", "nhwc", "forward", 128, 128);
set.add("fp16", "nhwc", "forward", 256, 256);
reg.register_set(set);
GroupedConvDispatcher dispatcher(&reg);
GroupedConvProblem problem = grouped_conv_utils::create_grouped_conv2d_problem(
4, 64, 128, 28, 28, 3, 3, 1, 1, GroupedConvOp::Forward);
const auto* selected = dispatcher.select(problem);
assert(selected != nullptr);
assert(selected->name().find("grouped_conv_") != std::string::npos);
assert(selected->matches(problem));
reg.clear();
std::cout << "PASSED\n";
}
int main()
{
std::cout << "\n=== Test Grouped Conv Registry ===\n\n";
test_grouped_conv_registry_basic();
test_grouped_conv_registry_register_set();
test_grouped_conv_registry_all_kernels();
test_grouped_conv_registry_clear();
test_grouped_conv_registry_thread_safe();
test_grouped_conv_registry_export_json();
test_grouped_conv_registry_filter();
test_grouped_conv_dispatcher_basic();
test_grouped_conv_dispatcher_select();
std::cout << "\n=== All Tests Passed! ===\n\n";
return 0;
}