okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a8a51b280
main
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 ){ 24using UInt8 = uint32_t __attribute__((ext_vector_type (8 ))); 25using Float8 = float __attribute__((ext_vector_type (8 ))); 26const auto words = sycl::bit_cast < sycl::uint2 > (bits ); 27const UInt8 data {words [0 ],words [0 ],words [0 ],words [0 ],words [1 ],words [1 ],words [1 ],words [1 ]}; 28const UInt8 shifts {0 ,8 ,16 ,24 ,0 ,8 ,16 ,24 }; 29const UInt8 biased = ((data >>shifts )& UInt8 (255 ))^UInt8 (0x4b000080U ); 30return 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 ){ 36using namespace cute ; 37const auto * base = reinterpret_cast < const uint64_t *> (p .k_q + row * HD ); 38auto K = make_tensor (make_gmem_ptr (base ),make_shape (_4 {},_8 {}),make_stride (_8 {},_1 {})); 39auto cp = make_block_2d_copy (XE_LOAD_2D < 64 ,4 ,8 > {},K ); 40auto ct = cp .get_slice (lane );auto coords = make_identity_tensor (K .shape ()); 41auto storage = make_tensor < uint64_t > (make_shape (_4 {})); 42auto layout = make_fragment_like < uint64_t > (ct .partition_S (coords )).layout (); 43auto codes = make_tensor (make_rmem_ptr (storage .data ()),layout ); 44copy (cp ,ct .partition_S (coords ),codes ); 45const auto lo = exact_signed_bytes (codes (0 )); 46const auto hi = exact_signed_bytes (codes (1 )); 47const float sc0 = h2f (p .k_scale [row * 4 + lane /8 ]),sc16 = h2f (p .k_scale [row * 4 + lane /8 + 2 ]); 48#pragma unroll 49for (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 ]); 56v = sycl::fma (sycl::float4 (k [0 ]),sycl::float4 (q [0 ][0 ],q [1 ][0 ],q [2 ][0 ],q [3 ][0 ]),v ); 57#pragma unroll 58for (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 ); 59return v ; 60} 61void launch (sycl::queue & queue ,const float * q ,QsaAttnPools p ,const int32_t * ids ,const int32_t * steps , 62int cap ,QsaShapes sh ,float * scratch ,float * out ,int nq ){ 63using namespace cute ; 64const int chunks = (cap + CH - 1 )/CH ,nkv = sh .n_head_kv ,heads = sh .n_head ,page = sh .page_size ; 65const long long stride = qsa_decode_attn_scratch_floats (cap ,sh ); 66float * pm = scratch + size_t (chunks )* heads * HD ,* pl = pm + size_t (chunks )* heads ; 67queue .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 )]]{ 70const 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 ; 71const auto sg = it .get_sub_group (); 72auto & sp =* sycl::ext::oneapi::group_local_memory_for_overwrite < float [G ][CH ]> (it .get_group ()); 73auto & srow =* sycl::ext::oneapi::group_local_memory_for_overwrite < long long [CH ]> (it .get_group ()); 74const int width = steps [size_t (qi )* kStepCount + kStepWidth ],c0 = chunk * CH ,n_here = sycl::min (CH ,width - c0 ),slot = kv * chunks + chunk ; 75float * pa = scratch + size_t (qi )* stride ,* ms = pm + size_t (qi )* stride ,* ls = pl + size_t (qi )* stride ; 76const float * query = q + (size_t (qi )* heads + kv * G )* HD ; 77if (n_here <=0 ){if (t < G ){ms [slot * G + t ]=- FLT_MAX ;ls [slot * G + t ]= 0.f ;}return ;} 78if (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 ;} 79const float down = 1.f /16.f ; 80it .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) 84for (int hg = 0 ;hg < G ;hg += 4 ){ 85float q0 [4 ][8 ],q16 [4 ][8 ]; 86#pragma unroll 87for (int h = 0 ;h < 4 ;++ h ){ 88auto src0 = make_tensor (make_gmem_ptr (query + (hg + h )* HD + lane * 8 ),make_shape (_8 {}),make_stride (_1 {})); 89auto src16 = make_tensor (make_gmem_ptr (query + (hg + h )* HD + (lane + 16 )* 8 ),make_shape (_8 {}),make_stride (_1 {})); 90auto dst0 = make_tensor (make_rmem_ptr (& q0 [h ][0 ]),make_shape (_8 {}),make_stride (_1 {})); 91auto dst16 = make_tensor (make_rmem_ptr (& q16 [h ][0 ]),make_shape (_8 {}),make_stride (_1 {})); 92copy (AutoVectorizingCopyWithAssumedAlignment < 128 > {},src0 ,dst0 ); 93copy (AutoVectorizingCopyWithAssumedAlignment < 128 > {},src16 ,dst16 ); 94 } 95for (int c = sgid ;c < CH ;c += Threads /16 ){ 96if (c >=n_here || srow [c ]< 0 ){if (lane < 4 )sp [hg + lane ][c ]=- FLT_MAX ;continue ;} 97float 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. 100v = sycl::reduce_over_group (sg ,v ,sycl::plus < sycl::float4 > {}); 101if (lane == 0 ){ 102#pragma unroll 103for (int h = 0 ;h < 4 ;++ h )sp [hg + h ][c ]= v [h ]* down ; 104 } 105 } 106 } 107it .barrier (sycl::access::fence_space::local_space ); 108// Fold original warp32 offset16 first, then reproduce offsets8/4/2/1. 109for (int h = sgid ;h < G ;h += Threads /16 ){ 110float 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 112for (int off = 8 ;off > 0 ;off >>=1 )m = sycl::fmax (m ,sycl::permute_group_by_xor (sg ,m ,off )); 113const float ea = (lane < n_here && srow [lane ]>= 0 )?sycl::native::exp (sp [h ][lane ]- m ):0.f ; 114const float eb = (lane + 32 < n_here && srow [lane + 32 ]>=0 )?sycl::native::exp (sp [h ][lane + 32 ]- m ):0.f ; 115const float ec = (lane + 16 < n_here && srow [lane + 16 ]>=0 )?sycl::native::exp (sp [h ][lane + 16 ]- m ):0.f ; 116const float ed = (lane + 48 < n_here && srow [lane + 48 ]>=0 )?sycl::native::exp (sp [h ][lane + 48 ]- m ):0.f ; 117sp [h ][lane ]= ea ;sp [h ][lane + 32 ]= eb ;sp [h ][lane + 16 ]= ec ;sp [h ][lane + 48 ]= ed ; 118float l = (ea + eb )+ (ec + ed ); 119#pragma unroll 120for (int off = 8 ;off > 0 ;off >>=1 )l += sycl::permute_group_by_xor (sg ,l ,off ); 121if (lane == 0 ){ms [slot * G + h ]= m ;ls [slot * G + h ]= l ;} 122 } 123it .barrier (sycl::access::fence_space::local_space ); 124const int d = 4 * t ;sycl::float4 acc [G ]; 125#pragma unroll 126for (int h = 0 ;h < G ;++ h )acc [h ]= sycl::float4 (0.f ); 127for (int c = 0 ;c < n_here ;c += 8 ){long long rows [8 ];bool full = c + 8 <=n_here ; 128#pragma unroll 129for (int j = 0 ;j < 8 ;++ j ){rows [j ]= c + j < n_here ?srow [c + j ]:-1 ;full = full && rows [j ]>=0 ;} 130if (full ){sycl::float4 values [8 ]; 131#pragma unroll 132for (int j = 0 ;j < 8 ;++ j ){const auto codes =* reinterpret_cast < const sycl::vec < int8_t ,4 >*> (p .v_q + rows [j ]* HD + d ); 133values [j ]= codes .convert < float ,sycl::rounding_mode::automatic > ()* h2f (p .v_scale [rows [j ]* 4 + d /64 ]);} 134#pragma unroll 135for (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 ]); 136acc [h ]= sycl::fma (sycl::float4 (p0 [0 ]),values [0 ],acc [h ]);acc [h ]= sycl::fma (sycl::float4 (p0 [1 ]),values [1 ],acc [h ]); 137acc [h ]= sycl::fma (sycl::float4 (p0 [2 ]),values [2 ],acc [h ]);acc [h ]= sycl::fma (sycl::float4 (p0 [3 ]),values [3 ],acc [h ]); 138acc [h ]= sycl::fma (sycl::float4 (p1 [0 ]),values [4 ],acc [h ]);acc [h ]= sycl::fma (sycl::float4 (p1 [1 ]),values [5 ],acc [h ]); 139acc [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 143for (int j = 0 ;j < 8 ;++ j ){if (rows [j ]< 0 )continue ; 144const auto codes =* reinterpret_cast < const sycl::vec < int8_t ,4 >*> (p .v_q + rows [j ]* HD + d ); 145const auto value = codes .convert < float ,sycl::rounding_mode::automatic > ()* h2f (p .v_scale [rows [j ]* 4 + d /64 ]); 146#pragma unroll 147for (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 152for (int h = 0 ;h < G ;++ h ){ 153#pragma unroll 154for (int j = 0 ;j < 4 ;++ j )pa [(size_t (slot )* G + h )* HD + d + j ]= acc [h ][j ];} 155 }); 156queue .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 )]]{ 158const int qi = it .get_group (1 ),head = it .get_group (2 ),d = it .get_local_id (2 ),kv = head /G ,h = head %G ; 159const float * pa = scratch + size_t (qi )* stride ,* ms = pm + size_t (qi )* stride ,* ls = pl + size_t (qi )* stride ; 160float maximum =- FLT_MAX ; 161#pragma unroll 162for (int c = 0 ;c < chunks ;++ c )maximum = sycl::fmax (maximum ,ms [(kv * chunks + c )* G + h ]); 163float sum = 0.f ,value = 0.f ; 164for (int c = 0 ;c < chunks ;++ c ){const int slot = kv * chunks + c ;const float m = ms [slot * G + h ];if (m ==- FLT_MAX )continue ; 165const 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 );} 166out [(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 , 172const QsaShapes & sh ,float * scratch ,float * out ,int64_t nq ,void * stream ){ 173if (!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 || 174nq <=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; 175if ((reinterpret_cast < uintptr_t > (p .v_q )& 3 )|| (reinterpret_cast < uintptr_t > (q )& 63 )|| (reinterpret_cast < uintptr_t > (p .k_q )& 63 ))return false; 176auto * queue = strata::q_of (stream ); 177#ifdef STRATA_HAVE_ZE 178if (queue -> get_backend ()!= sycl::backend::ext_oneapi_level_zero )return false; 179const auto device = sycl::get_native < sycl::backend::ext_oneapi_level_zero > (queue -> get_device ()); 180static thread_localze_device_handle_t cached_device = nullptr ; 181static thread_localbool supported = false; 182if (device != cached_device ){ 183ze_device_properties_t props {};props .stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES ; 184supported = zeDeviceGetProperties (device ,& props )== ZE_RESULT_SUCCESS && props .vendorId == 0x8086 && props .deviceId == 0xe223 ; 185cached_device = device ; 186 } 187if (!supported )return false; 188#else 189return false; 190#endif 191 tla_prompt_fp32::launch (* queue ,q ,p ,ids ,steps ,int (cap ),sh ,scratch ,out ,int (nq )); 192static thread_localbool announced = false; 193if (!announced ){std::fprintf (stderr ,"strata: using SYCL TLA FP32 prompt attention\n" );announced = true;} 194return true; 195} 196}