add version checking

This commit is contained in:
slaren 2024-11-24 23:54:16 +01:00
parent bd9f7b4297
commit 53d7f4f658
12 changed files with 49 additions and 32 deletions

View File

@ -409,8 +409,9 @@ static const struct ggml_backend_reg_i ggml_backend_amx_reg_i = {
ggml_backend_reg_t ggml_backend_amx_reg(void) { ggml_backend_reg_t ggml_backend_amx_reg(void) {
static struct ggml_backend_reg ggml_backend_amx_reg = { static struct ggml_backend_reg ggml_backend_amx_reg = {
/* .iface = */ ggml_backend_amx_reg_i, /* .api_version = */ GGML_BACKEND_API_VERSION,
/* .context = */ NULL, /* .iface = */ ggml_backend_amx_reg_i,
/* .context = */ NULL,
}; };
return &ggml_backend_amx_reg; return &ggml_backend_amx_reg;

View File

@ -8,6 +8,8 @@
extern "C" { extern "C" {
#endif #endif
#define GGML_BACKEND_API_VERSION 1
// //
// Backend buffer type // Backend buffer type
// //
@ -199,7 +201,7 @@ extern "C" {
}; };
struct ggml_backend_reg { struct ggml_backend_reg {
// int api_version; // TODO: for dynamic loading int api_version; // initialize to GGML_BACKEND_API_VERSION
struct ggml_backend_reg_i iface; struct ggml_backend_reg_i iface;
void * context; void * context;
}; };

View File

@ -167,9 +167,14 @@ struct ggml_backend_registry {
} }
#endif #endif
ggml_backend_reg_t reg = backend_init(); ggml_backend_reg_t reg = backend_init();
if (!reg) { if (!reg || reg->api_version != GGML_BACKEND_API_VERSION) {
if (!silent) { if (!silent) {
GGML_LOG_ERROR("%s: failed to initialize backend from %s\n", __func__, path); if (!reg) {
GGML_LOG_ERROR("%s: failed to initialize backend from %s: ggml_backend_init returned NULL\n", __func__, path);
} else {
GGML_LOG_ERROR("%s: failed to initialize backend from %s: incompatible API version (backend: %d, current: %d)\n",
__func__, path, reg->api_version, GGML_BACKEND_API_VERSION);
}
} }
#ifdef _WIN32 #ifdef _WIN32
FreeLibrary(handle); FreeLibrary(handle);

View File

@ -506,8 +506,9 @@ static const struct ggml_backend_reg_i ggml_backend_blas_reg_i = {
ggml_backend_reg_t ggml_backend_blas_reg(void) { ggml_backend_reg_t ggml_backend_blas_reg(void) {
static struct ggml_backend_reg ggml_backend_blas_reg = { static struct ggml_backend_reg ggml_backend_blas_reg = {
/* .iface = */ ggml_backend_blas_reg_i, /* .api_version = */ GGML_BACKEND_API_VERSION,
/* .context = */ NULL, /* .iface = */ ggml_backend_blas_reg_i,
/* .context = */ NULL,
}; };
return &ggml_backend_blas_reg; return &ggml_backend_blas_reg;

View File

@ -2064,16 +2064,17 @@ ggml_backend_reg_t ggml_backend_cann_reg() {
dev_ctx->name = GGML_CANN_NAME + std::to_string(i); dev_ctx->name = GGML_CANN_NAME + std::to_string(i);
ggml_cann_set_device(i); ggml_cann_set_device(i);
ggml_backend_dev_t dev = new ggml_backend_device { ggml_backend_dev_t dev = new ggml_backend_device {
/* .interface = */ ggml_backend_cann_device_interface, /* .iface = */ ggml_backend_cann_device_interface,
/* .reg = */ &reg, /* .reg = */ &reg,
/* .context = */ dev_ctx /* .context = */ dev_ctx
}; };
ctx->devices.push_back(dev); ctx->devices.push_back(dev);
} }
reg = ggml_backend_reg { reg = ggml_backend_reg {
/* .interface = */ ggml_backend_cann_reg_interface, /* .api_version = */ GGML_BACKEND_API_VERSION,
/* .context = */ ctx /* .iface = */ ggml_backend_cann_reg_interface,
/* .context = */ ctx
}; };
} }

View File

@ -678,8 +678,9 @@ ggml_backend_reg_t ggml_backend_cpu_reg(void) {
ggml_cpu_init(); ggml_cpu_init();
static struct ggml_backend_reg ggml_backend_cpu_reg = { static struct ggml_backend_reg ggml_backend_cpu_reg = {
/* .iface = */ ggml_backend_cpu_reg_i, /* .api_version = */ GGML_BACKEND_API_VERSION,
/* .context = */ NULL, /* .iface = */ ggml_backend_cpu_reg_i,
/* .context = */ NULL,
}; };
return &ggml_backend_cpu_reg; return &ggml_backend_cpu_reg;

View File

@ -3227,16 +3227,17 @@ ggml_backend_reg_t ggml_backend_cuda_reg() {
dev_ctx->description = prop.name; dev_ctx->description = prop.name;
ggml_backend_dev_t dev = new ggml_backend_device { ggml_backend_dev_t dev = new ggml_backend_device {
/* .interface = */ ggml_backend_cuda_device_interface, /* .iface = */ ggml_backend_cuda_device_interface,
/* .reg = */ &reg, /* .reg = */ &reg,
/* .context = */ dev_ctx /* .context = */ dev_ctx
}; };
ctx->devices.push_back(dev); ctx->devices.push_back(dev);
} }
reg = ggml_backend_reg { reg = ggml_backend_reg {
/* .interface = */ ggml_backend_cuda_reg_interface, /* .api_version = */ GGML_BACKEND_API_VERSION,
/* .context = */ ctx /* .iface = */ ggml_backend_cuda_reg_interface,
/* .context = */ ctx
}; };
} }

View File

@ -2176,8 +2176,9 @@ static const struct ggml_backend_reg_i ggml_backend_kompute_reg_i = {
ggml_backend_reg_t ggml_backend_kompute_reg() { ggml_backend_reg_t ggml_backend_kompute_reg() {
static ggml_backend_reg reg = { static ggml_backend_reg reg = {
/* .iface = */ ggml_backend_kompute_reg_i, /* .api_version = */ GGML_BACKEND_API_VERSION,
/* .context = */ nullptr, /* .iface = */ ggml_backend_kompute_reg_i,
/* .context = */ nullptr,
}; };
return ® return ®

View File

@ -4408,8 +4408,9 @@ ggml_backend_reg_t ggml_backend_metal_reg(void) {
// TODO: make this thread-safe somehow? // TODO: make this thread-safe somehow?
{ {
g_ggml_backend_metal_reg = (struct ggml_backend_reg) { g_ggml_backend_metal_reg = (struct ggml_backend_reg) {
/* .iface = */ ggml_backend_metal_reg_i, /* .api_version = */ GGML_BACKEND_API_VERSION,
/* .context = */ NULL, /* .iface = */ ggml_backend_metal_reg_i,
/* .context = */ NULL,
}; };
g_ggml_backend_metal_device = (struct ggml_backend_device) { g_ggml_backend_metal_device = (struct ggml_backend_device) {

View File

@ -1369,8 +1369,9 @@ static const struct ggml_backend_reg_i ggml_backend_rpc_reg_i = {
ggml_backend_reg_t ggml_backend_rpc_reg(void) { ggml_backend_reg_t ggml_backend_rpc_reg(void) {
static struct ggml_backend_reg ggml_backend_rpc_reg = { static struct ggml_backend_reg ggml_backend_rpc_reg = {
/* .iface = */ ggml_backend_rpc_reg_i, /* .api_version = */ GGML_BACKEND_API_VERSION,
/* .context = */ NULL, /* .iface = */ ggml_backend_rpc_reg_i,
/* .context = */ NULL,
}; };
return &ggml_backend_rpc_reg; return &ggml_backend_rpc_reg;

View File

@ -4637,16 +4637,17 @@ ggml_backend_reg_t ggml_backend_sycl_reg() {
dev_ctx->description = prop.get_name(); dev_ctx->description = prop.get_name();
ggml_backend_dev_t dev = new ggml_backend_device { ggml_backend_dev_t dev = new ggml_backend_device {
/* .interface = */ ggml_backend_sycl_device_interface, /* .iface = */ ggml_backend_sycl_device_interface,
/* .reg = */ &reg, /* .reg = */ &reg,
/* .context = */ dev_ctx /* .context = */ dev_ctx
}; };
ctx->devices.push_back(dev); ctx->devices.push_back(dev);
} }
reg = ggml_backend_reg { reg = ggml_backend_reg {
/* .interface = */ ggml_backend_sycl_reg_interface, /* .api_version = */ GGML_BACKEND_API_VERSION,
/* .context = */ ctx /* .iface = */ ggml_backend_sycl_reg_interface,
/* .context = */ ctx
}; };
} }

View File

@ -6738,8 +6738,9 @@ static const struct ggml_backend_reg_i ggml_backend_vk_reg_i = {
ggml_backend_reg_t ggml_backend_vk_reg() { ggml_backend_reg_t ggml_backend_vk_reg() {
static ggml_backend_reg reg = { static ggml_backend_reg reg = {
/* .iface = */ ggml_backend_vk_reg_i, /* .api_version = */ GGML_BACKEND_API_VERSION,
/* .context = */ nullptr, /* .iface = */ ggml_backend_vk_reg_i,
/* .context = */ nullptr,
}; };
return ® return ®