Skip to content

Fix MaxSim memory use for long query batches - #4153

Merged
tomaarsen merged 3 commits into
huggingface:mainfrom
Arthur031221:fix-maxsim-query-chunking-4151-s4
Oct 13, 2026
Merged

tomaarsen merged 3 commits into
huggingface:mainfrom
Arthur031221:fix-maxsim-query-chunking-4151-s4

Conversation

@Arthur031221

Copy link
Copy Markdown
Contributor

Fixes #4151.

A long query could push late-interaction retrieval batches over chunk_elements. MaxSim padded every query to the longest one and always scored at least one document, leaving the scoring intermediate unbounded.

This chunks queries and documents separately, counting both padded inputs and the 4D intermediate against the budget. Query chunks are sized against the widest document. Documents are processed again for each query chunk, and a single pair can still exceed the budget. With 200 16-token queries, one 2,000-token query and four 1,000-token documents (dim 128, fp16, budget 10M), extra peak allocated CUDA memory fell from 869.7 MiB to 18.3 MiB, with identical scores.

Shared masks and padding dtypes behave as before.

Added budget, gradient, mask and dtype regressions. .venv/bin/python -m pytest -q tests/util/test_similarity.py passes. The offline full-suite run had existing failures and missing model fixtures.

Assisted by Claude/Codex.

@tomaarsen

Copy link
Copy Markdown
Member

Hello!

I've pushed some changes to simplify the implementation, tests, and docstrings. The mixed-dtype compatibility logic added complexity and slowed ordinary inputs, so I removed it while keeping the query chunking fix.

In the #4151 reproduction, scoring was about 12x faster and peak additional CUDA memory fell from 871 MiB to 17 MiB, with identical scores. Most standard cases were unchanged, though the small control case added about 0.15 ms. I think I'll merge this when the tests pass.

  • Tom Aarsen

@tomaarsen
tomaarsen merged commit 0d7e81f into huggingface:main Oct 13, 2026
12 checks passed
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.

maxsim pads all queries to the longest one

2 participants