





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  ):







					'''simple docstring'''

					def __init__(						self    :  Optional[int] ,					__lowercase    :  Optional[int] ,					__lowercase    :  List[str]=3 ,					__lowercase    :  Optional[int]=32 ,					__lowercase    :  Tuple=3 ,					__lowercase    :  Tuple=10 ,					__lowercase    :  Dict=[10, 20, 30, 40] ,					__lowercase    :  Tuple=[1, 1, 2, 1] ,					__lowercase    :  Union[str, Any]=True ,					__lowercase    :  Any=True ,					__lowercase    :  List[Any]="relu" ,					__lowercase    :  List[Any]=3 ,					__lowercase    :  List[str]=None ,					):






						'''simple docstring'''







						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(__lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  int		):






						'''simple docstring'''







						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    :  str		):






						'''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        SCREAMING_SNAKE_CASE							(						self    :  Optional[Any] ,					__lowercase    :  int ,					__lowercase    :  str		):






						'''simple docstring'''







						UpperCAmelCase_								=     FlaxRegNetModel(config=__lowercase		)
						UpperCAmelCase_								=     model(__lowercase		)

						# 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    :  Optional[int] ,					__lowercase    :  List[str] ,					__lowercase    :  List[str]		):






						'''simple docstring'''







						UpperCAmelCase_								=     self.num_labels
						UpperCAmelCase_								=     FlaxRegNetForImageClassification(config=__lowercase		)
						UpperCAmelCase_								=     model(__lowercase		)
						self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_labels)		)





					def        SCREAMING_SNAKE_CASE							(						self    :  Tuple		):






						'''simple docstring'''







						UpperCAmelCase_								=     self.prepare_config_and_inputs()
						UpperCAmelCase_						,	UpperCAmelCase_								=     config_and_inputs
						UpperCAmelCase_								=     {"""pixel_values""": pixel_values}
						return config, inputs_dict







@require_flax
class   _UpperCamelCase  (       A_						,       unittest.TestCase  ):







					'''simple docstring'''

					lowerCamelCase     :     str			    =       (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

					lowerCamelCase     :     Dict			    =       False
					lowerCamelCase     :     Any			    =       False
					lowerCamelCase     :     Union[str, Any]			    =       False





					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any]		):






						'''simple docstring'''







						UpperCAmelCase_								=     FlaxRegNetModelTester(self		)
						UpperCAmelCase_								=     ConfigTester(self ,					config_class=__lowercase ,					has_text_modality=__lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  Tuple		):






						'''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        SCREAMING_SNAKE_CASE							(						self    :  Any		):






						'''simple docstring'''







						return





					def        SCREAMING_SNAKE_CASE							(						self    :  Optional[Any]		):






						'''simple docstring'''







						UpperCAmelCase_								=     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  Dict		):






						'''simple docstring'''







						UpperCAmelCase_								=     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*__lowercase		)





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






						'''simple docstring'''







						pass





					@unittest.skip(reason="""RegNet does not support input and output embeddings"""		)
					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any]		):






						'''simple docstring'''







						pass





					def        SCREAMING_SNAKE_CASE							(						self    :  Dict		):






						'''simple docstring'''







						UpperCAmelCase_						,	UpperCAmelCase_								=     self.model_tester.prepare_config_and_inputs_for_common()

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





					def        SCREAMING_SNAKE_CASE							(						self    :  Dict		):






						'''simple docstring'''







						def check_hidden_states_output(__lowercase    :  Dict ,					__lowercase    :  List[str] ,					__lowercase    :  List[Any]		):
							UpperCAmelCase_								=     model_class(__lowercase		)

							UpperCAmelCase_								=     model(**self._prepare_for_class(__lowercase ,					__lowercase		)		)

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

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

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

							check_hidden_states_output(__lowercase ,					__lowercase ,					__lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any]		):






						'''simple docstring'''







						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(__lowercase ,					__lowercase		)
								UpperCAmelCase_								=     model_class(__lowercase		)

								@jax.jit
								def model_jitted(__lowercase    :  Tuple ,					**__lowercase    :  List[Any]		):
									return model(pixel_values=__lowercase ,					**__lowercase		)

								with self.subTest("""JIT Enabled"""		):
									UpperCAmelCase_								=     model_jitted(**__lowercase		).to_tuple()

								with self.subTest("""JIT Disabled"""		):
									with jax.disable_jit():
										UpperCAmelCase_								=     model_jitted(**__lowercase		).to_tuple()

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




