


import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import MaskaFormerConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import MaskaFormerForUniversalSegmentation, MaskaFormerModel

	if is_vision_available():
		from transformers import MaskaFormerImageProcessor

if is_vision_available():
	from PIL import Image




class       lowercase_					:





				def __init__(				self       ,  lowercase_       ,  lowercase_=2       ,  lowercase_=True       ,  lowercase_=False       ,  lowercase_=10       ,  lowercase_=3       ,  lowercase_=32 * 8       ,  lowercase_=32 * 8       ,  lowercase_=4       ,  lowercase_=64       ,  )  -> Union[str, Any]:
											a__      =parent
											a__      =batch_size
											a__      =is_training
											a__      =use_auxiliary_loss
											a__      =num_queries
											a__      =num_channels
											a__      =min_size
											a__      =max_size
											a__      =num_labels
											a__      =hidden_dim
											a__      =hidden_dim





				def 						__UpperCamelCase    (				self)  -> int:
											a__      =floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]).to(
											    lowercase_)

											a__      =torch.ones([self.batch_size, self.min_size, self.max_size]       ,  device=lowercase_)

											a__      =(
											    torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]       ,  device=lowercase_) > 0.5
											).float()
											a__      =(torch.rand((self.batch_size, self.num_labels)       ,  device=lowercase_) > 0.5).long()

											a__      =self.get_config()
											return config, pixel_values, pixel_mask, mask_labels, class_labels





				def 						__UpperCamelCase    (				self)  -> List[Any]:
											a__      =MaskaFormerConfig(
											    hidden_size=self.hidden_dim       ,  )
											a__      =self.num_queries
											a__      =self.num_labels

											a__      =[1, 1, 1, 1]
											a__      =self.num_channels

											a__      =64
											a__      =128
											a__      =self.hidden_dim
											a__      =self.hidden_dim
											a__      =self.hidden_dim
											return config





				def 						__UpperCamelCase    (				self)  -> str:
											a__    ,     a__    ,     a__    ,     a__    ,     a__      =self.prepare_config_and_inputs()
											a__      ={'pixel_values': pixel_values, 'pixel_mask': pixel_mask}
											return config, inputs_dict





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_)  -> Union[str, Any]:
											a__      =output.encoder_hidden_states
											a__      =output.pixel_decoder_hidden_states
											a__      =output.transformer_decoder_hidden_states

											self.parent.assertTrue(len(lowercase_)       ,  len(config.backbone_config.depths))
											self.parent.assertTrue(len(lowercase_)       ,  len(config.backbone_config.depths))
											self.parent.assertTrue(len(lowercase_)       ,  config.decoder_layers)





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_=False)  -> Any:
											with torch.no_grad():
																		a__      =MaskaFormerModel(config=lowercase_)
																		model.to(lowercase_)
																		model.eval()

																		a__      =model(pixel_values=lowercase_       ,  pixel_mask=lowercase_)
																		a__      =model(lowercase_       ,  output_hidden_states=lowercase_)

											self.parent.assertEqual(
											    output.transformer_decoder_last_hidden_state.shape       ,  (self.batch_size, self.num_queries, self.hidden_dim)       ,  )
											# let's ensure the other two hidden state exists
											self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None)
											self.parent.assertTrue(output.encoder_last_hidden_state is not None)

											if output_hidden_states:
																		self.check_output_hidden_state(lowercase_       ,  lowercase_)





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_)  -> Any:
											a__      =MaskaFormerForUniversalSegmentation(config=lowercase_)
											model.to(lowercase_)
											model.eval()

											def comm_check_on_output(lowercase_):
																		# let's still check that all the required stuff is there
																		self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None)
																		self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None)
																		self.parent.assertTrue(result.encoder_last_hidden_state is not None)
																		# okay, now we need to check the logits shape
																		# due to the encoder compression, masks have a //4 spatial size
																		self.parent.assertEqual(
																		    result.masks_queries_logits.shape       ,  (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4)       ,  )
																		# + 1 for null class
																		self.parent.assertEqual(
																		    result.class_queries_logits.shape       ,  (self.batch_size, self.num_queries, self.num_labels + 1))

											with torch.no_grad():
																		a__      =model(pixel_values=lowercase_       ,  pixel_mask=lowercase_)
																		a__      =model(lowercase_)

																		comm_check_on_output(lowercase_)

																		a__      =model(
																		    pixel_values=lowercase_       ,  pixel_mask=lowercase_       ,  mask_labels=lowercase_       ,  class_labels=lowercase_)

											comm_check_on_output(lowercase_)

											self.parent.assertTrue(result.loss is not None)
											self.parent.assertEqual(result.loss.shape       ,  torch.Size([1]))







