mirror of
https://github.com/ROCm/composable_kernel.git
synced 2026-05-14 02:02:46 +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.
217 lines
8.4 KiB
C++
217 lines
8.4 KiB
C++
// Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Example 02: All Convolution Directions
|
|
//
|
|
// Forward, backward-data, and backward-weight for 2D convolution,
|
|
// each executed on GPU with non-zero verification.
|
|
//
|
|
// Build: cd dispatcher/build && cmake .. && make grouped_conv_02_all_dirs
|
|
|
|
#include <hip/hip_runtime.h>
|
|
#include <iostream>
|
|
#include <iomanip>
|
|
#include <vector>
|
|
#include <cmath>
|
|
|
|
#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;
|
|
|
|
DECL_GROUPED_CONV_KERNEL_SET(
|
|
conv_fwd_2d,
|
|
.add(GroupedConvSig().dtype("fp16").layout("nhwgc").conv_type("forward").dims(2),
|
|
GroupedConvAlgo().tile(1, 128, 128).pipeline("compv4").vector_sizes(4, 8, 8),
|
|
"gfx950"));
|
|
|
|
DECL_GROUPED_CONV_KERNEL_SET(
|
|
conv_bwdd_2d,
|
|
.add(GroupedConvSig().dtype("fp16").layout("nhwgc").conv_type("bwd_data").dims(2),
|
|
GroupedConvAlgo().tile(1, 128, 128).pipeline("compv3").vector_sizes(4, 8, 8),
|
|
"gfx950"));
|
|
|
|
DECL_GROUPED_CONV_KERNEL_SET(
|
|
conv_bwdw_2d,
|
|
.add(GroupedConvSig().dtype("fp16").layout("nhwgc").conv_type("bwd_weight").dims(2),
|
|
GroupedConvAlgo()
|
|
.tile(1, 128, 128)
|
|
.pipeline("compv3")
|
|
.memory_op("atomic_add")
|
|
.vector_sizes(4, 8, 8),
|
|
"gfx950"));
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
utils::ExampleArgs args("Example 02: All Convolution Directions",
|
|
"Forward/BwdData/BwdWeight with GPU execution and verification");
|
|
args.add_option("--arch", "gfx950", "GPU architecture");
|
|
|
|
if(!args.parse(argc, argv))
|
|
return 0;
|
|
|
|
utils::print_header("Example 02: All Convolution Directions");
|
|
|
|
std::string gfx_arch = args.get("--arch", "gfx950");
|
|
|
|
GroupedConvRegistry registry;
|
|
registry.set_name("all_directions");
|
|
REGISTER_GENERATED_KERNELS(registry, gfx_arch);
|
|
std::cout << " Registered " << registry.size() << " kernel(s)\n";
|
|
|
|
GroupedConvDispatcher dispatcher(®istry);
|
|
|
|
const int N = 1, G = 1, C = 64, K = 128, Hi = 14, Wi = 14, Y = 3, X = 3;
|
|
|
|
ck_tile::conv::ConvParam conv_param{
|
|
2,
|
|
static_cast<ck_tile::index_t>(G),
|
|
static_cast<ck_tile::index_t>(N),
|
|
static_cast<ck_tile::index_t>(K),
|
|
static_cast<ck_tile::index_t>(C),
|
|
{static_cast<ck_tile::index_t>(Y), static_cast<ck_tile::index_t>(X)},
|
|
{static_cast<ck_tile::index_t>(Hi), static_cast<ck_tile::index_t>(Wi)},
|
|
{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;
|
|
|
|
auto in_desc =
|
|
ck_tile::conv::make_input_host_tensor_descriptor_g_n_c_wis_packed<InLayout>(conv_param);
|
|
auto wei_desc =
|
|
ck_tile::conv::make_weight_host_tensor_descriptor_g_k_c_xs_packed<WeiLayout>(conv_param);
|
|
auto out_desc =
|
|
ck_tile::conv::make_output_host_tensor_descriptor_g_n_k_wos_packed<OutLayout>(conv_param);
|
|
|
|
ck_tile::HostTensor<InDataType> input(in_desc);
|
|
ck_tile::HostTensor<WeiDataType> weight(wei_desc);
|
|
ck_tile::HostTensor<OutDataType> output(out_desc);
|
|
|
|
ck_tile::FillUniformDistribution<InDataType>{-0.5f, 0.5f}(input);
|
|
ck_tile::FillUniformDistribution<WeiDataType>{-0.5f, 0.5f}(weight);
|
|
|
|
ck_tile::DeviceMem input_dev(input.get_element_space_size_in_bytes());
|
|
ck_tile::DeviceMem weight_dev(weight.get_element_space_size_in_bytes());
|
|
ck_tile::DeviceMem output_dev(output.get_element_space_size_in_bytes());
|
|
|
|
input_dev.ToDevice(input.data());
|
|
weight_dev.ToDevice(weight.data());
|
|
|
|
std::cout << "\n " << std::left << std::setw(12) << "Direction" << std::right << std::setw(10)
|
|
<< "Time(ms)" << std::setw(10) << "TFLOPS" << std::setw(14) << "NonZero"
|
|
<< std::setw(10) << "Status" << "\n";
|
|
std::cout << " " << std::string(56, '-') << "\n";
|
|
|
|
bool all_pass = true;
|
|
|
|
auto print_result =
|
|
[](const char* label, float time_ms, double tflops, size_t nz, size_t total, bool ok) {
|
|
std::cout << " " << std::left << std::setw(12) << label << std::right << std::fixed
|
|
<< std::setprecision(4) << std::setw(10) << time_ms << std::setprecision(2)
|
|
<< std::setw(10) << tflops << std::setw(14)
|
|
<< (std::to_string(nz) + "/" + std::to_string(total)) << std::setw(10)
|
|
<< (ok ? "OK" : "FAIL") << "\n";
|
|
};
|
|
|
|
// Forward: run(X, W, Y)
|
|
{
|
|
auto problem =
|
|
create_grouped_conv2d_problem(N, C, K, Hi, Wi, Y, X, 1, 1, GroupedConvOp::Forward);
|
|
float time_ms = dispatcher.run(input_dev.GetDeviceBuffer(),
|
|
weight_dev.GetDeviceBuffer(),
|
|
output_dev.GetDeviceBuffer(),
|
|
problem,
|
|
nullptr);
|
|
output_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;
|
|
bool ok = nz > 0;
|
|
print_result("forward",
|
|
time_ms,
|
|
calculate_conv_tflops(problem, time_ms),
|
|
nz,
|
|
output.get_element_space_size(),
|
|
ok);
|
|
if(!ok)
|
|
all_pass = false;
|
|
}
|
|
|
|
// Backward Data: run(dY, W, dX)
|
|
{
|
|
auto problem =
|
|
create_grouped_conv2d_problem(N, C, K, Hi, Wi, Y, X, 1, 1, GroupedConvOp::BackwardData);
|
|
ck_tile::HostTensor<InDataType> dx_host(in_desc);
|
|
ck_tile::DeviceMem dx_dev(dx_host.get_element_space_size_in_bytes());
|
|
float time_ms = dispatcher.run(output_dev.GetDeviceBuffer(), // dY (from forward pass)
|
|
weight_dev.GetDeviceBuffer(), // W
|
|
dx_dev.GetDeviceBuffer(), // dX (output)
|
|
problem,
|
|
nullptr);
|
|
dx_dev.FromDevice(dx_host.data());
|
|
size_t nz = 0;
|
|
for(size_t i = 0; i < dx_host.get_element_space_size(); ++i)
|
|
if(static_cast<float>(dx_host.data()[i]) != 0.0f)
|
|
++nz;
|
|
bool ok = nz > 0;
|
|
print_result("bwd_data",
|
|
time_ms,
|
|
calculate_conv_tflops(problem, time_ms),
|
|
nz,
|
|
dx_host.get_element_space_size(),
|
|
ok);
|
|
if(!ok)
|
|
all_pass = false;
|
|
}
|
|
|
|
// Backward Weight: run(X, dY, dW)
|
|
{
|
|
auto problem = create_grouped_conv2d_problem(
|
|
N, C, K, Hi, Wi, Y, X, 1, 1, GroupedConvOp::BackwardWeight);
|
|
ck_tile::HostTensor<WeiDataType> dw_host(wei_desc);
|
|
ck_tile::DeviceMem dw_dev(dw_host.get_element_space_size_in_bytes());
|
|
float time_ms = dispatcher.run(input_dev.GetDeviceBuffer(), // X
|
|
output_dev.GetDeviceBuffer(), // dY
|
|
dw_dev.GetDeviceBuffer(), // dW (output)
|
|
problem,
|
|
nullptr);
|
|
dw_dev.FromDevice(dw_host.data());
|
|
size_t nz = 0;
|
|
for(size_t i = 0; i < dw_host.get_element_space_size(); ++i)
|
|
if(static_cast<float>(dw_host.data()[i]) != 0.0f)
|
|
++nz;
|
|
bool ok = nz > 0;
|
|
print_result("bwd_weight",
|
|
time_ms,
|
|
calculate_conv_tflops(problem, time_ms),
|
|
nz,
|
|
dw_host.get_element_space_size(),
|
|
ok);
|
|
if(!ok)
|
|
all_pass = false;
|
|
}
|
|
|
|
utils::print_separator();
|
|
std::cout << " Status: " << (all_pass ? "PASS" : "FAIL") << "\n";
|
|
utils::print_separator();
|
|
|
|
return all_pass ? 0 : 1;
|
|
}
|