Skip to content

CK: Refactor 144 contraction instance files with shared CK_CONTRACTION_INSTANCE macro#6325

Merged
AviralGoelAMD merged 3 commits into
users/avirgoel/ck/contraction-macro-experimentfrom
users/avirgoel/ck/contraction-instance-macro
Apr 10, 2026
Merged

CK: Refactor 144 contraction instance files with shared CK_CONTRACTION_INSTANCE macro#6325
AviralGoelAMD merged 3 commits into
users/avirgoel/ck/contraction-macro-experimentfrom
users/avirgoel/ck/contraction-instance-macro

Conversation

@AviralGoelAMD
Copy link
Copy Markdown
Contributor

@AviralGoelAMD AviralGoelAMD commented Apr 9, 2026

Depends on #6324

Summary

Refactor all 144 contraction library instance .cpp files (bilinear + scale, 2D/6D, f32/f64/bf16/cf32/cf64) to use a shared CK_CONTRACTION_INSTANCE macro defined in contraction_instance_common.hpp. Each 58-line file is reduced to 12 lines — zero unique logic, pure parameterization.

Metric Value
Files changed 145 (144 instances + 1 new shared header)
Insertions +1,373
Deletions −7,890
Net lines removed −6,517

What changed

Before After
144 instance files, each ~58 lines of identical boilerplate (includes, namespace, type alias, registration function) differing only in 12 template parameters 1 shared macro header + 144 files at ~12 lines each (copyright + include + macro invocation)

Macro parameters (12)

CK_CONTRACTION_INSTANCE(INST_TPL, OP_NAME, CDE_OP, NDIM_VAL, NAME_SUFFIX,
    ADATA, BDATA, ACC, CSHUFFLE, DS_TUPLE, EDATA, COMPUTE)
Parameter Example Purpose
INST_TPL device_contraction_kk_instance Device template to instantiate
OP_NAME bilinear Lowercase, used in ## token pasting for function/type names
CDE_OP Bilinear C++ type name (capitalized) for template args
NDIM_VAL 2 or 6 Number of dimensions
NAME_SUFFIX f32_f32_f32_f32_kknn Data type + layout suffix
ADATA..COMPUTE F32, F32, ... Template type arguments

Readability assessment

A code realist review confirmed this change improves readability: the original 58-line files contained zero unique logic — just mechanical boilerplate wrappers that varied only in 12 template parameters. After the macro, each file's intent is immediately clear from a single macro call, and the 12 parameters serve as a concise specification of what the instance does (data types, layout, operation). Adding a new contraction instance requires writing 1 line instead of copying and modifying 58 lines. The realist also noted that the area has very low activity (1 functional commit in 18 months), so merge conflict risk is negligible.

Cumulative cleanup series stats

PR Description Net lines
#6300 Remove 61 dead #if 0 blocks −2,648
#6302 Remove 41 commented-out dead code blocks −2,861
#6303 Remove 4 orphaned files −3,886
#6323 Extract gemm_quant test boilerplate −693
#6324 Extract contraction example boilerplate −1,016
This PR Refactor 144 contraction instance files −6,517
Total −17,621

Introduce contraction_instance_common.hpp with a shared macro that
generates both the using-alias and add_device_* registration function
for contraction instances from 12 parameters.

Refactor all 36 bilinear-2D instance files to use the macro, reducing
each from 58 to 12 lines. Net savings: 1,656 lines.
Apply CK_CONTRACTION_INSTANCE macro to the remaining 108 instance
files: bilinear-6D (36), scale-2D (36), scale-6D (36). Combined
with the previous commit, all 144 contraction instance files now
use the shared macro. Total savings: ~6,600 lines.
The parent CMakeLists.txt globs all subdirectories (line 218:
file(GLOB dir_list LIST_DIRECTORIES true *)) and attempts to
file(READ .../contraction/CMakeLists.txt) for each. Without this
file, CMake fails:

  file failed to open for reading: .../gpu/contraction/CMakeLists.txt

The contraction/ directory is header-only (contraction_instance_common.hpp).
Adding an empty CMakeLists.txt satisfies the glob/read requirement. The
directory defines no targets, so add_subdirectory() processes it harmlessly.

Fixes Jenkins CI failure on PR #6325.
@AviralGoelAMD AviralGoelAMD marked this pull request as ready for review April 10, 2026 02:21
@AviralGoelAMD AviralGoelAMD requested a review from a team as a code owner April 10, 2026 02:21
@AviralGoelAMD AviralGoelAMD merged commit 17dd5a7 into users/avirgoel/ck/contraction-macro-experiment Apr 10, 2026
31 checks passed
@AviralGoelAMD AviralGoelAMD deleted the users/avirgoel/ck/contraction-instance-macro branch April 10, 2026 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant