
"""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 			lowercase					( unittest.TestCase		):






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





				"""simple docstring"""


				lowerCAmelCase												=  parent
				lowerCAmelCase												=  batch_size
				lowerCAmelCase												=  image_size
				lowerCAmelCase												=  num_channels
				lowerCAmelCase												=  embeddings_size
				lowerCAmelCase												=  hidden_sizes
				lowerCAmelCase												=  depths
				lowerCAmelCase												=  is_training
				lowerCAmelCase												=  use_labels
				lowerCAmelCase												=  hidden_act
				lowerCAmelCase												=  num_labels
				lowerCAmelCase												=  scope
				lowerCAmelCase												=  len(SCREAMING_SNAKE_CASE_			)






			def 					UpperCAmelCase							(self  :	int			)       ->	Dict:





				"""simple docstring"""


				lowerCAmelCase												=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

				lowerCAmelCase												=  self.get_config()

				return config, pixel_values






			def 					UpperCAmelCase							(self  :	Optional[int]			)       ->	Optional[int]:





				"""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 					UpperCAmelCase							(self  :	Any      ,SCREAMING_SNAKE_CASE_  :	Dict      ,SCREAMING_SNAKE_CASE_  :	Dict			)       ->	List[str]:





				"""simple docstring"""


				lowerCAmelCase												=  FlaxRegNetModel(config=SCREAMING_SNAKE_CASE_			)
				lowerCAmelCase												=  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 					UpperCAmelCase							(self  :	Any      ,SCREAMING_SNAKE_CASE_  :	str      ,SCREAMING_SNAKE_CASE_  :	str			)       ->	str:





				"""simple docstring"""


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






			def 					UpperCAmelCase							(self  :	Any			)       ->	Optional[int]:





				"""simple docstring"""


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





@require_flax
class 			lowercase					( lowercase__   ,unittest.TestCase		):
			lowercase        =     (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

			lowercase        =     False
			lowercase        =     False
			lowercase        =     False






			def 					UpperCAmelCase							(self  :	int			)       ->	None:





				"""simple docstring"""


				lowerCAmelCase												=  FlaxRegNetModelTester(self			)
				lowerCAmelCase												=  ConfigTester(self      ,config_class=SCREAMING_SNAKE_CASE_      ,has_text_modality=SCREAMING_SNAKE_CASE_			)






			def 					UpperCAmelCase							(self  :	Any			)       ->	Any:





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





				"""simple docstring"""


				return






			def 					UpperCAmelCase							(self  :	int			)       ->	str:





				"""simple docstring"""


				lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_			)






			def 					UpperCAmelCase							(self  :	Tuple			)       ->	str:





				"""simple docstring"""


				lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_			)






			@unittest.skip(reason='''RegNet does not use inputs_embeds'''			)
			def 					UpperCAmelCase							(self  :	Optional[Any]			)       ->	Tuple:





				"""simple docstring"""


				pass






			@unittest.skip(reason='''RegNet does not support input and output embeddings'''			)
			def 					UpperCAmelCase							(self  :	Optional[int]			)       ->	Any:





				"""simple docstring"""


				pass






			def 					UpperCAmelCase							(self  :	Union[str, Any]			)       ->	Any:





				"""simple docstring"""


				lowerCAmelCase							,       lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs_for_common()

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

					lowerCAmelCase												=  ['''pixel_values''']
					self.assertListEqual(arg_names[:1]      ,SCREAMING_SNAKE_CASE_			)






			def 					UpperCAmelCase							(self  :	Optional[int]			)       ->	Dict:





				"""simple docstring"""


				def check_hidden_states_output(SCREAMING_SNAKE_CASE_  :	Optional[int]      ,SCREAMING_SNAKE_CASE_  :	List[Any]      ,SCREAMING_SNAKE_CASE_  :	Union[str, Any]			):
					lowerCAmelCase												=  model_class(SCREAMING_SNAKE_CASE_			)

					lowerCAmelCase												=  model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)			)

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

					lowerCAmelCase												=  self.model_tester.num_stages
					self.assertEqual(len(SCREAMING_SNAKE_CASE_			)      ,expected_num_stages + 1			)

				lowerCAmelCase							,       lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					lowerCAmelCase												=  True
					check_hidden_states_output(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)

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

					check_hidden_states_output(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)






			def 					UpperCAmelCase							(self  :	List[Any]			)       ->	List[str]:





				"""simple docstring"""


				lowerCAmelCase							,       lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					with self.subTest(model_class.__name__			):
						lowerCAmelCase												=  self._prepare_for_class(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)
						lowerCAmelCase												=  model_class(SCREAMING_SNAKE_CASE_			)

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

						with self.subTest('''JIT Enabled'''			):
							lowerCAmelCase												=  model_jitted(**SCREAMING_SNAKE_CASE_			).to_tuple()

						with self.subTest('''JIT Disabled'''			):
							with jax.disable_jit():
								lowerCAmelCase												=  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 __magic_name__	(				)					->						Tuple:

	'''simple docstring'''




	lowerCAmelCase												=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
	return image



