


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








'''simple docstring'''


from pickle import UnpicklingError

import jax
import jax.numpy as jnp
import numpy as np
from flax.serialization import from_bytes
from flax.traverse_util import flatten_dict

from ..utils import logging


UpperCAmelCase_		:   List[Any]							   =		logging.get_logger(__name__)







def 			snake_case_   (  SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				try:
								with open(SCREAMING_SNAKE_CASE__							,      """rb"""     ) as flax_state_f:
												_SCREAMING_SNAKE_CASE :       Dict     		=      from_bytes(SCREAMING_SNAKE_CASE__							,      flax_state_f.read()     )
				except UnpicklingError as e:
								try:
												with open(SCREAMING_SNAKE_CASE__     ) as f:
																if f.read().startswith("""version"""     ):
																				raise OSError(
																				    """You seem to have cloned a repository without having git-lfs installed. Please"""
																				    """ install git-lfs and run `git lfs install` followed by `git lfs pull` in the"""
																				    """ folder you cloned."""     )
																else:
																				raise ValueError from e
								except (UnicodeDecodeError, ValueError):
												raise EnvironmentError(f"""Unable to convert {model_file} to Flax deserializable object. """     )

				return load_flax_weights_in_pytorch_model(SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     )







def 			snake_case_   (  SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				try:
								import torch  # noqa: F401
				except ImportError:
								logger.error(
								    """Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see"""
								    """ https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation"""
								    """ instructions."""     )
								raise

				# check if we have bf16 weights
				_SCREAMING_SNAKE_CASE :       List[Any]     		=      flatten_dict(jax.tree_util.tree_map(lambda SCREAMING_SNAKE_CASE__     : x.dtype == jnp.bfloataa							,      SCREAMING_SNAKE_CASE__     )     ).values()
				if any(SCREAMING_SNAKE_CASE__     ):
								# convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16

								# and bf16 is not fully supported in PT yet.
								logger.warning(
								    """Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` """
								    """before loading those in PyTorch model."""     )
								_SCREAMING_SNAKE_CASE :       Dict     		=      jax.tree_util.tree_map(
								    lambda SCREAMING_SNAKE_CASE__     : params.astype(np.floataa     ) if params.dtype == jnp.bfloataa else params							,      SCREAMING_SNAKE_CASE__     )

				_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      """"""

				_SCREAMING_SNAKE_CASE :       str     		=      flatten_dict(SCREAMING_SNAKE_CASE__							,      sep="""."""     )
				_SCREAMING_SNAKE_CASE :       str     		=      pt_model.state_dict()

				# keep track of unexpected & missing keys
				_SCREAMING_SNAKE_CASE :       Tuple     		=      []
				_SCREAMING_SNAKE_CASE :       int     		=      set(pt_model_dict.keys()     )

				for flax_key_tuple, flax_tensor in flax_state_dict.items():
								_SCREAMING_SNAKE_CASE :       Any     		=      flax_key_tuple.split("""."""     )

								if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4:
												_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      flax_key_tuple_array[:-1] + ["""weight"""]
												_SCREAMING_SNAKE_CASE :       List[str]     		=      jnp.transpose(SCREAMING_SNAKE_CASE__							,      (3, 2, 0, 1)     )
								elif flax_key_tuple_array[-1] == "kernel":
												_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      flax_key_tuple_array[:-1] + ["""weight"""]
												_SCREAMING_SNAKE_CASE :       Any     		=      flax_tensor.T
								elif flax_key_tuple_array[-1] == "scale":
												_SCREAMING_SNAKE_CASE :       Optional[int]     		=      flax_key_tuple_array[:-1] + ["""weight"""]

								if "time_embedding" not in flax_key_tuple_array:
												for i, flax_key_tuple_string in enumerate(SCREAMING_SNAKE_CASE__     ):
																_SCREAMING_SNAKE_CASE :       Optional[int]     		=      (
																    flax_key_tuple_string.replace("""_0"""							,      """.0"""     )
																    .replace("""_1"""							,      """.1"""     )
																    .replace("""_2"""							,      """.2"""     )
																    .replace("""_3"""							,      """.3"""     )
																    .replace("""_4"""							,      """.4"""     )
																    .replace("""_5"""							,      """.5"""     )
																    .replace("""_6"""							,      """.6"""     )
																    .replace("""_7"""							,      """.7"""     )
																    .replace("""_8"""							,      """.8"""     )
																    .replace("""_9"""							,      """.9"""     )
																)

								_SCREAMING_SNAKE_CASE :       Tuple     		=      """.""".join(SCREAMING_SNAKE_CASE__     )

								if flax_key in pt_model_dict:
												if flax_tensor.shape != pt_model_dict[flax_key].shape:
																raise ValueError(
																    f"""Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected """
																    f"""to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}."""     )
												else:
																# add weight to pytorch dict
																_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      np.asarray(SCREAMING_SNAKE_CASE__     ) if not isinstance(SCREAMING_SNAKE_CASE__							,      np.ndarray     ) else flax_tensor
																_SCREAMING_SNAKE_CASE :       int     		=      torch.from_numpy(SCREAMING_SNAKE_CASE__     )
																# remove from missing keys
																missing_keys.remove(SCREAMING_SNAKE_CASE__     )
								else:
												# weight is not expected by PyTorch model
												unexpected_keys.append(SCREAMING_SNAKE_CASE__     )

				pt_model.load_state_dict(SCREAMING_SNAKE_CASE__     )

				# re-transform missing_keys to list
				_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      list(SCREAMING_SNAKE_CASE__     )

				if len(SCREAMING_SNAKE_CASE__     ) > 0:
								logger.warning(
								    """Some weights of the Flax model were not used when initializing the PyTorch model"""
								    f""" {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing"""
								    f""" {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture"""
								    """ (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This"""
								    f""" IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect"""
								    """ to be exactly identical (e.g. initializing a BertForSequenceClassification model from a"""
								    """ FlaxBertForSequenceClassification model)."""     )
				if len(SCREAMING_SNAKE_CASE__     ) > 0:
								logger.warning(
								    f"""Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly"""
								    f""" initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to"""
								    """ use it for predictions and inference."""     )

				return pt_model




