Skip to content

Conversation

@yonigozlan
Copy link
Member

I have been wanting to change that for a while, it shouldn't be a breaking change, but align what we support in BatchFeature between numpy arrays and torch tensors.
The issue was that np.array() works on lists of array and even nested list of arrays), but torch.tensor() doesn't, so if we tried to call batch feature, with lists of tensors, we would get an error. I haven't added support for nested lists of tensors as I haven't seen the need anywhere.
Also the errors we were getting were very generic, and not very useful, this should help with that.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

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.

Great PR! Left a few questions to make sure I got it right

return BatchFeature(
data={"pixel_values": processed_images, "patch_offsets": patch_offsets},
tensor_type=return_tensors,
skip_tensor_conversion=["patch_offsets"],
Copy link
Member

Choose a reason for hiding this comment

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

For my understanding, do we skip the conversion because offsets aren't padded?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep! We used to create the batch then add the non-converted path_offsets, but I fell it's cleaner to be explicit this way

Comment on lines 92 to 95
BatchFeature class for Fuyu image processor and processor.
The outputs dictionary from the processors contains a mix of tensors and lists of tensors.
"""
Copy link
Member

Choose a reason for hiding this comment

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

looks like Fuyu has its own BatchFeature because we couldn't skip conversion or convert nested lists? 🤔 Can you inspect and maybe remove it if possible, now that the base class supports skipping?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I can do that in another PR! I didn't look into too much details what the Fuyu batch feature does, but it would be great to get rid of it

Comment on lines +122 to +127

# stack list of tensors if tensor_type is PyTorch (# torch.tensor() does not support list of tensors)
if isinstance(value, (list, tuple)) and len(value) > 0 and torch.is_tensor(value[0]):
return torch.stack(value)

# convert list of numpy arrays to numpy array (stack) if tensor_type is Numpy
Copy link
Member

Choose a reason for hiding this comment

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

i dunno if you saw the PR. Community member noticed that VideoMetadata objects throw and error when return type is 'pt', because they can't be converted to tensors

I think we can add the fix here by checking if value is a list/array/etc and early existing otherwise. We won't be able to convert non-list objects anyway

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed, I was just wondering why we restricted BatchFeature to only be able to contain arrays/tensors structures in the first place, just to make sure we wouldn't break an important assumption by silently allowing other objects in BatchFeature.
Also these changes should be made along with changes to the ".to()" method no?


mean_value = round(pixel_values.mean().item(), 4)
self.assertEqual(mean_value, 0.2353)
self.assertEqual(mean_value, -0.2303)
Copy link
Member

Choose a reason for hiding this comment

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

to make sure: was it a typo or did this PR change pixel outputs?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll have to check on a CI runner if that value is correct, but this tests failed on main on my end (using an A10)

Comment on lines +43 to +45
class BatchFeatureTester(unittest.TestCase):
"""Tests for the BatchFeature class and tensor conversion."""

Copy link
Member

Choose a reason for hiding this comment

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

thanks for adding a test 🤩

@github-actions
Copy link
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: beit, bridgetower, cohere2_vision, convnext, deepseek_vl, deepseek_vl_hybrid, depth_pro, dinov3_vit, donut, dpt, efficientloftr, efficientnet, eomt, flava, fuyu, gemma3

@yonigozlan yonigozlan force-pushed the improve-batch-feature branch from e2ceac2 to 875c36e Compare December 10, 2025 18:33
@yonigozlan yonigozlan changed the title Improve BatchFeature: stack list and nested lists of torch tensors Improve BatchFeature: stack list and lists of torch tensors Dec 10, 2025
@github-actions
Copy link
Contributor

View the CircleCI Test Summary for this PR:

https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=42750&sha=e2ceac

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.

3 participants