Skip to content

rpc: retype TQ4_1S tensors that the server converts to q8_0 - #405

Merged
TheTom merged 4 commits into
TheTom:feature/turboquant-kv-cachefrom
superbudvar:fix/rpc-tq4-1s-retype
Nov 22, 2026
Merged

TheTom merged 4 commits into
TheTom:feature/turboquant-kv-cachefrom
superbudvar:fix/rpc-tq4-1s-retype

Conversation

@superbudvar

@superbudvar superbudvar commented Oct 22, 2026 •

Copy link
Copy Markdown

Overview

A TQ4_1S model served over the RPC peer produces garbage. The model answers "What is 17 plus 25?" with !!!!!!!!!!!!!!.

TQ4_1S is converted to q8_0 when it loads (the conversion is on by default, because q8_0 prefill is much faster than the native TQ path), and the conversion expands the data by 1.7x. Three things had to line up, and each was broken on its own:

  1. The client sized the RPC buffer with ggml_nbytes(), which is the TQ4_1S size, and told the server to allocate that much. The server then converted in place and wrote past the end of it. The client now asks the server for the size, the way it already does for padded quantised tensors, FLASH_ATTN_EXT and MUL_MAT_ID.
  2. The server's own allocation was short for padded rows. The base size was already the q8_0 size the conversion writes, but the MATRIX_ROW_PADDING term was still computed with the TQ4_1S row size - 20 bytes per 32 values against q8_0's 34.
  3. The client never retyped the tensor. The local CUDA path does this after the conversion:
// Update tensor metadata to q8_0
tensor->type = GGML_TYPE_Q8_0;

Over RPC that retyping happened on the server only. The client kept the TQ4_1S type while the buffer held q8_0, so every matmul read q8_0 bytes as TQ4_1S.

TQ3_1S is unaffected: it is never converted, so both sides agree on the type. That is why a TQ3_1S model worked over RPC while the TQ4_1S one did not.

Additional information

How it was found

Reproduced over RPC with TQ4_1S models. The same prompt, the same peer:

configuration output
TQ4_1S, local only 17 + 25 = 42
TQ4_1S, --rpc !!!!!!!!!!!!!!
TQ3_1S, --rpc 17 + 25 = 42

The client's env var made no difference - the client does not hold the weights - and neither did the client's own size fix, which is why this survived the earlier TQ4_1S-over-RPC work and why replacing the server binaries changed nothing.

Instrumenting the client to print what the server reports as the allocated size showed the cause directly:

peer alloc size = 35651584, client nbytes = 20971520 -> peer CONVERTED to q8_0
peer alloc size = 53477376, client nbytes = 31457280 -> peer CONVERTED to q8_0

1.7x, which is the q8_0-to-TQ4_1S size ratio. The server was behaving as designed; the client just did not follow.

The retyping test is exact

The client asks the server how much it allocated for the tensor and compares it with the size a converted TQ4_1S tensor would need:

n_blocks * sizeof(block_q8_0)  +  q8_0 row padding when ne0 % 512 != 0

If they match, the server converted it and the client retypes the tensor the same way the local path does. The comparison is exact rather than "bigger than nbytes", so it cannot fire when the server kept the TQ4_1S bytes - which is the case for a server running with GGML_TQ_NATIVE=1, and it also cannot fire on a padded row whose padding term the server still sizes as TQ4_1S, which is what commit 2 fixes.

Measurements

Prompt "What is 17 plus 25? Answer with just the number.", RPC peer (RTX 3960 Ti):

model before after
TQ4_1S model over RPC !!!!!!!!!!!!!! 17 + 25 = 42

Scope

  • Commits 1 and 2 are the allocation-size half: the client must ask, and the server's answer must be right. Commit 3 is the type half.
  • TQ3_1S needs none of this: it is never converted.
  • No behaviour change for non-TQ4_1S tensors: the new path only runs for GGML_TYPE_TQ4_1S.
  • The server side of commit 2 only matters for a TQ4_1S tensor whose ne[0] % 512 != 0; the base size was already correct.
  • I could not run a CUDA-side sweep of this: the change is in the RPC client, and the only CUDA-side part is the allocator's row-padding term, which is exercised by any TQ4_1S model with a padded row.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES - an AI coding agent (opencode) was used throughout this series: the reproduction, the diagnosis, the fixes and the verification runs.

🤖 Generated with opencode

Comment thread ggml/src/ggml-rpc/ggml-rpc.cpp Outdated
bool status = send_rpc_cmd(ctx->sock, RPC_CMD_SET_TENSOR, input.data(), input.size());
RPC_STATUS_ASSERT(status);

