

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        A__(a_							):
						"""simple docstring"""






						def 			UpperCamelCase__					(    self    )     -> List[str]:
									a_						:						Optional[Any] 							= 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        A__:
						"""simple docstring"""






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





						def 			UpperCamelCase__					(    self    )     -> List[str]:
									a_						:						Dict 							= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

									a_						:						int 							= None
									if self.use_labels:
												a_						:						List[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 			UpperCamelCase__					(    self    )     -> 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 			UpperCamelCase__					(    self  ,      _lowercase  ,      _lowercase  ,      _lowercase    )     -> List[Any]:
									a_						:						Optional[Any] 							= SegformerModel(config=_lowercase    )
									model.to(_lowercase    )
									model.eval()
									a_						:						List[str] 							= model(_lowercase    )
									a_						:						int 							= 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 			UpperCamelCase__					(    self  ,      _lowercase  ,      _lowercase  ,      _lowercase    )     -> Optional[int]:
									a_						:						Any 							= self.num_labels
									a_						:						Union[str, Any] 							= SegformerForSemanticSegmentation(_lowercase    )
									model.to(_lowercase    )
									model.eval()
									a_						:						str 							= model(_lowercase    )
									self.parent.assertEqual(
									    result.logits.shape  ,      (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)    )
									a_						:						Optional[Any] 							= 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 			UpperCamelCase__					(    self  ,      _lowercase  ,      _lowercase  ,      _lowercase    )     -> List[Any]:
									a_						:						int 							= 1
									a_						:						Union[str, Any] 							= SegformerForSemanticSegmentation(config=_lowercase    )
									model.to(_lowercase    )
									model.eval()
									a_						:						int 							= torch.randint(0  ,      1  ,      (self.batch_size, self.image_size, self.image_size)    ).to(_lowercase    )
									a_						:						Optional[int] 							= model(_lowercase  ,      labels=_lowercase    )
									self.parent.assertGreater(result.loss  ,      0.0    )





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									a_						:						Union[str, Any] 							= self.prepare_config_and_inputs()
									a_							,					a_							,					a_						:						Optional[int] 							= config_and_inputs
									a_						:						int 							= {"""pixel_values""": pixel_values}
									return config, inputs_dict







@require_torch
class        A__(a_,  a_,  unittest.TestCase							):
						"""simple docstring"""






						_A    :				Dict       					=			(
						    (
						        SegformerModel,
						        SegformerForSemanticSegmentation,
						        SegformerForImageClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						_A    :				List[Any]       					=			(
						    {
						        '''feature-extraction''': SegformerModel,
						        '''image-classification''': SegformerForImageClassification,
						        '''image-segmentation''': SegformerForSemanticSegmentation,
						    }
						    if is_torch_available()
						    else {}
						)

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





						def 			UpperCamelCase__					(    self    )     -> Dict:
									a_						:						str 							= SegformerModelTester(self    )
									a_						:						Optional[int] 							= SegformerConfigTester(self  ,      config_class=_lowercase    )





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





						def 			UpperCamelCase__					(    self    )     -> List[Any]:
									a_						:						str 							= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_lowercase    )





						def 			UpperCamelCase__					(    self    )     -> int:
									a_						:						int 							= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_binary_image_segmentation(*_lowercase    )





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									a_						:						int 							= 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 			UpperCamelCase__					(    self    )     -> Optional[int]:
									pass





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





						def 			UpperCamelCase__					(    self    )     -> Dict:
									a_							,					a_						:						Any 							= self.model_tester.prepare_config_and_inputs_for_common()

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

												a_						:						List[str] 							= ["""pixel_values"""]
												self.assertListEqual(arg_names[:1]  ,      _lowercase    )





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									a_							,					a_						:						Any 							= self.model_tester.prepare_config_and_inputs_for_common()
									a_						:						Optional[int] 							= True

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

												a_						:						Optional[int] 							= sum(self.model_tester.depths    )
												self.assertEqual(len(_lowercase    )  ,      _lowercase    )

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

												self.assertEqual(len(_lowercase    )  ,      _lowercase    )

												# verify the first attentions (first block, first layer)
												a_						:						List[str] 							= (self.model_tester.image_size // 4) ** 2
												a_						:						int 							= (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_						:						int 							= (self.model_tester.image_size // 32) ** 2
												a_						:						Optional[int] 							= (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_						:						Dict 							= len(_lowercase    )

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

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

												a_						:						Optional[int] 							= outputs.attentions

												self.assertEqual(len(_lowercase    )  ,      _lowercase    )
												# verify the first attentions (first block, first layer)
												a_						:						int 							= (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 			UpperCamelCase__					(    self    )     -> Optional[int]:
									def check_hidden_states_output(_lowercase  ,      _lowercase  ,      _lowercase    ):
												a_						:						Tuple 							= model_class(_lowercase    )
												model.to(_lowercase    )
												model.eval()

												with torch.no_grad():
															a_						:						Tuple 							= model(**self._prepare_for_class(_lowercase  ,      _lowercase    )    )

												a_						:						Optional[Any] 							= outputs.hidden_states

												a_						:						Optional[Any] 							= 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,
												    ]  ,      )

									a_							,					a_						:						int 							= self.model_tester.prepare_config_and_inputs_for_common()

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

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

												check_hidden_states_output(_lowercase  ,      _lowercase  ,      _lowercase    )





						def 			UpperCamelCase__					(    self    )     -> Optional[Any]:
									if not self.model_tester.is_training:
												return

									a_							,					a_						:						Dict 							= 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(_lowercase    ):
															continue

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





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





						@slow
						def 			UpperCamelCase__					(    self    )     -> Optional[Any]:
									for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												a_						:						Union[str, Any] 							= SegformerModel.from_pretrained(_lowercase    )
												self.assertIsNotNone(_lowercase    )







def 					_UpperCAmelCase				(						):

			'''simple docstring'''



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



@require_torch
class        A__(unittest.TestCase							):
						"""simple docstring"""






						@slow
						def 			UpperCamelCase__					(    self    )     -> List[str]:
									# only resize + normalize
									a_						:						Tuple 							= SegformerImageProcessor(
									    image_scale=(512, 512)  ,      keep_ratio=_lowercase  ,      align=_lowercase  ,      do_random_crop=_lowercase    )
									a_						:						Dict 							= SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""    ).to(
									    _lowercase    )

									a_						:						str 							= prepare_img()
									a_						:						str 							= image_processor(images=_lowercase  ,      return_tensors="""pt"""    )
									a_						:						List[str] 							= encoded_inputs.pixel_values.to(_lowercase    )

									with torch.no_grad():
												a_						:						Union[str, Any] 							= model(_lowercase    )

									a_						:						Tuple 							= torch.Size((1, model.config.num_labels, 128, 128)    )
									self.assertEqual(outputs.logits.shape  ,      _lowercase    )

									a_						:						Any 							= torch.tensor(
									    [
									        [[-4.6_3_1_0, -5.5_2_3_2, -6.2_3_5_6], [-5.1_9_2_1, -6.1_4_4_4, -6.5_9_9_6], [-5.4_4_2_4, -6.2_7_9_0, -6.7_5_7_4]],
									        [[-1_2.1_3_9_1, -1_3.3_1_2_2, -1_3.9_5_5_4], [-1_2.8_7_3_2, -1_3.9_3_5_2, -1_4.3_5_6_3], [-1_2.9_4_3_8, -1_3.8_2_2_6, -1_4.2_5_1_3]],
									        [[-1_2.5_1_3_4, -1_3.4_6_8_6, -1_4.4_9_1_5], [-1_2.8_6_6_9, -1_4.4_3_4_3, -1_4.7_7_5_8], [-1_3.2_5_2_3, -1_4.5_8_1_9, -1_5.0_6_9_4]],
									    ]    ).to(_lowercase    )
									self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]  ,      _lowercase  ,      atol=1e-4    )    )





						@slow
						def 			UpperCamelCase__					(    self    )     -> Optional[Any]:
									# only resize + normalize
									a_						:						Optional[Any] 							= SegformerImageProcessor(
									    image_scale=(512, 512)  ,      keep_ratio=_lowercase  ,      align=_lowercase  ,      do_random_crop=_lowercase    )
									a_						:						int 							= SegformerForSemanticSegmentation.from_pretrained(
									    """nvidia/segformer-b1-finetuned-cityscapes-1024-1024"""    ).to(_lowercase    )

									a_						:						Any 							= prepare_img()
									a_						:						List[Any] 							= image_processor(images=_lowercase  ,      return_tensors="""pt"""    )
									a_						:						Optional[int] 							= encoded_inputs.pixel_values.to(_lowercase    )

									with torch.no_grad():
												a_						:						List[str] 							= model(_lowercase    )

									a_						:						Tuple 							= torch.Size((1, model.config.num_labels, 128, 128)    )
									self.assertEqual(outputs.logits.shape  ,      _lowercase    )

									a_						:						Any 							= torch.tensor(
									    [
									        [[-1_3.5_7_4_8, -1_3.9_1_1_1, -1_2.6_5_0_0], [-1_4.3_5_0_0, -1_5.3_6_8_3, -1_4.2_3_2_8], [-1_4.7_5_3_2, -1_6.0_4_2_4, -1_5.6_0_8_7]],
									        [[-1_7.1_6_5_1, -1_5.8_7_2_5, -1_2.9_6_5_3], [-1_7.2_5_8_0, -1_7.3_7_1_8, -1_4.8_2_2_3], [-1_6.6_0_5_8, -1_6.8_7_8_3, -1_6.7_4_5_2]],
									        [[-3.6_4_5_6, -3.0_2_0_9, -1.4_2_0_3], [-3.0_7_9_7, -3.1_9_5_9, -2.0_0_0_0], [-1.8_7_5_7, -1.9_2_1_7, -1.6_9_9_7]],
									    ]    ).to(_lowercase    )
									self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]  ,      _lowercase  ,      atol=1e-1    )    )





						@slow
						def 			UpperCamelCase__					(    self    )     -> List[str]:
									# only resize + normalize
									a_						:						Tuple 							= SegformerImageProcessor(
									    image_scale=(512, 512)  ,      keep_ratio=_lowercase  ,      align=_lowercase  ,      do_random_crop=_lowercase    )
									a_						:						str 							= SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""    ).to(
									    _lowercase    )

									a_						:						int 							= prepare_img()
									a_						:						Any 							= image_processor(images=_lowercase  ,      return_tensors="""pt"""    )
									a_						:						Optional[int] 							= encoded_inputs.pixel_values.to(_lowercase    )

									with torch.no_grad():
												a_						:						Any 							= model(_lowercase    )

									a_						:						str 							= outputs.logits.detach().cpu()

									a_						:						int 							= image_processor.post_process_semantic_segmentation(outputs=_lowercase  ,      target_sizes=[(500, 300)]    )
									a_						:						Any 							= torch.Size((500, 300)    )
									self.assertEqual(segmentation[0].shape  ,      _lowercase    )

									a_						:						Tuple 							= image_processor.post_process_semantic_segmentation(outputs=_lowercase    )
									a_						:						Optional[Any] 							= torch.Size((128, 128)    )
									self.assertEqual(segmentation[0].shape  ,      _lowercase    )





import argparse

import torch
from torch import nn

from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration


def 					_UpperCAmelCase				(						a__):

			'''simple docstring'''



			a_						:						Optional[Any] 							= [
			    """encoder.version""",
			    """decoder.version""",
			    """model.encoder.version""",
			    """model.decoder.version""",
			    """decoder.output_projection.weight""",
			    """_float_tensor""",
			    """encoder.embed_positions._float_tensor""",
			    """decoder.embed_positions._float_tensor""",
			]
			for k in ignore_keys:
						state_dict.pop(a__  ,	a__)


def 					_UpperCAmelCase				(						a__):

			'''simple docstring'''



			a_							,					a_						:						List[Any] 							= emb.weight.shape
			a_						:						Dict 							= nn.Linear(a__  ,	a__  ,	bias=a__)
			a_						:						str 							= emb.weight.data
			return lin_layer


def 					_UpperCAmelCase				(						a__):

			'''simple docstring'''



			a_						:						Optional[int] 							= torch.load(a__  ,	map_location="""cpu""")
			a_						:						int 							= mam_aaa["""args"""] or mam_aaa["""cfg"""]["""model"""]
			a_						:						Dict 							= mam_aaa["""model"""]
			remove_ignore_keys_(a__)
			a_						:						List[str] 							= state_dict["""encoder.embed_tokens.weight"""].shape[0]

			a_						:						Union[str, Any] 							= MaMaaaConfig(
			    vocab_size=a__  ,	max_position_embeddings=1_0_2_4  ,	encoder_layers=args.encoder_layers  ,	decoder_layers=args.decoder_layers  ,	encoder_attention_heads=args.encoder_attention_heads  ,	decoder_attention_heads=args.decoder_attention_heads  ,	encoder_ffn_dim=args.encoder_ffn_embed_dim  ,	decoder_ffn_dim=args.decoder_ffn_embed_dim  ,	d_model=args.encoder_embed_dim  ,	encoder_layerdrop=args.encoder_layerdrop  ,	decoder_layerdrop=args.decoder_layerdrop  ,	dropout=args.dropout  ,	attention_dropout=args.attention_dropout  ,	activation_dropout=args.activation_dropout  ,	activation_function="""relu"""  ,	)

			a_						:						Union[str, Any] 							= state_dict["""decoder.embed_tokens.weight"""]
			a_						:						List[Any] 							= MaMaaaForConditionalGeneration(a__)
			model.model.load_state_dict(a__  ,	strict=a__)
			a_						:						Any 							= make_linear_from_emb(model.model.shared)

			return model


if __name__ == "__main__":
	__snake_case				:      Union[str, Any]   		=			argparse.ArgumentParser()
	# Required parameters
	parser.add_argument("""fairseq_path""", type=str, help="""path to a model.pt on local filesystem.""")
	parser.add_argument("""pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""")
	__snake_case				:      int   		=			parser.parse_args()
	__snake_case				:      Any   		=			convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß)
	model.save_pretrained(args.pytorch_dump_folder_path)


