






"""simple docstring"""





import unittest

from transformers import YosoConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import (
      YosoForMaskedLM,
      YosoForMultipleChoice,
      YosoForQuestionAnswering,
      YosoForSequenceClassification,
      YosoForTokenClassification,
      YosoModel,
  )
  from transformers.models.yoso.modeling_yoso import YOSO_PRETRAINED_MODEL_ARCHIVE_LIST
class  __lowerCamelCase     :

   def __init__(       self			,					snake_case_			,					snake_case_=13			,					snake_case_=7			,					snake_case_=True			,					snake_case_=True			,					snake_case_=True			,					snake_case_=True			,					snake_case_=99			,					snake_case_=32			,					snake_case_=5			,					snake_case_=4			,					snake_case_=37			,					snake_case_="gelu"			,					snake_case_=0.1			,					snake_case_=0.1			,					snake_case_=512			,					snake_case_=16			,					snake_case_=2			,					snake_case_=0.02			,					snake_case_=3			,					snake_case_=4			,					snake_case_=None			,					)		->							List[str]:
       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

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Union[str, Any]:
       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
       if self.use_token_type_ids:
           UpperCamelCase__								=     ids_tensor([self.batch_size, self.seq_length]			,					self.type_vocab_size   )

       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, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							List[str]:
       return YosoConfig(
           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			,					hidden_dropout_prob=self.hidden_dropout_prob			,					attention_probs_dropout_prob=self.attention_probs_dropout_prob			,					max_position_embeddings=self.max_position_embeddings			,					type_vocab_size=self.type_vocab_size			,					is_decoder=_UpperCAmelCase			,					initializer_range=self.initializer_range			,					)

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Optional[Any]:
       UpperCamelCase__								=     self.get_config()
       UpperCamelCase__								=     300
       return config

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Tuple:
       (
           (
           UpperCamelCase__    
       )							,    (
           UpperCamelCase__    
       )							,    (
           UpperCamelCase__    
       )							,    (
           UpperCamelCase__    
       )							,    (
           UpperCamelCase__    
       )							,    (
           UpperCamelCase__    
       )							,    (
           UpperCamelCase__    
       )							,        
       )								=     self.prepare_config_and_inputs()

       UpperCamelCase__								=     True
       UpperCamelCase__								=     floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
       UpperCamelCase__								=     ids_tensor([self.batch_size, self.seq_length]			,					vocab_size=2   )

       return (
           config,
           input_ids,
           token_type_ids,
           input_mask,
           sequence_labels,
           token_labels,
           choice_labels,
           encoder_hidden_states,
           encoder_attention_mask,
       )

   def     SCREAMING_SNAKE_CASE__			(       self			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_   )		->							str:
       UpperCamelCase__								=     YosoModel(config=_UpperCAmelCase   )
       model.to(_UpperCAmelCase   )
       model.eval()
       UpperCamelCase__								=     model(_UpperCAmelCase			,					attention_mask=_UpperCAmelCase			,					token_type_ids=_UpperCAmelCase   )
       UpperCamelCase__								=     model(_UpperCAmelCase			,					token_type_ids=_UpperCAmelCase   )
       UpperCamelCase__								=     model(_UpperCAmelCase   )
       self.parent.assertEqual(result.last_hidden_state.shape			,					(self.batch_size, self.seq_length, self.hidden_size)   )

   def     SCREAMING_SNAKE_CASE__			(       self			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					)		->							Optional[int]:
       UpperCamelCase__								=     True
       UpperCamelCase__								=     YosoModel(_UpperCAmelCase   )
       model.to(_UpperCAmelCase   )
       model.eval()
       UpperCamelCase__								=     model(
           _UpperCAmelCase			,					attention_mask=_UpperCAmelCase			,					token_type_ids=_UpperCAmelCase			,					encoder_hidden_states=_UpperCAmelCase			,					encoder_attention_mask=_UpperCAmelCase			,					)
       UpperCamelCase__								=     model(
           _UpperCAmelCase			,					attention_mask=_UpperCAmelCase			,					token_type_ids=_UpperCAmelCase			,					encoder_hidden_states=_UpperCAmelCase			,					)
       UpperCamelCase__								=     model(_UpperCAmelCase			,					attention_mask=_UpperCAmelCase			,					token_type_ids=_UpperCAmelCase   )
       self.parent.assertEqual(result.last_hidden_state.shape			,					(self.batch_size, self.seq_length, self.hidden_size)   )

   def     SCREAMING_SNAKE_CASE__			(       self			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_   )		->							List[str]:
       UpperCamelCase__								=     YosoForMaskedLM(config=_UpperCAmelCase   )
       model.to(_UpperCAmelCase   )
       model.eval()
       UpperCamelCase__								=     model(_UpperCAmelCase			,					attention_mask=_UpperCAmelCase			,					token_type_ids=_UpperCAmelCase			,					labels=_UpperCAmelCase   )
       self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.seq_length, self.vocab_size)   )

   def     SCREAMING_SNAKE_CASE__			(       self			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_   )		->							Union[str, Any]:
       UpperCamelCase__								=     YosoForQuestionAnswering(config=_UpperCAmelCase   )
       model.to(_UpperCAmelCase   )
       model.eval()
       UpperCamelCase__								=     model(
           _UpperCAmelCase			,					attention_mask=_UpperCAmelCase			,					token_type_ids=_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     SCREAMING_SNAKE_CASE__			(       self			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_   )		->							Union[str, Any]:
       UpperCamelCase__								=     self.num_labels
       UpperCamelCase__								=     YosoForSequenceClassification(_UpperCAmelCase   )
       model.to(_UpperCAmelCase   )
       model.eval()
       UpperCamelCase__								=     model(_UpperCAmelCase			,					attention_mask=_UpperCAmelCase			,					token_type_ids=_UpperCAmelCase			,					labels=_UpperCAmelCase   )
       self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.num_labels)   )

   def     SCREAMING_SNAKE_CASE__			(       self			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_   )		->							Optional[Any]:
       UpperCamelCase__								=     self.num_labels
       UpperCamelCase__								=     YosoForTokenClassification(config=_UpperCAmelCase   )
       model.to(_UpperCAmelCase   )
       model.eval()
       UpperCamelCase__								=     model(_UpperCAmelCase			,					attention_mask=_UpperCAmelCase			,					token_type_ids=_UpperCAmelCase			,					labels=_UpperCAmelCase   )
       self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.seq_length, self.num_labels)   )

   def     SCREAMING_SNAKE_CASE__			(       self			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_			,					snake_case_   )		->							Any:
       UpperCamelCase__								=     self.num_choices
       UpperCamelCase__								=     YosoForMultipleChoice(config=_UpperCAmelCase   )
       model.to(_UpperCAmelCase   )
       model.eval()
       UpperCamelCase__								=     input_ids.unsqueeze(1   ).expand(-1			,					self.num_choices			,					-1   ).contiguous()
       UpperCamelCase__								=     token_type_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			,					token_type_ids=_UpperCAmelCase			,					labels=_UpperCAmelCase			,					)
       self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.num_choices)   )







   def     SCREAMING_SNAKE_CASE__			(       self   )		->							List[str]:
       UpperCamelCase__								=     self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase__    
       )							,    (
           UpperCamelCase__    
       )							,    (
           UpperCamelCase__    
       )							,    (
           UpperCamelCase__    
       )							,    (
           UpperCamelCase__    
       )							,    (
           UpperCamelCase__    
       )							,    (
           UpperCamelCase__    
       )							,        
       )								=     config_and_inputs
       UpperCamelCase__								=     {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
       return config, inputs_dict







@require_torch
class  __lowerCamelCase     (	_a				,						_a				,						unittest.TestCase	):
   a   :					Optional[Any]			    =(
       (
           YosoModel,
           YosoForMaskedLM,
           YosoForMultipleChoice,
           YosoForQuestionAnswering,
           YosoForSequenceClassification,
           YosoForTokenClassification,
       )
       if is_torch_available()
       else ()
   )
   a   :					Dict			    =False
   a   :					Tuple			    =False
   a   :					Optional[Any]			    =False

   a   :					List[Any]			    =()
   a   :					Any			    =(
       {
           """feature-extraction""": YosoModel,
           """fill-mask""": YosoForMaskedLM,
           """question-answering""": YosoForQuestionAnswering,
           """text-classification""": YosoForSequenceClassification,
           """token-classification""": YosoForTokenClassification,
           """zero-shot""": YosoForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Optional[Any]:
       UpperCamelCase__								=     YosoModelTester(self   )
       UpperCamelCase__								=     ConfigTester(self			,					config_class=_UpperCAmelCase			,					hidden_size=37   )

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							List[str]:
       self.config_tester.run_common_tests()

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Any:
       UpperCamelCase__								=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_UpperCAmelCase   )

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Union[str, Any]:
       UpperCamelCase__								=     self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
           UpperCamelCase__								=     type
           self.model_tester.create_and_check_model(*_UpperCAmelCase   )

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Optional[Any]:
       UpperCamelCase__								=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*_UpperCAmelCase   )

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Dict:
       UpperCamelCase__								=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*_UpperCAmelCase   )

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							List[Any]:
       UpperCamelCase__								=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase   )

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Optional[Any]:
       UpperCamelCase__								=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase   )

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Tuple:
       UpperCamelCase__								=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase   )

   @slow
   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Union[str, Any]:



       for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__								=     YosoModel.from_pretrained(_UpperCAmelCase   )
           self.assertIsNotNone(_UpperCAmelCase   )







   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Dict:
       return







