Skip to content
KernelIndex
Search⌘K

submission 382879

yue · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

submit_aggressive_opt.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-modal-nvfp4-dual-gemm-382879?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 dual GEMMsuite of 4 cases
NVIDIA B200
16.1µs
#86 of 161
2026-01-19

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:b427fb506d221ce9c789901602b70562b27f723706280c3fa4f7060c8653fa3a
license declaredunknown
license concludedunknown
authorsyue
imported2026-08-15

Techniques

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

mbarriervoid mbarrier_init(int mbar_addr, int count) {
shared-memoryextern __shared__ __align__(1024) char smem_ptr[];
tcgen05asm volatile("tcgen05.cp.cta_group::1.32x128b.warpx4 [%0], %1;" :: "r"(taddr), "l"(s_desc));
tmaasm volatile("cp.async.bulk.shared::cta.global.mbarrier::complete_tx::bytes.L2::cache_hint [%0], [%1], %2, [%3], %4;"

Kernel source

submit_aggressive_opt.py923 lines
# Dual GEMM: C = silu(A @ B1) * (A @ B2)


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




CUDA_SRC_COMMON = 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


// 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));
}


__device__ inline
void mbarrier_init_pred(int mbar_addr, int count, int pred) {
 asm volatile(
   "{\n\t"
   ".reg .pred p;\n\t"
   "setp.ne.b32 p, %2, 0;\n\t"
   "@p mbarrier.init.shared::cta.b64 [%0], %1;\n\t"
   "}"
   :: "r"(mbar_addr), "r"(count), "r"(pred)
 );
}


// 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;
 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)
 );
}


__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));
}


__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");
}


__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(
 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"
   "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
};


struct NUM {
 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_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); }


// Based on Modular's Blackwell optimization blog
// Fast silu using packed f32x2 operations for better ILP
// silu(x) = x * sigmoid(x) = x / (1 + exp(-x)) = x / (1 + 2^(-x*log2e))
__device__ inline
float silu(float x) {
 float result;
 asm volatile(
   "{\n\t"
   ".reg .f32 neg_scaled, exp_val, denom, rcp_val;\n\t"
   "mul.f32 neg_scaled, %1, 0FBFB8AA3B;\n\t"    // -x * log2(e)
   "ex2.approx.ftz.f32 exp_val, neg_scaled;\n\t" // exp(-x) via 2^(-x*log2e)
   "add.f32 denom, exp_val, 0F3F800000;\n\t"     // 1 + exp(-x)
   "rcp.approx.ftz.f32 rcp_val, denom;\n\t"      // 1/(1+exp(-x)) = sigmoid(x)
   "mul.f32 %0, %1, rcp_val;\n\t"                // x * sigmoid(x) = silu(x)
   "}"
   : "=f"(result)
   : "f"(x)
 );
 return result;
}

// Process 4 elements at once for better instruction-level parallelism
// Based on Modular's packed f32x2 optimization technique
__device__ inline
void silu_mul_x4(float *r, const float *a, const float *b) {
 asm volatile(
   "{\n\t"
   // Registers for 4 parallel silu computations
   ".reg .f32 ns0, ns1, ns2, ns3;\n\t"
   ".reg .f32 ex0, ex1, ex2, ex3;\n\t"
   ".reg .f32 dn0, dn1, dn2, dn3;\n\t"
   ".reg .f32 rc0, rc1, rc2, rc3;\n\t"
   ".reg .f32 sg0, sg1, sg2, sg3;\n\t"
   
   // Compute -a[i] * log2(e) for all 4
   "mul.f32 ns0, %4, 0FBFB8AA3B;\n\t"
   "mul.f32 ns1, %5, 0FBFB8AA3B;\n\t"
   "mul.f32 ns2, %6, 0FBFB8AA3B;\n\t"
   "mul.f32 ns3, %7, 0FBFB8AA3B;\n\t"
   
   // exp(-a[i]) via ex2
   "ex2.approx.ftz.f32 ex0, ns0;\n\t"
   "ex2.approx.ftz.f32 ex1, ns1;\n\t"
   "ex2.approx.ftz.f32 ex2, ns2;\n\t"
   "ex2.approx.ftz.f32 ex3, ns3;\n\t"
   
   // 1 + exp(-a[i])
   "add.f32 dn0, ex0, 0F3F800000;\n\t"
   "add.f32 dn1, ex1, 0F3F800000;\n\t"
   "add.f32 dn2, ex2, 0F3F800000;\n\t"
   "add.f32 dn3, ex3, 0F3F800000;\n\t"
   
   // 1 / (1 + exp(-a[i])) = sigmoid(a[i])
   "rcp.approx.ftz.f32 rc0, dn0;\n\t"
   "rcp.approx.ftz.f32 rc1, dn1;\n\t"
   "rcp.approx.ftz.f32 rc2, dn2;\n\t"
   "rcp.approx.ftz.f32 rc3, dn3;\n\t"
   
   // a[i] * sigmoid(a[i]) = silu(a[i])
   "mul.f32 sg0, %4, rc0;\n\t"
   "mul.f32 sg1, %5, rc1;\n\t"
   "mul.f32 sg2, %6, rc2;\n\t"
   "mul.f32 sg3, %7, rc3;\n\t"
   
   // silu(a[i]) * b[i]
   "mul.f32 %0, sg0, %8;\n\t"
   "mul.f32 %1, sg1, %9;\n\t"
   "mul.f32 %2, sg2, %10;\n\t"
   "mul.f32 %3, sg3, %11;\n\t"
   "}"
   : "=f"(r[0]), "=f"(r[1]), "=f"(r[2]), "=f"(r[3])
   : "f"(a[0]), "f"(a[1]), "f"(a[2]), "f"(a[3]),
     "f"(b[0]), "f"(b[1]), "f"(b[2]), "f"(b[3])
 );
}

