





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 		SCREAMING_SNAKE_CASE_							:
				"""simple docstring"""




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




				def 					__lowercase  (     self  :Optional[Any]			):
											__lowerCamelCase      :      str         =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

											__lowerCamelCase      :      Union[str, Any]         =None
											if self.use_labels:
																		__lowerCamelCase      :      int         =ids_tensor([self.batch_size]						,    self.num_labels			)

											__lowerCamelCase      :      Tuple         =self.get_config()
											return config, pixel_values, labels




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




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




				def 					__lowercase  (     self  :Any			):
											__lowerCamelCase      :      Union[str, Any]         =self.prepare_config_and_inputs()
											__lowerCamelCase      :      Tuple         =config_and_inputs
											__lowerCamelCase      :      List[Any]         ={'''pixel_values''': pixel_values}
											return config, inputs_dict

@require_tf
class 		SCREAMING_SNAKE_CASE_							(    snake_case__       ,  snake_case__       ,  unittest.TestCase   ):
				"""simple docstring"""
				__snake_case   :       List[str]	 =		(TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
				__snake_case   :       str	 =		(
				    {"""feature-extraction""": TFRegNetModel, """image-classification""": TFRegNetForImageClassification}
				    if is_tf_available()
				    else {}
				)

				__snake_case   :       Any	 =		False
				__snake_case   :       List[str]	 =		False
				__snake_case   :       int	 =		False
				__snake_case   :       Optional[Any]	 =		False
				__snake_case   :       Optional[int]	 =		False




				def 					__lowercase  (     self  :Dict			):
											__lowerCamelCase      :      Dict         =TFRegNetModelTester(self			)
											__lowerCamelCase      :      Union[str, Any]         =ConfigTester(self						,    config_class=__lowercase						,    has_text_modality=__lowercase			)




				def 					__lowercase  (     self  :Union[str, Any]			):
											return




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




				@unittest.skip(reason='''RegNet does not support input and output embeddings'''			)
				def 					__lowercase  (     self  :int			):
											pass




				def 					__lowercase  (     self  :Any			):
											__lowerCamelCase      :      str         =self.model_tester.prepare_config_and_inputs_for_common()

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

																		__lowerCamelCase      :      int         =['''pixel_values''']
																		self.assertListEqual(arg_names[:1]						,    __lowercase			)




				def 					__lowercase  (     self  :Any			):
											__lowerCamelCase      :      str         =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__lowercase			)




				def 					__lowercase  (     self  :List[str]			):
											def check_hidden_states_output(__lowercase  :Dict						,    __lowercase  :List[str]						,    __lowercase  :Union[str, Any]			):
																		__lowerCamelCase      :      List[str]         =model_class(__lowercase			)
																		__lowerCamelCase      :      int         =model(**self._prepare_for_class(__lowercase						,    __lowercase			)						,    training=__lowercase			)

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

																		__lowerCamelCase      :      Union[str, Any]         =self.model_tester.num_stages
																		self.assertEqual(len(__lowercase			)						,    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]						,    )

											__lowerCamelCase      :      Dict         =self.model_tester.prepare_config_and_inputs_for_common()
											__lowerCamelCase      :      Optional[Any]         =['''basic''', '''bottleneck''']
											for model_class in self.all_model_classes:
																		for layer_type in layers_type:
																									__lowerCamelCase      :      Tuple         =layer_type
																									__lowerCamelCase      :      Any         =True
																									check_hidden_states_output(__lowercase						,    __lowercase						,    __lowercase			)

																									# check that output_hidden_states also work using config
																									del inputs_dict["output_hidden_states"]
																									__lowerCamelCase      :      int         =True

																									check_hidden_states_output(__lowercase						,    __lowercase						,    __lowercase			)




				def 					__lowercase  (     self  :str			):
											__lowerCamelCase      :      Union[str, Any]         =self.model_tester.prepare_config_and_inputs_for_common()

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

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

																									recursive_check(__lowercase						,    __lowercase			)

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

																		__lowerCamelCase      :      List[Any]         =self._prepare_for_class(__lowercase						,    __lowercase			)
																		__lowerCamelCase      :      Optional[Any]         =self._prepare_for_class(__lowercase						,    __lowercase			)
																		check_equivalence(__lowercase						,    __lowercase						,    __lowercase			)

																		__lowerCamelCase      :      int         =self._prepare_for_class(__lowercase						,    __lowercase						,    return_labels=__lowercase			)
																		__lowerCamelCase      :      Any         =self._prepare_for_class(__lowercase						,    __lowercase						,    return_labels=__lowercase			)
																		check_equivalence(__lowercase						,    __lowercase						,    __lowercase			)

																		__lowerCamelCase      :      Union[str, Any]         =self._prepare_for_class(__lowercase						,    __lowercase			)
																		__lowerCamelCase      :      Dict         =self._prepare_for_class(__lowercase						,    __lowercase			)
																		check_equivalence(__lowercase						,    __lowercase						,    __lowercase						,    {'''output_hidden_states''': True}			)

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




				def 					__lowercase  (     self  :Tuple			):
											__lowerCamelCase      :      List[Any]         =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*__lowercase			)




				@slow
				def 					__lowercase  (     self  :List[Any]			):
											for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		__lowerCamelCase      :      Any         =TFRegNetModel.from_pretrained(__lowercase			)
																		self.assertIsNotNone(__lowercase			)



