




from dataclasses import dataclass
from typing import Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps
from .modeling_utils import ModelMixin
from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block



@dataclass
class    lowercase_		(SCREAMING_SNAKE_CASE__ ):
						lowerCAmelCase__			      =42



class    lowercase_		(SCREAMING_SNAKE_CASE__							,	SCREAMING_SNAKE_CASE__ ):



						@register_to_config
						def __init__(			self	:   List[Any]					,							snake_case__	:   int = 6_55_36					,							snake_case__	:   Optional[int] = None					,							snake_case__	:   int = 2					,							snake_case__	:   int = 2					,							snake_case__	:   int = 0					,							snake_case__	:   str = "fourier"					,							snake_case__	:   bool = True					,							snake_case__	:   bool = False					,							snake_case__	:   float = 0.0					,							snake_case__	:   Tuple[str] = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D")					,							snake_case__	:   Tuple[str] = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip")					,							snake_case__	:   Tuple[str] = "UNetMidBlock1D"					,							snake_case__	:   str = None					,							snake_case__	:   Tuple[int] = (32, 32, 64)					,							snake_case__	:   str = None					,							snake_case__	:   int = 8					,							snake_case__	:   int = 1					,							snake_case__	:   bool = False					,							):

								"""simple docstring"""

								super().__init__()
								SCREAMING_SNAKE_CASE_					      =							sample_size

								# time
								if time_embedding_type == "fourier":
										SCREAMING_SNAKE_CASE_					      =							GaussianFourierProjection(
										    embedding_size=8					,							set_W_to_weight=snake_case__					,							log=snake_case__					,							flip_sin_to_cos=snake_case__  )
										SCREAMING_SNAKE_CASE_					      =							2 * block_out_channels[0]
								elif time_embedding_type == "positional":
										SCREAMING_SNAKE_CASE_					      =							Timesteps(
										    block_out_channels[0]					,							flip_sin_to_cos=snake_case__					,							downscale_freq_shift=snake_case__  )
										SCREAMING_SNAKE_CASE_					      =							block_out_channels[0]

								if use_timestep_embedding:
										SCREAMING_SNAKE_CASE_					      =							block_out_channels[0] * 4
										SCREAMING_SNAKE_CASE_					      =							TimestepEmbedding(
										    in_channels=snake_case__					,							time_embed_dim=snake_case__					,							act_fn=snake_case__					,							out_dim=block_out_channels[0]					,							)

								SCREAMING_SNAKE_CASE_					      =							nn.ModuleList([]  )
								SCREAMING_SNAKE_CASE_					      =							None
								SCREAMING_SNAKE_CASE_					      =							nn.ModuleList([]  )
								SCREAMING_SNAKE_CASE_					      =							None

								# down
								SCREAMING_SNAKE_CASE_					      =							in_channels
								for i, down_block_type in enumerate(snake_case__  ):
										SCREAMING_SNAKE_CASE_					      =							output_channel
										SCREAMING_SNAKE_CASE_					      =							block_out_channels[i]

										if i == 0:
												input_channel += extra_in_channels

										SCREAMING_SNAKE_CASE_					      =							i == len(snake_case__  ) - 1

										SCREAMING_SNAKE_CASE_					      =							get_down_block(
										    snake_case__					,							num_layers=snake_case__					,							in_channels=snake_case__					,							out_channels=snake_case__					,							temb_channels=block_out_channels[0]					,							add_downsample=not is_final_block or downsample_each_block					,							)
										self.down_blocks.append(snake_case__  )

								# mid
								SCREAMING_SNAKE_CASE_					      =							get_mid_block(
								    snake_case__					,							in_channels=block_out_channels[-1]					,							mid_channels=block_out_channels[-1]					,							out_channels=block_out_channels[-1]					,							embed_dim=block_out_channels[0]					,							num_layers=snake_case__					,							add_downsample=snake_case__					,							)

								# up
								SCREAMING_SNAKE_CASE_					      =							list(reversed(snake_case__  )  )
								SCREAMING_SNAKE_CASE_					      =							reversed_block_out_channels[0]
								if out_block_type is None:
										SCREAMING_SNAKE_CASE_					      =							out_channels
								else:
										SCREAMING_SNAKE_CASE_					      =							block_out_channels[0]

								for i, up_block_type in enumerate(snake_case__  ):
										SCREAMING_SNAKE_CASE_					      =							output_channel
										SCREAMING_SNAKE_CASE_					      =							(
										    reversed_block_out_channels[i + 1] if i < len(snake_case__  ) - 1 else final_upsample_channels
										)

										SCREAMING_SNAKE_CASE_					      =							i == len(snake_case__  ) - 1

										SCREAMING_SNAKE_CASE_					      =							get_up_block(
										    snake_case__					,							num_layers=snake_case__					,							in_channels=snake_case__					,							out_channels=snake_case__					,							temb_channels=block_out_channels[0]					,							add_upsample=not is_final_block					,							)
										self.up_blocks.append(snake_case__  )
										SCREAMING_SNAKE_CASE_					      =							output_channel

								# out
								SCREAMING_SNAKE_CASE_					      =							norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4					,							32  )
								SCREAMING_SNAKE_CASE_					      =							get_out_block(
								    out_block_type=snake_case__					,							num_groups_out=snake_case__					,							embed_dim=block_out_channels[0]					,							out_channels=snake_case__					,							act_fn=snake_case__					,							fc_dim=block_out_channels[-1] // 4					,							)






						def 	__a					(			self	:   Dict					,							snake_case__	:   torch.FloatTensor					,							snake_case__	:   Union[torch.Tensor, float, int]					,							snake_case__	:   bool = True					,							):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							timestep
								if not torch.is_tensor(snake_case__  ):
										SCREAMING_SNAKE_CASE_					      =							torch.tensor([timesteps]					,							dtype=torch.long					,							device=sample.device  )
								elif torch.is_tensor(snake_case__  ) and len(timesteps.shape  ) == 0:
										SCREAMING_SNAKE_CASE_					      =							timesteps[None].to(sample.device  )

								SCREAMING_SNAKE_CASE_					      =							self.time_proj(snake_case__  )
								if self.config.use_timestep_embedding:
										SCREAMING_SNAKE_CASE_					      =							self.time_mlp(snake_case__  )
								else:
										SCREAMING_SNAKE_CASE_					      =							timestep_embed[..., None]
										SCREAMING_SNAKE_CASE_					      =							timestep_embed.repeat([1, 1, sample.shape[2]]  ).to(sample.dtype  )
										SCREAMING_SNAKE_CASE_					      =							timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:])  )

								# 2. down
								SCREAMING_SNAKE_CASE_					      =							()
								for downsample_block in self.down_blocks:
										SCREAMING_SNAKE_CASE_      ,				SCREAMING_SNAKE_CASE_					      =							downsample_block(hidden_states=snake_case__					,							temb=snake_case__  )
										down_block_res_samples += res_samples

								# 3. mid
								if self.mid_block:
										SCREAMING_SNAKE_CASE_					      =							self.mid_block(snake_case__					,							snake_case__  )

								# 4. up
								for i, upsample_block in enumerate(self.up_blocks  ):
										SCREAMING_SNAKE_CASE_					      =							down_block_res_samples[-1:]
										SCREAMING_SNAKE_CASE_					      =							down_block_res_samples[:-1]
										SCREAMING_SNAKE_CASE_					      =							upsample_block(snake_case__					,							res_hidden_states_tuple=snake_case__					,							temb=snake_case__  )

								# 5. post-process
								if self.out_block:
										SCREAMING_SNAKE_CASE_					      =							self.out_block(snake_case__					,							snake_case__  )

								if not return_dict:
										return (sample,)

								return UNetaDOutput(sample=snake_case__  )





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



												@staticmethod
												def 	__a					(			*snake_case__	:   List[Any]					,							**snake_case__	:   List[Any]  ):

														"""simple docstring"""

														pass