// Process 8 elements and convert to half with pipeline
__device__ inline
void silu_mul_cvt_x8(half *h, const float *a, const float *b) {
 asm volatile(
   "{\n\t"
   ".reg .f32 ns0, ns1, ns2, ns3, ns4, ns5, ns6, ns7;\n\t"
   ".reg .f32 ex0, ex1, ex2, ex3, ex4, ex5, ex6, ex7;\n\t"
   ".reg .f32 dn0, dn1, dn2, dn3, dn4, dn5, dn6, dn7;\n\t"
   ".reg .f32 rc0, rc1, rc2, rc3, rc4, rc5, rc6, rc7;\n\t"
   ".reg .f32 sg0, sg1, sg2, sg3, sg4, sg5, sg6, sg7;\n\t"
   ".reg .f32 r0, r1, r2, r3, r4, r5, r6, r7;\n\t"
   ".reg .b16 h0, h1, h2, h3, h4, h5, h6, h7;\n\t"
   
   // Compute -a[i] * log2(e) for all 8
   "mul.f32 ns0, %8, 0FBFB8AA3B;\n\t"
   "mul.f32 ns1, %9, 0FBFB8AA3B;\n\t"
   "mul.f32 ns2, %10, 0FBFB8AA3B;\n\t"
   "mul.f32 ns3, %11, 0FBFB8AA3B;\n\t"
   "mul.f32 ns4, %12, 0FBFB8AA3B;\n\t"
   "mul.f32 ns5, %13, 0FBFB8AA3B;\n\t"
   "mul.f32 ns6, %14, 0FBFB8AA3B;\n\t"
   "mul.f32 ns7, %15, 0FBFB8AA3B;\n\t"
   
   // exp(-a[i]) via ex2
   "ex2.approx.ftz.f32 ex0, ns0;\n\t"
   "ex2.approx.ftz.f32 ex1, ns1;\n\t"
   "ex2.approx.ftz.f32 ex2, ns2;\n\t"
   "ex2.approx.ftz.f32 ex3, ns3;\n\t"
   "ex2.approx.ftz.f32 ex4, ns4;\n\t"
   "ex2.approx.ftz.f32 ex5, ns5;\n\t"
   "ex2.approx.ftz.f32 ex6, ns6;\n\t"
   "ex2.approx.ftz.f32 ex7, ns7;\n\t"
   
   // 1 + exp(-a[i])
   "add.f32 dn0, ex0, 0F3F800000;\n\t"
   "add.f32 dn1, ex1, 0F3F800000;\n\t"
   "add.f32 dn2, ex2, 0F3F800000;\n\t"
   "add.f32 dn3, ex3, 0F3F800000;\n\t"
   "add.f32 dn4, ex4, 0F3F800000;\n\t"
   "add.f32 dn5, ex5, 0F3F800000;\n\t"
   "add.f32 dn6, ex6, 0F3F800000;\n\t"
   "add.f32 dn7, ex7, 0F3F800000;\n\t"
   
   // 1 / (1 + exp(-a[i])) = sigmoid(a[i])
   "rcp.approx.ftz.f32 rc0, dn0;\n\t"
   "rcp.approx.ftz.f32 rc1, dn1;\n\t"
   "rcp.approx.ftz.f32 rc2, dn2;\n\t"
   "rcp.approx.ftz.f32 rc3, dn3;\n\t"
   "rcp.approx.ftz.f32 rc4, dn4;\n\t"
   "rcp.approx.ftz.f32 rc5, dn5;\n\t"
   "rcp.approx.ftz.f32 rc6, dn6;\n\t"
   "rcp.approx.ftz.f32 rc7, dn7;\n\t"
   
   // a[i] * sigmoid(a[i]) = silu(a[i])
   "mul.f32 sg0, %8, rc0;\n\t"
   "mul.f32 sg1, %9, rc1;\n\t"
   "mul.f32 sg2, %10, rc2;\n\t"
   "mul.f32 sg3, %11, rc3;\n\t"
   "mul.f32 sg4, %12, rc4;\n\t"
   "mul.f32 sg5, %13, rc5;\n\t"
   "mul.f32 sg6, %14, rc6;\n\t"
   "mul.f32 sg7, %15, rc7;\n\t"
   
   // silu(a[i]) * b[i]
   "mul.f32 r0, sg0, %16;\n\t"
   "mul.f32 r1, sg1, %17;\n\t"
   "mul.f32 r2, sg2, %18;\n\t"
   "mul.f32 r3, sg3, %19;\n\t"
   "mul.f32 r4, sg4, %20;\n\t"
   "mul.f32 r5, sg5, %21;\n\t"
   "mul.f32 r6, sg6, %22;\n\t"
   "mul.f32 r7, sg7, %23;\n\t"
   
   // Convert to half
   "cvt.rn.f16.f32 h0, r0;\n\t"
   "cvt.rn.f16.f32 h1, r1;\n\t"
   "cvt.rn.f16.f32 h2, r2;\n\t"
   "cvt.rn.f16.f32 h3, r3;\n\t"
   "cvt.rn.f16.f32 h4, r4;\n\t"
   "cvt.rn.f16.f32 h5, r5;\n\t"
   "cvt.rn.f16.f32 h6, r6;\n\t"
   "cvt.rn.f16.f32 h7, r7;\n\t"
   
   "mov.b16 %0, h0;\n\t"
   "mov.b16 %1, h1;\n\t"
   "mov.b16 %2, h2;\n\t"
   "mov.b16 %3, h3;\n\t"
   "mov.b16 %4, h4;\n\t"
   "mov.b16 %5, h5;\n\t"
   "mov.b16 %6, h6;\n\t"
   "mov.b16 %7, h7;\n\t"
   "}"
   : "=h"(*(unsigned short*)&h[0]), "=h"(*(unsigned short*)&h[1]),
     "=h"(*(unsigned short*)&h[2]), "=h"(*(unsigned short*)&h[3]),
     "=h"(*(unsigned short*)&h[4]), "=h"(*(unsigned short*)&h[5]),
     "=h"(*(unsigned short*)&h[6]), "=h"(*(unsigned short*)&h[7])
   : "f"(a[0]), "f"(a[1]), "f"(a[2]), "f"(a[3]),
     "f"(a[4]), "f"(a[5]), "f"(a[6]), "f"(a[7]),
     "f"(b[0]), "f"(b[1]), "f"(b[2]), "f"(b[3]),
     "f"(b[4]), "f"(b[5]), "f"(b[6]), "f"(b[7])
 );
}


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);
}
"""




CUDA_SRC_DUAL_GEMM = r"""
template <
 int K,
 int BLOCK_M,
 int BLOCK_N,
 int BLOCK_K,
 int NUM_STAGES
