correct
65_Conv2d_AvgPool_Sigmoid_Sum.py
started
2026-05-05T02:36:34Z
timingkernelbench.timing
kernel mean / median / std
13.50 / — / 0.02 μs
ref mean / median / std
12.10 / — / 0.34 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
5.50x
energyNVML (pynvml)
ratio (ref / kernel)
0.50x
SOL (speed-of-light)nsight_hardware_counters
roofline / occupancynsight_hardware_counters
warp stalls (top 5)nsight
reference PyTorch source (the prompt the agent sees)
import torch
import torch.nn as nn
class Model(nn.Module):
"""
This model performs a convolution, average pooling, applies sigmoid, and sums the result.
"""
def __init__(self, in_channels, out_channels, kernel_size, pool_kernel_size):
super(Model, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size)
self.avg_pool = nn.AvgPool2d(pool_kernel_size)
def forward(self, x):
x = self.conv(x)
x = self.avg_pool(x)
x = torch.sigmoid(x)
x = torch.sum(x, dim=[1,2,3]) # Sum over all spatial dimensions
return x
batch_size = 128
in_channels = 8
out_channels = 64
height, width = 384, 384
kernel_size = 3
pool_kernel_size = 4
def get_inputs():
return [torch.rand(batch_size, in_channels, height, width)]
def get_init_inputs():
return [in_channels, out_channels, kernel_size, pool_kernel_size]turn 0 · FINAL · 1 tool call(s) · 93.6s turn · 93.6s LLM · 7,665 chars (click to expand)
turn 093.6s turn · 93.6s LLM · 0.0s tools · FINAL
reasoning
**Exploring kernel optimization**
I'm thinking about delving deeper into kernel optimization. There seems to be so much potential for improving performance, and it could really make a difference in how efficiently systems run. I'm curious about the specific techniques or methods that can be used for optimization and how they might apply to different scenarios. It's definitely a topic worth more exploration for me, so I should gather more information and insights to better understand it!
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_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias);\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\n#define FULL_MASK 0xffffffffu\n\n__inline__ __device__ float warp_sum(float v) {\n v += __shfl_down_sync(FULL_MASK, v, 16);\n v += __shfl_down_sync(FULL_MASK, v, 8);\n v += __shfl_down_sync(FULL_MASK, v, 4);\n v += __shfl_down_sync(FULL_MASK, v, 2);\n v += __shfl_down_sync(FULL_MASK, v, 1);\n return v;\n}\n\n// Build transposed effective 6x6 filters: effT[(c*36+r*6+s)*64 + oc]\n// for avgpool4(conv3(x)). This makes per-warp weight loads coalesced over oc.\n__global__ void make_eff_kernel(const float* __restrict__ w, float* __restrict__ effT) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n int total = 64 * 8 * 36;\n if (idx >= total) return;\n\n int oc = idx & 63; // fastest varying: output channel\n int q = idx >> 6;\n int s = q % 6;\n int r = (q / 6) % 6;\n int c = q / 36;\n\n float acc = 0.0f;\n #pragma unroll\n for (int ky = 0; ky < 3; ++ky) {\n int dy = r - ky;\n if ((unsigned)dy < 4u) {\n #pragma unroll\n for (int kx = 0; kx < 3; ++kx) {\n int dx = s - kx;\n if ((unsigned)dx < 4u) {\n acc += w[(((oc * 8 + c) * 3 + ky) * 3 + kx)];\n }\n }\n }\n }\n effT[idx] = acc * 0.0625f;\n}\n\n// One block handles several spatial pooled locations for one batch item.\n// Each pair of warps computes all 64 output channels for one spatial location:\n// lanes are output channels, and lane 0 broadcasts each input value. This avoids\n// reloading the same input patch 64 times and keeps effT loads coalesced.\n__global__ void fused_kernel(const float* __restrict__ x,\n const float* __restrict__ effT,\n const float* __restrict__ bias,\n float* __restrict__ out,\n int H, int W, int PH, int PW,\n int spatial, int blocks_per_n) {\n int n = blockIdx.y;\n int bx = blockIdx.x;\n int tid = threadIdx.x;\n int lane = tid & 31;\n int warp = tid >> 5; // 0..7 for 256 threads\n int pair = warp >> 1; // 0..3 spatial locations per iteration\n int half = warp & 1; // 0: oc 0..31, 1: oc 32..63\n int oc = (half << 5) + lane;\n\n const float* xN = x + ((long long)n * 8 * H * W);\n float local = 0.0f;\n\n for (int pos = bx * 4 + pair; pos < spatial; pos += blocks_per_n * 4) {\n int ph = pos / PW;\n int pw = pos - ph * PW;\n int base_h = ph << 2;\n int base_w = pw << 2;\n\n float acc = bias[oc];\n int q = 0;\n #pragma unroll\n for (int c = 0; c < 8; ++c) {\n const float* xC = xN + ((long long)c * H * W);\n #pragma unroll\n for (int r = 0; r < 6; ++r) {\n const float* row = xC + ((base_h + r) * W + base_w);\n #pragma unroll\n for (int s = 0; s < 6; ++s, ++q) {\n float xv = 0.0f;\n if (lane == 0) xv = __ldg(row + s);\n xv = __shfl_sync(FULL_MASK, xv, 0);\n float wt = __ldg(effT + q * 64 + oc);\n acc = fmaf(wt, xv, acc);\n }\n }\n }\n\n float sig = 1.0f / (1.0f + __expf(-acc));\n float ws = warp_sum(sig);\n if (lane == 0) local += ws;\n }\n\n __shared__ float smem[8];\n float wsum = warp_sum(local);\n if (lane == 0) smem[warp] = wsum;\n __syncthreads();\n\n if (warp == 0) {\n float v = (lane < 8) ? smem[lane] : 0.0f;\n v = warp_sum(v);\n if (lane == 0) atomicAdd(out + n, v);\n }\n}\n\ntorch::Tensor fused_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias) {\n const int N = (int)x.size(0);\n const int H = (int)x.size(2);\n const int W = (int)x.size(3);\n // Specialized to Conv2d(kernel=3,stride=1,padding=0) then AvgPool2d(kernel=stride=4)\n const int PH = ((H - 2) - 4) / 4 + 1;\n const int PW = ((W - 2) - 4) / 4 + 1;\n const int spatial = PH * PW;\n\n auto out = torch::empty({N}, x.options());\n auto effT = torch::empty({8 * 36 * 64}, x.options());\n\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n make_eff_kernel<<<(64 * 8 * 36 + 255) / 256, 256, 0, stream>>>(\n weight.data_ptr<float>(), effT.data_ptr<float>());\n\n cudaMemsetAsync(out.data_ptr<float>(), 0, (size_t)N * sizeof(float), stream);\n\n const int threads = 256;\n const int blocks_per_n = 256;\n dim3 grid(blocks_per_n, N, 1);\n fused_kernel<<<grid, threads, 0, stream>>>(\n x.data_ptr<float>(), effT.data_ptr<float>(), bias.data_ptr<float>(), out.data_ptr<float>(),\n H, W, PH, PW, spatial, blocks_per_n);\n\n return out;\n}\n\"\"\"\n\n_ext = load_inline(\n name=\"fused_conv_avgpool_sigmoid_sum_ext_v1\",\n cpp_sources=_cpp_src,\n cuda_sources=_cuda_src,\n functions=[\"fused_forward\"],\n extra_cflags=[\"-O3\"],\n extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"],\n verbose=False,\n)\n\n\nclass ModelNew(nn.Module):\n def __init__(self, in_channels, out_channels, kernel_size, pool_kernel_size):\n super(ModelNew, self).__init__()\n self.conv = nn.Conv2d(in_channels, out_channels, kernel_size)\n self.avg_pool = nn.AvgPool2d(pool_kernel_size)\n\n def forward(self, x):\n return _ext.fused_forward(x, self.conv.weight, self.conv.bias)\n"
}
result ← submit_kernel (ok)
oksubmit_kernel
submit_kernel PASSED: (5 / 5) correctness trials all passed.
Kernel runtime: 13.50 μs
Runtime stats: mean=13.50μs median=?μs std=0.02μs
Numerical precision: max_abs_err=2.19e-01 mean_abs_err=5.52e-02 max_rel_err=7.72e-07
Memory: 608.11 MB peak (ref: 5450.19 MB, ratio: 0.1116x)
Kernel launches: 2 (ref: 11, fusion ratio: 5.5)
Energy: 2900.98 mJ/run (ref: 1449.62 mJ, ratio: 0.4997x)
SOL score: 0.154
final submitted kernel (171 lines, 5,685 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_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias);
"""
_cuda_src = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda.h>
#include <cuda_runtime.h>
#define FULL_MASK 0xffffffffu
__inline__ __device__ float warp_sum(float v) {
v += __shfl_down_sync(FULL_MASK, v, 16);
v += __shfl_down_sync(FULL_MASK, v, 8);
v += __shfl_down_sync(FULL_MASK, v, 4);
v += __shfl_down_sync(FULL_MASK, v, 2);
v += __shfl_down_sync(FULL_MASK, v, 1);
return v;
}
// Build transposed effective 6x6 filters: effT[(c*36+r*6+s)*64 + oc]
// for avgpool4(conv3(x)). This makes per-warp weight loads coalesced over oc.
__global__ void make_eff_kernel(const float* __restrict__ w, float* __restrict__ effT) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
int total = 64 * 8 * 36;
if (idx >= total) return;
int oc = idx & 63; // fastest varying: output channel
int q = idx >> 6;
int s = q % 6;
int r = (q / 6) % 6;
int c = q / 36;
float acc = 0.0f;
#pragma unroll
for (int ky = 0; ky < 3; ++ky) {
int dy = r - ky;
if ((unsigned)dy < 4u) {
#pragma unroll
for (int kx = 0; kx < 3; ++kx) {
int dx = s - kx;
if ((unsigned)dx < 4u) {
acc += w[(((oc * 8 + c) * 3 + ky) * 3 + kx)];
}
}
}
}
effT[idx] = acc * 0.0625f;
}
// One block handles several spatial pooled locations for one batch item.
// Each pair of warps computes all 64 output channels for one spatial location:
// lanes are output channels, and lane 0 broadcasts each input value. This avoids
// reloading the same input patch 64 times and keeps effT loads coalesced.
__global__ void fused_kernel(const float* __restrict__ x,
const float* __restrict__ effT,
const float* __restrict__ bias,
float* __restrict__ out,
int H, int W, int PH, int PW,
int spatial, int blocks_per_n) {
int n = blockIdx.y;
int bx = blockIdx.x;
int tid = threadIdx.x;
int lane = tid & 31;
int warp = tid >> 5; // 0..7 for 256 threads
int pair = warp >> 1; // 0..3 spatial locations per iteration
int half = warp & 1; // 0: oc 0..31, 1: oc 32..63
int oc = (half << 5) + lane;
const float* xN = x + ((long long)n * 8 * H * W);
float local = 0.0f;
for (int pos = bx * 4 + pair; pos < spatial; pos += blocks_per_n * 4) {
int ph = pos / PW;
int pw = pos - ph * PW;
int base_h = ph << 2;
int base_w = pw << 2;
float acc = bias[oc];
int q = 0;
#pragma unroll
for (int c = 0; c < 8; ++c) {
const float* xC = xN + ((long long)c * H * W);
#pragma unroll
for (int r = 0; r < 6; ++r) {
const float* row = xC + ((base_h + r) * W + base_w);
#pragma unroll
for (int s = 0; s < 6; ++s, ++q) {
float xv = 0.0f;
if (lane == 0) xv = __ldg(row + s);
xv = __shfl_sync(FULL_MASK, xv, 0);
float wt = __ldg(effT + q * 64 + oc);
acc = fmaf(wt, xv, acc);
}
}
}
float sig = 1.0f / (1.0f + __expf(-acc));
float ws = warp_sum(sig);
if (lane == 0) local += ws;
}
__shared__ float smem[8];
float wsum = warp_sum(local);
if (lane == 0) smem[warp] = wsum;
__syncthreads();
if (warp == 0) {
float v = (lane < 8) ? smem[lane] : 0.0f;
v = warp_sum(v);
if (lane == 0) atomicAdd(out + n, v);
}
}
torch::Tensor fused_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias) {
const int N = (int)x.size(0);
const int H = (int)x.size(2);
const int W = (int)x.size(3);
// Specialized to Conv2d(kernel=3,stride=1,padding=0) then AvgPool2d(kernel=stride=4)
const int PH = ((H - 2) - 4) / 4 + 1;
const int PW = ((W - 2) - 4) / 4 + 1;
const int spatial = PH * PW;
auto out = torch::empty({N}, x.options());
auto effT = torch::empty({8 * 36 * 64}, x.options());
cudaStream_t stream = at::cuda::getCurrentCUDAStream();
make_eff_kernel<<<(64 * 8 * 36 + 255) / 256, 256, 0, stream>>>(
weight.data_ptr<float>(), effT.data_ptr<float>());
cudaMemsetAsync(out.data_ptr<float>(), 0, (size_t)N * sizeof(float), stream);
const int threads = 256;
const int blocks_per_n = 256;
dim3 grid(blocks_per_n, N, 1);
fused_kernel<<<grid, threads, 0, stream>>>(
x.data_ptr<float>(), effT.data_ptr<float>(), bias.data_ptr<float>(), out.data_ptr<float>(),
H, W, PH, PW, spatial, blocks_per_n);
return out;
}
"""
_ext = load_inline(
name="fused_conv_avgpool_sigmoid_sum_ext_v1",
cpp_sources=_cpp_src,
cuda_sources=_cuda_src,
functions=["fused_forward"],
extra_cflags=["-O3"],
extra_cuda_cflags=["-O3", "--use_fast_math"],
verbose=False,
)
class ModelNew(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, pool_kernel_size):
super(ModelNew, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size)
self.avg_pool = nn.AvgPool2d(pool_kernel_size)
def forward(self, x):
return _ext.fused_forward(x, self.conv.weight, self.conv.bias)