




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






       def __init__( self:       Tuple							,       _lowerCAmelCase:       Dict							,       _lowerCAmelCase:       List[Any]=13							,       _lowerCAmelCase:       Optional[Any]=32							,       _lowerCAmelCase:       str=3							,       _lowerCAmelCase:       Optional[int]=4							,       _lowerCAmelCase:       List[str]=[10, 20, 30, 40]							,       _lowerCAmelCase:       Optional[Any]=[2, 2, 3, 2]							,       _lowerCAmelCase:       List[Any]=True							,       _lowerCAmelCase:       Optional[Any]=True							,       _lowerCAmelCase:       Union[str, Any]=37							,       _lowerCAmelCase:       int="gelu"							,       _lowerCAmelCase:       int=10							,       _lowerCAmelCase:       int=0.02							,       _lowerCAmelCase:       Optional[int]=3							,       _lowerCAmelCase:       Union[str, Any]=None							,       ):
         lowercase     :str   						=    parent
         lowercase     :Optional[int]   						=    batch_size
         lowercase     :str   						=    image_size
         lowercase     :Union[str, Any]   						=    num_channels
         lowercase     :Optional[Any]   						=    num_stages
         lowercase     :str   						=    hidden_sizes
         lowercase     :Any   						=    depths
         lowercase     :List[str]   						=    is_training
         lowercase     :List[Any]   						=    use_labels
         lowercase     :Union[str, Any]   						=    intermediate_size
         lowercase     :Tuple   						=    hidden_act
         lowercase     :int   						=    type_sequence_label_size
         lowercase     :List[Any]   						=    initializer_range
         lowercase     :List[Any]   						=    scope






       def        SCREAMING_SNAKE_CASE     ( self:       Optional[Any]					):
         lowercase     :Optional[Any]   						=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

         lowercase     :Dict   						=    None
         if self.use_labels:
           lowercase     :Any   						=    ids_tensor([self.batch_size]							,       self.type_sequence_label_size					)

         lowercase     :Union[str, Any]   						=    self.get_config()

         return config, pixel_values, labels






       def        SCREAMING_SNAKE_CASE     ( self:       Tuple					):
         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=_lowerCAmelCase							,       initializer_range=self.initializer_range							,       )






       def        SCREAMING_SNAKE_CASE     ( self:       List[Any]							,       _lowerCAmelCase:       Any							,       _lowerCAmelCase:       str							,       _lowerCAmelCase:       Optional[Any]					):
         lowercase     :Optional[int]   						=    TFConvNextModel(config=_lowerCAmelCase					)
         lowercase     :Optional[Any]   						=    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:       str							,       _lowerCAmelCase:       Optional[Any]							,       _lowerCAmelCase:       Any							,       _lowerCAmelCase:       Tuple					):
         lowercase     :int   						=    self.type_sequence_label_size
         lowercase     :int   						=    TFConvNextForImageClassification(_lowerCAmelCase					)
         lowercase     :Optional[Any]   						=    model(_lowerCAmelCase							,       labels=_lowerCAmelCase							,       training=_lowerCAmelCase					)
         self.parent.assertEqual(result.logits.shape							,       (self.batch_size, self.type_sequence_label_size)					)






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







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

       _a			       =    False
       _a			       =    False
       _a			       =    False
       _a			       =    False
       _a			       =    False






       def        SCREAMING_SNAKE_CASE     ( self:       Optional[int]					):
         lowercase     :List[Any]   						=    TFConvNextModelTester(self					)
         lowercase     :Union[str, Any]   						=    ConfigTester(
             self							,       config_class=_lowerCAmelCase							,       has_text_modality=_lowerCAmelCase							,       hidden_size=37							,       )






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






       @unittest.skip(reason="ConvNext does not support input and output embeddings"					)
       def        SCREAMING_SNAKE_CASE     ( self:       Any					):
         pass






       def        SCREAMING_SNAKE_CASE     ( self:       List[str]					):
         lowercase						,     lowercase     :Any   						=    self.model_tester.prepare_config_and_inputs_for_common()

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

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






       def        SCREAMING_SNAKE_CASE     ( self:       int					):
         lowercase     :List[str]   						=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowerCAmelCase					)






       @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:       Optional[int]					):
         super().test_dataset_conversion()






       def        SCREAMING_SNAKE_CASE     ( self:       Optional[Any]					):
         def check_hidden_states_output(_lowerCAmelCase:       int							,       _lowerCAmelCase:       Tuple							,       _lowerCAmelCase:       Optional[int]					):
           lowercase     :Any   						=    model_class(_lowerCAmelCase					)

           lowercase     :Optional[int]   						=    model(**self._prepare_for_class(_lowerCAmelCase							,       _lowerCAmelCase					)					)
           lowercase     :Any   						=    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

           lowercase     :List[Any]   						=    self.model_tester.num_stages
           self.assertEqual(len(_lowerCAmelCase					)							,       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]							,       )

         lowercase						,     lowercase     :Optional[Any]   						=    self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           lowercase     :Optional[Any]   						=    True
           check_hidden_states_output(_lowerCAmelCase							,       _lowerCAmelCase							,       _lowerCAmelCase					)

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

           check_hidden_states_output(_lowerCAmelCase							,       _lowerCAmelCase							,       _lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       Tuple					):
         lowercase						,     lowercase     :str   						=    self.model_tester.prepare_config_and_inputs_for_common()

         def check_equivalence(_lowerCAmelCase:       Union[str, Any]							,       _lowerCAmelCase:       Tuple							,       _lowerCAmelCase:       Any							,       _lowerCAmelCase:       Union[str, Any]={}					):
           lowercase     :Union[str, Any]   						=    model(_lowerCAmelCase							,       return_dict=_lowerCAmelCase							,       **_lowerCAmelCase					)
           lowercase     :List[str]   						=    model(_lowerCAmelCase							,       return_dict=_lowerCAmelCase							,       **_lowerCAmelCase					).to_tuple()

           def recursive_check(_lowerCAmelCase:       Dict							,       _lowerCAmelCase:       Tuple					):
             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     :Dict   						=    model_class(_lowerCAmelCase					)

           lowercase     :int   						=    self._prepare_for_class(_lowerCAmelCase							,       _lowerCAmelCase					)
           lowercase     :List[Any]   						=    self._prepare_for_class(_lowerCAmelCase							,       _lowerCAmelCase					)
           check_equivalence(_lowerCAmelCase							,       _lowerCAmelCase							,       _lowerCAmelCase					)

           lowercase     :Optional[int]   						=    self._prepare_for_class(_lowerCAmelCase							,       _lowerCAmelCase							,       return_labels=_lowerCAmelCase					)
           lowercase     :Optional[Any]   						=    self._prepare_for_class(_lowerCAmelCase							,       _lowerCAmelCase							,       return_labels=_lowerCAmelCase					)
           check_equivalence(_lowerCAmelCase							,       _lowerCAmelCase							,       _lowerCAmelCase					)

           lowercase     :List[Any]   						=    self._prepare_for_class(_lowerCAmelCase							,       _lowerCAmelCase					)
           lowercase     :Optional[Any]   						=    self._prepare_for_class(_lowerCAmelCase							,       _lowerCAmelCase					)
           check_equivalence(_lowerCAmelCase							,       _lowerCAmelCase							,       _lowerCAmelCase							,       {"output_hidden_states": True}					)

           lowercase     :Optional[int]   						=    self._prepare_for_class(_lowerCAmelCase							,       _lowerCAmelCase							,       return_labels=_lowerCAmelCase					)
           lowercase     :List[str]   						=    self._prepare_for_class(_lowerCAmelCase							,       _lowerCAmelCase							,       return_labels=_lowerCAmelCase					)
           check_equivalence(_lowerCAmelCase							,       _lowerCAmelCase							,       _lowerCAmelCase							,       {"output_hidden_states": True}					)






       def        SCREAMING_SNAKE_CASE     ( self:       int					):
         lowercase     :Optional[Any]   						=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase					)






       @slow
       def        SCREAMING_SNAKE_CASE     ( self:       Dict					):
         lowercase     :Dict   						=    TFConvNextModel.from_pretrained("facebook/convnext-tiny-224"					)
         self.assertIsNotNone(_lowerCAmelCase					)







