Skip to content

ggml : add ggml_backend_op_alloc_size_may_expand, use it in RPC - #27960

Merged
ggerganov merged 1 commit into
masterfrom
gg/rpc-stronger-get-alloc-size
Aug 30, 2026
Merged

ggerganov merged 1 commit into
masterfrom
gg/rpc-stronger-get-alloc-size

Conversation

@ggerganov

Copy link
Copy Markdown
Member

Overview

Some backends (Metal, SYCL, WebGPU) require additional memory for fleeting data for certain ops, which is reflected in their get_alloc_size implementations. The RPC backend had a hardcoded list of such ops (FLASH_ATTN_EXT, MUL_MAT_ID): if a backend started expanding the alloc size of another op and the list was not updated, the RPC client would under-allocate.

  • add ggml_backend_op_alloc_size_may_expand() to the backend utils, returning true for the ops that expand the alloc size in any backend (FLASH_ATTN_EXT, MUL_MAT_ID, CUMSUM, ARGSORT, TOP_K)
  • add a GGML_ASSERT in ggml_backend_buft_get_alloc_size() catching a backend that expands the alloc size of a compute op that is not listed in the helper
  • use the helper in the RPC backend instead of the hardcoded list, which also fixes the previously missing CUMSUM/ARGSORT/TOP_K for e.g. a Metal server

Additional information

The op list was derived by auditing all get_alloc_size implementations in the tree. All relevant sites are marked with a [TAG_ALLOC_SIZE_EXPAND] comment so that they can be found easily when adding a new op.

The assert exempts non-compute tensors (ggml_op_is_empty), since type-dependent expansions (e.g. quantized weight padding) apply to weights regardless of op.

ref: #15966

Requirements

some backends (Metal, SYCL, WebGPU) require additional memory for
fleeting data for certain ops, which is reflected in their
get_alloc_size implementations.

add ggml_backend_op_alloc_size_may_expand() to the backend utils,
listing these ops, and assert in ggml_backend_buft_get_alloc_size
that a backend expanding the alloc size of a compute op only does so
for ops listed in the helper.

use the helper in the RPC backend to decide whether to query the
remote server for the actual alloc size, instead of a hardcoded list.

Assisted-by: pi:llama.cpp/Qwen3.8-27B
@ggerganov
ggerganov marked this pull request as ready for review August 29, 2026 11:58
@ggerganov
ggerganov requested a review from a team as a code owner August 29, 2026 11:58
@ggerganov

ggerganov commented Aug 29, 2026 •

Copy link
Copy Markdown
Member Author

This fixes Qwen4 garbage generation when running over RPC with Metal.

@github-actions github-actions Bot added the ggml changes relating to the ggml tensor library for machine learning label Aug 29, 2026
@ggerganov
ggerganov merged commit 73f56d1 into master Aug 30, 2026
28 of 34 checks passed
@ggerganov
ggerganov deleted the gg/rpc-stronger-get-alloc-size branch August 30, 2026 06:17
danielhanchen added a commit to unslothai/llama.cpp that referenced this pull request Aug 31, 2026
The pin was 50 commits behind the PR head and sat before "Add MTP support",
so the nightly shipped GLM-5-Next without the NextN draft head, without the
master merge, and without the pooled-key shift fix.

Verified against b10705 by replaying the resolve step: the new commit fetches
from ggml-org, is a commit of ggml-org#27754, and merges onto the base plus the pins
listed before it with no conflict.

Unrelated, and not fixed here: ggml-org#25731 stops merging at b10705.
Upstream ggml-org#27960 touched ggml/src/ggml-rpc/ggml-rpc.cpp, which the Inkling
branch also edits, and additive_merge.py correctly refuses it. It merges on
b10698, the base of the last shipped nightly, so the next run on a newer base
will fail there until that branch is merged forward.
ggerganov added a commit that referenced this pull request Sep 4, 2026
* ggml : rename and make private ggml_op_alloc_size_may_expand() (ggml/0)

cont #27960

* ggml : bump version to 0.23.0 (ggml/1618)

