call → compile_kernel
callcompile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n_cpp_src = r'''\n#include <torch/extension.h>\ntorch::Tensor forward_cuda(torch::Tensor x, torch::Tensor weight, torch::Tensor bias, torch::Tensor temp, torch::Tensor out);\ntorch::Tensor forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias, torch::Tensor temp, torch::Tensor out) { return forward_cuda(x, weight, bias, temp, out); }\n'''\n_cuda_src = r'''\n#include <torch/extension.h>\n#include <ATen/cuda/CUDAContext.h>\n#include <cuda_runtime.h>\n#include <cublasLt.h>\n#include <math.h>\n__global__ void reduce_lse_kernel(const float* __restrict__ temp, const float* __restrict__ bias, float* __restrict__ out, int N, int has_bias) {\n int row=blockIdx.x, tid=threadIdx.x; const float* base=temp+(size_t)row*N; float mx=-INFINITY;\n for(int j=tid;j<N;j+=blockDim.x){float v=base[j]+(has_bias?bias[j]:0.0f); mx=fmaxf(mx,v);} __shared__ float sm[256]; sm[tid]=mx; __syncthreads();\n for(int s=128;s>0;s>>=1){if(tid<s) sm[tid]=fmaxf(sm[tid],sm[tid+s]); __syncthreads();} float maxv=sm[0], sum=0.f;\n for(int j=tid;j<N;j+=blockDim.x){float v=base[j]+(has_bias?bias[j]:0.0f); sum+=expf(v-maxv);} sm[tid]=sum; __syncthreads();\n for(int s=128;s>0;s>>=1){if(tid<s) sm[tid]+=sm[tid+s]; __syncthreads();} if(tid==0) out[row]=maxv+logf(sm[0]);\n}\nstatic cublasLtHandle_t ltHandle = nullptr;\ntorch::Tensor forward_cuda(torch::Tensor x, torch::Tensor weight, torch::Tensor bias, torch::Tensor temp, torch::Tensor out) {\n int M=(int)x.size(0), K=(int)x.size(1), N=(int)weight.size(0); int has_bias=(bias.numel()!=0); if(ltHandle==nullptr) cublasLtCreate(<Handle);\n cublasOperation_t transa=CUBLAS_OP_T, transb=CUBLAS_OP_N; float alpha=1.f,beta=0.f;\n cublasLtMatmulDesc_t op; cublasLtMatrixLayout_t Adesc,Bdesc,Cdesc;\n cublasLtMatmulDescCreate(&op, CUBLAS_COMPUTE_32F_FAST_TF32, CUDA_R_32F);\n cublasLtMatmulDescSetAttribute(op, CUBLASLT_MATMUL_DESC_TRANSA, &transa, sizeof(transa));\n cublasLtMatmulDescSetAttribute(op, CUBLASLT_MATMUL_DESC_TRANSB, &transb, sizeof(transb));\n cublasLtMatrixLayoutCreate(&Adesc, CUDA_R_32F, K, N, K);\n cublasLtMatrixLayoutCreate(&Bdesc, CUDA_R_32F, K, M, K);\n cublasLtMatrixLayoutCreate(&Cdesc, CUDA_R_32F, N, M, N);\n cublasStatus_t stat=cublasLtMatmul(ltHandle, op, &alpha, weight.data_ptr<float>(), Adesc, x.data_ptr<float>(), Bdesc, &beta, temp.data_ptr<float>(), Cdesc, temp.data_ptr<float>(), Cdesc, nullptr, nullptr, 0, at::cuda::getCurrentCUDAStream());\n cublasLtMatmulDescDestroy(op); cublasLtMatrixLayoutDestroy(Adesc); cublasLtMatrixLayoutDestroy(Bdesc); cublasLtMatrixLayoutDestroy(Cdesc);\n TORCH_CHECK(stat==CUBLAS_STATUS_SUCCESS, \"lt matmul failed\");\n reduce_lse_kernel<<<M,256,0,at::cuda::getCurrentCUDAStream()>>>(temp.data_ptr<float>(), has_bias?bias.data_ptr<float>():nullptr, out.data_ptr<float>(), N, has_bias); return out;\n}\n'''\n_ext=load_inline(name='gemm_lse_lt_v1', cpp_sources=_cpp_src, cuda_sources=_cuda_src, functions=['forward'], extra_cuda_cflags=['-O3'], extra_cflags=['-O3'], extra_ldflags=['-lcublasLt','-lcublas'], verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self,in_features,out_features,bias=True):\n super().__init__(); self.linear=nn.Linear(in_features,out_features,bias=bias); self._temp=None; self._out=None\n def forward(self,x):\n M=x.shape[0]; N=self.linear.weight.shape[0]\n if self._temp is None or self._temp.shape!=(M,N) or self._temp.device!=x.device:\n self._temp=torch.empty((M,N),device=x.device,dtype=x.dtype); self._out=torch.empty((M,1),device=x.device,dtype=x.dtype)\n b=self.linear.bias if self.linear.bias is not None else torch.empty((0,),device=x.device,dtype=x.dtype)\n return _ext.forward(x,self.linear.weight,b,self._temp,self._out)\n"
}