Skip to content

cuda: do not read src[0] of source-less nodes in the graph compat check - #412

Merged
TheTom merged 1 commit into
TheTom:feature/turboquant-kv-cachefrom
jasstrong:pr/graph-compat-null-src
Nov 11, 2026
Merged

TheTom merged 1 commit into
TheTom:feature/turboquant-kv-cachefrom
jasstrong:pr/graph-compat-null-src

Conversation

@jasstrong

@jasstrong jasstrong commented Nov 7, 2026 •

Copy link
Copy Markdown

What

ggml_cuda_graph_check_compability reads node->src[0]->type for every node in the graph, before it checks whether the node is a MUL_MAT_ID:

// [TAG_MUL_MAT_ID_CUDA_GRAPHS]
const bool is_tq_w = (node->src[0]->type == GGML_TYPE_TQ4_1S || node->src[0]->type == GGML_TYPE_TQ3_1S);
if (node->op == GGML_OP_MUL_MAT_ID) {

A node with no sources, such as ARANGE, has src[0] == NULL. The value is only used inside the MUL_MAT_ID branch, so an optimizing compiler usually moves the load into the branch and nothing goes wrong. Nothing guarantees that, though. Whether it crashes depends on how the type test happens to compile.

This PR moves is_tq_w inside the MUL_MAT_ID branch, where src[0] always exists. It's a 2-line change with no effect on behavior.

How it showed up

In a downstream branch that adds one more TQ weight type to this test, clang compiled the three-way comparison into a bitmask test. That kept the load ahead of the op check. test-backend-ops then segfaulted at ARANGE on all three cards I tried (gfx90a, gfx1100, gfx1030):

Thread 1 "test-backend-op" received signal SIGSEGV, Segmentation fault.
ggml_cuda_graph_check_compability (cgraph=...) at ggml/src/ggml-cuda/ggml-cuda.cu:3990
3990        const bool is_tq_w = (node->src[0]->type == GGML_TYPE_TQ4_1S || ...
(gdb) p node->op
$1 = GGML_OP_ARANGE
(gdb) p node->src
$3 = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}

It also reproduces on this branch unmodified (6439568). Build with host code unoptimized and device code at -O3 (-DCMAKE_HIP_FLAGS_RELEASE="-DNDEBUG -Xarch_host -O0 -Xarch_device -O3"), then run:

build test-backend-ops -b ROCm0 -o ARANGE
6439568 segfault (rc 139)
6439568 + this PR OK

Testing

ROCm 7.2.3; the first run is on the MI210 (gfx90a):

  • The -O0-host build above with this PR, full test-backend-ops -b ROCm0: 21062/21063. The one failure is MUL_MAT(type_a=q5_cr, ..., cr_pattern=random), the intermittent ConvRot tolerance failure, which also fails on base. With host code unoptimized, no other source-less-node dereference turned up anywhere in the suite.
  • The downstream branch where the crash first appeared, Release build with the same change: full sweep 21538/21538 on gfx90a. On gfx1100 it was 21537/21538 and on gfx1030 21536/21538, and every failure was the same ConvRot random-pattern case. Before the change, all three segfaulted at ARANGE.

Limits of this testing

  • AMD only. No NVIDIA card was available, so the CUDA build itself is untested. This is host code shared by both backends, so the change is the same on CUDA.

🤖 Generated with Claude Code

ggml_cuda_graph_check_compability computed is_tq_w from node->src[0]->type
for every node, before testing node->op == GGML_OP_MUL_MAT_ID. Nodes with no
sources (ARANGE, ...) have src[0] == NULL, so this is a NULL dereference
whenever the compiler keeps the load ahead of the op test. Release builds
here happen to sink it into the branch; an unoptimized build, or a change to
the type test, does not, and test-backend-ops then segfaults at ARANGE.

Compute is_tq_w inside the MUL_MAT_ID branch, where src[0] always exists.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@TheTom

TheTom commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Thanks @jasstrong, merged. The ARANGE segfault also reproduces on an NVIDIA GB10 in a plain Release build, so this fixed a real crash there too, not only the -O0 host case.

jasstrong pushed a commit to jasstrong/llama-cpp-turboquant that referenced this pull request Nov 11, 2026
… into the TQ3_4S stack

One conflict, in ggml_cuda_graph_check_compatibility: TheTom#412 moved is_tq_w
inside the MUL_MAT_ID branch so source-less nodes are not dereferenced, and
this branch adds TQ3_4S to that test. Resolved as TheTom#412's placement with
TQ3_4S added. TheTom#414's tq_ncols change to the fused TQ condition merged
cleanly with this branch's TQ3_4S additions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

2 participants