


"""simple docstring"""


import gc
import unittest

from parameterized import parameterized

from diffusers import FlaxUNetaDConditionModel
from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow


if is_flax_available():
	import jax
	import jax.numpy as jnp



@slow
@require_flax
class 			lowerCamelCase__						(   unittest.TestCase			):







						def __a						(							self   :    Tuple			,			_lowercase   :    Tuple			,			_lowercase   :    str		):
								return f'gaussian_noise_s={seed}_shape={"_".join([str(_lowercase		) for s in shape]		)}.npy'







						def __a						(							self   :    Dict		):
								# clean up the VRAM after each test
								super().tearDown()
								gc.collect()







						def __a						(							self   :    Dict			,			_lowercase   :    Any=0			,			_lowercase   :    int=(4, 4, 64, 64)			,			_lowercase   :    Dict=False		):
								A					       = jnp.bfloataa if fpaa else jnp.floataa
								A					       = jnp.array(load_hf_numpy(self.get_file_format(_lowercase			,			_lowercase		)		)			,			dtype=_lowercase		)
								return image







						def __a						(							self   :    str			,			_lowercase   :    List[str]=False			,			_lowercase   :    Optional[int]="CompVis/stable-diffusion-v1-4"		):
								A					       = jnp.bfloataa if fpaa else jnp.floataa
								A					       = 'bf16' if fpaa else None

								A       , A					       = FlaxUNetaDConditionModel.from_pretrained(
								    _lowercase			,			subfolder='unet'			,			dtype=_lowercase			,			revision=_lowercase		)
								return model, params







						def __a						(							self   :    Tuple			,			_lowercase   :    Any=0			,			_lowercase   :    Dict=(4, 77, 768)			,			_lowercase   :    List[str]=False		):
								A					       = jnp.bfloataa if fpaa else jnp.floataa
								A					       = jnp.array(load_hf_numpy(self.get_file_format(_lowercase			,			_lowercase		)		)			,			dtype=_lowercase		)
								return hidden_states







						@parameterized.expand(
						    [
						        # fmt: off
						        [83, 4, [-0.2_3_2_3, -0.1_3_0_4, 0.0_8_1_3, -0.3_0_9_3, -0.0_9_1_9, -0.1_5_7_1, -0.1_1_2_5, -0.5_8_0_6]],
						        [17, 0.5_5, [-0.0_8_3_1, -0.2_4_4_3, 0.0_9_0_1, -0.0_9_1_9, 0.3_3_9_6, 0.0_1_0_3, -0.3_7_4_3, 0.0_7_0_1]],
						        [8, 0.8_9, [-0.4_8_6_3, 0.0_8_5_9, 0.0_8_7_5, -0.1_6_5_8, 0.9_1_9_9, -0.0_1_1_4, 0.4_8_3_9, 0.4_6_3_9]],
						        [3, 1_000, [-0.5_6_4_9, 0.2_4_0_2, -0.5_5_1_8, 0.1_2_4_8, 1.1_3_2_8, -0.2_4_4_3, -0.0_3_2_5, -1.0_0_7_8]],
						        # fmt: on
						    ]		)
						def __a						(							self   :    List[Any]			,			_lowercase   :    str			,			_lowercase   :    Tuple			,			_lowercase   :    Optional[Any]		):
								A       , A					       = self.get_unet_model(model_id='CompVis/stable-diffusion-v1-4'			,			fpaa=_lowercase		)
								A					       = self.get_latents(_lowercase			,			fpaa=_lowercase		)
								A					       = self.get_encoder_hidden_states(_lowercase			,			fpaa=_lowercase		)

								A					       = model.apply(
								    {'params': params}			,			_lowercase			,			jnp.array(_lowercase			,			dtype=jnp.intaa		)			,			encoder_hidden_states=_lowercase			,			).sample

								assert sample.shape == latents.shape

								A					       = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())		)			,			dtype=jnp.floataa		)
								A					       = jnp.array(_lowercase			,			dtype=jnp.floataa		)

								# Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware
								assert jnp.allclose(_lowercase			,			_lowercase			,			atol=1e-2		)







						@parameterized.expand(
						    [
						        # fmt: off
						        [83, 4, [0.1_5_1_4, 0.0_8_0_7, 0.1_6_2_4, 0.1_0_1_6, -0.1_8_9_6, 0.0_2_6_3, 0.0_6_7_7, 0.2_3_1_0]],
						        [17, 0.5_5, [0.1_1_6_4, -0.0_2_1_6, 0.0_1_7_0, 0.1_5_8_9, -0.3_1_2_0, 0.1_0_0_5, -0.0_5_8_1, -0.1_4_5_8]],
						        [8, 0.8_9, [-0.1_7_5_8, -0.0_1_6_9, 0.1_0_0_4, -0.1_4_1_1, 0.1_3_1_2, 0.1_1_0_3, -0.1_9_9_6, 0.2_1_3_9]],
						        [3, 1_000, [0.1_2_1_4, 0.0_3_5_2, -0.0_7_3_1, -0.1_5_6_2, -0.0_9_9_4, -0.0_9_0_6, -0.2_3_4_0, -0.0_5_3_9]],
						        # fmt: on
						    ]		)
						def __a						(							self   :    int			,			_lowercase   :    Optional[Any]			,			_lowercase   :    str			,			_lowercase   :    List[Any]		):
								A       , A					       = self.get_unet_model(model_id='stabilityai/stable-diffusion-2'			,			fpaa=_lowercase		)
								A					       = self.get_latents(_lowercase			,			shape=(4, 4, 96, 96)			,			fpaa=_lowercase		)
								A					       = self.get_encoder_hidden_states(_lowercase			,			shape=(4, 77, 1_024)			,			fpaa=_lowercase		)

								A					       = model.apply(
								    {'params': params}			,			_lowercase			,			jnp.array(_lowercase			,			dtype=jnp.intaa		)			,			encoder_hidden_states=_lowercase			,			).sample

								assert sample.shape == latents.shape

								A					       = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())		)			,			dtype=jnp.floataa		)
								A					       = jnp.array(_lowercase			,			dtype=jnp.floataa		)

								# Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware
								assert jnp.allclose(_lowercase			,			_lowercase			,			atol=1e-2		)









