





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




					def __init__(		self				,						lowerCAmelCase_				,						lowerCAmelCase_=13				,						lowerCAmelCase_=7				,						lowerCAmelCase_=True				,						lowerCAmelCase_=True				,						lowerCAmelCase_=False				,						lowerCAmelCase_=True				,						lowerCAmelCase_=99				,						lowerCAmelCase_=32				,						lowerCAmelCase_=5				,						lowerCAmelCase_=4				,						lowerCAmelCase_=64				,						lowerCAmelCase_="gelu"				,						lowerCAmelCase_=0.1				,						lowerCAmelCase_=0.1				,						lowerCAmelCase_=512				,						lowerCAmelCase_=16				,						lowerCAmelCase_=2				,						lowerCAmelCase_=0.02				,						lowerCAmelCase_=3				,						lowerCAmelCase_=4				,						lowerCAmelCase_=None				,						lowerCAmelCase_=2				,						lowerCAmelCase_=2				,						lowerCAmelCase_=2				,						lowerCAmelCase_=2				,						lowerCAmelCase_=4				,						lowerCAmelCase_=1				,						)		->							Dict:
										_snake_case									=				parent
										_snake_case									=				batch_size
										_snake_case									=				seq_length
										_snake_case									=				is_training
										_snake_case									=				use_input_mask
										_snake_case									=				use_token_type_ids
										_snake_case									=				use_labels
										_snake_case									=				vocab_size
										_snake_case									=				hidden_size
										_snake_case									=				num_hidden_layers
										_snake_case									=				num_attention_heads
										_snake_case									=				intermediate_size
										_snake_case									=				hidden_act
										_snake_case									=				hidden_dropout_prob
										_snake_case									=				attention_probs_dropout_prob
										_snake_case									=				max_position_embeddings
										_snake_case									=				type_vocab_size
										_snake_case									=				type_sequence_label_size
										_snake_case									=				initializer_range
										_snake_case									=				num_labels
										_snake_case									=				num_choices
										_snake_case									=				scope
										_snake_case									=				q_groups
										_snake_case									=				k_groups
										_snake_case									=				v_groups
										_snake_case									=				post_attention_groups
										_snake_case									=				intermediate_groups
										_snake_case									=				output_groups




					def 	lowerCAmelCase			(		self  )		->							int:
										_snake_case									=				ids_tensor([self.batch_size, self.seq_length]				,						self.vocab_size  )

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

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

										_snake_case									=				self.get_config()

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




					def 	lowerCAmelCase			(		self  )		->							Union[str, 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 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							List[str]:
										_snake_case									=				SqueezeBertModel(config=lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										model.eval()
										_snake_case									=				model(lowerCAmelCase_				,						lowerCAmelCase_  )
										_snake_case									=				model(lowerCAmelCase_  )
										self.parent.assertEqual(result.last_hidden_state.shape				,						(self.batch_size, self.seq_length, self.hidden_size)  )




					def 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							Optional[int]:
										_snake_case									=				SqueezeBertForMaskedLM(config=lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										model.eval()
										_snake_case									=				model(lowerCAmelCase_				,						attention_mask=lowerCAmelCase_				,						labels=lowerCAmelCase_  )
										self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length, self.vocab_size)  )




					def 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							Optional[int]:
										_snake_case									=				SqueezeBertForQuestionAnswering(config=lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										model.eval()
										_snake_case									=				model(
										    lowerCAmelCase_				,						attention_mask=lowerCAmelCase_				,						start_positions=lowerCAmelCase_				,						end_positions=lowerCAmelCase_  )
										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 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							List[Any]:
										_snake_case									=				self.num_labels
										_snake_case									=				SqueezeBertForSequenceClassification(lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										model.eval()
										_snake_case									=				model(lowerCAmelCase_				,						attention_mask=lowerCAmelCase_				,						labels=lowerCAmelCase_  )
										self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.num_labels)  )




					def 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							Any:
										_snake_case									=				self.num_labels
										_snake_case									=				SqueezeBertForTokenClassification(config=lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										model.eval()

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




					def 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							Union[str, Any]:
										_snake_case									=				self.num_choices
										_snake_case									=				SqueezeBertForMultipleChoice(config=lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										model.eval()
										_snake_case									=				input_ids.unsqueeze(1  ).expand(-1				,						self.num_choices				,						-1  ).contiguous()
										_snake_case									=				input_mask.unsqueeze(1  ).expand(-1				,						self.num_choices				,						-1  ).contiguous()
										_snake_case									=				model(
										    lowerCAmelCase_				,						attention_mask=lowerCAmelCase_				,						labels=lowerCAmelCase_				,						)
										self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.num_choices)  )






					def 	lowerCAmelCase			(		self  )		->							List[Any]:
										_snake_case									=				self.prepare_config_and_inputs()
										((_snake_case)							,     (_snake_case)							,     (_snake_case)							,     (_snake_case)							,     (_snake_case)							,     (_snake_case))									=				config_and_inputs
										_snake_case									=				{'input_ids': input_ids, 'attention_mask': input_mask}
										return config, inputs_dict





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




					def 	lowerCAmelCase			(		self  )		->							str:
										_snake_case									=				SqueezeBertModelTester(self  )
										_snake_case									=				ConfigTester(self				,						config_class=lowerCAmelCase_				,						dim=37  )




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




					def 	lowerCAmelCase			(		self  )		->							Any:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_squeezebert_model(*lowerCAmelCase_  )




					def 	lowerCAmelCase			(		self  )		->							Tuple:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_squeezebert_for_masked_lm(*lowerCAmelCase_  )




					def 	lowerCAmelCase			(		self  )		->							List[Any]:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_squeezebert_for_question_answering(*lowerCAmelCase_  )




					def 	lowerCAmelCase			(		self  )		->							Tuple:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_squeezebert_for_sequence_classification(*lowerCAmelCase_  )




					def 	lowerCAmelCase			(		self  )		->							Tuple:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_squeezebert_for_token_classification(*lowerCAmelCase_  )




					def 	lowerCAmelCase			(		self  )		->							Any:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_squeezebert_for_multiple_choice(*lowerCAmelCase_  )






					@slow
					def 	lowerCAmelCase			(		self  )		->							Tuple:
										for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_snake_case									=				SqueezeBertModel.from_pretrained(lowerCAmelCase_  )
															self.assertIsNotNone(lowerCAmelCase_  )





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




					@slow
					def 	lowerCAmelCase			(		self  )		->							int:
										_snake_case									=				SqueezeBertForSequenceClassification.from_pretrained('squeezebert/squeezebert-mnli'  )

										_snake_case									=				torch.tensor([[1, 2_9414, 232, 328, 740, 1140, 1_2695, 69, 13, 1588, 2]]  )
										_snake_case									=				model(lowerCAmelCase_  )[0]
										_snake_case									=				torch.Size((1, 3)  )
										self.assertEqual(output.shape				,						lowerCAmelCase_  )
										_snake_case									=				torch.tensor([[0.64_01, -0.03_49, -0.60_41]]  )
										self.assertTrue(torch.allclose(lowerCAmelCase_				,						lowerCAmelCase_				,						atol=1E-4  )  )










from typing import TYPE_CHECKING

from ...utils import _LazyModule


UpperCAmelCase_      							=     {"""tokenization_byt5""": ["""ByT5Tokenizer"""]}


if TYPE_CHECKING:
					from .tokenization_byta import ByTaTokenizer
else:
					import sys

					UpperCAmelCase_      							=     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



