


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

       def __init__( self   :  Union[str, Any]   ,				lowerCamelCase_   :  str   ,				lowerCamelCase_   :  int=1_3   ,				lowerCamelCase_   :  str=3_2   ,				lowerCamelCase_   :  int=3   ,				lowerCamelCase_   :  List[str]=4   ,				lowerCamelCase_   :  Tuple=[1_0, 2_0, 3_0, 4_0]   ,				lowerCamelCase_   :  Optional[int]=[2, 2, 3, 2]   ,				lowerCamelCase_   :  List[str]=True   ,				lowerCamelCase_   :  Tuple=True   ,				lowerCamelCase_   :  str=3_7   ,				lowerCamelCase_   :  str="gelu"   ,				lowerCamelCase_   :  Union[str, Any]=1_0   ,				lowerCamelCase_   :  int=0.0_2   ,				lowerCamelCase_   :  Dict=3   ,				lowerCamelCase_   :  Optional[int]=None   ,				):
            a_     :  Union[str, Any]				      =     parent
            a_     :  Union[str, Any]				      =     batch_size
            a_     :  List[Any]				      =     image_size
            a_     :  List[str]				      =     num_channels
            a_     :  Optional[Any]				      =     num_stages
            a_     :  Any				      =     hidden_sizes
            a_     :  str				      =     depths
            a_     :  Optional[Any]				      =     is_training
            a_     :  Optional[Any]				      =     use_labels
            a_     :  str				      =     intermediate_size
            a_     :  Tuple				      =     hidden_act
            a_     :  Union[str, Any]				      =     type_sequence_label_size
            a_     :  List[str]				      =     initializer_range
            a_     :  str				      =     scope

       def     UpperCAmelCase( self   :  Dict     ):
            a_     :  List[Any]				      =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

            a_     :  str				      =     None
            if self.use_labels:
                 a_     :  str				      =     ids_tensor([self.batch_size]   ,				self.type_sequence_label_size     )

            a_     :  Optional[Any]				      =     self.get_config()

            return config, pixel_values, labels

       def     UpperCAmelCase( self   :  List[str]     ):
            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=__SCREAMING_SNAKE_CASE   ,				initializer_range=self.initializer_range   ,				)

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

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


