call → run_correctness
callrun_correctness
{
"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_cpp = r'''\n#include <torch/extension.h>\ntorch::Tensor add_relu_cuda(torch::Tensor a, torch::Tensor b);\ntorch::Tensor bn_relu_cuda(torch::Tensor x, torch::Tensor weight, torch::Tensor bias, torch::Tensor mean, torch::Tensor var, double eps);\ntorch::Tensor bn_add_relu_cuda(torch::Tensor x, torch::Tensor identity, torch::Tensor weight, torch::Tensor bias, torch::Tensor mean, torch::Tensor var, double eps);\ntorch::Tensor bn2_add_relu_cuda(torch::Tensor x, torch::Tensor id, torch::Tensor w1, torch::Tensor b1, torch::Tensor m1, torch::Tensor v1, double eps1, torch::Tensor w2, torch::Tensor b2, torch::Tensor m2, torch::Tensor v2, double eps2);\ntorch::Tensor gap_cuda(torch::Tensor x);\n'''\n_cuda = r'''\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <ATen/cuda/CUDAContext.h>\n__device__ __forceinline__ int chan_of(long long idx,int C,int H,int W,bool cl){return cl?(int)(idx%C):(int)((idx/(H*W))%C);} \n__global__ void add_relu_kernel(float* __restrict__ a,const float* __restrict__ b,long long total){long long i=blockIdx.x*(long long)blockDim.x+threadIdx.x; if(i<total){float v=a[i]+b[i]; a[i]=v>0.f?v:0.f;}}\n__global__ void bn_relu_kernel(const float* __restrict__ x,float* __restrict__ y,const float* __restrict__ w,const float* __restrict__ b,const float* __restrict__ m,const float* __restrict__ v,long long total,int C,int H,int Wd,float eps,bool cl){long long i=blockIdx.x*(long long)blockDim.x+threadIdx.x; if(i<total){int c=chan_of(i,C,H,Wd,cl); float val=(x[i]-m[c])*(w[c]*rsqrtf(v[c]+eps))+b[c]; y[i]=val>0.f?val:0.f;}}\n__global__ void bn_add_relu_kernel(const float* __restrict__ x,const float* __restrict__ id,float* __restrict__ y,const float* __restrict__ w,const float* __restrict__ b,const float* __restrict__ m,const float* __restrict__ v,long long total,int C,int H,int Wd,float eps,bool cl){long long i=blockIdx.x*(long long)blockDim.x+threadIdx.x; if(i<total){int c=chan_of(i,C,H,Wd,cl); float val=(x[i]-m[c])*(w[c]*rsqrtf(v[c]+eps))+b[c]+id[i]; y[i]=val>0.f?val:0.f;}}\n__global__ void bn2_add_relu_kernel(const float* __restrict__ x,const float* __restrict__ id,float* __restrict__ y,const float* __restrict__ w1,const float* __restrict__ b1,const float* __restrict__ m1,const float* __restrict__ v1,float eps1,const float* __restrict__ w2,const float* __restrict__ b2,const float* __restrict__ m2,const float* __restrict__ v2,float eps2,long long total,int C,int H,int Wd,bool cl){long long i=blockIdx.x*(long long)blockDim.x+threadIdx.x; if(i<total){int c=chan_of(i,C,H,Wd,cl); float val=(x[i]-m1[c])*(w1[c]*rsqrtf(v1[c]+eps1))+b1[c]+(id[i]-m2[c])*(w2[c]*rsqrtf(v2[c]+eps2))+b2[c]; y[i]=val>0.f?val:0.f;}}\n__global__ void gap_kernel(const float* __restrict__ x,float* __restrict__ y,int N,int C,int H,int Wd,bool cl){int idx=blockIdx.x*blockDim.x+threadIdx.x; if(idx<N*C){int c=idx%C,n=idx/C; float s=0.f; if(cl){int base=n*H*Wd*C+c; for(int h=0;h<H;h++) for(int w=0;w<Wd;w++) s+=x[base+(h*Wd+w)*C];}else{int base=n*C*H*Wd+c*H*Wd; for(int k=0;k<H*Wd;k++) s+=x[base+k];} y[idx]=s/(float)(H*Wd);}}\ntorch::Tensor add_relu_cuda(torch::Tensor a,torch::Tensor b){long long total=a.numel(); int threads=256,blocks=(total+threads-1)/threads; add_relu_kernel<<<blocks,threads,0,at::cuda::getCurrentCUDAStream()>>>(a.data_ptr<float>(),b.data_ptr<float>(),total); return a;}\ntorch::Tensor bn_relu_cuda(torch::Tensor x,torch::Tensor weight,torch::Tensor bias,torch::Tensor mean,torch::Tensor var,double eps){auto y=torch::empty_like(x); int C=x.size(1),H=x.size(2),Wd=x.size(3); bool cl=x.is_contiguous(at::MemoryFormat::ChannelsLast); long long total=x.numel(); int threads=256,blocks=(total+threads-1)/threads; bn_relu_kernel<<<blocks,threads,0,at::cuda::getCurrentCUDAStream()>>>(x.data_ptr<float>(),y.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),mean.data_ptr<float>(),var.data_ptr<float>(),total,C,H,Wd,(float)eps,cl); return y;}\ntorch::Tensor bn_add_relu_cuda(torch::Tensor x,torch::Tensor identity,torch::Tensor weight,torch::Tensor bias,torch::Tensor mean,torch::Tensor var,double eps){auto y=torch::empty_like(x); int C=x.size(1),H=x.size(2),Wd=x.size(3); bool cl=x.is_contiguous(at::MemoryFormat::ChannelsLast); long long total=x.numel(); int threads=256,blocks=(total+threads-1)/threads; bn_add_relu_kernel<<<blocks,threads,0,at::cuda::getCurrentCUDAStream()>>>(x.data_ptr<float>(),identity.data_ptr<float>(),y.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),mean.data_ptr<float>(),var.data_ptr<float>(),total,C,H,Wd,(float)eps,cl); return y;}\ntorch::Tensor bn2_add_relu_cuda(torch::Tensor x,torch::Tensor id,torch::Tensor w1,torch::Tensor b1,torch::Tensor m1,torch::Tensor v1,double eps1,torch::Tensor w2,torch::Tensor b2,torch::Tensor m2,torch::Tensor v2,double eps2){auto y=torch::empty_like(x); int C=x.size(1),H=x.size(2),Wd=x.size(3); bool cl=x.is_contiguous(at::MemoryFormat::ChannelsLast); long long total=x.numel(); int threads=256,blocks=(total+threads-1)/threads; bn2_add_relu_kernel<<<blocks,threads,0,at::cuda::getCurrentCUDAStream()>>>(x.data_ptr<float>(),id.data_ptr<float>(),y.data_ptr<float>(),w1.data_ptr<float>(),b1.data_ptr<float>(),m1.data_ptr<float>(),v1.data_ptr<float>(),(float)eps1,w2.data_ptr<float>(),b2.data_ptr<float>(),m2.data_ptr<float>(),v2.data_ptr<float>(),(float)eps2,total,C,H,Wd,cl); return y;}\ntorch::Tensor gap_cuda(torch::Tensor x){int N=x.size(0),C=x.size(1),H=x.size(2),Wd=x.size(3); bool cl=x.is_contiguous(at::MemoryFormat::ChannelsLast); auto y=torch::empty({N,C},x.options()); int total=N*C,threads=256,blocks=(total+threads-1)/threads; gap_kernel<<<blocks,threads,0,at::cuda::getCurrentCUDAStream()>>>(x.data_ptr<float>(),y.data_ptr<float>(),N,C,H,Wd,cl); return y;}\n'''\n_ext=load_inline(name='resnet101_fused_ext3',cpp_sources=_cpp,cuda_sources=_cuda,functions=['add_relu_cuda','bn_relu_cuda','bn_add_relu_cuda','bn2_add_relu_cuda','gap_cuda'],extra_cflags=['-O3'],extra_cuda_cflags=['-O3'],verbose=False)\nclass BottleneckNew(nn.Module):\n expansion=4\n def __init__(self,in_channels,out_channels,stride=1,downsample=None):\n super().__init__(); self.conv1=nn.Conv2d(in_channels,out_channels,1,bias=False); self.bn1=nn.BatchNorm2d(out_channels); self.conv2=nn.Conv2d(out_channels,out_channels,3,stride=stride,padding=1,bias=False); self.bn2=nn.BatchNorm2d(out_channels); self.conv3=nn.Conv2d(out_channels,out_channels*4,1,bias=False); self.bn3=nn.BatchNorm2d(out_channels*4); self.relu=nn.ReLU(inplace=True); self.downsample=downsample; self.stride=stride\n def forward(self,x):\n identity=x\n if self.training:\n out=self.conv1(x); out=self.relu(self.bn1(out)); out=self.conv2(out); out=self.relu(self.bn2(out)); out=self.conv3(out); out=self.bn3(out)\n if self.downsample is not None: identity=self.downsample(x)\n return _ext.add_relu_cuda(out,identity)\n out=F.conv2d(x,self.conv1.weight,None,1,0,1,1); out=_ext.bn_relu_cuda(out,self.bn1.weight,self.bn1.bias,self.bn1.running_mean,self.bn1.running_var,self.bn1.eps); out=F.conv2d(out,self.conv2.weight,None,self.stride,1,1,1); out=_ext.bn_relu_cuda(out,self.bn2.weight,self.bn2.bias,self.bn2.running_mean,self.bn2.running_var,self.bn2.eps); out=F.conv2d(out,self.conv3.weight,None,1,0,1,1)\n if self.downsample is not None:\n idraw=F.conv2d(x,self.downsample[0].weight,None,self.stride,0,1,1); dbn=self.downsample[1]; return _ext.bn2_add_relu_cuda(out,idraw,self.bn3.weight,self.bn3.bias,self.bn3.running_mean,self.bn3.running_var,self.bn3.eps,dbn.weight,dbn.bias,dbn.running_mean,dbn.running_var,dbn.eps)\n return _ext.bn_add_relu_cuda(out,identity,self.bn3.weight,self.bn3.bias,self.bn3.running_mean,self.bn3.running_var,self.bn3.eps)\nclass ModelNew(nn.Module):\n def __init__(self,layers,num_classes=1000):\n super().__init__(); self.in_channels=64; self.conv1=nn.Conv2d(3,64,7,stride=2,padding=3,bias=False); self.bn1=nn.BatchNorm2d(64); self.relu=nn.ReLU(inplace=True); self.maxpool=nn.MaxPool2d(3,stride=2,padding=1); block=BottleneckNew; self.layer1=self._make_layer(block,64,layers[0]); self.layer2=self._make_layer(block,128,layers[1],stride=2); self.layer3=self._make_layer(block,256,layers[2],stride=2); self.layer4=self._make_layer(block,512,layers[3],stride=2); self.avgpool=nn.AdaptiveAvgPool2d((1,1)); self.fc=nn.Linear(2048,num_classes)\n def _make_layer(self,block,out_channels,blocks,stride=1):\n downsample=None\n if stride!=1 or self.in_channels!=out_channels*block.expansion: downsample=nn.Sequential(nn.Conv2d(self.in_channels,out_channels*block.expansion,1,stride=stride,bias=False),nn.BatchNorm2d(out_channels*block.expansion))\n layers=[block(self.in_channels,out_channels,stride,downsample)]; self.in_channels=out_channels*block.expansion\n for _ in range(1,blocks): layers.append(block(self.in_channels,out_channels))\n return nn.Sequential(*layers)\n def forward(self,x):\n if self.training:\n x=self.conv1(x); x=self.relu(self.bn1(x)); x=self.maxpool(x); x=self.layer1(x); x=self.layer2(x); x=self.layer3(x); x=self.layer4(x); x=self.avgpool(x); x=torch.flatten(x,1); return self.fc(x)\n x=F.conv2d(x,self.conv1.weight,None,2,3,1,1); x=_ext.bn_relu_cuda(x,self.bn1.weight,self.bn1.bias,self.bn1.running_mean,self.bn1.running_var,self.bn1.eps); x=self.maxpool(x); x=self.layer1(x); x=self.layer2(x); x=self.layer3(x); x=self.layer4(x); x=_ext.gap_cuda(x); return F.linear(x,self.fc.weight,self.fc.bias)\n"
}