import torch import torch.nn as nn from torch.utils.cpp_extension import load_inline cuda_source = r""" #include #include #include #include #define BLOCK_M 128 #define BLOCK_N 128 #define BLOCK_K 32 #define MMA_M 16 #define MMA_N 8 #define MMA_K 16 #define BLOCK_ROW_WARPS 2 #define BLOCK_COL_WARPS 2 #define WARP_M 64 #define WARP_N 64 #define WARP_ROW_TILES 4 #define WARP_COL_TILES 8 #define THREADS_PER_BLOCK 128 __global__ void fp8_gemm_kernel( const __nv_fp8_e4m3* __restrict__ A, const __nv_fp8_e4m3* __restrict__ B, __nv_bfloat16* __restrict__ C, int M, int N, int K ) { const int block_m = blockIdx.y * BLOCK_M; const int block_n = blockIdx.x * BLOCK_N; const int warp_id = threadIdx.x / 32; const int lane_id = threadIdx.x % 32; const int warp_row = warp_id / BLOCK_COL_WARPS; const int warp_col = warp_id % BLOCK_COL_WARPS; const int warp_m = warp_row * WARP_M; const int warp_n = warp_col * WARP_N; const __nv_fp8_e4m3* A_block = A + block_m * K; const __nv_fp8_e4m3* B_block = B + block_n * K; extern __shared__ char smem[]; __nv_fp8_e4m3* As = reinterpret_cast<__nv_fp8_e4m3*>(smem); __nv_fp8_e4m3* Bs = reinterpret_cast<__nv_fp8_e4m3*>(smem + BLOCK_M * BLOCK_K * 2 * sizeof(__nv_fp8_e4m3)); float accum[WARP_ROW_TILES][WARP_COL_TILES][2][2]; for (int i = 0; i < WARP_ROW_TILES; ++i) { for (int j = 0; j < WARP_COL_TILES; ++j) { for (int ii = 0; ii < 2; ++ii) { for (int jj = 0; jj < 2; ++jj) { accum[i][j][ii][jj] = 0.0f; } } } } uint32_t a_frag[WARP_ROW_TILES][4]; uint32_t b_frag[WARP_COL_TILES][2]; for (int k_base = 0; k_base < K; k_base += BLOCK_K) { const int k_limit = min(k_base + BLOCK_K, K); const int k_chunk = k_limit - k_base; const int tid = threadIdx.x; const int elements_per_thread_A = (BLOCK_M * k_chunk + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK; for (int i = 0; i < elements_per_thread_A; ++i) { const int idx = tid + i * THREADS_PER_BLOCK; if (idx < BLOCK_M * k_chunk) { const int row = idx / k_chunk; const int col = idx % k_chunk; const int global_row = block_m + row; const int global_col = k_base + col; if (global_row < M && global_col < K) { As[row * BLOCK_K * 2 + col] = A_block[global_row * K + global_col]; } else { As[row * BLOCK_K * 2 + col] = __float2e4m3_rn(0.0f); } } } const int elements_per_thread_B = (BLOCK_N * k_chunk + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK; for (int i = 0; i < elements_per_thread_B; ++i) { const int idx = tid + i * THREADS_PER_BLOCK; if (idx < BLOCK_N * k_chunk) { const int row = idx / k_chunk; const int col = idx % k_chunk; const int global_row = block_n + row; const int global_col = k_base + col; if (global_row < N && global_col < K) { Bs[col * BLOCK_N + row] = B_block[global_row * K + global_col]; } else { Bs[col * BLOCK_N + row] = __float2e4m3_rn(0.0f); } } } __syncthreads(); for (int k_inner = 0; k_inner < k_chunk; k_inner += MMA_K) { for (int i = 0; i < WARP_ROW_TILES; ++i) { const int smem_row = warp_m + i * MMA_M; const int smem_col = k_inner; asm volatile( "ldmatrix.sync.aligned.x4.m8n8.shared.b16 {%0, %1, %2, %3}, [%4];" : "=r"(a_frag[i][0]), "=r"(a_frag[i][1]), "=r"(a_frag[i][2]), "=r"(a_frag[i][3]) : "r"(reinterpret_cast(&As[smem_row * BLOCK_K * 2 + smem_col])) ); } for (int j = 0; j < WARP_COL_TILES; ++j) { const int smem_row = k_inner; const int smem_col = warp_n + j * MMA_N; asm volatile( "ldmatrix.sync.aligned.x2.m8n8.shared.b16 {%0, %1}, [%2];" : "=r"(b_frag[j][0]), "=r"(b_frag[j][1]) : "r"(reinterpret_cast(&Bs[smem_row * BLOCK_N + smem_col])) ); } for (int i = 0; i < WARP_ROW_TILES; ++i) { for (int j = 0; j < WARP_COL_TILES; ++j) { float c00 = accum[i][j][0][0]; float c01 = accum[i][j][0][1]; float c10 = accum[i][j][1][0]; float c11 = accum[i][j][1][1]; asm volatile( "mma.sync.aligned.m16n8k16.row.col.f8f6f4.f8.f8.f32 {%0, %1, %2, %3}, {%4, %5, %6, %7}, {%8, %9}, {%10, %11, %12, %13};" : "=f"(c00), "=f"(c01), "=f"(c10), "=f"(c11) : "r"(a_frag[i][0]), "r"(a_frag[i][1]), "r"(a_frag[i][2]), "r"(a_frag[i][3]), "r"(b_frag[j][0]), "r"(b_frag[j][1]), "f"(c00), "f"(c01), "f"(c10), "f"(c11) ); accum[i][j][0][0] = c00; accum[i][j][0][1] = c01; accum[i][j][1][0] = c10; accum[i][j][1][1] = c11; } } } __syncthreads(); } const int out_row = block_m + warp_m; const int out_col = block_n + warp_n; if (out_row < M && out_col < N) { for (int i = 0; i < WARP_ROW_TILES; ++i) { for (int j = 0; j < WARP_COL_TILES; ++j) { int row = out_row + i * MMA_M + lane_id / 4 * 2; int col = out_col + j * MMA_N + (lane_id % 4) * 2; if (row + 1 < M && col + 1 < N) { C[row * N + col] = __float2bfloat16_rn(accum[i][j][0][0]); C[row * N + col + 1] = __float2bfloat16_rn(accum[i][j][0][1]); C[(row + 1) * N + col] = __float2bfloat16_rn(accum[i][j][1][0]); C[(row + 1) * N + col + 1] = __float2bfloat16_rn(accum[i][j][1][1]); } } } } } """ cpp_source = r""" #include #include #include void launch_fp8_gemm(const __nv_fp8_e4m3* A, const __nv_fp8_e4m3* B, __nv_bfloat16* C, int M, int N, int K) { dim3 grid((N + 127) / 128, (M + 127) / 128); dim3 block(128); size_t smem_size = 128 * 32 * 2 * sizeof(__nv_fp8_e4m3) + 128 * 32 * 2 * sizeof(__nv_fp8_e4m3); fp8_gemm_kernel<<>>(A, B, C, M, N, K); } torch::Tensor fp8_gemm(torch::Tensor A, torch::Tensor B) { int M = A.size(0); int K = A.size(1); int N = B.size(0); auto C = torch::empty({M, N}, torch::TensorOptions().dtype(torch::kBFloat16).device(A.device())); launch_fp8_gemm( reinterpret_cast(A.data_ptr()), reinterpret_cast(B.data_ptr()), reinterpret_cast<__nv_bfloat16*>(C.data_ptr()), M, N, K ); return C; } """ fp8_gemm_ext = load_inline( name="fp8_gemm", cpp_sources=cpp_source, cuda_sources=cuda_source, functions=["fp8_gemm"], verbose=True, extra_cuda_cflags=["-arch=sm_120", "-std=c++17"], ) class Model(nn.Module): def __init__(self, M, N, K): super().__init__() self.M, self.N, self.K = M, N, K self.weight = nn.Parameter(torch.empty(N, K, dtype=torch.bfloat16)) nn.init.normal_(self.weight, std=0.02) def forward(self, x): w_fp8 = self.weight.to(torch.float8_e4m3fn) x_fp8 = x.to(torch.float8_e4m3fn) return fp8_gemm_ext.fp8_gemm(x_fp8, w_fp8) M = 4096 N = 4096 K = 4096 def get_inputs(): x = (torch.rand(M, K) * 8 - 4).to(torch.float8_e4m3fn) return [x] def get_init_inputs(): return [M, N, K]