char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
153d324bc
char/aliceai-t5-moe
1#include "ggml-remoting.h" 2 3#include <mutex> 4 5static const char * ggml_backend_remoting_device_get_name (ggml_backend_dev_t dev ) { 6virtgpu * gpu = DEV_TO_GPU (dev ); 7 8// Return the prefixed name that was built once during initialization 9return gpu -> cached_device_info .name ; 10} 11 12static const char * ggml_backend_remoting_device_get_description (ggml_backend_dev_t dev ) { 13virtgpu * gpu = DEV_TO_GPU (dev ); 14 15// Return the pre-cached description from the virtgpu structure 16return gpu -> cached_device_info .description ; 17} 18 19static enum ggml_backend_dev_type ggml_backend_remoting_device_get_type (ggml_backend_dev_t dev ) { 20virtgpu * gpu = DEV_TO_GPU (dev ); 21 22return (enum ggml_backend_dev_type )gpu -> cached_device_info .type ; 23} 24 25static void ggml_backend_remoting_device_get_memory (ggml_backend_dev_t dev ,size_t * free ,size_t * total ) { 26virtgpu * gpu = DEV_TO_GPU (dev ); 27 28* free = gpu -> cached_device_info .memory_free ; 29* total = gpu -> cached_device_info .memory_total ; 30} 31 32static bool ggml_backend_remoting_device_supports_op (ggml_backend_dev_t dev ,const ggml_tensor * op ) { 33#if USE_ALWAYS_TRUE_SUPPORTS_OP == 1 34/* ggml-rpc cheats it like this */ 35/* with the current implementation of serialize_tensor, the src/view aren't properly passed */ 36UNUSED (dev ); 37UNUSED (op ); 38 39return true; 40#else 41virtgpu * gpu = DEV_TO_GPU (dev ); 42 43return apir_device_supports_op (gpu ,op ); 44#endif 45} 46 47static bool ggml_backend_remoting_device_supports_buft (ggml_backend_dev_t dev ,ggml_backend_buffer_type_t buft ) { 48bool supported = buft -> device == dev ; 49 50return supported ; 51} 52 53static bool ggml_backend_remoting_device_offload_op (ggml_backend_dev_t dev ,const ggml_tensor * op ) { 54UNUSED (dev ); 55UNUSED (op ); 56 57return false; 58} 59 60static void ggml_backend_remoting_device_get_props (ggml_backend_dev_t dev ,ggml_backend_dev_props * props ) { 61props -> name = ggml_backend_remoting_device_get_name (dev ); 62props -> description = ggml_backend_remoting_device_get_description (dev ); 63props -> type = ggml_backend_remoting_device_get_type (dev ); 64ggml_backend_remoting_device_get_memory (dev ,& props -> memory_free ,& props -> memory_total ); 65 66virtgpu * gpu = DEV_TO_GPU (dev ); 67apir_device_get_props (gpu ,& props -> caps .async ,& props -> caps .host_buffer ,& props -> caps .buffer_from_host_ptr , 68& props -> caps .events ,& props -> caps .mmap_support ); 69 70props -> caps .buffer_from_host_ptr = false; 71props -> caps .async = false; 72props -> caps .events = false; 73} 74 75ggml_backend_buffer_type_t ggml_backend_remoting_device_get_buffer_type (ggml_backend_dev_t dev ) { 76virtgpu * gpu = DEV_TO_GPU (dev ); 77 78static std::atomic < bool > initialized = false; 79static ggml_backend_buffer_type buft ; 80 81if (!initialized ) { 82static std::mutex mutex ; 83 std::lock_guard < std::mutex > lock (mutex ); 84 85if (!initialized ) { 86buft = { 87/* .iface = */ ggml_backend_remoting_buffer_type_interface , 88/* .device = */ dev , 89/* .context = */ (void * )gpu -> cached_buffer_type .host_handle , 90 }; 91initialized = true; 92 } 93 } 94 95return & buft ; 96} 97 98static ggml_backend_buffer_type_t ggml_backend_remoting_device_get_buffer_from_ptr_type (ggml_backend_dev_t dev ) { 99virtgpu * gpu = DEV_TO_GPU (dev ); 100 101static std::atomic < bool > initialized = false; 102static ggml_backend_buffer_type buft ; 103 104if (!initialized ) { 105static std::mutex mutex ; 106 std::lock_guard < std::mutex > lock (mutex ); 107 108if (!initialized ) { 109buft = { 110/* .iface = */ ggml_backend_remoting_buffer_from_ptr_type_interface , 111/* .device = */ dev , 112/* .context = */ (void * )gpu -> cached_buffer_type .host_handle , 113 }; 114initialized = true; 115 } 116 } 117 118return & buft ; 119} 120 121static ggml_backend_buffer_t ggml_backend_remoting_device_buffer_from_ptr (ggml_backend_dev_t dev , 122void * ptr , 123size_t size , 124size_t max_tensor_size ) { 125virtgpu * gpu = DEV_TO_GPU (dev ); 126 127ggml_backend_remoting_buffer_context * context = (ggml_backend_remoting_buffer_context * )malloc (sizeof (* context )); 128if (!context ) { 129GGML_ABORT (GGML_VIRTGPU "%s: Couldn't allocate the buffer context ..." ,__func__ ); 130 } 131 132context -> gpu = gpu ; 133context -> apir_context = apir_device_buffer_from_ptr (gpu ,size ,max_tensor_size ); 134context -> base = ptr ; 135context -> is_from_ptr = true; 136 137ggml_backend_buffer_t buffer = 138ggml_backend_buffer_init (ggml_backend_remoting_device_get_buffer_from_ptr_type (dev ), 139ggml_backend_remoting_buffer_from_ptr_interface , (void * )context ,size ); 140 141return buffer ; 142} 143 144const ggml_backend_device_i ggml_backend_remoting_device_interface = { 145/* .get_name = */ ggml_backend_remoting_device_get_name , 146/* .get_description = */ ggml_backend_remoting_device_get_description , 147/* .get_memory = */ ggml_backend_remoting_device_get_memory , 148/* .get_type = */ ggml_backend_remoting_device_get_type , 149/* .get_props = */ ggml_backend_remoting_device_get_props , 150/* .init_backend = */ ggml_backend_remoting_device_init , 151/* .get_buffer_type = */ ggml_backend_remoting_device_get_buffer_type , 152/* .get_host_buffer_type = */ NULL , 153/* .buffer_from_host_ptr = */ ggml_backend_remoting_device_buffer_from_ptr , 154/* .supports_op = */ ggml_backend_remoting_device_supports_op , 155/* .supports_buft = */ ggml_backend_remoting_device_supports_buft , 156/* .offload_op = */ ggml_backend_remoting_device_offload_op , 157/* .event_new = */ NULL , 158/* .event_free = */ NULL , 159/* .event_synchronize = */ NULL , 160};