



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




					'''simple docstring'''



					def __init__(	self					:						List[Any]  ,	UpperCamelCase_					:						Dict  ,	UpperCamelCase_					:						Optional[Any]=3  ,	UpperCamelCase_					:						List[str]=32  ,	UpperCamelCase_					:						Tuple=3  ,	UpperCamelCase_					:						Tuple=10  ,	UpperCamelCase_					:						str=[10, 20, 30, 40]  ,	UpperCamelCase_					:						Tuple=[1, 1, 2, 1]  ,	UpperCamelCase_					:						Dict=True  ,	UpperCamelCase_					:						str=True  ,	UpperCamelCase_					:						Optional[int]="relu"  ,	UpperCamelCase_					:						Union[str, Any]=3  ,	UpperCamelCase_					:						str=None  ,	)	->							Optional[Any]:



						'''simple docstring'''





						_lowercase       :							Tuple  	=		parent
						_lowercase       :							Optional[int]  	=		batch_size
						_lowercase       :							str  	=		image_size
						_lowercase       :							int  	=		num_channels
						_lowercase       :							Optional[Any]  	=		embeddings_size
						_lowercase       :							Dict  	=		hidden_sizes
						_lowercase       :							List[Any]  	=		depths
						_lowercase       :							Dict  	=		is_training
						_lowercase       :							List[str]  	=		use_labels
						_lowercase       :							List[str]  	=		hidden_act
						_lowercase       :							int  	=		num_labels
						_lowercase       :							Tuple  	=		scope
						_lowercase       :							int  	=		len(UpperCamelCase_       )




					def     __UpperCAmelCase  (	self					:						Tuple       )	->							int:



						'''simple docstring'''





						_lowercase       :							Optional[int]  	=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

						_lowercase       :							int  	=		self.get_config()

						return config, pixel_values




					def     __UpperCAmelCase  (	self					:						Optional[int]       )	->							List[str]:



						'''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					:						List[Any]  ,	UpperCamelCase_					:						List[Any]  ,	UpperCamelCase_					:						Tuple       )	->							Optional[int]:



						'''simple docstring'''





						_lowercase       :							List[str]  	=		FlaxResNetModel(config=UpperCamelCase_       )
						_lowercase       :							List[str]  	=		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     __UpperCAmelCase  (	self					:						Optional[Any]  ,	UpperCamelCase_					:						Dict  ,	UpperCamelCase_					:						List[str]       )	->							Optional[int]:



						'''simple docstring'''





						_lowercase       :							Optional[Any]  	=		self.num_labels
						_lowercase       :							List[str]  	=		FlaxResNetForImageClassification(config=UpperCamelCase_       )
						_lowercase       :							Optional[int]  	=		model(UpperCamelCase_       )
						self.parent.assertEqual(result.logits.shape  ,	(self.batch_size, self.num_labels)       )




					def     __UpperCAmelCase  (	self					:						List[str]       )	->							Dict:



						'''simple docstring'''





						_lowercase       :							Tuple  	=		self.prepare_config_and_inputs()
						_lowercase	,     _lowercase       :							Optional[Any]  	=		config_and_inputs
						_lowercase       :							Any  	=		{'pixel_values': pixel_values}
						return config, inputs_dict






@require_flax
class 		lowerCamelCase__   (    A						, unittest.TestCase  ):




					'''simple docstring'''



					A_         =    (FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

					A_         =    False
					A_         =    False
					A_         =    False




					def     __UpperCAmelCase  (	self					:						List[str]       )	->							None:



						'''simple docstring'''





						_lowercase       :							List[Any]  	=		FlaxResNetModelTester(self       )
						_lowercase       :							str  	=		ConfigTester(self  ,	config_class=UpperCamelCase_  ,	has_text_modality=UpperCamelCase_       )




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



						'''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					:						Optional[int]       )	->							Tuple:



						'''simple docstring'''





						return




					def     __UpperCAmelCase  (	self					:						List[str]       )	->							Any:



						'''simple docstring'''





						_lowercase       :							str  	=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCamelCase_       )




					def     __UpperCAmelCase  (	self					:						Any       )	->							Optional[int]:



						'''simple docstring'''





						_lowercase       :							Dict  	=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_       )




					@unittest.skip(reason='ResNet does not use inputs_embeds'       )
					def     __UpperCAmelCase  (	self					:						List[str]       )	->							int:



						'''simple docstring'''





						pass




					@unittest.skip(reason='ResNet does not support input and output embeddings'       )
					def     __UpperCAmelCase  (	self					:						int       )	->							List[Any]:



						'''simple docstring'''





						pass




					def     __UpperCAmelCase  (	self					:						int       )	->							Optional[Any]:



						'''simple docstring'''





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

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

							_lowercase       :							Dict  	=		['pixel_values']
							self.assertListEqual(arg_names[:1]  ,	UpperCamelCase_       )




					def     __UpperCAmelCase  (	self					:						Optional[int]       )	->							Any:



						'''simple docstring'''





						def check_hidden_states_output(UpperCamelCase_					:						List[str]  ,	UpperCamelCase_					:						Any  ,	UpperCamelCase_					:						Union[str, Any]       ):
							_lowercase       :							List[Any]  	=		model_class(UpperCamelCase_       )

							_lowercase       :							Union[str, Any]  	=		model(**self._prepare_for_class(UpperCamelCase_  ,	UpperCamelCase_       )       )

							_lowercase       :							int  	=		outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

							_lowercase       :							Tuple  	=		self.model_tester.num_stages
							self.assertEqual(len(UpperCamelCase_       )  ,	expected_num_stages + 1       )




					@unittest.skip(reason='ResNet does not use feedforward chunking'       )
					def     __UpperCAmelCase  (	self					:						int       )	->							Union[str, Any]:



						'''simple docstring'''





						pass




					def     __UpperCAmelCase  (	self					:						str       )	->							Tuple:



						'''simple docstring'''





						_lowercase	,     _lowercase       :							Dict  	=		self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							with self.subTest(model_class.__name__       ):
								_lowercase       :							Dict  	=		self._prepare_for_class(UpperCamelCase_  ,	UpperCamelCase_       )
								_lowercase       :							int  	=		model_class(UpperCamelCase_       )

								@jax.jit
								def model_jitted(UpperCamelCase_					:						Dict  ,	**UpperCamelCase_					:						Optional[Any]       ):
									return model(pixel_values=UpperCamelCase_  ,	**UpperCamelCase_       )

								with self.subTest('JIT Enabled'       ):
									_lowercase       :							Union[str, Any]  	=		model_jitted(**UpperCamelCase_       ).to_tuple()

								with self.subTest('JIT Disabled'       ):
									with jax.disable_jit():
										_lowercase       :							Tuple  	=		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     __UpperCamelCase					(				)			->      Union[str, Any]:
	_lowercase       :							Dict  	=		Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
	return image






