





'''simple docstring'''


import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_gpu,
    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 ViTForImageClassification, ViTForMaskedImageModeling, ViTModel
 from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import ViTImageProcessor
class       a_   :





     def __init__( self		:					str	,      lowercase		:					Dict	,      lowercase		:					Tuple=13	,      lowercase		:					int=30	,      lowercase		:					Any=2	,      lowercase		:					int=3	,      lowercase		:					Dict=True	,      lowercase		:					Tuple=True	,      lowercase		:					Optional[Any]=32	,      lowercase		:					List[str]=5	,      lowercase		:					Dict=4	,      lowercase		:					Optional[int]=37	,      lowercase		:					int="gelu"	,      lowercase		:					List[Any]=0.1	,      lowercase		:					Union[str, Any]=0.1	,      lowercase		:					Any=10	,      lowercase		:					List[Any]=0.02	,      lowercase		:					Union[str, Any]=None	,      lowercase		:					Optional[Any]=2	,      ):





        """simple docstring"""




        lowercase_			:List[str]    			= parent
        lowercase_			:Optional[int]    			= batch_size
        lowercase_			:int    			= image_size
        lowercase_			:List[str]    			= patch_size
        lowercase_			:Optional[int]    			= num_channels
        lowercase_			:Union[str, Any]    			= is_training
        lowercase_			:Dict    			= use_labels
        lowercase_			:Union[str, Any]    			= hidden_size
        lowercase_			:Tuple    			= num_hidden_layers
        lowercase_			:Union[str, Any]    			= num_attention_heads
        lowercase_			:List[Any]    			= intermediate_size
        lowercase_			:Optional[Any]    			= hidden_act
        lowercase_			:List[Any]    			= hidden_dropout_prob
        lowercase_			:List[str]    			= attention_probs_dropout_prob
        lowercase_			:Optional[int]    			= type_sequence_label_size
        lowercase_			:List[str]    			= initializer_range
        lowercase_			:Any    			= scope
        lowercase_			:Optional[int]    			= encoder_stride

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





     def 							lowercase__      ( self		:					Tuple						):





        """simple docstring"""




        lowercase_			:Optional[Any]    			= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

        lowercase_			:Dict    			= None
        if self.use_labels:
           lowercase_			:int    			= ids_tensor([self.batch_size]	,      self.type_sequence_label_size						)

        lowercase_			:int    			= self.get_config()

        return config, pixel_values, labels





     def 							lowercase__      ( self		:					List[Any]						):





        """simple docstring"""




        return ViTConfig(
            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	,      is_decoder=lowercase	,      initializer_range=self.initializer_range	,      encoder_stride=self.encoder_stride	,      )





     def 							lowercase__      ( self		:					List[Any]	,      lowercase		:					Dict	,      lowercase		:					Union[str, Any]	,      lowercase		:					List[str]						):





        """simple docstring"""




        lowercase_			:Tuple    			= ViTModel(config=lowercase						)
        model.to(lowercase						)
        model.eval()
        lowercase_			:Union[str, Any]    			= model(lowercase						)
        self.parent.assertEqual(result.last_hidden_state.shape	,      (self.batch_size, self.seq_length, self.hidden_size)						)





     def 							lowercase__      ( self		:					Dict	,      lowercase		:					Tuple	,      lowercase		:					Tuple	,      lowercase		:					str						):





        """simple docstring"""




        lowercase_			:Any    			= ViTForMaskedImageModeling(config=lowercase						)
        model.to(lowercase						)
        model.eval()
        lowercase_			:Optional[Any]    			= model(lowercase						)
        self.parent.assertEqual(
            result.reconstruction.shape	,      (self.batch_size, self.num_channels, self.image_size, self.image_size)						)

        # test greyscale images
        lowercase_			:Optional[int]    			= 1
        lowercase_			:Tuple    			= ViTForMaskedImageModeling(lowercase						)
        model.to(lowercase						)
        model.eval()

        lowercase_			:int    			= floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
        lowercase_			:int    			= model(lowercase						)
        self.parent.assertEqual(result.reconstruction.shape	,      (self.batch_size, 1, self.image_size, self.image_size)						)





     def 							lowercase__      ( self		:					Union[str, Any]	,      lowercase		:					Any	,      lowercase		:					Optional[Any]	,      lowercase		:					Union[str, Any]						):





        """simple docstring"""




        lowercase_			:List[Any]    			= self.type_sequence_label_size
        lowercase_			:Optional[Any]    			= ViTForImageClassification(lowercase						)
        model.to(lowercase						)
        model.eval()
        lowercase_			:str    			= model(lowercase	,      labels=lowercase						)
        self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.type_sequence_label_size)						)

        # test greyscale images
        lowercase_			:Optional[Any]    			= 1
        lowercase_			:Optional[Any]    			= ViTForImageClassification(lowercase						)
        model.to(lowercase						)
        model.eval()

        lowercase_			:Any    			= floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
        lowercase_			:Tuple    			= model(lowercase						)
        self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.type_sequence_label_size)						)







     def 							lowercase__      ( self		:					Optional[int]						):





        """simple docstring"""




        lowercase_			:Any    			= self.prepare_config_and_inputs()
        (
            (
            lowercase_
        )			,							(
            lowercase_
        )			,							(
            lowercase_
        )			,							
        )			:List[str]    			= config_and_inputs
        lowercase_			:str    			= {"pixel_values": pixel_values}
        return config, inputs_dict







