
'''simple docstring'''
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import LevitImageProcessor







class  __A				( unittest.TestCase			):
 def __init__(   self			:    Dict				,   lowerCamelCase			:    Any				,   lowerCamelCase			:    Dict=7				,   lowerCamelCase			:    Dict=3				,   lowerCamelCase			:    List[str]=18				,   lowerCamelCase			:    Optional[int]=30				,   lowerCamelCase			:    Any=4_00				,   lowerCamelCase			:    Union[str, Any]=True				,   lowerCamelCase			:    Optional[Any]=None				,   lowerCamelCase			:    Dict=True				,   lowerCamelCase			:    str=None				,   lowerCamelCase			:    Any=True				,   lowerCamelCase			:    Optional[int]=[0.5, 0.5, 0.5]				,   lowerCamelCase			:    Tuple=[0.5, 0.5, 0.5]				,   ):



   """simple docstring"""





   __A		:     Optional[Any]												=		size if size is not None else {"""shortest_edge""": 18}
   __A		:     Tuple												=		crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
   __A		:     Tuple												=		parent
   __A		:     Union[str, Any]												=		batch_size
   __A		:     Tuple												=		num_channels
   __A		:     List[str]												=		image_size
   __A		:     List[str]												=		min_resolution
   __A		:     Tuple												=		max_resolution
   __A		:     Any												=		do_resize
   __A		:     int												=		size
   __A		:     Optional[Any]												=		do_center_crop
   __A		:     Tuple												=		crop_size
   __A		:     Optional[Any]												=		do_normalize
   __A		:     Any												=		image_mean
   __A		:     Any												=		image_std






 def 		lowercase_(   self			:    str       ):



   """simple docstring"""





   return {
       "image_mean": self.image_mean,
       "image_std": self.image_std,
       "do_normalize": self.do_normalize,
       "do_resize": self.do_resize,
       "do_center_crop": self.do_center_crop,
       "size": self.size,
       "crop_size": self.crop_size,
   }







