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 					A							:


   def __init__(self					:						Dict	,  __UpperCAmelCase					:						Union[str, Any]	,  __UpperCAmelCase					:						List[Any]=1_3	,  __UpperCAmelCase					:						Optional[int]=3_2	,  __UpperCAmelCase					:						Optional[Any]=2	,  __UpperCAmelCase					:						int=3	,  __UpperCAmelCase					:						List[str]=1_6	,  __UpperCAmelCase					:						Tuple=[3_2, 6_4, 1_2_8]	,  __UpperCAmelCase					:						Tuple=[1, 2, 1]	,  __UpperCAmelCase					:						List[Any]=[2, 2, 4]	,  __UpperCAmelCase					:						Optional[Any]=2	,  __UpperCAmelCase					:						Tuple=2.0	,  __UpperCAmelCase					:						Optional[int]=True	,  __UpperCAmelCase					:						List[Any]=0.0	,  __UpperCAmelCase					:						Optional[int]=0.0	,  __UpperCAmelCase					:						List[Any]=0.1	,  __UpperCAmelCase					:						Union[str, Any]="gelu"	,  __UpperCAmelCase					:						Optional[int]=False	,  __UpperCAmelCase					:						Dict=True	,  __UpperCAmelCase					:						Any=0.02	,  __UpperCAmelCase					:						str=1E-5	,  __UpperCAmelCase					:						Dict=True	,  __UpperCAmelCase					:						List[Any]=None	,  __UpperCAmelCase					:						Dict=True	,  __UpperCAmelCase					:						Dict=1_0	,  __UpperCAmelCase					:						Union[str, Any]=8	,  __UpperCAmelCase					:						List[Any]=["stage1", "stage2"]	,  __UpperCAmelCase					:						Dict=[1, 2]	,  )						->    Optional[int]:
         """simple docstring"""



         UpperCAmelCase__								=  parent
         UpperCAmelCase__								=  batch_size
         UpperCAmelCase__								=  image_size
         UpperCAmelCase__								=  patch_size
         UpperCAmelCase__								=  num_channels
         UpperCAmelCase__								=  embed_dim
         UpperCAmelCase__								=  hidden_sizes
         UpperCAmelCase__								=  depths
         UpperCAmelCase__								=  num_heads
         UpperCAmelCase__								=  window_size
         UpperCAmelCase__								=  mlp_ratio
         UpperCAmelCase__								=  qkv_bias
         UpperCAmelCase__								=  hidden_dropout_prob
         UpperCAmelCase__								=  attention_probs_dropout_prob
         UpperCAmelCase__								=  drop_path_rate
         UpperCAmelCase__								=  hidden_act
         UpperCAmelCase__								=  use_absolute_embeddings
         UpperCAmelCase__								=  patch_norm
         UpperCAmelCase__								=  layer_norm_eps
         UpperCAmelCase__								=  initializer_range
         UpperCAmelCase__								=  is_training
         UpperCAmelCase__								=  scope
         UpperCAmelCase__								=  use_labels
         UpperCAmelCase__								=  type_sequence_label_size
         UpperCAmelCase__								=  encoder_stride
         UpperCAmelCase__								=  out_features
         UpperCAmelCase__								=  out_indices


   def       lowercase_				(self					:						List[Any]		)						->    Optional[int]:
         """simple docstring"""



         UpperCAmelCase__								=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

         UpperCAmelCase__								=  None
         if self.use_labels:
               UpperCAmelCase__								=  ids_tensor([self.batch_size]	,  self.type_sequence_label_size		)

         UpperCAmelCase__								=  self.get_config()

         return config, pixel_values, labels


   def       lowercase_				(self					:						int		)						->    Tuple:
         """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       lowercase_				(self					:						Any	,  __UpperCAmelCase					:						List[str]	,  __UpperCAmelCase					:						Dict	,  __UpperCAmelCase					:						List[Any]		)						->    Union[str, Any]:
         """simple docstring"""



         UpperCAmelCase__								=  FocalNetModel(config=__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()
         UpperCAmelCase__								=  model(__UpperCAmelCase		)

         UpperCAmelCase__								=  ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths		) - 1))
         UpperCAmelCase__								=  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       lowercase_				(self					:						int	,  __UpperCAmelCase					:						Union[str, Any]	,  __UpperCAmelCase					:						str	,  __UpperCAmelCase					:						List[Any]		)						->    Optional[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  FocalNetBackbone(config=__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()
         UpperCAmelCase__								=  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
         UpperCAmelCase__								=  None
         UpperCAmelCase__								=  FocalNetBackbone(config=__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()
         UpperCAmelCase__								=  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       lowercase_				(self					:						Tuple	,  __UpperCAmelCase					:						Optional[int]	,  __UpperCAmelCase					:						Optional[Any]	,  __UpperCAmelCase					:						Tuple		)						->    int:
         """simple docstring"""



         UpperCAmelCase__								=  FocalNetForMaskedImageModeling(config=__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()
         UpperCAmelCase__								=  model(__UpperCAmelCase		)
         self.parent.assertEqual(
             result.reconstruction.shape	,  (self.batch_size, self.num_channels, self.image_size, self.image_size)		)

         # test greyscale images
         UpperCAmelCase__								=  1
         UpperCAmelCase__								=  FocalNetForMaskedImageModeling(__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()

         UpperCAmelCase__								=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
         UpperCAmelCase__								=  model(__UpperCAmelCase		)
         self.parent.assertEqual(result.reconstruction.shape	,  (self.batch_size, 1, self.image_size, self.image_size)		)


   def       lowercase_				(self					:						Any	,  __UpperCAmelCase					:						Dict	,  __UpperCAmelCase					:						Dict	,  __UpperCAmelCase					:						Dict		)						->    Dict:
         """simple docstring"""



         UpperCAmelCase__								=  self.type_sequence_label_size
         UpperCAmelCase__								=  FocalNetForImageClassification(__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()
         UpperCAmelCase__								=  model(__UpperCAmelCase	,  labels=__UpperCAmelCase		)
         self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.type_sequence_label_size)		)

         # test greyscale images
         UpperCAmelCase__								=  1
         UpperCAmelCase__								=  FocalNetForImageClassification(__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()

         UpperCAmelCase__								=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
         UpperCAmelCase__								=  model(__UpperCAmelCase		)
         self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.type_sequence_label_size)		)





   def       lowercase_				(self					:						str		)						->    List[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  self.prepare_config_and_inputs()

         UpperCAmelCase__		,				UpperCAmelCase__		,				UpperCAmelCase__								=  config_and_inputs
         UpperCAmelCase__								=  {"pixel_values": pixel_values}
         return config, inputs_dict





@require_torch
class 					A							(    UpperCAmelCase_		,    UpperCAmelCase_		,    unittest.TestCase					):
   __UpperCAmelCase			:			int              =      (
       (
           FocalNetModel,
           FocalNetForImageClassification,
           FocalNetForMaskedImageModeling,
           FocalNetBackbone,
       )
       if is_torch_available()
       else ()
   )
   __UpperCAmelCase			:			Union[str, Any]              =      (
       {'feature-extraction': FocalNetModel, 'image-classification': FocalNetForImageClassification}
       if is_torch_available()
       else {}
   )
   __UpperCAmelCase			:			Any              =      False

   __UpperCAmelCase			:			List[Any]              =      False
   __UpperCAmelCase			:			List[str]              =      False
   __UpperCAmelCase			:			Dict              =      False
   __UpperCAmelCase			:			Optional[int]              =      False


   def       lowercase_				(self					:						List[Any]		)						->    str:
         """simple docstring"""



         UpperCAmelCase__								=  FocalNetModelTester(self		)
         UpperCAmelCase__								=  ConfigTester(self	,  config_class=__UpperCAmelCase	,  embed_dim=3_7	,  has_text_modality=__UpperCAmelCase		)


   def       lowercase_				(self					:						Union[str, Any]		)						->    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       lowercase_				(self					:						Tuple		)						->    Optional[int]:
         """simple docstring"""



         return


   def       lowercase_				(self					:						Any		)						->    Union[str, Any]:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__UpperCAmelCase		)


   def       lowercase_				(self					:						Dict		)						->    Any:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_backbone(*__UpperCAmelCase		)


   def       lowercase_				(self					:						Optional[int]		)						->    Tuple:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCAmelCase		)


   def       lowercase_				(self					:						int		)						->    List[str]:
         """simple docstring"""



         UpperCAmelCase__								=  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       lowercase_				(self					:						Optional[Any]		)						->    int:
         """simple docstring"""



         pass


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



         pass


   def       lowercase_				(self					:						int		)						->    Any:
         """simple docstring"""



         UpperCAmelCase__		,				UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()

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


   def       lowercase_				(self					:						str		)						->    Optional[int]:
         """simple docstring"""



         UpperCAmelCase__		,				UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()

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

               UpperCAmelCase__								=  ["pixel_values"]
               self.assertListEqual(arg_names[:1]	,  __UpperCAmelCase		)


   def       lowercase_				(self					:						Union[str, Any]	,  __UpperCAmelCase					:						int	,  __UpperCAmelCase					:						Optional[int]	,  __UpperCAmelCase					:						List[Any]	,  __UpperCAmelCase					:						Union[str, Any]		)						->    Tuple:
         """simple docstring"""



         UpperCAmelCase__								=  model_class(__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()

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

         UpperCAmelCase__								=  outputs.hidden_states

         UpperCAmelCase__								=  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
         UpperCAmelCase__								=  (
             config.patch_size
             if isinstance(config.patch_size	,  collections.abc.Iterable		)
             else (config.patch_size, config.patch_size)
         )

         UpperCAmelCase__								=  (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__								=  outputs.reshaped_hidden_states
         self.assertEqual(len(__UpperCAmelCase		)	,  __UpperCAmelCase		)

         UpperCAmelCase__		,				UpperCAmelCase__		,				UpperCAmelCase__		,				UpperCAmelCase__								=  reshaped_hidden_states[0].shape
         UpperCAmelCase__								=  (
             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       lowercase_				(self					:						List[str]		)						->    Tuple:
         """simple docstring"""



         UpperCAmelCase__		,				UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()

         UpperCAmelCase__								=  (
             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__								=  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"]
               UpperCAmelCase__								=  True

               self.check_hidden_states_output(__UpperCAmelCase	,  __UpperCAmelCase	,  __UpperCAmelCase	,  __UpperCAmelCase		)


   def       lowercase_				(self					:						List[Any]		)						->    Any:
         """simple docstring"""



         UpperCAmelCase__		,				UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()
         UpperCAmelCase__								=  3

         UpperCAmelCase__								=  (
             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__								=  (
             config.patch_size
             if isinstance(config.patch_size	,  collections.abc.Iterable		)
             else (config.patch_size, config.patch_size)
         )

         UpperCAmelCase__								=  image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
         UpperCAmelCase__								=  image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

         for model_class in self.all_model_classes[:-1]:
               UpperCAmelCase__								=  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"]
               UpperCAmelCase__								=  True
               self.check_hidden_states_output(__UpperCAmelCase	,  __UpperCAmelCase	,  __UpperCAmelCase	,  (padded_height, padded_width)		)


   @slow
   def       lowercase_				(self					:						Optional[int]		)						->    Union[str, Any]:
         """simple docstring"""



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





   def       lowercase_				(self					:						Union[str, Any]		)						->    Tuple:
         """simple docstring"""



         UpperCAmelCase__		,				UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()

         UpperCAmelCase__								=  _config_zero_init(__UpperCAmelCase		)
         for model_class in self.all_model_classes:
               UpperCAmelCase__								=  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 					A							(    unittest.TestCase					):


   @cached_property
   def       lowercase_				(self					:						Any		)						->    Optional[int]:
         """simple docstring"""



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





   @slow
   def       lowercase_				(self					:						Tuple		)						->    Optional[int]:
         """simple docstring"""



         UpperCAmelCase__								=  FocalNetForImageClassification.from_pretrained("microsoft/focalnet-tiny"		).to(__UpperCAmelCase		)
         UpperCAmelCase__								=  self.default_image_processor

         UpperCAmelCase__								=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
         UpperCAmelCase__								=  image_processor(images=__UpperCAmelCase	,  return_tensors="pt"		).to(__UpperCAmelCase		)

         # forward pass
         with torch.no_grad():
               UpperCAmelCase__								=  model(**__UpperCAmelCase		)

         # verify the logits
         UpperCAmelCase__								=  torch.Size((1, 1_0_0_0)		)
         self.assertEqual(outputs.logits.shape	,  __UpperCAmelCase		)
         UpperCAmelCase__								=  torch.tensor([0.2166, -0.4368, 0.2191]		).to(__UpperCAmelCase		)
         self.assertTrue(torch.allclose(outputs.logits[0, :3]	,  __UpperCAmelCase	,  atol=1E-4		)		)
         self.assertTrue(outputs.logits.argmax(dim=-1		).item()	,  2_8_1		)





@require_torch
class 					A							(    UpperCAmelCase_		,    unittest.TestCase					):
   __UpperCAmelCase			:			Tuple              =      (FocalNetBackbone,) if is_torch_available() else ()
   __UpperCAmelCase			:			Union[str, Any]              =      FocalNetConfig

   __UpperCAmelCase			:			str              =      False


   def       lowercase_				(self					:						List[str]		)						->    Any:
         """simple docstring"""



         UpperCAmelCase__								=  FocalNetModelTester(self		)




from __future__ import annotations




def        lowerCAmelCase_			(					__A							)					->    list[int]:







      '''simple docstring'''


      if len(__A							) == 0:
            return array

      UpperCAmelCase__		,				UpperCAmelCase__								=  min(__A							), max(__A							)

      # Compute the variables
      UpperCAmelCase__								=  _max - _min + 1
      UpperCAmelCase__		,				UpperCAmelCase__								=  [0] * holes_range, [0] * holes_range

      # Make the sorting.
      for i in array:
            UpperCAmelCase__								=  i - _min
            UpperCAmelCase__								=  i
            holes_repeat[index] += 1

      # Makes the array back by replacing the numbers.
      UpperCAmelCase__								=  0
      for i in range(__A							):
            while holes_repeat[i] > 0:
                  UpperCAmelCase__								=  holes[i]
                  index += 1
                  holes_repeat[i] -= 1

    # Returns the sorted array.
      return array


if __name__ == "__main__":
      import doctest

      doctest.testmod()
      UpperCamelCase__    							= input('Enter numbers separated by comma:\n')
      UpperCamelCase__    							= [int(x) for x in user_input.split(',')]
      print(pigeon_sort(unsorted))