def 				lowerCAmelCase_			(      ):


							'''simple docstring'''







							__lowerCamelCase      :      Union[str, Any]         =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
							return image
@require_tf
@require_vision
class 		SCREAMING_SNAKE_CASE_							(    unittest.TestCase   ):
				"""simple docstring"""




				@cached_property
				def 					__lowercase  (     self  :Union[str, Any]			):
											return (
											    AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]			)
											    if is_vision_available()
											    else None
											)




				@slow
				def 					__lowercase  (     self  :Any			):
											__lowerCamelCase      :      List[Any]         =TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]			)

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

											# forward pass
											__lowerCamelCase      :      int         =model(**__lowercase						,    training=__lowercase			)

											# verify the logits
											__lowerCamelCase      :      Optional[Any]         =tf.TensorShape((1, 1000)			)
											self.assertEqual(outputs.logits.shape						,    __lowercase			)

											__lowerCamelCase      :      Dict         =tf.constant([-0.4180, -1.5051, -3.4836]			)

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











"""simple docstring"""





import unittest

from .lib import (
    Matrix,
    Vector,
    axpy,
    square_zero_matrix,
    unit_basis_vector,
    zero_vector,
)
class 		SCREAMING_SNAKE_CASE_							(    unittest.TestCase   ):
				"""simple docstring"""




				def 					__lowercase  (     self  :Tuple			):
											__lowerCamelCase      :      Any         =Vector([1, 2, 3]			)
											self.assertEqual(x.component(0			)						,    1			)
											self.assertEqual(x.component(2			)						,    3			)
											__lowerCamelCase      :      Any         =Vector()




				def 					__lowercase  (     self  :Dict			):
											__lowerCamelCase      :      Tuple         =Vector([0, 0, 0, 0, 0, 1]			)
											self.assertEqual(str(__lowercase			)						,    '''(0,0,0,0,0,1)'''			)




				def 					__lowercase  (     self  :Dict			):
											__lowerCamelCase      :      int         =Vector([1, 2, 3, 4]			)
											self.assertEqual(len(__lowercase			)						,    4			)




				def 					__lowercase  (     self  :Dict			):
											__lowerCamelCase      :      Optional[Any]         =Vector([1, 2]			)
											__lowerCamelCase      :      Dict         =Vector([1, 2, 3, 4, 5]			)
											__lowerCamelCase      :      List[Any]         =Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]			)
											__lowerCamelCase      :      int         =Vector([1, -1, 1, -1, 2, -3, 4, -5]			)
											self.assertAlmostEqual(x.euclidean_length()						,    2.236						,    3			)
											self.assertAlmostEqual(y.euclidean_length()						,    7.416						,    3			)
											self.assertEqual(z.euclidean_length()						,    0			)
											self.assertAlmostEqual(w.euclidean_length()						,    7.616						,    3			)




				def 					__lowercase  (     self  :Optional[int]			):
											__lowerCamelCase      :      Tuple         =Vector([1, 2, 3]			)
											__lowerCamelCase      :      Any         =Vector([1, 1, 1]			)
											self.assertEqual((x + y).component(0			)						,    2			)
											self.assertEqual((x + y).component(1			)						,    3			)
											self.assertEqual((x + y).component(2			)						,    4			)




				def 					__lowercase  (     self  :str			):
											__lowerCamelCase      :      Union[str, Any]         =Vector([1, 2, 3]			)
											__lowerCamelCase      :      int         =Vector([1, 1, 1]			)
											self.assertEqual((x - y).component(0			)						,    0			)
											self.assertEqual((x - y).component(1			)						,    1			)
											self.assertEqual((x - y).component(2			)						,    2			)




				def 					__lowercase  (     self  :int			):
											__lowerCamelCase      :      List[Any]         =Vector([1, 2, 3]			)
											__lowerCamelCase      :      List[Any]         =Vector([2, -1, 4]			)  # for test of dot product
											__lowerCamelCase      :      Any         =Vector([1, -2, -1]			)
											self.assertEqual(str(x * 3.0			)						,    '''(3.0,6.0,9.0)'''			)
											self.assertEqual((a * b)						,    0			)




				def 					__lowercase  (     self  :List[Any]			):
											self.assertEqual(str(zero_vector(10			)			).count('''0'''			)						,    10			)




				def 					__lowercase  (     self  :Union[str, Any]			):
											self.assertEqual(str(unit_basis_vector(3						,    1			)			)						,    '''(0,1,0)'''			)




				def 					__lowercase  (     self  :List[Any]			):
											__lowerCamelCase      :      Any         =Vector([1, 2, 3]			)
											__lowerCamelCase      :      Optional[int]         =Vector([1, 0, 1]			)
											self.assertEqual(str(axpy(2						,    __lowercase						,    __lowercase			)			)						,    '''(3,4,7)'''			)




				def 					__lowercase  (     self  :Dict			):
											__lowerCamelCase      :      List[Any]         =Vector([1, 0, 0, 0, 0, 0]			)
											__lowerCamelCase      :      Optional[int]         =x.copy()
											self.assertEqual(str(__lowercase			)						,    str(__lowercase			)			)




				def 					__lowercase  (     self  :int			):
											__lowerCamelCase      :      str         =Vector([1, 0, 0]			)
											x.change_component(0						,    0			)
											x.change_component(1						,    1			)
											self.assertEqual(str(__lowercase			)						,    '''(0,1,0)'''			)




				def 					__lowercase  (     self  :int			):
											__lowerCamelCase      :      Any         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											self.assertEqual('''|1,2,3|\n|2,4,5|\n|6,7,8|\n'''						,    str(__lowercase			)			)




				def 					__lowercase  (     self  :int			):
											__lowerCamelCase      :      Tuple         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											__lowerCamelCase      :      List[Any]         =[[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]]
											for x in range(a.height()			):
																		for y in range(a.width()			):
																									self.assertEqual(minors[x][y]						,    a.minor(__lowercase						,    __lowercase			)			)




				def 					__lowercase  (     self  :Optional[int]			):
											__lowerCamelCase      :      Optional[Any]         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											__lowerCamelCase      :      Tuple         =[[-3, 14, -10], [5, -10, 5], [-2, 1, 0]]
											for x in range(a.height()			):
																		for y in range(a.width()			):
																									self.assertEqual(cofactors[x][y]						,    a.cofactor(__lowercase						,    __lowercase			)			)




				def 					__lowercase  (     self  :Tuple			):
											__lowerCamelCase      :      Tuple         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											self.assertEqual(-5						,    a.determinant()			)




				def 					__lowercase  (     self  :int			):
											__lowerCamelCase      :      Union[str, Any]         =Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]						,    3						,    3			)
											__lowerCamelCase      :      Tuple         =Vector([1, 2, 3]			)
											self.assertEqual('''(14,32,50)'''						,    str(a * x			)			)
											self.assertEqual('''|2,4,6|\n|8,10,12|\n|14,16,18|\n'''						,    str(a * 2			)			)




				def 					__lowercase  (     self  :Optional[Any]			):
											__lowerCamelCase      :      Optional[int]         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											a.change_component(0						,    2						,    5			)
											self.assertEqual('''|1,2,5|\n|2,4,5|\n|6,7,8|\n'''						,    str(__lowercase			)			)




				def 					__lowercase  (     self  :str			):
											__lowerCamelCase      :      str         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											self.assertEqual(7						,    a.component(2						,    1			)						,    0.01			)




				def 					__lowercase  (     self  :Optional[int]			):
											__lowerCamelCase      :      List[str]         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											__lowerCamelCase      :      List[str]         =Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]						,    3						,    3			)
											self.assertEqual('''|2,4,10|\n|4,8,10|\n|12,14,18|\n'''						,    str(a + b			)			)




				def 					__lowercase  (     self  :Union[str, Any]			):
											__lowerCamelCase      :      int         =Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]						,    3						,    3			)
											__lowerCamelCase      :      Optional[int]         =Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]						,    3						,    3			)
											self.assertEqual('''|0,0,-4|\n|0,0,0|\n|0,0,-2|\n'''						,    str(a - b			)			)




				def 					__lowercase  (     self  :Any			):
											self.assertEqual(
											    '''|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n'''						,    str(square_zero_matrix(5			)			)						,    )


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




