char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
b2dd28a3b
char/aliceai-t5-moe
1#pragma clang diagnostic ignored "-Wunused-function" 2 3#include <stdbool.h> 4#include <stdlib.h> 5#include <string.h> 6 7#include <qurt_thread.h> 8#include <qurt_futex.h> 9#include <qurt_hvx.h> 10 11#include <HAP_compute_res.h> 12 13#include "hmx-queue.h" 14 15#define QURT_LOWEST_PRIO (254) 16 17static inline void hmx_lock (hmx_queue_t q ) 18{ 19if (!q -> hmx_locked ) { 20HAP_compute_res_hmx_lock (q -> hap_rctx ); 21q -> hmx_locked = true; 22 } 23} 24 25static inline void hmx_unlock (hmx_queue_t q ) 26{ 27if (q -> hmx_locked ) { 28HAP_compute_res_hmx_unlock (q -> hap_rctx ); 29q -> hmx_locked = false; 30 } 31} 32 33static inline void hmx_queue_process (hmx_queue_t q ,bool * killed ) { 34unsigned int ir = atomic_load (& q -> idx_read ); 35 36while (ir != atomic_load (& q -> idx_write )) { 37struct hmx_queue_desc * d = & q -> desc [ir ]; 38if (!d -> done ) { 39FARF (HIGH ,"hmx-queue-process: ir %u func %p data %p" ,ir ,d -> func ,d -> data ); 40 41uintptr_t sig = (uintptr_t )d -> func ; 42switch (sig ) { 43case HMX_QUEUE_NOOP :/* noop */ ;break ; 44case HMX_QUEUE_KILL :* killed = true;break ; 45case HMX_QUEUE_SUSPEND :hmx_unlock (q );break ; 46case HMX_QUEUE_WAKEUP :hmx_lock (q );break ; 47default : 48hmx_lock (q ); 49htp_trace_event_start (q -> trace ,HTP_TRACE_EVT_HMX_COMP ,ir ); 50d -> func (d -> data ); 51htp_trace_event_stop (q -> trace ,HTP_TRACE_EVT_HMX_COMP ,ir ); 52break ; 53 } 54 55atomic_fetch_add (& d -> done ,1 ); 56 } 57 58ir = (ir + 1 )& q -> idx_mask ; 59atomic_store (& q -> idx_read ,ir ); 60 } 61} 62 63static void hmx_queue_thread (void * arg ) { 64hmx_queue_t q = (hmx_queue_t )arg ; 65 66FARF (HIGH ,"hmx-queue-thread: started" ); 67 68bool killed = false; 69 70unsigned int poll_cnt = HMX_QUEUE_POLL_COUNT ; 71unsigned int prev_seqn = 0 ; 72while (!killed ) { 73unsigned int seqn = atomic_load (& q -> seqn ); 74if (seqn == prev_seqn ) { 75// drop HVX context while spinning 76if (poll_cnt > 1 && poll_cnt == HMX_QUEUE_POLL_COUNT ) { 77qurt_hvx_unlock (); 78 } 79if (-- poll_cnt ) {hex_pause ();continue ; } 80FARF (HIGH ,"hmx-queue-thread: sleeping" ); 81qurt_futex_wait (& q -> seqn ,prev_seqn ); 82poll_cnt = HMX_QUEUE_POLL_COUNT ; 83continue ; 84 } 85prev_seqn = seqn ; 86poll_cnt = HMX_QUEUE_POLL_COUNT ; 87 88FARF (HIGH ,"hmx-queue-thread: new work" ); 89 90hmx_queue_process (q ,& killed ); 91 } 92 93FARF (HIGH ,"hmx-queue-thread: stopped" ); 94} 95 96size_t hmx_queue_sizeof (size_t capacity ,uint32_t stack_size ) { 97capacity = hex_ceil_pow2 (capacity ); 98size_t size_q = hex_align_up (sizeof (struct hmx_queue_s ),HEX_L2_LINE_SIZE ); 99size_t size_desc = hex_align_up (capacity * sizeof (struct hmx_queue_desc ),HEX_L2_LINE_SIZE ); 100size_t size_stack = stack_size ; 101return size_q + size_desc + size_stack ; 102} 103 104size_t hmx_queue_alignof (void ) { 105return HEX_L2_LINE_SIZE ; 106} 107 108hmx_queue_t hmx_queue_init (void * ptr ,size_t capacity ,uint32_t stack_size ,uint32_t hap_rctx ,struct htp_thread_trace * trace ) { 109capacity = hex_ceil_pow2 (capacity ); 110size_t size_q = hex_align_up (sizeof (struct hmx_queue_s ),HEX_L2_LINE_SIZE ); 111size_t size_desc = hex_align_up (capacity * sizeof (struct hmx_queue_desc ),HEX_L2_LINE_SIZE ); 112 113uint8_t * block = (uint8_t * )ptr ; 114 115hmx_queue_t q = (hmx_queue_t )block ;block += size_q ; 116memset (q ,0 ,sizeof (struct hmx_queue_s )); 117 118q -> capacity = capacity ; 119q -> idx_mask = capacity - 1 ; 120q -> hap_rctx = hap_rctx ; 121q -> external_mem = true; 122 123q -> desc = (struct hmx_queue_desc * )block ;block += size_desc ; 124memset (q -> desc ,0 ,capacity * sizeof (struct hmx_queue_desc )); 125 126q -> stack = block ; 127memset (q -> stack ,0 ,stack_size ); 128 129q -> trace = trace ; 130 131// Match caller thread priority (same pattern as worker-pool.c). 132int prio = qurt_thread_get_priority (qurt_thread_get_id ()); 133if (prio < 1 ) { 134prio = 1 ; 135 } 136if (prio > QURT_LOWEST_PRIO ) { 137prio = QURT_LOWEST_PRIO ; 138 } 139 140qurt_thread_attr_t attr ; 141qurt_thread_attr_init (& attr ); 142qurt_thread_attr_set_stack_addr (& attr ,q -> stack ); 143qurt_thread_attr_set_stack_size (& attr ,stack_size ); 144qurt_thread_attr_set_priority (& attr ,prio ); 145qurt_thread_attr_set_name (& attr ,"hmx-queue" ); 146 147int err = qurt_thread_create (& q -> thread ,& attr ,hmx_queue_thread ,q ); 148if (err ) { 149FARF (ERROR ,"hmx-worker: thread create failed (%d)" ,err ); 150return NULL ; 151 } 152 153FARF (HIGH ,"hmx-queue: capacity %u\n" ,capacity ); 154 155return q ; 156} 157 158void hmx_queue_free (hmx_queue_t q ) { 159if (!q ) { 160return ; 161 } 162 163// Tell the worker to exit. 164hmx_queue_flush (q ); 165hmx_queue_signal (q ,HMX_QUEUE_KILL ); 166hmx_queue_flush (q ); 167 168int status ; 169qurt_thread_join (q -> thread ,& status ); 170}