call → compile_kernel
callcompile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\ncpp_src=r'''\n#include <torch/extension.h>\ntorch::Tensor causal_conv1d_forward(torch::Tensor x, torch::Tensor w, c10::optional<torch::Tensor> bias_opt, int64_t dilation);\n'''\ncuda_src=r'''\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <mma.h>\nusing namespace nvcuda;\n#define WM 16\n#define WN 16\n#define WK 8\n__device__ __forceinline__ float tf32_round(float x){ unsigned u=__float_as_uint(x); u += 0x00001000u + ((u >> 13) & 1u); u &= 0xffffe000u; return __uint_as_float(u); }\n__global__ void causal_conv1d_wmma_kernel(const float* __restrict__ x,const float* __restrict__ w,const float* __restrict__ bias,float* __restrict__ y,int B,int IC,int L,int OC,int K,int dilation,int pad){\n __shared__ float As[WM*WK]; __shared__ float Bs[WK*WN]; __shared__ float Cs[WM*WN];\n int lane=threadIdx.x; int row0=blockIdx.x*WM; int oc0=blockIdx.y*WN; int total=B*L;\n wmma::fragment<wmma::matrix_a, WM, WN, WK, wmma::precision::tf32, wmma::row_major> a_frag;\n wmma::fragment<wmma::matrix_b, WM, WN, WK, wmma::precision::tf32, wmma::row_major> b_frag;\n wmma::fragment<wmma::accumulator, WM, WN, WK, float> c_frag;\n wmma::fill_fragment(c_frag, 0.0f);\n for(int p0=0;p0<IC*K;p0+=WK){\n for(int idx=lane; idx<WM*WK; idx+=32){ int r=idx/WK, pk=idx-r*WK, p=p0+pk; float val=0.f; int grow=row0+r; if(grow<total&&p<IC*K){ int ic=p/K, kk=p-ic*K; int b=grow/L, t=grow-b*L, xt=t+kk*dilation-pad; if((unsigned)xt<(unsigned)L) val=tf32_round(x[(b*IC+ic)*L+xt]); } As[idx]=val; }\n for(int idx=lane; idx<WK*WN; idx+=32){ int pk=idx/WN, cn=idx-pk*WN, p=p0+pk, goc=oc0+cn; float val=0.f; if(p<IC*K&&goc<OC){ int ic=p/K, kk=p-ic*K; val=tf32_round(w[(goc*IC+ic)*K+kk]); } Bs[idx]=val; }\n __syncthreads();\n wmma::load_matrix_sync(a_frag, As, WK);\n wmma::load_matrix_sync(b_frag, Bs, WN);\n wmma::mma_sync(c_frag, a_frag, b_frag, c_frag);\n __syncthreads();\n }\n wmma::store_matrix_sync(Cs, c_frag, WN, wmma::mem_row_major);\n __syncthreads();\n for(int idx=lane; idx<WM*WN; idx+=32){ int r=idx/WN, cn=idx-r*WN; int row=row0+r, oc=oc0+cn; if(row<total&&oc<OC){ float acc=Cs[idx]; if(bias) acc+=bias[oc]; int b=row/L,t=row-b*L; y[(b*OC+oc)*L+t]=acc; } }\n}\ntorch::Tensor causal_conv1d_forward(torch::Tensor x,torch::Tensor w,c10::optional<torch::Tensor> bias_opt,int64_t dilation64){x=x.contiguous();w=w.contiguous();int B=x.size(0),IC=x.size(1),L=x.size(2),OC=w.size(0),K=w.size(2);int dilation=(int)dilation64,pad=(K-1)*dilation;auto y=torch::empty({B,OC,L},x.options());const float* bp=nullptr;torch::Tensor bt;if(bias_opt.has_value()&&bias_opt.value().defined()){bt=bias_opt.value().contiguous();bp=bt.data_ptr<float>();}dim3 block(32),grid((B*L+WM-1)/WM,(OC+WN-1)/WN);causal_conv1d_wmma_kernel<<<grid,block>>>(x.data_ptr<float>(),w.data_ptr<float>(),bp,y.data_ptr<float>(),B,IC,L,OC,K,dilation,pad);return y;}\n'''\n_ext=load_inline(name='causal_conv1d_wmma_test',cpp_sources=cpp_src,cuda_sources=cuda_src,functions=['causal_conv1d_forward'],extra_cuda_cflags=['-O3'],verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self,in_channels:int,out_channels:int,kernel_size:int,dilation:int=1,bias:bool=False): super().__init__(); self.causal_padding=(kernel_size-1)*dilation; self.dilation=dilation; self.conv1d=nn.Conv1d(in_channels,out_channels,kernel_size,dilation=dilation,bias=bias)\n def forward(self,x): return _ext.causal_conv1d_forward(x,self.conv1d.weight,self.conv1d.bias,self.dilation)\n"
}