
'''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       _lowerCAmelCase   (   unittest.TestCase				):







							'''simple docstring'''

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

							def 	lowercase							(self  )					->       str:
														_snake_case       =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

														_snake_case       =  self.get_config()

														return config, pixel_values

							def 	lowercase							(self  )					->       int:
														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 	lowercase							(self  ,  UpperCAmelCase  ,  UpperCAmelCase  )					->       List[Any]:
														_snake_case       =  FlaxRegNetModel(config=UpperCAmelCase  )
														_snake_case       =  model(UpperCAmelCase  )

														# 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 	lowercase							(self  ,  UpperCAmelCase  ,  UpperCAmelCase  )					->       Optional[Any]:
														_snake_case       =  self.num_labels
														_snake_case       =  FlaxRegNetForImageClassification(config=UpperCAmelCase  )
														_snake_case       =  model(UpperCAmelCase  )
														self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.num_labels)  )





							def 	lowercase							(self  )					->       Optional[Any]:
														_snake_case       =  self.prepare_config_and_inputs()
														_snake_case,							_snake_case       =  config_and_inputs
														_snake_case       =  {"""pixel_values""": pixel_values}
														return config, inputs_dict


@require_flax
class       _lowerCAmelCase   (   __snake_case					,							unittest.TestCase				):







							'''simple docstring'''

							lowerCAmelCase_ 						=						(FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

							lowerCAmelCase_ 						=						False
							lowerCAmelCase_ 						=						False
							lowerCAmelCase_ 						=						False

							def 	lowercase							(self  )					->       None:
														_snake_case       =  FlaxRegNetModelTester(self  )
														_snake_case       =  ConfigTester(self  ,  config_class=UpperCAmelCase  ,  has_text_modality=UpperCAmelCase  )

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

							def 	lowercase							(self  )					->       List[Any]:
														_snake_case       =  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*UpperCAmelCase  )

							def 	lowercase							(self  )					->       List[str]:
														_snake_case       =  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase  )

							@unittest.skip(reason="""RegNet does not use inputs_embeds"""  )
							def 	lowercase							(self  )					->       Any:
														pass

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

							def 	lowercase							(self  )					->       Optional[int]:
														_snake_case,							_snake_case       =  self.model_tester.prepare_config_and_inputs_for_common()

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

																					_snake_case       =  ["""pixel_values"""]
																					self.assertListEqual(arg_names[:1]  ,  UpperCAmelCase  )

							def 	lowercase							(self  )					->       List[str]:
														def check_hidden_states_output(UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  ):
																					_snake_case       =  model_class(UpperCAmelCase  )

																					_snake_case       =  model(**self._prepare_for_class(UpperCAmelCase  ,  UpperCAmelCase  )  )

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

																					_snake_case       =  self.model_tester.num_stages
																					self.assertEqual(len(UpperCAmelCase  )  ,  expected_num_stages + 1  )

														_snake_case,							_snake_case       =  self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					_snake_case       =  True
																					check_hidden_states_output(UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  )

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

																					check_hidden_states_output(UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  )





							def 	lowercase							(self  )					->       Optional[Any]:
														_snake_case,							_snake_case       =  self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					with self.subTest(model_class.__name__  ):
																												_snake_case       =  self._prepare_for_class(UpperCAmelCase  ,  UpperCAmelCase  )
																												_snake_case       =  model_class(UpperCAmelCase  )

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

																												with self.subTest("""JIT Enabled"""  ):
																																			_snake_case       =  model_jitted(**UpperCAmelCase  ).to_tuple()

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

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






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


@require_flax
class       _lowerCAmelCase   (   unittest.TestCase				):







							'''simple docstring'''

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





							@slow
							def 	lowercase							(self  )					->       Union[str, Any]:
														_snake_case       =  FlaxRegNetForImageClassification.from_pretrained("""facebook/regnet-y-040"""  )

														_snake_case       =  self.default_image_processor
														_snake_case       =  prepare_img()
														_snake_case       =  image_processor(images=UpperCAmelCase  ,  return_tensors="""np"""  )

														_snake_case       =  model(**UpperCAmelCase  )

														# verify the logits
														_snake_case       =  (1, 1000)
														self.assertEqual(outputs.logits.shape  ,  UpperCAmelCase  )

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

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

'''simple docstring'''
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


__lowerCAmelCase												=	{
    'configuration_encodec': [
        'ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'EncodecConfig',
    ],
    'feature_extraction_encodec': ['EncodecFeatureExtractor'],
}

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__lowerCAmelCase												=	[
				    'ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST',
				    'EncodecModel',
				    'EncodecPreTrainedModel',
				]

if TYPE_CHECKING:
				from .configuration_encodec import (
				    ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP,
				    EncodecConfig,
				)
				from .feature_extraction_encodec import EncodecFeatureExtractor

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_encodec import (
								    ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST,
								    EncodecModel,
								    EncodecPreTrainedModel,
								)

else:
				import sys

				__lowerCAmelCase												=	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)