char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
31558dbb7
char/aliceai-t5-moe
1#include "lightning-indexer.hpp" 2#include "dequantize.hpp" 3 4static void lightning_indexer_f32_sycl ( 5const char * q ,const char * k ,const char * w ,const char * m ,float * dst , 6int64_t n_embd ,int64_t n_head ,int64_t n_batch ,int64_t n_stream ,int64_t n_kv , 7int64_t nem3 , 8int64_t nbq1 ,int64_t nbq2 ,int64_t nbq3 , 9int64_t nbk2 ,int64_t nbk3 , 10int64_t nbw1 ,int64_t nbw3 , 11int64_t nbm1 ,int64_t nbm3 , 12int64_t nb1 ,int64_t nb3 , 13ggml_type k_type , 14queue_ptr stream ) { 15 16constexpr int64_t LANES = WARP_SIZE ; 17constexpr int64_t ELEMS_PER_LANE = 8 ; 18constexpr int64_t ROWS_PER_BLOCK = 4 ; 19constexpr int64_t BLOCK_SIZE = ROWS_PER_BLOCK * LANES ; 20 21const int64_t n_rows = n_batch * n_stream * n_kv ; 22const int64_t n_blocks = (n_rows + ROWS_PER_BLOCK - 1 ) /ROWS_PER_BLOCK ; 23 24stream -> parallel_for ( 25 sycl::nd_range < 1 > ( 26 sycl::range < 1 > (n_blocks * BLOCK_SIZE ), 27 sycl::range < 1 > (BLOCK_SIZE )), 28 [= ](sycl::nd_item < 1 > item ) [[sycl ::reqd_sub_group_size (WARP_SIZE )]] { 29const int64_t ir = item .get_global_id (0 ); 30const int64_t lane = ir %LANES ; 31const int64_t row = ir /LANES ; 32if (row >=n_rows ) { 33return ; 34 } 35 36const int64_t i_bs = row /n_kv ; 37const int64_t i_kv = row %n_kv ; 38const int64_t i_batch = i_bs /n_stream ; 39const int64_t i_stream = i_bs %n_stream ; 40 41// load K row slice into registers (row is contiguous, nbk0 == type size) 42const char * k_base = k + i_kv * nbk2 + i_stream * nbk3 ; 43float k_local [ELEMS_PER_LANE ]; 44if (k_type == GGML_TYPE_F16 ) { 45const sycl::half * k_row = (const sycl::half * )k_base ; 46#pragma unroll 47for (int64_t j = 0 ;j < ELEMS_PER_LANE ;++ j ) { 48k_local [j ]= static_cast < float > (k_row [lane * ELEMS_PER_LANE + j ]); 49 } 50 }else if (k_type == GGML_TYPE_F32 ) { 51const float * k_row = (const float * )k_base ; 52#pragma unroll 53for (int64_t j = 0 ;j < ELEMS_PER_LANE ;++ j ) { 54k_local [j ]= k_row [lane * ELEMS_PER_LANE + j ]; 55 } 56 }else { 57const int64_t lane_base = lane * ELEMS_PER_LANE ; 58switch (k_type ) { 59case GGML_TYPE_BF16 : { 60const sycl::ext::oneapi::bfloat16 * k_row = (const sycl::ext::oneapi::bfloat16 * )k_base ; 61#pragma unroll 62for (int64_t j = 0 ;j < ELEMS_PER_LANE ;++ j ) { 63k_local [j ]= static_cast < float > (k_row [lane_base + j ]); 64 } 65 }break ; 66case GGML_TYPE_Q4_0 : 67case GGML_TYPE_Q4_1 : 68case GGML_TYPE_Q5_0 : 69case GGML_TYPE_Q5_1 : { 70#pragma unroll 71for (int64_t j = 0 ;j < ELEMS_PER_LANE ;++ j ) { 72const int64_t idx = lane_base + j ; 73const int64_t ib = idx /QK4_0 ; 74const int iqs = idx % (QK4_0 /2 ); 75dfloat2 kv ; 76if (k_type == GGML_TYPE_Q4_0 ) { 77dequantize_q4_0 (k_base ,ib ,iqs ,kv ); 78 }else if (k_type == GGML_TYPE_Q4_1 ) { 79dequantize_q4_1 (k_base ,ib ,iqs ,kv ); 80 }else if (k_type == GGML_TYPE_Q5_0 ) { 81dequantize_q5_0 (k_base ,ib ,iqs ,kv ); 82 }else { 83dequantize_q5_1 (k_base ,ib ,iqs ,kv ); 84 } 85k_local [j ]= (idx %QK4_0 )< (QK4_0 /2 ) ?static_cast < float > (kv .x ()) :static_cast < float > (kv .y ()); 86 } 87 }break ; 88case GGML_TYPE_Q8_0 : { 89#pragma unroll 90for (int64_t pair = 0 ;pair < ELEMS_PER_LANE /2 ;++ pair ) { 91const int64_t elem0 = lane_base + 2 * pair ; 92dfloat2 kv ; 93dequantize_q8_0 (k_base ,elem0 /QK8_0 ,elem0 %QK8_0 ,kv ); 94k_local [2 * pair + 0 ]= static_cast < float > (kv .x ()); 95k_local [2 * pair + 1 ]= static_cast < float > (kv .y ()); 96 } 97 }break ; 98case GGML_TYPE_IQ4_NL : { 99#pragma unroll 100for (int64_t pair = 0 ;pair < ELEMS_PER_LANE /2 ;++ pair ) { 101const int64_t elem0 = lane_base + 2 * pair ; 102dfloat2 kv ; 103dequantize_iq4_nl (k_base ,elem0 /QK4_NL ,elem0 %QK4_NL ,kv ); 104k_local [2 * pair + 0 ]= static_cast < float > (kv .x ()); 105k_local [2 * pair + 1 ]= static_cast < float > (kv .y ()); 106 } 107 }break ; 108default : 109#pragma unroll 110for (int64_t j = 0 ;j < ELEMS_PER_LANE ;++ j ) { 111k_local [j ]= 0.0f ; 112 } 113break ; 114 } 115 } 116 117const char * q_base = q + i_batch * nbq2 + i_stream * nbq3 ; 118const float * w_base = (const float * ) (w + i_batch * nbw1 + i_stream * nbw3 ); 119 120float score = 0.0f ; 121for (int64_t h = 0 ;h < n_head ;++ h ) { 122const float * q_row = (const float * ) (q_base + h * nbq1 ); 123float dot = 0.0f ; 124#pragma unroll 125for (int64_t j = 0 ;j < ELEMS_PER_LANE ;++ j ) { 126const int64_t i = lane * ELEMS_PER_LANE + j ; 127if (i < n_embd ) { 128dot += q_row [i ]* k_local [j ]; 129 } 130 } 131dot = sycl::reduce_over_group (item .get_sub_group (),dot , sycl::plus < float > ()); 132if (lane == 0 ) { 133score += sycl::max (dot ,0.0f )* w_base [h ]; 134 } 135 } 136 137if (lane == 0 ) { 138const sycl::half * m_base = (const sycl::half * ) (m + i_batch * nbm1 + (i_stream %nem3 )* nbm3 ); 139// flat-index store: storing through a strided base pointer 140// hangs/misroutes writes on this stack when n_batch*n_stream > 1 141const int64_t dst_idx = i_kv + i_batch * (nb1 /sizeof (float ))+ i_stream * (nb3 /sizeof (float )); 142dst [dst_idx ]= score + static_cast < float > (m_base [i_kv ]); 143 } 144 }); 145} 146 147void ggml_sycl_op_lightning_indexer (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ) { 148scope_op_debug_print scope_dbg_print (__func__ ,dst ,/*num_src=*/ 4 ); 149const ggml_tensor * q = dst -> src [0 ]; 150const ggml_tensor * k = dst -> src [1 ]; 151const ggml_tensor * w = dst -> src [2 ];// weights 152const ggml_tensor * m = dst -> src [3 ];// mask 153 154GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 155GGML_ASSERT (q -> type == GGML_TYPE_F32 ); 156GGML_ASSERT (w -> type == GGML_TYPE_F32 ); 157GGML_ASSERT (m -> type == GGML_TYPE_F16 ); 158GGML_ASSERT (k -> type == GGML_TYPE_F16 || k -> type == GGML_TYPE_F32 || k -> type == GGML_TYPE_BF16 || 159k -> type == GGML_TYPE_Q8_0 || k -> type == GGML_TYPE_Q5_1 || k -> type == GGML_TYPE_Q5_0 || 160k -> type == GGML_TYPE_Q4_1 || k -> type == GGML_TYPE_Q4_0 || k -> type == GGML_TYPE_IQ4_NL ); 161 162GGML_TENSOR_LOCALS (int64_t ,neq ,q ,ne ); 163GGML_TENSOR_LOCALS (size_t ,nbq ,q ,nb ); 164GGML_TENSOR_LOCALS (int64_t ,nek ,k ,ne ); 165GGML_TENSOR_LOCALS (size_t ,nbk ,k ,nb ); 166GGML_TENSOR_LOCALS (size_t ,nbw ,w ,nb ); 167GGML_TENSOR_LOCALS (int64_t ,nem ,m ,ne ); 168GGML_TENSOR_LOCALS (size_t ,nbm ,m ,nb ); 169GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ); 170GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ); 171 172// input rows must be contiguous 173GGML_ASSERT (nbq0 == ggml_type_size (q -> type )); 174GGML_ASSERT (nbk0 == ggml_type_size (k -> type )); 175GGML_ASSERT (nbm0 == ggml_type_size (m -> type )); 176GGML_ASSERT (nb0 == ggml_type_size (dst -> type )); 177 178const int64_t n_embd = neq0 ; 179const int64_t n_head = neq1 ; 180const int64_t n_batch = neq2 ; 181const int64_t n_stream = neq3 ; 182const int64_t n_kv = nek2 ; 183 184GGML_ASSERT (n_embd == WARP_SIZE * 8 ); 185 186lightning_indexer_f32_sycl ( 187 (const char * )q -> data , (const char * )k -> data , 188 (const char * )w -> data , (const char * )m -> data , (float * )dst -> data , 189n_embd ,n_head ,n_batch ,n_stream ,n_kv ,nem3 , 190nbq1 ,nbq2 ,nbq3 , 191nbk2 ,nbk3 , 192nbw1 ,nbw3 , 193nbm1 ,nbm3 , 194nb1 ,nb3 , 195k -> type , 196ctx .stream ()); 197}