




"""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   :     List[Any]   ,	UpperCamelCase_   :     Any   ,	UpperCamelCase_   :     Tuple=3   ,	UpperCamelCase_   :     Optional[int]=32   ,	UpperCamelCase_   :     Dict=3   ,	UpperCamelCase_   :     List[str]=10   ,	UpperCamelCase_   :     str=[10, 20, 30, 40]   ,	UpperCamelCase_   :     Tuple=[1, 1, 2, 1]   ,	UpperCamelCase_   :     str=True   ,	UpperCamelCase_   :     Optional[int]=True   ,	UpperCamelCase_   :     Dict="relu"   ,	UpperCamelCase_   :     str=3   ,	UpperCamelCase_   :     int=None   ,	):


						"""simple docstring"""




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


			def    a_  (   self   :     Any):


						"""simple docstring"""




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

						__UpperCAmelCase						:			Dict												=       self.get_config()

						return config, pixel_values


			def    a_  (   self   :     Dict):


						"""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    a_  (   self   :     Any   ,	UpperCamelCase_   :     Union[str, Any]   ,	UpperCamelCase_   :     Union[str, Any]):


						"""simple docstring"""




						__UpperCAmelCase						:			List[str]												=       FlaxRegNetModel(config=UpperCamelCase_)
						__UpperCAmelCase						:			Dict												=       model(UpperCamelCase_)

						# 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   :     Any   ,	UpperCamelCase_   :     Dict   ,	UpperCamelCase_   :     Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase						:			List[Any]												=       self.num_labels
						__UpperCAmelCase						:			Tuple												=       FlaxRegNetForImageClassification(config=UpperCamelCase_)
						__UpperCAmelCase						:			str												=       model(UpperCamelCase_)
						self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.num_labels))


			def    a_  (   self   :     Optional[Any]):


						"""simple docstring"""




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






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

			lowercase_		=						False
			lowercase_		=						False
			lowercase_		=						False


			def    a_  (   self   :     Tuple):


						"""simple docstring"""




						__UpperCAmelCase						:			Tuple												=       FlaxRegNetModelTester(self)
						__UpperCAmelCase						:			str												=       ConfigTester(self   ,	config_class=UpperCamelCase_   ,	has_text_modality=UpperCamelCase_)


			def    a_  (   self   :     Dict):


						"""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    a_  (   self   :     Tuple):


						"""simple docstring"""




						return


			def    a_  (   self   :     Optional[Any]):


						"""simple docstring"""




						__UpperCAmelCase						:			Tuple												=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCamelCase_)


			def    a_  (   self   :     Union[str, Any]):


						"""simple docstring"""




						__UpperCAmelCase						:			Dict												=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_)


			@unittest.skip(reason="RegNet does not use inputs_embeds")
			def    a_  (   self   :     Union[str, Any]):


						"""simple docstring"""




						pass


			@unittest.skip(reason="RegNet does not support input and output embeddings")
			def    a_  (   self   :     Optional[int]):


						"""simple docstring"""




						pass


			def    a_  (   self   :     str):


						"""simple docstring"""




						__UpperCAmelCase						,  __UpperCAmelCase						:			Tuple												=       self.model_tester.prepare_config_and_inputs_for_common()

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

									__UpperCAmelCase						:			Dict												=       ["pixel_values"]
									self.assertListEqual(arg_names[:1]   ,	UpperCamelCase_)


			def    a_  (   self   :     int):


						"""simple docstring"""




						def check_hidden_states_output(UpperCamelCase_   :     Optional[Any]   ,	UpperCamelCase_   :     Tuple   ,	UpperCamelCase_   :     Union[str, Any]):
									__UpperCAmelCase						:			Union[str, Any]												=       model_class(UpperCamelCase_)

									__UpperCAmelCase						:			Optional[Any]												=       model(**self._prepare_for_class(UpperCamelCase_   ,	UpperCamelCase_))

									__UpperCAmelCase						:			List[str]												=       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

									__UpperCAmelCase						:			str												=       self.model_tester.num_stages
									self.assertEqual(len(UpperCamelCase_)   ,	expected_num_stages + 1)

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

						for model_class in self.all_model_classes:
									__UpperCAmelCase						:			List[str]												=       True
									check_hidden_states_output(UpperCamelCase_   ,	UpperCamelCase_   ,	UpperCamelCase_)

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

									check_hidden_states_output(UpperCamelCase_   ,	UpperCamelCase_   ,	UpperCamelCase_)


			def    a_  (   self   :     Tuple):


						"""simple docstring"""




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

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

												@jax.jit
												def model_jitted(UpperCamelCase_   :     int   ,	**UpperCamelCase_   :     Optional[int]):
															return model(pixel_values=UpperCamelCase_   ,	**UpperCamelCase_)

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

												with self.subTest("JIT Disabled"):
															with jax.disable_jit():
																		__UpperCAmelCase						:			Dict												=       model_jitted(**UpperCamelCase_).to_tuple()

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






def        _UpperCamelCase     (      )      ->      Any:


			"""simple docstring"""
			__UpperCAmelCase						:			Optional[Any]												=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"				)
			return image





@require_flax
class   a__					(						unittest.TestCase ):


			@cached_property
			def    a_  (   self   :     Optional[int]):


						"""simple docstring"""




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


			@slow
			def    a_  (   self   :     int):


						"""simple docstring"""




						__UpperCAmelCase						:			Any												=       FlaxRegNetForImageClassification.from_pretrained("facebook/regnet-y-040")

						__UpperCAmelCase						:			Dict												=       self.default_image_processor
						__UpperCAmelCase						:			str												=       prepare_img()
						__UpperCAmelCase						:			int												=       image_processor(images=UpperCamelCase_   ,	return_tensors="np")

						__UpperCAmelCase						:			Dict												=       model(**UpperCamelCase_)

						# verify the logits
						__UpperCAmelCase						:			Dict												=       (1, 1000)
						self.assertEqual(outputs.logits.shape   ,	UpperCamelCase_)

						__UpperCAmelCase						:			Any												=       jnp.array([-0.4180, -1.5051, -3.4836])

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








from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


if TYPE_CHECKING:
     from ... import FeatureExtractionMixin, TensorType

__a							     =					logging.get_logger(__name__)

__a							     =					{
    'openai/imagegpt-small': '',
    'openai/imagegpt-medium': '',
    'openai/imagegpt-large': '',
}

class     __a(	_a  ):






    """simple docstring"""





    lowerCAmelCase         = '''imagegpt'''
    lowerCAmelCase         = ['''past_key_values''']
    lowerCAmelCase         = {
        '''hidden_size''': '''n_embd''',
        '''max_position_embeddings''': '''n_positions''',
        '''num_attention_heads''': '''n_head''',
        '''num_hidden_layers''': '''n_layer''',
    }





    def __init__( self  ,_SCREAMING_SNAKE_CASE=512 + 1  ,_SCREAMING_SNAKE_CASE=32 * 32  ,_SCREAMING_SNAKE_CASE=512  ,_SCREAMING_SNAKE_CASE=24  ,_SCREAMING_SNAKE_CASE=8  ,_SCREAMING_SNAKE_CASE=None  ,_SCREAMING_SNAKE_CASE="quick_gelu"  ,_SCREAMING_SNAKE_CASE=0.1  ,_SCREAMING_SNAKE_CASE=0.1  ,_SCREAMING_SNAKE_CASE=0.1  ,_SCREAMING_SNAKE_CASE=1e-5  ,_SCREAMING_SNAKE_CASE=0.02  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=False  ,_SCREAMING_SNAKE_CASE=False  ,_SCREAMING_SNAKE_CASE=False  ,**_SCREAMING_SNAKE_CASE  ,)					->						Optional[int]:
         UpperCAmelCase_     :				Optional[int]  =     vocab_size
         UpperCAmelCase_     :				Union[str, Any]  =     n_positions
         UpperCAmelCase_     :				Union[str, Any]  =     n_embd
         UpperCAmelCase_     :				Any  =     n_layer
         UpperCAmelCase_     :				Optional[Any]  =     n_head
         UpperCAmelCase_     :				Union[str, Any]  =     n_inner
         UpperCAmelCase_     :				List[Any]  =     activation_function
         UpperCAmelCase_     :				List[str]  =     resid_pdrop
         UpperCAmelCase_     :				str  =     embd_pdrop
         UpperCAmelCase_     :				Optional[Any]  =     attn_pdrop
         UpperCAmelCase_     :				Dict  =     layer_norm_epsilon
         UpperCAmelCase_     :				Union[str, Any]  =     initializer_range
         UpperCAmelCase_     :				Dict  =     scale_attn_weights
         UpperCAmelCase_     :				Any  =     use_cache
         UpperCAmelCase_     :				List[str]  =     scale_attn_by_inverse_layer_idx
         UpperCAmelCase_     :				Tuple  =     reorder_and_upcast_attn
         UpperCAmelCase_     :				int  =     tie_word_embeddings

         super().__init__(tie_word_embeddings=_SCREAMING_SNAKE_CASE  ,**_SCREAMING_SNAKE_CASE    )


class     __a(	_a  ):






    """simple docstring"""





    @property
    def 				a__ ( self    )					->						Mapping[str, Mapping[int, str]]:
         return OrderedDict(
             [
                 ('''input_ids''', {0: '''batch''', 1: '''sequence'''}),
             ]    )





    def 				a__ ( self  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE = 1  ,_SCREAMING_SNAKE_CASE = -1  ,_SCREAMING_SNAKE_CASE = False  ,_SCREAMING_SNAKE_CASE = None  ,_SCREAMING_SNAKE_CASE = 3  ,_SCREAMING_SNAKE_CASE = 32  ,_SCREAMING_SNAKE_CASE = 32  ,)					->						Mapping[str, Any]:
         UpperCAmelCase_     :				Any  =     self._generate_dummy_images(_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE    )
         UpperCAmelCase_     :				Optional[int]  =     dict(preprocessor(images=_SCREAMING_SNAKE_CASE  ,return_tensors=_SCREAMING_SNAKE_CASE    )    )

         return inputs