


"""simple docstring"""
import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import TransformeraDModel, VQDiffusionPipeline, VQDiffusionScheduler, VQModel
from diffusers.pipelines.vq_diffusion.pipeline_vq_diffusion import LearnedClassifierFreeSamplingEmbeddings
from diffusers.utils import load_numpy, slow, torch_device
from diffusers.utils.testing_utils import require_torch_gpu


UpperCAmelCase				=						False




class 	lowercase__				(					unittest.TestCase				):





   def 		UpperCamelCase_  (							self)  ->  Optional[Any]:
      # clean up the VRAM after each test
      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()





   @property
   def 		UpperCamelCase_  (							self)  ->  Optional[int]:
      return 12





   @property
   def 		UpperCamelCase_  (							self)  ->  List[Any]:
      return 12





   @property
   def 		UpperCamelCase_  (							self)  ->  Dict:
      return 32





   @property
   def 		UpperCamelCase_  (							self)  ->  Dict:
      torch.manual_seed(0)
      _lowerCamelCase	:					Tuple   =		VQModel(
          block_out_channels=[32, 64]      ,   in_channels=3      ,   out_channels=3      ,   down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]      ,   up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]      ,   latent_channels=3      ,   num_vq_embeddings=self.num_embed      ,   vq_embed_dim=3      ,   )
      return model





   @property
   def 		UpperCamelCase_  (							self)  ->  Tuple:
      _lowerCamelCase	:					Union[str, Any]   =		CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""")
      return tokenizer





   @property
   def 		UpperCamelCase_  (							self)  ->  Tuple:
      torch.manual_seed(0)
      _lowerCamelCase	:					List[str]   =		CLIPTextConfig(
          bos_token_id=0      ,   eos_token_id=2      ,   hidden_size=self.text_embedder_hidden_size      ,   intermediate_size=37      ,   layer_norm_eps=1e-0_5      ,   num_attention_heads=4      ,   num_hidden_layers=5      ,   pad_token_id=1      ,   vocab_size=1000      ,   )
      return CLIPTextModel(SCREAMING_SNAKE_CASE)





   @property
   def 		UpperCamelCase_  (							self)  ->  Optional[int]:
      torch.manual_seed(0)

      _lowerCamelCase	:					Tuple   =		12
      _lowerCamelCase	:					List[Any]   =		12

      _lowerCamelCase	:					List[str]   =		{
          """attention_bias""": True,
          """cross_attention_dim""": 32,
          """attention_head_dim""": height * width,
          """num_attention_heads""": 1,
          """num_vector_embeds""": self.num_embed,
          """num_embeds_ada_norm""": self.num_embeds_ada_norm,
          """norm_num_groups""": 32,
          """sample_size""": width,
          """activation_fn""": """geglu-approximate""",
      }

      _lowerCamelCase	:					Dict   =		TransformeraDModel(**SCREAMING_SNAKE_CASE)
      return model





   def 		UpperCamelCase_  (							self)  ->  Any:
      _lowerCamelCase	:					Optional[Any]   =		"""cpu"""

      _lowerCamelCase	:					Optional[Any]   =		self.dummy_vqvae
      _lowerCamelCase	:					Dict   =		self.dummy_text_encoder
      _lowerCamelCase	:					int   =		self.dummy_tokenizer
      _lowerCamelCase	:					List[str]   =		self.dummy_transformer
      _lowerCamelCase	:					List[Any]   =		VQDiffusionScheduler(self.num_embed)
      _lowerCamelCase	:					Tuple   =		LearnedClassifierFreeSamplingEmbeddings(learnable=SCREAMING_SNAKE_CASE)

      _lowerCamelCase	:					int   =		VQDiffusionPipeline(
          vqvae=SCREAMING_SNAKE_CASE      ,   text_encoder=SCREAMING_SNAKE_CASE      ,   tokenizer=SCREAMING_SNAKE_CASE      ,   transformer=SCREAMING_SNAKE_CASE      ,   scheduler=SCREAMING_SNAKE_CASE      ,   learned_classifier_free_sampling_embeddings=SCREAMING_SNAKE_CASE      ,   )
      _lowerCamelCase	:					str   =		pipe.to(SCREAMING_SNAKE_CASE)
      pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE)

      _lowerCamelCase	:					List[str]   =		"""teddy bear playing in the pool"""

      _lowerCamelCase	:					Union[str, Any]   =		torch.Generator(device=SCREAMING_SNAKE_CASE).manual_seed(0)
      _lowerCamelCase	:					Dict   =		pipe([prompt]      ,   generator=SCREAMING_SNAKE_CASE      ,   num_inference_steps=2      ,   output_type="""np""")
      _lowerCamelCase	:					int   =		output.images

      _lowerCamelCase	:					int   =		torch.Generator(device=SCREAMING_SNAKE_CASE).manual_seed(0)
      _lowerCamelCase	:					Dict   =		pipe(
          [prompt]      ,   generator=SCREAMING_SNAKE_CASE      ,   output_type="""np"""      ,   return_dict=SCREAMING_SNAKE_CASE      ,   num_inference_steps=2)[0]

      _lowerCamelCase	:					Optional[int]   =		image[0, -3:, -3:, -1]
      _lowerCamelCase	:					Tuple   =		image_from_tuple[0, -3:, -3:, -1]

      assert image.shape == (1, 24, 24, 3)

      _lowerCamelCase	:					Dict   =		np.array([0.65_51, 0.61_68, 0.50_08, 0.56_76, 0.56_59, 0.42_95, 0.60_73, 0.55_99, 0.49_92])

      assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2
      assert np.abs(image_from_tuple_slice.flatten() - expected_slice).max() < 1e-2





   def 		UpperCamelCase_  (							self)  ->  str:
      _lowerCamelCase	:					Dict   =		"""cpu"""

      _lowerCamelCase	:					Optional[int]   =		self.dummy_vqvae
      _lowerCamelCase	:					Union[str, Any]   =		self.dummy_text_encoder
      _lowerCamelCase	:					Union[str, Any]   =		self.dummy_tokenizer
      _lowerCamelCase	:					Optional[int]   =		self.dummy_transformer
      _lowerCamelCase	:					Any   =		VQDiffusionScheduler(self.num_embed)
      _lowerCamelCase	:					Tuple   =		LearnedClassifierFreeSamplingEmbeddings(
          learnable=SCREAMING_SNAKE_CASE      ,   hidden_size=self.text_embedder_hidden_size      ,   length=tokenizer.model_max_length)

      _lowerCamelCase	:					Dict   =		VQDiffusionPipeline(
          vqvae=SCREAMING_SNAKE_CASE      ,   text_encoder=SCREAMING_SNAKE_CASE      ,   tokenizer=SCREAMING_SNAKE_CASE      ,   transformer=SCREAMING_SNAKE_CASE      ,   scheduler=SCREAMING_SNAKE_CASE      ,   learned_classifier_free_sampling_embeddings=SCREAMING_SNAKE_CASE      ,   )
      _lowerCamelCase	:					Any   =		pipe.to(SCREAMING_SNAKE_CASE)
      pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE)

      _lowerCamelCase	:					Dict   =		"""teddy bear playing in the pool"""

      _lowerCamelCase	:					Any   =		torch.Generator(device=SCREAMING_SNAKE_CASE).manual_seed(0)
      _lowerCamelCase	:					Optional[int]   =		pipe([prompt]      ,   generator=SCREAMING_SNAKE_CASE      ,   num_inference_steps=2      ,   output_type="""np""")
      _lowerCamelCase	:					Union[str, Any]   =		output.images

      _lowerCamelCase	:					List[str]   =		torch.Generator(device=SCREAMING_SNAKE_CASE).manual_seed(0)
      _lowerCamelCase	:					str   =		pipe(
          [prompt]      ,   generator=SCREAMING_SNAKE_CASE      ,   output_type="""np"""      ,   return_dict=SCREAMING_SNAKE_CASE      ,   num_inference_steps=2)[0]

      _lowerCamelCase	:					Any   =		image[0, -3:, -3:, -1]
      _lowerCamelCase	:					str   =		image_from_tuple[0, -3:, -3:, -1]

      assert image.shape == (1, 24, 24, 3)

      _lowerCamelCase	:					List[str]   =		np.array([0.66_93, 0.60_75, 0.49_59, 0.57_01, 0.55_83, 0.43_33, 0.61_71, 0.56_84, 0.49_88])

      assert np.abs(image_slice.flatten() - expected_slice).max() < 2.0
      assert np.abs(image_from_tuple_slice.flatten() - expected_slice).max() < 1e-2







@slow
@require_torch_gpu
class 	lowercase__				(					unittest.TestCase				):





   def 		UpperCamelCase_  (							self)  ->  Optional[int]:
      # clean up the VRAM after each test
      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()





   def 		UpperCamelCase_  (							self)  ->  Any:
      _lowerCamelCase	:					Union[str, Any]   =		load_numpy(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
          """/vq_diffusion/teddy_bear_pool_classifier_free_sampling.npy""")

      _lowerCamelCase	:					Dict   =		VQDiffusionPipeline.from_pretrained("""microsoft/vq-diffusion-ithq""")
      _lowerCamelCase	:					List[Any]   =		pipeline.to(SCREAMING_SNAKE_CASE)
      pipeline.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE)

      # requires GPU generator for gumbel softmax
      # don't use GPU generator in tests though
      _lowerCamelCase	:					Union[str, Any]   =		torch.Generator(device=SCREAMING_SNAKE_CASE).manual_seed(0)
      _lowerCamelCase	:					List[Any]   =		pipeline(
          """teddy bear playing in the pool"""      ,   num_images_per_prompt=1      ,   generator=SCREAMING_SNAKE_CASE      ,   output_type="""np"""      ,   )

      _lowerCamelCase	:					str   =		output.images[0]

      assert image.shape == (256, 256, 3)
      assert np.abs(expected_image - image).max() < 2.0




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 						__magic_name__					:






      @staticmethod
      def 	UpperCAmelCase_				(				*_lowercase		,						**_lowercase	)->     Optional[int]:
          pass






@is_pipeline_test
@require_vision
@require_torch
class 						__magic_name__					(							unittest.TestCase				):
     UpperCamelCase_	:Optional[Any]										=      MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING






     def 	UpperCAmelCase_				(				self		,						_lowercase		,						_lowercase		,						_lowercase	)->     Union[str, Any]:
         UpperCamelCase_			  =						pipeline(
             "zero-shot-object-detection"		,						model="hf-internal-testing/tiny-random-owlvit-object-detection"	)

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






     def 	UpperCAmelCase_				(				self		,						_lowercase		,						_lowercase	)->     Union[str, Any]:
         UpperCamelCase_			  =						object_detector(examples[0]		,						threshold=0.0	)

         UpperCamelCase_			  =						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 	UpperCAmelCase_				(				self	)->     str:
         pass






     @require_torch
     def 	UpperCAmelCase_				(				self	)->     List[Any]:
         UpperCamelCase_			  =						pipeline(
             "zero-shot-object-detection"		,						model="hf-internal-testing/tiny-random-owlvit-object-detection"	)

         UpperCamelCase_			  =						object_detector(
             "./tests/fixtures/tests_samples/COCO/000000039769.png"		,						candidate_labels=["cat", "remote", "couch"]		,						threshold=0.64		,						)

         self.assertEqual(
             nested_simplify(_lowercase		,						decimals=4	)		,						[
                 {"score": 0.7_235, "label": "cat", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                 {"score": 0.7_218, "label": "remote", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                 {"score": 0.7_184, "label": "couch", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                 {"score": 0.6_748, "label": "remote", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                 {"score": 0.6_656, "label": "cat", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                 {"score": 0.6_614, "label": "couch", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                 {"score": 0.6_456, "label": "remote", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
                 {"score": 0.642, "label": "remote", "box": {"xmin": 67, "ymin": 274, "xmax": 93, "ymax": 297}},
                 {"score": 0.6_419, "label": "cat", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
             ]		,						)

         UpperCamelCase_			  =						object_detector(
             [
                 {
                     "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
                     "candidate_labels": ["cat", "remote", "couch"],
                 }
             ]		,						threshold=0.64		,						)

         self.assertEqual(
             nested_simplify(_lowercase		,						decimals=4	)		,						[
                 [
                     {"score": 0.7_235, "label": "cat", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                     {"score": 0.7_218, "label": "remote", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                     {"score": 0.7_184, "label": "couch", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                     {"score": 0.6_748, "label": "remote", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                     {"score": 0.6_656, "label": "cat", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                     {"score": 0.6_614, "label": "couch", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                     {"score": 0.6_456, "label": "remote", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
                     {"score": 0.642, "label": "remote", "box": {"xmin": 67, "ymin": 274, "xmax": 93, "ymax": 297}},
                     {"score": 0.6_419, "label": "cat", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
                 ]
             ]		,						)






     @require_torch
     @slow
     def 	UpperCAmelCase_				(				self	)->     Any:
         UpperCamelCase_			  =						pipeline("zero-shot-object-detection"	)

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

         UpperCamelCase_			  =						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_868, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
                     {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
                     {"score": 0.2_537, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
                     {"score": 0.1_474, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
                     {"score": 0.1_208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
                 ],
                 [
                     {"score": 0.2_868, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
                     {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
                     {"score": 0.2_537, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
                     {"score": 0.1_474, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
                     {"score": 0.1_208, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
                 ],
             ]		,						)






     @require_tf
     @unittest.skip("Zero Shot Object Detection not implemented in TF"	)
     def 	UpperCAmelCase_				(				self	)->     Optional[int]:
         pass






     @require_torch
     @slow
     def 	UpperCAmelCase_				(				self	)->     Optional[Any]:
         UpperCamelCase_			  =						0.2
         UpperCamelCase_			  =						pipeline("zero-shot-object-detection"	)

         UpperCamelCase_			  =						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_868, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
                 {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
                 {"score": 0.2_537, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
             ]		,						)






     @require_torch
     @slow
     def 	UpperCAmelCase_				(				self	)->     Optional[int]:
         UpperCamelCase_			  =						2
         UpperCamelCase_			  =						pipeline("zero-shot-object-detection"	)

         UpperCamelCase_			  =						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_868, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
                 {"score": 0.277, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
             ]		,						)
