from typing import Callable, Dict, Optional, Tuple

import torch
from torch import nn
from torch.distributions import (
    AffineTransform,
    Distribution,
    Independent,
    NegativeBinomial,
    Normal,
    StudentT,
    TransformedDistribution,
)



class        lowerCamelCase__				(   UpperCAmelCase				):





			def __init__(       self	:	Optional[Any]  ,  lowercase__	:	Distribution  ,  lowercase__	:	Any=None  ,  lowercase__	:	Optional[int]=None  ,  lowercase__	:	List[str]=0						):
										_lowerCAmelCase							=							1.0 if scale is None else scale
										_lowerCAmelCase							=							0.0 if loc is None else loc

										super().__init__(lowercase__  ,  [AffineTransform(loc=self.loc  ,  scale=self.scale  ,  event_dim=lowercase__						)]						)





			@property
			def   SCREAMING_SNAKE_CASE__     (       self	:	List[str]						):
										return self.base_dist.mean * self.scale + self.loc





			@property
			def   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, Any]						):
										return self.base_dist.variance * self.scale**2





			@property
			def   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										return self.variance.sqrt()







class        lowerCamelCase__				(   nn.Module				):





			def __init__(       self	:	List[str]  ,  lowercase__	:	int  ,  lowercase__	:	Dict[str, int]  ,  lowercase__	:	Callable[..., Tuple[torch.Tensor]]  ,  **lowercase__	:	Any						):
										super().__init__(**lowercase__						)
										_lowerCAmelCase							=							args_dim
										_lowerCAmelCase							=							nn.ModuleList([nn.Linear(lowercase__  ,  lowercase__						) for dim in args_dim.values()]						)
										_lowerCAmelCase							=							domain_map





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]  ,  lowercase__	:	torch.Tensor						):
										_lowerCAmelCase							=							[proj(lowercase__						) for proj in self.proj]

										return self.domain_map(*lowercase__						)







class        lowerCamelCase__				(   nn.Module				):





			def __init__(       self	:	Optional[Any]  ,  lowercase__	:	int						):
										super().__init__()
										_lowerCAmelCase							=							function





			def   SCREAMING_SNAKE_CASE__     (       self	:	Any  ,  lowercase__	:	Optional[Any]  ,  *lowercase__	:	List[str]						):
										return self.function(lowercase__  ,  *lowercase__						)







class        lowerCamelCase__				:
			UpperCamelCase__    							=42
			UpperCamelCase__    							=42
			UpperCamelCase__    							=42





			def __init__(       self	:	Any  ,  lowercase__	:	int = 1						):
										_lowerCAmelCase							=							dim
										_lowerCAmelCase							=							{k: dim * self.args_dim[k] for k in self.args_dim}





			def   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, Any]  ,  lowercase__	:	List[Any]						):
										if self.dim == 1:
																	return self.distribution_class(*lowercase__						)
										else:
																	return Independent(self.distribution_class(*lowercase__						)  ,  1						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[Any]  ,  lowercase__	:	Dict  ,  lowercase__	:	Optional[torch.Tensor] = None  ,  lowercase__	:	Optional[torch.Tensor] = None  ,  ):
										_lowerCAmelCase							=							self._base_distribution(lowercase__						)
										if loc is None and scale is None:
																	return distr
										else:
																	return AffineTransformed(lowercase__  ,  loc=lowercase__  ,  scale=lowercase__  ,  event_dim=self.event_dim						)





			@property
			def   SCREAMING_SNAKE_CASE__     (       self	:	List[str]						):
										return () if self.dim == 1 else (self.dim,)





			@property
			def   SCREAMING_SNAKE_CASE__     (       self	:	Dict						):
										return len(self.event_shape						)





			@property
			def   SCREAMING_SNAKE_CASE__     (       self	:	Any						):
										return 0.0





			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[Any]  ,  lowercase__	:	int						):
										return ParameterProjection(
										    in_features=lowercase__  ,  args_dim=self.args_dim  ,  domain_map=LambdaLayer(self.domain_map						)  ,  )





			def   SCREAMING_SNAKE_CASE__     (       self	:	Any  ,  *lowercase__	:	torch.Tensor						):
										raise NotImplementedError()





			@staticmethod
			def   SCREAMING_SNAKE_CASE__     (       lowercase__	:	torch.Tensor						):
										return (x + torch.sqrt(torch.square(lowercase__						) + 4.0						)) / 2.0