* sync : ggml
@BrewTestBot BrewTestBot mentioned this pull request Sep 4, 2026
1 task done
pull Bot pushed a commit to joyshmitz/whisper.cpp that referenced this pull request Sep 4, 2026
alin-o pushed a commit to alin-o/llama.cpp-qwen38 that referenced this pull request Sep 6, 2026
* ggml : rename and make private ggml_op_alloc_size_may_expand() (ggml/0)

cont ggml-org/llama.cpp#27960

* ggml : bump version to 0.23.0 (ggml/1618)

* sync : ggml
alin-o pushed a commit to alin-o/llama.cpp-qwen38 that referenced this pull request Sep 6, 2026
* ggml : rename and make private ggml_op_alloc_size_may_expand() (ggml/0)

cont ggml-org/llama.cpp#27960

* ggml : bump version to 0.23.0 (ggml/1618)

* sync : ggml
alin-o pushed a commit to alin-o/llama.cpp-qwen38 that referenced this pull request Sep 6, 2026
* ggml : rename and make private ggml_op_alloc_size_may_expand() (ggml/0)

cont ggml-org/llama.cpp#27960

* ggml : bump version to 0.23.0 (ggml/1618)

* sync : ggml
thecodacus pushed a commit to thecodacus/llama.cpp that referenced this pull request Sep 7, 2026
…-org#27960)

some backends (Metal, SYCL, WebGPU) require additional memory for
fleeting data for certain ops, which is reflected in their
get_alloc_size implementations.

add ggml_backend_op_alloc_size_may_expand() to the backend utils,
listing these ops, and assert in ggml_backend_buft_get_alloc_size
that a backend expanding the alloc size of a compute op only does so
for ops listed in the helper.

use the helper in the RPC backend to decide whether to query the
remote server for the actual alloc size, instead of a hardcoded list.

Assisted-by: pi:llama.cpp/Qwen3.8-27B
x1250 pushed a commit to x1250/llama.cpp that referenced this pull request Sep 9, 2026
* ggml : rename and make private ggml_op_alloc_size_may_expand() (ggml/0)

cont ggml-org#27960

* ggml : bump version to 0.23.0 (ggml/1618)

* sync : ggml
milpster pushed a commit to milpster/gfx906-llama-cpp that referenced this pull request Sep 10, 2026
* ggml : rename and make private ggml_op_alloc_size_may_expand() (ggml/0)

cont ggml-org/llama.cpp#27960

* ggml : bump version to 0.23.0 (ggml/1618)

* sync : ggml
zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
…-org#27960)

some backends (Metal, SYCL, WebGPU) require additional memory for
fleeting data for certain ops, which is reflected in their
get_alloc_size implementations.

add ggml_backend_op_alloc_size_may_expand() to the backend utils,
listing these ops, and assert in ggml_backend_buft_get_alloc_size
that a backend expanding the alloc size of a compute op only does so
for ops listed in the helper.

use the helper in the RPC backend to decide whether to query the
remote server for the actual alloc size, instead of a hardcoded list.

Assisted-by: pi:llama.cpp/Qwen3.8-27B
zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
* ggml : rename and make private ggml_op_alloc_size_may_expand() (ggml/0)

cont ggml-org#27960

* ggml : bump version to 0.23.0 (ggml/1618)

* sync : ggml
pl752 pushed a commit to pl752/llama.cpp that referenced this pull request Sep 15, 2026
…-org#27960)

some backends (Metal, SYCL, WebGPU) require additional memory for
fleeting data for certain ops, which is reflected in their
get_alloc_size implementations.

add ggml_backend_op_alloc_size_may_expand() to the backend utils,
listing these ops, and assert in ggml_backend_buft_get_alloc_size
that a backend expanding the alloc size of a compute op only does so
for ops listed in the helper.

use the helper in the RPC backend to decide whether to query the
remote server for the actual alloc size, instead of a hardcoded list.

Assisted-by: pi:llama.cpp/Qwen3.8-27B
pl752 pushed a commit to pl752/llama.cpp that referenced this pull request Sep 15, 2026
* ggml : rename and make private ggml_op_alloc_size_may_expand() (ggml/0)

