-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Support for NemotronH Nano VLM with an optimized vision model (vLLM native) #23753
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
Draft
danielafrimi
wants to merge
6
commits into
vllm-project:main
Choose a base branch
from
danielafrimi:vlm_optimized_pr
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.
Draft
Changes from all commits
Commits
Show all changes
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
import pytest | ||
import torch | ||
import torch.nn as nn | ||
from huggingface_hub import snapshot_download | ||
from transformers import AutoConfig, AutoModel, CLIPImageProcessor | ||
|
||
from vllm.distributed import cleanup_dist_env_and_memory | ||
from vllm.model_executor.models.radio import RadioModel | ||
from vllm.utils import STR_DTYPE_TO_TORCH_DTYPE | ||
|
||
from ....conftest import ImageTestAssets | ||
|
||
# we use snapshot_download to prevent conflicts between | ||
# dynamic_module and trust_remote_code for hf_runner | ||
DOWNLOAD_PATTERN = ["*.json", "*.py", "*.safetensors", "*.txt", "*.model"] | ||
|
||
|
||
def map_hf_radio_to_vllm_intern(hf_sd: dict, radio_vllm) -> dict: | ||
mapped = {} | ||
for k, v in hf_sd.items(): | ||
if not k.startswith("radio_model."): | ||
continue | ||
k2 = k[len("radio_model."):] | ||
|
||
# skip buffers not used in vLLM | ||
if k2 in {"summary_idxs"}: | ||
continue | ||
|
||
if k2.startswith("model.patch_generator."): | ||
mapped_key = f"model.patch_generator.{k2.split('.', 2)[-1]}" | ||
mapped[mapped_key] = v | ||
continue | ||
|
||
if k2.startswith("input_conditioner."): | ||
mapped_key = f"input_conditioner.{k2.split('.', 1)[-1]}" | ||
mapped[mapped_key] = v | ||
continue | ||
|
||
if k2.startswith("model.blocks."): | ||
parts = k2.split(".") | ||
layer_idx = parts[2] | ||
suffix = ".".join( | ||
parts[3:] | ||
) # e.g. norm1.weight, attn.qkv.weight, mlp.fc1.weight, etc. | ||
# ls1/ls2 do not exist in HF (Identity); vLLM has params – | ||
# leave them default | ||
if suffix in {"ls1", "ls2"} or suffix.startswith(("ls1.", "ls2.")): | ||
continue | ||
mapped_key = f"model.encoder.layers.{layer_idx}.{suffix}" | ||
mapped[mapped_key] = v | ||
continue | ||
|
||
return mapped | ||
|
||
|
||
VIT_DIMS = { | ||
"vit_small_patch16_224": (384, 12, 6, 1536), | ||
"vit_base_patch16_224": (768, 12, 12, 3072), | ||
"vit_large_patch16_224": (1024, 24, 16, 4096), | ||
"vit_huge_patch16_224": (1280, 32, 16, 5120), | ||
} | ||
|
||
|
||
def get_args_from_model_type(model_type): | ||
return VIT_DIMS[model_type] | ||
|
||
|
||
@torch.inference_mode() | ||
def run_radio_test( | ||
image_assets: ImageTestAssets, | ||
model_id: str, | ||
*, | ||
dtype: str, | ||
): | ||
model = snapshot_download(model_id, allow_patterns=DOWNLOAD_PATTERN) | ||
torch_dtype = STR_DTYPE_TO_TORCH_DTYPE[dtype] | ||
|
||
img_processor = CLIPImageProcessor.from_pretrained(model) | ||
images = [asset.pil_image for asset in image_assets] | ||
# Input resolution must be a multiple of `self.min_resolution_step`. | ||
# Using `self.get_nearest_supported_resolution`, for assets 432x642 the | ||
# nearest supported resolution is 432x640. | ||
pixel_values = [ | ||
img_processor( | ||
images, | ||
return_tensors='pt').pixel_values.to(torch_dtype)[:, :, :, :640] | ||
for images in images | ||
] | ||
|
||
config = AutoConfig.from_pretrained(model_id, trust_remote_code=True) | ||
|
||
hidden_size, num_layers, num_heads, intermediate_size = ( | ||
get_args_from_model_type(config.args["model"])) | ||
|
||
config.num_hidden_layers = num_layers | ||
config.hidden_size = hidden_size | ||
config.num_attention_heads = num_heads | ||
config.intermediate_size = intermediate_size | ||
config.norm_type = "layer_norm" | ||
config.image_size = 224 | ||
config.hidden_act = "gelu" | ||
config.layer_norm_eps = 1e-6 | ||
config.initializer_factor = 1.0 | ||
config.qkv_bias = True | ||
config.qk_normalization = False | ||
config.max_img_size = 2048 | ||
config.reg_tokens = config.args["register_multiple"] | ||
|
||
hf_model = AutoModel.from_pretrained( | ||
model_id, | ||
config=config, | ||
torch_dtype=torch_dtype, | ||
trust_remote_code=True, | ||
).to("cuda") | ||
hf_model.eval() | ||
|
||
hf_outputs_per_image = [ | ||
hf_model(pixel_value.to("cuda")).features | ||
for pixel_value in pixel_values | ||
] | ||
|
||
vllm_model = RadioModel(config) | ||
vllm_state_dict = map_hf_radio_to_vllm_intern(hf_model.state_dict(), | ||
vllm_model) | ||
vllm_model.load_state_dict(vllm_state_dict, strict=False) | ||
|
||
del hf_model | ||
cleanup_dist_env_and_memory() | ||
|
||
vllm_model = vllm_model.to("cuda", torch_dtype) | ||
vllm_outputs_per_image = [ | ||
vllm_model(pixel_values=pixel_value.to("cuda")) | ||
for pixel_value in pixel_values | ||
] | ||
del vllm_model | ||
cleanup_dist_env_and_memory() | ||
|
||
cos_similar = nn.CosineSimilarity(dim=-1) | ||
for vllm_output, hf_output in zip(vllm_outputs_per_image, | ||
hf_outputs_per_image): | ||
assert cos_similar(vllm_output, hf_output).mean() > 0.99 | ||
|
||
|
||
@pytest.mark.parametrize("model_id", [ | ||
"nvidia/C-RADIOv2-H", | ||
]) | ||
@pytest.mark.parametrize("dtype", ["half"]) | ||
def test_radio(dist_init, image_assets, model_id, dtype: str) -> None: | ||
run_radio_test( | ||
image_assets, | ||
model_id, | ||
dtype=dtype, | ||
) |
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.
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.
The list comprehension for
pixel_values
seems to have a bug. Theimg_processor
is called with a single PIL image, which returns a 3D tensor forpixel_values
. However, a 4D slice[:, :, :, :640]
is then applied to this 3D tensor, which will cause a runtime error.To fix this, you can process each image as a list containing a single image to ensure the
pixel_values
tensor is 4D. Also, using a more descriptive loop variable name would improve readability.