






import inspect
import unittest

from transformers import ResNetConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
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 torch import nn

		from transformers import ResNetBackbone, ResNetForImageClassification, ResNetModel
		from transformers.models.resnet.modeling_resnet import RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor





class __lowerCamelCase					:





	"""simple docstring"""






	def __init__(				self			,       UpperCAmelCase			,       UpperCAmelCase=3			,       UpperCAmelCase=32			,       UpperCAmelCase=3			,       UpperCAmelCase=10			,       UpperCAmelCase=[10, 20, 30, 40]			,       UpperCAmelCase=[1, 1, 2, 1]			,       UpperCAmelCase=True			,       UpperCAmelCase=True			,       UpperCAmelCase="relu"			,       UpperCAmelCase=3			,       UpperCAmelCase=None			,       UpperCAmelCase=["stage2", "stage3", "stage4"]			,       UpperCAmelCase=[2, 3, 4]			,       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     parent
							_UpperCAmelCase							   =     batch_size
							_UpperCAmelCase							   =     image_size
							_UpperCAmelCase							   =     num_channels
							_UpperCAmelCase							   =     embeddings_size
							_UpperCAmelCase							   =     hidden_sizes
							_UpperCAmelCase							   =     depths
							_UpperCAmelCase							   =     is_training
							_UpperCAmelCase							   =     use_labels
							_UpperCAmelCase							   =     hidden_act
							_UpperCAmelCase							   =     num_labels
							_UpperCAmelCase							   =     scope
							_UpperCAmelCase							   =     len(UpperCAmelCase       )
							_UpperCAmelCase							   =     out_features
							_UpperCAmelCase							   =     out_indices

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_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.num_labels       )

							_UpperCAmelCase							   =     self.get_config()

							return config, pixel_values, labels

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							return ResNetConfig(
							    num_channels=self.num_channels			,       embeddings_size=self.embeddings_size			,       hidden_sizes=self.hidden_sizes			,       depths=self.depths			,       hidden_act=self.hidden_act			,       num_labels=self.num_labels			,       out_features=self.out_features			,       out_indices=self.out_indices			,       )

	def 			UpperCamelCase       (				self			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     ResNetModel(config=UpperCAmelCase       )
							model.to(UpperCAmelCase       )
							model.eval()
							_UpperCAmelCase							   =     model(UpperCAmelCase       )
							# expected last hidden states: B, C, H // 32, W // 32
							self.parent.assertEqual(
							    result.last_hidden_state.shape			,       (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)			,       )

	def 			UpperCamelCase       (				self			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     self.num_labels
							_UpperCAmelCase							   =     ResNetForImageClassification(UpperCAmelCase       )
							model.to(UpperCAmelCase       )
							model.eval()
							_UpperCAmelCase							   =     model(UpperCAmelCase			,       labels=UpperCAmelCase       )
							self.parent.assertEqual(result.logits.shape			,       (self.batch_size, self.num_labels)       )

	def 			UpperCamelCase       (				self			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     ResNetBackbone(config=UpperCAmelCase       )
							model.to(UpperCAmelCase       )
							model.eval()
							_UpperCAmelCase							   =     model(UpperCAmelCase       )

							# verify feature maps
							self.parent.assertEqual(len(result.feature_maps       )			,       len(config.out_features       )       )
							self.parent.assertListEqual(list(result.feature_maps[0].shape       )			,       [self.batch_size, self.hidden_sizes[1], 4, 4]       )

							# verify channels
							self.parent.assertEqual(len(model.channels       )			,       len(config.out_features       )       )
							self.parent.assertListEqual(model.channels			,       config.hidden_sizes[1:]       )

							# verify backbone works with out_features=None
							_UpperCAmelCase							   =     None
							_UpperCAmelCase							   =     ResNetBackbone(config=UpperCAmelCase       )
							model.to(UpperCAmelCase       )
							model.eval()
							_UpperCAmelCase							   =     model(UpperCAmelCase       )

							# verify feature maps
							self.parent.assertEqual(len(result.feature_maps       )			,       1       )
							self.parent.assertListEqual(list(result.feature_maps[0].shape       )			,       [self.batch_size, self.hidden_sizes[-1], 1, 1]       )

							# verify channels
							self.parent.assertEqual(len(model.channels       )			,       1       )
							self.parent.assertListEqual(model.channels			,       [config.hidden_sizes[-1]]       )

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_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					( snake_case__	,			snake_case__	,			unittest.TestCase):





	"""simple docstring"""






	UpperCamelCase__								= (
	    (
	        ResNetModel,
	        ResNetForImageClassification,
	        ResNetBackbone,
	    )
	    if is_torch_available()
	    else ()
	)
	UpperCamelCase__								= (
	    {"feature-extraction": ResNetModel, "image-classification": ResNetForImageClassification}
	    if is_torch_available()
	    else {}
	)

	UpperCamelCase__								= True
	UpperCamelCase__								= False
	UpperCamelCase__								= False
	UpperCamelCase__								= False
	UpperCamelCase__								= False

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     ResNetModelTester(self       )
							_UpperCAmelCase							   =     ConfigTester(self			,       config_class=UpperCAmelCase			,       has_text_modality=UpperCAmelCase       )

	def 			UpperCamelCase       (				self       ):
							"""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 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							return

	@unittest.skip(reason='ResNet does not use inputs_embeds'       )
	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							pass

	@unittest.skip(reason='ResNet does not support input and output embeddings'       )
	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							pass

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_UpperCAmelCase   ,					_UpperCAmelCase							   =     self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													_UpperCAmelCase							   =     model_class(UpperCAmelCase       )
													_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]			,       UpperCAmelCase       )

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*UpperCAmelCase       )

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_backbone(*UpperCAmelCase       )

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_UpperCAmelCase   ,					_UpperCAmelCase							   =     self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													_UpperCAmelCase							   =     model_class(config=UpperCAmelCase       )
													for name, module in model.named_modules():
																			if isinstance(UpperCAmelCase			,       (nn.BatchNormad, nn.GroupNorm)       ):
																									self.assertTrue(
																									    torch.all(module.weight == 1       )			,       msg=F"""Parameter {name} of model {model_class} seems not properly initialized"""			,       )
																									self.assertTrue(
																									    torch.all(module.bias == 0       )			,       msg=F"""Parameter {name} of model {model_class} seems not properly initialized"""			,       )

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							def check_hidden_states_output(UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase       ):
													_UpperCAmelCase							   =     model_class(UpperCAmelCase       )
													model.to(UpperCAmelCase       )
													model.eval()

													with torch.no_grad():
																			_UpperCAmelCase							   =     model(**self._prepare_for_class(UpperCAmelCase			,       UpperCAmelCase       )       )

													_UpperCAmelCase							   =     outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

													_UpperCAmelCase							   =     self.model_tester.num_stages
													self.assertEqual(len(UpperCAmelCase       )			,       expected_num_stages + 1       )

													# ResNet's feature maps are of shape (batch_size, num_channels, height, width)
													self.assertListEqual(
													    list(hidden_states[0].shape[-2:]       )			,       [self.model_tester.image_size // 4, self.model_tester.image_size // 4]			,       )

							_UpperCAmelCase   ,					_UpperCAmelCase							   =     self.model_tester.prepare_config_and_inputs_for_common()
							_UpperCAmelCase							   =     ['basic', 'bottleneck']
							for model_class in self.all_model_classes:
													for layer_type in layers_type:
																			_UpperCAmelCase							   =     layer_type
																			_UpperCAmelCase							   =     True
																			check_hidden_states_output(UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase       )

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

																			check_hidden_states_output(UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase       )

	@unittest.skip(reason='ResNet does not use feedforward chunking'       )
	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							pass

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase       )

	@slow
	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							for model_name in RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_UpperCAmelCase							   =     ResNetModel.from_pretrained(UpperCAmelCase       )
													self.assertIsNotNone(UpperCAmelCase       )



def __A       ( )->    Tuple:





						"""simple docstring"""



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





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





	"""simple docstring"""






	@cached_property
	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							return (
							    AutoImageProcessor.from_pretrained(RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]       )
							    if is_vision_available()
							    else None
							)

	@slow
	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     ResNetForImageClassification.from_pretrained(RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]       ).to(UpperCAmelCase       )

							_UpperCAmelCase							   =     self.default_image_processor
							_UpperCAmelCase							   =     prepare_img()
							_UpperCAmelCase							   =     image_processor(images=UpperCAmelCase			,       return_tensors='pt'       ).to(UpperCAmelCase       )

							# forward pass
							with torch.no_grad():
													_UpperCAmelCase							   =     model(**UpperCAmelCase       )

							# verify the logits
							_UpperCAmelCase							   =     torch.Size((1, 1000)       )
							self.assertEqual(outputs.logits.shape			,       UpperCAmelCase       )

							_UpperCAmelCase							   =     torch.tensor([-11.10_69, -9.78_77, -8.37_77]       ).to(UpperCAmelCase       )

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





@require_torch
class __lowerCamelCase					( snake_case__	,			unittest.TestCase):





	"""simple docstring"""






	UpperCamelCase__								= (ResNetBackbone,) if is_torch_available() else ()
	UpperCamelCase__								= False
	UpperCamelCase__								= ResNetConfig

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     ResNetModelTester(self       )








import argparse
import pathlib

import fairseq
import torch
from fairseq.models.roberta import RobertaModel as FairseqRobertaModel
from fairseq.modules import TransformerSentenceEncoderLayer
from packaging import version

from transformers import RobertaConfig, RobertaForMaskedLM, RobertaForSequenceClassification
from transformers.models.bert.modeling_bert import (
    BertIntermediate,
    BertLayer,
    BertOutput,
    BertSelfAttention,
    BertSelfOutput,
)
from transformers.utils import logging


if version.parse(fairseq.__version__) < version.parse('''0.9.0'''):
		raise Exception('''requires fairseq >= 0.9.0''')


logging.set_verbosity_info()
_a       =    logging.get_logger(__name__)

_a       =    '''Hello world! cécé herlolip'''


def __A       ( __lowerCAmelCase					,						__lowerCAmelCase					,						__lowerCAmelCase      )->    List[Any]:





						"""simple docstring"""



						_UpperCAmelCase							   =     FairseqRobertaModel.from_pretrained(__lowerCAmelCase      )
						roberta.eval()  # disable dropout
						_UpperCAmelCase							   =     roberta.model.encoder.sentence_encoder
						_UpperCAmelCase							   =     RobertaConfig(
						    vocab_size=roberta_sent_encoder.embed_tokens.num_embeddings					,						hidden_size=roberta.args.encoder_embed_dim					,						num_hidden_layers=roberta.args.encoder_layers					,						num_attention_heads=roberta.args.encoder_attention_heads					,						intermediate_size=roberta.args.encoder_ffn_embed_dim					,						max_position_embeddings=514					,						type_vocab_size=1					,						layer_norm_eps=1E-5					,						)
						if classification_head:
												_UpperCAmelCase							   =     roberta.model.classification_heads['mnli'].out_proj.weight.shape[0]
						print('Our BERT config:'					,						__lowerCAmelCase      )

						_UpperCAmelCase							   =     RobertaForSequenceClassification(__lowerCAmelCase      ) if classification_head else RobertaForMaskedLM(__lowerCAmelCase      )
						model.eval()

						# Now let's copy all the weights.
						# Embeddings
						_UpperCAmelCase							   =     roberta_sent_encoder.embed_tokens.weight
						_UpperCAmelCase							   =     roberta_sent_encoder.embed_positions.weight
						_UpperCAmelCase							   =     torch.zeros_like(
						    model.roberta.embeddings.token_type_embeddings.weight      )  # just zero them out b/c RoBERTa doesn't use them.
						_UpperCAmelCase							   =     roberta_sent_encoder.emb_layer_norm.weight
						_UpperCAmelCase							   =     roberta_sent_encoder.emb_layer_norm.bias

						for i in range(config.num_hidden_layers      ):
												# Encoder: start of layer
												_UpperCAmelCase							   =     model.roberta.encoder.layer[i]
												_UpperCAmelCase							   =     roberta_sent_encoder.layers[i]

												# self attention
												_UpperCAmelCase							   =     layer.attention.self
												assert (
												    roberta_layer.self_attn.k_proj.weight.data.shape
												    == roberta_layer.self_attn.q_proj.weight.data.shape
												    == roberta_layer.self_attn.v_proj.weight.data.shape
												    == torch.Size((config.hidden_size, config.hidden_size)      )
												)

												_UpperCAmelCase							   =     roberta_layer.self_attn.q_proj.weight
												_UpperCAmelCase							   =     roberta_layer.self_attn.q_proj.bias
												_UpperCAmelCase							   =     roberta_layer.self_attn.k_proj.weight
												_UpperCAmelCase							   =     roberta_layer.self_attn.k_proj.bias
												_UpperCAmelCase							   =     roberta_layer.self_attn.v_proj.weight
												_UpperCAmelCase							   =     roberta_layer.self_attn.v_proj.bias

												# self-attention output
												_UpperCAmelCase							   =     layer.attention.output
												assert self_output.dense.weight.shape == roberta_layer.self_attn.out_proj.weight.shape
												_UpperCAmelCase							   =     roberta_layer.self_attn.out_proj.weight
												_UpperCAmelCase							   =     roberta_layer.self_attn.out_proj.bias
												_UpperCAmelCase							   =     roberta_layer.self_attn_layer_norm.weight
												_UpperCAmelCase							   =     roberta_layer.self_attn_layer_norm.bias

												# intermediate
												_UpperCAmelCase							   =     layer.intermediate
												assert intermediate.dense.weight.shape == roberta_layer.fca.weight.shape
												_UpperCAmelCase							   =     roberta_layer.fca.weight
												_UpperCAmelCase							   =     roberta_layer.fca.bias

												# output
												_UpperCAmelCase							   =     layer.output
												assert bert_output.dense.weight.shape == roberta_layer.fca.weight.shape
												_UpperCAmelCase							   =     roberta_layer.fca.weight
												_UpperCAmelCase							   =     roberta_layer.fca.bias
												_UpperCAmelCase							   =     roberta_layer.final_layer_norm.weight
												_UpperCAmelCase							   =     roberta_layer.final_layer_norm.bias
												# end of layer

						if classification_head:
												_UpperCAmelCase							   =     roberta.model.classification_heads['mnli'].dense.weight
												_UpperCAmelCase							   =     roberta.model.classification_heads['mnli'].dense.bias
												_UpperCAmelCase							   =     roberta.model.classification_heads['mnli'].out_proj.weight
												_UpperCAmelCase							   =     roberta.model.classification_heads['mnli'].out_proj.bias
						else:
												# LM Head
												_UpperCAmelCase							   =     roberta.model.encoder.lm_head.dense.weight
												_UpperCAmelCase							   =     roberta.model.encoder.lm_head.dense.bias
												_UpperCAmelCase							   =     roberta.model.encoder.lm_head.layer_norm.weight
												_UpperCAmelCase							   =     roberta.model.encoder.lm_head.layer_norm.bias
												_UpperCAmelCase							   =     roberta.model.encoder.lm_head.weight
												_UpperCAmelCase							   =     roberta.model.encoder.lm_head.bias

						# Let's check that we get the same results.
						_UpperCAmelCase							   =     roberta.encode(__lowerCAmelCase      ).unsqueeze(0      )  # batch of size 1

						_UpperCAmelCase							   =     model(__lowerCAmelCase      )[0]
						if classification_head:
												_UpperCAmelCase							   =     roberta.model.classification_heads['mnli'](roberta.extract_features(__lowerCAmelCase      )      )
						else:
												_UpperCAmelCase							   =     roberta.model(__lowerCAmelCase      )[0]
						print(our_output.shape					,						their_output.shape      )
						_UpperCAmelCase							   =     torch.max(torch.abs(our_output - their_output      )      ).item()
						print(F"""max_absolute_diff = {max_absolute_diff}"""      )  # ~ 1e-7
						_UpperCAmelCase							   =     torch.allclose(__lowerCAmelCase					,						__lowerCAmelCase					,						atol=1E-3      )
						print('Do both models output the same tensors?'					,						'🔥' if success else '💩'      )
						if not success:
												raise Exception('Something went wRoNg'      )

						pathlib.Path(__lowerCAmelCase      ).mkdir(parents=__lowerCAmelCase					,						exist_ok=__lowerCAmelCase      )
						print(F"""Saving model to {pytorch_dump_folder_path}"""      )
						model.save_pretrained(__lowerCAmelCase      )


if __name__ == "__main__":
		_a       =    argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    '''--roberta_checkpoint_path''', default=None, type=str, required=True, help='''Path the official PyTorch dump.'''
		)
		parser.add_argument(
		    '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.'''
		)
		parser.add_argument(
		    '''--classification_head''', action='''store_true''', help='''Whether to convert a final classification head.'''
		)
		_a       =    parser.parse_args()
		convert_roberta_checkpoint_to_pytorch(
		    args.roberta_checkpoint_path, args.pytorch_dump_folder_path, args.classification_head
		)
