from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
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 TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
   from PIL import Image

   from transformers import ConvNextImageProcessor





class 	_lowercase		:

 def __init__(				self			:							List[Any]     ,   snake_case			:							Optional[int]     ,   snake_case			:							Tuple=1_3     ,   snake_case			:							str=3_2     ,   snake_case			:							List[str]=3     ,   snake_case			:							str=4     ,   snake_case			:							int=[1_0, 2_0, 3_0, 4_0]     ,   snake_case			:							Dict=[2, 2, 3, 2]     ,   snake_case			:							Union[str, Any]=True     ,   snake_case			:							Union[str, Any]=True     ,   snake_case			:							Any=3_7     ,   snake_case			:							List[str]="gelu"     ,   snake_case			:							Optional[Any]=1_0     ,   snake_case			:							Optional[int]=0.02     ,   snake_case			:							Dict=3     ,   snake_case			:							List[Any]=None     ,   )     ->		str:

  """simple docstring"""





  UpperCamelCase_      :		Dict				=      parent
  UpperCamelCase_      :		Any				=      batch_size
  UpperCamelCase_      :		Optional[Any]				=      image_size
  UpperCamelCase_      :		List[Any]				=      num_channels
  UpperCamelCase_      :		int				=      num_stages
  UpperCamelCase_      :		int				=      hidden_sizes
  UpperCamelCase_      :		Optional[int]				=      depths
  UpperCamelCase_      :		str				=      is_training
  UpperCamelCase_      :		List[str]				=      use_labels
  UpperCamelCase_      :		Optional[int]				=      intermediate_size
  UpperCamelCase_      :		Dict				=      hidden_act
  UpperCamelCase_      :		Optional[int]				=      type_sequence_label_size
  UpperCamelCase_      :		Optional[int]				=      initializer_range
  UpperCamelCase_      :		List[Any]				=      scope

 def 		SCREAMING_SNAKE_CASE__     (				self			:							Optional[int]    )     ->		int:

  """simple docstring"""





  UpperCamelCase_      :		Tuple				=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

  UpperCamelCase_      :		List[str]				=      None
  if self.use_labels:
   UpperCamelCase_      :		Optional[Any]				=      ids_tensor([self.batch_size]     ,   self.type_sequence_label_size    )

  UpperCamelCase_      :		Optional[int]				=      self.get_config()

  return config, pixel_values, labels

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

  """simple docstring"""





  return ConvNextConfig(
      num_channels=self.num_channels     ,   hidden_sizes=self.hidden_sizes     ,   depths=self.depths     ,   num_stages=self.num_stages     ,   hidden_act=self.hidden_act     ,   is_decoder=snake_case     ,   initializer_range=self.initializer_range     ,   )

 def 		SCREAMING_SNAKE_CASE__     (				self			:							Tuple     ,   snake_case			:							List[Any]     ,   snake_case			:							Any     ,   snake_case			:							Optional[int]    )     ->		Tuple:

  """simple docstring"""





  UpperCamelCase_      :		List[Any]				=      TFConvNextModel(config=snake_case    )
  UpperCamelCase_      :		List[str]				=      model(snake_case     ,   training=snake_case    )
  # 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 // 3_2, self.image_size // 3_2)     ,   )

 def 		SCREAMING_SNAKE_CASE__     (				self			:							Any     ,   snake_case			:							str     ,   snake_case			:							Union[str, Any]     ,   snake_case			:							Any    )     ->		int:

  """simple docstring"""





  UpperCamelCase_      :		str				=      self.type_sequence_label_size
  UpperCamelCase_      :		Optional[Any]				=      TFConvNextForImageClassification(snake_case    )
  UpperCamelCase_      :		Union[str, Any]				=      model(snake_case     ,   labels=snake_case     ,   training=snake_case    )
  self.parent.assertEqual(result.logits.shape     ,   (self.batch_size, self.type_sequence_label_size)    )


 def 		SCREAMING_SNAKE_CASE__     (				self			:							List[str]    )     ->		Any:

  """simple docstring"""





  UpperCamelCase_      :		List[str]				=      self.prepare_config_and_inputs()
  UpperCamelCase_,				UpperCamelCase_,				UpperCamelCase_      :		Optional[Any]				=      config_and_inputs
  UpperCamelCase_      :		int				=      {'pixel_values': pixel_values}
  return config, inputs_dict





