


"""simple docstring"""





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       :      Optional[Any]						,				__lowercase       :      Tuple						,				__lowercase       :      Optional[Any]=13						,				__lowercase       :      int=32						,				__lowercase       :      Dict=2						,				__lowercase       :      int=3						,				__lowercase       :      List[Any]=16						,				__lowercase       :      str=[32, 64, 128]						,				__lowercase       :      str=[1, 2, 1]						,				__lowercase       :      List[Any]=[2, 2, 4]						,				__lowercase       :      Dict=2						,				__lowercase       :      int=2.0						,				__lowercase       :      Dict=True						,				__lowercase       :      Optional[Any]=0.0						,				__lowercase       :      str=0.0						,				__lowercase       :      Union[str, Any]=0.1						,				__lowercase       :      List[str]="gelu"						,				__lowercase       :      Union[str, Any]=False						,				__lowercase       :      List[Any]=True						,				__lowercase       :      List[str]=0.0_2						,				__lowercase       :      Optional[Any]=1e-5						,				__lowercase       :      Tuple=True						,				__lowercase       :      str=None						,				__lowercase       :      List[str]=True						,				__lowercase       :      Optional[int]=10						,				__lowercase       :      List[Any]=8						,				__lowercase       :      Optional[int]=["stage1", "stage2"]						,				__lowercase       :      Dict=[1, 2]						,				):


        '''simple docstring'''




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

      def       A_					(							self       :      Tuple ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[str]       =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

        __UpperCAmelCase    :						Optional[int]       =						self.get_config()

        return config, pixel_values, labels

      def       A_					(							self       :      List[Any] ):


        '''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       A_					(							self       :      Any						,				__lowercase       :      Dict						,				__lowercase       :      int						,				__lowercase       :      Union[str, Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[Any]       =						FocalNetModel(config=__lowercase )
        model.to(__lowercase )
        model.eval()
        __UpperCAmelCase    :						List[str]       =						model(__lowercase )

        __UpperCAmelCase    :						List[str]       =						((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1))
        __UpperCAmelCase    :						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       A_					(							self       :      Optional[int]						,				__lowercase       :      List[Any]						,				__lowercase       :      int						,				__lowercase       :      Tuple ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[str]       =						FocalNetBackbone(config=__lowercase )
        model.to(__lowercase )
        model.eval()
        __UpperCAmelCase    :						int       =						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.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
        __UpperCAmelCase    :						Any       =						None
        __UpperCAmelCase    :						Union[str, Any]       =						FocalNetBackbone(config=__lowercase )
        model.to(__lowercase )
        model.eval()
        __UpperCAmelCase    :						Any       =						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.image_size * 2, 4, 4] )

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

      def       A_					(							self       :      Union[str, Any]						,				__lowercase       :      int						,				__lowercase       :      Dict						,				__lowercase       :      Union[str, Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[Any]       =						FocalNetForMaskedImageModeling(config=__lowercase )
        model.to(__lowercase )
        model.eval()
        __UpperCAmelCase    :						int       =						model(__lowercase )
        self.parent.assertEqual(
            result.reconstruction.shape						,				(self.batch_size, self.num_channels, self.image_size, self.image_size) )

        # test greyscale images
        __UpperCAmelCase    :						List[Any]       =						1
        __UpperCAmelCase    :						List[Any]       =						FocalNetForMaskedImageModeling(__lowercase )
        model.to(__lowercase )
        model.eval()

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

      def       A_					(							self       :      Dict						,				__lowercase       :      Optional[Any]						,				__lowercase       :      int						,				__lowercase       :      Any ):


        '''simple docstring'''




        __UpperCAmelCase    :						Union[str, Any]       =						self.type_sequence_label_size
        __UpperCAmelCase    :						List[Any]       =						FocalNetForImageClassification(__lowercase )
        model.to(__lowercase )
        model.eval()
        __UpperCAmelCase    :						List[str]       =						model(__lowercase						,				labels=__lowercase )
        self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.type_sequence_label_size) )

        # test greyscale images
        __UpperCAmelCase    :						List[Any]       =						1
        __UpperCAmelCase    :						str       =						FocalNetForImageClassification(__lowercase )
        model.to(__lowercase )
        model.eval()

        __UpperCAmelCase    :						Optional[int]       =						floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
        __UpperCAmelCase    :						Union[str, Any]       =						model(__lowercase )
        self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.type_sequence_label_size) )







      def       A_					(							self       :      Dict ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[str]       =						self.prepare_config_and_inputs()

        __UpperCAmelCase    :						Optional[int]       =						config_and_inputs
        __UpperCAmelCase    :						int       =						{'''pixel_values''': pixel_values}
        return config, inputs_dict




@require_torch
class 							snake_case							(			__UpperCAmelCase      ,  __UpperCAmelCase      ,  unittest.TestCase ):

      '''simple docstring'''



      _A     :       Dict         =       (
          (
              FocalNetModel,
              FocalNetForImageClassification,
              FocalNetForMaskedImageModeling,
              FocalNetBackbone,
          )
          if is_torch_available()
          else ()
      )
      _A     :       Any         =       (
          {'feature-extraction': FocalNetModel, 'image-classification': FocalNetForImageClassification}
          if is_torch_available()
          else {}
      )
      _A     :       Union[str, Any]         =       False

      _A     :       List[str]         =       False
      _A     :       Any         =       False
      _A     :       List[Any]         =       False
      _A     :       List[Any]         =       False

      def       A_					(							self       :      List[str] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[int]       =						FocalNetModelTester(self )
        __UpperCAmelCase    :						int       =						ConfigTester(self						,				config_class=__lowercase						,				embed_dim=37						,				has_text_modality=__lowercase )

      def       A_					(							self       :      List[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       :      int ):


        '''simple docstring'''




        return

      def       A_					(							self       :      Optional[int] ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[Any]       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__lowercase )

      def       A_					(							self       :      int ):


        '''simple docstring'''




        __UpperCAmelCase    :						int       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_backbone(*__lowercase )

      def       A_					(							self       :      Any ):


        '''simple docstring'''




        __UpperCAmelCase    :						Any       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_image_modeling(*__lowercase )

      def       A_					(							self       :      List[str] ):


        '''simple docstring'''




        __UpperCAmelCase    :						str       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__lowercase )

      @unittest.skip(reason='''FocalNet does not use inputs_embeds''' )
      def       A_					(							self       :      Dict ):


        '''simple docstring'''




        pass

      @unittest.skip(reason='''FocalNet does not use feedforward chunking''' )
      def       A_					(							self       :      Dict ):


        '''simple docstring'''




        pass

      def       A_					(							self       :      Any ):


        '''simple docstring'''




        __UpperCAmelCase    :						Tuple       =						self.model_tester.prepare_config_and_inputs_for_common()

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

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


        '''simple docstring'''




        __UpperCAmelCase    :						Dict       =						self.model_tester.prepare_config_and_inputs_for_common()

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

          __UpperCAmelCase    :						Tuple       =						['''pixel_values''']
          self.assertListEqual(arg_names[:1]						,				__lowercase )

      def       A_					(							self       :      List[Any]						,				__lowercase       :      List[str]						,				__lowercase       :      Tuple						,				__lowercase       :      Union[str, Any]						,				__lowercase       :      Any ):


        '''simple docstring'''




        __UpperCAmelCase    :						Dict       =						model_class(__lowercase )
        model.to(__lowercase )
        model.eval()

        with torch.no_grad():
          __UpperCAmelCase    :						Any       =						model(**self._prepare_for_class(__lowercase						,				__lowercase ) )

        __UpperCAmelCase    :						Tuple       =						outputs.hidden_states

        __UpperCAmelCase    :						Dict       =						getattr(
            self.model_tester						,				'''expected_num_hidden_layers'''						,				len(self.model_tester.depths ) + 1 )
        self.assertEqual(len(__lowercase )						,				__lowercase )

        # FocalNet has a different seq_length
        __UpperCAmelCase    :						str       =						(
            config.patch_size
            if isinstance(config.patch_size						,				collections.abc.Iterable )
            else (config.patch_size, config.patch_size)
        )

        __UpperCAmelCase    :						str       =						(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]						,				)

        __UpperCAmelCase    :						Any       =						outputs.reshaped_hidden_states
        self.assertEqual(len(__lowercase )						,				__lowercase )

        __UpperCAmelCase    :						Tuple       =						reshaped_hidden_states[0].shape
        __UpperCAmelCase    :						int       =						(
            reshaped_hidden_states[0].view(__lowercase						,				__lowercase						,				height * width ).permute(0						,				2						,				1 )
        )
        self.assertListEqual(
            list(reshaped_hidden_states.shape[-2:] )						,				[num_patches, self.model_tester.embed_dim]						,				)

      def       A_					(							self       :      Optional[Any] ):


        '''simple docstring'''




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

        __UpperCAmelCase    :						Optional[int]       =						(
            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]:
          __UpperCAmelCase    :						Dict       =						True
          self.check_hidden_states_output(__lowercase						,				__lowercase						,				__lowercase						,				__lowercase )

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

          self.check_hidden_states_output(__lowercase						,				__lowercase						,				__lowercase						,				__lowercase )

      def       A_					(							self       :      Any ):


        '''simple docstring'''




        __UpperCAmelCase    :						Union[str, Any]       =						self.model_tester.prepare_config_and_inputs_for_common()
        __UpperCAmelCase    :						Union[str, Any]       =						3

        __UpperCAmelCase    :						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)
        )
        __UpperCAmelCase    :						Optional[int]       =						(
            config.patch_size
            if isinstance(config.patch_size						,				collections.abc.Iterable )
            else (config.patch_size, config.patch_size)
        )

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

        for model_class in self.all_model_classes[:-1]:
          __UpperCAmelCase    :						str       =						True
          self.check_hidden_states_output(__lowercase						,				__lowercase						,				__lowercase						,				(padded_height, padded_width) )

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

      @slow
      def       A_					(							self       :      Union[str, Any] ):


        '''simple docstring'''




        for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          __UpperCAmelCase    :						Tuple       =						FocalNetModel.from_pretrained(__lowercase )
          self.assertIsNotNone(__lowercase )







      def       A_					(							self       :      Any ):


        '''simple docstring'''




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

        __UpperCAmelCase    :						Tuple       =						_config_zero_init(__lowercase )
        for model_class in self.all_model_classes:
          __UpperCAmelCase    :						Dict       =						model_class(config=__lowercase )
          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       A_					(							self       :      Union[str, Any] ):


        '''simple docstring'''




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







      @slow
      def       A_					(							self       :      Dict ):


        '''simple docstring'''




        __UpperCAmelCase    :						int       =						FocalNetForImageClassification.from_pretrained('''microsoft/focalnet-tiny''' ).to(__lowercase )
        __UpperCAmelCase    :						Optional[int]       =						self.default_image_processor

        __UpperCAmelCase    :						Tuple       =						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
        __UpperCAmelCase    :						List[Any]       =						image_processor(images=__lowercase						,				return_tensors='''pt''' ).to(__lowercase )

        # forward pass
        with torch.no_grad():
          __UpperCAmelCase    :						List[str]       =						model(**__lowercase )

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




@require_torch
class 							snake_case							(			__UpperCAmelCase      ,  unittest.TestCase ):

      '''simple docstring'''



      _A     :       List[str]         =       (FocalNetBackbone,) if is_torch_available() else ()
      _A     :       Dict         =       FocalNetConfig

      _A     :       str         =       False

      def       A_					(							self       :      List[str] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[Any]       =						FocalNetModelTester(self )






"""simple docstring"""





def       lowerCamelCase_       (   UpperCAmelCase_ = 10_00  )						->int:





  """simple docstring"""


  return sum(2 * a * ((a - 1) // 2) for a in range(3  ,			n + 1  )  )


if __name__ == "__main__":
  print(solution())