Skip to content

Commit

Permalink
Introduce backend GUIDs (ggml/743)
Browse files Browse the repository at this point in the history
* Introduce backend GUIDs

Initial proposed implementation of backend GUIDs
(Discussed in ggerganov/ggml#741)

Hardcoded CPU backend GUID (for now)
Change ggml_backend_is_cpu logic to use GUID

* Remove redundant functions

Remove redundant functions `ggml_backend_i::get_name` and `ggml_backend_guid` which are not desired for future expansion

* Add spaces to match style

Co-authored-by: slaren <slarengh@gmail.com>

* Fix brace style to match

Co-authored-by: slaren <slarengh@gmail.com>

* Add void to () in function signature

Co-authored-by: slaren <slarengh@gmail.com>

* Add back ggml_backend_guid and make CPU_GUID a local static in ggml_backend_cpu_guid

* add guids to all backends

ggml-ci

---------

Co-authored-by: slaren <slarengh@gmail.com>
  • Loading branch information
2 people authored and ggerganov committed Feb 25, 2024
1 parent f18738f commit 1cb64f7
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 8 deletions.
2 changes: 2 additions & 0 deletions ggml-backend-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ extern "C" {
};

struct ggml_backend {
ggml_guid_t guid;

struct ggml_backend_i iface;

ggml_backend_context_t context;
Expand Down
16 changes: 14 additions & 2 deletions ggml-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#define MAX(a, b) ((a) > (b) ? (a) : (b))


// backend buffer type

const char * ggml_backend_buft_name(ggml_backend_buffer_type_t buft) {
Expand Down Expand Up @@ -159,6 +158,13 @@ bool ggml_backend_buffer_copy_tensor(const struct ggml_tensor * src, struct ggml

// backend

ggml_guid_t ggml_backend_guid(ggml_backend_t backend) {
if (backend == NULL) {
return NULL;
}
return backend->guid;
}

const char * ggml_backend_name(ggml_backend_t backend) {
if (backend == NULL) {
return "NULL";
Expand Down Expand Up @@ -781,6 +787,11 @@ static struct ggml_backend_i cpu_backend_i = {
/* .supports_op = */ ggml_backend_cpu_supports_op,
};

static ggml_guid_t ggml_backend_cpu_guid(void) {
static ggml_guid guid = { 0xaa, 0x67, 0xc7, 0x43, 0x96, 0xe6, 0xa3, 0x8a, 0xe3, 0xaf, 0xea, 0x92, 0x36, 0xbc, 0xfc, 0x89 };
return &guid;
}

ggml_backend_t ggml_backend_cpu_init(void) {
struct ggml_backend_cpu_context * ctx = malloc(sizeof(struct ggml_backend_cpu_context));
if (ctx == NULL) {
Expand All @@ -800,14 +811,15 @@ ggml_backend_t ggml_backend_cpu_init(void) {
}

*cpu_backend = (struct ggml_backend) {
/* .guid = */ ggml_backend_cpu_guid(),
/* .interface = */ cpu_backend_i,
/* .context = */ ctx
};
return cpu_backend;
}

GGML_CALL bool ggml_backend_is_cpu(ggml_backend_t backend) {
return backend && backend->iface.get_name == ggml_backend_cpu_name;
return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_cpu_guid());
}

void ggml_backend_cpu_set_n_threads(ggml_backend_t backend_cpu, int n_threads) {
Expand Down
2 changes: 1 addition & 1 deletion ggml-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extern "C" {
// Backend
//


GGML_API ggml_guid_t ggml_backend_guid(ggml_backend_t backend);
GGML_API const char * ggml_backend_name(ggml_backend_t backend);
GGML_API void ggml_backend_free(ggml_backend_t backend);

Expand Down
8 changes: 7 additions & 1 deletion ggml-cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -11647,6 +11647,11 @@ static ggml_backend_i ggml_backend_cuda_interface = {
/* .supports_op = */ ggml_backend_cuda_supports_op,
};

static ggml_guid_t ggml_backend_cuda_guid() {
static ggml_guid guid = { 0x2c, 0xdd, 0xe8, 0x1c, 0x65, 0xb3, 0x65, 0x73, 0x6a, 0x12, 0x88, 0x61, 0x1c, 0xc9, 0xdc, 0x25 };
return &guid;
}

GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device) {
ggml_init_cublas(); // TODO: remove from ggml.c

Expand All @@ -11664,6 +11669,7 @@ GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device) {
};

ggml_backend_t cuda_backend = new ggml_backend {
/* .guid = */ ggml_backend_cuda_guid(),
/* .interface = */ ggml_backend_cuda_interface,
/* .context = */ ctx
};
Expand All @@ -11672,7 +11678,7 @@ GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device) {
}

GGML_CALL bool ggml_backend_is_cuda(ggml_backend_t backend) {
return backend && backend->iface.get_name == ggml_backend_cuda_name;
return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_cuda_guid());
}

GGML_CALL int ggml_backend_cuda_get_device_count() {
Expand Down
8 changes: 7 additions & 1 deletion ggml-kompute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1953,11 +1953,17 @@ static struct ggml_backend_i kompute_backend_i = {
/* .supports_op = */ ggml_backend_kompute_supports_op,
};

static ggml_guid_t ggml_backend_kompute_guid() {
static ggml_guid guid = { 0x7b, 0x57, 0xdc, 0xaf, 0xde, 0x12, 0x1d, 0x49, 0xfb, 0x35, 0xfa, 0x9b, 0x18, 0x31, 0x1d, 0xca };
return &guid;
}

ggml_backend_t ggml_backend_kompute_init(int device) {
GGML_ASSERT(s_kompute_context == nullptr);
s_kompute_context = new ggml_kompute_context(device);

ggml_backend_t kompute_backend = new ggml_backend {
/* .guid = */ ggml_backend_kompute_guid(),
/* .interface = */ kompute_backend_i,
/* .context = */ s_kompute_context,
};
Expand All @@ -1966,7 +1972,7 @@ ggml_backend_t ggml_backend_kompute_init(int device) {
}

bool ggml_backend_is_kompute(ggml_backend_t backend) {
return backend && backend->iface.get_name == ggml_backend_kompute_name;
return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_kompute_guid());
}

static ggml_backend_t ggml_backend_reg_kompute_init(const char * params, void * user_data) {
Expand Down
8 changes: 7 additions & 1 deletion ggml-metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -2696,6 +2696,11 @@ void ggml_backend_metal_log_set_callback(ggml_log_callback log_callback, void *
ggml_metal_log_user_data = user_data;
}

static ggml_guid_t ggml_backend_metal_guid(void) {
static ggml_guid guid = { 0x81, 0xa1, 0x8b, 0x1e, 0x71, 0xec, 0x79, 0xed, 0x2b, 0x85, 0xdc, 0x8a, 0x61, 0x98, 0x30, 0xe6 };
return &guid;
}

ggml_backend_t ggml_backend_metal_init(void) {
struct ggml_metal_context * ctx = ggml_metal_init(GGML_DEFAULT_N_THREADS);

Expand All @@ -2706,6 +2711,7 @@ ggml_backend_t ggml_backend_metal_init(void) {
ggml_backend_t metal_backend = malloc(sizeof(struct ggml_backend));

*metal_backend = (struct ggml_backend) {
/* .guid = */ ggml_backend_metal_guid(),
/* .interface = */ ggml_backend_metal_i,
/* .context = */ ctx,
};
Expand All @@ -2714,7 +2720,7 @@ ggml_backend_t ggml_backend_metal_init(void) {
}

bool ggml_backend_is_metal(ggml_backend_t backend) {
return backend && backend->iface.get_name == ggml_backend_metal_name;
return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_metal_guid());
}

void ggml_backend_metal_set_n_cb(ggml_backend_t backend, int n_cb) {
Expand Down
8 changes: 7 additions & 1 deletion ggml-sycl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15078,6 +15078,11 @@ static ggml_backend_i ggml_backend_sycl_interface = {
/* .supports_op = */ ggml_backend_sycl_supports_op,
};

static ggml_guid_t ggml_backend_sycl_guid() {
static ggml_guid guid = { 0x58, 0x05, 0x13, 0x8f, 0xcd, 0x3a, 0x61, 0x9d, 0xe7, 0xcd, 0x98, 0xa9, 0x03, 0xfd, 0x7c, 0x53 };
return &guid;
}

ggml_backend_t ggml_backend_sycl_init(int device) {
ggml_init_sycl(); // TODO: remove from ggml.c

Expand All @@ -15095,6 +15100,7 @@ ggml_backend_t ggml_backend_sycl_init(int device) {
};

ggml_backend_t sycl_backend = new ggml_backend {
/* .guid = */ ggml_backend_sycl_guid(),
/* .interface = */ ggml_backend_sycl_interface,
/* .context = */ ctx
};
Expand All @@ -15103,7 +15109,7 @@ ggml_backend_t ggml_backend_sycl_init(int device) {
}

bool ggml_backend_is_sycl(ggml_backend_t backend) {
return backend->iface.get_name == ggml_backend_sycl_name;
return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_sycl_guid());
}

static ggml_backend_t ggml_backend_reg_sycl_init(const char * params, void * user_data) {
Expand Down
8 changes: 7 additions & 1 deletion ggml-vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5244,6 +5244,11 @@ static ggml_backend_i ggml_backend_vk_interface = {
/* .supports_op = */ ggml_backend_vk_supports_op,
};

static ggml_guid_t ggml_backend_vk_guid() {
static ggml_guid guid = { 0xb8, 0xf7, 0x4f, 0x86, 0x40, 0x3c, 0xe1, 0x02, 0x91, 0xc8, 0xdd, 0xe9, 0x02, 0x3f, 0xc0, 0x2b };
return &guid;
}

GGML_CALL ggml_backend_t ggml_backend_vk_init(size_t idx) {
if (vk_instance.initialized[idx]) {
return vk_instance.backends[idx];
Expand All @@ -5262,6 +5267,7 @@ GGML_CALL ggml_backend_t ggml_backend_vk_init(size_t idx) {
vk_instance.initialized[idx] = true;

ggml_backend_t vk_backend = new ggml_backend {
/* .guid = */ ggml_backend_vk_guid(),
/* .interface = */ ggml_backend_vk_interface,
/* .context = */ &vk_instance.contexts[ctx->idx],
};
Expand All @@ -5272,7 +5278,7 @@ GGML_CALL ggml_backend_t ggml_backend_vk_init(size_t idx) {
}

GGML_CALL bool ggml_backend_is_vk(ggml_backend_t backend) {
return backend && backend->iface.get_name == ggml_backend_vk_name;
return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_vk_guid());
}

GGML_CALL int ggml_backend_vk_get_device_count() {
Expand Down
4 changes: 4 additions & 0 deletions ggml.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ void ggml_fp32_to_fp16_row(const float * x, ggml_fp16_t * y, int n) {
}
}

bool ggml_guid_matches(ggml_guid_t guid_a, ggml_guid_t guid_b) {
return memcmp(guid_a, guid_b, sizeof(ggml_guid)) == 0;
}

//
// timing
//
Expand Down
10 changes: 10 additions & 0 deletions ggml.h
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,16 @@ extern "C" {
GGML_NUMA_STRATEGY_COUNT
};

//
// GUID
//

// GUID types
typedef uint8_t ggml_guid[16];
typedef ggml_guid * ggml_guid_t;

GGML_API bool ggml_guid_matches(ggml_guid_t guid_a, ggml_guid_t guid_b);

// misc

GGML_API void ggml_time_init(void); // call this once at the beginning of the program
Expand Down

0 comments on commit 1cb64f7

Please sign in to comment.