>
__global__
__launch_bounds__(BLOCK_M + 2 * WARP_SIZE, 1)
void dual_gemm_kernel(
 const __grid_constant__ CUtensorMap A_tmap,
 const __grid_constant__ CUtensorMap B1_tmap,
 const __grid_constant__ CUtensorMap B2_tmap,
 const char *SFA_ptr,
 const char *SFB1_ptr,
 const char *SFB2_ptr,
 half *C_ptr,
 int M, int N
) {
 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;


 constexpr int NUM_WARPS = BLOCK_M / WARP_SIZE + 2;


 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;
 // always copy 128xBLOCK_K/16, because the tcgen05.cp that copy from smem to tmem
 // has work work with fixed (32, 16) 8 bit scale, which is 4 tmem columns, each column has 32 rows
 // and each column value has 4 8-bit scale factor as draw in https://docs.nvidia.com/cuda/parallel-thread-execution/#tcgen05-mma-scale-factor-a-layout-4x
 // The scale factor need to layout in that way, so our SFA and SFB both need to be 128 rows,
 // so each MMA work on (128, 4) scale factors which can be (32, 4, 4) which is (32, 16).
 // so no matter what the block_m and block_n are we need the 128 * (BLOCK_K / 16), the BLOCK_K / 16
 // includes the scale factors for all MMA in the block (e.g. BLOCK_K=256, MMA_K = 64, so it has shape
 // [128, 4 (4 MMA), 32, 4, 4])
 constexpr int SFA_size = 128 * BLOCK_K / 16;
 constexpr int SFB_size = 128 * BLOCK_K / 16;
 // For dual GEMM: A, B1, B2, SFA, SFB1, SFB2
 constexpr int STAGE_SIZE = A_size + B_size * 2 + SFA_size + SFB_size * 2;


 // 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;


 // TMEM layout for dual GEMM:
 // - Acc1: columns 0 to BLOCK_N-1 (d_tmem = 0)
 // - SFA: columns BLOCK_N onwards
 // - SFB1, SFB2: after SFA
 // - Acc2: columns BLOCK_N*2 onwards (d_tmem = BLOCK_N*2)
 constexpr int SFA_tmem = BLOCK_N;
 // The columns of SFA_tmem is 4 * 4, the first 4 is 4 tmem columns
 // (128, 64 / 16) = (128, 4) per mma = (32, 4, 4) as in https://docs.nvidia.com/cuda/parallel-thread-execution/#tcgen05-mma-scale-factor-a-layout-4x
 // the second 4 mmas per block_k.
 constexpr int SFB1_tmem = SFA_tmem + 4 * (BLOCK_K / MMA_K);
 constexpr int SFB2_tmem = SFB1_tmem + 4 * (BLOCK_K / MMA_K);


 mbarrier_init_pred(tma_mbar_addr + lane_id * 8, 1, (warp_id == 0) & (lane_id < NUM_STAGES * 2 + 1));
 // We actually not need BLOCK_N * 4 TMEM, but if we use BLOCK_N * 3 here it will error out,
 // so somehow TMEM require it to be power of 2?
 if (warp_id == 1) {
   // TMEM allocation MUST be power of 2! Tested: 384 fails, 448 fails, 512 works
   // For BLOCK_N=64: need 192 cols (Acc2 ends at 191), so 256 works
   // For BLOCK_N=128: need 384 cols (Acc2 ends at 383), so 512 needed
   asm volatile("tcgen05.alloc.cta_group::1.sync.aligned.shared::cta.b32 [%0], %1;" :: "r"(smem), "r"(BLOCK_N * 4));
 }
 __syncthreads();


 const int num_iters = K / BLOCK_K;


 if (warp_id == 4 && elect_sync()) {
   // TMA warp - cache eviction policy (optimized for M < N benchmark cases)
   // A tile reuse = N/BLOCK_N (high when N > M) → keep in cache longest
   // B tile reuse = M/BLOCK_M (low when M < N) → evict first
   constexpr uint64_t cache_A = EVICT_LAST;
   constexpr uint64_t cache_B = EVICT_FIRST;


   // K / 16 / 4: 16 elements share same scale factor, 4 fp8 packed to 1 value
   // it's the total number of (4 scale factors pack to one 32 bit) scale factors along K.
   constexpr int rest_k = K / 16 / 4;
   // layout of SFA IS [M/128, rest_k, 32, 4, 4]
   // so SFA_base:
   // Move ptr to the current block's position for the SFA, SFB - not including
   // the k iter offset yet, which will be added during the loop along k.
   const char *SFA_base = SFA_ptr + (off_m / 128) * rest_k * 512;
   // SFB is [N/128, rest_k, 32, 4, 4]
   // When block_n = 64, because we are always using 128 here, whcih is required by
   // tcgen05, then block 0 and block 1 both load the entire 128 n rows, and block 0
   // use the first half and block 1 use the second half.
   const char *SFB1_base = SFB1_ptr + (off_n / 128) * rest_k * 512;
   const char *SFB2_base = SFB2_ptr + (off_n / 128) * rest_k * 512;


   auto issue_tma = [&](int iter_k, int stage_id) {
     const int mbar_addr = tma_mbar_addr + stage_id * 8;
     const int stage_smem = smem + stage_id * STAGE_SIZE;
     const int A_smem = stage_smem;
     const int B1_smem = stage_smem + A_size;
     const int B2_smem = stage_smem + A_size + B_size;
     const int SFA_smem = stage_smem + A_size + B_size * 2;
     const int SFB1_smem = stage_smem + A_size + B_size * 2 + SFA_size;
     const int SFB2_smem = stage_smem + A_size + B_size * 2 + SFA_size + SFB_size;


     // issue TMA for A, B1, B2
     const int k_idx = iter_k * (BLOCK_K / 256);
     tma_3d_gmem2smem(A_smem, &A_tmap, 0, off_m, k_idx, mbar_addr, cache_A);
     tma_3d_gmem2smem(B1_smem, &B1_tmap, 0, off_n, k_idx, mbar_addr, cache_B);
     tma_3d_gmem2smem(B2_smem, &B2_tmap, 0, off_n, k_idx, mbar_addr, cache_B);


     // Scale factor TMA with pre-computed base
     const char *SFA_src = SFA_base + iter_k * (BLOCK_K / 64) * 512;
     const char *SFB1_src = SFB1_base + iter_k * (BLOCK_K / 64) * 512;
     const char *SFB2_src = SFB2_base + iter_k * (BLOCK_K / 64) * 512;
     tma_gmem2smem(SFA_smem, SFA_src, SFA_size, mbar_addr, cache_A);
     tma_gmem2smem(SFB1_smem, SFB1_src, SFB_size, mbar_addr, cache_B);
     tma_gmem2smem(SFB2_smem, SFB2_src, SFB_size, mbar_addr, cache_B);


     asm volatile("mbarrier.arrive.expect_tx.release.cta.shared::cta.b64 _, [%0], %1;"
                 :: "r"(mbar_addr), "r"(STAGE_SIZE) : "memory");
   };


   // issue TMA without waiting for MMA
   for (int iter_k = 0; iter_k < NUM_STAGES; iter_k++)
     issue_tma(iter_k, iter_k);


   for (int iter_k = NUM_STAGES; iter_k < num_iters; iter_k++) {
     const int stage_id = iter_k % NUM_STAGES;
     const int mma_phase = (iter_k / NUM_STAGES - 1) % 2;
     mbarrier_wait(mma_mbar_addr + stage_id * 8, mma_phase);
     issue_tma(iter_k, stage_id);
   }
 }
 else if (warp_id == 5 && elect_sync()) {
   // MMA warp - performs dual GEMM
   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;
     const int tma_phase = (iter_k / NUM_STAGES) % 2;
     mbarrier_wait(tma_mbar_addr + stage_id * 8, tma_phase);


     const int A_smem = smem + stage_id * STAGE_SIZE;
     const int B1_smem = A_smem + A_size;
     const int B2_smem = B1_smem + B_size;
     const int SFA_smem = B2_smem + B_size;
     const int SFB1_smem = SFA_smem + SFA_size;
     const int SFB2_smem = SFB1_smem + SFB_size;


     // shared memory descriptors for A and B (128-byte swizzling)
     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 for scale factors
     auto make_desc_SF = [](int addr) -> uint64_t {
       const int SBO = 8 * 16;
       return desc_encode(addr) | (desc_encode(SBO) << 32ULL) | (1ULL << 46ULL);
     };


     constexpr uint64_t SF_desc = make_desc_SF(0);
     const uint64_t SFA_desc = SF_desc + ((uint64_t)SFA_smem >> 4ULL);
     const uint64_t SFB1_desc = SF_desc + ((uint64_t)SFB1_smem >> 4ULL);
     const uint64_t SFB2_desc = SF_desc + ((uint64_t)SFB2_smem >> 4ULL);


     constexpr int d_tmem_acc2 = BLOCK_N * 2;
     constexpr uint64_t SF_stride = 512ULL >> 4ULL;


     const int scale_A_base = SFA_tmem + (bid_m % (128 / BLOCK_M)) * (BLOCK_M / 32);
     const int scale_B1_base = SFB1_tmem + (bid_n % (128 / BLOCK_N)) * (BLOCK_N / 32);
     const int scale_B2_base = SFB2_tmem + (bid_n % (128 / BLOCK_N)) * (BLOCK_N / 32);


     const uint64_t a_desc_base = make_desc_AB(A_smem);
     const uint64_t b1_desc_base = make_desc_AB(B1_smem);
     const uint64_t b2_desc_base = make_desc_AB(B2_smem);


     // Prefetch first scale factor
     tcgen05_cp_nvfp4(SFA_tmem, SFA_desc);
     tcgen05_cp_nvfp4(SFB1_tmem, SFB1_desc);
     tcgen05_cp_nvfp4(SFB2_tmem, SFB2_desc);


     constexpr int A_k256_stride = BLOCK_M * 128;
     constexpr int B_k256_stride = BLOCK_N * 128;
     constexpr int NUM_K_ITERS = BLOCK_K / MMA_K;


     #pragma unroll
     for (int k = 0; k < NUM_K_ITERS; k++) {
       const int k1 = k >> 2;
       const int k2 = k & 3;


       // Prefetch next scale (only if within bounds)
       if (k + 1 < NUM_K_ITERS) {
         const int next_k = k + 1;
         tcgen05_cp_nvfp4(SFA_tmem + (next_k << 2), SFA_desc + next_k * SF_stride);
         tcgen05_cp_nvfp4(SFB1_tmem + (next_k << 2), SFB1_desc + next_k * SF_stride);
         tcgen05_cp_nvfp4(SFB2_tmem + (next_k << 2), SFB2_desc + next_k * SF_stride);
       }


       const int ab_off = k1 * A_k256_stride + (k2 << 5);
       const int bb_off = k1 * B_k256_stride + (k2 << 5);
       const uint64_t a_desc = a_desc_base + (ab_off >> 4);
       const uint64_t b1_desc = b1_desc_base + (bb_off >> 4);
       const uint64_t b2_desc = b2_desc_base + (bb_off >> 4);


       const int scale_A = scale_A_base + (k << 2);
       const int scale_B1 = scale_B1_base + (k << 2);
       const int scale_B2 = scale_B2_base + (k << 2);


       const int enable_d = (k == 0) ? iter_k : 1;
       tcgen05_mma_nvfp4(0, a_desc, b1_desc, i_desc, scale_A, scale_B1, enable_d);
       tcgen05_mma_nvfp4(d_tmem_acc2, a_desc, b2_desc, i_desc, scale_A, scale_B2, enable_d);
     }


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


   asm volatile("tcgen05.commit.cta_group::1.mbarrier::arrive::one.shared::cluster.b64 [%0];"
               :: "r"(mainloop_mbar_addr) : "memory");
 }
 else if (tid < BLOCK_M) {
   // epilogue warps - read both accumulators and apply silu(Acc1) * Acc2
   mbarrier_wait(mainloop_mbar_addr, 0);
   asm volatile("tcgen05.fence::after_thread_sync;");


   constexpr int WIDTH = std::min(BLOCK_N, 64);


   for (int n = 0; n < BLOCK_N / WIDTH; n++) {
     float tmp1[WIDTH];
     float tmp2[WIDTH];


     // Issue BOTH TMEM reads first to overlap latency
     if constexpr (WIDTH == 128) tcgen05_ld_32x32bx128(tmp1, warp_id * 32, n * WIDTH);
     if constexpr (WIDTH == 64) tcgen05_ld_32x32bx64(tmp1, warp_id * 32, n * WIDTH);
     if constexpr (WIDTH == 32) tcgen05_ld_32x32bx32(tmp1, warp_id * 32, n * WIDTH);
     if constexpr (WIDTH == 128) tcgen05_ld_32x32bx128(tmp2, warp_id * 32, n * WIDTH + BLOCK_N * 2);
     if constexpr (WIDTH == 64) tcgen05_ld_32x32bx64(tmp2, warp_id * 32, n * WIDTH + BLOCK_N * 2);
     if constexpr (WIDTH == 32) tcgen05_ld_32x32bx32(tmp2, warp_id * 32, n * WIDTH + BLOCK_N * 2);
     asm volatile("tcgen05.wait::ld.sync.aligned;");


     const int col = off_m + tid;
     const int base_row = off_n + n * WIDTH;


     // Process 8 elements at a time for maximum ILP
     // Based on Modular's Blackwell optimization technique
     #pragma unroll
     for (int i = 0; i < WIDTH; i += 8) {
       half h[8];
       silu_mul_cvt_x8(h, &tmp1[i], &tmp2[i]);
       
       // Store all 8 results
       C_ptr[(base_row + i) * M + col] = h[0];
       C_ptr[(base_row + i + 1) * M + col] = h[1];
       C_ptr[(base_row + i + 2) * M + col] = h[2];
       C_ptr[(base_row + i + 3) * M + col] = h[3];
       C_ptr[(base_row + i + 4) * M + col] = h[4];
       C_ptr[(base_row + i + 5) * M + col] = h[5];
       C_ptr[(base_row + i + 6) * M + col] = h[6];
       C_ptr[(base_row + i + 7) * M + col] = h[7];
     }
   }


   asm volatile("bar.sync 1, %0;" :: "r"(BLOCK_M) : "memory");
   if (warp_id == 0)
     asm volatile("tcgen05.dealloc.cta_group::1.sync.aligned.b32 %0, %1;" :: "r"(0), "r"(BLOCK_N * 4));
 }
}


