






'''simple docstring'''







import collections
import inspect
import unittest
from typing import Dict, List, Tuple

from transformers import MaskFormerSwinConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device
from transformers.utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch
							from torch import nn

							from transformers import MaskFormerSwinBackbone
							from transformers.models.maskformer import MaskFormerSwinModel




class 		UpperCamelCase__     :




		'''simple docstring'''





		def __init__(   self ,_lowerCAmelCase ,_lowerCAmelCase=13 ,_lowerCAmelCase=32 ,_lowerCAmelCase=2 ,_lowerCAmelCase=3 ,_lowerCAmelCase=16 ,_lowerCAmelCase=[1, 2, 1] ,_lowerCAmelCase=[2, 2, 4] ,_lowerCAmelCase=2 ,_lowerCAmelCase=2.0 ,_lowerCAmelCase=True ,_lowerCAmelCase=0.0 ,_lowerCAmelCase=0.0 ,_lowerCAmelCase=0.1 ,_lowerCAmelCase="gelu" ,_lowerCAmelCase=False ,_lowerCAmelCase=True ,_lowerCAmelCase=0.02 ,_lowerCAmelCase=1E-5 ,_lowerCAmelCase=True ,_lowerCAmelCase=None ,_lowerCAmelCase=True ,_lowerCAmelCase=10 ,_lowerCAmelCase=8 ,_lowerCAmelCase=["stage1", "stage2", "stage3"] ,_lowerCAmelCase=[1, 2, 3] ,):
								lowerCamelCase__								=	parent
								lowerCamelCase__								=	batch_size
								lowerCamelCase__								=	image_size
								lowerCamelCase__								=	patch_size
								lowerCamelCase__								=	num_channels
								lowerCamelCase__								=	embed_dim
								lowerCamelCase__								=	depths
								lowerCamelCase__								=	num_heads
								lowerCamelCase__								=	window_size
								lowerCamelCase__								=	mlp_ratio
								lowerCamelCase__								=	qkv_bias
								lowerCamelCase__								=	hidden_dropout_prob
								lowerCamelCase__								=	attention_probs_dropout_prob
								lowerCamelCase__								=	drop_path_rate
								lowerCamelCase__								=	hidden_act
								lowerCamelCase__								=	use_absolute_embeddings
								lowerCamelCase__								=	patch_norm
								lowerCamelCase__								=	layer_norm_eps
								lowerCamelCase__								=	initializer_range
								lowerCamelCase__								=	is_training
								lowerCamelCase__								=	scope
								lowerCamelCase__								=	use_labels
								lowerCamelCase__								=	type_sequence_label_size
								lowerCamelCase__								=	encoder_stride
								lowerCamelCase__								=	out_features
								lowerCamelCase__								=	out_indices






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

								lowerCamelCase__								=	self.get_config()

								return config, pixel_values, labels






		def       UpperCamelCase_       (   self							):
								return MaskFormerSwinConfig(
								    image_size=self.image_size ,patch_size=self.patch_size ,num_channels=self.num_channels ,embed_dim=self.embed_dim ,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 ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							):
								lowerCamelCase__								=	MaskFormerSwinModel(config=_lowerCAmelCase							)
								model.to(_lowerCAmelCase							)
								model.eval()
								lowerCamelCase__								=	model(_lowerCAmelCase							)

								lowerCamelCase__								=	((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths							) - 1))
								lowerCamelCase__								=	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 ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							):
								lowerCamelCase__								=	MaskFormerSwinBackbone(config=_lowerCAmelCase							)
								model.to(_lowerCAmelCase							)
								model.eval()
								lowerCamelCase__								=	model(_lowerCAmelCase							)

								# verify feature maps
								self.parent.assertEqual(len(result.feature_maps							) ,len(config.out_features							)							)
								self.parent.assertListEqual(list(result.feature_maps[0].shape							) ,[13, 16, 16, 16]							)

								# verify channels
								self.parent.assertEqual(len(model.channels							) ,len(config.out_features							)							)
								self.parent.assertListEqual(model.channels ,[16, 32, 64]							)

								# verify ValueError
								with self.parent.assertRaises(_lowerCAmelCase							):
														lowerCamelCase__								=	["""stem"""]
														lowerCamelCase__								=	MaskFormerSwinBackbone(config=_lowerCAmelCase							)






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	self.prepare_config_and_inputs()
								lowerCamelCase__							,      lowerCamelCase__							,      lowerCamelCase__								=	config_and_inputs
								lowerCamelCase__								=	{"""pixel_values""": pixel_values}
								return config, inputs_dict




@require_torch
class 		UpperCamelCase__     (a ,a ,unittest.TestCase      ):




		'''simple docstring'''





		_UpperCamelCase						=       (
		    (
		        MaskFormerSwinModel,
		        MaskFormerSwinBackbone,
		    )
		    if is_torch_available()
		    else ()
		)
		_UpperCamelCase						=       {'feature-extraction': MaskFormerSwinModel} if is_torch_available() else {}
		_UpperCamelCase						=       False
		_UpperCamelCase						=       False
		_UpperCamelCase						=       False
		_UpperCamelCase						=       False
		_UpperCamelCase						=       False






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	MaskFormerSwinModelTester(self							)
								lowerCamelCase__								=	ConfigTester(self ,config_class=_lowerCAmelCase ,embed_dim=37							)






		@require_torch_multi_gpu
		@unittest.skip(
		    reason=(
		        """`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn't work well with"""
		        """ `nn.DataParallel`"""
		    )							)
		def       UpperCamelCase_       (   self							):
								pass






		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							):
								lowerCamelCase__								=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_lowerCAmelCase							)






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_backbone(*_lowerCAmelCase							)






		@unittest.skip("""Swin does not use inputs_embeds"""							)
		def       UpperCamelCase_       (   self							):
								pass






		@unittest.skip("""Swin does not support feedforward chunking"""							)
		def       UpperCamelCase_       (   self							):
								pass






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__							,      lowerCamelCase__								=	self.model_tester.prepare_config_and_inputs_for_common()

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






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__							,      lowerCamelCase__								=	self.model_tester.prepare_config_and_inputs_for_common()

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

														lowerCamelCase__								=	["""pixel_values"""]
														self.assertListEqual(arg_names[:1] ,_lowerCAmelCase							)






		@unittest.skip(reason="""MaskFormerSwin is only used as backbone and doesn't support output_attentions"""							)
		def       UpperCamelCase_       (   self							):
								pass






		@unittest.skip(reason="""MaskFormerSwin is only used as an internal backbone"""							)
		def       UpperCamelCase_       (   self							):
								pass






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							):
								lowerCamelCase__								=	model_class(_lowerCAmelCase							)
								model.to(_lowerCAmelCase							)
								model.eval()

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

								lowerCamelCase__								=	outputs.hidden_states

								lowerCamelCase__								=	getattr(
								    self.model_tester ,"""expected_num_hidden_layers""" ,len(self.model_tester.depths							) + 1							)
								self.assertEqual(len(_lowerCAmelCase							) ,_lowerCAmelCase							)

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

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






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__							,      lowerCamelCase__								=	self.model_tester.prepare_config_and_inputs_for_common()

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

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

														self.check_hidden_states_output(_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__							,      lowerCamelCase__								=	self.model_tester.prepare_config_and_inputs_for_common()
								lowerCamelCase__								=	3

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

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

								for model_class in self.all_model_classes:
														lowerCamelCase__								=	True
														self.check_hidden_states_output(_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,(padded_height, padded_width)							)

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






		@unittest.skip(reason="""MaskFormerSwin doesn't have pretrained checkpoints"""							)
		def       UpperCamelCase_       (   self							):
								pass






		@unittest.skip(reason="""This will be fixed once MaskFormerSwin is replaced by native Swin"""							)
		def       UpperCamelCase_       (   self							):
								pass






		@unittest.skip(reason="""This will be fixed once MaskFormerSwin is replaced by native Swin"""							)
		def       UpperCamelCase_       (   self							):
								pass






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__							,      lowerCamelCase__								=	self.model_tester.prepare_config_and_inputs_for_common()

								def set_nan_tensor_to_zero(_lowerCAmelCase							):
														lowerCamelCase__								=	0
														return t

								def check_equivalence(_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase={}							):
														with torch.no_grad():
																				lowerCamelCase__								=	model(**_lowerCAmelCase ,return_dict=_lowerCAmelCase ,**_lowerCAmelCase							)
																				lowerCamelCase__								=	model(**_lowerCAmelCase ,return_dict=_lowerCAmelCase ,**_lowerCAmelCase							).to_tuple()

																				def recursive_check(_lowerCAmelCase ,_lowerCAmelCase							):
																										if isinstance(_lowerCAmelCase ,(List, Tuple)							):
																																for tuple_iterable_value, dict_iterable_value in zip(_lowerCAmelCase ,_lowerCAmelCase							):
																																						recursive_check(_lowerCAmelCase ,_lowerCAmelCase							)
																										elif isinstance(_lowerCAmelCase ,_lowerCAmelCase							):
																																for tuple_iterable_value, dict_iterable_value in zip(
																																    tuple_object.values() ,dict_object.values()							):
																																						recursive_check(_lowerCAmelCase ,_lowerCAmelCase							)
																										elif tuple_object is None:
																																return
																										else:
																																self.assertTrue(
																																    torch.allclose(
																																        set_nan_tensor_to_zero(_lowerCAmelCase							) ,set_nan_tensor_to_zero(_lowerCAmelCase							) ,atol=1E-5							) ,msg=(
																																        """Tuple and dict output are not equal. Difference:"""
																																        F''' {torch.max(torch.abs(tuple_object - dict_object							)							)}. Tuple has `nan`:'''
																																        F''' {torch.isnan(_lowerCAmelCase							).any()} and `inf`: {torch.isinf(_lowerCAmelCase							)}. Dict has'''
																																        F''' `nan`: {torch.isnan(_lowerCAmelCase							).any()} and `inf`: {torch.isinf(_lowerCAmelCase							)}.'''
																																    ) ,)

																				recursive_check(_lowerCAmelCase ,_lowerCAmelCase							)

								for model_class in self.all_model_classes:
														lowerCamelCase__								=	model_class(_lowerCAmelCase							)
														model.to(_lowerCAmelCase							)
														model.eval()

														lowerCamelCase__								=	self._prepare_for_class(_lowerCAmelCase ,_lowerCAmelCase							)
														lowerCamelCase__								=	self._prepare_for_class(_lowerCAmelCase ,_lowerCAmelCase							)
														check_equivalence(_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)

														lowerCamelCase__								=	self._prepare_for_class(_lowerCAmelCase ,_lowerCAmelCase ,return_labels=_lowerCAmelCase							)
														lowerCamelCase__								=	self._prepare_for_class(_lowerCAmelCase ,_lowerCAmelCase ,return_labels=_lowerCAmelCase							)
														check_equivalence(_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)

														lowerCamelCase__								=	self._prepare_for_class(_lowerCAmelCase ,_lowerCAmelCase							)
														lowerCamelCase__								=	self._prepare_for_class(_lowerCAmelCase ,_lowerCAmelCase							)
														check_equivalence(_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,{"""output_hidden_states""": True}							)

														lowerCamelCase__								=	self._prepare_for_class(_lowerCAmelCase ,_lowerCAmelCase ,return_labels=_lowerCAmelCase							)
														lowerCamelCase__								=	self._prepare_for_class(_lowerCAmelCase ,_lowerCAmelCase ,return_labels=_lowerCAmelCase							)
														check_equivalence(_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,{"""output_hidden_states""": True}							)




@require_torch
class 		UpperCamelCase__     (unittest.TestCase ,a      ):




		'''simple docstring'''





		_UpperCamelCase						=       (MaskFormerSwinBackbone,) if is_torch_available() else ()
		_UpperCamelCase						=       MaskFormerSwinConfig






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__								=	MaskFormerSwinModelTester(self							)






		def       UpperCamelCase_       (   self							):
								lowerCamelCase__							,      lowerCamelCase__								=	self.model_tester.prepare_config_and_inputs_for_common()
								lowerCamelCase__								=	inputs_dict["""pixel_values"""].shape[0]

								for backbone_class in self.all_model_classes:
														lowerCamelCase__								=	backbone_class(_lowerCAmelCase							)
														backbone.to(_lowerCAmelCase							)
														backbone.eval()

														lowerCamelCase__								=	backbone(**_lowerCAmelCase							)

														# Test default outputs and verify feature maps
														self.assertIsInstance(outputs.feature_maps ,_lowerCAmelCase							)
														self.assertTrue(len(outputs.feature_maps							) == len(backbone.channels							)							)
														for feature_map, n_channels in zip(outputs.feature_maps ,backbone.channels							):
																				self.assertTrue(feature_map.shape[:2] ,(batch_size, n_channels)							)
														self.assertIsNone(outputs.hidden_states							)
														self.assertIsNone(outputs.attentions							)

														# Test output_hidden_states=True
														lowerCamelCase__								=	backbone(**_lowerCAmelCase ,output_hidden_states=_lowerCAmelCase							)
														self.assertIsNotNone(outputs.hidden_states							)
														self.assertTrue(len(outputs.hidden_states							) ,len(backbone.stage_names							)							)
														# We skip the stem layer
														for hidden_states, n_channels in zip(outputs.hidden_states[1:] ,backbone.channels							):
																				for hidden_state in hidden_states:
																										# Hidden states are in the format (batch_size, (height * width), n_channels)
																										lowerCamelCase__							,      lowerCamelCase__							,      lowerCamelCase__								=	hidden_state.shape
																										self.assertTrue((h_batch_size, h_n_channels) ,(batch_size, n_channels)							)

            # Test output_attentions=True
														if self.has_attentions:
																				lowerCamelCase__								=	backbone(**_lowerCAmelCase ,output_attentions=_lowerCAmelCase							)
																				self.assertIsNotNone(outputs.attentions							)





# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse

from .config import config_command_parser
from .config_args import default_config_file, load_config_from_file  # noqa: F401
from .default import default_command_parser
from .update import update_command_parser






def 	__lowerCamelCase						(							lowerCamelCase__		:						Any=None      ):





			'''simple docstring'''




			lowerCamelCase    		=  argparse.ArgumentParser(add_help=lowerCamelCase__  ,							allow_abbrev=lowerCamelCase__      )
			# The main config parser
			lowerCamelCase    		=  config_command_parser(lowerCamelCase__      )
			# The subparser to add commands to
			lowerCamelCase    		=  config_parser.add_subparsers(title="""subcommands"""  ,							dest="""subcommand"""      )

			# Then add other parsers with the parent parser
			default_command_parser(lowerCamelCase__  ,							parents=[parent_parser]      )
			update_command_parser(lowerCamelCase__  ,							parents=[parent_parser]      )

			return config_parser






def 	__lowerCamelCase						(							):





			'''simple docstring'''




			lowerCamelCase    		=  get_config_parser()
			lowerCamelCase    		=  config_parser.parse_args()

			if not hasattr(lowerCamelCase__  ,							"""func"""      ):
						config_parser.print_help()
						exit(1      )

			# Run
			args.func(lowerCamelCase__      )


if __name__ == "__main__":
		main()

