submission 501386
gau.nernst · python · License unknown
Use it
Vendorable · source mirrored · license unknownView source →
No package. Vendor the mirrored source: 944 lines, June 9 Researcher Reciprocity License v1.0.
submission_v5.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-nvfp4-group-gemm-501386?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
Reported · How evidence levels are derived →
Source and license
sourceavailable
revision digestsha256:ed58aa3365745c3cd994cd0d006eefcea1921bcfcc08cf5a42aeb62606fe2303
license declaredunknown
license concludedunknown
authorsgau.nernst
imported2026-08-15
Techniques
Extracted from the mirrored source by pattern, never inferred. Each row cites its line.
cluster
__cluster_dims__(2, 1, 1)fused-epilogue
const int epilogue_mbar_addr = mainloop_mbar_addr + 2 * 8;mbarrier
void mbarrier_init(int mbar_addr, int count) {num-warps = 6
constexpr int NUM_WARPS = 6;shared-memory
extern __shared__ __align__(1024) char smem_ptr[];tcgen05
asm volatile("tcgen05.cp.cta_group::%2.32x128b.warpx4 [%0], %1;" :: "r"(taddr), "l"(s_desc), "n"(CTA_GROUP));tile-k = 256
constexpr int BLOCK_K = 256;tile-m = 128
constexpr int BLOCK_M = 128;tile-n = 128
constexpr int BLOCK_N = 128;tma
asm volatile("cp.async.bulk.prefetch.L2.global.L2::cache_hint [%0], %1, %2;"Kernel source
submission_v5.py944 lines
#!POPCORN leaderboard nvfp4_group_gemm
#!POPCORN gpu B200
import torch
from task import input_t, output_t
from torch.utils.cpp_extension import load_inline
CUDA_SRC = r"""
#include <cudaTypedefs.h>
#include <cuda_fp16.h>
#include <torch/library.h>
#include <ATen/core/Tensor.h>
constexpr int WARP_SIZE = 32;
constexpr int MMA_K = 64; // 32 bytes
__device__ __host__
constexpr int cdiv(int a, int b) { return (a + b - 1) / b; }
// 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;
}
// named barrier
template <int bar>
__device__ inline
void bar_sync(int count) {
asm volatile("bar.sync %0, %1;" :: "n"(bar), "r"(count) : "memory");
}
__device__ inline
void mbarrier_init(int mbar_addr, int count) {
asm volatile("mbarrier.init.shared::cta.b64 [%0], %1;" :: "r"(mbar_addr), "r"(count));
}
__device__ inline
void mbarrier_arrive(int mbar_addr) {
asm volatile("mbarrier.arrive.release.cta.shared::cluster.b64 _, [%0];" :: "r"(mbar_addr) : "memory");
}
// NOTE: using .shared::cluster
__device__ inline
void mbarrier_arrive_expect_tx(int mbar_addr, int size) {
asm volatile("mbarrier.arrive.expect_tx.release.cta.shared::cluster.b64 _, [%0], %1;" :: "r"(mbar_addr), "r"(size) : "memory");
}
// 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 LAB_WAIT;\n\t"
"}"
:: "r"(mbar_addr), "r"(phase), "r"(ticks)
);
}
__device__ inline
void prefetch_tensormap(const void *tmap_ptr) {
asm volatile("prefetch.tensormap [%0];" :: "l"(tmap_ptr));
}
__device__ inline
void tma_prefetch(const void *src, int size, uint64_t cache_policy) {
asm volatile("cp.async.bulk.prefetch.L2.global.L2::cache_hint [%0], %1, %2;"
:: "l"(src), "r"(size), "l"(cache_policy) : "memory");
}
__device__ inline
void tma_1d_prefetch(const void *tmap_ptr, int x, uint64_t cache_policy) {
asm volatile("cp.async.bulk.prefetch.tensor.1d.L2.global.L2::cache_hint [%0, {%1}], %2;"
:: "l"(tmap_ptr), "r"(x), "l"(cache_policy) : "memory");
}
__device__ inline
void tma_2d_prefetch(const void *tmap_ptr, int x, int y, uint64_t cache_policy) {
asm volatile("cp.async.bulk.prefetch.tensor.2d.L2.global.L2::cache_hint [%0, {%1, %2}], %3;"
:: "l"(tmap_ptr), "r"(x), "r"(y), "l"(cache_policy) : "memory");
}
__device__ inline
void tma_3d_prefetch(const void *tmap_ptr, int x, int y, int z, uint64_t cache_policy) {
asm volatile("cp.async.bulk.prefetch.tensor.3d.L2.global.L2::cache_hint [%0, {%1, %2, %3}], %4;"
:: "l"(tmap_ptr), "r"(x), "r"(y), "r"(z), "l"(cache_policy) : "memory");
}
__device__ inline
void tma_g2s(int dst, const void *src, int size, int mbar_addr, uint64_t cache_policy) {
asm volatile("cp.async.bulk.shared::cluster.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 <int CTA_GROUP = 1>
__device__ inline
void tma_1d_g2s(int dst, const void *tmap_ptr, int x, int mbar_addr, uint64_t cache_policy) {
asm volatile("cp.async.bulk.tensor.1d.shared::cluster.global.mbarrier::complete_tx::bytes.cta_group::%5.L2::cache_hint "
"[%0], [%1, {%2}], [%3], %4;"
:: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(mbar_addr), "l"(cache_policy), "n"(CTA_GROUP)
: "memory");
}
template <int CTA_GROUP = 1>
__device__ inline
void tma_1d_g2s_mcast(int dst, const void *tmap_ptr, int x, int mbar_addr, int16_t cta_mask, uint64_t cache_policy) {
asm volatile("cp.async.bulk.tensor.1d.shared::cluster.global.mbarrier::complete_tx::bytes.multicast::cluster.cta_group::%6.L2::cache_hint "
"[%0], [%1, {%2}], [%3], %4, %5;"
:: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(mbar_addr), "h"(cta_mask), "l"(cache_policy), "n"(CTA_GROUP)
: "memory");
}
template <int CTA_GROUP = 1>
__device__ inline
void tma_2d_g2s(int dst, const void *tmap_ptr, int x, int y, int mbar_addr, uint64_t cache_policy) {
asm volatile("cp.async.bulk.tensor.2d.shared::cluster.global.mbarrier::complete_tx::bytes.cta_group::%6.L2::cache_hint "
"[%0], [%1, {%2, %3}], [%4], %5;"
:: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(y), "r"(mbar_addr), "l"(cache_policy), "n"(CTA_GROUP)
: "memory");
}
template <int CTA_GROUP = 1>
__device__ inline
void tma_2d_g2s_mcast(int dst, const void *tmap_ptr, int x, int y, int mbar_addr, int16_t cta_mask, uint64_t cache_policy) {
asm volatile("cp.async.bulk.tensor.2d.shared::cluster.global.mbarrier::complete_tx::bytes.multicast::cluster.cta_group::%7.L2::cache_hint "
"[%0], [%1, {%2, %3}], [%4], %5, %6;"
:: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(y), "r"(mbar_addr), "h"(cta_mask), "l"(cache_policy), "n"(CTA_GROUP)
: "memory");
}
template <int CTA_GROUP = 1>
__device__ inline
void tma_3d_g2s(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::cluster.global.mbarrier::complete_tx::bytes.cta_group::%7.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), "n"(CTA_GROUP)
: "memory");
}
template <int CTA_GROUP = 1>
__device__ inline
void tma_3d_g2s_mcast(int dst, const void *tmap_ptr, int x, int y, int z, int mbar_addr, int16_t cta_mask, uint64_t cache_policy) {
asm volatile("cp.async.bulk.tensor.3d.shared::cluster.global.mbarrier::complete_tx::bytes.multicast::cluster.cta_group::%8.L2::cache_hint "
"[%0], [%1, {%2, %3, %4}], [%5], %6, %7;"
:: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(y), "r"(z), "r"(mbar_addr), "h"(cta_mask), "l"(cache_policy), "n"(CTA_GROUP)
: "memory");
}
template <int CTA_GROUP = 1>
__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::%2.32x128b.warpx4 [%0], %1;" :: "r"(taddr), "l"(s_desc), "n"(CTA_GROUP));
}
template <int CTA_GROUP = 1>
__device__ inline
void tcgen05_alloc(int addr, int size) {
asm volatile("tcgen05.alloc.cta_group::%2.sync.aligned.shared::cta.b32 [%0], %1;" :: "r"(addr), "r"(size), "n"(CTA_GROUP));
}
template <int CTA_GROUP = 1>
__device__ inline
void tcgen05_dealloc(int addr, int size) {
asm volatile("tcgen05.dealloc.cta_group::%2.sync.aligned.b32 %0, %1;" :: "r"(0), "r"(size), "n"(CTA_GROUP));
}
template <int CTA_GROUP = 1>
__device__ inline
void tcgen05_commit(int mbar_addr) {
asm volatile("tcgen05.commit.cta_group::%1.mbarrier::arrive::one.shared::cluster.b64 [%0];"
:: "r"(mbar_addr), "n"(CTA_GROUP) : "memory");
}
template <int CTA_GROUP = 1>
__device__ inline
void tcgen05_commit_mcast(int mbar_addr, uint16_t cta_mask) {
asm volatile("tcgen05.commit.cta_group::%2.mbarrier::arrive::one.shared::cluster.multicast::cluster.b64 [%0], %1;"
:: "r"(mbar_addr), "h"(cta_mask), "n"(CTA_GROUP) : "memory");
}
struct COLLECTOR_USAGE {
static constexpr char NONE[] = "";
static constexpr char A_FILL[] = ".collector::a::fill";
static constexpr char A_USE[] = ".collector::a::use";
static constexpr char A_LASTUSE[] = ".collector::a::lastuse";
static constexpr char A_DISCARD[] = ".collector::a::discard";
};
template <int CTA_GROUP = 1, const char *collector_usage = COLLECTOR_USAGE::NONE>
__device__ inline
void tcgen05_mma_nvfp4(
int d_tmem,
uint64_t a_desc,
uint64_t b_desc,
uint32_t i_desc,
int scale_A_tmem,
int scale_B_tmem,
int enable_input_d
) {
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::%7.kind::mxf4nvf4.block_scale.block16%8 [%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),
"n"(CTA_GROUP), "C"(collector_usage)
);
}
// 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
};
template <int NUM_REGS, const char *SHAPE, int NUM>
__device__ inline
void tcgen05_ld(float *tmp, int row, int col) {
int addr = (row << 16) | col;
if constexpr (NUM_REGS == 1)
asm volatile("tcgen05.ld.sync.aligned%2.x%3.b32 {%0}, [%1];"
: "=f"(tmp[0]) : "r"(addr), "C"(SHAPE), "n"(NUM));
if constexpr (NUM_REGS == 2)
asm volatile("tcgen05.ld.sync.aligned%3.x%4.b32 {%0, %1}, [%2];"
: "=f"(tmp[0]), "=f"(tmp[1]) : "r"(addr), "C"(SHAPE), "n"(NUM));
if constexpr (NUM_REGS == 4)
asm volatile("tcgen05.ld.sync.aligned%5.x%6.b32 "
"{%0, %1, %2, %3}, [%4];"
: "=f"(tmp[0]), "=f"(tmp[1]), "=f"(tmp[2]), "=f"(tmp[3])
: "r"(addr), "C"(SHAPE), "n"(NUM));
if constexpr (NUM_REGS == 8)
asm volatile("tcgen05.ld.sync.aligned%9.x%10.b32 "
"{ %0, %1, %2, %3, %4, %5, %6, %7}, [%8];"
: "=f"(tmp[0]), "=f"(tmp[1]), "=f"(tmp[2]), "=f"(tmp[3]), "=f"(tmp[4]), "=f"(tmp[5]), "=f"(tmp[6]), "=f"(tmp[7])
: "r"(addr), "C"(SHAPE), "n"(NUM));
if constexpr (NUM_REGS == 16)
asm volatile("tcgen05.ld.sync.aligned%17.x%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"(addr), "C"(SHAPE), "n"(NUM));
if constexpr (NUM_REGS == 32)
asm volatile("tcgen05.ld.sync.aligned%33.x%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"(addr), "C"(SHAPE), "n"(NUM));
if constexpr (NUM_REGS == 64)
asm volatile("tcgen05.ld.sync.aligned%65.x%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"(addr), "C"(SHAPE), "n"(NUM));
if constexpr (NUM_REGS == 128)
asm volatile("tcgen05.ld.sync.aligned%129.x%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"(addr), "C"(SHAPE), "n"(NUM));
}
template <int num>
__device__ inline void
tcgen05_ld_32x32b(float *tmp, int row, int col) {
// each 32x32b tile uses 1 register per thread
tcgen05_ld<num, SHAPE::_32x32b, num>(tmp, row, col);
}
template <int num>
__device__ inline
void tcgen05_ld_16x128b(float *tmp, int row, int col) {
// each 16x128b tile uses 2 registers per thread
tcgen05_ld<num * 2, SHAPE::_16x128b, num>(tmp, row, col);
}
template <int num>
__device__ inline
void tcgen05_ld_16x256b(float *tmp, int row, int col) {
// each 16x256b tile uses 4 registers per thread
tcgen05_ld<num * 4, SHAPE::_16x256b, num>(tmp, row, col);
}
constexpr int BLOCK_M = 128;
constexpr int BLOCK_N = 128;
constexpr int BLOCK_K = 256;
constexpr int NUM_WARPS = 6;
constexpr int TB_SIZE = NUM_WARPS * WARP_SIZE;
template <typename T>
__device__ __inline__
T warp_uniform(T x) { return __shfl_sync(0xFFFF'FFFF, x, 0); }
template <int NUM_GROUPS>
struct Arguments {
CUtensorMap A_tmap_list[NUM_GROUPS];
CUtensorMap B_tmap_list[NUM_GROUPS];
CUtensorMap SFA_tmap_list[NUM_GROUPS];
CUtensorMap SFB_tmap_list[NUM_GROUPS];
half *C_ptr_list[NUM_GROUPS];
int M_list[NUM_GROUPS];
int grid_cu[NUM_GROUPS + 1];
};
enum class Raster { M, N };
// enable threadblock cluster speeds up benchmark.0, even though we are not using any cluster features.
// __cluster_dims__(1, 1, 1) gives different benchmark results from that of NOT specifying __cluster_dims__ at all.
template <int NUM_GROUPS, int N, int K, int NUM_STAGES, int CTA_GROUP, Raster raster>
__global__
__cluster_dims__(2, 1, 1)
__launch_bounds__(TB_SIZE)
void kernel_cutlass(const __grid_constant__ Arguments<NUM_GROUPS> args, int K_dyn) {
const int tid = threadIdx.x;
const int lane_id = tid % WARP_SIZE;
const int warp_id = warp_uniform(tid / WARP_SIZE);
// 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 / CTA_GROUP) * BLOCK_K / 2;
constexpr int B_size = BLOCK_N * BLOCK_K / 2;
constexpr int SF_size = 128 * BLOCK_K / 16;
constexpr int STAGE_SIZE = A_size + B_size + SF_size * 2;
// set up mbarriers and tmem
const int tma_mbar_addr = smem + NUM_STAGES * STAGE_SIZE;
const int mma_mbar_addr = tma_mbar_addr + NUM_STAGES * 8;
const int mainloop_mbar_addr = mma_mbar_addr + NUM_STAGES * 8;
const int epilogue_mbar_addr = mainloop_mbar_addr + 2 * 8;
constexpr uint64_t cache_A = EVICT_NORMAL;
constexpr uint64_t cache_B = EVICT_FIRST;
constexpr int bar_epilogue = 2;
constexpr int rest_k = K / 16 / 4;
if (warp_id == 0 && elect_sync()) {
// not important that we prefetch tmap for the corresponding GEMM group
int group_id = blockIdx.x % NUM_GROUPS;
prefetch_tensormap(args.A_tmap_list + group_id);
prefetch_tensormap(args.B_tmap_list + group_id);
prefetch_tensormap(args.SFA_tmap_list + group_id);
prefetch_tensormap(args.SFB_tmap_list + group_id);
}
else if (warp_id == 1 && elect_sync()) {
// 1 thread init mbarrier
for (int i = 0; i < NUM_STAGES; i++) {
mbarrier_init(tma_mbar_addr + i * 8, CTA_GROUP);
mbarrier_init(mma_mbar_addr + i * 8, 1);
}
for (int i = 0; i < 2; i++)
mbarrier_init(mainloop_mbar_addr + i * 8, 1);
for (int i = 0; i < 2; i++)
mbarrier_init(epilogue_mbar_addr + i * 8, 4 * WARP_SIZE * CTA_GROUP);
asm volatile("fence.mbarrier_init.release.cluster;"); // visible to async proxy
}
if constexpr (CTA_GROUP == 2) {
asm volatile("barrier.cluster.arrive.relaxed.aligned;");
asm volatile("barrier.cluster.wait.acquire.aligned;");
}
else {
__syncthreads();
}
constexpr int grid_n = N / BLOCK_N;
const int num_tiles = args.grid_cu[NUM_GROUPS];
const int cta_rank = blockIdx.x % CTA_GROUP;
// group_id, bid_m, bid_n, M will be mutated
auto find_bid = [&](int bid, int& group_id, int& M, int& bid_m, int& bid_n) {
for (; group_id < NUM_GROUPS; group_id++) {
if (bid < args.grid_cu[group_id + 1]) {
bid -= args.grid_cu[group_id];
break;
}
}
M = args.M_list[group_id];
if constexpr (raster == Raster::M) {
const int grid_m = cdiv(M, BLOCK_M);
bid_m = bid % grid_m;
bid_n = bid / grid_m;
}
if constexpr (raster == Raster::N) {
bid_m = bid / grid_n;
bid_n = bid % grid_n;
}
};
if (warp_id == NUM_WARPS - 2) {
// TMA warp
if (elect_sync()) {
int stage_id = 0;
int mma_phase = 1;
int group_id = 0;
const int tma_mbar_addr_ = CTA_GROUP == 2 ? (tma_mbar_addr & 0xFEFFFFFF) : tma_mbar_addr; // report to CTA0
for (int bid = blockIdx.x; bid < num_tiles; bid += gridDim.x) {
int M, bid_m, bid_n;
find_bid(bid, group_id, M, bid_m, bid_n);
const int off_m = bid_m * BLOCK_M + cta_rank * (BLOCK_M / CTA_GROUP);
const int off_n = bid_n * BLOCK_N;
auto A_tmap = args.A_tmap_list + group_id;
auto B_tmap = args.B_tmap_list + group_id;
auto SFA_tmap = args.SFA_tmap_list + group_id;
auto SFB_tmap = args.SFB_tmap_list + group_id;
for (int iter_k = 0; iter_k < K_dyn / BLOCK_K; iter_k++) {
// select tma mbar and smem
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 + SF_size;
// wait MMA
mbarrier_wait(mma_mbar_addr + stage_id * 8, mma_phase);
// issue MMA
tma_3d_g2s<CTA_GROUP>(B_smem, B_tmap, 0, off_n, iter_k, mbar_addr, cache_B);
tma_3d_g2s<CTA_GROUP>(A_smem, A_tmap, 0, off_m, iter_k, mbar_addr, cache_A);
// divide by 8 because we use int64 as dtype for tensor map (to get around boxDim<=256 restriction)
const int off_sfb = bid_n * rest_k * 512 + iter_k * 2048;
const int off_sfa = bid_m * rest_k * 512 + iter_k * 2048;
tma_1d_g2s<CTA_GROUP>(SFB_smem, SFB_tmap, off_sfb / 8, mbar_addr, cache_B);
tma_1d_g2s<CTA_GROUP>(SFA_smem, SFA_tmap, off_sfa / 8, mbar_addr, cache_A);
mbarrier_arrive_expect_tx(mbar_addr, STAGE_SIZE); // signal TMA done
stage_id = (stage_id + 1) % NUM_STAGES;
if (stage_id == 0)
mma_phase ^= 1;
}
}
}
}
else if (warp_id == NUM_WARPS - 1) {
// MMA warp
tcgen05_alloc<CTA_GROUP>(epilogue_mbar_addr + 2 * 8, 512); // allocate tmem
// instruction desc
constexpr uint32_t MMA_M = BLOCK_N * CTA_GROUP;
constexpr uint32_t MMA_N = BLOCK_M;
constexpr uint32_t i_desc = (1U << 7U) // atype=E2M1
| (1U << 10U) // btype=E2M1
| (MMA_N >> 3U << 17U)
| (MMA_M >> 7U << 27U)
;
if (cta_rank == 0 && elect_sync()) {
int outer_stage = 0;
int epilogue_phase = 1;
int inner_stage = 0;
int tma_phase = 0;
for (int bid = blockIdx.x; bid < num_tiles; bid += gridDim.x) {
const int acc_tmem = outer_stage * BLOCK_M;
mbarrier_wait(epilogue_mbar_addr + outer_stage * 8, epilogue_phase);
// we use K_dyn to prevent the compiler from unrolling this loop.
// when using cutlass incantation, adding #pragma unroll 1 to this loop
// results in segmentation fault.
for (int iter_k = 0; iter_k < K_dyn / BLOCK_K; iter_k++) {
// select smem
const int A_smem = smem + inner_stage * STAGE_SIZE;
const int B_smem = A_smem + A_size;
const int SFA_smem = B_smem + B_size;
const int SFB_smem = SFA_smem + SF_size;
// set up smem desc
// AB: 128-byte swizzling
constexpr uint64_t AB_desc = (desc_encode(8 * 128) << 32ULL) | (1ULL << 46ULL) | (2ULL << 61ULL);
uint64_t a_desc = AB_desc | (A_smem >> 4);
uint64_t b_desc = AB_desc | (B_smem >> 4);
// SF: no swizzling
constexpr uint64_t SF_desc = (desc_encode(8 * 16) << 32ULL) | (1ULL << 46ULL);
uint64_t sfa_desc = SF_desc | (SFA_smem >> 4);
uint64_t sfb_desc = SF_desc | (SFB_smem >> 4);
// each SF consumes 16 columns per BLOCK_K=256
int sfa_tmem = BLOCK_N * 2;
int sfb_tmem = sfa_tmem + 16;
// wait TMA
mbarrier_wait(tma_mbar_addr + inner_stage * 8, tma_phase);
// manual unroll 1st iteration
tcgen05_cp_nvfp4<CTA_GROUP>(sfa_tmem, sfa_desc);
tcgen05_cp_nvfp4<CTA_GROUP>(sfb_tmem, sfb_desc);
tcgen05_mma_nvfp4<CTA_GROUP>(acc_tmem, b_desc, a_desc, i_desc, sfb_tmem, sfa_tmem, iter_k);
for (int k = 1; k < BLOCK_K / MMA_K; k++) {
// next 4 columns
sfa_tmem += 4;
sfb_tmem += 4;
// next 512-byte
sfa_desc += (512 >> 4);
sfb_desc += (512 >> 4);
// next 32-byte
a_desc += (32 >> 4);
b_desc += (32 >> 4);
tcgen05_cp_nvfp4<CTA_GROUP>(sfa_tmem, sfa_desc);
tcgen05_cp_nvfp4<CTA_GROUP>(sfb_tmem, sfb_desc);
tcgen05_mma_nvfp4<CTA_GROUP>(acc_tmem, b_desc, a_desc, i_desc, sfb_tmem, sfa_tmem, 1);
}
// signal MMA done
if constexpr (CTA_GROUP == 2)
tcgen05_commit_mcast<CTA_GROUP>(mma_mbar_addr + inner_stage * 8, 0b11);
else
tcgen05_commit<CTA_GROUP>(mma_mbar_addr + inner_stage * 8);
inner_stage = (inner_stage + 1) % NUM_STAGES;
if (inner_stage == 0)
tma_phase ^= 1;
}
// signal mainloop done
if constexpr (CTA_GROUP == 2)
tcgen05_commit_mcast<CTA_GROUP>(mainloop_mbar_addr + outer_stage * 8, 0b11);
else
tcgen05_commit<CTA_GROUP>(mainloop_mbar_addr + outer_stage * 8);
outer_stage ^= 1;
if (outer_stage == 0)
epilogue_phase ^= 1;
}
}
}
else {
// epilogue warps
auto stg_16 = [](half *ptr, float *tmp) {
asm volatile(
"{\n"
".reg .b32 out0, out1, out2, out3, out4, out5, out6, out7;\n"
"cvt.rn.f16x2.f32 out0, %2, %1;\n"
"cvt.rn.f16x2.f32 out1, %4, %3;\n"
"cvt.rn.f16x2.f32 out2, %6, %5;\n"
"cvt.rn.f16x2.f32 out3, %8, %7;\n"
"cvt.rn.f16x2.f32 out4, %10, %9;\n"
"cvt.rn.f16x2.f32 out5, %12, %11;\n"
"cvt.rn.f16x2.f32 out6, %14, %13;\n"
"cvt.rn.f16x2.f32 out7, %16, %15;\n"
"st.global.v8.b32 [%0], {out0, out1, out2, out3, out4, out5, out6, out7};\n"
"}"
:: "l"(ptr),
"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])
);
};
int stage_id = 0;
int mainloop_phase = 0;
int group_id = 0;
const int epilogue_mbar_addr_ = CTA_GROUP == 2 ? (epilogue_mbar_addr & 0xFEFFFFFF) : epilogue_mbar_addr; // report to CTA0
for (int bid = blockIdx.x; bid < num_tiles; bid += gridDim.x) {
int M, bid_m, bid_n;
find_bid(bid, group_id, M, bid_m, bid_n);
const int off_m = bid_m * BLOCK_M;
const int off_n = bid_n * BLOCK_N;
half *C_ptr = args.C_ptr_list[group_id];
const int stride_cn = cdiv(M, 16) * 16; // multiple of 16
if (warp_id == 0)
mbarrier_wait(mainloop_mbar_addr + stage_id * 8, mainloop_phase);
bar_sync<bar_epilogue>(4 * WARP_SIZE);
asm volatile("tcgen05.fence::after_thread_sync;");
constexpr int WIDTH = 16;
for (int m = 0; m < BLOCK_M / WIDTH; m++) {
float tmp[WIDTH];
tcgen05_ld_32x32b<WIDTH>(tmp, cta_rank * BLOCK_N + warp_id * 32, stage_id * BLOCK_M + m * WIDTH);
asm volatile("tcgen05.wait::ld.sync.aligned;");
const int row = off_n + tid;
const int col = off_m + m * WIDTH;
if (col < M)
stg_16(C_ptr + (row * stride_cn + col), tmp);
}
mbarrier_arrive(epilogue_mbar_addr_ + stage_id * 8);
stage_id ^= 1;
if (stage_id == 0)
mainloop_phase ^= 1;
}
if constexpr (CTA_GROUP == 2) {
asm volatile("barrier.cluster.arrive.relaxed.aligned;");
asm volatile("barrier.cluster.wait.acquire.aligned;");
}
else {
bar_sync<bar_epilogue>(4 * WARP_SIZE);
}
if (warp_id == 0)
tcgen05_dealloc<CTA_GROUP>(0, 512);
}
}
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,
void *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,
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);
}
void init_SF_tmap(CUtensorMap *tmap, void *ptr, uint64_t MN, uint64_t K) {
MN = cdiv(MN, 128) * 128; // round up to multiple of 128
const uint64_t global_size = MN * K / 16;
constexpr int SF_size = 128 * BLOCK_K / 16;
// use int64 as dtype, hence divide sizes by 8
constexpr uint32_t rank = 1;
uint64_t globalDim[rank] = {global_size / 8};
uint64_t globalStrides[rank-1] = {}; // in bytes
uint32_t boxDim[rank] = {SF_size / 8};
uint32_t elementStrides[rank] = {1};
auto err = cuTensorMapEncodeTiled(
tmap,
CUtensorMapDataType::CU_TENSOR_MAP_DATA_TYPE_INT64,
rank,
ptr,
globalDim,
globalStrides,
boxDim,
elementStrides,
CUtensorMapInterleave::CU_TENSOR_MAP_INTERLEAVE_NONE,
CUtensorMapSwizzle::CU_TENSOR_MAP_SWIZZLE_NONE,
CUtensorMapL2promotion::CU_TENSOR_MAP_L2_PROMOTION_NONE,
CUtensorMapFloatOOBfill::CU_TENSOR_MAP_FLOAT_OOB_FILL_NONE
);
//check_cu(err);
}
template <int NUM_GROUPS, int N, int K, int CTA_GROUP>
Arguments<NUM_GROUPS> create_args_template() {
Arguments<NUM_GROUPS> args;
args.grid_cu[0] = 0;
for (int i = 0; i < NUM_GROUPS; i++) {
init_AB_tmap(args.A_tmap_list + i, nullptr, 1, K, BLOCK_M / CTA_GROUP, BLOCK_K);
init_AB_tmap(args.B_tmap_list + i, nullptr, N, K, BLOCK_N, BLOCK_K);
init_SF_tmap(args.SFA_tmap_list + i, nullptr, 1, K);
init_SF_tmap(args.SFB_tmap_list + i, nullptr, N, K);
}
return args;
}
// from ChatGPT
template <int N>
void argsort_desc(const int (&values)[N], int (&indices)[N]) {
// initialize indices
for (int i = 0; i < N; ++i)
indices[i] = i;
// sort indices by values
std::sort(indices, indices + N, [&](int i, int j) { return values[i] > values[j]; });
}
template <int NUM_GROUPS, int N, int K, int CTA_GROUP, Raster raster>
void group_gemm_launch(
at::TensorList A_list,
at::TensorList B_list,
at::TensorList SFA_list,
at::TensorList SFB_list,
at::TensorList C_list
) {
constexpr int grid_n = N / BLOCK_N;
// notice static. we init once, then only change M and pointer addresses.
static Arguments<NUM_GROUPS> args = create_args_template<NUM_GROUPS, N, K, CTA_GROUP>();
// sort by descending M values
// this helps benchmark.0 a bit, probably thanks to reduced tail effect of the epilogue.
int values[NUM_GROUPS];
for (int i = 0; i < NUM_GROUPS; i++) {
values[i] = A_list[i].size(0);
}
int indices[NUM_GROUPS];
argsort_desc<NUM_GROUPS>(values, indices);
for (int i = 0; i < NUM_GROUPS; i++) {
const int idx = indices[i];
const int M = A_list[idx].size(0);
// exploit the internal encodings of CUtensorMap. doesn't seem to be faster.
reinterpret_cast<void **>(args.A_tmap_list + i)[0] = A_list[idx].data_ptr();
reinterpret_cast<int *>(args.A_tmap_list + i)[9] = M - 1;
reinterpret_cast<void **>(args.B_tmap_list + i)[0] = B_list[idx].data_ptr();
reinterpret_cast<void **>(args.SFA_tmap_list + i)[0] = SFA_list[idx].data_ptr();
reinterpret_cast<int *>(args.SFA_tmap_list + i)[8] = (cdiv(M, 128) * 128 * K / 16) - 1;
reinterpret_cast<void **>(args.SFB_tmap_list + i)[0] = SFB_list[idx].data_ptr();
args.C_ptr_list[i] = reinterpret_cast<half *>(C_list[idx].data_ptr());
args.M_list[i] = M;
args.grid_cu[i + 1] = args.grid_cu[i] + cdiv(M, BLOCK_M) * grid_n;
}
// make sure num SMs used is a multiple of grid_n
const int grid = std::min(148 / grid_n * grid_n, args.grid_cu[NUM_GROUPS]);
constexpr int AB_size = ((BLOCK_M / CTA_GROUP) + BLOCK_N) * (BLOCK_K / 2);
constexpr int SF_size = 128 * (BLOCK_K / 16) * 2;
constexpr int sm100_size = 227 * 1024;
constexpr int dynamic_size = AB_size + SF_size + 2 * 8; // 1 tma_mbar, 1 mma_mbar
constexpr int static_size = 2 * 2 * 8 + 4; // 2 mainloop_mbar, 2 epilogue_mbar, tmem_addr
constexpr int NUM_STAGES = (sm100_size - static_size) / dynamic_size;
constexpr int smem_size = dynamic_size * NUM_STAGES + static_size;
// cutlass incantation (this affects ptxas)
auto this_kernel = kernel_cutlass<NUM_GROUPS, N, K, NUM_STAGES, CTA_GROUP, raster>;
cudaFuncSetAttribute(this_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size);
this_kernel<<<grid, TB_SIZE, smem_size>>>(args, K);
}
void group_gemm(
at::TensorList A_list,
at::TensorList B_list,
at::TensorList SFA_list,
at::TensorList SFB_list,
at::TensorList C_list
) {
const int G = A_list.size();
const int N = B_list[0].size(0);
const int K = B_list[0].size(1) * 2;
#define LAUNCH(G_, N_, K_, CTA_GROUP, raster) \
else if (G == G_ && N == N_ && K == K_) { \
group_gemm_launch<G_, N_, K_, CTA_GROUP, raster>(A_list, B_list, SFA_list, SFB_list, C_list); \
}
if (false) {}
LAUNCH(8, 4096, 7168, 2, Raster::N)
LAUNCH(8, 7168, 2048, 2, Raster::N)
LAUNCH(2, 3072, 4096, 2, Raster::N)
LAUNCH(2, 4096, 1536, 1, Raster::N)
#undef LAUNCH
}
TORCH_LIBRARY(my_module, m) {
m.def("group_gemm(Tensor[] A_list, Tensor[] B_list, Tensor[] SFA_list, Tensor[] SFB_list, Tensor(a!)[] C_list) -> ()");
m.impl("group_gemm", &group_gemm);
}
"""
load_inline(
"group_gemm",
cpp_sources="",
cuda_sources=CUDA_SRC,
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"],
)
group_gemm = torch.ops.my_module.group_gemm
def ref(A_list, B_list, SFA_list, SFB_list, C_list):
for a, b, sfa, sfb, c in zip(A_list, B_list, SFA_list, SFB_list, C_list):
torch._scaled_mm(
a[..., 0],
b[..., 0].T,
sfa.permute(5, 2, 4, 0, 1, 3).view(-1),
sfb.permute(5, 2, 4, 0, 1, 3).view(-1),
out=c[..., 0],
)
def custom_kernel(data: input_t) -> output_t:
abc_list, _, sf_list, shape_list = data
A_list, B_list, C_list = zip(*abc_list)
SFA_list, SFB_list = zip(*sf_list)
_, N0, K0, _ = shape_list[0]
# M-major, and pad M to multiple of 16
C_list = []
for M, N, _, _ in shape_list:
new_M = (M + 16 - 1) // 16 * 16
new_C = torch.empty(new_M * N, dtype=torch.half, device="cuda")
new_C = new_C.as_strided((M, N, 1), (1, new_M, 0))
C_list.append(new_C)
for _, N, K, _ in shape_list:
if N != N0 or K != K0:
ref(A_list, B_list, SFA_list, SFB_list, C_list)
break
else:
# benchmark shapes: same N and K across groups
group_gemm(A_list, B_list, SFA_list, SFB_list, C_list)
# torch.cuda.synchronize()
return C_list
scrolls · 944 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 489191.
⋯ 41 unchanged linesreturn pred;}+ // named barrier+ template <int bar>__device__ inline+ void bar_sync(int count) {+ asm volatile("bar.sync %0, %1;" :: "n"(bar), "r"(count) : "memory");+ }++ __device__ inlinevoid mbarrier_init(int mbar_addr, int count) {asm volatile("mbarrier.init.shared::cta.b64 [%0], %1;" :: "r"(mbar_addr), "r"(count));}⋯ 54 unchanged lines}__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;"+ void tma_g2s(int dst, const void *src, int size, int mbar_addr, uint64_t cache_policy) {+ asm volatile("cp.async.bulk.shared::cluster.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 <int CTA_GROUP = 1>__device__ inline- void tma_1d_gmem2smem(int dst, const void *tmap_ptr, int x, int mbar_addr, uint64_t cache_policy) {- asm volatile("cp.async.bulk.tensor.1d.shared::cta.global.mbarrier::complete_tx::bytes.cta_group::%5.L2::cache_hint "+ void tma_1d_g2s(int dst, const void *tmap_ptr, int x, int mbar_addr, uint64_t cache_policy) {+ asm volatile("cp.async.bulk.tensor.1d.shared::cluster.global.mbarrier::complete_tx::bytes.cta_group::%5.L2::cache_hint ""[%0], [%1, {%2}], [%3], %4;":: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(mbar_addr), "l"(cache_policy), "n"(CTA_GROUP): "memory");⋯ 1 unchanged linestemplate <int CTA_GROUP = 1>__device__ inline- void tma_1d_gmem2smem_mcast(int dst, const void *tmap_ptr, int x, int mbar_addr, int16_t cta_mask, uint64_t cache_policy) {+ void tma_1d_g2s_mcast(int dst, const void *tmap_ptr, int x, int mbar_addr, int16_t cta_mask, uint64_t cache_policy) {asm volatile("cp.async.bulk.tensor.1d.shared::cluster.global.mbarrier::complete_tx::bytes.multicast::cluster.cta_group::%6.L2::cache_hint ""[%0], [%1, {%2}], [%3], %4, %5;":: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(mbar_addr), "h"(cta_mask), "l"(cache_policy), "n"(CTA_GROUP)⋯ 2 unchanged linestemplate <int CTA_GROUP = 1>__device__ inline- void tma_2d_gmem2smem(int dst, const void *tmap_ptr, int x, int y, int mbar_addr, uint64_t cache_policy) {- asm volatile("cp.async.bulk.tensor.2d.shared::cta.global.mbarrier::complete_tx::bytes.cta_group::%6.L2::cache_hint "+ void tma_2d_g2s(int dst, const void *tmap_ptr, int x, int y, int mbar_addr, uint64_t cache_policy) {+ asm volatile("cp.async.bulk.tensor.2d.shared::cluster.global.mbarrier::complete_tx::bytes.cta_group::%6.L2::cache_hint ""[%0], [%1, {%2, %3}], [%4], %5;":: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(y), "r"(mbar_addr), "l"(cache_policy), "n"(CTA_GROUP): "memory");⋯ 1 unchanged linestemplate <int CTA_GROUP = 1>__device__ inline- void tma_2d_gmem2smem_mcast(int dst, const void *tmap_ptr, int x, int y, int mbar_addr, int16_t cta_mask, uint64_t cache_policy) {+ void tma_2d_g2s_mcast(int dst, const void *tmap_ptr, int x, int y, int mbar_addr, int16_t cta_mask, uint64_t cache_policy) {asm volatile("cp.async.bulk.tensor.2d.shared::cluster.global.mbarrier::complete_tx::bytes.multicast::cluster.cta_group::%7.L2::cache_hint ""[%0], [%1, {%2, %3}], [%4], %5, %6;":: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(y), "r"(mbar_addr), "h"(cta_mask), "l"(cache_policy), "n"(CTA_GROUP)⋯ 2 unchanged linestemplate <int CTA_GROUP = 1>__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::%7.L2::cache_hint "+ void tma_3d_g2s(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::cluster.global.mbarrier::complete_tx::bytes.cta_group::%7.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), "n"(CTA_GROUP): "memory");⋯ 1 unchanged linestemplate <int CTA_GROUP = 1>__device__ inline- void tma_3d_gmem2smem_mcast(int dst, const void *tmap_ptr, int x, int y, int z, int mbar_addr, int16_t cta_mask, uint64_t cache_policy) {+ void tma_3d_g2s_mcast(int dst, const void *tmap_ptr, int x, int y, int z, int mbar_addr, int16_t cta_mask, uint64_t cache_policy) {asm volatile("cp.async.bulk.tensor.3d.shared::cluster.global.mbarrier::complete_tx::bytes.multicast::cluster.cta_group::%8.L2::cache_hint ""[%0], [%1, {%2, %3, %4}], [%5], %6, %7;":: "r"(dst), "l"(tmap_ptr), "r"(x), "r"(y), "r"(z), "r"(mbar_addr), "h"(cta_mask), "l"(cache_policy), "n"(CTA_GROUP)⋯ 202 unchanged linesstruct Arguments {CUtensorMap A_tmap_list[NUM_GROUPS];CUtensorMap B_tmap_list[NUM_GROUPS];- char *SFA_ptr_list[NUM_GROUPS];- char *SFB_ptr_list[NUM_GROUPS];+ CUtensorMap SFA_tmap_list[NUM_GROUPS];+ CUtensorMap SFB_tmap_list[NUM_GROUPS];half *C_ptr_list[NUM_GROUPS];int M_list[NUM_GROUPS];- int grid_m_cu[NUM_GROUPS + 1];+ int grid_cu[NUM_GROUPS + 1];};- template <int NUM_GROUPS, int N, int K, int NUM_STAGES>+ enum class Raster { M, N };++ // enable threadblock cluster speeds up benchmark.0, even though we are not using any cluster features.+ // __cluster_dims__(1, 1, 1) gives different benchmark results from that of NOT specifying __cluster_dims__ at all.+ template <int NUM_GROUPS, int N, int K, int NUM_STAGES, int CTA_GROUP, Raster raster>__global__+ __cluster_dims__(2, 1, 1)__launch_bounds__(TB_SIZE)- void kernel_cutlass (const __grid_constant__ Arguments<NUM_GROUPS> args) {+ void kernel_cutlass(const __grid_constant__ Arguments<NUM_GROUPS> args, int K_dyn) {const int tid = threadIdx.x;const int lane_id = tid % WARP_SIZE;const int warp_id = warp_uniform(tid / WARP_SIZE);⋯ 1 unchanged lines// set up smemextern __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 A_size = (BLOCK_M / CTA_GROUP) * BLOCK_K / 2;constexpr int B_size = BLOCK_N * BLOCK_K / 2;constexpr int SF_size = 128 * BLOCK_K / 16;constexpr int STAGE_SIZE = A_size + B_size + SF_size * 2;⋯ 15 unchanged linesint group_id = blockIdx.x % NUM_GROUPS;prefetch_tensormap(args.A_tmap_list + group_id);prefetch_tensormap(args.B_tmap_list + group_id);+ prefetch_tensormap(args.SFA_tmap_list + group_id);+ prefetch_tensormap(args.SFB_tmap_list + group_id);}else if (warp_id == 1 && elect_sync()) {// 1 thread init mbarrierfor (int i = 0; i < NUM_STAGES; i++) {- mbarrier_init(tma_mbar_addr + i * 8, 1);+ mbarrier_init(tma_mbar_addr + i * 8, CTA_GROUP);mbarrier_init(mma_mbar_addr + i * 8, 1);}- for (int i = 0; i < 2; i++) {+ for (int i = 0; i < 2; i++)mbarrier_init(mainloop_mbar_addr + i * 8, 1);- mbarrier_init(epilogue_mbar_addr + i * 8, 4 * WARP_SIZE);- }+ for (int i = 0; i < 2; i++)+ mbarrier_init(epilogue_mbar_addr + i * 8, 4 * WARP_SIZE * CTA_GROUP);asm volatile("fence.mbarrier_init.release.cluster;"); // visible to async proxy}- __syncthreads();+ if constexpr (CTA_GROUP == 2) {+ asm volatile("barrier.cluster.arrive.relaxed.aligned;");+ asm volatile("barrier.cluster.wait.acquire.aligned;");+ }+ else {+ __syncthreads();+ }constexpr int grid_n = N / BLOCK_N;- const int num_tiles = args.grid_m_cu[NUM_GROUPS] * grid_n;+ const int num_tiles = args.grid_cu[NUM_GROUPS];+ const int cta_rank = blockIdx.x % CTA_GROUP;+ // group_id, bid_m, bid_n, M will be mutated+ auto find_bid = [&](int bid, int& group_id, int& M, int& bid_m, int& bid_n) {+ for (; group_id < NUM_GROUPS; group_id++) {+ if (bid < args.grid_cu[group_id + 1]) {+ bid -= args.grid_cu[group_id];+ break;+ }+ }+ M = args.M_list[group_id];++ if constexpr (raster == Raster::M) {+ const int grid_m = cdiv(M, BLOCK_M);+ bid_m = bid % grid_m;+ bid_n = bid / grid_m;+ }+ if constexpr (raster == Raster::N) {+ bid_m = bid / grid_n;+ bid_n = bid % grid_n;+ }+ };+if (warp_id == NUM_WARPS - 2) {// TMA warpif (elect_sync()) {⋯ 1 unchanged linesint mma_phase = 1;int group_id = 0;+ const int tma_mbar_addr_ = CTA_GROUP == 2 ? (tma_mbar_addr & 0xFEFFFFFF) : tma_mbar_addr; // report to CTA0for (int bid = blockIdx.x; bid < num_tiles; bid += gridDim.x) {- const int bid_n = bid % grid_n;+ int M, bid_m, bid_n;+ find_bid(bid, group_id, M, bid_m, bid_n);- // find bid_m- // NOTE: this might be bad- int bid_m = bid / grid_n;- for (; group_id < NUM_GROUPS; group_id++) {- if (bid_m < args.grid_m_cu[group_id + 1]) {- bid_m -= args.grid_m_cu[group_id];- break;- }- }-- const int off_m = bid_m * BLOCK_M;+ const int off_m = bid_m * BLOCK_M + cta_rank * (BLOCK_M / CTA_GROUP);const int off_n = bid_n * BLOCK_N;auto A_tmap = args.A_tmap_list + group_id;auto B_tmap = args.B_tmap_list + group_id;- const char *SFA_ptr = args.SFA_ptr_list[group_id];- const char *SFB_ptr = args.SFB_ptr_list[group_id];+ auto SFA_tmap = args.SFA_tmap_list + group_id;+ auto SFB_tmap = args.SFB_tmap_list + group_id;- #pragma unroll 1- for (int iter_k = 0; iter_k < K / BLOCK_K; iter_k++) {+ for (int iter_k = 0; iter_k < K_dyn / BLOCK_K; iter_k++) {// select tma mbar and smem- const int mbar_addr = tma_mbar_addr + stage_id * 8;+ 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;⋯ 3 unchanged linesmbarrier_wait(mma_mbar_addr + stage_id * 8, mma_phase);// issue MMA- tma_3d_gmem2smem(B_smem, B_tmap, 0, off_n, iter_k, mbar_addr, cache_B);- tma_3d_gmem2smem(A_smem, A_tmap, 0, off_m, iter_k, mbar_addr, cache_A);+ tma_3d_g2s<CTA_GROUP>(B_smem, B_tmap, 0, off_n, iter_k, mbar_addr, cache_B);+ tma_3d_g2s<CTA_GROUP>(A_smem, A_tmap, 0, off_m, iter_k, mbar_addr, cache_A);- const char *SFB_src = SFB_ptr + (bid_n * rest_k * 512 + iter_k * 2048);- const char *SFA_src = SFA_ptr + (bid_m * rest_k * 512 + iter_k * 2048);- tma_gmem2smem(SFB_smem, SFB_src, SF_size, mbar_addr, cache_B);- tma_gmem2smem(SFA_smem, SFA_src, SF_size, mbar_addr, cache_A);+ // divide by 8 because we use int64 as dtype for tensor map (to get around boxDim<=256 restriction)+ const int off_sfb = bid_n * rest_k * 512 + iter_k * 2048;+ const int off_sfa = bid_m * rest_k * 512 + iter_k * 2048;+ tma_1d_g2s<CTA_GROUP>(SFB_smem, SFB_tmap, off_sfb / 8, mbar_addr, cache_B);+ tma_1d_g2s<CTA_GROUP>(SFA_smem, SFA_tmap, off_sfa / 8, mbar_addr, cache_A);mbarrier_arrive_expect_tx(mbar_addr, STAGE_SIZE); // signal TMA done⋯ 6 unchanged lines}else if (warp_id == NUM_WARPS - 1) {// MMA warp- tcgen05_alloc(epilogue_mbar_addr + 2 * 8, 512); // allocate tmem+ tcgen05_alloc<CTA_GROUP>(epilogue_mbar_addr + 2 * 8, 512); // allocate tmem// instruction desc- constexpr uint32_t MMA_M = BLOCK_M;- constexpr uint32_t MMA_N = BLOCK_N;+ constexpr uint32_t MMA_M = BLOCK_N * CTA_GROUP;+ constexpr uint32_t MMA_N = BLOCK_M;constexpr uint32_t i_desc = (1U << 7U) // atype=E2M1| (1U << 10U) // btype=E2M1| (MMA_N >> 3U << 17U)| (MMA_M >> 7U << 27U);- if (elect_sync()) {+ if (cta_rank == 0 && elect_sync()) {int outer_stage = 0;int epilogue_phase = 1;⋯ 1 unchanged linesint tma_phase = 0;for (int bid = blockIdx.x; bid < num_tiles; bid += gridDim.x) {- const int acc_tmem = outer_stage * BLOCK_N;+ const int acc_tmem = outer_stage * BLOCK_M;mbarrier_wait(epilogue_mbar_addr + outer_stage * 8, epilogue_phase);- for (int iter_k = 0; iter_k < K / BLOCK_K; iter_k++) {+ // we use K_dyn to prevent the compiler from unrolling this loop.+ // when using cutlass incantation, adding #pragma unroll 1 to this loop+ // results in segmentation fault.+ for (int iter_k = 0; iter_k < K_dyn / BLOCK_K; iter_k++) {// select smemconst int A_smem = smem + inner_stage * STAGE_SIZE;const int B_smem = A_smem + A_size;⋯ 11 unchanged linesuint64_t sfa_desc = SF_desc | (SFA_smem >> 4);uint64_t sfb_desc = SF_desc | (SFB_smem >> 4);- // first BLOCK_N * 2 columns are for acc (double buffer)// each SF consumes 16 columns per BLOCK_K=256int sfa_tmem = BLOCK_N * 2;int sfb_tmem = sfa_tmem + 16;⋯ 2 unchanged linesmbarrier_wait(tma_mbar_addr + inner_stage * 8, tma_phase);// manual unroll 1st iteration- tcgen05_cp_nvfp4(sfa_tmem, sfa_desc);- tcgen05_cp_nvfp4(sfb_tmem, sfb_desc);- tcgen05_mma_nvfp4(acc_tmem, a_desc, b_desc, i_desc, sfa_tmem, sfb_tmem, iter_k);+ tcgen05_cp_nvfp4<CTA_GROUP>(sfa_tmem, sfa_desc);+ tcgen05_cp_nvfp4<CTA_GROUP>(sfb_tmem, sfb_desc);+ tcgen05_mma_nvfp4<CTA_GROUP>(acc_tmem, b_desc, a_desc, i_desc, sfb_tmem, sfa_tmem, iter_k);for (int k = 1; k < BLOCK_K / MMA_K; k++) {// next 4 columns⋯ 8 unchanged linesa_desc += (32 >> 4);b_desc += (32 >> 4);- tcgen05_cp_nvfp4(sfa_tmem, sfa_desc);- tcgen05_cp_nvfp4(sfb_tmem, sfb_desc);- tcgen05_mma_nvfp4(acc_tmem, a_desc, b_desc, i_desc, sfa_tmem, sfb_tmem, 1);+ tcgen05_cp_nvfp4<CTA_GROUP>(sfa_tmem, sfa_desc);+ tcgen05_cp_nvfp4<CTA_GROUP>(sfb_tmem, sfb_desc);+ tcgen05_mma_nvfp4<CTA_GROUP>(acc_tmem, b_desc, a_desc, i_desc, sfb_tmem, sfa_tmem, 1);}- tcgen05_commit(mma_mbar_addr + inner_stage * 8); // signal MMA done+ // signal MMA done+ if constexpr (CTA_GROUP == 2)+ tcgen05_commit_mcast<CTA_GROUP>(mma_mbar_addr + inner_stage * 8, 0b11);+ else+ tcgen05_commit<CTA_GROUP>(mma_mbar_addr + inner_stage * 8);inner_stage = (inner_stage + 1) % NUM_STAGES;if (inner_stage == 0)tma_phase ^= 1;}- tcgen05_commit(mainloop_mbar_addr + outer_stage * 8); // signal mainloop done- outer_stage = (outer_stage + 1) % 2;+ // signal mainloop done+ if constexpr (CTA_GROUP == 2)+ tcgen05_commit_mcast<CTA_GROUP>(mainloop_mbar_addr + outer_stage * 8, 0b11);+ else+ tcgen05_commit<CTA_GROUP>(mainloop_mbar_addr + outer_stage * 8);++ outer_stage ^= 1;if (outer_stage == 0)epilogue_phase ^= 1;}⋯ 27 unchanged linesint mainloop_phase = 0;int group_id = 0;+ const int epilogue_mbar_addr_ = CTA_GROUP == 2 ? (epilogue_mbar_addr & 0xFEFFFFFF) : epilogue_mbar_addr; // report to CTA0for (int bid = blockIdx.x; bid < num_tiles; bid += gridDim.x) {- const int bid_n = bid % grid_n;+ int M, bid_m, bid_n;+ find_bid(bid, group_id, M, bid_m, bid_n);- // find bid_m- int bid_m = bid / grid_n;- for (; group_id < NUM_GROUPS; group_id++) {- if (bid_m < args.grid_m_cu[group_id + 1]) {- bid_m -= args.grid_m_cu[group_id];- break;- }- }-- const int M = args.M_list[group_id];- half *C_ptr = args.C_ptr_list[group_id];-const int off_m = bid_m * BLOCK_M;const int off_n = bid_n * BLOCK_N;+ half *C_ptr = args.C_ptr_list[group_id];+ const int stride_cn = cdiv(M, 16) * 16; // multiple of 16+if (warp_id == 0)mbarrier_wait(mainloop_mbar_addr + stage_id * 8, mainloop_phase);- asm volatile("bar.sync %0, %1;" :: "n"(bar_epilogue), "r"(4 * WARP_SIZE) : "memory");+ bar_sync<bar_epilogue>(4 * WARP_SIZE);asm volatile("tcgen05.fence::after_thread_sync;");constexpr int WIDTH = 16;- for (int n = 0; n < BLOCK_N / WIDTH; n++) {+ for (int m = 0; m < BLOCK_M / WIDTH; m++) {float tmp[WIDTH];- tcgen05_ld_32x32b<WIDTH>(tmp, warp_id * 32, stage_id * BLOCK_N + n * WIDTH);+ tcgen05_ld_32x32b<WIDTH>(tmp, cta_rank * BLOCK_N + warp_id * 32, stage_id * BLOCK_M + m * WIDTH);asm volatile("tcgen05.wait::ld.sync.aligned;");- const int row = off_m + tid;- const int col = off_n + n * WIDTH;-- if (row < M)- stg_16(C_ptr + (row * N + col), tmp);+ const int row = off_n + tid;+ const int col = off_m + m * WIDTH;+ if (col < M)+ stg_16(C_ptr + (row * stride_cn + col), tmp);}- mbarrier_arrive(epilogue_mbar_addr + stage_id * 8);- stage_id = (stage_id + 1) % 2;+ mbarrier_arrive(epilogue_mbar_addr_ + stage_id * 8);+ stage_id ^= 1;if (stage_id == 0)mainloop_phase ^= 1;}- asm volatile("bar.sync %0, %1;" :: "n"(bar_epilogue), "r"(4 * WARP_SIZE) : "memory");+ if constexpr (CTA_GROUP == 2) {+ asm volatile("barrier.cluster.arrive.relaxed.aligned;");+ asm volatile("barrier.cluster.wait.acquire.aligned;");+ }+ else {+ bar_sync<bar_epilogue>(4 * WARP_SIZE);+ }+if (warp_id == 0)- tcgen05_dealloc(0, 512);+ tcgen05_dealloc<CTA_GROUP>(0, 512);}}⋯ 39 unchanged lines//check_cu(err);}- template <int NUM_GROUPS, int N, int K>+ void init_SF_tmap(CUtensorMap *tmap, void *ptr, uint64_t MN, uint64_t K) {+ MN = cdiv(MN, 128) * 128; // round up to multiple of 128++ const uint64_t global_size = MN * K / 16;+ constexpr int SF_size = 128 * BLOCK_K / 16;++ // use int64 as dtype, hence divide sizes by 8+ constexpr uint32_t rank = 1;+ uint64_t globalDim[rank] = {global_size / 8};+ uint64_t globalStrides[rank-1] = {}; // in bytes+ uint32_t boxDim[rank] = {SF_size / 8};+ uint32_t elementStrides[rank] = {1};++ auto err = cuTensorMapEncodeTiled(+ tmap,+ CUtensorMapDataType::CU_TENSOR_MAP_DATA_TYPE_INT64,+ rank,+ ptr,+ globalDim,+ globalStrides,+ boxDim,+ elementStrides,+ CUtensorMapInterleave::CU_TENSOR_MAP_INTERLEAVE_NONE,+ CUtensorMapSwizzle::CU_TENSOR_MAP_SWIZZLE_NONE,+ CUtensorMapL2promotion::CU_TENSOR_MAP_L2_PROMOTION_NONE,+ CUtensorMapFloatOOBfill::CU_TENSOR_MAP_FLOAT_OOB_FILL_NONE+ );+ //check_cu(err);+ }++ template <int NUM_GROUPS, int N, int K, int CTA_GROUP>+ Arguments<NUM_GROUPS> create_args_template() {+ Arguments<NUM_GROUPS> args;+ args.grid_cu[0] = 0;++ for (int i = 0; i < NUM_GROUPS; i++) {+ init_AB_tmap(args.A_tmap_list + i, nullptr, 1, K, BLOCK_M / CTA_GROUP, BLOCK_K);+ init_AB_tmap(args.B_tmap_list + i, nullptr, N, K, BLOCK_N, BLOCK_K);+ init_SF_tmap(args.SFA_tmap_list + i, nullptr, 1, K);+ init_SF_tmap(args.SFB_tmap_list + i, nullptr, N, K);+ }+ return args;+ }++ // from ChatGPT+ template <int N>+ void argsort_desc(const int (&values)[N], int (&indices)[N]) {+ // initialize indices+ for (int i = 0; i < N; ++i)+ indices[i] = i;++ // sort indices by values+ std::sort(indices, indices + N, [&](int i, int j) { return values[i] > values[j]; });+ }++ template <int NUM_GROUPS, int N, int K, int CTA_GROUP, Raster raster>void group_gemm_launch(at::TensorList A_list,at::TensorList B_list,⋯ 1 unchanged linesat::TensorList SFB_list,at::TensorList C_list) {- Arguments<NUM_GROUPS> args;- args.grid_m_cu[0] = 0;+ constexpr int grid_n = N / BLOCK_N;+ // notice static. we init once, then only change M and pointer addresses.+ static Arguments<NUM_GROUPS> args = create_args_template<NUM_GROUPS, N, K, CTA_GROUP>();++ // sort by descending M values+ // this helps benchmark.0 a bit, probably thanks to reduced tail effect of the epilogue.+ int values[NUM_GROUPS];for (int i = 0; i < NUM_GROUPS; i++) {- const int M = A_list[i].size(0);+ values[i] = A_list[i].size(0);+ }+ int indices[NUM_GROUPS];+ argsort_desc<NUM_GROUPS>(values, indices);- init_AB_tmap(args.A_tmap_list + i, A_list[i].data_ptr(), M, K, BLOCK_M, BLOCK_K);- init_AB_tmap(args.B_tmap_list + i, B_list[i].data_ptr(), N, K, BLOCK_N, BLOCK_K);- args.SFA_ptr_list[i] = reinterpret_cast<char *>(SFA_list[i].data_ptr());- args.SFB_ptr_list[i] = reinterpret_cast<char *>(SFB_list[i].data_ptr());- args.C_ptr_list[i] = reinterpret_cast<half *>(C_list[i].data_ptr());+ for (int i = 0; i < NUM_GROUPS; i++) {+ const int idx = indices[i];+ const int M = A_list[idx].size(0);++ // exploit the internal encodings of CUtensorMap. doesn't seem to be faster.+ reinterpret_cast<void **>(args.A_tmap_list + i)[0] = A_list[idx].data_ptr();+ reinterpret_cast<int *>(args.A_tmap_list + i)[9] = M - 1;++ reinterpret_cast<void **>(args.B_tmap_list + i)[0] = B_list[idx].data_ptr();++ reinterpret_cast<void **>(args.SFA_tmap_list + i)[0] = SFA_list[idx].data_ptr();+ reinterpret_cast<int *>(args.SFA_tmap_list + i)[8] = (cdiv(M, 128) * 128 * K / 16) - 1;++ reinterpret_cast<void **>(args.SFB_tmap_list + i)[0] = SFB_list[idx].data_ptr();++ args.C_ptr_list[i] = reinterpret_cast<half *>(C_list[idx].data_ptr());args.M_list[i] = M;- args.grid_m_cu[i + 1] = args.grid_m_cu[i] + cdiv(M, BLOCK_M);+ args.grid_cu[i + 1] = args.grid_cu[i] + cdiv(M, BLOCK_M) * grid_n;}- // using only 128 SMs is faster than 148 SMs for benchmark.0.- // likely voodoo cache behavior.- constexpr int grid_n = N / BLOCK_N;- const int num_tiles = args.grid_m_cu[NUM_GROUPS] * grid_n;- const int grid = std::min(128, num_tiles);+ // make sure num SMs used is a multiple of grid_n+ const int grid = std::min(148 / grid_n * grid_n, args.grid_cu[NUM_GROUPS]);- constexpr int AB_size = (BLOCK_M + BLOCK_N) * (BLOCK_K / 2);+ constexpr int AB_size = ((BLOCK_M / CTA_GROUP) + BLOCK_N) * (BLOCK_K / 2);constexpr int SF_size = 128 * (BLOCK_K / 16) * 2;constexpr int sm100_size = 227 * 1024;constexpr int dynamic_size = AB_size + SF_size + 2 * 8; // 1 tma_mbar, 1 mma_mbar- constexpr int static_size = 4 * 8 + 4; // 2 mainloop_mbar, 2 epilogue_mbar, tmem_addr+ constexpr int static_size = 2 * 2 * 8 + 4; // 2 mainloop_mbar, 2 epilogue_mbar, tmem_addrconstexpr int NUM_STAGES = (sm100_size - static_size) / dynamic_size;constexpr int smem_size = dynamic_size * NUM_STAGES + static_size;// cutlass incantation (this affects ptxas)- auto this_kernel = kernel_cutlass<NUM_GROUPS, N, K, NUM_STAGES>;+ auto this_kernel = kernel_cutlass<NUM_GROUPS, N, K, NUM_STAGES, CTA_GROUP, raster>;cudaFuncSetAttribute(this_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size);- this_kernel<<<grid, TB_SIZE, smem_size>>>(args);+ this_kernel<<<grid, TB_SIZE, smem_size>>>(args, K);}void group_gemm(⋯ 7 unchanged linesconst int N = B_list[0].size(0);const int K = B_list[0].size(1) * 2;- #define LAUNCH(G_, N_, K_) \+ #define LAUNCH(G_, N_, K_, CTA_GROUP, raster) \else if (G == G_ && N == N_ && K == K_) { \- group_gemm_launch<G_, N_, K_>(A_list, B_list, SFA_list, SFB_list, C_list); \+ group_gemm_launch<G_, N_, K_, CTA_GROUP, raster>(A_list, B_list, SFA_list, SFB_list, C_list); \}if (false) {}- LAUNCH(8, 4096, 7168)- LAUNCH(8, 7168, 2048)- LAUNCH(2, 3072, 4096)- LAUNCH(2, 4096, 1536)+ LAUNCH(8, 4096, 7168, 2, Raster::N)+ LAUNCH(8, 7168, 2048, 2, Raster::N)+ LAUNCH(2, 3072, 4096, 2, Raster::N)+ LAUNCH(2, 4096, 1536, 1, Raster::N)#undef LAUNCH}⋯ 47 unchanged lines_, N0, K0, _ = shape_list[0]+ # M-major, and pad M to multiple of 16+ C_list = []+ for M, N, _, _ in shape_list:+ new_M = (M + 16 - 1) // 16 * 16+ new_C = torch.empty(new_M * N, dtype=torch.half, device="cuda")+ new_C = new_C.as_strided((M, N, 1), (1, new_M, 0))+ C_list.append(new_C)+for _, N, K, _ in shape_list:if N != N0 or K != K0:ref(A_list, B_list, SFA_list, SFB_list, C_list)
scrolls · 596 diff lines total
Best evidence level for this revision: reported
JSON