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\ncpp_src=r'''\n#include <torch/extension.h>\nvoid prep_cuda(torch::Tensor x, torch::Tensor out, torch::Tensor temp, torch::Tensor mean, torch::Tensor var, torch::Tensor gamma, torch::Tensor beta, int C);\nvoid copy_new_cuda(torch::Tensor y, torch::Tensor out, int C);\nvoid prep(torch::Tensor x, torch::Tensor out, torch::Tensor temp, torch::Tensor mean, torch::Tensor var, torch::Tensor gamma, torch::Tensor beta, int C){prep_cuda(x,out,temp,mean,var,gamma,beta,C);} \nvoid copy_new(torch::Tensor y, torch::Tensor out, int C){copy_new_cuda(y,out,C);} \n'''\ncuda_src=r'''\n#include <torch/extension.h>\n#include <cuda_runtime.h>\n__global__ void copy_x(const float* x,float* out,int total,int C0,int Ctot,int HW){int i=blockIdx.x*blockDim.x+threadIdx.x;if(i<total){int p=i%HW,c=(i/HW)%C0,n=i/(C0*HW);out[(n*Ctot+c)*HW+p]=x[i];}}\n__global__ void mean_k(const float* out,float* mean,int N,int Ctot,int HW){int c=blockIdx.x,t=threadIdx.x;float s=0;int M=N*HW;for(int i=t;i<M;i+=256){int n=i/HW,p=i-n*HW;s+=out[(n*Ctot+c)*HW+p];}__shared__ float a[256];a[t]=s;__syncthreads();for(int o=128;o;o>>=1){if(t<o)a[t]+=a[t+o];__syncthreads();}if(!t)mean[c]=a[0]/M;}\n__global__ void var_k(const float* out,const float* mean,float* var,int N,int Ctot,int HW){int c=blockIdx.x,t=threadIdx.x;float m=mean[c],s=0;int M=N*HW;for(int i=t;i<M;i+=256){int n=i/HW,p=i-n*HW;float d=out[(n*Ctot+c)*HW+p]-m;s+=d*d;}__shared__ float a[256];a[t]=s;__syncthreads();for(int o=128;o;o>>=1){if(t<o)a[t]+=a[t+o];__syncthreads();}if(!t)var[c]=a[0]/M;}\n__global__ void bn_k(const float* out,float* temp,const float* mean,const float* var,const float* g,const float* b,int total,int C,int Ctot,int HW){int i=blockIdx.x*blockDim.x+threadIdx.x;if(i<total){int p=i%HW,c=(i/HW)%C,n=i/(C*HW);float v=out[(n*Ctot+c)*HW+p];float y=(v-mean[c])*rsqrtf(var[c]+1e-5f)*g[c]+b[c];temp[i]=y>0?y:0;}}\n__global__ void copy_y(const float* y,float* out,int total,int G,int C,int Ctot,int HW){int i=blockIdx.x*blockDim.x+threadIdx.x;if(i<total){int p=i%HW,oc=(i/HW)%G,n=i/(G*HW);out[(n*Ctot+C+oc)*HW+p]=y[i];}}\nvoid prep_cuda(torch::Tensor x, torch::Tensor out, torch::Tensor temp, torch::Tensor mean, torch::Tensor var, torch::Tensor gamma, torch::Tensor beta, int C){int N=x.size(0),C0=x.size(1),H=x.size(2),W=x.size(3),HW=H*W,Ctot=out.size(1);int th=256;if(C==C0){int tot=N*C0*HW;copy_x<<<(tot+th-1)/th,th>>>(x.data_ptr<float>(),out.data_ptr<float>(),tot,C0,Ctot,HW);}mean_k<<<C,256>>>(out.data_ptr<float>(),mean.data_ptr<float>(),N,Ctot,HW);var_k<<<C,256>>>(out.data_ptr<float>(),mean.data_ptr<float>(),var.data_ptr<float>(),N,Ctot,HW);int tot=N*C*HW;bn_k<<<(tot+th-1)/th,th>>>(out.data_ptr<float>(),temp.data_ptr<float>(),mean.data_ptr<float>(),var.data_ptr<float>(),gamma.data_ptr<float>(),beta.data_ptr<float>(),tot,C,Ctot,HW);} \nvoid copy_new_cuda(torch::Tensor y, torch::Tensor out, int C){int N=y.size(0),G=y.size(1),H=y.size(2),W=y.size(3),HW=H*W,Ctot=out.size(1),tot=N*G*HW,th=256;copy_y<<<(tot+th-1)/th,th>>>(y.data_ptr<float>(),out.data_ptr<float>(),tot,G,C,Ctot,HW);} \n'''\n_ext=load_inline(name='dense_hybrid_v1',cpp_sources=[cpp_src],cuda_sources=[cuda_src],functions=['prep','copy_new'],extra_cuda_cflags=['-O3'],extra_cflags=['-O3'],verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self,num_layers:int,num_input_features:int,growth_rate:int):\n super().__init__();self.num_layers=num_layers;self.num_input_features=num_input_features;self.growth_rate=growth_rate;self.layers=nn.ModuleList([nn.Sequential(nn.BatchNorm2d(num_input_features+i*growth_rate),nn.ReLU(inplace=True),nn.Conv2d(num_input_features+i*growth_rate,growth_rate,kernel_size=3,padding=1,bias=False),nn.Dropout(0.0)) for i in range(num_layers)])\n def forward(self,x):\n N,C0,H,W=x.shape; Ctot=C0+self.num_layers*self.growth_rate; out=torch.empty((N,Ctot,H,W),device=x.device,dtype=x.dtype); temp=torch.empty((N,Ctot-self.growth_rate,H,W),device=x.device,dtype=x.dtype); mean=torch.empty((Ctot-self.growth_rate,),device=x.device,dtype=x.dtype); var=torch.empty_like(mean)\n for i in range(self.num_layers):\n C=C0+i*self.growth_rate\n _ext.prep(x,out,temp,mean,var,self.layers[i][0].weight,self.layers[i][0].bias,C)\n y=F.conv2d(temp[:,:C], self.layers[i][2].weight, None, stride=1, padding=1)\n _ext.copy_new(y,out,C)\n return out\n"
}