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 					lowercase__						(   UpperCamelCase_):


						def __init__(       self	:				List[str]    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				Optional[Any]=13    ,		UpperCamelCase__	:				int=7    ,		UpperCamelCase__	:				List[Any]=True    ,		UpperCamelCase__	:				int=True    ,		UpperCamelCase__	:				Tuple=False    ,		UpperCamelCase__	:				Optional[Any]=True    ,		UpperCamelCase__	:				List[Any]=99    ,		UpperCamelCase__	:				Optional[int]=32    ,		UpperCamelCase__	:				int=5    ,		UpperCamelCase__	:				Optional[int]=4    ,		UpperCamelCase__	:				Any=64    ,		UpperCamelCase__	:				str="gelu"    ,		UpperCamelCase__	:				Union[str, Any]=0.1    ,		UpperCamelCase__	:				Optional[int]=0.1    ,		UpperCamelCase__	:				Tuple=512    ,		UpperCamelCase__	:				str=16    ,		UpperCamelCase__	:				Any=2    ,		UpperCamelCase__	:				int=0.02    ,		UpperCamelCase__	:				Optional[int]=3    ,		UpperCamelCase__	:				Union[str, Any]=4    ,		UpperCamelCase__	:				Tuple=None    ,		UpperCamelCase__	:				Optional[int]=2    ,		UpperCamelCase__	:				Optional[Any]=2    ,		UpperCamelCase__	:				int=2    ,		UpperCamelCase__	:				Union[str, Any]=2    ,		UpperCamelCase__	:				str=4    ,		UpperCamelCase__	:				Optional[Any]=1    ,		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							parent
												SCREAMING_SNAKE_CASE							:    str							=							batch_size
												SCREAMING_SNAKE_CASE							:    Tuple							=							seq_length
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							is_training
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							use_input_mask
												SCREAMING_SNAKE_CASE							:    List[str]							=							use_token_type_ids
												SCREAMING_SNAKE_CASE							:    Dict							=							use_labels
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							vocab_size
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							hidden_size
												SCREAMING_SNAKE_CASE							:    List[Any]							=							num_hidden_layers
												SCREAMING_SNAKE_CASE							:    List[Any]							=							num_attention_heads
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							intermediate_size
												SCREAMING_SNAKE_CASE							:    Any							=							hidden_act
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							hidden_dropout_prob
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							attention_probs_dropout_prob
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							max_position_embeddings
												SCREAMING_SNAKE_CASE							:    List[str]							=							type_vocab_size
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							type_sequence_label_size
												SCREAMING_SNAKE_CASE							:    List[Any]							=							initializer_range
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							num_labels
												SCREAMING_SNAKE_CASE							:    Tuple							=							num_choices
												SCREAMING_SNAKE_CASE							:    Tuple							=							scope
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							q_groups
												SCREAMING_SNAKE_CASE							:    List[Any]							=							k_groups
												SCREAMING_SNAKE_CASE							:    Dict							=							v_groups
												SCREAMING_SNAKE_CASE							:    int							=							post_attention_groups
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							intermediate_groups
												SCREAMING_SNAKE_CASE							:    List[Any]							=							output_groups


						def        __A       (       self	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[str]							=							ids_tensor([self.batch_size, self.seq_length]    ,		self.vocab_size						)

												SCREAMING_SNAKE_CASE							:    Optional[int]							=							None
												if self.use_input_mask:
																		SCREAMING_SNAKE_CASE							:    Optional[int]							=							random_attention_mask([self.batch_size, self.seq_length]						)

												SCREAMING_SNAKE_CASE							:    str							=							None
												SCREAMING_SNAKE_CASE							:    int							=							None
												SCREAMING_SNAKE_CASE							:    Tuple							=							None
												if self.use_labels:
																		SCREAMING_SNAKE_CASE							:    Any							=							ids_tensor([self.batch_size]    ,		self.type_sequence_label_size						)
																		SCREAMING_SNAKE_CASE							:    Dict							=							ids_tensor([self.batch_size, self.seq_length]    ,		self.num_labels						)
																		SCREAMING_SNAKE_CASE							:    Tuple							=							ids_tensor([self.batch_size]    ,		self.num_choices						)

												SCREAMING_SNAKE_CASE							:    List[Any]							=							self.get_config()

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


						def        __A       (       self	:				Tuple						):





												'''simple docstring'''





												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	:				Dict    ,		UpperCamelCase__	:				Any    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				str    ,		UpperCamelCase__	:				Optional[int]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							SqueezeBertModel(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    Tuple							=							model(UpperCamelCase__    ,		UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							model(UpperCamelCase__						)
												self.parent.assertEqual(result.last_hidden_state.shape    ,		(self.batch_size, self.seq_length, self.hidden_size)						)


						def        __A       (       self	:				Any    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				Optional[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Dict							=							SqueezeBertForMaskedLM(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							model(UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		labels=UpperCamelCase__						)
												self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.vocab_size)						)


						def        __A       (       self	:				Dict    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				str    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Any							=							SqueezeBertForQuestionAnswering(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    str							=							model(
												    UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		start_positions=UpperCamelCase__    ,		end_positions=UpperCamelCase__						)
												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	:				List[Any]    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				Dict    ,		UpperCamelCase__	:				Any    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				Dict    ,		UpperCamelCase__	:				Optional[int]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							self.num_labels
												SCREAMING_SNAKE_CASE							:    Dict							=							SqueezeBertForSequenceClassification(UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							model(UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		labels=UpperCamelCase__						)
												self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.num_labels)						)


						def        __A       (       self	:				Optional[int]    ,		UpperCamelCase__	:				str    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[str]							=							self.num_labels
												SCREAMING_SNAKE_CASE							:    Dict							=							SqueezeBertForTokenClassification(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()

												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							model(UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		labels=UpperCamelCase__						)
												self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.num_labels)						)


						def        __A       (       self	:				List[str]    ,		UpperCamelCase__	:				Dict    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				List[str]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							self.num_choices
												SCREAMING_SNAKE_CASE							:    List[Any]							=							SqueezeBertForMultipleChoice(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    List[Any]							=							input_ids.unsqueeze(1						).expand(-1    ,		self.num_choices    ,		-1						).contiguous()
												SCREAMING_SNAKE_CASE							:    List[str]							=							input_mask.unsqueeze(1						).expand(-1    ,		self.num_choices    ,		-1						).contiguous()
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							model(
												    UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		labels=UpperCamelCase__    ,		)
												self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.num_choices)						)



						def        __A       (       self	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[Any]							=							self.prepare_config_and_inputs()
												((SCREAMING_SNAKE_CASE)				,  (SCREAMING_SNAKE_CASE)				,  (SCREAMING_SNAKE_CASE)				,  (SCREAMING_SNAKE_CASE)				,  (SCREAMING_SNAKE_CASE)				,  (SCREAMING_SNAKE_CASE))							:    Dict							=							config_and_inputs
												SCREAMING_SNAKE_CASE							:    Tuple							=							{'''input_ids''': input_ids, '''attention_mask''': input_mask}
												return config, inputs_dict






@require_torch
class 					lowercase__						(   UpperCamelCase_	,							UpperCamelCase_	,							unittest.TestCase):
						UpperCamelCase_     =		(
						    (
						        SqueezeBertModel,
						        SqueezeBertForMaskedLM,
						        SqueezeBertForMultipleChoice,
						        SqueezeBertForQuestionAnswering,
						        SqueezeBertForSequenceClassification,
						        SqueezeBertForTokenClassification,
						    )
						    if is_torch_available()
						    else None
						)
						UpperCamelCase_     =		(
						    {
						        """feature-extraction""": SqueezeBertModel,
						        """fill-mask""": SqueezeBertForMaskedLM,
						        """question-answering""": SqueezeBertForQuestionAnswering,
						        """text-classification""": SqueezeBertForSequenceClassification,
						        """token-classification""": SqueezeBertForTokenClassification,
						        """zero-shot""": SqueezeBertForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						UpperCamelCase_     =		False
						UpperCamelCase_     =		True
						UpperCamelCase_     =		False


						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							SqueezeBertModelTester(self						)
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							ConfigTester(self    ,		config_class=UpperCamelCase__    ,		dim=37						)


						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												self.config_tester.run_common_tests()


						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_squeezebert_model(*UpperCamelCase__						)


						def        __A       (       self	:				str						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[str]							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_squeezebert_for_masked_lm(*UpperCamelCase__						)


						def        __A       (       self	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Any							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_squeezebert_for_question_answering(*UpperCamelCase__						)


						def        __A       (       self	:				str						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Tuple							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_squeezebert_for_sequence_classification(*UpperCamelCase__						)


						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    int							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_squeezebert_for_token_classification(*UpperCamelCase__						)


						def        __A       (       self	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[Any]							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_squeezebert_for_multiple_choice(*UpperCamelCase__						)



						@slow
						def        __A       (       self	:				Union[str, Any]						):





												'''simple docstring'''





												for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							SqueezeBertModel.from_pretrained(UpperCamelCase__						)
																		self.assertIsNotNone(UpperCamelCase__						)






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


						@slow
						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    int							=							SqueezeBertForSequenceClassification.from_pretrained('''squeezebert/squeezebert-mnli'''						)

												SCREAMING_SNAKE_CASE							:    int							=							torch.tensor([[1, 2_9414, 232, 328, 740, 1140, 1_2695, 69, 13, 1588, 2]]						)
												SCREAMING_SNAKE_CASE							:    int							=							model(UpperCamelCase__						)[0]
												SCREAMING_SNAKE_CASE							:    Dict							=							torch.Size((1, 3)						)
												self.assertEqual(output.shape    ,		UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    List[str]							=							torch.tensor([[0.6401, -0.0349, -0.6041]]						)
												self.assertTrue(torch.allclose(UpperCamelCase__    ,		UpperCamelCase__    ,		atol=1E-4						)						)






from packaging import version

from .import_utils import is_accelerate_available


if is_accelerate_available():
					import accelerate
def       A	(    _lowercase						):
						if not is_accelerate_available():
												return method
						SCREAMING_SNAKE_CASE							:    int							=							version.parse(accelerate.__version__						).base_version
						if version.parse(_lowercase						) < version.parse('''0.17.0'''						):
												return method

						def wrapper(self		,  *_lowercase		,  **_lowercase						):
												if hasattr(self		,  '''_hf_hook'''						) and hasattr(self._hf_hook		,  '''pre_forward'''						):
																		self._hf_hook.pre_forward(self						)
												return method(self		,  *_lowercase		,  **_lowercase						)

						return wrapper





