





"""simple docstring"""





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      (       _lowercase ):


						def     snake_case_   (self		) ->   Tuple:
								UpperCamelCase   =	self.config_class(**self.inputs_dict		)
								self.parent.assertTrue(hasattr(lowercase_   ,							"hidden_sizes"		)		)
								self.parent.assertTrue(hasattr(lowercase_   ,							"num_attention_heads"		)		)
								self.parent.assertTrue(hasattr(lowercase_   ,							"num_encoder_blocks"		)		)





class        _lowerCamelCase      :


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


						def     snake_case_   (self		) ->   Dict:
								UpperCamelCase   =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

								UpperCamelCase   =	None
								if self.use_labels:
										UpperCamelCase   =	ids_tensor([self.batch_size, self.image_size, self.image_size]   ,							self.num_labels		)

								UpperCamelCase   =	self.get_config()
								return config, pixel_values, labels


						def     snake_case_   (self		) ->   Union[str, Any]:
								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     snake_case_   (self   ,							__a   ,							__a   ,							__a		) ->   List[str]:
								UpperCamelCase   =	SegformerModel(config=lowercase_		)
								model.to(lowercase_		)
								model.eval()
								UpperCamelCase   =	model(lowercase_		)
								UpperCamelCase   =	UpperCamelCase   =	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     snake_case_   (self   ,							__a   ,							__a   ,							__a		) ->   Union[str, Any]:
								UpperCamelCase   =	self.num_labels
								UpperCamelCase   =	SegformerForSemanticSegmentation(lowercase_		)
								model.to(lowercase_		)
								model.eval()
								UpperCamelCase   =	model(lowercase_		)
								self.parent.assertEqual(
								    result.logits.shape   ,							(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)		)
								UpperCamelCase   =	model(lowercase_   ,							labels=lowercase_		)
								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     snake_case_   (self   ,							__a   ,							__a   ,							__a		) ->   str:
								UpperCamelCase   =	1
								UpperCamelCase   =	SegformerForSemanticSegmentation(config=lowercase_		)
								model.to(lowercase_		)
								model.eval()
								UpperCamelCase   =	torch.randint(0   ,							1   ,							(self.batch_size, self.image_size, self.image_size)		).to(lowercase_		)
								UpperCamelCase   =	model(lowercase_   ,							labels=lowercase_		)
								self.parent.assertGreater(result.loss   ,							0.0		)




						def     snake_case_   (self		) ->   Dict:
								UpperCamelCase   =	self.prepare_config_and_inputs()
								UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   =	config_and_inputs
								UpperCamelCase   =	{"pixel_values": pixel_values}
								return config, inputs_dict





