

'''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        a__  (							lowerCamelCase_  ):


   def __init__(			self      ,    _UpperCamelCase      ,    _UpperCamelCase=13      ,    _UpperCamelCase=7      ,    _UpperCamelCase=True      ,    _UpperCamelCase=True      ,    _UpperCamelCase=False      ,    _UpperCamelCase=True      ,    _UpperCamelCase=99      ,    _UpperCamelCase=32      ,    _UpperCamelCase=5      ,    _UpperCamelCase=4      ,    _UpperCamelCase=64      ,    _UpperCamelCase="gelu"      ,    _UpperCamelCase=0.1      ,    _UpperCamelCase=0.1      ,    _UpperCamelCase=512      ,    _UpperCamelCase=16      ,    _UpperCamelCase=2      ,    _UpperCamelCase=0.0_2      ,    _UpperCamelCase=3      ,    _UpperCamelCase=4      ,    _UpperCamelCase=None      ,    _UpperCamelCase=2      ,    _UpperCamelCase=2      ,    _UpperCamelCase=2      ,    _UpperCamelCase=2      ,    _UpperCamelCase=4      ,    _UpperCamelCase=1      ,    ):





      """simple docstring"""





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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





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

      _lowercase      :				Any      					=       None
      if self.use_input_mask:
         _lowercase      :				Union[str, Any]      					=       random_attention_mask([self.batch_size, self.seq_length]  )

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

      _lowercase      :				Dict      					=       self.get_config()

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


   def 						_lowerCamelCase       (			self  ):





      """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 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase  ):





      """simple docstring"""





      _lowercase      :				int      					=       SqueezeBertModel(config=_UpperCamelCase  )
      model.to(_UpperCamelCase  )
      model.eval()
      _lowercase      :				Dict      					=       model(_UpperCamelCase      ,    _UpperCamelCase  )
      _lowercase      :				Union[str, Any]      					=       model(_UpperCamelCase  )
      self.parent.assertEqual(result.last_hidden_state.shape      ,    (self.batch_size, self.seq_length, self.hidden_size)  )


   def 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase  ):





      """simple docstring"""





      _lowercase      :				List[Any]      					=       SqueezeBertForMaskedLM(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.vocab_size)  )


   def 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase  ):





      """simple docstring"""





      _lowercase      :				Tuple      					=       SqueezeBertForQuestionAnswering(config=_UpperCamelCase  )
      model.to(_UpperCamelCase  )
      model.eval()
      _lowercase      :				Union[str, Any]      					=       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 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase  ):





      """simple docstring"""





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


   def 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase  ):





      """simple docstring"""





      _lowercase      :				Optional[int]      					=       self.num_labels
      _lowercase      :				str      					=       SqueezeBertForTokenClassification(config=_UpperCamelCase  )
      model.to(_UpperCamelCase  )
      model.eval()

      _lowercase      :				List[Any]      					=       model(_UpperCamelCase      ,    attention_mask=_UpperCamelCase      ,    labels=_UpperCamelCase  )
      self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.seq_length, self.num_labels)  )


   def 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase  ):





      """simple docstring"""





      _lowercase      :				List[str]      					=       self.num_choices
      _lowercase      :				Any      					=       SqueezeBertForMultipleChoice(config=_UpperCamelCase  )
      model.to(_UpperCamelCase  )
      model.eval()
      _lowercase      :				Union[str, Any]      					=       input_ids.unsqueeze(1  ).expand(-1      ,    self.num_choices      ,    -1  ).contiguous()
      _lowercase      :				Tuple      					=       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 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Union[str, Any]      					=       self.prepare_config_and_inputs()
      ((_lowercase)					,				(_lowercase)					,				(_lowercase)					,				(_lowercase)					,				(_lowercase)					,				(_lowercase))      :				int      					=       config_and_inputs
      _lowercase      :				Tuple      					=       {"input_ids": input_ids, "attention_mask": input_mask}
      return config, inputs_dict




@require_torch
class        a__  (							lowerCamelCase_  ,				lowerCamelCase_  ,				unittest.TestCase  ):
   _SCREAMING_SNAKE_CASE   :					Dict     	=							(
       (
           SqueezeBertModel,
           SqueezeBertForMaskedLM,
           SqueezeBertForMultipleChoice,
           SqueezeBertForQuestionAnswering,
           SqueezeBertForSequenceClassification,
           SqueezeBertForTokenClassification,
       )
       if is_torch_available()
       else None
   )
   _SCREAMING_SNAKE_CASE   :					Union[str, Any]     	=							(
       {
           'feature-extraction': SqueezeBertModel,
           'fill-mask': SqueezeBertForMaskedLM,
           'question-answering': SqueezeBertForQuestionAnswering,
           'text-classification': SqueezeBertForSequenceClassification,
           'token-classification': SqueezeBertForTokenClassification,
           'zero-shot': SqueezeBertForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   _SCREAMING_SNAKE_CASE   :					List[Any]     	=							False
   _SCREAMING_SNAKE_CASE   :					List[str]     	=							True
   _SCREAMING_SNAKE_CASE   :					Optional[int]     	=							False


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Tuple      					=       SqueezeBertModelTester(self  )
      _lowercase      :				List[Any]      					=       ConfigTester(self      ,    config_class=_UpperCamelCase      ,    dim=37  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      self.config_tester.run_common_tests()


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Any      					=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_squeezebert_model(*_UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Optional[int]      					=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_squeezebert_for_masked_lm(*_UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				int      					=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_squeezebert_for_question_answering(*_UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				List[str]      					=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_squeezebert_for_sequence_classification(*_UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Optional[Any]      					=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_squeezebert_for_token_classification(*_UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				str      					=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_squeezebert_for_multiple_choice(*_UpperCamelCase  )






   @slow
   def 						_lowerCamelCase       (			self  ):





      """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        a__  (							unittest.TestCase  ):


   @slow
   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Optional[int]      					=       SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli"  )

      _lowercase      :				Tuple      					=       torch.tensor([[1, 29414, 232, 328, 740, 1140, 12695, 69, 13, 1588, 2]]  )
      _lowercase      :				int      					=       model(_UpperCamelCase  )[0]
      _lowercase      :				Optional[Any]      					=       torch.Size((1, 3)  )
      self.assertEqual(output.shape      ,    _UpperCamelCase  )
      _lowercase      :				List[str]      					=       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'''
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin
from .feature_extraction_wavaveca import WavaVecaFeatureExtractor
from .tokenization_wavaveca import WavaVecaCTCTokenizer


