Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-virtgpu/backend/shared/apir_cs_ggml.h # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-virtgpu / backend / shared / apir_cs_ggml.h` 232 lines ยท 8506 bytes Raw Blame History 1 #include "apir_cs.h" 2 #include "apir_cs_rpc.h" 3 #include "ggml-impl.h" 4 5 // ggml_buffer_to_apir_host_handle(ggml_backend_buffer_t buffer); 6 7 static inline void apir_encode_ggml_buffer_host_handle(apir_encoder * enc, const apir_buffer_host_handle_t * handle); 8 9 static inline ggml_backend_buffer_t apir_decode_ggml_buffer(apir_decoder * dec); 10 11 /* apir_rpc_tensor */ 12 13 static inline void apir_encode_rcp_tensor(apir_encoder * enc, const apir_rpc_tensor * apir_rpc_tensor) { 14 size_t apir_rpc_tensor_size = sizeof(*apir_rpc_tensor); 15 apir_encode(enc, apir_rpc_tensor_size, apir_rpc_tensor, apir_rpc_tensor_size); 16 } 17 18 static inline apir_rpc_tensor * apir_decode_apir_rpc_tensor_inplace(apir_decoder * dec) { 19 size_t apir_rpc_tensor_size = sizeof(apir_rpc_tensor); 20 21 return (apir_rpc_tensor *) (uintptr_t) apir_decoder_use_inplace(dec, apir_rpc_tensor_size); 22 } 23 24 static inline apir_rpc_tensor * apir_decode_apir_rpc_tensor_array_inplace(apir_decoder * dec, uint32_t n_tensors) { 25 size_t apir_rpc_tensor_size = sizeof(apir_rpc_tensor) * n_tensors; 26 27 return (apir_rpc_tensor *) (uintptr_t) apir_decoder_use_inplace(dec, apir_rpc_tensor_size); 28 } 29 30 /* ggml_tensor */ 31 32 static inline void apir_encode_ggml_tensor(apir_encoder * enc, const ggml_tensor * tensor) { 33 apir_rpc_tensor serialized = apir_serialize_tensor(tensor); 34 35 apir_encode_rcp_tensor(enc, &serialized); 36 } 37 38 static inline const ggml_tensor * apir_decode_ggml_tensor(apir_decoder * dec) { 39 const apir_rpc_tensor * apir_rpc_tensor = apir_decode_apir_rpc_tensor_inplace(dec); 40 41 if (!apir_rpc_tensor) { 42 return NULL; 43 } 44 45 ggml_init_params params{ 46 /*.mem_size =*/ggml_tensor_overhead(), 47 /*.mem_buffer =*/NULL, 48 /*.no_alloc =*/true, 49 }; 50 51 ggml_context * ctx = ggml_init(params); 52 53 const ggml_tensor * tensor = apir_deserialize_tensor(ctx, apir_rpc_tensor); 54 55 return tensor; 56 } 57 58 /* *** ggml_backend_buffer_type_t *** */ 59 60 // ggml_backend_buffer_type_t is a POINTER (to a struct). 61 // Only the host pointer is shared between the host and guest. 62 // The guest stores it in `buft->context`. 63 // The host simply writes the pointer address in the buffer variable. 64 65 static inline void apir_encode_ggml_buffer_type(apir_encoder * enc, ggml_backend_buffer_type_t buft) { 66 apir_buffer_type_host_handle_t handle = ggml_buffer_type_to_apir_handle(buft); 67 apir_encoder_write(enc, sizeof(handle), &handle, sizeof(handle)); 68 } 69 70 static inline ggml_backend_buffer_type_t apir_decode_ggml_buffer_type(apir_decoder * dec) { 71 apir_buffer_type_host_handle_t handle; 72 73 apir_decoder_read(dec, sizeof(handle), &handle, sizeof(handle)); 74 75 return (ggml_backend_buffer_type_t) handle; 76 } 77 78 static inline void apir_encode_apir_buffer_type_host_handle(apir_encoder * enc, apir_buffer_type_host_handle_t handle) { 79 apir_encoder_write(enc, sizeof(handle), &handle, sizeof(handle)); 80 } 81 82 static inline apir_buffer_type_host_handle_t apir_decode_apir_buffer_type_host_handle(apir_decoder * dec) { 83 apir_buffer_type_host_handle_t handle; 84 85 apir_decoder_read(dec, sizeof(handle), &handle, sizeof(handle)); 86 87 return handle; 88 } 89 90 /* *** ggml_backend_type_t *** */ 91 92 // ggml_backend_buffer_t is a POINTER. 93 // same logic as for ggml_backend_buffer_type_t 94 95 static inline void apir_encode_ggml_buffer(apir_encoder * enc, const ggml_backend_buffer_t buffer) { 96 apir_buffer_host_handle_t handle = BUFFER_TO_HOST_HANDLE(buffer); 97 apir_encoder_write(enc, sizeof(handle), &handle, sizeof(handle)); 98 } 99 100 static inline ggml_backend_buffer_t apir_decode_ggml_buffer(apir_decoder * dec) { 101 ggml_backend_buffer_t buffer; 102 size_t buffer_ptr_size = sizeof(buffer); 103 104 apir_decoder_read(dec, buffer_ptr_size, &buffer, buffer_ptr_size); 105 106 // SECURITY: Validate buffer handle against tracked buffers to prevent 107 // guest VM from providing arbitrary host memory addresses 108 if (buffer) { 109 extern std::unordered_set backend_buffers; 110 if (backend_buffers.find(buffer) == backend_buffers.end()) { 111 GGML_LOG_WARN("ggml-virtgpu-backend: %s: Invalid buffer handle from guest: %p\n", __func__, 112 (void *) buffer); 113 // Set fatal flag to prevent further processing with invalid handle 114 apir_decoder_set_fatal(dec); 115 return NULL; 116 } 117 } 118 119 return buffer; 120 } 121 122 /* enum ggml_status */ 123 124 static inline void apir_encode_ggml_status(apir_encoder * enc, const ggml_status * status) { 125 apir_encoder_write(enc, sizeof(*status), status, sizeof(*status)); 126 } 127 128 static inline void apir_decode_ggml_status(apir_decoder * dec, ggml_status * status) { 129 apir_decoder_read(dec, sizeof(*status), status, sizeof(*status)); 130 } 131 132 /* virtgpu_shmem */ 133 134 static inline void apir_encode_virtgpu_shmem_res_id(apir_encoder * enc, uint32_t shmem_res_id) { 135 apir_encode_uint32_t(enc, &shmem_res_id); 136 } 137 138 static inline void apir_decode_virtgpu_shmem_res_id(apir_decoder * dec, uint32_t * shmem_res_id) { 139 apir_decode_uint32_t(dec, shmem_res_id); 140 } 141 142 /* ggml_cgraph */ 143 144 static inline size_t apir_serialize_ggml_cgraph(ggml_cgraph * cgraph, std::vector & cgraph_data) { 145 apir_serialize_graph(cgraph, cgraph_data); 146 147 return cgraph_data.size(); 148 } 149 150 static inline void apir_encode_cgraph_data(apir_encoder * enc, std::vector & cgraph_data) { 151 size_t cgraph_size = cgraph_data.size(); 152 153 apir_encode(enc, cgraph_size, cgraph_data.data(), cgraph_size); 154 } 155 156 static inline ggml_cgraph * apir_decode_ggml_cgraph(apir_decoder * dec, size_t cgraph_size) { 157 GGML_UNUSED(cgraph_size); 158 159 uint32_t n_nodes; 160 apir_decode_uint32_t(dec, &n_nodes); 161 const uint64_t * nodes = apir_decode_uint64_t_array_inplace(dec, n_nodes); 162 163 uint32_t n_tensors; 164 apir_decode_uint32_t(dec, &n_tensors); 165 const apir_rpc_tensor * tensors = apir_decode_apir_rpc_tensor_array_inplace(dec, n_tensors); 166 167 return apir_deserialize_graph(n_nodes, n_tensors, tensors, nodes); 168 } 169 170 static inline void apir_encode_ggml_buffer_handle(apir_encoder * enc, const apir_buffer_host_handle_t * handle) { 171 apir_encoder_write(enc, sizeof(*handle), &handle, sizeof(*handle)); 172 } 173 174 static inline void apir_encode_ggml_tensor_inline(apir_encoder * enc, const ggml_tensor * tensor) { 175 size_t tensor_size = sizeof(*tensor); 176 177 if (tensor->extra) { 178 GGML_ABORT("%s: Cannot pass tensors with extra", __func__); 179 } 180 181 if (tensor->src[0] && tensor->buffer) { 182 static int first = 1; 183 if (first) { 184 GGML_LOG_WARN("%s: Cannot pass tensors with src and buffer\n", __func__); 185 first = 0; 186 } 187 } 188 189 apir_encoder_write(enc, tensor_size, tensor, tensor_size); 190 191 // tensor->data is a pointer inside the device buffer. No need to touch it 192 // tensor->buffer is a pointer to a buffer. Encoding the buffer handle in sequence. 193 // (could also make a copy of the tensor, and update locally.) 194 195 if (tensor->buffer) { 196 apir_buffer_host_handle_t buffer_handle = ggml_buffer_to_apir_handle(tensor->buffer); 197 apir_encode_ggml_buffer_handle(enc, &buffer_handle); 198 } 199 200 if (tensor->view_src) { 201 apir_encoder_write(enc, tensor_size, tensor->view_src, tensor_size); 202 } 203 204 for (int i = 0; tensor->src[i]; i++) { 205 const ggml_tensor * tensor_src = tensor->src[i]; 206 apir_encoder_write(enc, tensor_size, tensor_src, tensor_size); 207 } 208 } 209 210 static inline const ggml_tensor * apir_decode_ggml_tensor_inplace(apir_decoder * dec) { 211 // it safe to remove the `const` qualifier here, we *do* want to 212 // modify the shared memory data to fix the `src` pointers. 213 ggml_tensor * tensor = (ggml_tensor *) (uintptr_t) apir_decoder_use_inplace(dec, sizeof(ggml_tensor)); 214 215 // tensor->data is a pointer inside the device buffer. No need to touch it 216 // tensor->buffer is a pointer to a buffer. Decode the buffer handle encoded in sequence. 217 if (tensor->buffer) { 218 tensor->buffer = apir_decode_ggml_buffer(dec); 219 } 220 221 if (tensor->view_src) { 222 ggml_tensor * tensor_view_src = (ggml_tensor *) (uintptr_t) apir_decoder_use_inplace(dec, sizeof(ggml_tensor)); 223 tensor->view_src = tensor_view_src; 224 } 225 226 for (int i = 0; tensor->src[i]; i++) { 227 ggml_tensor * tensor_src = (ggml_tensor *) (uintptr_t) apir_decoder_use_inplace(dec, sizeof(ggml_tensor)); 228 tensor->src[i] = tensor_src; // overwrite op->src[i] pointer with the actual location of the src tensor 229 } 230 231 return tensor; 232 }