Skip to content

Conversation

@leejianwoo-collab
Copy link

@leejianwoo-collab leejianwoo-collab commented Dec 16, 2025

feat: Add device consistency support to BatchFeature and ProcessorMixin

fix : #42722

What does this PR do?

feat: Add device consistency support to BatchFeature and ProcessorMixin

  • Add device parameter to BatchFeature.init for automatic tensor device placement
  • Add device parameter to ProcessingKwargs as a common argument alongside return_tensors
  • Update ProcessorMixin.call to pass device parameter to BatchFeature
  • Add comprehensive documentation explaining device parameter usage
  • Implement device consistency logic that moves all tensors to the specified device

This change addresses the device inconsistency issue in OneFormerProcessor and provides
a general solution for all processors that use BatchFeature. The implementation
follows community feedback to centralize device handling in BatchFeature rather than
individual processors.

Usage: processor(image, ["semantic"], return_tensors="pt", device="cuda")

Fixes #42722

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

feat: Add device consistency support to BatchFeature and ProcessorMixin
@leejianwoo-collab leejianwoo-collab changed the title feat: Add device consistency support to BatchFeature and ProcessorMixin feat: Add device consistency support to BatchFeature and ProcessorMixin #42722 Dec 16, 2025
@github-actions
Copy link
Contributor

View the CircleCI Test Summary for this PR:

https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=42892&sha=3a5eeb

Copy link
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @leejianwoo-collab ! I am not sure this is a good issue to choose if it's your first contribution. I would love for someone from transformers to work on it

Comment on lines +86 to +87
if device is not None:
self.to(device)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo instead of doing this, it needs to be passed to convert_to_tensors. And then we can use if tensor_type is 'pt' as torch.tensor(value, device=device)

Comment on lines +663 to +665
# Extract device parameter if present
device = kwargs.get("device")
return BatchFeature(outputs, device=device)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will not really work with structured inputs. It needs to be in common kwargs which will structure it properly in self.merge_kwargs. Then we can re-use it from merged_inputs

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.

Ensure that OneFormerProcessor place text task_inputs to the same device as other inputs

2 participants