call → compile_kernel
callcompile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\n_cpp_src = r'''\n#include <torch/extension.h>\n#include <ATen/cuda/CUDAContext.h>\n#include <cuda_runtime_api.h>\n#include <cstdint>\nextern \"C\" void launch_causal_attn_int8(const int8_t*, const int8_t*, const int8_t*, const float*, const float*, const float*, float*, int, int, int, int, cudaStream_t);\nextern \"C\" void launch_causal_attn_float(const float*, const float*, const float*, const float*, const float*, const float*, float*, int, int, int, int, cudaStream_t);\ntorch::Tensor attn_int8_forward(torch::Tensor q_q, torch::Tensor k_q, torch::Tensor v_q, torch::Tensor q_scale, torch::Tensor k_scale, torch::Tensor v_scale) {\n int B=(int)q_q.size(0), T=(int)q_q.size(1), H=(int)q_q.size(2), D=(int)q_q.size(3);\n auto out=torch::empty({B,T,H,D}, q_q.options().dtype(torch::kFloat32));\n auto stream=at::cuda::getCurrentCUDAStream();\n if(q_q.scalar_type()==torch::kInt8) launch_causal_attn_int8(q_q.data_ptr<int8_t>(),k_q.data_ptr<int8_t>(),v_q.data_ptr<int8_t>(),q_scale.data_ptr<float>(),k_scale.data_ptr<float>(),v_scale.data_ptr<float>(),out.data_ptr<float>(),B,T,H,D,stream);\n else launch_causal_attn_float(q_q.data_ptr<float>(),k_q.data_ptr<float>(),v_q.data_ptr<float>(),q_scale.data_ptr<float>(),k_scale.data_ptr<float>(),v_scale.data_ptr<float>(),out.data_ptr<float>(),B,T,H,D,stream);\n return out;\n}\n'''\n_cuda_src = r'''\n#include <cuda_runtime.h>\n#include <stdint.h>\n#include <math.h>\n#define FULL_MASK 0xffffffffu\n__device__ __forceinline__ int warp_sum_int(int v){\n #pragma unroll\n for(int off=16; off>0; off>>=1) v += __shfl_down_sync(FULL_MASK, v, off);\n return v;\n}\n__device__ __forceinline__ float warp_sum_float(float v){\n #pragma unroll\n for(int off=16; off>0; off>>=1) v += __shfl_down_sync(FULL_MASK, v, off);\n return v;\n}\n__global__ void causal_attn_int8_kernel(const int8_t* __restrict__ q,const int8_t* __restrict__ k,const int8_t* __restrict__ v,const float* __restrict__ qs,const float* __restrict__ ks,const float* __restrict__ vs,float* __restrict__ out,int B,int T,int H,int D){\n int tid=threadIdx.x, lane=tid&31, warp=tid>>5, bid=blockIdx.x;\n int t=bid%T, tmp=bid/T, h=tmp%H, b=tmp/H;\n __shared__ float scores[72]; __shared__ float maxv, invden;\n int qbase=((b*T+t)*H+h)*D; float qk_scale=qs[h]*ks[h]*rsqrtf((float)D);\n for(int i=warp; i<=t; i+=4){\n int kbase=((b*T+i)*H+h)*D;\n int prod=(int)q[qbase+lane]*(int)k[kbase+lane];\n int dot=warp_sum_int(prod);\n if(lane==0) scores[i]=(float)dot*qk_scale;\n }\n __syncthreads();\n if(tid==0){float m=scores[0]; for(int i=1;i<=t;++i){float x=scores[i]; m=x>m?x:m;} maxv=m;}\n __syncthreads();\n if(tid<=t) scores[tid]=expf(scores[tid]-maxv);\n __syncthreads();\n if(tid==0){float s=0.f; for(int i=0;i<=t;++i) s+=scores[i]; invden=1.f/s;}\n __syncthreads();\n if(tid<32){float acc=0.f; for(int i=0;i<=t;++i) acc += scores[i]*(float)v[((b*T+i)*H+h)*D+lane]; out[qbase+lane]=acc*invden*vs[h];}\n}\n__global__ void causal_attn_float_kernel(const float* __restrict__ q,const float* __restrict__ k,const float* __restrict__ v,const float* __restrict__ qs,const float* __restrict__ ks,const float* __restrict__ vs,float* __restrict__ out,int B,int T,int H,int D){\n int tid=threadIdx.x, lane=tid&31, warp=tid>>5, bid=blockIdx.x;\n int t=bid%T, tmp=bid/T, h=tmp%H, b=tmp/H;\n __shared__ float scores[72]; __shared__ float maxv, invden;\n int qbase=((b*T+t)*H+h)*D; float qk_scale=qs[h]*ks[h]*rsqrtf((float)D);\n for(int i=warp; i<=t; i+=4){int kbase=((b*T+i)*H+h)*D; float dot=warp_sum_float(q[qbase+lane]*k[kbase+lane]); if(lane==0) scores[i]=dot*qk_scale;}\n __syncthreads();\n if(tid==0){float m=scores[0]; for(int i=1;i<=t;++i){float x=scores[i]; m=x>m?x:m;} maxv=m;}\n __syncthreads();\n if(tid<=t) scores[tid]=expf(scores[tid]-maxv);\n __syncthreads();\n if(tid==0){float s=0.f; for(int i=0;i<=t;++i) s+=scores[i]; invden=1.f/s;}\n __syncthreads();\n if(tid<32){float acc=0.f; for(int i=0;i<=t;++i) acc += scores[i]*v[((b*T+i)*H+h)*D+lane]; out[qbase+lane]=acc*invden*vs[h];}\n}\nextern \"C\" void launch_causal_attn_int8(const int8_t* q,const int8_t* k,const int8_t* v,const float* qs,const float* ks,const float* vs,float* out,int B,int T,int H,int D,cudaStream_t stream){causal_attn_int8_kernel<<<B*T*H,128,0,stream>>>(q,k,v,qs,ks,vs,out,B,T,H,D);} \nextern \"C\" void launch_causal_attn_float(const float* q,const float* k,const float* v,const float* qs,const float* ks,const float* vs,float* out,int B,int T,int H,int D,cudaStream_t stream){causal_attn_float_kernel<<<B*T*H,128,0,stream>>>(q,k,v,qs,ks,vs,out,B,T,H,D);} \n'''\n_ext=load_inline(name='attn_warp_v1', cpp_sources=_cpp_src, cuda_sources=_cuda_src, functions=['attn_int8_forward'], extra_cuda_cflags=['-O3','--use_fast_math'], verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self): super().__init__()\n def forward(self,q_q,k_q,v_q,q_scale,k_scale,v_scale):\n return _ext.attn_int8_forward(q_q,k_q,v_q,q_scale,k_scale,v_scale)\n"
}