Repository: vllm-project/vllm
File: vllm/model_executor/models/paddleocr_vl.py (Python)

Code before:
        *,
        image_width: int,
        image_height: int,
        image_processor: BaseImageProcessor,
        mm_kwargs: Mapping[str, object],
    ) -> int:
        hf_config = self.get_hf_config()
        vision_config = hf_config.vision_config
        patch_size = vision_config.patch_size
        merge_size = vision_config.spatial_merge_size

        mm_kwargs = self.ctx.get_merged_mm_kwargs(mm_kwargs)
        size = image_processor.size
        if override_size := mm_kwargs.get("size"):
            size = size | override_size
        if (override_min_pixels := mm_kwargs.get("min_pixels")) is not None:
            size = size | {"shortest_edge": override_min_pixels}
        if (override_max_pixels := mm_kwargs.get("max_pixels")) is not None:
            size = size | {"longest_edge": override_max_pixels}

        resized_height, resized_width = smart_resize(
            height=image_height,
            width=image_width,
            factor=patch_size * merge_size,
            min_pixels=size["shortest_edge"],
            max_pixels=size["longest_edge"],
        )
        preprocessed_size = ImageSize(width=resized_width, height=resized_height)

        grid_t = 1
        grid_h = preprocessed_size.height // patch_size
        grid_w = preprocessed_size.width // patch_size

        num_patches = grid_t * grid_h * grid_w
        num_image_tokens = num_patches // (merge_size**2)

        return num_image_tokens

    def get_image_size_with_most_features(self) -> ImageSize:
        hf_config = self.get_hf_config()
        image_processor = self.get_image_processor()

        # See `smart_resize` for the calculation of the image size.
        merge_size = hf_config.vision_config.spatial_merge_size
        patch_size = hf_config.vision_config.patch_size
        factor = merge_size * patch_size
        max_num_tokens = image_processor.max_pixels // (factor**2)
        # Find factors of max_num_tokens close to its square root
        # to create a dummy image with a reasonable aspect ratio.
        h_patches = int(math.sqrt(max_num_tokens))
        max_num_tokens -= max_num_tokens % h_patches

Review comment (suggestion):
maybe it can works and pass the v4 tests:

```suggestion
        if (override_min_pixels := mm_kwargs.get("shortest_edge")) is not None:
            size = size | {"min_pixels": override_min_pixels}
        if (override_max_pixels := mm_kwargs.get("longest_edge")) is not None:
            size = size | {"max_pixels": override_max_pixels}

        resized_height, resized_width = smart_resize(
            height=image_height,
            width=image_width,
            factor=patch_size * merge_size,
            min_pixels=size["min_pixels"],
            max_pixels=size["max_pixels"],
```

Code after:
        *,
        image_width: int,
        image_height: int,
        image_processor: BaseImageProcessor,
        mm_kwargs: Mapping[str, object],
    ) -> int:
        hf_config = self.get_hf_config()
        vision_config = hf_config.vision_config
        patch_size = vision_config.patch_size
        merge_size = vision_config.spatial_merge_size

        if self.ctx.model_config.trust_remote_code:
            # Defined in HF Hub repo
            min_pixels_key = "min_pixels"
            max_pixels_key = "max_pixels"
        else:
            # Defined in Transformers library (requires v5.0 or above)
            min_pixels_key = "shortest_edge"
            max_pixels_key = "longest_edge"

        mm_kwargs = self.ctx.get_merged_mm_kwargs(mm_kwargs)
        size = image_processor.size
        if override_size := mm_kwargs.get("size"):
            size = size | override_size
        if (override_min_pixels := mm_kwargs.get("min_pixels")) is not None:
            size = size | {min_pixels_key: override_min_pixels}
        if (override_max_pixels := mm_kwargs.get("max_pixels")) is not None:
            size = size | {max_pixels_key: override_max_pixels}

        resized_height, resized_width = smart_resize(
            height=image_height,
            width=image_width,
            factor=patch_size * merge_size,
            min_pixels=size[min_pixels_key],
            max_pixels=size[max_pixels_key],
        )
        preprocessed_size = ImageSize(width=resized_width, height=resized_height)

        grid_t = 1
        grid_h = preprocessed_size.height // patch_size
        grid_w = preprocessed_size.width // patch_size

        num_patches = grid_t * grid_h * grid_w
        num_image_tokens = num_patches // (merge_size**2)

        return num_image_tokens

    def get_image_size_with_most_features(self) -> ImageSize:
        hf_config = self.get_hf_config()
        image_processor = self.get_image_processor()