Commit a89559540 for llama.cpp
commit a89559540d6dec7ac9d01e3bddd0e0a004dec1c9
Author: yoshi <61565054+Yoshi4470@users.noreply.github.com>
Date: Thu Dec 3 22:19:06 2026 +0900
ggml : fix view init skipped after buft max_size split (#25584)
* ggml : fix view init skipped after buft max_size split
When ggml_backend_buft_alloc_buffer_n splits allocation on buft
max_size, a view-only tail has size 0 and is left out of the plan.
Persistent views (e.g. KV k_stream / v_stream) were then left without
ggml_backend_view_init.
Do not split on a size-0 tensor. The views stay in the parent range,
and the existing view_init runs.
* ggml : add regression test for view init after max_size split
Cover the buft max_size split path where a view-only tail is left out
of the alloc plan and would skip ggml_backend_view_init.
diff --git a/ggml/src/ggml-backend.cpp b/ggml/src/ggml-backend.cpp
index 9a12391bb..88def1505 100644
--- a/ggml/src/ggml-backend.cpp
+++ b/ggml/src/ggml-backend.cpp
@@ -73,7 +73,7 @@ static ggml_backend_buft_alloc_buffer_n_plan_t ggml_backend_buft_alloc_buffer_n_
}
// flush the current buffer if adding this tensor would exceed max_size
- if (cur_buf_size > 0 && (cur_buf_size + this_size) > max_size) {
+ if (this_size > 0 && cur_buf_size > 0 && (cur_buf_size + this_size) > max_size) {
plan.push_back({ cur_buf_size, first, i });
cur_buf_size = this_size;
first = i;
diff --git a/tests/test-alloc.cpp b/tests/test-alloc.cpp
index 6f86a5865..80a15003a 100644
--- a/tests/test-alloc.cpp
+++ b/tests/test-alloc.cpp
@@ -1167,6 +1167,30 @@ static void test_buft_get_alloc_size_n_does_not_allocate() {
GGML_ASSERT(x[1]->data == nullptr);
}
+// Parent larger than max_size splits off a view-only tail with size 0.
+// That tail is not in the alloc plan, so views must be initialized after the split.
+static void test_view_init_after_max_size_split() {
+ const size_t max_size = 16;
+ dummy_backend backend = dummy_backend_init(max_size);
+ auto [ctx, graph, ctx_ptr] = make_context();
+ (void) graph;
+
+ ggml_tensor * parent = make_input_with_size(ctx, 24); // 6 x f32, > max_size
+ ggml_tensor * view0 = ggml_view_1d(ctx, parent, 2, 0);
+ ggml_tensor * view1 = ggml_view_1d(ctx, parent, 2, 2 * sizeof(float));
+ assign_names(ctx);
+
+ ggml_backend_buffer_ptr buf(ggml_backend_alloc_ctx_tensors_from_buft(ctx, &backend.buffer_type));
+ GGML_ASSERT(buf);
+
+ for (ggml_tensor * t = ggml_get_first_tensor(ctx); t; t = ggml_get_next_tensor(ctx, t)) {
+ GGML_ASSERT(t->buffer != nullptr);
+ GGML_ASSERT(t->data != nullptr);
+ }
+ GGML_ASSERT(view0->view_src == parent);
+ GGML_ASSERT(view1->view_src == parent);
+}
+
static void run(const char * name, void (*f)()) {
printf("%s ", name);
fflush(stdout);
@@ -1208,5 +1232,6 @@ int main() {
run("test_buft_get_alloc_size_n_custom_override", test_buft_get_alloc_size_n_custom_override);
run("test_buft_get_alloc_size_n_custom_override_ctx_size", test_buft_get_alloc_size_n_custom_override_ctx_size);
run("test_buft_get_alloc_size_n_does_not_allocate", test_buft_get_alloc_size_n_does_not_allocate);
+ run("test_view_init_after_max_size_split", test_view_init_after_max_size_split);
return 0;
}