Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

callback to abort ggml_graph_compute() #328

Merged
merged 13 commits into from
Jul 11, 2023
Prev Previous commit
Next Next commit
accept callback data
  • Loading branch information
CCLDArjun committed Jul 3, 2023
commit f132e16fc9ceb9bce1fb5a14147dd1f0793b4f80
2 changes: 1 addition & 1 deletion include/ggml/ggml.h
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ extern "C" {
GGML_API struct ggml_cgraph ggml_build_backward(struct ggml_context * ctx, struct ggml_cgraph * gf, bool keep);

GGML_API void ggml_graph_compute(struct ggml_context * ctx, struct ggml_cgraph * cgraph);
GGML_API void ggml_graph_compute_with_abort(struct ggml_context * ctx, struct ggml_cgraph * cgraph, bool (*abort_callback)());
GGML_API void ggml_graph_compute_with_abort(struct ggml_context * ctx, struct ggml_cgraph * cgraph, bool (*abort_callback)(), void * abort_callback_data);
GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph);

GGML_API struct ggml_tensor * ggml_graph_get_tensor(struct ggml_cgraph * cgraph, const char * name);
Expand Down
14 changes: 8 additions & 6 deletions src/ggml.c
Original file line number Diff line number Diff line change
Expand Up @@ -16752,7 +16752,8 @@ struct ggml_compute_state_shared {
atomic_int n_active; // num active threads
atomic_int node_n; // active graph node

bool (*abort_callback)(); // abort ggml_graph_compute when true
bool (*abort_callback)(void * data); // abort ggml_graph_compute when true
void * abort_callback_data;
};

struct ggml_compute_state {
Expand Down Expand Up @@ -16780,7 +16781,7 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
int node_n = -1;

while (true) {
if (state->ith == 0 && state->shared->abort_callback()) {
if (state->ith == 0 && state->shared->abort_callback(state->shared->abort_callback_data)) {
return GGML_EXIT_ABORTED;
}
if (atomic_fetch_sub(&state->shared->n_active, 1) == 1) {
Expand Down Expand Up @@ -16830,7 +16831,7 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
break;
}

if (state->shared->abort_callback()) {
if (state->shared->abort_callback(state->shared->abort_callback_data)) {
break;
}
}
Expand Down Expand Up @@ -16868,13 +16869,13 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
return GGML_EXIT_SUCCESS;
}

static bool always_false() { return false; }
static bool always_false(void * data) { return false; }

void ggml_graph_compute(struct ggml_context * ctx, struct ggml_cgraph * cgraph) {
ggml_graph_compute_with_abort(ctx, cgraph, always_false);
ggml_graph_compute_with_abort(ctx, cgraph, always_false, NULL);
}

void ggml_graph_compute_with_abort(struct ggml_context * ctx, struct ggml_cgraph * cgraph, bool (*abort_callback)(void)) {
void ggml_graph_compute_with_abort(struct ggml_context * ctx, struct ggml_cgraph * cgraph, bool (*abort_callback)(void*), void *abort_callback_data) {
const int n_threads = cgraph->n_threads;

struct ggml_compute_state_shared state_shared = {
Expand All @@ -16885,6 +16886,7 @@ void ggml_graph_compute_with_abort(struct ggml_context * ctx, struct ggml_cgraph
/*.n_active =*/ n_threads,
/*.node_n =*/ -1,
/*.abort_callback =*/ abort_callback,
/*.abort_callback_data =*/ abort_callback_data,
};
struct ggml_compute_state * workers = alloca(sizeof(struct ggml_compute_state)*n_threads);

Expand Down