


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__     ( A__  ):



  """simple docstring"""



  def __init__( self			:  str				,				lowerCamelCase__			:  Any				,				lowerCamelCase__			:  Union[str, Any]=13				,				lowerCamelCase__			:  str=7				,				lowerCamelCase__			:  Optional[Any]=True				,				lowerCamelCase__			:  List[str]=True				,				lowerCamelCase__			:  Tuple=False				,				lowerCamelCase__			:  Any=True				,				lowerCamelCase__			:  List[str]=99				,				lowerCamelCase__			:  Optional[Any]=32				,				lowerCamelCase__			:  str=5				,				lowerCamelCase__			:  Optional[int]=4				,				lowerCamelCase__			:  Union[str, Any]=64				,				lowerCamelCase__			:  List[str]="gelu"				,				lowerCamelCase__			:  List[Any]=0.1				,				lowerCamelCase__			:  Any=0.1				,				lowerCamelCase__			:  Any=512				,				lowerCamelCase__			:  Any=16				,				lowerCamelCase__			:  Union[str, Any]=2				,				lowerCamelCase__			:  str=0.02				,				lowerCamelCase__			:  List[Any]=3				,				lowerCamelCase__			:  Dict=4				,				lowerCamelCase__			:  Optional[int]=None				,				lowerCamelCase__			:  List[str]=2				,				lowerCamelCase__			:  Optional[int]=2				,				lowerCamelCase__			:  Optional[Any]=2				,				lowerCamelCase__			:  List[str]=2				,				lowerCamelCase__			:  List[str]=4				,				lowerCamelCase__			:  Dict=1				,				):
        a__	:  Optional[int]     =   parent
        a__	:  Any     =   batch_size
        a__	:  Tuple     =   seq_length
        a__	:  Union[str, Any]     =   is_training
        a__	:  List[str]     =   use_input_mask
        a__	:  int     =   use_token_type_ids
        a__	:  Optional[Any]     =   use_labels
        a__	:  Tuple     =   vocab_size
        a__	:  Optional[int]     =   hidden_size
        a__	:  Any     =   num_hidden_layers
        a__	:  List[Any]     =   num_attention_heads
        a__	:  List[Any]     =   intermediate_size
        a__	:  int     =   hidden_act
        a__	:  List[Any]     =   hidden_dropout_prob
        a__	:  List[Any]     =   attention_probs_dropout_prob
        a__	:  Optional[int]     =   max_position_embeddings
        a__	:  Any     =   type_vocab_size
        a__	:  Union[str, Any]     =   type_sequence_label_size
        a__	:  Union[str, Any]     =   initializer_range
        a__	:  Union[str, Any]     =   num_labels
        a__	:  str     =   num_choices
        a__	:  Any     =   scope
        a__	:  Tuple     =   q_groups
        a__	:  Optional[Any]     =   k_groups
        a__	:  Optional[int]     =   v_groups
        a__	:  List[str]     =   post_attention_groups
        a__	:  Optional[int]     =   intermediate_groups
        a__	:  Union[str, Any]     =   output_groups

  def    _UpperCamelCase( self			:  List[Any]       ):
        a__	:  Any     =   ids_tensor([self.batch_size, self.seq_length]				,				self.vocab_size       )

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

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

        a__	:  Optional[int]     =   self.get_config()

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

  def    _UpperCamelCase( self			:  Any       ):
        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			:  Dict				,				lowerCamelCase__			:  Any				,				lowerCamelCase__			:  Dict				,				lowerCamelCase__			:  str				,				lowerCamelCase__			:  Union[str, Any]				,				lowerCamelCase__			:  List[str]				,				lowerCamelCase__			:  str       ):
        a__	:  Dict     =   SqueezeBertModel(config=lowerCamelCase__       )
        model.to(lowerCamelCase__       )
        model.eval()
        a__	:  Union[str, Any]     =   model(lowerCamelCase__				,				lowerCamelCase__       )
        a__	:  Dict     =   model(lowerCamelCase__       )
        self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.seq_length, self.hidden_size)       )

  def    _UpperCamelCase( self			:  int				,				lowerCamelCase__			:  Union[str, Any]				,				lowerCamelCase__			:  Tuple				,				lowerCamelCase__			:  Optional[int]				,				lowerCamelCase__			:  Optional[int]				,				lowerCamelCase__			:  Optional[Any]				,				lowerCamelCase__			:  Optional[Any]       ):
        a__	:  int     =   SqueezeBertForMaskedLM(config=lowerCamelCase__       )
        model.to(lowerCamelCase__       )
        model.eval()
        a__	:  Any     =   model(lowerCamelCase__				,				attention_mask=lowerCamelCase__				,				labels=lowerCamelCase__       )
        self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.seq_length, self.vocab_size)       )

  def    _UpperCamelCase( self			:  Optional[int]				,				lowerCamelCase__			:  List[str]				,				lowerCamelCase__			:  Union[str, Any]				,				lowerCamelCase__			:  Optional[Any]				,				lowerCamelCase__			:  Any				,				lowerCamelCase__			:  Optional[Any]				,				lowerCamelCase__			:  Union[str, Any]       ):
        a__	:  Any     =   SqueezeBertForQuestionAnswering(config=lowerCamelCase__       )
        model.to(lowerCamelCase__       )
        model.eval()
        a__	:  Optional[Any]     =   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    _UpperCamelCase( self			:  Union[str, Any]				,				lowerCamelCase__			:  List[Any]				,				lowerCamelCase__			:  Any				,				lowerCamelCase__			:  Tuple				,				lowerCamelCase__			:  str				,				lowerCamelCase__			:  str				,				lowerCamelCase__			:  Union[str, Any]       ):
        a__	:  Optional[Any]     =   self.num_labels
        a__	:  Any     =   SqueezeBertForSequenceClassification(lowerCamelCase__       )
        model.to(lowerCamelCase__       )
        model.eval()
        a__	:  List[str]     =   model(lowerCamelCase__				,				attention_mask=lowerCamelCase__				,				labels=lowerCamelCase__       )
        self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_labels)       )

  def    _UpperCamelCase( self			:  List[Any]				,				lowerCamelCase__			:  int				,				lowerCamelCase__			:  Union[str, Any]				,				lowerCamelCase__			:  Optional[int]				,				lowerCamelCase__			:  Union[str, Any]				,				lowerCamelCase__			:  str				,				lowerCamelCase__			:  int       ):
        a__	:  List[str]     =   self.num_labels
        a__	:  str     =   SqueezeBertForTokenClassification(config=lowerCamelCase__       )
        model.to(lowerCamelCase__       )
        model.eval()

        a__	:  List[str]     =   model(lowerCamelCase__				,				attention_mask=lowerCamelCase__				,				labels=lowerCamelCase__       )
        self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.seq_length, self.num_labels)       )

  def    _UpperCamelCase( self			:  Optional[int]				,				lowerCamelCase__			:  Dict				,				lowerCamelCase__			:  Optional[Any]				,				lowerCamelCase__			:  List[Any]				,				lowerCamelCase__			:  Optional[int]				,				lowerCamelCase__			:  List[Any]				,				lowerCamelCase__			:  List[str]       ):
        a__	:  str     =   self.num_choices
        a__	:  Optional[Any]     =   SqueezeBertForMultipleChoice(config=lowerCamelCase__       )
        model.to(lowerCamelCase__       )
        model.eval()
        a__	:  Tuple     =   input_ids.unsqueeze(1       ).expand(-1				,				self.num_choices				,				-1       ).contiguous()
        a__	:  Tuple     =   input_mask.unsqueeze(1       ).expand(-1				,				self.num_choices				,				-1       ).contiguous()
        a__	:  List[Any]     =   model(
            lowerCamelCase__				,				attention_mask=lowerCamelCase__				,				labels=lowerCamelCase__				,				)
        self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_choices)       )

  def    _UpperCamelCase( self			:  Any       ):
        a__	:  Optional[Any]     =   self.prepare_config_and_inputs()
        ((a__),	(a__),	(a__),	(a__),	(a__),	(a__))	:  Any     =   config_and_inputs
        a__	:  Optional[int]     =   {"input_ids": input_ids, "attention_mask": input_mask}
        return config, inputs_dict




