char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
81b0d882a
char/aliceai-t5-moe
1#ifndef SPINE_TCM_PUBLIC_H_ 2#define SPINE_TCM_PUBLIC_H_ 3 4/* 5* spine_tcm public API 6* 7* Usage: 8* 1. Direct link mode 9* Define SPINE_TCM_DIRECT_LINK and link against libspine_tcm.so. 10* 11* if (spine_tcm_is_available()) { 12* void *buffer = spine_tcm_mem_get(0); 13* spine_tcm_mem_free(0); 14* } 15* 16* 2. Header-only loader mode 17* Include this header without linking libspine_tcm.so. The loader first 18* tries to reuse a process-global spine_tcm instance and falls back to 19* dlopen("libspine_tcm.so") when needed. 20* 21* spine_tcm_open_handle(NULL); // optional pre-bind 22* if (spine_tcm_is_available()) { 23* void *buffer = spine_tcm_mem_get(0); 24* spine_tcm_mem_free(0); 25* } 26*/ 27 28#include <stddef.h> 29#include <stdint.h> 30#include <string.h> 31 32#if !defined(SPINE_TCM_BUILD_SHARED )&& !defined(SPINE_TCM_DIRECT_LINK ) 33# include <dlfcn.h> 34#endif 35 36#ifdef __cplusplus 37extern "C" { 38#endif 39 40#if defined(_WIN32 ) 41# if defined(SPINE_TCM_BUILD_SHARED ) 42# define SPINE_TCM_API __declspec(dllexport) 43# else 44# define SPINE_TCM_API __declspec(dllimport) 45# endif 46#else 47# define SPINE_TCM_API __attribute__((visibility("default"))) 48#endif 49 50typedef struct spine_tcm_mem_info { 51size_t blk_size ; 52size_t blk_num ; 53int is_fake_tcm ; 54}spine_tcm_mem_info_t ; 55 56typedef struct spine_tcm_block_info { 57int id ; 58void * va ; 59size_t size ; 60uint64_t phys_addr ; 61uint64_t cpu_affinity_mask ; 62int owner_tid ; 63int is_acquired ; 64}spine_tcm_block_info_t ; 65 66/* Shared-library runtime ABI exported by libspine_tcm.so. */ 67SPINE_TCM_API const char * spine_tcm_runtime_version (void ); 68SPINE_TCM_API int spine_tcm_runtime_is_available (void ); 69SPINE_TCM_API int spine_tcm_runtime_layout_info (spine_tcm_mem_info_t * info ); 70SPINE_TCM_API int spine_tcm_runtime_mem_info (int id ,spine_tcm_block_info_t * info ); 71SPINE_TCM_API void * spine_tcm_runtime_mem_get (int id ); 72SPINE_TCM_API int spine_tcm_runtime_mem_free (int id ); 73SPINE_TCM_API void * spine_tcm_runtime_mem_try_wait (int id ,size_t timeout_us ); 74SPINE_TCM_API int spine_tcm_runtime_mem_release (int id ); 75SPINE_TCM_API int spine_tcm_runtime_mem_force_release (int id ); 76SPINE_TCM_API int spine_tcm_runtime_mem_query (int id ); 77 78#if defined(SPINE_TCM_DIRECT_LINK ) 79/* Optional no-op in direct-link mode. */ 80static inline int spine_tcm_open_handle (const char * so_path ) { 81 (void )so_path ; 82return 0 ; 83} 84 85static inline const char * spine_tcm_version (void ) { 86return spine_tcm_runtime_version (); 87} 88 89/* Returns 1 when the runtime driver is available, otherwise 0. */ 90static inline int spine_tcm_is_available (void ) { 91return spine_tcm_runtime_is_available (); 92} 93 94/* Returns runtime memory geometry and whether the current backend is fake TCM. */ 95static inline int spine_tcm_mem_info (spine_tcm_mem_info_t * info ) { 96return spine_tcm_runtime_layout_info (info ); 97} 98 99/* Returns per-block runtime metadata for the given TCM id. */ 100static inline int spine_tcm_block_info (int id ,spine_tcm_block_info_t * info ) { 101return spine_tcm_runtime_mem_info (id ,info ); 102} 103 104/* Returns a cached buffer for the given TCM id, or NULL on failure. */ 105static inline void * spine_tcm_mem_get (int id ) { 106return spine_tcm_runtime_mem_get (id ); 107} 108 109/* Releases one reference acquired by spine_tcm_mem_get(id). */ 110static inline int spine_tcm_mem_free (int id ) { 111return spine_tcm_runtime_mem_free (id ); 112} 113 114/* Waits for a TCM block handoff and returns the driver-owned buffer when available. */ 115static inline void * spine_tcm_mem_try_wait (int id ,size_t over_time ) { 116return spine_tcm_runtime_mem_try_wait (id ,over_time ); 117} 118 119/* Releases a buffer acquired by spine_tcm_mem_try_wait(id, over_time). */ 120static inline int spine_tcm_mem_release (int id ) { 121return spine_tcm_runtime_mem_release (id ); 122} 123 124/* Forces a release for the given TCM id when the backend supports it. */ 125static inline int spine_tcm_mem_force_release (int id ) { 126return spine_tcm_runtime_mem_force_release (id ); 127} 128 129/* Returns whether the given TCM id is currently acquired. */ 130static inline int spine_tcm_mem_query (int id ) { 131return spine_tcm_runtime_mem_query (id ); 132} 133#elif !defined(SPINE_TCM_BUILD_SHARED ) 134typedef struct spine_tcm_handle { 135void * module_handle ; 136int use_global_scope ; 137int owns_module_handle ; 138const char * (* runtime_version )(void ); 139int (* runtime_is_available )(void ); 140int (* runtime_layout_info )(spine_tcm_mem_info_t * info ); 141int (* runtime_mem_info )(int id ,spine_tcm_block_info_t * info ); 142void * (* runtime_mem_get )(int id ); 143int (* runtime_mem_free )(int id ); 144void * (* runtime_mem_try_wait )(int id ,size_t over_time ); 145int (* runtime_mem_release )(int id ); 146int (* runtime_mem_force_release )(int id ); 147int (* runtime_mem_query )(int id ); 148}spine_tcm_handle_t ; 149 150static inline spine_tcm_handle_t * spine_tcm_default_handle (void ) { 151static spine_tcm_handle_t handle = {0 }; 152return & handle ; 153} 154 155static inline void spine_tcm_handle_reset (spine_tcm_handle_t * handle ) { 156if (handle != NULL ) { 157memset (handle ,0 ,sizeof (* handle )); 158 } 159} 160 161static inline int spine_tcm_handle_bind (spine_tcm_handle_t * handle ) { 162void * symbol_scope = handle -> use_global_scope ?RTLD_DEFAULT :handle -> module_handle ; 163 164handle -> runtime_version = (const char * (* ) (void ) )dlsym (symbol_scope ,"spine_tcm_runtime_version" ); 165handle -> runtime_is_available = (int (* )(void ))dlsym (symbol_scope ,"spine_tcm_runtime_is_available" ); 166handle -> runtime_layout_info = 167 (int (* )(spine_tcm_mem_info_t * ))dlsym (symbol_scope ,"spine_tcm_runtime_layout_info" ); 168handle -> runtime_mem_info = 169 (int (* )(int ,spine_tcm_block_info_t * ))dlsym (symbol_scope ,"spine_tcm_runtime_mem_info" ); 170handle -> runtime_mem_get = (void * (* ) (int ) )dlsym (symbol_scope ,"spine_tcm_runtime_mem_get" ); 171handle -> runtime_mem_free = (int (* )(int ))dlsym (symbol_scope ,"spine_tcm_runtime_mem_free" ); 172handle -> runtime_mem_try_wait = (void * (* ) (int ,size_t ))dlsym (symbol_scope ,"spine_tcm_runtime_mem_try_wait" ); 173handle -> runtime_mem_release = (int (* )(int ))dlsym (symbol_scope ,"spine_tcm_runtime_mem_release" ); 174handle -> runtime_mem_force_release = (int (* )(int ))dlsym (symbol_scope ,"spine_tcm_runtime_mem_force_release" ); 175handle -> runtime_mem_query = (int (* )(int ))dlsym (symbol_scope ,"spine_tcm_runtime_mem_query" ); 176 177return handle -> runtime_version != NULL && handle -> runtime_is_available != NULL && 178handle -> runtime_layout_info != NULL && handle -> runtime_mem_info != NULL && 179handle -> runtime_mem_get != NULL && handle -> runtime_mem_free != NULL && 180handle -> runtime_mem_try_wait != NULL && handle -> runtime_mem_release != NULL && 181handle -> runtime_mem_force_release != NULL && handle -> runtime_mem_query != NULL ? 1820 : 183-1 ; 184} 185 186/* 187* Try to bind against an already-loaded process-global spine_tcm instance. 188* The shared library exports spine_tcm_runtime_marker only for this probe. 189*/ 190static inline int spine_tcm_try_bind_global (spine_tcm_handle_t * handle ) { 191if (dlsym (RTLD_DEFAULT ,"spine_tcm_runtime_marker" )== NULL ) { 192return -1 ; 193 } 194 195handle -> use_global_scope = 1 ; 196return spine_tcm_handle_bind (handle ); 197} 198 199/* 200* Optional pre-bind entry point. 201* 202* Behavior: 203* - Reuses an already-loaded global spine_tcm instance when available. 204* - Otherwise loads the shared library from so_path or the default soname. 205* - Repeated calls are safe and return 0 after the first successful bind. 206*/ 207static inline int spine_tcm_open_handle (const char * so_path ) { 208spine_tcm_handle_t * resolved = spine_tcm_default_handle (); 209const char * library = (so_path != NULL && so_path [0 ]!= '\0' ) ?so_path :"libspine_tcm.so" ; 210 211if (resolved -> module_handle != NULL || resolved -> use_global_scope ) { 212return 0 ; 213 } 214 215if (spine_tcm_try_bind_global (resolved )== 0 ) { 216return 0 ; 217 } 218 219spine_tcm_handle_reset (resolved ); 220 221resolved -> module_handle = dlopen (library ,RTLD_LAZY |RTLD_GLOBAL ); 222resolved -> owns_module_handle = resolved -> module_handle != NULL ?1 :0 ; 223 224if (resolved -> module_handle == NULL ) { 225spine_tcm_handle_reset (resolved ); 226return -1 ; 227 } 228 229if (spine_tcm_handle_bind (resolved )!= 0 ) { 230if (resolved -> owns_module_handle ) { 231dlclose (resolved -> module_handle ); 232 } 233spine_tcm_handle_reset (resolved ); 234return -1 ; 235 } 236 237return 0 ; 238} 239 240/* Returns 1 when the runtime driver is available, otherwise 0. */ 241static inline int spine_tcm_is_available (void ) { 242spine_tcm_handle_t * resolved = spine_tcm_default_handle (); 243 244if (resolved -> module_handle == NULL && !resolved -> use_global_scope ) { 245 (void )spine_tcm_open_handle (NULL ); 246 } 247 248if ((resolved -> module_handle == NULL && !resolved -> use_global_scope )|| resolved -> runtime_is_available == NULL ) { 249return 0 ; 250 } 251 252return resolved -> runtime_is_available (); 253} 254 255/* Returns runtime memory geometry and whether the current backend is fake TCM. */ 256static inline int spine_tcm_mem_info (spine_tcm_mem_info_t * info ) { 257spine_tcm_handle_t * resolved = spine_tcm_default_handle (); 258 259if (resolved -> module_handle == NULL && !resolved -> use_global_scope ) { 260 (void )spine_tcm_open_handle (NULL ); 261 } 262 263if ((resolved -> module_handle == NULL && !resolved -> use_global_scope )|| resolved -> runtime_layout_info == NULL ) { 264return -1 ; 265 } 266 267return resolved -> runtime_layout_info (info ); 268} 269 270static inline const char * spine_tcm_version (void ) { 271spine_tcm_handle_t * resolved = spine_tcm_default_handle (); 272 273if (resolved -> module_handle == NULL && !resolved -> use_global_scope ) { 274 (void )spine_tcm_open_handle (NULL ); 275 } 276 277if ((resolved -> module_handle == NULL && !resolved -> use_global_scope )|| resolved -> runtime_version == NULL ) { 278return "unknown" ; 279 } 280 281return resolved -> runtime_version (); 282} 283 284/* Returns per-block runtime metadata for the given TCM id. */ 285static inline int spine_tcm_block_info (int id ,spine_tcm_block_info_t * info ) { 286spine_tcm_handle_t * resolved = spine_tcm_default_handle (); 287 288if (resolved -> module_handle == NULL && !resolved -> use_global_scope ) { 289 (void )spine_tcm_open_handle (NULL ); 290 } 291 292if ((resolved -> module_handle == NULL && !resolved -> use_global_scope )|| resolved -> runtime_mem_info == NULL ) { 293return -1 ; 294 } 295 296return resolved -> runtime_mem_info (id ,info ); 297} 298 299/* Returns a cached buffer for the given TCM id, or NULL on failure. */ 300static inline void * spine_tcm_mem_get (int id ) { 301spine_tcm_handle_t * resolved = spine_tcm_default_handle (); 302 303if (resolved -> module_handle == NULL && !resolved -> use_global_scope ) { 304 (void )spine_tcm_open_handle (NULL ); 305 } 306 307if (resolved -> module_handle == NULL && !resolved -> use_global_scope ) { 308return NULL ; 309 } 310 311if (resolved -> runtime_mem_get == NULL ) { 312return NULL ; 313 } 314 315return resolved -> runtime_mem_get (id ); 316} 317 318/* Releases one reference acquired by spine_tcm_mem_get(id). */ 319static inline int spine_tcm_mem_free (int id ) { 320spine_tcm_handle_t * resolved = spine_tcm_default_handle (); 321 322if (resolved -> module_handle == NULL && !resolved -> use_global_scope ) { 323 (void )spine_tcm_open_handle (NULL ); 324 } 325 326if ((resolved -> module_handle == NULL && !resolved -> use_global_scope )|| resolved -> runtime_mem_free == NULL ) { 327return -1 ; 328 } 329 330return resolved -> runtime_mem_free (id ); 331} 332 333/* Waits for a TCM block handoff and returns the driver-owned buffer when available. */ 334static inline void * spine_tcm_mem_try_wait (int id ,size_t over_time ) { 335spine_tcm_handle_t * resolved = spine_tcm_default_handle (); 336 337if (resolved -> module_handle == NULL && !resolved -> use_global_scope ) { 338 (void )spine_tcm_open_handle (NULL ); 339 } 340 341if (resolved -> module_handle == NULL && !resolved -> use_global_scope ) { 342return NULL ; 343 } 344 345if (resolved -> runtime_mem_try_wait == NULL ) { 346return NULL ; 347 } 348 349return resolved -> runtime_mem_try_wait (id ,over_time ); 350} 351 352/* Releases a buffer acquired by spine_tcm_mem_try_wait(id, over_time). */ 353static inline int spine_tcm_mem_release (int id ) { 354spine_tcm_handle_t * resolved = spine_tcm_default_handle (); 355 356if (resolved -> module_handle == NULL && !resolved -> use_global_scope ) { 357 (void )spine_tcm_open_handle (NULL ); 358 } 359 360if ((resolved -> module_handle == NULL && !resolved -> use_global_scope )|| resolved -> runtime_mem_release == NULL ) { 361return -1 ; 362 } 363 364return resolved -> runtime_mem_release (id ); 365} 366 367/* Forces a release for the given TCM id when the backend supports it. */ 368static inline int spine_tcm_mem_force_release (int id ) { 369spine_tcm_handle_t * resolved = spine_tcm_default_handle (); 370 371if (resolved -> module_handle == NULL && !resolved -> use_global_scope ) { 372 (void )spine_tcm_open_handle (NULL ); 373 } 374 375if ((resolved -> module_handle == NULL && !resolved -> use_global_scope )|| 376resolved -> runtime_mem_force_release == NULL ) { 377return -1 ; 378 } 379 380return resolved -> runtime_mem_force_release (id ); 381} 382 383/* Returns whether the given TCM id is currently acquired. */ 384static inline int spine_tcm_mem_query (int id ) { 385spine_tcm_handle_t * resolved = spine_tcm_default_handle (); 386 387if (resolved -> module_handle == NULL && !resolved -> use_global_scope ) { 388 (void )spine_tcm_open_handle (NULL ); 389 } 390 391if ((resolved -> module_handle == NULL && !resolved -> use_global_scope )|| resolved -> runtime_mem_query == NULL ) { 392return -1 ; 393 } 394 395return resolved -> runtime_mem_query (id ); 396} 397#else 398static inline const char * spine_tcm_version (void ) { 399return spine_tcm_runtime_version (); 400} 401#endif 402 403#define SPINE_TCM_VERSION (spine_tcm_version()) 404 405#ifdef __cplusplus 406} 407#endif 408 409#endif