



'''simple docstring'''

from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import RegNetConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
		import tensorflow as tf

		from transformers import TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel


if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor



class 		lowerCamelCase__      :




					'''simple docstring'''



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



						'''simple docstring'''





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




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



						'''simple docstring'''





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

						_lowercase					:       Optional[Any]       =		None
						if self.use_labels:
							_lowercase					:       Any       =		ids_tensor([self.batch_size]     ,  self.num_labels					)

						_lowercase					:       List[Any]       =		self.get_config()
						return config, pixel_values, labels




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



						'''simple docstring'''





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




					def 						__UpperCAmelCase     (  self				:	Union[str, Any]     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	List[str]     ,  UpperCamelCase_				:	Union[str, Any]					)  ->       Tuple:



						'''simple docstring'''





						_lowercase					:       List[str]       =		TFRegNetModel(config=__lowerCamelCase					)
						_lowercase					:       str       =		model(__lowerCamelCase     ,  training=__lowerCamelCase					)
						# expected last hidden states: B, C, H // 32, W // 32
						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_				:	Optional[Any]     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	Tuple					)  ->       List[str]:



						'''simple docstring'''





						_lowercase					:       List[Any]       =		self.num_labels
						_lowercase					:       int       =		TFRegNetForImageClassification(__lowerCamelCase					)
						_lowercase					:       str       =		model(__lowerCamelCase     ,  labels=__lowerCamelCase     ,  training=__lowerCamelCase					)
						self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.num_labels)					)




					def 						__UpperCAmelCase     (  self				:	str					)  ->       Dict:



						'''simple docstring'''





						_lowercase					:       Any       =		self.prepare_config_and_inputs()
						_lowercase					:       str       =		config_and_inputs
						_lowercase					:       Any       =		{"pixel_values": pixel_values}
						return config, inputs_dict






