ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-virtgpu / virtgpu-forward-device.cpp
| 1 | #include "virtgpu-forward-impl.h" |
| 2 | #include "virtgpu-shm.h" |
| 3 | |
| 4 | int apir_device_get_count(virtgpu * gpu) { |
| 5 | apir_encoder * encoder; |
| 6 | apir_decoder * decoder; |
| 7 | ApirForwardReturnCode ret; |
| 8 | |
| 9 | REMOTE_CALL_PREPARE(gpu, encoder, APIR_COMMAND_TYPE_DEVICE_GET_COUNT); |
| 10 | REMOTE_CALL(gpu, encoder, decoder, ret); |
| 11 | |
| 12 | int32_t dev_count = -1; |
| 13 | apir_decode_int32_t(decoder, &dev_count); |
| 14 | |
| 15 | remote_call_finish(gpu, encoder, decoder); |
| 16 | |
| 17 | return dev_count; |
| 18 | } |
| 19 | |
| 20 | char * apir_device_get_name(virtgpu * gpu) { |
| 21 | apir_encoder * encoder; |
| 22 | apir_decoder * decoder; |
| 23 | ApirForwardReturnCode ret; |
| 24 | |
| 25 | REMOTE_CALL_PREPARE(gpu, encoder, APIR_COMMAND_TYPE_DEVICE_GET_NAME); |
| 26 | REMOTE_CALL(gpu, encoder, decoder, ret); |
| 27 | |
| 28 | const size_t string_size = apir_decode_array_size_unchecked(decoder); |
| 29 | char * string = (char *) apir_decoder_alloc_array(sizeof(char), string_size); |
| 30 | if (!string) { |
| 31 | GGML_LOG_ERROR(GGML_VIRTGPU "%s: Could not allocate the device name buffer\n", __func__); |
| 32 | return NULL; |
| 33 | } |
| 34 | apir_decode_char_array(decoder, string, string_size); |
| 35 | |
| 36 | remote_call_finish(gpu, encoder, decoder); |
| 37 | |
| 38 | return string; |
| 39 | } |
| 40 | |
| 41 | char * apir_device_get_description(virtgpu * gpu) { |
| 42 | apir_encoder * encoder; |
| 43 | apir_decoder * decoder; |
| 44 | ApirForwardReturnCode ret; |
| 45 | |
| 46 | REMOTE_CALL_PREPARE(gpu, encoder, APIR_COMMAND_TYPE_DEVICE_GET_DESCRIPTION); |
| 47 | |
| 48 | REMOTE_CALL(gpu, encoder, decoder, ret); |
| 49 | |
| 50 | const size_t string_size = apir_decode_array_size_unchecked(decoder); |
| 51 | char * string = (char *) apir_decoder_alloc_array(sizeof(char), string_size); |
| 52 | if (!string) { |
| 53 | GGML_LOG_ERROR(GGML_VIRTGPU "%s: Could not allocate the device description buffer\n", __func__); |
| 54 | |
| 55 | return NULL; |
| 56 | } |
| 57 | apir_decode_char_array(decoder, string, string_size); |
| 58 | |
| 59 | remote_call_finish(gpu, encoder, decoder); |
| 60 | |
| 61 | return string; |
| 62 | } |
| 63 | |
| 64 | uint32_t apir_device_get_type(virtgpu * gpu) { |
| 65 | static uint32_t dev_type = 255; |
| 66 | if (dev_type != 255) { |
| 67 | return dev_type; |
| 68 | } |
| 69 | |
| 70 | apir_encoder * encoder; |
| 71 | apir_decoder * decoder; |
| 72 | ApirForwardReturnCode ret; |
| 73 | |
| 74 | REMOTE_CALL_PREPARE(gpu, encoder, APIR_COMMAND_TYPE_DEVICE_GET_TYPE); |
| 75 | |
| 76 | REMOTE_CALL(gpu, encoder, decoder, ret); |
| 77 | |
| 78 | apir_decode_uint32_t(decoder, &dev_type); |
| 79 | |
| 80 | remote_call_finish(gpu, encoder, decoder); |
| 81 | |
| 82 | return dev_type; |
| 83 | } |
| 84 | |
| 85 | void apir_device_get_memory(virtgpu * gpu, size_t * free, size_t * total) { |
| 86 | static size_t dev_free = 0; |
| 87 | static size_t dev_total = 0; |
| 88 | apir_encoder * encoder; |
| 89 | apir_decoder * decoder; |
| 90 | ApirForwardReturnCode ret; |
| 91 | |
| 92 | REMOTE_CALL_PREPARE(gpu, encoder, APIR_COMMAND_TYPE_DEVICE_GET_MEMORY); |
| 93 | |
| 94 | REMOTE_CALL(gpu, encoder, decoder, ret); |
| 95 | |
| 96 | apir_decode_size_t(decoder, &dev_free); |
| 97 | apir_decode_size_t(decoder, &dev_total); |
| 98 | |
| 99 | *free = dev_free; |
| 100 | *total = dev_total; |
| 101 | |
| 102 | remote_call_finish(gpu, encoder, decoder); |
| 103 | |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | bool apir_device_supports_op(virtgpu * gpu, const ggml_tensor * op) { |
| 108 | apir_encoder * encoder; |
| 109 | apir_decoder * decoder; |
| 110 | ApirForwardReturnCode ret; |
| 111 | |
| 112 | REMOTE_CALL_PREPARE(gpu, encoder, APIR_COMMAND_TYPE_DEVICE_SUPPORTS_OP); |
| 113 | |
| 114 | apir_encode_ggml_tensor_inline(encoder, op); |
| 115 | |
| 116 | REMOTE_CALL(gpu, encoder, decoder, ret); |
| 117 | |
| 118 | bool supports_op; |
| 119 | apir_decode_bool_t(decoder, &supports_op); |
| 120 | |
| 121 | remote_call_finish(gpu, encoder, decoder); |
| 122 | |
| 123 | return supports_op; |
| 124 | } |
| 125 | |
| 126 | apir_buffer_type_host_handle_t apir_device_get_buffer_type(virtgpu * gpu) { |
| 127 | apir_encoder * encoder; |
| 128 | apir_decoder * decoder; |
| 129 | ApirForwardReturnCode ret; |
| 130 | |
| 131 | REMOTE_CALL_PREPARE(gpu, encoder, APIR_COMMAND_TYPE_DEVICE_GET_BUFFER_TYPE); |
| 132 | |
| 133 | REMOTE_CALL(gpu, encoder, decoder, ret); |
| 134 | |
| 135 | apir_buffer_type_host_handle_t buft_handle; |
| 136 | apir_decode_apir_buffer_type_host_handle_t(decoder, &buft_handle); |
| 137 | |
| 138 | remote_call_finish(gpu, encoder, decoder); |
| 139 | |
| 140 | return buft_handle; |
| 141 | } |
| 142 | |
| 143 | void apir_device_get_props(virtgpu * gpu, |
| 144 | bool * async, |
| 145 | bool * host_buffer, |
| 146 | bool * buffer_from_host_ptr, |
| 147 | bool * events, |
| 148 | bool * mmap_support) { |
| 149 | apir_encoder * encoder; |
| 150 | apir_decoder * decoder; |
| 151 | ApirForwardReturnCode ret; |
| 152 | |
| 153 | REMOTE_CALL_PREPARE(gpu, encoder, APIR_COMMAND_TYPE_DEVICE_GET_PROPS); |
| 154 | |
| 155 | REMOTE_CALL(gpu, encoder, decoder, ret); |
| 156 | |
| 157 | apir_decode_bool_t(decoder, async); |
| 158 | apir_decode_bool_t(decoder, host_buffer); |
| 159 | apir_decode_bool_t(decoder, buffer_from_host_ptr); |
| 160 | apir_decode_bool_t(decoder, events); |
| 161 | apir_decode_bool_t(decoder, mmap_support); |
| 162 | |
| 163 | remote_call_finish(gpu, encoder, decoder); |
| 164 | |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | apir_buffer_context_t apir_device_buffer_from_ptr(virtgpu * gpu, size_t size, size_t max_tensor_size) { |
| 169 | apir_encoder * encoder; |
| 170 | apir_decoder * decoder; |
| 171 | ApirForwardReturnCode ret; |
| 172 | |
| 173 | apir_buffer_context_t buffer_context; |
| 174 | |
| 175 | REMOTE_CALL_PREPARE(gpu, encoder, APIR_COMMAND_TYPE_DEVICE_BUFFER_FROM_PTR); |
| 176 | |
| 177 | if (virtgpu_shmem_create(gpu, size, &buffer_context.shmem)) { |
| 178 | GGML_ABORT(GGML_VIRTGPU "%s: Couldn't allocate %ldb of guest-host shared buffer", __func__, size); |
| 179 | } |
| 180 | |
| 181 | apir_encode_virtgpu_shmem_res_id(encoder, buffer_context.shmem.res_id); |
| 182 | |
| 183 | apir_encode_size_t(encoder, &size); |
| 184 | apir_encode_size_t(encoder, &max_tensor_size); |
| 185 | |
| 186 | REMOTE_CALL(gpu, encoder, decoder, ret); |
| 187 | |
| 188 | apir_decode_apir_buffer_host_handle_t(decoder, &buffer_context.host_handle); |
| 189 | buffer_context.buft_host_handle = apir_decode_apir_buffer_type_host_handle(decoder); |
| 190 | |
| 191 | remote_call_finish(gpu, encoder, decoder); |
| 192 | |
| 193 | return buffer_context; |
| 194 | } |