




import inspect
import unittest

from transformers import RegNetConfig
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 torch import nn

  from transformers import RegNetForImageClassification, RegNetModel
  from transformers.models.regnet.modeling_regnet import REGNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor







class 						lowercase							:

     def __init__(  self		,     _a		,     _a=3		,     _a=32		,     _a=3		,     _a=10		,     _a=[10, 20, 30, 40]		,     _a=[1, 1, 2, 1]		,     _a=True		,     _a=True		,     _a="relu"		,     _a=3		,     _a=None		,     )						->							Union[str, Any]:
           _A							:     Any			=						parent
           _A							:     Dict			=						batch_size
           _A							:     Dict			=						image_size
           _A							:     List[str]			=						num_channels
           _A							:     Dict			=						embeddings_size
           _A							:     int			=						hidden_sizes
           _A							:     List[str]			=						depths
           _A							:     Optional[int]			=						is_training
           _A							:     str			=						use_labels
           _A							:     List[Any]			=						hidden_act
           _A							:     Any			=						num_labels
           _A							:     Optional[Any]			=						scope
           _A							:     List[str]			=						len(lowercase_				)

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

           _A							:     Tuple			=						None
           if self.use_labels:
                 _A							:     int			=						ids_tensor([self.batch_size]		,     self.num_labels				)

           _A							:     Tuple			=						self.get_config()

           return config, pixel_values, labels

     def 					a__	(  self				)						->							Union[str, Any]:
           return RegNetConfig(
               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		,     )

     def 					a__	(  self		,     _a		,     _a		,     _a				)						->							Dict:
           _A							:     Optional[Any]			=						RegNetModel(config=lowercase_				)
           model.to(lowercase_				)
           model.eval()
           _A							:     Dict			=						model(lowercase_				)
           # 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 					a__	(  self		,     _a		,     _a		,     _a				)						->							List[str]:
           _A							:     Union[str, Any]			=						self.num_labels
           _A							:     Dict			=						RegNetForImageClassification(lowercase_				)
           model.to(lowercase_				)
           model.eval()
           _A							:     Union[str, Any]			=						model(lowercase_		,     labels=lowercase_				)
           self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels)				)



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







