



import inspect
import unittest

from transformers import ResNetConfig, 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.resnet.modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel

if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor

class 		lowerCamelCase_			(       unittest.TestCase  ):






						'''simple docstring'''







						def __init__(		self					:	Optional[Any]		,		__lowerCamelCase					:	List[Any]		,		__lowerCamelCase					:	Optional[int]=3		,		__lowerCamelCase					:	Optional[Any]=32		,		__lowerCamelCase					:	int=3		,		__lowerCamelCase					:	Union[str, Any]=10		,		__lowerCamelCase					:	Optional[int]=[10, 20, 30, 40]		,		__lowerCamelCase					:	List[Any]=[1, 1, 2, 1]		,		__lowerCamelCase					:	Any=True		,		__lowerCamelCase					:	List[str]=True		,		__lowerCamelCase					:	Tuple="relu"		,		__lowerCamelCase					:	Tuple=3		,		__lowerCamelCase					:	List[str]=None		,		)					-> List[Any]:
									A							:    Dict								=  parent
									A							:    str								=  batch_size
									A							:    Tuple								=  image_size
									A							:    str								=  num_channels
									A							:    Dict								=  embeddings_size
									A							:    List[Any]								=  hidden_sizes
									A							:    Optional[Any]								=  depths
									A							:    Tuple								=  is_training
									A							:    Optional[int]								=  use_labels
									A							:    Optional[int]								=  hidden_act
									A							:    Optional[int]								=  num_labels
									A							:    Union[str, Any]								=  scope
									A							:    List[Any]								=  len(_SCREAMING_SNAKE_CASE       )







						def 	SCREAMING_SNAKE_CASE__						(		self					:	List[str]       )					-> int:
									A							:    Dict								=  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 	SCREAMING_SNAKE_CASE__						(		self					:	Optional[int]       )					-> Optional[Any]:
									return ResNetConfig(
									    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					:	str		,		__lowerCamelCase					:	str		,		__lowerCamelCase					:	Any       )					-> Dict:
									A							:    Union[str, Any]								=  FlaxResNetModel(config=_SCREAMING_SNAKE_CASE       )
									A							:    Optional[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 	SCREAMING_SNAKE_CASE__						(		self					:	Any		,		__lowerCamelCase					:	Union[str, Any]		,		__lowerCamelCase					:	Any       )					-> List[Any]:
									A							:    str								=  self.num_labels
									A							:    List[Any]								=  FlaxResNetForImageClassification(config=_SCREAMING_SNAKE_CASE       )
									A							:    Any								=  model(_SCREAMING_SNAKE_CASE       )
									self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.num_labels)       )







						def 	SCREAMING_SNAKE_CASE__						(		self					:	List[str]       )					-> Optional[int]:
									A							:    Tuple								=  self.prepare_config_and_inputs()
									A							:    Optional[Any]								=  config_and_inputs
									A							:    Any								=  {"pixel_values": pixel_values}
									return config, inputs_dict







