


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  : str       ,  _lowerCamelCase  : Union[str, Any]=1_3       ,  _lowerCamelCase  : List[str]=7       ,  _lowerCamelCase  : List[Any]=True       ,  _lowerCamelCase  : List[str]=True       ,  _lowerCamelCase  : Optional[Any]=False       ,  _lowerCamelCase  : int=True       ,  _lowerCamelCase  : Any=9_9       ,  _lowerCamelCase  : int=3_2       ,  _lowerCamelCase  : int=5       ,  _lowerCamelCase  : int=4       ,  _lowerCamelCase  : Union[str, Any]=6_4       ,  _lowerCamelCase  : Union[str, Any]="gelu"       ,  _lowerCamelCase  : List[str]=0.1       ,  _lowerCamelCase  : Any=0.1       ,  _lowerCamelCase  : Optional[Any]=5_1_2       ,  _lowerCamelCase  : int=1_6       ,  _lowerCamelCase  : Optional[int]=2       ,  _lowerCamelCase  : Dict=0.02       ,  _lowerCamelCase  : str=3       ,  _lowerCamelCase  : Optional[Any]=4       ,  _lowerCamelCase  : List[str]=None       ,  _lowerCamelCase  : Optional[int]=2       ,  _lowerCamelCase  : Any=2       ,  _lowerCamelCase  : str=2       ,  _lowerCamelCase  : Union[str, Any]=2       ,  _lowerCamelCase  : str=4       ,  _lowerCamelCase  : Optional[int]=1       ,  ):






         '''simple docstring'''



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

   def   _snake_case    (  self  : int							):






         '''simple docstring'''



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

         __lowerCamelCase     :      int       						=						None
         if self.use_input_mask:
               __lowerCamelCase     :      Any       						=						random_attention_mask([self.batch_size, self.seq_length]							)

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

         __lowerCamelCase     :      List[Any]       						=						self.get_config()

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

   def   _snake_case    (  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   _snake_case    (  self  : Dict       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : Optional[int]       ,  _lowerCamelCase  : str       ,  _lowerCamelCase  : Tuple       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : Tuple							):






         '''simple docstring'''



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

   def   _snake_case    (  self  : Union[str, Any]       ,  _lowerCamelCase  : str       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : List[Any]       ,  _lowerCamelCase  : Tuple       ,  _lowerCamelCase  : Union[str, Any]       ,  _lowerCamelCase  : Optional[int]							):






         '''simple docstring'''



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

   def   _snake_case    (  self  : int       ,  _lowerCamelCase  : Union[str, Any]       ,  _lowerCamelCase  : Optional[Any]       ,  _lowerCamelCase  : Tuple       ,  _lowerCamelCase  : Tuple       ,  _lowerCamelCase  : str       ,  _lowerCamelCase  : Any							):






         '''simple docstring'''



         __lowerCamelCase     :      List[str]       						=						SqueezeBertForQuestionAnswering(config=_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()
         __lowerCamelCase     :      Optional[int]       						=						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   _snake_case    (  self  : str       ,  _lowerCamelCase  : str       ,  _lowerCamelCase  : Optional[int]       ,  _lowerCamelCase  : Optional[int]       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : List[str]       ,  _lowerCamelCase  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						self.num_labels
         __lowerCamelCase     :      Optional[int]       						=						SqueezeBertForSequenceClassification(_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()
         __lowerCamelCase     :      int       						=						model(_lowerCamelCase       ,  attention_mask=_lowerCamelCase       ,  labels=_lowerCamelCase							)
         self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_labels)							)

   def   _snake_case    (  self  : Tuple       ,  _lowerCamelCase  : Any       ,  _lowerCamelCase  : Optional[int]       ,  _lowerCamelCase  : int       ,  _lowerCamelCase  : Any       ,  _lowerCamelCase  : Any       ,  _lowerCamelCase  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						self.num_labels
         __lowerCamelCase     :      Dict       						=						SqueezeBertForTokenClassification(config=_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()

         __lowerCamelCase     :      Tuple       						=						model(_lowerCamelCase       ,  attention_mask=_lowerCamelCase       ,  labels=_lowerCamelCase							)
         self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.num_labels)							)

   def   _snake_case    (  self  : Any       ,  _lowerCamelCase  : Optional[Any]       ,  _lowerCamelCase  : Any       ,  _lowerCamelCase  : Optional[int]       ,  _lowerCamelCase  : str       ,  _lowerCamelCase  : Union[str, Any]       ,  _lowerCamelCase  : Optional[int]							):






         '''simple docstring'''



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


   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[Any]       						=						self.prepare_config_and_inputs()
         ((__lowerCamelCase)						, (__lowerCamelCase)						, (__lowerCamelCase)						, (__lowerCamelCase)						, (__lowerCamelCase)						, (__lowerCamelCase))     :      Any       						=						config_and_inputs
         __lowerCamelCase     :      List[Any]       						=						{"""input_ids""": input_ids, """attention_mask""": input_mask}
         return config, inputs_dict



@require_torch
class 							_UpperCamelCase (						A,A,unittest.TestCase						):

   '''simple docstring'''







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

   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						SqueezeBertModelTester(self							)
         __lowerCamelCase     :      Tuple       						=						ConfigTester(self       ,  config_class=_lowerCamelCase       ,  dim=3_7							)

   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         self.config_tester.run_common_tests()

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






         '''simple docstring'''



         __lowerCamelCase     :      str       						=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_model(*_lowerCamelCase							)

   def   _snake_case    (  self  : Dict							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_for_masked_lm(*_lowerCamelCase							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_for_question_answering(*_lowerCamelCase							)

   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_for_sequence_classification(*_lowerCamelCase							)

   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_for_token_classification(*_lowerCamelCase							)

   def   _snake_case    (  self  : List[str]							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_squeezebert_for_multiple_choice(*_lowerCamelCase							)


   @slow
   def   _snake_case    (  self  : List[str]							):






         '''simple docstring'''



         for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __lowerCamelCase     :      str       						=						SqueezeBertModel.from_pretrained(_lowerCamelCase							)
               self.assertIsNotNone(_lowerCamelCase							)



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

   '''simple docstring'''







   @slow
   def   _snake_case    (  self  : Dict							):






         '''simple docstring'''



         __lowerCamelCase     :      str       						=						SqueezeBertForSequenceClassification.from_pretrained("""squeezebert/squeezebert-mnli"""							)

         __lowerCamelCase     :      Any       						=						torch.tensor([[1, 2_9_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 1_3, 1_5_8_8, 2]]							)
         __lowerCamelCase     :      List[Any]       						=						model(_lowerCamelCase							)[0]
         __lowerCamelCase     :      int       						=						torch.Size((1, 3)							)
         self.assertEqual(output.shape       ,  _lowerCamelCase							)
         __lowerCamelCase     :      List[Any]       						=						torch.tensor([[0.6_401, -0.0_349, -0.6_041]]							)
         self.assertTrue(torch.allclose(_lowerCamelCase       ,  _lowerCamelCase       ,  atol=1E-4							)							)







import re
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin



class 							_UpperCamelCase (						A						):

   '''simple docstring'''







   a_				:							List[Any]    = ["image_processor", "tokenizer"]
   a_				:							Tuple    = "AutoImageProcessor"
   a_				:							Tuple    = "AutoTokenizer"

   def __init__(  self  : Union[str, Any]       ,  _lowerCamelCase  : List[Any]=None       ,  _lowerCamelCase  : Any=None       ,  **_lowerCamelCase  : List[str]							):






         '''simple docstring'''



         __lowerCamelCase     :      Tuple       						=						None
         if "feature_extractor" in kwargs:
               warnings.warn(
                   """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
                   """ instead."""       ,  _lowerCamelCase       ,  )
               __lowerCamelCase     :      List[Any]       						=						kwargs.pop("""feature_extractor"""							)

         __lowerCamelCase     :      Optional[Any]       						=						image_processor if image_processor is not None else feature_extractor
         if image_processor is None:
               raise ValueError("""You need to specify an `image_processor`."""							)
         if tokenizer is None:
               raise ValueError("""You need to specify a `tokenizer`."""							)

         super().__init__(_lowerCamelCase       ,  _lowerCamelCase							)
         __lowerCamelCase     :      List[Any]       						=						self.image_processor
         __lowerCamelCase     :      Any       						=						False

   def __call__(  self  : Union[str, Any]       ,  *_lowerCamelCase  : str       ,  **_lowerCamelCase  : Dict							):






         '''simple docstring'''



         if self._in_target_context_manager:
               return self.current_processor(*_lowerCamelCase       ,  **_lowerCamelCase							)

         __lowerCamelCase     :      List[str]       						=						kwargs.pop("""images"""       ,  _lowerCamelCase							)
         __lowerCamelCase     :      List[str]       						=						kwargs.pop("""text"""       ,  _lowerCamelCase							)
         if len(_lowerCamelCase							) > 0:
               __lowerCamelCase     :      Tuple       						=						args[0]
               __lowerCamelCase     :      List[Any]       						=						args[1:]

         if images is None and text is None:
               raise ValueError("""You need to specify either an `images` or `text` input to process."""							)

         if images is not None:
               __lowerCamelCase     :      Tuple       						=						self.image_processor(_lowerCamelCase       ,  *_lowerCamelCase       ,  **_lowerCamelCase							)
         if text is not None:
               __lowerCamelCase     :      int       						=						self.tokenizer(_lowerCamelCase       ,  **_lowerCamelCase							)

         if text is None:
               return inputs
         elif images is None:
               return encodings
         else:
               __lowerCamelCase     :      Optional[int]       						=						encodings["""input_ids"""]
               return inputs

   def   _snake_case    (  self  : List[Any]       ,  *_lowerCamelCase  : Tuple       ,  **_lowerCamelCase  : Union[str, Any]							):






         '''simple docstring'''



         return self.tokenizer.batch_decode(*_lowerCamelCase       ,  **_lowerCamelCase							)

   def   _snake_case    (  self  : List[str]       ,  *_lowerCamelCase  : Optional[int]       ,  **_lowerCamelCase  : int							):






         '''simple docstring'''



         return self.tokenizer.decode(*_lowerCamelCase       ,  **_lowerCamelCase							)

   @contextmanager
   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         warnings.warn(
             """`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your """
             """labels by using the argument `text` of the regular `__call__` method (either in the same call as """
             """your images inputs, or in a separate call."""							)
         __lowerCamelCase     :      Any       						=						True
         __lowerCamelCase     :      int       						=						self.tokenizer
         yield
         __lowerCamelCase     :      Dict       						=						self.image_processor
         __lowerCamelCase     :      List[Any]       						=						False

   def   _snake_case    (  self  : List[str]       ,  _lowerCamelCase  : str       ,  _lowerCamelCase  : List[str]=False       ,  _lowerCamelCase  : List[Any]=None							):






         '''simple docstring'''



         if added_vocab is None:
               __lowerCamelCase     :      List[str]       						=						self.tokenizer.get_added_vocab()

         __lowerCamelCase     :      List[Any]       						=						{}

         while tokens:
               __lowerCamelCase     :      Dict       						=						re.search(R"""<s_(.*?)>"""       ,  _lowerCamelCase       ,  re.IGNORECASE							)
               if start_token is None:
                     break
               __lowerCamelCase     :      Dict       						=						start_token.group(1							)
               __lowerCamelCase     :      List[str]       						=						re.search(RF"""</s_{key}>"""       ,  _lowerCamelCase       ,  re.IGNORECASE							)
               __lowerCamelCase     :      Optional[int]       						=						start_token.group()
               if end_token is None:
                     __lowerCamelCase     :      int       						=						tokens.replace(_lowerCamelCase       ,  """"""							)
               else:
                     __lowerCamelCase     :      Dict       						=						end_token.group()
                     __lowerCamelCase     :      Optional[Any]       						=						re.escape(_lowerCamelCase							)
                     __lowerCamelCase     :      List[str]       						=						re.escape(_lowerCamelCase							)
                     __lowerCamelCase     :      Any       						=						re.search(F"""{start_token_escaped}(.*?){end_token_escaped}"""       ,  _lowerCamelCase       ,  re.IGNORECASE							)
                     if content is not None:
                           __lowerCamelCase     :      Union[str, Any]       						=						content.group(1							).strip()
                           if r"<s_" in content and r"</s_" in content:  # non-leaf node
                                 __lowerCamelCase     :      Union[str, Any]       						=						self.tokenajson(_lowerCamelCase       ,  is_inner_value=_lowerCamelCase       ,  added_vocab=_lowerCamelCase							)
                                 if value:
                                       if len(_lowerCamelCase							) == 1:
                                             __lowerCamelCase     :      Optional[int]       						=						value[0]
                                       __lowerCamelCase     :      Union[str, Any]       						=						value
                           else:  # leaf nodes
                                 __lowerCamelCase     :      List[Any]       						=						[]
                                 for leaf in content.split(R"""<sep/>"""							):
                                       __lowerCamelCase     :      Optional[Any]       						=						leaf.strip()
                                       if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>":
                                             __lowerCamelCase     :      str       						=						leaf[1:-2]  # for categorical special tokens
                                       output[key].append(_lowerCamelCase							)
                                 if len(output[key]							) == 1:
                                       __lowerCamelCase     :      Tuple       						=						output[key][0]

                     __lowerCamelCase     :      Tuple       						=						tokens[tokens.find(_lowerCamelCase							) + len(_lowerCamelCase							) :].strip()
                     if tokens[:6] == r"<sep/>":  # non-leaf nodes
                           return [output] + self.tokenajson(tokens[6:]       ,  is_inner_value=_lowerCamelCase       ,  added_vocab=_lowerCamelCase							)

         if len(_lowerCamelCase							):
               return [output] if is_inner_value else output
         else:
               return [] if is_inner_value else {"text_sequence": tokens}

   @property
   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         warnings.warn(
             """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."""       ,  _lowerCamelCase       ,  )
         return self.image_processor_class


   @property
   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         warnings.warn(
             """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."""       ,  _lowerCamelCase       ,  )
         return self.image_processor



