"""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       ,  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       ,  ):
       _lowerCAmelCase		: Any						    =  parent
       _lowerCAmelCase		: str						    =  batch_size
       _lowerCAmelCase		: List[Any]						    =  image_size
       _lowerCAmelCase		: str						    =  num_channels
       _lowerCAmelCase		: Union[str, Any]						    =  embeddings_size
       _lowerCAmelCase		: int						    =  hidden_sizes
       _lowerCAmelCase		: List[str]						    =  depths
       _lowerCAmelCase		: int						    =  is_training
       _lowerCAmelCase		: Optional[Any]						    =  use_labels
       _lowerCAmelCase		: List[Any]						    =  hidden_act
       _lowerCAmelCase		: Any						    =  num_labels
       _lowerCAmelCase		: Any						    =  scope
       _lowerCAmelCase		: List[Any]						    =  len(a__						)






     def 					__A						(	self						):
       _lowerCAmelCase		: Dict						    =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

       _lowerCAmelCase		: List[str]						    =  self.get_config()

       return config, pixel_values






     def 					__A						(	self						):
       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 					__A						(	self       ,  a__       ,  a__						):
       _lowerCAmelCase		: str						    =  FlaxRegNetModel(config=a__						)
       _lowerCAmelCase		: Dict						    =  model(a__						)

       # 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 					__A						(	self       ,  a__       ,  a__						):
       _lowerCAmelCase		: Dict						    =  self.num_labels
       _lowerCAmelCase		: List[Any]						    =  FlaxRegNetForImageClassification(config=a__						)
       _lowerCAmelCase		: Any						    =  model(a__						)
       self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_labels)						)






     def 					__A						(	self						):
       _lowerCAmelCase		: List[str]						    =  self.prepare_config_and_inputs()
       _lowerCAmelCase    ,			_lowerCAmelCase		: int						    =  config_and_inputs
       _lowerCAmelCase		: Union[str, Any]						    =  {"""pixel_values""": pixel_values}
       return config, inputs_dict







@require_flax
class 		__A							(				SCREAMING_SNAKE_CASE_					,			unittest.TestCase	):
     _UpperCamelCase							:    int	      =	(FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

     _UpperCamelCase							:    str	      =	False
     _UpperCamelCase							:    Dict	      =	False
     _UpperCamelCase							:    Optional[Any]	      =	False






     def 					__A						(	self						):
       _lowerCAmelCase		: Optional[Any]						    =  FlaxRegNetModelTester(self						)
       _lowerCAmelCase		: Optional[int]						    =  ConfigTester(self       ,  config_class=a__       ,  has_text_modality=a__						)






     def 					__A						(	self						):
       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						):
       return






     def 					__A						(	self						):
       _lowerCAmelCase		: Tuple						    =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*a__						)






     def 					__A						(	self						):
       _lowerCAmelCase		: Dict						    =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*a__						)






     @unittest.skip(reason="""RegNet does not use inputs_embeds"""						)
     def 					__A						(	self						):
       pass






     @unittest.skip(reason="""RegNet does not support input and output embeddings"""						)
     def 					__A						(	self						):
       pass






     def 					__A						(	self						):
       _lowerCAmelCase    ,			_lowerCAmelCase		: List[Any]						    =  self.model_tester.prepare_config_and_inputs_for_common()

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

         _lowerCAmelCase		: List[Any]						    =  ["""pixel_values"""]
         self.assertListEqual(arg_names[:1]       ,  a__						)






     def 					__A						(	self						):
       def check_hidden_states_output(a__       ,  a__       ,  a__						):
         _lowerCAmelCase		: Union[str, Any]						    =  model_class(a__						)

         _lowerCAmelCase		: int						    =  model(**self._prepare_for_class(a__       ,  a__						)						)

         _lowerCAmelCase		: int						    =  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

         _lowerCAmelCase		: Tuple						    =  self.model_tester.num_stages
         self.assertEqual(len(a__						)       ,  expected_num_stages + 1						)

       _lowerCAmelCase    ,			_lowerCAmelCase		: str						    =  self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
         _lowerCAmelCase		: Dict						    =  True
         check_hidden_states_output(a__       ,  a__       ,  a__						)

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         _lowerCAmelCase		: List[Any]						    =  True

         check_hidden_states_output(a__       ,  a__       ,  a__						)






     def 					__A						(	self						):
       _lowerCAmelCase    ,			_lowerCAmelCase		: int						    =  self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
         with self.subTest(model_class.__name__						):
           _lowerCAmelCase		: Optional[int]						    =  self._prepare_for_class(a__       ,  a__						)
           _lowerCAmelCase		: Any						    =  model_class(a__						)

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

           with self.subTest("""JIT Enabled"""						):
             _lowerCAmelCase		: Optional[Any]						    =  model_jitted(**a__						).to_tuple()

           with self.subTest("""JIT Disabled"""						):
             with jax.disable_jit():
               _lowerCAmelCase		: List[str]						    =  model_jitted(**a__						).to_tuple()

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







def 							SCREAMING_SNAKE_CASE  (    ) ->   Optional[Any]:
  _lowerCAmelCase		: Union[str, Any]						    =  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
  return image






@require_flax
class 		__A							(				unittest.TestCase	):






     @cached_property
     def 					__A						(	self						):
       return AutoImageProcessor.from_pretrained("""facebook/regnet-y-040"""						) if is_vision_available() else None






     @slow
     def 					__A						(	self						):
       _lowerCAmelCase		: Optional[int]						    =  FlaxRegNetForImageClassification.from_pretrained("""facebook/regnet-y-040"""						)

       _lowerCAmelCase		: Dict						    =  self.default_image_processor
       _lowerCAmelCase		: Dict						    =  prepare_img()
       _lowerCAmelCase		: List[str]						    =  image_processor(images=a__       ,  return_tensors="""np"""						)

       _lowerCAmelCase		: Union[str, Any]						    =  model(**a__						)

       # verify the logits
       _lowerCAmelCase		: Optional[Any]						    =  (1, 1000)
       self.assertEqual(outputs.logits.shape       ,  a__						)

       _lowerCAmelCase		: Dict						    =  jnp.array([-0.4_1_8_0, -1.5_0_5_1, -3.4_8_3_6]						)

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





"""simple docstring"""




from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
     if not (is_transformers_available() and is_torch_available() and is_transformers_version('>=', '4.25.0')):
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline
else:
     from .pipeline_unclip import UnCLIPPipeline
     from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline
     from .text_proj import UnCLIPTextProjModel




