Skip to content

optimum-emb: feed position_ids to embedding IRs that take them - #204

Merged
SearchSavior merged 2 commits into
SearchSavior:mainfrom
meatposes:emb-position-ids
Nov 25, 2026
Merged

SearchSavior merged 2 commits into
SearchSavior:mainfrom
meatposes:emb-position-ids

Conversation

@meatposes

Copy link
Copy Markdown
Contributor

TL;DR: Makes the listed Qwen3 embedding model work instead of failing every request.


Fixes #106.

the Qwen3-Embedding export in docs/models.md (Echo9Zulu/Qwen3-Embedding-0.6B-int8_asym-ov) was made with optimum-intel 1.25.2 and has position_ids as a graph input. OVModelForFeatureExtraction.forward() only passes input_ids and attention_mask (extra kwargs are dropped), so every request fails, even one string:

  • GPU: Got unexpected inputs: expecting the following inputs {'position_ids', 'attention_mask', 'input_ids'} but got ...
  • CPU: NaN, or the same missing-input error depending on the openvino version

when the compiled model lists position_ids, the engine builds it from the attention mask (cumsum - 1, padding set to 1, the rule optimum-intel's OVModelForCausalLM uses, so left and right padding both work) and calls the compiled model with every input forward() would have sent plus position_ids. IRs without it (bert-style, and decoder exports from optimum-intel >= 1.26, after huggingface/optimum-intel#1415) still go through forward(), nothing changes for them. at load the engine logs when an IR takes position_ids and warns about any input it can't fill.

re-exporting with a current optimum-intel is the other fix (a 2.3.0.dev0 int8 re-export has only input_ids + attention_mask), so the hf upload / docs/models.md could be updated too. this PR keeps the existing export working.

B50, Qwen3-Embedding-0.6B int8, both IR layouts:

main this PR
Echo9Zulu export (takes position_ids) fails, GPU and CPU works, position_ids path
re-export, optimum-intel 2.3.0.dev0 works works, forward() path, same as main

vs the PyTorch reference (Qwen/Qwen3-Embedding-0.6B fp32, last-token pooling): cosine 0.996-0.999 per text for both exports, model card example query->passage 0.764 / 0.765 vs 0.767.

tests

  • unit: position_ids for left and right padding, the position_ids path vs the forward() path, token_type_ids on the position_ids path, load-time logging
  • integration: now embeds a padded batch and checks finite unit vectors and alone-vs-batched. the old one (one vector, len > 0) passed on this bug, the new one fails on main with the Echo9Zulu export
  • unit suite: only main's 2 known kokoro failures (test_chunk_forward_pass_yields_chunks, test_make_chunks_handles_newlines_and_empty)

note: batches with different-length texts still need "config": {"padding": true} on main; making plain OpenAI requests work is a separate PR (stacked on this one).

https://claude.ai/code/session_01H45h8vWtUhMNST1Fk179WR

Decoder-based embedding IRs exported before optimum-intel 1.26, such as
Echo9Zulu/Qwen3-Embedding-0.6B-int8_asym-ov (optimum-intel 1.25.2), have
position_ids as a graph input. OVModelForFeatureExtraction.forward() only
passes input_ids and attention_mask (it drops extra kwargs), so:

- GPU: every request fails with "Got unexpected inputs: expecting the
  following inputs {'position_ids', 'attention_mask', 'input_ids'}",
  even a single string.
- CPU: inference runs on an unset tensor and returns NaN, which then
  fails JSON encoding ("Out of range float values are not JSON
  compliant: nan").

When the compiled model lists position_ids among its inputs, build them
from the attention mask (cumsum - 1, padding positions set to 1, the
same rule optimum-intel's OVModelForCausalLM uses, so left and right
padding both work) and call the compiled model with all three inputs.
IRs without position_ids (encoder models, current optimum-intel
exports) keep the forward() path unchanged.

Fixes SearchSavior#106.

Claude-Session: https://claude.ai/code/session_01H45h8vWtUhMNST1Fk179WR
Hardening for the position_ids path added in the previous commit:

- Also fill token_type_ids (zeros) and decoder_input_ids when the IR
  takes them, as OVModelForFeatureExtraction.forward() does, so an IR
  with position_ids plus one of these still gets every input.
- At load, log when an IR takes position_ids, and warn about inputs the
  engine cannot fill, with a pointer to re-export.
- Integration test: a padded batch of different lengths instead of one
  text, finite unit-length vectors, and a text alone vs in the batch.
  The old test (one vector, len > 0) passed on the bug; this one fails
  on main with the old Qwen3-Embedding export ("Got unexpected inputs:
  expecting ... position_ids").

Checked on both IR layouts of Qwen3-Embedding-0.6B int8: the Echo9Zulu
export (optimum-intel 1.25.2, takes position_ids) and a re-export with
optimum-intel 2.3.0.dev0 (input_ids + attention_mask only, forward()
path).

Claude-Session: https://claude.ai/code/session_01H45h8vWtUhMNST1Fk179WR
@meatposes
meatposes marked this pull request as ready for review November 22, 2026 07:49
@SearchSavior
SearchSavior merged commit 8bf790b into SearchSavior:main Nov 25, 2026
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.

Qwen3-Embedding fails on optimum/emb path — OVModelForFeatureExtraction doesn't provide position_ids

2 participants