@require_flax
class 		lowerCamelCase_			(       SCREAMING_SNAKE_CASE__  ,unittest.TestCase  ):






						'''simple docstring'''
						a__      =     (FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

						a__      =     False
						a__      =     False
						a__      =     False







						def 	SCREAMING_SNAKE_CASE__						(		self					:	Optional[int]       )					-> None:
									A							:    int								=  FlaxResNetModelTester(self       )
									A							:    Optional[Any]								=  ConfigTester(self		,		config_class=_SCREAMING_SNAKE_CASE		,		has_text_modality=_SCREAMING_SNAKE_CASE       )







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







						def 	SCREAMING_SNAKE_CASE__						(		self					:	str       )					-> int:
									A							:    Union[str, Any]								=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE       )







						def 	SCREAMING_SNAKE_CASE__						(		self					:	Dict       )					-> Optional[int]:
									A							:    Optional[Any]								=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE       )







						@unittest.skip(reason="ResNet does not use inputs_embeds"       )
						def 	SCREAMING_SNAKE_CASE__						(		self					:	int       )					-> Optional[int]:
									pass







						@unittest.skip(reason="ResNet does not support input and output embeddings"       )
						def 	SCREAMING_SNAKE_CASE__						(		self					:	Tuple       )					-> int:
									pass







						def 	SCREAMING_SNAKE_CASE__						(		self					:	str       )					-> Tuple:
									A							:    Optional[int]								=  self.model_tester.prepare_config_and_inputs_for_common()

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

												A							:    int								=  ["pixel_values"]
												self.assertListEqual(arg_names[:1]		,		_SCREAMING_SNAKE_CASE       )







						def 	SCREAMING_SNAKE_CASE__						(		self					:	List[str]       )					-> Tuple:
									def check_hidden_states_output(__lowerCamelCase					:	str		,		__lowerCamelCase					:	List[str]		,		__lowerCamelCase					:	Dict       ):
												A							:    Optional[int]								=  model_class(_SCREAMING_SNAKE_CASE       )

												A							:    Tuple								=  model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE		,		_SCREAMING_SNAKE_CASE       )       )

												A							:    Dict								=  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       )







						@unittest.skip(reason="ResNet does not use feedforward chunking"       )
						def 	SCREAMING_SNAKE_CASE__						(		self					:	Optional[Any]       )					-> str:
									pass







						def 	SCREAMING_SNAKE_CASE__						(		self					:	List[Any]       )					-> Tuple:
									A							:    Union[str, 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[Any]								=  self._prepare_for_class(_SCREAMING_SNAKE_CASE		,		_SCREAMING_SNAKE_CASE       )
															A							:    Dict								=  model_class(_SCREAMING_SNAKE_CASE       )

															@jax.jit
															def model_jitted(__lowerCamelCase					:	Union[str, Any]		,		**__lowerCamelCase					:	List[Any]       ):
																		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							:    List[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 		UpperCAmelCase      (  ):
			A							:    Dict								=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"			)
			return image






@require_flax
class 		lowerCamelCase_			(       unittest.TestCase  ):






						'''simple docstring'''







						@cached_property
						def 	SCREAMING_SNAKE_CASE__						(		self					:	Optional[int]       )					-> str:
									return AutoImageProcessor.from_pretrained("microsoft/resnet-50"       ) if is_vision_available() else None







						@slow
						def 	SCREAMING_SNAKE_CASE__						(		self					:	int       )					-> int:
									A							:    List[str]								=  FlaxResNetForImageClassification.from_pretrained("microsoft/resnet-50"       )

									A							:    str								=  self.default_image_processor
									A							:    int								=  prepare_img()
									A							:    Union[str, Any]								=  image_processor(images=_SCREAMING_SNAKE_CASE		,		return_tensors="np"       )

									A							:    Any								=  model(**_SCREAMING_SNAKE_CASE       )

									# verify the logits
									A							:    List[str]								=  (1, 10_00)
									self.assertEqual(outputs.logits.shape		,		_SCREAMING_SNAKE_CASE       )

									A							:    Optional[Any]								=  jnp.array([-11.1069, -9.7877, -8.3777]       )

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







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


lowercase     :							Any	     =			logging.get_logger(__name__)

lowercase     :							int	     =			{
    # pcqm4mv1 now deprecated
    '''graphormer-base''': '''https://huggingface.co/clefourrier/graphormer-base-pcqm4mv2/resolve/main/config.json''',
    # See all Graphormer models at https://huggingface.co/models?filter=graphormer
}




class 						UpperCAmelCase_  (		SCREAMING_SNAKE_CASE__    ):


						'''simple docstring'''




						A   :      Optional[int]						 =    'graphormer'
						A   :      List[str]						 =    ['past_key_values']






						def __init__(		self						, _SCREAMING_SNAKE_CASE = 1						, _SCREAMING_SNAKE_CASE = 512 * 9						, _SCREAMING_SNAKE_CASE = 512 * 3						, _SCREAMING_SNAKE_CASE = 512						, _SCREAMING_SNAKE_CASE = 512						, _SCREAMING_SNAKE_CASE = 512						, _SCREAMING_SNAKE_CASE = 128						, _SCREAMING_SNAKE_CASE = 5						, _SCREAMING_SNAKE_CASE = 1024						, _SCREAMING_SNAKE_CASE = "multi_hop"						, _SCREAMING_SNAKE_CASE = 512						, _SCREAMING_SNAKE_CASE = False						, _SCREAMING_SNAKE_CASE = 12						, _SCREAMING_SNAKE_CASE = 768						, _SCREAMING_SNAKE_CASE = 768						, _SCREAMING_SNAKE_CASE = 32						, _SCREAMING_SNAKE_CASE = 0.1						, _SCREAMING_SNAKE_CASE = 0.1						, _SCREAMING_SNAKE_CASE = 0.0						, _SCREAMING_SNAKE_CASE = False						, _SCREAMING_SNAKE_CASE = False						, _SCREAMING_SNAKE_CASE = False						, _SCREAMING_SNAKE_CASE = "gelu"						, _SCREAMING_SNAKE_CASE = None						, _SCREAMING_SNAKE_CASE = False						, _SCREAMING_SNAKE_CASE = 0						, _SCREAMING_SNAKE_CASE = False						, _SCREAMING_SNAKE_CASE = 0.0						, _SCREAMING_SNAKE_CASE = 8						, _SCREAMING_SNAKE_CASE = None						, _SCREAMING_SNAKE_CASE = None						, _SCREAMING_SNAKE_CASE = True						, _SCREAMING_SNAKE_CASE = True						, _SCREAMING_SNAKE_CASE=0						, _SCREAMING_SNAKE_CASE=1						, _SCREAMING_SNAKE_CASE=2						, **_SCREAMING_SNAKE_CASE						, )       -> List[str]:
								snake_case_ :							str         =				num_classes
								snake_case_ :							Union[str, Any]         =				num_atoms
								snake_case_ :							Dict         =				num_in_degree
								snake_case_ :							Union[str, Any]         =				num_out_degree
								snake_case_ :							List[Any]         =				num_edges
								snake_case_ :							Union[str, Any]         =				num_spatial
								snake_case_ :							int         =				num_edge_dis
								snake_case_ :							List[Any]         =				edge_type
								snake_case_ :							Any         =				multi_hop_max_dist
								snake_case_ :							Tuple         =				spatial_pos_max
								snake_case_ :							Union[str, Any]         =				max_nodes
								snake_case_ :							str         =				num_hidden_layers
								snake_case_ :							str         =				embedding_dim
								snake_case_ :							List[str]         =				embedding_dim
								snake_case_ :							Dict         =				ffn_embedding_dim
								snake_case_ :							Any         =				num_attention_heads
								snake_case_ :							List[str]         =				dropout
								snake_case_ :							List[str]         =				attention_dropout
								snake_case_ :							Optional[Any]         =				layerdrop
								snake_case_ :							Optional[int]         =				encoder_normalize_before
								snake_case_ :							Union[str, Any]         =				pre_layernorm
								snake_case_ :							int         =				apply_graphormer_init
								snake_case_ :							List[Any]         =				activation_fn
								snake_case_ :							str         =				embed_scale
								snake_case_ :							int         =				freeze_embeddings
								snake_case_ :							Optional[Any]         =				num_trans_layers_to_freeze
								snake_case_ :							int         =				share_input_output_embed
								snake_case_ :							int         =				traceable
								snake_case_ :							Tuple         =				q_noise
								snake_case_ :							Tuple         =				qn_block_size

								# These parameters are here for future extensions
								# atm, the model only supports self attention
								snake_case_ :							List[Any]         =				kdim
								snake_case_ :							Tuple         =				vdim
								snake_case_ :							Any         =				self_attention
								snake_case_ :							Any         =				bias

								super().__init__(
								    pad_token_id=_SCREAMING_SNAKE_CASE						, bos_token_id=_SCREAMING_SNAKE_CASE						, eos_token_id=_SCREAMING_SNAKE_CASE						, **_SCREAMING_SNAKE_CASE						, )
