
'''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      __SCREAMING_SNAKE_CASE      :




 def __init__(      self	:							Optional[int]					,      __lowercase	:							Union[str, Any]					,      __lowercase	:							Optional[Any]=13					,      __lowercase	:							str=32					,      __lowercase	:							int=2					,      __lowercase	:							str=3					,      __lowercase	:							Tuple=16					,      __lowercase	:							int=[32, 64, 1_28]					,      __lowercase	:							Union[str, Any]=[1, 2, 1]					,      __lowercase	:							Union[str, Any]=[2, 2, 4]					,      __lowercase	:							Any=2					,      __lowercase	:							int=2.0					,      __lowercase	:							Optional[int]=True					,      __lowercase	:							Any=0.0					,      __lowercase	:							Union[str, Any]=0.0					,      __lowercase	:							Tuple=0.1					,      __lowercase	:							Optional[Any]="gelu"					,      __lowercase	:							Optional[Any]=False					,      __lowercase	:							Dict=True					,      __lowercase	:							List[Any]=0.02					,      __lowercase	:							Optional[Any]=1e-5					,      __lowercase	:							List[Any]=True					,      __lowercase	:							str=None					,      __lowercase	:							str=True					,      __lowercase	:							str=10					,      __lowercase	:							List[Any]=8					,      __lowercase	:							Any=["stage1", "stage2"]					,      __lowercase	:							List[str]=[1, 2]					,      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=parent
       SCREAMING_SNAKE_CASE__     :				str									=batch_size
       SCREAMING_SNAKE_CASE__     :				str									=image_size
       SCREAMING_SNAKE_CASE__     :				int									=patch_size
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=num_channels
       SCREAMING_SNAKE_CASE__     :				Dict									=embed_dim
       SCREAMING_SNAKE_CASE__     :				str									=hidden_sizes
       SCREAMING_SNAKE_CASE__     :				Any									=depths
       SCREAMING_SNAKE_CASE__     :				List[str]									=num_heads
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=window_size
       SCREAMING_SNAKE_CASE__     :				Dict									=mlp_ratio
       SCREAMING_SNAKE_CASE__     :				Any									=qkv_bias
       SCREAMING_SNAKE_CASE__     :				Dict									=hidden_dropout_prob
       SCREAMING_SNAKE_CASE__     :				str									=attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE__     :				List[Any]									=drop_path_rate
       SCREAMING_SNAKE_CASE__     :				int									=hidden_act
       SCREAMING_SNAKE_CASE__     :				List[Any]									=use_absolute_embeddings
       SCREAMING_SNAKE_CASE__     :				Any									=patch_norm
       SCREAMING_SNAKE_CASE__     :				List[Any]									=layer_norm_eps
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=initializer_range
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=is_training
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=scope
       SCREAMING_SNAKE_CASE__     :				int									=use_labels
       SCREAMING_SNAKE_CASE__     :				Dict									=type_sequence_label_size
       SCREAMING_SNAKE_CASE__     :				List[str]									=encoder_stride
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=out_features
       SCREAMING_SNAKE_CASE__     :				List[str]									=out_indices




 def      __magic_name__	(      self	:							Union[str, Any]      )	->						List[Any]:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

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

       SCREAMING_SNAKE_CASE__     :				Any									=self.get_config()

       return config, pixel_values, labels




 def      __magic_name__	(      self	:							Any      )	->						Tuple:
       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      __magic_name__	(      self	:							Optional[Any]					,      __lowercase	:							Any					,      __lowercase	:							Any					,      __lowercase	:							Optional[Any]      )	->						int:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=FocalNetModel(config=__lowercase      )
       model.to(__lowercase      )
       model.eval()
       SCREAMING_SNAKE_CASE__     :				Tuple									=model(__lowercase      )

       SCREAMING_SNAKE_CASE__     :				int									=((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths      ) - 1))
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=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      __magic_name__	(      self	:							int					,      __lowercase	:							str					,      __lowercase	:							Optional[int]					,      __lowercase	:							str      )	->						List[str]:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=FocalNetBackbone(config=__lowercase      )
       model.to(__lowercase      )
       model.eval()
       SCREAMING_SNAKE_CASE__     :				Dict									=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
       SCREAMING_SNAKE_CASE__     :				Dict									=None
       SCREAMING_SNAKE_CASE__     :				List[str]									=FocalNetBackbone(config=__lowercase      )
       model.to(__lowercase      )
       model.eval()
       SCREAMING_SNAKE_CASE__     :				int									=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      __magic_name__	(      self	:							Dict					,      __lowercase	:							Optional[Any]					,      __lowercase	:							Optional[Any]					,      __lowercase	:							Any      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=FocalNetForMaskedImageModeling(config=__lowercase      )
       model.to(__lowercase      )
       model.eval()
       SCREAMING_SNAKE_CASE__     :				List[Any]									=model(__lowercase      )
       self.parent.assertEqual(
           result.reconstruction.shape					,      (self.batch_size, self.num_channels, self.image_size, self.image_size)      )

       # test greyscale images
       SCREAMING_SNAKE_CASE__     :				Any									=1
       SCREAMING_SNAKE_CASE__     :				str									=FocalNetForMaskedImageModeling(__lowercase      )
       model.to(__lowercase      )
       model.eval()

       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=model(__lowercase      )
       self.parent.assertEqual(result.reconstruction.shape					,      (self.batch_size, 1, self.image_size, self.image_size)      )




 def      __magic_name__	(      self	:							str					,      __lowercase	:							Optional[Any]					,      __lowercase	:							Optional[int]					,      __lowercase	:							str      )	->						Optional[int]:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=self.type_sequence_label_size
       SCREAMING_SNAKE_CASE__     :				int									=FocalNetForImageClassification(__lowercase      )
       model.to(__lowercase      )
       model.eval()
       SCREAMING_SNAKE_CASE__     :				List[str]									=model(__lowercase					,      labels=__lowercase      )
       self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.type_sequence_label_size)      )

       # test greyscale images
       SCREAMING_SNAKE_CASE__     :				Any									=1
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=FocalNetForImageClassification(__lowercase      )
       model.to(__lowercase      )
       model.eval()

       SCREAMING_SNAKE_CASE__     :				Dict									=floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
       SCREAMING_SNAKE_CASE__     :				List[Any]									=model(__lowercase      )
       self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.type_sequence_label_size)      )




 def      __magic_name__	(      self	:							Optional[int]      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				str									=self.prepare_config_and_inputs()

       SCREAMING_SNAKE_CASE__     :				Dict									=config_and_inputs
       SCREAMING_SNAKE_CASE__     :				int									={"pixel_values": pixel_values}
       return config, inputs_dict






@require_torch
class      __SCREAMING_SNAKE_CASE      (			_UpperCamelCase	,				_UpperCamelCase	,				unittest.TestCase				):
 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      __magic_name__	(      self	:							Union[str, Any]      )	->						str:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=FocalNetModelTester(self      )
       SCREAMING_SNAKE_CASE__     :				Any									=ConfigTester(self					,      config_class=__lowercase					,      embed_dim=37					,      has_text_modality=__lowercase      )




 def      __magic_name__	(      self	:							int      )	->						Any:
       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      __magic_name__	(      self	:							Union[str, Any]      )	->						Any:
       return




 def      __magic_name__	(      self	:							List[Any]      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__     :				List[Any]									=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__lowercase      )




 def      __magic_name__	(      self	:							List[Any]      )	->						int:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_backbone(*__lowercase      )




 def      __magic_name__	(      self	:							Optional[int]      )	->						Any:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*__lowercase      )




 def      __magic_name__	(      self	:							Dict      )	->						Optional[int]:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=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      __magic_name__	(      self	:							Union[str, Any]      )	->						Optional[int]:
       pass




 @unittest.skip(reason='''FocalNet does not use feedforward chunking'''      )
 def      __magic_name__	(      self	:							Optional[int]      )	->						Dict:
       pass




 def      __magic_name__	(      self	:							Any      )	->						List[str]:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=self.model_tester.prepare_config_and_inputs_for_common()

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




 def      __magic_name__	(      self	:							Any      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=self.model_tester.prepare_config_and_inputs_for_common()

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

             SCREAMING_SNAKE_CASE__     :				Any									=["pixel_values"]
             self.assertListEqual(arg_names[:1]					,      __lowercase      )




 def      __magic_name__	(      self	:							Any					,      __lowercase	:							Any					,      __lowercase	:							int					,      __lowercase	:							int					,      __lowercase	:							int      )	->						Any:
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=model_class(__lowercase      )
       model.to(__lowercase      )
       model.eval()

       with torch.no_grad():
             SCREAMING_SNAKE_CASE__     :				List[Any]									=model(**self._prepare_for_class(__lowercase					,      __lowercase      )      )

       SCREAMING_SNAKE_CASE__     :				int									=outputs.hidden_states

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

       SCREAMING_SNAKE_CASE__     :				Dict									=(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]					,      )

       SCREAMING_SNAKE_CASE__     :				int									=outputs.reshaped_hidden_states
       self.assertEqual(len(__lowercase      )					,      __lowercase      )

       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=reshaped_hidden_states[0].shape
       SCREAMING_SNAKE_CASE__     :				str									=(
           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      __magic_name__	(      self	:							int      )	->						str:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=self.model_tester.prepare_config_and_inputs_for_common()

       SCREAMING_SNAKE_CASE__     :				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)
       )

       for model_class in self.all_model_classes[:-1]:
             SCREAMING_SNAKE_CASE__     :				Optional[int]									=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"]
             SCREAMING_SNAKE_CASE__     :				Any									=True

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




 def      __magic_name__	(      self	:							Any      )	->						List[str]:
       SCREAMING_SNAKE_CASE__     :				Tuple									=self.model_tester.prepare_config_and_inputs_for_common()
       SCREAMING_SNAKE_CASE__     :				Any									=3

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

       SCREAMING_SNAKE_CASE__     :				List[Any]									=image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
       SCREAMING_SNAKE_CASE__     :				Dict									=image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

       for model_class in self.all_model_classes[:-1]:
             SCREAMING_SNAKE_CASE__     :				Any									=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"]
             SCREAMING_SNAKE_CASE__     :				List[str]									=True
             self.check_hidden_states_output(__lowercase					,      __lowercase					,      __lowercase					,      (padded_height, padded_width)      )




 @slow
 def      __magic_name__	(      self	:							Optional[int]      )	->						Any:
       for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE__     :				List[Any]									=FocalNetModel.from_pretrained(__lowercase      )
             self.assertIsNotNone(__lowercase      )




 def      __magic_name__	(      self	:							Dict      )	->						List[Any]:
       SCREAMING_SNAKE_CASE__     :				List[str]									=self.model_tester.prepare_config_and_inputs_for_common()

       SCREAMING_SNAKE_CASE__     :				str									=_config_zero_init(__lowercase      )
       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE__     :				Optional[Any]									=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      __SCREAMING_SNAKE_CASE      (			unittest.TestCase				):




 @cached_property
 def      __magic_name__	(      self	:							str      )	->						int:
       # TODO update organization
       return AutoImageProcessor.from_pretrained('''microsoft/focalnet-tiny'''      ) if is_vision_available() else None




 @slow
 def      __magic_name__	(      self	:							Tuple      )	->						str:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=FocalNetForImageClassification.from_pretrained('''microsoft/focalnet-tiny'''      ).to(__lowercase      )
       SCREAMING_SNAKE_CASE__     :				Tuple									=self.default_image_processor

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

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

       # verify the logits
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=torch.Size((1, 10_00)      )
       self.assertEqual(outputs.logits.shape					,      __lowercase      )
       SCREAMING_SNAKE_CASE__     :				List[str]									=torch.tensor([0.2166, -0.4368, 0.2191]      ).to(__lowercase      )
       self.assertTrue(torch.allclose(outputs.logits[0, :3]					,      __lowercase					,      atol=1e-4      )      )
       self.assertTrue(outputs.logits.argmax(dim=-1      ).item()					,      2_81      )






@require_torch
class      __SCREAMING_SNAKE_CASE      (			_UpperCamelCase	,				unittest.TestCase				):
 snake_case_  							=							(FocalNetBackbone,) if is_torch_available() else ()
 snake_case_  							=							FocalNetConfig

 snake_case_  							=							False




 def      __magic_name__	(      self	:							List[Any]      )	->						str:
       SCREAMING_SNAKE_CASE__     :				Any									=FocalNetModelTester(self      )







'''simple docstring'''
def  _a(  UpperCamelCase__				:						str,	UpperCamelCase__				:						str							):




      '''simple docstring'''






      SCREAMING_SNAKE_CASE__     :				Optional[int]									=len(UpperCamelCase__							)
      SCREAMING_SNAKE_CASE__     :				Optional[Any]									=len(UpperCamelCase__							)
      SCREAMING_SNAKE_CASE__     :				Tuple									=[[False for _ in range(m + 1							)] for _ in range(n + 1							)]
      SCREAMING_SNAKE_CASE__     :				List[Any]									=True
      for i in range(UpperCamelCase__							):
            for j in range(m + 1							):
                  if dp[i][j]:
                        if j < m and a[i].upper() == b[j]:
                              SCREAMING_SNAKE_CASE__     :				Optional[int]									=True
                        if a[i].islower():
                              SCREAMING_SNAKE_CASE__     :				List[Any]									=True
      return dp[n][m]


if __name__ == "__main__":
  import doctest

  doctest.testmod()