

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

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





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

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





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

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

         A_							       =		self.get_config()

         return config, pixel_values, labels

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





         A_							       =		FocalNetModel(config=lowerCamelCase__       )
         model.to(lowerCamelCase__       )
         model.eval()
         A_							       =		model(lowerCamelCase__       )

         A_							       =		((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths       ) - 1))
         A_							       =		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     UpperCamelCase					(				self	:    Union[str, Any]			,						lowerCamelCase__	:    str			,						lowerCamelCase__	:    str			,						lowerCamelCase__	:    str       ) ->			Dict:
         """simple docstring"""





         A_							       =		FocalNetBackbone(config=lowerCamelCase__       )
         model.to(lowerCamelCase__       )
         model.eval()
         A_							       =		model(lowerCamelCase__       )

         # 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
         A_							       =		None
         A_							       =		FocalNetBackbone(config=lowerCamelCase__       )
         model.to(lowerCamelCase__       )
         model.eval()
         A_							       =		model(lowerCamelCase__       )

         # 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     UpperCamelCase					(				self	:    Union[str, Any]			,						lowerCamelCase__	:    str			,						lowerCamelCase__	:    Any			,						lowerCamelCase__	:    Optional[Any]       ) ->			Optional[int]:
         """simple docstring"""





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

         # test greyscale images
         A_							       =		1
         A_							       =		FocalNetForMaskedImageModeling(lowerCamelCase__       )
         model.to(lowerCamelCase__       )
         model.eval()

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

   def     UpperCamelCase					(				self	:    Tuple			,						lowerCamelCase__	:    str			,						lowerCamelCase__	:    Dict			,						lowerCamelCase__	:    Tuple       ) ->			int:
         """simple docstring"""





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

         # test greyscale images
         A_							       =		1
         A_							       =		FocalNetForImageClassification(lowerCamelCase__       )
         model.to(lowerCamelCase__       )
         model.eval()

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





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





         A_							       =		self.prepare_config_and_inputs()

         A_       ,A_       ,A_							       =		config_and_inputs
         A_							       =		{'''pixel_values''': pixel_values}
         return config, inputs_dict



