move device backend_reg to the struct

This commit is contained in:
slaren 2024-10-02 21:08:07 +02:00
parent dc475c3b20
commit d0c4954fa0
3 changed files with 13 additions and 23 deletions

View file

@ -166,9 +166,6 @@ extern "C" {
// device properties // device properties
void (*get_props)(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props); void (*get_props)(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props);
// get the backend (reg) associated with this device
ggml_backend_reg_t (*get_backend_reg)(ggml_backend_dev_t dev);
// backend (stream) initialization // backend (stream) initialization
ggml_backend_t (*init_backend)(ggml_backend_dev_t dev, const char * params); ggml_backend_t (*init_backend)(ggml_backend_dev_t dev, const char * params);
@ -200,6 +197,7 @@ extern "C" {
struct ggml_backend_device { struct ggml_backend_device {
struct ggml_backend_device_i iface; struct ggml_backend_device_i iface;
ggml_backend_reg_t reg;
void * context; void * context;
}; };

View file

@ -453,7 +453,7 @@ void ggml_backend_dev_get_props(ggml_backend_dev_t device, struct ggml_backend_d
} }
ggml_backend_reg_t ggml_backend_dev_backend_reg(ggml_backend_dev_t device) { ggml_backend_reg_t ggml_backend_dev_backend_reg(ggml_backend_dev_t device) {
return device->iface.get_backend_reg(device); return device->reg;
} }
ggml_backend_t ggml_backend_dev_init(ggml_backend_dev_t device, const char * params) { ggml_backend_t ggml_backend_dev_init(ggml_backend_dev_t device, const char * params) {
@ -1061,12 +1061,6 @@ static void ggml_backend_cpu_device_props(ggml_backend_dev_t dev, struct ggml_ba
}; };
} }
static ggml_backend_reg_t ggml_backend_cpu_device_reg(ggml_backend_dev_t dev) {
return ggml_backend_cpu_reg();
GGML_UNUSED(dev);
}
static ggml_backend_t ggml_backend_cpu_device_init(ggml_backend_dev_t dev, const char * params) { static ggml_backend_t ggml_backend_cpu_device_init(ggml_backend_dev_t dev, const char * params) {
return ggml_backend_cpu_init(); return ggml_backend_cpu_init();
@ -1122,7 +1116,6 @@ static struct ggml_backend_device_i ggml_backend_cpu_device_i = {
/* .get_memory = */ ggml_backend_cpu_device_memory, /* .get_memory = */ ggml_backend_cpu_device_memory,
/* .get_type = */ ggml_backend_cpu_device_type, /* .get_type = */ ggml_backend_cpu_device_type,
/* .get_props = */ ggml_backend_cpu_device_props, /* .get_props = */ ggml_backend_cpu_device_props,
/* .get_backend_reg = */ ggml_backend_cpu_device_reg,
/* .init_backend = */ ggml_backend_cpu_device_init, /* .init_backend = */ ggml_backend_cpu_device_init,
/* .buffer_type = */ ggml_backend_cpu_device_buffer_type, /* .buffer_type = */ ggml_backend_cpu_device_buffer_type,
/* .host_buffer_type = */ NULL, /* .host_buffer_type = */ NULL,
@ -1154,6 +1147,7 @@ static ggml_backend_dev_t ggml_backend_cpu_reg_device_get(ggml_backend_reg_t reg
static ggml_backend_device ggml_backend_cpu_device = { static ggml_backend_device ggml_backend_cpu_device = {
/* .iface = */ ggml_backend_cpu_device_i, /* .iface = */ ggml_backend_cpu_device_i,
/* .reg = */ reg,
/* .context = */ NULL, /* .context = */ NULL,
}; };

View file

@ -2954,11 +2954,6 @@ static void ggml_backend_cuda_device_props(ggml_backend_dev_t dev, ggml_backend_
}; };
} }
static ggml_backend_reg_t ggml_backend_cuda_device_reg(ggml_backend_dev_t dev) {
GGML_UNUSED(dev);
return ggml_backend_cuda_reg();
}
static ggml_backend_t ggml_backend_cuda_device_init(ggml_backend_dev_t dev, const char * params) { static ggml_backend_t ggml_backend_cuda_device_init(ggml_backend_dev_t dev, const char * params) {
GGML_UNUSED(params); GGML_UNUSED(params);
ggml_backend_cuda_device_context * ctx = (ggml_backend_cuda_device_context *)dev->context; ggml_backend_cuda_device_context * ctx = (ggml_backend_cuda_device_context *)dev->context;
@ -3262,7 +3257,6 @@ static ggml_backend_device_i ggml_backend_cuda_device_interface = {
/* .get_memory = */ ggml_backend_cuda_device_memory, /* .get_memory = */ ggml_backend_cuda_device_memory,
/* .get_type = */ ggml_backend_cuda_device_type, /* .get_type = */ ggml_backend_cuda_device_type,
/* .get_props = */ ggml_backend_cuda_device_props, /* .get_props = */ ggml_backend_cuda_device_props,
/* .get_backend_reg = */ ggml_backend_cuda_device_reg,
/* .init_backend = */ ggml_backend_cuda_device_init, /* .init_backend = */ ggml_backend_cuda_device_init,
/* .buffer_type = */ ggml_backend_cuda_device_buffer_type, /* .buffer_type = */ ggml_backend_cuda_device_buffer_type,
/* .host_buffer_type = */ ggml_backend_cuda_device_host_buffer_type, /* .host_buffer_type = */ ggml_backend_cuda_device_host_buffer_type,
@ -3291,7 +3285,7 @@ static size_t ggml_backend_cuda_reg_get_device_count(ggml_backend_reg_t reg) {
return ctx->devices.size(); return ctx->devices.size();
} }
static ggml_backend_dev_t ggml_backend_cuda_reg_get_device(ggml_backend_reg_t reg, size_t index) { static ggml_backend_dev_t ggml_backend_cuda_reg_device_get(ggml_backend_reg_t reg, size_t index) {
ggml_backend_cuda_reg_context * ctx = (ggml_backend_cuda_reg_context *)reg->context; ggml_backend_cuda_reg_context * ctx = (ggml_backend_cuda_reg_context *)reg->context;
GGML_ASSERT(index < ctx->devices.size()); GGML_ASSERT(index < ctx->devices.size());
return ctx->devices[index]; return ctx->devices[index];
@ -3319,19 +3313,20 @@ static void ggml_backend_cuda_reg_set_log_callback(ggml_backend_reg_t reg, ggml_
static ggml_backend_reg_i ggml_backend_cuda_reg_interface = { static ggml_backend_reg_i ggml_backend_cuda_reg_interface = {
/* .get_name = */ ggml_backend_cuda_reg_name, /* .get_name = */ ggml_backend_cuda_reg_name,
/* .device_count = */ ggml_backend_cuda_reg_get_device_count, /* .device_count = */ ggml_backend_cuda_reg_get_device_count,
/* .device_get = */ ggml_backend_cuda_reg_get_device, /* .device_get = */ ggml_backend_cuda_reg_device_get,
/* .get_proc_address = */ ggml_backend_cuda_get_proc_address, /* .get_proc_address = */ ggml_backend_cuda_get_proc_address,
/* .set_log_callback = */ ggml_backend_cuda_reg_set_log_callback, /* .set_log_callback = */ ggml_backend_cuda_reg_set_log_callback,
}; };
// backend registry // backend registry
ggml_backend_reg_t ggml_backend_cuda_reg() { ggml_backend_reg_t ggml_backend_cuda_reg() {
static ggml_backend_reg_t reg = nullptr; static ggml_backend_reg reg;
static bool initialized = false;
{ {
static std::mutex mutex; static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
if (!reg) { if (!initialized) {
ggml_backend_cuda_reg_context * ctx = new ggml_backend_cuda_reg_context; ggml_backend_cuda_reg_context * ctx = new ggml_backend_cuda_reg_context;
for (int i = 0; i < ggml_cuda_info().device_count; i++) { for (int i = 0; i < ggml_cuda_info().device_count; i++) {
@ -3346,19 +3341,22 @@ ggml_backend_reg_t ggml_backend_cuda_reg() {
ggml_backend_dev_t dev = new ggml_backend_device { ggml_backend_dev_t dev = new ggml_backend_device {
/* .interface = */ ggml_backend_cuda_device_interface, /* .interface = */ ggml_backend_cuda_device_interface,
/* .reg = */ &reg,
/* .context = */ dev_ctx /* .context = */ dev_ctx
}; };
ctx->devices.push_back(dev); ctx->devices.push_back(dev);
} }
reg = new ggml_backend_reg { reg = ggml_backend_reg {
/* .interface = */ ggml_backend_cuda_reg_interface, /* .interface = */ ggml_backend_cuda_reg_interface,
/* .context = */ ctx /* .context = */ ctx
}; };
} }
initialized = true;
} }
return reg; return &reg;
} }
ggml_backend_t ggml_backend_cuda_init(int device) { ggml_backend_t ggml_backend_cuda_init(int device) {