



'''simple docstring'''







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






	'''simple docstring'''

	def __init__(  self		:  List[str]   ,		__A		:  int   ,		__A		:  Optional[int]=12   ,		__A		:  int=7   ,		__A		:  Dict=True   ,		__A		:  Optional[int]=True   ,		__A		:  List[str]=True   ,		__A		:  str=99   ,		__A		:  Tuple=32   ,		__A		:  Tuple=32   ,		__A		:  Tuple=5   ,		__A		:  Dict=4   ,		__A		:  Optional[int]=37   ,		__A		:  Optional[int]=0.1   ,		__A		:  Dict=0.1   ,		__A		:  List[Any]=512   ,		__A		:  Optional[int]=0.0_2   ,		__A		:  List[str]=0   ,		__A		:  Optional[Any]=None   ,		)     ->			Optional[int]:




								'''simple docstring'''





								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      lowercase__							(  self		:  str					)     ->			int:




								'''simple docstring'''





								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__  ,lowerCAmelCase__  =   input_mask.shape
															lowerCAmelCase__  =   np.random.randint(1   ,		seq_length - 1   ,		size=(batch_size,)					)
															for batch_idx, start_index in enumerate(__A					):
																						lowerCAmelCase__  =   1
																						lowerCAmelCase__  =   0

								lowerCAmelCase__  =   self.get_config()

								return config, input_ids, input_mask




	def      lowercase__							(  self		:  List[str]					)     ->			Optional[int]:




								'''simple docstring'''





								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      lowercase__							(  self		:  List[Any]   ,		__A		:  Optional[Any]   ,		__A		:  List[str]   ,		__A		:  Union[str, Any]					)     ->			Optional[int]:




								'''simple docstring'''





								lowerCAmelCase__  =   BlipTextModel(config=__A					)
								model.to(__A					)
								model.eval()
								with torch.no_grad():
															lowerCAmelCase__  =   model(__A   ,		attention_mask=__A					)
															lowerCAmelCase__  =   model(__A					)
								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      lowercase__							(  self		:  int					)     ->			List[str]:




								'''simple docstring'''





								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_torch
class      lowerCamelCase__     (     _A,     unittest.TestCase				):






	'''simple docstring'''

	A__		=	(BlipTextModel,) if is_torch_available() else ()
	A__		=	False
	A__		=	False
	A__		=	False




	def      lowercase__							(  self		:  Dict					)     ->			List[Any]:




								'''simple docstring'''





								lowerCAmelCase__  =   BlipTextModelTester(self					)
								lowerCAmelCase__  =   ConfigTester(self   ,		config_class=__A   ,		hidden_size=37					)




	def      lowercase__							(  self		:  List[Any]					)     ->			List[Any]:




								'''simple docstring'''





								self.config_tester.run_common_tests()




	def      lowercase__							(  self		:  Optional[int]					)     ->			Union[str, Any]:




								'''simple docstring'''





								lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__A					)




	def      lowercase__							(  self		:  Optional[Any]					)     ->			Union[str, Any]:




								'''simple docstring'''





								pass




	def      lowercase__							(  self		:  List[str]					)     ->			Optional[int]:




								'''simple docstring'''





								pass




	@unittest.skip(reason="""Blip does not use inputs_embeds"""					)
	def      lowercase__							(  self		:  Tuple					)     ->			int:




								'''simple docstring'''





								pass




	@unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING"""					)
	def      lowercase__							(  self		:  List[Any]					)     ->			List[Any]:




								'''simple docstring'''





								pass




	@unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING"""					)
	def      lowercase__							(  self		:  str					)     ->			List[Any]:




								'''simple docstring'''





								pass




	@slow
	def      lowercase__							(  self		:  Dict					)     ->			Tuple:




								'''simple docstring'''





								for model_name in BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowerCAmelCase__  =   BlipTextModel.from_pretrained(__A					)
															self.assertIsNotNone(__A					)




	def      lowercase__							(  self		:  Dict					)     ->			Optional[Any]:




								'''simple docstring'''





								super().test_pt_tf_model_equivalence(allow_missing_keys=__A					)










'''simple docstring'''







from functools import partial
from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload

import torch
import torch.nn as nn
import torch.types
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	bool						) ->							torch.Tensor:
							# The first operation in a checkpoint can't be in-place, but it's
							# nice to have in-place addition during inference. Thus...
							if not inplace:
														lowerCAmelCase__  =   ma + ma
							else:
														ma += ma

							return ma
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	List[int]						) ->							torch.Tensor:
							lowerCAmelCase__  =   -1 * len(UpperCAmelCase_						)
							lowerCAmelCase__  =   list(range(len(tensor.shape[:zero_index]						)						)						)
							return tensor.permute(first_inds + [zero_index + i for i in inds]						)
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	int						) ->							torch.Tensor:
							return t.reshape(t.shape[:-no_dims] + (-1,)						)
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	int					, UpperCAmelCase_      :	float = 1E-4						) ->							torch.Tensor:
							lowerCAmelCase__  =   mask.expand(*value.shape						)
							return torch.sum(mask * value					, dim=UpperCAmelCase_						) / (eps + torch.sum(UpperCAmelCase_					, dim=UpperCAmelCase_						))
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	torch.types.Number = 2.31_25					, UpperCAmelCase_      :	torch.types.Number = 21.68_75					, UpperCAmelCase_      :	int = 64						) ->							torch.Tensor:
							lowerCAmelCase__  =   torch.linspace(UpperCAmelCase_					, UpperCAmelCase_					, no_bins - 1					, device=pts.device						)
							lowerCAmelCase__  =   torch.sqrt(torch.sum((pts.unsqueeze(-2						) - pts.unsqueeze(-3						)) ** 2					, dim=-1						)						)
							return torch.bucketize(UpperCAmelCase_					, UpperCAmelCase_						)
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[list], Any]					, UpperCAmelCase_      :	List[dict]						) ->							dict:
							lowerCAmelCase__  =   dicts[0]
							lowerCAmelCase__  =   {}
							for k, v in first.items():
														lowerCAmelCase__  =   [d[k] for d in dicts]
														if isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
																					lowerCAmelCase__  =   dict_multimap(UpperCAmelCase_					, UpperCAmelCase_						)
														else:
																					lowerCAmelCase__  =   fn(UpperCAmelCase_						)

							return new_dict
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	torch.Tensor						) ->							torch.Tensor:
							lowerCAmelCase__  =   v_bins.view(((1,) * len(x.shape						)) + (len(UpperCAmelCase_						),)						)
							lowerCAmelCase__  =   x[..., None] - reshaped_bins
							lowerCAmelCase__  =   torch.argmin(torch.abs(UpperCAmelCase_						)					, dim=-1						)
							return nn.functional.one_hot(UpperCAmelCase_					, num_classes=len(UpperCAmelCase_						)						).float()
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	int = 0					, UpperCAmelCase_      :	int = 0						) ->							torch.Tensor:
							lowerCAmelCase__  =   []
							for i, s in enumerate(data.shape[:no_batch_dims]						):
														lowerCAmelCase__  =   torch.arange(UpperCAmelCase_						)
														lowerCAmelCase__  =   r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape						) - i - 1)))						)
														ranges.append(UpperCAmelCase_						)

							lowerCAmelCase__  =   [slice(UpperCAmelCase_						) for _ in range(len(data.shape						) - no_batch_dims						)]
							lowerCAmelCase__  =   inds
							ranges.extend(UpperCAmelCase_						)
							# Matt note: Editing this to get around the behaviour of using a list as an array index changing
							# in recent Numpy versions
							return data[tuple(UpperCAmelCase_						)]


_UpperCamelCase  						=			TypeVar("""T""")
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[T], Any]					, UpperCAmelCase_      :	Dict[Any, Union[dict, list, tuple, T]]					, UpperCAmelCase_      :	Type[T]						) ->							Dict[Any, Union[dict, list, tuple, Any]]:
							lowerCAmelCase__  =   {}
							for k, v in dic.items():
														if isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
																					lowerCAmelCase__  =   dict_map(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						)
														else:
																					lowerCAmelCase__  =   tree_map(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						)

							return new_dict
@overload
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[T], Any]					, UpperCAmelCase_      :	T					, UpperCAmelCase_      :	Type[T]						) ->							Any:
							...
@overload
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[T], Any]					, UpperCAmelCase_      :	dict					, UpperCAmelCase_      :	Type[T]						) ->							dict:
							...
@overload
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[T], Any]					, UpperCAmelCase_      :	list					, UpperCAmelCase_      :	Type[T]						) ->							list:
							...
@overload
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[T], Any]					, UpperCAmelCase_      :	tuple					, UpperCAmelCase_      :	Type[T]						) ->							tuple:
							...
def 			_lowerCAmelCase(      UpperCAmelCase_      :	str					, UpperCAmelCase_      :	Dict					, UpperCAmelCase_      :	Union[str, Any]						) ->							List[str]:
							if isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
														return dict_map(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						)
							elif isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
														return [tree_map(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						) for x in tree]
							elif isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
														return tuple(tree_map(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						) for x in tree						)
							elif isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
														return fn(UpperCAmelCase_						)
							else:
														print(type(UpperCAmelCase_						)						)
														raise ValueError("""Not supported"""						)






_UpperCamelCase  						=			partial(tree_map, leaf_type=torch.Tensor)





