Skip to content

fix(embed): respect physical batch limits for non-causal embeddings - #2387

Open
original4422 wants to merge 13 commits into
abetlen:mainfrom
original4422:fix/noncausal-embedding-microbatches
Open

original4422 wants to merge 13 commits into
abetlen:mainfrom
original4422:fix/noncausal-embedding-microbatches

Conversation

@original4422

Copy link
Copy Markdown

Summary

Fixes #1762. A 513-token BGE-M3 input with n_batch=513 currently reaches a native assertion because the omitted n_ubatch stays at 512.

omitted n_ubatch: embedding -> n_batch; generation -> 512
explicit n_ubatch: retain the requested limit
non-causal/encoder embedding: pack within the native physical batch
single sequence exceeding an explicit physical limit: raise ValueError

Resolve defaults before native context creation, including n_ctx=0 and logical batch clamping. Keep logical truncation unchanged, preserve causal microbatch splitting, and cache the fixed long-context model for the native CI regressions. No vendor changes.

Evidence

  • Before: original 0.3.36 / vendor 0c1e57098, real BGE-M3 F16, actual non-causal context: one 513-token sequence and two sequences totaling 256+257 tokens both terminate isolated processes with SIGABRT (encoder requires n_ubatch >= n_tokens). The initial regression set has 5 failures and 3 passing controls.
  • After: complete python -m pytest --full-trace -v: 41 passed. This includes real long embeddings, aggregate batches, explicit limits/zero, n_ctx=0, clamping, causal generation/embedding controls, and the existing model tests. Long embeddings retain every token and have finite output values.
  • python -m ruff check llama_cpp tests and python -m ruff format --check llama_cpp tests: pass; git diff --check: pass.

Local environment: macOS arm64, Python 3.12.14, original native build with Metal available; the new regression cases explicitly use CPU model layers (n_gpu_layers=0). BGE-M3 F16 revision: 9379ce497e8814b200f2dc0d18eb4045426dcb8c.

Merge Danger

Door: two-way

Blast Radius: embeddings

Omitted embedding microbatches now follow the logical batch size. Explicit physical limits remain supported; a sequence that cannot fit raises a Python error instead of aborting the process. Ordinary generation retains its 512-token default microbatch.

@original4422

original4422 commented Nov 8, 2026 •

Copy link
Copy Markdown
Author

AI-assisted maintenance update (2026-11-23): merged current main 77fbb5d into signed commit 862c4c4ca6bf, preserving the embedding regression coverage and upstream 0.3.38 changes. Rebuilt exact llama.cpp vendor f5e6915 on macOS arm64 / Python 3.12.14 with two build jobs (GGML_NATIVE=OFF, GGML_ACCELERATE=ON): the original native suite passed 41 tests in 12.88s. Three additional real-package controls passed: incremental disk-cache LRU eviction, offline snapshot-cache loading, and offline populated flat local_dir loading. These used the existing tiny model on CPU; this does not report hosted CI or other-platform validation. Nested local_dir subfolder loading has an inherited upstream path limitation, which this update leaves unchanged.

Previous validation receipts (retained unchanged):

AI-assisted maintenance update (2026-11-23): merged current main 10bd146d587cb9c0f5d0e1b592fa5a47e7c1dc81 in signed commit 6026435e8256771d2d63a8c3a0cbafcb03db5f88, preserving both changelog entries and the upstream wheel-index SHA256 script unchanged. Cumulative embedding change remains four files (+164/-4).

Validation on this retained tree: shell syntax and six offline fixtures running the real upstream script with real jq pass (only curl metadata responses substituted; valid/missing/null/malformed hashes and non-wheel filtering). All 13 owning embedding/native/build inputs match the prior validation. The 2026-11-22 native result, 41 passed in 9.88s, and four cache/server lifecycle controls are reused, not rerun; server controls used a settings fixture and do not validate the full server. No current hosted-CI or other-platform pass is claimed.

Previous validation receipts (retained unchanged):

Current-main synchronization in a55e524fd66d86e3da824a4c47a1a6e5536f37e7 merges ed2a26e while retaining the embedding microbatch fix, actual-context buffer fix, and all three changelog entries. The upstream disk-cache and tokenizer-config file-lifetime changes are preserved.

Validation on macOS arm64 / Python 3.12.14 reused the exact unchanged afe77e18f native build without installation or rebuilding: the configured original/native suite passed 41 tests in 9.88s. Four focused controls passed: two use real diskcache for repeated lookup/LRU eviction; two exercise the real server loader with real files/JSON for closure before handler construction and on parse failure. Those server controls supply a settings import/type fixture because the existing environment lacks Pydantic; they do not validate the full server stack. Full Ruff check/format (30 files) and whitespace checks passed. This is local validation, not hosted CI.


Current-main synchronization in 940b5a44ef234738d6c5c041e75d46bcfa59472c retains the embedding microbatch fix and the upstream actual-context buffer sizing change from c0d5530f. Both changelog entries are preserved.

Validation on macOS arm64 / Python 3.12.14: the exact unchanged afe77e18f native build was reused (no rebuild/download); the configured original/native suite passed 41 tests in 10.14s. Six additional native controls (n_ctx=0/200/256, both logits modes, CPU layers) reached the actual 256-token context boundary with correctly sized Python history/logits buffers. Ruff check and format (30 files) plus whitespace checks passed. This is a local validation receipt, not a hosted-CI pass.


