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

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

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						int						      =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

			__lowerCAmelCase      :						List[str]						      =				None
			if self.use_labels:
				__lowerCAmelCase      :						List[str]						      =				ids_tensor([self.batch_size]	,		self.type_sequence_label_size			)

			__lowerCAmelCase      :						Dict						      =				self.get_config()

			return config, pixel_values, labels

		def 							__lowerCamelCase							(				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 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			):
			__lowerCAmelCase      :						int						      =				MaskFormerSwinModel(config=_SCREAMING_SNAKE_CASE			)
			model.to(_SCREAMING_SNAKE_CASE			)
			model.eval()
			__lowerCAmelCase      :						Tuple						      =				model(_SCREAMING_SNAKE_CASE			)

			__lowerCAmelCase      :						Tuple						      =				((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths			) - 1))
			__lowerCAmelCase      :						str						      =				int(config.embed_dim * 2 ** (len(config.depths			) - 1)			)

			self.parent.assertEqual(result.last_hidden_state.shape	,		(self.batch_size, expected_seq_len, expected_dim)			)

		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			):
			__lowerCAmelCase      :						Optional[Any]						      =				MaskFormerSwinBackbone(config=_SCREAMING_SNAKE_CASE			)
			model.to(_SCREAMING_SNAKE_CASE			)
			model.eval()
			__lowerCAmelCase      :						List[str]						      =				model(_SCREAMING_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(_SCREAMING_SNAKE_CASE			):
				__lowerCAmelCase      :						int						      =				['stem']
				__lowerCAmelCase      :						str						      =				MaskFormerSwinBackbone(config=_SCREAMING_SNAKE_CASE			)







		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Optional[int]						      =				self.prepare_config_and_inputs()
			__lowerCAmelCase      ,       __lowerCAmelCase      ,       __lowerCAmelCase      :						int						      =				config_and_inputs
			__lowerCAmelCase      :						Union[str, Any]						      =				{'pixel_values': pixel_values}
			return config, inputs_dict




@require_torch
class 			A__	(     _lowerCamelCase	,					_lowerCamelCase	,					unittest.TestCase):
		A_						:	List[Any]           =     (
		    (
		        MaskFormerSwinModel,
		        MaskFormerSwinBackbone,
		    )
		    if is_torch_available()
		    else ()
		)
		A_						:	List[str]           =     {'feature-extraction': MaskFormerSwinModel} if is_torch_available() else {}
		A_						:	Optional[Any]           =     False
		A_						:	Optional[Any]           =     False
		A_						:	Union[str, Any]           =     False
		A_						:	Optional[int]           =     False
		A_						:	Optional[int]           =     False

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Optional[int]						      =				MaskFormerSwinModelTester(self			)
			__lowerCAmelCase      :						Optional[int]						      =				ConfigTester(self	,		config_class=_SCREAMING_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 							__lowerCamelCase							(				self			):
			pass

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

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Optional[int]						      =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Union[str, Any]						      =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_backbone(*_SCREAMING_SNAKE_CASE			)

		@unittest.skip('Swin does not use inputs_embeds'			)
		def 							__lowerCamelCase							(				self			):
			pass

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

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      ,       __lowerCAmelCase      :						Union[str, Any]						      =				self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
				__lowerCAmelCase      :						Union[str, Any]						      =				model_class(_SCREAMING_SNAKE_CASE			)
				self.assertIsInstance(model.get_input_embeddings()	,		(nn.Module)			)
				__lowerCAmelCase      :						Union[str, Any]						      =				model.get_output_embeddings()
				self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE	,		nn.Linear			)			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      ,       __lowerCAmelCase      :						Union[str, Any]						      =				self.model_tester.prepare_config_and_inputs_for_common()

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

				__lowerCAmelCase      :						Optional[int]						      =				['pixel_values']
				self.assertListEqual(arg_names[:1]	,		_SCREAMING_SNAKE_CASE			)

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

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

		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			):
			__lowerCAmelCase      :						List[str]						      =				model_class(_SCREAMING_SNAKE_CASE			)
			model.to(_SCREAMING_SNAKE_CASE			)
			model.eval()

			with torch.no_grad():
				__lowerCAmelCase      :						Tuple						      =				model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)			)

			__lowerCAmelCase      :						Optional[int]						      =				outputs.hidden_states

			__lowerCAmelCase      :						Dict						      =				getattr(
			    self.model_tester	,		'expected_num_hidden_layers'	,		len(self.model_tester.depths			) + 1			)
			self.assertEqual(len(_SCREAMING_SNAKE_CASE			)	,		_SCREAMING_SNAKE_CASE			)

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

			__lowerCAmelCase      :						List[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]	,		)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      ,       __lowerCAmelCase      :						int						      =				self.model_tester.prepare_config_and_inputs_for_common()

			__lowerCAmelCase      :						Optional[int]						      =				(
			    self.model_tester.image_size
			    if isinstance(self.model_tester.image_size	,		collections.abc.Iterable			)
			    else (self.model_tester.image_size, self.model_tester.image_size)
			)

			for model_class in self.all_model_classes:
				__lowerCAmelCase      :						List[Any]						      =				True
				self.check_hidden_states_output(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)

				# check that output_hidden_states also work using config
				del inputs_dict["output_hidden_states"]
				__lowerCAmelCase      :						Tuple						      =				True

				self.check_hidden_states_output(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      ,       __lowerCAmelCase      :						List[Any]						      =				self.model_tester.prepare_config_and_inputs_for_common()
			__lowerCAmelCase      :						Dict						      =				3

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

			__lowerCAmelCase      :						str						      =				image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
			__lowerCAmelCase      :						Tuple						      =				image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

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

				# check that output_hidden_states also work using config
				del inputs_dict["output_hidden_states"]
				__lowerCAmelCase      :						str						      =				True
				self.check_hidden_states_output(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		(padded_height, padded_width)			)

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

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

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







		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      ,       __lowerCAmelCase      :						List[Any]						      =				self.model_tester.prepare_config_and_inputs_for_common()

			def set_nan_tensor_to_zero(_SCREAMING_SNAKE_CASE			):
				__lowerCAmelCase      :						List[str]						      =				0
				return t

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

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

					recursive_check(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)

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

				__lowerCAmelCase      :						Tuple						      =				self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)
				__lowerCAmelCase      :						Any						      =				self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)
				check_equivalence(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)

				__lowerCAmelCase      :						List[Any]						      =				self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		return_labels=_SCREAMING_SNAKE_CASE			)
				__lowerCAmelCase      :						Any						      =				self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		return_labels=_SCREAMING_SNAKE_CASE			)
				check_equivalence(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)

				__lowerCAmelCase      :						List[str]						      =				self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)
				__lowerCAmelCase      :						List[str]						      =				self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)
				check_equivalence(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		{'output_hidden_states': True}			)

				__lowerCAmelCase      :						Dict						      =				self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		return_labels=_SCREAMING_SNAKE_CASE			)
				__lowerCAmelCase      :						Dict						      =				self._prepare_for_class(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		return_labels=_SCREAMING_SNAKE_CASE			)
				check_equivalence(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		{'output_hidden_states': True}			)




@require_torch
class 			A__	(     unittest.TestCase	,					_lowerCamelCase):
		A_						:	List[str]           =     (MaskFormerSwinBackbone,) if is_torch_available() else ()
		A_						:	Optional[Any]           =     MaskFormerSwinConfig

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Any						      =				MaskFormerSwinModelTester(self			)







		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      ,       __lowerCAmelCase      :						Union[str, Any]						      =				self.model_tester.prepare_config_and_inputs_for_common()
			__lowerCAmelCase      :						int						      =				inputs_dict['pixel_values'].shape[0]

			for backbone_class in self.all_model_classes:
				__lowerCAmelCase      :						Any						      =				backbone_class(_SCREAMING_SNAKE_CASE			)
				backbone.to(_SCREAMING_SNAKE_CASE			)
				backbone.eval()

				__lowerCAmelCase      :						str						      =				backbone(**_SCREAMING_SNAKE_CASE			)

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

            # Test output_attentions=True
				if self.has_attentions:
					__lowerCAmelCase      :						List[Any]						      =				backbone(**_SCREAMING_SNAKE_CASE	,		output_attentions=_SCREAMING_SNAKE_CASE			)
					self.assertIsNotNone(outputs.attentions			)

"""simple docstring"""







import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
							import tensorflow as tf

							from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments




@require_tf
class 			A__	(     unittest.TestCase):

		def 							__lowerCamelCase							(				self	,		_SCREAMING_SNAKE_CASE			):
			for model_result in results.values():
				for batch_size, sequence_length in zip(model_result['bs']	,		model_result['ss']			):
					__lowerCAmelCase      :						Optional[Any]						      =				model_result['result'][batch_size][sequence_length]
					self.assertIsNotNone(_SCREAMING_SNAKE_CASE			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Dict						      =				'sshleifer/tiny-gpt2'
			__lowerCAmelCase      :						int						      =				TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]	,		training=_SCREAMING_SNAKE_CASE	,		inference=_SCREAMING_SNAKE_CASE	,		sequence_lengths=[8]	,		batch_sizes=[1]	,		eager_mode=_SCREAMING_SNAKE_CASE	,		multi_process=_SCREAMING_SNAKE_CASE	,		)
			__lowerCAmelCase      :						Tuple						      =				TensorFlowBenchmark(_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						Optional[int]						      =				benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result			)
			self.check_results_dict_not_empty(results.memory_inference_result			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						int						      =				'sgugger/tiny-distilbert-classification'
			__lowerCAmelCase      :						Any						      =				TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]	,		training=_SCREAMING_SNAKE_CASE	,		inference=_SCREAMING_SNAKE_CASE	,		sequence_lengths=[8]	,		batch_sizes=[1]	,		multi_process=_SCREAMING_SNAKE_CASE	,		only_pretrain_model=_SCREAMING_SNAKE_CASE	,		)
			__lowerCAmelCase      :						Optional[int]						      =				TensorFlowBenchmark(_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						Optional[Any]						      =				benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result			)
			self.check_results_dict_not_empty(results.memory_inference_result			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Optional[int]						      =				'sshleifer/tiny-gpt2'
			__lowerCAmelCase      :						str						      =				TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]	,		training=_SCREAMING_SNAKE_CASE	,		inference=_SCREAMING_SNAKE_CASE	,		sequence_lengths=[8]	,		batch_sizes=[1]	,		multi_process=_SCREAMING_SNAKE_CASE	,		)
			__lowerCAmelCase      :						List[str]						      =				TensorFlowBenchmark(_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						Tuple						      =				benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result			)
			self.check_results_dict_not_empty(results.memory_inference_result			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Union[str, Any]						      =				'sshleifer/tiny-gpt2'
			__lowerCAmelCase      :						int						      =				AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						Any						      =				TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]	,		training=_SCREAMING_SNAKE_CASE	,		inference=_SCREAMING_SNAKE_CASE	,		sequence_lengths=[8]	,		batch_sizes=[1]	,		eager_mode=_SCREAMING_SNAKE_CASE	,		multi_process=_SCREAMING_SNAKE_CASE	,		)
			__lowerCAmelCase      :						Dict						      =				TensorFlowBenchmark(_SCREAMING_SNAKE_CASE	,		[config]			)
			__lowerCAmelCase      :						str						      =				benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result			)
			self.check_results_dict_not_empty(results.memory_inference_result			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						List[Any]						      =				'sshleifer/tiny-gpt2'
			__lowerCAmelCase      :						Tuple						      =				AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						int						      =				TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]	,		training=_SCREAMING_SNAKE_CASE	,		inference=_SCREAMING_SNAKE_CASE	,		sequence_lengths=[8]	,		batch_sizes=[1]	,		multi_process=_SCREAMING_SNAKE_CASE	,		)
			__lowerCAmelCase      :						Any						      =				TensorFlowBenchmark(_SCREAMING_SNAKE_CASE	,		[config]			)
			__lowerCAmelCase      :						Dict						      =				benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result			)
			self.check_results_dict_not_empty(results.memory_inference_result			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Any						      =				'sshleifer/tiny-gpt2'
			__lowerCAmelCase      :						str						      =				TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]	,		training=_SCREAMING_SNAKE_CASE	,		inference=_SCREAMING_SNAKE_CASE	,		sequence_lengths=[8]	,		batch_sizes=[1]	,		multi_process=_SCREAMING_SNAKE_CASE	,		)
			__lowerCAmelCase      :						str						      =				TensorFlowBenchmark(_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						List[str]						      =				benchmark.run()
			self.check_results_dict_not_empty(results.time_train_result			)
			self.check_results_dict_not_empty(results.memory_train_result			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						List[str]						      =				'sshleifer/tiny-gpt2'
			__lowerCAmelCase      :						Any						      =				AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						List[str]						      =				TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]	,		training=_SCREAMING_SNAKE_CASE	,		inference=_SCREAMING_SNAKE_CASE	,		sequence_lengths=[8]	,		batch_sizes=[1]	,		multi_process=_SCREAMING_SNAKE_CASE	,		)
			__lowerCAmelCase      :						List[Any]						      =				TensorFlowBenchmark(_SCREAMING_SNAKE_CASE	,		[config]			)
			__lowerCAmelCase      :						List[Any]						      =				benchmark.run()
			self.check_results_dict_not_empty(results.time_train_result			)
			self.check_results_dict_not_empty(results.memory_train_result			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Optional[int]						      =				'patrickvonplaten/t5-tiny-random'
			__lowerCAmelCase      :						int						      =				AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						List[str]						      =				TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]	,		training=_SCREAMING_SNAKE_CASE	,		inference=_SCREAMING_SNAKE_CASE	,		sequence_lengths=[8]	,		batch_sizes=[1]	,		multi_process=_SCREAMING_SNAKE_CASE	,		)
			__lowerCAmelCase      :						Dict						      =				TensorFlowBenchmark(_SCREAMING_SNAKE_CASE	,		configs=[config]			)
			__lowerCAmelCase      :						Any						      =				benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result			)
			self.check_results_dict_not_empty(results.memory_inference_result			)

		@unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices('GPU'			)			) == 0	,		'Cannot do xla on CPU.'			)
		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						List[str]						      =				'sshleifer/tiny-gpt2'
			__lowerCAmelCase      :						List[Any]						      =				TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]	,		training=_SCREAMING_SNAKE_CASE	,		inference=_SCREAMING_SNAKE_CASE	,		sequence_lengths=[8]	,		batch_sizes=[1]	,		use_xla=_SCREAMING_SNAKE_CASE	,		multi_process=_SCREAMING_SNAKE_CASE	,		)
			__lowerCAmelCase      :						Optional[int]						      =				TensorFlowBenchmark(_SCREAMING_SNAKE_CASE			)
			__lowerCAmelCase      :						Dict						      =				benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result			)
			self.check_results_dict_not_empty(results.memory_inference_result			)

		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Optional[int]						      =				'sshleifer/tiny-gpt2'
			with tempfile.TemporaryDirectory() as tmp_dir:
				__lowerCAmelCase      :						List[str]						      =				TensorFlowBenchmarkArguments(
				    models=[MODEL_ID]	,		inference=_SCREAMING_SNAKE_CASE	,		save_to_csv=_SCREAMING_SNAKE_CASE	,		sequence_lengths=[8]	,		batch_sizes=[1]	,		inference_time_csv_file=os.path.join(_SCREAMING_SNAKE_CASE	,		'inf_time.csv'			)	,		inference_memory_csv_file=os.path.join(_SCREAMING_SNAKE_CASE	,		'inf_mem.csv'			)	,		env_info_csv_file=os.path.join(_SCREAMING_SNAKE_CASE	,		'env.csv'			)	,		multi_process=_SCREAMING_SNAKE_CASE	,		)
				__lowerCAmelCase      :						Tuple						      =				TensorFlowBenchmark(_SCREAMING_SNAKE_CASE			)
				benchmark.run()
				self.assertTrue(Path(os.path.join(_SCREAMING_SNAKE_CASE	,		'inf_time.csv'			)			).exists()			)
				self.assertTrue(Path(os.path.join(_SCREAMING_SNAKE_CASE	,		'inf_mem.csv'			)			).exists()			)
				self.assertTrue(Path(os.path.join(_SCREAMING_SNAKE_CASE	,		'env.csv'			)			).exists()			)







		def 							__lowerCamelCase							(				self			):
			__lowerCAmelCase      :						Tuple						      =				'sshleifer/tiny-gpt2'

			def _check_summary_is_not_empty(_SCREAMING_SNAKE_CASE			):
				self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE	,		'sequential'			)			)
				self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE	,		'cumulative'			)			)
				self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE	,		'current'			)			)
				self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE	,		'total'			)			)

			with tempfile.TemporaryDirectory() as tmp_dir:
				__lowerCAmelCase      :						Any						      =				TensorFlowBenchmarkArguments(
				    models=[MODEL_ID]	,		inference=_SCREAMING_SNAKE_CASE	,		sequence_lengths=[8]	,		batch_sizes=[1]	,		log_filename=os.path.join(_SCREAMING_SNAKE_CASE	,		'log.txt'			)	,		log_print=_SCREAMING_SNAKE_CASE	,		trace_memory_line_by_line=_SCREAMING_SNAKE_CASE	,		eager_mode=_SCREAMING_SNAKE_CASE	,		multi_process=_SCREAMING_SNAKE_CASE	,		)
				__lowerCAmelCase      :						Any						      =				TensorFlowBenchmark(_SCREAMING_SNAKE_CASE			)
				__lowerCAmelCase      :						int						      =				benchmark.run()
				_check_summary_is_not_empty(result.inference_summary			)
				self.assertTrue(Path(os.path.join(_SCREAMING_SNAKE_CASE	,		'log.txt'			)			).exists()			)