





'''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 UpperCAmelCase							(		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     ,			):
     __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(__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

     __UpperCAmelCase					=   self.get_config()

     return config, pixel_values



    def 					__lowerCamelCase						(  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 					__lowerCamelCase						(  self     ,			__A     ,			__A					):
     __UpperCAmelCase					=   FlaxRegNetModel(config=__A					)
     __UpperCAmelCase					=   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 					__lowerCamelCase						(  self     ,			__A     ,			__A					):
     __UpperCAmelCase					=   self.num_labels
     __UpperCAmelCase					=   FlaxRegNetForImageClassification(config=__A					)
     __UpperCAmelCase					=   model(__A					)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.num_labels)					)




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






@require_flax
class UpperCAmelCase							(		UpperCAmelCase_					,			unittest.TestCase							):
    _A :			List[Any]						   =       (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

    _A :			Any						   =       False
    _A :			int						   =       False
    _A :			Union[str, Any]						   =       False



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   FlaxRegNetModelTester(self					)
     __UpperCAmelCase					=   ConfigTester(self     ,			config_class=__A     ,			has_text_modality=__A					)



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



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   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 					__lowerCamelCase						(  self					):
     pass



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



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					,    __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
      __UpperCAmelCase					=   model_class(__A					)
      __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]     ,			__A					)



    def 					__lowerCamelCase						(  self					):
     def check_hidden_states_output(__A     ,			__A     ,			__A					):
      __UpperCAmelCase					=   model_class(__A					)

      __UpperCAmelCase					=   model(**self._prepare_for_class(__A     ,			__A					)					)

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

      __UpperCAmelCase					=   self.model_tester.num_stages
      self.assertEqual(len(__A					)     ,			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(__A     ,			__A     ,			__A					)

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

      check_hidden_states_output(__A     ,			__A     ,			__A					)




    def 					__lowerCamelCase						(  self					):
     __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(__A     ,			__A					)
       __UpperCAmelCase					=   model_class(__A					)

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

       with self.subTest('JIT Enabled'					):
        __UpperCAmelCase					=   model_jitted(**__A					).to_tuple()

       with self.subTest('JIT Disabled'					):
        with jax.disable_jit():
         __UpperCAmelCase					=   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 _lowerCAmelCase     (      )->     Any:
 __UpperCAmelCase					=   Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'    )
 return image






@require_flax
class UpperCAmelCase							(		unittest.TestCase							):



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




    @slow
    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   FlaxRegNetForImageClassification.from_pretrained('facebook/regnet-y-040'					)

     __UpperCAmelCase					=   self.default_image_processor
     __UpperCAmelCase					=   prepare_img()
     __UpperCAmelCase					=   image_processor(images=__A     ,			return_tensors='np'					)

     __UpperCAmelCase					=   model(**__A					)

     # verify the logits
     __UpperCAmelCase					=   (1, 1_000)
     self.assertEqual(outputs.logits.shape     ,			__A					)

     __UpperCAmelCase					=   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 typing import List, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_A:					str							=				logging.get_logger(__name__)

_A:					Optional[Any]							=				{
    """huggingface/autoformer-tourism-monthly""": """https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json""",
}






class UpperCAmelCase							(		UpperCAmelCase_							):
    _A :			Tuple						   =       """autoformer"""
    _A :			str						   =       {
        """hidden_size""": """d_model""",
        """num_attention_heads""": """encoder_attention_heads""",
        """num_hidden_layers""": """encoder_layers""",
    }



    def __init__(  self     ,			__A = None     ,			__A = None     ,			__A = "student_t"     ,			__A = "nll"     ,			__A = 1     ,			__A = [1, 2, 3, 4, 5, 6, 7]     ,			__A = True     ,			__A = 0     ,			__A = 0     ,			__A = 0     ,			__A = 0     ,			__A = None     ,			__A = None     ,			__A = 64     ,			__A = 2     ,			__A = 2     ,			__A = 2     ,			__A = 2     ,			__A = 32     ,			__A = 32     ,			__A = "gelu"     ,			__A = 0.1     ,			__A = 0.1     ,			__A = 0.1     ,			__A = 0.1     ,			__A = 0.1     ,			__A = 100     ,			__A = 0.0_2     ,			__A = True     ,			__A=True     ,			__A = 10     ,			__A = 25     ,			__A = 3     ,			**__A     ,			):
     # time series specific configuration
     __UpperCAmelCase					=   prediction_length
     __UpperCAmelCase					=   context_length if context_length is not None else prediction_length
     __UpperCAmelCase					=   distribution_output
     __UpperCAmelCase					=   loss
     __UpperCAmelCase					=   input_size
     __UpperCAmelCase					=   num_time_features
     __UpperCAmelCase					=   lags_sequence
     __UpperCAmelCase					=   scaling
     __UpperCAmelCase					=   num_dynamic_real_features
     __UpperCAmelCase					=   num_static_real_features
     __UpperCAmelCase					=   num_static_categorical_features
     if cardinality is not None and num_static_categorical_features > 0:
      if len(__A					) != num_static_categorical_features:
       raise ValueError(
           'The cardinality should be a list of the same length as `num_static_categorical_features`'					)
      __UpperCAmelCase					=   cardinality
     else:
      __UpperCAmelCase					=   [0]
     if embedding_dimension is not None and num_static_categorical_features > 0:
      if len(__A					) != num_static_categorical_features:
       raise ValueError(
           'The embedding dimension should be a list of the same length as `num_static_categorical_features`'					)
      __UpperCAmelCase					=   embedding_dimension
     else:
      __UpperCAmelCase					=   [min(50     ,			(cat + 1) // 2					) for cat in self.cardinality]
     __UpperCAmelCase					=   num_parallel_samples

     # Transformer architecture configuration
     __UpperCAmelCase					=   input_size * len(self.lags_sequence					) + self._number_of_features
     __UpperCAmelCase					=   d_model
     __UpperCAmelCase					=   encoder_attention_heads
     __UpperCAmelCase					=   decoder_attention_heads
     __UpperCAmelCase					=   encoder_ffn_dim
     __UpperCAmelCase					=   decoder_ffn_dim
     __UpperCAmelCase					=   encoder_layers
     __UpperCAmelCase					=   decoder_layers

     __UpperCAmelCase					=   dropout
     __UpperCAmelCase					=   attention_dropout
     __UpperCAmelCase					=   activation_dropout
     __UpperCAmelCase					=   encoder_layerdrop
     __UpperCAmelCase					=   decoder_layerdrop

     __UpperCAmelCase					=   activation_function
     __UpperCAmelCase					=   init_std

     __UpperCAmelCase					=   use_cache

     # Autoformer
     __UpperCAmelCase					=   label_length
     __UpperCAmelCase					=   moving_average
     __UpperCAmelCase					=   autocorrelation_factor

     super().__init__(is_encoder_decoder=__A     ,			**__A					)




    @property
    def 					__lowerCamelCase						(  self					):
     return (
         sum(self.embedding_dimension					)
         + self.num_dynamic_real_features
         + self.num_time_features
         + self.num_static_real_features
         + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
     )





