



"""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 					a	:



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


     '''simple docstring'''




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



  def 	lowerCamelCase__       (						self :					Tuple		)     ->					Union[str, Any]:


     '''simple docstring'''




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

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

     SCREAMING_SNAKE_CASE_: Any     			=self.get_config()

     return config, pixel_values, labels



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


     '''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 	lowerCamelCase__       (						self :					str       ,		lowerCAmelCase :					Any       ,		lowerCAmelCase :					Optional[int]       ,		lowerCAmelCase :					int		)     ->					Union[str, Any]:


     '''simple docstring'''




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



  def 	lowerCamelCase__       (						self :					Any       ,		lowerCAmelCase :					str       ,		lowerCAmelCase :					Tuple       ,		lowerCAmelCase :					Optional[Any]		)     ->					Dict:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: int     			=self.num_labels
     SCREAMING_SNAKE_CASE_: Optional[int]     			=BitForImageClassification(_lowercase		)
     model.to(_lowercase		)
     model.eval()
     SCREAMING_SNAKE_CASE_: List[str]     			=model(_lowercase       ,		labels=_lowercase		)
     self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.num_labels)		)



  def 	lowerCamelCase__       (						self :					int       ,		lowerCAmelCase :					Optional[Any]       ,		lowerCAmelCase :					Dict       ,		lowerCAmelCase :					Any		)     ->					Optional[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: int     			=BitBackbone(config=_lowercase		)
     model.to(_lowercase		)
     model.eval()
     SCREAMING_SNAKE_CASE_: Optional[Any]     			=model(_lowercase		)

     # 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
     SCREAMING_SNAKE_CASE_: Optional[int]     			=None
     SCREAMING_SNAKE_CASE_: Dict     			=BitBackbone(config=_lowercase		)
     model.to(_lowercase		)
     model.eval()
     SCREAMING_SNAKE_CASE_: List[str]     			=model(_lowercase		)

     # 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 	lowerCamelCase__       (						self :					Union[str, Any]		)     ->					str:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[Any]     			=self.prepare_config_and_inputs()
     SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Any     			=config_and_inputs
     SCREAMING_SNAKE_CASE_: int     			={"""pixel_values""": pixel_values}
     return config, inputs_dict







@require_torch
class 					a	(  UpperCAmelCase_	,      UpperCAmelCase_	,      unittest.TestCase       ):
  UpperCamelCase	:				List[str]     						=					(BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else ()
  UpperCamelCase	:				List[str]     						=					(
      {'feature-extraction': BitModel, 'image-classification': BitForImageClassification}
      if is_torch_available()
      else {}
  )

  UpperCamelCase	:				str     						=					False
  UpperCamelCase	:				int     						=					False
  UpperCamelCase	:				Optional[int]     						=					False
  UpperCamelCase	:				str     						=					False
  UpperCamelCase	:				List[Any]     						=					False



  def 	lowerCamelCase__       (						self :					Dict		)     ->					List[str]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Any     			=BitModelTester(self		)
     SCREAMING_SNAKE_CASE_: str     			=ConfigTester(self       ,		config_class=_lowercase       ,		has_text_modality=_lowercase		)



  def 	lowerCamelCase__       (						self :					List[str]		)     ->					Tuple:


     '''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 	lowerCamelCase__       (						self :					Tuple		)     ->					Union[str, Any]:


     '''simple docstring'''




     return



  @unittest.skip(reason="""Bit does not output attentions"""		)
  def 	lowerCamelCase__       (						self :					Tuple		)     ->					str:


     '''simple docstring'''




     pass



  @unittest.skip(reason="""Bit does not use inputs_embeds"""		)
  def 	lowerCamelCase__       (						self :					int		)     ->					Union[str, Any]:


     '''simple docstring'''




     pass



  @unittest.skip(reason="""Bit does not support input and output embeddings"""		)
  def 	lowerCamelCase__       (						self :					Dict		)     ->					Tuple:


     '''simple docstring'''




     pass



  def 	lowerCamelCase__       (						self :					str		)     ->					Tuple:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: int     			=self.model_tester.prepare_config_and_inputs_for_common()

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

        SCREAMING_SNAKE_CASE_: Any     			=["""pixel_values"""]
        self.assertListEqual(arg_names[:1]       ,		_lowercase		)



  def 	lowerCamelCase__       (						self :					Tuple		)     ->					Union[str, Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Any     			=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_lowercase		)



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: List[Any]     			=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_backbone(*_lowercase		)



  def 	lowerCamelCase__       (						self :					Dict		)     ->					Any:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Dict     			=self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
        SCREAMING_SNAKE_CASE_: Dict     			=model_class(config=_lowercase		)
        for name, module in model.named_modules():
           if isinstance(_lowercase       ,		(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 	lowerCamelCase__       (						self :					int		)     ->					int:


     '''simple docstring'''




     def check_hidden_states_output(lowerCAmelCase :					Union[str, Any]       ,		lowerCAmelCase :					Union[str, Any]       ,		lowerCAmelCase :					List[str]		):
        SCREAMING_SNAKE_CASE_: Union[str, Any]     			=model_class(_lowercase		)
        model.to(_lowercase		)
        model.eval()

        with torch.no_grad():
           SCREAMING_SNAKE_CASE_: List[str]     			=model(**self._prepare_for_class(_lowercase       ,		_lowercase		)		)

        SCREAMING_SNAKE_CASE_: Any     			=outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

        SCREAMING_SNAKE_CASE_: Union[str, Any]     			=self.model_tester.num_stages
        self.assertEqual(len(_lowercase		)       ,		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]       ,		)

     SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Optional[Any]     			=self.model_tester.prepare_config_and_inputs_for_common()
     SCREAMING_SNAKE_CASE_: List[Any]     			=["""preactivation""", """bottleneck"""]
     for model_class in self.all_model_classes:
        for layer_type in layers_type:
           SCREAMING_SNAKE_CASE_: Optional[int]     			=layer_type
           SCREAMING_SNAKE_CASE_: Optional[int]     			=True
           check_hidden_states_output(_lowercase       ,		_lowercase       ,		_lowercase		)

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

           check_hidden_states_output(_lowercase       ,		_lowercase       ,		_lowercase		)



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


     '''simple docstring'''




     pass



  def 	lowerCamelCase__       (						self :					Dict		)     ->					Dict:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: List[str]     			=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*_lowercase		)





  @slow
  def 	lowerCamelCase__       (						self :					List[str]		)     ->					List[str]:


     '''simple docstring'''




     for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        SCREAMING_SNAKE_CASE_: Dict     			=BitModel.from_pretrained(_lowercase		)
        self.assertIsNotNone(_lowercase		)







def      __magic_name__				(							):
   SCREAMING_SNAKE_CASE_: str     			=Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
   return image
@require_torch
@require_vision
class 					a	(  unittest.TestCase       ):



  @cached_property
  def 	lowerCamelCase__       (						self :					Dict		)     ->					str:


     '''simple docstring'''




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





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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Any     			=BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		).to(_lowercase		)

     SCREAMING_SNAKE_CASE_: str     			=self.default_image_processor
     SCREAMING_SNAKE_CASE_: Optional[Any]     			=prepare_img()
     SCREAMING_SNAKE_CASE_: Optional[Any]     			=image_processor(images=_lowercase       ,		return_tensors="""pt"""		).to(_lowercase		)

     # forward pass
     with torch.no_grad():
        SCREAMING_SNAKE_CASE_: Optional[Any]     			=model(**_lowercase		)

     # verify the logits
     SCREAMING_SNAKE_CASE_: Optional[int]     			=torch.Size((1, 1000)		)
     self.assertEqual(outputs.logits.shape       ,		_lowercase		)

     SCREAMING_SNAKE_CASE_: Optional[Any]     			=torch.tensor([[-0.6_5_2_6, -0.5_2_6_3, -1.4_3_9_8]]		).to(_lowercase		)

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







@require_torch
class 					a	(  UpperCAmelCase_	,      unittest.TestCase       ):
  UpperCamelCase	:				str     						=					(BitBackbone,) if is_torch_available() else ()
  UpperCamelCase	:				List[Any]     						=					BitConfig

  UpperCamelCase	:				str     						=					False



  def 	lowerCamelCase__       (						self :					List[str]		)     ->					Dict:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Tuple     			=BitModelTester(self		)




"""simple docstring"""
from typing import Dict

import numpy as np
import torch

from . import residue_constants as rc
from .tensor_utils import tensor_tree_map, tree_map





def      __magic_name__				(							lowercase ):
   SCREAMING_SNAKE_CASE_: Optional[Any]     			=[]
   SCREAMING_SNAKE_CASE_: List[str]     			=[]
   SCREAMING_SNAKE_CASE_: Any     			=[]

   for rt in rc.restypes:
      SCREAMING_SNAKE_CASE_: Optional[int]     			=rc.restype_name_to_atomaa_names[rc.restype_atoa[rt]]
      restype_atomaa_to_atomaa_list.append([(rc.atom_order[name] if name else 0) for name in atom_names] )
      SCREAMING_SNAKE_CASE_: Any     			={name: i for i, name in enumerate(lowercase )}
      restype_atomaa_to_atomaa_list.append(
          [(atom_name_to_idxaa[name] if name in atom_name_to_idxaa else 0) for name in rc.atom_types] )

      restype_atomaa_mask_list.append([(1.0 if name else 0.0) for name in atom_names] )

   # Add dummy mapping for restype 'UNK'
   restype_atomaa_to_atomaa_list.append([0] * 14 )
   restype_atomaa_to_atomaa_list.append([0] * 37 )
   restype_atomaa_mask_list.append([0.0] * 14 )

   SCREAMING_SNAKE_CASE_: Union[str, Any]     			=torch.tensor(
       lowercase							,			dtype=torch.intaa							,			device=protein["""aatype"""].device							,			)
   SCREAMING_SNAKE_CASE_: Optional[Any]     			=torch.tensor(
       lowercase							,			dtype=torch.intaa							,			device=protein["""aatype"""].device							,			)
   SCREAMING_SNAKE_CASE_: List[str]     			=torch.tensor(
       lowercase							,			dtype=torch.floataa							,			device=protein["""aatype"""].device							,			)
   SCREAMING_SNAKE_CASE_: Optional[Any]     			=protein["""aatype"""].to(torch.long )

   # create the mapping for (residx, atom14) --> atom37, i.e. an array
   # with shape (num_res, 14) containing the atom37 indices for this protein
   SCREAMING_SNAKE_CASE_: List[str]     			=restype_atomaa_to_atomaa[protein_aatype]
   SCREAMING_SNAKE_CASE_: Any     			=restype_atomaa_mask[protein_aatype]

   SCREAMING_SNAKE_CASE_: Tuple     			=residx_atomaa_mask
   SCREAMING_SNAKE_CASE_: Dict     			=residx_atomaa_to_atomaa.long()

   # create the gather indices for mapping back
   SCREAMING_SNAKE_CASE_: Dict     			=restype_atomaa_to_atomaa[protein_aatype]
   SCREAMING_SNAKE_CASE_: Optional[int]     			=residx_atomaa_to_atomaa.long()

   # create the corresponding mask
   SCREAMING_SNAKE_CASE_: Optional[int]     			=torch.zeros([21, 37]							,			dtype=torch.floataa							,			device=protein["""aatype"""].device )
   for restype, restype_letter in enumerate(rc.restypes ):
      SCREAMING_SNAKE_CASE_: int     			=rc.restype_atoa[restype_letter]
      SCREAMING_SNAKE_CASE_: Any     			=rc.residue_atoms[restype_name]
      for atom_name in atom_names:
         SCREAMING_SNAKE_CASE_: Optional[int]     			=rc.atom_order[atom_name]
         SCREAMING_SNAKE_CASE_: Dict     			=1

   SCREAMING_SNAKE_CASE_: List[str]     			=restype_atomaa_mask[protein_aatype]
   SCREAMING_SNAKE_CASE_: List[Any]     			=residx_atomaa_mask

   return protein





def      __magic_name__				(							lowercase ):
   SCREAMING_SNAKE_CASE_: Any     			=tree_map(lambda lowercase : torch.tensor(lowercase							,			device=batch["""aatype"""].device )							,			lowercase							,			np.ndarray )
   SCREAMING_SNAKE_CASE_: int     			=tensor_tree_map(lambda lowercase : np.array(lowercase )							,			make_atomaa_masks(lowercase ) )
   return out



