char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Max Krasnyanskyhexagon: support for multi-device model split (aka row-split) (#28589)eafe15a5e

char/aliceai-t5-moe
7.1 KiB243 linesraw
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 {
9    HTP_STATUS_OK             = 1,
10    HTP_STATUS_INTERNAL_ERR   = 2,
11    HTP_STATUS_NO_SUPPORT     = 3,
12    HTP_STATUS_INVAL_PARAMS   = 4,
13    HTP_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 {
20    HTP_TYPE_F32    = 0,
21    HTP_TYPE_F16    = 1,
22    HTP_TYPE_Q4_0   = 2,
23    HTP_TYPE_Q4_1   = 3,
24    HTP_TYPE_Q8_0   = 8,
25    HTP_TYPE_IQ4_NL = 20,
26    HTP_TYPE_I32    = 26,
27    HTP_TYPE_I64    = 27,
28    HTP_TYPE_MXFP4  = 39,
29
30    // types used internally for repack, dyn.quant, etc
31    HTP_TYPE_Q4_0_TILED = 200,
32    HTP_TYPE_Q4_1_TILED,
33    HTP_TYPE_Q8_0_TILED,
34    HTP_TYPE_MXFP4_TILED,
35
36    HTP_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 {
48    HTP_OP_MUL = 0,
49    HTP_OP_ADD = 1,
50    HTP_OP_SUB = 2,
51    HTP_OP_DIV = 3,
52    HTP_OP_MUL_MAT,
53    HTP_OP_MUL_MAT_ID,
54    HTP_OP_MUL_MAT_NX,
55    HTP_OP_MUL_MAT_ID_NX,
56    HTP_OP_MUL_MAT_ADD,
57    HTP_OP_RMS_NORM,
58    HTP_OP_RMS_NORM_MUL,
59    HTP_OP_UNARY_SILU,
60    HTP_OP_UNARY_GELU,
61    HTP_OP_UNARY_SIGMOID,
62    HTP_OP_UNARY_EXP,
63    HTP_OP_UNARY_NEG,
64    HTP_OP_UNARY_SOFTPLUS,
65    HTP_OP_UNARY_TANH,
66    HTP_OP_UNARY_ABS,
67    HTP_OP_UNARY_LOG,
68    HTP_OP_UNARY_RELU,
69    HTP_OP_GLU_SWIGLU,
70    HTP_OP_GLU_SWIGLU_OAI,
71    HTP_OP_GLU_GEGLU,
72    HTP_OP_SOFTMAX,
73    HTP_OP_ADD_ID,
74    HTP_OP_ROPE,
75    HTP_OP_FLASH_ATTN_EXT,
76    HTP_OP_SET_ROWS,
77    HTP_OP_GET_ROWS,
78    HTP_OP_SCALE,
79    HTP_OP_CPY,
80    HTP_OP_CPY_FENCE,
81    HTP_OP_ARGSORT,
82    HTP_OP_SQR,
83    HTP_OP_SQRT,
84    HTP_OP_SUM_ROWS,
85    HTP_OP_SSM_CONV,
86    HTP_OP_REPEAT,
87    HTP_OP_CUMSUM,
88    HTP_OP_FILL,
89    HTP_OP_DIAG,
90    HTP_OP_SOLVE_TRI,
91    HTP_OP_L2_NORM,
92    HTP_OP_GATED_DELTA_NET,
93    HTP_OP_TRI,
94    HTP_OP_PAD,
95    HTP_OP_NORM,
96    HTP_OP_CONCAT,
97    HTP_OP_CLAMP,
98    HTP_OP_LEAKY_RELU,
99    HTP_OP_IM2COL,
100    HTP_OP_FENCE,
101    HTP_OP_ALLREDUCE,
102    HTP_OP_ALLREDUCE_ADD,
103    HTP_OP_GLU_SWIGLU_CLAMP,
104    HTP_OP_MDEV_GROUP,
105
106    HTP_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 {
126    HTP_TENSOR_WEIGHT  = (1U << 0), // Tensor buffer model weight data (not compute)
127    HTP_TENSOR_REPACK  = (1U << 1), // Tensor is in repacked tiled format
128    HTP_TENSOR_FENCE   = (1U << 2)  // Tensor is synchronization fence (explicitly managed)
129};
130
131// Tensor descriptor
132struct htp_tensor {
133    uint32_t data;                 // Buffer offset in the messages, and data pointer on the NPU
134    uint32_t reserved;             // Reserved for alignment padding (must be multiple of 8)
135    uint32_t size;                 // Data size in bytes
136    uint32_t flags;                // Buffer / tensor flags
137    uint32_t type;                 // Data type
138    uint16_t bi;                   // Buffer index
139    uint16_t ti;                   // Tensor index
140    uint32_t ne[HTP_OP_MAX_DIMS];  // Number of elements
141    uint32_t nb[HTP_OP_MAX_DIMS];  // Stride in bytes (see ggml.h ggml_tensor)
142};
143
144// Buffer descriptor
145struct htp_buf_desc {
146    uint64_t base;     // base address
147    uint64_t size;     // total size
148    uint32_t flags;    // buffer flags (unused)
149    uint32_t fd;       // file descriptor
150};
151
152enum htp_op_flags {
153    HTP_OPFLAGS_SKIP_COMPUTE  = (1U << 0), // Skip actual computation (used for profiling)
154};
155
156// Op descriptor
157struct htp_op_desc {
158    uint32_t opcode;                    // GGML/HTP Op
159    uint32_t flags;                     // Op flags
160    int32_t  params[HTP_OP_MAX_PARAMS]; // Params for the op, e.g. epsilon of RMS norm
161    int32_t  kernel_params[HTP_OP_MAX_KERN_PARAMS]; // generic blob for host-precomputed parameters
162    uint16_t src[HTP_OP_MAX_INPUTS];    // Input tensors indices
163    uint16_t dst[HTP_OP_MAX_OUTPUTS];   // Output tensor indices
164    uint16_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 {
174    HTP_PROF_DISABLED = 0,
175    HTP_PROF_BASIC    = 1,
176    HTP_PROF_PMU      = 2,
177    HTP_PROF_TRACE    = 3,
178};
179
180enum htp_trace_event_id {
181    HTP_TRACE_EVT_DMA                 = 0,
182    HTP_TRACE_EVT_L2FLUSH             = 1,
183    HTP_TRACE_EVT_INIT                = 2,
184    HTP_TRACE_EVT_BUFF                = 3,
185    HTP_TRACE_EVT_FENCE               = 4,
186
187    HTP_TRACE_EVT_HVX_COMP            = 20,
188    HTP_TRACE_EVT_HVX_A_QUANT         = 21,
189    HTP_TRACE_EVT_HVX_A_PREP          = 22,
190    HTP_TRACE_EVT_HVX_W_DEQUANT       = 23,
191    HTP_TRACE_EVT_HVX_W_PREP          = 24,
192    HTP_TRACE_EVT_HVX_O_PROC          = 25,
193    HTP_TRACE_EVT_HVX_FA_QK           = 26,
194    HTP_TRACE_EVT_HVX_FA_SFM          = 27,
195    HTP_TRACE_EVT_HVX_FA_Q_PREP       = 28,
196    HTP_TRACE_EVT_HVX_FA_K_PREP       = 29,
197    HTP_TRACE_EVT_HVX_FA_V_PREP       = 30,
198
199    HTP_TRACE_EVT_HMX_COMP            = 40,
200};
201
202struct htp_trace_desc {
203    uint32_t cycles;  // lower 32-bits of cycle counter
204    uint16_t id;      // Event ID
205    uint16_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 {
212    uint32_t opcode;                 // GGML/HTP Op
213    uint32_t usecs;                  // Number of usec
214    uint32_t cycles_start;           // Start cycle counter
215    uint32_t cycles_stop;            // Stop cycle counter
216    uint32_t pmu[HTP_PROF_PMU_NCNT]; // PMU counters
217};
218
219struct htp_opbatch_req {
220    uint64_t seq;         // Sequence number
221    uint32_t n_bufs;      // Number of buffers
222    uint32_t n_tensors;   // Number of tensors
223    uint32_t n_ops;       // Number of ops
224    uint32_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 {
231    uint64_t seq;            // Sequence number
232    uint64_t cycles_start;   // Start cycle counter
233    uint64_t cycles_stop;    // Stop cycle counter
234    uint32_t status;         // HTP_STATUS_...
235    uint32_t n_bufs;         // Number of buffers
236    uint32_t n_tensors;      // Number of tensors
237    uint32_t n_ops;          // Number of op profile descriptors
238    uint32_t usecs;          // Number of usec
239    uint32_t n_traces[HTP_MAX_NTHREADS + 1];
240    // struct htp_prof_desc profs[];  -- dspqueue buf 0
241};
242
243#endif /* HTP_OPS_H */