Skip to content

server : allow RANK pooling batch splitting for causal LLM rerankers (ie. Qwen3 and Qwen3-VL) - #28876

Merged
CISC merged 2 commits into
ggml-org:masterfrom
timothywang21:qwen3vl-rerank-split-prefill-v2
Sep 27, 2026
Merged

CISC merged 2 commits into
ggml-org:masterfrom
timothywang21:qwen3vl-rerank-split-prefill-v2

Conversation

@timothywang21

@timothywang21 timothywang21 commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Traditionally, rerank models were bidirectional cross-encoders like BERT, which mandated that all the tokens in the input must be within the same physical batch due to bidirectional attention. However, the Qwen3 and Qwen3-VL family of rerankers are just repurposed LLMs (causal decoders). This meant that prefill does not have to be all within the same physical batch. They can be chunked into batches just like any other decoder LLM.

Currently, llama-server is outdated in enforcing this difference. llama-server is too conservative and forces all rerank models to have to batch their prefill into one physical batch (default 512 tokens). In other words, if the document is greater than the physical batch size, the rerank doesn't work. Thus, long documents or images cannot be fed into the rerank model in llama.cpp

Code Changes

  1. Expose llama_get_causal_attn(ctx) so the server can check the effective runtime attention type, reflecting any --attention override or set_causal_attn call. (cparams).

  2. can_split() now permits chunked prefill for causal RANK models instead of rejecting inputs larger than n_ubatch. The graph builder's inline arch check is refactored to use the same cparams.causal_attn predicate, removing the duplication.

Additional information

