correct
17_Matmul_with_transposed_B.py
started
2026-05-05T02:17:25Z
timingkernelbench.timing
kernel mean / median / std
2.69 / — / 0.01 μs
ref mean / median / std
2.74 / — / 0.25 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
5.00x
energyNVML (pynvml)
ratio (ref / kernel)
4.00x
SOL (speed-of-light)nsight_hardware_counters
roofline / occupancynsight_hardware_counters
dram bandwidth
113.07 GB/s
warp stalls (top 5)nsight
reference PyTorch source (the prompt the agent sees)
import torch
import torch.nn as nn
class Model(nn.Module):
"""
Simple model that performs a single matrix multiplication (C = A * B)
"""
def __init__(self):
super(Model, self).__init__()
def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
"""
Performs matrix multiplication.
Args:
A: Input tensor of shape (M, K).
B: Input tensor of shape (K, N).
Returns:
Output tensor of shape (M, N).
"""
return torch.matmul(A, B.T)
M = 1024 * 2
K = 4096 * 2
N = 2048 * 2
def get_inputs():
A = torch.rand(M, K)
B = torch.rand(N, K)
return [A, B]
def get_init_inputs():
return [] # No special initialization inputs neededturn 027.4s turn · 27.4s LLM · 0.0s tools · FINAL
call → submit_kernel
callsubmit_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\n_cpp_src = r'''\n#include <torch/extension.h>\n#include <ATen/cuda/CUDAContext.h>\n#include <c10/cuda/CUDAGuard.h>\n#include <cublas_v2.h>\n\nstatic inline void cublas_check(cublasStatus_t status) {\n TORCH_CHECK(status == CUBLAS_STATUS_SUCCESS, \"cuBLAS error: \", (int)status);\n}\n\ntorch::Tensor matmul_forward(torch::Tensor A, torch::Tensor B) {\n TORCH_CHECK(A.is_cuda() && B.is_cuda(), \"inputs must be CUDA tensors\");\n TORCH_CHECK(A.scalar_type() == torch::kFloat32 && B.scalar_type() == torch::kFloat32, \"inputs must be float32\");\n TORCH_CHECK(A.dim() == 2 && B.dim() == 2, \"inputs must be 2D\");\n TORCH_CHECK(A.is_contiguous() && B.is_contiguous(), \"inputs must be contiguous\");\n const int64_t M64 = A.size(0);\n const int64_t K64 = A.size(1);\n const int64_t N64 = B.size(0);\n TORCH_CHECK(B.size(1) == K64, \"inner dimensions must match\");\n TORCH_CHECK(M64 <= INT_MAX && N64 <= INT_MAX && K64 <= INT_MAX, \"dimensions too large\");\n\n c10::cuda::CUDAGuard guard(A.device());\n auto C = torch::empty({M64, N64}, A.options());\n\n const int M = (int)M64;\n const int N = (int)N64;\n const int K = (int)K64;\n const float alpha = 1.0f;\n const float beta = 0.0f;\n\n cublasHandle_t handle = at::cuda::getCurrentCUDABlasHandle();\n // Row-major C = A * B^T is computed as column-major C^T = B * A^T.\n#if defined(CUBLAS_COMPUTE_32F_FAST_TF32)\n cublas_check(cublasGemmEx(\n handle,\n CUBLAS_OP_T, CUBLAS_OP_N,\n N, M, K,\n &alpha,\n B.data_ptr<float>(), CUDA_R_32F, K,\n A.data_ptr<float>(), CUDA_R_32F, K,\n &beta,\n C.data_ptr<float>(), CUDA_R_32F, N,\n CUBLAS_COMPUTE_32F_FAST_TF32,\n CUBLAS_GEMM_DEFAULT_TENSOR_OP));\n#else\n cublas_check(cublasSgemm(\n handle,\n CUBLAS_OP_T, CUBLAS_OP_N,\n N, M, K,\n &alpha,\n B.data_ptr<float>(), K,\n A.data_ptr<float>(), K,\n &beta,\n C.data_ptr<float>(), N));\n#endif\n return C;\n}\n'''\n\n_ext = load_inline(\n name='modelnew_cublas_abt_ext',\n cpp_sources=_cpp_src,\n functions=['matmul_forward'],\n with_cuda=True,\n extra_cflags=['-O3'],\n extra_ldflags=['-lcublas'],\n verbose=False,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super(ModelNew, self).__init__()\n\n def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:\n return _ext.matmul_forward(A, B)\n"
}
result ← submit_kernel (ok)
oksubmit_kernel
submit_kernel PASSED: (5 / 5) correctness trials all passed.
Kernel runtime: 2.69 μs
Runtime stats: mean=2.69μs median=?μs std=0.01μs
Numerical precision: max_abs_err=0.00e+00 mean_abs_err=0.00e+00 max_rel_err=0.00e+00
Memory: 256.0 MB peak (ref: 256.0 MB, ratio: 1.0x)
Kernel launches: 1 (ref: 5, fusion ratio: 5.0)
Energy: 241.7 mJ/run (ref: 966.04 mJ, ratio: 3.9969x)
SOL score: 0.7941
final submitted kernel (80 lines, 2,484 chars) — full source, untruncated
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline
_cpp_src = r'''
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAGuard.h>
#include <cublas_v2.h>
static inline void cublas_check(cublasStatus_t status) {
TORCH_CHECK(status == CUBLAS_STATUS_SUCCESS, "cuBLAS error: ", (int)status);
}
torch::Tensor matmul_forward(torch::Tensor A, torch::Tensor B) {
TORCH_CHECK(A.is_cuda() && B.is_cuda(), "inputs must be CUDA tensors");
TORCH_CHECK(A.scalar_type() == torch::kFloat32 && B.scalar_type() == torch::kFloat32, "inputs must be float32");
TORCH_CHECK(A.dim() == 2 && B.dim() == 2, "inputs must be 2D");
TORCH_CHECK(A.is_contiguous() && B.is_contiguous(), "inputs must be contiguous");
const int64_t M64 = A.size(0);
const int64_t K64 = A.size(1);
const int64_t N64 = B.size(0);
TORCH_CHECK(B.size(1) == K64, "inner dimensions must match");
TORCH_CHECK(M64 <= INT_MAX && N64 <= INT_MAX && K64 <= INT_MAX, "dimensions too large");
c10::cuda::CUDAGuard guard(A.device());
auto C = torch::empty({M64, N64}, A.options());
const int M = (int)M64;
const int N = (int)N64;
const int K = (int)K64;
const float alpha = 1.0f;
const float beta = 0.0f;
cublasHandle_t handle = at::cuda::getCurrentCUDABlasHandle();
// Row-major C = A * B^T is computed as column-major C^T = B * A^T.
#if defined(CUBLAS_COMPUTE_32F_FAST_TF32)
cublas_check(cublasGemmEx(
handle,
CUBLAS_OP_T, CUBLAS_OP_N,
N, M, K,
&alpha,
B.data_ptr<float>(), CUDA_R_32F, K,
A.data_ptr<float>(), CUDA_R_32F, K,
&beta,
C.data_ptr<float>(), CUDA_R_32F, N,
CUBLAS_COMPUTE_32F_FAST_TF32,
CUBLAS_GEMM_DEFAULT_TENSOR_OP));
#else
cublas_check(cublasSgemm(
handle,
CUBLAS_OP_T, CUBLAS_OP_N,
N, M, K,
&alpha,
B.data_ptr<float>(), K,
A.data_ptr<float>(), K,
&beta,
C.data_ptr<float>(), N));
#endif
return C;
}
'''
_ext = load_inline(
name='modelnew_cublas_abt_ext',
cpp_sources=_cpp_src,
functions=['matmul_forward'],
with_cuda=True,
extra_cflags=['-O3'],
extra_ldflags=['-lcublas'],
verbose=False,
)
class ModelNew(nn.Module):
def __init__(self):
super(ModelNew, self).__init__()
def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
return _ext.matmul_forward(A, B)