
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__		(    __SCREAMING_SNAKE_CASE   ):







	'''simple docstring'''





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





	def   __snake_case				(			self		):
					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]		)

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

					A__         =							self.get_config()

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





	def   __snake_case				(			self		):
					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   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__         =							SqueezeBertModel(config=_a		)
					model.to(_a		)
					model.eval()
					A__         =							model(_a				,       _a		)
					A__         =							model(_a		)
					self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, self.seq_length, self.hidden_size)		)





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__         =							SqueezeBertForMaskedLM(config=_a		)
					model.to(_a		)
					model.eval()
					A__         =							model(_a				,       attention_mask=_a				,       labels=_a		)
					self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length, self.vocab_size)		)





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__         =							SqueezeBertForQuestionAnswering(config=_a		)
					model.to(_a		)
					model.eval()
					A__         =							model(
					    _a				,       attention_mask=_a				,       start_positions=_a				,       end_positions=_a		)
					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   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__         =							self.num_labels
					A__         =							SqueezeBertForSequenceClassification(_a		)
					model.to(_a		)
					model.eval()
					A__         =							model(_a				,       attention_mask=_a				,       labels=_a		)
					self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_labels)		)





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__         =							self.num_labels
					A__         =							SqueezeBertForTokenClassification(config=_a		)
					model.to(_a		)
					model.eval()

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





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__				,       UpperCamelCase__		):
					A__         =							self.num_choices
					A__         =							SqueezeBertForMultipleChoice(config=_a		)
					model.to(_a		)
					model.eval()
					A__         =							input_ids.unsqueeze(1		).expand(-1				,       self.num_choices				,       -1		).contiguous()
					A__         =							input_mask.unsqueeze(1		).expand(-1				,       self.num_choices				,       -1		).contiguous()
					A__         =							model(
					    _a				,       attention_mask=_a				,       labels=_a				,       )
					self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_choices)		)





	def   __snake_case				(			self		):
					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  UpperCamelCase__		(    __SCREAMING_SNAKE_CASE,			__SCREAMING_SNAKE_CASE,			unittest.TestCase   ):







	'''simple docstring'''
	_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   __snake_case				(			self		):
					A__         =							SqueezeBertModelTester(self		)
					A__         =							ConfigTester(self				,       config_class=_a				,       dim=37		)





	def   __snake_case				(			self		):
					self.config_tester.run_common_tests()





	def   __snake_case				(			self		):
					A__         =							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_squeezebert_model(*_a		)





	def   __snake_case				(			self		):
					A__         =							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_squeezebert_for_masked_lm(*_a		)





	def   __snake_case				(			self		):
					A__         =							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_squeezebert_for_question_answering(*_a		)





	def   __snake_case				(			self		):
					A__         =							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_squeezebert_for_sequence_classification(*_a		)





	def   __snake_case				(			self		):
					A__         =							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_squeezebert_for_token_classification(*_a		)





	def   __snake_case				(			self		):
					A__         =							self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_squeezebert_for_multiple_choice(*_a		)





	@slow
	def   __snake_case				(			self		):




					for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									A__         =							SqueezeBertModel.from_pretrained(_a		)
									self.assertIsNotNone(_a		)







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







	'''simple docstring'''





	@slow
	def   __snake_case				(			self		):
					A__         =							SqueezeBertForSequenceClassification.from_pretrained('''squeezebert/squeezebert-mnli'''		)

					A__         =							torch.tensor([[1, 2_9414, 232, 328, 740, 1140, 1_2695, 69, 13, 1588, 2]]		)
					A__         =							model(_a		)[0]
					A__         =							torch.Size((1, 3)		)
					self.assertEqual(output.shape				,       _a		)
					A__         =							torch.tensor([[0.6_4_0_1, -0.0_3_4_9, -0.6_0_4_1]]		)
					self.assertTrue(torch.allclose(_a				,       _a				,       atol=1e-4		)		)

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tensorflow_text_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_SCREAMING_SNAKE_CASE    :      int          =			{
    'configuration_bert': ['BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BertConfig', 'BertOnnxConfig'],
    'tokenization_bert': ['BasicTokenizer', 'BertTokenizer', 'WordpieceTokenizer'],
}

try:
					if not is_tokenizers_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_SCREAMING_SNAKE_CASE    :      Optional[Any]          =			['BertTokenizerFast']

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_SCREAMING_SNAKE_CASE    :      Union[str, Any]          =			[
					    'BERT_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'BertForMaskedLM',
					    'BertForMultipleChoice',
					    'BertForNextSentencePrediction',
					    'BertForPreTraining',
					    'BertForQuestionAnswering',
					    'BertForSequenceClassification',
					    'BertForTokenClassification',
					    'BertLayer',
					    'BertLMHeadModel',
					    'BertModel',
					    'BertPreTrainedModel',
					    'load_tf_weights_in_bert',
					]

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_SCREAMING_SNAKE_CASE    :      Tuple          =			[
					    'TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'TFBertEmbeddings',
					    'TFBertForMaskedLM',
					    'TFBertForMultipleChoice',
					    'TFBertForNextSentencePrediction',
					    'TFBertForPreTraining',
					    'TFBertForQuestionAnswering',
					    'TFBertForSequenceClassification',
					    'TFBertForTokenClassification',
					    'TFBertLMHeadModel',
					    'TFBertMainLayer',
					    'TFBertModel',
					    'TFBertPreTrainedModel',
					]
try:
					if not is_tensorflow_text_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_SCREAMING_SNAKE_CASE    :      Dict          =			['TFBertTokenizer']

try:
					if not is_flax_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_SCREAMING_SNAKE_CASE    :      Optional[int]          =			[
					    'FlaxBertForCausalLM',
					    'FlaxBertForMaskedLM',
					    'FlaxBertForMultipleChoice',
					    'FlaxBertForNextSentencePrediction',
					    'FlaxBertForPreTraining',
					    'FlaxBertForQuestionAnswering',
					    'FlaxBertForSequenceClassification',
					    'FlaxBertForTokenClassification',
					    'FlaxBertModel',
					    'FlaxBertPreTrainedModel',
					]

if TYPE_CHECKING:
					from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig, BertOnnxConfig
					from .tokenization_bert import BasicTokenizer, BertTokenizer, WordpieceTokenizer

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_bert_fast import BertTokenizerFast

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_bert import (
										    BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
										    BertForMaskedLM,
										    BertForMultipleChoice,
										    BertForNextSentencePrediction,
										    BertForPreTraining,
										    BertForQuestionAnswering,
										    BertForSequenceClassification,
										    BertForTokenClassification,
										    BertLayer,
										    BertLMHeadModel,
										    BertModel,
										    BertPreTrainedModel,
										    load_tf_weights_in_bert,
										)

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_bert import (
										    TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFBertEmbeddings,
										    TFBertForMaskedLM,
										    TFBertForMultipleChoice,
										    TFBertForNextSentencePrediction,
										    TFBertForPreTraining,
										    TFBertForQuestionAnswering,
										    TFBertForSequenceClassification,
										    TFBertForTokenClassification,
										    TFBertLMHeadModel,
										    TFBertMainLayer,
										    TFBertModel,
										    TFBertPreTrainedModel,
										)

					try:
										if not is_tensorflow_text_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_bert_tf import TFBertTokenizer

					try:
										if not is_flax_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_flax_bert import (
										    FlaxBertForCausalLM,
										    FlaxBertForMaskedLM,
										    FlaxBertForMultipleChoice,
										    FlaxBertForNextSentencePrediction,
										    FlaxBertForPreTraining,
										    FlaxBertForQuestionAnswering,
										    FlaxBertForSequenceClassification,
										    FlaxBertForTokenClassification,
										    FlaxBertModel,
										    FlaxBertPreTrainedModel,
										)

else:
					import sys

					_SCREAMING_SNAKE_CASE    :      Union[str, Any]          =			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)