


"""simple docstring"""


import math

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from .attention_processor import Attention
from .embeddings import get_timestep_embedding
from .modeling_utils import ModelMixin



class 			lowerCamelCase__						(   UpperCAmelCase_							,    UpperCAmelCase_			):







						@register_to_config
						def __init__(							self   :    int			,			_lowercase   :    int = 128			,			_lowercase   :    int = 256			,			_lowercase   :    float = 2_0_0_0.0			,			_lowercase   :    int = 768			,			_lowercase   :    int = 12			,			_lowercase   :    int = 12			,			_lowercase   :    int = 64			,			_lowercase   :    int = 2_048			,			_lowercase   :    float = 0.1			,			):
								super().__init__()

								A					       = nn.Sequential(
								    nn.Linear(_lowercase			,			d_model * 4			,			bias=_lowercase		)			,			nn.SiLU()			,			nn.Linear(d_model * 4			,			d_model * 4			,			bias=_lowercase		)			,			nn.SiLU()			,			)

								A					       = nn.Embedding(_lowercase			,			_lowercase		)
								A					       = False

								A					       = nn.Linear(_lowercase			,			_lowercase			,			bias=_lowercase		)

								A					       = nn.Dropout(p=_lowercase		)

								A					       = nn.ModuleList()
								for lyr_num in range(_lowercase		):
										# FiLM conditional T5 decoder
										A					       = DecoderLayer(d_model=_lowercase			,			d_kv=_lowercase			,			num_heads=_lowercase			,			d_ff=_lowercase			,			dropout_rate=_lowercase		)
										self.decoders.append(_lowercase		)

								A					       = TaLayerNorm(_lowercase		)

								A					       = nn.Dropout(p=_lowercase		)
								A					       = nn.Linear(_lowercase			,			_lowercase			,			bias=_lowercase		)







						def __a						(							self   :    List[Any]			,			_lowercase   :    Union[str, Any]			,			_lowercase   :    Any		):
								A					       = torch.mul(query_input.unsqueeze(-1		)			,			key_input.unsqueeze(-2		)		)
								return mask.unsqueeze(-3		)







						def __a						(							self   :    Dict			,			_lowercase   :    List[str]			,			_lowercase   :    List[str]			,			_lowercase   :    Any		):
								A       , A       , A					       = decoder_input_tokens.shape
								assert decoder_noise_time.shape == (batch,)

								# decoder_noise_time is in [0, 1), so rescale to expected timing range.
								A					       = get_timestep_embedding(
								    decoder_noise_time * self.config.max_decoder_noise_time			,			embedding_dim=self.config.d_model			,			max_period=self.config.max_decoder_noise_time			,			).to(dtype=self.dtype		)

								A					       = self.conditioning_emb(_lowercase		).unsqueeze(1		)

								assert conditioning_emb.shape == (batch, 1, self.config.d_model * 4)

								A					       = decoder_input_tokens.shape[1]

								# If we want to use relative positions for audio context, we can just offset
								# this sequence by the length of encodings_and_masks.
								A					       = torch.broadcast_to(
								    torch.arange(_lowercase			,			device=decoder_input_tokens.device		)			,			(batch, seq_length)			,			)

								A					       = self.position_encoding(_lowercase		)

								A					       = self.continuous_inputs_projection(_lowercase		)
								inputs += position_encodings
								A					       = self.dropout(_lowercase		)

								# decoder: No padding present.
								A					       = torch.ones(
								    decoder_input_tokens.shape[:2]			,			device=decoder_input_tokens.device			,			dtype=inputs.dtype		)

								# Translate encoding masks to encoder-decoder masks.
								A					       = [(x, self.encoder_decoder_mask(_lowercase			,			_lowercase		)) for x, y in encodings_and_masks]

								# cross attend style: concat encodings
								A					       = torch.cat([x[0] for x in encodings_and_encdec_masks]			,			dim=1		)
								A					       = torch.cat([x[1] for x in encodings_and_encdec_masks]			,			dim=-1		)

								for lyr in self.decoders:
										A					       = lyr(
										    _lowercase			,			conditioning_emb=_lowercase			,			encoder_hidden_states=_lowercase			,			encoder_attention_mask=_lowercase			,			)[0]

								A					       = self.decoder_norm(_lowercase		)
								A					       = self.post_dropout(_lowercase		)

								A					       = self.spec_out(_lowercase		)
								return spec_out