template <
 int K,
 int BLOCK_M,
 int BLOCK_N,
 int BLOCK_K,
 int NUM_STAGES
>
at::Tensor dual_gemm_launch(
 const at::Tensor& A,
 const at::Tensor& B1,
 const at::Tensor& B2,
 const at::Tensor& SFA,
 const at::Tensor& SFB1,
 const at::Tensor& SFB2,
       at::Tensor& C
) {
 static_assert(BLOCK_K % 256 == 0);


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


 auto A_ptr   = reinterpret_cast<const char *>(A.data_ptr());
 auto B1_ptr  = reinterpret_cast<const char *>(B1.data_ptr());
 auto B2_ptr  = reinterpret_cast<const char *>(B2.data_ptr());
 auto SFA_ptr = reinterpret_cast<const char *>(SFA.data_ptr());
 auto SFB1_ptr = reinterpret_cast<const char *>(SFB1.data_ptr());
 auto SFB2_ptr = reinterpret_cast<const char *>(SFB2.data_ptr());
 auto C_ptr   = reinterpret_cast<half *>(C.data_ptr());


 CUtensorMap A_tmap, B1_tmap, B2_tmap;
 init_AB_tmap(&A_tmap, A_ptr, M, K, BLOCK_M, BLOCK_K);
 init_AB_tmap(&B1_tmap, B1_ptr, N, K, BLOCK_N, BLOCK_K);
 init_AB_tmap(&B2_tmap, B2_ptr, N, K, BLOCK_N, BLOCK_K);


 int grid = (M / BLOCK_M) * (N / BLOCK_N);
 int tb_size = BLOCK_M + 2 * WARP_SIZE;
 int AB_size = (BLOCK_M + BLOCK_N * 2) * (BLOCK_K / 2);
 int SFAB_size = 128 * (BLOCK_K / 16) * 3;
 int smem_size = (AB_size + SFAB_size) * NUM_STAGES;


 auto this_kernel = dual_gemm_kernel<K, BLOCK_M, BLOCK_N, BLOCK_K, NUM_STAGES>;
 if (smem_size > 48'000)
   cudaFuncSetAttribute(this_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, smem_size);
 this_kernel<<<grid, tb_size, smem_size>>>(A_tmap, B1_tmap, B2_tmap, SFA_ptr, SFB1_ptr, SFB2_ptr, C_ptr, M, N);


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


at::Tensor dual_gemm(
 const at::Tensor& A,
 const at::Tensor& B1,
 const at::Tensor& B2,
 const at::Tensor& SFA,
 const at::Tensor& SFB1,
 const at::Tensor& SFB2,
       at::Tensor& C
) {
 const int K = A.size(1) * 2;
 const int M = A.size(0);
 const int N = B1.size(0);


#define LAUNCH_EXACT(K_, M_, N_, BLOCK_M, BLOCK_N, BLOCK_K, NUM_STAGES) \
 else if (K == K_ && M == M_ && N == N_) C = dual_gemm_launch<K_, BLOCK_M, BLOCK_N, BLOCK_K, NUM_STAGES>(A, B1, B2, SFA, SFB1, SFB2, C);


#define LAUNCH(K_, BLOCK_M, BLOCK_N, BLOCK_K, NUM_STAGES) \
 else if (K == K_) C = dual_gemm_launch<K_, BLOCK_M, BLOCK_N, BLOCK_K, NUM_STAGES>(A, B1, B2, SFA, SFB1, SFB2, C);


 if (false) {}
 LAUNCH_EXACT(7168, 256, 4096, 128, 64, 256, 5)
 LAUNCH_EXACT(7168, 512, 4096, 128, 128, 256, 4)
 LAUNCH_EXACT(4096, 256, 3072, 128, 64, 256, 5)
 LAUNCH_EXACT(7168, 512, 3072, 128, 128, 256, 4)
 LAUNCH(16384, 128, 64, 256, 5)
 LAUNCH( 7168, 128, 64, 256, 5)
 LAUNCH( 4096, 128, 64, 256, 5)
 LAUNCH( 3072, 128, 64, 256, 5)
 LAUNCH( 2048, 128, 64, 256, 5)
 LAUNCH( 2304, 128, 64, 256, 5)
 LAUNCH( 1536, 128, 64, 256, 5)
 LAUNCH( 1024, 128, 64, 256, 5)
 LAUNCH(  512, 128, 64, 256, 5)
 LAUNCH(  256, 128, 64, 256, 5)


#undef LAUNCH_EXACT
#undef LAUNCH


 return C;
}


TORCH_LIBRARY(my_module_dual_gemm_aggressive_opt_v1, m) {
 m.def("dual_gemm(Tensor A, Tensor B1, Tensor B2, Tensor SFA, Tensor SFB1, Tensor SFB2, Tensor(a!) C) -> Tensor");
 m.impl("dual_gemm", &dual_gemm);
}
"""




load_inline(
   "dual_gemm_cuda_aggressive_opt_v1",
   cpp_sources="",
   cuda_sources=CUDA_SRC_COMMON + CUDA_SRC_DUAL_GEMM,
   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",
   ],
   extra_ldflags=["-lcuda"],
)


dual_gemm_cuda = torch.ops.my_module_dual_gemm_aggressive_opt_v1.dual_gemm




def custom_kernel(data: input_t) -> output_t:
   # data contains: A, B1, B2, _, _, _, SFA, SFB1, SFB2, C
   # Dual GEMM computes: C = silu(A @ B1) * (A @ B2)
   return dual_gemm_cuda(data[0], data[1], data[2], data[6], data[7], data[8], data[9])



scrolls · 923 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 382312.

⋯ 226 unchanged lines
__device__ inline void tcgen05_ld_32x32bx128(float *tmp, int row, int col) { tcgen05_ld_128regs<SHAPE::_32x32b, NUM::x128>(tmp, row, col); }
- // Fast silu using PTX SFU with FMA for better pipelining
+ // Based on Modular's Blackwell optimization blog
+ // Fast silu using packed f32x2 operations for better ILP
// silu(x) = x * sigmoid(x) = x / (1 + exp(-x)) = x / (1 + 2^(-x*log2e))
__device__ inline
float silu(float x) {
⋯ 1 unchanged lines
asm volatile(
"{\n\t"
".reg .f32 neg_scaled, exp_val, denom, rcp_val;\n\t"
- "mul.f32 neg_scaled, %1, 0FBFB8AA3B;\n\t" // -x * log2(e) in one instruction
- "ex2.approx.ftz.f32 exp_val, neg_scaled;\n\t" // exp(-x)
+ "mul.f32 neg_scaled, %1, 0FBFB8AA3B;\n\t" // -x * log2(e)
+ "ex2.approx.ftz.f32 exp_val, neg_scaled;\n\t" // exp(-x) via 2^(-x*log2e)
"add.f32 denom, exp_val, 0F3F800000;\n\t" // 1 + exp(-x)
- "rcp.approx.ftz.f32 rcp_val, denom;\n\t" // 1/(1+exp(-x))
- "mul.f32 %0, %1, rcp_val;\n\t" // x * sigmoid(x)
+ "rcp.approx.ftz.f32 rcp_val, denom;\n\t" // 1/(1+exp(-x)) = sigmoid(x)
+ "mul.f32 %0, %1, rcp_val;\n\t" // x * sigmoid(x) = silu(x)
"}"
: "=f"(result)
: "f"(x)
⋯ 1 unchanged lines
return result;
}
+ // Process 4 elements at once for better instruction-level parallelism
+ // Based on Modular's packed f32x2 optimization technique
+ __device__ inline
+ void silu_mul_x4(float *r, const float *a, const float *b) {
+ asm volatile(
+ "{\n\t"
+ // Registers for 4 parallel silu computations
+ ".reg .f32 ns0, ns1, ns2, ns3;\n\t"
+ ".reg .f32 ex0, ex1, ex2, ex3;\n\t"
+ ".reg .f32 dn0, dn1, dn2, dn3;\n\t"
+ ".reg .f32 rc0, rc1, rc2, rc3;\n\t"
+ ".reg .f32 sg0, sg1, sg2, sg3;\n\t"
+
+ // Compute -a[i] * log2(e) for all 4
+ "mul.f32 ns0, %4, 0FBFB8AA3B;\n\t"
+ "mul.f32 ns1, %5, 0FBFB8AA3B;\n\t"
+ "mul.f32 ns2, %6, 0FBFB8AA3B;\n\t"
+ "mul.f32 ns3, %7, 0FBFB8AA3B;\n\t"
+
+ // exp(-a[i]) via ex2
+ "ex2.approx.ftz.f32 ex0, ns0;\n\t"
+ "ex2.approx.ftz.f32 ex1, ns1;\n\t"
+ "ex2.approx.ftz.f32 ex2, ns2;\n\t"
+ "ex2.approx.ftz.f32 ex3, ns3;\n\t"
+
+ // 1 + exp(-a[i])
+ "add.f32 dn0, ex0, 0F3F800000;\n\t"
+ "add.f32 dn1, ex1, 0F3F800000;\n\t"
+ "add.f32 dn2, ex2, 0F3F800000;\n\t"
+ "add.f32 dn3, ex3, 0F3F800000;\n\t"
+
+ // 1 / (1 + exp(-a[i])) = sigmoid(a[i])
+ "rcp.approx.ftz.f32 rc0, dn0;\n\t"
+ "rcp.approx.ftz.f32 rc1, dn1;\n\t"
+ "rcp.approx.ftz.f32 rc2, dn2;\n\t"
+ "rcp.approx.ftz.f32 rc3, dn3;\n\t"
+
+ // a[i] * sigmoid(a[i]) = silu(a[i])
+ "mul.f32 sg0, %4, rc0;\n\t"
+ "mul.f32 sg1, %5, rc1;\n\t"
+ "mul.f32 sg2, %6, rc2;\n\t"
+ "mul.f32 sg3, %7, rc3;\n\t"
+
+ // silu(a[i]) * b[i]
+ "mul.f32 %0, sg0, %8;\n\t"
+ "mul.f32 %1, sg1, %9;\n\t"
+ "mul.f32 %2, sg2, %10;\n\t"
+ "mul.f32 %3, sg3, %11;\n\t"
+ "}"
+ : "=f"(r[0]), "=f"(r[1]), "=f"(r[2]), "=f"(r[3])
+ : "f"(a[0]), "f"(a[1]), "f"(a[2]), "f"(a[3]),
+ "f"(b[0]), "f"(b[1]), "f"(b[2]), "f"(b[3])
+ );
+ }
+ // Process 8 elements and convert to half with pipeline
+ __device__ inline
+ void silu_mul_cvt_x8(half *h, const float *a, const float *b) {
+ asm volatile(
+ "{\n\t"
+ ".reg .f32 ns0, ns1, ns2, ns3, ns4, ns5, ns6, ns7;\n\t"
+ ".reg .f32 ex0, ex1, ex2, ex3, ex4, ex5, ex6, ex7;\n\t"
+ ".reg .f32 dn0, dn1, dn2, dn3, dn4, dn5, dn6, dn7;\n\t"
+ ".reg .f32 rc0, rc1, rc2, rc3, rc4, rc5, rc6, rc7;\n\t"
+ ".reg .f32 sg0, sg1, sg2, sg3, sg4, sg5, sg6, sg7;\n\t"
+ ".reg .f32 r0, r1, r2, r3, r4, r5, r6, r7;\n\t"
+ ".reg .b16 h0, h1, h2, h3, h4, h5, h6, h7;\n\t"
+
+ // Compute -a[i] * log2(e) for all 8
+ "mul.f32 ns0, %8, 0FBFB8AA3B;\n\t"
+ "mul.f32 ns1, %9, 0FBFB8AA3B;\n\t"
+ "mul.f32 ns2, %10, 0FBFB8AA3B;\n\t"
+ "mul.f32 ns3, %11, 0FBFB8AA3B;\n\t"
+ "mul.f32 ns4, %12, 0FBFB8AA3B;\n\t"
+ "mul.f32 ns5, %13, 0FBFB8AA3B;\n\t"
+ "mul.f32 ns6, %14, 0FBFB8AA3B;\n\t"
+ "mul.f32 ns7, %15, 0FBFB8AA3B;\n\t"
+
+ // exp(-a[i]) via ex2
+ "ex2.approx.ftz.f32 ex0, ns0;\n\t"
+ "ex2.approx.ftz.f32 ex1, ns1;\n\t"
+ "ex2.approx.ftz.f32 ex2, ns2;\n\t"
+ "ex2.approx.ftz.f32 ex3, ns3;\n\t"
+ "ex2.approx.ftz.f32 ex4, ns4;\n\t"
+ "ex2.approx.ftz.f32 ex5, ns5;\n\t"
+ "ex2.approx.ftz.f32 ex6, ns6;\n\t"
+ "ex2.approx.ftz.f32 ex7, ns7;\n\t"
+
+ // 1 + exp(-a[i])
+ "add.f32 dn0, ex0, 0F3F800000;\n\t"
+ "add.f32 dn1, ex1, 0F3F800000;\n\t"
+ "add.f32 dn2, ex2, 0F3F800000;\n\t"
+ "add.f32 dn3, ex3, 0F3F800000;\n\t"
+ "add.f32 dn4, ex4, 0F3F800000;\n\t"
+ "add.f32 dn5, ex5, 0F3F800000;\n\t"
+ "add.f32 dn6, ex6, 0F3F800000;\n\t"
+ "add.f32 dn7, ex7, 0F3F800000;\n\t"
+
+ // 1 / (1 + exp(-a[i])) = sigmoid(a[i])
+ "rcp.approx.ftz.f32 rc0, dn0;\n\t"
+ "rcp.approx.ftz.f32 rc1, dn1;\n\t"
+ "rcp.approx.ftz.f32 rc2, dn2;\n\t"
+ "rcp.approx.ftz.f32 rc3, dn3;\n\t"
+ "rcp.approx.ftz.f32 rc4, dn4;\n\t"
+ "rcp.approx.ftz.f32 rc5, dn5;\n\t"
+ "rcp.approx.ftz.f32 rc6, dn6;\n\t"
+ "rcp.approx.ftz.f32 rc7, dn7;\n\t"
+
+ // a[i] * sigmoid(a[i]) = silu(a[i])
+ "mul.f32 sg0, %8, rc0;\n\t"
+ "mul.f32 sg1, %9, rc1;\n\t"
+ "mul.f32 sg2, %10, rc2;\n\t"
+ "mul.f32 sg3, %11, rc3;\n\t"
+ "mul.f32 sg4, %12, rc4;\n\t"
+ "mul.f32 sg5, %13, rc5;\n\t"
+ "mul.f32 sg6, %14, rc6;\n\t"
+ "mul.f32 sg7, %15, rc7;\n\t"
+
+ // silu(a[i]) * b[i]
+ "mul.f32 r0, sg0, %16;\n\t"
+ "mul.f32 r1, sg1, %17;\n\t"
+ "mul.f32 r2, sg2, %18;\n\t"
+ "mul.f32 r3, sg3, %19;\n\t"
+ "mul.f32 r4, sg4, %20;\n\t"
+ "mul.f32 r5, sg5, %21;\n\t"
+ "mul.f32 r6, sg6, %22;\n\t"
+ "mul.f32 r7, sg7, %23;\n\t"
+
+ // Convert to half
+ "cvt.rn.f16.f32 h0, r0;\n\t"
+ "cvt.rn.f16.f32 h1, r1;\n\t"
+ "cvt.rn.f16.f32 h2, r2;\n\t"
+ "cvt.rn.f16.f32 h3, r3;\n\t"
+ "cvt.rn.f16.f32 h4, r4;\n\t"
+ "cvt.rn.f16.f32 h5, r5;\n\t"
+ "cvt.rn.f16.f32 h6, r6;\n\t"
+ "cvt.rn.f16.f32 h7, r7;\n\t"
+
+ "mov.b16 %0, h0;\n\t"
+ "mov.b16 %1, h1;\n\t"
+ "mov.b16 %2, h2;\n\t"
+ "mov.b16 %3, h3;\n\t"
+ "mov.b16 %4, h4;\n\t"
+ "mov.b16 %5, h5;\n\t"
+ "mov.b16 %6, h6;\n\t"
+ "mov.b16 %7, h7;\n\t"
+ "}"
+ : "=h"(*(unsigned short*)&h[0]), "=h"(*(unsigned short*)&h[1]),
+ "=h"(*(unsigned short*)&h[2]), "=h"(*(unsigned short*)&h[3]),
+ "=h"(*(unsigned short*)&h[4]), "=h"(*(unsigned short*)&h[5]),
+ "=h"(*(unsigned short*)&h[6]), "=h"(*(unsigned short*)&h[7])
+ : "f"(a[0]), "f"(a[1]), "f"(a[2]), "f"(a[3]),
+ "f"(a[4]), "f"(a[5]), "f"(a[6]), "f"(a[7]),
+ "f"(b[0]), "f"(b[1]), "f"(b[2]), "f"(b[3]),
+ "f"(b[4]), "f"(b[5]), "f"(b[6]), "f"(b[7])
+ );
+ }
+
+
void check_cu(CUresult err) {
if (err == CUDA_SUCCESS) return;
const char *error_msg_ptr;
⋯ 339 unchanged lines
const int base_row = off_n + n * WIDTH;
+ // Process 8 elements at a time for maximum ILP
+ // Based on Modular's Blackwell optimization technique
#pragma unroll
- for (int i = 0; i < WIDTH; i += 2) {
- float r0 = silu(tmp1[i]) * tmp2[i];
- float r1 = silu(tmp1[i+1]) * tmp2[i+1];
- C_ptr[(base_row + i) * M + col] = __float2half(r0);
- C_ptr[(base_row + i + 1) * M + col] = __float2half(r1);
+ for (int i = 0; i < WIDTH; i += 8) {
+ half h[8];
+ silu_mul_cvt_x8(h, &tmp1[i], &tmp2[i]);
+
+ // Store all 8 results
+ C_ptr[(base_row + i) * M + col] = h[0];
+ C_ptr[(base_row + i + 1) * M + col] = h[1];
+ C_ptr[(base_row + i + 2) * M + col] = h[2];
+ C_ptr[(base_row + i + 3) * M + col] = h[3];
+ C_ptr[(base_row + i + 4) * M + col] = h[4];
+ C_ptr[(base_row + i + 5) * M + col] = h[5];
+ C_ptr[(base_row + i + 6) * M + col] = h[6];
+ C_ptr[(base_row + i + 7) * M + col] = h[7];
}
}
⋯ 107 unchanged lines
}
- TORCH_LIBRARY(my_module_dual_gemm, m) {
+ TORCH_LIBRARY(my_module_dual_gemm_aggressive_opt_v1, m) {
m.def("dual_gemm(Tensor A, Tensor B1, Tensor B2, Tensor SFA, Tensor SFB1, Tensor SFB2, Tensor(a!) C) -> Tensor");
m.impl("dual_gemm", &dual_gemm);
}
⋯ 3 unchanged lines
load_inline(
- "dual_gemm_cuda_best_v1",
+ "dual_gemm_cuda_aggressive_opt_v1",
cpp_sources="",
cuda_sources=CUDA_SRC_COMMON + CUDA_SRC_DUAL_GEMM,
verbose=True,
⋯ 5 unchanged lines
"--use_fast_math",
"--expt-relaxed-constexpr",
"--relocatable-device-code=false",
- "-lineinfo",
- "-Xptxas=-v",
],
extra_ldflags=["-lcuda"],
)
- dual_gemm_cuda = torch.ops.my_module_dual_gemm.dual_gemm
+ dual_gemm_cuda = torch.ops.my_module_dual_gemm_aggressive_opt_v1.dual_gemm
scrolls · 254 diff lines total

Best evidence level for this revision: reported

JSON