char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
6602dd338
char/aliceai-t5-moe
1// 2// MIT license 3// Copyright (C) 2026 Intel Corporation 4// SPDX-License-Identifier: MIT 5// 6 7// 8// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 9// See https://llvm.org/LICENSE.txt for license information. 10// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 11// 12 13#include "im2col.hpp" 14 15#define MAX_GRIDDIM_Z 65535 16 17template < typename T > 18static void im2col_kernel ( 19const float * x ,T * dst , 20int64_t IC ,int64_t IW ,int64_t IH ,int64_t OH ,int64_t OW ,int64_t KW ,int64_t KH , 21int64_t IC_IH_IW ,int64_t IH_IW ,int64_t N_OH ,int64_t KH_KW ,int64_t IC_KH_KW , 22int s0 ,int s1 ,int p0 ,int p1 ,int d0 ,int d1 ) { 23auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 24const int64_t i = item_ct1 .get_local_id (2 )+ item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 ); 25if (i >=IC_KH_KW ) { 26return ; 27 } 28 29const int64_t iic = i / (KH_KW ); 30const int64_t rem = i - iic * KH_KW ; 31const int64_t ikh = rem /KW ; 32const int64_t ikw = rem - ikh * KW ; 33 34const int64_t iow = item_ct1 .get_group (1 ); 35for (int64_t iz = item_ct1 .get_group (0 );iz < N_OH ;iz += MAX_GRIDDIM_Z ) { 36const int64_t in = iz /OH ; 37const int64_t ioh = iz - in * OH ; 38 39const int64_t iiw = iow * s0 + ikw * d0 - p0 ; 40const int64_t iih = ioh * s1 + ikh * d1 - p1 ; 41 42const int64_t offset_dst = 43 ((in * OH + ioh )* OW + iow )* IC_KH_KW + iic * KH_KW + ikh * KW + ikw ; 44 45if (iih < 0 || iih >=IH || iiw < 0 || iiw >=IW ) { 46dst [offset_dst ]= 0.0f ; 47 }else { 48const int64_t offset_src = iic * IC_IH_IW + in * IH_IW ; 49dst [offset_dst ]= x [offset_src + iih * IW + iiw ]; 50 } 51 } 52 53GGML_UNUSED (IC ); 54GGML_UNUSED (KH ); 55} 56 57// im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW] 58template < typename T > 59static void im2col_sycl (const float * x , 60T * dst , 61int64_t IW , 62int64_t IH , 63int64_t OW , 64int64_t OH , 65int64_t KW , 66int64_t KH , 67int64_t IC , 68int64_t N , 69int64_t IC_IH_IW , 70int64_t IH_IW , 71int s0 , 72int s1 , 73int p0 , 74int p1 , 75int d0 , 76int d1 , 77 dpct::queue_ptr stream ) { 78const int64_t IC_KH_KW = IC * KH * KW ; 79const int64_t num_blocks = (IC_KH_KW + SYCL_IM2COL_BLOCK_SIZE - 1 ) /SYCL_IM2COL_BLOCK_SIZE ; 80const int64_t N_OH = N * OH ; 81const int64_t KH_KW = KW * KH ; 82 dpct::dim3 block_nums (num_blocks ,OW ,MIN (N_OH ,MAX_GRIDDIM_Z )); 83/* 84DPCT1049:73: The work-group size passed to the SYCL kernel may exceed the limit. To get the device limit, query info::device::max_work_group_size. Adjust the work-group size if needed. 85*/ 86stream -> parallel_for (sycl::nd_range < 3 > (block_nums * sycl::range < 3 > (1 ,1 ,MIN (IC_KH_KW ,SYCL_IM2COL_BLOCK_SIZE )), 87 sycl::range < 3 > (1 ,1 ,MIN (IC_KH_KW ,SYCL_IM2COL_BLOCK_SIZE ))), 88 [= ](sycl::nd_item < 3 > ) { 89im2col_kernel (x ,dst ,IC ,IW ,IH ,OH ,OW ,KW ,KH ,IC_IH_IW ,IH_IW ,N_OH ,KH_KW ,IC_KH_KW , 90s0 ,s1 ,p0 ,p1 ,d0 ,d1 ); 91 }); 92} 93 94static void im2col_sycl_f16 (const float * x , 95 sycl::half * dst , 96int64_t IW , 97int64_t IH , 98int64_t OW , 99int64_t OH , 100int64_t KW , 101int64_t KH , 102int64_t IC , 103int64_t N , 104int64_t IC_IH_IW , 105int64_t IH_IW , 106int s0 , 107int s1 , 108int p0 , 109int p1 , 110int d0 , 111int d1 , 112 dpct::queue_ptr stream ) { 113im2col_sycl < sycl::half > (x ,dst ,IW ,IH ,OW ,OH ,KW ,KH ,IC ,N ,IC_IH_IW ,IH_IW ,s0 ,s1 ,p0 ,p1 ,d0 ,d1 ,stream ); 114} 115 116static void im2col_sycl_f32 (const float * x , 117float * dst , 118int64_t IW , 119int64_t IH , 120int64_t OW , 121int64_t OH , 122int64_t KW , 123int64_t KH , 124int64_t IC , 125int64_t N , 126int64_t IC_IH_IW , 127int64_t IH_IW , 128int s0 , 129int s1 , 130int p0 , 131int p1 , 132int d0 , 133int d1 , 134 dpct::queue_ptr stream ) { 135im2col_sycl < float > (x ,dst ,IW ,IH ,OW ,OH ,KW ,KH ,IC ,N ,IC_IH_IW ,IH_IW ,s0 ,s1 ,p0 ,p1 ,d0 ,d1 ,stream ); 136} 137 138void ggml_sycl_op_im2col (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ) { 139const ggml_tensor * src0 = dst -> src [0 ]; 140const ggml_tensor * src1 = dst -> src [1 ]; 141const float * src1_d = (const float * )src1 -> data ; 142float * dst_d = (float * )dst -> data ; 143 dpct::queue_ptr stream = ctx .stream (); 144 145GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 146GGML_ASSERT (dst -> type == GGML_TYPE_F16 || dst -> type == GGML_TYPE_F32 ); 147 148const int32_t s0 = ((const int32_t * )(dst -> op_params ))[0 ]; 149const int32_t s1 = ((const int32_t * )(dst -> op_params ))[1 ]; 150const int32_t p0 = ((const int32_t * )(dst -> op_params ))[2 ]; 151const int32_t p1 = ((const int32_t * )(dst -> op_params ))[3 ]; 152const int32_t d0 = ((const int32_t * )(dst -> op_params ))[4 ]; 153const int32_t d1 = ((const int32_t * )(dst -> op_params ))[5 ]; 154 155const bool is_2D = ((const int32_t * )(dst -> op_params ))[6 ]== 1 ; 156 157const int64_t IC = src1 -> ne [is_2D ?2 :1 ]; 158const int64_t IH = is_2D ?src1 -> ne [1 ] :1 ; 159const int64_t IW = src1 -> ne [0 ]; 160 161const int64_t KH = is_2D ?src0 -> ne [1 ] :1 ; 162const int64_t KW = src0 -> ne [0 ]; 163 164const int64_t OH = is_2D ?dst -> ne [2 ] :1 ; 165const int64_t OW = dst -> ne [1 ]; 166 167const int64_t IC_IH_IW = src1 -> nb [is_2D ?2 :1 ] /4 ;// nb is byte offset, src is type float32 168const int64_t N = src1 -> ne [is_2D ?3 :2 ]; 169const int64_t IH_IW = src1 -> nb [is_2D ?3 :2 ] /4 ;// nb is byte offset, src is type float32 170 171if (dst -> type == GGML_TYPE_F16 ) { 172im2col_sycl_f16 (src1_d , (sycl::half * )dst_d ,IW ,IH ,OW ,OH ,KW ,KH ,IC ,N ,IC_IH_IW ,IH_IW ,s0 ,s1 ,p0 ,p1 , 173d0 ,d1 ,stream ); 174 }else { 175im2col_sycl_f32 (src1_d , (float * )dst_d ,IW ,IH ,OW ,OH ,KW ,KH ,IC ,N ,IC_IH_IW ,IH_IW ,s0 ,s1 ,p0 ,p1 ,d0 ,d1 ,stream ); 176 } 177} 178 179// [N*IC, ID, IH, IW] => [N*OD, OH, OW, IC * KD * KH * KW] 180template < typename T > 181static void im2col_3d_kernel ( 182const float * src ,T * dst , 183int64_t N ,int64_t IC ,int64_t ID ,int64_t IH ,int64_t IW ,int64_t OC , 184int64_t KD ,int64_t KH ,int64_t KW ,int64_t OD ,int64_t OH ,int64_t OW , 185int64_t OH_OW ,int64_t KD_KH_KW ,int64_t ID_IH_IW ,int64_t KH_KW ,int64_t IH_IW ,int64_t IC_ID_IH_IW , 186int64_t IC_KD_KH_KW ,int64_t OW_KD_KH_KW ,int64_t OD_OH_OW_IC_KD_KH_KW ,int64_t OH_OW_IC_KD_KH_KW , 187int64_t OW_IC_KD_KH_KW ,int64_t N_OD_OH ,int64_t OD_OH , 188int64_t stride_q ,int64_t stride_z ,int64_t stride_y ,int64_t stride_x , 189int s0 ,int s1 ,int s2 ,int p0 ,int p1 ,int p2 ,int d0 ,int d1 ,int d2 ) { 190auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 191const int64_t i = item_ct1 .get_local_id (2 )+ item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 ); 192if (i >=IC_KD_KH_KW ) { 193return ; 194 } 195GGML_UNUSED (N );GGML_UNUSED (OC );GGML_UNUSED (OH_OW );GGML_UNUSED (OD );GGML_UNUSED (OW );GGML_UNUSED (KD );GGML_UNUSED (KH ); 196GGML_UNUSED (ID_IH_IW );GGML_UNUSED (IH_IW );GGML_UNUSED (IC_ID_IH_IW );GGML_UNUSED (OW_KD_KH_KW ); 197 198const int64_t iic = i /KD_KH_KW ; 199const int64_t ikd = (i - iic * KD_KH_KW ) /KH_KW ; 200const int64_t ikh = (i - iic * KD_KH_KW - ikd * KH_KW ) /KW ; 201const int64_t ikw = i %KW ; 202 203const int64_t iow = item_ct1 .get_group (1 ); 204for (int64_t iz = item_ct1 .get_group (0 );iz < N_OD_OH ;iz += MAX_GRIDDIM_Z ) { 205const int64_t in = iz /OD_OH ; 206const int64_t iod = (iz - in * OD_OH ) /OH ; 207const int64_t ioh = iz %OH ; 208 209const int64_t iiw = iow * s0 + ikw * d0 - p0 ; 210const int64_t iih = ioh * s1 + ikh * d1 - p1 ; 211const int64_t iid = iod * s2 + ikd * d2 - p2 ; 212 213const int64_t offset_dst = in * OD_OH_OW_IC_KD_KH_KW + iod * OH_OW_IC_KD_KH_KW + ioh * OW_IC_KD_KH_KW + iow * IC_KD_KH_KW + iic * KD_KH_KW + ikd * KH_KW + ikh * KW + ikw ; 214 215if (iih < 0 || iih >=IH || iiw < 0 || iiw >=IW || iid < 0 || iid >=ID ) { 216dst [offset_dst ]= 0.0f ; 217 }else { 218const int64_t offset_src = ((in * IC + iic )* stride_q )+ (iid * stride_z )+ (iih * stride_y )+ (iiw * stride_x ); 219dst [offset_dst ]= src [offset_src ]; 220 } 221 } 222} 223 224// [N*IC, ID, IH, IW] => [N*OD, OH, OW, IC * KD * KH * KW] 225template < typename T > 226static void im2col_3d_sycl (const float * src , 227T * dst , 228int64_t N , 229int64_t IC , 230int64_t ID , 231int64_t IH , 232int64_t IW , 233int64_t OC , 234int64_t KD , 235int64_t KH , 236int64_t KW , 237int64_t OD , 238int64_t OH , 239int64_t OW , 240int64_t stride_q , 241int64_t stride_z , 242int64_t stride_y , 243int64_t stride_x , 244int s0 , 245int s1 , 246int s2 , 247int p0 , 248int p1 , 249int p2 , 250int d0 , 251int d1 , 252int d2 , 253 dpct::queue_ptr stream ) { 254const int64_t OH_OW = OH * OW ; 255const int64_t KD_KH_KW = KD * KH * KW ; 256const int64_t ID_IH_IW = ID * IH * IW ; 257const int64_t KH_KW = KH * KW ; 258const int64_t IH_IW = IH * IW ; 259const int64_t IC_KD_KH_KW = IC * KD * KH * KW ; 260const int64_t OW_KD_KH_KW = OW * KD * KH * KW ; 261const int64_t N_OD_OH = N * OD * OH ; 262const int64_t OD_OH = OD * OH ; 263const int64_t IC_ID_IH_IW = IC * ID * IH * IW ; 264const int64_t OD_OH_OW_IC_KD_KH_KW = OD * OH * OW * IC * KD * KH * KW ; 265const int64_t OH_OW_IC_KD_KH_KW = OH * OW * IC * KD * KH * KW ; 266const int64_t OW_IC_KD_KH_KW = OW * IC * KD * KH * KW ; 267const int64_t num_blocks = (IC_KD_KH_KW + SYCL_IM2COL_BLOCK_SIZE - 1 ) /SYCL_IM2COL_BLOCK_SIZE ; 268 dpct::dim3 block_nums (num_blocks ,OW ,MIN (N_OD_OH ,MAX_GRIDDIM_Z )); 269/* 270DPCT1049:74: The work-group size passed to the SYCL kernel may exceed the limit. To get the device limit, query info::device::max_work_group_size. Adjust the work-group size if needed. 271*/ 272stream -> parallel_for (sycl::nd_range < 3 > (block_nums * sycl::range < 3 > (1 ,1 ,MIN (IC_KD_KH_KW ,SYCL_IM2COL_BLOCK_SIZE )), 273 sycl::range < 3 > (1 ,1 ,MIN (IC_KD_KH_KW ,SYCL_IM2COL_BLOCK_SIZE ))), 274 [= ](sycl::nd_item < 3 > ) { 275im2col_3d_kernel (src ,dst ,N ,IC ,ID ,IH ,IW ,OC ,KD ,KH ,KW ,OD ,OH ,OW ,OH_OW ,KD_KH_KW , 276ID_IH_IW ,KH_KW ,IH_IW ,IC_ID_IH_IW ,IC_KD_KH_KW ,OW_KD_KH_KW , 277OD_OH_OW_IC_KD_KH_KW ,OH_OW_IC_KD_KH_KW ,OW_IC_KD_KH_KW ,N_OD_OH ,OD_OH , 278stride_q ,stride_z ,stride_y ,stride_x ,s0 ,s1 ,s2 ,p0 ,p1 ,p2 ,d0 ,d1 , 279d2 ); 280 }); 281} 282 283static void im2col_3d_sycl_f16 (const float * src , 284 sycl::half * dst , 285int64_t N , 286int64_t IC , 287int64_t ID , 288int64_t IH , 289int64_t IW , 290int64_t OC , 291int64_t KD , 292int64_t KH , 293int64_t KW , 294int64_t OD , 295int64_t OH , 296int64_t OW , 297int64_t stride_q , 298int64_t stride_z , 299int64_t stride_y , 300int64_t stride_x , 301int s0 , 302int s1 , 303int s2 , 304int p0 , 305int p1 , 306int p2 , 307int d0 , 308int d1 , 309int d2 , 310 dpct::queue_ptr stream ) { 311im2col_3d_sycl < sycl::half > (src ,dst ,N ,IC ,ID ,IH ,IW ,OC ,KD ,KH ,KW ,OD ,OH ,OW ,stride_q ,stride_z ,stride_y , 312stride_x ,s0 ,s1 ,s2 ,p0 ,p1 ,p2 ,d0 ,d1 ,d2 ,stream ); 313} 314 315static void im2col_3d_sycl_f32 (const float * src , 316float * dst , 317int64_t N , 318int64_t IC , 319int64_t ID , 320int64_t IH , 321int64_t IW , 322int64_t OC , 323int64_t KD , 324int64_t KH , 325int64_t KW , 326int64_t OD , 327int64_t OH , 328int64_t OW , 329int64_t stride_q , 330int64_t stride_z , 331int64_t stride_y , 332int64_t stride_x , 333int s0 , 334int s1 , 335int s2 , 336int p0 , 337int p1 , 338int p2 , 339int d0 , 340int d1 , 341int d2 , 342 dpct::queue_ptr stream ) { 343im2col_3d_sycl < float > (src ,dst ,N ,IC ,ID ,IH ,IW ,OC ,KD ,KH ,KW ,OD ,OH ,OW , 344stride_q ,stride_z ,stride_y ,stride_x , 345s0 ,s1 ,s2 ,p0 ,p1 ,p2 ,d0 ,d1 ,d2 ,stream ); 346} 347 348void ggml_sycl_op_im2col_3d (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ) { 349const ggml_tensor * src0 = dst -> src [0 ]; 350const ggml_tensor * src1 = dst -> src [1 ]; 351const float * src1_d = (const float * )src1 -> data ; 352float * dst_d = (float * )dst -> data ; 353 dpct::queue_ptr stream = ctx .stream (); 354 355GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 356GGML_ASSERT (dst -> type == GGML_TYPE_F16 || dst -> type == GGML_TYPE_F32 ); 357 358GGML_TENSOR_BINARY_OP_LOCALS 359 360const int32_t s0 = ((const int32_t * )(dst -> op_params ))[0 ]; 361const int32_t s1 = ((const int32_t * )(dst -> op_params ))[1 ]; 362const int32_t s2 = ((const int32_t * )(dst -> op_params ))[2 ]; 363const int32_t p0 = ((const int32_t * )(dst -> op_params ))[3 ]; 364const int32_t p1 = ((const int32_t * )(dst -> op_params ))[4 ]; 365const int32_t p2 = ((const int32_t * )(dst -> op_params ))[5 ]; 366const int32_t d0 = ((const int32_t * )(dst -> op_params ))[6 ]; 367const int32_t d1 = ((const int32_t * )(dst -> op_params ))[7 ]; 368const int32_t d2 = ((const int32_t * )(dst -> op_params ))[8 ]; 369const int32_t IC = ((const int32_t * )(dst -> op_params ))[9 ]; 370 371const int64_t N = ne13 /IC ; 372const int64_t ID = ne12 ; 373const int64_t IH = ne11 ; 374const int64_t IW = ne10 ; 375 376const int64_t OC = ne03 /IC ; 377const int64_t KD = ne02 ; 378const int64_t KH = ne01 ; 379const int64_t KW = ne00 ; 380 381const int64_t OD = ne3 /N ; 382const int64_t OH = ne2 ; 383const int64_t OW = ne1 ; 384 385const size_t es = ggml_element_size (src1 ); 386const int64_t stride_x = src1 -> nb [0 ] /es ; 387const int64_t stride_y = src1 -> nb [1 ] /es ; 388const int64_t stride_z = src1 -> nb [2 ] /es ; 389const int64_t stride_q = src1 -> nb [3 ] /es ; 390 391if (dst -> type == GGML_TYPE_F16 ) { 392im2col_3d_sycl_f16 (src1_d , (sycl::half * )dst_d ,N ,IC ,ID ,IH ,IW ,OC ,KD ,KH ,KW ,OD ,OH ,OW , 393stride_q ,stride_z ,stride_y ,stride_x , 394s0 ,s1 ,s2 ,p0 ,p1 ,p2 ,d0 ,d1 ,d2 ,stream ); 395 }else { 396im2col_3d_sycl_f32 (src1_d , (float * )dst_d ,N ,IC ,ID ,IH ,IW ,OC ,KD ,KH ,KW ,OD ,OH ,OW , 397stride_q ,stride_z ,stride_y ,stride_x , 398s0 ,s1 ,s2 ,p0 ,p1 ,p2 ,d0 ,d1 ,d2 ,stream ); 399 } 400}