class 			lowerCamelCase__						(   nn.Module			):







						def __init__(							self   :    Optional[Any]			,			_lowercase   :    Any			,			_lowercase   :    Any			,			_lowercase   :    Dict			,			_lowercase   :    Optional[int]			,			_lowercase   :    Dict			,			_lowercase   :    Optional[Any]=1e-6		):
								super().__init__()
								A					       = nn.ModuleList()

								# cond self attention: layer 0
								self.layer.append(
								    TaLayerSelfAttentionCond(d_model=_lowercase			,			d_kv=_lowercase			,			num_heads=_lowercase			,			dropout_rate=_lowercase		)		)

								# cross attention: layer 1
								self.layer.append(
								    TaLayerCrossAttention(
								        d_model=_lowercase			,			d_kv=_lowercase			,			num_heads=_lowercase			,			dropout_rate=_lowercase			,			layer_norm_epsilon=_lowercase			,			)		)

								# Film Cond MLP + dropout: last layer
								self.layer.append(
								    TaLayerFFCond(d_model=_lowercase			,			d_ff=_lowercase			,			dropout_rate=_lowercase			,			layer_norm_epsilon=_lowercase		)		)







						def __a						(							self   :    Union[str, Any]			,			_lowercase   :    List[Any]			,			_lowercase   :    List[Any]=None			,			_lowercase   :    Dict=None			,			_lowercase   :    Optional[int]=None			,			_lowercase   :    List[str]=None			,			_lowercase   :    Optional[int]=None			,			):
								A					       = self.layer[0](
								    _lowercase			,			conditioning_emb=_lowercase			,			attention_mask=_lowercase			,			)

								if encoder_hidden_states is not None:
										A					       = torch.where(encoder_attention_mask > 0			,			0			,			-1e10		).to(
										    encoder_hidden_states.dtype		)

										A					       = self.layer[1](
										    _lowercase			,			key_value_states=_lowercase			,			attention_mask=_lowercase			,			)

								# Apply Film Conditional Feed Forward layer
								A					       = self.layer[-1](_lowercase			,			_lowercase		)

								return (hidden_states,)



class 			lowerCamelCase__						(   nn.Module			):







						def __init__(							self   :    int			,			_lowercase   :    Optional[Any]			,			_lowercase   :    Optional[Any]			,			_lowercase   :    Tuple			,			_lowercase   :    str		):
								super().__init__()
								A					       = TaLayerNorm(_lowercase		)
								A					       = TaFiLMLayer(in_features=d_model * 4			,			out_features=_lowercase		)
								A					       = Attention(query_dim=_lowercase			,			heads=_lowercase			,			dim_head=_lowercase			,			out_bias=_lowercase			,			scale_qk=_lowercase		)
								A					       = nn.Dropout(_lowercase		)







						def __a						(							self   :    Any			,			_lowercase   :    Union[str, Any]			,			_lowercase   :    List[str]=None			,			_lowercase   :    str=None			,			):
								# pre_self_attention_layer_norm
								A					       = self.layer_norm(_lowercase		)

								if conditioning_emb is not None:
										A					       = self.FiLMLayer(_lowercase			,			_lowercase		)

								# Self-attention block
								A					       = self.attention(_lowercase		)

								A					       = hidden_states + self.dropout(_lowercase		)

								return hidden_states



class 			lowerCamelCase__						(   nn.Module			):







						def __init__(							self   :    Optional[int]			,			_lowercase   :    Any			,			_lowercase   :    Any			,			_lowercase   :    Optional[Any]			,			_lowercase   :    List[str]			,			_lowercase   :    int		):
								super().__init__()
								A					       = Attention(query_dim=_lowercase			,			heads=_lowercase			,			dim_head=_lowercase			,			out_bias=_lowercase			,			scale_qk=_lowercase		)
								A					       = TaLayerNorm(_lowercase			,			eps=_lowercase		)
								A					       = nn.Dropout(_lowercase		)







						def __a						(							self   :    Any			,			_lowercase   :    Any			,			_lowercase   :    Any=None			,			_lowercase   :    str=None			,			):
								A					       = self.layer_norm(_lowercase		)
								A					       = self.attention(
								    _lowercase			,			encoder_hidden_states=_lowercase			,			attention_mask=attention_mask.squeeze(1		)			,			)
								A					       = hidden_states + self.dropout(_lowercase		)
								return layer_output



