





"""simple docstring"""




import os
import tempfile
import unittest

import numpy as np
import torch

from diffusers.models import ModelMixin, UNetaDConditionModel
from diffusers.models.attention_processor import AttnProcessor, LoRAAttnProcessor
from diffusers.utils import (
    floats_tensor,
    logging,
    skip_mps,
    torch_device,
)
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()

lowerCamelCase_	:		Tuple         =   logging.get_logger(__name__)





def 		UpperCAmelCase__						(	_UpperCAmelCase					,   _UpperCAmelCase = True   ):






  """simple docstring"""





  A_    :			Tuple									=    {}
  for name in model.attn_processors.keys():
    A_    :			Optional[Any]									=    name.endswith('attn2.processor'   ) and not (
        name.startswith('transformer_in'   ) or 'temp_attentions' in name.split('.'   )
    )
    A_    :			int									=    model.config.cross_attention_dim if has_cross_attention else None
    if name.startswith('mid_block'   ):
      A_    :			Tuple									=    model.config.block_out_channels[-1]
    elif name.startswith('up_blocks'   ):
      A_    :			Tuple									=    int(name[len('up_blocks.'   )]   )
      A_    :			Optional[Any]									=    list(reversed(model.config.block_out_channels   )   )[block_id]
    elif name.startswith('down_blocks'   ):
      A_    :			List[str]									=    int(name[len('down_blocks.'   )]   )
      A_    :			int									=    model.config.block_out_channels[block_id]
    elif name.startswith('transformer_in'   ):
      # Note that the `8 * ...` comes from: https://github.com/huggingface/diffusers/blob/7139f0e874f10b2463caa8cbd585762a309d12d6/src/diffusers/models/unet_3d_condition.py#L148
      A_    :			Tuple									=    8 * model.config.attention_head_dim

    A_    :			Tuple									=    LoRAAttnProcessor(hidden_size=_UpperCAmelCase					,   cross_attention_dim=_UpperCAmelCase   )
    A_    :			Optional[int]									=    lora_attn_procs[name].to(model.device   )

    if mock_weights:
      # add 1 to weights to mock trained weights
      with torch.no_grad():
        lora_attn_procs[name].to_q_lora.up.weight += 1
        lora_attn_procs[name].to_k_lora.up.weight += 1
        lora_attn_procs[name].to_v_lora.up.weight += 1
        lora_attn_procs[name].to_out_lora.up.weight += 1

  return lora_attn_procs