@require_tf
class 	a__						(    _a    ,							_a    ,							unittest.TestCase     ):
       lowerCamelCase__:  int				     =  (TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
       lowerCamelCase__:  List[Any]				     =  (
           {"feature-extraction": TFConvNextModel, "image-classification": TFConvNextForImageClassification}
           if is_tf_available()
           else {}
       )

       lowerCamelCase__:  List[Any]				     =  False
       lowerCamelCase__:  List[str]				     =  False
       lowerCamelCase__:  str				     =  False
       lowerCamelCase__:  Dict				     =  False
       lowerCamelCase__:  int				     =  False

       def     UpperCAmelCase( self   :  int     ):
            a_     :  Optional[Any]				      =     TFConvNextModelTester(self     )
            a_     :  Tuple				      =     ConfigTester(
                self   ,				config_class=__SCREAMING_SNAKE_CASE   ,				has_text_modality=__SCREAMING_SNAKE_CASE   ,				hidden_size=3_7   ,				)

       @unittest.skip(reason="""ConvNext does not use inputs_embeds"""     )
       def     UpperCAmelCase( self   :  Union[str, 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   :  str     ):
            super().test_keras_fit()

       @unittest.skip(reason="""ConvNext does not support input and output embeddings"""     )
       def     UpperCAmelCase( self   :  Union[str, Any]     ):
            pass

       def     UpperCAmelCase( self   :  Dict     ):
            a_     :  Optional[Any]				      =     self.model_tester.prepare_config_and_inputs_for_common()

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

                 a_     :  str				      =     ['''pixel_values''']
                 self.assertListEqual(arg_names[:1]   ,				__SCREAMING_SNAKE_CASE     )

       def     UpperCAmelCase( self   :  List[Any]     ):
            a_     :  Dict				      =     self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__SCREAMING_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     UpperCAmelCase( self   :  Optional[int]     ):
            super().test_dataset_conversion()

       def     UpperCAmelCase( self   :  Optional[Any]     ):




            def check_hidden_states_output(lowerCamelCase_   :  Optional[Any]   ,				lowerCamelCase_   :  Tuple   ,				lowerCamelCase_   :  Optional[Any]     ):
                 a_     :  Optional[Any]				      =     model_class(__SCREAMING_SNAKE_CASE     )

                 a_     :  List[Any]				      =     model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE     )     )
                 a_     :  List[str]				      =     outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

            a_     :  int				      =     self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                 a_     :  Any				      =     True
                 check_hidden_states_output(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE     )

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

                 check_hidden_states_output(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE     )

       def     UpperCAmelCase( self   :  List[Any]     ):
            a_     :  Any				      =     self.model_tester.prepare_config_and_inputs_for_common()

            def check_equivalence(lowerCamelCase_   :  List[str]   ,				lowerCamelCase_   :  Optional[Any]   ,				lowerCamelCase_   :  Union[str, Any]   ,				lowerCamelCase_   :  int={}     ):
                 a_     :  Tuple				      =     model(__SCREAMING_SNAKE_CASE   ,				return_dict=__SCREAMING_SNAKE_CASE   ,				**__SCREAMING_SNAKE_CASE     )
                 a_     :  Dict				      =     model(__SCREAMING_SNAKE_CASE   ,				return_dict=__SCREAMING_SNAKE_CASE   ,				**__SCREAMING_SNAKE_CASE     ).to_tuple()

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

                      recursive_check(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE     )

            for model_class in self.all_model_classes:
                 a_     :  str				      =     model_class(__SCREAMING_SNAKE_CASE     )

                 a_     :  Dict				      =     self._prepare_for_class(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE     )
                 a_     :  Dict				      =     self._prepare_for_class(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE     )
                 check_equivalence(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE     )

                 a_     :  str				      =     self._prepare_for_class(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				return_labels=__SCREAMING_SNAKE_CASE     )
                 a_     :  Dict				      =     self._prepare_for_class(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				return_labels=__SCREAMING_SNAKE_CASE     )
                 check_equivalence(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE     )

                 a_     :  Any				      =     self._prepare_for_class(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE     )
                 a_     :  List[Any]				      =     self._prepare_for_class(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE     )
                 check_equivalence(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				{"""output_hidden_states""": True}     )

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

       def     UpperCAmelCase( self   :  List[Any]     ):
            a_     :  Tuple				      =     self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE     )

       @slow
       def     UpperCAmelCase( self   :  Any     ):
            a_     :  Union[str, Any]				      =     TFConvNextModel.from_pretrained("""facebook/convnext-tiny-224"""     )
            self.assertIsNotNone(__SCREAMING_SNAKE_CASE     )



def 			_a      (    ):
     a_     :  Any				      =     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
     return image


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

       @cached_property
       def     UpperCAmelCase( self   :  List[str]     ):
            return ConvNextImageProcessor.from_pretrained("""facebook/convnext-tiny-224"""     ) if is_vision_available() else None

       @slow
       def     UpperCAmelCase( self   :  Union[str, Any]     ):
            a_     :  Tuple				      =     TFConvNextForImageClassification.from_pretrained("""facebook/convnext-tiny-224"""     )

            a_     :  Any				      =     self.default_image_processor
            a_     :  Any				      =     prepare_img()
            a_     :  List[str]				      =     image_processor(images=__SCREAMING_SNAKE_CASE   ,				return_tensors="""tf"""     )

            # forward pass
            a_     :  Any				      =     model(**__SCREAMING_SNAKE_CASE     )

            # verify the logits
            a_     :  Dict				      =     tf.TensorShape((1, 1_0_0_0)     )
            self.assertEqual(outputs.logits.shape   ,				__SCREAMING_SNAKE_CASE     )

            a_     :  Optional[Any]				      =     tf.constant([-0.0_2_6_0, -0.4_7_3_9, 0.1_9_1_1]     )

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







def   a						(				SCREAMING_SNAKE_CASE_   :		int							):






       """simple docstring"""





       UpperCamelCase     :						Tuple 						=		[True for i in range(n + 1							)]
       UpperCamelCase     :						Tuple 						=		[i - 1 for i in range(n + 1							)]
       UpperCamelCase     :						Optional[Any] 						=		[]
       for i in range(2				,      n + 1							):
              if is_prime[i]:
                     primes.append(SCREAMING_SNAKE_CASE_							)
              for j in range(0				,      len(SCREAMING_SNAKE_CASE_							)							):
                     if i * primes[j] >= n:
                            break
                     UpperCamelCase     :						Any 						=		False

                     if i % primes[j] == 0:
                            UpperCamelCase     :						int 						=		totients[i] * primes[j]
                            break

                     UpperCamelCase     :						Dict 						=		totients[i] * (primes[j] - 1)

       return totients


if __name__ == "__main__":
  import doctest

  doctest.testmod()

