






"""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 					snake_case		:


   '''simple docstring'''

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

     '''simple docstring'''
     __A									= parent
     __A									= batch_size
     __A									= image_size
     __A									= num_channels
     __A									= embeddings_size
     __A									= hidden_sizes
     __A									= depths
     __A									= is_training
     __A									= use_labels
     __A									= hidden_act
     __A									= num_labels
     __A									= scope
     __A									= len(_lowerCamelCase  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Any  ):

     '''simple docstring'''
     __A									= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

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


   def 				_SCREAMING_SNAKE_CASE				(	self :     Dict  ):

     '''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 				_SCREAMING_SNAKE_CASE				(	self :     str,       _lowerCamelCase :     int,       _lowerCamelCase :     int,       _lowerCamelCase :     Dict  ):

     '''simple docstring'''
     __A									= TFRegNetModel(config=_lowerCamelCase  )
     __A									= 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 :     int,       _lowerCamelCase :     List[str],       _lowerCamelCase :     Optional[Any],       _lowerCamelCase :     Any  ):

     '''simple docstring'''
     __A									= self.num_labels
     __A									= TFRegNetForImageClassification(_lowerCamelCase  )
     __A									= model(_lowerCamelCase,       labels=_lowerCamelCase,       training=_lowerCamelCase  )
     self.parent.assertEqual(result.logits.shape,       (self.batch_size, self.num_labels)  )






   def 				_SCREAMING_SNAKE_CASE				(	self :     Tuple  ):

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





@require_tf
class 					snake_case		(							_lowerCAmelCase   ,	_lowerCAmelCase   ,	unittest.TestCase						):


   '''simple docstring'''

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

   A_						:	Any						 = False
   A_						:	Optional[Any]						 = False
   A_						:	List[str]						 = False
   A_						:	Tuple						 = False
   A_						:	List[str]						 = False


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int]  ):

     '''simple docstring'''
     __A									= TFRegNetModelTester(self  )
     __A									= ConfigTester(self,       config_class=_lowerCamelCase,       has_text_modality=_lowerCamelCase  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     int  ):

     '''simple docstring'''
     return


   @unittest.skip(reason='''RegNet does not use inputs_embeds'''  )
   def 				_SCREAMING_SNAKE_CASE				(	self :     int  ):

     '''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 				_SCREAMING_SNAKE_CASE				(	self :     List[Any]  ):

     '''simple docstring'''
     super().test_keras_fit()


   @unittest.skip(reason='''RegNet does not support input and output embeddings'''  )
   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int]  ):

     '''simple docstring'''
     pass


   def 				_SCREAMING_SNAKE_CASE				(	self :     Tuple  ):

     '''simple docstring'''
     __A			,    __A									= self.model_tester.prepare_config_and_inputs_for_common()

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

       __A									= ['''pixel_values''']
       self.assertListEqual(arg_names[:1],       _lowerCamelCase  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     __A									= self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_lowerCamelCase  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Union[str, Any]  ):

     '''simple docstring'''
     def check_hidden_states_output(_lowerCamelCase :     Optional[Any],       _lowerCamelCase :     int,       _lowerCamelCase :     List[str]  ):
       __A									= model_class(_lowerCamelCase  )
       __A									= model(**self._prepare_for_class(_lowerCamelCase,       _lowerCamelCase  ),       training=_lowerCamelCase  )

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

       __A									= 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],       )

     __A			,    __A									= self.model_tester.prepare_config_and_inputs_for_common()
     __A									= ['''basic''', '''bottleneck''']
     for model_class in self.all_model_classes:
       for layer_type in layers_type:
         __A									= layer_type
         __A									= True
         check_hidden_states_output(_lowerCamelCase,       _lowerCamelCase,       _lowerCamelCase  )

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

         check_hidden_states_output(_lowerCamelCase,       _lowerCamelCase,       _lowerCamelCase  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     str  ):

     '''simple docstring'''
     __A			,    __A									= self.model_tester.prepare_config_and_inputs_for_common()

     def check_equivalence(_lowerCamelCase :     Dict,       _lowerCamelCase :     Tuple,       _lowerCamelCase :     str,       _lowerCamelCase :     int={}  ):
       __A									= model(_lowerCamelCase,       return_dict=_lowerCamelCase,       **_lowerCamelCase  )
       __A									= model(_lowerCamelCase,       return_dict=_lowerCamelCase,       **_lowerCamelCase  ).to_tuple()

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

       __A									= self._prepare_for_class(_lowerCamelCase,       _lowerCamelCase  )
       __A									= self._prepare_for_class(_lowerCamelCase,       _lowerCamelCase  )
       check_equivalence(_lowerCamelCase,       _lowerCamelCase,       _lowerCamelCase  )

       __A									= self._prepare_for_class(_lowerCamelCase,       _lowerCamelCase,       return_labels=_lowerCamelCase  )
       __A									= self._prepare_for_class(_lowerCamelCase,       _lowerCamelCase,       return_labels=_lowerCamelCase  )
       check_equivalence(_lowerCamelCase,       _lowerCamelCase,       _lowerCamelCase  )

       __A									= self._prepare_for_class(_lowerCamelCase,       _lowerCamelCase  )
       __A									= self._prepare_for_class(_lowerCamelCase,       _lowerCamelCase  )
       check_equivalence(_lowerCamelCase,       _lowerCamelCase,       _lowerCamelCase,       {'''output_hidden_states''': True}  )

       __A									= self._prepare_for_class(_lowerCamelCase,       _lowerCamelCase,       return_labels=_lowerCamelCase  )
       __A									= self._prepare_for_class(_lowerCamelCase,       _lowerCamelCase,       return_labels=_lowerCamelCase  )
       check_equivalence(_lowerCamelCase,       _lowerCamelCase,       _lowerCamelCase,       {'''output_hidden_states''': True}  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Any  ):

     '''simple docstring'''
     __A									= self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase  )






   @slow
   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __A									= TFRegNetModel.from_pretrained(_lowerCamelCase  )
       self.assertIsNotNone(_lowerCamelCase  )





def 					lowerCAmelCase				(    ):




  """simple docstring"""







  __A									= Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
  return image





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


   '''simple docstring'''

   @cached_property
   def 				_SCREAMING_SNAKE_CASE				(	self :     int  ):

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






   @slow
   def 				_SCREAMING_SNAKE_CASE				(	self :     List[str]  ):

     '''simple docstring'''
     __A									= TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]  )

     __A									= self.default_image_processor
     __A									= prepare_img()
     __A									= image_processor(images=_lowerCamelCase,       return_tensors='''tf'''  )

     # forward pass
     __A									= model(**_lowerCamelCase,       training=_lowerCamelCase  )

     # verify the logits
     __A									= tf.TensorShape((1, 10_00)  )
     self.assertEqual(outputs.logits.shape,       _lowerCamelCase  )

     __A									= 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"""


