






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      ( A__			):

				"""simple docstring"""




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

								'''simple docstring'''
								lowercase__     				=    parent
								lowercase__     				=    batch_size
								lowercase__     				=    seq_length
								lowercase__     				=    is_training
								lowercase__     				=    use_input_mask
								lowercase__     				=    use_token_type_ids
								lowercase__     				=    use_labels
								lowercase__     				=    vocab_size
								lowercase__     				=    hidden_size
								lowercase__     				=    num_hidden_layers
								lowercase__     				=    num_attention_heads
								lowercase__     				=    intermediate_size
								lowercase__     				=    hidden_act
								lowercase__     				=    hidden_dropout_prob
								lowercase__     				=    attention_probs_dropout_prob
								lowercase__     				=    max_position_embeddings
								lowercase__     				=    type_vocab_size
								lowercase__     				=    type_sequence_label_size
								lowercase__     				=    initializer_range
								lowercase__     				=    num_labels
								lowercase__     				=    num_choices
								lowercase__     				=    scope
								lowercase__     				=    q_groups
								lowercase__     				=    k_groups
								lowercase__     				=    v_groups
								lowercase__     				=    post_attention_groups
								lowercase__     				=    intermediate_groups
								lowercase__     				=    output_groups




				def        lowercase__     (  self		:					List[Any]						):

								'''simple docstring'''
								lowercase__     				=    ids_tensor([self.batch_size, self.seq_length],						self.vocab_size						)

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

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

								lowercase__     				=    self.get_config()

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




				def        lowercase__     (  self		:					int						):

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

								'''simple docstring'''
								lowercase__     				=    SqueezeBertModel(config=lowerCamelCase						)
								model.to(lowerCamelCase						)
								model.eval()
								lowercase__     				=    model(lowerCamelCase,						lowerCamelCase						)
								lowercase__     				=    model(lowerCamelCase						)
								self.parent.assertEqual(result.last_hidden_state.shape,						(self.batch_size, self.seq_length, self.hidden_size)						)




				def        lowercase__     (  self		:					int,						lowerCamelCase		:					int,						lowerCamelCase		:					Optional[Any],						lowerCamelCase		:					List[str],						lowerCamelCase		:					Tuple,						lowerCamelCase		:					str,						lowerCamelCase		:					Union[str, Any]						):

								'''simple docstring'''
								lowercase__     				=    SqueezeBertForMaskedLM(config=lowerCamelCase						)
								model.to(lowerCamelCase						)
								model.eval()
								lowercase__     				=    model(lowerCamelCase,						attention_mask=lowerCamelCase,						labels=lowerCamelCase						)
								self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.seq_length, self.vocab_size)						)




				def        lowercase__     (  self		:					List[str],						lowerCamelCase		:					Optional[Any],						lowerCamelCase		:					Tuple,						lowerCamelCase		:					Tuple,						lowerCamelCase		:					str,						lowerCamelCase		:					int,						lowerCamelCase		:					List[str]						):

								'''simple docstring'''
								lowercase__     				=    SqueezeBertForQuestionAnswering(config=lowerCamelCase						)
								model.to(lowerCamelCase						)
								model.eval()
								lowercase__     				=    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        lowercase__     (  self		:					List[Any],						lowerCamelCase		:					Union[str, Any],						lowerCamelCase		:					List[str],						lowerCamelCase		:					Optional[Any],						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Optional[Any],						lowerCamelCase		:					Dict						):

								'''simple docstring'''
								lowercase__     				=    self.num_labels
								lowercase__     				=    SqueezeBertForSequenceClassification(lowerCamelCase						)
								model.to(lowerCamelCase						)
								model.eval()
								lowercase__     				=    model(lowerCamelCase,						attention_mask=lowerCamelCase,						labels=lowerCamelCase						)
								self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.num_labels)						)




				def        lowercase__     (  self		:					List[Any],						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Tuple,						lowerCamelCase		:					Union[str, Any],						lowerCamelCase		:					Dict,						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Union[str, Any]						):

								'''simple docstring'''
								lowercase__     				=    self.num_labels
								lowercase__     				=    SqueezeBertForTokenClassification(config=lowerCamelCase						)
								model.to(lowerCamelCase						)
								model.eval()

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




				def        lowercase__     (  self		:					List[Any],						lowerCamelCase		:					Tuple,						lowerCamelCase		:					Optional[int],						lowerCamelCase		:					Dict,						lowerCamelCase		:					Tuple,						lowerCamelCase		:					Dict,						lowerCamelCase		:					str						):

								'''simple docstring'''
								lowercase__     				=    self.num_choices
								lowercase__     				=    SqueezeBertForMultipleChoice(config=lowerCamelCase						)
								model.to(lowerCamelCase						)
								model.eval()
								lowercase__     				=    input_ids.unsqueeze(1						).expand(-1,						self.num_choices,						-1						).contiguous()
								lowercase__     				=    input_mask.unsqueeze(1						).expand(-1,						self.num_choices,						-1						).contiguous()
								lowercase__     				=    model(
								    lowerCamelCase,						attention_mask=lowerCamelCase,						labels=lowerCamelCase,						)
								self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.num_choices)						)




				def        lowercase__     (  self		:					str						):

								'''simple docstring'''
								lowercase__     				=    self.prepare_config_and_inputs()
								((lowercase__)				,		(lowercase__)				,		(lowercase__)				,		(lowercase__)				,		(lowercase__)				,		(lowercase__))     				=    config_and_inputs
								lowercase__     				=    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
								return config, inputs_dict



@require_torch
class    _UpperCAmelCase      ( A__	,A__	,unittest.TestCase			):

				"""simple docstring"""




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




				def        lowercase__     (  self		:					Optional[int]						):

								'''simple docstring'''
								lowercase__     				=    SqueezeBertModelTester(self						)
								lowercase__     				=    ConfigTester(self,						config_class=lowerCamelCase,						dim=37						)




				def        lowercase__     (  self		:					List[Any]						):

								'''simple docstring'''
								self.config_tester.run_common_tests()




				def        lowercase__     (  self		:					List[Any]						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_squeezebert_model(*lowerCamelCase						)




				def        lowercase__     (  self		:					Any						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_squeezebert_for_masked_lm(*lowerCamelCase						)




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

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_squeezebert_for_question_answering(*lowerCamelCase						)




				def        lowercase__     (  self		:					Any						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_squeezebert_for_sequence_classification(*lowerCamelCase						)




				def        lowercase__     (  self		:					Any						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_squeezebert_for_token_classification(*lowerCamelCase						)




				def        lowercase__     (  self		:					Optional[Any]						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_squeezebert_for_multiple_choice(*lowerCamelCase						)




				@slow
				def        lowercase__     (  self		:					int						):

								'''simple docstring'''
								for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												lowercase__     				=    SqueezeBertModel.from_pretrained(lowerCamelCase						)
												self.assertIsNotNone(lowerCamelCase						)



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

				"""simple docstring"""




				@slow
				def        lowercase__     (  self		:					List[Any]						):

								'''simple docstring'''
								lowercase__     				=    SqueezeBertForSequenceClassification.from_pretrained('''squeezebert/squeezebert-mnli'''						)

								lowercase__     				=    torch.tensor([[1, 29_414, 232, 328, 740, 1_140, 12_695, 69, 13, 1_588, 2]]						)
								lowercase__     				=    model(lowerCamelCase						)[0]
								lowercase__     				=    torch.Size((1, 3)						)
								self.assertEqual(output.shape,						lowerCamelCase						)
								lowercase__     				=    torch.tensor([[0.6401, -0.0349, -0.6041]]						)
								self.assertTrue(torch.allclose(lowerCamelCase,						lowerCamelCase,						atol=1E-4						)						)












from transformers import DistilBertTokenizer, DistilBertTokenizerFast
from transformers.testing_utils import require_tokenizers, slow

from ..bert.test_tokenization_bert import BertTokenizationTest
@require_tokenizers
class    _UpperCAmelCase      ( A__			):

				"""simple docstring"""




				lowercase__       							= DistilBertTokenizer
				lowercase__       							= DistilBertTokenizerFast
				lowercase__       							= True




				@slow
				def        lowercase__     (  self		:					str						):

								'''simple docstring'''
								lowercase__     				=    DistilBertTokenizer.from_pretrained('''distilbert-base-uncased'''						)

								lowercase__     				=    tokenizer.encode('''sequence builders''',						add_special_tokens=lowerCamelCase						)
								lowercase__     				=    tokenizer.encode('''multi-sequence build''',						add_special_tokens=lowerCamelCase						)

								lowercase__     				=    tokenizer.build_inputs_with_special_tokens(lowerCamelCase						)
								lowercase__     				=    tokenizer.build_inputs_with_special_tokens(lowerCamelCase,						lowerCamelCase						)

								assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
								assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [
								    tokenizer.sep_token_id
								]




