mirror of
https://github.com/ROCm/composable_kernel.git
synced 2026-05-14 18:17:44 +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>
155 lines
6.0 KiB
C++
155 lines
6.0 KiB
C++
// Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Example 04: Heuristic Selection + JSON Export
|
|
//
|
|
// Demonstrates runtime kernel selection with heuristic ranking,
|
|
// GPU execution, and JSON registry export.
|
|
//
|
|
// Build: cd dispatcher/build && cmake .. && make grouped_conv_04_registry_json
|
|
|
|
#include <hip/hip_runtime.h>
|
|
#include <iostream>
|
|
#include <iomanip>
|
|
#include <vector>
|
|
|
|
#include "ck_tile/core.hpp"
|
|
#include "ck_tile/host.hpp"
|
|
#include "ck_tile/host/convolution_parameter.hpp"
|
|
#include "ck_tile/ops/grouped_convolution.hpp"
|
|
|
|
#include "ck_tile/dispatcher/grouped_conv_utils.hpp"
|
|
#include "ck_tile/dispatcher/example_args.hpp"
|
|
|
|
using namespace ck_tile::dispatcher;
|
|
using namespace ck_tile::dispatcher::grouped_conv_utils;
|
|
using GroupedConvSig = grouped_conv_decl::GroupedConvSignature;
|
|
using GroupedConvAlgo = grouped_conv_decl::GroupedConvAlgorithm;
|
|
|
|
using InDataType = ck_tile::half_t;
|
|
using WeiDataType = ck_tile::half_t;
|
|
using OutDataType = ck_tile::half_t;
|
|
|
|
// Two tile configs for heuristic selection
|
|
DECL_GROUPED_CONV_KERNEL_SET(
|
|
heuristic_kernels,
|
|
.add(GroupedConvSig().dtype("fp16").layout("nhwgc").conv_type("forward").dims(2),
|
|
GroupedConvAlgo().tile(1, 128, 128).pipeline("compv4").vector_sizes(4, 8, 8),
|
|
"gfx950")
|
|
.add(GroupedConvSig().dtype("fp16").layout("nhwgc").conv_type("forward").dims(2),
|
|
GroupedConvAlgo().tile(1, 64, 64).pipeline("compv3").vector_sizes(4, 8, 8),
|
|
"gfx950"));
|
|
|
|
std::vector<std::string> conv_heuristic(const GroupedConvProblem& problem)
|
|
{
|
|
int64_t spatial = problem.Ho() * problem.Wo();
|
|
if(spatial > 400)
|
|
return {"128x128", "64x64"};
|
|
return {"64x64", "128x128"};
|
|
}
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
utils::ExampleArgs args("Example 04: Heuristic + JSON",
|
|
"Runtime kernel selection and JSON export");
|
|
args.add_option("--arch", "gfx950", "GPU architecture");
|
|
|
|
if(!args.parse(argc, argv))
|
|
return 0;
|
|
|
|
utils::print_header("Example 04: Heuristic Selection + JSON Export");
|
|
|
|
std::string gfx_arch = args.get("--arch", "gfx950");
|
|
|
|
// Step 1: Register
|
|
std::cout << "\nStep 1: Register Kernels" << std::endl;
|
|
GroupedConvRegistry registry;
|
|
registry.set_name("heuristic_conv");
|
|
REGISTER_GENERATED_KERNELS(registry, gfx_arch);
|
|
std::cout << " Registered " << registry.size() << " kernel(s)" << std::endl;
|
|
|
|
// Step 2: Heuristic dispatcher
|
|
std::cout << "\nStep 2: Heuristic Dispatcher" << std::endl;
|
|
GroupedConvDispatcher dispatcher(®istry);
|
|
dispatcher.set_strategy(GroupedConvDispatcher::SelectionStrategy::Heuristic);
|
|
dispatcher.set_heuristic(conv_heuristic);
|
|
|
|
// Step 3: Select kernels (no GPU yet)
|
|
std::cout << "\nStep 3: Kernel Selection" << std::endl;
|
|
|
|
auto problem = create_grouped_conv2d_problem(1, 64, 128, 14, 14, 3, 3, 1, 1);
|
|
|
|
auto* selected = dispatcher.select_kernel(problem);
|
|
std::cout << " Selected: " << (selected ? selected->name() : "none") << std::endl;
|
|
|
|
// Step 4: GPU execution
|
|
std::cout << "\nStep 4: GPU Execution" << std::endl;
|
|
|
|
ck_tile::conv::ConvParam cp{
|
|
2,
|
|
static_cast<ck_tile::index_t>(1),
|
|
static_cast<ck_tile::index_t>(1),
|
|
static_cast<ck_tile::index_t>(128),
|
|
static_cast<ck_tile::index_t>(64),
|
|
{static_cast<ck_tile::index_t>(3), static_cast<ck_tile::index_t>(3)},
|
|
{static_cast<ck_tile::index_t>(14), static_cast<ck_tile::index_t>(14)},
|
|
{1, 1},
|
|
{1, 1},
|
|
{1, 1},
|
|
{1, 1}};
|
|
|
|
using InLayout = ck_tile::tensor_layout::convolution::NHWGC;
|
|
using WeiLayout = ck_tile::tensor_layout::convolution::GKYXC;
|
|
using OutLayout = ck_tile::tensor_layout::convolution::NHWGK;
|
|
|
|
std::cout << " Creating tensors..." << std::endl;
|
|
auto in_d = ck_tile::conv::make_input_host_tensor_descriptor_g_n_c_wis_packed<InLayout>(cp);
|
|
auto wei_d = ck_tile::conv::make_weight_host_tensor_descriptor_g_k_c_xs_packed<WeiLayout>(cp);
|
|
auto out_d = ck_tile::conv::make_output_host_tensor_descriptor_g_n_k_wos_packed<OutLayout>(cp);
|
|
|
|
ck_tile::HostTensor<InDataType> input(in_d);
|
|
ck_tile::HostTensor<WeiDataType> weight(wei_d);
|
|
ck_tile::HostTensor<OutDataType> output(out_d);
|
|
ck_tile::FillUniformDistribution<InDataType>{-0.5f, 0.5f}(input);
|
|
ck_tile::FillUniformDistribution<WeiDataType>{-0.5f, 0.5f}(weight);
|
|
|
|
std::cout << " Allocating device memory..." << std::endl;
|
|
ck_tile::DeviceMem in_dev(input.get_element_space_size_in_bytes());
|
|
ck_tile::DeviceMem wei_dev(weight.get_element_space_size_in_bytes());
|
|
ck_tile::DeviceMem out_dev(output.get_element_space_size_in_bytes());
|
|
in_dev.ToDevice(input.data());
|
|
wei_dev.ToDevice(weight.data());
|
|
|
|
std::cout << " Launching kernel..." << std::endl;
|
|
float time_ms = dispatcher.run(in_dev.GetDeviceBuffer(),
|
|
wei_dev.GetDeviceBuffer(),
|
|
out_dev.GetDeviceBuffer(),
|
|
problem,
|
|
nullptr);
|
|
|
|
std::cout << " Reading back..." << std::endl;
|
|
out_dev.FromDevice(output.data());
|
|
size_t nz = 0;
|
|
for(size_t i = 0; i < output.get_element_space_size(); ++i)
|
|
if(static_cast<float>(output.data()[i]) != 0.0f)
|
|
++nz;
|
|
|
|
std::cout << " Time: " << std::fixed << std::setprecision(4) << time_ms << " ms"
|
|
<< std::endl;
|
|
std::cout << " TFLOPS: " << std::setprecision(2) << calculate_conv_tflops(problem, time_ms)
|
|
<< std::endl;
|
|
std::cout << " NonZero: " << nz << "/" << output.get_element_space_size() << std::endl;
|
|
|
|
// Step 5: JSON export
|
|
std::cout << "\nStep 5: JSON Export" << std::endl;
|
|
std::string json = registry.export_json(false);
|
|
std::cout << " JSON size: " << json.size() << " bytes" << std::endl;
|
|
|
|
bool passed = nz > 0;
|
|
utils::print_separator();
|
|
std::cout << " Status: " << (passed ? "PASS" : "FAIL") << "\n";
|
|
utils::print_separator();
|
|
|
|
return passed ? 0 : 1;
|
|
}
|