






'''simple docstring'''



import inspect
import unittest

from transformers import BitConfig
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_backbone_common import BackboneTesterMixin
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 BitBackbone, BitForImageClassification, BitImageProcessor, BitModel
      from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image
class UpperCAmelCase_					:




      """simple docstring"""




      def __init__( self			:		int     ,      UpperCAmelCase			:		Optional[int]     ,      UpperCAmelCase			:		int=3     ,      UpperCAmelCase			:		Dict=32     ,      UpperCAmelCase			:		Dict=3     ,      UpperCAmelCase			:		str=10     ,      UpperCAmelCase			:		Any=[8, 16, 32, 64]     ,      UpperCAmelCase			:		int=[1, 1, 2, 1]     ,      UpperCAmelCase			:		Union[str, Any]=True     ,      UpperCAmelCase			:		Union[str, Any]=True     ,      UpperCAmelCase			:		Tuple="relu"     ,      UpperCAmelCase			:		List[str]=3     ,      UpperCAmelCase			:		Union[str, Any]=None     ,      UpperCAmelCase			:		Optional[int]=["stage2", "stage3", "stage4"]     ,      UpperCAmelCase			:		int=[2, 3, 4]     ,      UpperCAmelCase			:		Tuple=1     ,      )  ->  Union[str, Any]:


       '''simple docstring'''

       lowercase					:   Optional[int]				  =parent
       lowercase					:   List[Any]				  =batch_size
       lowercase					:   str				  =image_size
       lowercase					:   List[Any]				  =num_channels
       lowercase					:   Union[str, Any]				  =embeddings_size
       lowercase					:   str				  =hidden_sizes
       lowercase					:   str				  =depths
       lowercase					:   Any				  =is_training
       lowercase					:   int				  =use_labels
       lowercase					:   List[Any]				  =hidden_act
       lowercase					:   Any				  =num_labels
       lowercase					:   Optional[Any]				  =scope
       lowercase					:   Optional[Any]				  =len(UpperCAmelCase		)
       lowercase					:   Optional[Any]				  =out_features
       lowercase					:   Dict				  =out_indices
       lowercase					:   List[str]				  =num_groups






      def 				A__      ( self			:		List[Any]		)  ->  Optional[Any]:


       '''simple docstring'''

       lowercase					:   List[Any]				  =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

       lowercase					:   Dict				  =None
       if self.use_labels:
        lowercase					:   Optional[Any]				  =ids_tensor([self.batch_size]     ,      self.num_labels		)

       lowercase					:   Union[str, Any]				  =self.get_config()

       return config, pixel_values, labels






      def 				A__      ( self			:		Tuple		)  ->  Tuple:


       '''simple docstring'''

       return BitConfig(
           num_channels=self.num_channels     ,      embeddings_size=self.embeddings_size     ,      hidden_sizes=self.hidden_sizes     ,      depths=self.depths     ,      hidden_act=self.hidden_act     ,      num_labels=self.num_labels     ,      out_features=self.out_features     ,      out_indices=self.out_indices     ,      num_groups=self.num_groups     ,      )






      def 				A__      ( self			:		Union[str, Any]     ,      UpperCAmelCase			:		List[str]     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		Any		)  ->  str:


       '''simple docstring'''

       lowercase					:   Union[str, Any]				  =BitModel(config=UpperCAmelCase		)
       model.to(UpperCAmelCase		)
       model.eval()
       lowercase					:   Dict				  =model(UpperCAmelCase		)
       self.parent.assertEqual(
           result.last_hidden_state.shape     ,      (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)     ,      )






      def 				A__      ( self			:		Optional[Any]     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		List[str]     ,      UpperCAmelCase			:		List[Any]		)  ->  str:


       '''simple docstring'''

       lowercase					:   Dict				  =self.num_labels
       lowercase					:   Dict				  =BitForImageClassification(UpperCAmelCase		)
       model.to(UpperCAmelCase		)
       model.eval()
       lowercase					:   List[Any]				  =model(UpperCAmelCase     ,      labels=UpperCAmelCase		)
       self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.num_labels)		)






      def 				A__      ( self			:		Tuple     ,      UpperCAmelCase			:		Union[str, Any]     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		Tuple		)  ->  Dict:


       '''simple docstring'''

       lowercase					:   Dict				  =BitBackbone(config=UpperCAmelCase		)
       model.to(UpperCAmelCase		)
       model.eval()
       lowercase					:   List[str]				  =model(UpperCAmelCase		)

       # verify feature maps
       self.parent.assertEqual(len(result.feature_maps		)     ,      len(config.out_features		)		)
       self.parent.assertListEqual(list(result.feature_maps[0].shape		)     ,      [self.batch_size, self.hidden_sizes[1], 4, 4]		)

       # verify channels
       self.parent.assertEqual(len(model.channels		)     ,      len(config.out_features		)		)
       self.parent.assertListEqual(model.channels     ,      config.hidden_sizes[1:]		)

       # verify backbone works with out_features=None
       lowercase					:   List[Any]				  =None
       lowercase					:   Union[str, Any]				  =BitBackbone(config=UpperCAmelCase		)
       model.to(UpperCAmelCase		)
       model.eval()
       lowercase					:   str				  =model(UpperCAmelCase		)

       # verify feature maps
       self.parent.assertEqual(len(result.feature_maps		)     ,      1		)
       self.parent.assertListEqual(list(result.feature_maps[0].shape		)     ,      [self.batch_size, self.hidden_sizes[-1], 1, 1]		)

       # verify channels
       self.parent.assertEqual(len(model.channels		)     ,      1		)
       self.parent.assertListEqual(model.channels     ,      [config.hidden_sizes[-1]]		)






      def 				A__      ( self			:		str		)  ->  int:


       '''simple docstring'''

       lowercase					:   Dict				  =self.prepare_config_and_inputs()
       lowercase  ,		lowercase  ,		lowercase					:   Any				  =config_and_inputs
       lowercase					:   Dict				  ={'''pixel_values''': pixel_values}
       return config, inputs_dict





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




      """simple docstring"""




      UpperCamelCase_								=			(BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else ()
      UpperCamelCase_								=			(
          {'''feature-extraction''': BitModel, '''image-classification''': BitForImageClassification}
          if is_torch_available()
          else {}
      )

      UpperCamelCase_								=			False
      UpperCamelCase_								=			False
      UpperCamelCase_								=			False
      UpperCamelCase_								=			False
      UpperCamelCase_								=			False






      def 				A__      ( self			:		List[str]		)  ->  List[str]:


       '''simple docstring'''

       lowercase					:   Dict				  =BitModelTester(self		)
       lowercase					:   List[Any]				  =ConfigTester(self     ,      config_class=UpperCAmelCase     ,      has_text_modality=UpperCAmelCase		)






      def 				A__      ( self			:		str		)  ->  Optional[Any]:


       '''simple docstring'''

       self.create_and_test_config_common_properties()
       self.config_tester.create_and_test_config_to_json_string()
       self.config_tester.create_and_test_config_to_json_file()
       self.config_tester.create_and_test_config_from_and_save_pretrained()
       self.config_tester.create_and_test_config_with_num_labels()
       self.config_tester.check_config_can_be_init_without_params()
       self.config_tester.check_config_arguments_init()






      def 				A__      ( self			:		Optional[int]		)  ->  Any:


       '''simple docstring'''

       return






      @unittest.skip(reason='''Bit does not output attentions'''		)
      def 				A__      ( self			:		int		)  ->  str:


       '''simple docstring'''

       pass






      @unittest.skip(reason='''Bit does not use inputs_embeds'''		)
      def 				A__      ( self			:		List[str]		)  ->  Tuple:


       '''simple docstring'''

       pass






      @unittest.skip(reason='''Bit does not support input and output embeddings'''		)
      def 				A__      ( self			:		Tuple		)  ->  Union[str, Any]:


       '''simple docstring'''

       pass






      def 				A__      ( self			:		str		)  ->  int:


       '''simple docstring'''

       lowercase  ,		lowercase					:   str				  =self.model_tester.prepare_config_and_inputs_for_common()

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

        lowercase					:   Optional[int]				  =['''pixel_values''']
        self.assertListEqual(arg_names[:1]     ,      UpperCAmelCase		)






      def 				A__      ( self			:		Any		)  ->  Optional[int]:


       '''simple docstring'''

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






      def 				A__      ( self			:		List[Any]		)  ->  Tuple:


       '''simple docstring'''

       lowercase					:   Optional[Any]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_backbone(*UpperCAmelCase		)






      def 				A__      ( self			:		Optional[Any]		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase  ,		lowercase					:   Optional[Any]				  =self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        lowercase					:   Optional[Any]				  =model_class(config=UpperCAmelCase		)
        for name, module in model.named_modules():
         if isinstance(UpperCAmelCase     ,      (nn.BatchNormad, nn.GroupNorm)		):
          self.assertTrue(
              torch.all(module.weight == 1		)     ,      msg=f'Parameter {name} of model {model_class} seems not properly initialized'     ,      )
          self.assertTrue(
              torch.all(module.bias == 0		)     ,      msg=f'Parameter {name} of model {model_class} seems not properly initialized'     ,      )






      def 				A__      ( self			:		Optional[Any]		)  ->  List[Any]:


       '''simple docstring'''

       def check_hidden_states_output(UpperCAmelCase			:		Union[str, Any]     ,      UpperCAmelCase			:		Dict     ,      UpperCAmelCase			:		Optional[int]		):
        lowercase					:   Dict				  =model_class(UpperCAmelCase		)
        model.to(UpperCAmelCase		)
        model.eval()

        with torch.no_grad():
         lowercase					:   Tuple				  =model(**self._prepare_for_class(UpperCAmelCase     ,      UpperCAmelCase		)		)

        lowercase					:   List[Any]				  =outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

        lowercase					:   Optional[Any]				  =self.model_tester.num_stages
        self.assertEqual(len(UpperCAmelCase		)     ,      expected_num_stages + 1		)

        # Bit's feature maps are of shape (batch_size, num_channels, height, width)
        self.assertListEqual(
            list(hidden_states[0].shape[-2:]		)     ,      [self.model_tester.image_size // 4, self.model_tester.image_size // 4]     ,      )

       lowercase  ,		lowercase					:   Tuple				  =self.model_tester.prepare_config_and_inputs_for_common()
       lowercase					:   str				  =['''preactivation''', '''bottleneck''']
       for model_class in self.all_model_classes:
        for layer_type in layers_type:
         lowercase					:   Union[str, Any]				  =layer_type
         lowercase					:   List[Any]				  =True
         check_hidden_states_output(UpperCAmelCase     ,      UpperCAmelCase     ,      UpperCAmelCase		)

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         lowercase					:   str				  =True

         check_hidden_states_output(UpperCAmelCase     ,      UpperCAmelCase     ,      UpperCAmelCase		)






      @unittest.skip(reason='''Bit does not use feedforward chunking'''		)
      def 				A__      ( self			:		Optional[Any]		)  ->  List[str]:


       '''simple docstring'''

       pass






      def 				A__      ( self			:		Any		)  ->  Any:


       '''simple docstring'''

       lowercase					:   Any				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase		)






      @slow
      def 				A__      ( self			:		Any		)  ->  int:


       '''simple docstring'''

       for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowercase					:   Union[str, Any]				  =BitModel.from_pretrained(UpperCAmelCase		)
        self.assertIsNotNone(UpperCAmelCase		)





def 							lowercase_				(    )       ->      Any:



 """simple docstring"""




 lowercase					:   Dict				  =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
 return image
@require_torch
@require_vision
class UpperCAmelCase_					(    unittest.TestCase				):




      """simple docstring"""




      @cached_property
      def 				A__      ( self			:		Optional[Any]		)  ->  List[Any]:


       '''simple docstring'''

       return (
           BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		) if is_vision_available() else None
       )






      @slow
      def 				A__      ( self			:		int		)  ->  int:


       '''simple docstring'''

       lowercase					:   Tuple				  =BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		).to(UpperCAmelCase		)

       lowercase					:   Union[str, Any]				  =self.default_image_processor
       lowercase					:   int				  =prepare_img()
       lowercase					:   Union[str, Any]				  =image_processor(images=UpperCAmelCase     ,      return_tensors='''pt'''		).to(UpperCAmelCase		)

       # forward pass
       with torch.no_grad():
        lowercase					:   Tuple				  =model(**UpperCAmelCase		)

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

       lowercase					:   List[Any]				  =torch.tensor([[-0.6_5_2_6, -0.5_2_6_3, -1.4_3_9_8]]		).to(UpperCAmelCase		)

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





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




      """simple docstring"""




      UpperCamelCase_								=			(BitBackbone,) if is_torch_available() else ()
      UpperCamelCase_								=			BitConfig

      UpperCamelCase_								=			False






      def 				A__      ( self			:		int		)  ->  Optional[Any]:


       '''simple docstring'''

       lowercase					:   Tuple				  =BitModelTester(self		)











'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


SCREAMING_SNAKE_CASE						=	{
    'configuration_m2m_100': ['M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP', 'M2M100Config', 'M2M100OnnxConfig'],
    'tokenization_m2m_100': ['M2M100Tokenizer'],
}


try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE						=	[
          'M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST',
          'M2M100ForConditionalGeneration',
          'M2M100Model',
          'M2M100PreTrainedModel',
      ]


if TYPE_CHECKING:
      from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig
      from .tokenization_mam_aaa import MaMaaaTokenizer

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_mam_aaa import (
                M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST,
                MaMaaaForConditionalGeneration,
                MaMaaaModel,
                MaMaaaPreTrainedModel,
            )


else:
      import sys

      SCREAMING_SNAKE_CASE						=	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



