From ee469bd010f77014e3276a083c7e4828dfe6cd47 Mon Sep 17 00:00:00 2001 From: co-seven Date: Thu, 3 Dec 2026 09:05:35 +0000 Subject: [PATCH] spacemit: fuse H32 GDN state transfers and write back in place Assisted-by: Codex --- ggml/src/ggml-spacemit/ggml-spacemit.cpp | 35 ++++++++++++++++++++---- ggml/src/ggml-spacemit/ime.cpp | 3 +- ggml/src/ggml-spacemit/rvv_kernels.cpp | 21 ++++++++------ ggml/src/ggml-spacemit/rvv_kernels.h | 4 +-- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/ggml/src/ggml-spacemit/ggml-spacemit.cpp b/ggml/src/ggml-spacemit/ggml-spacemit.cpp index 73ae9ee74304..c0fb19f6f890 100644 --- a/ggml/src/ggml-spacemit/ggml-spacemit.cpp +++ b/ggml/src/ggml-spacemit/ggml-spacemit.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #define GGML_COMMON_IMPL_CPP @@ -404,6 +405,7 @@ static bool ssm_conv_concat_fusable(const ggml_cgraph * graph, int ci, bool need static constexpr int SSM_PERSISTENT_PARAM_INPUTS_KEPT = GGML_MAX_OP_PARAMS / sizeof(int32_t) - 2; static constexpr int GDN_STATE_PARAM_INPUT_KEPT = GGML_MAX_OP_PARAMS / sizeof(int32_t) - 1; +static constexpr int GDN_PARAM_CACHE_ONLY = GGML_MAX_OP_PARAMS / sizeof(int32_t) - 1; static const ggml_tensor * tensor_storage_root(const ggml_tensor * t) { while (t && t->view_src) { @@ -488,7 +490,7 @@ static ggml_tensor * ssm_conv_state_store_match(const ggml_cgraph * graph, int c } // Match the cache write and ensure merging its execution into GDN preserves -// the local ordering. The full GDN output is always materialized. +// the local ordering. Cross-split readers are checked separately during planning. 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 || @@ -502,11 +504,15 @@ static ggml_tensor * gdn_state_store_match(const ggml_cgraph * graph, int gi, in } 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 || + const int64_t heads = v->ne[1]; + if (heads != 16 && heads != 32) { + return nullptr; + } + const size_t attention_bytes = 128 * (size_t) heads * sizeof(float); + const size_t state_bytes = 128 * 128 * (size_t) heads * sizeof(float); + if (v->ne[0] != 128 || 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 || + st->ne[0] != 128 || st->ne[1] != 128 || st->ne[2] != heads || st->ne[3] != 1 || ggml_nbytes(gdn) != attention_bytes + state_bytes) { return nullptr; } @@ -790,6 +796,9 @@ static bool try_fuse_node(const ggml_cgraph * graph, int & i, std::vector{}, SPACEMIT_OP_GDN_STATE_STORE, std::vector{copy, gather}); + // Eval callbacks use uid-zero graph slices and may inspect an unmarked tail. + nodes.back().kernel_params[0] = graph->uid != 0 && gather && + ggml_get_op_params_i32(n, GDN_PARAM_CACHE_ONLY) == 1; i = copy_index; return true; } @@ -1157,6 +1166,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_GATED_DELTA_NET) { + ggml_set_op_params_i32(graph->nodes[i], GDN_PARAM_CACHE_ONLY, 0); + } if (graph->nodes[i]->op == GGML_OP_GET_ROWS) { ggml_set_op_params_i32(graph->nodes[i], GDN_STATE_PARAM_INPUT_KEPT, 0); } @@ -1168,6 +1180,15 @@ static void ggml_backend_spacemit_graph_optimize(ggml_backend_t backend, ggml_cg if (params == nullptr || params->add_alloc_dep == nullptr || params->is_split_export == nullptr) { return; } + // A terminal output view can live outside this split without a compute edge + // for the scheduler to report as an export. The hash set spans the full graph. + std::unordered_set output_roots; + const auto & tensors = graph->visited_hash_set; + for (size_t i = 0; i < tensors.size; ++i) { + if (ggml_bitset_get(tensors.used, i) && (tensors.keys[i]->flags & GGML_TENSOR_FLAG_OUTPUT)) { + output_roots.insert(tensor_storage_root(tensors.keys[i])); + } + } for (int i = 0; i < graph->n_nodes; ++i) { ggml_tensor * c = graph->nodes[i]; if (c->op == GGML_OP_GATED_DELTA_NET) { @@ -1177,6 +1198,10 @@ static void ggml_backend_spacemit_graph_optimize(ggml_backend_t backend, ggml_cg 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); + if (c->src[2]->ne[1] == 32 && tensors.size && output_roots.count(c) == 0 && + !params->is_split_export(params->exports, c)) { + ggml_set_op_params_i32(c, GDN_PARAM_CACHE_ONLY, 1); + } } } ggml_tensor * ssm = nullptr; diff --git a/ggml/src/ggml-spacemit/ime.cpp b/ggml/src/ggml-spacemit/ime.cpp index 8b3b3871cc48..3c705f7b6f9b 100644 --- a/ggml/src/ggml-spacemit/ime.cpp +++ b/ggml/src/ggml-spacemit/ime.cpp @@ -2839,7 +2839,8 @@ bool ggml_spacemit_compute_opnode(ggml::spacemit::context & ctx, const spacemit_ 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]); + spacemit_kernels::rvv::forward_gdn_state_store(ctx, node.node, node.extra_dsts[0], node.extra_dsts[1], + node.kernel_params[0] != 0); return true; case SPACEMIT_OP_CONCAT_STATE: // only the trailing columns read back through views (the conv state update) diff --git a/ggml/src/ggml-spacemit/rvv_kernels.cpp b/ggml/src/ggml-spacemit/rvv_kernels.cpp index cf48465ad067..40b4cd8fd2da 100644 --- a/ggml/src/ggml-spacemit/rvv_kernels.cpp +++ b/ggml/src/ggml-spacemit/rvv_kernels.cpp @@ -3599,7 +3599,7 @@ static __attribute__((noinline)) void gdn_recurrent_rows_lookahead(float * } void forward_gated_delta_net(ggml::spacemit::context & ctx, ggml_tensor * op, - const float * state_input, float * state_output) { + const float * state_input, float * state_output, bool cache_only) { const ggml_tensor * src_q = op->src[0]; const ggml_tensor * src_k = op->src[1]; const ggml_tensor * src_v = op->src[2]; @@ -3623,7 +3623,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 = attn_out_base + attn_score_elems; + float * state_out_base = cache_only ? 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); @@ -3704,7 +3704,7 @@ void forward_gated_delta_net(ggml::spacemit::context & ctx, ggml_tensor * op, const float * s_in = state_in_base + iv3 * state_seq_stride + iv1 * S_v * S_v; if (transposed) { gdn_transpose(S_v, S_v, s_in, S_v, s_out, S_v); - } else if (!fused) { + } else if (!fused && s_out != s_in) { memcpy(s_out, s_in, (size_t) (S_v * S_v) * sizeof(float)); } // In the fused form the first token is fused with the state copy: it @@ -3811,7 +3811,7 @@ 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) { + if (state_output && state_output != state_out_base) { // 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; @@ -3831,7 +3831,11 @@ static bool gdn_state_ranges_overlap(const void * a, size_t na, const void * b, 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); + const int64_t heads = gdn->src[2]->ne[1]; + if (heads != 16 && heads != 32) { + return false; + } + const size_t state_bytes = 128 * 128 * (size_t) heads * 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))) { @@ -3863,11 +3867,12 @@ void forward_gdn_state_gather(ggml::spacemit::context & ctx, ggml_tensor * gathe } 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)) { + ggml_tensor * copy, ggml_tensor * gather, bool cache_only) { + const bool direct = gdn_state_direct_valid(gdn, copy, gather); + if (direct) { // 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); + (float *) copy->data, cache_only && gather); } else { forward_gated_delta_net(ctx, gdn); ctx.sync(); diff --git a/ggml/src/ggml-spacemit/rvv_kernels.h b/ggml/src/ggml-spacemit/rvv_kernels.h index c0763460a6db..a72d9153865d 100644 --- a/ggml/src/ggml-spacemit/rvv_kernels.h +++ b/ggml/src/ggml-spacemit/rvv_kernels.h @@ -405,11 +405,11 @@ template void forward_get_rows(ggml::spacemit::context & ctx, ggml_ template void forward_concat(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); + const float * state_input = nullptr, float * state_output = nullptr, bool cache_only = false); 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); + ggml_tensor * copy, ggml_tensor * gather, bool cache_only); // 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);