ggml_backend_rpc_retype_converted_tq4_1s(buffer, tensor);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This call is skipped when the hash check above finds the data already on the server (the return in the response.result branch). TQ4_1S weights are almost always above HASH_THRESHOLD, so with a server cache dir the second load takes that path, the server converts, and the client keeps TQ4_1S, which is the original bug. Run the retype on that path too.

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.

Fixed. The early return is gone: the hash hit now only skips the upload, and the retype runs on both paths.

    bool cached = false;
    if (size > HASH_THRESHOLD) {
        ...
        cached = response.result != 0;
    }

    if (!cached) {
        ... send the data ...
    }

    ggml_backend_rpc_retype_converted_tq4_1s(buffer, tensor, offset, size);

The server converts the weight either way. On the hash path it calls ggml_backend_tensor_set() with the cached file, which goes through the same backend conversion, so the buffer holds q8_0 and the client has to retype.

It matters because the client is the side that carries the type into the graph: the server deserializes each graph's tensors from the client's serialization, so a client that still says TQ4_1S makes the server read q8_0 bytes as TQ4_1S.

Verified with the server's cache enabled (ggml-rpc-server -c): the first load sends the data and the second load takes the hash path. Both answer the prompt correctly now; before the fix the second load answered !!!!!!!!!!!!!!.

Comment thread ggml/src/ggml-rpc/ggml-rpc.cpp Outdated
bool status = send_rpc_cmd(ctx->sock, RPC_CMD_SET_TENSOR, input.data(), input.size());
RPC_STATUS_ASSERT(status);

ggml_backend_rpc_retype_converted_tq4_1s(buffer, tensor);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

ggml_backend_cuda_buffer_set_tensor converts only when offset == 0 && size == ggml_nbytes(tensor). The alloc size answers whether the server would convert a full write, so a partial write still matches and the client retypes over unconverted bytes. Any later chunk is then serialized as q8_0 and the server writes it raw. Gate the retype on offset == 0 && size == ggml_nbytes(tensor), checked before the type changes.

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.

Fixed. The helper takes the offset and the size and returns early unless

    offset == 0 && size == ggml_nbytes(tensor)

which is the same condition ggml_backend_cuda_buffer_set_tensor uses, and it is evaluated before the type changes, so ggml_nbytes() is still the TQ4_1S size when it is read.

A partial write therefore leaves the TQ4_1S bytes on both sides: the server does not convert either, and the client does not retype, so the two agree. Without the gate the client retyped over bytes the server had not converted, and the next chunk was serialized as q8_0 and written raw.

Comment thread ggml/src/ggml-rpc/ggml-rpc.cpp Outdated
return response.alloc_size;
}

// The server converts TQ4_1S weights to q8_0 when it loads them (the conversion

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Cut this block, the one at line 518 and the one at line 679 down to one or two lines each. Say what the code checks, not the story of the bug. This one also says the test is alloc larger than nbytes, but the code compares against the exact q8_0 size.

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.

Fixed. All three blocks are one or two lines now and say what the code checks:

  • the helper: the server may have converted a TQ4_1S weight to q8_0, and the alloc size tells which it is;
  • the alloc formula: the q8_0 alloc size, n_blocks * sizeof(block_q8_0) plus the q8_0 sized row padding;
  • the get_alloc_size call site: the server may convert TQ4_1S to q8_0, so ask it for the real size.

The "larger than nbytes" wording is gone with them: the code compares the alloc size with the exact q8_0 size, not with nbytes.

@TheTom

TheTom commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Thanks @superbudvar. The RPC retype diagnosis is right. Two gaps and a comment trim are inline.

@superbudvar
superbudvar force-pushed the fix/rpc-tq4-1s-retype branch from e89e9c6 to 4cd1458 Compare November 12, 2026 00:19
@superbudvar

Copy link
Copy Markdown
Author

The three review comments are addressed

One new commit on top of the previous head:

commit change
rpc: retype the TQ4_1S tensor on the hash path and gate it on a full write the retype runs on the hash-hit path as well, and only on a full write

The hash path is the bug you described, and it is now reproduced and fixed

The server's cache was on (ggml-rpc-server -c) and the server ran locally with the client connecting over RPC. The server logs saved to '<file>' for every tensor it caches, so the number of those lines is the number of uploads: a load that hits the cache uploads nothing.

Previous head:

