
import unittest

from transformers import SqueezeBertConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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


if is_torch_available():
					import torch

					from transformers import (
					    SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
					    SqueezeBertForMaskedLM,
					    SqueezeBertForMultipleChoice,
					    SqueezeBertForQuestionAnswering,
					    SqueezeBertForSequenceClassification,
					    SqueezeBertForTokenClassification,
					    SqueezeBertModel,
					)

class    lowerCAmelCase__     (	__lowercase			):






		def __init__(		self		:							Tuple							,			SCREAMING_SNAKE_CASE__		:							Optional[int]							,			SCREAMING_SNAKE_CASE__		:							Tuple=13							,			SCREAMING_SNAKE_CASE__		:							str=7							,			SCREAMING_SNAKE_CASE__		:							Optional[int]=True							,			SCREAMING_SNAKE_CASE__		:							int=True							,			SCREAMING_SNAKE_CASE__		:							Any=False							,			SCREAMING_SNAKE_CASE__		:							Dict=True							,			SCREAMING_SNAKE_CASE__		:							int=99							,			SCREAMING_SNAKE_CASE__		:							List[Any]=32							,			SCREAMING_SNAKE_CASE__		:							str=5							,			SCREAMING_SNAKE_CASE__		:							List[str]=4							,			SCREAMING_SNAKE_CASE__		:							Union[str, Any]=64							,			SCREAMING_SNAKE_CASE__		:							int="gelu"							,			SCREAMING_SNAKE_CASE__		:							Any=0.1							,			SCREAMING_SNAKE_CASE__		:							Tuple=0.1							,			SCREAMING_SNAKE_CASE__		:							int=5_12							,			SCREAMING_SNAKE_CASE__		:							int=16							,			SCREAMING_SNAKE_CASE__		:							Optional[int]=2							,			SCREAMING_SNAKE_CASE__		:							Dict=0.02							,			SCREAMING_SNAKE_CASE__		:							Tuple=3							,			SCREAMING_SNAKE_CASE__		:							Union[str, Any]=4							,			SCREAMING_SNAKE_CASE__		:							Optional[int]=None							,			SCREAMING_SNAKE_CASE__		:							Union[str, Any]=2							,			SCREAMING_SNAKE_CASE__		:							int=2							,			SCREAMING_SNAKE_CASE__		:							Optional[int]=2							,			SCREAMING_SNAKE_CASE__		:							Tuple=2							,			SCREAMING_SNAKE_CASE__		:							Union[str, Any]=4							,			SCREAMING_SNAKE_CASE__		:							str=1							,			)     ->							Tuple:
						__lowerCamelCase    					=	parent
						__lowerCamelCase    					=	batch_size
						__lowerCamelCase    					=	seq_length
						__lowerCamelCase    					=	is_training
						__lowerCamelCase    					=	use_input_mask
						__lowerCamelCase    					=	use_token_type_ids
						__lowerCamelCase    					=	use_labels
						__lowerCamelCase    					=	vocab_size
						__lowerCamelCase    					=	hidden_size
						__lowerCamelCase    					=	num_hidden_layers
						__lowerCamelCase    					=	num_attention_heads
						__lowerCamelCase    					=	intermediate_size
						__lowerCamelCase    					=	hidden_act
						__lowerCamelCase    					=	hidden_dropout_prob
						__lowerCamelCase    					=	attention_probs_dropout_prob
						__lowerCamelCase    					=	max_position_embeddings
						__lowerCamelCase    					=	type_vocab_size
						__lowerCamelCase    					=	type_sequence_label_size
						__lowerCamelCase    					=	initializer_range
						__lowerCamelCase    					=	num_labels
						__lowerCamelCase    					=	num_choices
						__lowerCamelCase    					=	scope
						__lowerCamelCase    					=	q_groups
						__lowerCamelCase    					=	k_groups
						__lowerCamelCase    					=	v_groups
						__lowerCamelCase    					=	post_attention_groups
						__lowerCamelCase    					=	intermediate_groups
						__lowerCamelCase    					=	output_groups






		def  __A       (		self		:							str  )     ->							Tuple:
						__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]  )

						__lowerCamelCase    					=	None
						__lowerCamelCase    					=	None
						__lowerCamelCase    					=	None
						if self.use_labels:
										__lowerCamelCase    					=	ids_tensor([self.batch_size]							,			self.type_sequence_label_size  )
										__lowerCamelCase    					=	ids_tensor([self.batch_size, self.seq_length]							,			self.num_labels  )
										__lowerCamelCase    					=	ids_tensor([self.batch_size]							,			self.num_choices  )

						__lowerCamelCase    					=	self.get_config()

						return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels






		def  __A       (		self		:							Dict  )     ->							Optional[Any]:
						return SqueezeBertConfig(
						    embedding_size=self.hidden_size							,			vocab_size=self.vocab_size							,			hidden_size=self.hidden_size							,			num_hidden_layers=self.num_hidden_layers							,			num_attention_heads=self.num_attention_heads							,			intermediate_size=self.intermediate_size							,			hidden_act=self.hidden_act							,			attention_probs_dropout_prob=self.hidden_dropout_prob							,			attention_dropout=self.attention_probs_dropout_prob							,			max_position_embeddings=self.max_position_embeddings							,			initializer_range=self.initializer_range							,			q_groups=self.q_groups							,			k_groups=self.k_groups							,			v_groups=self.v_groups							,			post_attention_groups=self.post_attention_groups							,			intermediate_groups=self.intermediate_groups							,			output_groups=self.output_groups							,			)






		def  __A       (		self		:							Any							,			SCREAMING_SNAKE_CASE__		:							Any							,			SCREAMING_SNAKE_CASE__		:							Tuple							,			SCREAMING_SNAKE_CASE__		:							Tuple							,			SCREAMING_SNAKE_CASE__		:							Union[str, Any]							,			SCREAMING_SNAKE_CASE__		:							Any							,			SCREAMING_SNAKE_CASE__		:							List[Any]  )     ->							Tuple:
						__lowerCamelCase    					=	SqueezeBertModel(config=SCREAMING_SNAKE_CASE__  )
						model.to(SCREAMING_SNAKE_CASE__  )
						model.eval()
						__lowerCamelCase    					=	model(SCREAMING_SNAKE_CASE__							,			SCREAMING_SNAKE_CASE__  )
						__lowerCamelCase    					=	model(SCREAMING_SNAKE_CASE__  )
						self.parent.assertEqual(result.last_hidden_state.shape							,			(self.batch_size, self.seq_length, self.hidden_size)  )






		def  __A       (		self		:							List[str]							,			SCREAMING_SNAKE_CASE__		:							List[str]							,			SCREAMING_SNAKE_CASE__		:							List[Any]							,			SCREAMING_SNAKE_CASE__		:							List[str]							,			SCREAMING_SNAKE_CASE__		:							Optional[int]							,			SCREAMING_SNAKE_CASE__		:							int							,			SCREAMING_SNAKE_CASE__		:							Tuple  )     ->							Tuple:
						__lowerCamelCase    					=	SqueezeBertForMaskedLM(config=SCREAMING_SNAKE_CASE__  )
						model.to(SCREAMING_SNAKE_CASE__  )
						model.eval()
						__lowerCamelCase    					=	model(SCREAMING_SNAKE_CASE__							,			attention_mask=SCREAMING_SNAKE_CASE__							,			labels=SCREAMING_SNAKE_CASE__  )
						self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.seq_length, self.vocab_size)  )






		def  __A       (		self		:							Tuple							,			SCREAMING_SNAKE_CASE__		:							int							,			SCREAMING_SNAKE_CASE__		:							List[str]							,			SCREAMING_SNAKE_CASE__		:							Dict							,			SCREAMING_SNAKE_CASE__		:							Tuple							,			SCREAMING_SNAKE_CASE__		:							Optional[int]							,			SCREAMING_SNAKE_CASE__		:							int  )     ->							Tuple:
						__lowerCamelCase    					=	SqueezeBertForQuestionAnswering(config=SCREAMING_SNAKE_CASE__  )
						model.to(SCREAMING_SNAKE_CASE__  )
						model.eval()
						__lowerCamelCase    					=	model(
						    SCREAMING_SNAKE_CASE__							,			attention_mask=SCREAMING_SNAKE_CASE__							,			start_positions=SCREAMING_SNAKE_CASE__							,			end_positions=SCREAMING_SNAKE_CASE__  )
						self.parent.assertEqual(result.start_logits.shape							,			(self.batch_size, self.seq_length)  )
						self.parent.assertEqual(result.end_logits.shape							,			(self.batch_size, self.seq_length)  )






		def  __A       (		self		:							Tuple							,			SCREAMING_SNAKE_CASE__		:							Optional[int]							,			SCREAMING_SNAKE_CASE__		:							Union[str, Any]							,			SCREAMING_SNAKE_CASE__		:							Any							,			SCREAMING_SNAKE_CASE__		:							Optional[int]							,			SCREAMING_SNAKE_CASE__		:							Optional[Any]							,			SCREAMING_SNAKE_CASE__		:							str  )     ->							Optional[Any]:
						__lowerCamelCase    					=	self.num_labels
						__lowerCamelCase    					=	SqueezeBertForSequenceClassification(SCREAMING_SNAKE_CASE__  )
						model.to(SCREAMING_SNAKE_CASE__  )
						model.eval()
						__lowerCamelCase    					=	model(SCREAMING_SNAKE_CASE__							,			attention_mask=SCREAMING_SNAKE_CASE__							,			labels=SCREAMING_SNAKE_CASE__  )
						self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.num_labels)  )






		def  __A       (		self		:							Tuple							,			SCREAMING_SNAKE_CASE__		:							Optional[Any]							,			SCREAMING_SNAKE_CASE__		:							Optional[int]							,			SCREAMING_SNAKE_CASE__		:							List[str]							,			SCREAMING_SNAKE_CASE__		:							Optional[Any]							,			SCREAMING_SNAKE_CASE__		:							List[Any]							,			SCREAMING_SNAKE_CASE__		:							int  )     ->							Any:
						__lowerCamelCase    					=	self.num_labels
						__lowerCamelCase    					=	SqueezeBertForTokenClassification(config=SCREAMING_SNAKE_CASE__  )
						model.to(SCREAMING_SNAKE_CASE__  )
						model.eval()

						__lowerCamelCase    					=	model(SCREAMING_SNAKE_CASE__							,			attention_mask=SCREAMING_SNAKE_CASE__							,			labels=SCREAMING_SNAKE_CASE__  )
						self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.seq_length, self.num_labels)  )






		def  __A       (		self		:							Any							,			SCREAMING_SNAKE_CASE__		:							Optional[int]							,			SCREAMING_SNAKE_CASE__		:							Union[str, Any]							,			SCREAMING_SNAKE_CASE__		:							Dict							,			SCREAMING_SNAKE_CASE__		:							Optional[int]							,			SCREAMING_SNAKE_CASE__		:							Optional[Any]							,			SCREAMING_SNAKE_CASE__		:							Dict  )     ->							Union[str, Any]:
						__lowerCamelCase    					=	self.num_choices
						__lowerCamelCase    					=	SqueezeBertForMultipleChoice(config=SCREAMING_SNAKE_CASE__  )
						model.to(SCREAMING_SNAKE_CASE__  )
						model.eval()
						__lowerCamelCase    					=	input_ids.unsqueeze(1  ).expand(-1							,			self.num_choices							,			-1  ).contiguous()
						__lowerCamelCase    					=	input_mask.unsqueeze(1  ).expand(-1							,			self.num_choices							,			-1  ).contiguous()
						__lowerCamelCase    					=	model(
						    SCREAMING_SNAKE_CASE__							,			attention_mask=SCREAMING_SNAKE_CASE__							,			labels=SCREAMING_SNAKE_CASE__							,			)
						self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.num_choices)  )







		def  __A       (		self		:							Optional[Any]  )     ->							Tuple:
						__lowerCamelCase    					=	self.prepare_config_and_inputs()
						((__lowerCamelCase)					,    (__lowerCamelCase)					,    (__lowerCamelCase)					,    (__lowerCamelCase)					,    (__lowerCamelCase)					,    (__lowerCamelCase))    					=	config_and_inputs
						__lowerCamelCase    					=	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
						return config, inputs_dict