@require_flax
class 		lowerCamelCase__   (    unittest.TestCase  ):




					'''simple docstring'''



					@cached_property
					def     __UpperCAmelCase  (	self					:						Optional[Any]       )	->							Union[str, Any]:



						'''simple docstring'''





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




					@slow
					def     __UpperCAmelCase  (	self					:						Union[str, Any]       )	->							Optional[int]:



						'''simple docstring'''





						_lowercase       :							Tuple  	=		FlaxResNetForImageClassification.from_pretrained('microsoft/resnet-50'       )

						_lowercase       :							Optional[int]  	=		self.default_image_processor
						_lowercase       :							Union[str, Any]  	=		prepare_img()
						_lowercase       :							Union[str, Any]  	=		image_processor(images=UpperCamelCase_  ,	return_tensors='np'       )

						_lowercase       :							List[str]  	=		model(**UpperCamelCase_       )

						# verify the logits
						_lowercase       :							List[str]  	=		(1, 1000)
						self.assertEqual(outputs.logits.shape  ,	UpperCamelCase_       )

						_lowercase       :							Dict  	=		jnp.array([-11.10_69, -9.78_77, -8.37_77]       )

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





'''simple docstring'''

from __future__ import annotations



class 		lowerCamelCase__   :




					'''simple docstring'''



					def __init__(	self					:						str  ,	UpperCamelCase_					:						int       )	->							None:



						'''simple docstring'''





						_lowercase       :							Any  	=		value
						_lowercase       :							Node | None  	=		None
						_lowercase       :							Node | None  	=		None






class 		lowerCamelCase__   :




					'''simple docstring'''



					A_         =    42




					def __init__(	self					:						List[Any]       )	->							None:



						'''simple docstring'''





						_lowercase       :							Optional[int]  	=		0




					def     __UpperCAmelCase  (	self					:						str  ,	UpperCamelCase_					:						Node | None  ,	UpperCamelCase_					:						int       )	->							None:



						'''simple docstring'''





						if node is None:
							return

						if path_sum == self.target:
							self.paths += 1

						if node.left:
							self.depth_first_search(node.left  ,	path_sum + node.left.value       )
						if node.right:
							self.depth_first_search(node.right  ,	path_sum + node.right.value       )




					def     __UpperCAmelCase  (	self					:						Optional[int]  ,	UpperCamelCase_					:						Node | None  ,	UpperCamelCase_					:						int | None = None       )	->							int:



						'''simple docstring'''





						if node is None:
							return 0
						if target is not None:
							_lowercase       :							Union[str, Any]  	=		target

						self.depth_first_search(UpperCamelCase_  ,	node.value       )
						self.path_sum(node.left       )
						self.path_sum(node.right       )

						return self.paths


if __name__ == "__main__":
		import doctest

		doctest.testmod()
