char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
2b9bd9bf4
char/aliceai-t5-moe
1#include "roll.hpp" 2#include "common.hpp" 3 4using namespace sycl ; 5 6static inline int wrap_add (int i ,int shift ,int n ) { 7 8int s = i + shift ; 9return (s >=n ) ? (s - n ) :s ; 10} 11 12static void kernel_roll_fused_i0_i1 ( 13queue & q , 14const float * src_d , 15float * dst_d , 16int ne0 ,int ne1 ,int ne2 ,int ne3 , 17int sh0 ,int sh1 ,int sh2 ,int sh3 ) 18{ 19if (ne0 == 0 || ne1 == 0 || ne2 == 0 || ne3 == 0 )return ; 20 21 22const int stride1 = ne0 ; 23const int stride2 = ne0 * ne1 ; 24const int stride3 = ne0 * ne1 * ne2 ; 25 26 27const int shNe0 = (ne0 - sh0 ) %ne0 ; 28const int shNe1 = (ne1 - sh1 ) %ne1 ; 29const int shNe2 = (ne2 - sh2 ) %ne2 ; 30const int shNe3 = (ne3 - sh3 ) %ne3 ; 31 32 33const size_t g0 = (size_t )ne3 ; 34const size_t g1 = (size_t )ne2 ; 35const size_t g2 = (size_t ) (ne1 * ne0 ); 36 37const range < 3 > global {g0 ,g1 ,g2 }; 38 39q .submit ([& ](handler & h ) { 40h .parallel_for (global , [= ](id < 3 > idx ) { 41const int i3 = (int )idx [0 ]; 42const int i2 = (int )idx [1 ]; 43 44const int fused = (int )idx [2 ]; 45const int i1 = fused /ne0 ; 46const int i0 = fused - i1 * ne0 ;// fused % ne0 47 48 49const int idx_dst = i0 50+ i1 * stride1 51+ i2 * stride2 52+ i3 * stride3 ; 53 54 55const int s0 = wrap_add (i0 ,shNe0 ,ne0 ); 56const int s1 = wrap_add (i1 ,shNe1 ,ne1 ); 57const int s2 = wrap_add (i2 ,shNe2 ,ne2 ); 58const int s3 = wrap_add (i3 ,shNe3 ,ne3 ); 59 60const int idx_src = s0 61+ s1 * stride1 62+ s2 * stride2 63+ s3 * stride3 ; 64 65dst_d [idx_dst ]= src_d [idx_src ]; 66 }); 67 }); 68} 69 70void ggml_sycl_roll (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ) { 71GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 72 73const ggml_tensor * src = dst -> src [0 ]; 74GGML_ASSERT (src && src -> type == GGML_TYPE_F32 ); 75 76const int ne0 = (int )dst -> ne [0 ]; 77const int ne1 = (int )dst -> ne [1 ]; 78const int ne2 = (int )dst -> ne [2 ]; 79const int ne3 = (int )dst -> ne [3 ]; 80 81const int32_t * params = (const int32_t * )dst -> op_params ; 82int shift0 = params [0 ]; 83int shift1 = params [1 ]; 84int shift2 = params [2 ]; 85int shift3 = params [3 ]; 86 87 88if ((shift0 |shift1 |shift2 |shift3 )== 0 ) { 89const size_t nb = ggml_nbytes (src ); 90queue * q = ctx .stream (); 91SYCL_CHECK (CHECK_TRY_ERROR (q -> memcpy (dst -> data ,src -> data ,nb ))); 92return ; 93 } 94 95auto norm = [](int sh ,int n )-> int { 96if (n <=0 )return 0 ; 97sh %=n ; 98if (sh < 0 )sh += n ; 99return sh ; 100 }; 101shift0 = norm (shift0 ,ne0 ); 102shift1 = norm (shift1 ,ne1 ); 103shift2 = norm (shift2 ,ne2 ); 104shift3 = norm (shift3 ,ne3 ); 105 106try { 107queue * q = ctx .stream (); 108 109const float * src_d = (const float * )src -> data ; 110float * dst_d = (float * )dst -> data ; 111GGML_ASSERT (src_d && dst_d ); 112 113kernel_roll_fused_i0_i1 ( 114* q ,src_d ,dst_d , 115ne0 ,ne1 ,ne2 ,ne3 , 116shift0 ,shift1 ,shift2 ,shift3 117 ); 118 }catch (const std::exception & e ) { 119 std::fprintf (stderr ,"[SYCL-ROLL] ERROR: %s\n" ,e .what ()); 120throw ; 121 } 122}