Skip to content

ggml : add alloc_buffer_n to buffer type interface - #23671

Merged
ggerganov merged 9 commits into
masterfrom
gg/ggml-alloc-refactor-meta
Oct 2, 2026
Merged

ggerganov merged 9 commits into
masterfrom
gg/ggml-alloc-refactor-meta

Conversation

@ggerganov

@ggerganov ggerganov commented May 25, 2026 •

Copy link
Copy Markdown
Member

Overview

cont #19378

The ggml_backend_meta_alloc_ctx_tensors_from_buft was a temporary workaround. This patch avoids the function by extending the buffer type interface with an API that allocates a backend buffer from a list of tensors. This decouples the ggml-alloc from meta backend specifics and allows more flexible buffer allocations in the future.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. pi:llama.cpp/DeepSeek-V4-Flash-Vision-Exp + pi:llama.cpp/Qwen3.8-27B

@github-actions github-actions Bot added Nvidia GPU Issues specific to Nvidia GPUs Vulkan Issues specific to the Vulkan backend ggml changes relating to the ggml tensor library for machine learning SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language Apple Metal https://en.wikipedia.org/wiki/Metal_(API) Ascend NPU issues specific to Ascend NPUs OpenCL Issues specific to the OpenCL backend IBM zDNN issues specific to IBM zDNN Accelerator Hexagon OpenVINO WebGPU labels May 25, 2026
michaelw9999 added a commit to michaelw9999/advanced-gguf-quantizer that referenced this pull request May 26, 2026
Port ggml-org/llama.cpp#23671 into advanced-gguf-quantizer as one local integration commit.

Includes upstream commits 0743dce657dc9901ee56734dbce571cf0a6abb8d (add alloc_buffer_n to buffer type interface) and 3f9bcab883c84c6336e100ce5d7408bb4bf47ac9 (fix cur_buf_size after buffer flush).
@ggerganov
ggerganov force-pushed the gg/ggml-alloc-refactor-meta branch from 3f9bcab to 7c7be0f Compare June 15, 2026 13:06
@github-actions github-actions Bot added the CUDA Related to the CUDA backend label Jun 15, 2026
@ggerganov
ggerganov force-pushed the gg/ggml-alloc-refactor-meta branch 2 times, most recently from d2ebf49 to a75c427 Compare September 19, 2026 10:11
@github-actions github-actions Bot added the testing Everything test related label Sep 19, 2026
@ggerganov
ggerganov marked this pull request as ready for review September 20, 2026 07:06
@ggerganov
ggerganov requested review from a team, marty1885 and wine99 as code owners September 20, 2026 07:06
@max-krasnyansky

Copy link
Copy Markdown
Member

Ah. I see that it's possible to return the multi-buffer where different tensors map to different sub-buffer. Thanks for adding that alloc-plan based implementation. Let me know if I misread that :)

Yes. We don't currently have a specific application of this new API apart from the meta backend. So would be nice in case you find a use case in the hexagon backend in order to give us more confidence that this change is worth it.

Sounds good. I'll give that a shot a bit later today.

Sorry for the delay. Seems to work as expected.

Here is the first cut where we split large tensors into standalone buffersmaxk/hexagon-alloc-buffer-n.
Feel free to pull that branch into this PR or we can merge this and I'll start a separate one.

@max-krasnyansky

Copy link
Copy Markdown
Member

@ggerganov @JohannesGaessler any reason we're not merging this yet?
I'm going to add some more logic to the ggml-hexagon alloc_buffer_n.
So it'd be good to merge this.

Add alloc_buffer_n method to ggml_backend_buffer_type_i
interface, with a public API ggml_backend_buft_alloc_buffer_n.

- Default implementation in ggml-backend.cpp handles multi-buffer
  splitting and tensor allocation via ggml_tallocr
- Meta buffer type provides custom implementation that creates
  per-device sub-contexts and delegates to simple buffer types
- ggml_backend_alloc_ctx_tensors_from_buft now collects tensors
  into a list and delegates to the new API
- Remove temporary ggml_backend_meta_alloc_ctx_tensors_from_buft
- Add NULL alloc_buffer_n to all existing buffer type
  interfaces (cpu, metal, openvino, hexagon, webgpu, zdnn, virtgpu, repack)

Assisted-by: llama.cpp:local pi
Assisted-by: pi:llama.cpp/DeepSeek-V4-Flash-Vision-Exp
Assisted-by: pi:llama.cpp/DeepSeek-V4-Flash-Vision-Exp
Assisted-by: pi:llama.cpp/Qwen3.8-27B
- restore GGML_LOG_ERROR on buffer alloc / tensor init failure in the
  default impl (name the failing tensor)
- check the malloc result and drop the _impl indirection in
  ggml_backend_alloc_ctx_tensors_from_buft
