call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n_cpp=r'''\n#include <torch/extension.h>\ntorch::Tensor lstm6_forward(torch::Tensor x, torch::Tensor h0, torch::Tensor c0,\n torch::Tensor wih0, torch::Tensor whh0, torch::Tensor bih0, torch::Tensor bhh0, torch::Tensor wih1, torch::Tensor whh1, torch::Tensor bih1, torch::Tensor bhh1, torch::Tensor wih2, torch::Tensor whh2, torch::Tensor bih2, torch::Tensor bhh2, torch::Tensor wih3, torch::Tensor whh3, torch::Tensor bih3, torch::Tensor bhh3, torch::Tensor wih4, torch::Tensor whh4, torch::Tensor bih4, torch::Tensor bhh4, torch::Tensor wih5, torch::Tensor whh5, torch::Tensor bih5, torch::Tensor bhh5);\n'''\n_cuda=r'''\n#include <torch/extension.h>\n#include <ATen/cuda/CUDAContext.h>\n#include <cuda_runtime.h>\n#include <cooperative_groups.h>\nnamespace cg=cooperative_groups;\n__device__ __forceinline__ float sigm(float x){return 1.f/(1.f+expf(-x));}\n__global__ void k_lstm(const float* __restrict__ x,const float* __restrict__ h0,const float* __restrict__ c0,float* __restrict__ h,float* __restrict__ c,float* __restrict__ gates,const float* __restrict__ wih0,const float* __restrict__ whh0,const float* __restrict__ bih0,const float* __restrict__ bhh0,const float* __restrict__ wih1,const float* __restrict__ whh1,const float* __restrict__ bih1,const float* __restrict__ bhh1,const float* __restrict__ wih2,const float* __restrict__ whh2,const float* __restrict__ bih2,const float* __restrict__ bhh2,const float* __restrict__ wih3,const float* __restrict__ whh3,const float* __restrict__ bih3,const float* __restrict__ bhh3,const float* __restrict__ wih4,const float* __restrict__ whh4,const float* __restrict__ bih4,const float* __restrict__ bhh4,const float* __restrict__ wih5,const float* __restrict__ whh5,const float* __restrict__ bih5,const float* __restrict__ bhh5,int B,int T,int I,int H){\n cg::grid_group grid=cg::this_grid(); int gid=blockIdx.x*blockDim.x+threadIdx.x, stride=gridDim.x*blockDim.x; int total=6*B*H; for(int idx=gid;idx<total;idx+=stride){h[idx]=h0[idx]; c[idx]=c0[idx];} grid.sync();\n for(int t=0;t<T;t++){ for(int l=0;l<6;l++){ const float *wih,*whh,*bih,*bhh; if(l==0){wih=wih0;whh=whh0;bih=bih0;bhh=bhh0;}else if(l==1){wih=wih1;whh=whh1;bih=bih1;bhh=bhh1;}else if(l==2){wih=wih2;whh=whh2;bih=bih2;bhh=bhh2;}else if(l==3){wih=wih3;whh=whh3;bih=bih3;bhh=bhh3;}else if(l==4){wih=wih4;whh=whh4;bih=bih4;bhh=bhh4;}else{wih=wih5;whh=whh5;bih=bih5;bhh=bhh5;} int K=(l==0?I:H); int rows=B*4*H;\n for(int row=gid; row<rows; row+=stride){ int b=row/(4*H); int gh=row-b*(4*H); float sum=bih[gh]+bhh[gh]; const float* wp=wih+gh*K; if(l==0){const float* xp=x+(b*T+t)*I; for(int kk=0;kk<K;kk++) sum=fmaf(wp[kk],xp[kk],sum);} else {const float* inp=h+(((l-1)*B+b)*H); for(int kk=0;kk<K;kk++) sum=fmaf(wp[kk],inp[kk],sum);} const float* hp=h+((l*B+b)*H); const float* wr=whh+gh*H; for(int kk=0;kk<H;kk++) sum=fmaf(wr[kk],hp[kk],sum); gates[row]=sum; }\n grid.sync(); int bh=B*H; for(int idx=gid;idx<bh;idx+=stride){int b=idx/H; int hh=idx-b*H; int base=b*4*H+hh; float ing=sigm(gates[base]); float forget=sigm(gates[base+H]); float cellg=tanhf(gates[base+2*H]); float outg=sigm(gates[base+3*H]); int off=(l*B+b)*H+hh; float cn=forget*c[off]+ing*cellg; c[off]=cn; h[off]=outg*tanhf(cn);} grid.sync(); }} }\n#define PTRS const float* px=x.data_ptr<float>(); const float* ph0=h0.data_ptr<float>(); const float* pc0=c0.data_ptr<float>(); float* ph=h.data_ptr<float>(); float* pc=c.data_ptr<float>(); float* pg=gates.data_ptr<float>(); const float *pwih0=wih0.data_ptr<float>(),*pwhh0=whh0.data_ptr<float>(),*pbih0=bih0.data_ptr<float>(),*pbhh0=bhh0.data_ptr<float>(),*pwih1=wih1.data_ptr<float>(),*pwhh1=whh1.data_ptr<float>(),*pbih1=bih1.data_ptr<float>(),*pbhh1=bhh1.data_ptr<float>(),*pwih2=wih2.data_ptr<float>(),*pwhh2=whh2.data_ptr<float>(),*pbih2=bih2.data_ptr<float>(),*pbhh2=bhh2.data_ptr<float>(),*pwih3=wih3.data_ptr<float>(),*pwhh3=whh3.data_ptr<float>(),*pbih3=bih3.data_ptr<float>(),*pbhh3=bhh3.data_ptr<float>(),*pwih4=wih4.data_ptr<float>(),*pwhh4=whh4.data_ptr<float>(),*pbih4=bih4.data_ptr<float>(),*pbhh4=bhh4.data_ptr<float>(),*pwih5=wih5.data_ptr<float>(),*pwhh5=whh5.data_ptr<float>(),*pbih5=bih5.data_ptr<float>(),*pbhh5=bhh5.data_ptr<float>();\ntorch::Tensor lstm6_forward(torch::Tensor x,torch::Tensor h0,torch::Tensor c0,torch::Tensor wih0,torch::Tensor whh0,torch::Tensor bih0,torch::Tensor bhh0,torch::Tensor wih1,torch::Tensor whh1,torch::Tensor bih1,torch::Tensor bhh1,torch::Tensor wih2,torch::Tensor whh2,torch::Tensor bih2,torch::Tensor bhh2,torch::Tensor wih3,torch::Tensor whh3,torch::Tensor bih3,torch::Tensor bhh3,torch::Tensor wih4,torch::Tensor whh4,torch::Tensor bih4,torch::Tensor bhh4,torch::Tensor wih5,torch::Tensor whh5,torch::Tensor bih5,torch::Tensor bhh5){int B=x.size(0),T=x.size(1),I=x.size(2),H=h0.size(2);auto h=torch::empty_like(h0);auto c=torch::empty_like(c0);auto gates=torch::empty({B,4*H},x.options());PTRS int threads=256,blocks=64;void* args[]={&px,&ph0,&pc0,&ph,&pc,&pg,&pwih0,&pwhh0,&pbih0,&pbhh0,&pwih1,&pwhh1,&pbih1,&pbhh1,&pwih2,&pwhh2,&pbih2,&pbhh2,&pwih3,&pwhh3,&pbih3,&pbhh3,&pwih4,&pwhh4,&pbih4,&pbhh4,&pwih5,&pwhh5,&pbih5,&pbhh5,&B,&T,&I,&H};cudaError_t e=cudaLaunchCooperativeKernel((void*)k_lstm,dim3(blocks),dim3(threads),args,0,at::cuda::getCurrentCUDAStream());TORCH_CHECK(e==cudaSuccess,\"coop launch failed: \",cudaGetErrorString(e));return h;}\n'''\n_ext=load_inline(name='lstm6_one_thread_v1',cpp_sources=[_cpp],cuda_sources=[_cuda],functions=['lstm6_forward'],extra_cuda_cflags=['-O3'],extra_cflags=['-O3'],verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self,input_size,hidden_size,num_layers,output_size,dropout=0.0): super().__init__(); self.lstm=nn.LSTM(input_size,hidden_size,num_layers,batch_first=True,dropout=dropout,bidirectional=False); self.fc=nn.Linear(hidden_size,output_size)\n def forward(self,x,h0,c0): return _ext.lstm6_forward(x.contiguous(),h0.contiguous(),c0.contiguous(),self.lstm.weight_ih_l0.contiguous(),self.lstm.weight_hh_l0.contiguous(),self.lstm.bias_ih_l0.contiguous(),self.lstm.bias_hh_l0.contiguous(),self.lstm.weight_ih_l1.contiguous(),self.lstm.weight_hh_l1.contiguous(),self.lstm.bias_ih_l1.contiguous(),self.lstm.bias_hh_l1.contiguous(),self.lstm.weight_ih_l2.contiguous(),self.lstm.weight_hh_l2.contiguous(),self.lstm.bias_ih_l2.contiguous(),self.lstm.bias_hh_l2.contiguous(),self.lstm.weight_ih_l3.contiguous(),self.lstm.weight_hh_l3.contiguous(),self.lstm.bias_ih_l3.contiguous(),self.lstm.bias_hh_l3.contiguous(),self.lstm.weight_ih_l4.contiguous(),self.lstm.weight_hh_l4.contiguous(),self.lstm.bias_ih_l4.contiguous(),self.lstm.bias_hh_l4.contiguous(),self.lstm.weight_ih_l5.contiguous(),self.lstm.weight_hh_l5.contiguous(),self.lstm.bias_ih_l5.contiguous(),self.lstm.bias_hh_l5.contiguous())\n"
}