turn 821.5s turn · 21.5s LLM · 0.0s tools
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\n_cpp_src = r'''\n#include <torch/extension.h>\ntorch::Tensor conv_div_lrelu_cuda(torch::Tensor x, torch::Tensor weight, torch::Tensor bias, double divisor);\ntorch::Tensor conv_div_lrelu_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias, double divisor) { return conv_div_lrelu_cuda(x, weight, bias, divisor); }\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__global__ void conv3x3_c8_o64_128_group16_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ b,\n float* __restrict__ y,\n float inv_div) {\n constexpr int C = 8, H = 128, W = 128, OH = 126, OW = 126, HW = 16384;\n constexpr int OC = 64, SP = 15876;\n int p = blockIdx.x * blockDim.x + threadIdx.x;\n if (p >= SP) return;\n int g = blockIdx.y;\n int n = blockIdx.z;\n int oh = p / OW;\n int ow = p - oh * OW;\n int oc0 = g << 4;\n\n float s0=b[oc0+0], s1=b[oc0+1], s2=b[oc0+2], s3=b[oc0+3];\n float s4=b[oc0+4], s5=b[oc0+5], s6=b[oc0+6], s7=b[oc0+7];\n float s8=b[oc0+8], s9=b[oc0+9], s10=b[oc0+10], s11=b[oc0+11];\n float s12=b[oc0+12], s13=b[oc0+13], s14=b[oc0+14], s15=b[oc0+15];\n const int x_base_n = n * C * HW + oh * W + ow;\n #pragma unroll\n for (int c = 0; c < C; ++c) {\n const float* xp = x + x_base_n + c * HW;\n #pragma unroll\n for (int kh = 0; kh < 3; ++kh) {\n #pragma unroll\n for (int kw = 0; kw < 3; ++kw) {\n float xv = xp[kh * W + kw];\n int wi = ((oc0 * C + c) * 3 + kh) * 3 + kw;\n s0=fmaf(xv,w[wi+0],s0); s1=fmaf(xv,w[wi+72],s1);\n s2=fmaf(xv,w[wi+144],s2); s3=fmaf(xv,w[wi+216],s3);\n s4=fmaf(xv,w[wi+288],s4); s5=fmaf(xv,w[wi+360],s5);\n s6=fmaf(xv,w[wi+432],s6); s7=fmaf(xv,w[wi+504],s7);\n s8=fmaf(xv,w[wi+576],s8); s9=fmaf(xv,w[wi+648],s9);\n s10=fmaf(xv,w[wi+720],s10); s11=fmaf(xv,w[wi+792],s11);\n s12=fmaf(xv,w[wi+864],s12); s13=fmaf(xv,w[wi+936],s13);\n s14=fmaf(xv,w[wi+1008],s14); s15=fmaf(xv,w[wi+1080],s15);\n }\n }\n }\n#define ACT(v) do { v *= inv_div; v = v >= 0.0f ? v : v * 0.01f; } while(0)\n ACT(s0); ACT(s1); ACT(s2); ACT(s3); ACT(s4); ACT(s5); ACT(s6); ACT(s7);\n ACT(s8); ACT(s9); ACT(s10); ACT(s11); ACT(s12); ACT(s13); ACT(s14); ACT(s15);\n#undef ACT\n int ybase = ((n * OC + oc0) * OH + oh) * OW + ow;\n y[ybase + 0 * SP]=s0; y[ybase + 1 * SP]=s1; y[ybase + 2 * SP]=s2; y[ybase + 3 * SP]=s3;\n y[ybase + 4 * SP]=s4; y[ybase + 5 * SP]=s5; y[ybase + 6 * SP]=s6; y[ybase + 7 * SP]=s7;\n y[ybase + 8 * SP]=s8; y[ybase + 9 * SP]=s9; y[ybase +10 * SP]=s10; y[ybase +11 * SP]=s11;\n y[ybase +12 * SP]=s12; y[ybase +13 * SP]=s13; y[ybase +14 * SP]=s14; y[ybase +15 * SP]=s15;\n}\n\n__global__ void conv3x3_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, int OC, int K,\n float inv_div) {\n int OH = H - K + 1, OW = W - K + 1;\n int64_t total = (int64_t)N * OC * OH * OW;\n int64_t idx = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;\n if (idx >= total) return;\n int ow = idx % OW;\n int64_t q = idx / OW;\n int oh = q % OH; q /= OH;\n int oc = q % OC;\n int n = q / OC;\n float sum = b[oc];\n for (int c=0;c<C;++c) for(int kh=0;kh<K;++kh) for(int kw=0;kw<K;++kw) {\n sum = fmaf(x[((n*C+c)*H + oh+kh)*W + ow+kw], w[((oc*C+c)*K+kh)*K+kw], sum);\n }\n sum *= inv_div;\n y[idx] = sum >= 0.0f ? sum : sum * 0.01f;\n}\n\ntorch::Tensor conv_div_lrelu_cuda(torch::Tensor x, torch::Tensor weight, torch::Tensor bias, double divisor) {\n int N=(int)x.size(0), C=(int)x.size(1), H=(int)x.size(2), W=(int)x.size(3);\n int OC=(int)weight.size(0), K=(int)weight.size(2);\n int OH=H-K+1, OW=W-K+1;\n auto y = torch::empty({N, OC, OH, OW}, x.options());\n float inv = 1.0f / (float)divisor;\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n int threads = 256;\n if (C == 8 && OC == 64 && K == 3 && H == 128 && W == 128) {\n dim3 grid((15876 + threads - 1) / threads, 4, N);\n conv3x3_c8_o64_128_group16_kernel<<<grid, threads, 0, stream>>>(x.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), y.data_ptr<float>(), inv);\n } else {\n int64_t total = (int64_t)N * OC * OH * OW;\n int blocks = (int)((total + threads - 1) / threads);\n conv3x3_generic_kernel<<<blocks, threads, 0, stream>>>(x.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), y.data_ptr<float>(), N, C, H, W, OC, K, inv);\n }\n return y;\n}\n'''\n\n_ext = load_inline(name='conv_div_lrelu_ext_v4', cpp_sources=_cpp_src, cuda_sources=_cuda_src,\n functions=['conv_div_lrelu_forward'], extra_cuda_cflags=['-O3', '--use_fast_math'], verbose=False)\n\nclass ModelNew(nn.Module):\n def __init__(self, in_channels, out_channels, kernel_size, divisor):\n super(ModelNew, self).__init__()\n self.conv = nn.Conv2d(in_channels, out_channels, kernel_size)\n self.divisor = divisor\n def forward(self, x):\n return _ext.conv_div_lrelu_forward(x, self.conv.weight, self.conv.bias, float(self.divisor))\n"
}
call → static_check
callstatic_check
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\n_cpp_src = r'''\n#include <torch/extension.h>\ntorch::Tensor conv_div_lrelu_cuda(torch::Tensor x, torch::Tensor weight, torch::Tensor bias, double divisor);\ntorch::Tensor conv_div_lrelu_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias, double divisor) { return conv_div_lrelu_cuda(x, weight, bias, divisor); }\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__global__ void conv3x3_c8_o64_128_group16_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ b,\n float* __restrict__ y,\n float inv_div) {\n constexpr int C = 8, H = 128, W = 128, OH = 126, OW = 126, HW = 16384;\n constexpr int OC = 64, SP = 15876;\n int p = blockIdx.x * blockDim.x + threadIdx.x;\n if (p >= SP) return;\n int g = blockIdx.y;\n int n = blockIdx.z;\n int oh = p / OW;\n int ow = p - oh * OW;\n int oc0 = g << 4;\n\n float s0=b[oc0+0], s1=b[oc0+1], s2=b[oc0+2], s3=b[oc0+3];\n float s4=b[oc0+4], s5=b[oc0+5], s6=b[oc0+6], s7=b[oc0+7];\n float s8=b[oc0+8], s9=b[oc0+9], s10=b[oc0+10], s11=b[oc0+11];\n float s12=b[oc0+12], s13=b[oc0+13], s14=b[oc0+14], s15=b[oc0+15];\n const int x_base_n = n * C * HW + oh * W + ow;\n #pragma unroll\n for (int c = 0; c < C; ++c) {\n const float* xp = x + x_base_n + c * HW;\n #pragma unroll\n for (int kh = 0; kh < 3; ++kh) {\n #pragma unroll\n for (int kw = 0; kw < 3; ++kw) {\n float xv = xp[kh * W + kw];\n int wi = ((oc0 * C + c) * 3 + kh) * 3 + kw;\n s0=fmaf(xv,w[wi+0],s0); s1=fmaf(xv,w[wi+72],s1);\n s2=fmaf(xv,w[wi+144],s2); s3=fmaf(xv,w[wi+216],s3);\n s4=fmaf(xv,w[wi+288],s4); s5=fmaf(xv,w[wi+360],s5);\n s6=fmaf(xv,w[wi+432],s6); s7=fmaf(xv,w[wi+504],s7);\n s8=fmaf(xv,w[wi+576],s8); s9=fmaf(xv,w[wi+648],s9);\n s10=fmaf(xv,w[wi+720],s10); s11=fmaf(xv,w[wi+792],s11);\n s12=fmaf(xv,w[wi+864],s12); s13=fmaf(xv,w[wi+936],s13);\n s14=fmaf(xv,w[wi+1008],s14); s15=fmaf(xv,w[wi+1080],s15);\n }\n }\n }\n#define ACT(v) do { v *= inv_div; v = v >= 0.0f ? v : v * 0.01f; } while(0)\n ACT(s0); ACT(s1); ACT(s2); ACT(s3); ACT(s4); ACT(s5); ACT(s6); ACT(s7);\n ACT(s8); ACT(s9); ACT(s10); ACT(s11); ACT(s12); ACT(s13); ACT(s14); ACT(s15);\n#undef ACT\n int ybase = ((n * OC + oc0) * OH + oh) * OW + ow;\n y[ybase + 0 * SP]=s0; y[ybase + 1 * SP]=s1; y[ybase + 2 * SP]=s2; y[ybase + 3 * SP]=s3;\n y[ybase + 4 * SP]=s4; y[ybase + 5 * SP]=s5; y[ybase + 6 * SP]=s6; y[ybase + 7 * SP]=s7;\n y[ybase + 8 * SP]=s8; y[ybase + 9 * SP]=s9; y[ybase +10 * SP]=s10; y[ybase +11 * SP]=s11;\n y[ybase +12 * SP]=s12; y[ybase +13 * SP]=s13; y[ybase +14 * SP]=s14; y[ybase +15 * SP]=s15;\n}\n\n__global__ void conv3x3_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, int OC, int K,\n float inv_div) {\n int OH = H - K + 1, OW = W - K + 1;\n int64_t total = (int64_t)N * OC * OH * OW;\n int64_t idx = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;\n if (idx >= total) return;\n int ow = idx % OW;\n int64_t q = idx / OW;\n int oh = q % OH; q /= OH;\n int oc = q % OC;\n int n = q / OC;\n float sum = b[oc];\n for (int c=0;c<C;++c) for(int kh=0;kh<K;++kh) for(int kw=0;kw<K;++kw) {\n sum = fmaf(x[((n*C+c)*H + oh+kh)*W + ow+kw], w[((oc*C+c)*K+kh)*K+kw], sum);\n }\n sum *= inv_div;\n y[idx] = sum >= 0.0f ? sum : sum * 0.01f;\n}\n\ntorch::Tensor conv_div_lrelu_cuda(torch::Tensor x, torch::Tensor weight, torch::Tensor bias, double divisor) {\n int N=(int)x.size(0), C=(int)x.size(1), H=(int)x.size(2), W=(int)x.size(3);\n int OC=(int)weight.size(0), K=(int)weight.size(2);\n int OH=H-K+1, OW=W-K+1;\n auto y = torch::empty({N, OC, OH, OW}, x.options());\n float inv = 1.0f / (float)divisor;\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n int threads = 256;\n if (C == 8 && OC == 64 && K == 3 && H == 128 && W == 128) {\n dim3 grid((15876 + threads - 1) / threads, 4, N);\n conv3x3_c8_o64_128_group16_kernel<<<grid, threads, 0, stream>>>(x.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), y.data_ptr<float>(), inv);\n } else {\n int64_t total = (int64_t)N * OC * OH * OW;\n int blocks = (int)((total + threads - 1) / threads);\n conv3x3_generic_kernel<<<blocks, threads, 0, stream>>>(x.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), y.data_ptr<float>(), N, C, H, W, OC, K, inv);\n }\n return y;\n}\n'''\n\n_ext = load_inline(name='conv_div_lrelu_ext_v4', cpp_sources=_cpp_src, cuda_sources=_cuda_src,\n functions=['conv_div_lrelu_forward'], extra_cuda_cflags=['-O3', '--use_fast_math'], verbose=False)\n\nclass ModelNew(nn.Module):\n def __init__(self, in_channels, out_channels, kernel_size, divisor):\n super(ModelNew, self).__init__()\n self.conv = nn.Conv2d(in_channels, out_channels, kernel_size)\n self.divisor = divisor\n def forward(self, x):\n return _ext.conv_div_lrelu_forward(x, self.conv.weight, self.conv.bias, float(self.divisor))\n"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (5 / 5) trials all matched the reference.
Numerical precision: max_abs_err=3.58e-07 mean_abs_err=8.41e-09 max_rel_err=1.99e+04
result ← static_check (ok)
okstatic_check
static_check PASSED: no violations or warnings detected.