@require_tf
class 		lowerCamelCase__      (						_A	,   _A	,   unittest.TestCase     ):




					'''simple docstring'''



					A_						    =						(TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
					A_						    =						(
					    {"""feature-extraction""": TFRegNetModel, """image-classification""": TFRegNetForImageClassification}
					    if is_tf_available()
					    else {}
					)

					A_						    =						False
					A_						    =						False
					A_						    =						False
					A_						    =						False
					A_						    =						False




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



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		TFRegNetModelTester(self					)
						_lowercase					:       int       =		ConfigTester(self     ,  config_class=__lowerCamelCase     ,  has_text_modality=__lowerCamelCase					)




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



						'''simple docstring'''





						return




					@unittest.skip(reason='RegNet does not use inputs_embeds'					)
					def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       Optional[Any]:



						'''simple docstring'''





						pass




					@unittest.skipIf(
					    not is_tf_available() or len(tf.config.list_physical_devices('GPU'					)					) == 0     ,  reason='TF does not support backprop for grouped convolutions on CPU.'     ,  )
					@slow
					def 						__UpperCAmelCase     (  self				:	str					)  ->       Any:



						'''simple docstring'''





						super().test_keras_fit()




					@unittest.skip(reason='RegNet does not support input and output embeddings'					)
					def 						__UpperCAmelCase     (  self				:	Union[str, Any]					)  ->       Union[str, Any]:



						'''simple docstring'''





						pass




					def 						__UpperCAmelCase     (  self				:	Union[str, Any]					)  ->       Tuple:



						'''simple docstring'''





						_lowercase					:       List[str]       =		self.model_tester.prepare_config_and_inputs_for_common()

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

							_lowercase					:       Optional[int]       =		["pixel_values"]
							self.assertListEqual(arg_names[:1]     ,  __lowerCamelCase					)




					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       Tuple:



						'''simple docstring'''





						_lowercase					:       Tuple       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__lowerCamelCase					)




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



						'''simple docstring'''





						def check_hidden_states_output(UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	str					):
							_lowercase					:       int       =		model_class(__lowerCamelCase					)
							_lowercase					:       int       =		model(**self._prepare_for_class(__lowerCamelCase     ,  __lowerCamelCase					)     ,  training=__lowerCamelCase					)

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

							_lowercase					:       Dict       =		self.model_tester.num_stages
							self.assertEqual(len(__lowerCamelCase					)     ,  expected_num_stages + 1					)

							# RegNet's feature maps are of shape (batch_size, num_channels, height, width)
							self.assertListEqual(
							    list(hidden_states[0].shape[-2:]					)     ,  [self.model_tester.image_size // 2, self.model_tester.image_size // 2]     ,  )

						_lowercase					:       int       =		self.model_tester.prepare_config_and_inputs_for_common()
						_lowercase					:       str       =		["basic", "bottleneck"]
						for model_class in self.all_model_classes:
							for layer_type in layers_type:
								_lowercase					:       List[str]       =		layer_type
								_lowercase					:       List[Any]       =		True
								check_hidden_states_output(__lowerCamelCase     ,  __lowerCamelCase     ,  __lowerCamelCase					)

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

								check_hidden_states_output(__lowerCamelCase     ,  __lowerCamelCase     ,  __lowerCamelCase					)




					def 						__UpperCAmelCase     (  self				:	Any					)  ->       Dict:



						'''simple docstring'''





						_lowercase					:       Tuple       =		self.model_tester.prepare_config_and_inputs_for_common()

						def check_equivalence(UpperCamelCase_				:	int     ,  UpperCamelCase_				:	List[str]     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	Optional[Any]={}					):
							_lowercase					:       Optional[int]       =		model(__lowerCamelCase     ,  return_dict=__lowerCamelCase     ,  **__lowerCamelCase					)
							_lowercase					:       int       =		model(__lowerCamelCase     ,  return_dict=__lowerCamelCase     ,  **__lowerCamelCase					).to_tuple()

							def recursive_check(UpperCamelCase_				:	List[str]     ,  UpperCamelCase_				:	Any					):
								if isinstance(__lowerCamelCase     ,  (List, Tuple)					):
									for tuple_iterable_value, dict_iterable_value in zip(__lowerCamelCase     ,  __lowerCamelCase					):
										recursive_check(__lowerCamelCase     ,  __lowerCamelCase					)
								elif tuple_object is None:
									return
								else:
									self.assertTrue(
									    all(tf.equal(__lowerCamelCase     ,  __lowerCamelCase					)					)     ,  msg=(
									        'Tuple and dict output are not equal. Difference:'
									        F''' {tf.math.reduce_max(tf.abs(tuple_object - dict_object					)					)}'''
									    )     ,  )

								recursive_check(__lowerCamelCase     ,  __lowerCamelCase					)

						for model_class in self.all_model_classes:
							_lowercase					:       Tuple       =		model_class(__lowerCamelCase					)

							_lowercase					:       Optional[int]       =		self._prepare_for_class(__lowerCamelCase     ,  __lowerCamelCase					)
							_lowercase					:       Tuple       =		self._prepare_for_class(__lowerCamelCase     ,  __lowerCamelCase					)
							check_equivalence(__lowerCamelCase     ,  __lowerCamelCase     ,  __lowerCamelCase					)

							_lowercase					:       Optional[int]       =		self._prepare_for_class(__lowerCamelCase     ,  __lowerCamelCase     ,  return_labels=__lowerCamelCase					)
							_lowercase					:       List[Any]       =		self._prepare_for_class(__lowerCamelCase     ,  __lowerCamelCase     ,  return_labels=__lowerCamelCase					)
							check_equivalence(__lowerCamelCase     ,  __lowerCamelCase     ,  __lowerCamelCase					)

							_lowercase					:       Optional[Any]       =		self._prepare_for_class(__lowerCamelCase     ,  __lowerCamelCase					)
							_lowercase					:       str       =		self._prepare_for_class(__lowerCamelCase     ,  __lowerCamelCase					)
							check_equivalence(__lowerCamelCase     ,  __lowerCamelCase     ,  __lowerCamelCase     ,  {'output_hidden_states': True}					)

							_lowercase					:       Optional[Any]       =		self._prepare_for_class(__lowerCamelCase     ,  __lowerCamelCase     ,  return_labels=__lowerCamelCase					)
							_lowercase					:       Tuple       =		self._prepare_for_class(__lowerCamelCase     ,  __lowerCamelCase     ,  return_labels=__lowerCamelCase					)
							check_equivalence(__lowerCamelCase     ,  __lowerCamelCase     ,  __lowerCamelCase     ,  {'output_hidden_states': True}					)




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



						'''simple docstring'''





						_lowercase					:       List[str]       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase					)




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



						'''simple docstring'''





						for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							_lowercase					:       Union[str, Any]       =		TFRegNetModel.from_pretrained(__lowerCamelCase					)
							self.assertIsNotNone(__lowerCamelCase					)







def 						__UpperCamelCase				(							) ->			Any:
	_lowercase					:       Dict       =		Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'					)
	return image




@require_tf
@require_vision
class 		lowerCamelCase__      (						unittest.TestCase     ):




					'''simple docstring'''



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



						'''simple docstring'''





						return (
						    AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]					)
						    if is_vision_available()
						    else None
						)




					@slow
					def 						__UpperCAmelCase     (  self				:	Dict					)  ->       Optional[Any]:



						'''simple docstring'''





						_lowercase					:       List[Any]       =		TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]					)

						_lowercase					:       Optional[int]       =		self.default_image_processor
						_lowercase					:       List[Any]       =		prepare_img()
						_lowercase					:       str       =		image_processor(images=__lowerCamelCase     ,  return_tensors='tf'					)

						# forward pass
						_lowercase					:       List[Any]       =		model(**__lowerCamelCase     ,  training=__lowerCamelCase					)

						# verify the logits
						_lowercase					:       Dict       =		tf.TensorShape((1, 1000)					)
						self.assertEqual(outputs.logits.shape     ,  __lowerCamelCase					)

						_lowercase					:       Optional[Any]       =		tf.constant([-0.41_80, -1.50_51, -3.48_36]					)

						tf.debugging.assert_near(outputs.logits[0, :3]     ,  __lowerCamelCase     ,  atol=1E-4					)





