
'''simple docstring'''




import inspect
import unittest

from transformers import RegNetConfig, 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.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel

if is_vision_available():
      from PIL import Image

      from transformers import AutoImageProcessor





class 					A							(				unittest.TestCase		):





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






         """simple docstring"""






         A			:  str   =		parent
         A			:  Any   =		batch_size
         A			:  Any   =		image_size
         A			:  List[Any]   =		num_channels
         A			:  Optional[int]   =		embeddings_size
         A			:  str   =		hidden_sizes
         A			:  Any   =		depths
         A			:  Dict   =		is_training
         A			:  Tuple   =		use_labels
         A			:  List[Any]   =		hidden_act
         A			:  str   =		num_labels
         A			:  List[str]   =		scope
         A			:  str   =		len(SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							List[str]:






         """simple docstring"""






         A			:  Any   =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

         A			:  Union[str, Any]   =		self.get_config()

         return config, pixel_values





     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






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





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							Optional[int]:






         """simple docstring"""






         A			:  int   =		FlaxRegNetModel(config=SCREAMING_SNAKE_CASE      )
         A			:  int   =		model(SCREAMING_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 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							Optional[int]:






         """simple docstring"""






         A			:  Optional[int]   =		self.num_labels
         A			:  Tuple   =		FlaxRegNetForImageClassification(config=SCREAMING_SNAKE_CASE      )
         A			:  Union[str, Any]   =		model(SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels)      )





     def 							__lowerCAmelCase				(				self      )					->							List[str]:






         """simple docstring"""






         A			:  Optional[Any]   =		self.prepare_config_and_inputs()
         A,    A			:  Optional[Any]   =		config_and_inputs
         A			:  Tuple   =		{'''pixel_values''': pixel_values}
         return config, inputs_dict





@require_flax
class 					A							(				__snake_case       ,     unittest.TestCase		):
     __magic_name__          =						(FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

     __magic_name__          =						False
     __magic_name__          =						False
     __magic_name__          =						False





     def 							__lowerCAmelCase				(				self      )					->							None:






         """simple docstring"""






         A			:  int   =		FlaxRegNetModelTester(self      )
         A			:  Optional[int]   =		ConfigTester(self		,     config_class=SCREAMING_SNAKE_CASE		,     has_text_modality=SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							List[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      )					->							Dict:






         """simple docstring"""






         return





     def 							__lowerCAmelCase				(				self      )					->							List[str]:






         """simple docstring"""






         A			:  Dict   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Dict:






         """simple docstring"""






         A			:  str   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE      )





     @unittest.skip(reason='''RegNet does not use inputs_embeds'''      )
     def 							__lowerCAmelCase				(				self      )					->							Optional[int]:






         """simple docstring"""






         pass





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






         """simple docstring"""






         pass





     def 							__lowerCAmelCase				(				self      )					->							Union[str, Any]:






         """simple docstring"""






         A,    A			:  List[str]   =		self.model_tester.prepare_config_and_inputs_for_common()

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

             A			:  List[Any]   =		['''pixel_values''']
             self.assertListEqual(arg_names[:1]		,     SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






         def check_hidden_states_output(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      ):
             A			:  Any   =		model_class(SCREAMING_SNAKE_CASE      )

             A			:  int   =		model(**self._prepare_for_class(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )      )

             A			:  Tuple   =		outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

             A			:  Any   =		self.model_tester.num_stages
             self.assertEqual(len(SCREAMING_SNAKE_CASE      )		,     expected_num_stages + 1      )

         A,    A			:  Union[str, Any]   =		self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             A			:  Any   =		True
             check_hidden_states_output(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )

             # check that output_hidden_states also work using config
             del inputs_dict["output_hidden_states"]
             A			:  int   =		True

             check_hidden_states_output(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






         A,    A			:  Any   =		self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             with self.subTest(model_class.__name__      ):
                 A			:  Optional[int]   =		self._prepare_for_class(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )
                 A			:  str   =		model_class(SCREAMING_SNAKE_CASE      )

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

                 with self.subTest('''JIT Enabled'''      ):
                     A			:  Any   =		model_jitted(**SCREAMING_SNAKE_CASE      ).to_tuple()

                 with self.subTest('''JIT Disabled'''      ):
                     with jax.disable_jit():
                         A			:  Optional[Any]   =		model_jitted(**SCREAMING_SNAKE_CASE      ).to_tuple()

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







def 						lowerCAmelCase_							(  ):




    '''simple docstring'''



    A			:  Union[str, Any]   =		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
    return image







@require_flax
class 					A							(				unittest.TestCase		):





     @cached_property
     def 							__lowerCAmelCase				(				self      )					->							Union[str, Any]:






         """simple docstring"""






         return AutoImageProcessor.from_pretrained('''facebook/regnet-y-040'''      ) if is_vision_available() else None





     @slow
     def 							__lowerCAmelCase				(				self      )					->							Optional[int]:






         """simple docstring"""






         A			:  Any   =		FlaxRegNetForImageClassification.from_pretrained('''facebook/regnet-y-040'''      )

         A			:  str   =		self.default_image_processor
         A			:  List[Any]   =		prepare_img()
         A			:  Dict   =		image_processor(images=SCREAMING_SNAKE_CASE		,     return_tensors='''np'''      )

         A			:  List[Any]   =		model(**SCREAMING_SNAKE_CASE      )

         # verify the logits
         A			:  int   =		(1, 1000)
         self.assertEqual(outputs.logits.shape		,     SCREAMING_SNAKE_CASE      )

         A			:  Any   =		jnp.array([-0.4_180, -1.5_051, -3.4_836]      )

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









from argparse import ArgumentParser

from .add_new_model import AddNewModelCommand
from .add_new_model_like import AddNewModelLikeCommand
from .convert import ConvertCommand
from .download import DownloadCommand
from .env import EnvironmentCommand
from .lfs import LfsCommands
from .pt_to_tf import PTtoTFCommand
from .run import RunCommand
from .serving import ServeCommand
from .user import UserCommands

def 	_lowerCAmelCase (    ):
      lowercase__  =	ArgumentParser('Transformers CLI tool'    ,	usage='transformers-cli <command> [<args>]'						)
      lowercase__  =	parser.add_subparsers(help='transformers-cli command helpers'						)

      # Register commands
      ConvertCommand.register_subcommand(A__						)
      DownloadCommand.register_subcommand(A__						)
      EnvironmentCommand.register_subcommand(A__						)
      RunCommand.register_subcommand(A__						)
      ServeCommand.register_subcommand(A__						)
      UserCommands.register_subcommand(A__						)
      AddNewModelCommand.register_subcommand(A__						)
      AddNewModelLikeCommand.register_subcommand(A__						)
      LfsCommands.register_subcommand(A__						)
      PTtoTFCommand.register_subcommand(A__						)

      # Let's go
      lowercase__  =	parser.parse_args()

      if not hasattr(A__    ,	'func'						):
            parser.print_help()
            exit(1						)

      # Run
      lowercase__  =	args.func(A__						)
      service.run()


if __name__ == "__main__":
       main()
