Kiefer van Teutem 05615558c6 [rocm-libraries] ROCm/rocm-libraries#8315 (commit bda7d43)
feat: CK Tile unification - swizzle support + gfx950 mixed
 prec scale + misc (#8315)

ISSUE ID #8960
https://github.com/ROCm/rocm-libraries/issues/8960

## Motivation

This MR is about adding Swizzle support to the Tile Distribution
Encoding Calculator and Mma Pipelines in the Unification framework.
Swizzle is a modifier for Tile Distribution Encodings that effectively
performs a permutation in the M dimension. This means that it affects
the Tile Distribution Encodings of A and C. When combined with
CTranspose, it affects the Encodings of B and C instead. In principle,
for a regular gemm, the Swizzle factor does not affect the correctness
of the kernel, since matrix multiplication is symmetric under
permutations of rows and columns (M). However, this is only true if the
same Encodings are used for the loading and storing of the data. For
consecutive matrix multiplications, we may be in a situation where we
use Swizzle to account for the effective layout of an intermediate
result, so that it can immediately be used in another matrix operation
without additional shuffling. In these cases, the Swizzle factor is
crucial for correctness. As far as I know, this seems most likely to
occur in attention kernels.

### Changes

- I adapted the Tile Distribution Encoding Calculator to accept any
Swizzle modifier, and use this to modify the layouts just like in CK
Tile. Note that Swizzle is only compatible with certain intrinsics, due
to the restriction that the Swizzle factor divides kCMNumAccess. This is
possible for 32x32 MFMA instructions with SFactor 2 or 4, and for gfx11
WMMA instructions with SFactor 2, 4, or 8, although this is not used in
CK Tile.

- I adapted the layout test to check the correctness of layout *with*
Swizzle modification, for all possible Swizzle factors for each
intrinsic.

- I adapted the Unification Dispatcher to take a Swizzle Factor and pass
it on to the MmaPipelines. Note that the original dispatcher takes a
boolean instead, which I convert to an SFactor of 2 when true. I believe
this is correct since in all cases where CK Tile previously used the old
dispatcher, and SFactor of 2 ended up being used. However, there are two
named WarpGemms
(WarpGemmMfmaFp8Fp8F32M32N32K32SwizzleBTransposedCDistribution and
WarpGemmMfmaI8I8I32M32N32K32SwizzleBTransposedCDistribution) which can
support any Swizzle factor, and are actually used with Swizzle factors
up to 4. These were not used in the old dispatcher but instead always
used directly in CK Tile pipelines.

- I added custom named WarpGemms in case the Unification flag is ON, for
the named WarpGemms using Swizzle that are directly used in CK Tile
pipelines. There are only two of them and they are the ones mentioned in
the previous point.

### Changes part 2

While trying to get a swizzle example to work, I ended up having to add
a lot of other changes which would have normally been their own issue.
We have:

- Adding all mixed precision gfx950 scale intrinsics (50 in total)
- Adding these intrinsics to the layout test
- Tile distribution encoding tweak: Allow for simplified C layouts in
blockless cases
- MmaPipelines tweaks: Make pretty much all old-style layout params
available

### Note on AttrNumAccess
For the scale gfx950 intrinsics, the "canonical" layouts for A and B
have NumAccess 1 or 2, depending on the A and B types. The 8-bit types
have a canonical NumAccess of 2, and the others 1. So overall we may
have (1, 1), (2, 1), (1, 2), or (2, 2). This is reflected in the
intrinsic definitions. However, for the fully 8-bit intrinsics I still
define them with (1, 1). The reason for this is that it is in principle
possible to use these intrinsics with (1, 1) as long as you don't use
scale. This may actually happen in CK Tile. Furthermore, there are some
pipelines that instantiate a WarpGemm with (1, 1) just to peek at some
parameters. Note that the (1, 2) and (2, 1) cases MUST have these
NumAccess values or the base MMA does not work (regardless of scale).
This is because you can't just permute K for A without doing the same
for B and vice versa.

### Tests

Layout tests with swizzle work. tile_example_fmha_fwd and
tile_example_fmha_bwd now compile and run, with correct verification for
default settings. With fp8bf16 and init=3, get 5% wrong results on both
this branch and develop, and this one is definitely sensitive to
swizzle, because without swizzle it's 50% wrong.

Better test: test_ck_tile_fmha_fwd_fp8bf16. This one behaves as expected
and confirms that swizzle is genuinely necessary for correctness and
working properly in the unification framework. It passed on develop and
on my this branch with unification on, and failed when I forced a
swizzlefactor of 1 (failed 40 out of 43 unskipped tests).
2026-07-07 08:16:56 +00:00
2018-10-08 22:49:58 -05:00
2025-01-07 08:29:40 -08:00
2025-07-24 12:38:24 -07:00

Composable Kernel

Note

The published documentation is available at Composable Kernel in an organized, easy-to-read format, with search and a table of contents. The documentation source files reside in the docs folder of this repository. As with all ROCm projects, the documentation is open source. For more information on contributing to the documentation, see Contribute to ROCm documentation.

The Composable Kernel (CK) library provides a programming model for writing performance-critical kernels for machine learning workloads across multiple architectures (GPUs, CPUs, etc.). The CK library uses general purpose kernel languages, such as HIP C++.

CK uses two concepts to achieve performance portability and code maintainability:

  • A tile-based programming model
  • Algorithm complexity reduction for complex machine learning (ML) operators. This uses an innovative technique called Tensor Coordinate Transformation.

ALT

The current CK library is structured into four layers:

  • Templated Tile Operators
  • Templated Kernel and Invoker
  • Instantiated Kernel and Invoker
  • Client API

ALT

General information

CK is released under the MIT license.

Building CK

We recommend building CK inside Docker containers, which include all necessary packages. Pre-built Docker images are available on DockerHub.

  1. To build a new Docker image, use the Dockerfile provided with the source code:

    DOCKER_BUILDKIT=1 docker build -t ck:latest -f Dockerfile .
    
  2. Launch the Docker container:

    docker run                                     \
    -it                                            \
    --privileged                                   \
    --group-add sudo                               \
    -w /root/workspace                             \
    -v ${PATH_TO_LOCAL_WORKSPACE}:/root/workspace  \
    ck:latest                                      \
    /bin/bash
    
  3. Clone CK source code from the GitHub repository and start the build:

    git clone https://github.com/ROCm/composable_kernel.git && \
    cd composable_kernel && \
    mkdir build && \
    cd build
    

    You must set the GPU_TARGETS macro to specify the GPU target architecture(s) you want to run CK on. You can specify single or multiple architectures. If you specify multiple architectures, use a semicolon between each; for example, gfx908;gfx90a;gfx942.

    cmake                                                                                             \
    -D CMAKE_PREFIX_PATH=/opt/rocm                                                                    \
    -D CMAKE_CXX_COMPILER=/opt/rocm/bin/hipcc                                                         \
    -D CMAKE_BUILD_TYPE=Release                                                                       \
    -D GPU_TARGETS="gfx908;gfx90a"                                                                    \
    ..
    

    If you don't set GPU_TARGETS on the cmake command line, CK is built for all GPU targets supported by the current compiler (this may take a long time). Tests and examples will only get built if the GPU_TARGETS is set by the user on the cmake command line.

    NOTE: If you try setting GPU_TARGETS to a list of architectures, the build will only work if the architectures are similar, e.g., gfx908;gfx90a, or gfx1100;gfx1101;gfx11012. Otherwise, if you want to build the library for a list of different architectures, you should use the GPU_ARCHS build argument, for example GPU_ARCHS=gfx908;gfx1030;gfx1100;gfx942.

    Convenience script for development builds:

    Alternatively, you can use the provided convenience script script/cmake-ck-dev.sh which automatically configures CK for development with sensible defaults. In the build directory:

    ../script/cmake-ck-dev.sh
    

    This script:

    • Cleans CMake cache files before configuring
    • Sets BUILD_DEV=ON for development mode
    • Defaults to GPU targets: gfx908;gfx90a;gfx942
    • Enables verbose makefile output
    • Sets additional compiler flags for better error messages

    By default, it considers the parent directory to be the project source directory.

    You can specify the source directory as the first argument. You can specify custom GPU targets (semicolon-separated) as the second argument:

    ../script/cmake-ck-dev.sh .. gfx1100
    

    Or pass additional cmake arguments:

    ../script/cmake-ck-dev.sh .. gfx90a -DCMAKE_BUILD_TYPE=Release
    

    Fast iteration builds:

    For faster CMake configuration during development (~5s vs ~150s), use the --minimal flag to disable building device instances, profiler, examples, tutorials, and tests:

    ../script/cmake-ck-dev.sh --minimal .. gfx90a
    

    You can also specify a custom preset:

    ../script/cmake-ck-dev.sh --preset=dev-minimal .. gfx90a
    
  4. Build the entire CK library:

    make -j"$(nproc)"
    
  5. Install CK:

    make -j install
    

    See Note on -j

Building for Windows

Install TheRock and run CMake configure as

    cmake                                                                                      \
    -D CMAKE_PREFIX_PATH="C:/dist/TheRock"                                                     \
    -D CMAKE_CXX_COMPILER="C:/dist/TheRock/bin/hipcc.exe"                                      \
    -D CMAKE_BUILD_TYPE=Release                                                                \
    -D GPU_TARGETS="gfx1151"                                                                   \
    -G Ninja                                                                                   \
    ..

Use Ninja to build either the whole library or individual targets.

Optional post-install steps

  • Build examples and tests:

    make -j examples tests
    
  • Build and run all examples and tests:

    make -j check
    

    You can find instructions for running each individual example in example.

  • Build and run smoke/regression examples and tests:

    make -j smoke # tests and examples that run for < 30 seconds each
    
    make -j regression # tests and examples that run for >= 30 seconds each
    
  • Build ckProfiler:

    make -j ckProfiler
    

    You can find instructions for running ckProfiler in profiler.

  • Build our documentation locally:

    cd docs
    pip3 install -r sphinx/requirements.txt
    python3 -m sphinx -T -E -b html -d _build/doctrees -D language=en . _build/html
    

Notes

The -j option for building with multiple threads in parallel, which speeds up the build significantly. However, -j launches unlimited number of threads, which can cause the build to run out of memory and crash. On average, you should expect each thread to use ~2Gb of RAM. Depending on the number of CPU cores and the amount of RAM on your system, you may want to limit the number of threads. For example, if you have a 128-core CPU and 128 Gb of RAM it's advisable to use -j32.

Additional cmake flags can be used to significantly speed-up the build:

  • DTYPES (default is not set) can be set to any subset of "fp64;fp32;tf32;fp16;fp8;bf16;int8" to build instances of select data types only. The main default data types are fp32 and fp16; you can safely skip other data types.

  • DISABLE_DL_KERNELS (default is OFF) must be set to ON in order not to build instances, such as gemm_dl or batched_gemm_multi_d_dl. These instances are useful on architectures like the NAVI2x, as most other platforms have faster instances, such as xdl or wmma, available.

  • DISABLE_DPP_KERNELS (default is OFF) must be set to ON in order not to build instances, such as gemm_dpp. These instances offer a slightly better performance of fp16 gemms on NAVI2x. But on other architectures faster alternatives are available.

  • CK_USE_FP8_ON_UNSUPPORTED_ARCH (default is OFF) must be set to ON in order to build instances, such as gemm_universal, and gemm_multiply_multiply for fp8 data type for GPU targets which do not have native support for fp8 data type, such as gfx908 or gfx90a. These instances are useful on architectures like the MI100/MI200 for the functional support only.

Using sccache for building

The default CK Docker images come with a pre-installed version of sccache, which supports clang being used as hip-compiler (" -x hip"). Using sccache can help reduce the time to re-build code from hours to 1-2 minutes. In order to invoke sccache, you need to run:

 sccache --start-server

then add the following flags to the cmake command line:

 -DCMAKE_HIP_COMPILER_LAUNCHER=sccache -DCMAKE_CXX_COMPILER_LAUNCHER=sccache -DCMAKE_C_COMPILER_LAUNCHER=sccache

You may need to clean up the build folder and repeat the cmake and make steps in order to take advantage of the sccache during subsequent builds.

Using CK as pre-built kernel library

You can find instructions for using CK as a pre-built kernel library in client_example.

Contributing to CK

When you contribute to CK, make sure you run clang-format on all changed files. We highly recommend using git hooks that are managed by the pre-commit framework. To install hooks, run:

sudo script/install_precommit.sh

With this approach, pre-commit adds the appropriate hooks to your local repository and automatically runs clang-format (and possibly additional checks) before any commit is created.

If you need to uninstall hooks from the repository, you can do so by running the following command:

script/uninstall_precommit.sh

If you need to temporarily disable pre-commit hooks, you can add the --no-verify option to the git commit command.

Description
[DEPRECATED] Moved to ROCm/rocm-libraries repo. NOTE: develop branch is maintained as a read-only mirror
Readme MIT Cite this repository 242 MiB
Languages
C++ 90.9%
Python 6.4%
CMake 1.7%
Shell 0.5%
Pawn 0.2%
Other 0.1%