





"""simple docstring"""
import inspect
import unittest

from transformers import ResNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
       import jax
       import jax.numpy as jnp

       from transformers.models.resnet.modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel

if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor







class 	a				(					unittest.TestCase   ):



 def __init__(			self							,    _snake_case							,    _snake_case=3							,    _snake_case=32							,    _snake_case=3							,    _snake_case=10							,    _snake_case=[10, 20, 30, 40]							,    _snake_case=[1, 1, 2, 1]							,    _snake_case=True							,    _snake_case=True							,    _snake_case="relu"							,    _snake_case=3							,    _snake_case=None							,    ):







  """simple docstring"""






  lowerCAmelCase  							=      parent
  lowerCAmelCase  							=      batch_size
  lowerCAmelCase  							=      image_size
  lowerCAmelCase  							=      num_channels
  lowerCAmelCase  							=      embeddings_size
  lowerCAmelCase  							=      hidden_sizes
  lowerCAmelCase  							=      depths
  lowerCAmelCase  							=      is_training
  lowerCAmelCase  							=      use_labels
  lowerCAmelCase  							=      hidden_act
  lowerCAmelCase  							=      num_labels
  lowerCAmelCase  							=      scope
  lowerCAmelCase  							=      len(_snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






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

  lowerCAmelCase  							=      self.get_config()

  return config, pixel_values



 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							,    image_size=self.image_size							,    )



 def 					UpperCamelCase__   (			self							,    _snake_case							,    _snake_case			):







  """simple docstring"""






  lowerCAmelCase  							=      FlaxResNetModel(config=_snake_case			)
  lowerCAmelCase  							=      model(_snake_case			)

  # Output shape (b, c, h, w)
  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							,    _snake_case							,    _snake_case			):







  """simple docstring"""






  lowerCAmelCase  							=      self.num_labels
  lowerCAmelCase  							=      FlaxResNetForImageClassification(config=_snake_case			)
  lowerCAmelCase  							=      model(_snake_case			)
  self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.num_labels)			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






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







