char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
050dde50c
char/aliceai-t5-moe
1#ifndef HTP_UNARY_OPS_H 2#define HTP_UNARY_OPS_H 3 4#include "hex-common.h" 5#include "htp-ops.h" 6 7// Op-specific struct for precomputed unary params 8struct htp_unary_kernel_params { 9uint32_t n_threads ; 10uint32_t col_tile ; 11uint32_t vtcm_row_per_thread ; 12uint32_t block ; 13uint32_t broadcast_weight ; 14 15uint32_t vtcm_src0_size_per_thread ; 16uint32_t vtcm_src1_size_per_thread ; 17uint32_t vtcm_dst_size_per_thread ; 18 19uint32_t vtcm_src0_size ; 20uint32_t vtcm_src1_size ; 21uint32_t vtcm_dst_size ; 22 23uint32_t src0_row_size_aligned ; 24uint32_t src1_row_size_aligned ; 25uint32_t dst_row_size_aligned ; 26 27uint32_t vtcm_size ; 28 29// Fastdiv helpers 30struct fastdiv_values div_ne01 ; 31struct fastdiv_values div_ne02 ; 32struct fastdiv_values div_ne012 ; 33struct fastdiv_values div_tpr ; 34}; 35 36#if defined(__cplusplus ) 37static_assert (sizeof (struct htp_unary_kernel_params ) <=128 ,"htp_unary_kernel_params is too large for kernel_params blob" ); 38#else 39_Static_assert (sizeof (struct htp_unary_kernel_params ) <=128 ,"htp_unary_kernel_params is too large for kernel_params blob" ); 40#endif 41 42static inline bool htp_op_is_unary (uint32_t opcode ) { 43switch (opcode ) { 44case HTP_OP_CLAMP : 45case HTP_OP_LEAKY_RELU : 46case HTP_OP_NORM : 47case HTP_OP_RMS_NORM : 48case HTP_OP_RMS_NORM_MUL : 49case HTP_OP_SCALE : 50case HTP_OP_SQR : 51case HTP_OP_SQRT : 52case HTP_OP_UNARY_NEG : 53case HTP_OP_UNARY_EXP : 54case HTP_OP_UNARY_SIGMOID : 55case HTP_OP_UNARY_SILU : 56case HTP_OP_UNARY_GELU : 57case HTP_OP_UNARY_SOFTPLUS : 58case HTP_OP_UNARY_TANH : 59case HTP_OP_UNARY_ABS : 60case HTP_OP_UNARY_LOG : 61case HTP_OP_UNARY_RELU : 62case HTP_OP_L2_NORM : 63case HTP_OP_TRI : 64return true; 65default : 66return false; 67 } 68} 69 70struct htp_unary_vtcm_layout { 71size_t total_bytes ; 72size_t off_src0 ; 73size_t off_src1 ; 74size_t off_dst ; 75 76size_t src0_bytes ; 77size_t src1_bytes ; 78size_t dst_bytes ; 79}; 80 81static inline void htp_unary_vtcm_layout_build ( 82struct htp_unary_vtcm_layout * L , 83uint32_t op , 84uint32_t ne00 , 85uint32_t ne10 , 86uint32_t ne11 , 87bool broadcast_weight , 88uint32_t n_threads , 89size_t vtcm_size , 90size_t elem_size , 91uint32_t * out_col_tile , 92uint32_t * out_vtcm_row_per_thread 93) { 94const size_t src0_data_row_size = ne00 * elem_size ; 95const size_t dst_data_row_size = ne10 * elem_size ; 96 97const size_t src0_row_size_aligned = hex_round_up (src0_data_row_size ,128 ); 98const size_t dst_row_size_aligned = hex_round_up (dst_data_row_size ,128 ); 99 100size_t src1_row_size_aligned = 0 ; 101if (op == HTP_OP_RMS_NORM_MUL ) { 102// RMS_NORM_MUL fusion is F32-only; its weight tensor is always F32. 103const size_t src1_data_row_size = ne11 * sizeof (float ); 104src1_row_size_aligned = hex_round_up (src1_data_row_size ,128 ); 105 } 106 107size_t vtcm_size_per_row = 0 ; 108size_t vtcm_row_per_thread = 0 ; 109 110if (op == HTP_OP_RMS_NORM_MUL ) { 111if (broadcast_weight ) { 112size_t available_vtcm = vtcm_size ; 113size_t src1_vtcm_total = n_threads * src1_row_size_aligned ; 114if (available_vtcm > src1_vtcm_total ) { 115available_vtcm -= src1_vtcm_total ; 116 }else { 117available_vtcm = 0 ; 118 } 119vtcm_size_per_row = 2 * (src0_row_size_aligned + dst_row_size_aligned ); 120vtcm_row_per_thread = available_vtcm / (n_threads * vtcm_size_per_row ); 121 }else { 122vtcm_size_per_row = 2 * (src0_row_size_aligned + dst_row_size_aligned + src1_row_size_aligned ); 123vtcm_row_per_thread = vtcm_size / (n_threads * vtcm_size_per_row ); 124 } 125 }else { 126vtcm_size_per_row = 2 * (src0_row_size_aligned + dst_row_size_aligned ); 127vtcm_row_per_thread = vtcm_size / (n_threads * vtcm_size_per_row ); 128 } 129 130const bool is_reduction = (op == HTP_OP_NORM || op == HTP_OP_RMS_NORM || 131op == HTP_OP_RMS_NORM_MUL || op == HTP_OP_L2_NORM ); 132// The tiled fallback path below only has F32 task functions (unary_task_f32_tiled_*); 133// F16 has no tiled kernels, so it must stay on the row-block path like reduction ops. 134// NOTE: if F16 ends up with vtcm_row_per_thread == 0 here (row too large for the VTCM 135// budget), execute_op_unary() will see BLOCK == 0 and skip computation for that op 136// (logged via FARF(ERROR, ...)) since there is no F16 tiled fallback. This is a known 137// limitation; supporting it would require adding F16 tiled kernels. 138const bool is_f16 = (elem_size == sizeof (_Float16 )); 139uint32_t col_tile = 0 ; 140 141if (vtcm_row_per_thread == 0 && !is_reduction && !is_f16 ) { 142const size_t per_thread_budget = vtcm_size /n_threads ; 143const size_t col_tile_bytes = hex_align_down (per_thread_budget /4 ,128 ); 144col_tile = (uint32_t ) (col_tile_bytes /elem_size ); 145 146L -> src0_bytes = col_tile_bytes * 2 ; 147L -> dst_bytes = col_tile_bytes * 2 ; 148L -> src1_bytes = 0 ; 149 }else { 150L -> src0_bytes = src0_row_size_aligned * vtcm_row_per_thread * 2 ; 151L -> dst_bytes = dst_row_size_aligned * vtcm_row_per_thread * 2 ; 152if (op == HTP_OP_RMS_NORM_MUL ) { 153if (broadcast_weight ) { 154L -> src1_bytes = src1_row_size_aligned ; 155 }else { 156L -> src1_bytes = src1_row_size_aligned * vtcm_row_per_thread * 2 ; 157 } 158 }else { 159L -> src1_bytes = 0 ; 160 } 161 } 162 163L -> off_src0 = 0 ; 164if (op == HTP_OP_RMS_NORM_MUL ) { 165L -> off_src1 = L -> off_src0 + L -> src0_bytes * n_threads ; 166L -> off_dst = L -> off_src1 + L -> src1_bytes * n_threads ; 167 }else { 168L -> off_src1 = 0 ; 169L -> off_dst = L -> off_src0 + L -> src0_bytes * n_threads ; 170 } 171 172L -> total_bytes = L -> off_dst + L -> dst_bytes * n_threads ; 173 174* out_col_tile = col_tile ; 175* out_vtcm_row_per_thread = vtcm_row_per_thread ; 176} 177 178#endif /* HTP_UNARY_OPS_H */