@require_torch
class    lowerCAmelCase__     (	__lowercase  ,	__lowercase  ,	unittest.TestCase			):
		a__       :						List[Any]        =		(
		    (
		        SqueezeBertModel,
		        SqueezeBertForMaskedLM,
		        SqueezeBertForMultipleChoice,
		        SqueezeBertForQuestionAnswering,
		        SqueezeBertForSequenceClassification,
		        SqueezeBertForTokenClassification,
		    )
		    if is_torch_available()
		    else None
		)
		a__       :						Any        =		(
		    {
		        """feature-extraction""": SqueezeBertModel,
		        """fill-mask""": SqueezeBertForMaskedLM,
		        """question-answering""": SqueezeBertForQuestionAnswering,
		        """text-classification""": SqueezeBertForSequenceClassification,
		        """token-classification""": SqueezeBertForTokenClassification,
		        """zero-shot""": SqueezeBertForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		a__       :						int        =		False
		a__       :						Dict        =		True
		a__       :						str        =		False






		def  __A       (		self		:							Any  )     ->							Tuple:
						__lowerCamelCase    					=	SqueezeBertModelTester(self  )
						__lowerCamelCase    					=	ConfigTester(self							,			config_class=SCREAMING_SNAKE_CASE__							,			dim=37  )






		def  __A       (		self		:							Tuple  )     ->							Tuple:
						self.config_tester.run_common_tests()






		def  __A       (		self		:							Optional[int]  )     ->							List[str]:
						__lowerCamelCase    					=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_squeezebert_model(*SCREAMING_SNAKE_CASE__  )






		def  __A       (		self		:							Dict  )     ->							Tuple:
						__lowerCamelCase    					=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_squeezebert_for_masked_lm(*SCREAMING_SNAKE_CASE__  )






		def  __A       (		self		:							Union[str, Any]  )     ->							List[str]:
						__lowerCamelCase    					=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_squeezebert_for_question_answering(*SCREAMING_SNAKE_CASE__  )






		def  __A       (		self		:							Optional[Any]  )     ->							str:
						__lowerCamelCase    					=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_squeezebert_for_sequence_classification(*SCREAMING_SNAKE_CASE__  )






		def  __A       (		self		:							Tuple  )     ->							List[str]:
						__lowerCamelCase    					=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_squeezebert_for_token_classification(*SCREAMING_SNAKE_CASE__  )






		def  __A       (		self		:							Any  )     ->							Any:
						__lowerCamelCase    					=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_squeezebert_for_multiple_choice(*SCREAMING_SNAKE_CASE__  )







		@slow
		def  __A       (		self		:							Dict  )     ->							Union[str, Any]:
						for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										__lowerCamelCase    					=	SqueezeBertModel.from_pretrained(SCREAMING_SNAKE_CASE__  )
										self.assertIsNotNone(SCREAMING_SNAKE_CASE__  )





@require_sentencepiece
@require_tokenizers
@require_torch
class    lowerCAmelCase__     (	unittest.TestCase			):






		@slow
		def  __A       (		self		:							str  )     ->							Optional[Any]:
						__lowerCamelCase    					=	SqueezeBertForSequenceClassification.from_pretrained('''squeezebert/squeezebert-mnli'''  )

						__lowerCamelCase    					=	torch.tensor([[1, 2_94_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 13, 15_88, 2]]  )
						__lowerCamelCase    					=	model(SCREAMING_SNAKE_CASE__  )[0]
						__lowerCamelCase    					=	torch.Size((1, 3)  )
						self.assertEqual(output.shape							,			SCREAMING_SNAKE_CASE__  )
						__lowerCamelCase    					=	torch.tensor([[0.6401, -0.0349, -0.6041]]  )
						self.assertTrue(torch.allclose(SCREAMING_SNAKE_CASE__							,			SCREAMING_SNAKE_CASE__							,			atol=1e-4  )  )





from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
					if not (is_transformers_available() and is_torch_available() and is_transformers_version(">=", "4.25.0")):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline
else:
					from .pipeline_unclip import UnCLIPPipeline
					from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline
					from .text_proj import UnCLIPTextProjModel



