code: 
  
  
  
  
  
  '''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 CLIPImageProcessor
  
  
  
  class 							__UpperCAmelCase  (       unittest.TestCase						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		def __init__(self						:      Optional[Any]      ,      _lowerCAmelCase						:      int      ,      _lowerCAmelCase						:      Optional[int]=7      ,      _lowerCAmelCase						:      Any=3      ,      _lowerCAmelCase						:      List[Any]=18      ,      _lowerCAmelCase						:      Optional[Any]=30      ,      _lowerCAmelCase						:      List[Any]=400      ,      _lowerCAmelCase						:      Union[str, Any]=True      ,      _lowerCAmelCase						:      Union[str, Any]=None      ,      _lowerCAmelCase						:      Union[str, Any]=True      ,      _lowerCAmelCase						:      List[Any]=None      ,      _lowerCAmelCase						:      Any=True      ,      _lowerCAmelCase						:      List[Any]=[0.48_145_466, 0.4_578_275, 0.40_821_073]      ,      _lowerCAmelCase						:      Optional[int]=[0.26_862_954, 0.26_130_258, 0.27_577_711]      ,      _lowerCAmelCase						:      List[str]=True      ,      ):
  			A							  =      size if size is not None else {"""shortest_edge""": 20}
  			A							  =      crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
  			A							  =      parent
  			A							  =      batch_size
  			A							  =      num_channels
  			A							  =      image_size
  			A							  =      min_resolution
  			A							  =      max_resolution
  			A							  =      do_resize
  			A							  =      size
  			A							  =      do_center_crop
  			A							  =      crop_size
  			A							  =      do_normalize
  			A							  =      image_mean
  			A							  =      image_std
  			A							  =      do_convert_rgb
  
  
  
  
  
  
  
  		def        A     (self						:      Tuple      ):
  			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        A     (self						:      Tuple      ,      _lowerCAmelCase						:      Any=False      ,      _lowerCAmelCase						:      Tuple=False      ,      _lowerCAmelCase						:      str=False      ):
  			assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"
  
  			if equal_resolution:
  				A							  =      []
  				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:
  				A							  =      []
  				for i in range(self.batch_size      ):
  					A  ,    A							  =      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
  				A							  =      [Image.fromarray(np.moveaxis(_lowerCAmelCase      ,      0      ,      -1      )      ) for x in image_inputs]
  
  			if torchify:
  				A							  =      [torch.from_numpy(_lowerCAmelCase      ) for x in image_inputs]
  
  			return image_inputs
  
  
  
  
  
  
  @require_torch
  @require_vision
  class 							__UpperCAmelCase  (       A__		,					unittest.TestCase						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		__lowerCAmelCase 				=    CLIPImageProcessor if is_vision_available() else None
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[int]      ):
  			A							  =      CLIPImageProcessingTester(self      )
  
  
  
  
  
  
  
  		@property
  		def        A     (self						:      str      ):
  			return self.image_processor_tester.prepare_image_processor_dict()
  
  
  
  
  
  
  
  		def        A     (self						:      int      ):
  			A							  =      self.image_processing_class(**self.image_processor_dict      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """do_resize"""      )      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """size"""      )      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """do_center_crop"""      )      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """center_crop"""      )      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """do_normalize"""      )      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """image_mean"""      )      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """image_std"""      )      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """do_convert_rgb"""      )      )
  
  
  
  
  
  
  
  		def        A     (self						:      Dict      ):
  			A							  =      self.image_processing_class.from_dict(self.image_processor_dict      )
  			self.assertEqual(image_processor.size      ,      {"""shortest_edge""": 20}      )
  			self.assertEqual(image_processor.crop_size      ,      {"""height""": 18, """width""": 18}      )
  
  			A							  =      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        A     (self						:      Any      ):
  			pass
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[Any]      ):
  			# Initialize image_processing
  			A							  =      self.image_processing_class(**self.image_processor_dict      )
  			# create random PIL images
  			A							  =      self.image_processor_tester.prepare_inputs(equal_resolution=_lowerCAmelCase      )
  			for image in image_inputs:
  				self.assertIsInstance(_lowerCAmelCase      ,      Image.Image      )
  
  			# Test not batched input
  			A							  =      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							  =      image_processing(_lowerCAmelCase      ,      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        A     (self						:      List[Any]      ):
  			# Initialize image_processing
  			A							  =      self.image_processing_class(**self.image_processor_dict      )
  			# create random numpy tensors
  			A							  =      self.image_processor_tester.prepare_inputs(equal_resolution=_lowerCAmelCase      ,      numpify=_lowerCAmelCase      )
  			for image in image_inputs:
  				self.assertIsInstance(_lowerCAmelCase      ,      np.ndarray      )
  
  			# Test not batched input
  			A							  =      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							  =      image_processing(_lowerCAmelCase      ,      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        A     (self						:      Tuple      ):
  			# Initialize image_processing
  			A							  =      self.image_processing_class(**self.image_processor_dict      )
  			# create random PyTorch tensors
  			A							  =      self.image_processor_tester.prepare_inputs(equal_resolution=_lowerCAmelCase      ,      torchify=_lowerCAmelCase      )
  			for image in image_inputs:
  				self.assertIsInstance(_lowerCAmelCase      ,      torch.Tensor      )
  
  			# Test not batched input
  			A							  =      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							  =      image_processing(_lowerCAmelCase      ,      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 							__UpperCAmelCase  (       A__		,					unittest.TestCase						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		__lowerCAmelCase 				=    CLIPImageProcessor if is_vision_available() else None
  
  
  
  
  
  
  
  		def        A     (self						:      int      ):
  			A							  =      CLIPImageProcessingTester(self      ,      num_channels=4      )
  			A							  =      3
  
  
  
  
  
  
  
  		@property
  		def        A     (self						:      Tuple      ):
  			return self.image_processor_tester.prepare_image_processor_dict()
  
  
  
  
  
  
  
  		def        A     (self						:      List[Any]      ):
  			A							  =      self.image_processing_class(**self.image_processor_dict      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """do_resize"""      )      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """size"""      )      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """do_center_crop"""      )      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """center_crop"""      )      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """do_normalize"""      )      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """image_mean"""      )      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """image_std"""      )      )
  			self.assertTrue(hasattr(_lowerCAmelCase      ,      """do_convert_rgb"""      )      )
  
  
  
  
  
  
  
  		def        A     (self						:      Any      ):
  			pass
  
  
  
  
  
  
  
  		def        A     (self						:      int      ):
  			# Initialize image_processing
  			A							  =      self.image_processing_class(**self.image_processor_dict      )
  			# create random PIL images
  			A							  =      self.image_processor_tester.prepare_inputs(equal_resolution=_lowerCAmelCase      )
  			for image in image_inputs:
  				self.assertIsInstance(_lowerCAmelCase      ,      Image.Image      )
  
  			# Test not batched input
  			A							  =      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
  			A							  =      image_processing(_lowerCAmelCase      ,      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"""],
  			    )      ,      )
  
  
style_context: 
  
  
  
  
  
  '''simple docstring'''
  
  
  import tempfile
  import unittest
  
  import numpy as np
  import torch
  
  from diffusers import DiffusionPipeline
  from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor
  
  
  
  class 							__UpperCAmelCase  (       unittest.TestCase						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[int]      ,      _lowerCAmelCase						:      bool = False      ):
  			A							  =      10
  
  			if only_cross_attention:
  				A							  =      12
  			else:
  				# when only cross attention is not set, the cross attention dim must be the same as the query dim
  				A							  =      query_dim
  
  			return {
  			    "query_dim": query_dim,
  			    "cross_attention_dim": cross_attention_dim,
  			    "heads": 2,
  			    "dim_head": 4,
  			    "added_kv_proj_dim": 6,
  			    "norm_num_groups": 1,
  			    "only_cross_attention": only_cross_attention,
  			    "processor": AttnAddedKVProcessor(),
  			}
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[int]      ,      _lowerCAmelCase						:      Dict      ,      _lowerCAmelCase						:      Any      ):
  			A							  =      2
  
  			A							  =      torch.rand(_lowerCAmelCase      ,      _lowerCAmelCase      ,      3      ,      2      )
  			A							  =      torch.rand(_lowerCAmelCase      ,      4      ,      _lowerCAmelCase      )
  			A							  =      None
  
  			return {
  			    "hidden_states": hidden_states,
  			    "encoder_hidden_states": encoder_hidden_states,
  			    "attention_mask": attention_mask,
  			}
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[Any]      ):
  			# self and cross attention
  
  			torch.manual_seed(0      )
  
  			A							  =      self.get_constructor_arguments(only_cross_attention=_lowerCAmelCase      )
  			A							  =      Attention(**_lowerCAmelCase      )
  
  			self.assertTrue(attn.to_k is not None      )
  			self.assertTrue(attn.to_v is not None      )
  
  			A							  =      self.get_forward_arguments(
  			    query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )
  
  			A							  =      attn(**_lowerCAmelCase      )
  
  			# only self attention
  
  			torch.manual_seed(0      )
  
  			A							  =      self.get_constructor_arguments(only_cross_attention=_lowerCAmelCase      )
  			A							  =      Attention(**_lowerCAmelCase      )
  
  			self.assertTrue(attn.to_k is None      )
  			self.assertTrue(attn.to_v is None      )
  
  			A							  =      self.get_forward_arguments(
  			    query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )
  
  			A							  =      attn(**_lowerCAmelCase      )
  
  			self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()      )
  
  
  
  
  
  
  class 							__UpperCAmelCase  (       unittest.TestCase						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		def        A     (self						:      Any      ):
  			A							  =      DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      safety_checker=_lowerCAmelCase      )
  
  			A							  =      pipe(
  			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images
  
  			# the initial conversion succeeds
  			A							  =      DiffusionPipeline.from_pretrained(
  			    """hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      device_map="""sequential"""      ,      safety_checker=_lowerCAmelCase      )
  
  			A							  =      pipe(
  			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images
  
  			with tempfile.TemporaryDirectory() as tmpdir:
  				# save the converted model
  				pipe.save_pretrained(_lowerCAmelCase      )
  
  				# can also load the converted weights
  				A							  =      DiffusionPipeline.from_pretrained(_lowerCAmelCase      ,      device_map="""sequential"""      ,      safety_checker=_lowerCAmelCase      )
  
  			A							  =      pipe(
  			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images
  
  			self.assertTrue(np.allclose(_lowerCAmelCase      ,      _lowerCAmelCase      ,      atol=1e-5      )      )
  			self.assertTrue(np.allclose(_lowerCAmelCase      ,      _lowerCAmelCase      ,      atol=1e-5      )      )
  
  