Skip to content

Commit d8d3c8e

Browse files
authored
Merge pull request #3 from kpouget/remoting
Integrate virt-gpu code
2 parents ece33a2 + 3270cf9 commit d8d3c8e

24 files changed

+2142
-489
lines changed

ggml/src/ggml-remotingfrontend/CMakeLists.txt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,29 @@ cmake_policy(SET CMP0114 NEW)
1212
message(STATUS "Enable API Remoting frontend found")
1313

1414
ggml_add_backend_library(ggml-remotingfrontend
15-
ggml-remoting-frontend.cpp
15+
ggml-backend-buffer.cpp
16+
ggml-backend.cpp
17+
ggml-backend-device.cpp
18+
ggml-backend-reg.cpp
19+
ggml-buffer-type.cpp
20+
ggml-host-buffer-type.cpp
21+
virtgpu.cpp
22+
virtgpu-shm.cpp
23+
virtgpu-utils.cpp
1624
../../include/ggml-remoting-frontend.h
1725
)
1826

19-
#target_link_libraries(ggml-remotingfrontend PRIVATE remotingfrontend)
27+
target_link_libraries(ggml-remotingfrontend PUBLIC drm)
28+
target_include_directories(ggml-remotingfrontend PUBLIC /usr/include/libdrm/)
29+
30+
set(REMOTING_PROJECT /Users/kevinpouget/remoting)
31+
set(MESA_PROJECT_HOME ${REMOTING_PROJECT}/mesa)
32+
set(MESA_PROJECT_SRC ${MESA_PROJECT_HOME}/src)
33+
34+
target_include_directories(ggml-remotingfrontend PUBLIC ${MESA_PROJECT_SRC}/virtio/virtio-gpu/)
35+
target_include_directories(ggml-remotingfrontend PUBLIC ${MESA_PROJECT_HOME}/include)
2036
target_include_directories(ggml-remotingfrontend PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
37+
38+
target_compile_options(ggml-remotingfrontend PRIVATE -std=c++20)
39+
40+
# dnf install -y libdrm-devel
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <memory>
2+
3+
#include "ggml-remoting.h"
4+
5+
void ggml_remoting_destroy_buffer(remoting_buffer& buf) {
6+
UNUSED(buf);
7+
}
8+
9+
static void ggml_remoting_buffer_write(remoting_buffer& dst, size_t offset, const void * src, size_t size) {
10+
UNUSED(dst);
11+
UNUSED(offset);
12+
UNUSED(src);
13+
UNUSED(size);
14+
}
15+
16+
static void ggml_remoting_buffer_read(remoting_buffer& src, size_t offset, void * dst, size_t size) {
17+
UNUSED(src);
18+
UNUSED(offset);
19+
UNUSED(dst);
20+
UNUSED(size);
21+
}
22+
23+
static void ggml_remoting_buffer_copy_async(remoting_context& ctx, remoting_buffer& dst, size_t dst_offset, remoting_buffer& src, size_t src_offset, size_t size) {
24+
UNUSED(ctx);
25+
UNUSED(dst);
26+
UNUSED(dst_offset);
27+
UNUSED(src);
28+
UNUSED(src_offset);
29+
UNUSED(size);
30+
}
31+
32+
static void * const remoting_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
33+
34+
static uint64_t remoting_tensor_offset(const ggml_tensor * tensor) {
35+
if (tensor->view_src) {
36+
return (uint8_t *) tensor->view_src->data - (uint8_t *) remoting_ptr_base;
37+
}
38+
return (uint8_t *) tensor->data - (uint8_t *) remoting_ptr_base;
39+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include "ggml-remoting.h"
2+
3+
static const char * ggml_backend_remoting_device_get_name(ggml_backend_dev_t dev) {
4+
UNUSED(dev);
5+
return "API Remoting";
6+
}
7+
8+
static const char * ggml_backend_remoting_device_get_description(ggml_backend_dev_t dev) {
9+
UNUSED(dev);
10+
return "API Remoting device";
11+
}
12+
13+
static enum ggml_backend_dev_type ggml_backend_remoting_device_get_type(ggml_backend_dev_t dev) {
14+
UNUSED(dev);
15+
return GGML_BACKEND_DEVICE_TYPE_GPU;
16+
}
17+
18+
static void ggml_backend_remoting_device_get_memory(ggml_backend_dev_t device, size_t * free, size_t * total) {
19+
UNUSED(device);
20+
*total = 1024*1024*1024;
21+
*free = *total;
22+
}
23+
24+
static bool ggml_backend_remoting_device_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
25+
UNUSED(dev);
26+
UNUSED(op);
27+
28+
return true;
29+
}
30+
31+
static bool ggml_backend_remoting_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
32+
UNUSED(dev);
33+
UNUSED(buft);
34+
return true;
35+
}
36+
37+
static bool ggml_backend_remoting_device_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) {
38+
const int min_batch_size = 32;
39+
40+
return (op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS) ||
41+
(op->ne[2] >= min_batch_size && op->op == GGML_OP_MUL_MAT_ID);
42+
43+
UNUSED(dev);
44+
}
45+
46+
static ggml_backend_buffer_type_t ggml_backend_remoting_device_get_host_buffer_type(ggml_backend_dev_t dev) {
47+
UNUSED(dev);
48+
return ggml_backend_remoting_host_buffer_type();
49+
}
50+
51+
52+
static void ggml_backend_remoting_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
53+
props->name = ggml_backend_remoting_device_get_name(dev);
54+
props->description = ggml_backend_remoting_device_get_description(dev);
55+
props->type = ggml_backend_remoting_device_get_type(dev);
56+
ggml_backend_remoting_device_get_memory(dev, &props->memory_free, &props->memory_total);
57+
props->caps = {
58+
/* .async = */ false,
59+
/* .host_buffer = */ true,
60+
/* .buffer_from_host_ptr = */ false,
61+
/* .events = */ false,
62+
};
63+
}
64+
65+
const struct ggml_backend_device_i ggml_backend_remoting_device_i = {
66+
/* .get_name = */ ggml_backend_remoting_device_get_name,
67+
/* .get_description = */ ggml_backend_remoting_device_get_description,
68+
/* .get_memory = */ ggml_backend_remoting_device_get_memory,
69+
/* .get_type = */ ggml_backend_remoting_device_get_type,
70+
/* .get_props = */ ggml_backend_remoting_device_get_props,
71+
/* .init_backend = */ ggml_backend_remoting_device_init,
72+
/* .get_buffer_type = */ ggml_backend_remoting_device_get_buffer_type,
73+
/* .get_host_buffer_type = */ ggml_backend_remoting_device_get_host_buffer_type,
74+
/* .buffer_from_host_ptr = */ NULL,
75+
/* .supports_op = */ ggml_backend_remoting_device_supports_op,
76+
/* .supports_buft = */ ggml_backend_remoting_device_supports_buft,
77+
/* .offload_op = */ ggml_backend_remoting_device_offload_op,
78+
/* .event_new = */ NULL,
79+
/* .event_free = */ NULL,
80+
/* .event_synchronize = */ NULL,
81+
};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <mutex>
2+
#include <iostream>
3+
4+
#include "ggml-remoting.h"
5+
6+
static int ggml_backend_remoting_get_device_count() {
7+
return 1;
8+
}
9+
10+
static size_t ggml_backend_remoting_reg_get_device_count(ggml_backend_reg_t reg) {
11+
UNUSED(reg);
12+
return ggml_backend_remoting_get_device_count();
13+
}
14+
15+
static ggml_backend_dev_t ggml_backend_remoting_reg_get_device(ggml_backend_reg_t reg, size_t device) {
16+
static std::vector<ggml_backend_dev_t> devices;
17+
18+
static bool initialized = false;
19+
20+
{
21+
static std::mutex mutex;
22+
std::lock_guard<std::mutex> lock(mutex);
23+
if (!initialized) {
24+
25+
create_virtgpu();
26+
27+
for (size_t i = 0; i < ggml_backend_remoting_reg_get_device_count(reg); i++) {
28+
ggml_backend_remoting_device_context * ctx = new ggml_backend_remoting_device_context;
29+
char desc[256] = "API Remoting device";
30+
31+
ctx->device = i;
32+
ctx->name = GGML_REMOTING_NAME + std::to_string(i);
33+
ctx->description = desc;
34+
devices.push_back(new ggml_backend_device {
35+
/* .iface = */ ggml_backend_remoting_device_i,
36+
/* .reg = */ reg,
37+
/* .context = */ ctx,
38+
});
39+
}
40+
initialized = true;
41+
}
42+
}
43+
44+
GGML_ASSERT(device < devices.size());
45+
return devices[device];
46+
}
47+
48+
static const char * ggml_backend_remoting_reg_get_name(ggml_backend_reg_t reg) {
49+
UNUSED(reg);
50+
return GGML_REMOTING_NAME;
51+
}
52+
53+
static const struct ggml_backend_reg_i ggml_backend_remoting_reg_i = {
54+
/* .get_name = */ ggml_backend_remoting_reg_get_name,
55+
/* .get_device_count = */ ggml_backend_remoting_reg_get_device_count,
56+
/* .get_device = */ ggml_backend_remoting_reg_get_device,
57+
/* .get_proc_address = */ NULL,
58+
};
59+
60+
ggml_backend_reg_t ggml_backend_remoting_reg() {
61+
static ggml_backend_reg reg = {
62+
/* .api_version = */ GGML_BACKEND_API_VERSION,
63+
/* .iface = */ ggml_backend_remoting_reg_i,
64+
/* .context = */ nullptr,
65+
};
66+
67+
RMT_LOG_DEBUG("ggml_backend_remoting_frontend_reg() hello :wave:");
68+
return &reg;
69+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "ggml-remoting.h"
2+
3+
static const char * ggml_backend_remoting_get_name(ggml_backend_t backend) {
4+
UNUSED(backend);
5+
6+
return "API Remoting backend";
7+
}
8+
9+
static void ggml_backend_remoting_free(ggml_backend_t backend) {
10+
UNUSED(backend);
11+
}
12+
13+
static ggml_status ggml_backend_remoting_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
14+
UNUSED(backend);
15+
UNUSED(cgraph);
16+
17+
return GGML_STATUS_SUCCESS;
18+
}
19+
20+
static ggml_backend_i ggml_backend_remoting_interface = {
21+
/* .get_name = */ ggml_backend_remoting_get_name,
22+
/* .free = */ ggml_backend_remoting_free,
23+
/* .set_tensor_async = */ NULL, // ggml_backend_remoting_set_tensor_async,
24+
/* .get_tensor_async = */ NULL, // ggml_backend_remoting_get_tensor_async,
25+
/* .cpy_tensor_async = */ NULL, // ggml_backend_remoting_cpy_tensor_async,
26+
/* .synchronize = */ NULL, // ggml_backend_remoting_synchronize,
27+
/* .graph_plan_create = */ NULL,
28+
/* .graph_plan_free = */ NULL,
29+
/* .graph_plan_update = */ NULL,
30+
/* .graph_plan_compute = */ NULL,
31+
/* .graph_compute = */ ggml_backend_remoting_graph_compute,
32+
/* .event_record = */ NULL,
33+
/* .event_wait = */ NULL,
34+
};
35+
36+
static ggml_guid_t ggml_backend_remoting_guid() {
37+
static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
38+
return &guid;
39+
}
40+
41+
42+
ggml_backend_t ggml_backend_remoting_device_init(ggml_backend_dev_t dev, const char * params) {
43+
UNUSED(params);
44+
ggml_backend_remoting_device_context * ctx = (ggml_backend_remoting_device_context *)dev->context;
45+
46+
ggml_backend_t remoting_backend = new ggml_backend {
47+
/* .guid = */ ggml_backend_remoting_guid(),
48+
/* .interface = */ ggml_backend_remoting_interface,
49+
/* .device = */ ggml_backend_reg_dev_get(ggml_backend_remoting_reg(), ctx->device),
50+
/* .context = */ ctx,
51+
};
52+
53+
return remoting_backend;
54+
}

0 commit comments

Comments
 (0)