


import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import is_torch_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask


if is_torch_available():
	import torch

	from transformers import BlipTextModel
	from transformers.models.blip.modeling_blip import BLIP_PRETRAINED_MODEL_ARCHIVE_LIST




class 			lowercase_				:





				def __init__(							self					,     lowercase_					,     lowercase_=12					,     lowercase_=7					,     lowercase_=True					,     lowercase_=True					,     lowercase_=True					,     lowercase_=99					,     lowercase_=32					,     lowercase_=32					,     lowercase_=5					,     lowercase_=4					,     lowercase_=37					,     lowercase_=0.1					,     lowercase_=0.1					,     lowercase_=512					,     lowercase_=0.02					,     lowercase_=0					,     lowercase_=None					,     )     ->   Optional[int]:
											a__     						=parent
											a__     						=batch_size
											a__     						=seq_length
											a__     						=is_training
											a__     						=use_input_mask
											a__     						=use_labels
											a__     						=vocab_size
											a__     						=hidden_size
											a__     						=projection_dim
											a__     						=num_hidden_layers
											a__     						=num_attention_heads
											a__     						=intermediate_size
											a__     						=dropout
											a__     						=attention_dropout
											a__     						=max_position_embeddings
											a__     						=initializer_range
											a__     						=scope
											a__     						=bos_token_id





				def 	__UpperCamelCase						(							self)     ->   Dict:
											a__     						=ids_tensor([self.batch_size, self.seq_length]					,     self.vocab_size)

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

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

											a__     						=self.get_config()

											return config, input_ids, input_mask





				def 	__UpperCamelCase						(							self)     ->   Optional[int]:
											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 	__UpperCamelCase						(							self					,     lowercase_					,     lowercase_					,     lowercase_)     ->   Optional[int]:
											a__     						=BlipTextModel(config=lowercase_)
											model.to(lowercase_)
											model.eval()
											with torch.no_grad():
																		a__     						=model(lowercase_					,     attention_mask=lowercase_)
																		a__     						=model(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 	__UpperCamelCase						(							self)     ->   int:
											a__     						=self.prepare_config_and_inputs()
											a__     						=config_and_inputs
											a__     						={'''input_ids''': input_ids, '''attention_mask''': input_mask}
											return config, inputs_dict







@require_torch
class 			lowercase_				(lowercase__			,				unittest.TestCase      ):
				snake_case  						=(BlipTextModel,) if is_torch_available() else ()
				snake_case  						=False
				snake_case  						=False
				snake_case  						=False





				def 	__UpperCamelCase						(							self)     ->   Tuple:
											a__     						=BlipTextModelTester(self)
											a__     						=ConfigTester(self					,     config_class=lowercase_					,     hidden_size=37)





				def 	__UpperCamelCase						(							self)     ->   Any:
											self.config_tester.run_common_tests()





				def 	__UpperCamelCase						(							self)     ->   int:
											a__     						=self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*lowercase_)





				def 	__UpperCamelCase						(							self)     ->   Dict:
											pass





				def 	__UpperCamelCase						(							self)     ->   Dict:
											pass





				@unittest.skip(reason='Blip does not use inputs_embeds')
				def 	__UpperCamelCase						(							self)     ->   List[str]:
											pass





				@unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING')
				def 	__UpperCamelCase						(							self)     ->   Dict:
											pass





				@unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING')
				def 	__UpperCamelCase						(							self)     ->   Dict:
											pass





				@slow
				def 	__UpperCamelCase						(							self)     ->   Dict:
											for model_name in BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		a__     						=BlipTextModel.from_pretrained(lowercase_)
																		self.assertIsNotNone(lowercase_)





				def 	__UpperCamelCase						(							self)     ->   Optional[int]:
											super().test_pt_tf_model_equivalence(allow_missing_keys=lowercase_)





from typing import Dict, Optional, Tuple

import torch






def      _lowercase(				__a							:	torch.Tensor    ):
							a__     						=boundaries[1] - boundaries[0]
							a__     						=boundaries + step / 2
							a__     						=torch.cat([bin_centers, (bin_centers[-1] + step).unsqueeze(-1    )]						,    dim=0    )
							return bin_centers






def      _lowercase(				__a							:	torch.Tensor						,    __a							:	torch.Tensor						,    ):
							a__     						=_calculate_bin_centers(__a    )
							return (
							    torch.sum(aligned_distance_error_probs * bin_centers						,    dim=-1    ),
							    bin_centers[-1],
							)






def      _lowercase(				__a							:	torch.Tensor						,    __a							:	int = 31						,    __a							:	int = 64						,    **__a							:	List[str]						,    ):
							a__     						=torch.linspace(0						,    __a						,    steps=(no_bins - 1)						,    device=logits.device    )

							a__     						=torch.nn.functional.softmax(__a						,    dim=-1    )
							a__						,    a__     						=_calculate_expected_aligned_error(
							    alignment_confidence_breaks=__a						,    aligned_distance_error_probs=__a						,    )

							return {
							    "aligned_confidence_probs": aligned_confidence_probs,
							    "predicted_aligned_error": predicted_aligned_error,
							    "max_predicted_aligned_error": max_predicted_aligned_error,
							}







def      _lowercase(				__a							:	torch.Tensor						,    __a							:	Optional[torch.Tensor] = None						,    __a							:	int = 31						,    __a							:	int = 64						,    __a							:	float = 1e-8						,    **__a							:	str						,    ):
							if residue_weights is None:
														a__     						=logits.new_ones(logits.shape[-2]    )

							a__     						=torch.linspace(0						,    __a						,    steps=(no_bins - 1)						,    device=logits.device    )

							a__     						=_calculate_bin_centers(__a    )
							torch.sum(__a    )
							a__     						=logits.shape[-2]
							a__     						=max(__a						,    19    )

							a__     						=1.24 * (clipped_n - 15) ** (1.0 / 3) - 1.8

							a__     						=torch.nn.functional.softmax(__a						,    dim=-1    )

							a__     						=1.0 / (1 + (bin_centers**2) / (da**2))
							a__     						=torch.sum(probs * tm_per_bin						,    dim=-1    )

							a__     						=residue_weights / (eps + residue_weights.sum())
							a__     						=torch.sum(predicted_tm_term * normed_residue_mask						,    dim=-1    )

							a__     						=per_alignment * residue_weights

							a__     						=(weighted == torch.max(__a    )).nonzero()[0]
							return per_alignment[tuple(__a    )]