cont ggml-org#27960

* ggml : bump version to 0.23.0 (ggml/1618)

* sync : ggml
zsogitbe pushed a commit to zsogitbe/llama.cpp that referenced this pull request Sep 17, 2026
…-org#27960)

some backends (Metal, SYCL, WebGPU) require additional memory for
fleeting data for certain ops, which is reflected in their
get_alloc_size implementations.

add ggml_backend_op_alloc_size_may_expand() to the backend utils,
listing these ops, and assert in ggml_backend_buft_get_alloc_size
that a backend expanding the alloc size of a compute op only does so
for ops listed in the helper.

use the helper in the RPC backend to decide whether to query the
remote server for the actual alloc size, instead of a hardcoded list.

Assisted-by: pi:llama.cpp/Qwen3.8-27B
zsogitbe pushed a commit to zsogitbe/llama.cpp that referenced this pull request Sep 17, 2026
* ggml : rename and make private ggml_op_alloc_size_may_expand() (ggml/0)

cont ggml-org#27960

* ggml : bump version to 0.23.0 (ggml/1618)

* sync : ggml
Te-eMster pushed a commit to Te-eMster/mx-llama.cpp that referenced this pull request Sep 18, 2026
* ggml : rename and make private ggml_op_alloc_size_may_expand() (ggml/0)

cont ggml-org#27960

* ggml : bump version to 0.23.0 (ggml/1618)

* sync : ggml
Te-eMster pushed a commit to Te-eMster/mx-llama.cpp that referenced this pull request Sep 18, 2026
* ggml : rename and make private ggml_op_alloc_size_may_expand() (ggml/0)

cont ggml-org#27960

* ggml : bump version to 0.23.0 (ggml/1618)

* sync : ggml
Jesssullivan added a commit to Jesssullivan/llama.cpp that referenced this pull request Sep 20, 2026
PLACEHOLDER MESSAGE — rewrite before opening a pull request. llama.cpp
CONTRIBUTING prohibits AI-written commit messages and PR descriptions;
this text was prepared by tooling so the branch could be pushed, and the
author is expected to replace it with their own words (git commit --amend).

ggml_backend_buft_get_alloc_size guards the documented lower bound of the
buffer-type contract with a plain assert(), which is compiled out under
NDEBUG, while the upper bound two lines below is a GGML_ASSERT. A buffer
type that under-reports therefore passes silently in release builds and
only fails later, in the in-place-reuse path, at
ggml-alloc.c GGML_ASSERT(parent_size >= node_size).

Promote the lower bound to GGML_ASSERT so the failure lands on the
buffer type that produced the wrong size. The neighbouring assert already
evaluates ggml_nbytes in release builds, so there is no added cost.

ref: ggml-org#27960
ref: ggml-org#28038
ref: ggml-org#17884

Assisted-by: Claude Opus 5
Jesssullivan added a commit to Jesssullivan/llama.cpp that referenced this pull request Sep 20, 2026
… at ggml_nbytes

PLACEHOLDER MESSAGE — rewrite before opening a pull request. llama.cpp
CONTRIBUTING prohibits AI-written commit messages and PR descriptions;
this text was prepared by tooling so the branch could be pushed, and the
author is expected to replace it with their own words (git commit --amend).

The process-global memo added in ggml-org#18626 keys RPC_CMD_GET_ALLOC_SIZE
results on {device, type, op, op_params, ne} and omits nb. Two tensors
with identical ne but different strides therefore share one entry, and
the second caller receives the first tensor's size. When that size is
smaller than ggml_nbytes, the buffer-type contract documented in ggml-org#27960
and ggml-org#28038 is violated and allocation fails downstream.

Add nb to the cache key and never return less than ggml_nbytes. The memo
is preserved: contiguous tensors, which are the common case, keep the
same hit rate. rpc_tensor already carries nb over the wire, so there is
no protocol or API change and RPC_PROTO_* is untouched.