@require_torch
class A__     ( A__				,		A__				,		unittest.TestCase  ):



  """simple docstring"""



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

  def    _UpperCamelCase( self			:  int       ):
        a__	:  str     =   SqueezeBertModelTester(self       )
        a__	:  List[str]     =   ConfigTester(self				,				config_class=lowerCamelCase__				,				dim=37       )

  def    _UpperCamelCase( self			:  Optional[int]       ):
        self.config_tester.run_common_tests()

  def    _UpperCamelCase( self			:  List[Any]       ):
        a__	:  Optional[int]     =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_squeezebert_model(*lowerCamelCase__       )

  def    _UpperCamelCase( self			:  Optional[int]       ):
        a__	:  int     =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_squeezebert_for_masked_lm(*lowerCamelCase__       )

  def    _UpperCamelCase( self			:  Tuple       ):
        a__	:  Dict     =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_squeezebert_for_question_answering(*lowerCamelCase__       )

  def    _UpperCamelCase( self			:  Dict       ):
        a__	:  Tuple     =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_squeezebert_for_sequence_classification(*lowerCamelCase__       )

  def    _UpperCamelCase( self			:  Tuple       ):
        a__	:  str     =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_squeezebert_for_token_classification(*lowerCamelCase__       )

  def    _UpperCamelCase( self			:  str       ):
        a__	:  Tuple     =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_squeezebert_for_multiple_choice(*lowerCamelCase__       )

  @slow
  def    _UpperCamelCase( self			:  Any       ):
        for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              a__	:  List[Any]     =   SqueezeBertModel.from_pretrained(lowerCamelCase__       )
              self.assertIsNotNone(lowerCamelCase__       )




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



  """simple docstring"""



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

        a__	:  Optional[Any]     =   torch.tensor([[1, 29_414, 232, 328, 740, 1_140, 12_695, 69, 13, 1_588, 2]]       )
        a__	:  Any     =   model(lowerCamelCase__       )[0]
        a__	:  str     =   torch.Size((1, 3)       )
        self.assertEqual(output.shape				,				lowerCamelCase__       )
        a__	:  List[str]     =   torch.tensor([[0.6401, -0.0349, -0.6041]]       )
        self.assertTrue(torch.allclose(lowerCamelCase__				,				lowerCamelCase__				,				atol=1E-4       )       )




