






import inspect
import unittest

from transformers import ViTMSNConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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


if is_torch_available():
   import torch
   from torch import nn

   from transformers import ViTMSNForImageClassification, ViTMSNModel
   from transformers.models.vit_msn.modeling_vit_msn import VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import ViTImageProcessor




class     _lowerCAmelCase:





       """simple docstring"""







       def __init__( self  ,      _lowerCamelCase  ,      _lowerCamelCase=1_3  ,      _lowerCamelCase=3_0  ,      _lowerCamelCase=2  ,      _lowerCamelCase=3  ,      _lowerCamelCase=True  ,      _lowerCamelCase=True  ,      _lowerCamelCase=3_2  ,      _lowerCamelCase=5  ,      _lowerCamelCase=4  ,      _lowerCamelCase=3_7  ,      _lowerCamelCase="gelu"  ,      _lowerCamelCase=0.1  ,      _lowerCamelCase=0.1  ,      _lowerCamelCase=1_0  ,      _lowerCamelCase=0.0_2  ,      _lowerCamelCase=None  ,      ):
         UpperCamelCase_: Optional[Any]   				=    parent
         UpperCamelCase_: Union[str, Any]   				=    batch_size
         UpperCamelCase_: Optional[int]   				=    image_size
         UpperCamelCase_: List[str]   				=    patch_size
         UpperCamelCase_: Optional[int]   				=    num_channels
         UpperCamelCase_: Tuple   				=    is_training
         UpperCamelCase_: Optional[int]   				=    use_labels
         UpperCamelCase_: str   				=    hidden_size
         UpperCamelCase_: Optional[Any]   				=    num_hidden_layers
         UpperCamelCase_: List[str]   				=    num_attention_heads
         UpperCamelCase_: List[str]   				=    intermediate_size
         UpperCamelCase_: Optional[Any]   				=    hidden_act
         UpperCamelCase_: List[Any]   				=    hidden_dropout_prob
         UpperCamelCase_: List[Any]   				=    attention_probs_dropout_prob
         UpperCamelCase_: int   				=    type_sequence_label_size
         UpperCamelCase_: int   				=    initializer_range
         UpperCamelCase_: Tuple   				=    scope

         # in ViT MSN, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
         UpperCamelCase_: List[Any]   				=    (image_size // patch_size) ** 2
         UpperCamelCase_: int   				=    num_patches + 1





       def       _a   ( self							):
         UpperCamelCase_: List[Any]   				=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

         UpperCamelCase_: List[Any]   				=    None
         if self.use_labels:
           UpperCamelCase_: List[str]   				=    ids_tensor([self.batch_size]  ,      self.type_sequence_label_size							)

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

         return config, pixel_values, labels





       def       _a   ( self							):
         return ViTMSNConfig(
             image_size=self.image_size  ,      patch_size=self.patch_size  ,      num_channels=self.num_channels  ,      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  ,      initializer_range=self.initializer_range  ,      )





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: Optional[int]   				=    ViTMSNModel(config=_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()
         UpperCamelCase_: Any   				=    model(_lowerCamelCase							)
         self.parent.assertEqual(result.last_hidden_state.shape  ,      (self.batch_size, self.seq_length, self.hidden_size)							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: Union[str, Any]   				=    self.type_sequence_label_size
         UpperCamelCase_: List[Any]   				=    ViTMSNForImageClassification(_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()
         UpperCamelCase_: Tuple   				=    model(_lowerCamelCase  ,      labels=_lowerCamelCase							)
         print('Pixel and labels shape: {pixel_values.shape}, {labels.shape}'							)
         print('Labels: {labels}'							)
         self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.type_sequence_label_size)							)

         # test greyscale images
         UpperCamelCase_: List[str]   				=    1
         UpperCamelCase_: Optional[Any]   				=    ViTMSNForImageClassification(_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()

         UpperCamelCase_: Dict   				=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
         UpperCamelCase_: Any   				=    model(_lowerCamelCase							)
         self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.type_sequence_label_size)							)





       def       _a   ( self							):
         UpperCamelCase_: Optional[int]   				=    self.prepare_config_and_inputs()
         UpperCamelCase_				,UpperCamelCase_				,UpperCamelCase_: str   				=    config_and_inputs
         UpperCamelCase_: Dict   				=    {'pixel_values': pixel_values}
         return config, inputs_dict







@require_torch
class     _lowerCAmelCase(	UpperCAmelCase_							,     UpperCAmelCase_							,     unittest.TestCase ):





       """simple docstring"""







       a    :		str									=(ViTMSNModel, ViTMSNForImageClassification) if is_torch_available() else ()
       a    :		Union[str, Any]									=(
           {'''feature-extraction''': ViTMSNModel, '''image-classification''': ViTMSNForImageClassification}
           if is_torch_available()
           else {}
       )

       a    :		Union[str, Any]									=False
       a    :		Dict									=False
       a    :		Union[str, Any]									=False
       a    :		List[str]									=False





       def       _a   ( self							):
         UpperCamelCase_: str   				=    ViTMSNModelTester(self							)
         UpperCamelCase_: Tuple   				=    ConfigTester(self  ,      config_class=_lowerCamelCase  ,      has_text_modality=_lowerCamelCase  ,      hidden_size=3_7							)





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





       @unittest.skip(reason='ViTMSN does not use inputs_embeds'							)
       def       _a   ( self							):
         pass





       def       _a   ( self							):
         UpperCamelCase_				,UpperCamelCase_: List[str]   				=    self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           UpperCamelCase_: int   				=    model_class(_lowerCamelCase							)
           self.assertIsInstance(model.get_input_embeddings()  ,      (nn.Module)							)
           UpperCamelCase_: Any   				=    model.get_output_embeddings()
           self.assertTrue(x is None or isinstance(_lowerCamelCase  ,      nn.Linear							)							)





       def       _a   ( self							):
         UpperCamelCase_				,UpperCamelCase_: Dict   				=    self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           UpperCamelCase_: Tuple   				=    model_class(_lowerCamelCase							)
           UpperCamelCase_: List[Any]   				=    inspect.signature(model.forward							)
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           UpperCamelCase_: Dict   				=    [*signature.parameters.keys()]

           UpperCamelCase_: Tuple   				=    ['pixel_values']
           self.assertListEqual(arg_names[:1]  ,      _lowerCamelCase							)





       def       _a   ( self							):
         UpperCamelCase_: Dict   				=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowerCamelCase							)





       def       _a   ( self							):
         UpperCamelCase_: str   				=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase							)





       @slow
       def       _a   ( self							):
         for model_name in VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase_: str   				=    ViTMSNModel.from_pretrained(_lowerCamelCase							)
           self.assertIsNotNone(_lowerCamelCase							)







