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-variable" 2#pragma clang diagnostic ignored "-Wunused-function" 3#pragma clang diagnostic ignored "-Wunused-but-set-variable" 4 5#include <HAP_farf.h> 6#include <HAP_perf.h> 7 8#include <string.h> 9 10#include "hvx-utils.h" 11 12#define GGML_COMMON_DECL_C 13#include "ggml-common.h" 14#include "htp-ctx.h" 15#include "hex-common.h" 16#include "hex-profile.h" 17#include "htp-ops.h" 18#include "htp-tensor.h" 19 20struct htp_repeat_context { 21struct htp_ops_context * octx ; 22 23uint32_t nr0 ; 24uint32_t nr1 ; 25uint32_t nr2 ; 26uint32_t nr3 ; 27 28uint32_t nrows_per_thread ; 29uint32_t total_dst_rows ;// ne1 * ne2 * ne3 30uint32_t row_start ; 31 32size_t type_size ; 33}; 34 35static void repeat_job_per_thread (unsigned int nth ,unsigned int ith ,void * data ) { 36const struct htp_repeat_context * rctx = (const struct htp_repeat_context * )data ; 37struct htp_ops_context * octx = rctx -> octx ; 38const struct htp_tensor * src = octx -> src [0 ]; 39const struct htp_tensor * dst = octx -> dst ; 40 41const uint32_t ne00 = src -> ne [0 ]; 42const uint32_t ne01 = src -> ne [1 ]; 43const uint32_t ne02 = src -> ne [2 ]; 44const uint32_t ne03 = src -> ne [3 ]; 45 46const uint32_t nb00 = src -> nb [0 ]; 47const uint32_t nb01 = src -> nb [1 ]; 48const uint32_t nb02 = src -> nb [2 ]; 49const uint32_t nb03 = src -> nb [3 ]; 50 51const uint32_t ne0 = dst -> ne [0 ]; 52const uint32_t ne1 = dst -> ne [1 ]; 53const uint32_t ne2 = dst -> ne [2 ]; 54const uint32_t ne3 = dst -> ne [3 ]; 55 56const uint32_t nb0 = dst -> nb [0 ]; 57const uint32_t nb1 = dst -> nb [1 ]; 58const uint32_t nb2 = dst -> nb [2 ]; 59const uint32_t nb3 = dst -> nb [3 ]; 60 61const uint32_t nr0 = rctx -> nr0 ; 62const uint32_t nr1 = rctx -> nr1 ; 63const uint32_t nr2 = rctx -> nr2 ; 64const uint32_t nr3 = rctx -> nr3 ; 65 66const size_t row_bytes = ne00 * rctx -> type_size ; 67 68const uint32_t row_start = rctx -> row_start + rctx -> nrows_per_thread * ith ; 69const uint32_t row_end = MIN (row_start + rctx -> nrows_per_thread ,rctx -> row_start + rctx -> total_dst_rows ); 70 71struct htp_thread_trace * tr = & octx -> ctx -> trace [ith ]; 72htp_trace_event_start (tr ,HTP_TRACE_EVT_HVX_COMP ,row_start ); 73 74for (uint32_t dst_row = row_start ;dst_row < row_end ;dst_row ++ ) { 75// Decompose flat dst row index into (i1, i2, i3) 76const uint32_t i1 = dst_row %ne1 ; 77const uint32_t i2 = (dst_row /ne1 ) %ne2 ; 78const uint32_t i3 = dst_row / (ne1 * ne2 ); 79 80// Map to source indices (tiling) 81const uint32_t k1 = i1 %ne01 ; 82const uint32_t k2 = i2 %ne02 ; 83const uint32_t k3 = i3 %ne03 ; 84 85const uint8_t * src_row = (const uint8_t * )src -> data + k1 * nb01 + k2 * nb02 + k3 * nb03 ; 86uint8_t * dst_base = (uint8_t * )dst -> data + i1 * nb1 + i2 * nb2 + i3 * nb3 ; 87 88// Tile along dimension 0 89for (uint32_t i0 = 0 ;i0 < nr0 ;i0 ++ ) { 90uint8_t * dst_ptr = dst_base + i0 * ne00 * nb0 ; 91memcpy (dst_ptr ,src_row ,row_bytes ); 92 } 93 } 94 95htp_trace_event_stop (tr ,HTP_TRACE_EVT_HVX_COMP ,row_start ); 96 97FARF (HIGH ,"repeat %d/%d: (%ux%ux%ux%u) -> (%ux%ux%ux%u) rows %u:%u\n" , 98ith ,nth ,src -> ne [0 ],src -> ne [1 ],src -> ne [2 ],src -> ne [3 ], 99dst -> ne [0 ],dst -> ne [1 ],dst -> ne [2 ],dst -> ne [3 ], 100row_start ,row_end ); 101} 102 103int op_repeat (struct htp_ops_context * octx ) { 104const struct htp_tensor * src0 = octx -> src [0 ]; 105const struct htp_tensor * dst = octx -> dst ; 106 107// Validate that dst dims are multiples of src dims 108if (dst -> ne [0 ] %src0 -> ne [0 ]!= 0 || 109dst -> ne [1 ] %src0 -> ne [1 ]!= 0 || 110dst -> ne [2 ] %src0 -> ne [2 ]!= 0 || 111dst -> ne [3 ] %src0 -> ne [3 ]!= 0 ) { 112FARF (ERROR ,"repeat: dst dims must be multiples of src dims\n" ); 113return HTP_STATUS_INVAL_PARAMS ; 114 } 115 116size_t type_size ; 117switch (src0 -> type ) { 118case HTP_TYPE_F32 :type_size = 4 ;break ; 119case HTP_TYPE_F16 :type_size = 2 ;break ; 120default : 121FARF (ERROR ,"repeat: unsupported type %u\n" ,src0 -> type ); 122return HTP_STATUS_NO_SUPPORT ; 123 } 124 125if (octx -> flags & HTP_OPFLAGS_SKIP_COMPUTE ) { 126return HTP_STATUS_OK ; 127 } 128 129const uint32_t total_dst_rows = dst -> ne [1 ]* dst -> ne [2 ]* dst -> ne [3 ]; 130const size_t dst_row_size = dst -> ne [0 ]* type_size ; 131 132uint32_t row_start = 0 ; 133uint32_t nrows = total_dst_rows ; 134 135if (octx -> ctx -> mdev .count > 1 ) { 136uint32_t rows_per_chunk = 0 ; 137htp_tensor_mdev_rows_per_chunk (dst ,type_size , (uint32_t )dst_row_size ,& rows_per_chunk ); 138const struct htp_tensor_mdev_range range = htp_tensor_mdev_partition (total_dst_rows ,rows_per_chunk ,octx -> ctx -> mdev .idx ,octx -> ctx -> mdev .count ,& octx -> ctx -> mdev .count_div ); 139row_start = range .start ; 140nrows = range .count ; 141 } 142 143if (nrows == 0 ) { 144return HTP_STATUS_OK ; 145 } 146 147const uint32_t n_threads = octx -> n_threads ; 148 149struct htp_repeat_context rctx = { 150 .octx = octx , 151 .nr0 = dst -> ne [0 ] /src0 -> ne [0 ], 152 .nr1 = dst -> ne [1 ] /src0 -> ne [1 ], 153 .nr2 = dst -> ne [2 ] /src0 -> ne [2 ], 154 .nr3 = dst -> ne [3 ] /src0 -> ne [3 ], 155 .nrows_per_thread = fastdiv (nrows + n_threads - 1 ,& octx -> n_threads_div ), 156 .total_dst_rows = nrows , 157 .row_start = row_start , 158 .type_size = type_size , 159 }; 160 161FARF (HIGH ,"repeat: (%ux%ux%ux%u) -> (%ux%ux%ux%u) nr=(%u,%u,%u,%u)\n" , 162src0 -> ne [0 ],src0 -> ne [1 ],src0 -> ne [2 ],src0 -> ne [3 ], 163dst -> ne [0 ],dst -> ne [1 ],dst -> ne [2 ],dst -> ne [3 ], 164rctx .nr0 ,rctx .nr1 ,rctx .nr2 ,rctx .nr3 ); 165 166work_queue_run (octx -> ctx -> work_queue ,repeat_job_per_thread ,& rctx ,n_threads ); 167 168return HTP_STATUS_OK ; 169}