This first PR is in support of adding full multimodal support for the /embedding and /rerank endpoints. See here for more context (#18665 (comment))

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes. Assisted-by: Opencode/Qwen3.8-27B but all code was verified by me.

@timothywang21
timothywang21 requested review from a team and CISC as code owners September 14, 2026 01:46
@CISC

CISC commented Sep 14, 2026

Copy link
Copy Markdown
Member

We don't need this arch check do we? We already have c/hparams.causal_attn, we just need to make sure it is set appropriately for all non-causal models.

And then just a generic llama_get_attention_type instead of the rank-specific one.

@timothywang21
timothywang21 force-pushed the qwen3vl-rerank-split-prefill-v2 branch from e669baf to bc90e23 Compare September 14, 2026 17:13
@github-actions github-actions Bot added the model Model specific label Sep 14, 2026
@timothywang21

Copy link
Copy Markdown
Contributor Author

@CISC I have made the changes you requested. I made the attention type function generic and removed the hardcoding of the models. I also updated the bidirectional encoder model's hparams in the /models folder to show that they do not have causal attention. This makes the patch more generic and flexible for future changes.

@CISC

CISC commented Sep 14, 2026

Copy link
Copy Markdown
Member
  1. Set hparams.causal_attn = false on all bidirectional BERT variants (bert, eurobert, jina-bert-v2/v3, modern-bert, neo-bert, nomic-bert, nomic-bert-moe) where it was previously an oversight.

Undo this, they should all have this set in their GGUF metadata already.

@timothywang21
timothywang21 force-pushed the qwen3vl-rerank-split-prefill-v2 branch from bc90e23 to b14e308 Compare September 14, 2026 17:51
@timothywang21

Copy link
Copy Markdown
Contributor Author

Undid the model param changes. Are we good to go?

@CISC

CISC commented Sep 14, 2026

Copy link
Copy Markdown
Member

Are we good to go?

Not sure, I'm not 100% convinced as it's possible to override attention type with --attention and set_causal_attn, and that will not be reflected in hparams, only cparams.

@timothywang21

Copy link
Copy Markdown
Contributor Author

If you are worried about overrides during runtime, we can add a public getter llama_get_causal_attn(ctx) alongside the existing llama_set_causal_attn(ctx), then use that in can_split() instead of llama_model_is_causal(model)? llama_get_causal_attn(ctx) would be the actual runtime cparams we can use to check splitting?

Rerank models fall into two categories: bidirectional cross-encoders
(BERT, etc.) that require all tokens in a single physical batch, and
causal LLMs repurposed as rerankers (Qwen3, Qwen3-VL) that can use
chunked prefill like any other decoder.

Previously the server rejected all RANK-pooling inputs larger than
n_ubatch, and the graph builder hardcoded QWEN3/QWEN3VL arch checks to
determine last-token pooling. This broke long-document and multimodal
reranking for causal models.

Fix: expose llama_get_causal_attn(ctx) so the server can check the
effective runtime attention type (reflecting any --attention override
or set_causal_attn call). Also expose llama_model_is_causal(model)
for querying the static architectural property from GGUF metadata.

can_split() now permits chunked prefill for RANK pooling when the
context is causal. The graph builder's inline arch check is replaced
with the same cparams.causal_attn predicate, removing the duplication.

Assisted-by: Opencode/Qwen3.8-27B
@timothywang21
timothywang21 force-pushed the qwen3vl-rerank-split-prefill-v2 branch from b14e308 to da3ad31 Compare September 14, 2026 18:43
abrisene pushed a commit to abrisene/llama.cpp that referenced this pull request Sep 18, 2026
…ream PR ggml-org#28876)

Squash of ggml-org#28876 at da3ad31. The last-token
RANK pooling condition now keys on causal_attn instead of an arch list, which covers
the Qwen3.5 classifier models added in f8b0dc15f.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
SelfRef added a commit to SelfRef/llama.cpp-rdna that referenced this pull request Sep 18, 2026
Assisted-by: Claude Opus 5
@timothywang21

Copy link
Copy Markdown
Contributor Author

@CISC Sorry to bother again, but do you think this PR will be merged? I have other PRs referencing this one that supports multimodality rerankers and embeddings support.

@CISC CISC left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, @ggerganov should sign off on the API.

Comment thread tools/server/server-context.cpp Resolved
Comment thread src/llama-context.h Resolved
Comment thread include/llama.h Resolved
@timothywang21

Copy link
Copy Markdown
Contributor Author

@ggerganov fixed the issues. LGTM?

@ggerganov ggerganov added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Sep 27, 2026
@timothywang21
timothywang21 force-pushed the qwen3vl-rerank-split-prefill-v2 branch from 1212af5 to 38a3d3d Compare September 27, 2026 20:22
@timothywang21

Copy link
Copy Markdown
Contributor Author

Just force pushed the trailing whitespace fix that the CI flagged. No other changes.

@CISC
CISC merged commit 4da6337 into ggml-org:master Sep 27, 2026
11 of 12 checks passed
wanghqc added a commit to qualcomm/llama.cpp that referenced this pull request Sep 29, 2026
ggml-opencl.cpp: this branch's layout kept, the three upstream OpenCL changes
(ggml-org#29401 q5_K bin kernels, ggml-org#29439 q8_0 dp4a bin kernel, ggml-org#29503 bin kernel
loading condition) replayed onto it. The q5_K bin layout and the q8_0 dp4a bin
GEMM are opt-in here (GGML_OPENCL_Q5_K_BIN=1, GGML_OPENCL_Q8_0_BIN_DP4A=1):
by default they would take the spec/MTP verify widths from the cooperative-K
and narrow kernels. q5_K bin is limited to 2-D weights, q8_0 bin to N > 16.

common/speculative, server-context: this branch's llama_batch implementation
kept (tree drafting puts several seq_ids on one token, which common_batch
cannot hold); a common_batch overload of common_speculative_process converts
for the new callers, the mtmd post-decode callback takes the new embd batch,
and ggml-org#28876, ggml-org#29556 and ggml-org#29648 are applied to server-context.
leonardomurillo added a commit to leonardomurillo/llama.cpp that referenced this pull request Oct 7, 2026
…ch split; keep MoE packs + decision engine)

conflicts: llama-graph.cpp (kept use_moe_packs wrapper + upstream GLM5_NEXT clamp arch + MISTRAL4 F32 down-proj),
test-backend-ops.cpp (test_mul_mat_id keeps upstream m_v at arg 10, our skip_ids appended at arg 11; call sites updated),
llama-bench.cpp (both sched_async_cpu and repack fields). decision-engine: local batch helpers (upstream ggml-org#29601 removed
common_batch_clear/add). local MSVC CPU build of llama-server, test-backend-ops, llama-bench OK.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. model Model specific server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants