-
Notifications
You must be signed in to change notification settings - Fork 926
smolvlm2: code snippet changes #2711
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,10 +81,11 @@ It’s so memory efficient, that you can run it even in a free Google Colab. | |
| <summary>Python Code</summary> | ||
|
|
||
| ```python | ||
| # Make sure we are running the latest version of Transformers | ||
| !pip install git+https://github.com/huggingface/transformers.git | ||
| # Install transformers from `main` or from this stable branch: | ||
| !pip install git+https://github.com/huggingface/transformers@v4.49.0-SmolVLM-2 | ||
|
|
||
| from transformers import AutoProcessor, AutoModelForImageTextToText | ||
| import torch | ||
|
|
||
| model_path = "HuggingFaceTB/SmolVLM2-2.2B-Instruct" | ||
| processor = AutoProcessor.from_pretrained(model_path) | ||
|
|
@@ -110,7 +111,7 @@ inputs = processor.apply_chat_template( | |
| tokenize=True, | ||
| return_dict=True, | ||
| return_tensors="pt", | ||
| ).to(model.device) | ||
| ).to(model.device, dtype=torch.bfloat16) | ||
|
|
||
| generated_ids = model.generate(**inputs, do_sample=False, max_new_tokens=64) | ||
| generated_texts = processor.batch_decode( | ||
|
|
@@ -193,27 +194,25 @@ The easiest way to run inference with the SmolVLM2 models is through the convers | |
| You can load the model as follows. | ||
|
|
||
| ```python | ||
|
|
||
| # Make sure we are running the latest version of Transformers | ||
| !pip install git+https://github.com/huggingface/transformers.git | ||
| # Install transformers from `main` or from this stable branch: | ||
| !pip install git+https://github.com/huggingface/transformers@v4.49.0-SmolVLM-2 | ||
|
|
||
| from transformers import AutoProcessor, AutoModelForImageTextToText | ||
| import torch | ||
|
|
||
| processor = AutoProcessor.from_pretrained(model_path) | ||
| model = AutoModelForImageTextToText.from_pretrained( | ||
| model_path, | ||
| torch_dtype=torch.bfloat16, | ||
| _attn_implementation="flash_attention_2" | ||
| ).to(DEVICE) | ||
| ).to("cuda") | ||
| ``` | ||
|
|
||
| #### Video Inference | ||
|
|
||
| You can pass videos through a chat template by passing in `{"type": "video", "path": {video_path}`. See below for a complete example. | ||
|
|
||
| ```python | ||
| import torch | ||
|
|
||
| messages = [ | ||
| { | ||
| "role": "user", | ||
|
|
@@ -230,7 +229,7 @@ inputs = processor.apply_chat_template( | |
| tokenize=True, | ||
| return_dict=True, | ||
| return_tensors="pt", | ||
| ).to(model.device) | ||
| ).to(model.device, dtype=torch.bfloat16) | ||
|
|
||
| generated_ids = model.generate(**inputs, do_sample=False, max_new_tokens=64) | ||
| generated_texts = processor.batch_decode( | ||
|
|
@@ -245,19 +244,16 @@ print(generated_texts[0]) | |
|
|
||
| #### Multiple Image Inference | ||
|
|
||
| In addition to video, SmolVLM2 supports multi-image conversations. You can use the same API through the chat template. | ||
| In addition to video, SmolVLM2 supports multi-image conversations. You can use the same API through the chat template, providing each image using a filesystem path, an URL, or a `PIL.Image` object: | ||
|
|
||
| ```python | ||
| import torch | ||
|
|
||
|
|
||
| messages = [ | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| {"type": "text", "text": "What are the differences between these two images?"}, | ||
| {"type": "image", "path": "image_1.png"}, | ||
| {"type": "image", "path": "image_2.png"} | ||
| {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"}, | ||
| {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg"}, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trying to make the example runnable (and fun) |
||
| ] | ||
| }, | ||
| ] | ||
|
|
@@ -268,7 +264,7 @@ inputs = processor.apply_chat_template( | |
| tokenize=True, | ||
| return_dict=True, | ||
| return_tensors="pt", | ||
| ).to(model.device) | ||
| ).to(model.device, dtype=torch.bfloat16) | ||
|
|
||
| generated_ids = model.generate(**inputs, do_sample=False, max_new_tokens=64) | ||
| generated_texts = processor.batch_decode( | ||
|
|
||
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.
Prefer to use the stable tagged release rather than
main