





"""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 			__UpperCamelCase							(		a__		):






							def        __a  (  self )      ->					Tuple:
								a :						Union[str, Any]      =							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 			__UpperCamelCase							:






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






							def        __a  (  self )      ->					int:
								a :						Optional[int]      =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

								a :						List[str]      =							self.get_config()
								return config, pixel_values, labels






							def        __a  (  self )      ->					int:
								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        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					Dict:
								a :						int      =							SegformerModel(config=lowerCAmelCase__ )
								model.to(lowerCAmelCase__ )
								model.eval()
								a :						Optional[int]      =							model(lowerCAmelCase__ )
								a :						Dict      =							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        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					Any:
								a :						List[Any]      =							self.num_labels
								a :						Any      =							SegformerForSemanticSegmentation(lowerCAmelCase__ )
								model.to(lowerCAmelCase__ )
								model.eval()
								a :						List[Any]      =							model(lowerCAmelCase__ )
								self.parent.assertEqual(
								    result.logits.shape ,   (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4) )
								a :						int      =							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        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					str:
								a :						List[Any]      =							1
								a :						Tuple      =							SegformerForSemanticSegmentation(config=lowerCAmelCase__ )
								model.to(lowerCAmelCase__ )
								model.eval()
								a :						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        __a  (  self )      ->					List[str]:
								a :						Tuple      =							self.prepare_config_and_inputs()
								a,			a,			a :						str      =							config_and_inputs
								a :						List[Any]      =							{"pixel_values": pixel_values}
								return config, inputs_dict


@require_torch
class 			__UpperCamelCase							(		a__						,			a__						,			unittest.TestCase		):
							lowerCamelCase  :   Any    =(
							    (
							        SegformerModel,
							        SegformerForSemanticSegmentation,
							        SegformerForImageClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							lowerCamelCase  :   Tuple    =(
							    {
							        """feature-extraction""": SegformerModel,
							        """image-classification""": SegformerForImageClassification,
							        """image-segmentation""": SegformerForSemanticSegmentation,
							    }
							    if is_torch_available()
							    else {}
							)

							lowerCamelCase  :   int    =True
							lowerCamelCase  :   Optional[Any]    =False
							lowerCamelCase  :   Optional[Any]    =False
							lowerCamelCase  :   Any    =False






							def        __a  (  self )      ->					List[str]:
								a :						Any      =							SegformerModelTester(self )
								a :						Any      =							SegformerConfigTester(self ,   config_class=lowerCAmelCase__ )






							def        __a  (  self )      ->					str:
								self.config_tester.run_common_tests()






							def        __a  (  self )      ->					Any:
								a :						Optional[Any]      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowerCAmelCase__ )






							def        __a  (  self )      ->					Optional[Any]:
								a :						Optional[int]      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_binary_image_segmentation(*lowerCAmelCase__ )






							def        __a  (  self )      ->					Union[str, Any]:
								a :						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        __a  (  self )      ->					str:
								pass






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






							def        __a  (  self )      ->					Tuple:
								a,			a :						int      =							self.model_tester.prepare_config_and_inputs_for_common()

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

									a :						str      =							["pixel_values"]
									self.assertListEqual(arg_names[:1] ,   lowerCAmelCase__ )






							def        __a  (  self )      ->					List[str]:
								a,			a :						List[Any]      =							self.model_tester.prepare_config_and_inputs_for_common()
								a :						Optional[int]      =							True

								for model_class in self.all_model_classes:
									a :						int      =							True
									a :						str      =							False
									a :						int      =							True
									a :						int      =							model_class(lowerCAmelCase__ )
									model.to(lowerCAmelCase__ )
									model.eval()
									with torch.no_grad():
										a :						int      =							model(**self._prepare_for_class(lowerCAmelCase__ ,   lowerCAmelCase__ ) )
									a :						Dict      =							outputs.attentions

									a :						List[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 :						Dict      =							True
									a :						List[str]      =							model_class(lowerCAmelCase__ )
									model.to(lowerCAmelCase__ )
									model.eval()
									with torch.no_grad():
										a :						Union[str, Any]      =							model(**self._prepare_for_class(lowerCAmelCase__ ,   lowerCAmelCase__ ) )
									a :						Dict      =							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(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 :						Any      =							(self.model_tester.image_size // 32) ** 2
									a :						Optional[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 :						Any      =							len(lowerCAmelCase__ )

									# Check attention is always last and order is fine
									a :						List[str]      =							True
									a :						List[Any]      =							True
									a :						Any      =							model_class(lowerCAmelCase__ )
									model.to(lowerCAmelCase__ )
									model.eval()
									with torch.no_grad():
										a :						int      =							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 :						str      =							(self.model_tester.image_size // 4) ** 2
									a :						Union[str, Any]      =							(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        __a  (  self )      ->					Union[str, Any]:
								def check_hidden_states_output(lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ):
									a :						Optional[int]      =							model_class(lowerCAmelCase__ )
									model.to(lowerCAmelCase__ )
									model.eval()

									with torch.no_grad():
										a :						List[Any]      =							model(**self._prepare_for_class(lowerCAmelCase__ ,   lowerCAmelCase__ ) )

									a :						str      =							outputs.hidden_states

									a :						int      =							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 :						List[Any]      =							self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
									a :						str      =							True
									check_hidden_states_output(lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )

									# check that output_hidden_states also work using config
									del inputs_dict["output_hidden_states"]
									a :						Union[str, Any]      =							True

									check_hidden_states_output(lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )






							def        __a  (  self )      ->					int:
								if not self.model_tester.is_training:
									return

								a,			a :						Any      =							self.model_tester.prepare_config_and_inputs_for_common()
								a :						List[Any]      =							True

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

									a :						List[Any]      =							model_class(lowerCAmelCase__ )
									model.to(lowerCAmelCase__ )
									model.train()
									a :						List[str]      =							self._prepare_for_class(lowerCAmelCase__ ,   lowerCAmelCase__ ,   return_labels=lowerCAmelCase__ )
									a :						Union[str, Any]      =							model(**lowerCAmelCase__ ).loss
									loss.backward()






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






							@slow
							def        __a  (  self )      ->					Optional[int]:
								for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									a :						int      =							SegformerModel.from_pretrained(lowerCAmelCase__ )
									self.assertIsNotNone(lowerCAmelCase__ )






def  _SCREAMING_SNAKE_CASE						(      )					->Dict:






	'''simple docstring'''






	a :						List[Any]      =							Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"   )
	return image







@require_torch
class 			__UpperCamelCase							(		unittest.TestCase		):






							@slow
							def        __a  (  self )      ->					int:
								# only resize + normalize
								a :						str      =							SegformerImageProcessor(
								    image_scale=(512, 512) ,   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 :						str      =							prepare_img()
								a :						Any      =							image_processor(images=lowerCAmelCase__ ,   return_tensors="pt" )
								a :						Optional[Any]      =							encoded_inputs.pixel_values.to(lowerCAmelCase__ )

								with torch.no_grad():
									a :						Any      =							model(lowerCAmelCase__ )

								a :						str      =							torch.Size((1, model.config.num_labels, 128, 128) )
								self.assertEqual(outputs.logits.shape ,   lowerCAmelCase__ )

								a :						List[str]      =							torch.tensor(
								    [
								        [[-4.6_310, -5.5_232, -6.2_356], [-5.1_921, -6.1_444, -6.5_996], [-5.4_424, -6.2_790, -6.7_574]],
								        [[-12.1_391, -13.3_122, -13.9_554], [-12.8_732, -13.9_352, -14.3_563], [-12.9_438, -13.8_226, -14.2_513]],
								        [[-12.5_134, -13.4_686, -14.4_915], [-12.8_669, -14.4_343, -14.7_758], [-13.2_523, -14.5_819, -15.0_694]],
								    ] ).to(lowerCAmelCase__ )
								self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] ,   lowerCAmelCase__ ,   atol=1E-4 ) )






							@slow
							def        __a  (  self )      ->					Union[str, Any]:
								# only resize + normalize
								a :						Union[str, Any]      =							SegformerImageProcessor(
								    image_scale=(512, 512) ,   keep_ratio=lowerCAmelCase__ ,   align=lowerCAmelCase__ ,   do_random_crop=lowerCAmelCase__ )
								a :						str      =							SegformerForSemanticSegmentation.from_pretrained(
								    "nvidia/segformer-b1-finetuned-cityscapes-1024-1024" ).to(lowerCAmelCase__ )

								a :						int      =							prepare_img()
								a :						str      =							image_processor(images=lowerCAmelCase__ ,   return_tensors="pt" )
								a :						str      =							encoded_inputs.pixel_values.to(lowerCAmelCase__ )

								with torch.no_grad():
									a :						Any      =							model(lowerCAmelCase__ )

								a :						List[Any]      =							torch.Size((1, model.config.num_labels, 128, 128) )
								self.assertEqual(outputs.logits.shape ,   lowerCAmelCase__ )

								a :						int      =							torch.tensor(
								    [
								        [[-13.5_748, -13.9_111, -12.6_500], [-14.3_500, -15.3_683, -14.2_328], [-14.7_532, -16.0_424, -15.6_087]],
								        [[-17.1_651, -15.8_725, -12.9_653], [-17.2_580, -17.3_718, -14.8_223], [-16.6_058, -16.8_783, -16.7_452]],
								        [[-3.6_456, -3.0_209, -1.4_203], [-3.0_797, -3.1_959, -2.0_000], [-1.8_757, -1.9_217, -1.6_997]],
								    ] ).to(lowerCAmelCase__ )
								self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3] ,   lowerCAmelCase__ ,   atol=1E-1 ) )






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

								a :						List[str]      =							prepare_img()
								a :						List[Any]      =							image_processor(images=lowerCAmelCase__ ,   return_tensors="pt" )
								a :						str      =							encoded_inputs.pixel_values.to(lowerCAmelCase__ )

								with torch.no_grad():
									a :						Union[str, Any]      =							model(lowerCAmelCase__ )

								a :						Optional[Any]      =							outputs.logits.detach().cpu()

								a :						List[str]      =							image_processor.post_process_semantic_segmentation(outputs=lowerCAmelCase__ ,   target_sizes=[(500, 300)] )
								a :						Tuple      =							torch.Size((500, 300) )
								self.assertEqual(segmentation[0].shape ,   lowerCAmelCase__ )

								a :						str      =							image_processor.post_process_semantic_segmentation(outputs=lowerCAmelCase__ )
								a :						Tuple      =							torch.Size((128, 128) )
								self.assertEqual(segmentation[0].shape ,   lowerCAmelCase__ )









"""simple docstring"""




import unittest

from transformers import DonutProcessor


a		:					Optional[int]	      =     '''naver-clova-ix/donut-base'''


class 			__UpperCamelCase							(		unittest.TestCase		):






							def        __a  (  self )      ->					Dict:
								a :						Optional[Any]      =							DonutProcessor.from_pretrained(lowerCAmelCase__ )






							def        __a  (  self )      ->					List[Any]:
								a :						Optional[Any]      =							{
								    "name": "John Doe",
								    "age": "99",
								    "city": "Atlanta",
								    "state": "GA",
								    "zip": "30301",
								    "phone": "123-4567",
								    "nicknames": [{"nickname": "Johnny"}, {"nickname": "JD"}],
								}

								a :						Tuple      =							(
								    "<s_name>John Doe</s_name><s_age>99</s_age><s_city>Atlanta</s_city>"
								    "<s_state>GA</s_state><s_zip>30301</s_zip><s_phone>123-4567</s_phone>"
								    "<s_nicknames><s_nickname>Johnny</s_nickname>"
								    "<sep/><s_nickname>JD</s_nickname></s_nicknames>"
								)
								a :						List[Any]      =							self.processor.tokenajson(lowerCAmelCase__ )

								self.assertDictEqual(lowerCAmelCase__ ,   lowerCAmelCase__ )


