char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
6f1034b32
char/aliceai-t5-moe
1#include "conv2d-transpose.hpp" 2#include "convert.hpp" 3 4template < typename kernel_t > 5static void conv2d_transpose_kernel (const float * input ,const kernel_t * kernel ,float * output , 6const int in_w ,const int in_h , 7const int out_w ,const int out_h , 8const int kernel_w ,const int kernel_h , 9const int stride , 10const int c_in ,const int c_out ,const int batches , 11const sycl::nd_item < 3 > & item_ct1 ) { 12const int global_idx = item_ct1 .get_local_id (2 )+ 13item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 ); 14const int total_elements = out_w * out_h * c_out * batches ; 15 16if (global_idx >=total_elements ) { 17return ; 18 } 19 20const int out_x = global_idx %out_w ; 21const int out_y = (global_idx /out_w ) %out_h ; 22const int c_idx = (global_idx / (out_w * out_h )) %c_out ; 23const int n_idx = global_idx / (out_w * out_h * c_out ); 24 25float acc = 0.0f ; 26 27for (int c_in_idx = 0 ;c_in_idx < c_in ;++ c_in_idx ) { 28for (int kh = 0 ;kh < kernel_h ;++ kh ) { 29int in_y = out_y - kh ; 30if (in_y < 0 || in_y %stride ) { 31continue ; 32 } 33in_y /=stride ; 34if (in_y >=in_h ) { 35continue ; 36 } 37 38for (int kw = 0 ;kw < kernel_w ;++ kw ) { 39int in_x = out_x - kw ; 40if (in_x < 0 || in_x %stride ) { 41continue ; 42 } 43in_x /=stride ; 44if (in_x >=in_w ) { 45continue ; 46 } 47 48const int input_idx = (in_w * in_h * c_in )* n_idx + (in_w * in_h )* c_in_idx + in_w * in_y + in_x ; 49const int kernel_idx = (kernel_h * kernel_w * c_out )* c_in_idx + (kernel_h * kernel_w )* c_idx + 50kernel_w * kh + kw ; 51 52acc += input [input_idx ]* ggml_sycl_cast < float > (kernel [kernel_idx ]); 53 } 54 } 55 } 56 57output [(out_w * out_h * c_out )* n_idx + (out_w * out_h )* c_idx + out_w * out_y + out_x ]= acc ; 58} 59 60template < typename kernel_t > 61static void conv2d_transpose_sycl (const float * input_d ,const kernel_t * kernel_d ,float * output_d , 62const int in_w ,const int in_h , 63const int out_w ,const int out_h , 64const int kernel_w ,const int kernel_h , 65const int stride , 66const int c_in ,const int c_out ,const int batches , 67const queue_ptr & stream ) { 68const int total = out_w * out_h * c_out * batches ; 69const int num_blocks = (total + SYCL_CONV2D_TRANSPOSE_BLOCK_SIZE - 1 ) /SYCL_CONV2D_TRANSPOSE_BLOCK_SIZE ; 70const sycl::range < 3 > block_dims (1 ,1 ,SYCL_CONV2D_TRANSPOSE_BLOCK_SIZE ); 71const sycl::range < 3 > block_nums (1 ,1 ,num_blocks ); 72stream -> parallel_for (sycl::nd_range < 3 > (block_nums * block_dims ,block_dims ), 73 [= ](sycl::nd_item < 3 > item_ct1 ) { 74conv2d_transpose_kernel < kernel_t > (input_d ,kernel_d ,output_d , 75in_w ,in_h ,out_w ,out_h ,kernel_w ,kernel_h , 76stride ,c_in ,c_out ,batches ,item_ct1 ); 77 }); 78} 79 80// input: (W, H, C_in, N) 81// kernel: (W, H, C_out, C_in) 82// output: (W, H, C_out, N) 83void ggml_sycl_op_conv2d_transpose (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ) { 84scope_op_debug_print scope_dbg_print (__func__ ,dst ,/*num_src=*/ 2 ); 85 86const ggml_tensor * kernel = dst -> src [0 ]; 87const ggml_tensor * input = dst -> src [1 ]; 88 89GGML_ASSERT (kernel -> type == GGML_TYPE_F16 || kernel -> type == GGML_TYPE_F32 ); 90GGML_ASSERT (input -> type == GGML_TYPE_F32 && dst -> type == GGML_TYPE_F32 ); 91 92GGML_ASSERT (ggml_is_contiguous (input )); 93GGML_ASSERT (ggml_is_contiguous (kernel )); 94GGML_ASSERT (ggml_is_contiguous (dst )); 95 96const float * input_d = (const float * )input -> data ; 97float * output_d = (float * )dst -> data ; 98const void * kernel_d = kernel -> data ; 99 100const int input_w = input -> ne [0 ]; 101const int input_h = input -> ne [1 ]; 102const int channels_in = input -> ne [2 ]; 103const int batches = input -> ne [3 ]; 104const int output_w = dst -> ne [0 ]; 105const int output_h = dst -> ne [1 ]; 106const int channels_out = kernel -> ne [2 ]; 107const int kernel_w = kernel -> ne [0 ]; 108const int kernel_h = kernel -> ne [1 ]; 109const int stride = dst -> op_params [0 ]; 110 111GGML_ASSERT (channels_in == kernel -> ne [3 ]); 112GGML_ASSERT (stride > 0 ); 113 114const queue_ptr stream = ctx .stream (); 115 116if (kernel -> type == GGML_TYPE_F16 ) { 117conv2d_transpose_sycl < sycl::half > (input_d , (const sycl::half * )kernel_d ,output_d , 118input_w ,input_h ,output_w ,output_h ,kernel_w ,kernel_h , 119stride ,channels_in ,channels_out ,batches ,stream ); 120 }else { 121conv2d_transpose_sycl < float > (input_d , (const float * )kernel_d ,output_d , 122input_w ,input_h ,output_w ,output_h ,kernel_w ,kernel_h , 123stride ,channels_in ,channels_out ,batches ,stream ); 124 } 125}