fix: guard model.half() with dtype check in all rerankers#1574
Open
nedeadinside wants to merge 3 commits intoFlagOpen:masterfrom
Open
fix: guard model.half() with dtype check in all rerankers#1574nedeadinside wants to merge 3 commits intoFlagOpen:masterfrom
nedeadinside wants to merge 3 commits intoFlagOpen:masterfrom
Conversation
Calling .half() on an already-FP16 model raises an error, causing
rerankers with use_fp16=True to crash on every request after the first.
Add a dtype guard so the conversion only runs when needed:
if self.use_fp16 and next(self.model.parameters()).dtype != torch.float16:
self.model.half()
Fixes BaseReranker, BaseLLMReranker, LightweightLLMReranker,
LayerWiseLLMReranker, and MatroyshkaReranker.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
fix: guard model.half() with dtype check in all rerankers
There was a problem hiding this comment.
Pull request overview
This PR prevents reranker inference from repeatedly calling model.half() when the loaded model is already FP16, which can crash subsequent requests when use_fp16=True. It adds a dtype guard in the compute_score_single_gpu path across the core rerankers (and the Matroyshka research reranker).
Changes:
- Guard
self.model.half()behind a dtype check (!= torch.float16) to avoid redundant FP16 conversion. - Apply the guard consistently across encoder-only, decoder-only, and Matroyshka reranker implementations.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| research/Matroyshka_reranker/inference/rank_model.py | Add FP16 dtype guard before calling model.half() in Matroyshka reranker inference. |
| FlagEmbedding/inference/reranker/encoder_only/base.py | Add FP16 dtype guard before calling model.half() in the encoder-only reranker. |
| FlagEmbedding/inference/reranker/decoder_only/lightweight.py | Add FP16 dtype guard before calling model.half() in the lightweight LLM reranker. |
| FlagEmbedding/inference/reranker/decoder_only/layerwise.py | Add FP16 dtype guard before calling model.half() in the layer-wise LLM reranker. |
| FlagEmbedding/inference/reranker/decoder_only/base.py | Add FP16 dtype guard before calling model.half() in the base decoder-only LLM reranker. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@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.
Calling .half() on an already-FP16 model raises an error, causing rerankers with use_fp16=True to crash on every request after the first. Add a dtype guard so the conversion only runs when needed:
if self.use_fp16 and next(self.model.parameters()).dtype != torch.float16:
self.model.half()
Fixes BaseReranker, BaseLLMReranker, LightweightLLMReranker, LayerWiseLLMReranker, and MatroyshkaReranker.