



"""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


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import BlipImageProcessor




class    __snake_case(					unittest.TestCase       ):




    def __init__(   self    ,							A_    ,							A_=7    ,							A_=3    ,							A_=18    ,							A_=30    ,							A_=400    ,							A_=True    ,							A_=None    ,							A_=True    ,							A_=False    ,							A_=[0.48_145_466, 0.4_578_275, 0.40_821_073]    ,							A_=[0.26_862_954, 0.26_130_258, 0.27_577_711]    ,							A_=True    ,							):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			size if size is not None else {'''height''': 20, '''width''': 20}
      _SCREAMING_SNAKE_CASE        =			parent
      _SCREAMING_SNAKE_CASE        =			batch_size
      _SCREAMING_SNAKE_CASE        =			num_channels
      _SCREAMING_SNAKE_CASE        =			image_size
      _SCREAMING_SNAKE_CASE        =			min_resolution
      _SCREAMING_SNAKE_CASE        =			max_resolution
      _SCREAMING_SNAKE_CASE        =			do_resize
      _SCREAMING_SNAKE_CASE        =			size
      _SCREAMING_SNAKE_CASE        =			do_normalize
      _SCREAMING_SNAKE_CASE        =			image_mean
      _SCREAMING_SNAKE_CASE        =			image_std
      _SCREAMING_SNAKE_CASE        =			do_pad
      _SCREAMING_SNAKE_CASE        =			do_convert_rgb




    def   A   (   self			):





      '''simple docstring'''



      return {
          "do_resize": self.do_resize,
          "size": self.size,
          "do_normalize": self.do_normalize,
          "image_mean": self.image_mean,
          "image_std": self.image_std,
          "do_convert_rgb": self.do_convert_rgb,
          "do_pad": self.do_pad,
      }




    def   A   (   self    ,							A_=False    ,							A_=False    ,							A_=False			):





      '''simple docstring'''



      assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

      if equal_resolution:
        _SCREAMING_SNAKE_CASE        =			[]
        for i in range(self.batch_size			):
          image_inputs.append(
              np.random.randint(
                  255    ,							size=(self.num_channels, self.max_resolution, self.max_resolution)    ,							dtype=np.uinta			)			)
      else:
        _SCREAMING_SNAKE_CASE        =			[]
        for i in range(self.batch_size			):
          _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE        =			np.random.choice(np.arange(self.min_resolution    ,							self.max_resolution			)    ,							2			)
          image_inputs.append(np.random.randint(255    ,							size=(self.num_channels, width, height)    ,							dtype=np.uinta			)			)

      if not numpify and not torchify:
        # PIL expects the channel dimension as last dimension
        _SCREAMING_SNAKE_CASE        =			[Image.fromarray(np.moveaxis(lowercase__    ,							0    ,							-1			)			) for x in image_inputs]

      if torchify:
        _SCREAMING_SNAKE_CASE        =			[torch.from_numpy(lowercase__			) for x in image_inputs]

      return image_inputs




@require_torch
@require_vision
class    __snake_case(					__A			,			unittest.TestCase       ):
    _A       = BlipImageProcessor if is_vision_available() else None




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			BlipImageProcessingTester(self			)




    @property
    def   A   (   self			):





      '''simple docstring'''



      return self.image_processor_tester.prepare_image_processor_dict()




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.image_processing_class(**self.image_processor_dict			)
      self.assertTrue(hasattr(lowercase__    ,							'''do_resize'''			)			)
      self.assertTrue(hasattr(lowercase__    ,							'''size'''			)			)
      self.assertTrue(hasattr(lowercase__    ,							'''do_normalize'''			)			)
      self.assertTrue(hasattr(lowercase__    ,							'''image_mean'''			)			)
      self.assertTrue(hasattr(lowercase__    ,							'''image_std'''			)			)
      self.assertTrue(hasattr(lowercase__    ,							'''do_convert_rgb'''			)			)




    def   A   (   self			):





      '''simple docstring'''



      pass




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.image_processing_class(**self.image_processor_dict			)
      # create random PIL images
      _SCREAMING_SNAKE_CASE        =			self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__			)
      for image in image_inputs:
        self.assertIsInstance(lowercase__    ,							Image.Image			)

      # Test not batched input
      _SCREAMING_SNAKE_CASE        =			image_processor(image_inputs[0]    ,							return_tensors='''pt'''			).pixel_values
      self.assertEqual(
          encoded_images.shape    ,							(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['''height'''],
              self.image_processor_tester.size['''width'''],
          )    ,							)

      # Test batched
      _SCREAMING_SNAKE_CASE        =			image_processor(lowercase__    ,							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.size['''height'''],
              self.image_processor_tester.size['''width'''],
          )    ,							)




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.image_processing_class(**self.image_processor_dict			)
      # create random numpy tensors
      _SCREAMING_SNAKE_CASE        =			self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__    ,							numpify=lowercase__			)
      for image in image_inputs:
        self.assertIsInstance(lowercase__    ,							np.ndarray			)

      # Test not batched input
      _SCREAMING_SNAKE_CASE        =			image_processor(image_inputs[0]    ,							return_tensors='''pt'''			).pixel_values
      self.assertEqual(
          encoded_images.shape    ,							(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['''height'''],
              self.image_processor_tester.size['''width'''],
          )    ,							)

      # Test batched
      _SCREAMING_SNAKE_CASE        =			image_processor(lowercase__    ,							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.size['''height'''],
              self.image_processor_tester.size['''width'''],
          )    ,							)




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.image_processing_class(**self.image_processor_dict			)
      # create random PyTorch tensors
      _SCREAMING_SNAKE_CASE        =			self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__    ,							torchify=lowercase__			)
      for image in image_inputs:
        self.assertIsInstance(lowercase__    ,							torch.Tensor			)

      # Test not batched input
      _SCREAMING_SNAKE_CASE        =			image_processor(image_inputs[0]    ,							return_tensors='''pt'''			).pixel_values
      self.assertEqual(
          encoded_images.shape    ,							(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['''height'''],
              self.image_processor_tester.size['''width'''],
          )    ,							)

      # Test batched
      _SCREAMING_SNAKE_CASE        =			image_processor(lowercase__    ,							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.size['''height'''],
              self.image_processor_tester.size['''width'''],
          )    ,							)




@require_torch
@require_vision
class    __snake_case(					__A			,			unittest.TestCase       ):
    _A       = BlipImageProcessor if is_vision_available() else None




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			BlipImageProcessingTester(self    ,							num_channels=4			)
      _SCREAMING_SNAKE_CASE        =			3




    @property
    def   A   (   self			):





      '''simple docstring'''



      return self.image_processor_tester.prepare_image_processor_dict()




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.image_processing_class(**self.image_processor_dict			)
      self.assertTrue(hasattr(lowercase__    ,							'''do_resize'''			)			)
      self.assertTrue(hasattr(lowercase__    ,							'''size'''			)			)
      self.assertTrue(hasattr(lowercase__    ,							'''do_normalize'''			)			)
      self.assertTrue(hasattr(lowercase__    ,							'''image_mean'''			)			)
      self.assertTrue(hasattr(lowercase__    ,							'''image_std'''			)			)
      self.assertTrue(hasattr(lowercase__    ,							'''do_convert_rgb'''			)			)




    def   A   (   self			):





      '''simple docstring'''



      pass




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.image_processing_class(**self.image_processor_dict			)
      # create random PIL images
      _SCREAMING_SNAKE_CASE        =			self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__			)
      for image in image_inputs:
        self.assertIsInstance(lowercase__    ,							Image.Image			)

      # Test not batched input
      _SCREAMING_SNAKE_CASE        =			image_processor(image_inputs[0]    ,							return_tensors='''pt'''			).pixel_values
      self.assertEqual(
          encoded_images.shape    ,							(
              1,
              self.expected_encoded_image_num_channels,
              self.image_processor_tester.size['''height'''],
              self.image_processor_tester.size['''width'''],
          )    ,							)

      # Test batched
      _SCREAMING_SNAKE_CASE        =			image_processor(lowercase__    ,							return_tensors='''pt'''			).pixel_values
      self.assertEqual(
          encoded_images.shape    ,							(
              self.image_processor_tester.batch_size,
              self.expected_encoded_image_num_channels,
              self.image_processor_tester.size['''height'''],
              self.image_processor_tester.size['''width'''],
          )    ,							)








'''simple docstring'''







import inspect
import os
import unittest

import torch

import accelerate
from accelerate import debug_launcher
from accelerate.test_utils import (
    execute_subprocess_async,
    require_cpu,
    require_multi_gpu,
    require_single_gpu,
    test_sync,
)
from accelerate.utils import patch_environment






class        a__     (      unittest.TestCase    ):
     '''simple docstring'''







     def      _UpperCamelCase (self     :						int       )	->							Optional[int]:
           __A		 =   inspect.getfile(accelerate.test_utils       )
           __A		 =   os.path.sep.join(mod_file.split(os.path.sep       )[:-1] + ["scripts", "test_sync.py"]       )






     @require_cpu
     def      _UpperCamelCase (self     :						str       )	->							Optional[Any]:
           debug_launcher(test_sync.main					,					num_processes=1       )






     @require_cpu
     def      _UpperCamelCase (self     :						Union[str, Any]       )	->							Optional[Any]:
           debug_launcher(test_sync.main       )






     @require_single_gpu
     def      _UpperCamelCase (self     :						Union[str, Any]       )	->							List[str]:
           test_sync.main()






     @require_multi_gpu
     def      _UpperCamelCase (self     :						Optional[int]       )	->							List[str]:
           print(f"Found {torch.cuda.device_count()} devices."       )
           __A		 =   ["torchrun", f"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path]
           with patch_environment(omp_num_threads=1       ):
                 execute_subprocess_async(lowercase__					,					env=os.environ.copy()       )


