correct
23_virtual_all_reduce_mean.py
started
2026-05-11T21:32:45Z
timingkernelbench.timing
kernel mean / median / std
0.01 / — / 9.81e-05 μs
ref mean / median / std
0.01 / — / 4.94e-04 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
2.00x
SOL (speed-of-light)nsight_hardware_counters
roofline / occupancynsight_hardware_counters
warp stalls (top 5)nsight
reference PyTorch source (the prompt the agent sees)
"""
Virtual ranks: input packs R per-rank tensors along dim 0. Output is the mean over ranks (B, D).
No distributed runtime required; the reference always reduces along dim 0.
"""
import torch
import torch.nn as nn
from kernelbench.distributed_collectives import default_device
R = 4
B = 16
D = 48
class Model(nn.Module):
"""x shape (R, B, D) -> mean over R -> (B, D)."""
def __init__(self):
super().__init__()
def forward(self, x: torch.Tensor) -> torch.Tensor:
return x.mean(dim=0)
def get_inputs():
dev = default_device()
p = popcorn_pri
return [torch.randn(p.jitter_int(R), p.jitter_int(B), p.jitter_int(D, align=8), device=dev)]
def get_init_inputs():
return []
turn 0110.8s turn · 12.7s LLM · 98.1s tools · tokens in=1,957 · out=386 · total=3,000
reasoning
The task is to replace the forward computation of a PyTorch model with a custom CUDA kernel. The model computes the mean over dimension 0 of a tensor with shape (R, B, D), where R=4, B=16, D=48.
I need to replace the forward computation of a PyTorch model with a custom CUDA kernel.
call → compile_kernel
callcompile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = '''\n__global__ void mean_dim0(const float* __restrict__ input, float* __restrict__ output, int R, int B, int D) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n if (idx < B * D) {\n int b = idx / D;\n int d = idx % D;\n float sum = 0.0f;\n for(int r = 0; r < R; r++) {\n sum += input[(r * B + b) * D + d];\n }\n output[idx] = sum / static_cast<float>(R);\n }\n}\n\ntorch::Tensor mean_dim0_cuda(torch::Tensor x) {\n auto R = x.size(0);\n auto B = x.size(1);\n auto D = x.size(2);\n auto output = torch::empty({B, D}, x.options());\n int total = B * D;\n int threads = 256;\n int blocks = (total + threads - 1) / threads;\n mean_dim0<<<blocks, threads>>>(x.data_ptr<float>(), output.data_ptr<float>(), R, B, D);\n return output;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor mean_dim0_cuda(torch::Tensor x);\n'''\n\nmodule = load_inline(\n name='mean_module',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['mean_dim0_cuda'],\n verbose=False\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.op = module\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n return self.op.mean_dim0_cuda(x)\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 15.6s turn · 5.5s LLM · 0.0s tools · tokens in=3,017 · out=387 · total=3,411
reasoning
Compiled successfully. Now check correctness.
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = '''\n__global__ void mean_dim0(const float* __restrict__ input, float* __restrict__ output, int R, int B, int D) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n if (idx < B * D) {\n int b = idx / D;\n int d = idx % D;\n float sum = 0.0f;\n for(int r = 0; r < R; r++) {\n sum += input[(r * B + b) * D + d];\n }\n output[idx] = sum / static_cast<float>(R);\n }\n}\n\ntorch::Tensor mean_dim0_cuda(torch::Tensor x) {\n auto R = x.size(0);\n auto B = x.size(1);\n auto D = x.size(2);\n auto output = torch::empty({B, D}, x.options());\n int total = B * D;\n int threads = 256;\n int blocks = (total + threads - 1) / threads;\n mean_dim0<<<blocks, threads>>>(x.data_ptr<float>(), output.data_ptr<float>(), R, B, D);\n return output;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor mean_dim0_cuda(torch::Tensor x);\n'''\n\nmodule = load_inline(\n name='mean_module',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['mean_dim0_cuda'],\n verbose=False\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.op = module\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n return self.op.mean_dim0_cuda(x)\n"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (3 / 3) trials all matched the reference.
Numerical precision: max_abs_err=0.00e+00 mean_abs_err=0.00e+00 max_rel_err=0.00e+00
turn 219.3s turn · 6.3s LLM · 13.0s tools · tokens in=3,471 · out=386 · total=3,953 · FINAL
reasoning
Correct. Now, to check if faster, I should profile or submit, but since it's the final, and small, likely faster as it's specialized.
call → submit_kernel
callsubmit_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = '''\n__global__ void mean_dim0(const float* __restrict__ input, float* __restrict__ output, int R, int B, int D) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n if (idx < B * D) {\n int b = idx / D;\n int d = idx % D;\n float sum = 0.0f;\n for(int r = 0; r < R; r++) {\n sum += input[(r * B + b) * D + d];\n }\n output[idx] = sum / static_cast<float>(R);\n }\n}\n\ntorch::Tensor mean_dim0_cuda(torch::Tensor x) {\n auto R = x.size(0);\n auto B = x.size(1);\n auto D = x.size(2);\n auto output = torch::empty({B, D}, x.options());\n int total = B * D;\n int threads = 256;\n int blocks = (total + threads - 1) / threads;\n mean_dim0<<<blocks, threads>>>(x.data_ptr<float>(), output.data_ptr<float>(), R, B, D);\n return output;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor mean_dim0_cuda(torch::Tensor x);\n'''\n\nmodule = load_inline(\n name='mean_module',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['mean_dim0_cuda'],\n verbose=False\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.op = module\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n return self.op.mean_dim0_cuda(x)\n"
}
result ← submit_kernel (ok)
oksubmit_kernel
submit_kernel PASSED: (3 / 3) correctness trials all passed.
Kernel runtime: 0.01 μs
Runtime stats: mean=0.01μ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: 33.01 MB peak (ref: 33.01 MB, ratio: 1.0x)
Kernel launches: 1 (ref: 2, fusion ratio: 2.0)
SOL score: 0.0019
final submitted kernel (51 lines, 1,340 chars) — full source, untruncated
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline
cuda_source = '''
__global__ void mean_dim0(const float* __restrict__ input, float* __restrict__ output, int R, int B, int D) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < B * D) {
int b = idx / D;
int d = idx % D;
float sum = 0.0f;
for(int r = 0; r < R; r++) {
sum += input[(r * B + b) * D + d];
}
output[idx] = sum / static_cast<float>(R);
}
}
torch::Tensor mean_dim0_cuda(torch::Tensor x) {
auto R = x.size(0);
auto B = x.size(1);
auto D = x.size(2);
auto output = torch::empty({B, D}, x.options());
int total = B * D;
int threads = 256;
int blocks = (total + threads - 1) / threads;
mean_dim0<<<blocks, threads>>>(x.data_ptr<float>(), output.data_ptr<float>(), R, B, D);
return output;
}
'''
cpp_source = '''
torch::Tensor mean_dim0_cuda(torch::Tensor x);
'''
module = load_inline(
name='mean_module',
cpp_sources=[cpp_source],
cuda_sources=[cuda_source],
functions=['mean_dim0_cuda'],
verbose=False
)
class ModelNew(nn.Module):
def __init__(self):
super().__init__()
self.op = module
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.op.mean_dim0_cuda(x)