


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






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






      def 	__lowercase(     self					:   Tuple   )  -> Optional[int]:
           UpperCamelCase__							:  Optional[Any]								=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

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

           return config, pixel_values, labels






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






      def 	__lowercase(     self					:   Optional[int]   )  -> Optional[int]:
           UpperCamelCase__							:  List[Any]								=     self.prepare_config_and_inputs()
           UpperCamelCase__	,UpperCamelCase__	,UpperCamelCase__							:  List[str]								=     config_and_inputs
           UpperCamelCase__							:  Optional[int]								=     {'''pixel_values''': pixel_values}
           return config, inputs_dict

@require_tf
class  UpperCamelCase__ ( __lowerCamelCase							,						__lowerCamelCase							,						unittest.TestCase			):
      a__   :     Dict								=						(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
      a__   :     Dict								=						(
          {'feature-extraction': TFConvNextModel, 'image-classification': TFConvNextForImageClassification}
          if is_tf_available()
          else {}
      )

      a__   :     Union[str, Any]								=						False
      a__   :     Any								=						False
      a__   :     int								=						False
      a__   :     Union[str, Any]								=						False
      a__   :     str								=						False






      def 	__lowercase(     self					:   Dict   )  -> Optional[int]:
           UpperCamelCase__							:  List[str]								=     TFConvNextModelTester(self   )
           UpperCamelCase__							:  int								=     ConfigTester(
               self,		config_class=__lowerCamelCase,		has_text_modality=__lowerCamelCase,		hidden_size=37,		)






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






      @unittest.skip(reason='''ConvNext does not support input and output embeddings'''   )
      def 	__lowercase(     self					:   Dict   )  -> Any:
           pass






      def 	__lowercase(     self					:   Any   )  -> Union[str, Any]:
           UpperCamelCase__	,UpperCamelCase__							:  int								=     self.model_tester.prepare_config_and_inputs_for_common()

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

                UpperCamelCase__							:  int								=     ['''pixel_values''']
                self.assertListEqual(arg_names[:1],		__lowerCamelCase   )






      def 	__lowercase(     self					:   int   )  -> int:
           UpperCamelCase__							:  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 	__lowercase(     self					:   Tuple   )  -> Union[str, Any]:
           super().test_dataset_conversion()






      def 	__lowercase(     self					:   str   )  -> List[Any]:
           def check_hidden_states_output(__lowerCamelCase					:   Dict,		__lowerCamelCase					:   Any,		__lowerCamelCase					:   str   ):
                UpperCamelCase__							:  Tuple								=     model_class(__lowerCamelCase   )

                UpperCamelCase__							:  int								=     model(**self._prepare_for_class(__lowerCamelCase,		__lowerCamelCase   )   )
                UpperCamelCase__							:  Union[str, Any]								=     outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                UpperCamelCase__							:  Optional[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],		)

           UpperCamelCase__	,UpperCamelCase__							:  List[Any]								=     self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                UpperCamelCase__							:  int								=     True
                check_hidden_states_output(__lowerCamelCase,		__lowerCamelCase,		__lowerCamelCase   )

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

                check_hidden_states_output(__lowerCamelCase,		__lowerCamelCase,		__lowerCamelCase   )






      def 	__lowercase(     self					:   Optional[int]   )  -> str:
           UpperCamelCase__	,UpperCamelCase__							:  Dict								=     self.model_tester.prepare_config_and_inputs_for_common()

           def check_equivalence(__lowerCamelCase					:   Any,		__lowerCamelCase					:   str,		__lowerCamelCase					:   Dict,		__lowerCamelCase					:   Dict={}   ):
                UpperCamelCase__							:  List[str]								=     model(__lowerCamelCase,		return_dict=__lowerCamelCase,		**__lowerCamelCase   )
                UpperCamelCase__							:  Dict								=     model(__lowerCamelCase,		return_dict=__lowerCamelCase,		**__lowerCamelCase   ).to_tuple()

                def recursive_check(__lowerCamelCase					:   Optional[Any],		__lowerCamelCase					:   Optional[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:
                UpperCamelCase__							:  int								=     model_class(__lowerCamelCase   )

                UpperCamelCase__							:  Tuple								=     self._prepare_for_class(__lowerCamelCase,		__lowerCamelCase   )
                UpperCamelCase__							:  Tuple								=     self._prepare_for_class(__lowerCamelCase,		__lowerCamelCase   )
                check_equivalence(__lowerCamelCase,		__lowerCamelCase,		__lowerCamelCase   )

                UpperCamelCase__							:  List[str]								=     self._prepare_for_class(__lowerCamelCase,		__lowerCamelCase,		return_labels=__lowerCamelCase   )
                UpperCamelCase__							:  Optional[Any]								=     self._prepare_for_class(__lowerCamelCase,		__lowerCamelCase,		return_labels=__lowerCamelCase   )
                check_equivalence(__lowerCamelCase,		__lowerCamelCase,		__lowerCamelCase   )

                UpperCamelCase__							:  Optional[Any]								=     self._prepare_for_class(__lowerCamelCase,		__lowerCamelCase   )
                UpperCamelCase__							:  Dict								=     self._prepare_for_class(__lowerCamelCase,		__lowerCamelCase   )
                check_equivalence(__lowerCamelCase,		__lowerCamelCase,		__lowerCamelCase,		{'''output_hidden_states''': True}   )

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






      def 	__lowercase(     self					:   Tuple   )  -> Any:
           UpperCamelCase__							:  List[str]								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase   )






      @slow
      def 	__lowercase(     self					:   int   )  -> Union[str, Any]:
           UpperCamelCase__							:  List[str]								=     TFConvNextModel.from_pretrained('''facebook/convnext-tiny-224'''   )
           self.assertIsNotNone(__lowerCamelCase   )







def 						_lowercase       (     )  ->   Any:





     '''simple docstring'''





     UpperCamelCase__							:  List[Any]								=     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
     return image







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






      @cached_property
      def 	__lowercase(     self					:   List[Any]   )  -> Tuple:
           return ConvNextImageProcessor.from_pretrained('''facebook/convnext-tiny-224'''   ) if is_vision_available() else None






      @slow
      def 	__lowercase(     self					:   Union[str, Any]   )  -> Union[str, Any]:
           UpperCamelCase__							:  Dict								=     TFConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224'''   )

           UpperCamelCase__							:  int								=     self.default_image_processor
           UpperCamelCase__							:  Dict								=     prepare_img()
           UpperCamelCase__							:  Optional[Any]								=     image_processor(images=__lowerCamelCase,		return_tensors='''tf'''   )

           # forward pass
           UpperCamelCase__							:  Optional[int]								=     model(**__lowerCamelCase   )

           # verify the logits
           UpperCamelCase__							:  str								=     tf.TensorShape((1, 10_00)   )
           self.assertEqual(outputs.logits.shape,		__lowerCamelCase   )

           UpperCamelCase__							:  int								=     tf.constant([-0.0260, -0.4739, 0.1911]   )

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








import os

from apache_beam.io.filesystems import FileSystems
from apache_beam.pipeline import Pipeline

from .logging import get_logger


_SCREAMING_SNAKE_CASE :					Optional[int]			=	2 << 20  # 2mb
_SCREAMING_SNAKE_CASE :					Optional[Any]			=	get_logger(__name__)

class  UpperCamelCase__ ( __lowerCamelCase			):






      def 	__lowercase(     self					:   List[str]   )  -> List[Any]:
           UpperCamelCase__							:  int								=     self._options.get_all_options().get('''runner'''   )
           return runner in [None, "DirectRunner", "PortableRunner"]







def 						_lowercase       (     __lowerCamelCase			:					List[str]		,__lowerCamelCase			:					Any		,__lowerCamelCase			:					List[str]=False			)  ->   Optional[int]:





     '''simple docstring'''





     UpperCamelCase__							:  Any								=     FileSystems
     if fs.exists(__lowerCamelCase			):
          if force_upload:
               logger.info(F'Remote path already exist: {remote_file_path}. Overwriting it as force_upload=True.'			)
          else:
               logger.info(F'Remote path already exist: {remote_file_path}. Skipping it as force_upload=False.'			)
               return
     with fs.create(__lowerCamelCase			) as remote_file:
          with open(__lowerCamelCase		,'''rb'''			) as local_file:
               UpperCamelCase__							:  List[Any]								=     local_file.read(__lowerCamelCase			)
               while chunk:
                    remote_file.write(__lowerCamelCase			)
                    UpperCamelCase__							:  Union[str, Any]								=     local_file.read(__lowerCamelCase			)







def 						_lowercase       (     __lowerCamelCase			:					Any		,__lowerCamelCase			:					int		,__lowerCamelCase			:					str=False			)  ->   Tuple:





     '''simple docstring'''





     UpperCamelCase__							:  Optional[int]								=     FileSystems
     if os.path.exists(__lowerCamelCase			):
          if force_download:
               logger.info(F'Local path already exist: {remote_file_path}. Overwriting it as force_upload=True.'			)
          else:
               logger.info(F'Local path already exist: {remote_file_path}. Skipping it as force_upload=False.'			)
               return
     with fs.open(__lowerCamelCase			) as remote_file:
          with open(__lowerCamelCase		,'''wb'''			) as local_file:
               UpperCamelCase__							:  Any								=     remote_file.read(__lowerCamelCase			)
               while chunk:
                    local_file.write(__lowerCamelCase			)
                    UpperCamelCase__							:  Dict								=     remote_file.read(__lowerCamelCase			)




