mirror of
https://github.com/ROCm/composable_kernel.git
synced 2026-05-16 02:54:21 +00:00
## 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. --------- Co-authored-by: Yaswanth Raparti <113389104+yraparti@users.noreply.github.com>
154 lines
4.2 KiB
C++
154 lines
4.2 KiB
C++
// Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#include "ck_tile/dispatcher/dispatcher.hpp"
|
|
#include "ck_tile/dispatcher/dispatcher_error.hpp"
|
|
#include <sstream>
|
|
#include <iostream>
|
|
|
|
namespace ck_tile {
|
|
namespace dispatcher {
|
|
|
|
Dispatcher::Dispatcher(Registry* registry, const std::string& gfx_arch)
|
|
: registry_(registry ? registry : &Registry::instance()),
|
|
heuristic_(nullptr),
|
|
strategy_(SelectionStrategy::FirstFit),
|
|
gfx_arch_(gfx_arch)
|
|
{
|
|
}
|
|
|
|
void Dispatcher::set_heuristic(HeuristicFunction heuristic)
|
|
{
|
|
heuristic_ = heuristic;
|
|
if(heuristic_)
|
|
{
|
|
strategy_ = SelectionStrategy::Heuristic;
|
|
}
|
|
}
|
|
|
|
void Dispatcher::set_strategy(SelectionStrategy strategy) { strategy_ = strategy; }
|
|
|
|
KernelInstancePtr Dispatcher::select_kernel(const Problem& problem) const
|
|
{
|
|
if(!problem.is_valid())
|
|
{
|
|
return nullptr;
|
|
}
|
|
|
|
switch(strategy_)
|
|
{
|
|
case SelectionStrategy::FirstFit: return select_first_fit(problem);
|
|
case SelectionStrategy::Heuristic: return select_heuristic(problem);
|
|
default: return nullptr;
|
|
}
|
|
}
|
|
|
|
float Dispatcher::run(
|
|
const void* a_ptr, const void* b_ptr, void* c_ptr, const Problem& problem, void* stream) const
|
|
{
|
|
return run_fused(a_ptr, b_ptr, c_ptr, nullptr, problem, stream);
|
|
}
|
|
|
|
float Dispatcher::run_fused(const void* a_ptr,
|
|
const void* b_ptr,
|
|
void* c_ptr,
|
|
const void** d_ptrs,
|
|
const Problem& problem,
|
|
void* stream) const
|
|
{
|
|
auto kernel = select_kernel(problem);
|
|
if(!kernel)
|
|
{
|
|
std::ostringstream oss;
|
|
oss << "No suitable kernel found for problem: M=" << problem.M << " N=" << problem.N
|
|
<< " K=" << problem.K;
|
|
throw NoKernelFound(oss.str());
|
|
}
|
|
|
|
return kernel->run(a_ptr, b_ptr, c_ptr, d_ptrs, problem, stream);
|
|
}
|
|
|
|
float Dispatcher::run_explicit(const std::string& kernel_id,
|
|
const void* a_ptr,
|
|
const void* b_ptr,
|
|
void* c_ptr,
|
|
const void** d_ptrs,
|
|
const Problem& problem,
|
|
void* stream) const
|
|
{
|
|
auto kernel = registry_->lookup(kernel_id);
|
|
if(!kernel)
|
|
{
|
|
throw NoKernelFound("Kernel not found: " + kernel_id);
|
|
}
|
|
|
|
if(!kernel->supports(problem))
|
|
{
|
|
std::ostringstream oss;
|
|
oss << "Kernel " << kernel_id << " does not support problem: M=" << problem.M
|
|
<< " N=" << problem.N << " K=" << problem.K;
|
|
throw UnsupportedProblem(oss.str());
|
|
}
|
|
|
|
return kernel->run(a_ptr, b_ptr, c_ptr, d_ptrs, problem, stream);
|
|
}
|
|
|
|
bool Dispatcher::validate(const void* a_ptr,
|
|
const void* b_ptr,
|
|
const void* c_ptr,
|
|
const void** d_ptrs,
|
|
const Problem& problem,
|
|
float tolerance) const
|
|
{
|
|
auto kernel = select_kernel(problem);
|
|
if(!kernel)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return kernel->validate(a_ptr, b_ptr, c_ptr, d_ptrs, problem, tolerance);
|
|
}
|
|
|
|
KernelInstancePtr Dispatcher::select_first_fit(const Problem& problem) const
|
|
{
|
|
auto all_kernels = registry_->get_all();
|
|
|
|
for(const auto& kernel : all_kernels)
|
|
{
|
|
if(kernel->supports(problem))
|
|
{
|
|
return kernel;
|
|
}
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
KernelInstancePtr Dispatcher::select_heuristic(const Problem& problem) const
|
|
{
|
|
if(!heuristic_)
|
|
{
|
|
// Fall back to first-fit if no heuristic available
|
|
return select_first_fit(problem);
|
|
}
|
|
|
|
// Get ranked list of kernel identifiers from heuristic
|
|
auto candidates = heuristic_(problem);
|
|
|
|
// Try each candidate in order
|
|
for(const auto& kernel_id : candidates)
|
|
{
|
|
auto kernel = registry_->lookup(kernel_id);
|
|
if(kernel && kernel->supports(problem))
|
|
{
|
|
return kernel;
|
|
}
|
|
}
|
|
|
|
// If no heuristic candidate works, fall back to first-fit
|
|
return select_first_fit(problem);
|
|
}
|
|
|
|
} // namespace dispatcher
|
|
} // namespace ck_tile
|