-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
[Model] Molmo vLLM Integration #9016
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 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
19154e0
molmo vllm integration
mrsalehi 4af9e3f
rmvd import
mrsalehi 72c365e
code cleanup
sangho-vision d4e2980
Merge branch 'main' into molmo
ywang96 0a8946a
format
ywang96 81c759d
delete old example
ywang96 beecc01
add molmo to correct registry
ywang96 877e71f
a lot of format cleanup
ywang96 46bad99
fix comment change
ywang96 ed41e0b
add NOTE
ywang96 aff4040
cleanup weight loading and error handling
ywang96 d48a28f
add to documentation
ywang96 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 |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import argparse | ||
| import numpy as np | ||
| import requests | ||
| from io import BytesIO | ||
| import base64 | ||
| from PIL import Image, ImageFile, ImageOps | ||
| import torch | ||
| from typing import Optional | ||
|
|
||
| from vllm import LLM | ||
| from vllm.sampling_params import SamplingParams | ||
|
|
||
|
|
||
| ImageFile.LOAD_TRUNCATED_IMAGES = True | ||
|
|
||
|
|
||
| def download_image_to_numpy(url): | ||
| # Send a GET request to the URL | ||
| response = requests.get(url) | ||
|
|
||
| # Check if the request was successful | ||
| if response.status_code == 200: | ||
| # Open the image from the response content | ||
| image = Image.open(BytesIO(response.content)).convert("RGB") | ||
|
|
||
| image = ImageOps.exif_transpose(image) | ||
|
|
||
| # Convert the image to a NumPy array | ||
| image_array = np.array(image).astype(np.uint8) | ||
ywang96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return image_array | ||
| else: | ||
| raise Exception(f"Failed to download image. Status code: {response.status_code}") | ||
|
|
||
|
|
||
| def vllm_generate(): | ||
| inputs = [ | ||
| { | ||
| "prompt": "Describe this image.", | ||
| "multi_modal_data": {"image": download_image_to_numpy("https://picsum.photos/id/9/1080/720")} | ||
| }, | ||
| { | ||
| "prompt": "Describe what you see in this image.", | ||
| "multi_modal_data": {"image": download_image_to_numpy("https://picsum.photos/id/23/1080/720")} | ||
| }, | ||
| ] | ||
|
|
||
| outputs = llm.generate( | ||
| inputs, | ||
| sampling_params=sampling_params | ||
| ) | ||
|
|
||
| for o in outputs: | ||
| generated_text = o.outputs[0].text | ||
| print(generated_text) | ||
|
|
||
|
|
||
| def vllm_chat(): | ||
| url_1 = "https://picsum.photos/id/9/1080/720" | ||
| messages = [ | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| { | ||
| "type": "text", | ||
| "text": "Describe the image." | ||
| }, | ||
| { | ||
| "type": "image_url", | ||
| "image_url": { | ||
| "url": url_1 | ||
| } | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| "role": "assistant", | ||
| "content": "The image shows some objects.", | ||
| }, | ||
| { | ||
| "role": "user", | ||
| "content": "What objects do you exactly see in the image?", | ||
| }, | ||
| ] | ||
|
|
||
| outputs = llm.chat( | ||
| messages, | ||
| sampling_params=sampling_params | ||
| ) | ||
|
|
||
| # Error: Invalid message role {{ message['role'] }} at index {{ loop.index }} | ||
| for o in outputs: | ||
| generated_text = o.outputs[0].text | ||
| print(generated_text) | ||
|
|
||
|
|
||
| def set_tf_memory_growth(): | ||
| import tensorflow as tf | ||
| gpus = tf.config.experimental.list_physical_devices('GPU') | ||
|
|
||
| if gpus: | ||
| try: | ||
| # Set memory growth for each GPU | ||
| for gpu in gpus: | ||
| tf.config.experimental.set_memory_growth(gpu, True) | ||
| print("Memory growth set for GPUs") | ||
| except RuntimeError as e: | ||
| print(e) | ||
ywang96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser( | ||
| description="vllm example for Molmo-7B-O-0924, Molmo-7B-D-0924, Molmo-72B-0924" | ||
| ) | ||
| parser.add_argument("--model_path", type=str, default="allenai/Molmo-7B-D-0924") | ||
| args = parser.parse_args() | ||
| llm = LLM( | ||
| model=args.model_path, | ||
| trust_remote_code=True, | ||
| gpu_memory_utilization=0.95, | ||
| dtype="bfloat16", | ||
| ) | ||
| sampling_params = SamplingParams( | ||
| max_tokens=768, | ||
| temperature=0, | ||
| ) | ||
| set_tf_memory_growth() | ||
| vllm_generate() | ||
| vllm_chat() | ||
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
Oops, something went wrong.
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.
It would be great if you can move this example to
examples/offline_inference_vision_language.py