


'''simple docstring'''


import logging
import re

import pytorch_quantization
import pytorch_quantization.nn as quant_nn
import torch
from pytorch_quantization import calib
from pytorch_quantization.tensor_quant import QuantDescriptor


UpperCAmelCase_						:  List[Any]				  =						logging.getLogger(__name__)

UpperCAmelCase_						:  int				  =						50  # max width of layer names
UpperCAmelCase_						:  str				  =						70  # max width of quantizer names







def      snake_case_  (    SCREAMING_SNAKE_CASE__	):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE					:			Any							= parser.add_argument_group("""quant_trainer arguments"""	)
				group.add_argument("""--wprec"""				, type=SCREAMING_SNAKE_CASE__				, default=8				, help="""weight precision"""	)
				group.add_argument("""--aprec"""				, type=SCREAMING_SNAKE_CASE__				, default=8				, help="""activation precision"""	)
				group.add_argument("""--quant-per-tensor"""				, action="""store_true"""				, help="""per tensor weight scaling"""	)
				group.add_argument("""--quant-disable"""				, action="""store_true"""				, help="""disable all quantizers"""	)
				group.add_argument("""--quant-disable-embeddings"""				, action="""store_true"""				, help="""disable all embeddings quantizers"""	)
				group.add_argument("""--quant-disable-keyword"""				, type=SCREAMING_SNAKE_CASE__				, nargs="""+"""				, help="""disable quantizers by keyword"""	)
				group.add_argument("""--quant-disable-layer-module"""				, type=SCREAMING_SNAKE_CASE__				, help="""disable quantizers by keyword under layer."""	)
				group.add_argument("""--quant-enable-layer-module"""				, type=SCREAMING_SNAKE_CASE__				, help="""enable quantizers by keyword under layer"""	)
				group.add_argument("""--calibrator"""				, default="""max"""				, help="""which quantization range calibrator to use"""	)
				group.add_argument("""--percentile"""				, default=SCREAMING_SNAKE_CASE__				, type=SCREAMING_SNAKE_CASE__				, help="""percentile for PercentileCalibrator"""	)
				group.add_argument("""--fuse-qkv"""				, action="""store_true"""				, help="""use the same scale factor for qkv"""	)
				group.add_argument("""--clip-gelu"""				, metavar="""N"""				, type=SCREAMING_SNAKE_CASE__				, help="""clip gelu output maximum value to N"""	)
				group.add_argument(
				    """--recalibrate-weights"""				, action="""store_true"""				, help=(
				        """recalibrate weight amaxes by taking the max of the weights."""
				        """ amaxes will be computed with the current quantization granularity (axis)."""
				    )				, )







def      snake_case_  (    SCREAMING_SNAKE_CASE__	):







				"""simple docstring"""





				if args.calibrator == "max":
								_SCREAMING_SNAKE_CASE					:			int							= """max"""
				elif args.calibrator == "percentile":
								if args.percentile is None:
												raise ValueError("""Specify --percentile when using percentile calibrator"""	)
								_SCREAMING_SNAKE_CASE					:			Optional[int]							= """histogram"""
				elif args.calibrator == "mse":
								_SCREAMING_SNAKE_CASE					:			Optional[int]							= """histogram"""
				else:
								raise ValueError(f"""Invalid calibrator {args.calibrator}"""	)

				_SCREAMING_SNAKE_CASE					:			List[str]							= QuantDescriptor(num_bits=args.aprec				, calib_method=SCREAMING_SNAKE_CASE__	)
				_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= QuantDescriptor(num_bits=args.wprec				, axis=(None if args.quant_per_tensor else (0,))	)
				quant_nn.QuantLinear.set_default_quant_desc_input(SCREAMING_SNAKE_CASE__	)
				quant_nn.QuantLinear.set_default_quant_desc_weight(SCREAMING_SNAKE_CASE__	)







def      snake_case_  (    SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__=False				, SCREAMING_SNAKE_CASE__=False	):







				"""simple docstring"""





				logger.info("""Configuring Model for Quantization"""	)
				logger.info(f"""using quantization package {pytorch_quantization.__file__}"""	)

				if not calib:
								if args.quant_disable_embeddings:
												set_quantizer_by_name(SCREAMING_SNAKE_CASE__				, ["""embeddings"""]				, which="""weight"""				, _disabled=SCREAMING_SNAKE_CASE__	)

								if args.quant_disable:
												set_quantizer_by_name(SCREAMING_SNAKE_CASE__				, [""""""]				, _disabled=SCREAMING_SNAKE_CASE__	)

								if args.quant_disable_keyword:
												set_quantizer_by_name(SCREAMING_SNAKE_CASE__				, args.quant_disable_keyword				, _disabled=SCREAMING_SNAKE_CASE__	)

								if args.quant_disable_layer_module:
												set_quantizer_by_name(SCREAMING_SNAKE_CASE__				, [R"""layer.\d+.""" + args.quant_disable_layer_module]				, _disabled=SCREAMING_SNAKE_CASE__	)

								if args.quant_enable_layer_module:
												set_quantizer_by_name(SCREAMING_SNAKE_CASE__				, [R"""layer.\d+.""" + args.quant_enable_layer_module]				, _disabled=SCREAMING_SNAKE_CASE__	)

								if args.recalibrate_weights:
												recalibrate_weights(SCREAMING_SNAKE_CASE__	)

								if args.fuse_qkv:
												fuse_qkv(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	)

				if args.clip_gelu:
								clip_gelu(SCREAMING_SNAKE_CASE__				, args.clip_gelu	)

				# if args.local_rank in [-1, 0] and not calib:
				print_quant_summary(SCREAMING_SNAKE_CASE__	)







def      snake_case_  (    SCREAMING_SNAKE_CASE__	):







				"""simple docstring"""





				logger.info("""Enabling Calibration"""	)
				for name, module in model.named_modules():
								if name.endswith("""_quantizer"""	):
												if module._calibrator is not None:
																module.disable_quant()
																module.enable_calib()
												else:
																module.disable()
												logger.info(f"""{name:80}: {module}"""	)







def      snake_case_  (    SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	):







				"""simple docstring"""





				logger.info("""Loading calibrated amax"""	)
				for name, module in model.named_modules():
								if name.endswith("""_quantizer"""	):
												if module._calibrator is not None:
																if isinstance(module._calibrator				, calib.MaxCalibrator	):
																				module.load_calib_amax()
																else:
																				module.load_calib_amax("""percentile"""				, percentile=args.percentile	)
																module.enable_quant()
																module.disable_calib()
												else:
																module.enable()
				model.cuda()
				print_quant_summary(SCREAMING_SNAKE_CASE__	)







def      snake_case_  (    SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	):







				"""simple docstring"""





				def fusea(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	):
								for mod in [qq, qk, qv]:
												if not hasattr(SCREAMING_SNAKE_CASE__				, """_amax"""	):
																print("""          WARNING: NO AMAX BUFFER"""	)
																return
								_SCREAMING_SNAKE_CASE					:			List[str]							= qq._amax.detach().item()
								_SCREAMING_SNAKE_CASE					:			str							= qk._amax.detach().item()
								_SCREAMING_SNAKE_CASE					:			List[str]							= qv._amax.detach().item()

								_SCREAMING_SNAKE_CASE					:			Any							= max(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	)
								qq._amax.fill_(SCREAMING_SNAKE_CASE__	)
								qk._amax.fill_(SCREAMING_SNAKE_CASE__	)
								qv._amax.fill_(SCREAMING_SNAKE_CASE__	)
								logger.info(f"""          q={q:5.2f} k={k:5.2f} v={v:5.2f} -> {amax:5.2f}"""	)

				for name, mod in model.named_modules():
								if name.endswith(""".attention.self"""	):
												logger.info(f"""FUSE_QKV: {name:{name_width}}"""	)
												fusea(mod.matmul_q_input_quantizer				, mod.matmul_k_input_quantizer				, mod.matmul_v_input_quantizer	)
												if args.quant_per_tensor:
																fusea(mod.query._weight_quantizer				, mod.key._weight_quantizer				, mod.value._weight_quantizer	)







def      snake_case_  (    SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	):







				"""simple docstring"""





				for name, mod in model.named_modules():
								if name.endswith(""".output.dense"""	) and not name.endswith("""attention.output.dense"""	):
												_SCREAMING_SNAKE_CASE					:			List[Any]							= mod._input_quantizer._amax.data.detach().item()
												mod._input_quantizer._amax.data.detach().clamp_(max=SCREAMING_SNAKE_CASE__	)
												_SCREAMING_SNAKE_CASE					:			Tuple							= mod._input_quantizer._amax.data.detach().item()
												logger.info(f"""CLIP_GELU: {name:{name_width}} amax: {amax_init:5.2f} -> {amax:5.2f}"""	)







def      snake_case_  (    SCREAMING_SNAKE_CASE__	):







				"""simple docstring"""





				for name, mod in model.named_modules():
								if hasattr(SCREAMING_SNAKE_CASE__				, """_weight_quantizer"""	) and mod._weight_quantizer.axis is not None:
												_SCREAMING_SNAKE_CASE					:			Optional[Any]							= mod.weight.shape[0]
												_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= mod._weight_quantizer._amax.detach()
												_SCREAMING_SNAKE_CASE					:			str							= torch.ones(SCREAMING_SNAKE_CASE__				, dtype=amax.dtype				, device=amax.device	) * amax
												print(f"""expanding {name} {amax} -> {mod._weight_quantizer._amax}"""	)







def      snake_case_  (    SCREAMING_SNAKE_CASE__	):







				"""simple docstring"""





				for name, mod in model.named_modules():
								if hasattr(SCREAMING_SNAKE_CASE__				, """_weight_quantizer"""	):
												if not hasattr(mod.weight_quantizer				, """_amax"""	):
																print("""RECALIB: {name:{name_width}} WARNING: NO AMAX BUFFER"""	)
																continue

												# determine which axes to reduce across
												# e.g. a 4D tensor quantized per axis 0 should reduce over (1,2,3)
												_SCREAMING_SNAKE_CASE					:			Any							= set() if mod._weight_quantizer.axis is None else set(mod._weight_quantizer.axis	)
												_SCREAMING_SNAKE_CASE					:			Tuple							= set(range(len(mod.weight.size()	)	)	) - axis_set
												_SCREAMING_SNAKE_CASE					:			Optional[int]							= pytorch_quantization.utils.reduce_amax(mod.weight				, axis=SCREAMING_SNAKE_CASE__				, keepdims=SCREAMING_SNAKE_CASE__	).detach()
												logger.info(f"""RECALIB: {name:{name_width}} {mod._weight_quantizer._amax.flatten()} -> {amax.flatten()}"""	)
												_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= amax







def      snake_case_  (    SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__=25				, SCREAMING_SNAKE_CASE__=180				, SCREAMING_SNAKE_CASE__=None	):







				"""simple docstring"""





				if ignore is None:
								_SCREAMING_SNAKE_CASE					:			Optional[Any]							= []
				elif not isinstance(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	):
								_SCREAMING_SNAKE_CASE					:			Any							= [ignore]

				_SCREAMING_SNAKE_CASE					:			Any							= 0
				for name, mod in model.named_modules():
								if not hasattr(SCREAMING_SNAKE_CASE__				, """weight"""	):
												continue
								_SCREAMING_SNAKE_CASE					:			Tuple							= max(SCREAMING_SNAKE_CASE__				, len(SCREAMING_SNAKE_CASE__	)	)

				for name, mod in model.named_modules():
								_SCREAMING_SNAKE_CASE					:			Any							= getattr(SCREAMING_SNAKE_CASE__				, """_input_quantizer"""				, SCREAMING_SNAKE_CASE__	)
								_SCREAMING_SNAKE_CASE					:			Dict							= getattr(SCREAMING_SNAKE_CASE__				, """_weight_quantizer"""				, SCREAMING_SNAKE_CASE__	)
								if not hasattr(SCREAMING_SNAKE_CASE__				, """weight"""	):
												continue
								if type(SCREAMING_SNAKE_CASE__	) in ignore:
												continue
								if [True for s in ignore if type(SCREAMING_SNAKE_CASE__	) is str and s in name]:
												continue
								_SCREAMING_SNAKE_CASE					:			Optional[int]							= f"""Act:{input_q.extra_repr()}"""
								_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= f"""Wgt:{weight_q.extra_repr()}"""
								_SCREAMING_SNAKE_CASE					:			Any							= f"""{name:{name_width}} {act_str} {wgt_str}"""
								if len(SCREAMING_SNAKE_CASE__	) <= line_width:
												logger.info(SCREAMING_SNAKE_CASE__	)
								else:
												logger.info(f"""{name:{name_width}} {act_str}"""	)
												logger.info(f"""{"  ":{name_width}} {wgt_str}"""	)







def      snake_case_  (    SCREAMING_SNAKE_CASE__	):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE					:			List[Any]							= 0
				for name, mod in model.named_modules():
								if isinstance(SCREAMING_SNAKE_CASE__				, pytorch_quantization.nn.TensorQuantizer	):
												print(f"""{name:80} {mod}"""	)
												count += 1
				print(f"""{count} TensorQuantizers found in model"""	)







def      snake_case_  (    SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE					:			Optional[Any]							= getattr(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	)
				if quantizer_mod is not None:
								assert hasattr(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	)
								setattr(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	)
				else:
								logger.warning(f"""{name} has no {quantizer}"""	)







def      snake_case_  (    SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__="both"				, **SCREAMING_SNAKE_CASE__	):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE					:			List[Any]							= f"""Warning: changing {which} quantizers of {name:{qname_width}}"""
				for k, v in kwargs.items():
								s += f""" {k}={v}"""
								if which in ["input", "both"]:
												set_quantizer(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__				, """_input_quantizer"""				, SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	)
								if which in ["weight", "both"]:
												set_quantizer(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__				, """_weight_quantizer"""				, SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	)
				logger.info(SCREAMING_SNAKE_CASE__	)







def      snake_case_  (    SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__				, **SCREAMING_SNAKE_CASE__	):







				"""simple docstring"""





				for name, mod in model.named_modules():
								if hasattr(SCREAMING_SNAKE_CASE__				, """_input_quantizer"""	) or hasattr(SCREAMING_SNAKE_CASE__				, """_weight_quantizer"""	):
												for n in names:
																if re.search(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	):
																				set_quantizers(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__				, **SCREAMING_SNAKE_CASE__	)
								elif name.endswith("""_quantizer"""	):
												for n in names:
																if re.search(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	):
																				_SCREAMING_SNAKE_CASE					:			Optional[Any]							= f"""Warning: changing {name:{name_width}}"""
																				for k, v in kwargs.items():
																								s += f""" {k}={v}"""
																								setattr(SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__				, SCREAMING_SNAKE_CASE__	)
																				logger.info(SCREAMING_SNAKE_CASE__	)








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





							'''simple docstring'''


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






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

											_SCREAMING_SNAKE_CASE					:			List[str]							= 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       ,   __snake_case       ,   __snake_case       ,   __snake_case    ):
											_SCREAMING_SNAKE_CASE					:			Any							= MaskFormerSwinModel(config=__snake_case    )
											model.to(__snake_case    )
											model.eval()
											_SCREAMING_SNAKE_CASE					:			Any							= model(__snake_case    )

											_SCREAMING_SNAKE_CASE					:			List[str]							= ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths    ) - 1))
											_SCREAMING_SNAKE_CASE					:			List[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 UpperCAmelCase_      (       self       ,   __snake_case       ,   __snake_case       ,   __snake_case    ):
											_SCREAMING_SNAKE_CASE					:			str							= MaskFormerSwinBackbone(config=__snake_case    )
											model.to(__snake_case    )
											model.eval()
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= model(__snake_case    )

											# 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(__snake_case    ):
															_SCREAMING_SNAKE_CASE					:			Optional[Any]							= ["""stem"""]
															_SCREAMING_SNAKE_CASE					:			Any							= MaskFormerSwinBackbone(config=__snake_case    )






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= self.prepare_config_and_inputs()
											_SCREAMING_SNAKE_CASE						,			_SCREAMING_SNAKE_CASE						,			_SCREAMING_SNAKE_CASE					:			List[str]							= config_and_inputs
											_SCREAMING_SNAKE_CASE					:			List[str]							= {"""pixel_values""": pixel_values}
											return config, inputs_dict





@require_torch
class        lowercase__    (		_snake_case	,	_snake_case	,	unittest.TestCase   ):





							'''simple docstring'''


							A_				: Any       =    (
							    (
							        MaskFormerSwinModel,
							        MaskFormerSwinBackbone,
							    )
							    if is_torch_available()
							    else ()
							)
							A_				: List[Any]       =    {"""feature-extraction""": MaskFormerSwinModel} if is_torch_available() else {}
							A_				: Optional[Any]       =    False
							A_				: Dict       =    False
							A_				: Optional[int]       =    False
							A_				: Union[str, Any]       =    False
							A_				: Union[str, Any]       =    False






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			str							= MaskFormerSwinModelTester(self    )
											_SCREAMING_SNAKE_CASE					:			Tuple							= ConfigTester(self       ,   config_class=__snake_case       ,   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    ):
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__snake_case    )






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Tuple							= self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_backbone(*__snake_case    )






							@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    ):
											_SCREAMING_SNAKE_CASE						,			_SCREAMING_SNAKE_CASE					:			Optional[int]							= self.model_tester.prepare_config_and_inputs_for_common()

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






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE						,			_SCREAMING_SNAKE_CASE					:			int							= self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE					:			Optional[int]							= model_class(__snake_case    )
															_SCREAMING_SNAKE_CASE					:			List[Any]							= inspect.signature(model.forward    )
															# signature.parameters is an OrderedDict => so arg_names order is deterministic
															_SCREAMING_SNAKE_CASE					:			Optional[int]							= [*signature.parameters.keys()]

															_SCREAMING_SNAKE_CASE					:			str							= ["""pixel_values"""]
															self.assertListEqual(arg_names[:1]       ,   __snake_case    )






							@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       ,   __snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case    ):
											_SCREAMING_SNAKE_CASE					:			List[str]							= model_class(__snake_case    )
											model.to(__snake_case    )
											model.eval()

											with torch.no_grad():
															_SCREAMING_SNAKE_CASE					:			str							= model(**self._prepare_for_class(__snake_case       ,   __snake_case    )    )

											_SCREAMING_SNAKE_CASE					:			str							= outputs.hidden_states

											_SCREAMING_SNAKE_CASE					:			Optional[int]							= getattr(
											    self.model_tester       ,   """expected_num_hidden_layers"""       ,   len(self.model_tester.depths    ) + 1    )
											self.assertEqual(len(__snake_case    )       ,   __snake_case    )

											# Swin has a different seq_length
											_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					:			Optional[int]							= (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    ):
											_SCREAMING_SNAKE_CASE						,			_SCREAMING_SNAKE_CASE					:			List[Any]							= self.model_tester.prepare_config_and_inputs_for_common()

											_SCREAMING_SNAKE_CASE					:			str							= (
											    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:
															_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= True
															self.check_hidden_states_output(__snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case    )

															# 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(__snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case    )






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE						,			_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= self.model_tester.prepare_config_and_inputs_for_common()
											_SCREAMING_SNAKE_CASE					:			str							= 3

											_SCREAMING_SNAKE_CASE					:			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)
											)
											_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					:			Tuple							= image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

											for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE					:			List[str]							= True
															self.check_hidden_states_output(__snake_case       ,   __snake_case       ,   __snake_case       ,   (padded_height, padded_width)    )

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															_SCREAMING_SNAKE_CASE					:			Dict							= True
															self.check_hidden_states_output(__snake_case       ,   __snake_case       ,   __snake_case       ,   (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    ):
											_SCREAMING_SNAKE_CASE						,			_SCREAMING_SNAKE_CASE					:			List[Any]							= self.model_tester.prepare_config_and_inputs_for_common()

											def set_nan_tensor_to_zero(__snake_case    ):
															_SCREAMING_SNAKE_CASE					:			Optional[int]							= 0
															return t

											def check_equivalence(__snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case={}    ):
															with torch.no_grad():
																			_SCREAMING_SNAKE_CASE					:			Dict							= model(**__snake_case       ,   return_dict=__snake_case       ,   **__snake_case    )
																			_SCREAMING_SNAKE_CASE					:			Optional[int]							= model(**__snake_case       ,   return_dict=__snake_case       ,   **__snake_case    ).to_tuple()

																			def recursive_check(__snake_case       ,   __snake_case    ):
																							if isinstance(__snake_case       ,   (List, Tuple)    ):
																											for tuple_iterable_value, dict_iterable_value in zip(__snake_case       ,   __snake_case    ):
																															recursive_check(__snake_case       ,   __snake_case    )
																							elif isinstance(__snake_case       ,   __snake_case    ):
																											for tuple_iterable_value, dict_iterable_value in zip(
																											    tuple_object.values()       ,   dict_object.values()    ):
																															recursive_check(__snake_case       ,   __snake_case    )
																							elif tuple_object is None:
																											return
																							else:
																											self.assertTrue(
																											    torch.allclose(
																											        set_nan_tensor_to_zero(__snake_case    )       ,   set_nan_tensor_to_zero(__snake_case    )       ,   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(__snake_case    ).any()} and `inf`: {torch.isinf(__snake_case    )}. Dict has"""
																											        f""" `nan`: {torch.isnan(__snake_case    ).any()} and `inf`: {torch.isinf(__snake_case    )}."""
																											    )       ,   )

																			recursive_check(__snake_case       ,   __snake_case    )

											for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= model_class(__snake_case    )
															model.to(__snake_case    )
															model.eval()

															_SCREAMING_SNAKE_CASE					:			str							= self._prepare_for_class(__snake_case       ,   __snake_case    )
															_SCREAMING_SNAKE_CASE					:			str							= self._prepare_for_class(__snake_case       ,   __snake_case    )
															check_equivalence(__snake_case       ,   __snake_case       ,   __snake_case    )

															_SCREAMING_SNAKE_CASE					:			Any							= self._prepare_for_class(__snake_case       ,   __snake_case       ,   return_labels=__snake_case    )
															_SCREAMING_SNAKE_CASE					:			int							= self._prepare_for_class(__snake_case       ,   __snake_case       ,   return_labels=__snake_case    )
															check_equivalence(__snake_case       ,   __snake_case       ,   __snake_case    )

															_SCREAMING_SNAKE_CASE					:			List[str]							= self._prepare_for_class(__snake_case       ,   __snake_case    )
															_SCREAMING_SNAKE_CASE					:			Optional[int]							= self._prepare_for_class(__snake_case       ,   __snake_case    )
															check_equivalence(__snake_case       ,   __snake_case       ,   __snake_case       ,   {"""output_hidden_states""": True}    )

															_SCREAMING_SNAKE_CASE					:			List[Any]							= self._prepare_for_class(__snake_case       ,   __snake_case       ,   return_labels=__snake_case    )
															_SCREAMING_SNAKE_CASE					:			Optional[Any]							= self._prepare_for_class(__snake_case       ,   __snake_case       ,   return_labels=__snake_case    )
															check_equivalence(__snake_case       ,   __snake_case       ,   __snake_case       ,   {"""output_hidden_states""": True}    )





@require_torch
class        lowercase__    (		unittest.TestCase	,	_snake_case   ):





							'''simple docstring'''


							A_				: Any       =    (MaskFormerSwinBackbone,) if is_torch_available() else ()
							A_				: List[Any]       =    MaskFormerSwinConfig






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= MaskFormerSwinModelTester(self    )






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE						,			_SCREAMING_SNAKE_CASE					:			Optional[int]							= self.model_tester.prepare_config_and_inputs_for_common()
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= inputs_dict["""pixel_values"""].shape[0]

											for backbone_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE					:			List[Any]							= backbone_class(__snake_case    )
															backbone.to(__snake_case    )
															backbone.eval()

															_SCREAMING_SNAKE_CASE					:			List[str]							= backbone(**__snake_case    )

															# Test default outputs and verify feature maps
															self.assertIsInstance(outputs.feature_maps       ,   __snake_case    )
															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
															_SCREAMING_SNAKE_CASE					:			Optional[Any]							= backbone(**__snake_case       ,   output_hidden_states=__snake_case    )
															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)
																							_SCREAMING_SNAKE_CASE						,			_SCREAMING_SNAKE_CASE						,			_SCREAMING_SNAKE_CASE					:			Dict							= hidden_state.shape
																							self.assertTrue((h_batch_size, h_n_channels)       ,   (batch_size, n_channels)    )

            # Test output_attentions=True
															if self.has_attentions:
																			_SCREAMING_SNAKE_CASE					:			Optional[int]							= backbone(**__snake_case       ,   output_attentions=__snake_case    )
																			self.assertIsNotNone(outputs.attentions    )




