



"""simple docstring"""






import unittest

from transformers import is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
	from PIL import Image
else:



	class     A__     :




		'''simple docstring'''





		@staticmethod
		def 							_SCREAMING_SNAKE_CASE     (  *_SCREAMING_SNAKE_CASE:	Optional[Any] ,    **_SCREAMING_SNAKE_CASE:	str)       -> int:

					"""simple docstring"""

					pass



@is_pipeline_test
@require_vision
class     A__     (						unittest.TestCase							):




	'''simple docstring'''





	@require_torch
	def 							_SCREAMING_SNAKE_CASE     (  self:	List[Any])       -> Any:

				"""simple docstring"""

				__lowerCAmelCase   :						int				= pipeline(
				    model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification" ,    )
				__lowerCAmelCase   :						List[str]				= Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
				__lowerCAmelCase   :						Optional[Any]				= image_classifier(_SCREAMING_SNAKE_CASE ,    candidate_labels=["a", "b", "c"])

				# The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
				# python and torch versions.
				self.assertIn(
				    nested_simplify(_SCREAMING_SNAKE_CASE) ,    [
				        [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}],
				        [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}, {"score": 0.333, "label": "b"}],
				    ] ,    )

				__lowerCAmelCase   :						str				= image_classifier([image] * 5 ,    candidate_labels=["A", "B", "C"] ,    batch_size=2)
				self.assertEqual(
				    nested_simplify(_SCREAMING_SNAKE_CASE) ,    [
				        [
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				        ],
				        [
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				        ],
				        [
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				        ],
				        [
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				        ],
				        [
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				        ],
				    ] ,    )

	@require_tf
	def 							_SCREAMING_SNAKE_CASE     (  self:	Dict)       -> Any:

				"""simple docstring"""

				__lowerCAmelCase   :						int				= pipeline(
				    model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification" ,    framework="tf")
				__lowerCAmelCase   :						Dict				= Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
				__lowerCAmelCase   :						Union[str, Any]				= image_classifier(_SCREAMING_SNAKE_CASE ,    candidate_labels=["a", "b", "c"])

				self.assertEqual(
				    nested_simplify(_SCREAMING_SNAKE_CASE) ,    [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}] ,    )

				__lowerCAmelCase   :						Any				= image_classifier([image] * 5 ,    candidate_labels=["A", "B", "C"] ,    batch_size=2)
				self.assertEqual(
				    nested_simplify(_SCREAMING_SNAKE_CASE) ,    [
				        [
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				        ],
				        [
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				        ],
				        [
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				        ],
				        [
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				        ],
				        [
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				            {"score": 0.333, "label": ANY(_SCREAMING_SNAKE_CASE)},
				        ],
				    ] ,    )

	@slow
	@require_torch
	def 							_SCREAMING_SNAKE_CASE     (  self:	List[str])       -> Any:

				"""simple docstring"""

				__lowerCAmelCase   :						Optional[int]				= pipeline(
				    task="zero-shot-image-classification" ,    model="openai/clip-vit-base-patch32" ,    )
				# This is an image of 2 cats with remotes and no planes
				__lowerCAmelCase   :						Union[str, Any]				= Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
				__lowerCAmelCase   :						Tuple				= image_classifier(_SCREAMING_SNAKE_CASE ,    candidate_labels=["cat", "plane", "remote"])

				self.assertEqual(
				    nested_simplify(_SCREAMING_SNAKE_CASE) ,    [
				        {"score": 0.511, "label": "remote"},
				        {"score": 0.485, "label": "cat"},
				        {"score": 0.004, "label": "plane"},
				    ] ,    )

				__lowerCAmelCase   :						Union[str, Any]				= image_classifier([image] * 5 ,    candidate_labels=["cat", "plane", "remote"] ,    batch_size=2)
				self.assertEqual(
				    nested_simplify(_SCREAMING_SNAKE_CASE) ,    [
				        [
				            {"score": 0.511, "label": "remote"},
				            {"score": 0.485, "label": "cat"},
				            {"score": 0.004, "label": "plane"},
				        ],
				    ]
				    * 5 ,    )







	@slow
	@require_tf
	def 							_SCREAMING_SNAKE_CASE     (  self:	Optional[Any])       -> Any:

				"""simple docstring"""

				__lowerCAmelCase   :						Optional[Any]				= pipeline(
				    task="zero-shot-image-classification" ,    model="openai/clip-vit-base-patch32" ,    framework="tf")
				# This is an image of 2 cats with remotes and no planes
				__lowerCAmelCase   :						Tuple				= Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
				__lowerCAmelCase   :						List[Any]				= image_classifier(_SCREAMING_SNAKE_CASE ,    candidate_labels=["cat", "plane", "remote"])
				self.assertEqual(
				    nested_simplify(_SCREAMING_SNAKE_CASE) ,    [
				        {"score": 0.511, "label": "remote"},
				        {"score": 0.485, "label": "cat"},
				        {"score": 0.004, "label": "plane"},
				    ] ,    )

				__lowerCAmelCase   :						Tuple				= image_classifier([image] * 5 ,    candidate_labels=["cat", "plane", "remote"] ,    batch_size=2)
				self.assertEqual(
				    nested_simplify(_SCREAMING_SNAKE_CASE) ,    [
				        [
				            {"score": 0.511, "label": "remote"},
				            {"score": 0.485, "label": "cat"},
				            {"score": 0.004, "label": "plane"},
				        ],
				    ]
				    * 5 ,    )




"""simple docstring"""






import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch



@require_torch
class     A__     :




	'''simple docstring'''





	@property
	def 							_SCREAMING_SNAKE_CASE     (  self:	Optional[int])       -> Optional[Any]:

				"""simple docstring"""

				return self.get_dummy_input()

	@property
	def 							_SCREAMING_SNAKE_CASE     (  self:	List[Any])       -> str:

				"""simple docstring"""

				if self.block_type == "down":
							return (4, 32, 16, 16)
				elif self.block_type == "mid":
							return (4, 32, 32, 32)
				elif self.block_type == "up":
							return (4, 32, 64, 64)

				raise ValueError(F"""'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'.""")

	def 							_SCREAMING_SNAKE_CASE     (  self:	Dict ,    _SCREAMING_SNAKE_CASE:	Union[str, Any]=True ,    _SCREAMING_SNAKE_CASE:	Tuple=False ,    _SCREAMING_SNAKE_CASE:	Union[str, Any]=False ,    _SCREAMING_SNAKE_CASE:	Dict=False ,    )       -> Optional[int]:

				"""simple docstring"""

				__lowerCAmelCase   :						Union[str, Any]				= 4
				__lowerCAmelCase   :						List[str]				= 32
				__lowerCAmelCase   :						str				= (32, 32)

				__lowerCAmelCase   :						int				= torch.manual_seed(0)
				__lowerCAmelCase   :						Dict				= torch.device(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase   :						str				= (batch_size, num_channels) + sizes
				__lowerCAmelCase   :						Dict				= randn_tensor(_SCREAMING_SNAKE_CASE ,    generator=_SCREAMING_SNAKE_CASE ,    device=_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase   :						str				= {"hidden_states": hidden_states}

				if include_temb:
							__lowerCAmelCase   :						List[str]				= 128
							__lowerCAmelCase   :						Dict				= randn_tensor((batch_size, temb_channels) ,    generator=_SCREAMING_SNAKE_CASE ,    device=_SCREAMING_SNAKE_CASE)

				if include_res_hidden_states_tuple:
							__lowerCAmelCase   :						Dict				= torch.manual_seed(1)
							__lowerCAmelCase   :						int				= (randn_tensor(_SCREAMING_SNAKE_CASE ,    generator=_SCREAMING_SNAKE_CASE ,    device=_SCREAMING_SNAKE_CASE),)

				if include_encoder_hidden_states:
							__lowerCAmelCase   :						int				= floats_tensor((batch_size, 32, 32)).to(_SCREAMING_SNAKE_CASE)

				if include_skip_sample:
							__lowerCAmelCase   :						Optional[Any]				= randn_tensor(((batch_size, 3) + sizes) ,    generator=_SCREAMING_SNAKE_CASE ,    device=_SCREAMING_SNAKE_CASE)

				return dummy_input

	def 							_SCREAMING_SNAKE_CASE     (  self:	Tuple)       -> Any:

				"""simple docstring"""

				__lowerCAmelCase   :						Optional[int]				= {
				    "in_channels": 32,
				    "out_channels": 32,
				    "temb_channels": 128,
				}
				if self.block_type == "up":
							__lowerCAmelCase   :						Tuple				= 32

				if self.block_type == "mid":
							init_dict.pop("out_channels")

				__lowerCAmelCase   :						str				= self.dummy_input
				return init_dict, inputs_dict

	def 							_SCREAMING_SNAKE_CASE     (  self:	List[str] ,    _SCREAMING_SNAKE_CASE:	List[str])       -> Optional[int]:

				"""simple docstring"""

				__lowerCAmelCase		,			__lowerCAmelCase   :						List[Any]				= self.prepare_init_args_and_inputs_for_common()
				__lowerCAmelCase   :						Dict				= self.block_class(**_SCREAMING_SNAKE_CASE)
				unet_block.to(_SCREAMING_SNAKE_CASE)
				unet_block.eval()

				with torch.no_grad():
							__lowerCAmelCase   :						int				= unet_block(**_SCREAMING_SNAKE_CASE)

				if isinstance(_SCREAMING_SNAKE_CASE ,    _SCREAMING_SNAKE_CASE):
							__lowerCAmelCase   :						List[Any]				= output[0]

				self.assertEqual(output.shape ,    self.output_shape)

				__lowerCAmelCase   :						str				= output[0, -1, -3:, -3:]
				__lowerCAmelCase   :						List[Any]				= torch.tensor(_SCREAMING_SNAKE_CASE).to(_SCREAMING_SNAKE_CASE)
				assert torch_all_close(output_slice.flatten() ,    _SCREAMING_SNAKE_CASE ,    atol=5e-3)







	@unittest.skipIf(torch_device == "mps" ,    "Training is not supported in mps")
	def 							_SCREAMING_SNAKE_CASE     (  self:	int)       -> Tuple:

				"""simple docstring"""

				__lowerCAmelCase		,			__lowerCAmelCase   :						Optional[Any]				= self.prepare_init_args_and_inputs_for_common()
				__lowerCAmelCase   :						str				= self.block_class(**_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.train()
				__lowerCAmelCase   :						Union[str, Any]				= model(**_SCREAMING_SNAKE_CASE)

				if isinstance(_SCREAMING_SNAKE_CASE ,    _SCREAMING_SNAKE_CASE):
							__lowerCAmelCase   :						Union[str, Any]				= output[0]

				__lowerCAmelCase   :						List[Any]				= torch.device(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase   :						int				= randn_tensor(output.shape ,    device=_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase   :						int				= torch.nn.functional.mse_loss(_SCREAMING_SNAKE_CASE ,    _SCREAMING_SNAKE_CASE)
				loss.backward()