Files
composable_kernel/example/25_gemm_bias_e_permute
John Shumway ad57f6ef0b [CK_BUILDER] Put global CK functions in an the CK namespace (#3232)
* Wrap ck host utitlies in CK namespace.

The CK and CK-Tile source code bases are incompatible because CK is not properly using namespaces everywhere. In particular, we need to put hip_check_error in the ck namespace.

Move all functions in include/ck_/host_utility that were in global namespace into the ck namespace.

There may be additional namespace problems like this, and it's possible we'll have namespace clashes. But it is good design to properly guard our to code bases (CK and CKTile) so that they can both coexist. Moreover, estabilishing this compatiblity is essential if we are going to allow the builder to instantiate  kernels from either template library.

* Add using declarations to test code.

After moving some of the untils into the ck namespace, most examples and a few tests had to be updated to recognize the new namespace declarations. We add using declarations to individual compute units for functions that were previously in the global namespace.

* Add using declarations to client examples.
2025-11-19 11:23:02 +01:00
..

GEMM with Bias, Elementwise, and Permute Fusion

Theory

This example demonstrates GEMM fused with bias addition, elementwise operation, and permutation. This pattern is used in transformer models and other neural architectures where a linear transformation is followed by bias, activation, and layout transformation.

Mathematical Formulation:

  • GEMM: Y = A \times B
  • Bias: Z = Y + \text{bias}
  • Elementwise: E = f(Z) (e.g., activation)
  • Permute: O = \text{permute}(E, \text{axes})

Algorithmic Background:

  • The GEMM result is kept in registers, bias and elementwise ops are fused in the epilogue, and permutation is applied before writing to global memory.
  • Permutation changes the layout/order of tensor axes (e.g., NCHW to NHWC).
  • This fusion reduces memory traffic and is common in transformer and CNN pipelines.

How to Run

Prerequisites

Please follow the instructions in the main Build Guide section as a prerequisite to building and running this example.

Build and run

cd composable_kernel/example/25_gemm_bias_e_permute
mkdir build && cd build
cmake -DCMAKE_CXX_COMPILER=/opt/rocm/bin/hipcc ..
make -j

# Example run
./gemm_bias_e_permute_xdl --verify=1 --time=1

Source Code Structure

Directory Layout

example/25_gemm_bias_e_permute/
├── gemm_bias_e_permute_xdl.cpp         # Main example: sets up, runs, and verifies GEMM+Bias+Elementwise+Permute
include/ck/tensor_operation/gpu/device/
│   └── device_gemm_bias_e_permute.hpp       # Device-level API for fused GEMM
include/ck/tensor_operation/gpu/device/impl/
│   └── device_gemm_bias_e_permute_impl.hpp  # Implementation
include/ck/tensor_operation/gpu/grid/
    └── gridwise_gemm_bias_e_permute.hpp     # Grid-level kernel

Key Classes and Functions

  • DeviceGemmBiasEPermute (in device_gemm_bias_e_permute.hpp):
    Device API for GEMM fused with bias, elementwise, and permutation.
  • gridwise_gemm_bias_e_permute (in gridwise_gemm_bias_e_permute.hpp):
    Implements the tiled/blocking GEMM kernel with fused epilogue and permutation.

This example demonstrates how Composable Kernel supports efficient fusion of linear, bias, activation, and layout operations for deep learning models.