






"""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 (	_lowercase			):






    def __init__(			self     ,A     ,A=13     ,A=7     ,A=True     ,A=True     ,A=False     ,A=True     ,A=99     ,A=32     ,A=5     ,A=4     ,A=64     ,A="gelu"     ,A=0.1     ,A=0.1     ,A=512     ,A=16     ,A=2     ,A=0.02     ,A=3     ,A=4     ,A=None     ,A=2     ,A=2     ,A=2     ,A=2     ,A=4     ,A=1     ,):
          UpperCAmelCase          =	parent
          UpperCAmelCase          =	batch_size
          UpperCAmelCase          =	seq_length
          UpperCAmelCase          =	is_training
          UpperCAmelCase          =	use_input_mask
          UpperCAmelCase          =	use_token_type_ids
          UpperCAmelCase          =	use_labels
          UpperCAmelCase          =	vocab_size
          UpperCAmelCase          =	hidden_size
          UpperCAmelCase          =	num_hidden_layers
          UpperCAmelCase          =	num_attention_heads
          UpperCAmelCase          =	intermediate_size
          UpperCAmelCase          =	hidden_act
          UpperCAmelCase          =	hidden_dropout_prob
          UpperCAmelCase          =	attention_probs_dropout_prob
          UpperCAmelCase          =	max_position_embeddings
          UpperCAmelCase          =	type_vocab_size
          UpperCAmelCase          =	type_sequence_label_size
          UpperCAmelCase          =	initializer_range
          UpperCAmelCase          =	num_labels
          UpperCAmelCase          =	num_choices
          UpperCAmelCase          =	scope
          UpperCAmelCase          =	q_groups
          UpperCAmelCase          =	k_groups
          UpperCAmelCase          =	v_groups
          UpperCAmelCase          =	post_attention_groups
          UpperCAmelCase          =	intermediate_groups
          UpperCAmelCase          =	output_groups






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	ids_tensor([self.batch_size, self.seq_length]     ,self.vocab_size )

          UpperCAmelCase          =	None
          if self.use_input_mask:
                UpperCAmelCase          =	random_attention_mask([self.batch_size, self.seq_length] )

          UpperCAmelCase          =	None
          UpperCAmelCase          =	None
          UpperCAmelCase          =	None
          if self.use_labels:
                UpperCAmelCase          =	ids_tensor([self.batch_size]     ,self.type_sequence_label_size )
                UpperCAmelCase          =	ids_tensor([self.batch_size, self.seq_length]     ,self.num_labels )
                UpperCAmelCase          =	ids_tensor([self.batch_size]     ,self.num_choices )

          UpperCAmelCase          =	self.get_config()

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






    def     _UpperCamelCase							(			self ):
          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     _UpperCamelCase							(			self     ,A     ,A     ,A     ,A     ,A     ,A ):
          UpperCAmelCase          =	SqueezeBertModel(config=__UpperCamelCase )
          model.to(__UpperCamelCase )
          model.eval()
          UpperCAmelCase          =	model(__UpperCamelCase     ,__UpperCamelCase )
          UpperCAmelCase          =	model(__UpperCamelCase )
          self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.hidden_size) )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,A     ,A     ,A ):
          UpperCAmelCase          =	SqueezeBertForMaskedLM(config=__UpperCamelCase )
          model.to(__UpperCamelCase )
          model.eval()
          UpperCAmelCase          =	model(__UpperCamelCase     ,attention_mask=__UpperCamelCase     ,labels=__UpperCamelCase )
          self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length, self.vocab_size) )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,A     ,A     ,A ):
          UpperCAmelCase          =	SqueezeBertForQuestionAnswering(config=__UpperCamelCase )
          model.to(__UpperCamelCase )
          model.eval()
          UpperCAmelCase          =	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     _UpperCamelCase							(			self     ,A     ,A     ,A     ,A     ,A     ,A ):
          UpperCAmelCase          =	self.num_labels
          UpperCAmelCase          =	SqueezeBertForSequenceClassification(__UpperCamelCase )
          model.to(__UpperCamelCase )
          model.eval()
          UpperCAmelCase          =	model(__UpperCamelCase     ,attention_mask=__UpperCamelCase     ,labels=__UpperCamelCase )
          self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.num_labels) )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,A     ,A     ,A ):
          UpperCAmelCase          =	self.num_labels
          UpperCAmelCase          =	SqueezeBertForTokenClassification(config=__UpperCamelCase )
          model.to(__UpperCamelCase )
          model.eval()

          UpperCAmelCase          =	model(__UpperCamelCase     ,attention_mask=__UpperCamelCase     ,labels=__UpperCamelCase )
          self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length, self.num_labels) )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,A     ,A     ,A ):
          UpperCAmelCase          =	self.num_choices
          UpperCAmelCase          =	SqueezeBertForMultipleChoice(config=__UpperCamelCase )
          model.to(__UpperCamelCase )
          model.eval()
          UpperCAmelCase          =	input_ids.unsqueeze(1 ).expand(-1     ,self.num_choices     ,-1 ).contiguous()
          UpperCAmelCase          =	input_mask.unsqueeze(1 ).expand(-1     ,self.num_choices     ,-1 ).contiguous()
          UpperCAmelCase          =	model(
              __UpperCamelCase     ,attention_mask=__UpperCamelCase     ,labels=__UpperCamelCase     ,)
          self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.num_choices) )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.prepare_config_and_inputs()
          ((UpperCAmelCase)      , (UpperCAmelCase)      , (UpperCAmelCase)      , (UpperCAmelCase)      , (UpperCAmelCase)      , (UpperCAmelCase))          =	config_and_inputs
          UpperCAmelCase          =	{"""input_ids""": input_ids, """attention_mask""": input_mask}
          return config, inputs_dict







@require_torch
class  UpperCAmelCase (	_lowercase   ,					_lowercase   ,					unittest.TestCase			):
    SCREAMING_SNAKE_CASE		=      (
        (
            SqueezeBertModel,
            SqueezeBertForMaskedLM,
            SqueezeBertForMultipleChoice,
            SqueezeBertForQuestionAnswering,
            SqueezeBertForSequenceClassification,
            SqueezeBertForTokenClassification,
        )
        if is_torch_available()
        else None
    )
    SCREAMING_SNAKE_CASE		=      (
        {
            '''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		=      False
    SCREAMING_SNAKE_CASE		=      True
    SCREAMING_SNAKE_CASE		=      False






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	SqueezeBertModelTester(self )
          UpperCAmelCase          =	ConfigTester(self     ,config_class=__UpperCamelCase     ,dim=37 )






    def     _UpperCamelCase							(			self ):
          self.config_tester.run_common_tests()






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_model(*__UpperCamelCase )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_masked_lm(*__UpperCamelCase )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_question_answering(*__UpperCamelCase )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_sequence_classification(*__UpperCamelCase )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_token_classification(*__UpperCamelCase )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_multiple_choice(*__UpperCamelCase )






    @slow
    def     _UpperCamelCase							(			self ):
          for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase          =	SqueezeBertModel.from_pretrained(__UpperCamelCase )
                self.assertIsNotNone(__UpperCamelCase )







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






    @slow
    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	SqueezeBertForSequenceClassification.from_pretrained("""squeezebert/squeezebert-mnli""" )

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











"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_UpperCamelCase					      =  {
    """configuration_roformer""": ["""ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """RoFormerConfig""", """RoFormerOnnxConfig"""],
    """tokenization_roformer""": ["""RoFormerTokenizer"""],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _UpperCamelCase					      =  ["""RoFormerTokenizerFast"""]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _UpperCamelCase					      =  [
      """ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """RoFormerForCausalLM""",
      """RoFormerForMaskedLM""",
      """RoFormerForMultipleChoice""",
      """RoFormerForQuestionAnswering""",
      """RoFormerForSequenceClassification""",
      """RoFormerForTokenClassification""",
      """RoFormerLayer""",
      """RoFormerModel""",
      """RoFormerPreTrainedModel""",
      """load_tf_weights_in_roformer""",
  ]


try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _UpperCamelCase					      =  [
      """TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TFRoFormerForCausalLM""",
      """TFRoFormerForMaskedLM""",
      """TFRoFormerForMultipleChoice""",
      """TFRoFormerForQuestionAnswering""",
      """TFRoFormerForSequenceClassification""",
      """TFRoFormerForTokenClassification""",
      """TFRoFormerLayer""",
      """TFRoFormerModel""",
      """TFRoFormerPreTrainedModel""",
  ]


try:
  if not is_flax_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _UpperCamelCase					      =  [
      """FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """FlaxRoFormerForMaskedLM""",
      """FlaxRoFormerForMultipleChoice""",
      """FlaxRoFormerForQuestionAnswering""",
      """FlaxRoFormerForSequenceClassification""",
      """FlaxRoFormerForTokenClassification""",
      """FlaxRoFormerModel""",
      """FlaxRoFormerPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig
  from .tokenization_roformer import RoFormerTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_roformer_fast import RoFormerTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_roformer import (
        ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
        RoFormerForCausalLM,
        RoFormerForMaskedLM,
        RoFormerForMultipleChoice,
        RoFormerForQuestionAnswering,
        RoFormerForSequenceClassification,
        RoFormerForTokenClassification,
        RoFormerLayer,
        RoFormerModel,
        RoFormerPreTrainedModel,
        load_tf_weights_in_roformer,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_roformer import (
        TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFRoFormerForCausalLM,
        TFRoFormerForMaskedLM,
        TFRoFormerForMultipleChoice,
        TFRoFormerForQuestionAnswering,
        TFRoFormerForSequenceClassification,
        TFRoFormerForTokenClassification,
        TFRoFormerLayer,
        TFRoFormerModel,
        TFRoFormerPreTrainedModel,
    )

  try:
    if not is_flax_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_flax_roformer import (
        FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
        FlaxRoFormerForMaskedLM,
        FlaxRoFormerForMultipleChoice,
        FlaxRoFormerForQuestionAnswering,
        FlaxRoFormerForSequenceClassification,
        FlaxRoFormerForTokenClassification,
        FlaxRoFormerModel,
        FlaxRoFormerPreTrainedModel,
    )


else:
  import sys

  _UpperCamelCase					      =  _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



