


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


  '''simple docstring'''







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

        '''simple docstring'''




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






  def        _a							(							self :       List[str] ):

        '''simple docstring'''




        A_						:  Union[str, Any]          =				ids_tensor([self.batch_size, self.seq_length]	,self.vocab_size )

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

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

        A_						:  Union[str, Any]          =				self.get_config()

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






  def        _a							(							self :       Optional[Any] ):

        '''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 :       List[Any]	,_a :       List[Any]	,_a :       List[str]	,_a :       Union[str, Any]	,_a :       str	,_a :       str	,_a :       Tuple ):

        '''simple docstring'''




        A_						:  List[Any]          =				SqueezeBertModel(config=_a )
        model.to(_a )
        model.eval()
        A_						:  Tuple          =				model(_a	,_a )
        A_						:  Any          =				model(_a )
        self.parent.assertEqual(result.last_hidden_state.shape	,(self.batch_size, self.seq_length, self.hidden_size) )






  def        _a							(							self :       List[Any]	,_a :       List[Any]	,_a :       int	,_a :       Dict	,_a :       Tuple	,_a :       Dict	,_a :       Union[str, Any] ):

        '''simple docstring'''




        A_						:  Tuple          =				SqueezeBertForMaskedLM(config=_a )
        model.to(_a )
        model.eval()
        A_						:  Optional[int]          =				model(_a	,attention_mask=_a	,labels=_a )
        self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.seq_length, self.vocab_size) )






  def        _a							(							self :       Any	,_a :       int	,_a :       str	,_a :       str	,_a :       List[str]	,_a :       List[Any]	,_a :       Tuple ):

        '''simple docstring'''




        A_						:  Optional[int]          =				SqueezeBertForQuestionAnswering(config=_a )
        model.to(_a )
        model.eval()
        A_						:  Dict          =				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        _a							(							self :       Tuple	,_a :       Any	,_a :       Union[str, Any]	,_a :       int	,_a :       List[Any]	,_a :       int	,_a :       int ):

        '''simple docstring'''




        A_						:  Any          =				self.num_labels
        A_						:  Union[str, Any]          =				SqueezeBertForSequenceClassification(_a )
        model.to(_a )
        model.eval()
        A_						:  List[Any]          =				model(_a	,attention_mask=_a	,labels=_a )
        self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.num_labels) )






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

        '''simple docstring'''




        A_						:  str          =				self.num_labels
        A_						:  Dict          =				SqueezeBertForTokenClassification(config=_a )
        model.to(_a )
        model.eval()

        A_						:  Optional[int]          =				model(_a	,attention_mask=_a	,labels=_a )
        self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.seq_length, self.num_labels) )






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

        '''simple docstring'''




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






  def        _a							(							self :       Optional[Any] ):

        '''simple docstring'''




        A_						:  Tuple          =				self.prepare_config_and_inputs()
        ((A_)   ,					(A_)   ,					(A_)   ,					(A_)   ,					(A_)   ,					(A_))						:  List[str]          =				config_and_inputs
        A_						:  List[str]          =				{"""input_ids""": input_ids, """attention_mask""": input_mask}
        return config, inputs_dict







@require_torch
class 	__lowerCAmelCase		(							__SCREAMING_SNAKE_CASE  , __SCREAMING_SNAKE_CASE  , unittest.TestCase    ):


  '''simple docstring'''







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






  def        _a							(							self :       Optional[Any] ):

        '''simple docstring'''




        A_						:  int          =				SqueezeBertModelTester(self )
        A_						:  int          =				ConfigTester(self	,config_class=_a	,dim=37 )






  def        _a							(							self :       List[str] ):

        '''simple docstring'''




        self.config_tester.run_common_tests()






  def        _a							(							self :       List[Any] ):

        '''simple docstring'''




        A_						:  Any          =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_squeezebert_model(*_a )






  def        _a							(							self :       Dict ):

        '''simple docstring'''




        A_						:  Tuple          =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_squeezebert_for_masked_lm(*_a )






  def        _a							(							self :       Optional[int] ):

        '''simple docstring'''




        A_						:  int          =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_squeezebert_for_question_answering(*_a )






  def        _a							(							self :       Optional[Any] ):

        '''simple docstring'''




        A_						:  int          =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_squeezebert_for_sequence_classification(*_a )






  def        _a							(							self :       Dict ):

        '''simple docstring'''




        A_						:  Optional[Any]          =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_squeezebert_for_token_classification(*_a )






  def        _a							(							self :       Tuple ):

        '''simple docstring'''




        A_						:  List[Any]          =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_squeezebert_for_multiple_choice(*_a )






  @slow
  def        _a							(							self :       str ):

        '''simple docstring'''




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







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


  '''simple docstring'''







  @slow
  def        _a							(							self :       Tuple ):

        '''simple docstring'''




        A_						:  Optional[Any]          =				SqueezeBertForSequenceClassification.from_pretrained("""squeezebert/squeezebert-mnli""" )

        A_						:  List[str]          =				torch.tensor([[1, 29414, 232, 328, 740, 1140, 12695, 69, 13, 1588, 2]] )
        A_						:  Union[str, Any]          =				model(_a )[0]
        A_						:  int          =				torch.Size((1, 3) )
        self.assertEqual(output.shape	,_a )
        A_						:  int          =				torch.tensor([[0.6401, -0.0349, -0.6041]] )
        self.assertTrue(torch.allclose(_a	,_a	,atol=1e-4 ) )




'''simple docstring'''


from ... import PretrainedConfig


__magic_name__			=      {
    'sijunhe/nezha-cn-base': 'https://huggingface.co/sijunhe/nezha-cn-base/resolve/main/config.json',
}



class 	__lowerCAmelCase		(							__SCREAMING_SNAKE_CASE    ):


  '''simple docstring'''







  a_  					=							NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP
  a_  					=							"""nezha"""






  def __init__(							self :       int	,_a :       Union[str, Any]=21128	,_a :       int=768	,_a :       Any=12	,_a :       List[str]=12	,_a :       str=3072	,_a :       int="gelu"	,_a :       int=0.1	,_a :       str=0.1	,_a :       Tuple=512	,_a :       List[Any]=64	,_a :       Dict=2	,_a :       List[Any]=0.02	,_a :       Optional[Any]=1e-12	,_a :       List[Any]=0.1	,_a :       Union[str, Any]=0	,_a :       Any=2	,_a :       Union[str, Any]=3	,_a :       int=True	,**_a :       int	,):

        '''simple docstring'''




        super().__init__(pad_token_id=_a	,bos_token_id=_a	,eos_token_id=_a	,**_a )

        A_						:  Tuple          =				vocab_size
        A_						:  int          =				hidden_size
        A_						:  Any          =				num_hidden_layers
        A_						:  List[Any]          =				num_attention_heads
        A_						:  Tuple          =				hidden_act
        A_						:  List[Any]          =				intermediate_size
        A_						:  List[str]          =				hidden_dropout_prob
        A_						:  Tuple          =				attention_probs_dropout_prob
        A_						:  Dict          =				max_position_embeddings
        A_						:  Optional[Any]          =				max_relative_position
        A_						:  List[Any]          =				type_vocab_size
        A_						:  int          =				initializer_range
        A_						:  Tuple          =				layer_norm_eps
        A_						:  Dict          =				classifier_dropout
        A_						:  int          =				use_cache