@require_torch
class       lowercase_					(lowercase__      ,					lowercase__      ,					unittest.TestCase   ):
				snake_case	    =(MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else ()
				snake_case	    ={'feature-extraction': MaskaFormerModel} if is_torch_available() else {}

				snake_case	    =False
				snake_case	    =False
				snake_case	    =False
				snake_case	    =False





				def 						__UpperCamelCase    (				self)  -> Optional[int]:
											a__      =MaskaFormerModelTester(self)
											a__      =ConfigTester(self       ,  config_class=lowercase_       ,  has_text_modality=lowercase_)





				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											self.config_tester.run_common_tests()





				def 						__UpperCamelCase    (				self)  -> List[str]:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()
											self.model_tester.create_and_check_maskaformer_model(lowercase_       ,  **lowercase_       ,  output_hidden_states=lowercase_)





				def 						__UpperCamelCase    (				self)  -> List[str]:
											a__      =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*lowercase_)





				@unittest.skip(reason='Mask2Former does not use inputs_embeds')
				def 						__UpperCamelCase    (				self)  -> str:
											pass





				@unittest.skip(reason='Mask2Former does not have a get_input_embeddings method')
				def 						__UpperCamelCase    (				self)  -> Tuple:
											pass





				@unittest.skip(reason='Mask2Former is not a generative model')
				def 						__UpperCamelCase    (				self)  -> List[str]:
											pass





				@unittest.skip(reason='Mask2Former does not use token embeddings')
				def 						__UpperCamelCase    (				self)  -> Tuple:
											pass





				@require_torch_multi_gpu
				@unittest.skip(
				    reason='Mask2Former has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`')
				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											pass





				@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.')
				def 						__UpperCamelCase    (				self)  -> str:
											pass





				def 						__UpperCamelCase    (				self)  -> Optional[Any]:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()

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

																		a__      =['pixel_values']
																		self.assertListEqual(arg_names[:1]       ,  lowercase_)





				@slow
				def 						__UpperCamelCase    (				self)  -> Any:
											for model_name in ["facebook/mask2former-swin-small-coco-instance"]:
																		a__      =MaskaFormerModel.from_pretrained(lowercase_)
																		self.assertIsNotNone(lowercase_)





				def 						__UpperCamelCase    (				self)  -> Dict:
											a__      =(self.model_tester.min_size,) * 2
											a__      ={
											    'pixel_values': torch.randn((2, 3, *size)       ,  device=lowercase_),
											    'mask_labels': torch.randn((2, 10, *size)       ,  device=lowercase_),
											    'class_labels': torch.zeros(2       ,  10       ,  device=lowercase_).long(),
											}
											a__      =self.model_tester.get_config()

											a__      =MaskaFormerForUniversalSegmentation(lowercase_).to(lowercase_)
											a__      =model(**lowercase_)
											self.assertTrue(outputs.loss is not None)





				def 						__UpperCamelCase    (				self)  -> Tuple:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()
											self.model_tester.create_and_check_maskaformer_model(lowercase_       ,  **lowercase_       ,  output_hidden_states=lowercase_)





				def 						__UpperCamelCase    (				self)  -> int:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		a__      =model_class(lowercase_).to(lowercase_)
																		a__      =model(**lowercase_       ,  output_attentions=lowercase_)
																		self.assertTrue(outputs.attentions is not None)





				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											if not self.model_tester.is_training:
																		return

											a__      =self.all_model_classes[1]
											a__    ,     a__    ,     a__    ,     a__    ,     a__      =self.model_tester.prepare_config_and_inputs()

											a__      =model_class(lowercase_)
											model.to(lowercase_)
											model.train()

											a__      =model(lowercase_       ,  mask_labels=lowercase_       ,  class_labels=lowercase_).loss
											loss.backward()





				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											a__      =self.all_model_classes[1]
											a__    ,     a__    ,     a__    ,     a__    ,     a__      =self.model_tester.prepare_config_and_inputs()
											a__      =True
											a__      =True

											a__      =model_class(lowercase_).to(lowercase_)
											model.train()

											a__      =model(lowercase_       ,  mask_labels=lowercase_       ,  class_labels=lowercase_)

											a__      =outputs.encoder_hidden_states[0]
											encoder_hidden_states.retain_grad()

											a__      =outputs.pixel_decoder_hidden_states[0]
											pixel_decoder_hidden_states.retain_grad()

											a__      =outputs.transformer_decoder_hidden_states[0]
											transformer_decoder_hidden_states.retain_grad()

											a__      =outputs.attentions[0]
											attentions.retain_grad()

											outputs.loss.backward(retain_graph=lowercase_)

											self.assertIsNotNone(encoder_hidden_states.grad)
											self.assertIsNotNone(pixel_decoder_hidden_states.grad)
											self.assertIsNotNone(transformer_decoder_hidden_states.grad)
											self.assertIsNotNone(attentions.grad)







_lowerCAmelCase:						str   				=       1e-4






def   _lowercase(					):
							a__      =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
							return image







@require_vision
@slow
class       lowercase_					(unittest.TestCase   ):





				@cached_property
				def 						__UpperCamelCase    (				self)  -> Tuple:
											return "facebook/mask2former-swin-small-coco-instance"





				@cached_property
				def 						__UpperCamelCase    (				self)  -> Optional[Any]:
											return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints) if is_vision_available() else None





				def 						__UpperCamelCase    (				self)  -> str:
											a__      =MaskaFormerModel.from_pretrained(self.model_checkpoints).to(lowercase_)
											a__      =self.default_image_processor
											a__      =prepare_img()
											a__      =image_processor(lowercase_       ,  return_tensors='pt').to(lowercase_)
											a__      =inputs['pixel_values'].shape
											# check size is divisible by 32
											self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0)
											# check size
											self.assertEqual(lowercase_       ,  (1, 3, 384, 384))

											with torch.no_grad():
																		a__      =model(**lowercase_)

											a__      =torch.tensor(
											    [[-0.27_90, -1.07_17, -1.16_68], [-0.51_28, -0.31_28, -0.49_87], [-0.58_32, 0.19_71, -0.01_97]]).to(lowercase_)
											self.assertTrue(
											    torch.allclose(
											        outputs.encoder_last_hidden_state[0, 0, :3, :3]       ,  lowercase_       ,  atol=lowercase_))

											a__      =torch.tensor(
											    [[0.89_73, 1.18_47, 1.17_76], [1.19_34, 1.50_40, 1.51_28], [1.11_53, 1.44_86, 1.49_51]]).to(lowercase_)
											self.assertTrue(
											    torch.allclose(
											        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]       ,  lowercase_       ,  atol=lowercase_))

											a__      =torch.tensor(
											    [[2.11_52, 1.70_00, -0.86_03], [1.58_08, 1.80_04, -0.93_53], [1.60_43, 1.74_95, -0.59_99]]).to(lowercase_)
											self.assertTrue(
											    torch.allclose(
											        outputs.transformer_decoder_last_hidden_state[0, :3, :3]       ,  lowercase_       ,  atol=lowercase_))





				def 						__UpperCamelCase    (				self)  -> Any:
											a__      =MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints).to(lowercase_).eval()
											a__      =self.default_image_processor
											a__      =prepare_img()
											a__      =image_processor(lowercase_       ,  return_tensors='pt').to(lowercase_)
											a__      =inputs['pixel_values'].shape
											# check size is divisible by 32
											self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0)
											# check size
											self.assertEqual(lowercase_       ,  (1, 3, 384, 384))

											with torch.no_grad():
																		a__      =model(**lowercase_)
											# masks_queries_logits
											a__      =outputs.masks_queries_logits
											self.assertEqual(
											    masks_queries_logits.shape       ,  (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4))
											a__      =[
											    [-8.78_39, -9.00_56, -8.81_21],
											    [-7.41_04, -7.03_13, -6.54_01],
											    [-6.61_05, -6.34_27, -6.46_75],
											]
											a__      =torch.tensor(lowercase_).to(lowercase_)
											self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]       ,  lowercase_       ,  atol=lowercase_))
											# class_queries_logits
											a__      =outputs.class_queries_logits
											self.assertEqual(class_queries_logits.shape       ,  (1, model.config.num_queries, model.config.num_labels + 1))
											a__      =torch.tensor(
											    [
											        [1.83_24, -8.08_35, -4.19_22],
											        [0.84_50, -9.00_50, -3.60_53],
											        [0.30_45, -7.72_93, -3.02_75],
											    ]).to(lowercase_)
											self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]       ,  lowercase_       ,  atol=lowercase_))





				def 						__UpperCamelCase    (				self)  -> Optional[Any]:
											a__      =MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints).to(lowercase_).eval()
											a__      =self.default_image_processor

											a__      =image_processor(
											    [np.zeros((3, 800, 1333)), np.zeros((3, 800, 1333))]       ,  segmentation_maps=[np.zeros((384, 384)).astype(np.floataa), np.zeros((384, 384)).astype(np.floataa)]       ,  return_tensors='pt'       ,  )

											a__      =inputs['pixel_values'].to(lowercase_)
											a__      =[el.to(lowercase_) for el in inputs['mask_labels']]
											a__      =[el.to(lowercase_) for el in inputs['class_labels']]

											with torch.no_grad():
																		a__      =model(**lowercase_)

											self.assertTrue(outputs.loss is not None)





import copy
import inspect
import unittest

from transformers import AutoBackbone
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import require_timm, require_torch, torch_device
from transformers.utils.import_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


if is_torch_available():
	import torch

	from transformers import TimmBackbone, TimmBackboneConfig

from ...test_pipeline_mixin import PipelineTesterMixin




class       lowercase_					:





				def __init__(				self       ,  lowercase_       ,  lowercase_=None       ,  lowercase_=None       ,  lowercase_=None       ,  lowercase_="resnet50"       ,  lowercase_=3       ,  lowercase_=32       ,  lowercase_=3       ,  lowercase_=True       ,  lowercase_=True       ,  )  -> Union[str, Any]:
											a__      =parent
											a__      =out_indices if out_indices is not None else [4]
											a__      =stage_names
											a__      =out_features
											a__      =backbone
											a__      =batch_size
											a__      =image_size
											a__      =num_channels
											a__      =use_pretrained_backbone
											a__      =is_training





				def 						__UpperCamelCase    (				self)  -> Optional[Any]:
											a__      =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])
											a__      =self.get_config()

											return config, pixel_values





				def 						__UpperCamelCase    (				self)  -> Tuple:
											return TimmBackboneConfig(
											    image_size=self.image_size       ,  num_channels=self.num_channels       ,  out_features=self.out_features       ,  out_indices=self.out_indices       ,  stage_names=self.stage_names       ,  use_pretrained_backbone=self.use_pretrained_backbone       ,  backbone=self.backbone       ,  )





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_)  -> str:
											a__      =TimmBackbone(config=lowercase_)
											model.to(lowercase_)
											model.eval()
											with torch.no_grad():
																		a__      =model(lowercase_)
											self.parent.assertEqual(
											    result.feature_map[-1].shape       ,  (self.batch_size, model.channels[-1], 14, 14)       ,  )





				def 						__UpperCamelCase    (				self)  -> str:
											a__      =self.prepare_config_and_inputs()
											a__    ,     a__      =config_and_inputs
											a__      ={'pixel_values': pixel_values}
											return config, inputs_dict







