Skip to content

Commit 5a9f071

Browse files
committed
wip
1 parent 7bed7eb commit 5a9f071

File tree

6 files changed

+703
-1508
lines changed

6 files changed

+703
-1508
lines changed

ggml-alloc.c

+11
Original file line numberDiff line numberDiff line change
@@ -779,10 +779,21 @@ ggml_backend_buffer_t ggml_backend_alloc_ctx_tensors_from_buft(struct ggml_conte
779779

780780
if (nbytes == 0) {
781781
// all the tensors in the context are already allocated
782+
#ifndef NDEBUG
783+
fprintf(stderr, "%s: all tensors in the context are already allocated\n", __func__);
784+
#endif
782785
return NULL;
783786
}
784787

785788
ggml_backend_buffer_t buffer = ggml_backend_buft_alloc_buffer(buft, nbytes);
789+
if (buffer == NULL) {
790+
// failed to allocate buffer
791+
#ifndef NDEBUG
792+
fprintf(stderr, "%s: failed to allocate buffer\n", __func__);
793+
#endif
794+
return NULL;
795+
}
796+
786797
ggml_tallocr_t tallocr = ggml_tallocr_new_from_buffer(buffer);
787798

788799
for (struct ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {

ggml-backend.c

+19-6
Original file line numberDiff line numberDiff line change
@@ -809,16 +809,16 @@ static ggml_backend_t sched_backend_from_cur(ggml_backend_sched_t sched, struct
809809
break;
810810
}
811811
ggml_backend_t src_backend = get_buffer_backend(sched, src->buffer);
812-
if (src_backend != NULL) {
812+
//if (src_backend != NULL) {
813813
int src_prio = sched_backend_prio(sched, src_backend);
814814
size_t src_size = ggml_nbytes(src);
815-
if (src_prio < cur_prio && src_size >= cur_size) {
815+
if (/*src_prio < cur_prio &&*/ src_size >= cur_size) {
816816
cur_prio = src_prio;
817817
cur_size = src_size;
818818
cur_backend = src_backend;
819819
SET_CAUSE(node, "1.src%d", i);
820820
}
821-
}
821+
//}
822822
}
823823
return cur_backend;
824824
}
@@ -1025,9 +1025,21 @@ static void sched_split_graph(ggml_backend_sched_t sched, struct ggml_cgraph * g
10251025
}
10261026
ggml_tallocr_t src_allocr = node_allocr(src);
10271027
if (src_allocr != node_allocr) {
1028-
int n_inputs = sched->splits[cur_split].n_inputs++;
1029-
GGML_ASSERT(n_inputs < GGML_MAX_SPLIT_INPUTS);
1030-
sched->splits[cur_split].inputs[n_inputs] = (struct ggml_tensor *)src;
1028+
// check if the input is already in the split
1029+
bool found = false;
1030+
for (int k = 0; k < sched->splits[cur_split].n_inputs; k++) {
1031+
if (sched->splits[cur_split].inputs[k] == src) {
1032+
found = true;
1033+
break;
1034+
}
1035+
}
1036+
1037+
if (!found) {
1038+
int n_inputs = sched->splits[cur_split].n_inputs++;
1039+
//printf("split %d input %d: %s (%s)\n", cur_split, n_inputs, src->name, ggml_backend_name(get_allocr_backend(sched, src_allocr)));
1040+
GGML_ASSERT(n_inputs < GGML_MAX_SPLIT_INPUTS);
1041+
sched->splits[cur_split].inputs[n_inputs] = (struct ggml_tensor *)src;
1042+
}
10311043

10321044
// create copies
10331045
size_t id = hash_id(src);
@@ -1316,6 +1328,7 @@ static void graph_init_tensor(struct ggml_hash_set hash_set, struct ggml_tensor
13161328

13171329
struct ggml_tensor * dst = node_copies[id];
13181330
if (dst->view_src != NULL) {
1331+
graph_init_tensor(hash_set, node_copies, node_init, src->view_src);
13191332
ggml_backend_view_init(dst->view_src->buffer, dst);
13201333
}
13211334
else {

0 commit comments

Comments
 (0)