
'''simple docstring'''
import inspect
import unittest

from transformers import ResNetConfig, 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.resnet.modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel

if is_vision_available():
						from PIL import Image

						from transformers import AutoImageProcessor


class        SCREAMING_SNAKE_CASE__	(			unittest.TestCase):







				def __init__(    self		,							A_		,							A_=3		,							A_=32		,							A_=3		,							A_=10		,							A_=[10, 20, 30, 40]		,							A_=[1, 1, 2, 1]		,							A_=True		,							A_=True		,							A_="relu"		,							A_=3		,							A_=None		,							)->						List[Any]:







											'''simple docstring'''






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







				def 		UpperCAmelCase_  (    self			)->						Dict:







											'''simple docstring'''






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

											UpperCamelCase					       =			self.get_config()

											return config, pixel_values







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







											'''simple docstring'''






											return ResNetConfig(
											    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		,							A_		,							A_			)->						Any:







											'''simple docstring'''






											UpperCamelCase					       =			FlaxResNetModel(config=A_			)
											UpperCamelCase					       =			model(A_			)

											# 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		,							A_		,							A_			)->						Optional[Any]:







											'''simple docstring'''






											UpperCamelCase					       =			self.num_labels
											UpperCamelCase					       =			FlaxResNetForImageClassification(config=A_			)
											UpperCamelCase					       =			model(A_			)
											self.parent.assertEqual(result.logits.shape		,							(self.batch_size, self.num_labels)			)







				def 		UpperCAmelCase_  (    self			)->						Any:







											'''simple docstring'''






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


@require_flax
class        SCREAMING_SNAKE_CASE__	(			snake_case_				,    unittest.TestCase):
				lowerCAmelCase_			       =				(FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

				lowerCAmelCase_			       =				False
				lowerCAmelCase_			       =				False
				lowerCAmelCase_			       =				False







				def 		UpperCAmelCase_  (    self			)->						None:







											'''simple docstring'''






											UpperCamelCase					       =			FlaxResNetModelTester(self			)
											UpperCamelCase					       =			ConfigTester(self		,							config_class=A_		,							has_text_modality=A_			)







				def 		UpperCAmelCase_  (    self			)->						Dict:







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







											'''simple docstring'''






											return







				def 		UpperCAmelCase_  (    self			)->						int:







											'''simple docstring'''






											UpperCamelCase					       =			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*A_			)







				def 		UpperCAmelCase_  (    self			)->						int:







											'''simple docstring'''






											UpperCamelCase					       =			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*A_			)







				@unittest.skip(reason='ResNet does not use inputs_embeds'			)
				def 		UpperCAmelCase_  (    self			)->						str:







											'''simple docstring'''






											pass







				@unittest.skip(reason='ResNet does not support input and output embeddings'			)
				def 		UpperCAmelCase_  (    self			)->						str:







											'''simple docstring'''






											pass







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







											'''simple docstring'''






											UpperCamelCase       ,     UpperCamelCase					       =			self.model_tester.prepare_config_and_inputs_for_common()

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

																		UpperCamelCase					       =			['pixel_values']
																		self.assertListEqual(arg_names[:1]		,							A_			)







				def 		UpperCAmelCase_  (    self			)->						Any:







											'''simple docstring'''






											def check_hidden_states_output(A_		,							A_		,							A_			):
																		UpperCamelCase					       =			model_class(A_			)

																		UpperCamelCase					       =			model(**self._prepare_for_class(A_		,							A_			)			)

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

																		UpperCamelCase					       =			self.model_tester.num_stages
																		self.assertEqual(len(A_			)		,							expected_num_stages + 1			)







				@unittest.skip(reason='ResNet does not use feedforward chunking'			)
				def 		UpperCAmelCase_  (    self			)->						Dict:







											'''simple docstring'''






											pass







				def 		UpperCAmelCase_  (    self			)->						List[Any]:







											'''simple docstring'''






											UpperCamelCase       ,     UpperCamelCase					       =			self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		with self.subTest(model_class.__name__			):
																									UpperCamelCase					       =			self._prepare_for_class(A_		,							A_			)
																									UpperCamelCase					       =			model_class(A_			)

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

																									with self.subTest('JIT Enabled'			):
																																UpperCamelCase					       =			model_jitted(**A_			).to_tuple()

																									with self.subTest('JIT Disabled'			):
																																with jax.disable_jit():
																																							UpperCamelCase					       =			model_jitted(**A_			).to_tuple()

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







def 						A_(			):
							UpperCamelCase					       =			Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
							return image





@require_flax
class        SCREAMING_SNAKE_CASE__	(			unittest.TestCase):







				@cached_property
				def 		UpperCAmelCase_  (    self			)->						Any:







											'''simple docstring'''






											return AutoImageProcessor.from_pretrained('microsoft/resnet-50'			) if is_vision_available() else None







				@slow
				def 		UpperCAmelCase_  (    self			)->						str:







											'''simple docstring'''






											UpperCamelCase					       =			FlaxResNetForImageClassification.from_pretrained('microsoft/resnet-50'			)

											UpperCamelCase					       =			self.default_image_processor
											UpperCamelCase					       =			prepare_img()
											UpperCamelCase					       =			image_processor(images=A_		,							return_tensors='np'			)

											UpperCamelCase					       =			model(**A_			)

											# verify the logits
											UpperCamelCase					       =			(1, 1000)
											self.assertEqual(outputs.logits.shape		,							A_			)

											UpperCamelCase					       =			jnp.array([-11.1_069, -9.7_877, -8.3_777]			)

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








'''simple docstring'''






from .hash_table import HashTable
from .number_theory.prime_numbers import is_prime, next_prime




class 		a__				(							a__  ):





							'''simple docstring'''






							def __init__(		self					,    *lowerCamelCase_					,    **lowerCamelCase_   )       ->							Optional[int]:
														super().__init__(*lowerCamelCase_					,    **lowerCamelCase_   )







							def   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_   )       ->							Optional[int]:
														lowerCAmelCase__          =	(
														    next_prime(value % self.size_table   )
														    if not is_prime(value % self.size_table   )
														    else value % self.size_table
														)  # gt = bigger than
														return next_prime_gt - (data % next_prime_gt)







							def   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_   )       ->							Optional[int]:
														return (increment * self.__hash_function_a(lowerCamelCase_					,    lowerCamelCase_   )) % self.size_table







							def   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_=None   )       ->							Any:
														lowerCAmelCase__          =	1
														lowerCAmelCase__          =	self.hash_function(lowerCamelCase_   )

														while self.values[new_key] is not None and self.values[new_key] != key:
																					lowerCAmelCase__          =	(
																					    self.__hash_double_function(lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_   )
																					    if self.balanced_factor() >= self.lim_charge
																					    else None
																					)
																					if new_key is None:
																												break
																					else:
																												i += 1

														return new_key