char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
9e54e687c
char/aliceai-t5-moe
1#include <filesystem> 2#include <set> 3#include <sstream> 4#include <string> 5 6#ifdef _WIN32 7# define WIN32_LEAN_AND_MEAN 8# ifndef NOMINMAX 9# define NOMINMAX 10# endif 11# include <windows.h> 12# include <winevt.h> 13#else 14# include <dlfcn.h> 15# include <unistd.h> 16#endif 17 18#pragma clang diagnostic ignored "-Wgnu-anonymous-struct" 19#pragma clang diagnostic ignored "-Wmissing-prototypes" 20#pragma clang diagnostic ignored "-Wsign-compare" 21#pragma clang diagnostic ignored "-Wlanguage-extension-token" 22#pragma clang diagnostic ignored "-Wmicrosoft-enum-value" 23#pragma clang diagnostic ignored "-Wnested-anon-types" 24 25#include "ggml-impl.h" 26#include "htp-drv.h" 27#include "libdl.h" 28 29#include <domain.h> 30 31// 32// Driver API types 33// 34 35typedef void * (* rpcmem_alloc_pfn_t )(int heapid ,uint32_t flags ,int size ); 36typedef void * (* rpcmem_alloc2_pfn_t )(int heapid ,uint32_t flags ,size_t size ); 37typedef void (* rpcmem_free_pfn_t )(void * po ); 38typedef int (* rpcmem_to_fd_pfn_t )(void * po ); 39 40typedef AEEResult (* dspqueue_create_pfn_t )(int domain , 41uint32_t flags , 42uint32_t req_queue_size , 43uint32_t resp_queue_size , 44dspqueue_callback_t packet_callback , 45dspqueue_callback_t error_callback , 46void * callback_context , 47dspqueue_t * queue ); 48typedef AEEResult (* dspqueue_close_pfn_t )(dspqueue_t queue ); 49typedef AEEResult (* dspqueue_export_pfn_t )(dspqueue_t queue ,uint64_t * queue_id ); 50typedef AEEResult (* dspqueue_write_pfn_t )(dspqueue_t queue ,uint32_t flags , 51uint32_t num_buffers , 52struct dspqueue_buffer * buffers , 53uint32_t message_length , 54const uint8_t * message , 55uint32_t timeout_us ); 56typedef AEEResult (* dspqueue_read_pfn_t )(dspqueue_t queue ,uint32_t * flags , 57uint32_t max_buffers ,uint32_t * num_buffers , 58struct dspqueue_buffer * buffers , 59uint32_t max_message_length , 60uint32_t * message_length ,uint8_t * message , 61uint32_t timeout_us ); 62typedef AEEResult (* dspqueue_read_noblock_pfn_t )(dspqueue_t queue ,uint32_t * flags , 63uint32_t max_buffers ,uint32_t * num_buffers , 64struct dspqueue_buffer * buffers , 65uint32_t max_message_length , 66uint32_t * message_length ,uint8_t * message ); 67typedef int (* fastrpc_mmap_pfn_t )(int domain ,int fd ,void * addr ,int offset ,size_t length ,enum fastrpc_map_flags flags ); 68typedef int (* fastrpc_munmap_pfn_t )(int domain ,int fd ,void * addr ,size_t length ); 69 70typedef int (* remote_handle64_open_pfn_t )(const char * name ,remote_handle64 * ph ); 71typedef int (* remote_handle64_invoke_pfn_t )(remote_handle64 h ,uint32_t dwScalars ,remote_arg * pra ); 72typedef int (* remote_handle64_close_pfn_t )(remote_handle h ); 73typedef int (* remote_handle_control_pfn_t )(uint32_t req ,void * data ,uint32_t datalen ); 74typedef int (* remote_handle64_control_pfn_t )(remote_handle64 h ,uint32_t req ,void * data ,uint32_t datalen ); 75typedef int (* remote_session_control_pfn_t )(uint32_t req ,void * data ,uint32_t datalen ); 76typedef int (* remote_system_request_pfn_t )(system_req_payload * req ); 77 78// 79// Driver API pfns 80// 81 82rpcmem_alloc_pfn_t rpcmem_alloc_pfn = nullptr ; 83rpcmem_alloc2_pfn_t rpcmem_alloc2_pfn = nullptr ; 84rpcmem_free_pfn_t rpcmem_free_pfn = nullptr ; 85rpcmem_to_fd_pfn_t rpcmem_to_fd_pfn = nullptr ; 86 87fastrpc_mmap_pfn_t fastrpc_mmap_pfn = nullptr ; 88fastrpc_munmap_pfn_t fastrpc_munmap_pfn = nullptr ; 89 90dspqueue_create_pfn_t dspqueue_create_pfn = nullptr ; 91dspqueue_close_pfn_t dspqueue_close_pfn = nullptr ; 92dspqueue_export_pfn_t dspqueue_export_pfn = nullptr ; 93dspqueue_write_pfn_t dspqueue_write_pfn = nullptr ; 94dspqueue_read_pfn_t dspqueue_read_pfn = nullptr ; 95dspqueue_read_noblock_pfn_t dspqueue_read_noblock_pfn = nullptr ; 96 97remote_handle64_open_pfn_t remote_handle64_open_pfn = nullptr ; 98remote_handle64_invoke_pfn_t remote_handle64_invoke_pfn = nullptr ; 99remote_handle64_close_pfn_t remote_handle64_close_pfn = nullptr ; 100remote_handle_control_pfn_t remote_handle_control_pfn = nullptr ; 101remote_handle64_control_pfn_t remote_handle64_control_pfn = nullptr ; 102remote_session_control_pfn_t remote_session_control_pfn = nullptr ; 103remote_system_request_pfn_t remote_system_request_pfn = nullptr ; 104 105// 106// Driver API 107// 108 109void * rpcmem_alloc (int heapid ,uint32_t flags ,int size ) { 110return rpcmem_alloc_pfn (heapid ,flags ,size ); 111} 112 113void * rpcmem_alloc2 (int heapid ,uint32_t flags ,size_t size ) { 114if (rpcmem_alloc2_pfn ) { 115return rpcmem_alloc2_pfn (heapid ,flags ,size ); 116 }else { 117GGML_LOG_INFO ("ggml-hex: rpcmem_alloc2 not found, falling back to rpcmem_alloc\n" ); 118return rpcmem_alloc_pfn (heapid ,flags ,size ); 119 } 120} 121 122void rpcmem_free (void * po ) { 123return rpcmem_free_pfn (po ); 124} 125 126int rpcmem_to_fd (void * po ) { 127return rpcmem_to_fd_pfn (po ); 128} 129 130HTPDRV_API int fastrpc_mmap (int domain ,int fd ,void * addr ,int offset ,size_t length ,enum fastrpc_map_flags flags ) { 131return fastrpc_mmap_pfn (domain ,fd ,addr ,offset ,length ,flags ); 132} 133 134HTPDRV_API int fastrpc_munmap (int domain ,int fd ,void * addr ,size_t length ) { 135return fastrpc_munmap_pfn (domain ,fd ,addr ,length ); 136} 137 138AEEResult dspqueue_create (int domain , 139uint32_t flags , 140uint32_t req_queue_size , 141uint32_t resp_queue_size , 142dspqueue_callback_t packet_callback , 143dspqueue_callback_t error_callback , 144void * callback_context , 145dspqueue_t * queue ) { 146return dspqueue_create_pfn (domain ,flags ,req_queue_size ,resp_queue_size ,packet_callback ,error_callback , 147callback_context ,queue ); 148} 149 150AEEResult dspqueue_close (dspqueue_t queue ) { 151return dspqueue_close_pfn (queue ); 152} 153 154AEEResult dspqueue_export (dspqueue_t queue ,uint64_t * queue_id ) { 155return dspqueue_export_pfn (queue ,queue_id ); 156} 157 158AEEResult dspqueue_write (dspqueue_t queue , 159uint32_t flags , 160uint32_t num_buffers , 161struct dspqueue_buffer * buffers , 162uint32_t message_length , 163const uint8_t * message , 164uint32_t timeout_us ) { 165return dspqueue_write_pfn (queue ,flags ,num_buffers ,buffers ,message_length ,message ,timeout_us ); 166} 167 168AEEResult dspqueue_read (dspqueue_t queue , 169uint32_t * flags , 170uint32_t max_buffers , 171uint32_t * num_buffers , 172struct dspqueue_buffer * buffers , 173uint32_t max_message_length , 174uint32_t * message_length , 175uint8_t * message , 176uint32_t timeout_us ) { 177#ifdef _WIN32 178if (timeout_us == 0 ) { 179return dspqueue_read_noblock_pfn (queue ,flags ,max_buffers ,num_buffers ,buffers ,max_message_length , 180message_length ,message ); 181 } 182#endif 183return dspqueue_read_pfn (queue ,flags ,max_buffers ,num_buffers ,buffers ,max_message_length ,message_length , 184message ,timeout_us ); 185} 186 187HTPDRV_API int remote_handle64_open (const char * name ,remote_handle64 * ph ) { 188return remote_handle64_open_pfn (name ,ph ); 189} 190 191HTPDRV_API int remote_handle64_invoke (remote_handle64 h ,uint32_t dwScalars ,remote_arg * pra ) { 192return remote_handle64_invoke_pfn (h ,dwScalars ,pra ); 193} 194 195HTPDRV_API int remote_handle64_close (remote_handle64 h ) { 196return remote_handle64_close_pfn (h ); 197} 198 199HTPDRV_API int remote_handle_control (uint32_t req ,void * data ,uint32_t datalen ) { 200return remote_handle_control_pfn (req ,data ,datalen ); 201} 202 203HTPDRV_API int remote_handle64_control (remote_handle64 h ,uint32_t req ,void * data ,uint32_t datalen ) { 204return remote_handle64_control_pfn (h ,req ,data ,datalen ); 205} 206 207HTPDRV_API int remote_session_control (uint32_t req ,void * data ,uint32_t datalen ) { 208return remote_session_control_pfn (req ,data ,datalen ); 209} 210 211HTPDRV_API int remote_system_request (system_req_payload * req ) { 212if (!remote_system_request_pfn ) { 213return AEE_EUNSUPPORTEDAPI ; 214 } 215return remote_system_request_pfn (req ); 216} 217 218#ifdef _WIN32 219 220static std::string wstr_to_str (std::wstring_view wstr ) { 221 std::string result ; 222if (wstr .empty ()) { 223return result ; 224 } 225auto bytes_needed = WideCharToMultiByte (CP_UTF8 ,WC_ERR_INVALID_CHARS , 226wstr .data (), (int )wstr .size (), 227nullptr ,0 ,nullptr ,nullptr ); 228if (bytes_needed == 0 ) { 229GGML_LOG_ERROR ("ggml-hex: WideCharToMultiByte failed. Error %lu\n" ,GetLastError ()); 230throw std::runtime_error ("Invalid wstring input" ); 231 } 232 233result .resize (bytes_needed ,'\0' ); 234int bytes_written = WideCharToMultiByte (CP_UTF8 ,WC_ERR_INVALID_CHARS , 235wstr .data (), (int )wstr .size (), 236result .data (),bytes_needed , 237nullptr ,nullptr ); 238if (bytes_written == 0 ) { 239GGML_LOG_ERROR ("ggml-hex: WideCharToMultiByte failed. Error %lu\n" ,GetLastError ()); 240throw std::runtime_error ("Wstring conversion failed" ); 241 } 242return result ; 243} 244 245static std::string get_driver_path () { 246 std::wstring serviceName = L"qcnspmcdm" ; 247 std::string result ; 248 249// Get a handle to the SCM database. 250SC_HANDLE schSCManager = OpenSCManagerW (NULL ,NULL ,STANDARD_RIGHTS_READ ); 251if (nullptr == schSCManager ) { 252GGML_LOG_ERROR ("ggml-hex: Failed to open SCManager. Error: %lu\n" ,GetLastError ()); 253return result ; 254 } 255 256// Get a handle to the service. 257SC_HANDLE schService = OpenServiceW (schSCManager ,// SCM database 258serviceName .c_str (),// name of service 259SERVICE_QUERY_CONFIG );// need query config access 260 261if (nullptr == schService ) { 262GGML_LOG_ERROR ("ggml-hex: Failed to open qcnspmcdm service. Error: %lu\n" ,GetLastError ()); 263CloseServiceHandle (schSCManager ); 264return result ; 265 } 266 267// Store the size of buffer used as an output. 268DWORD bufferSize ; 269if (!QueryServiceConfigW (schService ,NULL ,0 ,& bufferSize )&& 270 (GetLastError ()!= ERROR_INSUFFICIENT_BUFFER )) { 271GGML_LOG_ERROR ("ggml-hex: Failed to query service config. Error: %lu\n" ,GetLastError ()); 272CloseServiceHandle (schService ); 273CloseServiceHandle (schSCManager ); 274return result ; 275 } 276// Get the configuration of the service. 277LPQUERY_SERVICE_CONFIGW serviceConfig = 278static_cast < LPQUERY_SERVICE_CONFIGW > (LocalAlloc (LMEM_FIXED ,bufferSize )); 279if (!QueryServiceConfigW (schService ,serviceConfig ,bufferSize ,& bufferSize )) { 280fprintf (stderr ,"ggml-hex: Failed to query service config. Error: %lu\n" ,GetLastError ()); 281LocalFree (serviceConfig ); 282CloseServiceHandle (schService ); 283CloseServiceHandle (schSCManager ); 284return result ; 285 } 286 287// Read the driver file path get its parent directory 288 std::wstring driverPath = std::wstring (serviceConfig -> lpBinaryPathName ); 289driverPath = driverPath .substr (0 ,driverPath .find_last_of (L"\\" )); 290 291// Clean up resources 292LocalFree (serviceConfig ); 293CloseServiceHandle (schService ); 294CloseServiceHandle (schSCManager ); 295 296// Driver path would contain invalid path string, like: 297// \SystemRoot\System32\DriverStore\FileRepository\qcadsprpc8280.inf_arm64_c2b9460c9a072f37 298// "\SystemRoot" should be replace with a correct one (e.g. C:\Windows) 299const std::wstring systemRootPlaceholder = L"\\SystemRoot" ; 300if (0 != driverPath .compare (0 ,systemRootPlaceholder .length (),systemRootPlaceholder )) { 301GGML_LOG_ERROR ("ggml-hex: String pattern not found in driver path.\n" ); 302return result ; 303 } 304 305// Replace \SystemRoot with an absolute path from system ENV windir 306const std::wstring systemRootEnv = L"windir" ; 307 308// Query the number of wide characters this variable requires 309DWORD numWords = GetEnvironmentVariableW (systemRootEnv .c_str (),NULL ,0 ); 310if (numWords == 0 ) { 311GGML_LOG_ERROR ("ggml-hex: Failed get systemRoot environment variable\n" ); 312return result ; 313 } 314 315// Query the actual system root name from environment variable 316 std::vector < wchar_t > systemRoot (numWords + 1 ); 317numWords = GetEnvironmentVariableW (systemRootEnv .c_str (),systemRoot .data (),numWords + 1 ); 318if (numWords == 0 ) { 319GGML_LOG_ERROR ("ggml-hex: Failed to read windir environment variable\n" ); 320return result ; 321 } 322driverPath .replace (0 ,systemRootPlaceholder .length (), std::wstring (systemRoot .data ())); 323 324return wstr_to_str (driverPath ); 325} 326 327#endif 328 329using dl_handle_ptr = std::unique_ptr < dl_handle ,dl_handle_deleter > ; 330 331int htpdrv_init () { 332static dl_handle_ptr lib_cdsp_rpc_handle = nullptr ; 333static bool initialized = false; 334#ifdef _WIN32 335 std::string drv_path = get_driver_path ()+ "\\" + "libcdsprpc.dll" ; 336#else 337 std::string drv_path = "libcdsprpc.so" ; 338#endif 339if (initialized ) { 340GGML_LOG_INFO ("ggml-hex: Driver already loaded\n" ); 341return AEE_SUCCESS ; 342 } 343GGML_LOG_INFO ("ggml-hex: Loading driver %s\n" ,drv_path .c_str ()); 344 345 fs::path path {drv_path .c_str () }; 346dl_handle_ptr handle {dl_load_library (path ) }; 347if (!handle ) { 348GGML_LOG_ERROR ("ggml-hex: failed to load %s: %s\n" ,path .u8string ().c_str (),dl_error ()); 349return AEE_EUNABLETOLOAD ; 350 } 351 352#define dlsym (drv ,type ,pfn ,symbol ,ignore ) \ 353 do { \ 354 pfn = (type) dl_get_sym(drv, #symbol); \ 355 if (!ignore && nullptr == pfn) { \ 356 GGML_LOG_ERROR("ggml-hex: failed to dlsym %s\n", #symbol); \ 357 return AEE_EUNABLETOLOAD; \ 358 } \ 359 } while (0) 360 361dlsym (handle .get (),rpcmem_alloc_pfn_t ,rpcmem_alloc_pfn ,rpcmem_alloc , false); 362dlsym (handle .get (),rpcmem_alloc2_pfn_t ,rpcmem_alloc2_pfn ,rpcmem_alloc2 , true); 363dlsym (handle .get (),rpcmem_free_pfn_t ,rpcmem_free_pfn ,rpcmem_free , false); 364dlsym (handle .get (),rpcmem_to_fd_pfn_t ,rpcmem_to_fd_pfn ,rpcmem_to_fd , false); 365dlsym (handle .get (),fastrpc_mmap_pfn_t ,fastrpc_mmap_pfn ,fastrpc_mmap , false); 366dlsym (handle .get (),fastrpc_munmap_pfn_t ,fastrpc_munmap_pfn ,fastrpc_munmap , false); 367dlsym (handle .get (),dspqueue_create_pfn_t ,dspqueue_create_pfn ,dspqueue_create , false); 368dlsym (handle .get (),dspqueue_close_pfn_t ,dspqueue_close_pfn ,dspqueue_close , false); 369dlsym (handle .get (),dspqueue_export_pfn_t ,dspqueue_export_pfn ,dspqueue_export , false); 370dlsym (handle .get (),dspqueue_write_pfn_t ,dspqueue_write_pfn ,dspqueue_write , false); 371dlsym (handle .get (),dspqueue_read_pfn_t ,dspqueue_read_pfn ,dspqueue_read , false); 372dlsym (handle .get (),dspqueue_read_noblock_pfn_t ,dspqueue_read_noblock_pfn ,dspqueue_read_noblock , false); 373dlsym (handle .get (),remote_handle64_open_pfn_t ,remote_handle64_open_pfn ,remote_handle64_open , false); 374dlsym (handle .get (),remote_handle64_invoke_pfn_t ,remote_handle64_invoke_pfn ,remote_handle64_invoke , false); 375dlsym (handle .get (),remote_handle_control_pfn_t ,remote_handle_control_pfn ,remote_handle_control , false); 376dlsym (handle .get (),remote_handle64_control_pfn_t ,remote_handle64_control_pfn ,remote_handle64_control , false); 377dlsym (handle .get (),remote_session_control_pfn_t ,remote_session_control_pfn ,remote_session_control , false); 378dlsym (handle .get (),remote_handle64_close_pfn_t ,remote_handle64_close_pfn ,remote_handle64_close , false); 379dlsym (handle .get (),remote_system_request_pfn_t ,remote_system_request_pfn ,remote_system_request , true); 380 381lib_cdsp_rpc_handle = std::move (handle ); 382initialized = true; 383 384return AEE_SUCCESS ; 385} 386 387domain * htpdrv_get_domain (int domain_id ) { 388int i = 0 ; 389int size = sizeof (supported_domains ) /sizeof (domain ); 390 391for (i = 0 ;i < size ;i ++ ) { 392if (supported_domains [i ].id == domain_id ) { 393return & supported_domains [i ]; 394 } 395 } 396 397return NULL ; 398} 399 400int htpdrv_get_arch (int domain ,int * arch ) { 401if (!remote_handle_control_pfn ) { 402GGML_LOG_ERROR ("ggml-hex: remote_handle_control is not supported on this device\n" ); 403return AEE_EUNSUPPORTEDAPI ; 404 } 405 406struct remote_dsp_capability arch_ver ; 407arch_ver .domain = (uint32_t )domain ; 408arch_ver .attribute_ID = ARCH_VER ; 409arch_ver .capability = (uint32_t )0 ; 410 411int err = remote_handle_control (DSPRPC_GET_DSP_INFO ,& arch_ver ,sizeof (arch_ver )); 412if ((err & 0xff )== (AEE_EUNSUPPORTEDAPI & 0xff )) { 413GGML_LOG_ERROR ("ggml-hex: FastRPC capability API is not supported on this device\n" ); 414return AEE_EUNSUPPORTEDAPI ; 415 } 416 417if (err != AEE_SUCCESS ) { 418GGML_LOG_ERROR ("ggml-hex: FastRPC capability query failed (err %d)\n" ,err ); 419return err ; 420 } 421 422uint32_t val = arch_ver .capability & 0xff ; 423* arch = (int ) ((val >>4 )* 10 + (val & 0x0f )); 424return 0 ; 425}