Improve embedder and reranker throughput with bf16 loading and single-call tokenization#1757
Open
oliverholworthy wants to merge 2 commits intoNVIDIA:mainfrom
Open
Conversation
7346422 to
c54b7ff
Compare
The text-only embedder was loading the model in float32 (HuggingFace default) and relying on torch.autocast during inference. This is slower than loading natively in bfloat16 — which is what the VL variant already does — because autocast introduces casting overhead on top of fp32 memory bandwidth costs. Additionally, the tokenizer was called once per batch chunk (16 calls for 1000 texts at batch_size=64). HuggingFace tokenizers have per-call setup overhead (padding computation, tensor allocation) that doesn't scale with batch size, so a single tokenize-all-then-slice approach saves ~80ms per 1000 texts. Combined these changes improve throughput from ~325 to ~381 texts/sec (+17%), closing the gap with the VL embedder (~386 texts/sec). Signed-off-by: Oliver Holworthy <1216955+oliverholworthy@users.noreply.github.com>
c54b7ff to
205011e
Compare
Same optimization as the embedder: call the tokenizer once with all texts then slice into GPU batches, instead of calling per chunk. Applied to both score() and score_pairs() methods. Signed-off-by: Oliver Holworthy <1216955+oliverholworthy@users.noreply.github.com>
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.
Description
Improve inference throughput for the text-only embedder and reranker by addressing two inefficiencies:
1. Load embed-1b-v2 in native bfloat16 (
torch_dtype=torch.bfloat16)The VL embedder and reranker already load in bf16, but the text-only embedder was loading in fp32 and relying on
torch.autocast. The autocast wrapper is removed since it adds casting overhead on bf16 weights. The last hidden state is upcast to float32 before pooling and normalization to avoid accumulation errors in bf16 reductions.2. Tokenize all texts in a single call (embedder + reranker)
Both models were calling the tokenizer per batch chunk inside the inference loop. HuggingFace tokenizers have per-call setup overhead that doesn't scale with batch size, so a single tokenize-all-then-slice approach is significantly faster.
In local benchmarking on an Ampere-class GPU:
Checklist