correct
20_ConvTranspose3d_Sum_ResidualAdd_Multiply_ResidualAdd.py
started
2026-05-05T00:12:15Z
timingkernelbench.timing
kernel mean / median / std
1.67 / — / 0.03 μs
ref mean / median / std
3.78 / — / 1.34 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
2.29x
energyNVML (pynvml)
ratio (ref / kernel)
0.29x
SOL (speed-of-light)nsight_hardware_counters
roofline / occupancynsight_hardware_counters
dram bandwidth
11834.60 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 3D transposed convolution, followed by a sum,
a residual add, a multiplication, and another residual add.
"""
def __init__(self, in_channels, out_channels, kernel_size, stride, padding, output_padding, bias_shape):
super(Model, self).__init__()
self.conv_transpose = nn.ConvTranspose3d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, output_padding=output_padding)
self.bias = nn.Parameter(torch.randn(bias_shape))
def forward(self, x):
x = self.conv_transpose(x)
original_x = x.clone().detach()
x = x + self.bias
x = x + original_x
x = x * original_x
x = x + original_x
return x
batch_size = 16
in_channels = 32
out_channels = 64
depth, height, width = 16, 32, 32
kernel_size = 3
stride = 2
padding = 1
output_padding = 1
bias_shape = (out_channels, 1, 1, 1)
def get_inputs():
return [torch.rand(batch_size, in_channels, depth, height, width)]
def get_init_inputs():
return [in_channels, out_channels, kernel_size, stride, padding, output_padding, bias_shape]turn 037.5s turn · 37.5s LLM · 0.0s tools · FINAL
call → submit_kernel
callsubmit_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.cpp_extension import load_inline\n\n\n_cpp_src = r\"\"\"\n#include <torch/extension.h>\n\ntorch::Tensor fused_residual_bias_forward(torch::Tensor y, torch::Tensor bias);\n\"\"\"\n\n_cuda_src = r\"\"\"\n#include <torch/extension.h>\n#include <ATen/cuda/CUDAContext.h>\n#include <c10/cuda/CUDAGuard.h>\n\n__global__ void fused_residual_bias_kernel(float* __restrict__ y,\n const float* __restrict__ bias,\n long long total,\n int C,\n long long spatial) {\n long long idx = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n long long stride = (long long)blockDim.x * gridDim.x;\n for (; idx < total; idx += stride) {\n long long c = (idx / spatial) % C;\n float v = y[idx];\n // Reference after transposed convolution y:\n // ((y + custom_bias) + y) * y + y = 2*y*y + (custom_bias + 1)*y\n y[idx] = fmaf(2.0f * v, v, (bias[c] + 1.0f) * v);\n }\n}\n\ntorch::Tensor fused_residual_bias_forward(torch::Tensor y, torch::Tensor bias) {\n const c10::cuda::CUDAGuard device_guard(y.device());\n y = y.contiguous();\n bias = bias.contiguous();\n\n const long long total = y.numel();\n if (total == 0) return y;\n\n const int C = (int)y.size(1);\n const long long spatial = (long long)y.size(2) * y.size(3) * y.size(4);\n\n const int threads = 256;\n int blocks = (int)((total + threads - 1) / threads);\n if (blocks > 65535) blocks = 65535;\n\n fused_residual_bias_kernel<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(\n y.data_ptr<float>(), bias.data_ptr<float>(), total, C, spatial);\n return y;\n}\n\"\"\"\n\n_fused_ext = load_inline(\n name=\"fused_convtranspose3d_epilogue_ext\",\n cpp_sources=[_cpp_src],\n cuda_sources=[_cuda_src],\n functions=[\"fused_residual_bias_forward\"],\n extra_cflags=[\"-O3\"],\n extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"],\n verbose=False,\n)\n\n\nclass ModelNew(nn.Module):\n \"\"\"\n Same module interface as the reference. The expensive ConvTranspose3d is\n executed by PyTorch/cuDNN, while the clone/add/add/mul/add tail is replaced\n by one custom CUDA epilogue kernel executed in-place on the convolution\n output.\n \"\"\"\n def __init__(self, in_channels, out_channels, kernel_size, stride, padding, output_padding, bias_shape):\n super(ModelNew, self).__init__()\n self.conv_transpose = nn.ConvTranspose3d(\n in_channels,\n out_channels,\n kernel_size,\n stride=stride,\n padding=padding,\n output_padding=output_padding,\n )\n self.bias = nn.Parameter(torch.randn(bias_shape))\n\n def forward(self, x):\n y = F.conv_transpose3d(\n x,\n self.conv_transpose.weight,\n self.conv_transpose.bias,\n stride=self.conv_transpose.stride,\n padding=self.conv_transpose.padding,\n output_padding=self.conv_transpose.output_padding,\n groups=self.conv_transpose.groups,\n dilation=self.conv_transpose.dilation,\n )\n return _fused_ext.fused_residual_bias_forward(y, self.bias)\n"
}
result ← submit_kernel (ok)
oksubmit_kernel
submit_kernel PASSED: (5 / 5) correctness trials all passed.
Kernel runtime: 1.67 μs
Runtime stats: mean=1.67μs median=?μs std=0.03μs
Numerical precision: max_abs_err=2.38e-07 mean_abs_err=3.15e-09 max_rel_err=1.08e+04
Memory: 1121.64 MB peak (ref: 1601.42 MB, ratio: 0.7004x)
Kernel launches: 7 (ref: 16, fusion ratio: 2.2857)
Energy: 1691.84 mJ/run (ref: 483.8 mJ, ratio: 0.286x)
SOL score: 1.0
final submitted kernel (97 lines, 3,291 chars) — full source, untruncated
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.cpp_extension import load_inline
_cpp_src = r"""
#include <torch/extension.h>
torch::Tensor fused_residual_bias_forward(torch::Tensor y, torch::Tensor bias);
"""
_cuda_src = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAGuard.h>
__global__ void fused_residual_bias_kernel(float* __restrict__ y,
const float* __restrict__ bias,
long long total,
int C,
long long spatial) {
long long idx = (long long)blockIdx.x * blockDim.x + threadIdx.x;
long long stride = (long long)blockDim.x * gridDim.x;
for (; idx < total; idx += stride) {
long long c = (idx / spatial) % C;
float v = y[idx];
// Reference after transposed convolution y:
// ((y + custom_bias) + y) * y + y = 2*y*y + (custom_bias + 1)*y
y[idx] = fmaf(2.0f * v, v, (bias[c] + 1.0f) * v);
}
}
torch::Tensor fused_residual_bias_forward(torch::Tensor y, torch::Tensor bias) {
const c10::cuda::CUDAGuard device_guard(y.device());
y = y.contiguous();
bias = bias.contiguous();
const long long total = y.numel();
if (total == 0) return y;
const int C = (int)y.size(1);
const long long spatial = (long long)y.size(2) * y.size(3) * y.size(4);
const int threads = 256;
int blocks = (int)((total + threads - 1) / threads);
if (blocks > 65535) blocks = 65535;
fused_residual_bias_kernel<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(
y.data_ptr<float>(), bias.data_ptr<float>(), total, C, spatial);
return y;
}
"""
_fused_ext = load_inline(
name="fused_convtranspose3d_epilogue_ext",
cpp_sources=[_cpp_src],
cuda_sources=[_cuda_src],
functions=["fused_residual_bias_forward"],
extra_cflags=["-O3"],
extra_cuda_cflags=["-O3", "--use_fast_math"],
verbose=False,
)
class ModelNew(nn.Module):
"""
Same module interface as the reference. The expensive ConvTranspose3d is
executed by PyTorch/cuDNN, while the clone/add/add/mul/add tail is replaced
by one custom CUDA epilogue kernel executed in-place on the convolution
output.
"""
def __init__(self, in_channels, out_channels, kernel_size, stride, padding, output_padding, bias_shape):
super(ModelNew, self).__init__()
self.conv_transpose = nn.ConvTranspose3d(
in_channels,
out_channels,
kernel_size,
stride=stride,
padding=padding,
output_padding=output_padding,
)
self.bias = nn.Parameter(torch.randn(bias_shape))
def forward(self, x):
y = F.conv_transpose3d(
x,
self.conv_transpose.weight,
self.conv_transpose.bias,
stride=self.conv_transpose.stride,
padding=self.conv_transpose.padding,
output_padding=self.conv_transpose.output_padding,
groups=self.conv_transpose.groups,
dilation=self.conv_transpose.dilation,
)
return _fused_ext.fused_residual_bias_forward(y, self.bias)