Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ggml/src/ggml-backend-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions ggml/src/ggml-backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <string.h>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef __APPLE__
Expand Down Expand Up @@ -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<const ggml_tensor *, int> producer_splits;
std::unordered_set<const ggml_tensor *> split_exports;
{
int i_split = 0;
struct ggml_backend_sched_split * split = &sched->splits[0];
Expand Down Expand Up @@ -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++) {
Expand All @@ -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

Expand Down Expand Up @@ -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<const std::unordered_set<const ggml_tensor *> *>(exports);
return roots.count(tensor) != 0;
},
/* .exports = */ &split_exports,
};

for (int i = 0; i < sched->n_splits; i++) {
Expand Down
190 changes: 190 additions & 0 deletions ggml/src/ggml-spacemit/ggml-spacemit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,159 @@ 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;
}


// 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 ||
(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<ggml_tensor *>(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<ggml_tensor *>(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<ggml_tensor *>(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<uintptr_t>(t->data);
const uintptr_t y = reinterpret_cast<uintptr_t>(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<spacemit_opnode> & nodes,
std::unordered_set<ggml_tensor *> & emitted) {
ggml_tensor * n = graph->nodes[i];
Expand All @@ -334,6 +487,31 @@ static bool try_fuse_node(const ggml_cgraph * graph, int & i, std::vector<spacem
}
ggml_tensor * next_node = (next_idx >= 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<ggml_tensor *>{}, SPACEMIT_OP_GDN_STATE_GATHER,
std::vector<ggml_tensor *>{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, &copy_index)) {
ggml_tensor * gather = gdn_state_gather_match(graph, i, copy, true);
nodes.emplace_back(n, std::vector<ggml_tensor *>{}, SPACEMIT_OP_GDN_STATE_STORE,
std::vector<ggml_tensor *>{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 };
Expand Down Expand Up @@ -694,6 +872,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);
}
Expand All @@ -703,6 +884,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->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);
}
}
ggml_tensor * ssm = nullptr;
int ssm_idx = -1;
int64_t col0 = 0;
Expand Down
10 changes: 6 additions & 4 deletions ggml/src/ggml-spacemit/ime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Loading
Loading