@require_torch
class  __lowerCamelCase     (	unittest.TestCase	):

   @slow
   def     SCREAMING_SNAKE_CASE__			(       self   )		->							List[Any]:
       UpperCamelCase__								=     YosoModel.from_pretrained('uw-madison/yoso-4096'   )
       UpperCamelCase__								=     torch.tensor([[0, 1, 2, 3, 4, 5]]   )

       with torch.no_grad():
           UpperCamelCase__								=     model(_UpperCAmelCase   )[0]

       UpperCamelCase__								=     torch.Size((1, 6, 768)   )
       self.assertEqual(output.shape			,					_UpperCAmelCase   )

       UpperCamelCase__								=     torch.tensor(
           [[[-0.0_611, 0.1_242, 0.0_840], [0.0_280, -0.0_048, 0.1_125], [0.0_106, 0.0_226, 0.0_751]]]   )

       self.assertTrue(torch.allclose(output[:, :3, :3]			,					_UpperCAmelCase			,					atol=1E-4   )   )

   @slow
   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Any:
       UpperCamelCase__								=     YosoForMaskedLM.from_pretrained('uw-madison/yoso-4096'   )
       UpperCamelCase__								=     torch.tensor([[0, 1, 2, 3, 4, 5]]   )

       with torch.no_grad():
           UpperCamelCase__								=     model(_UpperCAmelCase   )[0]

       UpperCamelCase__								=     5_0265

       UpperCamelCase__								=     torch.Size((1, 6, vocab_size)   )
       self.assertEqual(output.shape			,					_UpperCAmelCase   )

       UpperCamelCase__								=     torch.tensor(
           [[[-2.1_313, -3.7_285, -2.2_407], [-2.7_047, -3.3_314, -2.6_408], [0.0_629, -2.5_166, -0.3_356]]]   )

       self.assertTrue(torch.allclose(output[:, :3, :3]			,					_UpperCAmelCase			,					atol=1E-4   )   )







   @slow
   def     SCREAMING_SNAKE_CASE__			(       self   )		->							int:
       UpperCamelCase__								=     YosoForMaskedLM.from_pretrained('uw-madison/yoso-4096'   )
       UpperCamelCase__								=     torch.arange(4096   ).unsqueeze(0   )

       with torch.no_grad():
           UpperCamelCase__								=     model(_UpperCAmelCase   )[0]

       UpperCamelCase__								=     5_0265

       UpperCamelCase__								=     torch.Size((1, 4096, vocab_size)   )
       self.assertEqual(output.shape			,					_UpperCAmelCase   )

       UpperCamelCase__								=     torch.tensor(
           [[[-2.3_914, -4.3_742, -5.0_956], [-4.0_988, -4.2_384, -7.0_406], [-3.1_427, -3.7_192, -6.6_800]]]   )

       self.assertTrue(torch.allclose(output[:, :3, :3]			,					_UpperCAmelCase			,					atol=1E-4   )   )





