



import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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 transformers import (
							    MODEL_MAPPING,
							    SegformerForImageClassification,
							    SegformerForSemanticSegmentation,
							    SegformerModel,
							)
							from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import SegformerImageProcessor

class 	lowerCamelCase_    ( _A						):






						'''simple docstring'''







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[str] )      ->				Union[str, Any]:
									A							:					List[str]              =						self.config_class(**self.inputs_dict )
									self.parent.assertTrue(hasattr(__lowerCamelCase	,	"hidden_sizes" ) )
									self.parent.assertTrue(hasattr(__lowerCamelCase	,	"num_attention_heads" ) )
									self.parent.assertTrue(hasattr(__lowerCamelCase	,	"num_encoder_blocks" ) )







class 	lowerCamelCase_    :






						'''simple docstring'''







						def __init__(	self      :			Union[str, Any]	,	__lowerCamelCase      :			str	,	__lowerCamelCase      :			Tuple=13	,	__lowerCamelCase      :			Union[str, Any]=64	,	__lowerCamelCase      :			Dict=3	,	__lowerCamelCase      :			Union[str, Any]=4	,	__lowerCamelCase      :			Dict=[2, 2, 2, 2]	,	__lowerCamelCase      :			Union[str, Any]=[8, 4, 2, 1]	,	__lowerCamelCase      :			List[Any]=[16, 32, 64, 1_28]	,	__lowerCamelCase      :			Optional[Any]=[1, 4, 8, 16]	,	__lowerCamelCase      :			Optional[Any]=[1, 2, 4, 8]	,	__lowerCamelCase      :			Union[str, Any]=True	,	__lowerCamelCase      :			int=True	,	__lowerCamelCase      :			Tuple="gelu"	,	__lowerCamelCase      :			List[Any]=0.1	,	__lowerCamelCase      :			int=0.1	,	__lowerCamelCase      :			List[Any]=0.02	,	__lowerCamelCase      :			List[Any]=3	,	__lowerCamelCase      :			Optional[int]=None	,	)      ->				Tuple:
									A							:					Tuple              =						parent
									A							:					Optional[int]              =						batch_size
									A							:					Dict              =						image_size
									A							:					Union[str, Any]              =						num_channels
									A							:					Union[str, Any]              =						num_encoder_blocks
									A							:					Optional[int]              =						sr_ratios
									A							:					Dict              =						depths
									A							:					Union[str, Any]              =						hidden_sizes
									A							:					Any              =						downsampling_rates
									A							:					int              =						num_attention_heads
									A							:					Optional[Any]              =						is_training
									A							:					Optional[Any]              =						use_labels
									A							:					List[Any]              =						hidden_act
									A							:					Tuple              =						hidden_dropout_prob
									A							:					Dict              =						attention_probs_dropout_prob
									A							:					Tuple              =						initializer_range
									A							:					str              =						num_labels
									A							:					Any              =						scope







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[Any] )      ->				Tuple:
									A							:					List[Any]              =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

									A							:					List[str]              =						None
									if self.use_labels:
												A							:					Any              =						ids_tensor([self.batch_size, self.image_size, self.image_size]	,	self.num_labels )

									A							:					Dict              =						self.get_config()
									return config, pixel_values, labels







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[Any] )      ->				str:
									return SegformerConfig(
									    image_size=self.image_size	,	num_channels=self.num_channels	,	num_encoder_blocks=self.num_encoder_blocks	,	depths=self.depths	,	hidden_sizes=self.hidden_sizes	,	num_attention_heads=self.num_attention_heads	,	hidden_act=self.hidden_act	,	hidden_dropout_prob=self.hidden_dropout_prob	,	attention_probs_dropout_prob=self.attention_probs_dropout_prob	,	initializer_range=self.initializer_range	,	)







						def 			SCREAMING_SNAKE_CASE__		(	self      :			int	,	__lowerCamelCase      :			Optional[int]	,	__lowerCamelCase      :			int	,	__lowerCamelCase      :			Optional[Any] )      ->				List[Any]:
									A							:					Dict              =						SegformerModel(config=__lowerCamelCase )
									model.to(__lowerCamelCase )
									model.eval()
									A							:					Tuple              =						model(__lowerCamelCase )
									A							:					str              =						self.image_size // (self.downsampling_rates[-1] * 2)
									self.parent.assertEqual(
									    result.last_hidden_state.shape	,	(self.batch_size, self.hidden_sizes[-1], expected_height, expected_width) )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any]	,	__lowerCamelCase      :			Tuple	,	__lowerCamelCase      :			Optional[int]	,	__lowerCamelCase      :			Tuple )      ->				Dict:
									A							:					Optional[int]              =						self.num_labels
									A							:					List[Any]              =						SegformerForSemanticSegmentation(__lowerCamelCase )
									model.to(__lowerCamelCase )
									model.eval()
									A							:					Union[str, Any]              =						model(__lowerCamelCase )
									self.parent.assertEqual(
									    result.logits.shape	,	(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) )
									A							:					str              =						model(__lowerCamelCase	,	labels=__lowerCamelCase )
									self.parent.assertEqual(
									    result.logits.shape	,	(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) )
									self.parent.assertGreater(result.loss	,	0.0 )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[Any]	,	__lowerCamelCase      :			Tuple	,	__lowerCamelCase      :			Dict	,	__lowerCamelCase      :			Optional[int] )      ->				int:
									A							:					Optional[int]              =						1
									A							:					int              =						SegformerForSemanticSegmentation(config=__lowerCamelCase )
									model.to(__lowerCamelCase )
									model.eval()
									A							:					List[Any]              =						torch.randint(0	,	1	,	(self.batch_size, self.image_size, self.image_size) ).to(__lowerCamelCase )
									A							:					Dict              =						model(__lowerCamelCase	,	labels=__lowerCamelCase )
									self.parent.assertGreater(result.loss	,	0.0 )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Tuple )      ->				Optional[Any]:
									A							:					Dict              =						self.prepare_config_and_inputs()
									A       , A       , A							:					List[str]              =						config_and_inputs
									A							:					int              =						{"pixel_values": pixel_values}
									return config, inputs_dict







@require_torch
class 	lowerCamelCase_    ( _A						,_A						,unittest.TestCase						):






						'''simple docstring'''
						a__   =  (
						    (
						        SegformerModel,
						        SegformerForSemanticSegmentation,
						        SegformerForImageClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						a__   =  (
						    {
						        "feature-extraction": SegformerModel,
						        "image-classification": SegformerForImageClassification,
						        "image-segmentation": SegformerForSemanticSegmentation,
						    }
						    if is_torch_available()
						    else {}
						)

						a__   =  True
						a__   =  False
						a__   =  False
						a__   =  False







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[Any] )      ->				List[Any]:
									A							:					List[str]              =						SegformerModelTester(self )
									A							:					Optional[Any]              =						SegformerConfigTester(self	,	config_class=__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[Any] )      ->				Optional[Any]:
									self.config_tester.run_common_tests()







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[str] )      ->				Tuple:
									A							:					List[str]              =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[Any] )      ->				Optional[int]:
									A							:					Optional[int]              =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_binary_image_segmentation(*__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			str )      ->				Optional[int]:
									A							:					List[str]              =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_segmentation(*__lowerCamelCase )







						@unittest.skip("SegFormer does not use inputs_embeds" )
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any] )      ->				Dict:
									pass







						@unittest.skip("SegFormer does not have get_input_embeddings method and get_output_embeddings methods" )
						def 			SCREAMING_SNAKE_CASE__		(	self      :			int )      ->				Dict:
									pass







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any] )      ->				Tuple:
									A       , A							:					Optional[Any]              =						self.model_tester.prepare_config_and_inputs_for_common()

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

												A							:					List[Any]              =						["pixel_values"]
												self.assertListEqual(arg_names[:1]	,	__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			int )      ->				Dict:
									A       , A							:					Union[str, Any]              =						self.model_tester.prepare_config_and_inputs_for_common()
									A							:					int              =						True

									for model_class in self.all_model_classes:
												A							:					int              =						True
												A							:					List[str]              =						False
												A							:					Optional[Any]              =						True
												A							:					int              =						model_class(__lowerCamelCase )
												model.to(__lowerCamelCase )
												model.eval()
												with torch.no_grad():
															A							:					str              =						model(**self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase ) )
												A							:					Dict              =						outputs.attentions

												A							:					Any              =						sum(self.model_tester.depths )
												self.assertEqual(len(__lowerCamelCase )	,	__lowerCamelCase )

												# check that output_attentions also work using config
												del inputs_dict["output_attentions"]
												A							:					str              =						True
												A							:					Optional[Any]              =						model_class(__lowerCamelCase )
												model.to(__lowerCamelCase )
												model.eval()
												with torch.no_grad():
															A							:					str              =						model(**self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase ) )
												A							:					Optional[int]              =						outputs.attentions

												self.assertEqual(len(__lowerCamelCase )	,	__lowerCamelCase )

												# verify the first attentions (first block, first layer)
												A							:					Tuple              =						(self.model_tester.image_size // 4) ** 2
												A							:					Tuple              =						(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
												self.assertListEqual(
												    list(attentions[0].shape[-3:] )	,	[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]	,	)

												# verify the last attentions (last block, last layer)
												A							:					Optional[int]              =						(self.model_tester.image_size // 32) ** 2
												A							:					Union[str, Any]              =						(self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
												self.assertListEqual(
												    list(attentions[-1].shape[-3:] )	,	[self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]	,	)
												A							:					Tuple              =						len(__lowerCamelCase )

												# Check attention is always last and order is fine
												A							:					Any              =						True
												A							:					int              =						True
												A							:					Union[str, Any]              =						model_class(__lowerCamelCase )
												model.to(__lowerCamelCase )
												model.eval()
												with torch.no_grad():
															A							:					str              =						model(**self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase ) )

												self.assertEqual(out_len + 1	,	len(__lowerCamelCase ) )

												A							:					Union[str, Any]              =						outputs.attentions

												self.assertEqual(len(__lowerCamelCase )	,	__lowerCamelCase )
												# verify the first attentions (first block, first layer)
												A							:					Dict              =						(self.model_tester.image_size // 4) ** 2
												A							:					Dict              =						(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
												self.assertListEqual(
												    list(self_attentions[0].shape[-3:] )	,	[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]	,	)







						def 			SCREAMING_SNAKE_CASE__		(	self      :			int )      ->				Union[str, Any]:
									def check_hidden_states_output(__lowerCamelCase      :			Union[str, Any]	,	__lowerCamelCase      :			Union[str, Any]	,	__lowerCamelCase      :			Optional[Any] ):
												A							:					int              =						model_class(__lowerCamelCase )
												model.to(__lowerCamelCase )
												model.eval()

												with torch.no_grad():
															A							:					Tuple              =						model(**self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase ) )

												A							:					List[str]              =						outputs.hidden_states

												A							:					Tuple              =						self.model_tester.num_encoder_blocks
												self.assertEqual(len(__lowerCamelCase )	,	__lowerCamelCase )

												# verify the first hidden states (first block)
												self.assertListEqual(
												    list(hidden_states[0].shape[-3:] )	,	[
												        self.model_tester.hidden_sizes[0],
												        self.model_tester.image_size // 4,
												        self.model_tester.image_size // 4,
												    ]	,	)

									A       , A							:					Union[str, Any]              =						self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
												A							:					Any              =						True
												check_hidden_states_output(__lowerCamelCase	,	__lowerCamelCase	,	__lowerCamelCase )

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

												check_hidden_states_output(__lowerCamelCase	,	__lowerCamelCase	,	__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			int )      ->				List[Any]:
									if not self.model_tester.is_training:
												return

									A       , A							:					Optional[int]              =						self.model_tester.prepare_config_and_inputs_for_common()
									A							:					Tuple              =						True

									for model_class in self.all_model_classes:
												if model_class in get_values(__lowerCamelCase ):
															continue

												A							:					int              =						model_class(__lowerCamelCase )
												model.to(__lowerCamelCase )
												model.train()
												A							:					List[str]              =						self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase	,	return_labels=__lowerCamelCase )
												A							:					int              =						model(**__lowerCamelCase ).loss
												loss.backward()







						@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." )
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any] )      ->				Tuple:
									pass







						@slow
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[Any] )      ->				Optional[Any]:
									for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												A							:					str              =						SegformerModel.from_pretrained(__lowerCamelCase )
												self.assertIsNotNone(__lowerCamelCase )







def 	UpperCAmelCase   (						):
			A							:					List[str]              =						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
			return image






@require_torch
class 	lowerCamelCase_    ( unittest.TestCase						):






						'''simple docstring'''







						@slow
						def 			SCREAMING_SNAKE_CASE__		(	self      :			int )      ->				Union[str, Any]:
									# only resize + normalize
									A							:					List[str]              =						SegformerImageProcessor(
									    image_scale=(5_12, 5_12)	,	keep_ratio=__lowerCamelCase	,	align=__lowerCamelCase	,	do_random_crop=__lowerCamelCase )
									A							:					Dict              =						SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512" ).to(
									    __lowerCamelCase )

									A							:					Optional[int]              =						prepare_img()
									A							:					str              =						image_processor(images=__lowerCamelCase	,	return_tensors="pt" )
									A							:					Optional[int]              =						encoded_inputs.pixel_values.to(__lowerCamelCase )

									with torch.no_grad():
												A							:					Tuple              =						model(__lowerCamelCase )

									A							:					List[str]              =						torch.Size((1, model.config.num_labels, 1_28, 1_28) )
									self.assertEqual(outputs.logits.shape	,	__lowerCamelCase )

									A							:					Tuple              =						torch.tensor(
									    [
									        [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]],
									        [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]],
									        [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]],
									    ] ).to(__lowerCamelCase )
									self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]	,	__lowerCamelCase	,	atol=1e-4 ) )







						@slow
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Dict )      ->				Optional[Any]:
									# only resize + normalize
									A							:					int              =						SegformerImageProcessor(
									    image_scale=(5_12, 5_12)	,	keep_ratio=__lowerCamelCase	,	align=__lowerCamelCase	,	do_random_crop=__lowerCamelCase )
									A							:					Optional[int]              =						SegformerForSemanticSegmentation.from_pretrained(
									    "nvidia/segformer-b1-finetuned-cityscapes-1024-1024" ).to(__lowerCamelCase )

									A							:					int              =						prepare_img()
									A							:					Tuple              =						image_processor(images=__lowerCamelCase	,	return_tensors="pt" )
									A							:					List[Any]              =						encoded_inputs.pixel_values.to(__lowerCamelCase )

									with torch.no_grad():
												A							:					Dict              =						model(__lowerCamelCase )

									A							:					Any              =						torch.Size((1, model.config.num_labels, 1_28, 1_28) )
									self.assertEqual(outputs.logits.shape	,	__lowerCamelCase )

									A							:					Any              =						torch.tensor(
									    [
									        [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]],
									        [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]],
									        [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]],
									    ] ).to(__lowerCamelCase )
									self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]	,	__lowerCamelCase	,	atol=1e-1 ) )







						@slow
						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[str] )      ->				int:
									# only resize + normalize
									A							:					Optional[int]              =						SegformerImageProcessor(
									    image_scale=(5_12, 5_12)	,	keep_ratio=__lowerCamelCase	,	align=__lowerCamelCase	,	do_random_crop=__lowerCamelCase )
									A							:					List[str]              =						SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512" ).to(
									    __lowerCamelCase )

									A							:					Union[str, Any]              =						prepare_img()
									A							:					int              =						image_processor(images=__lowerCamelCase	,	return_tensors="pt" )
									A							:					Any              =						encoded_inputs.pixel_values.to(__lowerCamelCase )

									with torch.no_grad():
												A							:					List[str]              =						model(__lowerCamelCase )

									A							:					Dict              =						outputs.logits.detach().cpu()

									A							:					Any              =						image_processor.post_process_semantic_segmentation(outputs=__lowerCamelCase	,	target_sizes=[(5_00, 3_00)] )
									A							:					Any              =						torch.Size((5_00, 3_00) )
									self.assertEqual(segmentation[0].shape	,	__lowerCamelCase )

									A							:					Optional[int]              =						image_processor.post_process_semantic_segmentation(outputs=__lowerCamelCase )
									A							:					Union[str, Any]              =						torch.Size((1_28, 1_28) )
									self.assertEqual(segmentation[0].shape	,	__lowerCamelCase )




from collections import deque

from .hash_table import HashTable

class 	lowerCamelCase_    ( _A						):






						'''simple docstring'''







						def __init__(	self      :			Union[str, Any]	,	*__lowerCamelCase      :			Dict	,	**__lowerCamelCase      :			int )      ->				Optional[int]:
									super().__init__(*__lowerCamelCase	,	**__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[str]	,	__lowerCamelCase      :			List[str]	,	__lowerCamelCase      :			Union[str, Any] )      ->				Union[str, Any]:
									A							:					Optional[Any]              =						deque([] ) if self.values[key] is None else self.values[key]
									self.values[key].appendleft(__lowerCamelCase )
									A							:					Dict              =						self.values[key]







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any )      ->				str:
									return (
									    sum(self.charge_factor - len(__lowerCamelCase ) for slot in self.values )
									    / self.size_table
									    * self.charge_factor
									)







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[Any]	,	__lowerCamelCase      :			Any	,	__lowerCamelCase      :			List[Any]=None )      ->				Optional[int]:
									if not (
									    len(self.values[key] ) == self.charge_factor and self.values.count(__lowerCamelCase ) == 0
									):
												return key
									return super()._collision_resolution(__lowerCamelCase	,	__lowerCamelCase )