call → compile_kernel
callcompile_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>\ntorch::Tensor deconv1d_forward(torch::Tensor x, torch::Tensor w, int64_t stride, int64_t padding, int64_t dilation, bool has_bias, torch::Tensor bias);\n'''\n\ncuda_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 deconv1d_vec8_kernel(const float* __restrict__ x, const float* __restrict__ w, float* __restrict__ y, int B, int L, int Lout, int T8) {\n long long u = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n long long total = (long long)B * 64LL * T8;\n if (u >= total) return;\n int tb = (int)(u % T8);\n int t = tb * 8;\n int co = (int)((u / T8) & 63LL);\n int b = (int)(u / ((long long)64 * T8));\n const float* xb = x + ((long long)b * 32LL * L);\n float s0=0.f,s1=0.f,s2=0.f,s3=0.f,s4=0.f,s5=0.f,s6=0.f,s7=0.f;\n if (t >= 12 && t + 7 < L) {\n #pragma unroll\n for (int ci=0; ci<32; ++ci) {\n const float* xp = xb + (long long)ci * L + t;\n const float* wp = w + ((ci * 64 + co) * 5);\n float a;\n a=wp[0]; s0=fmaf(xp[0],a,s0); s1=fmaf(xp[1],a,s1); s2=fmaf(xp[2],a,s2); s3=fmaf(xp[3],a,s3); s4=fmaf(xp[4],a,s4); s5=fmaf(xp[5],a,s5); s6=fmaf(xp[6],a,s6); s7=fmaf(xp[7],a,s7);\n a=wp[1]; s0=fmaf(xp[-3],a,s0); s1=fmaf(xp[-2],a,s1); s2=fmaf(xp[-1],a,s2); s3=fmaf(xp[0],a,s3); s4=fmaf(xp[1],a,s4); s5=fmaf(xp[2],a,s5); s6=fmaf(xp[3],a,s6); s7=fmaf(xp[4],a,s7);\n a=wp[2]; s0=fmaf(xp[-6],a,s0); s1=fmaf(xp[-5],a,s1); s2=fmaf(xp[-4],a,s2); s3=fmaf(xp[-3],a,s3); s4=fmaf(xp[-2],a,s4); s5=fmaf(xp[-1],a,s5); s6=fmaf(xp[0],a,s6); s7=fmaf(xp[1],a,s7);\n a=wp[3]; s0=fmaf(xp[-9],a,s0); s1=fmaf(xp[-8],a,s1); s2=fmaf(xp[-7],a,s2); s3=fmaf(xp[-6],a,s3); s4=fmaf(xp[-5],a,s4); s5=fmaf(xp[-4],a,s5); s6=fmaf(xp[-3],a,s6); s7=fmaf(xp[-2],a,s7);\n a=wp[4]; s0=fmaf(xp[-12],a,s0); s1=fmaf(xp[-11],a,s1); s2=fmaf(xp[-10],a,s2); s3=fmaf(xp[-9],a,s3); s4=fmaf(xp[-8],a,s4); s5=fmaf(xp[-7],a,s5); s6=fmaf(xp[-6],a,s6); s7=fmaf(xp[-5],a,s7);\n }\n float* yp = y + ((long long)b * 64 + co) * Lout + t;\n yp[0]=s0; yp[1]=s1; yp[2]=s2; yp[3]=s3; yp[4]=s4; yp[5]=s5; yp[6]=s6; yp[7]=s7;\n } else {\n float ss[8] = {0,0,0,0,0,0,0,0};\n #pragma unroll\n for (int ci=0; ci<32; ++ci) {\n const float* xpbase = xb + (long long)ci * L;\n const float* wp = w + ((ci * 64 + co) * 5);\n #pragma unroll\n for (int j=0;j<8;++j) {\n int tt=t+j; if (tt>=Lout) continue;\n int p=tt; if ((unsigned)p<(unsigned)L) ss[j]=fmaf(xpbase[p],wp[0],ss[j]);\n p=tt-3; if ((unsigned)p<(unsigned)L) ss[j]=fmaf(xpbase[p],wp[1],ss[j]);\n p=tt-6; if ((unsigned)p<(unsigned)L) ss[j]=fmaf(xpbase[p],wp[2],ss[j]);\n p=tt-9; if ((unsigned)p<(unsigned)L) ss[j]=fmaf(xpbase[p],wp[3],ss[j]);\n p=tt-12; if ((unsigned)p<(unsigned)L) ss[j]=fmaf(xpbase[p],wp[4],ss[j]);\n }\n }\n float* yp = y + ((long long)b * 64 + co) * Lout + t;\n #pragma unroll\n for(int j=0;j<8;++j) if(t+j<Lout) yp[j]=ss[j];\n }\n}\n\n__global__ void deconv1d_generic_kernel(const float* __restrict__ x, const float* __restrict__ w, const float* __restrict__ bias,\n float* __restrict__ y, int B, int Cin, int L, int Cout, int K,\n int stride, int padding, int dilation, int Lout, bool has_bias) {\n long long idx = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n long long total = (long long)B * Cout * Lout;\n if (idx >= total) return;\n int t = (int)(idx % Lout);\n int co = (int)((idx / Lout) % Cout);\n int b = (int)(idx / ((long long)Cout * Lout));\n float sum = has_bias ? bias[co] : 0.0f;\n for (int ci=0; ci<Cin; ++ci) {\n const float* xp = x + ((long long)b * Cin + ci) * L;\n const float* wp = w + ((long long)ci * Cout + co) * K;\n for (int k=0; k<K; ++k) { int pos = t + padding - k*dilation; if (pos % stride == 0) { int ip=pos/stride; if ((unsigned)ip<(unsigned)L) sum=fmaf(xp[ip],wp[k],sum); } }\n }\n y[idx]=sum;\n}\n\ntorch::Tensor deconv1d_forward(torch::Tensor x, torch::Tensor w, int64_t stride, int64_t padding, int64_t dilation, bool has_bias, torch::Tensor bias) {\n int B=x.size(0), Cin=x.size(1), L=x.size(2), Cout=w.size(1), K=w.size(2);\n int Lout=(L-1)*(int)stride-2*(int)padding+(int)dilation*(K-1)+1;\n auto y=torch::empty({B,Cout,Lout}, x.options());\n cudaStream_t stream=at::cuda::getCurrentCUDAStream();\n if (x.scalar_type()==at::kFloat && Cin==32 && Cout==64 && K==5 && stride==1 && padding==0 && dilation==3 && !has_bias) {\n int T8=(Lout+7)/8; long long total=(long long)B*64*T8; int threads=256; int blocks=(int)((total+threads-1)/threads);\n deconv1d_vec8_kernel<<<blocks,threads,0,stream>>>(x.data_ptr<float>(), w.data_ptr<float>(), y.data_ptr<float>(), B, L, Lout, T8);\n } else {\n long long total=(long long)B*Cout*Lout; int threads=256; int blocks=(int)((total+threads-1)/threads);\n deconv1d_generic_kernel<<<blocks,threads,0,stream>>>(x.data_ptr<float>(),w.data_ptr<float>(),has_bias?bias.data_ptr<float>():nullptr,y.data_ptr<float>(),B,Cin,L,Cout,K,(int)stride,(int)padding,(int)dilation,Lout,has_bias);\n }\n return y;\n}\n'''\n\n_ext = load_inline(name='deconv1d_ext_vec8', cpp_sources=cpp_src, cuda_sources=cuda_src, functions=['deconv1d_forward'], verbose=False, extra_cuda_cflags=['-O3','--use_fast_math'])\n\nclass ModelNew(nn.Module):\n def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1, bias: bool = False):\n super(ModelNew,self).__init__()\n self.conv1d_transpose=nn.ConvTranspose1d(in_channels,out_channels,kernel_size,stride=stride,padding=padding,dilation=dilation,bias=bias)\n self.stride=stride; self.padding=padding; self.dilation=dilation\n def forward(self,x):\n b=self.conv1d_transpose.bias\n if b is None: b=torch.empty(0,device=x.device,dtype=x.dtype)\n return _ext.deconv1d_forward(x.contiguous(), self.conv1d_transpose.weight.contiguous(), self.stride, self.padding, self.dilation, self.conv1d_transpose.bias is not None, b)\n"
}