




"""simple docstring"""



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_  (						_UpperCAmelCase							):




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




     '''simple docstring'''

     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 							__magic_name__							(   self						:	int				)				->							int:




     '''simple docstring'''

     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 							__magic_name__							(   self						:	str				)				->							str:




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




     '''simple docstring'''

     A__	=    SqueezeBertModel(config=SCREAMING_SNAKE_CASE_				)
     model.to(SCREAMING_SNAKE_CASE_				)
     model.eval()
     A__	=    model(SCREAMING_SNAKE_CASE_     ,			SCREAMING_SNAKE_CASE_				)
     A__	=    model(SCREAMING_SNAKE_CASE_				)
     self.parent.assertEqual(result.last_hidden_state.shape     ,			(self.batch_size, self.seq_length, self.hidden_size)				)




    def 							__magic_name__							(   self						:	str     ,			snake_case_						:	Any     ,			snake_case_						:	List[Any]     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	Tuple     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Optional[Any]				)				->							Optional[Any]:




     '''simple docstring'''

     A__	=    SqueezeBertForMaskedLM(config=SCREAMING_SNAKE_CASE_				)
     model.to(SCREAMING_SNAKE_CASE_				)
     model.eval()
     A__	=    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 							__magic_name__							(   self						:	Union[str, Any]     ,			snake_case_						:	List[str]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	int     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	Dict				)				->							List[Any]:




     '''simple docstring'''

     A__	=    SqueezeBertForQuestionAnswering(config=SCREAMING_SNAKE_CASE_				)
     model.to(SCREAMING_SNAKE_CASE_				)
     model.eval()
     A__	=    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 							__magic_name__							(   self						:	Tuple     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	List[Any]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	List[Any]     ,			snake_case_						:	Dict     ,			snake_case_						:	Dict				)				->							Any:




     '''simple docstring'''

     A__	=    self.num_labels
     A__	=    SqueezeBertForSequenceClassification(SCREAMING_SNAKE_CASE_				)
     model.to(SCREAMING_SNAKE_CASE_				)
     model.eval()
     A__	=    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 							__magic_name__							(   self						:	str     ,			snake_case_						:	Any     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	str     ,			snake_case_						:	int     ,			snake_case_						:	str				)				->							List[Any]:




     '''simple docstring'''

     A__	=    self.num_labels
     A__	=    SqueezeBertForTokenClassification(config=SCREAMING_SNAKE_CASE_				)
     model.to(SCREAMING_SNAKE_CASE_				)
     model.eval()

     A__	=    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 							__magic_name__							(   self						:	Optional[int]     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	Any     ,			snake_case_						:	str     ,			snake_case_						:	str     ,			snake_case_						:	int     ,			snake_case_						:	Optional[int]				)				->							List[str]:




     '''simple docstring'''

     A__	=    self.num_choices
     A__	=    SqueezeBertForMultipleChoice(config=SCREAMING_SNAKE_CASE_				)
     model.to(SCREAMING_SNAKE_CASE_				)
     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(
         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 							__magic_name__							(   self						:	Dict				)				->							Dict:




     '''simple docstring'''

     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_  (						_UpperCAmelCase,       _UpperCAmelCase,       unittest.TestCase							):
    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 							__magic_name__							(   self						:	Dict				)				->							Dict:




     '''simple docstring'''

     A__	=    SqueezeBertModelTester(self				)
     A__	=    ConfigTester(self     ,			config_class=SCREAMING_SNAKE_CASE_     ,			dim=37				)




    def 							__magic_name__							(   self						:	Dict				)				->							Tuple:




     '''simple docstring'''

     self.config_tester.run_common_tests()




    def 							__magic_name__							(   self						:	Optional[int]				)				->							Dict:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_squeezebert_model(*SCREAMING_SNAKE_CASE_				)




    def 							__magic_name__							(   self						:	Tuple				)				->							Any:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_squeezebert_for_masked_lm(*SCREAMING_SNAKE_CASE_				)




    def 							__magic_name__							(   self						:	List[Any]				)				->							Union[str, Any]:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_squeezebert_for_question_answering(*SCREAMING_SNAKE_CASE_				)




    def 							__magic_name__							(   self						:	str				)				->							List[str]:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_squeezebert_for_sequence_classification(*SCREAMING_SNAKE_CASE_				)




    def 							__magic_name__							(   self						:	List[str]				)				->							Optional[int]:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_squeezebert_for_token_classification(*SCREAMING_SNAKE_CASE_				)




    def 							__magic_name__							(   self						:	Optional[int]				)				->							List[Any]:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_squeezebert_for_multiple_choice(*SCREAMING_SNAKE_CASE_				)




    @slow
    def 							__magic_name__							(   self						:	int				)				->							List[Any]:




     '''simple docstring'''

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






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




    @slow
    def 							__magic_name__							(   self						:	Any				)				->							int:




     '''simple docstring'''

     A__	=    SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli"				)

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











"""simple docstring"""



from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE    =	logging.get_logger(__name__)

SCREAMING_SNAKE_CASE    =	{
    "SCUT-DLVCLab/lilt-roberta-en-base": (
        "https://huggingface.co/SCUT-DLVCLab/lilt-roberta-en-base/resolve/main/config.json"
    ),
}




class      UpperCAmelCase_  (						A_							):
    lowercase__						 =				'''lilt'''




    def __init__(   self						:	List[str]     ,			snake_case_						:	Any=30_522     ,			snake_case_						:	Optional[Any]=768     ,			snake_case_						:	Union[str, Any]=12     ,			snake_case_						:	Union[str, Any]=12     ,			snake_case_						:	Any=3_072     ,			snake_case_						:	List[str]="gelu"     ,			snake_case_						:	Union[str, Any]=0.1     ,			snake_case_						:	List[Any]=0.1     ,			snake_case_						:	Any=512     ,			snake_case_						:	Optional[Any]=2     ,			snake_case_						:	List[str]=0.02     ,			snake_case_						:	Optional[Any]=1e-12     ,			snake_case_						:	List[Any]=0     ,			snake_case_						:	Any="absolute"     ,			snake_case_						:	str=None     ,			snake_case_						:	int=4     ,			snake_case_						:	int=1_024     ,			**snake_case_						:	Tuple     ,			)				->							List[Any]:




     '''simple docstring'''

     super().__init__(pad_token_id=snake_case_     ,			**snake_case_				)

     A__	=    vocab_size
     A__	=    hidden_size
     A__	=    num_hidden_layers
     A__	=    num_attention_heads
     A__	=    hidden_act
     A__	=    intermediate_size
     A__	=    hidden_dropout_prob
     A__	=    attention_probs_dropout_prob
     A__	=    max_position_embeddings
     A__	=    type_vocab_size
     A__	=    initializer_range
     A__	=    layer_norm_eps
     A__	=    position_embedding_type
     A__	=    classifier_dropout
     A__	=    channel_shrink_ratio
     A__	=    max_ad_position_embeddings