def    A_(			):
	UpperCAmelCase_								=     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""   )
	return image







@require_flax
class   _UpperCamelCase  (       unittest.TestCase  ):







					'''simple docstring'''

					@cached_property
					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any]		):






						'''simple docstring'''







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





					@slow
					def        SCREAMING_SNAKE_CASE							(						self    :  Union[str, Any]		):






						'''simple docstring'''







						UpperCAmelCase_								=     FlaxRegNetForImageClassification.from_pretrained("""facebook/regnet-y-040"""		)

						UpperCAmelCase_								=     self.default_image_processor
						UpperCAmelCase_								=     prepare_img()
						UpperCAmelCase_								=     image_processor(images=__lowercase ,					return_tensors="""np"""		)

						UpperCAmelCase_								=     model(**__lowercase		)

						# verify the logits
						UpperCAmelCase_								=     (1, 10_00)
						self.assertEqual(outputs.logits.shape ,					__lowercase		)

						UpperCAmelCase_								=     jnp.array([-0.4180, -1.5051, -3.4836]		)

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









from __future__ import annotations

import unittest

from transformers import is_tf_available, is_torch_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow


if is_tf_available():
				from transformers import (
				    AutoConfig,
				    BertConfig,
				    GPTaConfig,
				    TaConfig,
				    TFAutoModel,
				    TFAutoModelForCausalLM,
				    TFAutoModelForMaskedLM,
				    TFAutoModelForPreTraining,
				    TFAutoModelForQuestionAnswering,
				    TFAutoModelForSeqaSeqLM,
				    TFAutoModelForSequenceClassification,
				    TFAutoModelWithLMHead,
				    TFBertForMaskedLM,
				    TFBertForPreTraining,
				    TFBertForQuestionAnswering,
				    TFBertForSequenceClassification,
				    TFBertModel,
				    TFGPTaLMHeadModel,
				    TFRobertaForMaskedLM,
				    TFTaForConditionalGeneration,
				)
				from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
				from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
				from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST

if is_torch_available():
				from transformers import (
				    AutoModel,
				    AutoModelForCausalLM,
				    AutoModelForMaskedLM,
				    AutoModelForPreTraining,
				    AutoModelForQuestionAnswering,
				    AutoModelForSeqaSeqLM,
				    AutoModelForSequenceClassification,
				    AutoModelWithLMHead,
				    BertForMaskedLM,
				    BertForPreTraining,
				    BertForQuestionAnswering,
				    BertForSequenceClassification,
				    BertModel,
				    GPTaLMHeadModel,
				    RobertaForMaskedLM,
				    TaForConditionalGeneration,
				)