- remove comments that restate the code
- fix the TAG_ALLOC_SHARED_BUFFER_SPLIT typo

Assisted-by: pi:llama.cpp/Qwen3.8-27B
- Add ggml_backend_buft_get_alloc_size_n public API
- Add optional get_alloc_size_n callback to ggml_backend_buffer_type_i
- Share tensor->buffer planning between alloc_buffer_n default and get_alloc_size_n default
- Replace unchecked realloc with std::vector in alloc_buffer_n default
- Make ggml_backend_alloc_ctx_tensors_from_buft_size use the new API
- Add test-alloc coverage for get_alloc_size_n

Assisted-by: pi:llama.cpp/DeepSeek-V4-Flash-Vision-Exp
@ggerganov
ggerganov force-pushed the gg/ggml-alloc-refactor-meta branch from aa7ee0b to d211167 Compare October 2, 2026 06:23
@ggerganov

ggerganov commented Oct 2, 2026 •

Copy link
Copy Markdown
Member Author

Sorry for the delay - I wanted to also implement get_alloc_size_n for the meta backend, but will do it in a follow-up PR to not block this. Merging on green.

@ggerganov
ggerganov merged commit 631109b into master Oct 2, 2026
66 of 69 checks passed
novkien added a commit to novkien/llama.cpp-fork that referenced this pull request Oct 2, 2026
Clean upstream sync past 254b177. Relevant to the fork's deployment:

- 4e2713c qwen4exp : optimize mask constructions (ggml-org#29824)
- 631109b ggml : add alloc_buffer_n to the buffer type interface (ggml-org#23671)

plus sycl / vulkan / opencl kernel work, llama warning and abort cleanups,
and a new server /v1/systemone API (ggml-org#29818).

Why it matters here: adopting upstream's Qwen4Exp MTP (ggml-org#29761) costs roughly
+2.6 GB of compute buffers per device on the Qwen3.8-Flash-Next route, because
the fixed attention path (ggml-org#29751) builds a k-pool input per QSA layer and makes
the indexer cache track the attention cache cell for cell. ggml-org#29824 reduces the
mask construction cost of that path.

Fork-own work (RAM prompt-cache retention, selective CUDA P2P transport, server
prefill/decode phase isolation, DFlash M-RoPE inference, docs, tests) is
unchanged.
hoivb612 pushed a commit to hoivb612/llama.cpp that referenced this pull request Oct 3, 2026
Merge the latest ggml-dx12-main work (linalg / flash-attn shader set,
autotune header, linalg-bench tooling) and adapt all three DX12 backends
to the buffer-type interface change from 631109b (ggml-org#23671), which
inserted the optional alloc_buffer_n and get_alloc_size_n members and
bumped GGML_BACKEND_API_VERSION from 2 to 3.

- ggml-dx12, ggml-dx12x, ggml-dx12-main: add the two new nullptr slots to
  dx12_buffer_type_interface so the positional initializers line up with
  the new member order. Both new members are optional with documented
  defaults, so behaviour is unchanged.
- ggml-dx12-main: forward-declare ggml_backend_dx12_set_env_refresh and
  ggml_backend_dx12_set_flag_sink ahead of dx12_reg_get_proc_address.
  They are defined later in the same file and are deliberately absent
  from ggml/include/ggml-dx12.h, being reached via get_proc_address.

graph_optimize is nullptr in ggml-dx12 and ggml-dx12x, so upstream's
signature change to that callback affected only ggml-dx12-main.

Build-verified with Ninja/Release under MSVC: GGML_DX12_MAIN=ON,
GGML_DX12=ON and GGML_DX12X=ON each configure and compile cleanly with
zero warnings in the DX12 sources. Not yet exercised on device.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 2d7f2fcd-a611-433c-a522-a341fb127413
hoivb612 pushed a commit to hoivb612/llama.cpp that referenced this pull request Oct 3, 2026
Flow-down of b43b8c1, which merges the latest ggml-dx12-main work and
adapts all three DX12 backends to the buffer-type interface change from
631109b (ggml-org#23671) that bumped GGML_BACKEND_API_VERSION from 2 to 3.

ggml/src/ggml-dx12, ggml-dx12-main and ggml-dx12x are maintained on
hv/b612_100326 and mirrored here verbatim, so the three directories are
replaced wholesale rather than merged. Their tree hashes match b43b8c1
exactly. No files outside ggml/src/ggml-dx12* are touched.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 2d7f2fcd-a611-433c-a522-a341fb127413
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 8, 2026
* ggml : add `alloc_buffer_n` to buffer type interface

Add alloc_buffer_n method to ggml_backend_buffer_type_i
interface, with a public API ggml_backend_buft_alloc_buffer_n.

- Default implementation in ggml-backend.cpp handles multi-buffer
  splitting and tensor allocation via ggml_tallocr
- Meta buffer type provides custom implementation that creates
  per-device sub-contexts and delegates to simple buffer types
- ggml_backend_alloc_ctx_tensors_from_buft now collects tensors
  into a list and delegates to the new API
- Remove temporary ggml_backend_meta_alloc_ctx_tensors_from_buft
- Add NULL alloc_buffer_n to all existing buffer type
  interfaces (cpu, metal, openvino, hexagon, webgpu, zdnn, virtgpu, repack)

Assisted-by: llama.cpp:local pi

* cont : fix `cur_buf_size` init after flushing a buffer

* ggml : add TODO tag for shared buffer split logic

Assisted-by: pi:llama.cpp/DeepSeek-V4-Flash-Vision-Exp

* tests : add alloc_buffer_n coverage

Assisted-by: pi:llama.cpp/DeepSeek-V4-Flash-Vision-Exp

* cont : fix compile warnings

* tests : add descriptions for alloc_buffer_n tests

Assisted-by: pi:llama.cpp/Qwen3.8-27B

* ggml : address review comments on alloc_buffer_n

- restore GGML_LOG_ERROR on buffer alloc / tensor init failure in the
  default impl (name the failing tensor)
- check the malloc result and drop the _impl indirection in
  ggml_backend_alloc_ctx_tensors_from_buft
- remove comments that restate the code
- fix the TAG_ALLOC_SHARED_BUFFER_SPLIT typo

Assisted-by: pi:llama.cpp/Qwen3.8-27B

* ggml : add get_alloc_size_n to buffer type interface

- Add ggml_backend_buft_get_alloc_size_n public API
- Add optional get_alloc_size_n callback to ggml_backend_buffer_type_i
- Share tensor->buffer planning between alloc_buffer_n default and get_alloc_size_n default
- Replace unchecked realloc with std::vector in alloc_buffer_n default
- Make ggml_backend_alloc_ctx_tensors_from_buft_size use the new API
- Add test-alloc coverage for get_alloc_size_n

Assisted-by: pi:llama.cpp/DeepSeek-V4-Flash-Vision-Exp

* cont : report malloc failure

(cherry picked from commit 631109b)
asf0 pushed a commit to asf0/strix-llama.cpp that referenced this pull request Oct 12, 2026
halo-box/llama.cpp is archived, so its last master comes in once: the
2026-10-07 upstream sync (halo halo-box#34) with its resolutions, plus the archival
README of that repository is not taken (strix keeps its own README).

Resolution rules: MTP follows upstream's design; everywhere else strix's
side wins where the two collide, and upstream's non-conflicting changes stay.

- qwen4exp: strix's QSA stack (selected-key / WMMA block selection,
  incremental pooled keys, PLE prefetch, HIP prefill work) is kept whole in
  qwen4exp.cpp and the qwen4exp struct of models.h. strix's graph_mtp is kept:
  it builds the same draft as upstream's ggml-org#29761 (eh_proj of [enorm(e);
  hnorm(h)], the draft's own nextn.hc_head_* mixer, sparse QSA attention) and
  also loads shared sidecars without embeddings / LM head. The converter,
  gguf-py tensor maps and arch tables are upstream's, so new conversions
  follow upstream's layout; strix loads both.
- llama-memory-hybrid-idx: both APIs kept side by side - strix's qsa_* for
  qwen4exp and upstream's k-pool for the new GLM5Next (ggml-org#27773 and follow-ups).
  strix's context set_input_qsa, which upstream deleted, is restored.
- llama-graph: strix's in-place gated-delta-net state path is kept; upstream's
  single recurrent-state gather (ggml-org#29856, avoids a realloc abort under
  GGML_SCHED_NO_REALLOC) is reverted on this path and needs porting.
- vulkan: strix's flash_attn.comp and lightning_indexer.comp are kept whole;
  upstream's FA shmem out-of-bounds fix (ggml-org#29988) is ported into strix's
  shader. Upstream's lightning indexer rewrite (ggml-org#29825) is reverted on the
  Vulkan host side to match strix's shader; its CUDA/Metal parts stay.
  Upstream's sparse FA path (ggml-org#29639) stays out: strix deleted its shader.
- ggml-alloc: upstream's alloc_buffer_n refactor (ggml-org#23671) replaces strix's
  backport of the view-init fix; strix's regression test is kept.
- cuda: strix's RDNA3.5 paths kept; upstream's shared-expert MoE fusion added
  next to strix's fused GDN decode, with both sets of fusion args.
- server: speculative accept keeps strix's replay branch, then upstream's
  rejection sampling at temp > 0.
- tests: both sides' new tests kept (rollback table gets both columns,
  save-load-state test 10 is strix's, 11 upstream's); strix's rollback test
  migrated to common_batch / llama_process.
- fusion.yml kept (upstream removed it), flash_attn_sparse_compact.comp
  stays deleted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alex-spacemit pushed a commit to spacemit-com/llama.cpp that referenced this pull request Oct 15, 2026
- ggml-spacemit: add alloc_buffer_n/get_alloc_size_n buffer type fields
  (upstream ggml-org#23671) and set mmap_support=true so load_mode AUTO keeps
  mmapping the model in host memory before copying into spine buffers
- server-media: replace removed params.vocoder.speaker_file with
  params.tts_speaker_file (upstream ggml-org#26254); pass mtmd_helper_init_opt
  through process_prompt for the new bitmap_init_from_buf signature
- server-context: add_prompt_mixed operates on server_media_chunk, use
  chunk->mtmd accessors and reject SMT embd chunks in mixed batches;
  pass init_opt at both process_prompt call sites
- server-decision: use server_media_chunk wrapper API for chunk
  type/n_tokens instead of raw mtmd_input_chunk accessors
- server-tts/server: port request parsing to common_json (contains/at/
  items instead of nlohmann find/end), catch common_json_error, and
  use ctx_http.join()/listening_addresses from the new http context
gaetan-puleo pushed a commit to rcfa/strix-llama.cpp that referenced this pull request Oct 16, 2026
When ggml_backend_alloc_ctx_tensors_from_buft splits allocation on
buft max_size, a view-only tail at the end of the context could skip
the final alloc_tensor_range. Persistent views (e.g. KV k_stream /
v_stream) were then left without ggml_backend_view_init.

Allocate only parent tensors in alloc_tensor_range and initialize all
views in a final pass over the context after all splits complete.

(cherry picked from commit 335c0989ed71e0207b212da063a40a5845a00582)
(cherry picked from commit 14aa71a)

[rebase: upstream moved this allocation into ggml_backend_buft_alloc_buffer_n_default (ggml-org#23671); the fix is applied there: plan items allocate parents only, views are initialized in a final pass]
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 24, 2026
* ggml : add `alloc_buffer_n` to buffer type interface

Add alloc_buffer_n method to ggml_backend_buffer_type_i
interface, with a public API ggml_backend_buft_alloc_buffer_n.

- Default implementation in ggml-backend.cpp handles multi-buffer
  splitting and tensor allocation via ggml_tallocr
- Meta buffer type provides custom implementation that creates
  per-device sub-contexts and delegates to simple buffer types
- ggml_backend_alloc_ctx_tensors_from_buft now collects tensors
  into a list and delegates to the new API
- Remove temporary ggml_backend_meta_alloc_ctx_tensors_from_buft
- Add NULL alloc_buffer_n to all existing buffer type
  interfaces (cpu, metal, openvino, hexagon, webgpu, zdnn, virtgpu, repack)

Assisted-by: llama.cpp:local pi

* cont : fix `cur_buf_size` init after flushing a buffer

* ggml : add TODO tag for shared buffer split logic

Assisted-by: pi:llama.cpp/DeepSeek-V4-Flash-Vision-Exp

* tests : add alloc_buffer_n coverage

Assisted-by: pi:llama.cpp/DeepSeek-V4-Flash-Vision-Exp

* cont : fix compile warnings

* tests : add descriptions for alloc_buffer_n tests

Assisted-by: pi:llama.cpp/Qwen3.8-27B

* ggml : address review comments on alloc_buffer_n

- restore GGML_LOG_ERROR on buffer alloc / tensor init failure in the
  default impl (name the failing tensor)
- check the malloc result and drop the _impl indirection in
  ggml_backend_alloc_ctx_tensors_from_buft
- remove comments that restate the code
- fix the TAG_ALLOC_SHARED_BUFFER_SPLIT typo

Assisted-by: pi:llama.cpp/Qwen3.8-27B

* ggml : add get_alloc_size_n to buffer type interface

- Add ggml_backend_buft_get_alloc_size_n public API
- Add optional get_alloc_size_n callback to ggml_backend_buffer_type_i
- Share tensor->buffer planning between alloc_buffer_n default and get_alloc_size_n default
- Replace unchecked realloc with std::vector in alloc_buffer_n default
- Make ggml_backend_alloc_ctx_tensors_from_buft_size use the new API
- Add test-alloc coverage for get_alloc_size_n

Assisted-by: pi:llama.cpp/DeepSeek-V4-Flash-Vision-Exp

* cont : report malloc failure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Apple Metal https://en.wikipedia.org/wiki/Metal_(API) Ascend NPU issues specific to Ascend NPUs CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning Hexagon IBM zDNN issues specific to IBM zDNN Accelerator Nvidia GPU Issues specific to Nvidia GPUs OpenCL Issues specific to the OpenCL backend OpenVINO SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language testing Everything test related Vulkan Issues specific to the Vulkan backend WebGPU

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants