


"""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 _lowerCAmelCase    :







   def __init__(     self						,     UpperCamelCase__						,     UpperCamelCase__=13						,     UpperCamelCase__=32						,     UpperCamelCase__=2						,     UpperCamelCase__=3						,     UpperCamelCase__=16						,     UpperCamelCase__=[32, 64, 128]						,     UpperCamelCase__=[1, 2, 1]						,     UpperCamelCase__=[2, 2, 4]						,     UpperCamelCase__=2						,     UpperCamelCase__=2.0						,     UpperCamelCase__=True						,     UpperCamelCase__=0.0						,     UpperCamelCase__=0.0						,     UpperCamelCase__=0.1						,     UpperCamelCase__="gelu"						,     UpperCamelCase__=False						,     UpperCamelCase__=True						,     UpperCamelCase__=0.02						,     UpperCamelCase__=1e-5						,     UpperCamelCase__=True						,     UpperCamelCase__=None						,     UpperCamelCase__=True						,     UpperCamelCase__=10						,     UpperCamelCase__=8						,     UpperCamelCase__=["stage1", "stage2"]						,     UpperCamelCase__=[1, 2]						,     )     ->		List[str]:

       '''simple docstring'''




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







   def 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




       snake_case   :				Tuple				 =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

       snake_case   :				Any				 =   self.get_config()

       return config, pixel_values, labels







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

       '''simple docstring'''




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







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		List[Any]:

       '''simple docstring'''




       snake_case   :				int				 =   FocalNetModel(config=UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()
       snake_case   :				str				 =   model(UpperCamelCase__						)

       snake_case   :				Optional[Any]				 =   ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths						) - 1))
       snake_case   :				str				 =   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 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		int:

       '''simple docstring'''




       snake_case   :				List[Any]				 =   FocalNetBackbone(config=UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()
       snake_case   :				Dict				 =   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
       snake_case   :				Union[str, Any]				 =   None
       snake_case   :				Union[str, Any]				 =   FocalNetBackbone(config=UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()
       snake_case   :				Union[str, Any]				 =   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 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		Dict:

       '''simple docstring'''




       snake_case   :				Dict				 =   FocalNetForMaskedImageModeling(config=UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()
       snake_case   :				Optional[Any]				 =   model(UpperCamelCase__						)
       self.parent.assertEqual(
           result.reconstruction.shape						,     (self.batch_size, self.num_channels, self.image_size, self.image_size)						)

       # test greyscale images
       snake_case   :				Optional[int]				 =   1
       snake_case   :				List[str]				 =   FocalNetForMaskedImageModeling(UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()

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







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		List[Any]:

       '''simple docstring'''




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

       # test greyscale images
       snake_case   :				List[Any]				 =   1
       snake_case   :				Any				 =   FocalNetForImageClassification(UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()

       snake_case   :				int				 =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
       snake_case   :				Optional[Any]				 =   model(UpperCamelCase__						)
       self.parent.assertEqual(result.logits.shape						,     (self.batch_size, self.type_sequence_label_size)						)







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

       '''simple docstring'''




       snake_case   :				str				 =   self.prepare_config_and_inputs()

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







@require_torch
class _lowerCAmelCase    (				snake_case_   ,							snake_case_   ,							unittest.TestCase     ):
   __UpperCAmelCase			:	Tuple							  =  (
       (
           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			:	Tuple							  =  False

   __UpperCAmelCase			:	Union[str, Any]							  =  False
   __UpperCAmelCase			:	Union[str, Any]							  =  False
   __UpperCAmelCase			:	Dict							  =  False
   __UpperCAmelCase			:	int							  =  False







   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       snake_case   :				Tuple				 =   FocalNetModelTester(self						)
       snake_case   :				Optional[int]				 =   ConfigTester(self						,     config_class=UpperCamelCase__						,     embed_dim=37						,     has_text_modality=UpperCamelCase__						)







   def 			lowerCamelCase		(     self						)     ->		Union[str, 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 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




       return







   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''simple docstring'''




       snake_case   :				List[str]				 =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*UpperCamelCase__						)







   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''simple docstring'''




       snake_case   :				Tuple				 =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_backbone(*UpperCamelCase__						)







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

       '''simple docstring'''




       snake_case   :				Union[str, Any]				 =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase__						)







   def 			lowerCamelCase		(     self						)     ->		List[Any]:

       '''simple docstring'''




       snake_case   :				Any				 =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__						)







   @unittest.skip(reason="FocalNet does not use inputs_embeds"						)
   def 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




       pass







   @unittest.skip(reason="FocalNet does not use feedforward chunking"						)
   def 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




       pass







   def 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




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

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







   def 			lowerCamelCase		(     self						)     ->		Optional[Any]:

       '''simple docstring'''




       snake_case ,snake_case   :				Dict				 =   self.model_tester.prepare_config_and_inputs_for_common()

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

           snake_case   :				List[str]				 =   ["pixel_values"]
           self.assertListEqual(arg_names[:1]						,     UpperCamelCase__						)







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		str:

       '''simple docstring'''




       snake_case   :				List[Any]				 =   model_class(UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()

       with torch.no_grad():
           snake_case   :				List[str]				 =   model(**self._prepare_for_class(UpperCamelCase__						,     UpperCamelCase__						)						)

       snake_case   :				Dict				 =   outputs.hidden_states

       snake_case   :				Any				 =   getattr(
           self.model_tester						,     "expected_num_hidden_layers"						,     len(self.model_tester.depths						) + 1						)
       self.assertEqual(len(UpperCamelCase__						)						,     UpperCamelCase__						)

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

       snake_case   :				Union[str, Any]				 =   (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

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

       snake_case   :				Union[str, Any]				 =   outputs.reshaped_hidden_states
       self.assertEqual(len(UpperCamelCase__						)						,     UpperCamelCase__						)

       snake_case ,snake_case ,snake_case ,snake_case   :				Union[str, Any]				 =   reshaped_hidden_states[0].shape
       snake_case   :				List[str]				 =   (
           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 			lowerCamelCase		(     self						)     ->		Optional[Any]:

       '''simple docstring'''




       snake_case ,snake_case   :				str				 =   self.model_tester.prepare_config_and_inputs_for_common()

       snake_case   :				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]:
           snake_case   :				Tuple				 =   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"]
           snake_case   :				Union[str, Any]				 =   True

           self.check_hidden_states_output(UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)







   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       snake_case ,snake_case   :				List[Any]				 =   self.model_tester.prepare_config_and_inputs_for_common()
       snake_case   :				Dict				 =   3

       snake_case   :				Union[str, 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)
       )
       snake_case   :				Dict				 =   (
           config.patch_size
           if isinstance(config.patch_size						,     collections.abc.Iterable						)
           else (config.patch_size, config.patch_size)
       )

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

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







   @slow
   def 			lowerCamelCase		(     self						)     ->		Optional[Any]:

       '''simple docstring'''




       for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           snake_case   :				str				 =   FocalNetModel.from_pretrained(UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)







   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       snake_case ,snake_case   :				Dict				 =   self.model_tester.prepare_config_and_inputs_for_common()

       snake_case   :				List[Any]				 =   _config_zero_init(UpperCamelCase__						)
       for model_class in self.all_model_classes:
           snake_case   :				Optional[int]				 =   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 _lowerCAmelCase    (				unittest.TestCase     ):







   @cached_property
   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




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







   @slow
   def 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




       snake_case   :				List[str]				 =   FocalNetForImageClassification.from_pretrained("microsoft/focalnet-tiny"						).to(UpperCamelCase__						)
       snake_case   :				int				 =   self.default_image_processor

       snake_case   :				Optional[Any]				 =   Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
       snake_case   :				Any				 =   image_processor(images=UpperCamelCase__						,     return_tensors="pt"						).to(UpperCamelCase__						)

       # forward pass
       with torch.no_grad():
           snake_case   :				Dict				 =   model(**UpperCamelCase__						)

       # verify the logits
       snake_case   :				Optional[Any]				 =   torch.Size((1, 1000)						)
       self.assertEqual(outputs.logits.shape						,     UpperCamelCase__						)
       snake_case   :				Tuple				 =   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()						,     281						)







@require_torch
class _lowerCAmelCase    (				snake_case_   ,							unittest.TestCase     ):
   __UpperCAmelCase			:	Union[str, Any]							  =  (FocalNetBackbone,) if is_torch_available() else ()
   __UpperCAmelCase			:	List[Any]							  =  FocalNetConfig

   __UpperCAmelCase			:	Union[str, Any]							  =  False







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

       '''simple docstring'''




       snake_case   :				Tuple				 =   FocalNetModelTester(self						)









"""simple docstring"""



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__snake_case             =   {
    """configuration_conditional_detr""": [
        """CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """ConditionalDetrConfig""",
        """ConditionalDetrOnnxConfig""",
    ]
}

try:
  if not is_vision_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __snake_case             =   ["""ConditionalDetrFeatureExtractor"""]
  __snake_case             =   ["""ConditionalDetrImageProcessor"""]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __snake_case             =   [
      """CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """ConditionalDetrForObjectDetection""",
      """ConditionalDetrForSegmentation""",
      """ConditionalDetrModel""",
      """ConditionalDetrPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_conditional_detr import (
      CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP,
      ConditionalDetrConfig,
      ConditionalDetrOnnxConfig,
  )

  try:
    if not is_vision_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .feature_extraction_conditional_detr import ConditionalDetrFeatureExtractor
    from .image_processing_conditional_detr import ConditionalDetrImageProcessor

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_conditional_detr import (
        CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST,
        ConditionalDetrForObjectDetection,
        ConditionalDetrForSegmentation,
        ConditionalDetrModel,
        ConditionalDetrPreTrainedModel,
    )

else:
  import sys

  __snake_case             =   _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