@require_torch
@require_timm
class       lowercase_					(lowercase__      ,					lowercase__      ,					lowercase__      ,					unittest.TestCase   ):
				snake_case	    =(TimmBackbone,) if is_torch_available() else ()
				snake_case	    ={'feature-extraction': TimmBackbone} if is_torch_available() else {}
				snake_case	    =False
				snake_case	    =False
				snake_case	    =False
				snake_case	    =False





				def 						__UpperCamelCase    (				self)  -> Optional[Any]:
											a__      =TimmBackboneModelTester(self)
											a__      =ConfigTester(self       ,  config_class=lowercase_       ,  has_text_modality=lowercase_)





				def 						__UpperCamelCase    (				self)  -> Dict:
											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)  -> str:
											a__      ='resnet18'
											a__      ='microsoft/resnet-18'

											a__      =AutoBackbone.from_pretrained(lowercase_       ,  use_timm_backbone=lowercase_)
											a__      =AutoBackbone.from_pretrained(lowercase_)

											self.assertEqual(len(timm_model.out_features)       ,  len(transformers_model.out_features))
											self.assertEqual(len(timm_model.stage_names)       ,  len(transformers_model.stage_names))
											self.assertEqual(timm_model.channels       ,  transformers_model.channels)
											# Out indices are set to the last layer by default. For timm models, we don't know
											# the number of layers in advance, so we set it to (-1,), whereas for transformers
											# models, we set it to [len(stage_names) - 1] (kept for backward compatibility).
											self.assertEqual(timm_model.out_indices       ,  (-1,))
											self.assertEqual(transformers_model.out_indices       ,  [len(timm_model.stage_names) - 1])

											a__      =AutoBackbone.from_pretrained(lowercase_       ,  use_timm_backbone=lowercase_       ,  out_indices=[1, 2, 3])
											a__      =AutoBackbone.from_pretrained(lowercase_       ,  out_indices=[1, 2, 3])

											self.assertEqual(timm_model.out_indices       ,  transformers_model.out_indices)
											self.assertEqual(len(timm_model.out_features)       ,  len(transformers_model.out_features))
											self.assertEqual(timm_model.channels       ,  transformers_model.channels)





				@unittest.skip('TimmBackbone doesn\'t support feed forward chunking')
				def 						__UpperCamelCase    (				self)  -> int:
											pass





				@unittest.skip('TimmBackbone doesn\'t have num_hidden_layers attribute')
				def 						__UpperCamelCase    (				self)  -> List[str]:
											pass





				@unittest.skip('TimmBackbone initialization is managed on the timm side')
				def 						__UpperCamelCase    (				self)  -> Any:
											pass





				@unittest.skip('TimmBackbone models doesn\'t have inputs_embeds')
				def 						__UpperCamelCase    (				self)  -> Any:
											pass





				@unittest.skip('TimmBackbone models doesn\'t have inputs_embeds')
				def 						__UpperCamelCase    (				self)  -> List[str]:
											pass





				@unittest.skip('TimmBackbone model cannot be created without specifying a backbone checkpoint')
				def 						__UpperCamelCase    (				self)  -> Optional[int]:
											pass





				@unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone')
				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											pass





				@unittest.skip('model weights aren\'t tied in TimmBackbone.')
				def 						__UpperCamelCase    (				self)  -> Dict:
											pass





				@unittest.skip('model weights aren\'t tied in TimmBackbone.')
				def 						__UpperCamelCase    (				self)  -> List[Any]:
											pass





				@unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone')
				def 						__UpperCamelCase    (				self)  -> List[str]:
											pass





				@unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone')
				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											pass





				@unittest.skip('TimmBackbone doesn\'t have hidden size info in its configuration.')
				def 						__UpperCamelCase    (				self)  -> int:
											pass





				@unittest.skip('TimmBackbone doesn\'t support output_attentions.')
				def 						__UpperCamelCase    (				self)  -> str:
											pass





				@unittest.skip('Safetensors is not supported by timm.')
				def 						__UpperCamelCase    (				self)  -> Optional[int]:
											pass





				@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.')
				def 						__UpperCamelCase    (				self)  -> Optional[Any]:
											pass





				def 						__UpperCamelCase    (				self)  -> Any:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()

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

																		a__      =['pixel_values']
																		self.assertListEqual(arg_names[:1]       ,  lowercase_)





				def 						__UpperCamelCase    (				self)  -> Any:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()
											a__      =True
											a__      =self.has_attentions

											# no need to test all models as different heads yield the same functionality
											a__      =self.all_model_classes[0]
											a__      =model_class(lowercase_)
											model.to(lowercase_)

											a__      =self._prepare_for_class(lowercase_       ,  lowercase_)
											a__      =model(**lowercase_)
											a__      =outputs[0][-1]

											# Encoder-/Decoder-only models
											a__      =outputs.hidden_states[0]
											hidden_states.retain_grad()

											if self.has_attentions:
																		a__      =outputs.attentions[0]
																		attentions.retain_grad()

											output.flatten()[0].backward(retain_graph=lowercase_)

											self.assertIsNotNone(hidden_states.grad)

											if self.has_attentions:
																		self.assertIsNotNone(attentions.grad)





				def 						__UpperCamelCase    (				self)  -> List[str]:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		a__      =model_class(lowercase_)
																		model.to(lowercase_)
																		model.eval()
																		a__      =model(**lowercase_)

																		self.assertEqual(len(result.feature_maps)       ,  len(config.out_indices))
																		self.assertEqual(len(model.channels)       ,  len(config.out_indices))

																		# Check output of last stage is taken if out_features=None, out_indices=None
																		a__      =copy.deepcopy(lowercase_)
																		a__      =None
																		a__      =model_class(lowercase_)
																		model.to(lowercase_)
																		model.eval()
																		a__      =model(**lowercase_)

																		self.assertEqual(len(result.feature_maps)       ,  1)
																		self.assertEqual(len(model.channels)       ,  1)

																		# Check backbone can be initialized with fresh weights
																		a__      =copy.deepcopy(lowercase_)
																		a__      =False
																		a__      =model_class(lowercase_)
																		model.to(lowercase_)
																		model.eval()
																		a__      =model(**lowercase_)