@require_flax
class 	a				(					a__					,      unittest.TestCase   ):
 snake_case__    	=    (FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

 snake_case__    	=    False
 snake_case__    	=    False
 snake_case__    	=    False



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      FlaxResNetModelTester(self			)
  lowerCAmelCase  							=      ConfigTester(self							,    config_class=_snake_case							,    has_text_modality=_snake_case			)



 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



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_model(*_snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_image_classification(*_snake_case			)



 @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"""






  lowerCAmelCase							,lowerCAmelCase  							=      self.model_tester.prepare_config_and_inputs_for_common()

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

   lowerCAmelCase  							=      ['pixel_values']
   self.assertListEqual(arg_names[:1]							,    _snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  def check_hidden_states_output(_snake_case							,    _snake_case							,    _snake_case			):
   lowerCAmelCase  							=      model_class(_snake_case			)

   lowerCAmelCase  							=      model(**self._prepare_for_class(_snake_case							,    _snake_case			)			)

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

   lowerCAmelCase  							=      self.model_tester.num_stages
   self.assertEqual(len(_snake_case			)							,    expected_num_stages + 1			)



 @unittest.skip(reason='ResNet does not use feedforward chunking'			)
 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  pass



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase							,lowerCAmelCase  							=      self.model_tester.prepare_config_and_inputs_for_common()

  for model_class in self.all_model_classes:
   with self.subTest(model_class.__name__			):
    lowerCAmelCase  							=      self._prepare_for_class(_snake_case							,    _snake_case			)
    lowerCAmelCase  							=      model_class(_snake_case			)

    @jax.jit
    def model_jitted(_snake_case							,    **_snake_case			):
     return model(pixel_values=_snake_case							,    **_snake_case			)

    with self.subTest('JIT Enabled'			):
     lowerCAmelCase  							=      model_jitted(**_snake_case			).to_tuple()

    with self.subTest('JIT Disabled'			):
     with jax.disable_jit():
      lowerCAmelCase  							=      model_jitted(**_snake_case			).to_tuple()

    self.assertEqual(len(_snake_case			)							,    len(_snake_case			)			)
    for jitted_output, output in zip(_snake_case							,    _snake_case			):
     self.assertEqual(jitted_output.shape							,    output.shape			)


def 						_SCREAMING_SNAKE_CASE       ():
 lowerCAmelCase  							=      Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
 return image







@require_flax
class 	a				(					unittest.TestCase   ):



 @cached_property
 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  return AutoImageProcessor.from_pretrained('microsoft/resnet-50'			) if is_vision_available() else None



 @slow
 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      FlaxResNetForImageClassification.from_pretrained('microsoft/resnet-50'			)

  lowerCAmelCase  							=      self.default_image_processor
  lowerCAmelCase  							=      prepare_img()
  lowerCAmelCase  							=      image_processor(images=_snake_case							,    return_tensors='np'			)

  lowerCAmelCase  							=      model(**_snake_case			)

  # verify the logits
  lowerCAmelCase  							=      (1, 10_00)
  self.assertEqual(outputs.logits.shape							,    _snake_case			)

  lowerCAmelCase  							=      jnp.array([-11.1_069, -9.7_877, -8.3_777]			)

  self.assertTrue(jnp.allclose(outputs.logits[0, :3]							,    _snake_case							,    atol=1E-4			)			)












"""simple docstring"""
from __future__ import annotations

import unittest

from transformers import MPNetConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
       import tensorflow as tf

       from transformers.models.mpnet.modeling_tf_mpnet import (
           TFMPNetForMaskedLM,
           TFMPNetForMultipleChoice,
           TFMPNetForQuestionAnswering,
           TFMPNetForSequenceClassification,
           TFMPNetForTokenClassification,
           TFMPNetModel,
       )







class 	a				:



 def __init__(			self							,    _snake_case							,    _snake_case=13							,    _snake_case=7							,    _snake_case=True							,    _snake_case=True							,    _snake_case=False							,    _snake_case=True							,    _snake_case=99							,    _snake_case=64							,    _snake_case=2							,    _snake_case=4							,    _snake_case=64							,    _snake_case="gelu"							,    _snake_case=0.1							,    _snake_case=0.1							,    _snake_case=5_12							,    _snake_case=16							,    _snake_case=2							,    _snake_case=0.02							,    _snake_case=3							,    _snake_case=4							,    _snake_case=None							,    ):







  """simple docstring"""






  lowerCAmelCase  							=      parent
  lowerCAmelCase  							=      batch_size
  lowerCAmelCase  							=      seq_length
  lowerCAmelCase  							=      is_training
  lowerCAmelCase  							=      use_input_mask
  lowerCAmelCase  							=      use_token_type_ids
  lowerCAmelCase  							=      use_labels
  lowerCAmelCase  							=      vocab_size
  lowerCAmelCase  							=      hidden_size
  lowerCAmelCase  							=      num_hidden_layers
  lowerCAmelCase  							=      num_attention_heads
  lowerCAmelCase  							=      intermediate_size
  lowerCAmelCase  							=      hidden_act
  lowerCAmelCase  							=      hidden_dropout_prob
  lowerCAmelCase  							=      attention_probs_dropout_prob
  lowerCAmelCase  							=      max_position_embeddings
  lowerCAmelCase  							=      type_vocab_size
  lowerCAmelCase  							=      type_sequence_label_size
  lowerCAmelCase  							=      initializer_range
  lowerCAmelCase  							=      num_labels
  lowerCAmelCase  							=      num_choices
  lowerCAmelCase  							=      scope



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      ids_tensor([self.batch_size, self.seq_length]							,    self.vocab_size			)

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

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

  lowerCAmelCase  							=      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							,    )
  return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels



 def 					UpperCamelCase__   (			self							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case			):







  """simple docstring"""






  lowerCAmelCase  							=      TFMPNetModel(config=_snake_case			)
  lowerCAmelCase  							=      {'input_ids': input_ids, 'attention_mask': input_mask}
  lowerCAmelCase  							=      model(_snake_case			)
  lowerCAmelCase  							=      [input_ids, input_mask]
  lowerCAmelCase  							=      model(_snake_case			)
  self.parent.assertEqual(result.last_hidden_state.shape							,    (self.batch_size, self.seq_length, self.hidden_size)			)



 def 					UpperCamelCase__   (			self							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case			):







  """simple docstring"""






  lowerCAmelCase  							=      TFMPNetForMaskedLM(config=_snake_case			)
  lowerCAmelCase  							=      {'input_ids': input_ids, 'attention_mask': input_mask}
  lowerCAmelCase  							=      model(_snake_case			)
  self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.seq_length, self.vocab_size)			)



 def 					UpperCamelCase__   (			self							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case			):







  """simple docstring"""






  lowerCAmelCase  							=      TFMPNetForQuestionAnswering(config=_snake_case			)
  lowerCAmelCase  							=      {
      'input_ids': input_ids,
      'attention_mask': input_mask,
  }
  lowerCAmelCase  							=      model(_snake_case			)
  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 					UpperCamelCase__   (			self							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case			):







  """simple docstring"""






  lowerCAmelCase  							=      self.num_labels
  lowerCAmelCase  							=      TFMPNetForSequenceClassification(_snake_case			)
  lowerCAmelCase  							=      {'input_ids': input_ids, 'attention_mask': input_mask}
  lowerCAmelCase  							=      model(_snake_case			)
  self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.num_labels)			)



 def 					UpperCamelCase__   (			self							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case			):







  """simple docstring"""






  lowerCAmelCase  							=      self.num_choices
  lowerCAmelCase  							=      TFMPNetForMultipleChoice(_snake_case			)
  lowerCAmelCase  							=      tf.tile(tf.expand_dims(_snake_case							,    1			)							,    (1, self.num_choices, 1)			)
  lowerCAmelCase  							=      tf.tile(tf.expand_dims(_snake_case							,    1			)							,    (1, self.num_choices, 1)			)
  lowerCAmelCase  							=      {
      'input_ids': multiple_choice_inputs_ids,
      'attention_mask': multiple_choice_input_mask,
  }
  lowerCAmelCase  							=      model(_snake_case			)
  self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.num_choices)			)



 def 					UpperCamelCase__   (			self							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case							,    _snake_case			):







  """simple docstring"""






  lowerCAmelCase  							=      self.num_labels
  lowerCAmelCase  							=      TFMPNetForTokenClassification(_snake_case			)
  lowerCAmelCase  							=      {'input_ids': input_ids, 'attention_mask': input_mask}
  lowerCAmelCase  							=      model(_snake_case			)
  self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.seq_length, self.num_labels)			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      self.prepare_config_and_inputs()
  ((lowerCAmelCase)							,(lowerCAmelCase)							,(lowerCAmelCase)							,(lowerCAmelCase)							,(lowerCAmelCase)							,(lowerCAmelCase))  							=      config_and_inputs
  lowerCAmelCase  							=      {'input_ids': input_ids, 'attention_mask': input_mask}
  return config, inputs_dict







@require_tf
class 	a				(					a__					,      a__					,      unittest.TestCase   ):
 snake_case__    	=    (
     (
         TFMPNetForMaskedLM,
         TFMPNetForMultipleChoice,
         TFMPNetForQuestionAnswering,
         TFMPNetForSequenceClassification,
         TFMPNetForTokenClassification,
         TFMPNetModel,
     )
     if is_tf_available()
     else ()
 )
 snake_case__    	=    (
     {
         '''feature-extraction''': TFMPNetModel,
         '''fill-mask''': TFMPNetForMaskedLM,
         '''question-answering''': TFMPNetForQuestionAnswering,
         '''text-classification''': TFMPNetForSequenceClassification,
         '''token-classification''': TFMPNetForTokenClassification,
         '''zero-shot''': TFMPNetForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 snake_case__    	=    False
 snake_case__    	=    False



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      TFMPNetModelTester(self			)
  lowerCAmelCase  							=      ConfigTester(self							,    config_class=_snake_case							,    hidden_size=37			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  self.config_tester.run_common_tests()



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_mpnet_model(*_snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_mpnet_for_masked_lm(*_snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_mpnet_for_question_answering(*_snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_mpnet_for_sequence_classification(*_snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_mpnet_for_multiple_choice(*_snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_mpnet_for_token_classification(*_snake_case			)



 @slow
 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  for model_name in ["microsoft/mpnet-base"]:
   lowerCAmelCase  							=      TFMPNetModel.from_pretrained(_snake_case			)
   self.assertIsNotNone(_snake_case			)







@require_tf
class 	a				(					unittest.TestCase   ):



 @slow
 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      TFMPNetModel.from_pretrained('microsoft/mpnet-base'			)
  lowerCAmelCase  							=      tf.constant([[0, 1, 2, 3, 4, 5]]			)
  lowerCAmelCase  							=      model(_snake_case			)[0]

  lowerCAmelCase  							=      [1, 6, 7_68]
  self.assertEqual(output.shape							,    _snake_case			)

  lowerCAmelCase  							=      tf.constant(
      [
          [
              [-0.1_067_172, 0.08_216_473, 0.0_024_543],
              [-0.03_465_879, 0.8_354_118, -0.03_252_288],
              [-0.06_569_476, -0.12_424_111, -0.0_494_436],
          ]
      ]			)
  tf.debugging.assert_near(output[:, :3, :3]							,    _snake_case							,    atol=1E-4			)





