correct
54_Conv2d_Multiply_LeakyReLU_GELU.py
started
2026-05-05T02:27:32Z
timingkernelbench.timing
kernel mean / median / std
5.17 / — / 0.01 μs
ref mean / median / std
6.15 / — / 0.17 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
1.43x
energyNVML (pynvml)
ratio (ref / kernel)
3.50x
SOL (speed-of-light)nsight_hardware_counters
roofline / occupancynsight_hardware_counters
dram bandwidth
10850.44 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):
"""
Model that performs a convolution, multiplies by a learnable scalar, applies LeakyReLU, and then GELU.
"""
def __init__(self, in_channels, out_channels, kernel_size, multiplier_shape):
super(Model, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size)
self.multiplier = nn.Parameter(torch.randn(multiplier_shape))
self.leaky_relu = nn.LeakyReLU()
def forward(self, x):
x = self.conv(x)
x = x * self.multiplier
x = self.leaky_relu(x)
x = torch.nn.functional.gelu(x)
return x
batch_size = 64
in_channels = 64
out_channels = 64
height, width = 256, 256
kernel_size = 3
multiplier_shape = (out_channels, 1, 1)
def get_inputs():
return [torch.rand(batch_size, in_channels, height, width)]
def get_init_inputs():
return [in_channels, out_channels, kernel_size, multiplier_shape]turn 077.1s turn · 77.1s 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\n_cpp_src = r\"\"\"\n#include <torch/extension.h>\n\ntorch::Tensor fused_post_inplace_cuda(torch::Tensor x, torch::Tensor multiplier);\n\ntorch::Tensor fused_post_inplace(torch::Tensor x, torch::Tensor multiplier) {\n return fused_post_inplace_cuda(x, multiplier);\n}\n\"\"\"\n\n_cuda_src = r\"\"\"\n#include <torch/extension.h>\n#include <ATen/cuda/CUDAContext.h>\n#include <c10/cuda/CUDAGuard.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n\n__global__ void fused_post_kernel(float* __restrict__ x,\n const float* __restrict__ multiplier,\n int C, int HW) {\n const int nc = (int)blockIdx.y; // combined N,C index\n const int c = nc % C;\n const float m = multiplier[c];\n const long long base = (long long)nc * (long long)HW;\n\n for (int hw = (int)blockIdx.x * blockDim.x + threadIdx.x;\n hw < HW;\n hw += (int)gridDim.x * blockDim.x) {\n const long long idx = base + (long long)hw;\n float v = x[idx] * m;\n v = (v >= 0.0f) ? v : (v * 0.01f);\n // Exact GELU: 0.5*x*(1+erf(x/sqrt(2))).\n float e = erff(v * 0.70710678118654752440f);\n x[idx] = 0.5f * v * (1.0f + e);\n }\n}\n\ntorch::Tensor fused_post_inplace_cuda(torch::Tensor x, torch::Tensor multiplier) {\n const at::cuda::OptionalCUDAGuard device_guard(device_of(x));\n\n const int N = (int)x.size(0);\n const int C = (int)x.size(1);\n const int H = (int)x.size(2);\n const int W = (int)x.size(3);\n const int HW = H * W;\n\n const int threads = 256;\n int bx = (HW + threads - 1) / threads;\n if (bx > 64) bx = 64; // grid-stride over spatial dimension; fewer CTAs, no per-element channel divides\n if (bx < 1) bx = 1;\n const dim3 blocks((unsigned)bx, (unsigned)(N * C));\n\n fused_post_kernel<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(\n x.data_ptr<float>(), multiplier.data_ptr<float>(), C, HW);\n return x;\n}\n\"\"\"\n\n_ext = load_inline(\n name=\"fused_conv_post_gelu_ext_v1\",\n cpp_sources=_cpp_src,\n cuda_sources=_cuda_src,\n functions=[\"fused_post_inplace\"],\n extra_cflags=[\"-O3\"],\n extra_cuda_cflags=[\"-O3\"],\n with_cuda=True,\n verbose=False,\n)\n\n\nclass ModelNew(nn.Module):\n \"\"\"\n Conv2d followed by per-output-channel multiplier, LeakyReLU, and exact GELU.\n The convolution is kept as cuDNN/PyTorch Conv2d while the three pointwise\n operations are fused into one custom CUDA kernel writing the temporary\n convolution output in-place.\n \"\"\"\n def __init__(self, in_channels, out_channels, kernel_size, multiplier_shape):\n super(ModelNew, self).__init__()\n self.conv = nn.Conv2d(in_channels, out_channels, kernel_size)\n self.multiplier = nn.Parameter(torch.randn(multiplier_shape))\n\n def forward(self, x):\n y = self.conv(x)\n return _ext.fused_post_inplace(y, self.multiplier)\n"
}
result ← submit_kernel (ok)
oksubmit_kernel
submit_kernel PASSED: (5 / 5) correctness trials all passed.
Kernel runtime: 5.17 μs
Runtime stats: mean=5.17μ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: 4097.55 MB peak (ref: 4097.55 MB, ratio: 1.0x)
Kernel launches: 7 (ref: 10, fusion ratio: 1.4286)
Energy: 483.24 mJ/run (ref: 1691.22 mJ, ratio: 3.4998x)
SOL score: 1.0
final submitted kernel (91 lines, 2,960 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>
torch::Tensor fused_post_inplace_cuda(torch::Tensor x, torch::Tensor multiplier);
torch::Tensor fused_post_inplace(torch::Tensor x, torch::Tensor multiplier) {
return fused_post_inplace_cuda(x, multiplier);
}
"""
_cuda_src = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAGuard.h>
#include <cuda.h>
#include <cuda_runtime.h>
__global__ void fused_post_kernel(float* __restrict__ x,
const float* __restrict__ multiplier,
int C, int HW) {
const int nc = (int)blockIdx.y; // combined N,C index
const int c = nc % C;
const float m = multiplier[c];
const long long base = (long long)nc * (long long)HW;
for (int hw = (int)blockIdx.x * blockDim.x + threadIdx.x;
hw < HW;
hw += (int)gridDim.x * blockDim.x) {
const long long idx = base + (long long)hw;
float v = x[idx] * m;
v = (v >= 0.0f) ? v : (v * 0.01f);
// Exact GELU: 0.5*x*(1+erf(x/sqrt(2))).
float e = erff(v * 0.70710678118654752440f);
x[idx] = 0.5f * v * (1.0f + e);
}
}
torch::Tensor fused_post_inplace_cuda(torch::Tensor x, torch::Tensor multiplier) {
const at::cuda::OptionalCUDAGuard device_guard(device_of(x));
const int N = (int)x.size(0);
const int C = (int)x.size(1);
const int H = (int)x.size(2);
const int W = (int)x.size(3);
const int HW = H * W;
const int threads = 256;
int bx = (HW + threads - 1) / threads;
if (bx > 64) bx = 64; // grid-stride over spatial dimension; fewer CTAs, no per-element channel divides
if (bx < 1) bx = 1;
const dim3 blocks((unsigned)bx, (unsigned)(N * C));
fused_post_kernel<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(
x.data_ptr<float>(), multiplier.data_ptr<float>(), C, HW);
return x;
}
"""
_ext = load_inline(
name="fused_conv_post_gelu_ext_v1",
cpp_sources=_cpp_src,
cuda_sources=_cuda_src,
functions=["fused_post_inplace"],
extra_cflags=["-O3"],
extra_cuda_cflags=["-O3"],
with_cuda=True,
verbose=False,
)
class ModelNew(nn.Module):
"""
Conv2d followed by per-output-channel multiplier, LeakyReLU, and exact GELU.
The convolution is kept as cuDNN/PyTorch Conv2d while the three pointwise
operations are fused into one custom CUDA kernel writing the temporary
convolution output in-place.
"""
def __init__(self, in_channels, out_channels, kernel_size, multiplier_shape):
super(ModelNew, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size)
self.multiplier = nn.Parameter(torch.randn(multiplier_shape))
def forward(self, x):
y = self.conv(x)
return _ext.fused_post_inplace(y, self.multiplier)