


'''simple docstring'''


import os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
				from deepspeed import DeepSpeedEngine

if is_tpu_available(check_device=False):
				import torch_xla.core.xla_model as xm







def 			snake_case_   (  SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				if is_torch_version("""<"""							,      """2.0.0"""     ) or not hasattr(SCREAMING_SNAKE_CASE__							,      """_dynamo"""     ):
								return False
				return isinstance(SCREAMING_SNAKE_CASE__							,      torch._dynamo.eval_frame.OptimizedModule     )







def 			snake_case_   (  SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__ = True     ):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE :       Any     		=      (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

				_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      is_compiled_module(SCREAMING_SNAKE_CASE__     )
				if is_compiled:
								_SCREAMING_SNAKE_CASE :       Tuple     		=      model
								_SCREAMING_SNAKE_CASE :       Optional[int]     		=      model._orig_mod

				if is_deepspeed_available():
								options += (DeepSpeedEngine,)

				while isinstance(SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     ):
								_SCREAMING_SNAKE_CASE :       Tuple     		=      model.module

				if not keep_fpaa_wrapper:
								_SCREAMING_SNAKE_CASE :       Any     		=      getattr(SCREAMING_SNAKE_CASE__							,      """forward"""     )
								_SCREAMING_SNAKE_CASE :       Tuple     		=      model.__dict__.pop("""_original_forward"""							,      SCREAMING_SNAKE_CASE__     )
								if original_forward is not None:
												while hasattr(SCREAMING_SNAKE_CASE__							,      """__wrapped__"""     ):
																_SCREAMING_SNAKE_CASE :       str     		=      forward.__wrapped__
																if forward == original_forward:
																				break
												_SCREAMING_SNAKE_CASE :       str     		=      forward
								if getattr(SCREAMING_SNAKE_CASE__							,      """_converted_to_transformer_engine"""							,      SCREAMING_SNAKE_CASE__     ):
												convert_model(SCREAMING_SNAKE_CASE__							,      to_transformer_engine=SCREAMING_SNAKE_CASE__     )

				if is_compiled:
								_SCREAMING_SNAKE_CASE :       Any     		=      model
								_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      compiled_model

				return model







def 			snake_case_   (  ):







				"""simple docstring"""





				PartialState().wait_for_everyone()







def 			snake_case_   (  SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				if PartialState().distributed_type == DistributedType.TPU:
								xm.save(SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     )
				elif PartialState().local_process_index == 0:
								torch.save(SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     )







@contextmanager
def 			snake_case_   (  **SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				for key, value in kwargs.items():
								_SCREAMING_SNAKE_CASE :       Any     		=      str(SCREAMING_SNAKE_CASE__     )

				yield

				for key in kwargs:
								if key.upper() in os.environ:
												del os.environ[key.upper()]







def 			snake_case_   (  SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				if not hasattr(SCREAMING_SNAKE_CASE__							,      """__qualname__"""     ) and not hasattr(SCREAMING_SNAKE_CASE__							,      """__name__"""     ):
								_SCREAMING_SNAKE_CASE :       Optional[int]     		=      getattr(SCREAMING_SNAKE_CASE__							,      """__class__"""							,      SCREAMING_SNAKE_CASE__     )
				if hasattr(SCREAMING_SNAKE_CASE__							,      """__qualname__"""     ):
								return obj.__qualname__
				if hasattr(SCREAMING_SNAKE_CASE__							,      """__name__"""     ):
								return obj.__name__
				return str(SCREAMING_SNAKE_CASE__     )







def 			snake_case_   (  SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				for key, value in source.items():
								if isinstance(SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     ):
												_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      destination.setdefault(SCREAMING_SNAKE_CASE__							,      {}     )
												merge_dicts(SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     )
								else:
												_SCREAMING_SNAKE_CASE :       str     		=      value

				return destination







def 			snake_case_   (  SCREAMING_SNAKE_CASE__ = None     ):







				"""simple docstring"""





				if port is None:
								_SCREAMING_SNAKE_CASE :       Any     		=      2_9500
				with socket.socket(socket.AF_INET							,      socket.SOCK_STREAM     ) as s:
								return s.connect_ex(("""localhost""", port)     ) == 0








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





							'''simple docstring'''


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






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

											_SCREAMING_SNAKE_CASE :       Any     		=      self.get_config()

											return config, pixel_values






							def UpperCAmelCase_	(    self  ):
											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    ,					__snake_case    ,					__snake_case  ):
											_SCREAMING_SNAKE_CASE :       List[str]     		=      FlaxRegNetModel(config=__snake_case  )
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      model(__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    ,					__snake_case    ,					__snake_case  ):
											_SCREAMING_SNAKE_CASE :       Tuple     		=      self.num_labels
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      FlaxRegNetForImageClassification(config=__snake_case  )
											_SCREAMING_SNAKE_CASE :       Tuple     		=      model(__snake_case  )
											self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.num_labels)  )






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      self.prepare_config_and_inputs()
											_SCREAMING_SNAKE_CASE		,       _SCREAMING_SNAKE_CASE :       List[Any]     		=      config_and_inputs
											_SCREAMING_SNAKE_CASE :       Dict     		=      {"""pixel_values""": pixel_values}
											return config, inputs_dict





@require_flax
class     lowercase__  (						_snake_case     ,     unittest.TestCase					):





							'''simple docstring'''


							A_							:      Dict      	=  (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

							A_							:      Union[str, Any]      	=  False
							A_							:      List[str]      	=  False
							A_							:      str      	=  False






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       str     		=      FlaxRegNetModelTester(self  )
											_SCREAMING_SNAKE_CASE :       Any     		=      ConfigTester(self    ,					config_class=__snake_case    ,					has_text_modality=__snake_case  )






							def UpperCAmelCase_	(    self  ):
											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  ):
											return






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       int     		=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__snake_case  )






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       Tuple     		=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*__snake_case  )






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






							@unittest.skip(reason="""RegNet does not support input and output embeddings"""  )
							def UpperCAmelCase_	(    self  ):
											pass






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE		,       _SCREAMING_SNAKE_CASE :       Optional[Any]     		=      self.model_tester.prepare_config_and_inputs_for_common()

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

															_SCREAMING_SNAKE_CASE :       Tuple     		=      ["""pixel_values"""]
															self.assertListEqual(arg_names[:1]    ,					__snake_case  )






							def UpperCAmelCase_	(    self  ):
											def check_hidden_states_output(__snake_case    ,					__snake_case    ,					__snake_case  ):
															_SCREAMING_SNAKE_CASE :       Any     		=      model_class(__snake_case  )

															_SCREAMING_SNAKE_CASE :       int     		=      model(**self._prepare_for_class(__snake_case    ,					__snake_case  )  )

															_SCREAMING_SNAKE_CASE :       Dict     		=      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

															_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      self.model_tester.num_stages
															self.assertEqual(len(__snake_case  )    ,					expected_num_stages + 1  )

											_SCREAMING_SNAKE_CASE		,       _SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE :       Optional[int]     		=      True
															check_hidden_states_output(__snake_case    ,					__snake_case    ,					__snake_case  )

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      True

															check_hidden_states_output(__snake_case    ,					__snake_case    ,					__snake_case  )






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE		,       _SCREAMING_SNAKE_CASE :       str     		=      self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															with self.subTest(model_class.__name__  ):
																			_SCREAMING_SNAKE_CASE :       List[Any]     		=      self._prepare_for_class(__snake_case    ,					__snake_case  )
																			_SCREAMING_SNAKE_CASE :       str     		=      model_class(__snake_case  )

																			@jax.jit
																			def model_jitted(__snake_case    ,					**__snake_case  ):
																							return model(pixel_values=__snake_case    ,					**__snake_case  )

																			with self.subTest("""JIT Enabled"""  ):
																							_SCREAMING_SNAKE_CASE :       Any     		=      model_jitted(**__snake_case  ).to_tuple()

																			with self.subTest("""JIT Disabled"""  ):
																							with jax.disable_jit():
																											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      model_jitted(**__snake_case  ).to_tuple()

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







def 			snake_case_   (  ):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE :       List[str]     		=      Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
				return image







@require_flax
class     lowercase__  (						unittest.TestCase					):





							'''simple docstring'''


							@cached_property
							def UpperCAmelCase_	(    self  ):
											return AutoImageProcessor.from_pretrained("""facebook/regnet-y-040"""  ) if is_vision_available() else None






							@slow
							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       Any     		=      FlaxRegNetForImageClassification.from_pretrained("""facebook/regnet-y-040"""  )

											_SCREAMING_SNAKE_CASE :       List[Any]     		=      self.default_image_processor
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      prepare_img()
											_SCREAMING_SNAKE_CASE :       Tuple     		=      image_processor(images=__snake_case    ,					return_tensors="""np"""  )

											_SCREAMING_SNAKE_CASE :       str     		=      model(**__snake_case  )

											# verify the logits
											_SCREAMING_SNAKE_CASE :       Tuple     		=      (1, 1000)
											self.assertEqual(outputs.logits.shape    ,					__snake_case  )

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

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