from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_UpperCAmelCase						:  Union[str, Any]									=			logging.get_logger(__name__)

_UpperCAmelCase						:  Dict									=			{
    """microsoft/resnet-50""": """https://huggingface.co/microsoft/resnet-50/blob/main/config.json""",
}







class 					lowercase							(			lowercase_      ,    lowercase_     ):
				__SCREAMING_SNAKE_CASE   :					List[str]			     =     '''resnet'''
				__SCREAMING_SNAKE_CASE   :					Dict			     =     ['''basic''', '''bottleneck''']




				def __init__(   self ,  snake_case=3 ,  snake_case=64 ,  snake_case=[256, 512, 1024, 2048] ,  snake_case=[3, 4, 6, 3] ,  snake_case="bottleneck" ,  snake_case="relu" ,  snake_case=False ,  snake_case=None ,  snake_case=None ,  **snake_case ,  ):
											super().__init__(**snake_case			)
											if layer_type not in self.layer_types:
																		raise ValueError(F'''layer_type={layer_type} is not one of {",".join(self.layer_types			)}'''			)
											snake_case_     							=       num_channels
											snake_case_     							=       embedding_size
											snake_case_     							=       hidden_sizes
											snake_case_     							=       depths
											snake_case_     							=       layer_type
											snake_case_     							=       hidden_act
											snake_case_     							=       downsample_in_first_stage
											snake_case_     							=       ['stem'] + [F'''stage{idx}''' for idx in range(1 ,  len(snake_case			) + 1			)]
											snake_case_							,       snake_case_     							=       get_aligned_output_features_output_indices(
											    out_features=snake_case ,  out_indices=snake_case ,  stage_names=self.stage_names			)







class 					lowercase							(			lowercase_     ):
				__SCREAMING_SNAKE_CASE   :					List[Any]			     =     version.parse('''1.11'''     )




				@property
				def        a				(   self			):
											return OrderedDict(
											    [
											        ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
											    ]			)




				@property
				def        a				(   self			):
											return 1e-3




