Skip to content

sched: an in-place node and its storage have to share one backend (engine #920) - #190

Merged
bong-water-water-bong merged 2 commits into
1bit/hrx-vulkan-patchedfrom
1bit/i920-inplace-storage
Nov 4, 2026
Merged

bong-water-water-bong merged 2 commits into
1bit/hrx-vulkan-patchedfrom
1bit/i920-inplace-storage

Conversation

@bong-water-water-bong

@bong-water-water-bong bong-water-water-bong commented Nov 4, 2026 •

Copy link
Copy Markdown

sched: an in-place node and its storage have to share one backend (engine ggml-org#920)

Root cause

ggml_backend_sched_split_graph decides placement in five passes. Pass 4 is the only pass that
consults a node's view parent, and it does so only while the node is still unassigned:

if (node->view_src != NULL && *cur_backend_id == -1) {   // ggml-backend.cpp:1227 (before this change)

An in-place node — SET_ROWS, or any *_inplace op, which ggml_set_rows() builds as
ggml_view_tensor(ctx, a) and then renames to GGML_OP_SET_ROWS (ggml.c:3956-3976) — is normally
assigned earlier, from its src[]. So the node ends up on the CPU (no device matcher claims
SET_ROWS) while the buffer it writes in place lives on a device backend, and the CPU writes through
a pointer it cannot map.

Engine ggml-org#920 is the measured instance: the private HIP add-on claims GGML_OP_FILL
(hip_declare_eager_op(GGML_OP_FILL)), so DeepSeek-V4.1's candidate-block mask bm
(src/models/deepseek41.cpp:607) is allocated in HRX device memory, and ggml_set_rows(ctx0, bm, ...)
gets a CPU destination of 0x1a000 = GGML_HRX_FAKE_PTR_BASE (0x1000) + 0x19000. SIGSEGV in
__memcpy_avx512_unaligned_erms under ggml_compute_forward_set_rows.

The class is not specific to HRX or to SET_ROWS: it is reachable in shared scheduler code whenever a
backend claims the producer of an in-place target, and upstream master has the same conditional
view rule (no in-place rule exists there). It is latent upstream only because CUDA/Metal/Vulkan do not
claim FILL.

The change

One new pass between pass 3 and pass 4 (ggml/src/ggml-backend.cpp, +48 lines, no other file). For every
in-place node (view_src != nullptr, not a view op) whose assigned backend differs from the backend of its
storage — the view root, walked up the chain:

  • the storage's memory type is taken from storage->buffer when it is allocated (ggml_backend_buffer_get_type),
    and from sched->bufts[assigned id] only when it is not. An id is a guess, a buffer is a fact — an earlier
    version of this patch aborted on the id and was wrong;
  • host-addressable storage is left alone: any backend can address it, so nothing needs to move;
  • device-local storage that is not yet allocated moves to the node's backend (the FILL then runs on the CPU
    and pass 5 inserts the copy back for its device consumers), provided the node's backend can produce it;
  • device-local storage that already exists, on another backend → GGML_ABORT naming the buffer type and both
    backends, instead of letting the node write through a pointer it cannot map.

Pass 4 is untouched, and all of this happens before pass 5 computes the splits and the copies, so the split logic
honours the move.

Scope note: this does not try to move the node onto the storage's backend even when that backend
supports the in-place op (it does not for SET_ROWS here). Doing so would change placement of ops
that run on the CPU today, and nothing measures that; it is a separate decision.

Why the new abort cannot fire on a legal configuration

The failure mode of a patch like this is a backend whose device memory the CPU can reach — SVM, or host-visible
vidmem on an APU. The check is ggml_backend_buft_is_host, which is the same predicate ggml_backend_buffer_is_host
is built from (ggml-backend.cpp:175-177) and the one the scheduler already uses elsewhere to decide CPU reachability
(ggml-backend.cpp:485, :2276), so a CPU-reachable buffer is skipped before any abort can be reached. Concretely:

  • HRX hands out GGML_HRX_FAKE_PTR_BASE (0x1000) for a device buffer and a real mapped pointer only for a host
    buffer (ggml-hrx.cpp:412-413); the two buffer types are created with host_visible = false and true
    respectively (ggml-hrx.cpp:1485, :1487). Device buffers are therefore never CPU-dereferenceable, with SVM or
    without
    — it is the fake base, not the memory model, that makes Fails to run quantize command ggml-org/llama.cpp#920 a crash, and SVM does not turn the abort on.
  • Vulkan declares is_host = NULL for its device buffer type (ggml-vulkan.cpp:354), and the CPU never writes
    tensor->data on that backend, so the scheduler already treats those buffers as unreachable.

So the abort is confined to memory no CPU op was ever allowed to write, and the case it catches — already allocated on
another backend, not host-addressable — is exactly the one that segfaulted before.

Before / after, on the issue's own reproducer

Reproducer unchanged: ~/lb/d3-setrows-ident.sh — -dev HRX0 -fit off -ngl 0 -c 256 -b 16 -ub 16,
GGML_HRX_HOST_WEIGHT_LIMIT_MB=64, gdb on ggml_compute_forward_set_rows printing dst per call.

arm binary SET_ROWS executed stop
before pin d8bd0ff60, libggml-base.so sha256 5a3cc818… 3 SIGSEGV in __memcpy_avx512_unaligned_erms, dst=0x1a000 = GGML_HRX_FAKE_PTR_BASE + 0x19000
after same tree + this commit, libggml-base.so sha256 26bfd55d… 1359 no fault; the graph then runs into #173's own refusal at the reproducer's 64 MiB host-weight budget (refusing to run: … a 32 MiB weight did not fit), which is a refusal, not a crash

Placement delta from GGML_HRX_REPORT=1, same model, same shapes:

before: placement: 28166 nodes, 886 off HRX (… SET_ROWS x 50 …)                       707 graph splits
after : placement: 28166 nodes, 928 off HRX (… SET_ROWS x 50, …, FILL x 42, …)        707 graph splits

+42 FILL nodes leave HRX, nothing else moves, and the split count is unchanged — one moved FILL per
bm-like in-place target, and the copies the fix needs fit inside splits that already existed.

Same issue shape as TDSV41's arm A (-c 512 -b 32, 64 MiB host-weight budget, plain llama-cli, no gdb), which is the
shape that actually lets this model generate — the crash-tuned -c 256 -b 16 run dies at #173's arena guard before any
token is emitted, in both arms once the budget is raised to 2048 MiB (measured: the unpatched binary refuses at 94 s
with 0 SET_ROWS, so that wall is the budget's, not this patch's):

arm rc generated text stop
before 139 (SIGSEGV) none fault
after 0 > The capital of France is → Paris one token, then 64 MiB host-weight arena refusal (32.8 MiB wanted)

Smaller, model-free form of the same defect (~/lb/i920-mini, 20 lines, 2 s, no model): bm = FILL(base) +
out = SET_ROWS(bm, val, ids) over [8,4] F32, read back and compared:

arm -dev CPU -dev HRX0
before PASS 32/32 SIGSEGV
after PASS 32/32 PASS 32/32

It is not shipped in the PR: no upstream CI backend claims FILL, so the case cannot run there; it is attached to
engine ggml-org#920 instead. Re-run after the 2026-11-04 reboot, on kernel 7.4.0-rc1-1bit (the earlier runs were on
7.3.0-rc4-1bit-perfopt+): same result, before SIGSEGV (rc=139) / after PASS 32/32 on -dev HRX0, both arms
PASS 32/32 on -dev CPU.

Both arms are private snapshots of build/bin (~/wt/i920-bin-before, ~/wt/i920-bin-after), hashed with
sha256sum immediately before and after each run (every hash prefix in this body is a sha256 prefix, not md5); only libggml-base.so.0.18.0 differs between them, so the A/B
isolates this commit. ggml/src/ggml-backend.cpp is byte-identical between the two source trees at the
pin, verified with diff -q.

Test matrix

  • test-backend-ops -o SET_ROWS,FILL -b CPU -j 8: 563/563 both arms.
  • Greedy-text identity on HRX0, both arms, same prompts: Qwen3-0.6B md5 abd8f61b…, Qwen3-4B md5 44bb0256… —
    identical before vs after, with no ggml_backend_sched abort. Throughput is not compared across arms (the box
    carried two servers and other lanes' holds; 4B read 37 vs 64 tok/s in the same window — co-tenant drift).
  • CPU-only run (-dev none, Qwen3-0.6B) clean in both arms.
  • Every device run brackets the binary: sha256sum of llama-cli, libggml-base, libggml-hrx before and after,
    and the snapshot is loaded through LD_LIBRARY_PATH with ldd printed into the log — the first attempt at this A/B
    silently loaded the shared build tree's libggml-base through DT_RUNPATH and measured nothing, so the check is
    now part of the instrument.

CI

This fork's Actions are not scheduled for branch pushes or PRs here (actions/runs?branch=… → 0 runs for the
branch, and gh pr checks reports no checks), so the verification above is local and reproducible with the harnesses
named in it. ggml-base builds clean with -Wall -Wextra at this pin (no new warnings).

Files

  • ggml/src/ggml-backend.cpp — pass 3.5 (only file).

closes engine ggml-org#920

Review nit, addressed by deletion rather than by argument

pi-prs' review (2026-11-04 06:56) noted that storage_backend_id == node_backend_id is tested twice: once inside the
allocated branch, once after it. Both arms continue on equality, so one guard ahead of the branch covers both. That
is 61a7e312e, 3 insertions / 6 deletions, no behaviour change:

if (storage_backend_id == node_backend_id) {
    continue; // one and the same device backend, the write stays in its own memory
}
if (allocated) { GGML_ABORT("... whose buffer is %s, but the node is placed on %s"); }
if (!ggml_backend_supports_op(...)) { GGML_ABORT("... which lives on %s, and %s cannot produce it"); }

All four cases are the same as before: allocated && same → continue, allocated && !same → abort, !allocated && same
→ continue, !allocated && !same → supports_op then adopt. storage_backend_id == -1 is untouched: node_backend_id == -1 already continued above, and an unallocated -1 storage takes the storage_buft == NULL continue before the hoist.

Re-proved on the model-free graph after the rebuild (libggml-base.so sha256 76b8f997…, three distinct arms by hash):

arm libggml-base.so (sha256) -dev CPU -dev HRX0
before 5a3cc818… PASS 32/32 SIGSEGV (rc=139)
after (reviewed head ed2731ee0) 26bfd55d… PASS 32/32 PASS 32/32
after + nit fix (61a7e312e) 76b8f997… PASS 32/32 PASS 32/32

Run under gpu-hold on this boot (kernel 7.4.0-rc1-1bit), 3 s of GPU, die 45 °C, token released.

Re-proved again after the 13:17 reboot of 2026-11-04 (gpu-hold -l sched-i920-boot14, three arms, 3 s of GPU, die
40-41 °C in-run): before sha256 5a3cc818 -> SIGSEGV rc=139 on -dev HRX0, after + nit fix sha256 76b8f997
-> PASS 32/32, both arms PASS 32/32 on -dev CPU. The before arm faulting inside the same hold is the negative
control, and ldd printed per arm confirms each arm loaded its own snapshot's libggml-base. Kernel build is unchanged
(7.4.0-rc1-1bit #1), so this is a fresh-boot confirmation, not a new kernel.

Base moved since the review, checked and inert: 1bit/hrx-vulkan-patched is now a5c32fbf8 — one commit,
convert: restore --dspark … (engine #961) (#187), which touches only convert_hf_to_gguf.py (+4 lines, an argparse
flag). No compiled path changed, so the measurements above still describe the merged result, and no rebase is needed.

…gine ggml-org#920)

pass 4 adopts the view parent's backend only for a node that is still unassigned, so a node
placed from src[] keeps its own backend. an in-place node (SET_ROWS, *_INPLACE) writes into
its view object's buffer, so when that storage is device memory and the node is on the CPU,
the CPU writes through a pointer it cannot map (HRX allocates device buffers at a fake base;
the DeepSeek-V4.1 candidate mask is the measured case in engine ggml-org#920).

new pass 3.5, before pass 4: for an assigned in-place node whose backend differs from its
storage's, test what the storage actually is - an allocated buffer's type when there is one,
the assignment's buffer type when there is not - and skip host-addressable memory, which any
backend can address. device-local storage that is still unallocated moves to the node's
backend; device-local storage that already exists and belongs to another backend aborts
naming the buffer type and both backends instead of writing through an unmapped pointer.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

READY on the scheduler logic; what I can verify without a device is that it changes nothing on the host path, which is the property the pass has to have.

ggml/src/ggml-backend.cpp @ pr/190 (ed2731ee0): rc=0, then a CPU-only build of this head:

$ test-backend-ops -b CPU -o SET_ROWS,GET_ROWS,FILL   ->  Backend CPU: OK, 1/1 backends passed
$ llama-cli -m Qwen3-0.6B-Q4_K_M.gguf -c 256 -n 8 ...  -> runs

Read:

  • Host is a hard skip. Every host buffer type returns early (ggml_backend_buft_is_host), so on a CPU-only graph pass 3.5 is a no-op — that is why the two runs above are unchanged, and it is also the reason this cannot regress a host-only build.
  • Unassigned storage is handled. storage_backend_id == -1 with no buffer gives storage_buft == NULL → continue, so pass 4 still gets to place it from the view parent; and the sched->bufts[storage_backend_id] index is only taken after that guard, so there is no bufts[-1].
  • The two aborts are the correct failure mode rather than a silent CPU write into device memory: an already-allocated foreign buffer cannot be moved, and a device that cannot produce the storage cannot host the in-place write.
  • View root, not the immediate parent, is walked, so a chain of views is covered; ggml_is_view_op(node->op) keeps genuine view ops out.

Nit, not a blocker: the storage_backend_id == node_backend_id test appears twice — once inside the allocated branch and once after it. Correct, just duplicated.

Not verified: the HRX case it exists for (engine ggml-org#920's SET_ROWS on an HRX-allocated bm). That needs the private HIP add-on claiming FILL and a GPU ticket, i.e. the issue's own reproducer.

…t on #190)

Both branches continued on the same predicate: the allocated branch before its
abort, and the unallocated path before the supports_op check. One guard ahead of
the branch covers both and drops the duplicate. No behaviour change.
@bong-water-water-bong

Copy link
Copy Markdown
Author

Nit taken — fixed, not argued: the double test was reachable from two mutually exclusive branches, both of which
continue on equality, so one guard ahead of if (allocated) covers both. That is 61a7e312e (3 insertions, 6
deletions, no behaviour change).

Re-proved after the rebuild rather than assumed, same model-free graph, three arms distinguished by libggml-base
hash: before 5a3cc818 → -dev HRX0 SIGSEGV rc=139, -dev CPU PASS 32/32; reviewed head 26bfd55d → PASS 32/32
both; this head 76b8f997 → PASS 32/32 both. 3 s of GPU under gpu-hold, kernel 7.4.0-rc1-1bit, die 45 °C.

Also checked your "not verified" line since it is the case the patch exists for: the HRX-side reproducer does not need
the private add-on to claim FILL — the model-free graph (FILL + SET_ROWS, no model, ~/lb/i920-mini) reproduces
the fault on the unpatched library and passes on this one, which is why it is the regression arm above. The full
model arm still stops at #173's budget refusal in both arms, so nothing here claims a tok/s number.

One thing for the merge: the base moved under this PR — 1bit/hrx-vulkan-patched is now a5c32fbf8 (only
convert_hf_to_gguf.py, the --dspark flag). No conflict, no rebase from me, and no compiled path changed.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY

61a7e312e, one file (ggml/src/ggml-backend.cpp +45): a new pass 3.5 in ggml_backend_sched_split_graph.

The assignment is valid — the thing I wanted to be sure of before calling this safe, because the pass mutates the
scheduler's placement table: tensor_backend_id is a macro over a hash-indexed array,
#define tensor_backend_id(tensor) sched->hv_tensor_backend_ids[hash_id(tensor)], so tensor_backend_id(storage) = node_backend_id; writes the table rather than failing to compile against an rvalue.

The pass's decisions, in the order they matter:

  • view ops and unassigned nodes are skipped (unassigned is pass 4's job) — so this never fights the existing passes;
  • a host buffer type (or no resolvable type) is skipped, because host memory is addressable from every backend;
  • same backend id on both sides is skipped — nothing to move;
  • allocated storage that sits on another backend aborts with both names, instead of silently writing through a
    pointer the CPU cannot map. That is the engine#920 failure mode turned into a message, and aborting is right: once the
    buffer exists there is no placement that works;
  • unallocated storage moves to the node's backend, but only after ggml_backend_supports_op(..., storage) confirms that
    backend can produce it — so the fix cannot create a placement that a later pass has to undo;
  • SET_CAUSE(storage, "3.inpl") keeps the move visible in the schedule dump, which is how a future reader will find out
    why their tensor moved.

Compile at this head: ggml/src/ggml-backend.cpp -> rc 0, 0 errors, 0 warnings.

One consequence worth stating rather than fixing: moving the storage to the in-place node's backend can push other
readers of that storage onto the other side of a backend boundary (a copy the scheduler then inserts), so the cost can
land on a node that is not in this diff. That is the correct trade against a wrong write, and it will show up as
graph splits in a placement report rather than silently — which is what #197's new line is for.

Not run by me: an HRX device run of a graph with an in-place node whose view parent sits on the CPU (engine#920's
shape). The abort path needs one to be sure the message fires; the reasoning above is why I am not withholding the
verdict on it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant