[VLM] Move supported limits and max tokens to merged multi-modal processor (#11669)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk> Signed-off-by: Isotr0py <2037008807@qq.com> Co-authored-by: Isotr0py <2037008807@qq.com>
This commit is contained in:
@@ -40,7 +40,6 @@ from vllm.attention import AttentionMetadata
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.distributed import parallel_state
|
||||
from vllm.distributed import utils as dist_utils
|
||||
from vllm.inputs import InputContext
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor import SamplingMetadata
|
||||
from vllm.model_executor.layers.activation import QuickGELU
|
||||
@@ -650,8 +649,9 @@ def _get_vision_info(
|
||||
width: int,
|
||||
min_pixels: int,
|
||||
max_pixels: int,
|
||||
*,
|
||||
do_resize: bool = True,
|
||||
data_type_key: str = "image",
|
||||
modality: str = "image",
|
||||
mm_count: int = 1,
|
||||
):
|
||||
"""Get information (resized height / width and number of vision tokens)
|
||||
@@ -671,11 +671,12 @@ def _get_vision_info(
|
||||
else:
|
||||
resized_height, resized_width = height, width
|
||||
|
||||
if data_type_key == "image":
|
||||
if modality == "image":
|
||||
grid_t = mm_count
|
||||
else:
|
||||
assert data_type_key == "video"
|
||||
elif modality == "video":
|
||||
grid_t = max(mm_count // temporal_patch_size, 1)
|
||||
else:
|
||||
raise ValueError(f"Modality {modality} is not supported")
|
||||
|
||||
grid_h = resized_height // patch_size
|
||||
grid_w = resized_width // patch_size
|
||||
@@ -691,41 +692,11 @@ def _get_image_processor(hf_processor: Qwen2VLProcessor):
|
||||
return image_processor
|
||||
|
||||
|
||||
def get_max_qwen2_vl_mm_tokens(ctx: InputContext,
|
||||
data_type_key: str,
|
||||
*,
|
||||
min_pixels: Optional[int] = None,
|
||||
max_pixels: Optional[int] = None) -> int:
|
||||
hf_config = ctx.get_hf_config(Qwen2VLConfig)
|
||||
vision_config = hf_config.vision_config
|
||||
|
||||
hf_processor = ctx.get_hf_processor(Qwen2VLProcessor)
|
||||
image_processor = _get_image_processor(hf_processor)
|
||||
|
||||
_, _, max_llm_image_tokens = _get_vision_info(
|
||||
vision_config,
|
||||
height=9999999,
|
||||
width=9999999,
|
||||
min_pixels=min_pixels or image_processor.min_pixels,
|
||||
max_pixels=max_pixels or image_processor.max_pixels,
|
||||
data_type_key=data_type_key,
|
||||
)
|
||||
return max_llm_image_tokens
|
||||
|
||||
|
||||
get_max_qwen2_vl_image_tokens = partial(get_max_qwen2_vl_mm_tokens,
|
||||
data_type_key="image")
|
||||
get_max_qwen2_vl_video_tokens = partial(get_max_qwen2_vl_mm_tokens,
|
||||
data_type_key="video")
|
||||
|
||||
|
||||
class Qwen2EmbeddingItems(ModalityDataItems[dict[str, torch.Tensor],
|
||||
dict[str, torch.Tensor]]):
|
||||
|
||||
def __init__(self, data: dict, modality: str) -> None:
|
||||
super().__init__(data)
|
||||
|
||||
self.modality = modality
|
||||
super().__init__(data, modality)
|
||||
|
||||
grid_thw = data[f"{modality}_grid_thw"]
|
||||
slice_idxs = [0] + grid_thw.prod(-1).cumsum_(0).tolist()
|
||||
@@ -734,9 +705,6 @@ class Qwen2EmbeddingItems(ModalityDataItems[dict[str, torch.Tensor],
|
||||
for i in range(len(grid_thw))
|
||||
]
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (f"{type(self).__name__}(modality={self.modality!r})")
|
||||
|
||||
def get_count(self) -> int:
|
||||
return len(self.data[f"{self.modality}_grid_thw"])
|
||||
|
||||
@@ -792,6 +760,32 @@ class Qwen2MultiModalDataParser(MultiModalDataParser):
|
||||
|
||||
class Qwen2VLMultiModalProcessor(BaseMultiModalProcessor):
|
||||
|
||||
def get_supported_mm_limits(self) -> Mapping[str, Optional[int]]:
|
||||
return {"image": None, "video": None}
|
||||
|
||||
def _get_max_mm_tokens(self, modality: str) -> int:
|
||||
hf_config = self.ctx.get_hf_config(Qwen2VLConfig)
|
||||
vision_config = hf_config.vision_config
|
||||
|
||||
hf_processor = self._get_hf_processor()
|
||||
image_processor = _get_image_processor(hf_processor)
|
||||
|
||||
_, _, max_llm_image_tokens = _get_vision_info(
|
||||
vision_config,
|
||||
height=9999999,
|
||||
width=9999999,
|
||||
min_pixels=image_processor.min_pixels,
|
||||
max_pixels=image_processor.max_pixels,
|
||||
modality=modality,
|
||||
)
|
||||
return max_llm_image_tokens
|
||||
|
||||
def get_mm_max_tokens_per_item(self) -> Mapping[str, int]:
|
||||
return {
|
||||
"image": self._get_max_mm_tokens("image"),
|
||||
"video": self._get_max_mm_tokens("video"),
|
||||
}
|
||||
|
||||
def _get_data_parser(self) -> MultiModalDataParser:
|
||||
return Qwen2MultiModalDataParser()
|
||||
|
||||
@@ -908,9 +902,6 @@ class Qwen2VLMultiModalProcessor(BaseMultiModalProcessor):
|
||||
)
|
||||
|
||||
|
||||
@MULTIMODAL_REGISTRY.register_max_image_tokens(get_max_qwen2_vl_image_tokens)
|
||||
@MULTIMODAL_REGISTRY.register_max_multimodal_tokens(
|
||||
"video", get_max_qwen2_vl_video_tokens)
|
||||
@MULTIMODAL_REGISTRY.register_processor(Qwen2VLMultiModalProcessor)
|
||||
class Qwen2VLForConditionalGeneration(nn.Module, SupportsMultiModal,
|
||||
SupportsLoRA, SupportsPP):
|
||||
|
||||
Reference in New Issue
Block a user