Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix max tokens
Browse files Browse the repository at this point in the history
Signed-off-by: DarkLight1337 <[email protected]>
DarkLight1337 committed Jan 29, 2025
1 parent 5604ec2 commit 24bf8c7
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions vllm/model_executor/models/internvl.py
Original file line number Diff line number Diff line change
@@ -36,7 +36,6 @@
from vllm.sequence import IntermediateTensors
from vllm.transformers_utils.tokenizer import AnyTokenizer

from .clip import get_clip_num_patches
from .interfaces import SupportsMultiModal, SupportsPP
from .utils import (AutoWeightsLoader, flatten_bn, init_vllm_registered_model,
maybe_prefix, merge_multimodal_embeddings)
@@ -180,16 +179,6 @@ def _get_max_dynamic_patch(
return max_dynamic_patch


def get_internvl_num_patches(hf_config: PretrainedConfig):
vision_config = hf_config.vision_config
downsample_ratio = hf_config.downsample_ratio
image_size = vision_config.image_size
patch_size = vision_config.patch_size
return int(
get_clip_num_patches(image_size=image_size, patch_size=patch_size) *
(downsample_ratio**2))


# adapted from https://huggingface.co/OpenGVLab/InternVL2-1B
def image_to_pixel_values(image: Image.Image, input_size: int, min_num: int,
max_num: int, use_thumbnail: bool) -> torch.Tensor:
@@ -377,8 +366,20 @@ def get_num_image_tokens(
def get_max_image_tokens(self) -> int:
processor = self.get_hf_processor()

hf_config = self.get_hf_config()
max_num_patches = get_internvl_num_patches(hf_config)
max_dynamic_patch = _get_max_dynamic_patch(
dynamic_image_size=processor.dynamic_image_size,
max_dynamic_patch=processor.max_dynamic_patch,
use_thumbnail=False, # Applied in calculate_targets
)

max_num_patches, _, _ = calculate_targets(
orig_width=processor.image_size,
orig_height=processor.image_size,
image_size=processor.image_size,
min_num=processor.min_dynamic_patch,
max_num=max_dynamic_patch,
use_thumbnail=processor.use_thumbnail,
)

return max_num_patches * processor.num_image_token

0 comments on commit 24bf8c7

Please sign in to comment.