@require_torch
class       a_   (				_lowerCAmelCase	,						_lowerCAmelCase	,						unittest.TestCase							):
     __A      =			(
         (
             ViTModel,
             ViTForImageClassification,
             ViTForMaskedImageModeling,
         )
         if is_torch_available()
         else ()
     )
     __A      =			(
         {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification}
         if is_torch_available()
         else {}
     )
     __A      =			True

     __A      =			False
     __A      =			False
     __A      =			False





     def 							lowercase__      ( self		:					List[str]						):





        """simple docstring"""




        lowercase_			:Union[str, Any]    			= ViTModelTester(self						)
        lowercase_			:Any    			= ConfigTester(self	,      config_class=lowercase	,      has_text_modality=lowercase	,      hidden_size=37						)





     def 							lowercase__      ( self		:					Optional[int]						):





        """simple docstring"""




        self.config_tester.run_common_tests()





     @unittest.skip(reason="ViT does not use inputs_embeds"						)
     def 							lowercase__      ( self		:					Dict						):





        """simple docstring"""




        pass





     def 							lowercase__      ( self		:					Union[str, Any]						):





        """simple docstring"""




        lowercase_			,							lowercase_			:Tuple    			= self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
           lowercase_			:List[Any]    			= model_class(lowercase						)
           self.assertIsInstance(model.get_input_embeddings()	,      (nn.Module)						)
           lowercase_			:Dict    			= model.get_output_embeddings()
           self.assertTrue(x is None or isinstance(lowercase	,      nn.Linear						)						)





     def 							lowercase__      ( self		:					Optional[int]						):





        """simple docstring"""




        lowercase_			,							lowercase_			:List[Any]    			= self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
           lowercase_			:List[Any]    			= model_class(lowercase						)
           lowercase_			:Optional[int]    			= inspect.signature(model.forward						)
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           lowercase_			:List[str]    			= [*signature.parameters.keys()]

           lowercase_			:Any    			= ["pixel_values"]
           self.assertListEqual(arg_names[:1]	,      lowercase						)





     def 							lowercase__      ( self		:					Any						):





        """simple docstring"""




        lowercase_			:str    			= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowercase						)





     def 							lowercase__      ( self		:					Optional[int]						):





        """simple docstring"""




        lowercase_			:int    			= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_image_modeling(*lowercase						)





     def 							lowercase__      ( self		:					Dict						):





        """simple docstring"""




        lowercase_			:Dict    			= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowercase						)







     @slow
     def 							lowercase__      ( self		:					Optional[int]						):





        """simple docstring"""




        for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase_			:Tuple    			= ViTModel.from_pretrained(lowercase						)
           self.assertIsNotNone(lowercase						)