def   snake_case ()	->    str:
  UpperCamelCase_: List[Any]   				=    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
  return image




@require_torch
@require_vision
class     _lowerCAmelCase(	unittest.TestCase ):





       """simple docstring"""







       @cached_property
       def       _a   ( self							):
         return ViTImageProcessor.from_pretrained('facebook/vit-msn-small'							) if is_vision_available() else None





       @slow
       def       _a   ( self							):
         torch.manual_seed(2							)
         UpperCamelCase_: str   				=    ViTMSNForImageClassification.from_pretrained('facebook/vit-msn-small'							).to(_lowerCamelCase							)

         UpperCamelCase_: Any   				=    self.default_image_processor
         UpperCamelCase_: List[Any]   				=    prepare_img()
         UpperCamelCase_: int   				=    image_processor(images=_lowerCamelCase  ,      return_tensors='pt'							).to(_lowerCamelCase							)

         # forward pass
         with torch.no_grad():
           UpperCamelCase_: Union[str, Any]   				=    model(**_lowerCamelCase							)

         # verify the logits
         UpperCamelCase_: str   				=    torch.Size((1, 1_0_0_0)							)
         self.assertEqual(outputs.logits.shape  ,      _lowerCamelCase							)

         UpperCamelCase_: str   				=    torch.tensor([-0.0_8_0_3, -0.4_4_5_4, -0.2_3_7_5]							).to(_lowerCamelCase							)

         self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,      _lowerCamelCase  ,      atol=1e-4							)							)







from typing import TYPE_CHECKING

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


A_		:		Tuple		     =			{
    'configuration_funnel': ['FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FunnelConfig'],
    'convert_funnel_original_tf_checkpoint_to_pytorch': [],
    'tokenization_funnel': ['FunnelTokenizer'],
}

try:
   if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   A_		:		List[str]		     =			['FunnelTokenizerFast']

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   A_		:		List[str]		     =			[
       'FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST',
       'FunnelBaseModel',
       'FunnelForMaskedLM',
       'FunnelForMultipleChoice',
       'FunnelForPreTraining',
       'FunnelForQuestionAnswering',
       'FunnelForSequenceClassification',
       'FunnelForTokenClassification',
       'FunnelModel',
       'FunnelPreTrainedModel',
       'load_tf_weights_in_funnel',
   ]

try:
   if not is_tf_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   A_		:		str		     =			[
       'TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST',
       'TFFunnelBaseModel',
       'TFFunnelForMaskedLM',
       'TFFunnelForMultipleChoice',
       'TFFunnelForPreTraining',
       'TFFunnelForQuestionAnswering',
       'TFFunnelForSequenceClassification',
       'TFFunnelForTokenClassification',
       'TFFunnelModel',
       'TFFunnelPreTrainedModel',
   ]


if TYPE_CHECKING:
   from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig
   from .tokenization_funnel import FunnelTokenizer

   try:
      if not is_tokenizers_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_funnel_fast import FunnelTokenizerFast

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_funnel import (
          FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
          FunnelBaseModel,
          FunnelForMaskedLM,
          FunnelForMultipleChoice,
          FunnelForPreTraining,
          FunnelForQuestionAnswering,
          FunnelForSequenceClassification,
          FunnelForTokenClassification,
          FunnelModel,
          FunnelPreTrainedModel,
          load_tf_weights_in_funnel,
      )

   try:
      if not is_tf_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_tf_funnel import (
          TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFFunnelBaseModel,
          TFFunnelForMaskedLM,
          TFFunnelForMultipleChoice,
          TFFunnelForPreTraining,
          TFFunnelForQuestionAnswering,
          TFFunnelForSequenceClassification,
          TFFunnelForTokenClassification,
          TFFunnelModel,
          TFFunnelPreTrainedModel,
      )

else:
   import sys

   A_		:		Any		     =			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)