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				,					__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]				,					):
			__A				:       Optional[Any]			    = parent
			__A				:       str			    = batch_size
			__A				:       List[Any]			    = image_size
			__A				:       List[str]			    = patch_size
			__A				:       List[Any]			    = num_channels
			__A				:       List[Any]			    = embed_dim
			__A				:       Dict			    = hidden_sizes
			__A				:       int			    = depths
			__A				:       Optional[int]			    = num_heads
			__A				:       Optional[Any]			    = window_size
			__A				:       List[Any]			    = mlp_ratio
			__A				:       List[Any]			    = qkv_bias
			__A				:       List[str]			    = hidden_dropout_prob
			__A				:       Tuple			    = attention_probs_dropout_prob
			__A				:       str			    = drop_path_rate
			__A				:       Tuple			    = hidden_act
			__A				:       List[str]			    = use_absolute_embeddings
			__A				:       Tuple			    = patch_norm
			__A				:       Dict			    = layer_norm_eps
			__A				:       List[str]			    = initializer_range
			__A				:       Any			    = is_training
			__A				:       Any			    = scope
			__A				:       Optional[int]			    = use_labels
			__A				:       Any			    = type_sequence_label_size
			__A				:       Tuple			    = encoder_stride
			__A				:       List[Any]			    = out_features
			__A				:       Optional[int]			    = out_indices

	def 	__UpperCAmelCase(  self       ):
			__A				:       Optional[int]			    = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

			__A				:       Optional[Any]			    = self.get_config()

			return config, pixel_values, labels

	def 	__UpperCAmelCase(  self       ):
			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				,					__UpperCAmelCase				,					__UpperCAmelCase				,					__UpperCAmelCase       ):
			__A				:       str			    = FocalNetModel(config=__UpperCAmelCase       )
			model.to(__UpperCAmelCase       )
			model.eval()
			__A				:       List[str]			    = model(__UpperCAmelCase       )

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

			# test greyscale images
			__A				:       Dict			    = 1
			__A				:       Optional[int]			    = FocalNetForMaskedImageModeling(__UpperCAmelCase       )
			model.to(__UpperCAmelCase       )
			model.eval()

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

	def 	__UpperCAmelCase(  self				,					__UpperCAmelCase				,					__UpperCAmelCase				,					__UpperCAmelCase       ):
			__A				:       str			    = self.type_sequence_label_size
			__A				:       Union[str, Any]			    = FocalNetForImageClassification(__UpperCAmelCase       )
			model.to(__UpperCAmelCase       )
			model.eval()
			__A				:       str			    = model(__UpperCAmelCase				,					labels=__UpperCAmelCase       )
			self.parent.assertEqual(result.logits.shape				,					(self.batch_size, self.type_sequence_label_size)       )

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

			__A				:       str			    = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
			__A				:       List[Any]			    = model(__UpperCAmelCase       )
			self.parent.assertEqual(result.logits.shape				,					(self.batch_size, self.type_sequence_label_size)       )







	def 	__UpperCAmelCase(  self       ):
			__A				:       Union[str, Any]			    = self.prepare_config_and_inputs()

			__A    ,  __A    ,  __A				:       Optional[Any]			    = config_and_inputs
			__A				:       str			    = {"pixel_values": pixel_values}
			return config, inputs_dict


@require_torch
class _a  (			lowercase_       ,				lowercase_       ,				unittest.TestCase   ):

	'''simple docstring'''



	lowerCamelCase_       :     Any          =			(
	    (
	        FocalNetModel,
	        FocalNetForImageClassification,
	        FocalNetForMaskedImageModeling,
	        FocalNetBackbone,
	    )
	    if is_torch_available()
	    else ()
	)
	lowerCamelCase_       :     Any          =			(
	    {"""feature-extraction""": FocalNetModel, """image-classification""": FocalNetForImageClassification}
	    if is_torch_available()
	    else {}
	)
	lowerCamelCase_       :     List[str]          =			False

	lowerCamelCase_       :     Any          =			False
	lowerCamelCase_       :     Optional[Any]          =			False
	lowerCamelCase_       :     List[Any]          =			False
	lowerCamelCase_       :     Tuple          =			False

	def 	__UpperCAmelCase(  self       ):
			__A				:       int			    = FocalNetModelTester(self       )
			__A				:       Any			    = ConfigTester(self				,					config_class=__UpperCAmelCase				,					embed_dim=37				,					has_text_modality=__UpperCAmelCase       )

	def 	__UpperCAmelCase(  self       ):
			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       ):
			return

	def 	__UpperCAmelCase(  self       ):
			__A				:       int			    = self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*__UpperCAmelCase       )

	def 	__UpperCAmelCase(  self       ):
			__A				:       str			    = self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_backbone(*__UpperCAmelCase       )

	def 	__UpperCAmelCase(  self       ):
			__A				:       Optional[Any]			    = self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCAmelCase       )

	def 	__UpperCAmelCase(  self       ):
			__A				:       Dict			    = 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 	__UpperCAmelCase(  self       ):
			pass

	@unittest.skip(reason="FocalNet does not use feedforward chunking"       )
	def 	__UpperCAmelCase(  self       ):
			pass

	def 	__UpperCAmelCase(  self       ):
			__A    ,  __A				:       List[str]			    = self.model_tester.prepare_config_and_inputs_for_common()

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

	def 	__UpperCAmelCase(  self       ):
			__A    ,  __A				:       int			    = self.model_tester.prepare_config_and_inputs_for_common()

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

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

	def 	__UpperCAmelCase(  self				,					__UpperCAmelCase				,					__UpperCAmelCase				,					__UpperCAmelCase				,					__UpperCAmelCase       ):
			__A				:       Optional[Any]			    = model_class(__UpperCAmelCase       )
			model.to(__UpperCAmelCase       )
			model.eval()

			with torch.no_grad():
					__A				:       Optional[int]			    = model(**self._prepare_for_class(__UpperCAmelCase				,					__UpperCAmelCase       )       )

			__A				:       Union[str, Any]			    = outputs.hidden_states

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

			__A				:       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]				,					)

			__A				:       Any			    = outputs.reshaped_hidden_states
			self.assertEqual(len(__UpperCAmelCase       )				,					__UpperCAmelCase       )

			__A    ,  __A    ,  __A    ,  __A				:       Any			    = reshaped_hidden_states[0].shape
			__A				:       List[Any]			    = (
			    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 	__UpperCAmelCase(  self       ):
			__A    ,  __A				:       Union[str, Any]			    = self.model_tester.prepare_config_and_inputs_for_common()

			__A				:       List[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)
			)

			for model_class in self.all_model_classes[:-1]:
					__A				:       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"]
					__A				:       Union[str, Any]			    = True

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

	def 	__UpperCAmelCase(  self       ):
			__A    ,  __A				:       Optional[Any]			    = self.model_tester.prepare_config_and_inputs_for_common()
			__A				:       Optional[Any]			    = 3

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

			__A				:       Optional[Any]			    = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
			__A				:       Optional[int]			    = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

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

	@slow
	def 	__UpperCAmelCase(  self       ):







			for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					__A				:       Union[str, Any]			    = FocalNetModel.from_pretrained(__UpperCAmelCase       )
					self.assertIsNotNone(__UpperCAmelCase       )







	def 	__UpperCAmelCase(  self       ):
			__A    ,  __A				:       str			    = self.model_tester.prepare_config_and_inputs_for_common()

			__A				:       Dict			    = _config_zero_init(__UpperCAmelCase       )
			for model_class in self.all_model_classes:
					__A				:       Tuple			    = 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 	__UpperCAmelCase(  self       ):
			return AutoImageProcessor.from_pretrained("microsoft/focalnet-tiny"       ) if is_vision_available() else None







	@slow
	def 	__UpperCAmelCase(  self       ):
			__A				:       Union[str, Any]			    = FocalNetForImageClassification.from_pretrained("microsoft/focalnet-tiny"       ).to(__UpperCAmelCase       )
			__A				:       Optional[Any]			    = self.default_image_processor

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

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

			# verify the logits
			__A				:       Any			    = torch.Size((1, 1_000)       )
			self.assertEqual(outputs.logits.shape				,					__UpperCAmelCase       )
			__A				:       Any			    = torch.tensor([0.21_66, -0.43_68, 0.21_91]       ).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 _a  (			lowercase_       ,				unittest.TestCase   ):

	'''simple docstring'''



	lowerCamelCase_       :     List[str]          =			(FocalNetBackbone,) if is_torch_available() else ()
	lowerCamelCase_       :     Optional[Any]          =			FocalNetConfig

	lowerCamelCase_       :     Any          =			False

	def 	__UpperCAmelCase(  self       ):
			__A				:       List[str]			    = FocalNetModelTester(self       )









'''simple docstring'''

from argparse import ArgumentParser

from .add_new_model import AddNewModelCommand
from .add_new_model_like import AddNewModelLikeCommand
from .convert import ConvertCommand
from .download import DownloadCommand
from .env import EnvironmentCommand
from .lfs import LfsCommands
from .pt_to_tf import PTtoTFCommand
from .run import RunCommand
from .serving import ServeCommand
from .user import UserCommands

def _SCREAMING_SNAKE_CASE ()			->			Dict:






       """simple docstring"""







       lowercase__      =					ArgumentParser('''Transformers CLI tool'''    ,					usage='''transformers-cli <command> [<args>]'''			)
       lowercase__      =					parser.add_subparsers(help='''transformers-cli command helpers'''			)

       # Register commands
       ConvertCommand.register_subcommand(A			)
       DownloadCommand.register_subcommand(A			)
       EnvironmentCommand.register_subcommand(A			)
       RunCommand.register_subcommand(A			)
       ServeCommand.register_subcommand(A			)
       UserCommands.register_subcommand(A			)
       AddNewModelCommand.register_subcommand(A			)
       AddNewModelLikeCommand.register_subcommand(A			)
       LfsCommands.register_subcommand(A			)
       PTtoTFCommand.register_subcommand(A			)

       # Let's go
       lowercase__      =					parser.parse_args()

       if not hasattr(A    ,					'''func'''			):
              parser.print_help()
              exit(1			)

       # Run
       lowercase__      =					args.func(A			)
       service.run()


if __name__ == "__main__":
 main()