AI-assisted native-sync receipt (2026-11-22 UTC): merged current main 98c25a5b729ed48e67ce8e54c0cc1f8bd65f3f71 in 73675bab99870b8f2886176892546b22f791050c, retaining the embedding changelog entry and latest vendor entry, along with all upstream MTMD bindings and vendor changes. The embedding fix, regression tests and CI model fixtures are unchanged.

Rebuilt the original package against exact vendor afe77e18fadc2e673c6a6299495a443e17378ce4 on macOS arm64 / Python 3.12.14, offline with cached models and two build jobs (-DGGML_NATIVE=OFF -DGGML_ACCELERATE=ON; Metal remains available). The complete configured local suite passed 41 tests in 11.30s, including CPU-layer embedding batch-limit and generation controls. The rebuilt local native/MTMD libraries load, including the new MTMD audio_duplex structure field. Full Ruff check/format (30 files) and whitespace checks pass. This is local native validation, not hosted CI or other-platform validation.

Earlier dated receipts retained unchanged below:

AI-assisted native-sync receipt (2026-11-21 UTC): merged current main 7fcf2743f97d21f815fa3db398b4df90c655b713 in 1ee78e17c6c87aac9c2ae724a1dc2bcbe6c2f3e8, preserving both Unreleased changelog entries and all upstream bindings/MTMD/vendor updates. The embedding fix, regression tests and CI model fixtures are unchanged.

Rebuilt the original native package against exact vendor 46eb1742db6d2caec1bfa848c5702b1bdcc815e5 on macOS arm64 / Python 3.12.14, offline with cached models and two build jobs (-DGGML_NATIVE=OFF -DGGML_ACCELERATE=ON; Metal remains available in the build). The complete configured local suite passed 41 tests in 17.81s, including the CPU-layer long-embedding, aggregate-batch, explicit-limit and causal controls. Full Ruff check/format (30 files) and whitespace checks pass. This is current local native validation, not hosted CI or other-platform validation.

Previous receipts retained unchanged below (their dates/vendor-specific results are historical):

AI-assisted CI-sync receipt: merged current main 0542f207802fcfd174167dac2b228abe2f12093d in c19d94e9ee55e3a7fc9cd1a221d5535a72aa59f7, retaining both Unreleased changelog entries and the upstream release-runner update. The inherited release workflows remove unsupported macOS 14 runners; their YAML parses and the resulting matrices retain macOS 15 (plus Ubuntu/Windows for the general wheel build). Whitespace checks pass. No release job or hosted CI run is claimed.

All owning Python, tests, native vendor a956a852, build files and dependencies remain unchanged. The actual native build / 41 passed in 15.76s and prior full Ruff checks below are reused, not rerun for this CI/changelog-only synchronization.

Previous release-sync and native validation receipts (retained unchanged):

AI-assisted release-sync receipt: merged current main 46816d22f55432ecd4b81e2b2ff99dc345578fe9 in 5fbd385dbad25287e033019d1dc83c1c76077662, retaining the embedding entry under Unreleased and the upstream 0.3.37 release section. This merge changes only the release heading and package version metadata; embedding source, tests, native vendor a956a852, CMake and dependencies are unchanged.

The actual native build and 41 passed in 15.76s result below were reused, not rerun for this metadata-only synchronization. The current package imports as 0.3.37 with the existing native library; Ruff check/format of all 30 Python files and whitespace checks pass. No new hosted-CI or other-platform validation is claimed.

Previous native validation receipt (2026-11-14, retained unchanged):

AI-assisted maintenance update: merged current main dd70c923 in 764ea76cabb7d7270c3d2f0590d6348529f4f982, retaining the embedding fix and both changelog entries alongside the upstream native bindings/vendor update.

Rebuilt the native library against actual vendor a956a852c9c8bc1fb07206364838ddef299178c0 on macOS arm64 / Python 3.12.14 with -DGGML_NATIVE=OFF -DGGML_ACCELERATE=ON (two build jobs). The complete configured local suite passed: 41 passed in 15.76s, including the CPU long-embedding, aggregate-batch, explicit-limit and causal controls. Models were reused from the local cache in offline mode. ruff check llama_cpp tests, ruff format --check llama_cpp tests and git diff --check also pass. This is fresh native verification for this vendor; it does not claim hosted CI or other-platform validation.

@abetlen, when convenient, could you review the non-causal embedding batch-limit change?

@original4422

Copy link
Copy Markdown
Author

AI-assisted follow-up: Hi @abetlen, when convenient, could you review this non-causal embedding batch-limit fix and let me know whether changes are needed? The earlier native validation remains recorded above. Thank you!

@original4422

Copy link
Copy Markdown
Author

Hi @abetlen, could you review the non-causal embedding microbatch fix for #1762 when convenient, or advise on any changes needed? This is an AI-assisted follow-up. The Nov 14 native run recorded above passed 41 tests on macOS arm64; that is historical validation, not a test of the latest vendor update or a current CI claim. Thank you!

@original4422

Copy link
Copy Markdown
Author

Hi @abetlen, when convenient, could you review the non-causal embedding batch-limit fix for #1762 and let me know whether changes are needed? The AI-assisted update records the Nov 23 native run (41 tests on macOS arm64) and its platform limits; those tests have not been rerun today, and no hosted-CI pass is claimed. Thank you!

This branch has not been deployed

No deployments
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.

Llama.embed crashes when n_batch > 512

1 participant