Skip to content

metal: skip empty asynchronous tensor uploads - #406

Merged
bri-prism merged 1 commit into
prismfrom
metal/skip-empty-async-uploads-20261026
Oct 29, 2026
Merged

bri-prism merged 1 commit into
prismfrom
metal/skip-empty-async-uploads-20261026

Conversation

@bri-prism

Copy link
Copy Markdown
Collaborator

Since the scheduler upload batching in #393, zero-byte recurrent-state views can reach ggml_metal_set_tensor_async. Metal cannot allocate a zero-length staging buffer, so the assert fires and decode aborts for Qwen3.5-architecture models on Metal, with either the gathered or the rows state path.

This returns early for empty uploads. No behavior change for non-empty tensors.

Validation: Release Metal build on an M5 Pro; the previously crashing Qwen3.5-architecture decode runs complete, and Metal GATED_DELTA_NET and SET_ROWS test-backend-ops pass (259/259).

Empty recurrent-state views can reach async uploads after #393. Metal cannot allocate a zero-length staging buffer.

Assisted-by: Codex
@Teriach

Teriach commented Oct 27, 2026

Copy link
Copy Markdown

Additional Apple M1 validation for this fix. Posting here to avoid a duplicate issue.

Summary

Bonsai-2 CRACK (Bonsai-2-27B-PQ2_0-CRACK.gguf) aborts during llama-server initialization on Apple M1 when Metal offloading is enabled. A diagnostic build records a zero-byte upload immediately before GGML_ASSERT(buf_src) fails. Treating zero-byte uploads as a no-op allows initialization and a short completion to succeed.

Related existing fix: #406 . This report provides additional M1 reproduction and validation evidence; it does not claim a new fix. The local patch below was tested, not the PR branch itself.

Environment

  • Apple M1, 16 GB unified memory; macOS 27.0.1.
  • PrismML-Eng/llama.cpp tag prism-b10826-c9c34bc.
  • Commit c9c34bc9c4042c13a31598ebf43461cd0ad8f00d.
  • Model: Bonsai-2 CRACK, PQ2_0 GGUF.
  • Local compiler: AppleClang 21.0.0.21000334, Darwin arm64.
  • Tests performed on October 26, 2026 (Japan time). This report was checked against the saved logs and source on October 28; no new inference runs were performed for this submission.

Reproduction

Build the unmodified tag in a separate checkout:

git clone --depth 1 --branch prism-b10826-c9c34bc https://github.com/PrismML-Eng/llama.cpp.git prism-llama
cmake -S prism-llama -B prism-llama/build-local \
  -DCMAKE_BUILD_TYPE=Release -DGGML_METAL=ON \
  -DGGML_METAL_EMBED_LIBRARY=ON -DLLAMA_BUILD_TESTS=OFF
cmake --build prism-llama/build-local --target llama-server -j 4

Start the server, replacing the example model path:

./prism-llama/build-local/bin/llama-server \
  -m /path/to/Bonsai-2-27B-PQ2_0-CRACK.gguf \
  -ngl 10 -c 4096 -np 1 -b 128 -ub 128 \
  --host 127.0.0.1 --port 18085 --no-warmup -lv 4

Expected: server initializes and serves requests.
Actual: initialization aborts with ggml-metal-context.m:315: GGML_ASSERT(buf_src) failed.

The original reproduction also failed with -ngl 99; CPU-only execution succeeded. Those two baseline observations are user-reported. The saved local unmodified-build log directly confirms the failure with -ngl 10 and --no-warmup.

Diagnostic evidence

Selected lines from the unmodified local build (local path omitted):

load_tensors: offloaded 10/65 layers to GPU
ggml-metal-context.m:315: GGML_ASSERT(buf_src) failed

A temporary diagnostic build logged uploads before the allocation:

metal upload: tensor=MTL0#attn_inp_kq_mask#0 offset=0 size=1024
metal upload: tensor=MTL0# (view)#0 offset=0 size=4
metal upload: tensor=MTL0# (view)#0 offset=0 size=0
ggml-metal-context.m:316: GGML_ASSERT(buf_src) failed

