-
Notifications
You must be signed in to change notification settings - Fork 32.8k
[docs] vlm addition #45271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stevhliu
wants to merge
3
commits into
huggingface:main
Choose a base branch
from
stevhliu:new-vlm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+90
−0
Open
[docs] vlm addition #45271
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| <!--Copyright 2026 The HuggingFace Team. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations under the License. | ||
|
|
||
| ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be | ||
| rendered properly in your Markdown viewer. | ||
|
|
||
| --> | ||
|
|
||
| # Add vision processing components | ||
|
|
||
| Adding a vision language model (VLM) requires two image processor classes on top of the standard [modular](./modular_transformers) approach. | ||
|
|
||
| > [!NOTE] | ||
| > For the modeling and config steps, follow the [modular](./modular_transformers) guide first. | ||
|
|
||
| - [torchvision](https://docs.pytorch.org/vision/stable/index.html) backend is the default and supports GPU acceleration. | ||
| - [PIL](https://pillow.readthedocs.io/en/stable/index.html) backend is a fallback when no GPU is available. | ||
|
|
||
| Both classes share the same preprocessing logic but have different backends. Their constructor signatures and default values must be identical. [`AutoImageProcessor.from_pretrained()`] selects the backend at load time and falls back to PIL when torchvision isn't available. Mismatched signatures cause the same saved config to behave differently across environments. | ||
|
|
||
| ## torchvision | ||
|
|
||
| Create `image_processing_<model_name>.py` with a class that inherits from [`TorchvisionBackend`]. Define a kwargs class first if your processor needs custom parameters beyond the standard [`ImagesKwargs`]. | ||
|
|
||
| ```py | ||
| from ...image_processing_backends import TorchvisionBackend | ||
| from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, PILImageResampling | ||
| from ...processing_utils import ImagesKwargs, Unpack | ||
| from ...utils import auto_docstring | ||
|
|
||
| class MyModelImageProcessorKwargs(ImagesKwargs, total=False): | ||
| tile_size: int # any model-specific kwargs | ||
|
|
||
| @auto_docstring | ||
| class MyModelImageProcessor(TorchvisionBackend): | ||
| resample = PILImageResampling.BICUBIC | ||
| image_mean = OPENAI_CLIP_MEAN | ||
| image_std = OPENAI_CLIP_STD | ||
| size = {"shortest_edge": 224} | ||
| do_resize = True | ||
| do_rescale = True | ||
| do_normalize = True | ||
| do_convert_rgb = True | ||
|
|
||
| def __init__(self, **kwargs: Unpack[MyModelImageProcessorKwargs]): | ||
| super().__init__(**kwargs) | ||
| ``` | ||
|
|
||
| ## PIL | ||
|
|
||
| Create `image_processing_pil_<model_name>.py` with a class that inherits from [`PilBackend`]. Import the kwargs class from the torchvision file, but don't redefine it. Sharing the same class keeps both backends' kwargs in sync. For processors with no custom parameters, use [`ImagesKwargs`] directly. | ||
|
|
||
| ```py | ||
| from ...image_processing_backends import PilBackend | ||
| from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, PILImageResampling | ||
| from ...utils import auto_docstring | ||
| from .image_processing_<model_name> import MyModelImageProcessorKwargs | ||
|
|
||
| @auto_docstring | ||
| class MyModelImageProcessorPil(PilBackend): | ||
| resample = PILImageResampling.BICUBIC | ||
| image_mean = OPENAI_CLIP_MEAN | ||
| image_std = OPENAI_CLIP_STD | ||
| size = {"shortest_edge": 224} | ||
| do_resize = True | ||
| do_rescale = True | ||
| do_normalize = True | ||
| do_convert_rgb = True | ||
|
|
||
| def __init__(self, **kwargs: Unpack[MyModelImageProcessorKwargs]): | ||
| super().__init__(**kwargs) | ||
| ``` | ||
|
|
||
| > [!TIP] | ||
| > See [`CLIPImageProcessor`]/[`CLIPImageProcessorPil`] and [`LlavaOnevisionImageProcessor`]/[`LlavaOnevisionImageProcessorPil`] for reference. | ||
|
|
||
| ## Next steps | ||
|
|
||
| - Read the [Auto-generating docstrings](./auto_docstring) guide to auto-generate consistent docstrings with `@auto_docstring`. | ||
| - Read the [Writing model tests](./testing) guide to write integration tests for your model. | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tbh this is not VLM-specific so prob we can name it differently. Like adding vision components or vision-processing components
Then we can add another for audio/video components if needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea, i like the flexibility!