@require_torch
class 						lowercase							(   _lowerCamelCase,_lowerCamelCase,unittest.TestCase		):
     _a							 =						(RegNetModel, RegNetForImageClassification) if is_torch_available() else ()
     _a							 =						(
         {"feature-extraction": RegNetModel, "image-classification": RegNetForImageClassification}
         if is_torch_available()
         else {}
     )

     _a							 =						False
     _a							 =						False
     _a							 =						False
     _a							 =						False

     def 					a__	(  self				)						->							Optional[Any]:
           _A							:     str			=						RegNetModelTester(self				)
           _A							:     Optional[int]			=						ConfigTester(self		,     config_class=lowercase_		,     has_text_modality=lowercase_				)

     def 					a__	(  self				)						->							List[Any]:
           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 					a__	(  self				)						->							Optional[Any]:
           return

     @unittest.skip(reason="""RegNet does not use inputs_embeds"""				)
     def 					a__	(  self				)						->							int:
           pass

     @unittest.skip(reason="""RegNet does not support input and output embeddings"""				)
     def 					a__	(  self				)						->							Union[str, Any]:
           pass

     def 					a__	(  self				)						->							Any:
           _A			,   _A							:     Optional[int]			=						self.model_tester.prepare_config_and_inputs_for_common()

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

                 _A							:     Optional[Any]			=						["""pixel_values"""]
                 self.assertListEqual(arg_names[:1]		,     lowercase_				)

     def 					a__	(  self				)						->							Optional[Any]:
           _A							:     Any			=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowercase_				)

     def 					a__	(  self				)						->							int:
           _A			,   _A							:     List[str]			=						self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                 _A							:     Any			=						model_class(config=lowercase_				)
                 for name, module in model.named_modules():
                       if isinstance(lowercase_		,     (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 					a__	(  self				)						->							Optional[Any]:
           def check_hidden_states_output(_a		,     _a		,     _a				):
                 _A							:     Tuple			=						model_class(lowercase_				)
                 model.to(lowercase_				)
                 model.eval()

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

                 _A							:     Union[str, Any]			=						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                 _A							:     Optional[Any]			=						self.model_tester.num_stages
                 self.assertEqual(len(lowercase_				)		,     expected_num_stages + 1				)

                 # RegNet'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 // 2, self.model_tester.image_size // 2]		,     )

           _A			,   _A							:     Optional[Any]			=						self.model_tester.prepare_config_and_inputs_for_common()
           _A							:     int			=						["""basic""", """bottleneck"""]
           for model_class in self.all_model_classes:
                 for layer_type in layers_type:
                       _A							:     Dict			=						layer_type
                       _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							:     int			=						True

                       check_hidden_states_output(lowercase_		,     lowercase_		,     lowercase_				)

     def 					a__	(  self				)						->							Any:
           _A							:     List[Any]			=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*lowercase_				)



     @slow
     def 					a__	(  self				)						->							int:
           for model_name in REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _A							:     Any			=						RegNetModel.from_pretrained(lowercase_				)
                 self.assertIsNotNone(lowercase_				)







def 				lowerCAmelCase_	(     ):
      _A							:     Optional[Any]			=						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
      return image







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

     @cached_property
     def 					a__	(  self				)						->							str:
           return (
               AutoImageProcessor.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]				)
               if is_vision_available()
               else None
           )



     @slow
     def 					a__	(  self				)						->							str:
           _A							:     Dict			=						RegNetForImageClassification.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]				).to(lowercase_				)

           _A							:     int			=						self.default_image_processor
           _A							:     Tuple			=						prepare_img()
           _A							:     List[str]			=						image_processor(images=lowercase_		,     return_tensors="""pt"""				).to(lowercase_				)

           # forward pass
           with torch.no_grad():
                 _A							:     Optional[int]			=						model(**lowercase_				)

           # verify the logits
           _A							:     int			=						torch.Size((1, 1000)				)
           self.assertEqual(outputs.logits.shape		,     lowercase_				)

           _A							:     Dict			=						torch.tensor([-0.4180, -1.5051, -3.4836]				).to(lowercase_				)

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







'''simple docstring'''

import unittest

from transformers import MPNetConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    MPNetForMaskedLM,
							    MPNetForMultipleChoice,
							    MPNetForQuestionAnswering,
							    MPNetForSequenceClassification,
							    MPNetForTokenClassification,
							    MPNetModel,
							)






