


"""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 	_a   :
    def __init__(			self				:       Union[str, Any]				,       SCREAMING_SNAKE_CASE__				:       Dict				,       SCREAMING_SNAKE_CASE__				:       List[str]=3				,       SCREAMING_SNAKE_CASE__				:       List[Any]=32				,       SCREAMING_SNAKE_CASE__				:       int=3				,       SCREAMING_SNAKE_CASE__				:       str=10				,       SCREAMING_SNAKE_CASE__				:       List[Any]=[10, 20, 30, 40]				,       SCREAMING_SNAKE_CASE__				:       Union[str, Any]=[1, 1, 2, 1]				,       SCREAMING_SNAKE_CASE__				:       Any=True				,       SCREAMING_SNAKE_CASE__				:       Optional[int]=True				,       SCREAMING_SNAKE_CASE__				:       int="relu"				,       SCREAMING_SNAKE_CASE__				:       Dict=3				,       SCREAMING_SNAKE_CASE__				:       Dict=None				,       ):
          lowerCamelCase__							     =		parent
          lowerCamelCase__							     =		batch_size
          lowerCamelCase__							     =		image_size
          lowerCamelCase__							     =		num_channels
          lowerCamelCase__							     =		embeddings_size
          lowerCamelCase__							     =		hidden_sizes
          lowerCamelCase__							     =		depths
          lowerCamelCase__							     =		is_training
          lowerCamelCase__							     =		use_labels
          lowerCamelCase__							     =		hidden_act
          lowerCamelCase__							     =		num_labels
          lowerCamelCase__							     =		scope
          lowerCamelCase__							     =		len(__A					)
    def   _UpperCamelCase							(			self				:       Dict					):
          lowerCamelCase__							     =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

          lowerCamelCase__							     =		self.get_config()
          return config, pixel_values, labels
    def   _UpperCamelCase							(			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   _UpperCamelCase							(			self				:       str				,       SCREAMING_SNAKE_CASE__				:       Optional[Any]				,       SCREAMING_SNAKE_CASE__				:       Tuple				,       SCREAMING_SNAKE_CASE__				:       List[str]					):
          lowerCamelCase__							     =		TFRegNetModel(config=__A					)
          lowerCamelCase__							     =		model(__A				,       training=__A					)
          # 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				:       Dict				,       SCREAMING_SNAKE_CASE__				:       str				,       SCREAMING_SNAKE_CASE__				:       Tuple				,       SCREAMING_SNAKE_CASE__				:       List[Any]					):
          lowerCamelCase__							     =		self.num_labels
          lowerCamelCase__							     =		TFRegNetForImageClassification(__A					)
          lowerCamelCase__							     =		model(__A				,       labels=__A				,       training=__A					)
          self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_labels)					)


    def   _UpperCamelCase							(			self				:       str					):
          lowerCamelCase__							     =		self.prepare_config_and_inputs()
          lowerCamelCase__							     =		config_and_inputs
          lowerCamelCase__							     =		{"""pixel_values""": pixel_values}
          return config, inputs_dict





@require_tf
class 	_a   (   _lowercase		,		_lowercase		,		unittest.TestCase			):
    a_     :					Optional[Any]   							=     (TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
    a_     :					Tuple   							=     (
        {'''feature-extraction''': TFRegNetModel, '''image-classification''': TFRegNetForImageClassification}
        if is_tf_available()
        else {}
    )

    a_     :					Union[str, Any]   							=     False
    a_     :					Any   							=     False
    a_     :					Optional[int]   							=     False
    a_     :					Optional[Any]   							=     False
    a_     :					Union[str, Any]   							=     False
    def   _UpperCamelCase							(			self				:       Dict					):
          lowerCamelCase__							     =		TFRegNetModelTester(self					)
          lowerCamelCase__							     =		ConfigTester(self				,       config_class=__A				,       has_text_modality=__A					)
    def   _UpperCamelCase							(			self				:       List[str]					):
          return
    @unittest.skip(reason='RegNet does not use inputs_embeds'					)
    def   _UpperCamelCase							(			self				:       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   _UpperCamelCase							(			self				:       Dict					):
          super().test_keras_fit()
    @unittest.skip(reason='RegNet does not support input and output embeddings'					)
    def   _UpperCamelCase							(			self				:       Optional[int]					):
          pass
    def   _UpperCamelCase							(			self				:       str					):
          lowerCamelCase__							     =		self.model_tester.prepare_config_and_inputs_for_common()

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

                lowerCamelCase__							     =		["""pixel_values"""]
                self.assertListEqual(arg_names[:1]				,       __A					)
    def   _UpperCamelCase							(			self				:       Any					):
          lowerCamelCase__							     =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__A					)
    def   _UpperCamelCase							(			self				:       List[str]					):
          def check_hidden_states_output(SCREAMING_SNAKE_CASE__				:       Optional[int]				,       SCREAMING_SNAKE_CASE__				:       str				,       SCREAMING_SNAKE_CASE__				:       str					):
                lowerCamelCase__							     =		model_class(__A					)
                lowerCamelCase__							     =		model(**self._prepare_for_class(__A				,       __A					)				,       training=__A					)

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

                lowerCamelCase__							     =		self.model_tester.num_stages
                self.assertEqual(len(__A					)				,       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__							     =		self.model_tester.prepare_config_and_inputs_for_common()
          lowerCamelCase__							     =		["""basic""", """bottleneck"""]
          for model_class in self.all_model_classes:
                for layer_type in layers_type:
                      lowerCamelCase__							     =		layer_type
                      lowerCamelCase__							     =		True
                      check_hidden_states_output(__A				,       __A				,       __A					)

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

                      check_hidden_states_output(__A				,       __A				,       __A					)
    def   _UpperCamelCase							(			self				:       Dict					):
          lowerCamelCase__							     =		self.model_tester.prepare_config_and_inputs_for_common()

          def check_equivalence(SCREAMING_SNAKE_CASE__				:       int				,       SCREAMING_SNAKE_CASE__				:       Any				,       SCREAMING_SNAKE_CASE__				:       List[Any]				,       SCREAMING_SNAKE_CASE__				:       Any={}					):
                lowerCamelCase__							     =		model(__A				,       return_dict=__A				,       **__A					)
                lowerCamelCase__							     =		model(__A				,       return_dict=__A				,       **__A					).to_tuple()

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

                      recursive_check(__A				,       __A					)

          for model_class in self.all_model_classes:
                lowerCamelCase__							     =		model_class(__A					)

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

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

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

                lowerCamelCase__							     =		self._prepare_for_class(__A				,       __A				,       return_labels=__A					)
                lowerCamelCase__							     =		self._prepare_for_class(__A				,       __A				,       return_labels=__A					)
                check_equivalence(__A				,       __A				,       __A				,       {'output_hidden_states': True}					)
    def   _UpperCamelCase							(			self				:       List[str]					):
          lowerCamelCase__							     =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*__A					)


    @slow
    def   _UpperCamelCase							(			self				:       int					):
          for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                lowerCamelCase__							     =		TFRegNetModel.from_pretrained(__A					)
                self.assertIsNotNone(__A					)





def 	snake_case    (					)->		List[str]:
      '''simple docstring'''


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






@require_tf
@require_vision
class 	_a   (   unittest.TestCase			):
    @cached_property
    def   _UpperCamelCase							(			self				:       Union[str, Any]					):
          return (
              AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]					)
              if is_vision_available()
              else None
          )


    @slow
    def   _UpperCamelCase							(			self				:       str					):
          lowerCamelCase__							     =		TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]					)

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

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

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

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

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






import math






class      lowerCamelCase_      :




	def __init__(     self						:     Union[str, Any]					,      __A						:     List[str]=0							):  # a graph with Node 0,1,...,N-1
		__A :			List[str]		    =			n
		__A :			List[str]		    =			[
		    [math.inf for j in range(0					,      __A							)] for i in range(0					,      __A							)
		]  # adjacency matrix for weight
		__A :			str		    =			[
		    [math.inf for j in range(0					,      __A							)] for i in range(0					,      __A							)
		]  # dp[i][j] stores minimum distance from i to j




	def        lowerCAmelCase_       (     self						:     str					,      __A						:     Union[str, Any]					,      __A						:     Any					,      __A						:     Optional[int]							):
		__A :			List[Any]		    =			w




	def        lowerCAmelCase_       (     self						:     Union[str, Any]							):
		for k in range(0					,      self.n							):
			for i in range(0					,      self.n							):
				for j in range(0					,      self.n							):
					__A :			List[Any]		    =			min(self.dp[i][j]					,      self.dp[i][k] + self.dp[k][j]							)




	def        lowerCAmelCase_       (     self						:     int					,      __A						:     List[str]					,      __A						:     List[str]							):
		return self.dp[u][v]


if __name__ == "__main__":
			UpperCAmelCase_							:					Tuple		 =   Graph(5)
			graph.add_edge(0, 2, 9)
			graph.add_edge(0, 4, 10)
			graph.add_edge(1, 3, 5)
			graph.add_edge(2, 3, 7)
			graph.add_edge(3, 0, 10)
			graph.add_edge(3, 1, 2)
			graph.add_edge(3, 2, 1)
			graph.add_edge(3, 4, 6)
			graph.add_edge(4, 1, 3)
			graph.add_edge(4, 2, 4)
			graph.add_edge(4, 3, 9)
			graph.floyd_warshall()
			graph.show_min(1, 4)
			graph.show_min(0, 3)