"""simple docstring"""


import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, 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 			lowerCamelCase__						:







							@staticmethod
							def __a						(							*_lowercase   :    int			,			**_lowercase   :    Optional[int]		):
									pass



@is_pipeline_test
@require_vision
@require_torch
class 			lowerCamelCase__						(   unittest.TestCase			):
						lowerCAmelCase       =     MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING







						def __a						(							self   :    int			,			_lowercase   :    Optional[Any]			,			_lowercase   :    int			,			_lowercase   :    Union[str, Any]		):
								A					       = pipeline(
								    'zero-shot-object-detection'			,			model='hf-internal-testing/tiny-random-owlvit-object-detection'		)

								A					       = [
								    {
								        'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
								        'candidate_labels': ['cat', 'remote', 'couch'],
								    }
								]
								return object_detector, examples







						def __a						(							self   :    Tuple			,			_lowercase   :    int			,			_lowercase   :    Tuple		):
								A					       = object_detector(examples[0]			,			threshold=0.0		)

								A					       = len(_lowercase		)
								self.assertGreater(_lowercase			,			0		)
								self.assertEqual(
								    _lowercase			,			[
								        {
								            'score': ANY(_lowercase		),
								            'label': ANY(_lowercase		),
								            'box': {'xmin': ANY(_lowercase		), 'ymin': ANY(_lowercase		), 'xmax': ANY(_lowercase		), 'ymax': ANY(_lowercase		)},
								        }
								        for i in range(_lowercase		)
								    ]			,			)







						@require_tf
						@unittest.skip('Zero Shot Object Detection not implemented in TF'		)
						def __a						(							self   :    Optional[int]		):
								pass







						@require_torch
						def __a						(							self   :    Optional[Any]		):
								A					       = pipeline(
								    'zero-shot-object-detection'			,			model='hf-internal-testing/tiny-random-owlvit-object-detection'		)

								A					       = object_detector(
								    './tests/fixtures/tests_samples/COCO/000000039769.png'			,			candidate_labels=['cat', 'remote', 'couch']			,			threshold=0.6_4			,			)

								self.assertEqual(
								    nested_simplify(_lowercase			,			decimals=4		)			,			[
								        {'score': 0.7_2_3_5, 'label': 'cat', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
								        {'score': 0.7_2_1_8, 'label': 'remote', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
								        {'score': 0.7_1_8_4, 'label': 'couch', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
								        {'score': 0.6_7_4_8, 'label': 'remote', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
								        {'score': 0.6_6_5_6, 'label': 'cat', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
								        {'score': 0.6_6_1_4, 'label': 'couch', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
								        {'score': 0.6_4_5_6, 'label': 'remote', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
								        {'score': 0.6_4_2, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 274, 'xmax': 93, 'ymax': 297}},
								        {'score': 0.6_4_1_9, 'label': 'cat', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
								    ]			,			)

								A					       = object_detector(
								    [
								        {
								            'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
								            'candidate_labels': ['cat', 'remote', 'couch'],
								        }
								    ]			,			threshold=0.6_4			,			)

								self.assertEqual(
								    nested_simplify(_lowercase			,			decimals=4		)			,			[
								        [
								            {'score': 0.7_2_3_5, 'label': 'cat', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
								            {'score': 0.7_2_1_8, 'label': 'remote', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
								            {'score': 0.7_1_8_4, 'label': 'couch', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
								            {'score': 0.6_7_4_8, 'label': 'remote', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
								            {'score': 0.6_6_5_6, 'label': 'cat', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
								            {'score': 0.6_6_1_4, 'label': 'couch', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
								            {'score': 0.6_4_5_6, 'label': 'remote', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
								            {'score': 0.6_4_2, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 274, 'xmax': 93, 'ymax': 297}},
								            {'score': 0.6_4_1_9, 'label': 'cat', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
								        ]
								    ]			,			)







						@require_torch
						@slow
						def __a						(							self   :    List[str]		):
								A					       = pipeline('zero-shot-object-detection'		)

								A					       = object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'			,			candidate_labels=['cat', 'remote', 'couch']			,			)
								self.assertEqual(
								    nested_simplify(_lowercase			,			decimals=4		)			,			[
								        {'score': 0.2_8_6_8, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
								        {'score': 0.2_7_7, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
								        {'score': 0.2_5_3_7, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
								        {'score': 0.1_4_7_4, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
								        {'score': 0.1_2_0_8, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
								    ]			,			)

								A					       = object_detector(
								    [
								        {
								            'image': 'http://images.cocodataset.org/val2017/000000039769.jpg',
								            'candidate_labels': ['cat', 'remote', 'couch'],
								        },
								        {
								            'image': 'http://images.cocodataset.org/val2017/000000039769.jpg',
								            'candidate_labels': ['cat', 'remote', 'couch'],
								        },
								    ]			,			)
								self.assertEqual(
								    nested_simplify(_lowercase			,			decimals=4		)			,			[
								        [
								            {'score': 0.2_8_6_8, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
								            {'score': 0.2_7_7, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
								            {'score': 0.2_5_3_7, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
								            {'score': 0.1_4_7_4, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
								            {'score': 0.1_2_0_8, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
								        ],
								        [
								            {'score': 0.2_8_6_8, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
								            {'score': 0.2_7_7, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
								            {'score': 0.2_5_3_7, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
								            {'score': 0.1_4_7_4, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
								            {'score': 0.1_2_0_8, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
								        ],
								    ]			,			)







						@require_tf
						@unittest.skip('Zero Shot Object Detection not implemented in TF'		)
						def __a						(							self   :    Optional[int]		):
								pass







						@require_torch
						@slow
						def __a						(							self   :    Optional[Any]		):
								A					       = 0.2
								A					       = pipeline('zero-shot-object-detection'		)

								A					       = object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'			,			candidate_labels=['cat', 'remote', 'couch']			,			threshold=_lowercase			,			)
								self.assertEqual(
								    nested_simplify(_lowercase			,			decimals=4		)			,			[
								        {'score': 0.2_8_6_8, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
								        {'score': 0.2_7_7, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
								        {'score': 0.2_5_3_7, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
								    ]			,			)







						@require_torch
						@slow
						def __a						(							self   :    Optional[int]		):
								A					       = 2
								A					       = pipeline('zero-shot-object-detection'		)

								A					       = object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'			,			candidate_labels=['cat', 'remote', 'couch']			,			top_k=_lowercase			,			)
								self.assertEqual(
								    nested_simplify(_lowercase			,			decimals=4		)			,			[
								        {'score': 0.2_8_6_8, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
								        {'score': 0.2_7_7, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
								    ]			,			)





