ThunderKittens Documentation

repository·main·Indexed 25 days ago

https://github.com/hazyresearch/thunderkittens

A high-performance CUDA framework for writing deep learning kernels using tile-based primitives. Optimized for NVIDIA Hopper (SM90) and Blackwell (SM100) architectures, it provides access to Tensor Cores, TMA, and asynchronous memory operations. The framework includes support for multi-GPU kernels, educational GEMM implementations for H100 and B200, and benchmarking tools for comparing performance against baselines like Flash Linear Attention, Mamba, and CUTLASS.

Tokens
12.6K
Snippets
19
Records
69
Agent score
87%

What's inside ThunderKittens

  1. Overview of ThunderKittens

    main

    ThunderKittens is a framework designed for writing high-performance deep learning kernels in CUDA for NVIDIA GPUs. It is built around the principle of manipulating small tiles of data (minimum 16x16) to maximize hardware utilization.

    Key features include:

    • Tensor Core acceleration: Support for asynchronous WGMMA (H100) and TCGEN05 (B200) calls.
    • Memory Management: Optimized shared memory usage to avoid bank conflicts, and support for Distributed Shared Memory.
    • Asynchronous Operations: Uses TMA (Tensor Memory Accelerator) for asynchronous loads/stores and hides latencies.
    • Work Overlapping: Provides a Load-Store-Compute-Finish template to overlap I/O with computation.
    • Multi-GPU Support: Enables data transfer over NVLink and NVSwitch acceleration.

    Note on Hardware Support:

    • Primary Support: Hopper and Blackwell GPUs.
    • Legacy Support: Ampere GPUs are supported but no longer actively maintained.
    • AMD Support: Use HipKittens instead.
  2. Understand ThunderKittens Data Types and Scopes

    main

    ThunderKittens operates on two primary data type categories across different NVIDIA GPU scopes:

    Data Types

    • Register Objects: Exist at the Warp level. Their contents are split among the threads of the warp.
      • Register Tiles (kittens::rt): Parameterized by layout, type, and size. Example: kittens::rt_bf<32,16> is a 32x16 bfloat16 register tile (row-layout is default).
      • Register Vectors: Associated with tiles. Flavors include naive (for compute-heavy tasks like layernorm), column (to reduce/map across tile rows), and row (to reduce/map across tile columns).
    • Shared Objects: Exist at the Block level and reside in shared memory. Example: kittens::st_hf<32, 64>.

    Scopes

    • Warp (Default): Most programming happens here. A single warp performs the work. Use kittens::warpid() to get the current warp ID.
    • Warpgroup: A group of 4 warps. Required for H100 asynchronous matrix multiply-accumulate (WGMMA) instructions. Access via kittens::group<4> or kittens::warpgroup.
    • Block: A group of warps (often 8) that shares shared memory.
    • Grid: A collection of blocks.
  3. Setup the Python environment for Based Architecture demos

    main

    To run the Based architecture demos, create a Python 3.11 environment and install the required dependencies including torch, transformers, einops, hydra-core, and flash-attn.

    conda create -n dev python=3.11
    pip3 install torch torchvision torchaudio
    pip install transformers
    pip install einops
    pip install hydra-core
    pip install flash-attn
  4. Run educational H100 GEMM kernels

    main

    This directory contains a step-by-step educational implementation of H100 GEMM kernels. To progress through the different implementation levels, you must modify the LEVEL field in the Makefile and then execute the build and run commands.

    Available levels:

    • Level 01: Simple for loop (float)
    • Level 02: Simple for loop (bf16)
    • Level 03: Use shared memory
    • Level 04: Use tensor cores (WMMA)
    • Level 05: Use tensor cores (WGMMA)
    • Level 06: Use tensor memory accelerator (TMA) and double buffering
    • Level 07: Use work partitioning
    • Level 08: Use multiple consumer warpgroups
  5. Build and Run Pre-implemented Kernels

    main

    To use the provided kernels (e.g., kernels/gemm/bf16_h100) in your PyTorch code, follow these steps:

    1. Prerequisites: Ensure your Python environment has PyTorch 2.8+ and PyBind11 installed, with CUDA compatibility.
    2. Environment Setup: Set the following environment variables to speed up the build process:
      export PYTHON_VERSION=$(python3 -c "import sysconfig; print(sysconfig.get_config_var('LDVERSION'))")
      export PYTHON_INCLUDES=$(python3 -c "import sysconfig; print('-I', sysconfig.get_path('include'), sep='')")
      export PYBIND_INCLUDES=$(python3 -m pybind11 --includes)
      export PYTORCH_INCLUDES=$(python3 -c "from torch.utils.cpp_extension import include_paths; print(' '.join(['-I' + p for p in include_paths()]))")
      export PYTHON_LIBDIR=$(python3 -c "import sysconfig; print('-L', sysconfig.get_config_var('LIBDIR'), sep='')")
      export PYTORCH_LIBDIR=$(python3 -c "from torch.utils.cpp_extension import library_paths; print(' '.join(['-L' + p for p in library_paths()]))")
    3. Build:
      • cd into the specific kernel directory.
      • Edit the Makefile if necessary.
      • Run make.
    4. Run: Execute make run to run correctness tests and benchmarks.
    # Setup environment
    export PYTHON_VERSION=$(python3 -c "import sysconfig; print(sysconfig.get_config_var('LDVERSION'))")
    export PYTHON_INCLUDES=$(python3 -c "import sysconfig; print('-I', sysconfig.get_path('include'), sep='')")
    export PYBIND_INCLUDES=$(python3 -m pybind11 --includes)
    export PYTORCH_INCLUDES=$(python3 -c "from torch.utils.cpp_extension import include_paths; print(' '.join(['-I' + p for p in include_paths()]))")
    export PYTHON_LIBDIR=$(python3 -c "import sysconfig; print('-L', sysconfig.get_config_var('LIBDIR'), sep='')")
    export PYTORCH_LIBDIR=$(python3 -c "from torch.utils.cpp_extension import library_paths; print(' '.join(['-L' + p for p in library_paths()]))")
    
    # Build and run
    cd kernels/gemm/bf16_h100
    make
    make run
  6. Install benchmarking baselines (Flash Linear Attention and Mamba)

    main

    To compare ThunderKittens against existing baselines, install the Flash Linear Attention and Mamba kernels.

    git clone https://github.com/sustcsonglin/flash-linear-attention.git
    pip install -U git+https://github.com/sustcsonglin/flash-linear-attention
    
    pip install mamba_ssm
  7. Run ThunderKittens Multi-GPU Kernels

    main

    To run a specific operator kernel, navigate to its directory and use the make run command. This will compile the kernel and execute it using an 8-GPU torchrun configuration.

    Note on GPU Count: All kernels assume 8 GPUs by default. To use a different number of devices, you must manually modify the static constexpr int NUM_DEVICES field within the kernel source code.

    cd <directory>
    make run
  8. Install ThunderKittens

    main

    ThunderKittens is a header-only library. There is no installation process required; simply clone the repository and include kittens.cuh in your CUDA projects.

    Build Requirements

    1. CUDA Environment

    Requires CUDA 12.8+. Ensure your environment variables are set correctly:

    export CUDA_HOME=/usr/local/cuda-<YOUR-CUDA-VERSION> # ex. cuda-12.8
    export PATH=${CUDA_HOME}/bin:${PATH} 
    export LD_LIBRARY_PATH=${CUDA_HOME}/lib64:$LD_LIBRARY_PATH

    2. C++20 Compiler

    ThunderKittens uses C++20 concepts. Ensure you have a modern compiler (e.g., gcc-11 or clang-11). On Ubuntu, you can update using:

    sudo apt update
    sudo apt install gcc-11 g++-11
    
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 100 --slave /usr/bin/g++ g++ /usr/bin/g++-11
    
    sudo apt update
    sudo apt install clang-11

    3. Troubleshooting libc10.so errors

    If you encounter libc10.so errors, you may need to add the PyTorch library path to your LD_LIBRARY_PATH. Find the path using Python and export it:

    # Find the path
    python -c "import torch; print(torch.file)"
    
    # Export the path (replace <PRINTED_PATH> with the output from the command above)
    export LD_LIBRARY_PATH=<PRINTED_PATH>/lib:$LD_LIBRARY_PATH
  9. Run and customize ThunderKittens unit tests

    main

    ThunderKittens includes a comprehensive unit testing suite located in the tests/ directory. Running the tests involves compiling thousands of kernels, which may cause high system load for several minutes.

    To compile the tests, run make -j inside the tests/ folder.

    You can customize the compilation using the following tests/Makefile options:

    • ARCH: Target GPU architecture. Options: SM80, SM90, SM100, SM103, or SM120 (default: SM90).
    • COMP_LEVEL: Compiler optimization level. Options: fast, debug, or profile (default: fast).
    • TEST_INTENSITY: Level of test intensity. Options: 1, 2, 3, or 4 (default: 2).
    • TEST_ALL: Compile and run all tests. You can also specify individual sections using flags like -DTEST_WARP_MEMORY or -DTEST_WARP_MEMORY_VEC_DSMEM.