char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
873c82561
char/aliceai-t5-moe
1#include "upscale.hpp" 2 3static void upscale_f32 (const float * x ,float * dst , 4const int nb00 ,const int nb01 ,const int nb02 ,const int nb03 , 5const int ne10 ,const int ne11 ,const int ne12 ,const int ne13 , 6const float sf0 ,const float sf1 ,const float sf2 ,const float sf3 ) { 7auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 8int index = item_ct1 .get_local_id (2 )+ item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 ); 9if (index >=ne10 * ne11 * ne12 * ne13 ) { 10return ; 11 } 12 13int i10 = index %ne10 ; 14int i11 = (index /ne10 ) %ne11 ; 15int i12 = (index / (ne10 * ne11 )) %ne12 ; 16int i13 = (index / (ne10 * ne11 * ne12 )) %ne13 ; 17 18int i00 = i10 /sf0 ; 19int i01 = i11 /sf1 ; 20int i02 = i12 /sf2 ; 21int i03 = i13 /sf3 ; 22 23dst [index ]= * ((const float * )((const char * )x + i03 * nb03 + i02 * nb02 + 24i01 * nb01 + i00 * nb00 )); 25} 26 27static void upscale_f32_bilinear (const float * x ,float * dst , 28const int nb00 ,const int nb01 ,const int nb02 ,const int nb03 , 29const int ne00_src ,const int ne01_src , 30const int ne10_dst ,const int ne11_dst ,const int ne12_dst ,const int ne13_dst , 31const float sf0 ,const float sf1 ,const float sf2 ,const float sf3 , 32const float pixel_offset ) { 33auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 34const int64_t index = item_ct1 .get_local_id (2 )+ 35item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 ); 36const int64_t dst_total_elements = ne10_dst * ne11_dst * ne12_dst * ne13_dst ; 37 38if (index >=dst_total_elements ) { 39return ; 40 } 41 42const int i10_dst = index %ne10_dst ; 43const int i11_dst = (index /ne10_dst ) %ne11_dst ; 44const int i12_dst = (index / (ne10_dst * ne11_dst )) %ne12_dst ; 45const int i13_dst = index / (ne10_dst * ne11_dst * ne12_dst ); 46 47const int i02_src = (int )(i12_dst /sf2 ); 48const int i03_src = (int )(i13_dst /sf3 ); 49 50const float y_src_f = ((float )i11_dst + pixel_offset ) /sf1 - pixel_offset ; 51int y0_src = (int ) sycl::floor ((float )y_src_f ); 52int y1_src = y0_src + 1 ; 53 54y0_src = sycl::max (0 , sycl::min (y0_src ,ne01_src - 1 )); 55y1_src = sycl::max (0 , sycl::min (y1_src ,ne01_src - 1 )); 56 57float dy = y_src_f - (float )y0_src ; 58dy = sycl::max (0.0f , sycl::min (dy ,1.0f )); 59 60float x_src_f = ((float )i10_dst + pixel_offset ) /sf0 - pixel_offset ; 61int x0_src = (int ) sycl::floor (x_src_f ); 62int x1_src = x0_src + 1 ; 63 64x0_src = sycl::max (0 , sycl::min (x0_src ,ne00_src - 1 )); 65x1_src = sycl::max (0 , sycl::min (x1_src ,ne00_src - 1 )); 66 67float dx = x_src_f - (float )x0_src ; 68dx = sycl::max (0.0f , sycl::min (dx ,1.0f )); 69 70const float * p_a = 71 (const float * )((const char * )x + (int64_t )x0_src * nb00 + 72 (int64_t )y0_src * nb01 + (int64_t )i02_src * nb02 + 73 (int64_t )i03_src * nb03 ); 74const float * p_b = 75 (const float * )((const char * )x + (int64_t )x1_src * nb00 + 76 (int64_t )y0_src * nb01 + (int64_t )i02_src * nb02 + 77 (int64_t )i03_src * nb03 ); 78const float * p_c = 79 (const float * )((const char * )x + (int64_t )x0_src * nb00 + 80 (int64_t )y1_src * nb01 + (int64_t )i02_src * nb02 + 81 (int64_t )i03_src * nb03 ); 82const float * p_d = 83 (const float * )((const char * )x + (int64_t )x1_src * nb00 + 84 (int64_t )y1_src * nb01 + (int64_t )i02_src * nb02 + 85 (int64_t )i03_src * nb03 ); 86 87const float val_a = * p_a ; 88const float val_b = * p_b ; 89const float val_c = * p_c ; 90const float val_d = * p_d ; 91 92float result = val_a * (1.0f - dx )* (1.0f - dy )+ 93val_b * dx * (1.0f - dy )+ 94val_c * (1.0f - dx )* dy + 95val_d * dx * dy ; 96 97dst [index ]= result ; 98} 99 100// Similar to F.interpolate(..., mode="bilinear", align_corners=False, antialias=True) 101// https://github.com/pytorch/pytorch/blob/8871ff29b743948d1225389d5b7068f37b22750b/aten/src/ATen/native/cpu/UpSampleKernel.cpp 102static void upscale_f32_bilinear_antialias (const float * src0 , 103float * dst , 104const int nb00 , 105const int nb01 , 106const int nb02 , 107const int nb03 , 108const int ne00_src , 109const int ne01_src , 110const int ne10_dst , 111const int ne11_dst , 112const int ne12_dst , 113const int ne13_dst , 114const float sf0 , 115const float sf1 , 116const float sf2 , 117const float sf3 , 118const float pixel_offset ) { 119auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 120const int64_t index = item_ct1 .get_local_id (2 )+ 121item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 ); 122const int64_t dst_total_elements = ne10_dst * ne11_dst * ne12_dst * ne13_dst ; 123 124if (index >=dst_total_elements ) { 125return ; 126 } 127 128const int i10_dst = index %ne10_dst ; 129const int i11_dst = (index /ne10_dst ) %ne11_dst ; 130const int i12_dst = (index / (ne10_dst * ne11_dst )) %ne12_dst ; 131const int i13_dst = index / (ne10_dst * ne11_dst * ne12_dst ); 132 133const int i02_src = (int )(i12_dst /sf2 ); 134const int i03_src = (int )(i13_dst /sf3 ); 135 136const float y = ((float )i11_dst + pixel_offset ) /sf1 ; 137const float x = ((float )i10_dst + pixel_offset ) /sf0 ; 138 139// support and invscale, minimum 1 pixel for bilinear 140const float support1 = sycl::max (1.0f /sf1 ,1.0f ); 141const float invscale1 = 1.0f /support1 ; 142const float support0 = sycl::max (1.0f /sf0 ,1.0f ); 143const float invscale0 = 1.0f /support0 ; 144 145// the range of source pixels that contribute 146const int64_t x_min = sycl::max (int64_t (0 ),int64_t (x - support0 + pixel_offset )); 147const int64_t x_max = sycl::min (int64_t (ne00_src ),int64_t (x + support0 + pixel_offset )); 148const int64_t y_min = sycl::max (int64_t (0 ),int64_t (y - support1 + pixel_offset )); 149const int64_t y_max = sycl::min (int64_t (ne01_src ),int64_t (y + support1 + pixel_offset )); 150 151// bilinear filter with antialiasing 152float val = 0.0f ; 153float total_weight = 0.0f ; 154 155auto triangle_filter = [](float x )-> float { 156return sycl::max (1.0f - sycl::fabs (x ),0.0f ); 157 }; 158 159for (int64_t sy = y_min ;sy < y_max ;sy ++ ) { 160const float weight_y = triangle_filter ((sy - y + pixel_offset )* invscale1 ); 161 162for (int64_t sx = x_min ;sx < x_max ;sx ++ ) { 163const float weight_x = triangle_filter ((sx - x + pixel_offset )* invscale0 ); 164const float weight = weight_x * weight_y ; 165 166if (weight <=0.0f ) { 167continue ; 168 } 169 170const float pixel = 171* (const float * )((const char * )src0 + sx * nb00 + sy * nb01 + 172i02_src * nb02 + i03_src * nb03 ); 173val += pixel * weight ; 174total_weight += weight ; 175 } 176 } 177 178if (total_weight > 0.0f ) { 179val /=total_weight ; 180 } 181 182dst [index ]= val ; 183} 184 185namespace bicubic_interpolation { 186static float weight1 (float x ,const float & a ) {return ((a + 2 )* x - (a + 3 ))* x * x + 1 ; }; 187static float weight2 (float x ,const float & a ) {return ((a * x - 5 * a )* x + 8 * a )* x - 4 * a ; }; 188 189static float bicubic (float p0 ,float p1 ,float p2 ,float p3 ,float x ,float a ) { 190const float w0 = weight2 (x + 1 ,a ); 191const float w1 = weight1 (x + 0 ,a ); 192const float w2 = weight1 (1 - x ,a ); 193const float w3 = weight2 (2 - x ,a ); 194return p0 * w0 + p1 * w1 + p2 * w2 + p3 * w3 ; 195}; 196 197} 198 199static void upscale_f32_bicubic (const float * x ,float * dst , 200const int nb00 ,const int nb01 ,const int nb02 ,const int nb03 , 201const int ne00_src ,const int ne01_src , 202const int ne10_dst ,const int ne11_dst ,const int ne12_dst ,const int ne13_dst , 203const float sf0 ,const float sf1 ,const float sf2 ,const float sf3 , 204const float pixel_offset ) { 205auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 206const float a = -0.75f ; 207using bicubic_interpolation::bicubic ; 208 209const int64_t index = item_ct1 .get_local_id (2 )+ 210item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 ); 211const int64_t dst_total_elements = 212ne10_dst * ne11_dst * ne12_dst * ne13_dst ; 213 214if (index >=dst_total_elements ) { 215return ; 216 } 217 218const int i10_dst = index %ne10_dst ; 219const int i11_dst = (index /ne10_dst ) %ne11_dst ; 220const int i12_dst = (index / (ne10_dst * ne11_dst )) %ne12_dst ; 221const int i13_dst = index / (ne10_dst * ne11_dst * ne12_dst ); 222 223const int i02_src = (int )(i12_dst /sf2 ); 224const int i03_src = (int )(i13_dst /sf3 ); 225 226const float y_src_f = ((float )i11_dst + pixel_offset ) /sf1 - pixel_offset ; 227const int y0_src = (int ) sycl::floor ((float )y_src_f ); 228const float dy = y_src_f - (float )y0_src ; 229 230const float x_src_f = ((float )i10_dst + pixel_offset ) /sf0 - pixel_offset ; 231const int x0_src = (int ) sycl::floor ((float )x_src_f ); 232const float dx = x_src_f - (float )x0_src ; 233 234const char * x_base = (const char * )x + (int64_t )i02_src * nb02 + (int64_t )i03_src * nb03 ; 235 236auto load = [= ](int x_off ,int y_off )-> float { 237int i00_src = sycl::max (0 , sycl::min (x0_src + x_off ,ne00_src - 1 )); 238int i01_src = sycl::max (0 , sycl::min (y0_src + y_off ,ne01_src - 1 )); 239return * (const float * )(x_base + (int64_t )i00_src * nb00 + (int64_t )i01_src * nb01 ); 240 }; 241 242const float result = bicubic ( 243bicubic (load (-1 ,-1 ),load (0 ,-1 ),load (1 ,-1 ),load (2 ,-1 ),dx ,a ), 244bicubic (load (-1 ,0 ),load (0 ,0 ),load (1 ,0 ),load (2 ,0 ),dx ,a ), 245bicubic (load (-1 ,1 ),load (0 ,1 ),load (1 ,1 ),load (2 ,1 ),dx ,a ), 246bicubic (load (-1 ,2 ),load (0 ,2 ),load (1 ,2 ),load (2 ,2 ),dx ,a ), 247dy , 248a ); 249 250dst [index ]= result ; 251} 252 253static void upscale_f32_sycl (const float * x , 254float * dst , 255const int nb00 , 256const int nb01 , 257const int nb02 , 258const int nb03 , 259const int ne10 , 260const int ne11 , 261const int ne12 , 262const int ne13 , 263const float sf0 , 264const float sf1 , 265const float sf2 , 266const float sf3 , 267 dpct::queue_ptr stream ) { 268const int64_t dst_size = ne10 * ne11 * ne12 * ne13 ; 269const int64_t num_blocks = (dst_size + SYCL_UPSCALE_BLOCK_SIZE - 1 ) /SYCL_UPSCALE_BLOCK_SIZE ; 270 271stream -> parallel_for ( 272 sycl::nd_range < 3 > ( 273 sycl::range < 3 > (1 ,1 ,num_blocks )* sycl::range < 3 > (1 ,1 ,SYCL_UPSCALE_BLOCK_SIZE ), 274 sycl::range < 3 > (1 ,1 ,SYCL_UPSCALE_BLOCK_SIZE )), 275 [= ](sycl::nd_item < 3 > /*item_ct1*/ ) { 276upscale_f32 (x ,dst ,nb00 ,nb01 ,nb02 ,nb03 ,ne10 ,ne11 ,ne12 ,ne13 ,sf0 ,sf1 ,sf2 ,sf3 ); 277 }); 278} 279 280static void upscale_f32_bilinear_sycl (const float * x , 281float * dst , 282const int nb00 , 283const int nb01 , 284const int nb02 , 285const int nb03 , 286const int ne00_src , 287const int ne01_src , 288const int ne10_dst , 289const int ne11_dst , 290const int ne12_dst , 291const int ne13_dst , 292const float sf0 , 293const float sf1 , 294const float sf2 , 295const float sf3 , 296const float pixel_offset , 297bool antialias , 298 dpct::queue_ptr stream ) { 299const int64_t dst_size = ne10_dst * ne11_dst * ne12_dst * ne13_dst ; 300const int64_t num_blocks = (dst_size + SYCL_UPSCALE_BLOCK_SIZE - 1 ) /SYCL_UPSCALE_BLOCK_SIZE ; 301 302if (antialias ) { 303stream -> parallel_for ( 304 sycl::nd_range < 3 > ( 305 sycl::range < 3 > (1 ,1 ,num_blocks )* sycl::range < 3 > (1 ,1 ,SYCL_UPSCALE_BLOCK_SIZE ), 306 sycl::range < 3 > (1 ,1 ,SYCL_UPSCALE_BLOCK_SIZE )), 307 [= ](sycl::nd_item < 3 > /*item_ct1*/ ) { 308upscale_f32_bilinear_antialias ( 309x ,dst ,nb00 ,nb01 ,nb02 ,nb03 ,ne00_src ,ne01_src ,ne10_dst ,ne11_dst , 310ne12_dst ,ne13_dst ,sf0 ,sf1 ,sf2 ,sf3 ,pixel_offset ); 311 }); 312 }else { 313stream -> parallel_for ( 314 sycl::nd_range < 3 > ( 315 sycl::range < 3 > (1 ,1 ,num_blocks )* sycl::range < 3 > (1 ,1 ,SYCL_UPSCALE_BLOCK_SIZE ), 316 sycl::range < 3 > (1 ,1 ,SYCL_UPSCALE_BLOCK_SIZE )), 317 [= ](sycl::nd_item < 3 > /*item_ct1*/ ) { 318upscale_f32_bilinear ( 319x ,dst ,nb00 ,nb01 ,nb02 ,nb03 ,ne00_src ,ne01_src ,ne10_dst ,ne11_dst ,ne12_dst , 320ne13_dst ,sf0 ,sf1 ,sf2 ,sf3 ,pixel_offset ); 321 }); 322 } 323} 324 325static void upscale_f32_bicubic_sycl (const float * x , 326float * dst , 327const int nb00 , 328const int nb01 , 329const int nb02 , 330const int nb03 , 331const int ne00_src , 332const int ne01_src , 333const int ne10_dst , 334const int ne11_dst , 335const int ne12_dst , 336const int ne13_dst , 337const float sf0 , 338const float sf1 , 339const float sf2 , 340const float sf3 , 341const float pixel_offset , 342 dpct::queue_ptr stream ) { 343const int64_t dst_size = ne10_dst * ne11_dst * ne12_dst * ne13_dst ; 344const int64_t num_blocks = (dst_size + SYCL_UPSCALE_BLOCK_SIZE - 1 ) /SYCL_UPSCALE_BLOCK_SIZE ; 345 346 { 347stream -> submit ([& ](sycl::handler & cgh ) { 348cgh .parallel_for ( 349 sycl::nd_range < 3 > ( 350 sycl::range < 3 > (1 ,1 ,num_blocks )* sycl::range < 3 > (1 ,1 ,SYCL_UPSCALE_BLOCK_SIZE ), 351 sycl::range < 3 > (1 ,1 ,SYCL_UPSCALE_BLOCK_SIZE )), 352 [= ](sycl::nd_item < 3 > /*item_ct1*/ ) { 353upscale_f32_bicubic ( 354x ,dst ,nb00 ,nb01 ,nb02 ,nb03 ,ne00_src ,ne01_src ,ne10_dst ,ne11_dst , 355ne12_dst ,ne13_dst ,sf0 ,sf1 ,sf2 ,sf3 ,pixel_offset ); 356 }); 357 }); 358 } 359} 360 361void ggml_sycl_op_upscale (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ) { 362const ggml_tensor * src0 = dst -> src [0 ]; 363const float * src0_d = (const float * )src0 -> data ; 364float * dst_d = (float * )dst -> data ; 365 dpct::queue_ptr stream = ctx .stream (); 366 367GGML_ASSERT (src0 -> type == GGML_TYPE_F32 ); 368GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 369 370const int mode_flags = dst -> op_params [0 ]; 371const ggml_scale_mode mode = (ggml_scale_mode )(mode_flags & 0xFF ); 372 373float sf0 = (float )dst -> ne [0 ]/src0 -> ne [0 ]; 374float sf1 = (float )dst -> ne [1 ]/src0 -> ne [1 ]; 375float sf2 = (float )dst -> ne [2 ]/src0 -> ne [2 ]; 376const float sf3 = (float )dst -> ne [3 ]/src0 -> ne [3 ]; 377 378float pixel_offset = 0.5f ; 379if (mode_flags & GGML_SCALE_FLAG_ALIGN_CORNERS ) { 380sf0 = dst -> ne [0 ]> 1 && src0 -> ne [0 ]> 1 381 ? (float )(dst -> ne [0 ]- 1 ) / (src0 -> ne [0 ]- 1 ) 382 :sf0 ; 383sf1 = dst -> ne [1 ]> 1 && src0 -> ne [1 ]> 1 384 ? (float )(dst -> ne [1 ]- 1 ) / (src0 -> ne [1 ]- 1 ) 385 :sf1 ; 386pixel_offset = 0.0f ; 387 } 388 389if (mode == GGML_SCALE_MODE_NEAREST ) { 390upscale_f32_sycl ( 391src0_d ,dst_d ,src0 -> nb [0 ],src0 -> nb [1 ],src0 -> nb [2 ],src0 -> nb [3 ], 392dst -> ne [0 ],dst -> ne [1 ],dst -> ne [2 ],dst -> ne [3 ],sf0 ,sf1 ,sf2 ,sf3 ,stream ); 393 }else if (mode == GGML_SCALE_MODE_BILINEAR ) { 394const bool antialias = (mode_flags & GGML_SCALE_FLAG_ANTIALIAS ); 395upscale_f32_bilinear_sycl ( 396src0_d ,dst_d ,src0 -> nb [0 ],src0 -> nb [1 ],src0 -> nb [2 ],src0 -> nb [3 ], 397src0 -> ne [0 ],src0 -> ne [1 ],dst -> ne [0 ],dst -> ne [1 ],dst -> ne [2 ],dst -> ne [3 ], 398sf0 ,sf1 ,sf2 ,sf3 ,pixel_offset ,antialias ,stream ); 399 }else if (mode == GGML_SCALE_MODE_BICUBIC ) { 400upscale_f32_bicubic_sycl ( 401src0_d ,dst_d ,src0 -> nb [0 ],src0 -> nb [1 ],src0 -> nb [2 ],src0 -> nb [3 ], 402src0 -> ne [0 ],src0 -> ne [1 ],dst -> ne [0 ],dst -> ne [1 ],dst -> ne [2 ],dst -> ne [3 ], 403sf0 ,sf1 ,sf2 ,sf3 ,pixel_offset ,stream ); 404 } 405} 406 407void ggml_sycl_upscale (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ) { 408scope_op_debug_print scope_dbg_print (__func__ ,dst ,/*num_src=*/ 1 ); 409ggml_sycl_op_upscale (ctx ,dst ); 410}