Repository navigation
fix: preserve sequence offsets in unpooled batched embeddings - #2395
Open
linjiaye929 wants to merge 4 commits into
Open
linjiaye929 wants to merge 4 commits into
linjiaye929 wants to merge 4 commits into
Conversation
linjiaye929
marked this pull request as ready for review
October 31, 2026 03:37
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
With
LLAMA_POOLING_TYPE_NONE,Llama.embed()advances the flat output pointer by a token count after each sequence, although the pointer indexes floats. When multiple sequences share a decode batch and the embedding dimension is greater than one, later sequences read the wrong rows.The pinned native API contract specifies contiguous output in batch order with shape
n_outputs * n_embd.The Python slicing code therefore needs to advance by
size * n_embdfloats, rather thansize.Change
Pooled embeddings and the public API are unchanged.
Validation
Validated against Python commit
1652066e0af45f2313b339670ef9555e8a54e545and its unmodified native dependency0c1e57098bba43ac29e6e3b677cdceebdd22334fon Linux x86-64, Python 3.12.14, GCC 14, CPU-only, with two parallel build jobs.The native library was built from source and imported normally.
Commands were run in an isolated environment with
HF_HUB_OFFLINE=1andHF_HUB_DISABLE_TELEMETRY=1:python -m pytest tests/test_embedding_offsets.py -q: baseline 4 failed, 2 passed; corrected code 6 passed.python -m pytest tests/test_embedding_offsets.py tests/test_llama_grammar.py tests/test_llama_chat_format.py tests/test_llama_speculative.py tests/test_llama.py::test_llama_cpp_version -q: 13 passed.make lint: passed, including formatting checks for all 30 Python/test files.The regression uses the real Python method with a deterministic ctypes float buffer and mocked decode/output-getter boundaries. It does not exercise model inference.
Full
make testwas not run because its model-download fixtures and vocabulary GGUF asset were unavailable within this validation scope. No model assets were downloaded, and GPU/backend-specific behavior was not tested.AI assistance
AI assistance was used for investigation, the patch, regression tests, and this description. The results above distinguish the automated checks actually run from model-dependent coverage that remains unverified.