@require_torch
class        _lowerCamelCase      (       _lowercase  ,			_lowercase  ,			unittest.TestCase ):
						UpperCAmelCase_ =			(
						    (
						        SegformerModel,
						        SegformerForSemanticSegmentation,
						        SegformerForImageClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						UpperCAmelCase_ =			(
						    {
						        "feature-extraction": SegformerModel,
						        "image-classification": SegformerForImageClassification,
						        "image-segmentation": SegformerForSemanticSegmentation,
						    }
						    if is_torch_available()
						    else {}
						)

						UpperCAmelCase_ =			True
						UpperCAmelCase_ =			False
						UpperCAmelCase_ =			False
						UpperCAmelCase_ =			False


						def     snake_case_   (self		) ->   List[Any]:
								UpperCamelCase   =	SegformerModelTester(self		)
								UpperCamelCase   =	SegformerConfigTester(self   ,							config_class=lowercase_		)


						def     snake_case_   (self		) ->   List[str]:
								self.config_tester.run_common_tests()


						def     snake_case_   (self		) ->   Tuple:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowercase_		)


						def     snake_case_   (self		) ->   List[str]:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_binary_image_segmentation(*lowercase_		)


						def     snake_case_   (self		) ->   Any:
								UpperCamelCase   =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_segmentation(*lowercase_		)


						@unittest.skip("SegFormer does not use inputs_embeds"		)
						def     snake_case_   (self		) ->   str:
								pass


						@unittest.skip("SegFormer does not have get_input_embeddings method and get_output_embeddings methods"		)
						def     snake_case_   (self		) ->   List[str]:
								pass


						def     snake_case_   (self		) ->   str:
								UpperCamelCase   ,				UpperCamelCase   =	self.model_tester.prepare_config_and_inputs_for_common()

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

										UpperCamelCase   =	["pixel_values"]
										self.assertListEqual(arg_names[:1]   ,							lowercase_		)


						def     snake_case_   (self		) ->   List[str]:
								UpperCamelCase   ,				UpperCamelCase   =	self.model_tester.prepare_config_and_inputs_for_common()
								UpperCamelCase   =	True

								for model_class in self.all_model_classes:
										UpperCamelCase   =	True
										UpperCamelCase   =	False
										UpperCamelCase   =	True
										UpperCamelCase   =	model_class(lowercase_		)
										model.to(lowercase_		)
										model.eval()
										with torch.no_grad():
												UpperCamelCase   =	model(**self._prepare_for_class(lowercase_   ,							lowercase_		)		)
										UpperCamelCase   =	outputs.attentions

										UpperCamelCase   =	sum(self.model_tester.depths		)
										self.assertEqual(len(lowercase_		)   ,							lowercase_		)

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

										self.assertEqual(len(lowercase_		)   ,							lowercase_		)

										# verify the first attentions (first block, first layer)
										UpperCamelCase   =	(self.model_tester.image_size // 4) ** 2
										UpperCamelCase   =	(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)
										UpperCamelCase   =	(self.model_tester.image_size // 32) ** 2
										UpperCamelCase   =	(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]   ,							)
										UpperCamelCase   =	len(lowercase_		)

										# Check attention is always last and order is fine
										UpperCamelCase   =	True
										UpperCamelCase   =	True
										UpperCamelCase   =	model_class(lowercase_		)
										model.to(lowercase_		)
										model.eval()
										with torch.no_grad():
												UpperCamelCase   =	model(**self._prepare_for_class(lowercase_   ,							lowercase_		)		)

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

										UpperCamelCase   =	outputs.attentions

										self.assertEqual(len(lowercase_		)   ,							lowercase_		)
										# verify the first attentions (first block, first layer)
										UpperCamelCase   =	(self.model_tester.image_size // 4) ** 2
										UpperCamelCase   =	(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     snake_case_   (self		) ->   List[Any]:







								def check_hidden_states_output(__a   ,							__a   ,							__a		):
										UpperCamelCase   =	model_class(lowercase_		)
										model.to(lowercase_		)
										model.eval()

										with torch.no_grad():
												UpperCamelCase   =	model(**self._prepare_for_class(lowercase_   ,							lowercase_		)		)

										UpperCamelCase   =	outputs.hidden_states

										UpperCamelCase   =	self.model_tester.num_encoder_blocks
										self.assertEqual(len(lowercase_		)   ,							lowercase_		)

										# 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,
										    ]   ,							)

								UpperCamelCase   ,				UpperCamelCase   =	self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
										UpperCamelCase   =	True
										check_hidden_states_output(lowercase_   ,							lowercase_   ,							lowercase_		)

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

										check_hidden_states_output(lowercase_   ,							lowercase_   ,							lowercase_		)


						def     snake_case_   (self		) ->   Optional[Any]:







								if not self.model_tester.is_training:
										return

								UpperCamelCase   ,				UpperCamelCase   =	self.model_tester.prepare_config_and_inputs_for_common()
								UpperCamelCase   =	True

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

										UpperCamelCase   =	model_class(lowercase_		)
										model.to(lowercase_		)
										model.train()
										UpperCamelCase   =	self._prepare_for_class(lowercase_   ,							lowercase_   ,							return_labels=lowercase_		)
										UpperCamelCase   =	model(**lowercase_		).loss
										loss.backward()


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




						@slow
						def     snake_case_   (self		) ->   Optional[Any]:







								for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										UpperCamelCase   =	SegformerModel.from_pretrained(lowercase_		)
										self.assertIsNotNone(lowercase_		)



def       a__					(					):




		"""simple docstring"""


		UpperCamelCase   =	Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
		return image






@require_torch
class        _lowerCamelCase      (       unittest.TestCase ):


						@slow
						def     snake_case_   (self		) ->   List[str]:
								UpperCamelCase   =	SegformerImageProcessor(
								    image_scale=(5_12, 5_12)   ,							keep_ratio=lowercase_   ,							align=lowercase_   ,							do_random_crop=lowercase_		)
								UpperCamelCase   =	SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"		).to(
								    lowercase_		)

								UpperCamelCase   =	prepare_img()
								UpperCamelCase   =	image_processor(images=lowercase_   ,							return_tensors="pt"		)
								UpperCamelCase   =	encoded_inputs.pixel_values.to(lowercase_		)

								with torch.no_grad():
										UpperCamelCase   =	model(lowercase_		)

								UpperCamelCase   =	torch.Size((1, model.config.num_labels, 1_28, 1_28)		)
								self.assertEqual(outputs.logits.shape   ,							lowercase_		)

								UpperCamelCase   =	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(lowercase_		)
								self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]   ,							lowercase_   ,							atol=1e-4		)		)


						@slow
						def     snake_case_   (self		) ->   List[str]:
								UpperCamelCase   =	SegformerImageProcessor(
								    image_scale=(5_12, 5_12)   ,							keep_ratio=lowercase_   ,							align=lowercase_   ,							do_random_crop=lowercase_		)
								UpperCamelCase   =	SegformerForSemanticSegmentation.from_pretrained(
								    "nvidia/segformer-b1-finetuned-cityscapes-1024-1024"		).to(lowercase_		)

								UpperCamelCase   =	prepare_img()
								UpperCamelCase   =	image_processor(images=lowercase_   ,							return_tensors="pt"		)
								UpperCamelCase   =	encoded_inputs.pixel_values.to(lowercase_		)

								with torch.no_grad():
										UpperCamelCase   =	model(lowercase_		)

								UpperCamelCase   =	torch.Size((1, model.config.num_labels, 1_28, 1_28)		)
								self.assertEqual(outputs.logits.shape   ,							lowercase_		)

								UpperCamelCase   =	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(lowercase_		)
								self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]   ,							lowercase_   ,							atol=1e-1		)		)




						@slow
						def     snake_case_   (self		) ->   Optional[int]:
								UpperCamelCase   =	SegformerImageProcessor(
								    image_scale=(5_12, 5_12)   ,							keep_ratio=lowercase_   ,							align=lowercase_   ,							do_random_crop=lowercase_		)
								UpperCamelCase   =	SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"		).to(
								    lowercase_		)

								UpperCamelCase   =	prepare_img()
								UpperCamelCase   =	image_processor(images=lowercase_   ,							return_tensors="pt"		)
								UpperCamelCase   =	encoded_inputs.pixel_values.to(lowercase_		)

								with torch.no_grad():
										UpperCamelCase   =	model(lowercase_		)

								UpperCamelCase   =	outputs.logits.detach().cpu()

								UpperCamelCase   =	image_processor.post_process_semantic_segmentation(outputs=lowercase_   ,							target_sizes=[(5_00, 3_00)]		)
								UpperCamelCase   =	torch.Size((5_00, 3_00)		)
								self.assertEqual(segmentation[0].shape   ,							lowercase_		)

								UpperCamelCase   =	image_processor.post_process_semantic_segmentation(outputs=lowercase_		)
								UpperCamelCase   =	torch.Size((1_28, 1_28)		)
								self.assertEqual(segmentation[0].shape   ,							lowercase_		)













"""simple docstring"""





import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
						from transformers import AutoModelForSeqaSeqLM, AutoTokenizer





@require_torch
@require_sentencepiece
@require_tokenizers
class        _lowerCamelCase      (       unittest.TestCase ):


						@slow
						def     snake_case_   (self		) ->   List[str]:
								UpperCamelCase   =	AutoModelForSeqaSeqLM.from_pretrained("google/mt5-small"   ,							return_dict=__a		).to(__a		)
								UpperCamelCase   =	AutoTokenizer.from_pretrained("google/mt5-small"		)

								UpperCamelCase   =	tokenizer("Hello there"   ,							return_tensors="pt"		).input_ids
								UpperCamelCase   =	tokenizer("Hi I am"   ,							return_tensors="pt"		).input_ids

								UpperCamelCase   =	model(input_ids.to(__a		)   ,							labels=labels.to(__a		)		).loss
								UpperCamelCase   =	-(labels.shape[-1] * loss.item())

								UpperCamelCase   =	-84.9127
								self.assertTrue(abs(mtf_score - EXPECTED_SCORE		) < 1e-4		)