'''simple docstring'''

import unittest

from knapsack import greedy_knapsack as kp



class 		lowerCamelCase__      (						unittest.TestCase     ):




					'''simple docstring'''



					def 						__UpperCAmelCase     (  self				:	int					)  ->       Any:



						'''simple docstring'''





						_lowercase					:       List[Any]       =		[10, 20, 30, 40, 50, 60]
						_lowercase					:       Tuple       =		[2, 4, 6, 8, 10, 12]
						_lowercase					:       Optional[Any]       =		100
						self.assertEqual(kp.calc_profit(UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_					)     ,  210					)




					def 						__UpperCAmelCase     (  self				:	int					)  ->       int:



						'''simple docstring'''





						self.assertRaisesRegex(UpperCamelCase_     ,  'max_weight must greater than zero.'					)




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



						'''simple docstring'''





						self.assertRaisesRegex(UpperCamelCase_     ,  'Weight can not be negative.'					)




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



						'''simple docstring'''





						self.assertRaisesRegex(UpperCamelCase_     ,  'Profit can not be negative.'					)




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



						'''simple docstring'''





						self.assertRaisesRegex(UpperCamelCase_     ,  'max_weight must greater than zero.'					)




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



						'''simple docstring'''





						self.assertRaisesRegex(
						    UpperCamelCase_     ,  'The length of profit and weight must be same.'					)


if __name__ == "__main__":
		unittest.main()