class 						a				:


				def __init__(		self    :					List[Any]       ,				lowercase_    :					Optional[Any]       ,				lowercase_    :					Any=13       ,				lowercase_    :					Optional[Any]=7       ,				lowercase_    :					Optional[Any]=True       ,				lowercase_    :					Dict=True       ,				lowercase_    :					Tuple=False       ,				lowercase_    :					Optional[Any]=True       ,				lowercase_    :					Any=99       ,				lowercase_    :					Union[str, Any]=64       ,				lowercase_    :					str=5       ,				lowercase_    :					int=4       ,				lowercase_    :					List[Any]=64       ,				lowercase_    :					Dict="gelu"       ,				lowercase_    :					Optional[int]=0.1       ,				lowercase_    :					Optional[Any]=0.1       ,				lowercase_    :					Tuple=512       ,				lowercase_    :					List[Any]=16       ,				lowercase_    :					str=2       ,				lowercase_    :					List[str]=0.02       ,				lowercase_    :					Optional[Any]=3       ,				lowercase_    :					Optional[Any]=4       ,				lowercase_    :					List[Any]=None       ,				):
									snake_case_									=				parent
									snake_case_									=				batch_size
									snake_case_									=				seq_length
									snake_case_									=				is_training
									snake_case_									=				use_input_mask
									snake_case_									=				use_token_type_ids
									snake_case_									=				use_labels
									snake_case_									=				vocab_size
									snake_case_									=				hidden_size
									snake_case_									=				num_hidden_layers
									snake_case_									=				num_attention_heads
									snake_case_									=				intermediate_size
									snake_case_									=				hidden_act
									snake_case_									=				hidden_dropout_prob
									snake_case_									=				attention_probs_dropout_prob
									snake_case_									=				max_position_embeddings
									snake_case_									=				type_vocab_size
									snake_case_									=				type_sequence_label_size
									snake_case_									=				initializer_range
									snake_case_									=				num_labels
									snake_case_									=				num_choices
									snake_case_									=				scope


				def   A_ (		self    :					List[str]					):
									return MPNetConfig.from_pretrained('''microsoft/mpnet-base'''					)


				def   A_ (		self    :					str					):
									snake_case_									=				ids_tensor([self.batch_size, self.seq_length]       ,				self.vocab_size					)

									snake_case_									=				None
									if self.use_input_mask:
														snake_case_									=				random_attention_mask([self.batch_size, self.seq_length]					)

									snake_case_									=				None
									snake_case_									=				None
									snake_case_									=				None
									if self.use_labels:
														snake_case_									=				ids_tensor([self.batch_size]       ,				self.type_sequence_label_size					)
														snake_case_									=				ids_tensor([self.batch_size, self.seq_length]       ,				self.num_labels					)
														snake_case_									=				ids_tensor([self.batch_size]       ,				self.num_choices					)

									snake_case_									=				self.get_config()
									return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels


				def   A_ (		self    :					Tuple					):
									return MPNetConfig(
									    vocab_size=self.vocab_size       ,				hidden_size=self.hidden_size       ,				num_hidden_layers=self.num_hidden_layers       ,				num_attention_heads=self.num_attention_heads       ,				intermediate_size=self.intermediate_size       ,				hidden_act=self.hidden_act       ,				hidden_dropout_prob=self.hidden_dropout_prob       ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,				max_position_embeddings=self.max_position_embeddings       ,				initializer_range=self.initializer_range       ,				)


				def   A_ (		self    :					Any       ,				lowercase_    :					Dict       ,				lowercase_    :					str       ,				lowercase_    :					Optional[Any]       ,				lowercase_    :					Union[str, Any]       ,				lowercase_    :					Dict       ,				lowercase_    :					Optional[int]					):
									snake_case_									=				MPNetModel(config=lowercase_					)
									model.to(lowercase_					)
									model.eval()
									snake_case_									=				model(lowercase_       ,				lowercase_					)
									snake_case_									=				model(lowercase_					)
									self.parent.assertEqual(result.last_hidden_state.shape       ,				(self.batch_size, self.seq_length, self.hidden_size)					)
									self.parent.assertEqual(result.pooler_output.shape       ,				(self.batch_size, self.hidden_size)					)


				def   A_ (		self    :					str       ,				lowercase_    :					Optional[Any]       ,				lowercase_    :					Any       ,				lowercase_    :					str       ,				lowercase_    :					Tuple       ,				lowercase_    :					Tuple       ,				lowercase_    :					Optional[int]					):
									snake_case_									=				MPNetForQuestionAnswering(config=lowercase_					)
									model.to(lowercase_					)
									model.eval()
									snake_case_									=				model(
									    lowercase_       ,				attention_mask=lowercase_       ,				start_positions=lowercase_       ,				end_positions=lowercase_       ,				)
									self.parent.assertEqual(result.start_logits.shape       ,				(self.batch_size, self.seq_length)					)
									self.parent.assertEqual(result.end_logits.shape       ,				(self.batch_size, self.seq_length)					)


				def   A_ (		self    :					Tuple       ,				lowercase_    :					Tuple       ,				lowercase_    :					List[str]       ,				lowercase_    :					str       ,				lowercase_    :					Optional[int]       ,				lowercase_    :					Optional[Any]       ,				lowercase_    :					Any					):
									snake_case_									=				self.num_labels
									snake_case_									=				MPNetForSequenceClassification(lowercase_					)
									model.to(lowercase_					)
									model.eval()
									snake_case_									=				model(lowercase_       ,				attention_mask=lowercase_       ,				labels=lowercase_					)
									self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.num_labels)					)


				def   A_ (		self    :					Any       ,				lowercase_    :					Any       ,				lowercase_    :					str       ,				lowercase_    :					Tuple       ,				lowercase_    :					List[str]       ,				lowercase_    :					Union[str, Any]       ,				lowercase_    :					Dict					):
									snake_case_									=				self.num_choices
									snake_case_									=				MPNetForMultipleChoice(config=lowercase_					)
									model.to(lowercase_					)
									model.eval()
									snake_case_									=				input_ids.unsqueeze(1					).expand(-1       ,				self.num_choices       ,				-1					).contiguous()
									snake_case_									=				input_mask.unsqueeze(1					).expand(-1       ,				self.num_choices       ,				-1					).contiguous()
									snake_case_									=				model(
									    lowercase_       ,				attention_mask=lowercase_       ,				labels=lowercase_       ,				)
									self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.num_choices)					)


				def   A_ (		self    :					Union[str, Any]       ,				lowercase_    :					List[Any]       ,				lowercase_    :					Optional[Any]       ,				lowercase_    :					Optional[int]       ,				lowercase_    :					str       ,				lowercase_    :					int       ,				lowercase_    :					List[str]					):
									snake_case_									=				self.num_labels
									snake_case_									=				MPNetForTokenClassification(config=lowercase_					)
									model.to(lowercase_					)
									model.eval()
									snake_case_									=				model(lowercase_       ,				attention_mask=lowercase_       ,				labels=lowercase_					)
									self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.seq_length, self.num_labels)					)


				def   A_ (		self    :					Union[str, Any]					):
									snake_case_									=				self.prepare_config_and_inputs()
									((snake_case_)			,(snake_case_)			,(snake_case_)			,(snake_case_)			,(snake_case_)			,(snake_case_))									=				config_and_inputs
									snake_case_									=				{'''input_ids''': input_ids, '''attention_mask''': input_mask}
									return config, inputs_dict