class 			lowerCamelCase__						(   nn.Module			):







						def __init__(							self   :    Tuple			,			_lowercase   :    Dict			,			_lowercase   :    str			,			_lowercase   :    Dict			,			_lowercase   :    Any		):
								super().__init__()
								A					       = TaDenseGatedActDense(d_model=_lowercase			,			d_ff=_lowercase			,			dropout_rate=_lowercase		)
								A					       = TaFiLMLayer(in_features=d_model * 4			,			out_features=_lowercase		)
								A					       = TaLayerNorm(_lowercase			,			eps=_lowercase		)
								A					       = nn.Dropout(_lowercase		)







						def __a						(							self   :    Optional[Any]			,			_lowercase   :    List[Any]			,			_lowercase   :    Any=None		):
								A					       = self.layer_norm(_lowercase		)
								if conditioning_emb is not None:
										A					       = self.film(_lowercase			,			_lowercase		)

								A					       = self.DenseReluDense(_lowercase		)
								A					       = hidden_states + self.dropout(_lowercase		)
								return hidden_states



class 			lowerCamelCase__						(   nn.Module			):







						def __init__(							self   :    Any			,			_lowercase   :    List[Any]			,			_lowercase   :    Optional[int]			,			_lowercase   :    Union[str, Any]		):
								super().__init__()
								A					       = nn.Linear(_lowercase			,			_lowercase			,			bias=_lowercase		)
								A					       = nn.Linear(_lowercase			,			_lowercase			,			bias=_lowercase		)
								A					       = nn.Linear(_lowercase			,			_lowercase			,			bias=_lowercase		)
								A					       = nn.Dropout(_lowercase		)
								A					       = NewGELUActivation()







						def __a						(							self   :    Dict			,			_lowercase   :    Any		):
								A					       = self.act(self.wi_a(_lowercase		)		)
								A					       = self.wi_a(_lowercase		)
								A					       = hidden_gelu * hidden_linear
								A					       = self.dropout(_lowercase		)

								A					       = self.wo(_lowercase		)
								return hidden_states



class 			lowerCamelCase__						(   nn.Module			):







						def __init__(							self   :    List[Any]			,			_lowercase   :    Union[str, Any]			,			_lowercase   :    Optional[Any]=1e-6		):
								super().__init__()
								A					       = nn.Parameter(torch.ones(_lowercase		)		)
								A					       = eps







						def __a						(							self   :    Any			,			_lowercase   :    List[str]		):
								# T5 uses a layer_norm which only scales and doesn't shift, which is also known as Root Mean
								# Square Layer Normalization https://arxiv.org/abs/1910.07467 thus variance is calculated
								# w/o mean and there is no bias. Additionally we want to make sure that the accumulation for
								# half-precision inputs is done in fp32

								A					       = hidden_states.to(torch.floataa		).pow(2		).mean(-1			,			keepdim=_lowercase		)
								A					       = hidden_states * torch.rsqrt(variance + self.variance_epsilon		)

								# convert into half-precision if necessary
								if self.weight.dtype in [torch.floataa, torch.bfloataa]:
										A					       = hidden_states.to(self.weight.dtype		)

								return self.weight * hidden_states



class 			lowerCamelCase__						(   nn.Module			):







						def __a						(							self   :    Optional[int]			,			_lowercase   :    torch.Tensor		):
								return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi		) * (input + 0.0_4_4_7_1_5 * torch.pow(_lowercase			,			3.0		))		))



class 			lowerCamelCase__						(   nn.Module			):







						def __init__(							self   :    Tuple			,			_lowercase   :    Any			,			_lowercase   :    Optional[int]		):
								super().__init__()
								A					       = nn.Linear(_lowercase			,			out_features * 2			,			bias=_lowercase		)







						def __a						(							self   :    Dict			,			_lowercase   :    Union[str, Any]			,			_lowercase   :    Optional[Any]		):
								A					       = self.scale_bias(_lowercase		)
								A       , A					       = torch.chunk(_lowercase			,			2			,			-1		)
								A					       = x * (1 + scale) + shift
								return x









"""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}},
								    ]			,			)





