correct
1_Square_matrix_multiplication_.py
started
2026-05-05T02:19:03Z
timingkernelbench.timing
kernel mean / median / std
2.66 / — / 0.00 μs
ref mean / median / std
2.66 / — / 0.00 μ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)
9.00x
SOL (speed-of-light)nsight_hardware_counters
roofline / occupancynsight_hardware_counters
dram bandwidth
149.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 square matrix multiplication (C = A * B)
"""
def __init__(self):
super(Model, self).__init__()
def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
"""
Performs the matrix multiplication.
Args:
A (torch.Tensor): Input matrix A of shape (N, N).
B (torch.Tensor): Input matrix B of shape (N, N).
Returns:
torch.Tensor: Output matrix C of shape (N, N).
"""
return torch.matmul(A, B)
N = 2048 * 2
def get_inputs():
A = torch.rand(N, N)
B = torch.rand(N, N)
return [A, B]
def get_init_inputs():
return [] # No special initialization inputs neededturn 043.8s turn · 43.8s 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 <cuda_runtime.h>\n#include <cublas_v2.h>\n\nstatic inline void check_cublas(cublasStatus_t status) {\n TORCH_CHECK(status == CUBLAS_STATUS_SUCCESS, \"cuBLAS call failed\");\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 matrices\");\n TORCH_CHECK(A.size(0) == A.size(1) && B.size(0) == B.size(1) && A.size(1) == B.size(0), \"bad matrix sizes\");\n TORCH_CHECK(A.is_contiguous() && B.is_contiguous(), \"inputs must be contiguous\");\n\n const int64_t n64 = A.size(0);\n TORCH_CHECK(n64 <= INT_MAX, \"matrix too large\");\n const int n = (int)n64;\n\n c10::cuda::CUDAGuard device_guard(A.device());\n auto C = torch::empty({n64, n64}, A.options());\n\n cublasHandle_t handle = at::cuda::getCurrentCUDABlasHandle();\n const float alpha = 1.0f;\n const float beta = 0.0f;\n\n // Tensors are row-major. cuBLAS is column-major, so computing\n // C_col = B_col * A_col produces the row-major C = A @ B in memory.\n#if defined(CUBLAS_COMPUTE_32F_FAST_TF32)\n check_cublas(cublasGemmEx(\n handle,\n CUBLAS_OP_N, CUBLAS_OP_N,\n n, n, n,\n &alpha,\n (const void*)B.data_ptr<float>(), CUDA_R_32F, n,\n (const void*)A.data_ptr<float>(), CUDA_R_32F, n,\n &beta,\n (void*)C.data_ptr<float>(), CUDA_R_32F, n,\n CUBLAS_COMPUTE_32F_FAST_TF32,\n CUBLAS_GEMM_DEFAULT_TENSOR_OP));\n#else\n check_cublas(cublasSgemm(\n handle,\n CUBLAS_OP_N, CUBLAS_OP_N,\n n, n, n,\n &alpha,\n B.data_ptr<float>(), n,\n A.data_ptr<float>(), n,\n &beta,\n C.data_ptr<float>(), n));\n#endif\n return C;\n}\n'''\n\n_ext = load_inline(\n name='modelnew_cublas_tf32_matmul_ext',\n cpp_sources=_cpp_src,\n functions=['matmul_forward'],\n with_cuda=True,\n extra_cflags=['-O3'],\n extra_cuda_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.66 μs
Runtime stats: mean=2.66μs median=?μs std=0.00μs
Numerical precision: max_abs_err=0.00e+00 mean_abs_err=0.00e+00 max_rel_err=0.00e+00
Memory: 225.0 MB peak (ref: 225.0 MB, ratio: 1.0x)
Kernel launches: 1 (ref: 2, fusion ratio: 2.0)
Energy: 241.56 mJ/run (ref: 2174.6 mJ, ratio: 9.0023x)
SOL score: 0.7998
final submitted kernel (80 lines, 2,537 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 <cuda_runtime.h>
#include <cublas_v2.h>
static inline void check_cublas(cublasStatus_t status) {
TORCH_CHECK(status == CUBLAS_STATUS_SUCCESS, "cuBLAS call failed");
}
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 matrices");
TORCH_CHECK(A.size(0) == A.size(1) && B.size(0) == B.size(1) && A.size(1) == B.size(0), "bad matrix sizes");
TORCH_CHECK(A.is_contiguous() && B.is_contiguous(), "inputs must be contiguous");
const int64_t n64 = A.size(0);
TORCH_CHECK(n64 <= INT_MAX, "matrix too large");
const int n = (int)n64;
c10::cuda::CUDAGuard device_guard(A.device());
auto C = torch::empty({n64, n64}, A.options());
cublasHandle_t handle = at::cuda::getCurrentCUDABlasHandle();
const float alpha = 1.0f;
const float beta = 0.0f;
// Tensors are row-major. cuBLAS is column-major, so computing
// C_col = B_col * A_col produces the row-major C = A @ B in memory.
#if defined(CUBLAS_COMPUTE_32F_FAST_TF32)
check_cublas(cublasGemmEx(
handle,
CUBLAS_OP_N, CUBLAS_OP_N,
n, n, n,
&alpha,
(const void*)B.data_ptr<float>(), CUDA_R_32F, n,
(const void*)A.data_ptr<float>(), CUDA_R_32F, n,
&beta,
(void*)C.data_ptr<float>(), CUDA_R_32F, n,
CUBLAS_COMPUTE_32F_FAST_TF32,
CUBLAS_GEMM_DEFAULT_TENSOR_OP));
#else
check_cublas(cublasSgemm(
handle,
CUBLAS_OP_N, CUBLAS_OP_N,
n, n, n,
&alpha,
B.data_ptr<float>(), n,
A.data_ptr<float>(), n,
&beta,
C.data_ptr<float>(), n));
#endif
return C;
}
'''
_ext = load_inline(
name='modelnew_cublas_tf32_matmul_ext',
cpp_sources=_cpp_src,
functions=['matmul_forward'],
with_cuda=True,
extra_cflags=['-O3'],
extra_cuda_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)