@is_pt_tf_cross_test
class   _UpperCamelCase  (       unittest.TestCase  ):







					'''simple docstring'''

					@slow
					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any]		):






						'''simple docstring'''







						for model_name in ["bert-base-uncased"]:
							UpperCAmelCase_								=     AutoConfig.from_pretrained(__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     TFAutoModel.from_pretrained(__lowercase ,					from_pt=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     AutoModel.from_pretrained(__lowercase ,					from_tf=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)





					@slow
					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any]		):






						'''simple docstring'''







						for model_name in ["bert-base-uncased"]:
							UpperCAmelCase_								=     AutoConfig.from_pretrained(__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     TFAutoModelForPreTraining.from_pretrained(__lowercase ,					from_pt=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     AutoModelForPreTraining.from_pretrained(__lowercase ,					from_tf=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)





					@slow
					def        SCREAMING_SNAKE_CASE							(						self    :  Any		):






						'''simple docstring'''







						for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase_								=     AutoConfig.from_pretrained(__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     TFAutoModelForCausalLM.from_pretrained(__lowercase ,					from_pt=__lowercase		)
							UpperCAmelCase_						,	UpperCAmelCase_								=     TFAutoModelForCausalLM.from_pretrained(
							    __lowercase ,					output_loading_info=__lowercase ,					from_pt=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     AutoModelForCausalLM.from_pretrained(__lowercase ,					from_tf=__lowercase		)
							UpperCAmelCase_						,	UpperCAmelCase_								=     AutoModelForCausalLM.from_pretrained(
							    __lowercase ,					output_loading_info=__lowercase ,					from_tf=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)





					@slow
					def        SCREAMING_SNAKE_CASE							(						self    :  Optional[Any]		):






						'''simple docstring'''







						for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase_								=     AutoConfig.from_pretrained(__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     TFAutoModelWithLMHead.from_pretrained(__lowercase ,					from_pt=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     AutoModelWithLMHead.from_pretrained(__lowercase ,					from_tf=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)





					@slow
					def        SCREAMING_SNAKE_CASE							(						self    :  str		):






						'''simple docstring'''







						for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase_								=     AutoConfig.from_pretrained(__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     TFAutoModelForMaskedLM.from_pretrained(__lowercase ,					from_pt=__lowercase		)
							UpperCAmelCase_						,	UpperCAmelCase_								=     TFAutoModelForMaskedLM.from_pretrained(
							    __lowercase ,					output_loading_info=__lowercase ,					from_pt=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     AutoModelForMaskedLM.from_pretrained(__lowercase ,					from_tf=__lowercase		)
							UpperCAmelCase_						,	UpperCAmelCase_								=     AutoModelForMaskedLM.from_pretrained(
							    __lowercase ,					output_loading_info=__lowercase ,					from_tf=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)





					@slow
					def        SCREAMING_SNAKE_CASE							(						self    :  Union[str, Any]		):






						'''simple docstring'''







						for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase_								=     AutoConfig.from_pretrained(__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     TFAutoModelForSeqaSeqLM.from_pretrained(__lowercase ,					from_pt=__lowercase		)
							UpperCAmelCase_						,	UpperCAmelCase_								=     TFAutoModelForSeqaSeqLM.from_pretrained(
							    __lowercase ,					output_loading_info=__lowercase ,					from_pt=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     AutoModelForSeqaSeqLM.from_pretrained(__lowercase ,					from_tf=__lowercase		)
							UpperCAmelCase_						,	UpperCAmelCase_								=     AutoModelForSeqaSeqLM.from_pretrained(
							    __lowercase ,					output_loading_info=__lowercase ,					from_tf=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)





					@slow
					def        SCREAMING_SNAKE_CASE							(						self    :  Optional[int]		):






						'''simple docstring'''







						for model_name in ["bert-base-uncased"]:
							UpperCAmelCase_								=     AutoConfig.from_pretrained(__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     TFAutoModelForSequenceClassification.from_pretrained(__lowercase ,					from_pt=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     AutoModelForSequenceClassification.from_pretrained(__lowercase ,					from_tf=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)





					@slow
					def        SCREAMING_SNAKE_CASE							(						self    :  str		):






						'''simple docstring'''







						for model_name in ["bert-base-uncased"]:
							UpperCAmelCase_								=     AutoConfig.from_pretrained(__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     TFAutoModelForQuestionAnswering.from_pretrained(__lowercase ,					from_pt=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)

							UpperCAmelCase_								=     AutoModelForQuestionAnswering.from_pretrained(__lowercase ,					from_tf=__lowercase		)
							self.assertIsNotNone(__lowercase		)
							self.assertIsInstance(__lowercase ,					__lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  Tuple		):






						'''simple docstring'''







						UpperCAmelCase_								=     TFAutoModelWithLMHead.from_pretrained(__lowercase ,					from_pt=__lowercase		)
						self.assertIsInstance(__lowercase ,					__lowercase		)
						self.assertEqual(model.num_parameters() ,					1_44_10		)
						self.assertEqual(model.num_parameters(only_trainable=__lowercase		) ,					1_44_10		)

						UpperCAmelCase_								=     AutoModelWithLMHead.from_pretrained(__lowercase ,					from_tf=__lowercase		)
						self.assertIsInstance(__lowercase ,					__lowercase		)
						self.assertEqual(model.num_parameters() ,					1_44_10		)
						self.assertEqual(model.num_parameters(only_trainable=__lowercase		) ,					1_44_10		)





					def        SCREAMING_SNAKE_CASE							(						self    :  Optional[Any]		):






						'''simple docstring'''







						UpperCAmelCase_								=     TFAutoModelWithLMHead.from_pretrained(__lowercase ,					from_pt=__lowercase		)
						self.assertIsInstance(__lowercase ,					__lowercase		)
						self.assertEqual(model.num_parameters() ,					1_44_10		)
						self.assertEqual(model.num_parameters(only_trainable=__lowercase		) ,					1_44_10		)

						UpperCAmelCase_								=     AutoModelWithLMHead.from_pretrained(__lowercase ,					from_tf=__lowercase		)
						self.assertIsInstance(__lowercase ,					__lowercase		)
						self.assertEqual(model.num_parameters() ,					1_44_10		)
						self.assertEqual(model.num_parameters(only_trainable=__lowercase		) ,					1_44_10		)


