Skip to content
KernelIndex
Search⌘K

submission 153299

gau.nernst · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

No package. Vendor the mirrored source: 1369 lines, June 9 Researcher Reciprocity License v1.0.

submission_mix.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-nvfp4-gemm-153299?include=source"
interfacepython
Compatibility
measured onNVIDIA B200
declared hardwareNVIDIA B200
architecturessm_100
dtypesfp8_e4m3, nvfp4

Benchmark evidence

1 measurement across 1 GPU, fastest first.

Operation / workload
Hardware
Latency
Rank
Observed
NVFP4 GEMMsuite of 3 cases
NVIDIA B200
10.7µs
#18 of 369
2025-12-13

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:8fdb3bf507fb8456a884d207c7ab60e435c00e6facb0fac763dcfe508df269a8
license declaredunknown
license concludedunknown
authorsgau.nernst
imported2026-08-15

Techniques

Extracted from the mirrored source by pattern, never inferred. Each row cites its line.

fused-epilogueauto epilogue_M_major = [&]() {
mbarriervoid mbarrier_init(int mbar_addr, int count) {
num-warps = 4constexpr int NUM_WARPS = 4;
shared-memoryextern __shared__ __align__(1024) char smem_ptr[];
split-kint SPLIT_K,
tcgen05asm volatile("tcgen05.cp.cta_group::1.32x128b.warpx4 [%0], %1;" :: "r"(taddr), "l"(s_desc));
tile-m = 128constexpr int BLOCK_M = 128;
tile-n = 128constexpr int BLOCK_N = 128;
tmaasm volatile("cp.async.bulk.shared::cta.global.mbarrier::complete_tx::bytes.L2::cache_hint [%0], [%1], %2, [%3];"
vector-width = float2reinterpret_cast<float2 *>(out_ptr + 0 * N)[0] = float2({tmp[i * 4 + 0], tmp[i * 4 + 1]});

Kernel source

submission_mix.py1369 lines
#!POPCORN leaderboard nvfp4_gemm
#!POPCORN gpu NVIDIA

import torch
from task import input_t, output_t
from torch.utils.cpp_extension import load_inline

CUDA_SRC_V1 = r"""
#include <cudaTypedefs.h>
#include <cuda_fp16.h>

#include <torch/library.h>
#include <ATen/core/Tensor.h>

constexpr int WARP_SIZE = 32;
constexpr int NUM_WARPS = 4;
constexpr int TB_SIZE = NUM_WARPS * WARP_SIZE;

constexpr int BLOCK_M = 128;
constexpr int BLOCK_N = 128;
constexpr int MMA_K = 64;  // 32 bytes

// https://github.com/NVIDIA/cutlass/blob/v4.3.2/include/cute/arch/copy_sm90_desc.hpp#L193-L197
constexpr uint64_t EVICT_NORMAL = 0x1000000000000000;
constexpr uint64_t EVICT_FIRST = 0x12F0000000000000;
constexpr uint64_t EVICT_LAST = 0x14F0000000000000;

__device__ inline
constexpr uint64_t desc_encode(uint64_t x) { return (x & 0x3'FFFFULL) >> 4ULL; };

// https://github.com/NVIDIA/cutlass/blob/v4.2.1/include/cute/arch/cluster_sm90.hpp#L180
__device__
uint32_t elect_sync() {
  uint32_t pred = 0;
  asm volatile(
    "{\n\t"
    ".reg .pred %%px;\n\t"
    "elect.sync _|%%px, %1;\n\t"
    "@%%px mov.s32 %0, 1;\n\t"
    "}"
    : "+r"(pred)
    : "r"(0xFFFFFFFF)
  );
  return pred;
}

__device__ inline
void mbarrier_init(int mbar_addr, int count) {
  asm volatile("mbarrier.init.shared::cta.b64 [%0], %1;" :: "r"(mbar_addr), "r"(count));
}

// https://github.com/NVIDIA/cutlass/blob/v4.2.1/include/cutlass/arch/barrier.h#L408
__device__
void mbarrier_wait(int mbar_addr, int phase) {
  uint32_t ticks = 0x989680;  // this is optional
  asm volatile(
    "{\n\t"
    ".reg .pred P1;\n\t"
    "LAB_WAIT:\n\t"
    "mbarrier.try_wait.parity.acquire.cta.shared::cta.b64 P1, [%0], %1, %2;\n\t"
    "@P1 bra.uni DONE;\n\t"
    "bra.uni LAB_WAIT;\n\t"
    "DONE:\n\t"
    "}"
    :: "r"(mbar_addr), "r"(phase), "r"(ticks)
  );
}

template <uint64_t cache_policy = 0>
__device__ inline
void tma_gmem2smem(int dst, const void *src, int size, int mbar_addr) {
  if constexpr (cache_policy == 0)
    asm volatile("cp.async.bulk.shared::cta.global.mbarrier::complete_tx::bytes.L2::cache_hint [%0], [%1], %2, [%3];"
                :: "r"(dst), "l"(src), "r"(size), "r"(mbar_addr));
  else
    asm volatile("cp.async.bulk.shared::cta.global.mbarrier::complete_tx::bytes.L2::cache_hint [%0], [%1], %2, [%3], %4;"
                :: "r"(dst), "l"(src), "r"(size), "r"(mbar_addr), "l"(cache_policy));
}

template <uint64_t cache_policy = 0>
__device__ inline
void tma_3d_gmem2smem(int dst, const void *tmap_ptr, int x, int y, int z, int mbar_addr) {
  if constexpr (cache_policy == 0)
    asm volatile("cp.async.bulk.tensor.3d.shared::cta.global.mbarrier::complete_tx::bytes.cta_group::1.L2::cache_hint "
                "[%0], [%1, {%2, %3, %4}], [%5];"
                :: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(y), "r"(z), "r"(mbar_addr)
                : "memory");
  else
    asm volatile("cp.async.bulk.tensor.3d.shared::cta.global.mbarrier::complete_tx::bytes.cta_group::1.L2::cache_hint "
                "[%0], [%1, {%2, %3, %4}], [%5], %6;"
                :: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(y), "r"(z), "r"(mbar_addr), "l"(cache_policy)
                : "memory");
}

__device__ inline
void tcgen05_cp_nvfp4(int taddr, uint64_t s_desc) {
  // .32x128b corresponds to (32, 16) 8-bit scale -> 1 MMA for nvfp4.
  // .warpx4 duplicates data across 32-lane groups.
  asm volatile("tcgen05.cp.cta_group::1.32x128b.warpx4 [%0], %1;" :: "r"(taddr), "l"(s_desc));
}

__device__ inline
void tcgen05_mma_nvfp4(
  uint64_t a_desc,
  uint64_t b_desc,
  uint32_t i_desc,
  int scale_A_tmem,
  int scale_B_tmem,
  int enable_input_d
) {
  const int d_tmem = 0;  // assume
  asm volatile(
    "{\n\t"
    ".reg .pred p;\n\t"  // predicate register enable-input-d
    "setp.ne.b32 p, %6, 0;\n\t"
    "tcgen05.mma.cta_group::1.kind::mxf4nvf4.block_scale.block16 [%0], %1, %2, %3, [%4], [%5], p;\n\t"
    "}"
    :: "r"(d_tmem), "l"(a_desc), "l"(b_desc), "r"(i_desc),
       "r"(scale_A_tmem), "r"(scale_B_tmem), "r"(enable_input_d)
  );
}

// see https://docs.nvidia.com/cuda/inline-ptx-assembly/index.html
struct SHAPE {
  static constexpr char _32x32b[]  = ".32x32b";   // 32x1 tile for each warp
  static constexpr char _16x128b[] = ".16x128b";  // 16x4 tile
  static constexpr char _16x256b[] = ".16x256b";  // 16x8 tile
};

struct NUM {
  static constexpr char x4[]  = ".x4";
  static constexpr char x8[]  = ".x8";
  static constexpr char x16[] = ".x16";
  static constexpr char x32[] = ".x32";
  static constexpr char x64[] = ".x64";
  static constexpr char x128[] = ".x128";
};

template <const char *SHAPE, const char *NUM>
__device__ inline
void tcgen05_ld_16regs(float *tmp, int row, int col) {
  asm volatile("tcgen05.ld.sync.aligned%17%18.b32 "
              "{ %0,  %1,  %2,  %3,  %4,  %5,  %6,  %7, "
              "  %8,  %9, %10, %11, %12, %13, %14, %15}, [%16];"
              : "=f"(tmp[ 0]), "=f"(tmp[ 1]), "=f"(tmp[ 2]), "=f"(tmp[ 3]), "=f"(tmp[ 4]), "=f"(tmp[ 5]), "=f"(tmp[ 6]), "=f"(tmp[ 7]),
                "=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15])
              : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
}

template <const char *SHAPE, const char *NUM>
__device__ inline
void tcgen05_ld_32regs(float *tmp, int row, int col) {
  asm volatile("tcgen05.ld.sync.aligned%33%34.b32 "
              "{ %0,  %1,  %2,  %3,  %4,  %5,  %6,  %7, "
              "  %8,  %9, %10, %11, %12, %13, %14, %15, "
              " %16, %17, %18, %19, %20, %21, %22, %23, "
              " %24, %25, %26, %27, %28, %29, %30, %31}, [%32];"
              : "=f"(tmp[ 0]), "=f"(tmp[ 1]), "=f"(tmp[ 2]), "=f"(tmp[ 3]), "=f"(tmp[ 4]), "=f"(tmp[ 5]), "=f"(tmp[ 6]), "=f"(tmp[ 7]),
                "=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15]),
                "=f"(tmp[16]), "=f"(tmp[17]), "=f"(tmp[18]), "=f"(tmp[19]), "=f"(tmp[20]), "=f"(tmp[21]), "=f"(tmp[22]), "=f"(tmp[23]),
                "=f"(tmp[24]), "=f"(tmp[25]), "=f"(tmp[26]), "=f"(tmp[27]), "=f"(tmp[28]), "=f"(tmp[29]), "=f"(tmp[30]), "=f"(tmp[31])
              : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
}

template <const char *SHAPE, const char *NUM>
__device__ inline
void tcgen05_ld_64regs(float *tmp, int row, int col) {
  asm volatile("tcgen05.ld.sync.aligned%65%66.b32 "
              "{ %0,  %1,  %2,  %3,  %4,  %5,  %6,  %7, "
              "  %8,  %9, %10, %11, %12, %13, %14, %15, "
              " %16, %17, %18, %19, %20, %21, %22, %23, "
              " %24, %25, %26, %27, %28, %29, %30, %31, "
              " %32, %33, %34, %35, %36, %37, %38, %39, "
              " %40, %41, %42, %43, %44, %45, %46, %47, "
              " %48, %49, %50, %51, %52, %53, %54, %55, "
              " %56, %57, %58, %59, %60, %61, %62, %63}, [%64];"
              : "=f"(tmp[ 0]), "=f"(tmp[ 1]), "=f"(tmp[ 2]), "=f"(tmp[ 3]), "=f"(tmp[ 4]), "=f"(tmp[ 5]), "=f"(tmp[ 6]), "=f"(tmp[ 7]),
                "=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15]),
                "=f"(tmp[16]), "=f"(tmp[17]), "=f"(tmp[18]), "=f"(tmp[19]), "=f"(tmp[20]), "=f"(tmp[21]), "=f"(tmp[22]), "=f"(tmp[23]),
                "=f"(tmp[24]), "=f"(tmp[25]), "=f"(tmp[26]), "=f"(tmp[27]), "=f"(tmp[28]), "=f"(tmp[29]), "=f"(tmp[30]), "=f"(tmp[31]),
                "=f"(tmp[32]), "=f"(tmp[33]), "=f"(tmp[34]), "=f"(tmp[35]), "=f"(tmp[36]), "=f"(tmp[37]), "=f"(tmp[38]), "=f"(tmp[39]),
                "=f"(tmp[40]), "=f"(tmp[41]), "=f"(tmp[42]), "=f"(tmp[43]), "=f"(tmp[44]), "=f"(tmp[45]), "=f"(tmp[46]), "=f"(tmp[47]),
                "=f"(tmp[48]), "=f"(tmp[49]), "=f"(tmp[50]), "=f"(tmp[51]), "=f"(tmp[52]), "=f"(tmp[53]), "=f"(tmp[54]), "=f"(tmp[55]),
                "=f"(tmp[56]), "=f"(tmp[57]), "=f"(tmp[58]), "=f"(tmp[59]), "=f"(tmp[60]), "=f"(tmp[61]), "=f"(tmp[62]), "=f"(tmp[63])
              : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
}

template <const char *SHAPE, const char *NUM>
__device__ inline
void tcgen05_ld_128regs(float *tmp, int row, int col) {
  asm volatile("tcgen05.ld.sync.aligned%129%130.b32 "
              "{ %0,  %1,  %2,  %3,  %4,  %5,  %6,  %7, "
              "  %8,  %9, %10, %11, %12, %13, %14, %15, "
              " %16, %17, %18, %19, %20, %21, %22, %23, "
              " %24, %25, %26, %27, %28, %29, %30, %31, "
              " %32, %33, %34, %35, %36, %37, %38, %39, "
              " %40, %41, %42, %43, %44, %45, %46, %47, "
              " %48, %49, %50, %51, %52, %53, %54, %55, "
              " %56, %57, %58, %59, %60, %61, %62, %63, "
              " %64, %65, %66, %67, %68, %69, %70, %71, "
              " %72, %73, %74, %75, %76, %77, %78, %79, "
              " %80, %81, %82, %83, %84, %85, %86, %87, "
              " %88, %89, %90, %91, %92, %93, %94, %95, "
              " %96, %97, %98, %99,%100,%101,%102,%103, "
              "%104,%105,%106,%107,%108,%109,%110,%111, "
              "%112,%113,%114,%115,%116,%117,%118,%119, "
              "%120,%121,%122,%123,%124,%125,%126,%127}, [%128];"
              : "=f"(tmp[ 0]), "=f"(tmp[ 1]), "=f"(tmp[ 2]), "=f"(tmp[ 3]), "=f"(tmp[ 4]), "=f"(tmp[ 5]), "=f"(tmp[ 6]), "=f"(tmp[ 7]),
                "=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15]),
                "=f"(tmp[16]), "=f"(tmp[17]), "=f"(tmp[18]), "=f"(tmp[19]), "=f"(tmp[20]), "=f"(tmp[21]), "=f"(tmp[22]), "=f"(tmp[23]),
                "=f"(tmp[24]), "=f"(tmp[25]), "=f"(tmp[26]), "=f"(tmp[27]), "=f"(tmp[28]), "=f"(tmp[29]), "=f"(tmp[30]), "=f"(tmp[31]),
                "=f"(tmp[32]), "=f"(tmp[33]), "=f"(tmp[34]), "=f"(tmp[35]), "=f"(tmp[36]), "=f"(tmp[37]), "=f"(tmp[38]), "=f"(tmp[39]),
                "=f"(tmp[40]), "=f"(tmp[41]), "=f"(tmp[42]), "=f"(tmp[43]), "=f"(tmp[44]), "=f"(tmp[45]), "=f"(tmp[46]), "=f"(tmp[47]),
                "=f"(tmp[48]), "=f"(tmp[49]), "=f"(tmp[50]), "=f"(tmp[51]), "=f"(tmp[52]), "=f"(tmp[53]), "=f"(tmp[54]), "=f"(tmp[55]),
                "=f"(tmp[56]), "=f"(tmp[57]), "=f"(tmp[58]), "=f"(tmp[59]), "=f"(tmp[60]), "=f"(tmp[61]), "=f"(tmp[62]), "=f"(tmp[63]),
                "=f"(tmp[64]), "=f"(tmp[65]), "=f"(tmp[66]), "=f"(tmp[67]), "=f"(tmp[68]), "=f"(tmp[69]), "=f"(tmp[70]), "=f"(tmp[71]),
                "=f"(tmp[72]), "=f"(tmp[73]), "=f"(tmp[74]), "=f"(tmp[75]), "=f"(tmp[76]), "=f"(tmp[77]), "=f"(tmp[78]), "=f"(tmp[79]),
                "=f"(tmp[80]), "=f"(tmp[81]), "=f"(tmp[82]), "=f"(tmp[83]), "=f"(tmp[84]), "=f"(tmp[85]), "=f"(tmp[86]), "=f"(tmp[87]),
                "=f"(tmp[88]), "=f"(tmp[89]), "=f"(tmp[90]), "=f"(tmp[91]), "=f"(tmp[92]), "=f"(tmp[93]), "=f"(tmp[94]), "=f"(tmp[95]),
                "=f"(tmp[96]), "=f"(tmp[97]), "=f"(tmp[98]), "=f"(tmp[99]), "=f"(tmp[100]),"=f"(tmp[101]),"=f"(tmp[102]),"=f"(tmp[103]),
                "=f"(tmp[104]),"=f"(tmp[105]),"=f"(tmp[106]),"=f"(tmp[107]),"=f"(tmp[108]),"=f"(tmp[109]),"=f"(tmp[110]),"=f"(tmp[111]),
                "=f"(tmp[112]),"=f"(tmp[113]),"=f"(tmp[114]),"=f"(tmp[115]),"=f"(tmp[116]),"=f"(tmp[117]),"=f"(tmp[118]),"=f"(tmp[119]),
                "=f"(tmp[120]),"=f"(tmp[121]),"=f"(tmp[122]),"=f"(tmp[123]),"=f"(tmp[124]),"=f"(tmp[125]),"=f"(tmp[126]),"=f"(tmp[127])
              : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
}

__device__ inline void tcgen05_ld_32x32bx32(float *tmp, int row, int col) { tcgen05_ld_32regs<SHAPE::_32x32b, NUM::x32>(tmp, row, col); }
__device__ inline void tcgen05_ld_32x32bx64(float *tmp, int row, int col) { tcgen05_ld_64regs<SHAPE::_32x32b, NUM::x64>(tmp, row, col); }
__device__ inline void tcgen05_ld_32x32bx128(float *tmp, int row, int col) { tcgen05_ld_128regs<SHAPE::_32x32b, NUM::x128>(tmp, row, col); }

__device__ inline void tcgen05_ld_16x128bx8(float *tmp, int row, int col) { tcgen05_ld_16regs<SHAPE::_16x128b, NUM::x8>(tmp, row, col); }
__device__ inline void tcgen05_ld_16x128bx16(float *tmp, int row, int col) { tcgen05_ld_32regs<SHAPE::_16x128b, NUM::x16>(tmp, row, col); }
__device__ inline void tcgen05_ld_16x128bx32(float *tmp, int row, int col) { tcgen05_ld_64regs<SHAPE::_16x128b, NUM::x32>(tmp, row, col); }

__device__ inline void tcgen05_ld_16x256bx4(float *tmp, int row, int col) { tcgen05_ld_16regs<SHAPE::_16x256b, NUM::x4>(tmp, row, col); }
__device__ inline void tcgen05_ld_16x256bx8(float *tmp, int row, int col) { tcgen05_ld_32regs<SHAPE::_16x256b, NUM::x8>(tmp, row, col); }
__device__ inline void tcgen05_ld_16x256bx16(float *tmp, int row, int col) { tcgen05_ld_64regs<SHAPE::_16x256b, NUM::x16>(tmp, row, col); }

template <
  int K,
  int BLOCK_K,
  int SPLIT_K,
  uint64_t CACHE_POLICY_A,
  uint64_t CACHE_POLICY_B,
  bool C_N_MAJOR,
  int NUM_STAGES
>
__global__
__launch_bounds__(TB_SIZE)
void kernel(
  const __grid_constant__ CUtensorMap A_tmap,
  const __grid_constant__ CUtensorMap B_tmap,
  const char *SFA_ptr,
  const char *SFB_ptr,
  float *C_ptr,
  int M, int N
) {
  const int tid = threadIdx.x;
  const int bid_k = blockIdx.x;
  const int bid = blockIdx.y;

  const int lane_id = tid % WARP_SIZE;
  const int warp_id = tid / WARP_SIZE;

  const int grid_m = M / BLOCK_M;
  const int grid_n = N / BLOCK_N;
  const int bid_m = bid / grid_n;
  const int bid_n = bid % grid_n;

  const int off_m = bid_m * BLOCK_M;
  const int off_n = bid_n * BLOCK_N;

  // set up smem
  extern __shared__ __align__(1024) char smem_ptr[];
  const int smem = static_cast<int>(__cvta_generic_to_shared(smem_ptr));
  constexpr int A_size = BLOCK_M * BLOCK_K / 2;
  constexpr int B_size = BLOCK_N * BLOCK_K / 2;
  constexpr int SFA_size = BLOCK_M * BLOCK_K / 16;
  constexpr int SFB_size = BLOCK_N * BLOCK_K / 16;
  constexpr int STAGE_SIZE = A_size + B_size + SFA_size + SFB_size;

  // set up mbarriers and tmem
  // we have NUM_STAGES mbars for TMA
  //         NUM_STAGES mbars for MMA
  //                  1 mbar  for mainloop
  #pragma nv_diag_suppress static_var_with_dynamic_init
  __shared__ int64_t mbars[NUM_STAGES * 2 + 1];
  const int tma_mbar_addr = static_cast<int>(__cvta_generic_to_shared(mbars));
  const int mma_mbar_addr = tma_mbar_addr + NUM_STAGES * 8;
  const int mainloop_mbar_addr = mma_mbar_addr + NUM_STAGES * 8;

  // https://docs.nvidia.com/cuda/parallel-thread-execution/#tcgen05-mma-scale-factor-a-layout-4x
  // each MMA consumes (128, 64) of A and (128, 64) of B (we only handle BLOCK_N=128 for now)
  // this requires (128, 4) of SFA and (128, 4) of SFB
  // which are reshaped as (32, 4', 4) of SFA and (32, 4', 4) of SFB
  // -> each MMA instruction requires 4 tmem columns of SFA and SFB each.
  constexpr int SFA_tmem = BLOCK_N;
  constexpr int SFB_tmem = SFA_tmem + 4 * (BLOCK_K / MMA_K);

  if (warp_id == 0 && elect_sync()) {
    // only 1 thread issue
    for (int i = 0; i < NUM_STAGES * 2 + 1; i++)
      mbarrier_init(tma_mbar_addr + i * 8, 1);
    asm volatile("fence.mbarrier_init.release.cluster;");  // visible to async proxy
  }
  else if (warp_id == 1) {
    // allocate tmem
    // tmem address should be 0, don't bother storing and reading it.
    asm volatile("tcgen05.alloc.cta_group::1.sync.aligned.shared::cta.b32 [%0], %1;" :: "r"(smem), "r"(BLOCK_N * 2));
  }
  __syncthreads();  // visible to all threads

  const int num_iters = K / BLOCK_K / SPLIT_K;

  // warp-specialization
  if (warp_id == 0 && elect_sync()) {
    // TMA warp
    int mma_phase = 1;  // init with 1, since it is initially available.

    for (int iter_k = 0; iter_k < num_iters; iter_k++) {
      const int stage_id = iter_k % NUM_STAGES;

      // wait MMA
      mbarrier_wait(mma_mbar_addr + stage_id * 8, mma_phase);

      // we have gone through all stages. flip the phase
      if (stage_id == NUM_STAGES - 1)
        mma_phase ^= 1;

      const int mbar_addr = tma_mbar_addr + stage_id * 8;
      const int A_smem = smem + stage_id * STAGE_SIZE;
      const int B_smem = A_smem + A_size;
      const int SFA_smem = B_smem + B_size;
      const int SFB_smem = SFA_smem + SFA_size;

      // issue TMA
      const int off_k = (iter_k * SPLIT_K + bid_k) * BLOCK_K;
      tma_3d_gmem2smem<CACHE_POLICY_A>(A_smem, &A_tmap, 0, off_m, off_k / 256, mbar_addr);
      tma_3d_gmem2smem<CACHE_POLICY_B>(B_smem, &B_tmap, 0, off_n, off_k / 256, mbar_addr);

      // layout of SFA is [M/128, rest_k, 32, 4, 4]
      //           SFB is [N/128, rest_k, 32, 4, 4]
      const int rest_k = K / 16 / 4;
      const char *SFA_src = SFA_ptr + (bid_m * rest_k + off_k / (16 * 4)) * 512;  // 512 = 32x4x4
      const char *SFB_src = SFB_ptr + (bid_n * rest_k + off_k / (16 * 4)) * 512;
      tma_gmem2smem<CACHE_POLICY_A>(SFA_smem, SFA_src, SFA_size, mbar_addr);
      tma_gmem2smem<CACHE_POLICY_B>(SFB_smem, SFB_src, SFB_size, mbar_addr);

      // signal TMA done
      asm volatile("mbarrier.arrive.expect_tx.release.cta.shared::cta.b64 _, [%0], %1;"
                  :: "r"(mbar_addr), "r"(STAGE_SIZE) : "memory");
    }
  }
  else if (warp_id == 1 && elect_sync()) {
    // MMA warp
    int tma_phase = 0;

    // https://docs.nvidia.com/cuda/parallel-thread-execution/#tcgen05-instruction-descriptor
    constexpr uint32_t i_desc = (1U << 7U)   // atype=E2M1
                              | (1U << 10U)  // btype=E2M1
                              | ((uint32_t)BLOCK_N >> 3U << 17U)  // MMA_N
                              | ((uint32_t)BLOCK_M >> 7U << 27U)  // MMA_M
                              ;

    for (int iter_k = 0; iter_k < num_iters; iter_k++) {
      const int stage_id = iter_k % NUM_STAGES;

      // wait TMA
      mbarrier_wait(tma_mbar_addr + stage_id * 8, tma_phase);

      // we have gone through all stages. flip the phase.
      if (stage_id == NUM_STAGES - 1)
        tma_phase ^= 1;

      const int A_smem = smem + stage_id * STAGE_SIZE;
      const int B_smem = A_smem + A_size;
      const int SFA_smem = B_smem + B_size;
      const int SFB_smem = SFA_smem + SFA_size;

      // set up shared memory descriptors for A and B
      // https://docs.nvidia.com/cuda/parallel-thread-execution/#tcgen05-shared-memory-descriptor
      // 128-byte swizzling. LBO is implied to be 1.
      auto make_desc_AB = [](int addr) -> uint64_t {
        const int SBO = 8 * 128;
        return desc_encode(addr) | (desc_encode(SBO) << 32ULL) | (1ULL << 46ULL) | (2ULL << 61ULL);
      };
      // no swizzling
      auto make_desc_SF = [](int addr) -> uint64_t {
        const int SBO = 8 * 16;
        return desc_encode(addr) | (desc_encode(SBO) << 32ULL) | (1ULL << 46ULL);
      };

      // tcgen05.cp -> tcgen05.mma should be pipelined correctly per PTX doc
      // https://docs.nvidia.com/cuda/parallel-thread-execution/#tcgen05-memory-consistency-model-pipelined-instructions
      // cutlass issues all of smem->tmem BEFORE mma
      // https://github.com/NVIDIA/cutlass/blob/v4.3.2/include/cutlass/gemm/collective/sm100_blockscaled_mma_warpspecialized.hpp#L1013-L1016
      for (int k = 0; k < BLOCK_K / MMA_K; k++) {
        tcgen05_cp_nvfp4(SFA_tmem + k * 4, make_desc_SF(SFA_smem + k * 512));  // 4 columns, 512 bytes of 128x4 / 32x4x4
        tcgen05_cp_nvfp4(SFB_tmem + k * 4, make_desc_SF(SFB_smem + k * 512));
      }

      // k1 selects the (BLOCK_M, 256) tile.
      // k2 selects the (BLOCK_M, 64) tile, whose rows are swizzled.
      for (int k1 = 0; k1 < BLOCK_K / 256; k1++)
        for (int k2 = 0; k2 < 256 / MMA_K; k2++) {
          uint64_t a_desc = make_desc_AB(A_smem + k1 * BLOCK_M * 128 + k2 * 32);
          uint64_t b_desc = make_desc_AB(B_smem + k1 * BLOCK_N * 128 + k2 * 32);

          int k_sf = k1 * 4 + k2;  // 4 is 256 / MMA_K
          int enable_input_d = (k1 == 0 && k2 == 0) ? iter_k : 1;
          tcgen05_mma_nvfp4(a_desc, b_desc, i_desc, SFA_tmem + k_sf * 4, SFB_tmem + k_sf * 4, enable_input_d);
        }

      // signal MMA done
      asm volatile("tcgen05.commit.cta_group::1.mbarrier::arrive::one.shared::cluster.b64 [%0];"
                  :: "r"(mma_mbar_addr + stage_id * 8) : "memory");
    }

    // signal mainloop done
    asm volatile("tcgen05.commit.cta_group::1.mbarrier::arrive::one.shared::cluster.b64 [%0];"
                :: "r"(mainloop_mbar_addr) : "memory");
  }
  __syncwarp();

  // wait mainloop
  mbarrier_wait(mainloop_mbar_addr, 0);
  asm volatile("tcgen05.fence::after_thread_sync;");

  auto epilogue_M_major = [&]() {
    // C is M-major
    constexpr int WIDTH = std::min(BLOCK_N, 64);  // using 128 might be slower

    // 32x32bx64 loads 32x64 tile for each warp
    for (int n = 0; n < BLOCK_N / WIDTH; n++) {
      float tmp[WIDTH];
      if constexpr (WIDTH == 128) tcgen05_ld_32x32bx128(tmp, warp_id * 32, n * WIDTH);
      if constexpr (WIDTH == 64) tcgen05_ld_32x32bx64(tmp, warp_id * 32, n * WIDTH);
      if constexpr (WIDTH == 32) tcgen05_ld_32x32bx32(tmp, warp_id * 32, n * WIDTH);
      asm volatile("tcgen05.wait::ld.sync.aligned;");

      for (int i = 0; i < WIDTH; i++) {
        float *out_ptr = C_ptr + (off_n + n * WIDTH + i) * M + (off_m + tid);
        if constexpr (SPLIT_K == 1)
          out_ptr[0] = tmp[i];
        else
          atomicAdd(out_ptr, tmp[i]);
      }
    }
  };
  auto epilogue_N_major = [&]() {
    // C is N-major
    // 16x256bx16 loads 16x128 tile for each warp
    for (int m = 0; m < 32 / 16; m++) {
      float tmp[64];
      tcgen05_ld_16x256bx16(tmp, warp_id * 32 + m * 16, 0);
      asm volatile("tcgen05.wait::ld.sync.aligned;");

      for (int i = 0; i < 16; i++) {
        const int row = off_m + warp_id * 32 + m * 16 + lane_id / 4;
        const int col = off_n + i * 8 + (lane_id % 4) * 2;
        float *out_ptr = C_ptr + row * N + col;

        if constexpr (SPLIT_K == 1) {
          reinterpret_cast<float2 *>(out_ptr + 0 * N)[0] = float2({tmp[i * 4 + 0], tmp[i * 4 + 1]});
          reinterpret_cast<float2 *>(out_ptr + 8 * N)[0] = float2({tmp[i * 4 + 2], tmp[i * 4 + 3]});
        } else {
          atomicAdd(reinterpret_cast<float2 *>(out_ptr + 0 * N), float2({tmp[i * 4 + 0], tmp[i * 4 + 1]}));
          atomicAdd(reinterpret_cast<float2 *>(out_ptr + 8 * N), float2({tmp[i * 4 + 2], tmp[i * 4 + 3]}));
        }
      }
    }
  };

  if constexpr (C_N_MAJOR)
    epilogue_N_major();
  else
    epilogue_M_major();

  __syncthreads();  // everyone is done with tmem
  if (warp_id == 0)  // deallocate tmem. tmem address should be 0.
    asm volatile("tcgen05.dealloc.cta_group::1.sync.aligned.b32 %0, %1;" :: "r"(0), "r"(BLOCK_N * 2));
}

void check_cu(CUresult err) {
  if (err == CUDA_SUCCESS) return;
  const char *error_msg_ptr;
  if (cuGetErrorString(err, &error_msg_ptr) != CUDA_SUCCESS)
    error_msg_ptr = "unable to get error string";
  TORCH_CHECK(false, "cuTensorMapEncodeTiled error: ", error_msg_ptr);
}

void check_cuda(cudaError_t err) {
  if (err == cudaSuccess) return;
  TORCH_CHECK(false, cudaGetErrorString(err));
}

void init_AB_tmap(
  CUtensorMap *tmap,
  const char *ptr,
  uint64_t global_height, uint64_t global_width,
  uint32_t shared_height, uint32_t shared_width
) {
  constexpr uint32_t rank = 3;
  uint64_t globalDim[rank]       = {256, global_height, global_width / 256};
  uint64_t globalStrides[rank-1] = {global_width / 2, 128};  // in bytes
  uint32_t boxDim[rank]          = {256, shared_height, shared_width / 256};
  uint32_t elementStrides[rank]  = {1, 1, 1};

  auto err = cuTensorMapEncodeTiled(
    tmap,
    CUtensorMapDataType::CU_TENSOR_MAP_DATA_TYPE_16U4_ALIGN8B,
    rank,
    (void *)ptr,
    globalDim,
    globalStrides,
    boxDim,
    elementStrides,
    CUtensorMapInterleave::CU_TENSOR_MAP_INTERLEAVE_NONE,
    CUtensorMapSwizzle::CU_TENSOR_MAP_SWIZZLE_128B,
    CUtensorMapL2promotion::CU_TENSOR_MAP_L2_PROMOTION_NONE,
    CUtensorMapFloatOOBfill::CU_TENSOR_MAP_FLOAT_OOB_FILL_NONE
  );
  check_cu(err);
}

template <
  int K,
  int BLOCK_K,
  int SPLIT_K,
  bool SWAP_AB,
  uint64_t CACHE_POLICY_A,
  uint64_t CACHE_POLICY_B,
  bool C_N_MAJOR,
  int NUM_STAGES
>
at::Tensor gemm_launch(
  const at::Tensor& A,
  const at::Tensor& B,
  const at::Tensor& SFA,
  const at::Tensor& SFB,
        at::Tensor& C
) {
  static_assert(BLOCK_K % 256 == 0);

  const int M = A.size(0);
  const int N = B.size(0);

  auto A_ptr   = reinterpret_cast<const char *>(A.data_ptr());
  auto B_ptr   = reinterpret_cast<const char *>(B.data_ptr());
  auto SFA_ptr = reinterpret_cast<const char *>(SFA.data_ptr());
  auto SFB_ptr = reinterpret_cast<const char *>(SFB.data_ptr());
  auto C_ptr   = reinterpret_cast<float *>(C.data_ptr());

  int new_M = M;
  int new_N = N;
  if constexpr (SWAP_AB) {
    std::swap(A_ptr, B_ptr);
    std::swap(SFA_ptr, SFB_ptr);
    std::swap(new_M, new_N);
  }

  CUtensorMap A_tmap, B_tmap;
  init_AB_tmap(&A_tmap, A_ptr, new_M, K, BLOCK_M, BLOCK_K);
  init_AB_tmap(&B_tmap, B_ptr, new_N, K, BLOCK_N, BLOCK_K);

  dim3 grid(SPLIT_K, (new_M / BLOCK_M) * (new_N / BLOCK_N));
  int smem_size = (BLOCK_M + BLOCK_N) * (BLOCK_K / 2 + BLOCK_K / 16) * NUM_STAGES;

  auto this_kernel = kernel<K, BLOCK_K, SPLIT_K, CACHE_POLICY_A, CACHE_POLICY_B, C_N_MAJOR != SWAP_AB, NUM_STAGES>;
  if (smem_size > 48'000)
    cudaFuncSetAttribute(this_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size);
  this_kernel<<<grid, TB_SIZE, smem_size>>>(A_tmap, B_tmap, SFA_ptr, SFB_ptr, C_ptr, new_M, new_N);

  return C_N_MAJOR ? C : C.view({N, M, 1}).transpose(0, 1);
}

at::Tensor gemm(
  const at::Tensor& A,
  const at::Tensor& B,
  const at::Tensor& SFA,
  const at::Tensor& SFB,
        at::Tensor& C
) {
  const int K = A.size(1) * 2;

#define LAUNCH(K_, SPLIT_K, SWAP_AB, CACHE_POLICY_A, CACHE_POLICY_B, C_N_MAJOR, NUM_STAGES) \
  else if (K == K_) C = gemm_launch<K_, 256, SPLIT_K, SWAP_AB, CACHE_POLICY_A, CACHE_POLICY_B, C_N_MAJOR, NUM_STAGES>(A, B, SFA, SFB, C);

  if (false) {}
  LAUNCH(16384, 2, true, EVICT_FIRST, EVICT_LAST, true, 6) // benchmark.0
  LAUNCH( 7168, 4, true, EVICT_FIRST, EVICT_LAST, true, 6) // benchmark.1
  LAUNCH( 2048, 2, true, EVICT_FIRST, EVICT_LAST, true, 4) // benchmark.2
  // the rest
  LAUNCH( 256, 1, true, EVICT_FIRST, EVICT_LAST, true, 4)
  LAUNCH( 512, 1, true, EVICT_FIRST, EVICT_LAST, true, 4)
  LAUNCH(1536, 1, true, EVICT_FIRST, EVICT_LAST, true, 4)
  LAUNCH(2304, 1, true, EVICT_FIRST, EVICT_LAST, true, 4)

#undef LAUNCH

  check_cuda(cudaGetLastError());
  return C;
}

TORCH_LIBRARY(my_module_v1, m) {
  m.def("gemm(Tensor A, Tensor B, Tensor SFA, Tensor SFB, Tensor(a!) C) -> Tensor");
  m.impl("gemm", &gemm);
}
"""

load_inline(
    "gemm",
    cpp_sources="",
    cuda_sources=CUDA_SRC_V1,
    verbose=True,
    is_python_module=False,
    no_implicit_headers=True,
    extra_cuda_cflags=[
        "-O3",
        "-gencode=arch=compute_100a,code=sm_100a",
        "--use_fast_math",
        "--expt-relaxed-constexpr",
        "--relocatable-device-code=false",
        "-lineinfo",
        "-Xptxas=-v",
        # "--keep",
        # "--keep-dir",
        # f"{Path(__file__).parent}/tmp",
    ],
    extra_ldflags=[
        "-lcuda",
    ],
)
gemm_v1 = torch.ops.my_module_v1.gemm

CUDA_SRC_V2 = r"""
#include <cudaTypedefs.h>
#include <cuda_fp16.h>

#include <torch/library.h>
#include <ATen/core/Tensor.h>

constexpr int WARP_SIZE = 32;
constexpr int NUM_WARPS = 4;
constexpr int TB_SIZE = NUM_WARPS * WARP_SIZE;

constexpr int MMA_K = 64;  // 32 bytes

// https://github.com/NVIDIA/cutlass/blob/v4.3.2/include/cute/arch/copy_sm90_desc.hpp#L193-L197
constexpr uint64_t EVICT_NORMAL = 0x1000000000000000;
constexpr uint64_t EVICT_FIRST = 0x12F0000000000000;
constexpr uint64_t EVICT_LAST = 0x14F0000000000000;

enum ProfilerTag {
  Setup = 0,
  IssueTMA,
  IssueMMA,
  WaitTMA,
  WaitMMA,
  WaitMainloop,
  WaitEpilogue,
  Epilogue,
};

__device__ inline
int64_t globaltimer() {
  int64_t t;
  asm volatile("mov.u64 %0, %globaltimer;" : "=l"(t) :: "memory");
  return t;
}

struct Profiler {
  int64_t *data_ptr_;
  int sm_id_;
  int cnt_;

  __device__
  void init(int num_entries, int64_t *data_ptr, int bid) {
    data_ptr_ = data_ptr + bid * (1 + num_entries * 4);
    asm volatile("mov.u32 %0, %smid;\n" : "=r"(sm_id_));
    cnt_ = 0;
  }

  __device__
  void start(ProfilerTag tag) {
    data_ptr_[1 + cnt_ * 4 + 0] = sm_id_;
    data_ptr_[1 + cnt_ * 4 + 1] = tag;
    data_ptr_[1 + cnt_ * 4 + 2] = globaltimer();
  }

  __device__
  void stop() {
    data_ptr_[1 + cnt_ * 4 + 3] = globaltimer() - data_ptr_[1 + cnt_ * 4 + 2];
    cnt_ += 1;
  }

  __device__
  void flush() {
    data_ptr_[0] = cnt_;
  }
};

__device__ inline
constexpr uint64_t desc_encode(uint64_t x) { return (x & 0x3'FFFFULL) >> 4ULL; };

// https://github.com/NVIDIA/cutlass/blob/v4.2.1/include/cute/arch/cluster_sm90.hpp#L180
__device__
uint32_t elect_sync() {
  uint32_t pred = 0;
  asm volatile(
    "{\n\t"
    ".reg .pred %%px;\n\t"
    "elect.sync _|%%px, %1;\n\t"
    "@%%px mov.s32 %0, 1;\n\t"
    "}"
    : "+r"(pred)
    : "r"(0xFFFFFFFF)
  );
  return pred;
}

__device__ inline
void mbarrier_init(int mbar_addr, int count) {
  asm volatile("mbarrier.init.shared::cta.b64 [%0], %1;" :: "r"(mbar_addr), "r"(count));
}

// https://github.com/NVIDIA/cutlass/blob/v4.2.1/include/cutlass/arch/barrier.h#L408
__device__
void mbarrier_wait(int mbar_addr, int phase) {
  uint32_t ticks = 0x989680;  // this is optional
  asm volatile(
    "{\n\t"
    ".reg .pred P1;\n\t"
    "LAB_WAIT:\n\t"
    "mbarrier.try_wait.parity.acquire.cta.shared::cta.b64 P1, [%0], %1, %2;\n\t"
    "@P1 bra.uni DONE;\n\t"
    "bra.uni LAB_WAIT;\n\t"
    "DONE:\n\t"
    "}"
    :: "r"(mbar_addr), "r"(phase), "r"(ticks)
  );
}

template <uint64_t cache_policy = 0>
__device__ inline
void tma_gmem2smem(int dst, const void *src, int size, int mbar_addr) {
  if constexpr (cache_policy == 0)
    asm volatile("cp.async.bulk.shared::cta.global.mbarrier::complete_tx::bytes.L2::cache_hint [%0], [%1], %2, [%3];"
                :: "r"(dst), "l"(src), "r"(size), "r"(mbar_addr));
  else
    asm volatile("cp.async.bulk.shared::cta.global.mbarrier::complete_tx::bytes.L2::cache_hint [%0], [%1], %2, [%3], %4;"
                :: "r"(dst), "l"(src), "r"(size), "r"(mbar_addr), "l"(cache_policy));
}

template <uint64_t cache_policy = 0>
__device__ inline
void tma_3d_gmem2smem(int dst, const void *tmap_ptr, int x, int y, int z, int mbar_addr) {
  if constexpr (cache_policy == 0)
    asm volatile("cp.async.bulk.tensor.3d.shared::cta.global.mbarrier::complete_tx::bytes.cta_group::1.L2::cache_hint "
                "[%0], [%1, {%2, %3, %4}], [%5];"
                :: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(y), "r"(z), "r"(mbar_addr)
                : "memory");
  else
    asm volatile("cp.async.bulk.tensor.3d.shared::cta.global.mbarrier::complete_tx::bytes.cta_group::1.L2::cache_hint "
                "[%0], [%1, {%2, %3, %4}], [%5], %6;"
                :: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(y), "r"(z), "r"(mbar_addr), "l"(cache_policy)
                : "memory");
}

__device__ inline
void tcgen05_cp_nvfp4(int taddr, uint64_t s_desc) {
  // .32x128b corresponds to (32, 16) 8-bit scale -> 1 MMA for nvfp4.
  // .warpx4 duplicates data across 32-lane groups.
  asm volatile("tcgen05.cp.cta_group::1.32x128b.warpx4 [%0], %1;" :: "r"(taddr), "l"(s_desc));
}

__device__ inline
void tcgen05_mma_nvfp4(
  uint64_t a_desc,
  uint64_t b_desc,
  uint32_t i_desc,
  int scale_A_tmem,
  int scale_B_tmem,
  int enable_input_d
) {
  const int d_tmem = 0;  // assume
  asm volatile(
    "{\n\t"
    ".reg .pred p;\n\t"  // predicate register enable-input-d
    "setp.ne.b32 p, %6, 0;\n\t"
    "tcgen05.mma.cta_group::1.kind::mxf4nvf4.block_scale.block16 [%0], %1, %2, %3, [%4], [%5], p;\n\t"
    "}"
    :: "r"(d_tmem), "l"(a_desc), "l"(b_desc), "r"(i_desc),
       "r"(scale_A_tmem), "r"(scale_B_tmem), "r"(enable_input_d)
  );
}

// see https://docs.nvidia.com/cuda/inline-ptx-assembly/index.html
struct SHAPE {
  static constexpr char _32x32b[]  = ".32x32b";   // 32x1 tile for each warp
  static constexpr char _16x128b[] = ".16x128b";  // 16x4 tile
  static constexpr char _16x256b[] = ".16x256b";  // 16x8 tile
};

struct NUM {
  static constexpr char x4[]  = ".x4";
  static constexpr char x8[]  = ".x8";
  static constexpr char x16[] = ".x16";
  static constexpr char x32[] = ".x32";
  static constexpr char x64[] = ".x64";
  static constexpr char x128[] = ".x128";
};

template <const char *SHAPE, const char *NUM>
__device__ inline
void tcgen05_ld_16regs(float *tmp, int row, int col) {
  asm volatile("tcgen05.ld.sync.aligned%17%18.b32 "
              "{ %0,  %1,  %2,  %3,  %4,  %5,  %6,  %7, "
              "  %8,  %9, %10, %11, %12, %13, %14, %15}, [%16];"
              : "=f"(tmp[ 0]), "=f"(tmp[ 1]), "=f"(tmp[ 2]), "=f"(tmp[ 3]), "=f"(tmp[ 4]), "=f"(tmp[ 5]), "=f"(tmp[ 6]), "=f"(tmp[ 7]),
                "=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15])
              : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
}

template <const char *SHAPE, const char *NUM>
__device__ inline
void tcgen05_ld_32regs(float *tmp, int row, int col) {
  asm volatile("tcgen05.ld.sync.aligned%33%34.b32 "
              "{ %0,  %1,  %2,  %3,  %4,  %5,  %6,  %7, "
              "  %8,  %9, %10, %11, %12, %13, %14, %15, "
              " %16, %17, %18, %19, %20, %21, %22, %23, "
              " %24, %25, %26, %27, %28, %29, %30, %31}, [%32];"
              : "=f"(tmp[ 0]), "=f"(tmp[ 1]), "=f"(tmp[ 2]), "=f"(tmp[ 3]), "=f"(tmp[ 4]), "=f"(tmp[ 5]), "=f"(tmp[ 6]), "=f"(tmp[ 7]),
                "=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15]),
                "=f"(tmp[16]), "=f"(tmp[17]), "=f"(tmp[18]), "=f"(tmp[19]), "=f"(tmp[20]), "=f"(tmp[21]), "=f"(tmp[22]), "=f"(tmp[23]),
                "=f"(tmp[24]), "=f"(tmp[25]), "=f"(tmp[26]), "=f"(tmp[27]), "=f"(tmp[28]), "=f"(tmp[29]), "=f"(tmp[30]), "=f"(tmp[31])
              : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
}

template <const char *SHAPE, const char *NUM>
__device__ inline
void tcgen05_ld_64regs(float *tmp, int row, int col) {
  asm volatile("tcgen05.ld.sync.aligned%65%66.b32 "
              "{ %0,  %1,  %2,  %3,  %4,  %5,  %6,  %7, "
              "  %8,  %9, %10, %11, %12, %13, %14, %15, "
              " %16, %17, %18, %19, %20, %21, %22, %23, "
              " %24, %25, %26, %27, %28, %29, %30, %31, "
              " %32, %33, %34, %35, %36, %37, %38, %39, "
              " %40, %41, %42, %43, %44, %45, %46, %47, "
              " %48, %49, %50, %51, %52, %53, %54, %55, "
              " %56, %57, %58, %59, %60, %61, %62, %63}, [%64];"
              : "=f"(tmp[ 0]), "=f"(tmp[ 1]), "=f"(tmp[ 2]), "=f"(tmp[ 3]), "=f"(tmp[ 4]), "=f"(tmp[ 5]), "=f"(tmp[ 6]), "=f"(tmp[ 7]),
                "=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15]),
                "=f"(tmp[16]), "=f"(tmp[17]), "=f"(tmp[18]), "=f"(tmp[19]), "=f"(tmp[20]), "=f"(tmp[21]), "=f"(tmp[22]), "=f"(tmp[23]),
                "=f"(tmp[24]), "=f"(tmp[25]), "=f"(tmp[26]), "=f"(tmp[27]), "=f"(tmp[28]), "=f"(tmp[29]), "=f"(tmp[30]), "=f"(tmp[31]),
                "=f"(tmp[32]), "=f"(tmp[33]), "=f"(tmp[34]), "=f"(tmp[35]), "=f"(tmp[36]), "=f"(tmp[37]), "=f"(tmp[38]), "=f"(tmp[39]),
                "=f"(tmp[40]), "=f"(tmp[41]), "=f"(tmp[42]), "=f"(tmp[43]), "=f"(tmp[44]), "=f"(tmp[45]), "=f"(tmp[46]), "=f"(tmp[47]),
                "=f"(tmp[48]), "=f"(tmp[49]), "=f"(tmp[50]), "=f"(tmp[51]), "=f"(tmp[52]), "=f"(tmp[53]), "=f"(tmp[54]), "=f"(tmp[55]),
                "=f"(tmp[56]), "=f"(tmp[57]), "=f"(tmp[58]), "=f"(tmp[59]), "=f"(tmp[60]), "=f"(tmp[61]), "=f"(tmp[62]), "=f"(tmp[63])
              : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
}

template <const char *SHAPE, const char *NUM>
__device__ inline
void tcgen05_ld_128regs(float *tmp, int row, int col) {
  asm volatile("tcgen05.ld.sync.aligned%129%130.b32 "
              "{ %0,  %1,  %2,  %3,  %4,  %5,  %6,  %7, "
              "  %8,  %9, %10, %11, %12, %13, %14, %15, "
              " %16, %17, %18, %19, %20, %21, %22, %23, "
              " %24, %25, %26, %27, %28, %29, %30, %31, "
              " %32, %33, %34, %35, %36, %37, %38, %39, "
              " %40, %41, %42, %43, %44, %45, %46, %47, "
              " %48, %49, %50, %51, %52, %53, %54, %55, "
              " %56, %57, %58, %59, %60, %61, %62, %63, "
              " %64, %65, %66, %67, %68, %69, %70, %71, "
              " %72, %73, %74, %75, %76, %77, %78, %79, "
              " %80, %81, %82, %83, %84, %85, %86, %87, "
              " %88, %89, %90, %91, %92, %93, %94, %95, "
              " %96, %97, %98, %99,%100,%101,%102,%103, "
              "%104,%105,%106,%107,%108,%109,%110,%111, "
              "%112,%113,%114,%115,%116,%117,%118,%119, "
              "%120,%121,%122,%123,%124,%125,%126,%127}, [%128];"
              : "=f"(tmp[ 0]), "=f"(tmp[ 1]), "=f"(tmp[ 2]), "=f"(tmp[ 3]), "=f"(tmp[ 4]), "=f"(tmp[ 5]), "=f"(tmp[ 6]), "=f"(tmp[ 7]),
                "=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15]),
                "=f"(tmp[16]), "=f"(tmp[17]), "=f"(tmp[18]), "=f"(tmp[19]), "=f"(tmp[20]), "=f"(tmp[21]), "=f"(tmp[22]), "=f"(tmp[23]),
                "=f"(tmp[24]), "=f"(tmp[25]), "=f"(tmp[26]), "=f"(tmp[27]), "=f"(tmp[28]), "=f"(tmp[29]), "=f"(tmp[30]), "=f"(tmp[31]),
                "=f"(tmp[32]), "=f"(tmp[33]), "=f"(tmp[34]), "=f"(tmp[35]), "=f"(tmp[36]), "=f"(tmp[37]), "=f"(tmp[38]), "=f"(tmp[39]),
                "=f"(tmp[40]), "=f"(tmp[41]), "=f"(tmp[42]), "=f"(tmp[43]), "=f"(tmp[44]), "=f"(tmp[45]), "=f"(tmp[46]), "=f"(tmp[47]),
                "=f"(tmp[48]), "=f"(tmp[49]), "=f"(tmp[50]), "=f"(tmp[51]), "=f"(tmp[52]), "=f"(tmp[53]), "=f"(tmp[54]), "=f"(tmp[55]),
                "=f"(tmp[56]), "=f"(tmp[57]), "=f"(tmp[58]), "=f"(tmp[59]), "=f"(tmp[60]), "=f"(tmp[61]), "=f"(tmp[62]), "=f"(tmp[63]),
                "=f"(tmp[64]), "=f"(tmp[65]), "=f"(tmp[66]), "=f"(tmp[67]), "=f"(tmp[68]), "=f"(tmp[69]), "=f"(tmp[70]), "=f"(tmp[71]),
                "=f"(tmp[72]), "=f"(tmp[73]), "=f"(tmp[74]), "=f"(tmp[75]), "=f"(tmp[76]), "=f"(tmp[77]), "=f"(tmp[78]), "=f"(tmp[79]),
                "=f"(tmp[80]), "=f"(tmp[81]), "=f"(tmp[82]), "=f"(tmp[83]), "=f"(tmp[84]), "=f"(tmp[85]), "=f"(tmp[86]), "=f"(tmp[87]),
                "=f"(tmp[88]), "=f"(tmp[89]), "=f"(tmp[90]), "=f"(tmp[91]), "=f"(tmp[92]), "=f"(tmp[93]), "=f"(tmp[94]), "=f"(tmp[95]),
                "=f"(tmp[96]), "=f"(tmp[97]), "=f"(tmp[98]), "=f"(tmp[99]), "=f"(tmp[100]),"=f"(tmp[101]),"=f"(tmp[102]),"=f"(tmp[103]),
                "=f"(tmp[104]),"=f"(tmp[105]),"=f"(tmp[106]),"=f"(tmp[107]),"=f"(tmp[108]),"=f"(tmp[109]),"=f"(tmp[110]),"=f"(tmp[111]),
                "=f"(tmp[112]),"=f"(tmp[113]),"=f"(tmp[114]),"=f"(tmp[115]),"=f"(tmp[116]),"=f"(tmp[117]),"=f"(tmp[118]),"=f"(tmp[119]),
                "=f"(tmp[120]),"=f"(tmp[121]),"=f"(tmp[122]),"=f"(tmp[123]),"=f"(tmp[124]),"=f"(tmp[125]),"=f"(tmp[126]),"=f"(tmp[127])
              : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
}

__device__ inline void tcgen05_ld_32x32bx32(float *tmp, int row, int col) { tcgen05_ld_32regs<SHAPE::_32x32b, NUM::x32>(tmp, row, col); }
__device__ inline void tcgen05_ld_32x32bx64(float *tmp, int row, int col) { tcgen05_ld_64regs<SHAPE::_32x32b, NUM::x64>(tmp, row, col); }
__device__ inline void tcgen05_ld_32x32bx128(float *tmp, int row, int col) { tcgen05_ld_128regs<SHAPE::_32x32b, NUM::x128>(tmp, row, col); }

__device__ inline void tcgen05_ld_16x128bx8(float *tmp, int row, int col) { tcgen05_ld_16regs<SHAPE::_16x128b, NUM::x8>(tmp, row, col); }
__device__ inline void tcgen05_ld_16x128bx16(float *tmp, int row, int col) { tcgen05_ld_32regs<SHAPE::_16x128b, NUM::x16>(tmp, row, col); }
__device__ inline void tcgen05_ld_16x128bx32(float *tmp, int row, int col) { tcgen05_ld_64regs<SHAPE::_16x128b, NUM::x32>(tmp, row, col); }

__device__ inline void tcgen05_ld_16x256bx4(float *tmp, int row, int col) { tcgen05_ld_16regs<SHAPE::_16x256b, NUM::x4>(tmp, row, col); }
__device__ inline void tcgen05_ld_16x256bx8(float *tmp, int row, int col) { tcgen05_ld_32regs<SHAPE::_16x256b, NUM::x8>(tmp, row, col); }
__device__ inline void tcgen05_ld_16x256bx16(float *tmp, int row, int col) { tcgen05_ld_64regs<SHAPE::_16x256b, NUM::x16>(tmp, row, col); }

template <
  int BLOCK_M,
  int BLOCK_N,
  int BLOCK_K,
  uint64_t CACHE_POLICY_A,
  uint64_t CACHE_POLICY_B,
  bool C_N_MAJOR,
  int NUM_STAGES,
  bool DO_PROFILE
>
__global__
__launch_bounds__(TB_SIZE)
void kernel(
  const __grid_constant__ CUtensorMap A_tmap,
  const __grid_constant__ CUtensorMap B_tmap,
  const char *SFA_ptr,
  const char *SFB_ptr,
  half *C_ptr,
  int M, int N, int K,
  int64_t *profiler_ptr,
  int num_entries
) {
  const int tid = threadIdx.x;
  const int bid = blockIdx.x;

  const int lane_id = tid % WARP_SIZE;
  const int warp_id = tid / WARP_SIZE;

  const int grid_m = M / BLOCK_M;
  const int grid_n = N / BLOCK_N;
  const int bid_m = bid / grid_n;
  const int bid_n = bid % grid_n;

  const int off_m = bid_m * BLOCK_M;
  const int off_n = bid_n * BLOCK_N;

  Profiler profiler;
  if constexpr (DO_PROFILE) if (elect_sync()) {
    profiler.init(num_entries, profiler_ptr, bid * NUM_WARPS + warp_id);
    profiler.start(ProfilerTag::Setup);
  }

  // set up smem
  extern __shared__ __align__(1024) char smem_ptr[];
  const int smem = static_cast<int>(__cvta_generic_to_shared(smem_ptr));
  constexpr int A_size = BLOCK_M * BLOCK_K / 2;
  constexpr int B_size = BLOCK_N * BLOCK_K / 2;
  constexpr int SFA_size = 128 * BLOCK_K / 16;  // always copy 128xBLOCK_K/16
  constexpr int SFB_size = 128 * BLOCK_K / 16;
  constexpr int STAGE_SIZE = A_size + B_size + SFA_size + SFB_size;

  // set up mbarriers and tmem
  // we have NUM_STAGES mbars for TMA
  //         NUM_STAGES mbars for MMA
  //                  1 mbar  for mainloop
  #pragma nv_diag_suppress static_var_with_dynamic_init
  __shared__ int64_t mbars[NUM_STAGES * 2 + 1];
  const int tma_mbar_addr = static_cast<int>(__cvta_generic_to_shared(mbars));
  const int mma_mbar_addr = tma_mbar_addr + NUM_STAGES * 8;
  const int mainloop_mbar_addr = mma_mbar_addr + NUM_STAGES * 8;

  // https://docs.nvidia.com/cuda/parallel-thread-execution/#tcgen05-mma-scale-factor-a-layout-4x
  // each MMA consumes:
  // - (128, 64) of A -> (128, 4) of SFA -> reshaped as (32, 4', 4) -> 4 tmem columns
  constexpr int SFA_tmem = BLOCK_N;
  constexpr int SFB_tmem = SFA_tmem + 4 * (BLOCK_K / MMA_K);

  if (warp_id == 0 && elect_sync()) {
    // only 1 thread issue
    for (int i = 0; i < NUM_STAGES * 2 + 1; i++)
      mbarrier_init(tma_mbar_addr + i * 8, 1);
    asm volatile("fence.mbarrier_init.release.cluster;");  // visible to async proxy
  }
  else if (warp_id == 1) {
    // allocate tmem
    // tmem address should be 0, don't bother storing and reading it.
    asm volatile("tcgen05.alloc.cta_group::1.sync.aligned.shared::cta.b32 [%0], %1;" :: "r"(smem), "r"(BLOCK_N * 2));
  }
  __syncthreads();  // visible to all threads
  if constexpr (DO_PROFILE) if (elect_sync()) profiler.stop();

  // TODO: make K constexpr as well
  const int num_iters = K / BLOCK_K;

  // warp-specialization
  if (warp_id == 0 && elect_sync()) {
    // TMA warp
    int mma_phase = 1;  // init with 1, since it is initially available.

    for (int iter_k = 0; iter_k < num_iters; iter_k++) {
      const int stage_id = iter_k % NUM_STAGES;

      // wait MMA
      if constexpr (DO_PROFILE) profiler.start(ProfilerTag::WaitMMA);
      mbarrier_wait(mma_mbar_addr + stage_id * 8, mma_phase);
      if constexpr (DO_PROFILE) profiler.stop();

      if constexpr (DO_PROFILE) profiler.start(ProfilerTag::IssueTMA);

      // we have gone through all stages. flip the phase
      if (stage_id == NUM_STAGES - 1)
        mma_phase ^= 1;

      const int mbar_addr = tma_mbar_addr + stage_id * 8;
      const int A_smem = smem + stage_id * STAGE_SIZE;
      const int B_smem = A_smem + A_size;
      const int SFA_smem = B_smem + B_size;
      const int SFB_smem = SFA_smem + SFA_size;

      // issue TMA
      const int off_k = iter_k * BLOCK_K;
      tma_3d_gmem2smem<CACHE_POLICY_A>(A_smem, &A_tmap, 0, off_m, off_k / 256, mbar_addr);
      tma_3d_gmem2smem<CACHE_POLICY_B>(B_smem, &B_tmap, 0, off_n, off_k / 256, mbar_addr);

      // layout of SFA is [M/128, rest_k, 32, 4, 4]
      //           SFB is [N/128, rest_k, 32, 4, 4]
      const int rest_k = K / 16 / 4;
      const char *SFA_src = SFA_ptr + ((off_m / 128) * rest_k + off_k / (16 * 4)) * 512;  // 512 = 32x4x4
      const char *SFB_src = SFB_ptr + ((off_n / 128) * rest_k + off_k / (16 * 4)) * 512;
      tma_gmem2smem<CACHE_POLICY_A>(SFA_smem, SFA_src, SFA_size, mbar_addr);
      tma_gmem2smem<CACHE_POLICY_B>(SFB_smem, SFB_src, SFB_size, mbar_addr);

      // signal TMA done
      asm volatile("mbarrier.arrive.expect_tx.release.cta.shared::cta.b64 _, [%0], %1;"
                  :: "r"(mbar_addr), "r"(STAGE_SIZE) : "memory");
      if constexpr (DO_PROFILE) profiler.stop();
    }
  }
  else if (warp_id == 1 && elect_sync()) {
    // MMA warp
    int tma_phase = 0;

    // https://docs.nvidia.com/cuda/parallel-thread-execution/#tcgen05-instruction-descriptor
    // fp4 MMA doesn't support MMA_M=64. Hence, we will use MMA_M=128 and ignore the rest.
    constexpr uint32_t i_desc = (1U << 7U)   // atype=E2M1
                              | (1U << 10U)  // btype=E2M1
                              | ((uint32_t)BLOCK_N >> 3U << 17U)  // MMA_N
                              | ((uint32_t)128 >> 7U << 27U)  // MMA_M
                              ;

    for (int iter_k = 0; iter_k < num_iters; iter_k++) {
      const int stage_id = iter_k % NUM_STAGES;

      // wait TMA
      if constexpr (DO_PROFILE) profiler.start(ProfilerTag::WaitTMA);
      mbarrier_wait(tma_mbar_addr + stage_id * 8, tma_phase);
      if constexpr (DO_PROFILE) profiler.stop();

      // we have gone through all stages. flip the phase.
      if (stage_id == NUM_STAGES - 1)
        tma_phase ^= 1;

      if constexpr (DO_PROFILE) profiler.start(ProfilerTag::IssueMMA);
      const int A_smem = smem + stage_id * STAGE_SIZE;
      const int B_smem = A_smem + A_size;
      const int SFA_smem = B_smem + B_size;
      const int SFB_smem = SFA_smem + SFA_size;

      // set up shared memory descriptors for A and B
      // https://docs.nvidia.com/cuda/parallel-thread-execution/#tcgen05-shared-memory-descriptor
      // 128-byte swizzling. LBO is implied to be 1.
      auto make_desc_AB = [](int addr) -> uint64_t {
        const int SBO = 8 * 128;
        return desc_encode(addr) | (desc_encode(SBO) << 32ULL) | (1ULL << 46ULL) | (2ULL << 61ULL);
      };
      // no swizzling
      auto make_desc_SF = [](int addr) -> uint64_t {
        const int SBO = 8 * 16;
        return desc_encode(addr) | (desc_encode(SBO) << 32ULL) | (1ULL << 46ULL);
      };

      // tcgen05.cp -> tcgen05.mma should be pipelined correctly per PTX doc
      // https://docs.nvidia.com/cuda/parallel-thread-execution/#tcgen05-memory-consistency-model-pipelined-instructions
      // cutlass issues all of smem->tmem BEFORE mma
      // https://github.com/NVIDIA/cutlass/blob/v4.3.2/include/cutlass/gemm/collective/sm100_blockscaled_mma_warpspecialized.hpp#L1013-L1016
      for (int k = 0; k < BLOCK_K / MMA_K; k++) {
        tcgen05_cp_nvfp4(SFA_tmem + k * 4, make_desc_SF(SFA_smem + k * 512));  // 4 columns, 512 bytes of 128x4 / 32x4x4
        tcgen05_cp_nvfp4(SFB_tmem + k * 4, make_desc_SF(SFB_smem + k * 512));
      }

      // k1 selects the (BLOCK_M, 256) tile.
      // k2 selects the (BLOCK_M, 64) tile, whose rows are swizzled.
      // NOTE: this doesn't work with BLOCK_N=32, since apparently tcgen05.mma requires SFB_tmem
      // to have 2-column (8-byte) alignment (looks like not documented).
      for (int k1 = 0; k1 < BLOCK_K / 256; k1++)
        for (int k2 = 0; k2 < 256 / MMA_K; k2++) {
          int k_sf = k1 * 4 + k2;  // 4 is 256 / MMA_K
          tcgen05_mma_nvfp4(
            make_desc_AB(A_smem + k1 * BLOCK_M * 128 + k2 * 32),
            make_desc_AB(B_smem + k1 * BLOCK_N * 128 + k2 * 32),
            i_desc,
            SFA_tmem + k_sf * 4 + (bid_m % (128 / BLOCK_M)) * (BLOCK_M / 32),
            SFB_tmem + k_sf * 4 + (bid_n % (128 / BLOCK_N)) * (BLOCK_N / 32),
            (k1 == 0 && k2 == 0) ? iter_k : 1
          );
        }

      // signal MMA done
      asm volatile("tcgen05.commit.cta_group::1.mbarrier::arrive::one.shared::cluster.b64 [%0];"
                  :: "r"(mma_mbar_addr + stage_id * 8) : "memory");
      if constexpr (DO_PROFILE) profiler.stop();
    }

    // signal mainloop done
    asm volatile("tcgen05.commit.cta_group::1.mbarrier::arrive::one.shared::cluster.b64 [%0];"
                :: "r"(mainloop_mbar_addr) : "memory");
  }
  __syncwarp();

  // wait mainloop
  if constexpr (DO_PROFILE) if (elect_sync()) profiler.start(ProfilerTag::WaitMainloop);
  mbarrier_wait(mainloop_mbar_addr, 0);
  asm volatile("tcgen05.fence::after_thread_sync;");

  if constexpr (DO_PROFILE) if (elect_sync()) {
    profiler.stop();
    profiler.start(ProfilerTag::Epilogue);
  }

  auto epilogue_M_major = [&]() {
    // C is M-major
    constexpr int WIDTH = std::min(BLOCK_N, 64);  // using 128 might be slower

    for (int n = 0; n < BLOCK_N / WIDTH; n++) {
      float tmp[WIDTH];  // if WIDTH=128, we are using 128 registers here
      if constexpr (WIDTH == 128) tcgen05_ld_32x32bx128(tmp, warp_id * 32, n * WIDTH);
      if constexpr (WIDTH == 64) tcgen05_ld_32x32bx64(tmp, warp_id * 32, n * WIDTH);
      if constexpr (WIDTH == 32) tcgen05_ld_32x32bx32(tmp, warp_id * 32, n * WIDTH);
      asm volatile("tcgen05.wait::ld.sync.aligned;");

      for (int i = 0; i < WIDTH; i++)
        C_ptr[(off_n + n * WIDTH + i) * M + (off_m + tid)] = __float2half(tmp[i]);
    }
  };
  auto epilogue_N_major = [&]() {
    // C is N-major
    for (int m = 0; m < 32 / 16; m++) {
      float tmp[BLOCK_N / 2];
      if constexpr (BLOCK_N == 128) tcgen05_ld_16x256bx16(tmp, warp_id * 32 + m * 16, 0);
      if constexpr (BLOCK_N == 64) tcgen05_ld_16x256bx8(tmp, warp_id * 32 + m * 16, 0);
      if constexpr (BLOCK_N == 32) tcgen05_ld_16x256bx4(tmp, warp_id * 32 + m * 16, 0);
      asm volatile("tcgen05.wait::ld.sync.aligned;");

      for (int i = 0; i < BLOCK_N / 8; i++) {
        const int row = off_m + warp_id * 32 + m * 16 + lane_id / 4;
        const int col = off_n + i * 8 + (lane_id % 4) * 2;

        reinterpret_cast<half2 *>(C_ptr + (row + 0) * N + col)[0] = __float22half2_rn({tmp[i * 4 + 0], tmp[i * 4 + 1]});
        reinterpret_cast<half2 *>(C_ptr + (row + 8) * N + col)[0] = __float22half2_rn({tmp[i * 4 + 2], tmp[i * 4 + 3]});
      }
    }
  };

  // when BLOCK_M = 128, use all 4 warps
  //      BLOCK_M = 64, only use the 1st 2 warps (maybe we can do 2-warp threadblock)
  if (BLOCK_M == 128 || warp_id < 2) {
    if constexpr (C_N_MAJOR)
      epilogue_N_major();
    else
      epilogue_M_major();
  }

  __syncthreads();  // everyone is done with tmem
  if (warp_id == 0)  // deallocate tmem. tmem address should be 0.
    asm volatile("tcgen05.dealloc.cta_group::1.sync.aligned.b32 %0, %1;" :: "r"(0), "r"(BLOCK_N * 2));

  if constexpr (DO_PROFILE) if (elect_sync()) {
    profiler.stop();
    profiler.flush();
  }
}

void check_cu(CUresult err) {
  if (err == CUDA_SUCCESS) return;
  const char *error_msg_ptr;
  if (cuGetErrorString(err, &error_msg_ptr) != CUDA_SUCCESS)
    error_msg_ptr = "unable to get error string";
  TORCH_CHECK(false, "cuTensorMapEncodeTiled error: ", error_msg_ptr);
}

void check_cuda(cudaError_t err) {
  if (err == cudaSuccess) return;
  TORCH_CHECK(false, cudaGetErrorString(err));
}

void init_AB_tmap(
  CUtensorMap *tmap,
  const char *ptr,
  uint64_t global_height, uint64_t global_width,
  uint32_t shared_height, uint32_t shared_width
) {
  constexpr uint32_t rank = 3;
  uint64_t globalDim[rank]       = {256, global_height, global_width / 256};
  uint64_t globalStrides[rank-1] = {global_width / 2, 128};  // in bytes
  uint32_t boxDim[rank]          = {256, shared_height, shared_width / 256};
  uint32_t elementStrides[rank]  = {1, 1, 1};

  auto err = cuTensorMapEncodeTiled(
    tmap,
    CUtensorMapDataType::CU_TENSOR_MAP_DATA_TYPE_16U4_ALIGN8B,
    rank,
    (void *)ptr,
    globalDim,
    globalStrides,
    boxDim,
    elementStrides,
    CUtensorMapInterleave::CU_TENSOR_MAP_INTERLEAVE_NONE,
    CUtensorMapSwizzle::CU_TENSOR_MAP_SWIZZLE_128B,
    CUtensorMapL2promotion::CU_TENSOR_MAP_L2_PROMOTION_NONE,
    CUtensorMapFloatOOBfill::CU_TENSOR_MAP_FLOAT_OOB_FILL_NONE
  );
  check_cu(err);
}

template <
  int BLOCK_M,
  int BLOCK_N,
  int BLOCK_K,
  bool SWAP_AB,
  uint64_t CACHE_POLICY_A,
  uint64_t CACHE_POLICY_B,
  bool C_N_MAJOR,
  int NUM_STAGES,
  bool DO_PROFILE
>
at::Tensor gemm_launch(
  const at::Tensor& A,
  const at::Tensor& B,
  const at::Tensor& SFA,
  const at::Tensor& SFB,
        at::Tensor& C,
  int64_t *profiler_ptr,
  int num_entries
) {
  static_assert(BLOCK_K % 256 == 0);

  const int M = A.size(0);
  const int N = B.size(0);
  const int K = A.size(1) * 2;

  auto A_ptr   = reinterpret_cast<const char *>(A.data_ptr());
  auto B_ptr   = reinterpret_cast<const char *>(B.data_ptr());
  auto SFA_ptr = reinterpret_cast<const char *>(SFA.data_ptr());
  auto SFB_ptr = reinterpret_cast<const char *>(SFB.data_ptr());
  auto C_ptr   = reinterpret_cast<half *>(C.data_ptr());

  int new_M = M;
  int new_N = N;
  if constexpr (SWAP_AB) {
    std::swap(A_ptr, B_ptr);
    std::swap(SFA_ptr, SFB_ptr);
    std::swap(new_M, new_N);
  }

  CUtensorMap A_tmap, B_tmap;
  init_AB_tmap(&A_tmap, A_ptr, new_M, K, BLOCK_M, BLOCK_K);
  init_AB_tmap(&B_tmap, B_ptr, new_N, K, BLOCK_N, BLOCK_K);

  int grid = (new_M / BLOCK_M) * (new_N / BLOCK_N);
  int AB_size = (BLOCK_M + BLOCK_N) * (BLOCK_K / 2);
  int SFAB_size = 128 * (BLOCK_K / 16) * 2;
  int smem_size = (AB_size + SFAB_size) * NUM_STAGES;

  auto this_kernel = kernel<BLOCK_M, BLOCK_N, BLOCK_K, CACHE_POLICY_A, CACHE_POLICY_B, C_N_MAJOR != SWAP_AB, NUM_STAGES, DO_PROFILE>;
  if (smem_size > 48'000)
    cudaFuncSetAttribute(this_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size);
  this_kernel<<<grid, TB_SIZE, smem_size>>>(A_tmap, B_tmap, SFA_ptr, SFB_ptr, C_ptr, new_M, new_N, K, profiler_ptr, num_entries);

  return C_N_MAJOR ? C : C.view({N, M, 1}).transpose(0, 1);
}

at::Tensor gemm(
  const at::Tensor& A,
  const at::Tensor& B,
  const at::Tensor& SFA,
  const at::Tensor& SFB,
        at::Tensor& C
) {
  C = gemm_launch<128, 64, 256, true, EVICT_FIRST, EVICT_LAST, false, 6, false>(A, B, SFA, SFB, C, nullptr, 0);

  return C;
}

at::Tensor profile(
  const at::Tensor& A,
  const at::Tensor& B,
  const at::Tensor& SFA,
  const at::Tensor& SFB,
        at::Tensor& C,
        at::Tensor& profiler,
  int64_t num_entries
) {
  auto profiler_ptr = profiler.data_ptr<int64_t>();
  C = gemm_launch<128, 64, 256, true, EVICT_FIRST, EVICT_LAST, false, 6, true>(A, B, SFA, SFB, C, profiler_ptr, num_entries);

  return C;
}

#undef LAUNCH

TORCH_LIBRARY(my_module_v2, m) {
  m.def("gemm(Tensor A, Tensor B, Tensor SFA, Tensor SFB, Tensor(a!) C) -> Tensor");
  m.impl("gemm", &gemm);

  m.def("profile(Tensor A, Tensor B, Tensor SFA, Tensor SFB, Tensor(a!) C, Tensor(b!) profiler, int num_entries) -> Tensor");
  m.impl("profile", &profile);
}
"""

load_inline(
    "gemm",
    cpp_sources="",
    cuda_sources=CUDA_SRC_V2,
    verbose=True,
    is_python_module=False,
    no_implicit_headers=True,
    extra_cuda_cflags=[
        "-O3",
        "-gencode=arch=compute_100a,code=sm_100a",
        "--use_fast_math",
        "--expt-relaxed-constexpr",
        "--relocatable-device-code=false",
        "-lineinfo",
        "-Xptxas=-v",
        # "--keep",
        # "--keep-dir",
        # f"{Path(__file__).parent}/tmp",
    ],
    extra_ldflags=["-lcuda"],
)
gemm_v2 = torch.ops.my_module_v2.gemm

start = 0
BIG_BUFFER = torch.zeros(int(1e10), dtype=torch.float, device="cuda")


def allocate(c: torch.Tensor):
    global start
    end = start + c.numel()
    buf = BIG_BUFFER[start:end].as_strided(c.shape, c.stride())
    start = end
    return buf


def custom_kernel(data: input_t) -> output_t:
    # a:   [M, K, 1],                     natural shape [1, M, K]
    # b:   [N, K, 1],                     natural shape [1, N, K] - only the 1st row is used
    # sfa: [32, 4, M/128, 4, rest_k, 1],  natural shape [1, M/128, rest_k, 32, 4, 4], where rest_k = K/16/4
    # sfb: [32, 4, N/128, 4, rest_k, 1],  natural shape [1, N/128, rest_k, 32, 4, 4]
    # c:   [M, N, 1],                     natural shape [1, M, N]
    K = data[0].shape[1] * 2
    if K == 16384 or K == 7168:
        return gemm_v1(data[0], data[1], data[4], data[5], allocate(data[6]))
    else:
        return gemm_v2(data[0], data[1], data[4], data[5], data[6])
scrolls · 1369 lines total

Source code from GPU Mode and the KernelBot dataset · June 9 Researcher Reciprocity License v1.0

Changes from previous submission

Against this author's previous submission submission 143993.

⋯ 4 unchanged lines
from task import input_t, output_t
from torch.utils.cpp_extension import load_inline
- CUDA_SRC = r"""
+ CUDA_SRC_V1 = r"""
#include <cudaTypedefs.h>
#include <cuda_fp16.h>
⋯ 8 unchanged lines
constexpr int BLOCK_N = 128;
constexpr int MMA_K = 64; // 32 bytes
+ // https://github.com/NVIDIA/cutlass/blob/v4.3.2/include/cute/arch/copy_sm90_desc.hpp#L193-L197
+ constexpr uint64_t EVICT_NORMAL = 0x1000000000000000;
+ constexpr uint64_t EVICT_FIRST = 0x12F0000000000000;
+ constexpr uint64_t EVICT_LAST = 0x14F0000000000000;
+
__device__ inline
constexpr uint64_t desc_encode(uint64_t x) { return (x & 0x3'FFFFULL) >> 4ULL; };
⋯ 35 unchanged lines
);
}
+ template <uint64_t cache_policy = 0>
__device__ inline
- void tma_gmem2smem(int dst, const void *src, int size, int mbar_addr, uint64_t cache_policy) {
- asm volatile("cp.async.bulk.shared::cta.global.mbarrier::complete_tx::bytes.L2::cache_hint [%0], [%1], %2, [%3], %4;"
- :: "r"(dst), "l"(src), "r"(size), "r"(mbar_addr), "l"(cache_policy));
+ void tma_gmem2smem(int dst, const void *src, int size, int mbar_addr) {
+ if constexpr (cache_policy == 0)
+ asm volatile("cp.async.bulk.shared::cta.global.mbarrier::complete_tx::bytes.L2::cache_hint [%0], [%1], %2, [%3];"
+ :: "r"(dst), "l"(src), "r"(size), "r"(mbar_addr));
+ else
+ asm volatile("cp.async.bulk.shared::cta.global.mbarrier::complete_tx::bytes.L2::cache_hint [%0], [%1], %2, [%3], %4;"
+ :: "r"(dst), "l"(src), "r"(size), "r"(mbar_addr), "l"(cache_policy));
}
+ template <uint64_t cache_policy = 0>
__device__ inline
- void tma_3d_gmem2smem(int dst, const void *tmap_ptr, int x, int y, int z, int mbar_addr, uint64_t cache_policy) {
- asm volatile("cp.async.bulk.tensor.3d.shared::cta.global.mbarrier::complete_tx::bytes.cta_group::1.L2::cache_hint "
- "[%0], [%1, {%2, %3, %4}], [%5], %6;"
- :: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(y), "r"(z), "r"(mbar_addr), "l"(cache_policy)
- : "memory");
+ void tma_3d_gmem2smem(int dst, const void *tmap_ptr, int x, int y, int z, int mbar_addr) {
+ if constexpr (cache_policy == 0)
+ asm volatile("cp.async.bulk.tensor.3d.shared::cta.global.mbarrier::complete_tx::bytes.cta_group::1.L2::cache_hint "
+ "[%0], [%1, {%2, %3, %4}], [%5];"
+ :: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(y), "r"(z), "r"(mbar_addr)
+ : "memory");
+ else
+ asm volatile("cp.async.bulk.tensor.3d.shared::cta.global.mbarrier::complete_tx::bytes.cta_group::1.L2::cache_hint "
+ "[%0], [%1, {%2, %3, %4}], [%5], %6;"
+ :: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(y), "r"(z), "r"(mbar_addr), "l"(cache_policy)
+ : "memory");
}
__device__ inline
⋯ 24 unchanged lines
);
}
- // 32x32b loads 32x1 tile for each warp
- // .x64 -> 32x64 tile
- __device__ inline
- void tcgen05_ld_32x32bx64(float *tmp, int row, int col) {
- asm volatile("tcgen05.ld.sync.aligned.32x32b.x64.b32 "
- "{ %0, %1, %2, %3, %4, %5, %6, %7, "
- " %8, %9, %10, %11, %12, %13, %14, %15, "
- " %16, %17, %18, %19, %20, %21, %22, %23, "
- " %24, %25, %26, %27, %28, %29, %30, %31, "
- " %32, %33, %34, %35, %36, %37, %38, %39, "
- " %40, %41, %42, %43, %44, %45, %46, %47, "
- " %48, %49, %50, %51, %52, %53, %54, %55, "
- " %56, %57, %58, %59, %60, %61, %62, %63}, [%64];"
- : "=f"(tmp[ 0]), "=f"(tmp[ 1]), "=f"(tmp[ 2]), "=f"(tmp[ 3]), "=f"(tmp[ 4]), "=f"(tmp[ 5]), "=f"(tmp[ 6]), "=f"(tmp[ 7]),
- "=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15]),
- "=f"(tmp[16]), "=f"(tmp[17]), "=f"(tmp[18]), "=f"(tmp[19]), "=f"(tmp[20]), "=f"(tmp[21]), "=f"(tmp[22]), "=f"(tmp[23]),
- "=f"(tmp[24]), "=f"(tmp[25]), "=f"(tmp[26]), "=f"(tmp[27]), "=f"(tmp[28]), "=f"(tmp[29]), "=f"(tmp[30]), "=f"(tmp[31]),
- "=f"(tmp[32]), "=f"(tmp[33]), "=f"(tmp[34]), "=f"(tmp[35]), "=f"(tmp[36]), "=f"(tmp[37]), "=f"(tmp[38]), "=f"(tmp[39]),
- "=f"(tmp[40]), "=f"(tmp[41]), "=f"(tmp[42]), "=f"(tmp[43]), "=f"(tmp[44]), "=f"(tmp[45]), "=f"(tmp[46]), "=f"(tmp[47]),
- "=f"(tmp[48]), "=f"(tmp[49]), "=f"(tmp[50]), "=f"(tmp[51]), "=f"(tmp[52]), "=f"(tmp[53]), "=f"(tmp[54]), "=f"(tmp[55]),
- "=f"(tmp[56]), "=f"(tmp[57]), "=f"(tmp[58]), "=f"(tmp[59]), "=f"(tmp[60]), "=f"(tmp[61]), "=f"(tmp[62]), "=f"(tmp[63])
- : "r"((row << 16) | col));
- }
+ // see https://docs.nvidia.com/cuda/inline-ptx-assembly/index.html
+ struct SHAPE {
+ static constexpr char _32x32b[] = ".32x32b"; // 32x1 tile for each warp
+ static constexpr char _16x128b[] = ".16x128b"; // 16x4 tile
+ static constexpr char _16x256b[] = ".16x256b"; // 16x8 tile
+ };
- // 16x256b loads 16x8 tile for each warp
- // .x4 -> 16x32 tile
- // .x8 -> 16x64 tile
- // .x16 -> 16x128 tile
+ struct NUM {
+ static constexpr char x4[] = ".x4";
+ static constexpr char x8[] = ".x8";
+ static constexpr char x16[] = ".x16";
+ static constexpr char x32[] = ".x32";
+ static constexpr char x64[] = ".x64";
+ static constexpr char x128[] = ".x128";
+ };
+
+ template <const char *SHAPE, const char *NUM>
__device__ inline
- void tcgen05_ld_16x256bx4(float *tmp, int row, int col) {
- asm volatile("tcgen05.ld.sync.aligned.16x256b.x4.b32 "
+ void tcgen05_ld_16regs(float *tmp, int row, int col) {
+ asm volatile("tcgen05.ld.sync.aligned%17%18.b32 "
"{ %0, %1, %2, %3, %4, %5, %6, %7, "
" %8, %9, %10, %11, %12, %13, %14, %15}, [%16];"
: "=f"(tmp[ 0]), "=f"(tmp[ 1]), "=f"(tmp[ 2]), "=f"(tmp[ 3]), "=f"(tmp[ 4]), "=f"(tmp[ 5]), "=f"(tmp[ 6]), "=f"(tmp[ 7]),
"=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15])
- : "r"((row << 16) | col));
+ : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
}
+ template <const char *SHAPE, const char *NUM>
__device__ inline
- void tcgen05_ld_16x256bx8(float *tmp, int row, int col) {
- asm volatile("tcgen05.ld.sync.aligned.16x256b.x8.b32 "
+ void tcgen05_ld_32regs(float *tmp, int row, int col) {
+ asm volatile("tcgen05.ld.sync.aligned%33%34.b32 "
"{ %0, %1, %2, %3, %4, %5, %6, %7, "
" %8, %9, %10, %11, %12, %13, %14, %15, "
" %16, %17, %18, %19, %20, %21, %22, %23, "
⋯ 2 unchanged lines
"=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15]),
"=f"(tmp[16]), "=f"(tmp[17]), "=f"(tmp[18]), "=f"(tmp[19]), "=f"(tmp[20]), "=f"(tmp[21]), "=f"(tmp[22]), "=f"(tmp[23]),
"=f"(tmp[24]), "=f"(tmp[25]), "=f"(tmp[26]), "=f"(tmp[27]), "=f"(tmp[28]), "=f"(tmp[29]), "=f"(tmp[30]), "=f"(tmp[31])
- : "r"((row << 16) | col));
+ : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
}
+ template <const char *SHAPE, const char *NUM>
__device__ inline
- void tcgen05_ld_16x256bx16(float *tmp, int row, int col) {
- asm volatile("tcgen05.ld.sync.aligned.16x256b.x16.b32 "
+ void tcgen05_ld_64regs(float *tmp, int row, int col) {
+ asm volatile("tcgen05.ld.sync.aligned%65%66.b32 "
"{ %0, %1, %2, %3, %4, %5, %6, %7, "
" %8, %9, %10, %11, %12, %13, %14, %15, "
" %16, %17, %18, %19, %20, %21, %22, %23, "
⋯ 10 unchanged lines
"=f"(tmp[40]), "=f"(tmp[41]), "=f"(tmp[42]), "=f"(tmp[43]), "=f"(tmp[44]), "=f"(tmp[45]), "=f"(tmp[46]), "=f"(tmp[47]),
"=f"(tmp[48]), "=f"(tmp[49]), "=f"(tmp[50]), "=f"(tmp[51]), "=f"(tmp[52]), "=f"(tmp[53]), "=f"(tmp[54]), "=f"(tmp[55]),
"=f"(tmp[56]), "=f"(tmp[57]), "=f"(tmp[58]), "=f"(tmp[59]), "=f"(tmp[60]), "=f"(tmp[61]), "=f"(tmp[62]), "=f"(tmp[63])
- : "r"((row << 16) | col));
+ : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
}
- template <int K, int BLOCK_K, int SPLIT_K, int TRANSPOSE, int NUM_STAGES>
+ template <const char *SHAPE, const char *NUM>
+ __device__ inline
+ void tcgen05_ld_128regs(float *tmp, int row, int col) {
+ asm volatile("tcgen05.ld.sync.aligned%129%130.b32 "
+ "{ %0, %1, %2, %3, %4, %5, %6, %7, "
+ " %8, %9, %10, %11, %12, %13, %14, %15, "
+ " %16, %17, %18, %19, %20, %21, %22, %23, "
+ " %24, %25, %26, %27, %28, %29, %30, %31, "
+ " %32, %33, %34, %35, %36, %37, %38, %39, "
+ " %40, %41, %42, %43, %44, %45, %46, %47, "
+ " %48, %49, %50, %51, %52, %53, %54, %55, "
+ " %56, %57, %58, %59, %60, %61, %62, %63, "
+ " %64, %65, %66, %67, %68, %69, %70, %71, "
+ " %72, %73, %74, %75, %76, %77, %78, %79, "
+ " %80, %81, %82, %83, %84, %85, %86, %87, "
+ " %88, %89, %90, %91, %92, %93, %94, %95, "
+ " %96, %97, %98, %99,%100,%101,%102,%103, "
+ "%104,%105,%106,%107,%108,%109,%110,%111, "
+ "%112,%113,%114,%115,%116,%117,%118,%119, "
+ "%120,%121,%122,%123,%124,%125,%126,%127}, [%128];"
+ : "=f"(tmp[ 0]), "=f"(tmp[ 1]), "=f"(tmp[ 2]), "=f"(tmp[ 3]), "=f"(tmp[ 4]), "=f"(tmp[ 5]), "=f"(tmp[ 6]), "=f"(tmp[ 7]),
+ "=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15]),
+ "=f"(tmp[16]), "=f"(tmp[17]), "=f"(tmp[18]), "=f"(tmp[19]), "=f"(tmp[20]), "=f"(tmp[21]), "=f"(tmp[22]), "=f"(tmp[23]),
+ "=f"(tmp[24]), "=f"(tmp[25]), "=f"(tmp[26]), "=f"(tmp[27]), "=f"(tmp[28]), "=f"(tmp[29]), "=f"(tmp[30]), "=f"(tmp[31]),
+ "=f"(tmp[32]), "=f"(tmp[33]), "=f"(tmp[34]), "=f"(tmp[35]), "=f"(tmp[36]), "=f"(tmp[37]), "=f"(tmp[38]), "=f"(tmp[39]),
+ "=f"(tmp[40]), "=f"(tmp[41]), "=f"(tmp[42]), "=f"(tmp[43]), "=f"(tmp[44]), "=f"(tmp[45]), "=f"(tmp[46]), "=f"(tmp[47]),
+ "=f"(tmp[48]), "=f"(tmp[49]), "=f"(tmp[50]), "=f"(tmp[51]), "=f"(tmp[52]), "=f"(tmp[53]), "=f"(tmp[54]), "=f"(tmp[55]),
+ "=f"(tmp[56]), "=f"(tmp[57]), "=f"(tmp[58]), "=f"(tmp[59]), "=f"(tmp[60]), "=f"(tmp[61]), "=f"(tmp[62]), "=f"(tmp[63]),
+ "=f"(tmp[64]), "=f"(tmp[65]), "=f"(tmp[66]), "=f"(tmp[67]), "=f"(tmp[68]), "=f"(tmp[69]), "=f"(tmp[70]), "=f"(tmp[71]),
+ "=f"(tmp[72]), "=f"(tmp[73]), "=f"(tmp[74]), "=f"(tmp[75]), "=f"(tmp[76]), "=f"(tmp[77]), "=f"(tmp[78]), "=f"(tmp[79]),
+ "=f"(tmp[80]), "=f"(tmp[81]), "=f"(tmp[82]), "=f"(tmp[83]), "=f"(tmp[84]), "=f"(tmp[85]), "=f"(tmp[86]), "=f"(tmp[87]),
+ "=f"(tmp[88]), "=f"(tmp[89]), "=f"(tmp[90]), "=f"(tmp[91]), "=f"(tmp[92]), "=f"(tmp[93]), "=f"(tmp[94]), "=f"(tmp[95]),
+ "=f"(tmp[96]), "=f"(tmp[97]), "=f"(tmp[98]), "=f"(tmp[99]), "=f"(tmp[100]),"=f"(tmp[101]),"=f"(tmp[102]),"=f"(tmp[103]),
+ "=f"(tmp[104]),"=f"(tmp[105]),"=f"(tmp[106]),"=f"(tmp[107]),"=f"(tmp[108]),"=f"(tmp[109]),"=f"(tmp[110]),"=f"(tmp[111]),
+ "=f"(tmp[112]),"=f"(tmp[113]),"=f"(tmp[114]),"=f"(tmp[115]),"=f"(tmp[116]),"=f"(tmp[117]),"=f"(tmp[118]),"=f"(tmp[119]),
+ "=f"(tmp[120]),"=f"(tmp[121]),"=f"(tmp[122]),"=f"(tmp[123]),"=f"(tmp[124]),"=f"(tmp[125]),"=f"(tmp[126]),"=f"(tmp[127])
+ : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
+ }
+
+ __device__ inline void tcgen05_ld_32x32bx32(float *tmp, int row, int col) { tcgen05_ld_32regs<SHAPE::_32x32b, NUM::x32>(tmp, row, col); }
+ __device__ inline void tcgen05_ld_32x32bx64(float *tmp, int row, int col) { tcgen05_ld_64regs<SHAPE::_32x32b, NUM::x64>(tmp, row, col); }
+ __device__ inline void tcgen05_ld_32x32bx128(float *tmp, int row, int col) { tcgen05_ld_128regs<SHAPE::_32x32b, NUM::x128>(tmp, row, col); }
+
+ __device__ inline void tcgen05_ld_16x128bx8(float *tmp, int row, int col) { tcgen05_ld_16regs<SHAPE::_16x128b, NUM::x8>(tmp, row, col); }
+ __device__ inline void tcgen05_ld_16x128bx16(float *tmp, int row, int col) { tcgen05_ld_32regs<SHAPE::_16x128b, NUM::x16>(tmp, row, col); }
+ __device__ inline void tcgen05_ld_16x128bx32(float *tmp, int row, int col) { tcgen05_ld_64regs<SHAPE::_16x128b, NUM::x32>(tmp, row, col); }
+
+ __device__ inline void tcgen05_ld_16x256bx4(float *tmp, int row, int col) { tcgen05_ld_16regs<SHAPE::_16x256b, NUM::x4>(tmp, row, col); }
+ __device__ inline void tcgen05_ld_16x256bx8(float *tmp, int row, int col) { tcgen05_ld_32regs<SHAPE::_16x256b, NUM::x8>(tmp, row, col); }
+ __device__ inline void tcgen05_ld_16x256bx16(float *tmp, int row, int col) { tcgen05_ld_64regs<SHAPE::_16x256b, NUM::x16>(tmp, row, col); }
+
+ template <
+ int K,
+ int BLOCK_K,
+ int SPLIT_K,
+ uint64_t CACHE_POLICY_A,
+ uint64_t CACHE_POLICY_B,
+ bool C_N_MAJOR,
+ int NUM_STAGES
+ >
__global__
__launch_bounds__(TB_SIZE)
void kernel(
⋯ 66 unchanged lines
// TMA warp
int mma_phase = 1; // init with 1, since it is initially available.
- // https://github.com/NVIDIA/cutlass/blob/v4.3.2/include/cute/arch/copy_sm90_desc.hpp#L193-L197
- uint64_t evict_first = 0x12F0000000000000;
- uint64_t evict_last = 0x14F0000000000000;
-
- uint64_t evict_A, evict_B;
- if constexpr (TRANSPOSE) {
- evict_A = evict_first; // read A once
- evict_B = evict_last;
- } else {
- evict_A = evict_last;
- evict_B = evict_first; // read B once
- }
-
for (int iter_k = 0; iter_k < num_iters; iter_k++) {
const int stage_id = iter_k % NUM_STAGES;
⋯ 12 unchanged lines
// issue TMA
const int off_k = (iter_k * SPLIT_K + bid_k) * BLOCK_K;
- tma_3d_gmem2smem(A_smem, &A_tmap, 0, off_m, off_k / 256, mbar_addr, evict_A);
- tma_3d_gmem2smem(B_smem, &B_tmap, 0, off_n, off_k / 256, mbar_addr, evict_B);
+ tma_3d_gmem2smem<CACHE_POLICY_A>(A_smem, &A_tmap, 0, off_m, off_k / 256, mbar_addr);
+ tma_3d_gmem2smem<CACHE_POLICY_B>(B_smem, &B_tmap, 0, off_n, off_k / 256, mbar_addr);
// layout of SFA is [M/128, rest_k, 32, 4, 4]
// SFB is [N/128, rest_k, 32, 4, 4]
const int rest_k = K / 16 / 4;
const char *SFA_src = SFA_ptr + (bid_m * rest_k + off_k / (16 * 4)) * 512; // 512 = 32x4x4
const char *SFB_src = SFB_ptr + (bid_n * rest_k + off_k / (16 * 4)) * 512;
- tma_gmem2smem(SFA_smem, SFA_src, SFA_size, mbar_addr, evict_A);
- tma_gmem2smem(SFB_smem, SFB_src, SFB_size, mbar_addr, evict_B);
+ tma_gmem2smem<CACHE_POLICY_A>(SFA_smem, SFA_src, SFA_size, mbar_addr);
+ tma_gmem2smem<CACHE_POLICY_B>(SFB_smem, SFB_src, SFB_size, mbar_addr);
// signal TMA done
asm volatile("mbarrier.arrive.expect_tx.release.cta.shared::cta.b64 _, [%0], %1;"
⋯ 75 unchanged lines
mbarrier_wait(mainloop_mbar_addr, 0);
asm volatile("tcgen05.fence::after_thread_sync;");
- if constexpr (TRANSPOSE) {
+ auto epilogue_M_major = [&]() {
// C is M-major
+ constexpr int WIDTH = std::min(BLOCK_N, 64); // using 128 might be slower
+
// 32x32bx64 loads 32x64 tile for each warp
- for (int n = 0; n < BLOCK_N / 64; n++) {
- float tmp[64];
- tcgen05_ld_32x32bx64(tmp, warp_id * 32, n * 64);
+ for (int n = 0; n < BLOCK_N / WIDTH; n++) {
+ float tmp[WIDTH];
+ if constexpr (WIDTH == 128) tcgen05_ld_32x32bx128(tmp, warp_id * 32, n * WIDTH);
+ if constexpr (WIDTH == 64) tcgen05_ld_32x32bx64(tmp, warp_id * 32, n * WIDTH);
+ if constexpr (WIDTH == 32) tcgen05_ld_32x32bx32(tmp, warp_id * 32, n * WIDTH);
asm volatile("tcgen05.wait::ld.sync.aligned;");
- for (int i = 0; i < 64; i++) {
- const int row = off_n + n * 64 + i;
- const int col = off_m + tid;
- if constexpr (SPLIT_K == 1) {
- C_ptr[row * M + col] = tmp[i];
- //C_ptr[row * M + col] = __float2half(tmp[i]);
- } else {
- atomicAdd(C_ptr + row * M + col, tmp[i]);
- //atomicAdd(C_ptr + row * M + col, __float2half(tmp[i]));
- }
+ for (int i = 0; i < WIDTH; i++) {
+ float *out_ptr = C_ptr + (off_n + n * WIDTH + i) * M + (off_m + tid);
+ if constexpr (SPLIT_K == 1)
+ out_ptr[0] = tmp[i];
+ else
+ atomicAdd(out_ptr, tmp[i]);
}
}
- }
- else {
+ };
+ auto epilogue_N_major = [&]() {
+ // C is N-major
// 16x256bx16 loads 16x128 tile for each warp
for (int m = 0; m < 32 / 16; m++) {
float tmp[64];
⋯ 3 unchanged lines
for (int i = 0; i < 16; i++) {
const int row = off_m + warp_id * 32 + m * 16 + lane_id / 4;
const int col = off_n + i * 8 + (lane_id % 4) * 2;
+ float *out_ptr = C_ptr + row * N + col;
if constexpr (SPLIT_K == 1) {
- reinterpret_cast<float2 *>(C_ptr + (row + 0) * N + col)[0] = float2({tmp[i * 4 + 0], tmp[i * 4 + 1]});
- reinterpret_cast<float2 *>(C_ptr + (row + 8) * N + col)[0] = float2({tmp[i * 4 + 2], tmp[i * 4 + 3]});
- //reinterpret_cast<half2 *>(C_ptr + (row + 0) * N + col)[0] = __float22half2_rn({tmp[i * 4 + 0], tmp[i * 4 + 1]});
- //reinterpret_cast<half2 *>(C_ptr + (row + 8) * N + col)[0] = __float22half2_rn({tmp[i * 4 + 2], tmp[i * 4 + 3]});
+ reinterpret_cast<float2 *>(out_ptr + 0 * N)[0] = float2({tmp[i * 4 + 0], tmp[i * 4 + 1]});
+ reinterpret_cast<float2 *>(out_ptr + 8 * N)[0] = float2({tmp[i * 4 + 2], tmp[i * 4 + 3]});
} else {
- atomicAdd(reinterpret_cast<float2 *>(C_ptr + (row + 0) * N + col), float2({tmp[i * 4 + 0], tmp[i * 4 + 1]}));
- atomicAdd(reinterpret_cast<float2 *>(C_ptr + (row + 8) * N + col), float2({tmp[i * 4 + 2], tmp[i * 4 + 3]}));
- //atomicAdd(reinterpret_cast<half2 *>(C_ptr + (row + 0) * N + col), __float22half2_rn({tmp[i * 4 + 0], tmp[i * 4 + 1]}));
- //atomicAdd(reinterpret_cast<half2 *>(C_ptr + (row + 8) * N + col), __float22half2_rn({tmp[i * 4 + 2], tmp[i * 4 + 3]}));
+ atomicAdd(reinterpret_cast<float2 *>(out_ptr + 0 * N), float2({tmp[i * 4 + 0], tmp[i * 4 + 1]}));
+ atomicAdd(reinterpret_cast<float2 *>(out_ptr + 8 * N), float2({tmp[i * 4 + 2], tmp[i * 4 + 3]}));
}
}
}
- }
+ };
+ if constexpr (C_N_MAJOR)
+ epilogue_N_major();
+ else
+ epilogue_M_major();
+
__syncthreads(); // everyone is done with tmem
if (warp_id == 0) // deallocate tmem. tmem address should be 0.
asm volatile("tcgen05.dealloc.cta_group::1.sync.aligned.b32 %0, %1;" :: "r"(0), "r"(BLOCK_N * 2));
⋯ 41 unchanged lines
check_cu(err);
}
- template <int K, int BLOCK_K, int SPLIT_K, int TRANSPOSE, int NUM_STAGES>
- void gemm_launch(
- const char *A_ptr,
- const char *B_ptr,
- const char *SFA_ptr,
- const char *SFB_ptr,
- float *C_ptr,
- int M, int N
+ template <
+ int K,
+ int BLOCK_K,
+ int SPLIT_K,
+ bool SWAP_AB,
+ uint64_t CACHE_POLICY_A,
+ uint64_t CACHE_POLICY_B,
+ bool C_N_MAJOR,
+ int NUM_STAGES
+ >
+ at::Tensor gemm_launch(
+ const at::Tensor& A,
+ const at::Tensor& B,
+ const at::Tensor& SFA,
+ const at::Tensor& SFB,
+ at::Tensor& C
) {
- static_assert(BLOCK_K % 256 == 0); // 128 bytes
- CUtensorMap A_tmap, B_tmap;
+ static_assert(BLOCK_K % 256 == 0);
- // swap A/B and M/N if TRANSPOSE is enabled
- if constexpr (TRANSPOSE) {
+ const int M = A.size(0);
+ const int N = B.size(0);
+
+ auto A_ptr = reinterpret_cast<const char *>(A.data_ptr());
+ auto B_ptr = reinterpret_cast<const char *>(B.data_ptr());
+ auto SFA_ptr = reinterpret_cast<const char *>(SFA.data_ptr());
+ auto SFB_ptr = reinterpret_cast<const char *>(SFB.data_ptr());
+ auto C_ptr = reinterpret_cast<float *>(C.data_ptr());
+
+ int new_M = M;
+ int new_N = N;
+ if constexpr (SWAP_AB) {
std::swap(A_ptr, B_ptr);
std::swap(SFA_ptr, SFB_ptr);
- std::swap(M, N);
+ std::swap(new_M, new_N);
}
- // TODO: create tensormap once and cache it. replace address with cuTensorMapReplaceAddress()
- init_AB_tmap(&A_tmap, A_ptr, M, K, BLOCK_M, BLOCK_K);
- init_AB_tmap(&B_tmap, B_ptr, N, K, BLOCK_N, BLOCK_K);
+ CUtensorMap A_tmap, B_tmap;
+ init_AB_tmap(&A_tmap, A_ptr, new_M, K, BLOCK_M, BLOCK_K);
+ init_AB_tmap(&B_tmap, B_ptr, new_N, K, BLOCK_N, BLOCK_K);
- dim3 grid(SPLIT_K, (M / BLOCK_M) * (N / BLOCK_N));
+ dim3 grid(SPLIT_K, (new_M / BLOCK_M) * (new_N / BLOCK_N));
int smem_size = (BLOCK_M + BLOCK_N) * (BLOCK_K / 2 + BLOCK_K / 16) * NUM_STAGES;
- auto this_kernel = kernel<K, BLOCK_K, SPLIT_K, TRANSPOSE, NUM_STAGES>;
+ auto this_kernel = kernel<K, BLOCK_K, SPLIT_K, CACHE_POLICY_A, CACHE_POLICY_B, C_N_MAJOR != SWAP_AB, NUM_STAGES>;
if (smem_size > 48'000)
cudaFuncSetAttribute(this_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size);
- this_kernel<<<grid, TB_SIZE, smem_size>>>(A_tmap, B_tmap, SFA_ptr, SFB_ptr, C_ptr, M, N);
+ this_kernel<<<grid, TB_SIZE, smem_size>>>(A_tmap, B_tmap, SFA_ptr, SFB_ptr, C_ptr, new_M, new_N);
+
+ return C_N_MAJOR ? C : C.view({N, M, 1}).transpose(0, 1);
}
at::Tensor gemm(
⋯ 3 unchanged lines
const at::Tensor& SFB,
at::Tensor& C
) {
- const int M = A.size(0);
- const int N = B.size(0);
const int K = A.size(1) * 2;
- auto A_ptr = reinterpret_cast<const char *>(A.data_ptr());
- auto B_ptr = reinterpret_cast<const char *>(B.data_ptr());
- auto SFA_ptr = reinterpret_cast<const char *>(SFA.data_ptr());
- auto SFB_ptr = reinterpret_cast<const char *>(SFB.data_ptr());
- auto C_ptr = reinterpret_cast<float *>(C.data_ptr());
+ #define LAUNCH(K_, SPLIT_K, SWAP_AB, CACHE_POLICY_A, CACHE_POLICY_B, C_N_MAJOR, NUM_STAGES) \
+ else if (K == K_) C = gemm_launch<K_, 256, SPLIT_K, SWAP_AB, CACHE_POLICY_A, CACHE_POLICY_B, C_N_MAJOR, NUM_STAGES>(A, B, SFA, SFB, C);
- #define LAUNCH(K_, SPLIT_K, TRANSPOSE, NUM_STAGES) \
- else if (K == K_) gemm_launch<K_, 256, SPLIT_K, TRANSPOSE, NUM_STAGES>(A_ptr, B_ptr, SFA_ptr, SFB_ptr, C_ptr, M, N);
-
if (false) {}
- LAUNCH(16384, 2, true, 6) // benchmark.0
- LAUNCH( 7168, 4, true, 6) // benchmark.1
- LAUNCH( 2048, 2, true, 4) // benchmark.2
+ LAUNCH(16384, 2, true, EVICT_FIRST, EVICT_LAST, true, 6) // benchmark.0
+ LAUNCH( 7168, 4, true, EVICT_FIRST, EVICT_LAST, true, 6) // benchmark.1
+ LAUNCH( 2048, 2, true, EVICT_FIRST, EVICT_LAST, true, 4) // benchmark.2
// the rest
- LAUNCH(256, 1, false, 4)
- LAUNCH(512, 1, false, 4)
- LAUNCH(1536, 1, false, 4)
- LAUNCH(2304, 1, false, 4)
+ LAUNCH( 256, 1, true, EVICT_FIRST, EVICT_LAST, true, 4)
+ LAUNCH( 512, 1, true, EVICT_FIRST, EVICT_LAST, true, 4)
+ LAUNCH(1536, 1, true, EVICT_FIRST, EVICT_LAST, true, 4)
+ LAUNCH(2304, 1, true, EVICT_FIRST, EVICT_LAST, true, 4)
#undef LAUNCH
⋯ 1 unchanged lines
return C;
}
- TORCH_LIBRARY(my_module, m) {
+ TORCH_LIBRARY(my_module_v1, m) {
m.def("gemm(Tensor A, Tensor B, Tensor SFA, Tensor SFB, Tensor(a!) C) -> Tensor");
m.impl("gemm", &gemm);
}
⋯ 2 unchanged lines
load_inline(
"gemm",
cpp_sources="",
- cuda_sources=CUDA_SRC,
+ cuda_sources=CUDA_SRC_V1,
verbose=True,
is_python_module=False,
no_implicit_headers=True,
⋯ 13 unchanged lines
"-lcuda",
],
)
- gemm = torch.ops.my_module.gemm
+ gemm_v1 = torch.ops.my_module_v1.gemm
+ CUDA_SRC_V2 = r"""
+ #include <cudaTypedefs.h>
+ #include <cuda_fp16.h>
+
+ #include <torch/library.h>
+ #include <ATen/core/Tensor.h>
+
+ constexpr int WARP_SIZE = 32;
+ constexpr int NUM_WARPS = 4;
+ constexpr int TB_SIZE = NUM_WARPS * WARP_SIZE;
+
+ constexpr int MMA_K = 64; // 32 bytes
+
+ // https://github.com/NVIDIA/cutlass/blob/v4.3.2/include/cute/arch/copy_sm90_desc.hpp#L193-L197
+ constexpr uint64_t EVICT_NORMAL = 0x1000000000000000;
+ constexpr uint64_t EVICT_FIRST = 0x12F0000000000000;
+ constexpr uint64_t EVICT_LAST = 0x14F0000000000000;
+
+ enum ProfilerTag {
+ Setup = 0,
+ IssueTMA,
+ IssueMMA,
+ WaitTMA,
+ WaitMMA,
+ WaitMainloop,
+ WaitEpilogue,
+ Epilogue,
+ };
+
+ __device__ inline
+ int64_t globaltimer() {
+ int64_t t;
+ asm volatile("mov.u64 %0, %globaltimer;" : "=l"(t) :: "memory");
+ return t;
+ }
+
+ struct Profiler {
+ int64_t *data_ptr_;
+ int sm_id_;
+ int cnt_;
+
+ __device__
+ void init(int num_entries, int64_t *data_ptr, int bid) {
+ data_ptr_ = data_ptr + bid * (1 + num_entries * 4);
+ asm volatile("mov.u32 %0, %smid;\n" : "=r"(sm_id_));
+ cnt_ = 0;
+ }
+
+ __device__
+ void start(ProfilerTag tag) {
+ data_ptr_[1 + cnt_ * 4 + 0] = sm_id_;
+ data_ptr_[1 + cnt_ * 4 + 1] = tag;
+ data_ptr_[1 + cnt_ * 4 + 2] = globaltimer();
+ }
+
+ __device__
+ void stop() {
+ data_ptr_[1 + cnt_ * 4 + 3] = globaltimer() - data_ptr_[1 + cnt_ * 4 + 2];
+ cnt_ += 1;
+ }
+
+ __device__
+ void flush() {
+ data_ptr_[0] = cnt_;
+ }
+ };
+
+ __device__ inline
+ constexpr uint64_t desc_encode(uint64_t x) { return (x & 0x3'FFFFULL) >> 4ULL; };
+
+ // https://github.com/NVIDIA/cutlass/blob/v4.2.1/include/cute/arch/cluster_sm90.hpp#L180
+ __device__
+ uint32_t elect_sync() {
+ uint32_t pred = 0;
+ asm volatile(
+ "{\n\t"
+ ".reg .pred %%px;\n\t"
+ "elect.sync _|%%px, %1;\n\t"
+ "@%%px mov.s32 %0, 1;\n\t"
+ "}"
+ : "+r"(pred)
+ : "r"(0xFFFFFFFF)
+ );
+ return pred;
+ }
+
+ __device__ inline
+ void mbarrier_init(int mbar_addr, int count) {
+ asm volatile("mbarrier.init.shared::cta.b64 [%0], %1;" :: "r"(mbar_addr), "r"(count));
+ }
+
+ // https://github.com/NVIDIA/cutlass/blob/v4.2.1/include/cutlass/arch/barrier.h#L408
+ __device__
+ void mbarrier_wait(int mbar_addr, int phase) {
+ uint32_t ticks = 0x989680; // this is optional
+ asm volatile(
+ "{\n\t"
+ ".reg .pred P1;\n\t"
+ "LAB_WAIT:\n\t"
+ "mbarrier.try_wait.parity.acquire.cta.shared::cta.b64 P1, [%0], %1, %2;\n\t"
+ "@P1 bra.uni DONE;\n\t"
+ "bra.uni LAB_WAIT;\n\t"
+ "DONE:\n\t"
+ "}"
+ :: "r"(mbar_addr), "r"(phase), "r"(ticks)
+ );
+ }
+
+ template <uint64_t cache_policy = 0>
+ __device__ inline
+ void tma_gmem2smem(int dst, const void *src, int size, int mbar_addr) {
+ if constexpr (cache_policy == 0)
+ asm volatile("cp.async.bulk.shared::cta.global.mbarrier::complete_tx::bytes.L2::cache_hint [%0], [%1], %2, [%3];"
+ :: "r"(dst), "l"(src), "r"(size), "r"(mbar_addr));
+ else
+ asm volatile("cp.async.bulk.shared::cta.global.mbarrier::complete_tx::bytes.L2::cache_hint [%0], [%1], %2, [%3], %4;"
+ :: "r"(dst), "l"(src), "r"(size), "r"(mbar_addr), "l"(cache_policy));
+ }
+
+ template <uint64_t cache_policy = 0>
+ __device__ inline
+ void tma_3d_gmem2smem(int dst, const void *tmap_ptr, int x, int y, int z, int mbar_addr) {
+ if constexpr (cache_policy == 0)
+ asm volatile("cp.async.bulk.tensor.3d.shared::cta.global.mbarrier::complete_tx::bytes.cta_group::1.L2::cache_hint "
+ "[%0], [%1, {%2, %3, %4}], [%5];"
+ :: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(y), "r"(z), "r"(mbar_addr)
+ : "memory");
+ else
+ asm volatile("cp.async.bulk.tensor.3d.shared::cta.global.mbarrier::complete_tx::bytes.cta_group::1.L2::cache_hint "
+ "[%0], [%1, {%2, %3, %4}], [%5], %6;"
+ :: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(y), "r"(z), "r"(mbar_addr), "l"(cache_policy)
+ : "memory");
+ }
+
+ __device__ inline
+ void tcgen05_cp_nvfp4(int taddr, uint64_t s_desc) {
+ // .32x128b corresponds to (32, 16) 8-bit scale -> 1 MMA for nvfp4.
+ // .warpx4 duplicates data across 32-lane groups.
+ asm volatile("tcgen05.cp.cta_group::1.32x128b.warpx4 [%0], %1;" :: "r"(taddr), "l"(s_desc));
+ }
+
+ __device__ inline
+ void tcgen05_mma_nvfp4(
+ uint64_t a_desc,
+ uint64_t b_desc,
+ uint32_t i_desc,
+ int scale_A_tmem,
+ int scale_B_tmem,
+ int enable_input_d
+ ) {
+ const int d_tmem = 0; // assume
+ asm volatile(
+ "{\n\t"
+ ".reg .pred p;\n\t" // predicate register enable-input-d
+ "setp.ne.b32 p, %6, 0;\n\t"
+ "tcgen05.mma.cta_group::1.kind::mxf4nvf4.block_scale.block16 [%0], %1, %2, %3, [%4], [%5], p;\n\t"
+ "}"
+ :: "r"(d_tmem), "l"(a_desc), "l"(b_desc), "r"(i_desc),
+ "r"(scale_A_tmem), "r"(scale_B_tmem), "r"(enable_input_d)
+ );
+ }
+
+ // see https://docs.nvidia.com/cuda/inline-ptx-assembly/index.html
+ struct SHAPE {
+ static constexpr char _32x32b[] = ".32x32b"; // 32x1 tile for each warp
+ static constexpr char _16x128b[] = ".16x128b"; // 16x4 tile
+ static constexpr char _16x256b[] = ".16x256b"; // 16x8 tile
+ };
+
+ struct NUM {
+ static constexpr char x4[] = ".x4";
+ static constexpr char x8[] = ".x8";
+ static constexpr char x16[] = ".x16";
+ static constexpr char x32[] = ".x32";
+ static constexpr char x64[] = ".x64";
+ static constexpr char x128[] = ".x128";
+ };
+
+ template <const char *SHAPE, const char *NUM>
+ __device__ inline
+ void tcgen05_ld_16regs(float *tmp, int row, int col) {
+ asm volatile("tcgen05.ld.sync.aligned%17%18.b32 "
+ "{ %0, %1, %2, %3, %4, %5, %6, %7, "
+ " %8, %9, %10, %11, %12, %13, %14, %15}, [%16];"
+ : "=f"(tmp[ 0]), "=f"(tmp[ 1]), "=f"(tmp[ 2]), "=f"(tmp[ 3]), "=f"(tmp[ 4]), "=f"(tmp[ 5]), "=f"(tmp[ 6]), "=f"(tmp[ 7]),
+ "=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15])
+ : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
+ }
+
+ template <const char *SHAPE, const char *NUM>
+ __device__ inline
+ void tcgen05_ld_32regs(float *tmp, int row, int col) {
+ asm volatile("tcgen05.ld.sync.aligned%33%34.b32 "
+ "{ %0, %1, %2, %3, %4, %5, %6, %7, "
+ " %8, %9, %10, %11, %12, %13, %14, %15, "
+ " %16, %17, %18, %19, %20, %21, %22, %23, "
+ " %24, %25, %26, %27, %28, %29, %30, %31}, [%32];"
+ : "=f"(tmp[ 0]), "=f"(tmp[ 1]), "=f"(tmp[ 2]), "=f"(tmp[ 3]), "=f"(tmp[ 4]), "=f"(tmp[ 5]), "=f"(tmp[ 6]), "=f"(tmp[ 7]),
+ "=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15]),
+ "=f"(tmp[16]), "=f"(tmp[17]), "=f"(tmp[18]), "=f"(tmp[19]), "=f"(tmp[20]), "=f"(tmp[21]), "=f"(tmp[22]), "=f"(tmp[23]),
+ "=f"(tmp[24]), "=f"(tmp[25]), "=f"(tmp[26]), "=f"(tmp[27]), "=f"(tmp[28]), "=f"(tmp[29]), "=f"(tmp[30]), "=f"(tmp[31])
+ : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
+ }
+
+ template <const char *SHAPE, const char *NUM>
+ __device__ inline
+ void tcgen05_ld_64regs(float *tmp, int row, int col) {
+ asm volatile("tcgen05.ld.sync.aligned%65%66.b32 "
+ "{ %0, %1, %2, %3, %4, %5, %6, %7, "
+ " %8, %9, %10, %11, %12, %13, %14, %15, "
+ " %16, %17, %18, %19, %20, %21, %22, %23, "
+ " %24, %25, %26, %27, %28, %29, %30, %31, "
+ " %32, %33, %34, %35, %36, %37, %38, %39, "
+ " %40, %41, %42, %43, %44, %45, %46, %47, "
+ " %48, %49, %50, %51, %52, %53, %54, %55, "
+ " %56, %57, %58, %59, %60, %61, %62, %63}, [%64];"
+ : "=f"(tmp[ 0]), "=f"(tmp[ 1]), "=f"(tmp[ 2]), "=f"(tmp[ 3]), "=f"(tmp[ 4]), "=f"(tmp[ 5]), "=f"(tmp[ 6]), "=f"(tmp[ 7]),
+ "=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15]),
+ "=f"(tmp[16]), "=f"(tmp[17]), "=f"(tmp[18]), "=f"(tmp[19]), "=f"(tmp[20]), "=f"(tmp[21]), "=f"(tmp[22]), "=f"(tmp[23]),
+ "=f"(tmp[24]), "=f"(tmp[25]), "=f"(tmp[26]), "=f"(tmp[27]), "=f"(tmp[28]), "=f"(tmp[29]), "=f"(tmp[30]), "=f"(tmp[31]),
+ "=f"(tmp[32]), "=f"(tmp[33]), "=f"(tmp[34]), "=f"(tmp[35]), "=f"(tmp[36]), "=f"(tmp[37]), "=f"(tmp[38]), "=f"(tmp[39]),
+ "=f"(tmp[40]), "=f"(tmp[41]), "=f"(tmp[42]), "=f"(tmp[43]), "=f"(tmp[44]), "=f"(tmp[45]), "=f"(tmp[46]), "=f"(tmp[47]),
+ "=f"(tmp[48]), "=f"(tmp[49]), "=f"(tmp[50]), "=f"(tmp[51]), "=f"(tmp[52]), "=f"(tmp[53]), "=f"(tmp[54]), "=f"(tmp[55]),
+ "=f"(tmp[56]), "=f"(tmp[57]), "=f"(tmp[58]), "=f"(tmp[59]), "=f"(tmp[60]), "=f"(tmp[61]), "=f"(tmp[62]), "=f"(tmp[63])
+ : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
+ }
+
+ template <const char *SHAPE, const char *NUM>
+ __device__ inline
+ void tcgen05_ld_128regs(float *tmp, int row, int col) {
+ asm volatile("tcgen05.ld.sync.aligned%129%130.b32 "
+ "{ %0, %1, %2, %3, %4, %5, %6, %7, "
+ " %8, %9, %10, %11, %12, %13, %14, %15, "
+ " %16, %17, %18, %19, %20, %21, %22, %23, "
+ " %24, %25, %26, %27, %28, %29, %30, %31, "
+ " %32, %33, %34, %35, %36, %37, %38, %39, "
+ " %40, %41, %42, %43, %44, %45, %46, %47, "
+ " %48, %49, %50, %51, %52, %53, %54, %55, "
+ " %56, %57, %58, %59, %60, %61, %62, %63, "
+ " %64, %65, %66, %67, %68, %69, %70, %71, "
+ " %72, %73, %74, %75, %76, %77, %78, %79, "
+ " %80, %81, %82, %83, %84, %85, %86, %87, "
+ " %88, %89, %90, %91, %92, %93, %94, %95, "
+ " %96, %97, %98, %99,%100,%101,%102,%103, "
+ "%104,%105,%106,%107,%108,%109,%110,%111, "
+ "%112,%113,%114,%115,%116,%117,%118,%119, "
+ "%120,%121,%122,%123,%124,%125,%126,%127}, [%128];"
+ : "=f"(tmp[ 0]), "=f"(tmp[ 1]), "=f"(tmp[ 2]), "=f"(tmp[ 3]), "=f"(tmp[ 4]), "=f"(tmp[ 5]), "=f"(tmp[ 6]), "=f"(tmp[ 7]),
+ "=f"(tmp[ 8]), "=f"(tmp[ 9]), "=f"(tmp[10]), "=f"(tmp[11]), "=f"(tmp[12]), "=f"(tmp[13]), "=f"(tmp[14]), "=f"(tmp[15]),
+ "=f"(tmp[16]), "=f"(tmp[17]), "=f"(tmp[18]), "=f"(tmp[19]), "=f"(tmp[20]), "=f"(tmp[21]), "=f"(tmp[22]), "=f"(tmp[23]),
+ "=f"(tmp[24]), "=f"(tmp[25]), "=f"(tmp[26]), "=f"(tmp[27]), "=f"(tmp[28]), "=f"(tmp[29]), "=f"(tmp[30]), "=f"(tmp[31]),
+ "=f"(tmp[32]), "=f"(tmp[33]), "=f"(tmp[34]), "=f"(tmp[35]), "=f"(tmp[36]), "=f"(tmp[37]), "=f"(tmp[38]), "=f"(tmp[39]),
+ "=f"(tmp[40]), "=f"(tmp[41]), "=f"(tmp[42]), "=f"(tmp[43]), "=f"(tmp[44]), "=f"(tmp[45]), "=f"(tmp[46]), "=f"(tmp[47]),
+ "=f"(tmp[48]), "=f"(tmp[49]), "=f"(tmp[50]), "=f"(tmp[51]), "=f"(tmp[52]), "=f"(tmp[53]), "=f"(tmp[54]), "=f"(tmp[55]),
+ "=f"(tmp[56]), "=f"(tmp[57]), "=f"(tmp[58]), "=f"(tmp[59]), "=f"(tmp[60]), "=f"(tmp[61]), "=f"(tmp[62]), "=f"(tmp[63]),
+ "=f"(tmp[64]), "=f"(tmp[65]), "=f"(tmp[66]), "=f"(tmp[67]), "=f"(tmp[68]), "=f"(tmp[69]), "=f"(tmp[70]), "=f"(tmp[71]),
+ "=f"(tmp[72]), "=f"(tmp[73]), "=f"(tmp[74]), "=f"(tmp[75]), "=f"(tmp[76]), "=f"(tmp[77]), "=f"(tmp[78]), "=f"(tmp[79]),
+ "=f"(tmp[80]), "=f"(tmp[81]), "=f"(tmp[82]), "=f"(tmp[83]), "=f"(tmp[84]), "=f"(tmp[85]), "=f"(tmp[86]), "=f"(tmp[87]),
+ "=f"(tmp[88]), "=f"(tmp[89]), "=f"(tmp[90]), "=f"(tmp[91]), "=f"(tmp[92]), "=f"(tmp[93]), "=f"(tmp[94]), "=f"(tmp[95]),
+ "=f"(tmp[96]), "=f"(tmp[97]), "=f"(tmp[98]), "=f"(tmp[99]), "=f"(tmp[100]),"=f"(tmp[101]),"=f"(tmp[102]),"=f"(tmp[103]),
+ "=f"(tmp[104]),"=f"(tmp[105]),"=f"(tmp[106]),"=f"(tmp[107]),"=f"(tmp[108]),"=f"(tmp[109]),"=f"(tmp[110]),"=f"(tmp[111]),
+ "=f"(tmp[112]),"=f"(tmp[113]),"=f"(tmp[114]),"=f"(tmp[115]),"=f"(tmp[116]),"=f"(tmp[117]),"=f"(tmp[118]),"=f"(tmp[119]),
+ "=f"(tmp[120]),"=f"(tmp[121]),"=f"(tmp[122]),"=f"(tmp[123]),"=f"(tmp[124]),"=f"(tmp[125]),"=f"(tmp[126]),"=f"(tmp[127])
+ : "r"((row << 16) | col), "C"(SHAPE), "C"(NUM));
+ }
+
+ __device__ inline void tcgen05_ld_32x32bx32(float *tmp, int row, int col) { tcgen05_ld_32regs<SHAPE::_32x32b, NUM::x32>(tmp, row, col); }
+ __device__ inline void tcgen05_ld_32x32bx64(float *tmp, int row, int col) { tcgen05_ld_64regs<SHAPE::_32x32b, NUM::x64>(tmp, row, col); }
+ __device__ inline void tcgen05_ld_32x32bx128(float *tmp, int row, int col) { tcgen05_ld_128regs<SHAPE::_32x32b, NUM::x128>(tmp, row, col); }
+
+ __device__ inline void tcgen05_ld_16x128bx8(float *tmp, int row, int col) { tcgen05_ld_16regs<SHAPE::_16x128b, NUM::x8>(tmp, row, col); }
+ __device__ inline void tcgen05_ld_16x128bx16(float *tmp, int row, int col) { tcgen05_ld_32regs<SHAPE::_16x128b, NUM::x16>(tmp, row, col); }
+ __device__ inline void tcgen05_ld_16x128bx32(float *tmp, int row, int col) { tcgen05_ld_64regs<SHAPE::_16x128b, NUM::x32>(tmp, row, col); }
+
+ __device__ inline void tcgen05_ld_16x256bx4(float *tmp, int row, int col) { tcgen05_ld_16regs<SHAPE::_16x256b, NUM::x4>(tmp, row, col); }
+ __device__ inline void tcgen05_ld_16x256bx8(float *tmp, int row, int col) { tcgen05_ld_32regs<SHAPE::_16x256b, NUM::x8>(tmp, row, col); }
+ __device__ inline void tcgen05_ld_16x256bx16(float *tmp, int row, int col) { tcgen05_ld_64regs<SHAPE::_16x256b, NUM::x16>(tmp, row, col); }
+
+ template <
+ int BLOCK_M,
+ int BLOCK_N,
+ int BLOCK_K,
+ uint64_t CACHE_POLICY_A,
+ uint64_t CACHE_POLICY_B,
+ bool C_N_MAJOR,
+ int NUM_STAGES,
+ bool DO_PROFILE
+ >
+ __global__
+ __launch_bounds__(TB_SIZE)
+ void kernel(
+ const __grid_constant__ CUtensorMap A_tmap,
+ const __grid_constant__ CUtensorMap B_tmap,
+ const char *SFA_ptr,
+ const char *SFB_ptr,
+ half *C_ptr,
+ int M, int N, int K,
+ int64_t *profiler_ptr,
+ int num_entries
+ ) {
+ const int tid = threadIdx.x;
+ const int bid = blockIdx.x;
+
+ const int lane_id = tid % WARP_SIZE;
+ const int warp_id = tid / WARP_SIZE;
+
+ const int grid_m = M / BLOCK_M;
+ const int grid_n = N / BLOCK_N;
+ const int bid_m = bid / grid_n;
+ const int bid_n = bid % grid_n;
+
+ const int off_m = bid_m * BLOCK_M;
+ const int off_n = bid_n * BLOCK_N;
+
+ Profiler profiler;
+ if constexpr (DO_PROFILE) if (elect_sync()) {
+ profiler.init(num_entries, profiler_ptr, bid * NUM_WARPS + warp_id);
+ profiler.start(ProfilerTag::Setup);
+ }
+
+ // set up smem
+ extern __shared__ __align__(1024) char smem_ptr[];
+ const int smem = static_cast<int>(__cvta_generic_to_shared(smem_ptr));
+ constexpr int A_size = BLOCK_M * BLOCK_K / 2;
+ constexpr int B_size = BLOCK_N * BLOCK_K / 2;
+ constexpr int SFA_size = 128 * BLOCK_K / 16; // always copy 128xBLOCK_K/16
+ constexpr int SFB_size = 128 * BLOCK_K / 16;
+ constexpr int STAGE_SIZE = A_size + B_size + SFA_size + SFB_size;
+
+ // set up mbarriers and tmem
+ // we have NUM_STAGES mbars for TMA
+ // NUM_STAGES mbars for MMA
+ // 1 mbar for mainloop
+ #pragma nv_diag_suppress static_var_with_dynamic_init
+ __shared__ int64_t mbars[NUM_STAGES * 2 + 1];
+ const int tma_mbar_addr = static_cast<int>(__cvta_generic_to_shared(mbars));
+ const int mma_mbar_addr = tma_mbar_addr + NUM_STAGES * 8;
+ const int mainloop_mbar_addr = mma_mbar_addr + NUM_STAGES * 8;
+
+ // https://docs.nvidia.com/cuda/parallel-thread-execution/#tcgen05-mma-scale-factor-a-layout-4x
+ // each MMA consumes:
+ // - (128, 64) of A -> (128, 4) of SFA -> reshaped as (32, 4', 4) -> 4 tmem columns
+ constexpr int SFA_tmem = BLOCK_N;
+ constexpr int SFB_tmem = SFA_tmem + 4 * (BLOCK_K / MMA_K);
+
+ if (warp_id == 0 && elect_sync()) {
+ // only 1 thread issue
+ for (int i = 0; i < NUM_STAGES * 2 + 1; i++)
+ mbarrier_init(tma_mbar_addr + i * 8, 1);
+ asm volatile("fence.mbarrier_init.release.cluster;"); // visible to async proxy
+ }
+ else if (warp_id == 1) {
+ // allocate tmem
+ // tmem address should be 0, don't bother storing and reading it.
+ asm volatile("tcgen05.alloc.cta_group::1.sync.aligned.shared::cta.b32 [%0], %1;" :: "r"(smem), "r"(BLOCK_N * 2));
+ }
+ __syncthreads(); // visible to all threads
+ if constexpr (DO_PROFILE) if (elect_sync()) profiler.stop();
+
+ // TODO: make K constexpr as well
+ const int num_iters = K / BLOCK_K;
+
+ // warp-specialization
+ if (warp_id == 0 && elect_sync()) {
+ // TMA warp
+ int mma_phase = 1; // init with 1, since it is initially available.
+
+ for (int iter_k = 0; iter_k < num_iters; iter_k++) {
+ const int stage_id = iter_k % NUM_STAGES;
+
+ // wait MMA
+ if constexpr (DO_PROFILE) profiler.start(ProfilerTag::WaitMMA);
+ mbarrier_wait(mma_mbar_addr + stage_id * 8, mma_phase);
+ if constexpr (DO_PROFILE) profiler.stop();
+
+ if constexpr (DO_PROFILE) profiler.start(ProfilerTag::IssueTMA);
+
+ // we have gone through all stages. flip the phase
+ if (stage_id == NUM_STAGES - 1)
+ mma_phase ^= 1;
+
+ const int mbar_addr = tma_mbar_addr + stage_id * 8;
+ const int A_smem = smem + stage_id * STAGE_SIZE;
+ const int B_smem = A_smem + A_size;
+ const int SFA_smem = B_smem + B_size;
+ const int SFB_smem = SFA_smem + SFA_size;
+
+ // issue TMA
+ const int off_k = iter_k * BLOCK_K;
+ tma_3d_gmem2smem<CACHE_POLICY_A>(A_smem, &A_tmap, 0, off_m, off_k / 256, mbar_addr);
+ tma_3d_gmem2smem<CACHE_POLICY_B>(B_smem, &B_tmap, 0, off_n, off_k / 256, mbar_addr);
+
+ // layout of SFA is [M/128, rest_k, 32, 4, 4]
+ // SFB is [N/128, rest_k, 32, 4, 4]
+ const int rest_k = K / 16 / 4;
+ const char *SFA_src = SFA_ptr + ((off_m / 128) * rest_k + off_k / (16 * 4)) * 512; // 512 = 32x4x4
+ const char *SFB_src = SFB_ptr + ((off_n / 128) * rest_k + off_k / (16 * 4)) * 512;
+ tma_gmem2smem<CACHE_POLICY_A>(SFA_smem, SFA_src, SFA_size, mbar_addr);
+ tma_gmem2smem<CACHE_POLICY_B>(SFB_smem, SFB_src, SFB_size, mbar_addr);
+
+ // signal TMA done
+ asm volatile("mbarrier.arrive.expect_tx.release.cta.shared::cta.b64 _, [%0], %1;"
+ :: "r"(mbar_addr), "r"(STAGE_SIZE) : "memory");
+ if constexpr (DO_PROFILE) profiler.stop();
+ }
+ }
+ else if (warp_id == 1 && elect_sync()) {
+ // MMA warp
+ int tma_phase = 0;
+
+ // https://docs.nvidia.com/cuda/parallel-thread-execution/#tcgen05-instruction-descriptor
+ // fp4 MMA doesn't support MMA_M=64. Hence, we will use MMA_M=128 and ignore the rest.
+ constexpr uint32_t i_desc = (1U << 7U) // atype=E2M1
+ | (1U << 10U) // btype=E2M1
+ | ((uint32_t)BLOCK_N >> 3U << 17U) // MMA_N
+ | ((uint32_t)128 >> 7U << 27U) // MMA_M
+ ;
+
+ for (int iter_k = 0; iter_k < num_iters; iter_k++) {
+ const int stage_id = iter_k % NUM_STAGES;
+
+ // wait TMA
+ if constexpr (DO_PROFILE) profiler.start(ProfilerTag::WaitTMA);
+ mbarrier_wait(tma_mbar_addr + stage_id * 8, tma_phase);
+ if constexpr (DO_PROFILE) profiler.stop();
+
+ // we have gone through all stages. flip the phase.
+ if (stage_id == NUM_STAGES - 1)
+ tma_phase ^= 1;
+
+ if constexpr (DO_PROFILE) profiler.start(ProfilerTag::IssueMMA);
+ const int A_smem = smem + stage_id * STAGE_SIZE;
+ const int B_smem = A_smem + A_size;
+ const int SFA_smem = B_smem + B_size;
+ const int SFB_smem = SFA_smem + SFA_size;
+
+ // set up shared memory descriptors for A and B
+ // https://docs.nvidia.com/cuda/parallel-thread-execution/#tcgen05-shared-memory-descriptor
+ // 128-byte swizzling. LBO is implied to be 1.
+ auto make_desc_AB = [](int addr) -> uint64_t {
+ const int SBO = 8 * 128;
+ return desc_encode(addr) | (desc_encode(SBO) << 32ULL) | (1ULL << 46ULL) | (2ULL << 61ULL);
+ };
+ // no swizzling
+ auto make_desc_SF = [](int addr) -> uint64_t {
+ const int SBO = 8 * 16;
+ return desc_encode(addr) | (desc_encode(SBO) << 32ULL) | (1ULL << 46ULL);
+ };
+
+ // tcgen05.cp -> tcgen05.mma should be pipelined correctly per PTX doc
+ // https://docs.nvidia.com/cuda/parallel-thread-execution/#tcgen05-memory-consistency-model-pipelined-instructions
+ // cutlass issues all of smem->tmem BEFORE mma
+ // https://github.com/NVIDIA/cutlass/blob/v4.3.2/include/cutlass/gemm/collective/sm100_blockscaled_mma_warpspecialized.hpp#L1013-L1016
+ for (int k = 0; k < BLOCK_K / MMA_K; k++) {
+ tcgen05_cp_nvfp4(SFA_tmem + k * 4, make_desc_SF(SFA_smem + k * 512)); // 4 columns, 512 bytes of 128x4 / 32x4x4
+ tcgen05_cp_nvfp4(SFB_tmem + k * 4, make_desc_SF(SFB_smem + k * 512));
+ }
+
+ // k1 selects the (BLOCK_M, 256) tile.
+ // k2 selects the (BLOCK_M, 64) tile, whose rows are swizzled.
+ // NOTE: this doesn't work with BLOCK_N=32, since apparently tcgen05.mma requires SFB_tmem
+ // to have 2-column (8-byte) alignment (looks like not documented).
+ for (int k1 = 0; k1 < BLOCK_K / 256; k1++)
+ for (int k2 = 0; k2 < 256 / MMA_K; k2++) {
+ int k_sf = k1 * 4 + k2; // 4 is 256 / MMA_K
+ tcgen05_mma_nvfp4(
+ make_desc_AB(A_smem + k1 * BLOCK_M * 128 + k2 * 32),
+ make_desc_AB(B_smem + k1 * BLOCK_N * 128 + k2 * 32),
+ i_desc,
+ SFA_tmem + k_sf * 4 + (bid_m % (128 / BLOCK_M)) * (BLOCK_M / 32),
+ SFB_tmem + k_sf * 4 + (bid_n % (128 / BLOCK_N)) * (BLOCK_N / 32),
+ (k1 == 0 && k2 == 0) ? iter_k : 1
+ );
+ }
+
+ // signal MMA done
+ asm volatile("tcgen05.commit.cta_group::1.mbarrier::arrive::one.shared::cluster.b64 [%0];"
+ :: "r"(mma_mbar_addr + stage_id * 8) : "memory");
+ if constexpr (DO_PROFILE) profiler.stop();
+ }
+
+ // signal mainloop done
+ asm volatile("tcgen05.commit.cta_group::1.mbarrier::arrive::one.shared::cluster.b64 [%0];"
+ :: "r"(mainloop_mbar_addr) : "memory");
+ }
+ __syncwarp();
+
+ // wait mainloop
+ if constexpr (DO_PROFILE) if (elect_sync()) profiler.start(ProfilerTag::WaitMainloop);
+ mbarrier_wait(mainloop_mbar_addr, 0);
+ asm volatile("tcgen05.fence::after_thread_sync;");
+
+ if constexpr (DO_PROFILE) if (elect_sync()) {
+ profiler.stop();
+ profiler.start(ProfilerTag::Epilogue);
+ }
+
+ auto epilogue_M_major = [&]() {
+ // C is M-major
+ constexpr int WIDTH = std::min(BLOCK_N, 64); // using 128 might be slower
+
+ for (int n = 0; n < BLOCK_N / WIDTH; n++) {
+ float tmp[WIDTH]; // if WIDTH=128, we are using 128 registers here
+ if constexpr (WIDTH == 128) tcgen05_ld_32x32bx128(tmp, warp_id * 32, n * WIDTH);
+ if constexpr (WIDTH == 64) tcgen05_ld_32x32bx64(tmp, warp_id * 32, n * WIDTH);
+ if constexpr (WIDTH == 32) tcgen05_ld_32x32bx32(tmp, warp_id * 32, n * WIDTH);
+ asm volatile("tcgen05.wait::ld.sync.aligned;");
+
+ for (int i = 0; i < WIDTH; i++)
+ C_ptr[(off_n + n * WIDTH + i) * M + (off_m + tid)] = __float2half(tmp[i]);
+ }
+ };
+ auto epilogue_N_major = [&]() {
+ // C is N-major
+ for (int m = 0; m < 32 / 16; m++) {
+ float tmp[BLOCK_N / 2];
+ if constexpr (BLOCK_N == 128) tcgen05_ld_16x256bx16(tmp, warp_id * 32 + m * 16, 0);
+ if constexpr (BLOCK_N == 64) tcgen05_ld_16x256bx8(tmp, warp_id * 32 + m * 16, 0);
+ if constexpr (BLOCK_N == 32) tcgen05_ld_16x256bx4(tmp, warp_id * 32 + m * 16, 0);
+ asm volatile("tcgen05.wait::ld.sync.aligned;");
+
+ for (int i = 0; i < BLOCK_N / 8; i++) {
+ const int row = off_m + warp_id * 32 + m * 16 + lane_id / 4;
+ const int col = off_n + i * 8 + (lane_id % 4) * 2;
+
+ reinterpret_cast<half2 *>(C_ptr + (row + 0) * N + col)[0] = __float22half2_rn({tmp[i * 4 + 0], tmp[i * 4 + 1]});
+ reinterpret_cast<half2 *>(C_ptr + (row + 8) * N + col)[0] = __float22half2_rn({tmp[i * 4 + 2], tmp[i * 4 + 3]});
+ }
+ }
+ };
+
+ // when BLOCK_M = 128, use all 4 warps
+ // BLOCK_M = 64, only use the 1st 2 warps (maybe we can do 2-warp threadblock)
+ if (BLOCK_M == 128 || warp_id < 2) {
+ if constexpr (C_N_MAJOR)
+ epilogue_N_major();
+ else
+ epilogue_M_major();
+ }
+
+ __syncthreads(); // everyone is done with tmem
+ if (warp_id == 0) // deallocate tmem. tmem address should be 0.
+ asm volatile("tcgen05.dealloc.cta_group::1.sync.aligned.b32 %0, %1;" :: "r"(0), "r"(BLOCK_N * 2));
+
+ if constexpr (DO_PROFILE) if (elect_sync()) {
+ profiler.stop();
+ profiler.flush();
+ }
+ }
+
+ void check_cu(CUresult err) {
+ if (err == CUDA_SUCCESS) return;
+ const char *error_msg_ptr;
+ if (cuGetErrorString(err, &error_msg_ptr) != CUDA_SUCCESS)
+ error_msg_ptr = "unable to get error string";
+ TORCH_CHECK(false, "cuTensorMapEncodeTiled error: ", error_msg_ptr);
+ }
+
+ void check_cuda(cudaError_t err) {
+ if (err == cudaSuccess) return;
+ TORCH_CHECK(false, cudaGetErrorString(err));
+ }
+
+ void init_AB_tmap(
+ CUtensorMap *tmap,
+ const char *ptr,
+ uint64_t global_height, uint64_t global_width,
+ uint32_t shared_height, uint32_t shared_width
+ ) {
+ constexpr uint32_t rank = 3;
+ uint64_t globalDim[rank] = {256, global_height, global_width / 256};
+ uint64_t globalStrides[rank-1] = {global_width / 2, 128}; // in bytes
+ uint32_t boxDim[rank] = {256, shared_height, shared_width / 256};
+ uint32_t elementStrides[rank] = {1, 1, 1};
+
+ auto err = cuTensorMapEncodeTiled(
+ tmap,
+ CUtensorMapDataType::CU_TENSOR_MAP_DATA_TYPE_16U4_ALIGN8B,
+ rank,
+ (void *)ptr,
+ globalDim,
+ globalStrides,
+ boxDim,
+ elementStrides,
+ CUtensorMapInterleave::CU_TENSOR_MAP_INTERLEAVE_NONE,
+ CUtensorMapSwizzle::CU_TENSOR_MAP_SWIZZLE_128B,
+ CUtensorMapL2promotion::CU_TENSOR_MAP_L2_PROMOTION_NONE,
+ CUtensorMapFloatOOBfill::CU_TENSOR_MAP_FLOAT_OOB_FILL_NONE
+ );
+ check_cu(err);
+ }
+
+ template <
+ int BLOCK_M,
+ int BLOCK_N,
+ int BLOCK_K,
+ bool SWAP_AB,
+ uint64_t CACHE_POLICY_A,
+ uint64_t CACHE_POLICY_B,
+ bool C_N_MAJOR,
+ int NUM_STAGES,
+ bool DO_PROFILE
+ >
+ at::Tensor gemm_launch(
+ const at::Tensor& A,
+ const at::Tensor& B,
+ const at::Tensor& SFA,
+ const at::Tensor& SFB,
+ at::Tensor& C,
+ int64_t *profiler_ptr,
+ int num_entries
+ ) {
+ static_assert(BLOCK_K % 256 == 0);
+
+ const int M = A.size(0);
+ const int N = B.size(0);
+ const int K = A.size(1) * 2;
+
+ auto A_ptr = reinterpret_cast<const char *>(A.data_ptr());
+ auto B_ptr = reinterpret_cast<const char *>(B.data_ptr());
+ auto SFA_ptr = reinterpret_cast<const char *>(SFA.data_ptr());
+ auto SFB_ptr = reinterpret_cast<const char *>(SFB.data_ptr());
+ auto C_ptr = reinterpret_cast<half *>(C.data_ptr());
+
+ int new_M = M;
+ int new_N = N;
+ if constexpr (SWAP_AB) {
+ std::swap(A_ptr, B_ptr);
+ std::swap(SFA_ptr, SFB_ptr);
+ std::swap(new_M, new_N);
+ }
+
+ CUtensorMap A_tmap, B_tmap;
+ init_AB_tmap(&A_tmap, A_ptr, new_M, K, BLOCK_M, BLOCK_K);
+ init_AB_tmap(&B_tmap, B_ptr, new_N, K, BLOCK_N, BLOCK_K);
+
+ int grid = (new_M / BLOCK_M) * (new_N / BLOCK_N);
+ int AB_size = (BLOCK_M + BLOCK_N) * (BLOCK_K / 2);
+ int SFAB_size = 128 * (BLOCK_K / 16) * 2;
+ int smem_size = (AB_size + SFAB_size) * NUM_STAGES;
+
+ auto this_kernel = kernel<BLOCK_M, BLOCK_N, BLOCK_K, CACHE_POLICY_A, CACHE_POLICY_B, C_N_MAJOR != SWAP_AB, NUM_STAGES, DO_PROFILE>;
+ if (smem_size > 48'000)
+ cudaFuncSetAttribute(this_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size);
+ this_kernel<<<grid, TB_SIZE, smem_size>>>(A_tmap, B_tmap, SFA_ptr, SFB_ptr, C_ptr, new_M, new_N, K, profiler_ptr, num_entries);
+
+ return C_N_MAJOR ? C : C.view({N, M, 1}).transpose(0, 1);
+ }
+
+ at::Tensor gemm(
+ const at::Tensor& A,
+ const at::Tensor& B,
+ const at::Tensor& SFA,
+ const at::Tensor& SFB,
+ at::Tensor& C
+ ) {
+ C = gemm_launch<128, 64, 256, true, EVICT_FIRST, EVICT_LAST, false, 6, false>(A, B, SFA, SFB, C, nullptr, 0);
+
+ return C;
+ }
+
+ at::Tensor profile(
+ const at::Tensor& A,
+ const at::Tensor& B,
+ const at::Tensor& SFA,
+ const at::Tensor& SFB,
+ at::Tensor& C,
+ at::Tensor& profiler,
+ int64_t num_entries
+ ) {
+ auto profiler_ptr = profiler.data_ptr<int64_t>();
+ C = gemm_launch<128, 64, 256, true, EVICT_FIRST, EVICT_LAST, false, 6, true>(A, B, SFA, SFB, C, profiler_ptr, num_entries);
+
+ return C;
+ }
+
+ #undef LAUNCH
+
+ TORCH_LIBRARY(my_module_v2, m) {
+ m.def("gemm(Tensor A, Tensor B, Tensor SFA, Tensor SFB, Tensor(a!) C) -> Tensor");
+ m.impl("gemm", &gemm);
+
+ m.def("profile(Tensor A, Tensor B, Tensor SFA, Tensor SFB, Tensor(a!) C, Tensor(b!) profiler, int num_entries) -> Tensor");
+ m.impl("profile", &profile);
+ }
+ """
+
+ load_inline(
+ "gemm",
+ cpp_sources="",
+ cuda_sources=CUDA_SRC_V2,
+ verbose=True,
+ is_python_module=False,
+ no_implicit_headers=True,
+ extra_cuda_cflags=[
+ "-O3",
+ "-gencode=arch=compute_100a,code=sm_100a",
+ "--use_fast_math",
+ "--expt-relaxed-constexpr",
+ "--relocatable-device-code=false",
+ "-lineinfo",
+ "-Xptxas=-v",
+ # "--keep",
+ # "--keep-dir",
+ # f"{Path(__file__).parent}/tmp",
+ ],
+ extra_ldflags=["-lcuda"],
+ )
+ gemm_v2 = torch.ops.my_module_v2.gemm
+
start = 0
BIG_BUFFER = torch.zeros(int(1e10), dtype=torch.float, device="cuda")
⋯ 12 unchanged lines
# sfa: [32, 4, M/128, 4, rest_k, 1], natural shape [1, M/128, rest_k, 32, 4, 4], where rest_k = K/16/4
# sfb: [32, 4, N/128, 4, rest_k, 1], natural shape [1, N/128, rest_k, 32, 4, 4]
# c: [M, N, 1], natural shape [1, M, N]
- return gemm(data[0], data[1], data[4], data[5], allocate(data[6])) # return FP32, might not be valid...
+ K = data[0].shape[1] * 2
+ if K == 16384 or K == 7168:
+ return gemm_v1(data[0], data[1], data[4], data[5], allocate(data[6]))
+ else:
⋯ diff truncated
scrolls · 1201 diff lines total

Best evidence level for this revision: reported

JSON