correct
2_Standard_matrix_multiplication_.py
started
2026-05-05T00:03:58Z
timingkernelbench.timing
kernel mean / median / std
2.87 / — / 0.17 μs
ref mean / median / std
2.63 / — / 9.93e-04 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
2.00x
energyNVML (pynvml)
ratio (ref / kernel)
0.50x
SOL (speed-of-light)nsight_hardware_counters
roofline / occupancynsight_hardware_counters
dram bandwidth
111.15 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)
M = 1024 * 2
K = 4096 * 2
N = 2048 * 2
def get_inputs():
A = torch.rand(M, K)
B = torch.rand(K, N)
return [A, B]
def get_init_inputs():
return [] # No special initialization inputs neededturn 0147.3s turn · 147.3s 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 cublasHandle_t get_handle() {\n static cublasHandle_t handle = nullptr;\n if (handle == nullptr) {\n cublasCreate(&handle);\n cublasSetMathMode(handle, CUBLAS_DEFAULT_MATH);\n }\n cublasSetStream(handle, at::cuda::getCurrentCUDAStream());\n return handle;\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, \"only fp32 supported\");\n TORCH_CHECK(A.dim() == 2 && B.dim() == 2, \"inputs must be 2D\");\n TORCH_CHECK(A.size(1) == B.size(0), \"shape mismatch\");\n\n const auto M64 = A.size(0);\n const auto K64 = A.size(1);\n const auto N64 = B.size(1);\n TORCH_CHECK(M64 <= INT_MAX && K64 <= INT_MAX && N64 <= INT_MAX, \"matrix too large\");\n const int M = (int)M64;\n const int K = (int)K64;\n const int N = (int)N64;\n\n c10::cuda::CUDAGuard guard(A.device());\n\n // The benchmark inputs are contiguous. Make the wrapper robust without\n // falling back to PyTorch matmul: copies are only for layout normalization.\n if (!A.is_contiguous()) A = A.contiguous();\n if (!B.is_contiguous()) B = B.contiguous();\n\n auto C = torch::empty({M64, N64}, A.options());\n\n const float alpha = 1.0f;\n const float beta = 0.0f;\n\n // Row-major C = A(MxK) * B(KxN) is computed as column-major\n // C^T(NxM) = B^T(NxK) * A^T(KxM), reusing the original row-major storage.\n cublasHandle_t h = get_handle();\n cublasStatus_t st = cublasSgemm(\n h,\n CUBLAS_OP_N, CUBLAS_OP_N,\n N, M, K,\n &alpha,\n B.data_ptr<float>(), N,\n A.data_ptr<float>(), K,\n &beta,\n C.data_ptr<float>(), N\n );\n TORCH_CHECK(st == CUBLAS_STATUS_SUCCESS, \"cublasSgemm failed\");\n return C;\n}\n'''\n\n_ext = load_inline(\n name='matmul_cublas_ext_v1',\n cpp_sources=[_cpp_src],\n cuda_sources=[],\n functions=['matmul_forward'],\n extra_cflags=['-O3'],\n extra_ldflags=['-lcublas'],\n with_cuda=True,\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.87 μs
Runtime stats: mean=2.87μs median=?μs std=0.17μs
Numerical precision: max_abs_err=0.00e+00 mean_abs_err=0.00e+00 max_rel_err=0.00e+00
Memory: 1281.25 MB peak (ref: 1281.25 MB, ratio: 1.0x)
Kernel launches: 1 (ref: 2, fusion ratio: 2.0)
Energy: 1961.06 mJ/run (ref: 989.68 mJ, ratio: 0.5047x)
SOL score: 0.8071
final submitted kernel (82 lines, 2,521 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 cublasHandle_t get_handle() {
static cublasHandle_t handle = nullptr;
if (handle == nullptr) {
cublasCreate(&handle);
cublasSetMathMode(handle, CUBLAS_DEFAULT_MATH);
}
cublasSetStream(handle, at::cuda::getCurrentCUDAStream());
return handle;
}
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, "only fp32 supported");
TORCH_CHECK(A.dim() == 2 && B.dim() == 2, "inputs must be 2D");
TORCH_CHECK(A.size(1) == B.size(0), "shape mismatch");
const auto M64 = A.size(0);
const auto K64 = A.size(1);
const auto N64 = B.size(1);
TORCH_CHECK(M64 <= INT_MAX && K64 <= INT_MAX && N64 <= INT_MAX, "matrix too large");
const int M = (int)M64;
const int K = (int)K64;
const int N = (int)N64;
c10::cuda::CUDAGuard guard(A.device());
// The benchmark inputs are contiguous. Make the wrapper robust without
// falling back to PyTorch matmul: copies are only for layout normalization.
if (!A.is_contiguous()) A = A.contiguous();
if (!B.is_contiguous()) B = B.contiguous();
auto C = torch::empty({M64, N64}, A.options());
const float alpha = 1.0f;
const float beta = 0.0f;
// Row-major C = A(MxK) * B(KxN) is computed as column-major
// C^T(NxM) = B^T(NxK) * A^T(KxM), reusing the original row-major storage.
cublasHandle_t h = get_handle();
cublasStatus_t st = cublasSgemm(
h,
CUBLAS_OP_N, CUBLAS_OP_N,
N, M, K,
&alpha,
B.data_ptr<float>(), N,
A.data_ptr<float>(), K,
&beta,
C.data_ptr<float>(), N
);
TORCH_CHECK(st == CUBLAS_STATUS_SUCCESS, "cublasSgemm failed");
return C;
}
'''
_ext = load_inline(
name='matmul_cublas_ext_v1',
cpp_sources=[_cpp_src],
cuda_sources=[],
functions=['matmul_forward'],
extra_cflags=['-O3'],
extra_ldflags=['-lcublas'],
with_cuda=True,
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)