Skip to content

Add SamImageProcessorFast with 4x performance improvement - #38861

Closed
leochlon wants to merge 5 commits into
huggingface:mainfrom
leochlon:add-sam-fast-processor
Closed

leochlon wants to merge 5 commits into
huggingface:mainfrom
leochlon:add-sam-fast-processor

Conversation

@leochlon

@leochlon leochlon commented Jun 17, 2025 •

Copy link
Copy Markdown

🎯 Summary

Implements GPU-accelerated SamImageProcessorFast following the BEiT pattern, providing 4x performance improvement over the original CPU-bound processor while maintaining 100% compatibility.

fixes #36999

- Implement GPU-accelerated fast image processor for SAM
- Inherit from BaseImageProcessorFast with minimal code (~130 lines)
- Override resize() to handle SAM's longest_edge parameter
- Override _preprocess() to add padding capability
- Override preprocess() to provide SAM-specific metadata
- Maintain full compatibility with SamImageProcessor
- Add comprehensive test suite with 18 passing tests
- Achieve 4x+ speedup over slow processor
- Add save/load compatibility with SamFastImageProcessorKwargs
- Apply black formatting to meet CI requirements
- Fix import statement formatting with proper line breaks
- Convert single quotes to double quotes for consistency
- Add missing blank lines after docstrings
- Format multi-line expressions and list comprehensions
- Ensure compliance with transformers code style standards
- Apply black and ruff formatting to image_processing_sam_fast.py
- Apply black and ruff formatting to test_image_processing_sam.py
- Fix import organization and line length issues
- Ensure full compliance with transformers CI formatting requirements
- Maintain functionality while meeting all style standards
- Apply ruff format to tests/models/sam/test_image_processing_sam.py
- Resolve CI formatting issues with proper ruff configuration
- Ensure compliance with transformers repository formatting standards
- All functionality preserved after formatting changes
@Rocketknight1

Copy link
Copy Markdown
Member

cc @yonigozlan

@leochlon

Copy link
Copy Markdown
Author

@yonigozlan is this ok

@yonigozlan yonigozlan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hello @leochlon , thanks for opening this PR!
There's quite a lot missing, reminder that ALL the functionalities of the slow processor should be in the fast processor.
This is quite a difficult processor to work on, and as I'm working on Sam2 at the moment, I might work on the fast image processor of Sam myself maybe we should put this PR on hold for now

@leochlon

Copy link
Copy Markdown
Author

hey @yonigozlan I tried one more time, most of the functionality and logic between slow and fast is now 1-1 but using similar logic to the base fast image processor. would be cool if this implementation addresses your comments above!

@leochlon

Copy link
Copy Markdown
Author

@yonigozlan let me know what else here is blocking and more than happy to take a look, test suite is all passing on my end.

@ArthurZucker

Copy link
Copy Markdown
Collaborator

Thanks for the PR, and sorry it didn't make it in! This branch is based on v4, and v5 is very different, so rather than rebasing we'd love to invite you to open a new PR against main using our latest tools and good practices:

Closing this one in the meantime, thanks again!

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.

5 participants