

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						:







				"""simple docstring"""





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





								'''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 						snake_case__					(				self			:	Tuple		)->   str:





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





								'''simple docstring'''







								A__   				=       FocalNetModel(config=UpperCamelCase__		)
								model.to(UpperCamelCase__		)
								model.eval()
								A__   				=       model(UpperCamelCase__		)

								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 						snake_case__					(				self			:	List[Any],lowercase_			:	Optional[Any],lowercase_			:	int,lowercase_			:	Dict		)->   Dict:





								'''simple docstring'''







								A__   				=       FocalNetBackbone(config=UpperCamelCase__		)
								model.to(UpperCamelCase__		)
								model.eval()
								A__   				=       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
								A__   				=       None
								A__   				=       FocalNetBackbone(config=UpperCamelCase__		)
								model.to(UpperCamelCase__		)
								model.eval()
								A__   				=       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 						snake_case__					(				self			:	Tuple,lowercase_			:	Any,lowercase_			:	List[str],lowercase_			:	List[Any]		)->   int:





								'''simple docstring'''







								A__   				=       FocalNetForMaskedImageModeling(config=UpperCamelCase__		)
								model.to(UpperCamelCase__		)
								model.eval()
								A__   				=       model(UpperCamelCase__		)
								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(UpperCamelCase__		)
								model.to(UpperCamelCase__		)
								model.eval()

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


				def 						snake_case__					(				self			:	List[str],lowercase_			:	Dict,lowercase_			:	Union[str, Any],lowercase_			:	Union[str, Any]		)->   Any:





								'''simple docstring'''







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

								# test greyscale images
								A__   				=       1
								A__   				=       FocalNetForImageClassification(UpperCamelCase__		)
								model.to(UpperCamelCase__		)
								model.eval()

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


				def 						snake_case__					(				self			:	Dict		)->   Optional[int]:





								'''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 			A						(							__a   ,							__a   ,							unittest.TestCase			):







				"""simple docstring"""





				lowerCamelCase		     =							(
				    (
				        FocalNetModel,
				        FocalNetForImageClassification,
				        FocalNetForMaskedImageModeling,
				        FocalNetBackbone,
				    )
				    if is_torch_available()
				    else ()
				)
				lowerCamelCase		     =							(
				    {'feature-extraction': FocalNetModel, 'image-classification': FocalNetForImageClassification}
				    if is_torch_available()
				    else {}
				)
				lowerCamelCase		     =							False

				lowerCamelCase		     =							False
				lowerCamelCase		     =							False
				lowerCamelCase		     =							False
				lowerCamelCase		     =							False


				def 						snake_case__					(				self			:	int		)->   List[str]:





								'''simple docstring'''







								A__   				=       FocalNetModelTester(self		)
								A__   				=       ConfigTester(self,config_class=UpperCamelCase__,embed_dim=3_7,has_text_modality=UpperCamelCase__		)


				def 						snake_case__					(				self			:	Tuple		)->   Optional[int]:





								'''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 						snake_case__					(				self			:	str		)->   int:





								'''simple docstring'''







								return


				def 						snake_case__					(				self			:	Any		)->   Optional[int]:





								'''simple docstring'''







								A__   				=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*UpperCamelCase__		)


				def 						snake_case__					(				self			:	str		)->   Any:





								'''simple docstring'''







								A__   				=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_backbone(*UpperCamelCase__		)


				def 						snake_case__					(				self			:	Dict		)->   Optional[Any]:





								'''simple docstring'''







								A__   				=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase__		)


				def 						snake_case__					(				self			:	Dict		)->   List[str]:





								'''simple docstring'''







								A__   				=       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 						snake_case__					(				self			:	Tuple		)->   List[Any]:





								'''simple docstring'''







								pass


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





								'''simple docstring'''







								pass


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





								'''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(UpperCamelCase__		)
												self.assertIsInstance(model.get_input_embeddings(),(nn.Module)		)
												A__   				=       model.get_output_embeddings()
												self.assertTrue(x is None or isinstance(UpperCamelCase__,nn.Linear		)		)


				def 						snake_case__					(				self			:	str		)->   Tuple:





								'''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(UpperCamelCase__		)
												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],UpperCamelCase__		)


				def 						snake_case__					(				self			:	List[str],lowercase_			:	Any,lowercase_			:	Any,lowercase_			:	Dict,lowercase_			:	Union[str, Any]		)->   Dict:





								'''simple docstring'''







								A__   				=       model_class(UpperCamelCase__		)
								model.to(UpperCamelCase__		)
								model.eval()

								with torch.no_grad():
												A__   				=       model(**self._prepare_for_class(UpperCamelCase__,UpperCamelCase__		)		)

								A__   				=       outputs.hidden_states

								A__   				=       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
								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(UpperCamelCase__		),UpperCamelCase__		)

								A__				,						A__				,						A__				,						A__   				=       reshaped_hidden_states[0].shape
								A__   				=       (
								    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 						snake_case__					(				self			:	Dict		)->   int:





								'''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(UpperCamelCase__,UpperCamelCase__,UpperCamelCase__,UpperCamelCase__		)

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

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


				def 						snake_case__					(				self			:	Union[str, Any]		)->   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(UpperCamelCase__,UpperCamelCase__,UpperCamelCase__,(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(UpperCamelCase__,UpperCamelCase__,UpperCamelCase__,(padded_height, padded_width)		)


				@slow
				def 						snake_case__					(				self			:	Union[str, Any]		)->   List[Any]:





								'''simple docstring'''







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


				def 						snake_case__					(				self			:	int		)->   Any:





								'''simple docstring'''







								A__				,						A__   				=       self.model_tester.prepare_config_and_inputs_for_common()

								A__   				=       _config_zero_init(UpperCamelCase__		)
								for model_class in self.all_model_classes:
												A__   				=       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			):







				"""simple docstring"""





				@cached_property
				def 						snake_case__					(				self			:	str		)->   Dict:





								'''simple docstring'''







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


				@slow
				def 						snake_case__					(				self			:	List[Any]		)->   str:





								'''simple docstring'''







								A__   				=       FocalNetForImageClassification.from_pretrained('microsoft/focalnet-tiny'		).to(UpperCamelCase__		)
								A__   				=       self.default_image_processor

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

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

								# verify the logits
								A__   				=       torch.Size((1, 1_0_0_0)		)
								self.assertEqual(outputs.logits.shape,UpperCamelCase__		)
								A__   				=       torch.tensor([0.2_166, -0.4_368, 0.2_191]		).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						(							__a   ,							unittest.TestCase			):







				"""simple docstring"""





				lowerCamelCase		     =							(FocalNetBackbone,) if is_torch_available() else ()
				lowerCamelCase		     =							FocalNetConfig

				lowerCamelCase		     =							False


				def 						snake_case__					(				self			:	Optional[int]		)->   Dict:





								'''simple docstring'''







								A__   				=       FocalNetModelTester(self		)




import numpy as np

from transformers import Pipeline
def 						_snake_case(   SCREAMING_SNAKE_CASE__      :     int				)							->				int:





    '''simple docstring'''






    A__   				=       np.max(SCREAMING_SNAKE_CASE__   ,      axis=-1   ,      keepdims=SCREAMING_SNAKE_CASE__				)
    A__   				=       np.exp(outputs - maxes				)
    return shifted_exp / shifted_exp.sum(axis=-1   ,      keepdims=SCREAMING_SNAKE_CASE__				)


class 			A						(							_UpperCAmelCase			):







    """simple docstring"""





    def 						snake_case__					(				self			:	Dict,**lowercase_			:	Tuple		)->   Tuple:





        '''simple docstring'''







        A__   				=       {}
        if "second_text" in kwargs:
            A__   				=       kwargs['second_text']
        return preprocess_kwargs, {}, {}


    def 						snake_case__					(				self			:	List[Any],lowercase_			:	int,lowercase_			:	Optional[int]=None		)->   List[str]:





        '''simple docstring'''







        return self.tokenizer(lowercase_,text_pair=lowercase_,return_tensors=self.framework		)


    def 						snake_case__					(				self			:	str,lowercase_			:	Dict		)->   List[str]:





        '''simple docstring'''







        return self.model(**lowercase_		)


    def 						snake_case__					(				self			:	Dict,lowercase_			:	Optional[int]		)->   Dict:





        '''simple docstring'''







        A__   				=       model_outputs.logits[0].numpy()
        A__   				=       softmax(lowercase_		)

        A__   				=       np.argmax(lowercase_		)
        A__   				=       self.model.config.idalabel[best_class]
        A__   				=       probabilities[best_class].item()
        A__   				=       logits.tolist()
        return {"label": label, "score": score, "logits": logits}