@skip_mps
class 					_UpperCAmelCase    (  UpperCAmelCase__     ,  UpperCAmelCase__     ,  unittest.TestCase						):



      '''simple docstring'''
      lowercase_ :					Optional[Any]             =  UNetaDConditionModel
      lowercase_ :					Optional[Any]             =  """sample"""

      @property
      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_    :			Optional[int]									=    4
        A_    :			Optional[int]									=    4
        A_    :			str									=    4
        A_    :			List[str]									=    (3_2, 3_2)

        A_    :			Tuple									=    floats_tensor((batch_size, num_channels, num_frames) + sizes    ).to(snake_case_    )
        A_    :			List[str]									=    torch.tensor([1_0]    ).to(snake_case_    )
        A_    :			Tuple									=    floats_tensor((batch_size, 4, 3_2)    ).to(snake_case_    )

        return {"sample": noise, "timestep": time_step, "encoder_hidden_states": encoder_hidden_states}

      @property
      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        return (4, 4, 3_2, 3_2)

      @property
      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        return (4, 4, 3_2, 3_2)

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_    :			Dict									=    {
            'block_out_channels': (3_2, 6_4),
            'down_block_types': (
                'CrossAttnDownBlock3D',
                'DownBlock3D',
            ),
            'up_block_types': ('UpBlock3D', 'CrossAttnUpBlock3D'),
            'cross_attention_dim': 3_2,
            'attention_head_dim': 8,
            'out_channels': 4,
            'in_channels': 4,
            'layers_per_block': 1,
            'sample_size': 3_2,
        }
        A_    :			List[Any]									=    self.dummy_input
        return init_dict, inputs_dict

      @unittest.skipIf(
          torch_device != 'cuda' or not is_xformers_available()   , reason='XFormers attention is only available with CUDA and `xformers` installed'   , )
      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Dict									=    self.prepare_init_args_and_inputs_for_common()
        A_    :			str									=    self.model_class(**snake_case_    )

        model.enable_xformers_memory_efficient_attention()

        assert (
            model.mid_block.attentions[0].transformer_blocks[0].attna.processor.__class__.__name__
            == "XFormersAttnProcessor"
        ), "xformers is not enabled"

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Optional[Any]									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			Optional[int]									=    3_2

        A_    :			int									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )
        model.eval()

        with torch.no_grad():
          A_    :			Dict									=    model(**snake_case_    )

          if isinstance(snake_case_   , snake_case_    ):
            A_    :			Any									=    output.sample

        self.assertIsNotNone(snake_case_    )
        A_    :			Any									=    inputs_dict['sample'].shape
        self.assertEqual(output.shape   , snake_case_   , 'Input and output shapes do not match'    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Dict									=    self.prepare_init_args_and_inputs_for_common()
        A_    :			Optional[int]									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )
        model.eval()

        with torch.no_grad():
          # Warmup pass when using mps (see #372)
          if torch_device == "mps" and isinstance(snake_case_   , snake_case_    ):
            model(**self.dummy_input    )

          A_    :			List[str]									=    model(**snake_case_    )
          if isinstance(snake_case_   , snake_case_    ):
            A_    :			str									=    first.sample

          A_    :			Dict									=    model(**snake_case_    )
          if isinstance(snake_case_   , snake_case_    ):
            A_    :			int									=    second.sample

        A_    :			Optional[Any]									=    first.cpu().numpy()
        A_    :			Any									=    second.cpu().numpy()
        A_    :			str									=    out_a[~np.isnan(snake_case_    )]
        A_    :			Tuple									=    out_a[~np.isnan(snake_case_    )]
        A_    :			List[str]									=    np.amax(np.abs(out_a - out_a    )    )
        self.assertLessEqual(snake_case_   , 1E-5    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Dict									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			Optional[Any]									=    8

        A_    :			int									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )
        model.eval()

        model.set_attention_slice('auto'    )
        with torch.no_grad():
          A_    :			List[str]									=    model(**snake_case_    )
        assert output is not None

        model.set_attention_slice('max'    )
        with torch.no_grad():
          A_    :			Optional[int]									=    model(**snake_case_    )
        assert output is not None

        model.set_attention_slice(2    )
        with torch.no_grad():
          A_    :			Any									=    model(**snake_case_    )
        assert output is not None

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			List[str]									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			List[str]									=    8

        A_    :			Any									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )

        with torch.no_grad():
          A_    :			Optional[Any]									=    model(**snake_case_    ).sample

        A_    :			Optional[Any]									=    create_lora_layers(snake_case_    )

        # make sure we can set a list of attention processors
        model.set_attn_processor(snake_case_    )
        model.to(snake_case_    )

        # test that attn processors can be set to itself
        model.set_attn_processor(model.attn_processors    )

        with torch.no_grad():
          A_    :			Dict									=    model(**snake_case_   , cross_attention_kwargs={'scale': 0.0}    ).sample
          A_    :			Dict									=    model(**snake_case_   , cross_attention_kwargs={'scale': 0.5}    ).sample
          A_    :			str									=    model(**snake_case_   , cross_attention_kwargs={'scale': 0.5}    ).sample

        assert (samplea - samplea).abs().max() < 3E-3
        assert (samplea - samplea).abs().max() < 3E-3

        # sample 2 and sample 3 should be different
        assert (samplea - samplea).abs().max() > 3E-3

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Any									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			Union[str, Any]									=    8

        torch.manual_seed(0    )
        A_    :			Optional[int]									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )

        with torch.no_grad():
          A_    :			Tuple									=    model(**snake_case_    ).sample

        A_    :			int									=    create_lora_layers(snake_case_    )
        model.set_attn_processor(snake_case_    )

        with torch.no_grad():
          A_    :			Dict									=    model(**snake_case_   , cross_attention_kwargs={'scale': 0.5}    ).sample

        with tempfile.TemporaryDirectory() as tmpdirname:
          model.save_attn_procs(snake_case_    )
          self.assertTrue(os.path.isfile(os.path.join(snake_case_   , 'pytorch_lora_weights.bin'    )    )    )
          torch.manual_seed(0    )
          A_    :			List[str]									=    self.model_class(**snake_case_    )
          new_model.to(snake_case_    )
          new_model.load_attn_procs(snake_case_    )

        with torch.no_grad():
          A_    :			int									=    new_model(**snake_case_   , cross_attention_kwargs={'scale': 0.5}    ).sample

        assert (sample - new_sample).abs().max() < 1E-3

        # LoRA and no LoRA should NOT be the same
        assert (sample - old_sample).abs().max() > 1E-4

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Optional[Any]									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			Dict									=    8

        torch.manual_seed(0    )
        A_    :			Optional[Any]									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )

        with torch.no_grad():
          A_    :			Union[str, Any]									=    model(**snake_case_    ).sample

        A_    :			Optional[int]									=    create_lora_layers(snake_case_    )
        model.set_attn_processor(snake_case_    )

        with torch.no_grad():
          A_    :			List[str]									=    model(**snake_case_   , cross_attention_kwargs={'scale': 0.5}    ).sample

        with tempfile.TemporaryDirectory() as tmpdirname:
          model.save_attn_procs(snake_case_   , safe_serialization=snake_case_    )
          self.assertTrue(os.path.isfile(os.path.join(snake_case_   , 'pytorch_lora_weights.safetensors'    )    )    )
          torch.manual_seed(0    )
          A_    :			Tuple									=    self.model_class(**snake_case_    )
          new_model.to(snake_case_    )
          new_model.load_attn_procs(snake_case_    )

        with torch.no_grad():
          A_    :			List[str]									=    new_model(**snake_case_   , cross_attention_kwargs={'scale': 0.5}    ).sample

        assert (sample - new_sample).abs().max() < 3E-3

        # LoRA and no LoRA should NOT be the same
        assert (sample - old_sample).abs().max() > 1E-4

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Any									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			Union[str, Any]									=    8

        torch.manual_seed(0    )
        A_    :			Any									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )

        A_    :			Optional[int]									=    create_lora_layers(snake_case_   , mock_weights=snake_case_    )
        model.set_attn_processor(snake_case_    )
        # Saving as torch, properly reloads with directly filename
        with tempfile.TemporaryDirectory() as tmpdirname:
          model.save_attn_procs(snake_case_    )
          self.assertTrue(os.path.isfile(os.path.join(snake_case_   , 'pytorch_lora_weights.bin'    )    )    )
          torch.manual_seed(0    )
          A_    :			List[str]									=    self.model_class(**snake_case_    )
          new_model.to(snake_case_    )
          new_model.load_attn_procs(snake_case_   , weight_name='pytorch_lora_weights.bin'    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Tuple									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			Dict									=    8

        torch.manual_seed(0    )
        A_    :			Dict									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )

        A_    :			Any									=    create_lora_layers(snake_case_   , mock_weights=snake_case_    )
        model.set_attn_processor(snake_case_    )
        # Saving as torch, properly reloads with directly filename
        with tempfile.TemporaryDirectory() as tmpdirname:
          model.save_attn_procs(snake_case_    )
          self.assertTrue(os.path.isfile(os.path.join(snake_case_   , 'pytorch_lora_weights.bin'    )    )    )
          torch.manual_seed(0    )
          A_    :			Optional[int]									=    self.model_class(**snake_case_    )
          new_model.to(snake_case_    )
          with self.assertRaises(snake_case_    ) as e:
            new_model.load_attn_procs(snake_case_   , use_safetensors=snake_case_    )
          self.assertIn('Error no file named pytorch_lora_weights.safetensors'   , str(e.exception    )    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Optional[int]									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			int									=    8

        torch.manual_seed(0    )
        A_    :			int									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )

        with torch.no_grad():
          A_    :			Optional[int]									=    model(**snake_case_    ).sample

        A_    :			Tuple									=    create_lora_layers(snake_case_    )
        model.set_attn_processor(snake_case_    )

        with torch.no_grad():
          A_    :			Dict									=    model(**snake_case_   , cross_attention_kwargs={'scale': 0.0}    ).sample

        model.set_attn_processor(AttnProcessor()    )

        with torch.no_grad():
          A_    :			List[Any]									=    model(**snake_case_    ).sample

        assert (sample - new_sample).abs().max() < 1E-4
        assert (sample - old_sample).abs().max() < 3E-3

      @unittest.skipIf(
          torch_device != 'cuda' or not is_xformers_available()   , reason='XFormers attention is only available with CUDA and `xformers` installed'   , )
      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			str									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			Union[str, Any]									=    4

        torch.manual_seed(0    )
        A_    :			int									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )
        A_    :			Optional[Any]									=    create_lora_layers(snake_case_    )
        model.set_attn_processor(snake_case_    )

        # default
        with torch.no_grad():
          A_    :			Any									=    model(**snake_case_    ).sample

          model.enable_xformers_memory_efficient_attention()
          A_    :			Optional[Any]									=    model(**snake_case_    ).sample

          model.disable_xformers_memory_efficient_attention()
          A_    :			List[Any]									=    model(**snake_case_    ).sample

        assert (sample - on_sample).abs().max() < 1E-4
        assert (sample - off_sample).abs().max() < 1E-4

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			List[Any]									=    self.prepare_init_args_and_inputs_for_common()
        A_    :			Any									=    3_2

        A_    :			str									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )
        model.eval()

        with torch.no_grad():
          A_    :			Dict									=    model(**snake_case_    )[0]

        model.enable_forward_chunking()
        with torch.no_grad():
          A_    :			Tuple									=    model(**snake_case_    )[0]

        self.assertEqual(output.shape   , output_a.shape   , 'Shape doesn\'t match'    )
        assert np.abs(output.cpu() - output_a.cpu()    ).max() < 1E-2


