call → submit_kernel
callsubmit_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncpp_src = r'''\n#include <torch/extension.h>\n#include <vector>\n\nvoid bn_relu_cuda(torch::Tensor inp, torch::Tensor out, torch::Tensor w, torch::Tensor b,\n torch::Tensor rm, torch::Tensor rv, double eps);\nvoid bn_add_relu_cuda(torch::Tensor conv, torch::Tensor ident, torch::Tensor out,\n torch::Tensor w2, torch::Tensor b2, torch::Tensor rm2, torch::Tensor rv2,\n torch::Tensor wi, torch::Tensor bi, torch::Tensor rmi, torch::Tensor rvi,\n double eps);\nvoid add_relu_inplace_cuda(torch::Tensor a, torch::Tensor b);\n\ntorch::Tensor resblock_forward(torch::Tensor x,\n torch::Tensor conv1_w, torch::Tensor bn1_w, torch::Tensor bn1_b, torch::Tensor bn1_rm, torch::Tensor bn1_rv,\n torch::Tensor conv2_w, torch::Tensor bn2_w, torch::Tensor bn2_b, torch::Tensor bn2_rm, torch::Tensor bn2_rv,\n torch::Tensor down_w, torch::Tensor down_bn_w, torch::Tensor down_bn_b, torch::Tensor down_bn_rm, torch::Tensor down_bn_rv,\n double eps, double momentum, bool training) {\n std::optional<torch::Tensor> bias = std::nullopt;\n std::vector<int64_t> s = {1,1}, p1 = {1,1}, p0 = {0,0};\n\n auto out1 = at::conv2d(x, conv1_w, bias, s, p1, s, 1);\n if (training) {\n auto b1 = at::batch_norm(out1, bn1_w, bn1_b, bn1_rm, bn1_rv, true, momentum, eps, true);\n at::relu_(b1);\n auto out2 = at::conv2d(b1, conv2_w, bias, s, p1, s, 1);\n auto b2 = at::batch_norm(out2, bn2_w, bn2_b, bn2_rm, bn2_rv, true, momentum, eps, true);\n auto ident = at::conv2d(x, down_w, bias, s, p0, s, 1);\n auto bi = at::batch_norm(ident, down_bn_w, down_bn_b, down_bn_rm, down_bn_rv, true, momentum, eps, true);\n add_relu_inplace_cuda(b2, bi);\n return b2;\n } else {\n auto act1 = torch::empty_like(out1);\n bn_relu_cuda(out1, act1, bn1_w, bn1_b, bn1_rm, bn1_rv, eps);\n auto out2 = at::conv2d(act1, conv2_w, bias, s, p1, s, 1);\n auto ident = at::conv2d(x, down_w, bias, s, p0, s, 1);\n auto out = torch::empty_like(out2);\n bn_add_relu_cuda(out2, ident, out, bn2_w, bn2_b, bn2_rm, bn2_rv,\n down_bn_w, down_bn_b, down_bn_rm, down_bn_rv, eps);\n return out;\n }\n}\n'''\n\ncuda_src = r'''\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n\n__global__ void bn_relu_kernel(const float* __restrict__ inp, float* __restrict__ out,\n const float* __restrict__ w, const float* __restrict__ b,\n const float* __restrict__ rm, const float* __restrict__ rv,\n int64_t total, int C, int HW, float eps) {\n int64_t idx = blockIdx.x * (int64_t)blockDim.x + threadIdx.x;\n if (idx >= total) return;\n int c = (idx / HW) % C;\n float val = (inp[idx] - rm[c]) * (w[c] * rsqrtf(rv[c] + eps)) + b[c];\n out[idx] = val > 0.0f ? val : 0.0f;\n}\n\n__global__ void bn_add_relu_kernel(const float* __restrict__ conv, const float* __restrict__ ident,\n float* __restrict__ out,\n const float* __restrict__ w2, const float* __restrict__ b2,\n const float* __restrict__ rm2, const float* __restrict__ rv2,\n const float* __restrict__ wi, const float* __restrict__ bi,\n const float* __restrict__ rmi, const float* __restrict__ rvi,\n int64_t total, int C, int HW, float eps) {\n int64_t idx = blockIdx.x * (int64_t)blockDim.x + threadIdx.x;\n if (idx >= total) return;\n int c = (idx / HW) % C;\n float v2 = (conv[idx] - rm2[c]) * (w2[c] * rsqrtf(rv2[c] + eps)) + b2[c];\n float vi = (ident[idx] - rmi[c]) * (wi[c] * rsqrtf(rvi[c] + eps)) + bi[c];\n float val = v2 + vi;\n out[idx] = val > 0.0f ? val : 0.0f;\n}\n\n__global__ void add_relu_inplace_kernel(float* __restrict__ a, const float* __restrict__ b, int64_t total) {\n int64_t idx = blockIdx.x * (int64_t)blockDim.x + threadIdx.x;\n if (idx >= total) return;\n float v = a[idx] + b[idx];\n a[idx] = v > 0.0f ? v : 0.0f;\n}\n\nvoid bn_relu_cuda(torch::Tensor inp, torch::Tensor out, torch::Tensor w, torch::Tensor b,\n torch::Tensor rm, torch::Tensor rv, double eps) {\n int64_t total = inp.numel();\n int C = inp.size(1);\n int HW = inp.size(2) * inp.size(3);\n int threads = 256;\n int blocks = (total + threads - 1) / threads;\n bn_relu_kernel<<<blocks, threads>>>(inp.data_ptr<float>(), out.data_ptr<float>(),\n w.data_ptr<float>(), b.data_ptr<float>(), rm.data_ptr<float>(), rv.data_ptr<float>(),\n total, C, HW, (float)eps);\n}\n\nvoid bn_add_relu_cuda(torch::Tensor conv, torch::Tensor ident, torch::Tensor out,\n torch::Tensor w2, torch::Tensor b2, torch::Tensor rm2, torch::Tensor rv2,\n torch::Tensor wi, torch::Tensor bi, torch::Tensor rmi, torch::Tensor rvi,\n double eps) {\n int64_t total = conv.numel();\n int C = conv.size(1);\n int HW = conv.size(2) * conv.size(3);\n int threads = 256;\n int blocks = (total + threads - 1) / threads;\n bn_add_relu_kernel<<<blocks, threads>>>(conv.data_ptr<float>(), ident.data_ptr<float>(), out.data_ptr<float>(),\n w2.data_ptr<float>(), b2.data_ptr<float>(), rm2.data_ptr<float>(), rv2.data_ptr<float>(),\n wi.data_ptr<float>(), bi.data_ptr<float>(), rmi.data_ptr<float>(), rvi.data_ptr<float>(),\n total, C, HW, (float)eps);\n}\n\nvoid add_relu_inplace_cuda(torch::Tensor a, torch::Tensor b) {\n int64_t total = a.numel();\n int threads = 256;\n int blocks = (total + threads - 1) / threads;\n add_relu_inplace_kernel<<<blocks, threads>>>(a.data_ptr<float>(), b.data_ptr<float>(), total);\n}\n'''\n\n_ext = load_inline(\n name='resblock_ext_final_submit',\n cpp_sources=[cpp_src],\n cuda_sources=[cuda_src],\n functions=['resblock_forward'],\n verbose=False,\n extra_cflags=['-O3'],\n extra_cuda_cflags=['-O3']\n)\n\nclass ModelNew(nn.Module):\n expansion = 1\n def __init__(self, in_channels, out_channels, stride=1):\n super(ModelNew, self).__init__()\n self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False)\n self.bn1 = nn.BatchNorm2d(out_channels)\n self.relu = nn.ReLU(inplace=True)\n self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False)\n self.bn2 = nn.BatchNorm2d(out_channels)\n self.downsample = nn.Sequential(\n nn.Conv2d(in_channels, out_channels * self.expansion, kernel_size=1, stride=stride, bias=False),\n nn.BatchNorm2d(out_channels * self.expansion),\n )\n self.stride = stride\n\n def forward(self, x):\n return _ext.resblock_forward(\n x.contiguous(),\n self.conv1.weight, self.bn1.weight, self.bn1.bias, self.bn1.running_mean, self.bn1.running_var,\n self.conv2.weight, self.bn2.weight, self.bn2.bias, self.bn2.running_mean, self.bn2.running_var,\n self.downsample[0].weight, self.downsample[1].weight, self.downsample[1].bias,\n self.downsample[1].running_mean, self.downsample[1].running_var,\n float(self.bn1.eps), float(self.bn1.momentum), bool(self.training)\n )\n"
}