




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     snake_case__							(unittest.TestCase   ):
				"""simple docstring"""



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





				def 							__UpperCAmelCase							(      self				:     List[str]							)					->     Dict:
							a    =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

							a    =			self.get_config()

							return config, pixel_values





				def 							__UpperCAmelCase							(      self				:     int							)					->     Tuple:
							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				:     List[str]					,      __lowerCamelCase				:     Union[str, Any]					,      __lowerCamelCase				:     Any							)					->     Dict:
							a    =			FlaxRegNetModel(config=__lowerCamelCase							)
							a    =			model(__lowerCamelCase							)

							# 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				:     Tuple					,      __lowerCamelCase				:     Tuple					,      __lowerCamelCase				:     List[Any]							)					->     Optional[int]:
							a    =			self.num_labels
							a    =			FlaxRegNetForImageClassification(config=__lowerCamelCase							)
							a    =			model(__lowerCamelCase							)
							self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_labels)							)





				def 							__UpperCAmelCase							(      self				:     int							)					->     List[Any]:
							a    =			self.prepare_config_and_inputs()
							a    ,		a    =			config_and_inputs
							a    =			{"pixel_values": pixel_values}
							return config, inputs_dict




@require_flax
class     snake_case__							(_UpperCamelCase    ,       unittest.TestCase   ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_       :							Tuple							 =					(FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

				SCREAMING_SNAKE_CASE_       :							str							 =					False
				SCREAMING_SNAKE_CASE_       :							Optional[Any]							 =					False
				SCREAMING_SNAKE_CASE_       :							List[str]							 =					False





				def 							__UpperCAmelCase							(      self				:     Any							)					->     None:
							a    =			FlaxRegNetModelTester(self							)
							a    =			ConfigTester(self					,      config_class=__lowerCamelCase					,      has_text_modality=__lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     int							)					->     Dict:
							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				:     str							)					->     Optional[Any]:
							return





				def 							__UpperCAmelCase							(      self				:     Tuple							)					->     List[Any]:
							a    =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     List[Any]							)					->     Tuple:
							a    =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase							)





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





				@unittest.skip(reason="RegNet does not support input and output embeddings"							)
				def 							__UpperCAmelCase							(      self				:     Tuple							)					->     Dict:
							pass





				def 							__UpperCAmelCase							(      self				:     Dict							)					->     Optional[int]:
							a    ,		a    =			self.model_tester.prepare_config_and_inputs_for_common()

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

										a    =			["pixel_values"]
										self.assertListEqual(arg_names[:1]					,      __lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     Dict							)					->     Tuple:
							def check_hidden_states_output(__lowerCamelCase				:     Dict					,      __lowerCamelCase				:     List[str]					,      __lowerCamelCase				:     List[str]							):
										a    =			model_class(__lowerCamelCase							)

										a    =			model(**self._prepare_for_class(__lowerCamelCase					,      __lowerCamelCase							)							)

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

										a    =			self.model_tester.num_stages
										self.assertEqual(len(__lowerCamelCase							)					,      expected_num_stages + 1							)

							a    ,		a    =			self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										a    =			True
										check_hidden_states_output(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase							)

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

										check_hidden_states_output(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase							)





				def 							__UpperCAmelCase							(      self				:     Dict							)					->     str:
							a    ,		a    =			self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										with self.subTest(model_class.__name__							):
													a    =			self._prepare_for_class(__lowerCamelCase					,      __lowerCamelCase							)
													a    =			model_class(__lowerCamelCase							)

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

													with self.subTest("JIT Enabled"							):
																a    =			model_jitted(**__lowerCamelCase							).to_tuple()

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

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




def 			__magic_name__				( ):






			'''simple docstring'''







			a    =			Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"   )
			return image






@require_flax
class     snake_case__							(unittest.TestCase   ):
				"""simple docstring"""



				@cached_property
				def 							__UpperCAmelCase							(      self				:     Any							)					->     List[str]:
							return AutoImageProcessor.from_pretrained("facebook/regnet-y-040"							) if is_vision_available() else None





				@slow
				def 							__UpperCAmelCase							(      self				:     List[Any]							)					->     List[str]:
							a    =			FlaxRegNetForImageClassification.from_pretrained("facebook/regnet-y-040"							)

							a    =			self.default_image_processor
							a    =			prepare_img()
							a    =			image_processor(images=__lowerCamelCase					,      return_tensors="np"							)

							a    =			model(**__lowerCamelCase							)

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

							a    =			jnp.array([-0.4_180, -1.5_051, -3.4_836]							)

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













a__:      Dict									=		{0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []}

a__:      str									=		{0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]}


def 	UpperCamelCase__(       UpperCamelCase__			:       dict[int, list[int]] ,     UpperCamelCase__			:       int ,     UpperCamelCase__			:       list[bool]  )->list[int]:
       A__											=						True
       A__											=						[]

       for neighbour in graph[vert]:
              if not visited[neighbour]:
                     order += topology_sort(UpperCamelCase__ ,     UpperCamelCase__ ,     UpperCamelCase__  )

       order.append(UpperCamelCase__  )

       return order


def 	UpperCamelCase__(       UpperCamelCase__			:       dict[int, list[int]] ,     UpperCamelCase__			:       int ,     UpperCamelCase__			:       list[bool]  )->list[int]:
       A__											=						True
       A__											=						[vert]

       for neighbour in reversed_graph[vert]:
              if not visited[neighbour]:
                     component += find_components(UpperCamelCase__ ,     UpperCamelCase__ ,     UpperCamelCase__  )

       return component


def 	UpperCamelCase__(       UpperCamelCase__			:       dict[int, list[int]]  )->list[list[int]]:
       A__											=						len(UpperCamelCase__  ) * [False]
       A__											=						{vert: [] for vert in range(len(UpperCamelCase__  )  )}

       for vert, neighbours in graph.items():
              for neighbour in neighbours:
                     reversed_graph[neighbour].append(UpperCamelCase__  )

       A__											=						[]
       for i, was_visited in enumerate(UpperCamelCase__  ):
              if not was_visited:
                     order += topology_sort(UpperCamelCase__ ,     UpperCamelCase__ ,     UpperCamelCase__  )

       A__											=						[]
       A__											=						len(UpperCamelCase__  ) * [False]

       for i in range(len(UpperCamelCase__  )  ):
              A__											=						order[len(UpperCamelCase__  ) - i - 1]
              if not visited[vert]:
                     A__											=						find_components(UpperCamelCase__ ,     UpperCamelCase__ ,     UpperCamelCase__  )
                     components_list.append(UpperCamelCase__  )

       return components_list