class        lowerCamelCase__				(   UpperCAmelCase				):
			UpperCamelCase__    							={"df": 1, "loc": 1, "scale": 1}
			UpperCamelCase__    							=StudentT





			@classmethod
			def   SCREAMING_SNAKE_CASE__     (       cls	:	Union[str, Any]  ,  lowercase__	:	torch.Tensor  ,  lowercase__	:	torch.Tensor  ,  lowercase__	:	torch.Tensor						):
										_lowerCAmelCase							=							cls.squareplus(lowercase__						).clamp_min(torch.finfo(scale.dtype						).eps						)
										_lowerCAmelCase							=							2.0 + cls.squareplus(lowercase__						)
										return df.squeeze(-1						), loc.squeeze(-1						), scale.squeeze(-1						)







class        lowerCamelCase__				(   UpperCAmelCase				):
			UpperCamelCase__    							={"loc": 1, "scale": 1}
			UpperCamelCase__    							=Normal





			@classmethod
			def   SCREAMING_SNAKE_CASE__     (       cls	:	List[Any]  ,  lowercase__	:	torch.Tensor  ,  lowercase__	:	torch.Tensor						):
										_lowerCAmelCase							=							cls.squareplus(lowercase__						).clamp_min(torch.finfo(scale.dtype						).eps						)
										return loc.squeeze(-1						), scale.squeeze(-1						)







class        lowerCamelCase__				(   UpperCAmelCase				):
			UpperCamelCase__    							={"total_count": 1, "logits": 1}
			UpperCamelCase__    							=NegativeBinomial





			@classmethod
			def   SCREAMING_SNAKE_CASE__     (       cls	:	Tuple  ,  lowercase__	:	torch.Tensor  ,  lowercase__	:	torch.Tensor						):
										_lowerCAmelCase							=							cls.squareplus(lowercase__						)
										return total_count.squeeze(-1						), logits.squeeze(-1						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, Any]  ,  lowercase__	:	Union[str, Any]						):
										_lowerCAmelCase			,		_lowerCAmelCase							=							distr_args
										if self.dim == 1:
																	return self.distribution_class(total_count=lowercase__  ,  logits=lowercase__						)
										else:
																	return Independent(self.distribution_class(total_count=lowercase__  ,  logits=lowercase__						)  ,  1						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[str]  ,  lowercase__	:	Dict  ,  lowercase__	:	Optional[torch.Tensor] = None  ,  lowercase__	:	Optional[torch.Tensor] = None						):
										_lowerCAmelCase			,		_lowerCAmelCase							=							distr_args

										if scale is not None:
																	# See scaling property of Gamma.
																	logits += scale.log()

										return self._base_distribution((total_count, logits)						)







from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
				import tensorflow as tf

				from transformers import TFBlipTextModel
				from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST



class        lowerCamelCase__				:





			def __init__(       self	:	Optional[Any]  ,  lowercase__	:	int  ,  lowercase__	:	List[str]=12  ,  lowercase__	:	Optional[int]=7  ,  lowercase__	:	List[Any]=True  ,  lowercase__	:	str=True  ,  lowercase__	:	Optional[Any]=True  ,  lowercase__	:	List[str]=99  ,  lowercase__	:	Optional[int]=32  ,  lowercase__	:	Tuple=32  ,  lowercase__	:	Dict=2  ,  lowercase__	:	Optional[int]=4  ,  lowercase__	:	str=37  ,  lowercase__	:	Optional[Any]=0.1  ,  lowercase__	:	List[str]=0.1  ,  lowercase__	:	Union[str, Any]=5_12  ,  lowercase__	:	List[str]=0.0_2  ,  lowercase__	:	Tuple=0  ,  lowercase__	:	List[Any]=None  ,  ):
										_lowerCAmelCase							=							parent
										_lowerCAmelCase							=							batch_size
										_lowerCAmelCase							=							seq_length
										_lowerCAmelCase							=							is_training
										_lowerCAmelCase							=							use_input_mask
										_lowerCAmelCase							=							use_labels
										_lowerCAmelCase							=							vocab_size
										_lowerCAmelCase							=							hidden_size
										_lowerCAmelCase							=							projection_dim
										_lowerCAmelCase							=							num_hidden_layers
										_lowerCAmelCase							=							num_attention_heads
										_lowerCAmelCase							=							intermediate_size
										_lowerCAmelCase							=							dropout
										_lowerCAmelCase							=							attention_dropout
										_lowerCAmelCase							=							max_position_embeddings
										_lowerCAmelCase							=							initializer_range
										_lowerCAmelCase							=							scope
										_lowerCAmelCase							=							bos_token_id





			def   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										_lowerCAmelCase							=							ids_tensor([self.batch_size, self.seq_length]  ,  self.vocab_size						)

										_lowerCAmelCase							=							None
										if self.use_input_mask:
																	_lowerCAmelCase							=							random_attention_mask([self.batch_size, self.seq_length]						)

										if input_mask is not None:
																	_lowerCAmelCase							=							input_mask.numpy()
																	_lowerCAmelCase			,		_lowerCAmelCase							=							input_mask.shape
																	_lowerCAmelCase							=							np.random.randint(1  ,  seq_length - 1  ,  size=(batch_size,)						)
																	for batch_idx, start_index in enumerate(lowercase__						):
																								_lowerCAmelCase							=							1
																								_lowerCAmelCase							=							0

										_lowerCAmelCase							=							self.get_config()

										return config, input_ids, tf.convert_to_tensor(lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										return BlipTextConfig(
										    vocab_size=self.vocab_size  ,  hidden_size=self.hidden_size  ,  projection_dim=self.projection_dim  ,  num_hidden_layers=self.num_hidden_layers  ,  num_attention_heads=self.num_attention_heads  ,  intermediate_size=self.intermediate_size  ,  dropout=self.dropout  ,  attention_dropout=self.attention_dropout  ,  max_position_embeddings=self.max_position_embeddings  ,  initializer_range=self.initializer_range  ,  bos_token_id=self.bos_token_id  ,  )





			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple  ,  lowercase__	:	int  ,  lowercase__	:	List[Any]  ,  lowercase__	:	Union[str, Any]						):
										_lowerCAmelCase							=							TFBlipTextModel(config=lowercase__						)
										_lowerCAmelCase							=							model(lowercase__  ,  attention_mask=lowercase__  ,  training=lowercase__						)
										_lowerCAmelCase							=							model(lowercase__  ,  training=lowercase__						)
										self.parent.assertEqual(result.last_hidden_state.shape  ,  (self.batch_size, self.seq_length, self.hidden_size)						)
										self.parent.assertEqual(result.pooler_output.shape  ,  (self.batch_size, self.hidden_size)						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										_lowerCAmelCase							=							self.prepare_config_and_inputs()
										_lowerCAmelCase			,		_lowerCAmelCase			,		_lowerCAmelCase							=							config_and_inputs
										_lowerCAmelCase							=							{'input_ids': input_ids, 'attention_mask': input_mask}
										return config, inputs_dict







@require_tf
class        lowerCamelCase__				(   UpperCAmelCase       ,unittest.TestCase				):
			UpperCamelCase__    							=(TFBlipTextModel,) if is_tf_available() else ()
			UpperCamelCase__    							=False
			UpperCamelCase__    							=False
			UpperCamelCase__    							=False





			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[Any]						):
										_lowerCAmelCase							=							BlipTextModelTester(self						)
										_lowerCAmelCase							=							ConfigTester(self  ,  config_class=lowercase__  ,  hidden_size=37						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, Any]						):
										self.config_tester.run_common_tests()





			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Dict						):
										pass





			def   SCREAMING_SNAKE_CASE__     (       self	:	Dict						):
										pass





			@unittest.skip(reason='Blip does not use inputs_embeds'						)
			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[Any]						):
										pass





			@unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING'						)
			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[Any]						):
										pass





			@unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING'						)
			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										pass





			@slow
			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	_lowerCAmelCase							=							TFBlipTextModel.from_pretrained(lowercase__						)
																	self.assertIsNotNone(lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Any  ,  lowercase__	:	Any=True						):
										super().test_pt_tf_model_equivalence(allow_missing_keys=lowercase__						)






