[CI/Build] Update vision tests (#5307)
This commit is contained in:
+21
-1
@@ -1,7 +1,8 @@
|
||||
import enum
|
||||
import json
|
||||
from dataclasses import dataclass, field, fields
|
||||
from typing import TYPE_CHECKING, ClassVar, List, Optional, Tuple, Union
|
||||
from typing import (TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Tuple,
|
||||
Union)
|
||||
|
||||
import torch
|
||||
from transformers import PretrainedConfig
|
||||
@@ -1114,6 +1115,25 @@ class VisionLanguageConfig:
|
||||
f"Expecting to choose from "
|
||||
f"{[x.name for x in cls.ImageInputType]}.") from e
|
||||
|
||||
def as_cli_args_dict(self) -> Dict[str, Any]:
|
||||
"""Flatten vision language config to pure args.
|
||||
|
||||
Compatible with what llm entrypoint expects.
|
||||
"""
|
||||
result: Dict[str, Any] = {}
|
||||
for f in fields(self):
|
||||
value = getattr(self, f.name)
|
||||
if isinstance(value, enum.Enum):
|
||||
result[f.name] = value.name.lower()
|
||||
elif isinstance(value, tuple):
|
||||
result[f.name] = ",".join([str(item) for item in value])
|
||||
else:
|
||||
result[f.name] = value
|
||||
|
||||
result["disable_image_processor"] = self.image_processor is None
|
||||
|
||||
return result
|
||||
|
||||
|
||||
_STR_DTYPE_TO_TORCH_DTYPE = {
|
||||
"half": torch.float16,
|
||||
|
||||
@@ -75,6 +75,14 @@ class ImagePixelData(MultiModalData):
|
||||
|
||||
self.image = image
|
||||
|
||||
def __repr__(self) -> str:
|
||||
image = self.image
|
||||
if isinstance(image, Image.Image):
|
||||
return f"{type(self).__name__}(image={image})"
|
||||
|
||||
return (f"{type(self).__name__}(image=torch.Tensor(shape="
|
||||
f"{image.shape}, dtype={image.dtype}))")
|
||||
|
||||
|
||||
class ImagePixelPlugin(MultiModalPlugin[ImagePixelData]):
|
||||
|
||||
@@ -96,10 +104,10 @@ class ImagePixelPlugin(MultiModalPlugin[ImagePixelData]):
|
||||
self, data: ImagePixelData, model_config: ModelConfig,
|
||||
vlm_config: VisionLanguageConfig) -> Dict[str, torch.Tensor]:
|
||||
image = data.image
|
||||
image_processor = self._get_hf_image_processor(model_config,
|
||||
vlm_config)
|
||||
|
||||
if isinstance(image, Image.Image):
|
||||
image_processor = self._get_hf_image_processor(
|
||||
model_config, vlm_config)
|
||||
if image_processor is None:
|
||||
raise RuntimeError("No HuggingFace processor is available"
|
||||
"to process the image object")
|
||||
@@ -127,6 +135,12 @@ class ImageFeatureData(MultiModalData):
|
||||
def __init__(self, image_features: torch.Tensor) -> None:
|
||||
self.image_features = image_features
|
||||
|
||||
def __repr__(self) -> str:
|
||||
image_features = self.image_features
|
||||
|
||||
return (f"{type(self).__name__}(image_features=torch.Tensor(shape="
|
||||
f"{image_features.shape}, dtype={image_features.dtype}))")
|
||||
|
||||
|
||||
class ImageFeaturePlugin(MultiModalPlugin[ImageFeatureData]):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user