@require_flax
class 			lowercase					( unittest.TestCase		):






			@cached_property
			def 					UpperCAmelCase							(self  :	str			)       ->	Optional[int]:





				"""simple docstring"""


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






			@slow
			def 					UpperCAmelCase							(self  :	Optional[int]			)       ->	Dict:





				"""simple docstring"""


				lowerCAmelCase												=  FlaxRegNetForImageClassification.from_pretrained('''facebook/regnet-y-040'''			)

				lowerCAmelCase												=  self.default_image_processor
				lowerCAmelCase												=  prepare_img()
				lowerCAmelCase												=  image_processor(images=SCREAMING_SNAKE_CASE_      ,return_tensors='''np'''			)

				lowerCAmelCase												=  model(**SCREAMING_SNAKE_CASE_			)

				# verify the logits
				lowerCAmelCase												=  (1, 1_000)
				self.assertEqual(outputs.logits.shape      ,SCREAMING_SNAKE_CASE_			)

				lowerCAmelCase												=  jnp.array([-0.41_80, -1.50_51, -3.48_36]			)

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




"""simple docstring"""






class 			lowercase					:






			def __init__(self  :	Dict			)       ->	List[Any]:





				"""simple docstring"""


				lowerCAmelCase												=  {}






			def 					UpperCAmelCase							(self  :	Union[str, Any]			)       ->	None:





				"""simple docstring"""


				print(self.vertex			)
				for i in self.vertex:
					print(SCREAMING_SNAKE_CASE_      ,''' -> '''      ,''' -> '''.join([str(SCREAMING_SNAKE_CASE_			) for j in self.vertex[i]]			)			)






			def 					UpperCAmelCase							(self  :	Optional[Any]      ,SCREAMING_SNAKE_CASE_  :	int      ,SCREAMING_SNAKE_CASE_  :	int			)       ->	None:





				"""simple docstring"""


				if from_vertex in self.vertex:
					self.vertex[from_vertex].append(SCREAMING_SNAKE_CASE_			)
				else:
					# else make a new vertex
					lowerCAmelCase												=  [to_vertex]






			def 					UpperCAmelCase							(self  :	List[str]			)       ->	None:





				"""simple docstring"""


				lowerCAmelCase												=  [False] * len(self.vertex			)

				# call the recursive helper function
				for i in range(len(self.vertex			)			):
					if not visited[i]:
						self.dfs_recursive(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)






			def 					UpperCAmelCase							(self  :	Optional[Any]      ,SCREAMING_SNAKE_CASE_  :	int      ,SCREAMING_SNAKE_CASE_  :	list			)       ->	None:





				"""simple docstring"""


				lowerCAmelCase												=  True

				print(SCREAMING_SNAKE_CASE_      ,end=''' '''			)

				# Recur for all the vertices that are adjacent to this node
				for i in self.vertex:
					if not visited[i]:
						self.dfs_recursive(SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_			)


if __name__ == "__main__":
					UpperCAmelCase        =				Graph()
					g.add_edge(0, 1)
					g.add_edge(0, 2)
					g.add_edge(1, 2)
					g.add_edge(2, 0)
					g.add_edge(2, 3)
					g.add_edge(3, 3)

					g.print_graph()
					print("""DFS:""")
					g.dfs()

					# OUTPUT:
					# 0  ->  1 -> 2
					# 1  ->  2
					# 2  ->  0 -> 3
					# 3  ->  3
					# DFS:
					#  0 1 2 3


