fix(sarm): handle BaseModelOutputWithPooling from transformers 5.x in…#3419
Open
masato-ka wants to merge 3 commits intohuggingface:mainfrom
Open
fix(sarm): handle BaseModelOutputWithPooling from transformers 5.x in…#3419masato-ka wants to merge 3 commits intohuggingface:mainfrom
masato-ka wants to merge 3 commits intohuggingface:mainfrom
Conversation
… CLIP encoding In transformers 5.x, CLIPModel.get_image_features() and get_text_features() return BaseModelOutputWithPooling instead of a plain torch.FloatTensor. Added isinstance check to extract pooler_output when the return value is not a tensor, maintaining backward compatibility with transformers 4.x. Fixes AttributeError: 'BaseModelOutputWithPooling' object has no attribute 'detach' Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
ChuyaoShen
reviewed
Apr 20, 2026
| # transformers 5.x returns BaseModelOutputWithPooling instead of a plain tensor | ||
| output = self.clip_model.get_image_features(**inputs) | ||
| if not isinstance(output, torch.Tensor): | ||
| output = output.pooler_output |
There was a problem hiding this comment.
nit: maybe we should assert output is not None to please mypy.
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.
Summary / Motivation
In transformers 5.x,
CLIPModel.get_image_features()andget_text_features()return a
BaseModelOutputWithPoolingobject instead of a plaintorch.FloatTensor.This caused an
AttributeError: 'BaseModelOutputWithPooling' object has no attribute 'detach'when running SARM policies with transformers 5.x.This PR adds an
isinstancecheck inSARMEncodingProcessorStepto extractpooler_outputwhen the return value is not a plain tensor, maintaining fullbackward compatibility with transformers 4.x.
Related issues
What changed
src/lerobot/policies/sarm/processor_sarm.py: In both_encode_images()and_encode_text(), added a guard to unwrapBaseModelOutputWithPooling.pooler_outputwhen
get_image_features()/get_text_features()does not return a plaintorch.Tensor. No behavioral change under transformers 4.x.How was this tested (or how to run locally)
AttributeErrorwith transformers 5.x,confirmed it is resolved after this fix.
the fix is a two-line guard and straightforward to inspect.
To reproduce the original error:
Checklist (required before merge)
here: # (insert PR number/link)
Reviewer notes
symmetric hunks (one for image, one for text encoding).
features in all HuggingFace model outputs — equivalent to what the old plain-tensor
return contained.