interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-virtgpu / ggml-backend-device.cpp

160 lines · 6133 bytes Raw Blame History
1#include "ggml-remoting.h"
2
3#include <mutex>
4
5static const char * ggml_backend_remoting_device_get_name(ggml_backend_dev_t dev) {
6 virtgpu * gpu = DEV_TO_GPU(dev);
7
8 // Return the prefixed name that was built once during initialization
9 return gpu->cached_device_info.name;
10}
11
12static const char * ggml_backend_remoting_device_get_description(ggml_backend_dev_t dev) {
13 virtgpu * gpu = DEV_TO_GPU(dev);
14
15 // Return the pre-cached description from the virtgpu structure
16 return gpu->cached_device_info.description;
17}
18
19static enum ggml_backend_dev_type ggml_backend_remoting_device_get_type(ggml_backend_dev_t dev) {
20 virtgpu * gpu = DEV_TO_GPU(dev);
21
22 return (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) {
26 virtgpu * 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 */
36 UNUSED(dev);
37 UNUSED(op);
38
39 return true;
40#else
41 virtgpu * gpu = DEV_TO_GPU(dev);
42
43 return 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) {
48 bool supported = buft->device == dev;
49
50 return supported;
51}
52
53static bool ggml_backend_remoting_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
54 UNUSED(dev);
55 UNUSED(op);
56
57 return false;
58}
59
60static void ggml_backend_remoting_device_get_props(ggml_backend_dev_t dev, ggml_backend_dev_props * props) {
61 props->name = ggml_backend_remoting_device_get_name(dev);
62 props->description = ggml_backend_remoting_device_get_description(dev);
63 props->type = ggml_backend_remoting_device_get_type(dev);
64 ggml_backend_remoting_device_get_memory(dev, &props->memory_free, &props->memory_total);
65
66 virtgpu * gpu = DEV_TO_GPU(dev);
67 apir_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
70 props->caps.buffer_from_host_ptr = false;
71 props->caps.async = false;
72 props->caps.events = false;
73}
74
75ggml_backend_buffer_type_t ggml_backend_remoting_device_get_buffer_type(ggml_backend_dev_t dev) {
76 virtgpu * gpu = DEV_TO_GPU(dev);
77
78 static std::atomic<bool> initialized = false;
79 static ggml_backend_buffer_type buft;
80
81 if (!initialized) {
82 static std::mutex mutex;
83 std::lock_guard<std::mutex> lock(mutex);
84
85 if (!initialized) {
86 buft = {
87 /* .iface = */ ggml_backend_remoting_buffer_type_interface,
88 /* .device = */ dev,
89 /* .context = */ (void *) gpu->cached_buffer_type.host_handle,
90 };
91 initialized = true;
92 }
93 }
94
95 return &buft;
96}
97
98static ggml_backend_buffer_type_t ggml_backend_remoting_device_get_buffer_from_ptr_type(ggml_backend_dev_t dev) {
99 virtgpu * gpu = DEV_TO_GPU(dev);
100
101 static std::atomic<bool> initialized = false;
102 static ggml_backend_buffer_type buft;
103
104 if (!initialized) {
105 static std::mutex mutex;
106 std::lock_guard<std::mutex> lock(mutex);
107
108 if (!initialized) {
109 buft = {
110 /* .iface = */ ggml_backend_remoting_buffer_from_ptr_type_interface,
111 /* .device = */ dev,
112 /* .context = */ (void *) gpu->cached_buffer_type.host_handle,
113 };
114 initialized = true;
115 }
116 }
117
118 return &buft;
119}
120
121static ggml_backend_buffer_t ggml_backend_remoting_device_buffer_from_ptr(ggml_backend_dev_t dev,
122 void * ptr,
123 size_t size,
124 size_t max_tensor_size) {
125 virtgpu * gpu = DEV_TO_GPU(dev);
126
127 ggml_backend_remoting_buffer_context * context = (ggml_backend_remoting_buffer_context *) malloc(sizeof(*context));
128 if (!context) {
129 GGML_ABORT(GGML_VIRTGPU "%s: Couldn't allocate the buffer context ...", __func__);
130 }
131
132 context->gpu = gpu;
133 context->apir_context = apir_device_buffer_from_ptr(gpu, size, max_tensor_size);
134 context->base = ptr;
135 context->is_from_ptr = true;
136
137 ggml_backend_buffer_t buffer =
138 ggml_backend_buffer_init(ggml_backend_remoting_device_get_buffer_from_ptr_type(dev),
139 ggml_backend_remoting_buffer_from_ptr_interface, (void *) context, size);
140
141 return 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};