from __future__ import annotations


class 					snake_case		:


   '''simple docstring'''

   def __init__(	self :     int,       _lowerCamelCase :     List[Any]=None  ):

     '''simple docstring'''
     __A									= data
     __A									= None






   def __repr__(	self :     Union[str, Any]  ):

     '''simple docstring'''
     __A									= []
     __A									= self
     while temp:
       string_rep.append(f'{temp.data}'  )
       __A									= temp.next
     return "->".join(_lowerCamelCase  )





def 					lowerCAmelCase				(    __UpperCamelCase ):




  """simple docstring"""







  if not elements_list:
    raise Exception('''The Elements List is empty''' )

  __A									= __A									= Node(elements_list[0] )
  for i in range(1		,       len(__UpperCamelCase ) ):
    __A									= Node(elements_list[i] )
    __A									= current.next
  return head



def 					lowerCAmelCase				(    __UpperCamelCase ):




  """simple docstring"""







  if head_node is not None and isinstance(__UpperCamelCase		,       __UpperCamelCase ):
    print_reverse(head_node.next )
    print(head_node.data )



def 					lowerCAmelCase				(    ):




  """simple docstring"""







  from doctest import testmod

  testmod()

  __A									= make_linked_list([1_4, 5_2, 1_4, 1_2, 4_3] )
  print('''Linked List:''' )
  print(__UpperCamelCase )
  print('''Elements in Reverse:''' )
  print_reverse(__UpperCamelCase )


if __name__ == "__main__":
       main()