class        a__  (							lowerCamelCase_  ):
   _SCREAMING_SNAKE_CASE   :					int     	=							'Wav2Vec2FeatureExtractor'
   _SCREAMING_SNAKE_CASE   :					List[str]     	=							'AutoTokenizer'


   def __init__(			self      ,    _UpperCamelCase      ,    _UpperCamelCase  ):





      """simple docstring"""





      super().__init__(_UpperCamelCase      ,    _UpperCamelCase  )
      _lowercase      :				List[Any]      					=       self.feature_extractor
      _lowercase      :				Optional[Any]      					=       False


   @classmethod
   def 						_lowerCamelCase       (			cls      ,    _UpperCamelCase      ,    **_UpperCamelCase  ):





      """simple docstring"""





      try:
         return super().from_pretrained(_UpperCamelCase      ,    **_UpperCamelCase  )
      except OSError:
         warnings.warn(
             f'''Loading a tokenizer inside {cls.__name__} from a config that does not'''
             " include a `tokenizer_class` attribute is deprecated and will be "
             "removed in v5. Please add `'tokenizer_class': 'Wav2Vec2CTCTokenizer'`"
             " attribute to either your `config.json` or `tokenizer_config.json` "
             "file to suppress this warning: "      ,    _UpperCamelCase      ,    )

         _lowercase      :				Dict      					=       WavaVecaFeatureExtractor.from_pretrained(_UpperCamelCase      ,    **_UpperCamelCase  )
         _lowercase      :				str      					=       WavaVecaCTCTokenizer.from_pretrained(_UpperCamelCase      ,    **_UpperCamelCase  )

         return cls(feature_extractor=_UpperCamelCase      ,    tokenizer=_UpperCamelCase  )


   def __call__(			self      ,    *_UpperCamelCase      ,    **_UpperCamelCase  ):





      """simple docstring"""





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

      if "raw_speech" in kwargs:
         warnings.warn("Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead."  )
         _lowercase      :				int      					=       kwargs.pop("raw_speech"  )
      else:
         _lowercase      :				List[Any]      					=       kwargs.pop("audio"      ,    _UpperCamelCase  )
      _lowercase      :				List[Any]      					=       kwargs.pop("sampling_rate"      ,    _UpperCamelCase  )
      _lowercase      :				Union[str, Any]      					=       kwargs.pop("text"      ,    _UpperCamelCase  )
      if len(_UpperCamelCase  ) > 0:
         _lowercase      :				int      					=       args[0]
         _lowercase      :				Any      					=       args[1:]

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

      if audio is not None:
         _lowercase      :				Dict      					=       self.feature_extractor(_UpperCamelCase      ,    *_UpperCamelCase      ,    sampling_rate=_UpperCamelCase      ,    **_UpperCamelCase  )
      if text is not None:
         _lowercase      :				Union[str, Any]      					=       self.tokenizer(_UpperCamelCase      ,    **_UpperCamelCase  )

      if text is None:
         return inputs
      elif audio is None:
         return encodings
      else:
         _lowercase      :				int      					=       encodings["input_ids"]
         return inputs


   def 						_lowerCamelCase       (			self      ,    *_UpperCamelCase      ,    **_UpperCamelCase  ):





      """simple docstring"""





      if self._in_target_context_manager:
         return self.current_processor.pad(*_UpperCamelCase      ,    **_UpperCamelCase  )

      _lowercase      :				List[Any]      					=       kwargs.pop("input_features"      ,    _UpperCamelCase  )
      _lowercase      :				Any      					=       kwargs.pop("labels"      ,    _UpperCamelCase  )
      if len(_UpperCamelCase  ) > 0:
         _lowercase      :				Any      					=       args[0]
         _lowercase      :				Any      					=       args[1:]

      if input_features is not None:
         _lowercase      :				Any      					=       self.feature_extractor.pad(_UpperCamelCase      ,    *_UpperCamelCase      ,    **_UpperCamelCase  )
      if labels is not None:
         _lowercase      :				int      					=       self.tokenizer.pad(_UpperCamelCase      ,    **_UpperCamelCase  )

      if labels is None:
         return input_features
      elif input_features is None:
         return labels
      else:
         _lowercase      :				Optional[Any]      					=       labels["input_ids"]
         return input_features


   def 						_lowerCamelCase       (			self      ,    *_UpperCamelCase      ,    **_UpperCamelCase  ):





      """simple docstring"""





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


   def 						_lowerCamelCase       (			self      ,    *_UpperCamelCase      ,    **_UpperCamelCase  ):





      """simple docstring"""





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






   @contextmanager
   def 						_lowerCamelCase       (			self  ):





      """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 audio inputs, or in a separate call."  )
      _lowercase      :				Optional[Any]      					=       True
      _lowercase      :				Dict      					=       self.tokenizer
      yield
      _lowercase      :				List[str]      					=       self.feature_extractor
      _lowercase      :				List[str]      					=       False






