char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
eafe15a5e
char/aliceai-t5-moe
1#ifndef HTP_OPS_H 2#define HTP_OPS_H 3 4#include <assert.h> 5 6// ggml-common.h must be included prio to this header 7 8enum htp_status { 9HTP_STATUS_OK = 1 , 10HTP_STATUS_INTERNAL_ERR = 2 , 11HTP_STATUS_NO_SUPPORT = 3 , 12HTP_STATUS_INVAL_PARAMS = 4 , 13HTP_STATUS_VTCM_TOO_SMALL = 5 , 14}; 15 16// First set of values must match the ggml_type. 17// Duplicated here because we can't include full ggml.h in the htp build. 18// We have some static_asserts in the cpp code to ensure things are in sync. 19enum htp_data_type { 20HTP_TYPE_F32 = 0 , 21HTP_TYPE_F16 = 1 , 22HTP_TYPE_Q4_0 = 2 , 23HTP_TYPE_Q4_1 = 3 , 24HTP_TYPE_Q8_0 = 8 , 25HTP_TYPE_IQ4_NL = 20 , 26HTP_TYPE_I32 = 26 , 27HTP_TYPE_I64 = 27 , 28HTP_TYPE_MXFP4 = 39 , 29 30// types used internally for repack, dyn.quant, etc 31HTP_TYPE_Q4_0_TILED = 200 , 32HTP_TYPE_Q4_1_TILED , 33HTP_TYPE_Q8_0_TILED , 34HTP_TYPE_MXFP4_TILED , 35 36HTP_TYPE_INVALID 37}; 38 39// Constats for internal types 40#define QK_Q4_0_TILED 256 // 32x32 Q4_0 tiled layout 41#define QK_Q8_0_TILED 128 // 32x32 Q8_0 tiled layout 42#define QK_MXFP4_TILED 256 // 32x32 MXFP4 tiled layout 43 44 45 46// Do not reorder first 4 (used as an index) 47enum htp_op_code { 48HTP_OP_MUL = 0 , 49HTP_OP_ADD = 1 , 50HTP_OP_SUB = 2 , 51HTP_OP_DIV = 3 , 52HTP_OP_MUL_MAT , 53HTP_OP_MUL_MAT_ID , 54HTP_OP_MUL_MAT_NX , 55HTP_OP_MUL_MAT_ID_NX , 56HTP_OP_MUL_MAT_ADD , 57HTP_OP_RMS_NORM , 58HTP_OP_RMS_NORM_MUL , 59HTP_OP_UNARY_SILU , 60HTP_OP_UNARY_GELU , 61HTP_OP_UNARY_SIGMOID , 62HTP_OP_UNARY_EXP , 63HTP_OP_UNARY_NEG , 64HTP_OP_UNARY_SOFTPLUS , 65HTP_OP_UNARY_TANH , 66HTP_OP_UNARY_ABS , 67HTP_OP_UNARY_LOG , 68HTP_OP_UNARY_RELU , 69HTP_OP_GLU_SWIGLU , 70HTP_OP_GLU_SWIGLU_OAI , 71HTP_OP_GLU_GEGLU , 72HTP_OP_SOFTMAX , 73HTP_OP_ADD_ID , 74HTP_OP_ROPE , 75HTP_OP_FLASH_ATTN_EXT , 76HTP_OP_SET_ROWS , 77HTP_OP_GET_ROWS , 78HTP_OP_SCALE , 79HTP_OP_CPY , 80HTP_OP_CPY_FENCE , 81HTP_OP_ARGSORT , 82HTP_OP_SQR , 83HTP_OP_SQRT , 84HTP_OP_SUM_ROWS , 85HTP_OP_SSM_CONV , 86HTP_OP_REPEAT , 87HTP_OP_CUMSUM , 88HTP_OP_FILL , 89HTP_OP_DIAG , 90HTP_OP_SOLVE_TRI , 91HTP_OP_L2_NORM , 92HTP_OP_GATED_DELTA_NET , 93HTP_OP_TRI , 94HTP_OP_PAD , 95HTP_OP_NORM , 96HTP_OP_CONCAT , 97HTP_OP_CLAMP , 98HTP_OP_LEAKY_RELU , 99HTP_OP_IM2COL , 100HTP_OP_FENCE , 101HTP_OP_ALLREDUCE , 102HTP_OP_ALLREDUCE_ADD , 103HTP_OP_GLU_SWIGLU_CLAMP , 104HTP_OP_MDEV_GROUP , 105 106HTP_OP_INVALID 107}; 108 109#define HTP_OP_MAX_DIMS 4 // aka GGML_MAX_DIMS 110#define HTP_OP_MAX_INPUTS 10 // aka GGML_MAX_SRCS 111#define HTP_OP_MAX_OUTPUTS 4 112#define HTP_OP_MAX_PARAMS 16 // aka GGML_MAX_OP_PARAMS 113#define HTP_OP_MAX_KERN_PARAMS 32 114 115#define HTP_OP_MAX_BUFS 16 116#define HTP_OP_MAX_TENSORS 8192 // must stay under 64K (uint16) 117 118#define HTP_FENCE_TIMEOUT (1000000000ULL) 119#define HTP_FENCE_SLOT_SIZE 128 120 121#define HTP_OP_MAX_VMEM_DEFAULT (3355443200u) 122 123#define HTP_MMAP_MAX_VMEM (2147483648u) 124 125enum htp_tensor_flags { 126HTP_TENSOR_WEIGHT = (1U <<0 ),// Tensor buffer model weight data (not compute) 127HTP_TENSOR_REPACK = (1U <<1 ),// Tensor is in repacked tiled format 128HTP_TENSOR_FENCE = (1U <<2 )// Tensor is synchronization fence (explicitly managed) 129}; 130 131// Tensor descriptor 132struct htp_tensor { 133uint32_t data ;// Buffer offset in the messages, and data pointer on the NPU 134uint32_t reserved ;// Reserved for alignment padding (must be multiple of 8) 135uint32_t size ;// Data size in bytes 136uint32_t flags ;// Buffer / tensor flags 137uint32_t type ;// Data type 138uint16_t bi ;// Buffer index 139uint16_t ti ;// Tensor index 140uint32_t ne [HTP_OP_MAX_DIMS ];// Number of elements 141uint32_t nb [HTP_OP_MAX_DIMS ];// Stride in bytes (see ggml.h ggml_tensor) 142}; 143 144// Buffer descriptor 145struct htp_buf_desc { 146uint64_t base ;// base address 147uint64_t size ;// total size 148uint32_t flags ;// buffer flags (unused) 149uint32_t fd ;// file descriptor 150}; 151 152enum htp_op_flags { 153HTP_OPFLAGS_SKIP_COMPUTE = (1U <<0 ),// Skip actual computation (used for profiling) 154}; 155 156// Op descriptor 157struct htp_op_desc { 158uint32_t opcode ;// GGML/HTP Op 159uint32_t flags ;// Op flags 160int32_t params [HTP_OP_MAX_PARAMS ];// Params for the op, e.g. epsilon of RMS norm 161int32_t kernel_params [HTP_OP_MAX_KERN_PARAMS ];// generic blob for host-precomputed parameters 162uint16_t src [HTP_OP_MAX_INPUTS ];// Input tensors indices 163uint16_t dst [HTP_OP_MAX_OUTPUTS ];// Output tensor indices 164uint16_t pad [2 ];// padding to align to 64 bits 165}; 166 167#ifndef HTP_MAX_NTHREADS 168#define HTP_MAX_NTHREADS 10 169#endif 170 171#define HTP_TRACE_MAX_EVENTS 256 172 173enum htp_profiler_mode { 174HTP_PROF_DISABLED = 0 , 175HTP_PROF_BASIC = 1 , 176HTP_PROF_PMU = 2 , 177HTP_PROF_TRACE = 3 , 178}; 179 180enum htp_trace_event_id { 181HTP_TRACE_EVT_DMA = 0 , 182HTP_TRACE_EVT_L2FLUSH = 1 , 183HTP_TRACE_EVT_INIT = 2 , 184HTP_TRACE_EVT_BUFF = 3 , 185HTP_TRACE_EVT_FENCE = 4 , 186 187HTP_TRACE_EVT_HVX_COMP = 20 , 188HTP_TRACE_EVT_HVX_A_QUANT = 21 , 189HTP_TRACE_EVT_HVX_A_PREP = 22 , 190HTP_TRACE_EVT_HVX_W_DEQUANT = 23 , 191HTP_TRACE_EVT_HVX_W_PREP = 24 , 192HTP_TRACE_EVT_HVX_O_PROC = 25 , 193HTP_TRACE_EVT_HVX_FA_QK = 26 , 194HTP_TRACE_EVT_HVX_FA_SFM = 27 , 195HTP_TRACE_EVT_HVX_FA_Q_PREP = 28 , 196HTP_TRACE_EVT_HVX_FA_K_PREP = 29 , 197HTP_TRACE_EVT_HVX_FA_V_PREP = 30 , 198 199HTP_TRACE_EVT_HMX_COMP = 40 , 200}; 201 202struct htp_trace_desc { 203uint32_t cycles ;// lower 32-bits of cycle counter 204uint16_t id ;// Event ID 205uint16_t info ;// bit 15: is_stop. bits 14-0: tile/chunk index or other metadata. 206}; 207 208#define HTP_PROF_PMU_NCNT 8 209 210// Profile descriptor 211struct htp_prof_desc { 212uint32_t opcode ;// GGML/HTP Op 213uint32_t usecs ;// Number of usec 214uint32_t cycles_start ;// Start cycle counter 215uint32_t cycles_stop ;// Stop cycle counter 216uint32_t pmu [HTP_PROF_PMU_NCNT ];// PMU counters 217}; 218 219struct htp_opbatch_req { 220uint64_t seq ;// Sequence number 221uint32_t n_bufs ;// Number of buffers 222uint32_t n_tensors ;// Number of tensors 223uint32_t n_ops ;// Number of ops 224uint32_t n_traces ;// Number of trace descriptors per thread 225// struct htp_buf_desc bufs[]; -- dspqueue buf 0 226// struct htp_tensor tensors[]; -- dspqueue buf 0 227// struct htp_op_desc ops[]; -- dspqueue buf 0 228}; 229 230struct htp_opbatch_rsp { 231uint64_t seq ;// Sequence number 232uint64_t cycles_start ;// Start cycle counter 233uint64_t cycles_stop ;// Stop cycle counter 234uint32_t status ;// HTP_STATUS_... 235uint32_t n_bufs ;// Number of buffers 236uint32_t n_tensors ;// Number of tensors 237uint32_t n_ops ;// Number of op profile descriptors 238uint32_t usecs ;// Number of usec 239uint32_t n_traces [HTP_MAX_NTHREADS + 1 ]; 240// struct htp_prof_desc profs[]; -- dspqueue buf 0 241}; 242 243#endif /* HTP_OPS_H */