correct
5_Matrix_scalar_multiplication.py
started
2026-05-05T00:02:02Z
timingkernelbench.timing
kernel mean / median / std
3.26 / — / 1.66 μs
ref mean / median / std
2.89 / — / 0.33 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
0.50x
energyNVML (pynvml)
ratio (ref / kernel)
0.47x
SOL (speed-of-light)nsight_hardware_counters
roofline / occupancynsight_hardware_counters
dram bandwidth
2841.73 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 matrix-scalar multiplication (C = A * s)
"""
def __init__(self):
super(Model, self).__init__()
def forward(self, A: torch.Tensor, s: float) -> torch.Tensor:
"""
Performs matrix-scalar multiplication.
Args:
A: Input matrix of shape (M, N)
s: Scalar value
Returns:
C: Resulting matrix of shape (M, N)
"""
return A * s
M = 16384 * 4
N = 4096 * 4
def get_inputs():
A = torch.rand(M, N)
s = 3.14
return [A, s]
def get_init_inputs():
return [] # No special initialization inputs neededturn 0146.7s turn · 146.7s 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\"\"\"\ntorch::Tensor scalar_mul_forward(torch::Tensor A, double s);\n\"\"\"\n\n_cuda_src = r\"\"\"\n#include <torch/extension.h>\n#include <ATen/cuda/CUDAContext.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <stdint.h>\n\n__global__ void scalar_mul_vec4_kernel(const float* __restrict__ A,\n float* __restrict__ C,\n float s,\n int64_t n4) {\n int64_t idx = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;\n int64_t stride = (int64_t)blockDim.x * gridDim.x;\n const float4* __restrict__ Av = reinterpret_cast<const float4*>(A);\n float4* __restrict__ Cv = reinterpret_cast<float4*>(C);\n for (int64_t i = idx; i < n4; i += stride) {\n float4 v = Av[i];\n v.x *= s;\n v.y *= s;\n v.z *= s;\n v.w *= s;\n Cv[i] = v;\n }\n}\n\n__global__ void scalar_mul_kernel(const float* __restrict__ A,\n float* __restrict__ C,\n float s,\n int64_t n) {\n int64_t idx = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;\n int64_t stride = (int64_t)blockDim.x * gridDim.x;\n for (int64_t i = idx; i < n; i += stride) {\n C[i] = A[i] * s;\n }\n}\n\ntorch::Tensor scalar_mul_forward(torch::Tensor A, double s) {\n auto C = torch::empty_like(A);\n const int64_t n = A.numel();\n if (n == 0) return C;\n const int threads = 256;\n int device = A.get_device();\n int sms = 0;\n cudaDeviceGetAttribute(&sms, cudaDevAttrMultiProcessorCount, device);\n // Enough blocks to saturate memory bandwidth without excessive launch overhead.\n int blocks = sms * 8;\n if (blocks < 1) blocks = 1;\n int64_t work = n;\n const float* Ap = A.data_ptr<float>();\n float* Cp = C.data_ptr<float>();\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n\n uintptr_t au = reinterpret_cast<uintptr_t>(Ap);\n uintptr_t cu = reinterpret_cast<uintptr_t>(Cp);\n if (((au | cu) & 15ULL) == 0ULL && (n % 4 == 0)) {\n work = n / 4;\n int64_t max_blocks = (work + threads - 1) / threads;\n if ((int64_t)blocks > max_blocks) blocks = (int)max_blocks;\n scalar_mul_vec4_kernel<<<blocks, threads, 0, stream>>>(Ap, Cp, (float)s, work);\n } else {\n int64_t max_blocks = (work + threads - 1) / threads;\n if ((int64_t)blocks > max_blocks) blocks = (int)max_blocks;\n scalar_mul_kernel<<<blocks, threads, 0, stream>>>(Ap, Cp, (float)s, work);\n }\n return C;\n}\n\"\"\"\n\n_ext = load_inline(\n name=\"scalar_mul_ext_v1\",\n cpp_sources=_cpp_src,\n cuda_sources=_cuda_src,\n functions=[\"scalar_mul_forward\"],\n extra_cflags=[\"-O3\"],\n extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"],\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, s: float) -> torch.Tensor:\n return _ext.scalar_mul_forward(A, float(s))\n"
}
result ← submit_kernel (ok)
oksubmit_kernel
submit_kernel PASSED: (5 / 5) correctness trials all passed.
Kernel runtime: 3.26 μs
Runtime stats: mean=3.26μs median=?μs std=1.66μs
Numerical precision: max_abs_err=0.00e+00 mean_abs_err=0.00e+00 max_rel_err=0.00e+00
Memory: 8225.0 MB peak (ref: 8225.0 MB, ratio: 1.0x)
Kernel launches: 2 (ref: 1, fusion ratio: 0.5)
Energy: 1957.5 mJ/run (ref: 929.1 mJ, ratio: 0.4746x)
SOL score: 0.8483
final submitted kernel (93 lines, 3,100 chars) — full source, untruncated
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline
_cpp_src = r"""
torch::Tensor scalar_mul_forward(torch::Tensor A, double s);
"""
_cuda_src = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <stdint.h>
__global__ void scalar_mul_vec4_kernel(const float* __restrict__ A,
float* __restrict__ C,
float s,
int64_t n4) {
int64_t idx = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
int64_t stride = (int64_t)blockDim.x * gridDim.x;
const float4* __restrict__ Av = reinterpret_cast<const float4*>(A);
float4* __restrict__ Cv = reinterpret_cast<float4*>(C);
for (int64_t i = idx; i < n4; i += stride) {
float4 v = Av[i];
v.x *= s;
v.y *= s;
v.z *= s;
v.w *= s;
Cv[i] = v;
}
}
__global__ void scalar_mul_kernel(const float* __restrict__ A,
float* __restrict__ C,
float s,
int64_t n) {
int64_t idx = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
int64_t stride = (int64_t)blockDim.x * gridDim.x;
for (int64_t i = idx; i < n; i += stride) {
C[i] = A[i] * s;
}
}
torch::Tensor scalar_mul_forward(torch::Tensor A, double s) {
auto C = torch::empty_like(A);
const int64_t n = A.numel();
if (n == 0) return C;
const int threads = 256;
int device = A.get_device();
int sms = 0;
cudaDeviceGetAttribute(&sms, cudaDevAttrMultiProcessorCount, device);
// Enough blocks to saturate memory bandwidth without excessive launch overhead.
int blocks = sms * 8;
if (blocks < 1) blocks = 1;
int64_t work = n;
const float* Ap = A.data_ptr<float>();
float* Cp = C.data_ptr<float>();
cudaStream_t stream = at::cuda::getCurrentCUDAStream();
uintptr_t au = reinterpret_cast<uintptr_t>(Ap);
uintptr_t cu = reinterpret_cast<uintptr_t>(Cp);
if (((au | cu) & 15ULL) == 0ULL && (n % 4 == 0)) {
work = n / 4;
int64_t max_blocks = (work + threads - 1) / threads;
if ((int64_t)blocks > max_blocks) blocks = (int)max_blocks;
scalar_mul_vec4_kernel<<<blocks, threads, 0, stream>>>(Ap, Cp, (float)s, work);
} else {
int64_t max_blocks = (work + threads - 1) / threads;
if ((int64_t)blocks > max_blocks) blocks = (int)max_blocks;
scalar_mul_kernel<<<blocks, threads, 0, stream>>>(Ap, Cp, (float)s, work);
}
return C;
}
"""
_ext = load_inline(
name="scalar_mul_ext_v1",
cpp_sources=_cpp_src,
cuda_sources=_cuda_src,
functions=["scalar_mul_forward"],
extra_cflags=["-O3"],
extra_cuda_cflags=["-O3", "--use_fast_math"],
verbose=False,
)
class ModelNew(nn.Module):
def __init__(self):
super(ModelNew, self).__init__()
def forward(self, A: torch.Tensor, s: float) -> torch.Tensor:
return _ext.scalar_mul_forward(A, float(s))