Reproduced against a local ggml-rpc-server -d CPU: a packed tensor
reports 16384/16384 and a strided view with the same ne reports
nbytes 32512, alloc_size 16384, then aborts; with this change the second
call reports 32512 and the run completes. The regression is folded into
tests/test-rpc-multi-server.

ref: ggml-org#28360
ref: ggml-org#18626
ref: ggml-org#27960

Assisted-by: Claude Opus 5
Jesssullivan added a commit to Jesssullivan/llama.cpp that referenced this pull request Sep 22, 2026
PLACEHOLDER MESSAGE — rewrite before opening a pull request. llama.cpp
CONTRIBUTING prohibits AI-written commit messages and PR descriptions;
this text was prepared by tooling so the branch could be pushed, and the
author is expected to replace it with their own words (git commit --amend).

ggml_backend_buft_get_alloc_size guards the documented lower bound of the
buffer-type contract with a plain assert(), which is compiled out under
NDEBUG, while the upper bound two lines below is a GGML_ASSERT. A buffer
type that under-reports therefore passes silently in release builds and
only fails later, in the in-place-reuse path, at
ggml-alloc.c GGML_ASSERT(parent_size >= node_size).

Promote the lower bound to GGML_ASSERT so the failure lands on the
buffer type that produced the wrong size. The neighbouring assert already
evaluates ggml_nbytes in release builds, so there is no added cost.

ref: ggml-org#27960
ref: ggml-org#28038
ref: ggml-org#17884

Assisted-by: Claude Opus 5
Jesssullivan added a commit to Jesssullivan/llama.cpp that referenced this pull request Sep 22, 2026
… at ggml_nbytes

PLACEHOLDER MESSAGE — rewrite before opening a pull request. llama.cpp
CONTRIBUTING prohibits AI-written commit messages and PR descriptions;
this text was prepared by tooling so the branch could be pushed, and the
author is expected to replace it with their own words (git commit --amend).

The process-global memo added in ggml-org#18626 keys RPC_CMD_GET_ALLOC_SIZE
results on {device, type, op, op_params, ne} and omits nb. Two tensors
with identical ne but different strides therefore share one entry, and
the second caller receives the first tensor's size. When that size is
smaller than ggml_nbytes, the buffer-type contract documented in ggml-org#27960
and ggml-org#28038 is violated and allocation fails downstream.

Add nb to the cache key and never return less than ggml_nbytes. The memo
is preserved: contiguous tensors, which are the common case, keep the
same hit rate. rpc_tensor already carries nb over the wire, so there is
no protocol or API change and RPC_PROTO_* is untouched.

Reproduced against a local ggml-rpc-server -d CPU: a packed tensor
reports 16384/16384 and a strided view with the same ne reports
nbytes 32512, alloc_size 16384, then aborts; with this change the second
call reports 32512 and the run completes. The regression is folded into
tests/test-rpc-multi-server.

ref: ggml-org#28360
ref: ggml-org#18626
ref: ggml-org#27960

Assisted-by: Claude Opus 5
frostyautumnleaf pushed a commit to frostyautumnleaf/llama.cpp that referenced this pull request Oct 5, 2026
…-org#27960)

some backends (Metal, SYCL, WebGPU) require additional memory for
fleeting data for certain ops, which is reflected in their
get_alloc_size implementations.

add ggml_backend_op_alloc_size_may_expand() to the backend utils,
listing these ops, and assert in ggml_backend_buft_get_alloc_size
that a backend expanding the alloc size of a compute op only does so
for ops listed in the helper.

use the helper in the RPC backend to decide whether to query the
remote server for the actual alloc size, instead of a hardcoded list.

Assisted-by: pi:llama.cpp/Qwen3.8-27B
frostyautumnleaf pushed a commit to frostyautumnleaf/llama.cpp that referenced this pull request Oct 5, 2026
* ggml : rename and make private ggml_op_alloc_size_may_expand() (ggml/0)

cont ggml-org#27960

* ggml : bump version to 0.23.0 (ggml/1618)

* sync : ggml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant