



'''simple docstring'''



import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, 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 CvtForImageClassification, CvtModel
		from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor





class _UpperCamelCase						(   _SCREAMING_SNAKE_CASE						):





				'''simple docstring'''





				def  __lowerCamelCase   (							self     :     Optional[Any]):







											'''simple docstring'''

											__lowercase						=self.config_class(**self.inputs_dict)
											self.parent.assertTrue(hasattr(A_     ,				'embed_dim'))
											self.parent.assertTrue(hasattr(A_     ,				'num_heads'))





class _UpperCamelCase						:





				'''simple docstring'''





				def __init__(							self     :     List[str]     ,				_lowerCAmelCase     :     List[str]     ,				_lowerCAmelCase     :     Tuple=1_3     ,				_lowerCAmelCase     :     Optional[Any]=6_4     ,				_lowerCAmelCase     :     Optional[int]=3     ,				_lowerCAmelCase     :     Tuple=[1_6, 4_8, 9_6]     ,				_lowerCAmelCase     :     Dict=[1, 3, 6]     ,				_lowerCAmelCase     :     Tuple=[1, 2, 1_0]     ,				_lowerCAmelCase     :     Optional[Any]=[7, 3, 3]     ,				_lowerCAmelCase     :     Optional[int]=[4, 2, 2]     ,				_lowerCAmelCase     :     Optional[int]=[2, 1, 1]     ,				_lowerCAmelCase     :     Dict=[2, 2, 2]     ,				_lowerCAmelCase     :     Union[str, Any]=[False, False, True]     ,				_lowerCAmelCase     :     Dict=[0.0, 0.0, 0.0]     ,				_lowerCAmelCase     :     Optional[Any]=0.02     ,				_lowerCAmelCase     :     Optional[Any]=1e-12     ,				_lowerCAmelCase     :     Optional[Any]=True     ,				_lowerCAmelCase     :     List[str]=True     ,				_lowerCAmelCase     :     List[Any]=2     ,				):







											'''simple docstring'''

											__lowercase						=parent
											__lowercase						=batch_size
											__lowercase						=image_size
											__lowercase						=patch_sizes
											__lowercase						=patch_stride
											__lowercase						=patch_padding
											__lowercase						=is_training
											__lowercase						=use_labels
											__lowercase						=num_labels
											__lowercase						=num_channels
											__lowercase						=embed_dim
											__lowercase						=num_heads
											__lowercase						=stride_kv
											__lowercase						=depth
											__lowercase						=cls_token
											__lowercase						=attention_drop_rate
											__lowercase						=initializer_range
											__lowercase						=layer_norm_eps

				def  __lowerCamelCase   (							self     :     Tuple):







											'''simple docstring'''

											__lowercase						=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

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

				def  __lowerCamelCase   (							self     :     Optional[Any]):







											'''simple docstring'''

											return CvtConfig(
											    image_size=self.image_size     ,				num_labels=self.num_labels     ,				num_channels=self.num_channels     ,				embed_dim=self.embed_dim     ,				num_heads=self.num_heads     ,				patch_sizes=self.patch_sizes     ,				patch_padding=self.patch_padding     ,				patch_stride=self.patch_stride     ,				stride_kv=self.stride_kv     ,				depth=self.depth     ,				cls_token=self.cls_token     ,				attention_drop_rate=self.attention_drop_rate     ,				initializer_range=self.initializer_range     ,				)

				def  __lowerCamelCase   (							self     :     str     ,				_lowerCAmelCase     :     Union[str, Any]     ,				_lowerCAmelCase     :     int     ,				_lowerCAmelCase     :     str):







											'''simple docstring'''

											__lowercase						=CvtModel(config=A_)
											model.to(A_)
											model.eval()
											__lowercase						=model(A_)
											__lowercase						=(self.image_size, self.image_size)
											__lowercase		, __lowercase						=image_size[0], image_size[1]
											for i in range(len(self.depth)):
																		__lowercase						=floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1)
																		__lowercase						=floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1)
											self.parent.assertEqual(result.last_hidden_state.shape     ,				(self.batch_size, self.embed_dim[-1], height, width))

				def  __lowerCamelCase   (							self     :     Dict     ,				_lowerCAmelCase     :     Tuple     ,				_lowerCAmelCase     :     Any     ,				_lowerCAmelCase     :     Optional[Any]):







											'''simple docstring'''

											__lowercase						=self.num_labels
											__lowercase						=CvtForImageClassification(A_)
											model.to(A_)
											model.eval()
											__lowercase						=model(A_     ,				labels=A_)
											self.parent.assertEqual(result.logits.shape     ,				(self.batch_size, self.num_labels))

				def  __lowerCamelCase   (							self     :     List[Any]):







											'''simple docstring'''

											__lowercase						=self.prepare_config_and_inputs()
											__lowercase		, __lowercase		, __lowercase						=config_and_inputs
											__lowercase						={'pixel_values': pixel_values}
											return config, inputs_dict





@require_torch
class _UpperCamelCase						(   _SCREAMING_SNAKE_CASE					,				_SCREAMING_SNAKE_CASE					,				unittest.TestCase						):





				'''simple docstring'''





				lowerCAmelCase__    			=			(CvtModel, CvtForImageClassification) if is_torch_available() else ()
				lowerCAmelCase__    			=			(
				    {"feature-extraction": CvtModel, "image-classification": CvtForImageClassification}
				    if is_torch_available()
				    else {}
				)

				lowerCAmelCase__    			=			False
				lowerCAmelCase__    			=			False
				lowerCAmelCase__    			=			False
				lowerCAmelCase__    			=			False
				lowerCAmelCase__    			=			False

				def  __lowerCamelCase   (							self     :     Optional[int]):







											'''simple docstring'''

											__lowercase						=CvtModelTester(self)
											__lowercase						=ConfigTester(self     ,				config_class=A_     ,				has_text_modality=A_     ,				hidden_size=3_7)

				def  __lowerCamelCase   (							self     :     Union[str, Any]):







											'''simple docstring'''

											self.create_and_test_config_common_properties()
											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  __lowerCamelCase   (							self     :     Any):







											'''simple docstring'''

											return

				@unittest.skip(reason='Cvt does not output attentions')
				def  __lowerCamelCase   (							self     :     List[Any]):







											'''simple docstring'''

											pass

				@unittest.skip(reason='Cvt does not use inputs_embeds')
				def  __lowerCamelCase   (							self     :     Any):







											'''simple docstring'''

											pass

				@unittest.skip(reason='Cvt does not support input and output embeddings')
				def  __lowerCamelCase   (							self     :     Optional[int]):







											'''simple docstring'''

											pass

				def  __lowerCamelCase   (							self     :     int):







											'''simple docstring'''

											__lowercase		, __lowercase						=self.model_tester.prepare_config_and_inputs_for_common()

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

																		__lowercase						=['pixel_values']
																		self.assertListEqual(arg_names[:1]     ,				A_)

				def  __lowerCamelCase   (							self     :     Any):







											'''simple docstring'''

											__lowercase						=self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*A_)

				def  __lowerCamelCase   (							self     :     Dict):







											'''simple docstring'''

											def check_hidden_states_output(_lowerCAmelCase     :     Tuple     ,				_lowerCAmelCase     :     Optional[int]     ,				_lowerCAmelCase     :     Tuple):
																		__lowercase						=model_class(A_)
																		model.to(A_)
																		model.eval()

																		with torch.no_grad():
																									__lowercase						=model(**self._prepare_for_class(A_     ,				A_))

																		__lowercase						=outputs.hidden_states

																		__lowercase						=len(self.model_tester.depth)
																		self.assertEqual(len(A_)     ,				A_)

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

											__lowercase		, __lowercase						=self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		__lowercase						=True
																		check_hidden_states_output(A_     ,				A_     ,				A_)

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

																		check_hidden_states_output(A_     ,				A_     ,				A_)

				def  __lowerCamelCase   (							self     :     int):







											'''simple docstring'''

											__lowercase						=self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*A_)

				@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.')
				def  __lowerCamelCase   (							self     :     Optional[int]):







											'''simple docstring'''

											pass

				@slow
				def  __lowerCamelCase   (							self     :     Optional[int]):







											'''simple docstring'''

											for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		__lowercase						=CvtModel.from_pretrained(A_)
																		self.assertIsNotNone(A_)



def      _A				(      ):



							"""simple docstring"""



							__lowercase						=Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'    )
							return image





