Files
composable_kernel/dispatcher/tests/test_dispatcher_common.py
Vidyasagar Ananthan a2b844d335 [CK] [CK_Tile] Add GroupConv to Kernel Dispatcher (#5168)
## 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>
2026-04-09 10:38:33 -07:00

244 lines
7.2 KiB
Python

#!/usr/bin/env python3
# Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
# SPDX-License-Identifier: MIT
"""
Tests for python/dispatcher_common.py -- shared Python dispatcher utilities.
Phase 1b TDD: tests written BEFORE implementation exists.
Run: python3 -m pytest tests/test_dispatcher_common.py -v
"""
import io
import sys
import unittest
from pathlib import Path
from unittest.mock import patch
SCRIPT_DIR = Path(__file__).parent.resolve()
DISPATCHER_DIR = SCRIPT_DIR.parent
sys.path.insert(0, str(DISPATCHER_DIR / "python"))
sys.path.insert(0, str(DISPATCHER_DIR / "codegen"))
from dispatcher_common import ( # noqa: E402
get_dispatcher_root,
get_ck_root,
get_build_dir,
get_generated_kernels_dir,
get_arch_filter_data,
ValidationResultBase,
validate_wave_config,
validate_warp_tile_config,
validate_trait_combo,
auto_correct_wave,
auto_correct_trait,
Colors,
print_phase,
print_success,
print_error,
print_info,
cleanup_generated_kernels,
)
class TestPathHelpers(unittest.TestCase):
"""Tests for path helper functions."""
def test_dispatcher_root_contains_codegen(self):
root = get_dispatcher_root()
self.assertTrue((root / "codegen").exists())
def test_ck_root_contains_include_or_is_parent(self):
root = get_ck_root()
self.assertTrue(root.exists())
self.assertEqual(root, get_dispatcher_root().parent)
def test_build_dir_is_under_dispatcher(self):
build = get_build_dir()
self.assertEqual(build.parent, get_dispatcher_root())
def test_generated_kernels_dir_under_build(self):
gen_dir = get_generated_kernels_dir()
self.assertEqual(gen_dir.parent, get_build_dir())
class TestGetArchFilterData(unittest.TestCase):
"""Tests for get_arch_filter_data."""
def test_returns_dict(self):
data = get_arch_filter_data()
self.assertIsInstance(data, dict)
def test_has_warp_combos(self):
data = get_arch_filter_data()
self.assertIn("warp_combos", data)
def test_has_warp_tile_combos(self):
data = get_arch_filter_data()
self.assertIn("warp_tile_combos", data)
def test_has_trait_unsupported(self):
data = get_arch_filter_data()
self.assertIn("trait_unsupported", data)
def test_has_supported_archs(self):
data = get_arch_filter_data()
self.assertIn("supported_archs", data)
self.assertIn("gfx942", data["supported_archs"])
def test_gfx942_wave_configs(self):
data = get_arch_filter_data()
gfx942 = data["warp_combos"].get("gfx942", [])
self.assertIn([2, 2, 1], gfx942)
class TestValidationResultBase(unittest.TestCase):
"""Tests for ValidationResultBase dataclass."""
def test_valid_result(self):
vr = ValidationResultBase(is_valid=True)
self.assertTrue(vr.is_valid)
self.assertEqual(vr.errors, [])
self.assertEqual(vr.warnings, [])
self.assertEqual(vr.suggested_fixes, {})
def test_invalid_result(self):
vr = ValidationResultBase(
is_valid=False,
errors=["bad wave"],
suggested_fixes={"wave_m": 2},
)
self.assertFalse(vr.is_valid)
self.assertEqual(len(vr.errors), 1)
self.assertIn("wave_m", vr.suggested_fixes)
class TestValidateWaveConfig(unittest.TestCase):
"""Tests for validate_wave_config."""
def test_valid_wave(self):
is_valid, msg = validate_wave_config([2, 2, 1], "gfx942")
self.assertTrue(is_valid)
self.assertEqual(msg, "")
def test_invalid_wave(self):
is_valid, msg = validate_wave_config([3, 3, 1], "gfx942")
self.assertFalse(is_valid)
self.assertIn("wave", msg.lower())
class TestValidateWarpTileConfig(unittest.TestCase):
"""Tests for validate_warp_tile_config."""
def test_valid_warp_tile(self):
is_valid, msg = validate_warp_tile_config([32, 32, 16], "gfx942", "fp16")
self.assertTrue(is_valid)
def test_invalid_warp_tile(self):
is_valid, msg = validate_warp_tile_config([99, 99, 99], "gfx942", "fp16")
self.assertFalse(is_valid)
self.assertIn("warp", msg.lower())
class TestValidateTraitCombo(unittest.TestCase):
"""Tests for validate_trait_combo."""
def test_valid_trait(self):
is_valid, msg = validate_trait_combo("compv3", "cshuffle", "intrawave")
self.assertTrue(is_valid)
def test_invalid_trait_interwave_compute(self):
is_valid, msg = validate_trait_combo("compv4", "cshuffle", "interwave")
self.assertFalse(is_valid)
def test_valid_mem_interwave(self):
is_valid, msg = validate_trait_combo("mem", "cshuffle", "interwave")
self.assertTrue(is_valid)
class TestAutoCorrectWave(unittest.TestCase):
"""Tests for auto_correct_wave."""
def test_corrects_invalid_wave(self):
corrected = auto_correct_wave([1, 1, 1], "gfx942")
self.assertIsInstance(corrected, list)
self.assertEqual(len(corrected), 3)
data = get_arch_filter_data()
valid_waves = data["warp_combos"].get("gfx942", [[2, 2, 1]])
self.assertIn(corrected, valid_waves)
class TestAutoCorrectTrait(unittest.TestCase):
"""Tests for auto_correct_trait."""
def test_corrects_invalid_scheduler(self):
corrected_pipeline, corrected_scheduler = auto_correct_trait(
"compv4", "interwave"
)
self.assertEqual(corrected_scheduler, "intrawave")
class TestColors(unittest.TestCase):
"""Tests for Colors class (cross-platform ANSI support from conv)."""
def test_green_returns_string(self):
result = Colors.green("ok")
self.assertIn("ok", result)
def test_red_returns_string(self):
result = Colors.red("error")
self.assertIn("error", result)
def test_yellow_returns_string(self):
result = Colors.yellow("warn")
self.assertIn("warn", result)
def test_bold_returns_string(self):
result = Colors.bold("title")
self.assertIn("title", result)
def test_plain_mode_no_ansi(self):
with patch.object(Colors, "_use_color", return_value=False):
result = Colors.green("plain")
self.assertEqual(result, "plain")
class TestPhasedOutput(unittest.TestCase):
"""Tests for phased output helpers."""
def test_print_phase(self):
buf = io.StringIO()
with patch("sys.stdout", buf):
print_phase(1, "Setup")
self.assertIn("Setup", buf.getvalue())
def test_print_success(self):
buf = io.StringIO()
with patch("sys.stdout", buf):
print_success("Done")
self.assertIn("Done", buf.getvalue())
def test_print_error(self):
buf = io.StringIO()
with patch("sys.stdout", buf):
print_error("Oops")
self.assertIn("Oops", buf.getvalue())
def test_print_info(self):
buf = io.StringIO()
with patch("sys.stdout", buf):
print_info("FYI")
self.assertIn("FYI", buf.getvalue())
class TestCleanup(unittest.TestCase):
"""Tests for cleanup_generated_kernels."""
def test_cleanup_nonexistent_dir_no_error(self):
cleanup_generated_kernels(Path("/tmp/nonexistent_ck_test_dir_12345"))
if __name__ == "__main__":
unittest.main()