@require_tf
class 	_lowercase		(      snake_case_		,   snake_case_		,   unittest.TestCase ):
 lowercase         =     (TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
 lowercase         =     (
     {'feature-extraction': TFConvNextModel, 'image-classification': TFConvNextForImageClassification}
     if is_tf_available()
     else {}
 )

 lowercase         =     False
 lowercase         =     False
 lowercase         =     False
 lowercase         =     False
 lowercase         =     False

 def 		SCREAMING_SNAKE_CASE__     (				self			:							int    )     ->		Optional[int]:

  """simple docstring"""





  UpperCamelCase_      :		str				=      TFConvNextModelTester(self    )
  UpperCamelCase_      :		List[Any]				=      ConfigTester(
      self     ,   config_class=snake_case     ,   has_text_modality=snake_case     ,   hidden_size=3_7     ,   )

 @unittest.skip(reason='ConvNext does not use inputs_embeds'    )
 def 		SCREAMING_SNAKE_CASE__     (				self			:							Union[str, Any]    )     ->		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 		SCREAMING_SNAKE_CASE__     (				self			:							int    )     ->		List[str]:

  """simple docstring"""





  super().test_keras_fit()

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

  """simple docstring"""





  pass

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

  """simple docstring"""





  UpperCamelCase_,				UpperCamelCase_      :		Any				=      self.model_tester.prepare_config_and_inputs_for_common()

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

   UpperCamelCase_      :		Tuple				=      ['pixel_values']
   self.assertListEqual(arg_names[:1]     ,   snake_case    )

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

  """simple docstring"""





  UpperCamelCase_      :		Optional[int]				=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_model(*snake_case    )

 @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.'     ,   )
 def 		SCREAMING_SNAKE_CASE__     (				self			:							Union[str, Any]    )     ->		Dict:

  """simple docstring"""





  super().test_dataset_conversion()

 def 		SCREAMING_SNAKE_CASE__     (				self			:							Dict    )     ->		List[str]:

  """simple docstring"""





  def check_hidden_states_output(snake_case			:							Union[str, Any]     ,   snake_case			:							str     ,   snake_case			:							Dict    ):
   UpperCamelCase_      :		Any				=      model_class(snake_case    )

   UpperCamelCase_      :		Dict				=      model(**self._prepare_for_class(snake_case     ,   snake_case    )    )
   UpperCamelCase_      :		int				=      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

   UpperCamelCase_      :		Optional[int]				=      self.model_tester.num_stages
   self.assertEqual(len(snake_case    )     ,   expected_num_stages + 1    )

   # ConvNext'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 // 4, self.model_tester.image_size // 4]     ,   )

  UpperCamelCase_,				UpperCamelCase_      :		Tuple				=      self.model_tester.prepare_config_and_inputs_for_common()

  for model_class in self.all_model_classes:
   UpperCamelCase_      :		Union[str, Any]				=      True
   check_hidden_states_output(snake_case     ,   snake_case     ,   snake_case    )

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

   check_hidden_states_output(snake_case     ,   snake_case     ,   snake_case    )

 def 		SCREAMING_SNAKE_CASE__     (				self			:							Any    )     ->		Optional[int]:

  """simple docstring"""





  UpperCamelCase_,				UpperCamelCase_      :		Optional[int]				=      self.model_tester.prepare_config_and_inputs_for_common()

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

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

    recursive_check(snake_case     ,   snake_case    )

  for model_class in self.all_model_classes:
   UpperCamelCase_      :		Optional[Any]				=      model_class(snake_case    )

   UpperCamelCase_      :		List[Any]				=      self._prepare_for_class(snake_case     ,   snake_case    )
   UpperCamelCase_      :		List[str]				=      self._prepare_for_class(snake_case     ,   snake_case    )
   check_equivalence(snake_case     ,   snake_case     ,   snake_case    )

   UpperCamelCase_      :		List[Any]				=      self._prepare_for_class(snake_case     ,   snake_case     ,   return_labels=snake_case    )
   UpperCamelCase_      :		Optional[Any]				=      self._prepare_for_class(snake_case     ,   snake_case     ,   return_labels=snake_case    )
   check_equivalence(snake_case     ,   snake_case     ,   snake_case    )

   UpperCamelCase_      :		Optional[Any]				=      self._prepare_for_class(snake_case     ,   snake_case    )
   UpperCamelCase_      :		Union[str, Any]				=      self._prepare_for_class(snake_case     ,   snake_case    )
   check_equivalence(snake_case     ,   snake_case     ,   snake_case     ,   {'output_hidden_states': True}    )

   UpperCamelCase_      :		str				=      self._prepare_for_class(snake_case     ,   snake_case     ,   return_labels=snake_case    )
   UpperCamelCase_      :		Optional[int]				=      self._prepare_for_class(snake_case     ,   snake_case     ,   return_labels=snake_case    )
   check_equivalence(snake_case     ,   snake_case     ,   snake_case     ,   {'output_hidden_states': True}    )

 def 		SCREAMING_SNAKE_CASE__     (				self			:							Union[str, Any]    )     ->		List[Any]:

  """simple docstring"""





  UpperCamelCase_      :		Optional[int]				=      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_image_classification(*snake_case    )


 @slow
 def 		SCREAMING_SNAKE_CASE__     (				self			:							Optional[int]    )     ->		Dict:

  """simple docstring"""





  UpperCamelCase_      :		str				=      TFConvNextModel.from_pretrained('facebook/convnext-tiny-224'    )
  self.assertIsNotNone(snake_case    )





def      __lowercase  (		):
 UpperCamelCase_      :		Tuple				=      Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
 return image






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

 @cached_property
 def 		SCREAMING_SNAKE_CASE__     (				self			:							Tuple    )     ->		Any:

  """simple docstring"""





  return ConvNextImageProcessor.from_pretrained('facebook/convnext-tiny-224'    ) if is_vision_available() else None


 @slow
 def 		SCREAMING_SNAKE_CASE__     (				self			:							List[str]    )     ->		Optional[int]:

  """simple docstring"""





  UpperCamelCase_      :		List[str]				=      TFConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'    )

  UpperCamelCase_      :		Any				=      self.default_image_processor
  UpperCamelCase_      :		Optional[Any]				=      prepare_img()
  UpperCamelCase_      :		Optional[int]				=      image_processor(images=snake_case     ,   return_tensors='tf'    )

  # forward pass
  UpperCamelCase_      :		List[str]				=      model(**snake_case    )

  # verify the logits
  UpperCamelCase_      :		List[Any]				=      tf.TensorShape((1, 1_0_0_0)    )
  self.assertEqual(outputs.logits.shape     ,   snake_case    )

  UpperCamelCase_      :		Union[str, Any]				=      tf.constant([-0.0260, -0.4739, 0.1911]    )

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




def      __lowercase  (		lowerCamelCase      :  int = 998001			):
 UpperCamelCase_      :		int				=      0
 for i in range(999						,						99						,						-1			):  # 3 digit numbers range from 999 down to 100
  for j in range(999						,						99						,						-1			):
   UpperCamelCase_      :		str				=      str(i * j			)
   if product_string == product_string[::-1] and i * j < n:
    UpperCamelCase_      :		str				=      max(lowerCamelCase						,						i * j			)
 return answer


if __name__ == "__main__":
   print(F"""{solution() = }""")



