Add Idefics2/3 and SmolVLM Fast image processors + improvements for fast image processors (#38157)
* add working idefics2 fast and improvements for fast nested images processing * add fast image processors idefics 3 and smolvlm * cleanup tests * fic doc idefics2 * PR review and fix issues after merge * Force providing disable_grouping to group_images_by_shape * simplify group_images_by_shape * fix modular * Fix nits after review
This commit is contained in:
@@ -15,9 +15,21 @@
|
||||
import unittest
|
||||
|
||||
import requests
|
||||
from packaging import version
|
||||
|
||||
from transformers.testing_utils import require_pytesseract, require_torch, require_vision
|
||||
from transformers.utils import is_pytesseract_available, is_torch_available, is_torchvision_available
|
||||
from transformers.testing_utils import (
|
||||
require_pytesseract,
|
||||
require_torch,
|
||||
require_torch_accelerator,
|
||||
require_vision,
|
||||
slow,
|
||||
torch_device,
|
||||
)
|
||||
from transformers.utils import (
|
||||
is_pytesseract_available,
|
||||
is_torch_available,
|
||||
is_torchvision_available,
|
||||
)
|
||||
|
||||
from ...test_image_processing_common import ImageProcessingTestMixin, prepare_image_inputs
|
||||
|
||||
@@ -157,16 +169,8 @@ class LayoutLMv2ImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase)
|
||||
|
||||
encoding_slow = image_processor_slow(dummy_image, return_tensors="pt")
|
||||
encoding_fast = image_processor_fast(dummy_image, return_tensors="pt")
|
||||
self.assertTrue(
|
||||
torch.allclose(
|
||||
encoding_slow.pixel_values.float() / 255, encoding_fast.pixel_values.float() / 255, atol=1e-1
|
||||
)
|
||||
)
|
||||
self.assertLessEqual(
|
||||
torch.mean(
|
||||
torch.abs(encoding_slow.pixel_values.float() - encoding_fast.pixel_values.float()) / 255
|
||||
).item(),
|
||||
1e-3,
|
||||
self._assert_slow_fast_tensors_equivalence(
|
||||
encoding_slow.pixel_values.float() / 255, encoding_fast.pixel_values.float() / 255
|
||||
)
|
||||
|
||||
@require_vision
|
||||
@@ -190,14 +194,28 @@ class LayoutLMv2ImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase)
|
||||
encoding_slow = image_processor_slow(dummy_images, return_tensors="pt")
|
||||
encoding_fast = image_processor_fast(dummy_images, return_tensors="pt")
|
||||
|
||||
self.assertTrue(
|
||||
torch.allclose(
|
||||
encoding_slow.pixel_values.float() / 255, encoding_fast.pixel_values.float() / 255, atol=1e-1
|
||||
)
|
||||
self._assert_slow_fast_tensors_equivalence(
|
||||
encoding_slow.pixel_values.float() / 255, encoding_fast.pixel_values.float() / 255
|
||||
)
|
||||
self.assertLessEqual(
|
||||
torch.mean(
|
||||
torch.abs(encoding_slow.pixel_values.float() - encoding_fast.pixel_values.float()) / 255
|
||||
).item(),
|
||||
1e-3,
|
||||
|
||||
# Overriding as we can't use torch.testing.assert_close on int8 tensors
|
||||
@slow
|
||||
@require_torch_accelerator
|
||||
@require_vision
|
||||
def test_can_compile_fast_image_processor(self):
|
||||
if self.fast_image_processing_class is None:
|
||||
self.skipTest("Skipping compilation test as fast image processor is not defined")
|
||||
if version.parse(torch.__version__) < version.parse("2.3"):
|
||||
self.skipTest(reason="This test requires torch >= 2.3 to run.")
|
||||
|
||||
torch.compiler.reset()
|
||||
input_image = torch.randint(0, 255, (3, 224, 224), dtype=torch.uint8)
|
||||
image_processor = self.fast_image_processing_class(**self.image_processor_dict)
|
||||
output_eager = image_processor(input_image, device=torch_device, return_tensors="pt")
|
||||
|
||||
image_processor = torch.compile(image_processor, mode="reduce-overhead")
|
||||
output_compiled = image_processor(input_image, device=torch_device, return_tensors="pt")
|
||||
|
||||
self._assert_slow_fast_tensors_equivalence(
|
||||
output_eager.pixel_values.float() / 255, output_compiled.pixel_values.float() / 255
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user