Skip to content

fix: batch long texts by the CPU budget under CoreML - #931

Merged
joein merged 1 commit into
perf/coreml-gpufrom
pr916-long-text-cap
Dec 2, 2026
Merged

joein merged 1 commit into
perf/coreml-gpufrom
pr916-long-text-cap

Conversation

@joein

@joein joein commented Dec 2, 2026

Copy link
Copy Markdown
Member

Follow-up to #916, opened against its branch perf/coreml-gpu.

Long texts no longer hold gigabytes of outputs under CoreML. With CoreML on, _batch_token_budget returned None, so every batch held batch_size texts however long they were. Texts longer than the largest CoreML length already run on the CPU a few at a time, but the runner keeps every text's per-token output for the whole batch, plus a joined copy. For 256 texts of 8192 tokens on jina-v2-base or nomic, that's about 13 GB.

Texts up to the largest CoreML length (512 by default) still fill batches of batch_size, since CoreML pads them to fixed shapes. Longer texts now get the CPU's token budget, as without CoreML:

  • iter_length_sorted_windows takes unbudgeted_length: batches whose texts have at most that many tokens ignore max_batch_tokens. It defaults to 0, so other models batch exactly as before.
  • OnnxModel._unbudgeted_length() returns 0, and CoreMLMixin returns its largest length. If the user sets max_batch_tokens, it still applies to every batch.

Checked on an M2 Max with jinaai/jina-embeddings-v2-small-en, 64 short texts and 256 texts of 2043 tokens at the default batch_size:

Batches of long texts Embed time Peak memory
Before 1 × 256 texts 66.3 s 3.98 GB
After 128 × 2 texts 62.0 s 2.24 GB

The embeddings are identical, and the short texts still run as one full batch. With the default unbudgeted_length=0, the old and new windowing give the same batches in 3,000 random cases.

One 8192-token text still needs about 8 GB with jina-v2-small, on the plain CPU path too. That's onnxruntime's attention for a single text, which this change doesn't touch.

@joein
joein merged commit d2e1cdf into perf/coreml-gpu Dec 2, 2026
9 checks passed
@joein
joein deleted the pr916-long-text-cap branch December 2, 2026 12:12
joein added a commit that referenced this pull request Dec 2, 2026
* perf: run text embeddings on the Apple GPU with CoreML

* fix: use np.flatnonzero so mypy passes with numpy 2.4 stubs

* chore: Address review

* chore: forward max_batch_tokens to CoreML workers

* refactor: move CoreML support into CoreMLMixin

* chore: run CoreML in the current process, ignoring parallel

* fix: fall back when the CoreML export or cache is unavailable (#930)

* fix: fall back when the CoreML export or cache is unavailable

* tests: remove CoreML fallback tests

* fix: lock CoreML session creation and load all lengths in the constructor

* fix: default to 3 CoreML lengths, document their cost and shorten load logs

* fix: keep the CPU batch budget for texts longer than the CoreML lengths (#931)

* fix: fall back quickly and quietly when the CoreML export can't be downloaded (#933)

* fix: download the CoreML export once and quietly

* fix: call the CoreML export the CoreML-compatible copy in the warning

* fix: run each CoreML batch of long texts in one CPU run (#932)

* docs: list CoreML models in the README and shorten its example

* fix: ignore parallel only for CoreML text models, with a warning (#934)

* fix: ignore parallel only for CoreML text models, with a warning

* fix: resolve parallel through a general hook that CoreML overrides

---------

Co-authored-by: George <george.panchuk@qdrant.tech>
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