@is_pipeline_test
@require_vision
@require_torch
class    lowercase_		(unittest.TestCase ):
						lowerCAmelCase__			      =MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING



						def 	__a					(			self	:   Any					,							snake_case__	:   List[str]					,							snake_case__	:   int					,							snake_case__	:   Optional[int]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							pipeline(
								    'zero-shot-object-detection'					,							model='hf-internal-testing/tiny-random-owlvit-object-detection'  )

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



						def 	__a					(			self	:   List[Any]					,							snake_case__	:   Union[str, Any]					,							snake_case__	:   Union[str, Any]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							object_detector(examples[0]					,							threshold=0.0  )

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



						@require_tf
						@unittest.skip('Zero Shot Object Detection not implemented in TF'  )
						def 	__a					(			self	:   Union[str, Any]  ):

								"""simple docstring"""

								pass



						@require_torch
						def 	__a					(			self	:   Tuple  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							pipeline(
								    'zero-shot-object-detection'					,							model='hf-internal-testing/tiny-random-owlvit-object-detection'  )

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

								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        {'score': 0.72_35, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.72_18, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.71_84, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								        {'score': 0.67_48, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.66_56, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.66_14, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								        {'score': 0.64_56, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								        {'score': 0.6_42, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
								        {'score': 0.64_19, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								    ]					,							)

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

								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        [
								            {'score': 0.72_35, 'label': 'cat', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.72_18, 'label': 'remote', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.71_84, 'label': 'couch', 'box': {'xmin': 2_04, 'ymin': 1_67, 'xmax': 2_32, 'ymax': 1_90}},
								            {'score': 0.67_48, 'label': 'remote', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.66_56, 'label': 'cat', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.66_14, 'label': 'couch', 'box': {'xmin': 5_71, 'ymin': 83, 'xmax': 5_98, 'ymax': 1_03}},
								            {'score': 0.64_56, 'label': 'remote', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								            {'score': 0.6_42, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 2_74, 'xmax': 93, 'ymax': 2_97}},
								            {'score': 0.64_19, 'label': 'cat', 'box': {'xmin': 4_94, 'ymin': 1_05, 'xmax': 5_21, 'ymax': 1_27}},
								        ]
								    ]					,							)



						@require_torch
						@slow
						def 	__a					(			self	:   Optional[Any]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							pipeline('zero-shot-object-detection'  )

								SCREAMING_SNAKE_CASE_					      =							object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,							candidate_labels=['cat', 'remote', 'couch']					,							)
								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								        {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								        {'score': 0.14_74, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								        {'score': 0.12_08, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								    ]					,							)

								SCREAMING_SNAKE_CASE_					      =							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(snake_case__					,							decimals=4  )					,							[
								        [
								            {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								            {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								            {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								            {'score': 0.14_74, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								            {'score': 0.12_08, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								        ],
								        [
								            {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								            {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								            {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								            {'score': 0.14_74, 'label': 'remote', 'box': {'xmin': 3_35, 'ymin': 74, 'xmax': 3_71, 'ymax': 1_87}},
								            {'score': 0.12_08, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 6_42, 'ymax': 4_76}},
								        ],
								    ]					,							)



						@require_tf
						@unittest.skip('Zero Shot Object Detection not implemented in TF'  )
						def 	__a					(			self	:   Union[str, Any]  ):

								"""simple docstring"""

								pass



						@require_torch
						@slow
						def 	__a					(			self	:   Union[str, Any]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							0.2
								SCREAMING_SNAKE_CASE_					      =							pipeline('zero-shot-object-detection'  )

								SCREAMING_SNAKE_CASE_					      =							object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,							candidate_labels=['cat', 'remote', 'couch']					,							threshold=snake_case__					,							)
								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								        {'score': 0.25_37, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 3_15, 'ymax': 4_72}},
								    ]					,							)






						@require_torch
						@slow
						def 	__a					(			self	:   Tuple  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							2
								SCREAMING_SNAKE_CASE_					      =							pipeline('zero-shot-object-detection'  )

								SCREAMING_SNAKE_CASE_					      =							object_detector(
								    'http://images.cocodataset.org/val2017/000000039769.jpg'					,							candidate_labels=['cat', 'remote', 'couch']					,							top_k=snake_case__					,							)
								self.assertEqual(
								    nested_simplify(snake_case__					,							decimals=4  )					,							[
								        {'score': 0.28_68, 'label': 'cat', 'box': {'xmin': 3_24, 'ymin': 20, 'xmax': 6_40, 'ymax': 3_73}},
								        {'score': 0.2_77, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 1_77, 'ymax': 1_15}},
								    ]					,							)