Skip to content

ggml-openvino: reshape the MUL_MAT_ID activations of a one-token static graph - #462

Open
yzhou51 wants to merge 1 commit into
ravi9:dev_backend_openvinofrom
yzhou51:llama_bench
Open

yzhou51 wants to merge 1 commit into
ravi9:dev_backend_openvinofrom
yzhou51:llama_bench

Conversation

@yzhou51

@yzhou51 yzhou51 commented Nov 27, 2026

Copy link
Copy Markdown

We meet this issue when mearsure the decod performance with llama-bench.

Overview

On the NPU, the first graph that the backend computes decides how the static prefill/decode pair is built. When that graph has one token, the prefill model of the pair is built from it and padded to the prefill chunk size. With a MoE model, its translation fails:

GGML OpenVINO backend ov::Exception: Check 'a_shape[0] == 1 || a_shape[0] == indices_shape[1]' failed at
src\common\transformations\src\ov_ops\gather_matmul.cpp:88:
While validating node 'extension::GatherMatmul GatherMatmul_3541 (opset1::Transpose Transpose_3539[0]:f32[128,1,2048],
opset1::Convert blk.0.ffn_gate_exps.weight[0]:f32[256,512,2048], opset1::Reshape Reshape_3537[0]:i32[128,8], ...)':
The first dimension of input A must be equal to number of activated experts. Got: 128 and 8 instead.

llama-bench -p 0 -n N -d D hits it at once: its generation warmup decodes one token into an empty context. llama-cli, llama-server and llama-perplexity start with a longer prompt and do not.

build_moe_ffn reshapes the MUL_MAT_ID activations from [n_embd, n_tokens] to [n_embd, 1, n_tokens]. The decoder gives this RESHAPE case 1, which puts the tokens in dim 2. But translate_reshape returns its input unchanged when the ggml input and output shapes are the same, and with one token they are: [n_embd, 1, 1, 1] both. The static model pads that input to 128 tokens in dim 1, the reshape keeps them there, and MUL_MAT_ID gets [n_tokens, n_used, k] = [1, 128, 2048] instead of [128, 1, 2048]. With more than one token the shapes differ, the reshape runs, and the graph is correct.

translate_reshape no longer takes the same-shape shortcut for case 1. The reshape is then always [1, -1, 1, n_embd], which puts the padded tokens in dim 2.

  • An unpadded one-token graph (the decode model) is not changed by this reshape.
  • Dynamic graphs (CPU, GPU) are not affected: their token dimension is dynamic, so the shortcut never applied.
  • In the decoder's classification, the only RESHAPE that gets case 1 with an unchanged ggml shape is the MUL_MAT_ID input. Other same-shape reshapes get case 5 first.

Additional information

PTL NPU, Windows, Qwen3.6-35B-A3B Q4_0 (MoE on the NPU needs PR 444), GGML_OPENVINO_PREFILL_CHUNK_SIZE=128:

llama-bench -fa 1 -p 0 -n 32 -d 1024 Before After
with the generation warmup (-r 5) fails (error above) 4.54 ± 0.31 t/s
--no-warmup 4.58 ± 0.09 t/s —

Requirements

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