def    UpperCAmelCase__	(  ):
  lowercase     :Optional[int]   						=    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
  return image







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






       @cached_property
       def        SCREAMING_SNAKE_CASE     ( self:       int					):
         return ConvNextImageProcessor.from_pretrained("facebook/convnext-tiny-224"					) if is_vision_available() else None






       @slow
       def        SCREAMING_SNAKE_CASE     ( self:       Optional[int]					):
         lowercase     :Optional[int]   						=    TFConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"					)

         lowercase     :List[Any]   						=    self.default_image_processor
         lowercase     :Optional[Any]   						=    prepare_img()
         lowercase     :List[str]   						=    image_processor(images=_lowerCAmelCase							,       return_tensors="tf"					)

         # forward pass
         lowercase     :Optional[int]   						=    model(**_lowerCAmelCase					)

         # verify the logits
         lowercase     :Union[str, Any]   						=    tf.TensorShape((1, 10_00)					)
         self.assertEqual(outputs.logits.shape							,       _lowerCAmelCase					)

         lowercase     :Dict   						=    tf.constant([-0.02_60, -0.47_39, 0.19_11]					)

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










def  __UpperCamelCase   (     a = 998001)			->int:

					# fetches the next number
					for number in range(n - 1,  9999,  -1):
										lowerCamelCase__      =							str(a)

										# checks whether 'str_number' is a palindrome.
										if str_number == str_number[::-1]:
															lowerCamelCase__      =							999

															# if 'number' is a product of two 3-digit numbers
															# then number is the answer otherwise fetch next number.
															while divisor != 99:
																				if (number % divisor == 0) and (len(str(number // divisor)) == 3.0):
																									return number
																				divisor -= 1
					raise ValueError("That number is larger than our acceptable range.")


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