@require_torch
class 						a				(   _lowerCamelCase   ,     _lowerCamelCase   ,     unittest.TestCase     ):
				snake_case_      =		(
				    (
				        MPNetForMaskedLM,
				        MPNetForMultipleChoice,
				        MPNetForQuestionAnswering,
				        MPNetForSequenceClassification,
				        MPNetForTokenClassification,
				        MPNetModel,
				    )
				    if is_torch_available()
				    else ()
				)
				snake_case_      =		(
				    {
				        "feature-extraction": MPNetModel,
				        "fill-mask": MPNetForMaskedLM,
				        "question-answering": MPNetForQuestionAnswering,
				        "text-classification": MPNetForSequenceClassification,
				        "token-classification": MPNetForTokenClassification,
				        "zero-shot": MPNetForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				snake_case_      =		False
				snake_case_      =		True


				def   A_ (		self    :					Tuple					):
									snake_case_									=				MPNetModelTester(self					)
									snake_case_									=				ConfigTester(self       ,				config_class=lowercase_       ,				hidden_size=37					)


				def   A_ (		self    :					Union[str, Any]					):
									self.config_tester.run_common_tests()


				def   A_ (		self    :					Tuple					):
									snake_case_									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_model(*lowercase_					)


				def   A_ (		self    :					List[Any]					):
									snake_case_									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_sequence_classification(*lowercase_					)


				def   A_ (		self    :					List[Any]					):
									snake_case_									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_multiple_choice(*lowercase_					)


				def   A_ (		self    :					Union[str, Any]					):
									snake_case_									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_token_classification(*lowercase_					)


				def   A_ (		self    :					Tuple					):
									snake_case_									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_question_answering(*lowercase_					)







@require_torch
class 						a				(   unittest.TestCase     ):


				@slow
				def   A_ (		self    :					List[Any]					):
									snake_case_									=				MPNetModel.from_pretrained('''microsoft/mpnet-base'''					)
									snake_case_									=				torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]					)
									snake_case_									=				model(lowercase_					)[0]
									snake_case_									=				torch.Size((1, 11, 768)					)
									self.assertEqual(output.shape       ,				lowercase_					)
									snake_case_									=				torch.tensor(
									    [[[-0.0550, 0.1943, -0.0740], [-0.0562, 0.2211, -0.0579], [-0.0437, 0.3337, -0.0641]]]					)
									# compare the actual values for a slice.
									self.assertTrue(torch.allclose(output[:, :3, :3]       ,				lowercase_       ,				atol=1e-4					)					)