@require_torch
@require_vision
class  __A				( __lowercase    ,						unittest.TestCase			):
 lowerCamelCase				       =LevitImageProcessor if is_vision_available() else None
 def 		lowercase_(   self			:    Optional[int]       ):



   """simple docstring"""





   __A		:     List[str]												=		LevitImageProcessingTester(self       )
 @property
 def 		lowercase_(   self			:    str       ):



   """simple docstring"""





   return self.image_processor_tester.prepare_image_processor_dict()
 def 		lowercase_(   self			:    List[Any]       ):



   """simple docstring"""





   __A		:     List[Any]												=		self.image_processing_class(**self.image_processor_dict       )
   self.assertTrue(hasattr(__A				,   """image_mean"""       )       )
   self.assertTrue(hasattr(__A				,   """image_std"""       )       )
   self.assertTrue(hasattr(__A				,   """do_normalize"""       )       )
   self.assertTrue(hasattr(__A				,   """do_resize"""       )       )
   self.assertTrue(hasattr(__A				,   """do_center_crop"""       )       )
   self.assertTrue(hasattr(__A				,   """size"""       )       )
 def 		lowercase_(   self			:    Any       ):



   """simple docstring"""





   __A		:     List[Any]												=		self.image_processing_class.from_dict(self.image_processor_dict       )
   self.assertEqual(image_processor.size				,   {"""shortest_edge""": 18}       )
   self.assertEqual(image_processor.crop_size				,   {"""height""": 18, """width""": 18}       )

   __A		:     Optional[Any]												=		self.image_processing_class.from_dict(self.image_processor_dict				,   size=42				,   crop_size=84       )
   self.assertEqual(image_processor.size				,   {"""shortest_edge""": 42}       )
   self.assertEqual(image_processor.crop_size				,   {"""height""": 84, """width""": 84}       )
 def 		lowercase_(   self			:    Any       ):



   """simple docstring"""





   pass
 def 		lowercase_(   self			:    str       ):



   """simple docstring"""





   __A		:     Any												=		self.image_processing_class(**self.image_processor_dict       )
   # create random PIL images
   __A		:     List[str]												=		prepare_image_inputs(self.image_processor_tester				,   equal_resolution=__A       )
   for image in image_inputs:
     self.assertIsInstance(__A				,   Image.Image       )

   # Test not batched input
   __A		:     int												=		image_processing(image_inputs[0]				,   return_tensors="""pt"""       ).pixel_values
   self.assertEqual(
       encoded_images.shape				,   (
           1,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["""height"""],
           self.image_processor_tester.crop_size["""width"""],
       )				,   )

   # Test batched
   __A		:     List[Any]												=		image_processing(__A				,   return_tensors="""pt"""       ).pixel_values
   self.assertEqual(
       encoded_images.shape				,   (
           self.image_processor_tester.batch_size,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["""height"""],
           self.image_processor_tester.crop_size["""width"""],
       )				,   )
 def 		lowercase_(   self			:    str       ):



   """simple docstring"""





   __A		:     Optional[int]												=		self.image_processing_class(**self.image_processor_dict       )
   # create random numpy tensors
   __A		:     Any												=		prepare_image_inputs(self.image_processor_tester				,   equal_resolution=__A				,   numpify=__A       )
   for image in image_inputs:
     self.assertIsInstance(__A				,   np.ndarray       )

   # Test not batched input
   __A		:     Optional[Any]												=		image_processing(image_inputs[0]				,   return_tensors="""pt"""       ).pixel_values
   self.assertEqual(
       encoded_images.shape				,   (
           1,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["""height"""],
           self.image_processor_tester.crop_size["""width"""],
       )				,   )

   # Test batched
   __A		:     List[Any]												=		image_processing(__A				,   return_tensors="""pt"""       ).pixel_values
   self.assertEqual(
       encoded_images.shape				,   (
           self.image_processor_tester.batch_size,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["""height"""],
           self.image_processor_tester.crop_size["""width"""],
       )				,   )






 def 		lowercase_(   self			:    str       ):



   """simple docstring"""





   __A		:     Union[str, Any]												=		self.image_processing_class(**self.image_processor_dict       )
   # create random PyTorch tensors
   __A		:     List[Any]												=		prepare_image_inputs(self.image_processor_tester				,   equal_resolution=__A				,   torchify=__A       )
   for image in image_inputs:
     self.assertIsInstance(__A				,   torch.Tensor       )

   # Test not batched input
   __A		:     Optional[Any]												=		image_processing(image_inputs[0]				,   return_tensors="""pt"""       ).pixel_values
   self.assertEqual(
       encoded_images.shape				,   (
           1,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["""height"""],
           self.image_processor_tester.crop_size["""width"""],
       )				,   )

   # Test batched
   __A		:     int												=		image_processing(__A				,   return_tensors="""pt"""       ).pixel_values
   self.assertEqual(
       encoded_images.shape				,   (
           self.image_processor_tester.batch_size,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["""height"""],
           self.image_processor_tester.crop_size["""width"""],
       )				,   )





'''simple docstring'''
from packaging import version

from .import_utils import is_accelerate_available


if is_accelerate_available():
     import accelerate







def        A_     (						__SCREAMING_SNAKE_CASE						:  Dict )				->       Optional[int]:





  """simple docstring"""

  if not is_accelerate_available():
    return method
  __A		:     int												=		version.parse(accelerate.__version__ ).base_version
  if version.parse(__SCREAMING_SNAKE_CASE ) < version.parse("""0.17.0""" ):
    return method

  def wrapper(self						:  str			,     *__SCREAMING_SNAKE_CASE						:  Tuple			,     **__SCREAMING_SNAKE_CASE						:  Tuple ):
    if hasattr(self			,     """_hf_hook""" ) and hasattr(self._hf_hook			,     """pre_forward""" ):
      self._hf_hook.pre_forward(self )
    return method(self			,     *__SCREAMING_SNAKE_CASE			,     **__SCREAMING_SNAKE_CASE )

  return wrapper