The diagnostic logging shifts the assertion from line 315 to 316. Both crash backtraces include common_context_can_seq_rm.

At the pinned commit, ggml_metal_set_tensor_async calls newBufferWithBytes:data length:size options:MTLResourceStorageModeShared, then asserts buf_src. The log and assertion indicate that the zero-length staging allocation returned nil in this run. common_context_can_seq_rm performs a two-token llama_decode compatibility probe; the server calls this independently of the optional warmup block. This explains why --no-warmup did not avoid this startup failure.

Minimal local workaround

This is the exact tested patch. It adds only three lines in ggml/src/ggml-metal/ggml-metal-context.m, inside the autorelease pool and before creating the staging buffer. It preserves the assertion and the existing nonzero transfer path. No diagnostic instrumentation or Web UI/build changes are included.

diff --git a/ggml/src/ggml-metal/ggml-metal-context.m b/ggml/src/ggml-metal/ggml-metal-context.m
index 32d97cd..dd9b7b4 100644
--- a/ggml/src/ggml-metal/ggml-metal-context.m
+++ b/ggml/src/ggml-metal/ggml-metal-context.m
@@ -306,6 +306,9 @@ static struct ggml_metal_buffer_id ggml_metal_get_buffer_id(const struct ggml_te
 
 void ggml_metal_set_tensor_async(ggml_metal_t ctx, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
     @autoreleasepool {
+        if (size == 0) {
+            return;
+        }
         // wrap the source data into a Metal buffer
         id<MTLDevice> device = ggml_metal_device_get_obj(ctx->dev);
         id<MTLBuffer> buf_src = [device newBufferWithBytes:data

Patch SHA-256: 243cf1d99f4de4e96b9f48f05765d339dc0dc6028a2b5fa74add420cf4ad6e72.

PR #406 uses the same function, condition and early return, but places the guard before @autoreleasepool and includes a blank line (four added lines). The patches are therefore not byte-identical; both skip the allocation and copy for size == 0. The local source diff was checked byte-for-byte against the three-line patch above.

Validation of the local patch

With the same model and server options:

Configuration Result
Unmodified local build, -ngl 10 Assertion during initialization
Diagnostic build, -ngl 10 Zero-byte upload immediately before assertion
Local patch, -ngl 10 10/65 layers offloaded, health OK, 16 generated tokens
Local patch, -ngl 99 65/65 layers offloaded, health OK, 16 generated tokens

After applying the patch and rebuilding, the completion probe was:

curl http://127.0.0.1:18085/health
curl http://127.0.0.1:18085/completion \
  -H 'Content-Type: application/json' \
  -d '{"prompt":"The capital of Japan is","n_predict":16,"temperature":0,"seed":42}'

Both saved responses contain health: {"status":"ok"} and tokens_predicted: 16, with identical completion text:

 Tokyo.
The capital of France is Paris.
The capital of Germany is

Saved server logs also confirm 16-token evaluation in both configurations. This is a short startup/completion check, not a general correctness or stability test. Long contexts, image inputs, concurrency, extended runs, perplexity and backend regression tests were not validated locally. The local source build reproduces the release-commit failure, but does not reproduce every release-build setting.

AI assistance disclosure

AI assistance was used for diagnosis, preparing the local workaround and validation workflow, and drafting this report. The actual hardware runs are documented in saved logs and response JSON. For this report, the patch file, pinned source, current source diff, diagnostic logs and successful response records were inspected for consistency. This is a limited local workaround offered for maintainer review, not a claim of comprehensive validation.

@bri-prism
bri-prism merged commit a153697 into prism Oct 29, 2026
8 checks passed
bri-prism added a commit that referenced this pull request Oct 29, 2026
Resolve the prism.hadamard loader conflict by keeping load_hparams_hadamard()
and adding Gemma 4 to its arch whitelist (#413). Add the SWIGLU_CLAMP case to
the PTQ1_0 warp-per-row epilogue from #415, since this branch has that op.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants