






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




      """simple docstring"""




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


       '''simple docstring'''

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






      def 				A__      ( self			:		Any		)  ->  Tuple:


       '''simple docstring'''

       lowercase					:   Dict				  =ids_tensor([self.batch_size, self.seq_length]     ,      self.vocab_size		)

       lowercase					:   Tuple				  =None
       if self.use_input_mask:
        lowercase					:   List[str]				  =random_attention_mask([self.batch_size, self.seq_length]		)

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

       lowercase					:   str				  =self.get_config()

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






      def 				A__      ( self			:		str		)  ->  Tuple:


       '''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			:		Optional[int]     ,      UpperCAmelCase			:		List[str]     ,      UpperCAmelCase			:		List[str]     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		Optional[int]     ,      UpperCAmelCase			:		str     ,      UpperCAmelCase			:		List[str]		)  ->  Optional[Any]:


       '''simple docstring'''

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






      def 				A__      ( self			:		str     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		Tuple     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		Union[str, Any]     ,      UpperCAmelCase			:		Tuple     ,      UpperCAmelCase			:		Any		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   List[str]				  =SqueezeBertForMaskedLM(config=UpperCAmelCase		)
       model.to(UpperCAmelCase		)
       model.eval()
       lowercase					:   Any				  =model(UpperCAmelCase     ,      attention_mask=UpperCAmelCase     ,      labels=UpperCAmelCase		)
       self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.seq_length, self.vocab_size)		)






      def 				A__      ( self			:		Optional[int]     ,      UpperCAmelCase			:		Union[str, Any]     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		str     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		str     ,      UpperCAmelCase			:		int		)  ->  Any:


       '''simple docstring'''

       lowercase					:   Any				  =SqueezeBertForQuestionAnswering(config=UpperCAmelCase		)
       model.to(UpperCAmelCase		)
       model.eval()
       lowercase					:   List[str]				  =model(
           UpperCAmelCase     ,      attention_mask=UpperCAmelCase     ,      start_positions=UpperCAmelCase     ,      end_positions=UpperCAmelCase		)
       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     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		Optional[int]     ,      UpperCAmelCase			:		Union[str, Any]     ,      UpperCAmelCase			:		str     ,      UpperCAmelCase			:		str     ,      UpperCAmelCase			:		Optional[Any]		)  ->  Union[str, Any]:


       '''simple docstring'''

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






      def 				A__      ( self			:		Any     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		Dict     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		Tuple     ,      UpperCAmelCase			:		Tuple		)  ->  Union[str, Any]:


       '''simple docstring'''

       lowercase					:   str				  =self.num_labels
       lowercase					:   Dict				  =SqueezeBertForTokenClassification(config=UpperCAmelCase		)
       model.to(UpperCAmelCase		)
       model.eval()

       lowercase					:   Optional[Any]				  =model(UpperCAmelCase     ,      attention_mask=UpperCAmelCase     ,      labels=UpperCAmelCase		)
       self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.seq_length, self.num_labels)		)






      def 				A__      ( self			:		str     ,      UpperCAmelCase			:		Optional[int]     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		int     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		Union[str, Any]     ,      UpperCAmelCase			:		Dict		)  ->  Any:


       '''simple docstring'''

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






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


       '''simple docstring'''

       lowercase					:   Dict				  =self.prepare_config_and_inputs()
       (lowercase)					:   List[Any]				  =config_and_inputs
       lowercase					:   Dict				  ={'input_ids': input_ids, 'attention_mask': input_mask}
       return config, inputs_dict





@require_torch
class UpperCAmelCase_					(    __A				,       __A				,       unittest.TestCase				):




      """simple docstring"""




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






      def 				A__      ( self			:		Optional[int]		)  ->  Union[str, Any]:


       '''simple docstring'''

       lowercase					:   Union[str, Any]				  =SqueezeBertModelTester(self		)
       lowercase					:   Dict				  =ConfigTester(self     ,      config_class=UpperCAmelCase     ,      dim=37		)






      def 				A__      ( self			:		Any		)  ->  Dict:


       '''simple docstring'''

       self.config_tester.run_common_tests()






      def 				A__      ( self			:		Union[str, Any]		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   str				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_model(*UpperCAmelCase		)






      def 				A__      ( self			:		Any		)  ->  Any:


       '''simple docstring'''

       lowercase					:   Optional[int]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_masked_lm(*UpperCAmelCase		)






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


       '''simple docstring'''

       lowercase					:   Optional[int]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_question_answering(*UpperCAmelCase		)






      def 				A__      ( self			:		List[Any]		)  ->  List[str]:


       '''simple docstring'''

       lowercase					:   Union[str, Any]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_sequence_classification(*UpperCAmelCase		)






      def 				A__      ( self			:		Tuple		)  ->  List[Any]:


       '''simple docstring'''

       lowercase					:   Any				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_token_classification(*UpperCAmelCase		)






      def 				A__      ( self			:		Optional[Any]		)  ->  Tuple:


       '''simple docstring'''

       lowercase					:   Union[str, Any]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_multiple_choice(*UpperCAmelCase		)






      @slow
      def 				A__      ( self			:		Optional[int]		)  ->  Dict:


       '''simple docstring'''

       for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowercase					:   Optional[int]				  =SqueezeBertModel.from_pretrained(UpperCAmelCase		)
        self.assertIsNotNone(UpperCAmelCase		)





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




      """simple docstring"""




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


       '''simple docstring'''

       lowercase					:   int				  =SqueezeBertForSequenceClassification.from_pretrained('''squeezebert/squeezebert-mnli'''		)

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











'''simple docstring'''



from typing import List, Optional, Tuple

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_herbert import HerbertTokenizer


SCREAMING_SNAKE_CASE						=	logging.get_logger(__name__)

SCREAMING_SNAKE_CASE						=	{'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'}

SCREAMING_SNAKE_CASE						=	{
    'vocab_file': {
        'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/vocab.json'
    },
    'merges_file': {
        'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/merges.txt'
    },
}

SCREAMING_SNAKE_CASE						=	{'allegro/herbert-base-cased': 514}
SCREAMING_SNAKE_CASE						=	{}
class UpperCAmelCase_					(    __A				):




      """simple docstring"""




      UpperCamelCase_								=			VOCAB_FILES_NAMES
      UpperCamelCase_								=			PRETRAINED_VOCAB_FILES_MAP
      UpperCamelCase_								=			PRETRAINED_INIT_CONFIGURATION
      UpperCamelCase_								=			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      UpperCamelCase_								=			HerbertTokenizer






      def __init__( self			:		Dict     ,      UpperCAmelCase			:		Optional[Any]=None     ,      UpperCAmelCase			:		Optional[int]=None     ,      UpperCAmelCase			:		Any=None     ,      UpperCAmelCase			:		Tuple="<s>"     ,      UpperCAmelCase			:		List[Any]="<unk>"     ,      UpperCAmelCase			:		str="<pad>"     ,      UpperCAmelCase			:		Optional[Any]="<mask>"     ,      UpperCAmelCase			:		List[str]="</s>"     ,      **UpperCAmelCase			:		List[str]     ,      )  ->  Dict:


       '''simple docstring'''

       super().__init__(
           UpperCAmelCase     ,      UpperCAmelCase     ,      tokenizer_file=UpperCAmelCase     ,      cls_token=UpperCAmelCase     ,      unk_token=UpperCAmelCase     ,      pad_token=UpperCAmelCase     ,      mask_token=UpperCAmelCase     ,      sep_token=UpperCAmelCase     ,      **UpperCAmelCase     ,      )






      def 				A__      ( self			:		int     ,      UpperCAmelCase			:		List[int]     ,      UpperCAmelCase			:		Optional[List[int]] = None		)  ->  List[int]:


       '''simple docstring'''

       lowercase					:   List[Any]				  =[self.cls_token_id]
       lowercase					:   Any				  =[self.sep_token_id]
       if token_ids_a is None:
        return cls + token_ids_a + sep

       return cls + token_ids_a + sep + token_ids_a + sep






      def 				A__      ( self			:		Any     ,      UpperCAmelCase			:		List[int]     ,      UpperCAmelCase			:		Optional[List[int]] = None     ,      UpperCAmelCase			:		bool = False		)  ->  List[int]:


       '''simple docstring'''

       if already_has_special_tokens:
        return super().get_special_tokens_mask(
            token_ids_a=UpperCAmelCase     ,      token_ids_a=UpperCAmelCase     ,      already_has_special_tokens=UpperCAmelCase		)

       if token_ids_a is None:
        return [1] + ([0] * len(UpperCAmelCase		)) + [1]
       return [1] + ([0] * len(UpperCAmelCase		)) + [1] + ([0] * len(UpperCAmelCase		)) + [1]






      def 				A__      ( self			:		Optional[int]     ,      UpperCAmelCase			:		List[int]     ,      UpperCAmelCase			:		Optional[List[int]] = None		)  ->  List[int]:


       '''simple docstring'''

       lowercase					:   Optional[Any]				  =[self.sep_token_id]
       lowercase					:   Dict				  =[self.cls_token_id]

       if token_ids_a is None:
        return len(cls + token_ids_a + sep		) * [0]
       return len(cls + token_ids_a + sep		) * [0] + len(token_ids_a + sep		) * [1]






      def 				A__      ( self			:		Union[str, Any]     ,      UpperCAmelCase			:		str     ,      UpperCAmelCase			:		Optional[str] = None		)  ->  Tuple[str]:


       '''simple docstring'''

       lowercase					:   List[Any]				  =self._tokenizer.model.save(UpperCAmelCase     ,      name=UpperCAmelCase		)
       return tuple(UpperCAmelCase		)