load tensors uploaded output
1 81 42
2 0 - the hash path """""""""""""""""""""""""""" (garbage)

This head:

load tensors uploaded output
1 81 42
2 0 - the hash path 42

Prompt "What is 17 plus 25? Answer with just the number.", TQ4_1S model, greedy, seed 42.

So the second load with the previous head took the hash path, the server converted the weight when it took it from the cache, and the client kept TQ4_1S - the original bug, reproduced on the path you pointed at. The retype now runs on both paths and the hash path gives the same answer as the upload path.

The reason the client has to be the one to retype is that the client is the side that carries the type into the graph: the server deserializes each graph's tensors from the client's serialization, so a client that still says TQ4_1S makes the server read q8_0 bytes as TQ4_1S.

The full-write gate

ggml_backend_cuda_buffer_set_tensor converts only when offset == 0 && size == ggml_nbytes(tensor), so the retype is now gated on the same condition, checked before the type changes - ggml_nbytes() is therefore still the TQ4_1S size when it is read. A partial write leaves the TQ4_1S bytes on both sides, so neither side retypes and the two agree.

The comment blocks

All three are one or two lines now and say what the code checks. The "larger than nbytes" wording is gone with them: the code compares the alloc size with the exact q8_0 size, not with nbytes.

Rebased

The branch is now the fork base 643956891 plus this change only, four commits:

commit change
rpc: ask the server for the TQ4_1S allocation size the client asks the server instead of assuming ggml_nbytes()
cuda: size the TQ4_1S row padding as q8_0 the server's MATRIX_ROW_PADDING term is q8_0 sized
rpc: retype TQ4_1S tensors that the server converts to q8_0 the client follows the server's conversion
rpc: retype the TQ4_1S tensor on the hash path and gate it on a full write the hash path and the full-write gate

The server converts TQ4_1S weights to q8_0 when it loads them (the
conversion is on by default for prefill speed), so the buffer it allocates
holds q8_0 - 1.7x the tensor's own size.

The client sized the RPC buffer with ggml_nbytes(), which is the TQ4_1S
size, and told the server to allocate that much. The server then converted
in place and wrote past the end of it.

Ask the server for the size instead, the way the client already does for
padded quantised tensors, FLASH_ATTN_EXT and MUL_MAT_ID.
The base size was already the q8_0 size the load-time conversion writes,
but the MATRIX_ROW_PADDING term was still computed with the TQ4_1S row
size, so a tensor with ne[0] % 512 != 0 was under-allocated per row: a
converted row holds q8_0, and q8_0 is 34 bytes per 32 values against
TQ4_1S's 20.
The local CUDA path retypes the tensor after the conversion:

    // Update tensor metadata to q8_0
    tensor->type = GGML_TYPE_Q8_0;

Over RPC that retyping happened on the server only. The client kept the
TQ4_1S type while the buffer held q8_0, so every matmul read q8_0 bytes
as TQ4_1S and the model produced garbage. A TQ4_1S model served over the
RPC peer answered "17 + 25" with "!!!!!!!!!!!!!!".

TQ3_1S is unaffected: it is never converted, so both sides agree on the
type.

The client asks the server how much it allocated for the tensor and
compares it with the size a converted TQ4_1S tensor would need
(n_blocks * sizeof(block_q8_0) plus the q8_0 row padding). If they
match, the server converted it and the client retypes the tensor the
same way the local path does. The comparison is exact, so it cannot fire
when the server kept the TQ4_1S bytes, which is the case for a server
running with GGML_TQ_NATIVE=1.

Measured over RPC (RTX 3960 Ti), prompt "What is 17 plus 25? Answer with
just the number.":

  Polaris-13B-TQ4_1S over RPC   before: "!!!!!!!!!!!!!!"   after: "17 + 25 = 42"
  Qwopus-9B-TQ4_1S over RPC     before: "!!!!!!!!!!!!!!"   after: "17 + 25 = 42."
…write

The server converts TQ4_1S weights to q8_0 whether it takes them from the
cache or from the write, so the client has to retype in both cases. The
server converts only on a full write, so a partial write leaves the TQ4_1S
bytes in place and the client must not retype it.
@superbudvar
superbudvar force-pushed the fix/rpc-tq4-1s-retype branch from 4cd1458 to 40533a4 Compare November 21, 2026 04:39
@superbudvar

Copy link
Copy Markdown
Author

Rebased onto the current fork base (4185b90a0), content-preserving: git range-diff reports all 4 commits as =.

It picks up #414 (src1 column count in the TQ fused and cuBLAS paths), #412 (null src[0] in the graph compat check) and #406 (conditional subgroup size for the TurboQuant SET_ROWS on wave64-only GPUs), plus turbo5/turbo6 KV types and the Spark2_5 model.

ggml-rpc.cpp compiles clean on the rebased head with GGML_RPC=ON.

@TheTom
TheTom merged commit 4d181d0 into TheTom:feature/turboquant-kv-cache Nov 22, 2026
superbudvar added a commit to superbudvar/llama-cpp-turboquant that referenced this pull request Nov 22, 2026
…heTom#417, TheTom#371, TheTom#401, TheTom#419)

# Conflicts:
#	AGENTS.md
#	docs/KV-cache-quantization.md
#	tests/CMakeLists.txt
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