call → submit_kernel
callsubmit_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\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 CHECK_CUDA(x) TORCH_CHECK(x.is_cuda(), #x \" must be a CUDA tensor\")\n#define CHECK_CONTIGUOUS(x) TORCH_CHECK(x.is_contiguous(), #x \" must be contiguous\")\n#define CHECK_FLOAT(x) TORCH_CHECK(x.scalar_type() == at::kFloat, #x \" must be float32\")\n\n// Specialized fast path for the benchmark case: depthwise 3x3, stride 1, padding 0,\n// NCHW contiguous, channel multiplier 1.\ntemplate<int BW, int BH, int WPT>\n__global__ __launch_bounds__(256, 2)\nvoid dw3x3_s1p0_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ b,\n float* __restrict__ y,\n int N, int C, int H, int W,\n int Ho, int Wo,\n int has_bias) {\n // blockDim = (32, 8). Each thread computes WPT outputs along x.\n constexpr int TX = 32;\n constexpr int SHW = BW + 2;\n constexpr int SHH = BH + 2;\n __shared__ float smem[SHH * SHW];\n __shared__ float sw[9];\n __shared__ float sbias;\n\n const int tx = threadIdx.x;\n const int ty = threadIdx.y;\n const int tid = ty * blockDim.x + tx;\n\n const int nc = blockIdx.z;\n const int n = nc / C;\n const int c = nc - n * C;\n const int h0 = blockIdx.y * BH;\n const int w0 = blockIdx.x * BW;\n\n if (tid < 9) sw[tid] = w[c * 9 + tid];\n if (tid == 9) sbias = has_bias ? b[c] : 0.0f;\n\n // Cooperative load of the input tile needed for the BH x BW output tile.\n const int tile_elems = SHH * SHW;\n const int base = (n * C + c) * H * W;\n for (int idx = tid; idx < tile_elems; idx += 256) {\n int ih = idx / SHW;\n int iw = idx - ih * SHW;\n int gh = h0 + ih;\n int gw = w0 + iw;\n float v = 0.0f;\n // For padding=0 benchmark, right/bottom edge tiles can overrun the logical output tile.\n if (gh < H && gw < W) v = x[base + gh * W + gw];\n smem[idx] = v;\n }\n __syncthreads();\n\n const int oh = h0 + ty;\n if (oh < Ho) {\n #pragma unroll\n for (int i = 0; i < WPT; ++i) {\n int ox_l = tx + i * TX;\n int ow = w0 + ox_l;\n if (ow < Wo) {\n const int sidx = ty * SHW + ox_l;\n float acc = sbias;\n acc = fmaf(smem[sidx + 0], sw[0], acc);\n acc = fmaf(smem[sidx + 1], sw[1], acc);\n acc = fmaf(smem[sidx + 2], sw[2], acc);\n acc = fmaf(smem[sidx + SHW + 0], sw[3], acc);\n acc = fmaf(smem[sidx + SHW + 1], sw[4], acc);\n acc = fmaf(smem[sidx + SHW + 2], sw[5], acc);\n acc = fmaf(smem[sidx + 2*SHW + 0], sw[6], acc);\n acc = fmaf(smem[sidx + 2*SHW + 1], sw[7], acc);\n acc = fmaf(smem[sidx + 2*SHW + 2], sw[8], acc);\n y[((n * C + c) * Ho + oh) * Wo + ow] = acc;\n }\n }\n }\n}\n\n// More general, still simple, fallback for non-special constructor settings (keeps semantics correct\n// for common depthwise cases without calling back into PyTorch conv).\n__global__ void dw_generic_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ b,\n float* __restrict__ y,\n int N, int C, int H, int W,\n int OC, int K, int stride, int pad,\n int Ho, int Wo, int has_bias) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n int total = N * OC * Ho * Wo;\n int mult = OC / C;\n for (; idx < total; idx += blockDim.x * gridDim.x) {\n int t = idx;\n int ow = t % Wo; t /= Wo;\n int oh = t % Ho; t /= Ho;\n int oc = t % OC; t /= OC;\n int n = t;\n int c = oc / mult;\n float acc = has_bias ? b[oc] : 0.0f;\n for (int kh = 0; kh < K; ++kh) {\n int ih = oh * stride + kh - pad;\n if ((unsigned)ih >= (unsigned)H) continue;\n for (int kw = 0; kw < K; ++kw) {\n int iw = ow * stride + kw - pad;\n if ((unsigned)iw < (unsigned)W) {\n acc = fmaf(x[((n*C + c)*H + ih)*W + iw], w[(oc*K + kh)*K + kw], acc);\n }\n }\n }\n y[idx] = acc;\n }\n}\n\ntorch::Tensor depthwise_conv_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,\n int64_t stride, int64_t padding) {\n CHECK_CUDA(x); CHECK_CUDA(weight); CHECK_CUDA(bias);\n CHECK_CONTIGUOUS(x); CHECK_CONTIGUOUS(weight); CHECK_CONTIGUOUS(bias);\n CHECK_FLOAT(x); CHECK_FLOAT(weight); CHECK_FLOAT(bias);\n TORCH_CHECK(x.dim() == 4, \"x must be NCHW\");\n TORCH_CHECK(weight.dim() == 4, \"weight must be OIHW\");\n int N = (int)x.size(0);\n int C = (int)x.size(1);\n int H = (int)x.size(2);\n int W = (int)x.size(3);\n int OC = (int)weight.size(0);\n int K = (int)weight.size(2);\n int has_bias = (bias.numel() != 0);\n int Ho = (H + 2*(int)padding - K) / (int)stride + 1;\n int Wo = (W + 2*(int)padding - K) / (int)stride + 1;\n auto y = torch::empty({N, OC, Ho, Wo}, x.options());\n\n const float* bp = has_bias ? bias.data_ptr<float>() : nullptr;\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n\n if (K == 3 && stride == 1 && padding == 0 && OC == C) {\n constexpr int BW = 128;\n constexpr int BH = 8;\n constexpr int WPT = 4;\n dim3 block(32, 8, 1);\n dim3 grid((Wo + BW - 1) / BW, (Ho + BH - 1) / BH, N * C);\n dw3x3_s1p0_kernel<BW, BH, WPT><<<grid, block, 0, stream>>>(\n x.data_ptr<float>(), weight.data_ptr<float>(), bp, y.data_ptr<float>(),\n N, C, H, W, Ho, Wo, has_bias);\n } else {\n int total = N * OC * Ho * Wo;\n int threads = 256;\n int blocks = min((total + threads - 1) / threads, 65535);\n dw_generic_kernel<<<blocks, threads, 0, stream>>>(\n x.data_ptr<float>(), weight.data_ptr<float>(), bp, y.data_ptr<float>(),\n N, C, H, W, OC, K, (int)stride, (int)padding, Ho, Wo, has_bias);\n }\n return y;\n}\n'''\n\n_ext = load_inline(\n name='dwconv3x3_s1p0_ext_v1',\n cpp_sources='',\n cuda_sources=_cuda_src,\n functions=['depthwise_conv_forward'],\n extra_cuda_cflags=['-O3', '--use_fast_math'],\n extra_cflags=['-O3'],\n verbose=False,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, bias: bool = False):\n super(ModelNew, self).__init__()\n self.conv2d = nn.Conv2d(in_channels, out_channels, kernel_size=(kernel_size, kernel_size), stride=stride, padding=padding, groups=in_channels, bias=bias)\n self.stride = stride\n self.padding = padding\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n # The benchmark supplies contiguous CUDA fp32 tensors. Keeping this lean avoids Python overhead.\n b = self.conv2d.bias\n if b is None:\n b = torch.empty(0, device=x.device, dtype=x.dtype)\n return _ext.depthwise_conv_forward(x, self.conv2d.weight, b, int(self.stride), int(self.padding))\n"
}