

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 						__magic_name__      (							unittest.TestCase							):

   '''simple docstring'''




   def __init__(    self,   lowercase_,   lowercase_=3,   lowercase_=32,   lowercase_=3,   lowercase_=10,   lowercase_=[10, 20, 30, 40],   lowercase_=[1, 1, 2, 1],   lowercase_=True,   lowercase_=True,   lowercase_="relu",   lowercase_=3,   lowercase_=None,   )							->   Optional[int]:






     """simple docstring"""






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



   def 			_UpperCAmelCase			(    self	)							->   Dict:






     """simple docstring"""






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

     a__  =self.get_config()

     return config, pixel_values



   def 			_UpperCAmelCase			(    self	)							->   List[Any]:






     """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,   lowercase_,   lowercase_	)							->   str:






     """simple docstring"""






     a__  =FlaxResNetModel(config=lowercase_	)
     a__  =model(lowercase_	)

     # 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,   lowercase_,   lowercase_	)							->   str:






     """simple docstring"""






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



   def 			_UpperCAmelCase			(    self	)							->   List[Any]:






     """simple docstring"""






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







@require_flax
class 						__magic_name__      (							lowerCamelCase__		,	unittest.TestCase							):

   '''simple docstring'''




   lowerCamelCase__			:     Optional[int]    =       (FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

   lowerCamelCase__			:     List[Any]    =       False
   lowerCamelCase__			:     str    =       False
   lowerCamelCase__			:     int    =       False



   def 			_UpperCAmelCase			(    self	)							->   None:






     """simple docstring"""






     a__  =FlaxResNetModelTester(self	)
     a__  =ConfigTester(self,   config_class=lowercase_,   has_text_modality=lowercase_	)



   def 			_UpperCAmelCase			(    self	)							->   Tuple:






     """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	)							->   Dict:






     """simple docstring"""






     return



   def 			_UpperCAmelCase			(    self	)							->   Any:






     """simple docstring"""






     a__  =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_	)



   def 			_UpperCAmelCase			(    self	)							->   List[Any]:






     """simple docstring"""






     a__  =self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*lowercase_	)



   @unittest.skip(reason='''ResNet does not use inputs_embeds'''	)
   def 			_UpperCAmelCase			(    self	)							->   str:






     """simple docstring"""






     pass



   @unittest.skip(reason='''ResNet does not support input and output embeddings'''	)
   def 			_UpperCAmelCase			(    self	)							->   List[str]:






     """simple docstring"""






     pass



   def 			_UpperCAmelCase			(    self	)							->   Optional[int]:






     """simple docstring"""






     a__,				a__  =self.model_tester.prepare_config_and_inputs_for_common()

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

       a__  =['''pixel_values''']
       self.assertListEqual(arg_names[:1],   lowercase_	)



   def 			_UpperCAmelCase			(    self	)							->   Any:






     """simple docstring"""






     def check_hidden_states_output(lowercase_,   lowercase_,   lowercase_	):
       a__  =model_class(lowercase_	)

       a__  =model(**self._prepare_for_class(lowercase_,   lowercase_	)	)

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

       a__  =self.model_tester.num_stages
       self.assertEqual(len(lowercase_	),   expected_num_stages + 1	)



   @unittest.skip(reason='''ResNet does not use feedforward chunking'''	)
   def 			_UpperCAmelCase			(    self	)							->   int:






     """simple docstring"""






     pass



   def 			_UpperCAmelCase			(    self	)							->   List[Any]:






     """simple docstring"""






     a__,				a__  =self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
       with self.subTest(model_class.__name__	):
         a__  =self._prepare_for_class(lowercase_,   lowercase_	)
         a__  =model_class(lowercase_	)

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

         with self.subTest('''JIT Enabled'''	):
           a__  =model_jitted(**lowercase_	).to_tuple()

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

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





def       UpperCAmelCase__    (      ):





  '''simple docstring'''







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







@require_flax
class 						__magic_name__      (							unittest.TestCase							):

   '''simple docstring'''




   @cached_property
   def 			_UpperCAmelCase			(    self	)							->   Optional[Any]:






     """simple docstring"""






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



   @slow
   def 			_UpperCAmelCase			(    self	)							->   List[Any]:






     """simple docstring"""






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

     a__  =self.default_image_processor
     a__  =prepare_img()
     a__  =image_processor(images=lowercase_,   return_tensors='''np'''	)

     a__  =model(**lowercase_	)

     # verify the logits
     a__  =(1, 1000)
     self.assertEqual(outputs.logits.shape,   lowercase_	)

     a__  =jnp.array([-11.1069, -9.7877, -8.3777]	)

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



import os
import unittest

import pytest
from parameterized import parameterized

from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device


# skipping in unittest tests

lowerCamelCase									= [(1,)]




def       UpperCAmelCase__    (      ):





  '''simple docstring'''







  a__  =bool(os.getenv('''RUN_SLOW'''     ,				0							)							)
  if run_slow:
    assert True
  else:
    assert False, "should have been skipped"




def       UpperCAmelCase__    (      ):





  '''simple docstring'''







  a__  =bool(os.getenv('''RUN_SLOW'''     ,				0							)							)
  if run_slow and torch_device == "cuda":
    assert True
  else:
    assert False, "should have been skipped"







@require_torch
class 						__magic_name__      (							unittest.TestCase							):

   '''simple docstring'''




   @slow
   @require_torch_gpu
   def 			_UpperCAmelCase			(    self	)							->   Union[str, Any]:






     """simple docstring"""






     check_slow_torch_cuda()



   @require_torch_gpu
   @slow
   def 			_UpperCAmelCase			(    self	)							->   List[str]:






     """simple docstring"""






     check_slow_torch_cuda()



   @parameterized.expand(lowercase_	)
   @slow
   def 			_UpperCAmelCase			(    self,   lowercase_=None	)							->   Tuple:






     """simple docstring"""






     check_slow()





@slow
@require_torch_gpu
def       UpperCAmelCase__    (      ):





  '''simple docstring'''







  check_slow_torch_cuda()




@require_torch_gpu
@slow
def       UpperCAmelCase__    (      ):





  '''simple docstring'''







  check_slow_torch_cuda()




@slow
@pytest.mark.parametrize('''param'''     ,				[1]							)
def       UpperCAmelCase__    (      _A				:			str							):





  '''simple docstring'''







  check_slow()




@pytest.mark.parametrize('''param'''     ,				[1]							)
@slow
def       UpperCAmelCase__    (      _A				:			Tuple							):





  '''simple docstring'''







  check_slow()