# (todo: sayakpaul) implement SLOW tests.


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 CLIPImageProcessor





class A__     (			unittest.TestCase			):



 """simple docstring"""







 def __init__(		self			:						Union[str, Any]  ,    A_			:						Optional[Any]  ,    A_			:						List[str]=7  ,    A_			:						Optional[Any]=3  ,    A_			:						int=1_8  ,    A_			:						List[Any]=3_0  ,    A_			:						str=4_0_0  ,    A_			:						Union[str, Any]=True  ,    A_			:						Union[str, Any]=None  ,    A_			:						Union[str, Any]=True  ,    A_			:						Optional[int]=None  ,    A_			:						List[str]=True  ,    A_			:						Union[str, Any]=[0.48145466, 0.4578275, 0.40821073]  ,    A_			:						int=[0.26862954, 0.26130258, 0.27577711]  ,    A_			:						Optional[int]=True  ,    ):






      '''simple docstring'''




      _lowerCAmelCase :       Optional[Any]	   =     size if size is not None else {"shortest_edge": 2_0}
      _lowerCAmelCase :       Union[str, Any]	   =     crop_size if crop_size is not None else {"height": 1_8, "width": 1_8}
      _lowerCAmelCase :       Tuple	   =     parent
      _lowerCAmelCase :       int	   =     batch_size
      _lowerCAmelCase :       str	   =     num_channels
      _lowerCAmelCase :       int	   =     image_size
      _lowerCAmelCase :       Optional[int]	   =     min_resolution
      _lowerCAmelCase :       Any	   =     max_resolution
      _lowerCAmelCase :       str	   =     do_resize
      _lowerCAmelCase :       Union[str, Any]	   =     size
      _lowerCAmelCase :       Optional[int]	   =     do_center_crop
      _lowerCAmelCase :       Union[str, Any]	   =     crop_size
      _lowerCAmelCase :       Optional[int]	   =     do_normalize
      _lowerCAmelCase :       Optional[int]	   =     image_mean
      _lowerCAmelCase :       int	   =     image_std
      _lowerCAmelCase :       Optional[Any]	   =     do_convert_rgb

 def  __magic_name__							(		self			:						Any							):






      '''simple docstring'''




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


 def  __magic_name__							(		self			:						List[Any]  ,    A_			:						List[Any]=False  ,    A_			:						Optional[Any]=False  ,    A_			:						Tuple=False							):






      '''simple docstring'''




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

      if equal_resolution:
           _lowerCAmelCase :       Tuple	   =     []
           for i in range(self.batch_size							):
                image_inputs.append(
                    np.random.randint(
                        2_5_5  ,    size=(self.num_channels, self.max_resolution, self.max_resolution)  ,    dtype=np.uinta							)							)
      else:
           _lowerCAmelCase :       Optional[int]	   =     []
           for i in range(self.batch_size							):
                _lowerCAmelCase   ,      _lowerCAmelCase :       Union[str, Any]	   =     np.random.choice(np.arange(self.min_resolution  ,    self.max_resolution							)  ,    2							)
                image_inputs.append(np.random.randint(2_5_5  ,    size=(self.num_channels, width, height)  ,    dtype=np.uinta							)							)

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

      if torchify:
           _lowerCAmelCase :       Tuple	   =     [torch.from_numpy(A_							) for x in image_inputs]

      return image_inputs