def 							UpperCAmelCase_  (						):
   lowercase_			:Union[str, Any]    			= Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
   return image


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





     @cached_property
     def 							lowercase__      ( self		:					Tuple						):





        """simple docstring"""




        return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224"						) if is_vision_available() else None





     @slow
     def 							lowercase__      ( self		:					Dict						):





        """simple docstring"""




        lowercase_			:int    			= ViTForImageClassification.from_pretrained("google/vit-base-patch16-224"						).to(lowercase						)

        lowercase_			:Optional[Any]    			= self.default_image_processor
        lowercase_			:Any    			= prepare_img()
        lowercase_			:int    			= image_processor(images=lowercase	,      return_tensors="pt"						).to(lowercase						)

        # forward pass
        with torch.no_grad():
           lowercase_			:Optional[int]    			= model(**lowercase						)

        # verify the logits
        lowercase_			:Optional[Any]    			= torch.Size((1, 1_000)						)
        self.assertEqual(outputs.logits.shape	,      lowercase						)

        lowercase_			:Union[str, Any]    			= torch.tensor([-0.27_44, 0.82_15, -0.08_36]						).to(lowercase						)

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





     @slow
     def 							lowercase__      ( self		:					str						):





        """simple docstring"""




        lowercase_			:Dict    			= ViTModel.from_pretrained("facebook/dino-vits8"						).to(lowercase						)

        lowercase_			:Any    			= ViTImageProcessor.from_pretrained("facebook/dino-vits8"	,      size=480						)
        lowercase_			:Tuple    			= prepare_img()
        lowercase_			:Optional[Any]    			= image_processor(images=lowercase	,      return_tensors="pt"						)
        lowercase_			:List[Any]    			= inputs.pixel_values.to(lowercase						)

        # forward pass
        with torch.no_grad():
           lowercase_			:Union[str, Any]    			= model(lowercase	,      interpolate_pos_encoding=lowercase						)

        # verify the logits
        lowercase_			:Optional[Any]    			= torch.Size((1, 3_601, 384)						)
        self.assertEqual(outputs.last_hidden_state.shape	,      lowercase						)

        lowercase_			:Tuple    			= torch.tensor(
            [[4.23_40, 4.39_06, -6.66_92], [4.54_63, 1.89_28, -6.72_57], [4.44_29, 0.84_96, -5.85_85]]						).to(lowercase						)

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







     @slow
     @require_accelerate
     @require_torch_gpu
     def 							lowercase__      ( self		:					Optional[Any]						):





        """simple docstring"""




        lowercase_			:int    			= ViTModel.from_pretrained("facebook/dino-vits8"	,      torch_dtype=torch.floataa	,      device_map="auto"						)
        lowercase_			:List[str]    			= self.default_image_processor

        lowercase_			:str    			= prepare_img()
        lowercase_			:List[Any]    			= image_processor(images=lowercase	,      return_tensors="pt"						)
        lowercase_			:Any    			= inputs.pixel_values.to(lowercase						)

        # forward pass to make sure inference works in fp16
        with torch.no_grad():
           lowercase_			:Any    			= model(lowercase						)









'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowerCAmelCase					:     Optional[Any]												={
    '''configuration_nllb_moe''': [
        '''NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''NllbMoeConfig''',
    ]
}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCAmelCase					:     Tuple												=[
     '''NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''NllbMoeForConditionalGeneration''',
     '''NllbMoeModel''',
     '''NllbMoePreTrainedModel''',
     '''NllbMoeTop2Router''',
     '''NllbMoeSparseMLP''',
 ]


if TYPE_CHECKING:
 from .configuration_nllb_moe import (
     NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP,
     NllbMoeConfig,
 )

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_nllb_moe import (
      NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST,
      NllbMoeForConditionalGeneration,
      NllbMoeModel,
      NllbMoePreTrainedModel,
      NllbMoeSparseMLP,
      NllbMoeTopaRouter,
  )


else:
 import sys

 lowerCAmelCase					:     Tuple												=_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


