






"""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    __lowerCamelCase    (  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 ,				)		->       Tuple:
       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(snake_case_      )

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

       UpperCamelCase__         =    self.get_config()

       return config, pixel_values

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       List[str]:
       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 						SCREAMING_SNAKE_CASE__ (					self ,				snake_case_ ,				snake_case_      )		->       Any:
       UpperCamelCase__         =    FlaxRegNetModel(config=snake_case_      )
       UpperCamelCase__         =    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 						SCREAMING_SNAKE_CASE__ (					self ,				snake_case_ ,				snake_case_      )		->       Union[str, Any]:
       UpperCamelCase__         =    self.num_labels
       UpperCamelCase__         =    FlaxRegNetForImageClassification(config=snake_case_      )
       UpperCamelCase__         =    model(snake_case_      )
       self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels)      )







   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       int:
       UpperCamelCase__         =    self.prepare_config_and_inputs()
       UpperCamelCase__       ,						UpperCamelCase__         =    config_and_inputs
       UpperCamelCase__         =    {'pixel_values': pixel_values}
       return config, inputs_dict







@require_flax
class    __lowerCamelCase    (  _a							,	unittest.TestCase  ):
   a      :				List[str] 						=(FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

   a      :				List[Any] 						=False
   a      :				Optional[Any] 						=False
   a      :				int 						=False

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       None:
       UpperCamelCase__         =    FlaxRegNetModelTester(self      )
       UpperCamelCase__         =    ConfigTester(self ,				config_class=snake_case_ ,				has_text_modality=snake_case_      )

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       str:
       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 						SCREAMING_SNAKE_CASE__ (					self      )		->       List[str]:
       return

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Optional[Any]:
       UpperCamelCase__         =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*snake_case_      )

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Optional[Any]:
       UpperCamelCase__         =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*snake_case_      )

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

   @unittest.skip(reason='RegNet does not support input and output embeddings'      )
   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Optional[Any]:
       pass

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       str:
       UpperCamelCase__       ,						UpperCamelCase__         =    self.model_tester.prepare_config_and_inputs_for_common()

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

           UpperCamelCase__         =    ['pixel_values']
           self.assertListEqual(arg_names[:1] ,				snake_case_      )

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Dict:
       def check_hidden_states_output(snake_case_ ,				snake_case_ ,				snake_case_      ):
           UpperCamelCase__         =    model_class(snake_case_      )

           UpperCamelCase__         =    model(**self._prepare_for_class(snake_case_ ,				snake_case_      )      )

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

           UpperCamelCase__         =    self.model_tester.num_stages
           self.assertEqual(len(snake_case_      ) ,				expected_num_stages + 1      )

       UpperCamelCase__       ,						UpperCamelCase__         =    self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           UpperCamelCase__         =    True
           check_hidden_states_output(snake_case_ ,				snake_case_ ,				snake_case_      )

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

           check_hidden_states_output(snake_case_ ,				snake_case_ ,				snake_case_      )







   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Union[str, Any]:
       UpperCamelCase__       ,						UpperCamelCase__         =    self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           with self.subTest(model_class.__name__      ):
               UpperCamelCase__         =    self._prepare_for_class(snake_case_ ,				snake_case_      )
               UpperCamelCase__         =    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'      ):
                   UpperCamelCase__         =    model_jitted(**snake_case_      ).to_tuple()

               with self.subTest('JIT Disabled'      ):
                   with jax.disable_jit():
                       UpperCamelCase__         =    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   lowerCAmelCase_( )					->	List[Any]:
    """simple docstring"""

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





@require_flax
class    __lowerCamelCase    (  unittest.TestCase  ):

   @cached_property
   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Union[str, Any]:
       return AutoImageProcessor.from_pretrained('facebook/regnet-y-040'      ) if is_vision_available() else None







   @slow
   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Any:
       UpperCamelCase__         =    FlaxRegNetForImageClassification.from_pretrained('facebook/regnet-y-040'      )

       UpperCamelCase__         =    self.default_image_processor
       UpperCamelCase__         =    prepare_img()
       UpperCamelCase__         =    image_processor(images=snake_case_ ,				return_tensors='np'      )

       UpperCamelCase__         =    model(**snake_case_      )

       # verify the logits
       UpperCamelCase__         =    (1, 1000)
       self.assertEqual(outputs.logits.shape ,				snake_case_      )

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

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












"""simple docstring"""





import doctest
import logging
import os
import unittest
from pathlib import Path
from typing import List, Union

import transformers
from transformers.testing_utils import require_tf, require_torch, slow


A__					: Union[str, Any]=	logging.getLogger()
@unittest.skip("""Temporarily disable the doc tests."""  )
@require_torch
@require_tf
@slow
class    __lowerCamelCase    (  unittest.TestCase  ):

   def 						SCREAMING_SNAKE_CASE__ (					self ,				snake_case_ ,				snake_case_ = None ,				snake_case_ = None ,				snake_case_ = None ,				snake_case_ = True ,				)		->       Tuple:
       UpperCamelCase__         =    [file for file in os.listdir(snake_case_      ) if os.path.isfile(os.path.join(snake_case_ ,				snake_case_      )      )]

       if identifier is not None:
           UpperCamelCase__         =    [file for file in files if identifier in file]

       if n_identifier is not None:
           if isinstance(snake_case_ ,				snake_case_      ):
               for n_ in n_identifier:
                   UpperCamelCase__         =    [file for file in files if n_ not in file]
           else:
               UpperCamelCase__         =    [file for file in files if n_identifier not in file]

       UpperCamelCase__         =    ignore_files or []
       ignore_files.append('__init__.py'      )
       UpperCamelCase__         =    [file for file in files if file not in ignore_files]

       for file in files:
           # Open all files
           print('Testing' ,				snake_case_      )

           if only_modules:
               UpperCamelCase__         =    file.split('.'      )[0]
               try:
                   UpperCamelCase__         =    getattr(snake_case_ ,				snake_case_      )
                   UpperCamelCase__         =    doctest.DocTestSuite(snake_case_      )
                   UpperCamelCase__         =    unittest.TextTestRunner().run(snake_case_      )
                   self.assertIs(len(result.failures      ) ,				0      )
               except AttributeError:
                   logger.info(F'{module_identifier} is not a module.'      )
           else:
               UpperCamelCase__         =    doctest.testfile(str('..' / directory / file      ) ,				optionflags=doctest.ELLIPSIS      )
               self.assertIs(result.failed ,				0      )

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Union[str, Any]:
       UpperCamelCase__         =    Path('src/transformers'      )
       UpperCamelCase__         =    'modeling'
       UpperCamelCase__         =    [
           'modeling_ctrl.py',
           'modeling_tf_ctrl.py',
       ]
       self.analyze_directory(snake_case_ ,				identifier=snake_case_ ,				ignore_files=snake_case_      )

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Optional[int]:
       UpperCamelCase__         =    Path('src/transformers'      )
       UpperCamelCase__         =    'tokenization'
       self.analyze_directory(snake_case_ ,				identifier=snake_case_      )

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Optional[Any]:
       UpperCamelCase__         =    Path('src/transformers'      )
       UpperCamelCase__         =    'configuration'
       self.analyze_directory(snake_case_ ,				identifier=snake_case_      )

   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       str:
       UpperCamelCase__         =    Path('src/transformers'      )
       UpperCamelCase__         =    ['configuration', 'modeling', 'tokenization']
       self.analyze_directory(snake_case_ ,				n_identifier=snake_case_      )







   def 						SCREAMING_SNAKE_CASE__ (					self      )		->       Union[str, Any]:
       UpperCamelCase__         =    Path('docs/source'      )
       UpperCamelCase__         =    ['favicon.ico']
       self.analyze_directory(snake_case_ ,				ignore_files=snake_case_ ,				only_modules=snake_case_      )




