From 847795648eef3b289b410b1d8c61d3dfef7318bc Mon Sep 17 00:00:00 2001 From: co-seven Date: Fri, 27 Nov 2026 03:20:30 +0000 Subject: [PATCH 1/3] spacemit: fuse GDN state transfers and retain transposed MTP state Assisted-by: Codex --- ggml/src/ggml-spacemit/ggml-spacemit.cpp | 189 +++++++++++++++++++++++ ggml/src/ggml-spacemit/ime.cpp | 10 +- ggml/src/ggml-spacemit/rvv_kernels.cpp | 77 ++++++++- ggml/src/ggml-spacemit/rvv_kernels.h | 7 +- ggml/src/ggml-spacemit/spacemit-opnode.h | 2 + 5 files changed, 273 insertions(+), 12 deletions(-) diff --git a/ggml/src/ggml-spacemit/ggml-spacemit.cpp b/ggml/src/ggml-spacemit/ggml-spacemit.cpp index a1c253350d32..555215800cf7 100644 --- a/ggml/src/ggml-spacemit/ggml-spacemit.cpp +++ b/ggml/src/ggml-spacemit/ggml-spacemit.cpp @@ -310,6 +310,158 @@ static bool ssm_conv_concat_fusable(const ggml_cgraph * graph, int ci, bool need return true; } +static constexpr int GDN_STATE_PARAM_INPUT_KEPT = GGML_MAX_OP_PARAMS / sizeof(int32_t) - 1; + +static const ggml_tensor * tensor_storage_root(const ggml_tensor * t) { + while (t && t->view_src) { + t = t->view_src; + } + return t; +} + +static size_t tensor_storage_offset(const ggml_tensor * t) { + size_t offset = 0; + while (t && t->view_src) { + offset += t->view_offs; + t = t->view_src; + } + return offset; +} + +static int next_compute_index(const ggml_cgraph * graph, int i) { + for (++i; i < graph->n_nodes; ++i) { + if (op_is_compute(graph->nodes[i])) { + return i; + } + } + return -1; +} + + +// Only the state tail may disappear; every other consumer must read the attention prefix. +static ggml_tensor * gdn_state_store_match(const ggml_cgraph * graph, int gi, int * copy_index = nullptr) { + ggml_tensor * gdn = graph->nodes[gi]; + if (gdn->op != GGML_OP_GATED_DELTA_NET || gdn->type != GGML_TYPE_F32 || + (gdn->flags & GGML_TENSOR_FLAG_OUTPUT) || ggml_get_op_params_i32(gdn, 0) != 1) { + return nullptr; + } + for (int j = 0; j < 6; ++j) { + if (!gdn->src[j] || gdn->src[j]->type != GGML_TYPE_F32) { + return nullptr; + } + } + const ggml_tensor * v = gdn->src[2]; + const ggml_tensor * st = gdn->src[5]; + constexpr size_t attention_bytes = 128 * 16 * sizeof(float); + constexpr size_t state_bytes = 128 * 128 * 16 * sizeof(float); + if (v->ne[0] != 128 || v->ne[1] != 16 || v->ne[2] != 1 || v->ne[3] != 1 || + gdn->src[3]->ne[0] != 1 || !ggml_is_contiguous(gdn) || !ggml_is_contiguous(st) || + st->ne[0] != 128 || st->ne[1] != 128 || st->ne[2] != 16 || st->ne[3] != 1 || + ggml_nbytes(gdn) != attention_bytes + state_bytes) { + return nullptr; + } + const int ci = next_compute_index(graph, gi); + if (ci < 0) { + return nullptr; + } + ggml_tensor * copy = graph->nodes[ci]; + const ggml_tensor * cache = tensor_storage_root(copy); + if (copy->op != GGML_OP_CPY || copy->type != GGML_TYPE_F32 || !copy->src[0] || !copy->src[1] || + !ggml_is_contiguous(copy) || !ggml_is_contiguous(copy->src[0]) || + ggml_nbytes(copy) != state_bytes || ggml_nbytes(copy->src[0]) != state_bytes || + tensor_storage_root(copy->src[0]) != gdn || tensor_storage_offset(copy->src[0]) != attention_bytes || + !cache || cache->op != GGML_OP_NONE || !cache->buffer || !cache->data) { + return nullptr; + } + for (int i = 0; i < graph->n_nodes + graph->n_leafs; ++i) { + const ggml_tensor * t = i < graph->n_nodes ? graph->nodes[i] : graph->leafs[i - graph->n_nodes]; + if (tensor_storage_root(t) == gdn && (t->flags & GGML_TENSOR_FLAG_OUTPUT) && + tensor_storage_offset(t) + ggml_nbytes(t) > attention_bytes) { + return nullptr; + } + if (!op_is_compute(const_cast(t))) { + continue; + } + for (int j = 0; j < GGML_MAX_SRC; ++j) { + const ggml_tensor * input = t->src[j]; + if (input && tensor_storage_root(input) == gdn && + tensor_storage_offset(input) + ggml_nbytes(input) > attention_bytes && !(t == copy && j == 0)) { + return nullptr; + } + } + } + if (copy_index) { + *copy_index = ci; + } + return copy; +} + +// A single dynamic index into one persistent cache row. Its lifetime is extended by graph_optimize. +static ggml_tensor * gdn_state_gather_match(const ggml_cgraph * graph, int gi, const ggml_tensor * copy, + bool need_kept) { + ggml_tensor * gdn = graph->nodes[gi]; + auto * gather = const_cast(tensor_storage_root(gdn->src[5])); + if (!gather || gather->op != GGML_OP_GET_ROWS || gather->type != GGML_TYPE_F32 || + tensor_storage_offset(gdn->src[5]) != 0 || !ggml_is_contiguous(gather) || + ggml_nbytes(gather) != ggml_nbytes(copy) || !gather->src[0] || !gather->src[1] || + (need_kept && ggml_get_op_params_i32(gather, GDN_STATE_PARAM_INPUT_KEPT) != 1)) { + return nullptr; + } + const ggml_tensor * cache = gather->src[0]; + const ggml_tensor * index = gather->src[1]; + if (cache->type != GGML_TYPE_F32 || !ggml_is_contiguous(cache) || ggml_nrows(cache) != 1 || + ggml_nbytes(cache) != ggml_nbytes(copy) || index->type != GGML_TYPE_I32 || ggml_nelements(index) != 1 || + tensor_storage_root(cache) != tensor_storage_root(copy) || + tensor_storage_offset(cache) != tensor_storage_offset(copy)) { + return nullptr; + } + int gather_index = -1; + for (int i = 0; i < graph->n_nodes; ++i) { + if (graph->nodes[i] == gather) { + gather_index = i; + } + } + if (gather_index < 0 || gather_index >= gi) { + return nullptr; + } + for (int i = 0; i < graph->n_nodes + graph->n_leafs; ++i) { + const ggml_tensor * t = i < graph->n_nodes ? graph->nodes[i] : graph->leafs[i - graph->n_nodes]; + if (tensor_storage_root(t) == gather && (t->flags & GGML_TENSOR_FLAG_OUTPUT) && !ggml_is_empty(t)) { + return nullptr; + } + if (!op_is_compute(const_cast(t))) { + continue; + } + if (i > gather_index && i < gi) { + if (tensor_storage_root(t) == tensor_storage_root(cache) || + tensor_storage_root(t) == tensor_storage_root(index)) { + return nullptr; + } + // The skipped GET_ROWS captured cache/index at its original position. + // Allocation dependencies retain storage, not that snapshot. At dispatch + // reject writes through external aliases with distinct view roots too. + if (need_kept && t->data) { + for (const ggml_tensor * input : {cache, index}) { + if (!input->data) { + continue; + } + const uintptr_t x = reinterpret_cast(t->data); + const uintptr_t y = reinterpret_cast(input->data); + if (x <= y ? y - x < ggml_nbytes(t) : x - y < ggml_nbytes(input)) { + return nullptr; + } + } + } + } + for (int j = 0; j < GGML_MAX_SRC; ++j) { + if (t->src[j] && tensor_storage_root(t->src[j]) == gather && !(t == gdn && j == 5)) { + return nullptr; + } + } + } + return gather; +} + static bool try_fuse_node(const ggml_cgraph * graph, int & i, std::vector & nodes, std::unordered_set & emitted) { ggml_tensor * n = graph->nodes[i]; @@ -334,6 +486,31 @@ static bool try_fuse_node(const ggml_cgraph * graph, int & i, std::vector= 0) ? graph->nodes[next_idx] : nullptr; + if (n->op == GGML_OP_GET_ROWS && ggml_get_op_params_i32(n, GDN_STATE_PARAM_INPUT_KEPT) == 1) { + for (int gi = i + 1; gi < graph->n_nodes; ++gi) { + if (graph->nodes[gi]->op != GGML_OP_GATED_DELTA_NET || + tensor_storage_root(graph->nodes[gi]->src[5]) != n) { + continue; + } + ggml_tensor * copy = gdn_state_store_match(graph, gi); + if (copy && gdn_state_gather_match(graph, gi, copy, true) == n) { + nodes.emplace_back(n, std::vector{}, SPACEMIT_OP_GDN_STATE_GATHER, + std::vector{graph->nodes[gi], copy}); + return true; + } + } + } + if (n->op == GGML_OP_GATED_DELTA_NET) { + int copy_index = -1; + if (ggml_tensor * copy = gdn_state_store_match(graph, i, ©_index)) { + ggml_tensor * gather = gdn_state_gather_match(graph, i, copy, true); + nodes.emplace_back(n, std::vector{}, SPACEMIT_OP_GDN_STATE_STORE, + std::vector{copy, gather}); + i = copy_index; + return true; + } + } + // Pattern 1: RMS_NORM + MUL if (n->op == GGML_OP_RMS_NORM && next_node && next_node->op == GGML_OP_MUL) { const int idxs[2] = { i, next_idx }; @@ -694,6 +871,9 @@ static void ggml_backend_spacemit_synchronize(ggml_backend_t backend) { static void ggml_backend_spacemit_graph_optimize(ggml_backend_t backend, ggml_cgraph * graph, ggml_backend_graph_optimize_params * params) { for (int i = 0; i < graph->n_nodes; ++i) { + if (graph->nodes[i]->op == GGML_OP_GET_ROWS) { + ggml_set_op_params_i32(graph->nodes[i], GDN_STATE_PARAM_INPUT_KEPT, 0); + } if (graph->nodes[i]->op == GGML_OP_SSM_CONV) { ggml_set_op_params_i32(graph->nodes[i], SSM_CONV_PARAM_INPUTS_KEPT, 0); } @@ -703,6 +883,15 @@ static void ggml_backend_spacemit_graph_optimize(ggml_backend_t backend, ggml_cg } for (int i = 0; i < graph->n_nodes; ++i) { ggml_tensor * c = graph->nodes[i]; + if (c->op == GGML_OP_GATED_DELTA_NET) { + ggml_tensor * copy = gdn_state_store_match(graph, i); + ggml_tensor * gather = copy ? gdn_state_gather_match(graph, i, copy, false) : nullptr; + if (gather) { + params->add_alloc_dep(params->user_data, gather->src[0], c); + params->add_alloc_dep(params->user_data, gather->src[1], c); + ggml_set_op_params_i32(gather, GDN_STATE_PARAM_INPUT_KEPT, 1); + } + } ggml_tensor * ssm = nullptr; int ssm_idx = -1; int64_t col0 = 0; diff --git a/ggml/src/ggml-spacemit/ime.cpp b/ggml/src/ggml-spacemit/ime.cpp index 493c989d4ccf..deed593d5a01 100644 --- a/ggml/src/ggml-spacemit/ime.cpp +++ b/ggml/src/ggml-spacemit/ime.cpp @@ -1860,10 +1860,6 @@ class tensor_traits_f16 : public ggml::spacemit::tensor_traits_base { const int ith = ctx.ith; const int nth = ctx.nth; - if (ith == 0 && getenv("GGML_SPACEMIT_Q41_TRACE") != nullptr) { - fprintf(stderr, "q41_mul_mat: N=%zu M=%zu K=%zu (%s)\n", N, M, K, M == 1 ? "decode" : "prefill"); - } - const size_t out_row_stride = dst->nb[1]; // bytes constexpr size_t m_tile = F16_M_TILE; @@ -2747,6 +2743,12 @@ bool ggml_spacemit_compute_opnode(ggml::spacemit::context & ctx, const spacemit_ // They run at their original graph positions (see try_fuse_node); the allocator plans buffer // reuse on that order, so they must not be merged into one opnode at the concat position. switch (node.opcode) { + case SPACEMIT_OP_GDN_STATE_GATHER: + spacemit_kernels::rvv::forward_gdn_state_gather(ctx, node.node, node.extra_dsts[0], node.extra_dsts[1]); + return true; + case SPACEMIT_OP_GDN_STATE_STORE: + spacemit_kernels::rvv::forward_gdn_state_store(ctx, node.node, node.extra_dsts[0], node.extra_dsts[1]); + return true; case SPACEMIT_OP_CONCAT_STATE: // only the trailing columns read back through views (the conv state update) if (!spacemit_kernels::rvv::forward_concat_dim0_transposed_f32(ctx, node.node, diff --git a/ggml/src/ggml-spacemit/rvv_kernels.cpp b/ggml/src/ggml-spacemit/rvv_kernels.cpp index a9b5032d9baa..d10acabc4061 100644 --- a/ggml/src/ggml-spacemit/rvv_kernels.cpp +++ b/ggml/src/ggml-spacemit/rvv_kernels.cpp @@ -3893,7 +3893,8 @@ static __attribute__((noinline)) void gdn_recurrent_rows_lookahead(float * } } -void forward_gated_delta_net(ggml::spacemit::context & ctx, ggml_tensor * op) { +void forward_gated_delta_net(ggml::spacemit::context & ctx, ggml_tensor * op, + const float * state_input, float * state_output) { const ggml_tensor * src_q = op->src[0]; const ggml_tensor * src_k = op->src[1]; const ggml_tensor * src_v = op->src[2]; @@ -3917,19 +3918,21 @@ void forward_gated_delta_net(ggml::spacemit::context & ctx, ggml_tensor * op) { const int64_t attn_score_elems = S_v * H * n_tokens * n_seqs; const int64_t state_size_per_snap = S_v * S_v * H * n_seqs; float * attn_out_base = (float *) op->data; - float * state_out_base = attn_out_base + attn_score_elems; - const float * state_in_base = (const float *) src_state->data; + float * state_out_base = state_output ? state_output : attn_out_base + attn_score_elems; + const float * state_in_base = state_input ? state_input : (const float *) src_state->data; const float scale = 1.0f / sqrtf((float) S_v); // Scalar GDN uses g[0]; KDA supplies one gate per state column. // When S_v == 1 the two forms coincide; retain the existing KDA dispatch. const bool kda = src_g->ne[0] == S_v; - // Amortize the TCM transpose over prefill tokens; short tails use the row kernels. + // Snapshot outputs remain row-major even when the working state lives transposed + // in TCM. Amortize the TCM transpose over prefill tokens; short tails use the row kernels. const uintptr_t shared_begin = reinterpret_cast(ctx.shared.data); const size_t tcm_offset = align_up(shared_begin, GDN_TCM_ALIGNMENT) - shared_begin; const size_t tcm_bytes = (S_v * S_v + 3 * S_v) * sizeof(float); - const bool transposed = K == 1 && !kda && n_tokens >= GDN_CHUNK_SIZE && S_v % 4 == 0 && + const bool transposed = !kda && + n_tokens >= GDN_CHUNK_SIZE && S_v % 4 == 0 && S_v >= (int64_t) __riscv_vsetvlmax_e32m2() && S_v <= (int64_t) __riscv_vsetvlmax_e32m4() && ctx.shared.data && tcm_offset <= ctx.shared.size && tcm_bytes <= ctx.shared.size - tcm_offset; @@ -4091,16 +4094,76 @@ void forward_gated_delta_net(ggml::spacemit::context & ctx, ggml_tensor * op) { if (target_slot >= 0 && target_slot < K) { float * state_snapshot = state_out_base + target_slot * state_size_per_snap + (iv3 * H + iv1) * S_v * S_v; - memcpy(state_snapshot, s_out, (size_t) (S_v * S_v) * sizeof(float)); + if (transposed) { + gdn_transpose(S_v, S_v, s_out, S_v, state_snapshot, S_v); + } else { + memcpy(state_snapshot, s_out, (size_t) (S_v * S_v) * sizeof(float)); + } } } } - if (transposed) { + // K > 1 already emitted slot0 on the final token above. + if (transposed && K == 1) { gdn_transpose(S_v, S_v, s_out, S_v, state_out_base + (iv3 * H + iv1) * S_v * S_v, S_v); } } } +static bool gdn_state_ranges_overlap(const void * a, size_t na, const void * b, size_t nb) { + if (!na || !nb) { + return false; + } + const uintptr_t x = reinterpret_cast(a); + const uintptr_t y = reinterpret_cast(b); + return x <= y ? y - x < na : x - y < nb; +} + +static bool gdn_state_direct_valid(const ggml_tensor * gdn, const ggml_tensor * copy, + const ggml_tensor * gather) { + constexpr size_t state_bytes = 128 * 128 * 16 * sizeof(float); + if (!gdn->data || !copy->data || !gdn->src[5]->data || + !ggml_is_contiguous(copy) || ggml_nbytes(copy) != state_bytes || + gdn_state_ranges_overlap(copy->data, state_bytes, gdn->data, ggml_nbytes(gdn))) { + return false; + } + for (int j = 0; j < 5; ++j) { + if (!gdn->src[j]->data || gdn_state_ranges_overlap(copy->data, state_bytes, + gdn->src[j]->data, ggml_nbytes(gdn->src[j]))) { + return false; + } + } + if (gather) { + if (!gather->src[0]->data || !gather->src[1]->data || + gather->src[0]->data != copy->data || *(const int32_t *) gather->src[1]->data != 0 || + gdn_state_ranges_overlap(copy->data, state_bytes, gather->src[1]->data, sizeof(int32_t))) { + return false; + } + } else if (gdn_state_ranges_overlap(copy->data, state_bytes, gdn->src[5]->data, state_bytes)) { + return false; + } + return true; +} + +void forward_gdn_state_gather(ggml::spacemit::context & ctx, ggml_tensor * gather, + ggml_tensor * gdn, ggml_tensor * copy) { + if (!gdn_state_direct_valid(gdn, copy, gather)) { + forward_get_rows(ctx, gather); + } +} + +void forward_gdn_state_store(ggml::spacemit::context & ctx, ggml_tensor * gdn, + ggml_tensor * copy, ggml_tensor * gather) { + if (gdn_state_direct_valid(gdn, copy, gather)) { + // Each head owns disjoint rows; the lookahead kernel loads each row before its store. + forward_gated_delta_net(ctx, gdn, gather ? (const float *) gather->src[0]->data : nullptr, + (float *) copy->data); + } else { + forward_gated_delta_net(ctx, gdn); + ctx.sync(); + forward_cpy_strided_f32(ctx, copy); + } +} + void forward_scale_f32(ggml::spacemit::context & ctx, ggml_tensor * op) { const ggml_tensor * src0 = op->src[0]; ggml_tensor * dst = op; diff --git a/ggml/src/ggml-spacemit/rvv_kernels.h b/ggml/src/ggml-spacemit/rvv_kernels.h index 2d8a757e94df..a5fea20897db 100644 --- a/ggml/src/ggml-spacemit/rvv_kernels.h +++ b/ggml/src/ggml-spacemit/rvv_kernels.h @@ -100,7 +100,12 @@ void forward_cpy_with_permute(ggml::spacemit::context & ctx, ggml_tensor * op); template void forward_get_rows(ggml::spacemit::context & ctx, ggml_tensor * op); template void forward_concat(ggml::spacemit::context & ctx, ggml_tensor * op); -void forward_gated_delta_net(ggml::spacemit::context & ctx, ggml_tensor * op); +void forward_gated_delta_net(ggml::spacemit::context & ctx, ggml_tensor * op, + const float * state_input = nullptr, float * state_output = nullptr); +void forward_gdn_state_gather(ggml::spacemit::context & ctx, ggml_tensor * gather, + ggml_tensor * gdn, ggml_tensor * copy); +void forward_gdn_state_store(ggml::spacemit::context & ctx, ggml_tensor * gdn, + ggml_tensor * copy, ggml_tensor * gather); // dim-0 concat of a contiguous src0 and a transposed src1 (channels contiguous), vectorized over rows; // writes columns >= col0 only bool forward_concat_dim0_transposed_f32(ggml::spacemit::context & ctx, ggml_tensor * op, int64_t col0); diff --git a/ggml/src/ggml-spacemit/spacemit-opnode.h b/ggml/src/ggml-spacemit/spacemit-opnode.h index b512349c4cf5..d5a967d6819c 100644 --- a/ggml/src/ggml-spacemit/spacemit-opnode.h +++ b/ggml/src/ggml-spacemit/spacemit-opnode.h @@ -22,6 +22,8 @@ enum spacemit_op_code { SPACEMIT_OP_RMS_NORM_MUL, SPACEMIT_OP_CONCAT_STATE, // concat writing only the trailing columns (fused with a later SSM_CONV) SPACEMIT_OP_SSM_CONV_CONCAT, // ssm_conv reading the concat inputs directly (the concat was partial) + SPACEMIT_OP_GDN_STATE_GATHER, + SPACEMIT_OP_GDN_STATE_STORE, SPACEMIT_OP_ADD, SPACEMIT_OP_UNARY_SILU, SPACEMIT_OP_UNARY_GELU, From 09a6dced75f57e1a4cb7af0430ae9e9ba39cc49b Mon Sep 17 00:00:00 2001 From: co-seven Date: Mon, 30 Nov 2026 13:46:45 +0000 Subject: [PATCH 2/3] ggml-spacemit: materialize GDN state for cross-split consumers Assisted-by: Codex --- ggml/src/ggml-spacemit/ggml-spacemit.cpp | 3 ++- ggml/src/ggml-spacemit/rvv_kernels.cpp | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/ggml/src/ggml-spacemit/ggml-spacemit.cpp b/ggml/src/ggml-spacemit/ggml-spacemit.cpp index 555215800cf7..4a3758afae26 100644 --- a/ggml/src/ggml-spacemit/ggml-spacemit.cpp +++ b/ggml/src/ggml-spacemit/ggml-spacemit.cpp @@ -338,7 +338,8 @@ static int next_compute_index(const ggml_cgraph * graph, int i) { } -// Only the state tail may disappear; every other consumer must read the attention prefix. +// Match the cache write and ensure merging its execution into GDN preserves +// the local ordering. The full GDN output is always materialized. static ggml_tensor * gdn_state_store_match(const ggml_cgraph * graph, int gi, int * copy_index = nullptr) { ggml_tensor * gdn = graph->nodes[gi]; if (gdn->op != GGML_OP_GATED_DELTA_NET || gdn->type != GGML_TYPE_F32 || diff --git a/ggml/src/ggml-spacemit/rvv_kernels.cpp b/ggml/src/ggml-spacemit/rvv_kernels.cpp index d10acabc4061..4f44c1a9109b 100644 --- a/ggml/src/ggml-spacemit/rvv_kernels.cpp +++ b/ggml/src/ggml-spacemit/rvv_kernels.cpp @@ -3918,7 +3918,7 @@ void forward_gated_delta_net(ggml::spacemit::context & ctx, ggml_tensor * op, const int64_t attn_score_elems = S_v * H * n_tokens * n_seqs; const int64_t state_size_per_snap = S_v * S_v * H * n_seqs; float * attn_out_base = (float *) op->data; - float * state_out_base = state_output ? state_output : attn_out_base + attn_score_elems; + float * state_out_base = attn_out_base + attn_score_elems; const float * state_in_base = state_input ? state_input : (const float *) src_state->data; const float scale = 1.0f / sqrtf((float) S_v); @@ -4106,6 +4106,12 @@ void forward_gated_delta_net(ggml::spacemit::context & ctx, ggml_tensor * op, if (transposed && K == 1) { gdn_transpose(S_v, S_v, s_out, S_v, state_out_base + (iv3 * H + iv1) * S_v * S_v, S_v); } + if (state_output) { + // Publish this head to the persistent cache without eliding the + // original tail, which can be read from another scheduler split. + const int64_t offset = (iv3 * H + iv1) * S_v * S_v; + memcpy(state_output + offset, state_out_base + offset, S_v * S_v * sizeof(float)); + } } } From 9f28cc52149799f02d0d5b287442d0a81a6e6b72 Mon Sep 17 00:00:00 2001 From: co-seven Date: Mon, 30 Nov 2026 14:25:22 +0000 Subject: [PATCH 3/3] ggml-spacemit: retain exported GDN gather results Assisted-by: Codex --- ggml/src/ggml-backend-impl.h | 4 ++++ ggml/src/ggml-backend.cpp | 23 +++++++++++++++++++++++ ggml/src/ggml-spacemit/ggml-spacemit.cpp | 2 +- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/ggml/src/ggml-backend-impl.h b/ggml/src/ggml-backend-impl.h index 545880b068c7..57120627b194 100644 --- a/ggml/src/ggml-backend-impl.h +++ b/ggml/src/ggml-backend-impl.h @@ -125,6 +125,10 @@ extern "C" { // can be called multiple times for the same tensor: the longest lifetime applies void (*add_alloc_dep)(void * user_data, struct ggml_tensor * tensor, struct ggml_tensor * until); void * user_data; + // True for a tensor whose storage is read by another scheduler split. + // This includes reads through views and directly shared buffers. + bool (*is_split_export)(const void * exports, const struct ggml_tensor * tensor); + const void * exports; }; struct ggml_backend_i { diff --git a/ggml/src/ggml-backend.cpp b/ggml/src/ggml-backend.cpp index 2e2f9c036560..c39df7910197 100644 --- a/ggml/src/ggml-backend.cpp +++ b/ggml/src/ggml-backend.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #ifdef __APPLE__ @@ -1712,6 +1713,8 @@ void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct ggml_cgra } // pass 5: split graph, find tensors that need to be copied + std::unordered_map producer_splits; + std::unordered_set split_exports; { int i_split = 0; struct ggml_backend_sched_split * split = &sched->splits[0]; @@ -1780,6 +1783,9 @@ void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct ggml_cgra cur_backend_id = node_backend_id; } split_has_nodes = true; + if (node->view_src == nullptr && (node->flags & GGML_TENSOR_FLAG_COMPUTE)) { + producer_splits.emplace(node, i_split); + } // find inputs that are not on the same backend for (int j = 0; j < GGML_MAX_SRC; j++) { @@ -1789,6 +1795,18 @@ void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct ggml_cgra } size_t src_id = hash_id(src); + // Keep the original storage identity before a copied input + // replaces this edge. Compatible buffers also cross splits. + if (node->flags & GGML_TENSOR_FLAG_COMPUTE) { + const ggml_tensor * root = src; + while (root->view_src) { + root = root->view_src; + } + const auto producer = producer_splits.find(root); + if (producer != producer_splits.end() && producer->second != i_split) { + split_exports.insert(root); + } + } const int src_backend_id = sched->hv_tensor_backend_ids[src_id]; GGML_ASSERT(src_backend_id != -1); // all inputs should be assigned by now @@ -1894,6 +1912,11 @@ void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct ggml_cgra } }, /* .user_data = */ &alloc_deps, + /* .is_split_export = */ [](const void * exports, const ggml_tensor * tensor) { + const auto & roots = *static_cast *>(exports); + return roots.count(tensor) != 0; + }, + /* .exports = */ &split_exports, }; for (int i = 0; i < sched->n_splits; i++) { diff --git a/ggml/src/ggml-spacemit/ggml-spacemit.cpp b/ggml/src/ggml-spacemit/ggml-spacemit.cpp index 4a3758afae26..416d2bb56502 100644 --- a/ggml/src/ggml-spacemit/ggml-spacemit.cpp +++ b/ggml/src/ggml-spacemit/ggml-spacemit.cpp @@ -887,7 +887,7 @@ static void ggml_backend_spacemit_graph_optimize(ggml_backend_t backend, ggml_cg if (c->op == GGML_OP_GATED_DELTA_NET) { ggml_tensor * copy = gdn_state_store_match(graph, i); ggml_tensor * gather = copy ? gdn_state_gather_match(graph, i, copy, false) : nullptr; - if (gather) { + if (gather && params->is_split_export && !params->is_split_export(params->exports, gather)) { params->add_alloc_dep(params->user_data, gather->src[0], c); params->add_alloc_dep(params->user_data, gather->src[1], c); ggml_set_op_params_i32(gather, GDN_STATE_PARAM_INPUT_KEPT, 1);