char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
eafe15a5e
char/aliceai-t5-moe
1#pragma clang diagnostic ignored "-Wunused-but-set-variable" 2 3#include <HAP_farf.h> 4#include <HAP_perf.h> 5 6#define GGML_COMMON_DECL_C 7#include "ggml-common.h" 8#include "hex-common.h" 9#include "hex-profile.h" 10#include "htp-ctx.h" 11#include "htp-ops.h" 12#include "htp-tensor.h" 13#include "hvx-types.h" 14#include "hex-utils.h" 15#include "hvx-copy.h" 16#include "hex-dma.h" 17 18#define htp_diag_tensors_preamble \ 19 const struct htp_tensor * restrict src0 = octx->src[0]; \ 20 const struct htp_tensor * restrict dst = octx->dst; \ 21 \ 22 const uint32_t ne02 = src0->ne[2]; \ 23 \ 24 const uint32_t ne0 = dst->ne[0]; \ 25 const uint32_t ne1 = dst->ne[1]; \ 26 \ 27 const uint32_t nb02 = src0->nb[2]; \ 28 const uint32_t nb03 = src0->nb[3]; \ 29 \ 30 const uint32_t nb1 = dst->nb[1]; \ 31 const uint32_t nb2 = dst->nb[2]; \ 32 const uint32_t nb3 = dst->nb[3]; 33 34struct htp_diag_context { 35struct htp_ops_context * octx ; 36size_t src_batch_size ; 37size_t dst_row_size ; 38size_t src_batch_size_aligned ; 39size_t dst_row_size_aligned ; 40uint32_t batches_per_thread ; 41uint32_t total_batches ; 42uint32_t batch_start ; 43}; 44 45#define htp_diag_preamble \ 46 struct htp_diag_context * dctx = (struct htp_diag_context *) data; \ 47 struct htp_ops_context * octx = dctx->octx; \ 48 htp_diag_tensors_preamble; 49 50static inline void hvx_diag_row_f32 (const float * restrictsrc ,float * restrictdst , 51uint32_t row_idx ,uint32_t n ) { 52hvx_splat_f32_a ((uint8_t * )dst ,0.0f ,n ); 53dst [row_idx ]= src [row_idx ]; 54} 55 56// --------------------------------------------------------------------------- 57// Per thread worker: DMA src fetch, compute in VTCM, DMA dst writeback 58// --------------------------------------------------------------------------- 59 60static void diag_thread_f32_dma (unsigned int nth ,unsigned int ith ,void * data ) { 61htp_diag_preamble ; 62dma_queue * dma_queue = octx -> ctx -> dma [ith ]; 63 64const uint32_t ib0 = dctx -> batch_start + dctx -> batches_per_thread * ith ; 65const uint32_t ib1 = MIN (ib0 + dctx -> batches_per_thread ,dctx -> batch_start + dctx -> total_batches ); 66 67if (ib0 >=ib1 ) { 68return ; 69 } 70 71const size_t src_batch_size = dctx -> src_batch_size ; 72const size_t dst_row_size = dctx -> dst_row_size ; 73const size_t src_batch_size_aligned = dctx -> src_batch_size_aligned ; 74const size_t dst_row_size_aligned = dctx -> dst_row_size_aligned ; 75 76const uint8_t * src_data = (const uint8_t * )src0 -> data ; 77uint8_t * dst_data = (uint8_t * )dst -> data ; 78 79// 1 src buffer + 1 dst row buffer per thread in VTCM 80uint8_t * src_spad = octx -> src0_spad .data + (ith * src_batch_size_aligned ); 81uint8_t * dst_spad = octx -> dst_spad .data + (ith * dst_row_size_aligned ); 82 83struct htp_thread_trace * tr = & octx -> ctx -> trace [ith ]; 84 85for (uint32_t ib = ib0 ;ib < ib1 ;ib ++ ) { 86const uint32_t i3 = ib /ne02 ; 87const uint32_t i2 = ib %ne02 ; 88 89const uint8_t * src_batch = src_data + i3 * nb03 + i2 * nb02 ; 90 91// Fetch source vector into VTCM 92dma_queue_push_ddr_to_vtcm (dma_queue , 93dma_make_ptr (src_spad ,src_batch ), 94src_batch_size_aligned ,src_batch_size ,1 ); 95dma_queue_flush (dma_queue ); 96 97const float * src_spad_f32 = (const float * )src_spad ; 98float * dst_spad_f32 = (float * )dst_spad ; 99 100for (uint32_t i1 = 0 ;i1 < ne1 ;i1 ++ ) { 101// Compute row in VTCM 102htp_trace_event_start (tr ,HTP_TRACE_EVT_HVX_COMP , (uint16_t ) (ib * ne1 + i1 )); 103hvx_diag_row_f32 (src_spad_f32 ,dst_spad_f32 ,i1 ,ne0 ); 104htp_trace_event_stop (tr ,HTP_TRACE_EVT_HVX_COMP , (uint16_t ) (ib * ne1 + i1 )); 105 106// Write completed row back to DDR 107uint8_t * dst_row = dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1 ; 108dma_queue_push_vtcm_to_ddr (dma_queue , 109dma_make_ptr (dst_row ,dst_spad ), 110dst_row_size ,dst_row_size_aligned ,1 ); 111dma_queue_flush (dma_queue ); 112 } 113 } 114 115FARF (HIGH ,"diag-f32-dma %d/%d: %ux%ux%ux%u (%u:%u) -> %ux%ux%ux%u\n" , 116ith ,nth ,src0 -> ne [0 ],src0 -> ne [1 ],src0 -> ne [2 ],src0 -> ne [3 ],ib0 ,ib1 , 117dst -> ne [0 ],dst -> ne [1 ],dst -> ne [2 ],dst -> ne [3 ]); 118} 119 120// --------------------------------------------------------------------------- 121// Per thread worker: Direct HVX (no DMA) 122// --------------------------------------------------------------------------- 123 124static void diag_thread_f32 (unsigned int nth ,unsigned int ith ,void * data ) { 125htp_diag_preamble ; 126 127const uint8_t * src_data = (const uint8_t * )src0 -> data ; 128uint8_t * dst_data = (uint8_t * )dst -> data ; 129 130const uint32_t ib0 = dctx -> batch_start + dctx -> batches_per_thread * ith ; 131const uint32_t ib1 = MIN (ib0 + dctx -> batches_per_thread ,dctx -> batch_start + dctx -> total_batches ); 132 133struct htp_thread_trace * tr = & octx -> ctx -> trace [ith ]; 134htp_trace_event_start (tr ,HTP_TRACE_EVT_HVX_COMP , (uint16_t )ib0 ); 135 136for (uint32_t ib = ib0 ;ib < ib1 ;ib ++ ) { 137const uint32_t i3 = ib /ne02 ; 138const uint32_t i2 = ib %ne02 ; 139 140const float * restrictsrc_batch = (const float * )(src_data + i3 * nb03 + i2 * nb02 ); 141 142for (uint32_t i1 = 0 ;i1 < ne1 ;i1 ++ ) { 143float * restrictdst_row = (float * )(dst_data + i3 * nb3 + i2 * nb2 + i1 * nb1 ); 144hvx_diag_row_f32 (src_batch ,dst_row ,i1 ,ne0 ); 145 } 146 } 147 148htp_trace_event_stop (tr ,HTP_TRACE_EVT_HVX_COMP , (uint16_t )ib0 ); 149 150FARF (HIGH ,"diag-f32 %d/%d: %ux%ux%ux%u (%u:%u) -> %ux%ux%ux%u\n" , 151ith ,nth ,src0 -> ne [0 ],src0 -> ne [1 ],src0 -> ne [2 ],src0 -> ne [3 ],ib0 ,ib1 , 152dst -> ne [0 ],dst -> ne [1 ],dst -> ne [2 ],dst -> ne [3 ]); 153} 154 155int op_diag_f32 (struct htp_ops_context * octx ) { 156const struct htp_tensor * src0 = octx -> src [0 ]; 157const struct htp_tensor * dst = octx -> dst ; 158 159if (octx -> flags & HTP_OPFLAGS_SKIP_COMPUTE ) { 160return HTP_STATUS_OK ; 161 } 162 163const uint32_t total_batches = src0 -> ne [2 ]* src0 -> ne [3 ]; 164const size_t dst_batch_size = dst -> ne [1 ]* dst -> nb [1 ]; 165 166uint32_t batch_start = 0 ; 167uint32_t nbatches = total_batches ; 168 169if (octx -> ctx -> mdev .count > 1 ) { 170bool can_split = htp_tensor_mdev_data_aligned (dst )&& (dst -> ne [0 ]== 1 || dst -> nb [0 ]== sizeof (float ))&& !htp_tensor_is_permuted (dst ); 171uint32_t batches_per_chunk = 1 ; 172if (can_split ) { 173if (dst -> ne [2 ]> 1 && (dst -> nb [2 ]& (HTP_TENSOR_MDEV_LINE_SIZE - 1 ))== 0 && 174 (dst -> ne [3 ] <=1 || (dst -> nb [3 ]& (HTP_TENSOR_MDEV_LINE_SIZE - 1 ))== 0 )) { 175batches_per_chunk = 1 ; 176 }else if (dst -> nb [2 ]== dst_batch_size && 177 (dst -> ne [3 ] <=1 || dst -> nb [3 ]== dst -> nb [2 ]* dst -> ne [2 ])) { 178batches_per_chunk = (dst_batch_size > 0 ) ? (HEX_L2_LINE_SIZE /hex_gcd_u32 (dst_batch_size ,HEX_L2_LINE_SIZE )) :1 ; 179 }else { 180can_split = false; 181 } 182 } 183 184const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition (total_batches ,can_split ?batches_per_chunk :0 ,octx -> ctx -> mdev .idx ,octx -> ctx -> mdev .count ,& octx -> ctx -> mdev .count_div ); 185batch_start = range .start ; 186nbatches = range .count ; 187 } 188 189if (nbatches == 0 ) { 190return HTP_STATUS_OK ; 191 } 192 193const uint32_t n_threads = octx -> n_threads ; 194 195const size_t src_batch_size = src0 -> ne [0 ]* sizeof (float ); 196const size_t dst_row_size = dst -> ne [0 ]* sizeof (float ); 197const size_t src_batch_size_aligned = hex_round_up (src_batch_size ,VLEN ); 198const size_t dst_row_size_aligned = hex_round_up (dst_row_size ,VLEN ); 199 200// 1 src buffer + 1 dst row buffer per thread 201const size_t spad_per_thread = src_batch_size_aligned + dst_row_size_aligned ; 202 203octx -> src0_spad .size_per_thread = src_batch_size_aligned ; 204octx -> dst_spad .size_per_thread = dst_row_size_aligned ; 205 206octx -> src0_spad .size = n_threads * octx -> src0_spad .size_per_thread ; 207octx -> dst_spad .size = n_threads * octx -> dst_spad .size_per_thread ; 208 209octx -> src0_spad .data = octx -> ctx -> vtcm_base ;octx -> src0_spad .src = NULL ; 210octx -> dst_spad .data = octx -> src0_spad .data + octx -> src0_spad .size ;octx -> dst_spad .src = NULL ; 211 212struct htp_diag_context dctx = { 213 .octx = octx , 214 .src_batch_size = src_batch_size , 215 .dst_row_size = dst_row_size , 216 .src_batch_size_aligned = src_batch_size_aligned , 217 .dst_row_size_aligned = dst_row_size_aligned , 218 .batches_per_thread = fastdiv (nbatches + n_threads - 1 ,& octx -> n_threads_div ), 219 .total_batches = nbatches , 220 .batch_start = batch_start , 221 }; 222 223if (octx -> ctx -> vtcm_size < spad_per_thread * n_threads ) { 224work_queue_run (octx -> ctx -> work_queue ,diag_thread_f32 ,& dctx ,n_threads ); 225 }else { 226work_queue_run (octx -> ctx -> work_queue ,diag_thread_f32_dma ,& dctx ,n_threads ); 227 } 228 229return HTP_STATUS_OK ; 230} 231 232int op_diag (struct htp_ops_context * octx ) { 233const struct htp_tensor * dst = octx -> dst ; 234 235int err = HTP_STATUS_OK ; 236 237switch (dst -> type ) { 238case HTP_TYPE_F32 : 239err = op_diag_f32 (octx ); 240break ; 241default : 242err = HTP_STATUS_NO_SUPPORT ; 243break ; 244 } 245 246return err ; 247}