@require_torch
@require_vision
class A__     (			A	,		unittest.TestCase			):



 """simple docstring"""







 _lowercase  :       Any  			=    CLIPImageProcessor if is_vision_available() else None

 def  __magic_name__							(		self			:						Union[str, Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       Tuple	   =     CLIPImageProcessingTester(self							)

 @property
 def  __magic_name__							(		self			:						List[Any]							):






      '''simple docstring'''




      return self.image_processor_tester.prepare_image_processor_dict()

 def  __magic_name__							(		self			:						Union[str, Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       Optional[int]	   =     self.image_processing_class(**self.image_processor_dict							)
      self.assertTrue(hasattr(A_  ,    "do_resize"							)							)
      self.assertTrue(hasattr(A_  ,    "size"							)							)
      self.assertTrue(hasattr(A_  ,    "do_center_crop"							)							)
      self.assertTrue(hasattr(A_  ,    "center_crop"							)							)
      self.assertTrue(hasattr(A_  ,    "do_normalize"							)							)
      self.assertTrue(hasattr(A_  ,    "image_mean"							)							)
      self.assertTrue(hasattr(A_  ,    "image_std"							)							)
      self.assertTrue(hasattr(A_  ,    "do_convert_rgb"							)							)

 def  __magic_name__							(		self			:						Optional[int]							):






      '''simple docstring'''




      _lowerCAmelCase :       Union[str, Any]	   =     self.image_processing_class.from_dict(self.image_processor_dict							)
      self.assertEqual(image_processor.size  ,    {"shortest_edge": 2_0}							)
      self.assertEqual(image_processor.crop_size  ,    {"height": 1_8, "width": 1_8}							)

      _lowerCAmelCase :       List[Any]	   =     self.image_processing_class.from_dict(self.image_processor_dict  ,    size=4_2  ,    crop_size=8_4							)
      self.assertEqual(image_processor.size  ,    {"shortest_edge": 4_2}							)
      self.assertEqual(image_processor.crop_size  ,    {"height": 8_4, "width": 8_4}							)

 def  __magic_name__							(		self			:						Dict							):






      '''simple docstring'''




      pass

 def  __magic_name__							(		self			:						List[Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       Optional[Any]	   =     self.image_processing_class(**self.image_processor_dict							)
      # create random PIL images
      _lowerCAmelCase :       Union[str, Any]	   =     self.image_processor_tester.prepare_inputs(equal_resolution=A_							)
      for image in image_inputs:
           self.assertIsInstance(A_  ,    Image.Image							)

      # Test not batched input
      _lowerCAmelCase :       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
      _lowerCAmelCase :       Optional[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  __magic_name__							(		self			:						List[Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       List[Any]	   =     self.image_processing_class(**self.image_processor_dict							)
      # create random numpy tensors
      _lowerCAmelCase :       Dict	   =     self.image_processor_tester.prepare_inputs(equal_resolution=A_  ,    numpify=A_							)
      for image in image_inputs:
           self.assertIsInstance(A_  ,    np.ndarray							)

      # Test not batched input
      _lowerCAmelCase :       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
      _lowerCAmelCase :       Union[str, 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  __magic_name__							(		self			:						Tuple							):






      '''simple docstring'''




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

      # Test not batched input
      _lowerCAmelCase :       Tuple	   =     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
      _lowerCAmelCase :       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"],
          )  ,    )





@require_torch
@require_vision
class A__     (			A	,		unittest.TestCase			):



 """simple docstring"""







 _lowercase  :       List[str]  			=    CLIPImageProcessor if is_vision_available() else None

 def  __magic_name__							(		self			:						Tuple							):






      '''simple docstring'''




      _lowerCAmelCase :       int	   =     CLIPImageProcessingTester(self  ,    num_channels=4							)
      _lowerCAmelCase :       Tuple	   =     3

 @property
 def  __magic_name__							(		self			:						Optional[int]							):






      '''simple docstring'''




      return self.image_processor_tester.prepare_image_processor_dict()

 def  __magic_name__							(		self			:						Optional[int]							):






      '''simple docstring'''




      _lowerCAmelCase :       List[str]	   =     self.image_processing_class(**self.image_processor_dict							)
      self.assertTrue(hasattr(A_  ,    "do_resize"							)							)
      self.assertTrue(hasattr(A_  ,    "size"							)							)
      self.assertTrue(hasattr(A_  ,    "do_center_crop"							)							)
      self.assertTrue(hasattr(A_  ,    "center_crop"							)							)
      self.assertTrue(hasattr(A_  ,    "do_normalize"							)							)
      self.assertTrue(hasattr(A_  ,    "image_mean"							)							)
      self.assertTrue(hasattr(A_  ,    "image_std"							)							)
      self.assertTrue(hasattr(A_  ,    "do_convert_rgb"							)							)

 def  __magic_name__							(		self			:						Optional[Any]							):






      '''simple docstring'''




      pass


 def  __magic_name__							(		self			:						Any							):






      '''simple docstring'''




      _lowerCAmelCase :       Tuple	   =     self.image_processing_class(**self.image_processor_dict							)
      # create random PIL images
      _lowerCAmelCase :       Optional[int]	   =     self.image_processor_tester.prepare_inputs(equal_resolution=A_							)
      for image in image_inputs:
           self.assertIsInstance(A_  ,    Image.Image							)

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

      # Test batched
      _lowerCAmelCase :       int	   =     image_processing(A_  ,    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.crop_size["height"],
              self.image_processor_tester.crop_size["width"],
          )  ,    )