@require_torch
class    _lowercase				(					__lowerCamelCase,__lowerCamelCase,unittest.TestCase			):
   _lowercase     :       Tuple  					=    (
       (
           FocalNetModel,
           FocalNetForImageClassification,
           FocalNetForMaskedImageModeling,
           FocalNetBackbone,
       )
       if is_torch_available()
       else ()
   )
   _lowercase     :       Optional[Any]  					=    (
       {'feature-extraction': FocalNetModel, 'image-classification': FocalNetForImageClassification}
       if is_torch_available()
       else {}
   )
   _lowercase     :       Optional[int]  					=    False

   _lowercase     :       List[Any]  					=    False
   _lowercase     :       Optional[Any]  					=    False
   _lowercase     :       Union[str, Any]  					=    False
   _lowercase     :       Optional[int]  					=    False

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





         A_							       =		FocalNetModelTester(self       )
         A_							       =		ConfigTester(self			,						config_class=lowerCamelCase__			,						embed_dim=3_7			,						has_text_modality=lowerCamelCase__       )

   def     UpperCamelCase					(				self	:    List[Any]       ) ->			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     UpperCamelCase					(				self	:    Any       ) ->			List[Any]:
         """simple docstring"""





         return

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





         A_							       =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*lowerCamelCase__       )

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





         A_							       =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_backbone(*lowerCamelCase__       )

   def     UpperCamelCase					(				self	:    Tuple       ) ->			List[str]:
         """simple docstring"""





         A_							       =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase__       )

   def     UpperCamelCase					(				self	:    int       ) ->			int:
         """simple docstring"""





         A_							       =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__       )

   @unittest.skip(reason='''FocalNet does not use inputs_embeds'''       )
   def     UpperCamelCase					(				self	:    Optional[Any]       ) ->			Optional[Any]:
         """simple docstring"""





         pass

   @unittest.skip(reason='''FocalNet does not use feedforward chunking'''       )
   def     UpperCamelCase					(				self	:    Union[str, Any]       ) ->			int:
         """simple docstring"""





         pass

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





         A_       ,A_							       =		self.model_tester.prepare_config_and_inputs_for_common()

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

   def     UpperCamelCase					(				self	:    int       ) ->			Dict:
         """simple docstring"""





         A_       ,A_							       =		self.model_tester.prepare_config_and_inputs_for_common()

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

               A_							       =		['''pixel_values''']
               self.assertListEqual(arg_names[:1]			,						lowerCamelCase__       )

   def     UpperCamelCase					(				self	:    str			,						lowerCamelCase__	:    Optional[int]			,						lowerCamelCase__	:    List[str]			,						lowerCamelCase__	:    Union[str, Any]			,						lowerCamelCase__	:    Any       ) ->			int:
         """simple docstring"""





         A_							       =		model_class(lowerCamelCase__       )
         model.to(lowerCamelCase__       )
         model.eval()

         with torch.no_grad():
               A_							       =		model(**self._prepare_for_class(lowerCamelCase__			,						lowerCamelCase__       )       )

         A_							       =		outputs.hidden_states

         A_							       =		getattr(
             self.model_tester			,						'''expected_num_hidden_layers'''			,						len(self.model_tester.depths       ) + 1       )
         self.assertEqual(len(lowerCamelCase__       )			,						lowerCamelCase__       )

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

         A_							       =		(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]			,						)

         A_							       =		outputs.reshaped_hidden_states
         self.assertEqual(len(lowerCamelCase__       )			,						lowerCamelCase__       )

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

   def     UpperCamelCase					(				self	:    Optional[Any]       ) ->			Tuple:
         """simple docstring"""





         A_       ,A_							       =		self.model_tester.prepare_config_and_inputs_for_common()

         A_							       =		(
             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]:
               A_							       =		True
               self.check_hidden_states_output(lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__       )

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               A_							       =		True

               self.check_hidden_states_output(lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__       )

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





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

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

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

         for model_class in self.all_model_classes[:-1]:
               A_							       =		True
               self.check_hidden_states_output(lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						(padded_height, padded_width)       )

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               A_							       =		True
               self.check_hidden_states_output(lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						(padded_height, padded_width)       )

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





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





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





         A_       ,A_							       =		self.model_tester.prepare_config_and_inputs_for_common()

         A_							       =		_config_zero_init(lowerCamelCase__       )
         for model_class in self.all_model_classes:
               A_							       =		model_class(config=lowerCamelCase__       )
               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    _lowercase				(					unittest.TestCase			):

   @cached_property
   def     UpperCamelCase					(				self	:    Any       ) ->			int:
         """simple docstring"""





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





   @slow
   def     UpperCamelCase					(				self	:    Optional[Any]       ) ->			Any:
         """simple docstring"""





         A_							       =		FocalNetForImageClassification.from_pretrained('''microsoft/focalnet-tiny'''       ).to(lowerCamelCase__       )
         A_							       =		self.default_image_processor

         A_							       =		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
         A_							       =		image_processor(images=lowerCamelCase__			,						return_tensors='''pt'''       ).to(lowerCamelCase__       )

         # forward pass
         with torch.no_grad():
               A_							       =		model(**lowerCamelCase__       )

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



@require_torch
class    _lowercase				(					__lowerCamelCase,unittest.TestCase			):
   _lowercase     :       Optional[Any]  					=    (FocalNetBackbone,) if is_torch_available() else ()
   _lowercase     :       str  					=    FocalNetConfig

   _lowercase     :       str  					=    False

   def     UpperCamelCase					(				self	:    Optional[Any]       ) ->			Any:
         """simple docstring"""





         A_							       =		FocalNetModelTester(self       )








__lowercase				      =						[4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
__lowercase				      =						[3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
__lowercase				      =						{
    0: """Sunday""",
    1: """Monday""",
    2: """Tuesday""",
    3: """Wednesday""",
    4: """Thursday""",
    5: """Friday""",
    6: """Saturday""",
}







def 							_lowerCamelCase   (			SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE   ):
      '''simple docstring'''







      assert len(str(SCREAMING_SNAKE_CASE   )   ) > 2, "year should be in YYYY format"
      assert 1 <= month <= 12, "month should be between 1 to 12"
      assert 1 <= day <= 31, "day should be between 1 to 31"

      # Doomsday algorithm:
      A_							       =		year // 100
      A_							       =		(5 * (century % 4) + 2) % 7
      A_							       =		year % 100
      A_							       =		centurian % 12
      A_							       =		(
          (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor
      ) % 7
      A_							       =		(
          DOOMSDAY_NOT_LEAP[month - 1]
          if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0)
          else DOOMSDAY_LEAP[month - 1]
      )
      A_							       =		(dooms_day + day - day_anchor) % 7
      return WEEK_DAY_NAMES[week_day]


if __name__ == "__main__":
      import doctest

      doctest.testmod()





