import collections
import inspect
import unittest

from transformers import FocalNetConfig
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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch
    from torch import nn

    from transformers import (
        FocalNetBackbone,
        FocalNetForImageClassification,
        FocalNetForMaskedImageModeling,
        FocalNetModel,
    )
    from transformers.models.focalnet.modeling_focalnet import FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
    from PIL import Image

    from transformers import AutoImageProcessor




class 	__snake_case	:





      '''simple docstring'''






      def __init__(					self     :				Tuple    ,  _UpperCamelCase     :				Any    ,  _UpperCamelCase     :				Dict=13    ,  _UpperCamelCase     :				Optional[Any]=32    ,  _UpperCamelCase     :				int=2    ,  _UpperCamelCase     :				Optional[Any]=3    ,  _UpperCamelCase     :				Union[str, Any]=16    ,  _UpperCamelCase     :				List[str]=[32, 64, 128]    ,  _UpperCamelCase     :				Any=[1, 2, 1]    ,  _UpperCamelCase     :				Tuple=[2, 2, 4]    ,  _UpperCamelCase     :				Union[str, Any]=2    ,  _UpperCamelCase     :				int=2.0    ,  _UpperCamelCase     :				Optional[int]=True    ,  _UpperCamelCase     :				Tuple=0.0    ,  _UpperCamelCase     :				List[str]=0.0    ,  _UpperCamelCase     :				Optional[int]=0.1    ,  _UpperCamelCase     :				int="gelu"    ,  _UpperCamelCase     :				Optional[int]=False    ,  _UpperCamelCase     :				int=True    ,  _UpperCamelCase     :				Any=0.0_2    ,  _UpperCamelCase     :				List[Any]=1E-5    ,  _UpperCamelCase     :				Optional[int]=True    ,  _UpperCamelCase     :				Any=None    ,  _UpperCamelCase     :				str=True    ,  _UpperCamelCase     :				Dict=10    ,  _UpperCamelCase     :				Dict=8    ,  _UpperCamelCase     :				Any=["stage1", "stage2"]    ,  _UpperCamelCase     :				Dict=[1, 2]    ,  )				->str:


          """simple docstring"""


          _lowerCamelCase     :		List[Any] 						=						parent
          _lowerCamelCase     :		List[Any] 						=						batch_size
          _lowerCamelCase     :		Union[str, Any] 						=						image_size
          _lowerCamelCase     :		Optional[Any] 						=						patch_size
          _lowerCamelCase     :		int 						=						num_channels
          _lowerCamelCase     :		Any 						=						embed_dim
          _lowerCamelCase     :		Union[str, Any] 						=						hidden_sizes
          _lowerCamelCase     :		str 						=						depths
          _lowerCamelCase     :		List[Any] 						=						num_heads
          _lowerCamelCase     :		Union[str, Any] 						=						window_size
          _lowerCamelCase     :		Optional[Any] 						=						mlp_ratio
          _lowerCamelCase     :		List[str] 						=						qkv_bias
          _lowerCamelCase     :		Any 						=						hidden_dropout_prob
          _lowerCamelCase     :		Tuple 						=						attention_probs_dropout_prob
          _lowerCamelCase     :		Optional[Any] 						=						drop_path_rate
          _lowerCamelCase     :		Union[str, Any] 						=						hidden_act
          _lowerCamelCase     :		str 						=						use_absolute_embeddings
          _lowerCamelCase     :		List[str] 						=						patch_norm
          _lowerCamelCase     :		Dict 						=						layer_norm_eps
          _lowerCamelCase     :		int 						=						initializer_range
          _lowerCamelCase     :		str 						=						is_training
          _lowerCamelCase     :		Optional[int] 						=						scope
          _lowerCamelCase     :		str 						=						use_labels
          _lowerCamelCase     :		List[Any] 						=						type_sequence_label_size
          _lowerCamelCase     :		Any 						=						encoder_stride
          _lowerCamelCase     :		List[Any] 						=						out_features
          _lowerCamelCase     :		str 						=						out_indices



      def 	_SCREAMING_SNAKE_CASE    (					self     :				str)				->str:


          """simple docstring"""


          _lowerCamelCase     :		Dict 						=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

          _lowerCamelCase     :		Tuple 						=						self.get_config()

          return config, pixel_values, labels



      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[Any])				->List[str]:


          """simple docstring"""


          return FocalNetConfig(
              image_size=self.image_size    ,  patch_size=self.patch_size    ,  num_channels=self.num_channels    ,  embed_dim=self.embed_dim    ,  hidden_sizes=self.hidden_sizes    ,  depths=self.depths    ,  num_heads=self.num_heads    ,  window_size=self.window_size    ,  mlp_ratio=self.mlp_ratio    ,  qkv_bias=self.qkv_bias    ,  hidden_dropout_prob=self.hidden_dropout_prob    ,  attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,  drop_path_rate=self.drop_path_rate    ,  hidden_act=self.hidden_act    ,  use_absolute_embeddings=self.use_absolute_embeddings    ,  path_norm=self.patch_norm    ,  layer_norm_eps=self.layer_norm_eps    ,  initializer_range=self.initializer_range    ,  encoder_stride=self.encoder_stride    ,  out_features=self.out_features    ,  out_indices=self.out_indices    ,  )



      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[str]    ,  _UpperCamelCase     :				Optional[Any]    ,  _UpperCamelCase     :				Any    ,  _UpperCamelCase     :				int)				->Dict:


          """simple docstring"""


          _lowerCamelCase     :		List[Any] 						=						FocalNetModel(config=_UpperCamelCase)
          model.to(_UpperCamelCase)
          model.eval()
          _lowerCamelCase     :		Optional[Any] 						=						model(_UpperCamelCase)

          _lowerCamelCase     :		int 						=						((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths) - 1))
          _lowerCamelCase     :		int 						=						int(config.embed_dim * 2 ** (len(config.depths) - 1))

          self.parent.assertEqual(result.last_hidden_state.shape    ,  (self.batch_size, expected_seq_len, expected_dim))



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Union[str, Any]    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				Tuple    ,  _UpperCamelCase     :				List[str])				->str:


          """simple docstring"""


          _lowerCamelCase     :		Dict 						=						FocalNetBackbone(config=_UpperCamelCase)
          model.to(_UpperCamelCase)
          model.eval()
          _lowerCamelCase     :		Any 						=						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.image_size, 8, 8])

          # 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
          _lowerCamelCase     :		List[Any] 						=						None
          _lowerCamelCase     :		int 						=						FocalNetBackbone(config=_UpperCamelCase)
          model.to(_UpperCamelCase)
          model.eval()
          _lowerCamelCase     :		Tuple 						=						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.image_size * 2, 4, 4])

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



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Any    ,  _UpperCamelCase     :				Tuple    ,  _UpperCamelCase     :				List[str]    ,  _UpperCamelCase     :				Optional[Any])				->str:


          """simple docstring"""


          _lowerCamelCase     :		Dict 						=						FocalNetForMaskedImageModeling(config=_UpperCamelCase)
          model.to(_UpperCamelCase)
          model.eval()
          _lowerCamelCase     :		Tuple 						=						model(_UpperCamelCase)
          self.parent.assertEqual(
              result.reconstruction.shape    ,  (self.batch_size, self.num_channels, self.image_size, self.image_size))

          # test greyscale images
          _lowerCamelCase     :		Tuple 						=						1
          _lowerCamelCase     :		int 						=						FocalNetForMaskedImageModeling(_UpperCamelCase)
          model.to(_UpperCamelCase)
          model.eval()

          _lowerCamelCase     :		str 						=						floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
          _lowerCamelCase     :		List[Any] 						=						model(_UpperCamelCase)
          self.parent.assertEqual(result.reconstruction.shape    ,  (self.batch_size, 1, self.image_size, self.image_size))



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Union[str, Any]    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				List[str]    ,  _UpperCamelCase     :				Dict)				->Dict:


          """simple docstring"""


          _lowerCamelCase     :		str 						=						self.type_sequence_label_size
          _lowerCamelCase     :		Optional[int] 						=						FocalNetForImageClassification(_UpperCamelCase)
          model.to(_UpperCamelCase)
          model.eval()
          _lowerCamelCase     :		Any 						=						model(_UpperCamelCase    ,  labels=_UpperCamelCase)
          self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.type_sequence_label_size))

          # test greyscale images
          _lowerCamelCase     :		List[str] 						=						1
          _lowerCamelCase     :		int 						=						FocalNetForImageClassification(_UpperCamelCase)
          model.to(_UpperCamelCase)
          model.eval()

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




      def 	_SCREAMING_SNAKE_CASE    (					self     :				int)				->Optional[int]:


          """simple docstring"""


          _lowerCamelCase     :		Any 						=						self.prepare_config_and_inputs()

          _lowerCamelCase     :		Optional[Any] 						=						config_and_inputs
          _lowerCamelCase     :		Optional[Any] 						=						{"""pixel_values""": pixel_values}
          return config, inputs_dict