@require_torch
@require_vision
class _UpperCamelCase						(   unittest.TestCase						):





				'''simple docstring'''





				@cached_property
				def  __lowerCamelCase   (							self     :     Tuple):







											'''simple docstring'''

											return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0])

				@slow
				def  __lowerCamelCase   (							self     :     Dict):







											'''simple docstring'''

											__lowercase						=CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]).to(A_)

											__lowercase						=self.default_image_processor
											__lowercase						=prepare_img()
											__lowercase						=image_processor(images=A_     ,				return_tensors='pt').to(A_)

											# forward pass
											with torch.no_grad():
																		__lowercase						=model(**A_)

											# verify the logits
											__lowercase						=torch.Size((1, 1_0_0_0))
											self.assertEqual(outputs.logits.shape     ,				A_)

											__lowercase						=torch.tensor([0.9285, 0.9015, -0.3150]).to(A_)

											self.assertTrue(torch.allclose(outputs.logits[0, :3]     ,				A_     ,				atol=1e-4))





import argparse

import torch
from torch import nn

from transformers import SpeechaTextConfig, SpeechaTextForConditionalGeneration


def       A       (					lowercase )   ->  Optional[Any]:
		'''simple docstring'''

		UpperCamelCase		=     [
		    '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(lowercase    ,       lowercase )


def       A       (					lowercase )   ->  List[Any]:
		'''simple docstring'''

		UpperCamelCase		=     list(s_dict.keys() )
		for key in keys:
				if "transformer_layers" in key:
						UpperCamelCase		=     s_dict.pop(lowercase )
				elif "subsample" in key:
						UpperCamelCase		=     s_dict.pop(lowercase )


def       A       (					lowercase )   ->  Tuple:
		'''simple docstring'''

		UpperCamelCase	,  UpperCamelCase		=     emb.weight.shape
		UpperCamelCase		=     nn.Linear(lowercase    ,       lowercase    ,       bias=lowercase )
		UpperCamelCase		=     emb.weight.data
		return lin_layer


def       A       (					lowercase    ,       lowercase )   ->  Dict:
		'''simple docstring'''

		UpperCamelCase		=     torch.load(lowercase    ,       map_location='cpu' )
		UpperCamelCase		=     mam_aaa['args']
		UpperCamelCase		=     mam_aaa['model']
		UpperCamelCase		=     state_dict['decoder.output_projection.weight']

		remove_ignore_keys_(lowercase )
		rename_keys(lowercase )

		UpperCamelCase		=     state_dict['decoder.embed_tokens.weight'].shape[0]

		UpperCamelCase		=     args.share_decoder_input_output_embed

		UpperCamelCase		=     [int(lowercase ) for i in args.conv_kernel_sizes.split(',' )]
		UpperCamelCase		=     SpeechaTextConfig(
		    vocab_size=lowercase    ,       max_source_positions=args.max_source_positions    ,       max_target_positions=args.max_target_positions    ,       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    ,       dropout=args.dropout    ,       attention_dropout=args.attention_dropout    ,       activation_dropout=args.activation_dropout    ,       activation_function='relu'    ,       num_conv_layers=len(lowercase )    ,       conv_channels=args.conv_channels    ,       conv_kernel_sizes=lowercase    ,       input_feat_per_channel=args.input_feat_per_channel    ,       input_channels=args.input_channels    ,       tie_word_embeddings=lowercase    ,       num_beams=5    ,       max_length=200    ,       use_cache=lowercase    ,       decoder_start_token_id=2    ,       early_stopping=lowercase    ,       )

		UpperCamelCase		=     SpeechaTextForConditionalGeneration(lowercase )
		UpperCamelCase	,  UpperCamelCase		=     model.model.load_state_dict(lowercase    ,       strict=lowercase )
		if len(lowercase ) > 0 and not set(lowercase ) <= {
		    "encoder.embed_positions.weights",
		    "decoder.embed_positions.weights",
		}:
				raise ValueError(
				    'Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights`  are allowed to be missing,'
				    f''' but all the following weights are missing {missing}''' )

		if tie_embeds:
				UpperCamelCase		=     make_linear_from_emb(model.model.decoder.embed_tokens )
		else:
				UpperCamelCase		=     lm_head_weights

		model.save_pretrained(lowercase )


if __name__ == "__main__":
					_UpperCAmelCase					: Optional[Any]      =       argparse.ArgumentParser()
					# Required parameters
					parser.add_argument("--fairseq_path", type=str, help="Path to the fairseq model (.pt) file.")
					parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
					_UpperCAmelCase					: Dict      =       parser.parse_args()
					convert_fairseq_sat_checkpoint_to_tfms(args.fairseq_path, args.pytorch_dump_folder_path)
