char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
ffaafde16
char/aliceai-t5-moe
1#include "backend-dispatched.h" 2#include "backend-virgl-apir.h" 3#include "ggml-backend-impl.h" 4#include "ggml-backend.h" 5#include "ggml-impl.h" 6#include "shared/apir_backend.h" 7 8#include <cstdint> 9 10static uint32_t validate_graph_operation (size_t cgraph_size ,uint32_t shmem_res_id ,const char * operation ) { 11if (cgraph_size == 0 ) { 12GGML_LOG_ERROR (GGML_VIRTGPU_BCK "%s: Zero-size computation graph\n" ,operation ); 13return 1 ; 14 } 15 16// place-holder: validate that the size of shmem_res_id is <= cgraph_size 17// need to add another method in the Virgl->APIR callback interface 18GGML_UNUSED (shmem_res_id ); 19 20return 0 ;// Valid 21} 22 23uint32_t backend_backend_graph_compute (apir_encoder * enc ,apir_decoder * dec ,virgl_apir_context * ctx ) { 24GGML_UNUSED (ctx ); 25 26static bool async_backend_initialized = false; 27static bool async_backend ; 28 29if (!async_backend_initialized ) { 30ggml_backend_dev_props props ; 31 32dev -> iface .get_props (dev ,& props ); 33async_backend = props .caps .async ; 34async_backend_initialized = true; 35 } 36 37uint32_t shmem_res_id ; 38apir_decode_virtgpu_shmem_res_id (dec ,& shmem_res_id ); 39 40const void * shmem_data = ctx -> iface -> get_shmem_ptr (ctx -> ctx_id ,shmem_res_id ); 41if (!shmem_data ) { 42GGML_LOG_ERROR (GGML_VIRTGPU_BCK "%s: Couldn't get the shmem addr from virgl\n" ,__func__ ); 43apir_decoder_set_fatal (dec ); 44return 1 ; 45 } 46size_t cgraph_size ; 47apir_decode_size_t (dec ,& cgraph_size ); 48 49if (validate_graph_operation (cgraph_size ,shmem_res_id ,__func__ )!= 0 ) { 50apir_decoder_set_fatal (dec ); 51return 1 ; 52 } 53 54apir_decoder secondary_dec = apir_new_decoder ((const char * )shmem_data ,cgraph_size ); 55 56ggml_cgraph * cgraph = apir_decode_ggml_cgraph (& secondary_dec ,cgraph_size ); 57 58if (!cgraph || apir_decoder_get_fatal (& secondary_dec )) { 59GGML_LOG_ERROR (GGML_VIRTGPU_BCK "%s: Failed to deserialize computation graph\n" ,__func__ ); 60return 1 ; 61 } 62 63if (cgraph -> n_nodes < 0 || cgraph -> n_leafs < 0 ) { 64GGML_LOG_ERROR (GGML_VIRTGPU_BCK "%s: Invalid negative node/leaf count: nodes=%d leafs=%d\n" ,__func__ , 65cgraph -> n_nodes ,cgraph -> n_leafs ); 66return 1 ; 67 } 68 69ggml_status status ; 70#if APIR_BACKEND_CHECK_SUPPORTS_OP == 1 71for (int idx = 0 ;idx < cgraph -> n_nodes ;idx ++ ) { 72ggml_tensor * op = ggml_graph_node (cgraph ,idx ); 73if (dev -> iface .supports_op (dev ,op )) { 74continue ; 75 } 76GGML_LOG_ERROR (GGML_VIRTGPU_BCK "%s: Graph node %d (%s) not supported by the backend\n" ,__func__ ,idx , 77ggml_op_desc (op )); 78 79status = GGML_STATUS_ABORTED ; 80apir_encode_ggml_status (enc ,& status ); 81 82return 0 ; 83 } 84#endif 85 86// Check if backend is properly initialized 87if (!bck ) { 88GGML_LOG_ERROR (GGML_VIRTGPU_BCK "%s: Backend not initialized (bck is null)\n" ,__func__ ); 89 90return 1 ; 91 } 92 93status = bck -> iface .graph_compute (bck ,cgraph ); 94 95if (async_backend && bck -> iface .synchronize ) { 96bck -> iface .synchronize (bck ); 97 } 98 99apir_encode_ggml_status (enc ,& status ); 100 101return 0 ; 102}