
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   UpperCamelCase_      :







						'''simple docstring'''



						def __init__(			self							:Union[str, Any]		,    lowerCAmelCase__							:Union[str, Any]		,    lowerCAmelCase__							:int=3		,    lowerCAmelCase__							:Optional[Any]=32		,    lowerCAmelCase__							:List[str]=3		,    lowerCAmelCase__							:Union[str, Any]=10		,    lowerCAmelCase__							:Tuple=[10, 20, 30, 40]		,    lowerCAmelCase__							:List[str]=[1, 1, 2, 1]		,    lowerCAmelCase__							:Optional[int]=True		,    lowerCAmelCase__							:Dict=True		,    lowerCAmelCase__							:str="relu"		,    lowerCAmelCase__							:str=3		,    lowerCAmelCase__							:List[str]=None		,    )  ->Union[str, Any]:
										lowercase         =      parent
										lowercase         =      batch_size
										lowercase         =      image_size
										lowercase         =      num_channels
										lowercase         =      embeddings_size
										lowercase         =      hidden_sizes
										lowercase         =      depths
										lowercase         =      is_training
										lowercase         =      use_labels
										lowercase         =      hidden_act
										lowercase         =      num_labels
										lowercase         =      scope
										lowercase         =      len(lowerCAmelCase__     )





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

										lowercase         =      None
										if self.use_labels:
														lowercase         =      ids_tensor([self.batch_size]		,    self.num_labels     )

										lowercase         =      self.get_config()
										return config, pixel_values, labels





						def        SCREAMING_SNAKE_CASE(			self							:Tuple     )  ->List[Any]:
										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        SCREAMING_SNAKE_CASE(			self							:str		,    lowerCAmelCase__							:Dict		,    lowerCAmelCase__							:int		,    lowerCAmelCase__							:int     )  ->List[Any]:
										lowercase         =      TFRegNetModel(config=lowerCAmelCase__     )
										lowercase         =      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        SCREAMING_SNAKE_CASE(			self							:Union[str, Any]		,    lowerCAmelCase__							:Tuple		,    lowerCAmelCase__							:Optional[Any]		,    lowerCAmelCase__							:List[str]     )  ->str:
										lowercase         =      self.num_labels
										lowercase         =      TFRegNetForImageClassification(lowerCAmelCase__     )
										lowercase         =      model(lowerCAmelCase__		,    labels=lowerCAmelCase__		,    training=lowerCAmelCase__     )
										self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.num_labels)     )





						def        SCREAMING_SNAKE_CASE(			self							:Union[str, Any]     )  ->int:
										lowercase         =      self.prepare_config_and_inputs()
										lowercase     ,	lowercase     ,	lowercase         =      config_and_inputs
										lowercase         =      {"pixel_values": pixel_values}
										return config, inputs_dict





@require_tf
class   UpperCamelCase_      (      __a		,    __a		,    unittest.TestCase		):







						'''simple docstring'''



						UpperCamelCase      :  Union[str, Any]				   =					(TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
						UpperCamelCase      :  Optional[Any]				   =					(
						    {'''feature-extraction''': TFRegNetModel, '''image-classification''': TFRegNetForImageClassification}
						    if is_tf_available()
						    else {}
						)

						UpperCamelCase      :  str				   =					False
						UpperCamelCase      :  str				   =					False
						UpperCamelCase      :  int				   =					False
						UpperCamelCase      :  int				   =					False
						UpperCamelCase      :  Union[str, Any]				   =					False





						def        SCREAMING_SNAKE_CASE(			self							:Optional[Any]     )  ->Optional[Any]:
										lowercase         =      TFRegNetModelTester(self     )
										lowercase         =      ConfigTester(self		,    config_class=lowerCAmelCase__		,    has_text_modality=lowerCAmelCase__     )





						def        SCREAMING_SNAKE_CASE(			self							:str     )  ->Union[str, Any]:
										return





						@unittest.skip(reason="RegNet does not use inputs_embeds"     )
						def        SCREAMING_SNAKE_CASE(			self							:Any     )  ->List[Any]:
										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        SCREAMING_SNAKE_CASE(			self							:str     )  ->int:
										super().test_keras_fit()





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





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

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

														lowercase         =      ["pixel_values"]
														self.assertListEqual(arg_names[:1]		,    lowerCAmelCase__     )





						def        SCREAMING_SNAKE_CASE(			self							:Union[str, Any]     )  ->Any:
										lowercase         =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase__     )





						def        SCREAMING_SNAKE_CASE(			self							:Any     )  ->Dict:
										def check_hidden_states_output(lowerCAmelCase__							:Optional[int]		,    lowerCAmelCase__							:Any		,    lowerCAmelCase__							:List[Any]     ):
														lowercase         =      model_class(lowerCAmelCase__     )
														lowercase         =      model(**self._prepare_for_class(lowerCAmelCase__		,    lowerCAmelCase__     )		,    training=lowerCAmelCase__     )

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

														lowercase         =      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     ,	lowercase         =      self.model_tester.prepare_config_and_inputs_for_common()
										lowercase         =      ["basic", "bottleneck"]
										for model_class in self.all_model_classes:
														for layer_type in layers_type:
																		lowercase         =      layer_type
																		lowercase         =      True
																		check_hidden_states_output(lowerCAmelCase__		,    lowerCAmelCase__		,    lowerCAmelCase__     )

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

																		check_hidden_states_output(lowerCAmelCase__		,    lowerCAmelCase__		,    lowerCAmelCase__     )





						def        SCREAMING_SNAKE_CASE(			self							:Union[str, Any]     )  ->Tuple:
										lowercase     ,	lowercase         =      self.model_tester.prepare_config_and_inputs_for_common()

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

														def recursive_check(lowerCAmelCase__							:List[Any]		,    lowerCAmelCase__							:List[str]     ):
																		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         =      model_class(lowerCAmelCase__     )

														lowercase         =      self._prepare_for_class(lowerCAmelCase__		,    lowerCAmelCase__     )
														lowercase         =      self._prepare_for_class(lowerCAmelCase__		,    lowerCAmelCase__     )
														check_equivalence(lowerCAmelCase__		,    lowerCAmelCase__		,    lowerCAmelCase__     )

														lowercase         =      self._prepare_for_class(lowerCAmelCase__		,    lowerCAmelCase__		,    return_labels=lowerCAmelCase__     )
														lowercase         =      self._prepare_for_class(lowerCAmelCase__		,    lowerCAmelCase__		,    return_labels=lowerCAmelCase__     )
														check_equivalence(lowerCAmelCase__		,    lowerCAmelCase__		,    lowerCAmelCase__     )

														lowercase         =      self._prepare_for_class(lowerCAmelCase__		,    lowerCAmelCase__     )
														lowercase         =      self._prepare_for_class(lowerCAmelCase__		,    lowerCAmelCase__     )
														check_equivalence(lowerCAmelCase__		,    lowerCAmelCase__		,    lowerCAmelCase__		,    {"output_hidden_states": True}     )

														lowercase         =      self._prepare_for_class(lowerCAmelCase__		,    lowerCAmelCase__		,    return_labels=lowerCAmelCase__     )
														lowercase         =      self._prepare_for_class(lowerCAmelCase__		,    lowerCAmelCase__		,    return_labels=lowerCAmelCase__     )
														check_equivalence(lowerCAmelCase__		,    lowerCAmelCase__		,    lowerCAmelCase__		,    {"output_hidden_states": True}     )





						def        SCREAMING_SNAKE_CASE(			self							:Union[str, Any]     )  ->Union[str, Any]:
										lowercase         =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__     )





						@slow
						def        SCREAMING_SNAKE_CASE(			self							:Union[str, Any]     )  ->str:
										for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														lowercase         =      TFRegNetModel.from_pretrained(lowerCAmelCase__     )
														self.assertIsNotNone(lowerCAmelCase__     )




def  __snake_case							(	):







				'''simple docstring'''


				lowercase         =      Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
				return image





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







						'''simple docstring'''



						@cached_property
						def        SCREAMING_SNAKE_CASE(			self							:Optional[int]     )  ->Tuple:
										return (
										    AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]     )
										    if is_vision_available()
										    else None
										)





						@slow
						def        SCREAMING_SNAKE_CASE(			self							:Optional[int]     )  ->str:
										lowercase         =      TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]     )

										lowercase         =      self.default_image_processor
										lowercase         =      prepare_img()
										lowercase         =      image_processor(images=lowerCAmelCase__		,    return_tensors="tf"     )

										# forward pass
										lowercase         =      model(**lowerCAmelCase__		,    training=lowerCAmelCase__     )

										# verify the logits
										lowercase         =      tf.TensorShape((1, 1000)     )
										self.assertEqual(outputs.logits.shape		,    lowerCAmelCase__     )

										lowercase         =      tf.constant([-0.41_80, -1.50_51, -3.48_36]     )

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


from __future__ import annotations

def  __snake_case							(	__magic_name__						):







				'''simple docstring'''


				if len(__magic_name__						) == 0:
								return []
				lowercase     ,	lowercase         =      min(__magic_name__						), max(__magic_name__						)
				lowercase         =      int(max_value - min_value						) + 1
				lowercase         =      [[] for _ in range(__magic_name__						)]

				for i in my_list:
								buckets[int(i - min_value						)].append(__magic_name__						)

				return [v for bucket in buckets for v in sorted(__magic_name__						)]


if __name__ == "__main__":
						from doctest import testmod

						testmod()
						assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5]
						assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]