@require_torch
class 	__snake_case	( __lowerCAmelCase ,  __lowerCAmelCase ,  unittest.TestCase		):





      '''simple docstring'''






      _snake_case    		=       (
          (
              FocalNetModel,
              FocalNetForImageClassification,
              FocalNetForMaskedImageModeling,
              FocalNetBackbone,
          )
          if is_torch_available()
          else ()
      )
      _snake_case    		=       (
          {'feature-extraction': FocalNetModel, 'image-classification': FocalNetForImageClassification}
          if is_torch_available()
          else {}
      )
      _snake_case    		=       False

      _snake_case    		=       False
      _snake_case    		=       False
      _snake_case    		=       False
      _snake_case    		=       False



      def 	_SCREAMING_SNAKE_CASE    (					self     :				int)				->Tuple:


          """simple docstring"""


          _lowerCamelCase     :		int 						=						FocalNetModelTester(self)
          _lowerCamelCase     :		str 						=						ConfigTester(self    ,  config_class=_UpperCamelCase    ,  embed_dim=37    ,  has_text_modality=_UpperCamelCase)



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


          """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 	_SCREAMING_SNAKE_CASE    (					self     :				int)				->List[Any]:


          """simple docstring"""


          return



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Any)				->Tuple:


          """simple docstring"""


          _lowerCamelCase     :		List[Any] 						=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				int)				->Any:


          """simple docstring"""


          _lowerCamelCase     :		str 						=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_backbone(*_UpperCamelCase)



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


          """simple docstring"""


          _lowerCamelCase     :		Dict 						=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_image_modeling(*_UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[int])				->int:


          """simple docstring"""


          _lowerCamelCase     :		List[Any] 						=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase)



      @unittest.skip(reason="""FocalNet does not use inputs_embeds""")
      def 	_SCREAMING_SNAKE_CASE    (					self     :				str)				->Tuple:


          """simple docstring"""


          pass



      @unittest.skip(reason="""FocalNet does not use feedforward chunking""")
      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[str])				->int:


          """simple docstring"""


          pass



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


          """simple docstring"""


          _lowerCamelCase     :		List[str] 						=						self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes[:-1]:
              _lowerCamelCase     :		Tuple 						=						model_class(_UpperCamelCase)
              self.assertIsInstance(model.get_input_embeddings()    ,  (nn.Module))
              _lowerCamelCase     :		Union[str, Any] 						=						model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(_UpperCamelCase    ,  nn.Linear))



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


          """simple docstring"""


          _lowerCamelCase     :		Any 						=						self.model_tester.prepare_config_and_inputs_for_common()

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

              _lowerCamelCase     :		List[Any] 						=						["""pixel_values"""]
              self.assertListEqual(arg_names[:1]    ,  _UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[Any]    ,  _UpperCamelCase     :				str    ,  _UpperCamelCase     :				List[str]    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				int)				->str:


          """simple docstring"""


          _lowerCamelCase     :		Dict 						=						model_class(_UpperCamelCase)
          model.to(_UpperCamelCase)
          model.eval()

          with torch.no_grad():
              _lowerCamelCase     :		int 						=						model(**self._prepare_for_class(_UpperCamelCase    ,  _UpperCamelCase))

          _lowerCamelCase     :		Union[str, Any] 						=						outputs.hidden_states

          _lowerCamelCase     :		Optional[Any] 						=						getattr(
              self.model_tester    ,  """expected_num_hidden_layers"""    ,  len(self.model_tester.depths) + 1)
          self.assertEqual(len(_UpperCamelCase)    ,  _UpperCamelCase)

          # FocalNet has a different seq_length
          _lowerCamelCase     :		List[Any] 						=						(
              config.patch_size
              if isinstance(config.patch_size    ,  collections.abc.Iterable)
              else (config.patch_size, config.patch_size)
          )

          _lowerCamelCase     :		Any 						=						(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

          self.assertListEqual(
              list(hidden_states[0].shape[-2:])    ,  [num_patches, self.model_tester.embed_dim]    ,  )

          _lowerCamelCase     :		str 						=						outputs.reshaped_hidden_states
          self.assertEqual(len(_UpperCamelCase)    ,  _UpperCamelCase)

          _lowerCamelCase     :		Any 						=						reshaped_hidden_states[0].shape
          _lowerCamelCase     :		Dict 						=						(
              reshaped_hidden_states[0].view(_UpperCamelCase    ,  _UpperCamelCase    ,  height * width).permute(0    ,  2    ,  1)
          )
          self.assertListEqual(
              list(reshaped_hidden_states.shape[-2:])    ,  [num_patches, self.model_tester.embed_dim]    ,  )



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Tuple)				->Any:


          """simple docstring"""


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

          _lowerCamelCase     :		Dict 						=						(
              self.model_tester.image_size
              if isinstance(self.model_tester.image_size    ,  collections.abc.Iterable)
              else (self.model_tester.image_size, self.model_tester.image_size)
          )

          for model_class in self.all_model_classes[:-1]:
              _lowerCamelCase     :		List[str] 						=						True
              self.check_hidden_states_output(_UpperCamelCase    ,  _UpperCamelCase    ,  _UpperCamelCase    ,  _UpperCamelCase)

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

              self.check_hidden_states_output(_UpperCamelCase    ,  _UpperCamelCase    ,  _UpperCamelCase    ,  _UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[int])				->List[str]:


          """simple docstring"""


          _lowerCamelCase     :		List[str] 						=						self.model_tester.prepare_config_and_inputs_for_common()
          _lowerCamelCase     :		int 						=						3

          _lowerCamelCase     :		Tuple 						=						(
              self.model_tester.image_size
              if isinstance(self.model_tester.image_size    ,  collections.abc.Iterable)
              else (self.model_tester.image_size, self.model_tester.image_size)
          )
          _lowerCamelCase     :		int 						=						(
              config.patch_size
              if isinstance(config.patch_size    ,  collections.abc.Iterable)
              else (config.patch_size, config.patch_size)
          )

          _lowerCamelCase     :		Union[str, Any] 						=						image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
          _lowerCamelCase     :		int 						=						image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

          for model_class in self.all_model_classes[:-1]:
              _lowerCamelCase     :		Dict 						=						True
              self.check_hidden_states_output(_UpperCamelCase    ,  _UpperCamelCase    ,  _UpperCamelCase    ,  (padded_height, padded_width))

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              _lowerCamelCase     :		Dict 						=						True
              self.check_hidden_states_output(_UpperCamelCase    ,  _UpperCamelCase    ,  _UpperCamelCase    ,  (padded_height, padded_width))



      @slow
      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[str])				->Optional[int]:


          """simple docstring"""


          for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _lowerCamelCase     :		Optional[Any] 						=						FocalNetModel.from_pretrained(_UpperCamelCase)
              self.assertIsNotNone(_UpperCamelCase)




      def 	_SCREAMING_SNAKE_CASE    (					self     :				Dict)				->int:


          """simple docstring"""


          _lowerCamelCase     :		Tuple 						=						self.model_tester.prepare_config_and_inputs_for_common()

          _lowerCamelCase     :		List[str] 						=						_config_zero_init(_UpperCamelCase)
          for model_class in self.all_model_classes:
              _lowerCamelCase     :		Any 						=						model_class(config=_UpperCamelCase)
              for name, param in model.named_parameters():
                  if "embeddings" not in name and param.requires_grad:
                      self.assertIn(
                          ((param.data.mean() * 1E9).round() / 1E9).item()    ,  [0.0, 1.0]    ,  msg=F"""Parameter {name} of model {model_class} seems not properly initialized"""    ,  )





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





      '''simple docstring'''






      @cached_property
      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[Any])				->int:


          """simple docstring"""


          return AutoImageProcessor.from_pretrained("""microsoft/focalnet-tiny""") if is_vision_available() else None




      @slow
      def 	_SCREAMING_SNAKE_CASE    (					self     :				Dict)				->Tuple:


          """simple docstring"""


          _lowerCamelCase     :		Dict 						=						FocalNetForImageClassification.from_pretrained("""microsoft/focalnet-tiny""").to(_UpperCamelCase)
          _lowerCamelCase     :		Dict 						=						self.default_image_processor

          _lowerCamelCase     :		Optional[int] 						=						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
          _lowerCamelCase     :		List[str] 						=						image_processor(images=_UpperCamelCase    ,  return_tensors="""pt""").to(_UpperCamelCase)

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

          # verify the logits
          _lowerCamelCase     :		List[Any] 						=						torch.Size((1, 1000))
          self.assertEqual(outputs.logits.shape    ,  _UpperCamelCase)
          _lowerCamelCase     :		Optional[Any] 						=						torch.tensor([0.2_1_6_6, -0.4_3_6_8, 0.2_1_9_1]).to(_UpperCamelCase)
          self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,  _UpperCamelCase    ,  atol=1E-4))
          self.assertTrue(outputs.logits.argmax(dim=-1).item()    ,  281)





@require_torch
class 	__snake_case	( __lowerCAmelCase ,  unittest.TestCase		):





      '''simple docstring'''






      _snake_case    		=       (FocalNetBackbone,) if is_torch_available() else ()
      _snake_case    		=       FocalNetConfig

      _snake_case    		=       False



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Union[str, Any])				->Optional[Any]:


          """simple docstring"""


          _lowerCamelCase     :		Optional[Any] 						=						FocalNetModelTester(self)





def 						A__  (					__A	):







    '''simple docstring'''
    _lowerCamelCase     :		Tuple 						=						0
    for ch in input_str:
        _lowerCamelCase     :		Optional[Any] 						=						ord(__A	)
        _lowerCamelCase     :		List[str] 						=						pow(2   , __A	)

        # If we already turned on bit for current character's unicode
        if bitmap >> ch_unicode & 1 == 1:
            return False
        bitmap |= ch_bit_index_on
    return True


if __name__ == "__main__":
    import doctest

    doctest.testmod()




