okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-sycl-prs e106b4c86 into the batch 16 candidate (SYCL pull requests 4185 and 3153)a8a51b280

main
10.5 KiB196 linesraw
1// FP32 sparse prompt attention with TLA block K loads and cached query tiles.
2#define DPCT_PROFILING_ENABLED
3#include <sycl/sycl.hpp>
4#include <dpct/dpct.hpp>
5#include "strata/sycl_queue.hpp"
6#include "strata/kernels/qsa_prompt_attn_tla.hpp"
7#include <cute/tensor.hpp>
8#include <cute/atom/copy_traits_xe_2d.hpp>
9#include <cfloat>
10#include <cstdio>
11#ifdef printf
12#undef printf
13#endif
14
15namespace strata::kernels::tla_prompt_fp32 {
16constexpr int HD=256,G=12,CH=64,Threads=64;
17class Core;
18class Merge;
19inline float h2f(uint16_t b){return float(sycl::bit_cast<sycl::half>(b));}
20// For every signed byte x, x is reconstructed exactly as
21// bit_cast<float>(0x4b000000 | (uint8_t(x)^0x80)) - (2^23+128).
22// The subtraction is exact over [-128,127]; scaling remains original FP32.
23inline auto exact_signed_bytes(uint64_t bits){
24 using UInt8=uint32_t __attribute__((ext_vector_type(8)));
25 using Float8=float __attribute__((ext_vector_type(8)));
26 const auto words=sycl::bit_cast<sycl::uint2>(bits);
27 const UInt8 data{words[0],words[0],words[0],words[0],words[1],words[1],words[1],words[1]};
28 const UInt8 shifts{0,8,16,24,0,8,16,24};
29 const UInt8 biased=((data>>shifts)&UInt8(255))^UInt8(0x4b000080U);
30 return sycl::bit_cast<Float8>(biased)-Float8(8388736.f);
31}
32// One nontranspose TLA copy supplies the original paired eight-byte inputs.
33// The pinned copy operation declares at least64 elements of operand storage;
34// keep four uint64 registers per lane behind the two-element logical view.
35inline void key16_tla(const QsaAttnPools&p,long long row,int lane,sycl::sub_group,float*out0,float*out16){
36 using namespace cute;
37 const auto*base=reinterpret_cast<const uint64_t*>(p.k_q+row*HD);
38 auto K=make_tensor(make_gmem_ptr(base),make_shape(_4{},_8{}),make_stride(_8{},_1{}));
39 auto cp=make_block_2d_copy(XE_LOAD_2D<64,4,8>{},K);
40 auto ct=cp.get_slice(lane);auto coords=make_identity_tensor(K.shape());
41 auto storage=make_tensor<uint64_t>(make_shape(_4{}));
42 auto layout=make_fragment_like<uint64_t>(ct.partition_S(coords)).layout();
43 auto codes=make_tensor(make_rmem_ptr(storage.data()),layout);
44 copy(cp,ct.partition_S(coords),codes);
45 const auto lo=exact_signed_bytes(codes(0));
46 const auto hi=exact_signed_bytes(codes(1));
47 const float sc0=h2f(p.k_scale[row*4+lane/8]),sc16=h2f(p.k_scale[row*4+lane/8+2]);
48#pragma unroll
49 for(int j=0;j<8;++j){out0[j]=lo[j]*sc0;out16[j]=hi[j]*sc16;}
50}
51// Retain the scalar dot rounding order.
52// Explicit vector FMAs avoid loss of contraction in the vector expression.
53inline sycl::float4 dot8_head4(const float q[4][8],const float*k){
54 // The scalar dot rounds product1, then fuses product0 into it.
55 sycl::float4 v=k[1]*sycl::float4(q[0][1],q[1][1],q[2][1],q[3][1]);
56 v=sycl::fma(sycl::float4(k[0]),sycl::float4(q[0][0],q[1][0],q[2][0],q[3][0]),v);
57#pragma unroll
58 for(int j=2;j<8;++j)v=sycl::fma(sycl::float4(k[j]),sycl::float4(q[0][j],q[1][j],q[2][j],q[3][j]),v);
59 return v;
60}
61void launch(sycl::queue&queue,const float*q,QsaAttnPools p,const int32_t*ids,const int32_t*steps,
62            int cap,QsaShapes sh,float*scratch,float*out,int nq){
63 using namespace cute;
64 const int chunks=(cap+CH-1)/CH,nkv=sh.n_head_kv,heads=sh.n_head,page=sh.page_size;
65 const long long stride=qsa_decode_attn_scratch_floats(cap,sh);
66 float*pm=scratch+size_t(chunks)*heads*HD,*pl=pm+size_t(chunks)*heads;
67 queue.parallel_for<Core>(
68  sycl::nd_range<3>(sycl::range<3>(nq,nkv,size_t(chunks)*Threads),sycl::range<3>(1,1,Threads)),
69  [=](sycl::nd_item<3>it)[[sycl::reqd_sub_group_size(16)]]{
70   const int qi=it.get_group(0),kv=it.get_group(1),chunk=it.get_group(2),t=it.get_local_id(2),lane=t&15,sgid=t>>4;
71   const auto sg=it.get_sub_group();
72   auto&sp=*sycl::ext::oneapi::group_local_memory_for_overwrite<float[G][CH]>(it.get_group());
73   auto&srow=*sycl::ext::oneapi::group_local_memory_for_overwrite<long long[CH]>(it.get_group());
74   const int width=steps[size_t(qi)*kStepCount+kStepWidth],c0=chunk*CH,n_here=sycl::min(CH,width-c0),slot=kv*chunks+chunk;
75   float*pa=scratch+size_t(qi)*stride,*ms=pm+size_t(qi)*stride,*ls=pl+size_t(qi)*stride;
76   const float*query=q+(size_t(qi)*heads+kv*G)*HD;
77   if(n_here<=0){if(t<G){ms[slot*G+t]=-FLT_MAX;ls[slot*G+t]=0.f;}return;}
78   if(t<CH){long long r=-1;if(t<n_here){const int cell=ids[size_t(qi)*cap+c0+t];const int pg=p.page_table[cell/page];if(pg>=0)r=(static_cast<long long>(pg)*nkv+kv)*page+cell%page;}srow[t]=r;}
79   const float down=1.f/16.f;
80   it.barrier(sycl::access::fence_space::local_space);
81    // Existing cute universal 128-bit vector copy, preserving each lane's
82    // original eight FP32 dimensions. Keep TLA block key copy unchanged.
83#pragma clang loop unroll(disable)
84    for(int hg=0;hg<G;hg+=4){
85     float q0[4][8],q16[4][8];
86#pragma unroll
87     for(int h=0;h<4;++h){
88      auto src0=make_tensor(make_gmem_ptr(query+(hg+h)*HD+lane*8),make_shape(_8{}),make_stride(_1{}));
89      auto src16=make_tensor(make_gmem_ptr(query+(hg+h)*HD+(lane+16)*8),make_shape(_8{}),make_stride(_1{}));
90      auto dst0=make_tensor(make_rmem_ptr(&q0[h][0]),make_shape(_8{}),make_stride(_1{}));
91      auto dst16=make_tensor(make_rmem_ptr(&q16[h][0]),make_shape(_8{}),make_stride(_1{}));
92      copy(AutoVectorizingCopyWithAssumedAlignment<128>{},src0,dst0);
93      copy(AutoVectorizingCopyWithAssumedAlignment<128>{},src16,dst16);
94     }
95     for(int c=sgid;c<CH;c+=Threads/16){
96      if(c>=n_here||srow[c]<0){if(lane<4)sp[hg+lane][c]=-FLT_MAX;continue;}
97      float k0[8],k16[8];key16_tla(p,srow[c],lane,sg,k0,k16);
98      sycl::float4 v=dot8_head4(q0,k0)+dot8_head4(q16,k16);
99      // Native reduction retains the lane-zero offset8/4/2/1 tree.
100      v=sycl::reduce_over_group(sg,v,sycl::plus<sycl::float4>{});
101      if(lane==0){
102#pragma unroll
103       for(int h=0;h<4;++h)sp[hg+h][c]=v[h]*down;
104      }
105     }
106    }
107   it.barrier(sycl::access::fence_space::local_space);
108   // Fold original warp32 offset16 first, then reproduce offsets8/4/2/1.
109   for(int h=sgid;h<G;h+=Threads/16){
110    float m=sycl::fmax(sycl::fmax(sp[h][lane],sp[h][lane+32]),sycl::fmax(sp[h][lane+16],sp[h][lane+48]));
111#pragma unroll
112    for(int off=8;off>0;off>>=1)m=sycl::fmax(m,sycl::permute_group_by_xor(sg,m,off));
113    const float ea=(lane<n_here&&srow[lane]>=0)?sycl::native::exp(sp[h][lane]-m):0.f;
114    const float eb=(lane+32<n_here&&srow[lane+32]>=0)?sycl::native::exp(sp[h][lane+32]-m):0.f;
115    const float ec=(lane+16<n_here&&srow[lane+16]>=0)?sycl::native::exp(sp[h][lane+16]-m):0.f;
116    const float ed=(lane+48<n_here&&srow[lane+48]>=0)?sycl::native::exp(sp[h][lane+48]-m):0.f;
117    sp[h][lane]=ea;sp[h][lane+32]=eb;sp[h][lane+16]=ec;sp[h][lane+48]=ed;
118    float l=(ea+eb)+(ec+ed);
119#pragma unroll
120    for(int off=8;off>0;off>>=1)l+=sycl::permute_group_by_xor(sg,l,off);
121    if(lane==0){ms[slot*G+h]=m;ls[slot*G+h]=l;}
122   }
123   it.barrier(sycl::access::fence_space::local_space);
124   const int d=4*t;sycl::float4 acc[G];
125#pragma unroll
126   for(int h=0;h<G;++h)acc[h]=sycl::float4(0.f);
127   for(int c=0;c<n_here;c+=8){long long rows[8];bool full=c+8<=n_here;
128#pragma unroll
129    for(int j=0;j<8;++j){rows[j]=c+j<n_here?srow[c+j]:-1;full=full&&rows[j]>=0;}
130    if(full){sycl::float4 values[8];
131#pragma unroll
132     for(int j=0;j<8;++j){const auto codes=*reinterpret_cast<const sycl::vec<int8_t,4>*>(p.v_q+rows[j]*HD+d);
133      values[j]=codes.convert<float,sycl::rounding_mode::automatic>()*h2f(p.v_scale[rows[j]*4+d/64]);}
134#pragma unroll
135     for(int h=0;h<G;++h){const auto p0=*reinterpret_cast<const sycl::float4*>(&sp[h][c]);const auto p1=*reinterpret_cast<const sycl::float4*>(&sp[h][c+4]);
136      acc[h]=sycl::fma(sycl::float4(p0[0]),values[0],acc[h]);acc[h]=sycl::fma(sycl::float4(p0[1]),values[1],acc[h]);
137      acc[h]=sycl::fma(sycl::float4(p0[2]),values[2],acc[h]);acc[h]=sycl::fma(sycl::float4(p0[3]),values[3],acc[h]);
138      acc[h]=sycl::fma(sycl::float4(p1[0]),values[4],acc[h]);acc[h]=sycl::fma(sycl::float4(p1[1]),values[5],acc[h]);
139      acc[h]=sycl::fma(sycl::float4(p1[2]),values[6],acc[h]);acc[h]=sycl::fma(sycl::float4(p1[3]),values[7],acc[h]);
140     }
141    }else{
142#pragma unroll
143     for(int j=0;j<8;++j){if(rows[j]<0)continue;
144      const auto codes=*reinterpret_cast<const sycl::vec<int8_t,4>*>(p.v_q+rows[j]*HD+d);
145      const auto value=codes.convert<float,sycl::rounding_mode::automatic>()*h2f(p.v_scale[rows[j]*4+d/64]);
146#pragma unroll
147      for(int h=0;h<G;++h)acc[h]=sycl::fma(sycl::float4(sp[h][c+j]),value,acc[h]);
148     }
149    }
150   }
151#pragma unroll
152   for(int h=0;h<G;++h){
153#pragma unroll
154    for(int j=0;j<4;++j)pa[(size_t(slot)*G+h)*HD+d+j]=acc[h][j];}
155  });
156 queue.parallel_for<Merge>(sycl::nd_range<3>(sycl::range<3>(1,nq,size_t(heads)*HD),sycl::range<3>(1,1,HD)),
157  [=](sycl::nd_item<3>it)[[sycl::reqd_sub_group_size(32)]]{
158   const int qi=it.get_group(1),head=it.get_group(2),d=it.get_local_id(2),kv=head/G,h=head%G;
159   const float*pa=scratch+size_t(qi)*stride,*ms=pm+size_t(qi)*stride,*ls=pl+size_t(qi)*stride;
160   float maximum=-FLT_MAX;
161#pragma unroll
162   for(int c=0;c<chunks;++c)maximum=sycl::fmax(maximum,ms[(kv*chunks+c)*G+h]);
163   float sum=0.f,value=0.f;
164   for(int c=0;c<chunks;++c){const int slot=kv*chunks+c;const float m=ms[slot*G+h];if(m==-FLT_MAX)continue;
165    const float w=sycl::native::exp(m-maximum);sum=sycl::fma(ls[slot*G+h],w,sum);value=sycl::fma(pa[(size_t(slot)*G+h)*HD+d],w,value);}
166   out[(size_t(qi)*heads+head)*HD+d]=sum>0.f?value/sum:0.f;
167  });
168}
169}
170namespace strata::kernels {
171bool qsa_prompt_attn_tla_batch(const float*q,const QsaAttnPools&p,const int32_t*ids,const int32_t*steps,int64_t cap,
172 const QsaShapes&sh,float*scratch,float*out,int64_t nq,void*stream){
173 if(!q||!ids||!steps||!scratch||!out||!p.k_q||!p.v_q||!p.k_scale||!p.v_scale||!p.page_table||p.k_q4||p.v_q4||
174    nq<=0||nq>32||cap<=0||cap>32768||sh.head_dim!=256||sh.n_head_kv!=2||sh.n_head!=24||sh.page_size<=0)return false;
175 if((reinterpret_cast<uintptr_t>(p.v_q)&3)||(reinterpret_cast<uintptr_t>(q)&63)||(reinterpret_cast<uintptr_t>(p.k_q)&63))return false;
176 auto*queue=strata::q_of(stream);
177#ifdef STRATA_HAVE_ZE
178 if(queue->get_backend()!=sycl::backend::ext_oneapi_level_zero)return false;
179 const auto device=sycl::get_native<sycl::backend::ext_oneapi_level_zero>(queue->get_device());
180 static thread_local ze_device_handle_t cached_device=nullptr;
181 static thread_local bool supported=false;
182 if(device!=cached_device){
183  ze_device_properties_t props{};props.stype=ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES;
184  supported=zeDeviceGetProperties(device,&props)==ZE_RESULT_SUCCESS&&props.vendorId==0x8086&&props.deviceId==0xe223;
185  cached_device=device;
186 }
187 if(!supported)return false;
188#else
189 return false;
190#endif
191 tla_prompt_fp32::launch(*queue,q,p,ids,steps,int(cap),sh,scratch,out,int(nq));
192 static thread_local bool announced=false;
193 if(!announced){std::fprintf(stderr,"strata: using SYCL TLA FP32 prompt attention\n");announced=true;}
194 return true;
195}
196}