"""simple docstring"""


import copy
from typing import Dict

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import CONFIG_MAPPING
from ..bit import BitConfig


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

lowerCAmelCase_:       Dict			     =       {
    "google/vit-hybrid-base-bit-384": "https://huggingface.co/vit-hybrid-base-bit-384/resolve/main/config.json",
    # See all ViT hybrid models at https://huggingface.co/models?filter=vit
}






class  a__     ( _a						):
   snake_case_				=   "vit-hybrid"


   def __init__(  self,					_UpperCAmelCase=None,					_UpperCAmelCase=768,					_UpperCAmelCase=12,					_UpperCAmelCase=12,					_UpperCAmelCase=3072,					_UpperCAmelCase="gelu",					_UpperCAmelCase=0.0,					_UpperCAmelCase=0.0,					_UpperCAmelCase=0.02,					_UpperCAmelCase=1E-12,					_UpperCAmelCase=224,					_UpperCAmelCase=1,					_UpperCAmelCase=3,					_UpperCAmelCase=[1, 1024, 24, 24],					_UpperCAmelCase=True,					**_UpperCAmelCase,					):
        '''simple docstring'''



        super().__init__(**_UpperCAmelCase	)

        if backbone_config is None:
             logger.info("`backbone_config` is `None`. Initializing the config with a `BiT` backbone."	)
             lowercase__								=				{
                 "global_padding": "same",
                 "layer_type": "bottleneck",
                 "depths": [3, 4, 9],
                 "out_features": ["stage3"],
                 "embedding_dynamic_padding": True,
             }

        if isinstance(_UpperCAmelCase,					_UpperCAmelCase	):
             if "model_type" in backbone_config:
                  lowercase__								=				CONFIG_MAPPING[backbone_config["model_type"]]
             else:
                  logger.info(
                      "`model_type` is not found in `backbone_config`. Use `Bit` as the backbone configuration class."	)
                  lowercase__								=				BitConfig
             lowercase__								=				backbone_config_class(**_UpperCAmelCase	)

        lowercase__								=				backbone_featmap_shape
        lowercase__								=				backbone_config
        lowercase__								=				hidden_size
        lowercase__								=				num_hidden_layers
        lowercase__								=				num_attention_heads
        lowercase__								=				intermediate_size
        lowercase__								=				hidden_act
        lowercase__								=				hidden_dropout_prob
        lowercase__								=				attention_probs_dropout_prob
        lowercase__								=				initializer_range
        lowercase__								=				layer_norm_eps
        lowercase__								=				image_size
        lowercase__								=				patch_size
        lowercase__								=				num_channels
        lowercase__								=				qkv_bias


   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				copy.deepcopy(self.__dict__	)
        lowercase__								=				self.backbone_config.to_dict()
        lowercase__								=				self.__class__.model_type
        return output




