ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-hexagon / htp / htp-ctx.h
| 1 | #ifndef HTP_CTX_H |
| 2 | #define HTP_CTX_H |
| 3 | |
| 4 | #include "dma-queue.h" |
| 5 | #include "hmx-queue.h" |
| 6 | #include "htp-dcache.h" |
| 7 | #include "htp-ops.h" |
| 8 | #include "hex-profile.h" |
| 9 | #include "work-queue.h" |
| 10 | #include "hex-fastdiv.h" |
| 11 | |
| 12 | #include <assert.h> |
| 13 | #include <dspqueue.h> |
| 14 | #include <stdatomic.h> |
| 15 | #include <stddef.h> |
| 16 | #include <stdint.h> |
| 17 | #include <stdbool.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #ifndef HTP_MAX_NTHREADS |
| 21 | #define HTP_MAX_NTHREADS 10 |
| 22 | #endif |
| 23 | |
| 24 | #define HTP_MAX_MMAPS 64 |
| 25 | |
| 26 | // Memory mapping |
| 27 | struct htp_mmap { |
| 28 | uint64_t size; |
| 29 | uint64_t base; |
| 30 | uint32_t fd; |
| 31 | uint32_t flags; |
| 32 | }; |
| 33 | |
| 34 | // Scratchpad state |
| 35 | struct htp_spad { |
| 36 | const struct htp_tensor * src; // original src of the data (for reuse) |
| 37 | uint8_t * data; // pointer to an area in vtcm |
| 38 | uint32_t stride; // stride used inside this spad |
| 39 | uint32_t size; // total size |
| 40 | uint32_t size_per_thread; // size per thread |
| 41 | }; |
| 42 | |
| 43 | struct htp_mdev_group { |
| 44 | uint16_t idx; |
| 45 | uint16_t count; |
| 46 | struct fastdiv_values count_div; |
| 47 | uint8_t * fence_base; |
| 48 | uint32_t fence_seq; |
| 49 | }; |
| 50 | |
| 51 | struct htp_context; |
| 52 | |
| 53 | // Context while processing an Op |
| 54 | // TODO: fold this into the main context |
| 55 | struct htp_ops_context { |
| 56 | struct htp_context * ctx; |
| 57 | |
| 58 | enum htp_op_code op; // FIXME: rename to opcode |
| 59 | int32_t op_params[HTP_OP_MAX_PARAMS]; |
| 60 | int32_t kernel_params[HTP_OP_MAX_KERN_PARAMS]; |
| 61 | |
| 62 | const struct htp_tensor * src[HTP_OP_MAX_INPUTS]; |
| 63 | union { |
| 64 | const struct htp_tensor * dst; |
| 65 | const struct htp_tensor * dsts[HTP_OP_MAX_OUTPUTS]; |
| 66 | }; |
| 67 | |
| 68 | // TODO convert these to an array |
| 69 | struct htp_spad src0_spad; |
| 70 | struct htp_spad src1_spad; |
| 71 | struct htp_spad dst_spad; |
| 72 | |
| 73 | uint32_t flags; |
| 74 | uint32_t n_threads; |
| 75 | struct fastdiv_values n_threads_div; |
| 76 | int status; |
| 77 | |
| 78 | // Must stay last: not cleared by htp_ops_context_reset(), ops init the caches they use |
| 79 | union { |
| 80 | dma_cache_dm dma_cache_dm[HTP_MAX_NTHREADS]; |
| 81 | dma_cache_fa dma_cache_fa[HTP_MAX_NTHREADS]; |
| 82 | } __attribute__((aligned(128))); |
| 83 | }; |
| 84 | |
| 85 | // Main context for htp DSP backend |
| 86 | struct htp_context { |
| 87 | dspqueue_t dsp_queue; |
| 88 | |
| 89 | struct htp_mmap mmap[HTP_MAX_MMAPS]; |
| 90 | dma_queue_t dma[HTP_MAX_NTHREADS]; |
| 91 | struct htp_thread_trace trace[HTP_MAX_NTHREADS + 1]; |
| 92 | work_queue_t work_queue; |
| 93 | hmx_queue_t hmx_queue; |
| 94 | |
| 95 | uint32_t n_threads; |
| 96 | struct fastdiv_values n_threads_div; |
| 97 | |
| 98 | int thread_id; |
| 99 | int thread_prio; |
| 100 | |
| 101 | bool hmx_enabled; |
| 102 | bool etm; |
| 103 | uint32_t profiler; |
| 104 | |
| 105 | uint8_t * vtcm_base; |
| 106 | size_t vtcm_size; |
| 107 | uint32_t vtcm_rctx; |
| 108 | atomic_bool vtcm_valid; |
| 109 | atomic_bool vtcm_needs_release; |
| 110 | |
| 111 | uint64_t max_vmem; |
| 112 | struct htp_dirty_range dirty_ranges[HTP_MAX_DIRTY_RANGES]; |
| 113 | |
| 114 | // Persistent DDR scratchpad for MUL_MAT_ID mappings |
| 115 | void * ddr_spad_base; |
| 116 | size_t ddr_spad_size; |
| 117 | |
| 118 | struct htp_mdev_group mdev; |
| 119 | struct htp_ops_context octx; |
| 120 | |
| 121 | qurt_thread_t main_thread; |
| 122 | void * main_stack; |
| 123 | atomic_bool killed; |
| 124 | size_t footprint; |
| 125 | }; |
| 126 | |
| 127 | static inline void htp_ops_context_reset(struct htp_ops_context * octx, struct htp_context * ctx) { |
| 128 | memset(octx, 0, offsetof(struct htp_ops_context, dma_cache_dm)); |
| 129 | octx->ctx = ctx; |
| 130 | octx->n_threads = ctx->n_threads; |
| 131 | octx->n_threads_div = ctx->n_threads_div; |
| 132 | } |
| 133 | |
| 134 | static inline bool htp_ops_context_set_n_threads_div( |
| 135 | struct htp_ops_context * octx, |
| 136 | uint32_t n_threads, |
| 137 | struct fastdiv_values n_threads_div) { |
| 138 | if (n_threads == 0 || n_threads > octx->ctx->n_threads) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | octx->n_threads = n_threads; |
| 143 | octx->n_threads_div = n_threads_div; |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | static inline bool htp_ops_context_set_n_threads(struct htp_ops_context * octx, uint32_t n_threads) { |
| 149 | if (n_threads == 0 || n_threads > octx->ctx->n_threads) { |
| 150 | return false; |
| 151 | } |
| 152 | if (n_threads == octx->n_threads) { |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | const struct fastdiv_values n_threads_div = n_threads == octx->ctx->n_threads |
| 157 | ? octx->ctx->n_threads_div |
| 158 | : init_fastdiv_values(n_threads); |
| 159 | return htp_ops_context_set_n_threads_div(octx, n_threads, n_threads_div); |
| 160 | } |
| 161 | |
| 162 | static inline void htp_ops_context_set_status(struct htp_ops_context * octx, int status) { |
| 163 | if (status > HTP_STATUS_OK && octx->status == HTP_STATUS_OK) { |
| 164 | octx->status = status; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | int op_matmul(struct htp_ops_context * octx); |
| 169 | int op_matmul_id(struct htp_ops_context * octx); |
| 170 | int op_matmul_nx(struct htp_ops_context * octx); |
| 171 | int op_matmul_id_nx(struct htp_ops_context * octx); |
| 172 | int op_binary(struct htp_ops_context * octx); |
| 173 | int op_unary(struct htp_ops_context * octx); |
| 174 | int op_sum(struct htp_ops_context * octx); |
| 175 | int op_sum_rows(struct htp_ops_context * octx); |
| 176 | int op_argmax(struct htp_ops_context * octx); |
| 177 | int op_glu(struct htp_ops_context * octx); |
| 178 | int op_softmax(struct htp_ops_context * octx); |
| 179 | int op_add_id(struct htp_ops_context * octx); |
| 180 | int op_rope(struct htp_ops_context * octx); |
| 181 | int op_flash_attn_ext(struct htp_ops_context * octx); |
| 182 | int op_set_rows(struct htp_ops_context * octx); |
| 183 | int op_get_rows(struct htp_ops_context * octx); |
| 184 | int op_cpy(struct htp_ops_context * octx); |
| 185 | int op_repeat(struct htp_ops_context * octx); |
| 186 | int op_argsort(struct htp_ops_context * octx); |
| 187 | int op_top_k(struct htp_ops_context * octx); |
| 188 | int op_ssm_conv(struct htp_ops_context * octx); |
| 189 | int op_cumsum(struct htp_ops_context * octx); |
| 190 | int op_top_k_mask(struct htp_ops_context * octx); |
| 191 | int op_fill(struct htp_ops_context * octx); |
| 192 | int op_concat(struct htp_ops_context * octx); |
| 193 | int op_diag(struct htp_ops_context * octx); |
| 194 | int op_solve_tri(struct htp_ops_context * octx); |
| 195 | int op_gated_delta_net(struct htp_ops_context * octx); |
| 196 | int op_pad(struct htp_ops_context * octx); |
| 197 | int op_im2col(struct htp_ops_context * octx); |
| 198 | int op_im2col_3d(struct htp_ops_context * octx); |
| 199 | int op_conv_2d(struct htp_ops_context * octx); |
| 200 | int op_conv_2d_dw(struct htp_ops_context * octx); |
| 201 | int op_pool_2d(struct htp_ops_context * octx); |
| 202 | int op_pool_1d(struct htp_ops_context * octx); |
| 203 | int op_allreduce(struct htp_ops_context * octx); |
| 204 | int op_roll(struct htp_ops_context * octx); |
| 205 | int op_upscale(struct htp_ops_context * octx); |
| 206 | int op_lightning_indexer(struct htp_ops_context * octx); |
| 207 | |
| 208 | #endif /* HTP_CTX_H */ |