


import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    IMAGE_PROCESSOR_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    CLIPConfig,
    CLIPImageProcessor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_image_processing import CustomImageProcessor  # noqa E402


class 	A_ (	unittest.TestCase   ):




      '''simple docstring'''


      def 					_snake_case     (    self:	Dict    ):
       __lowerCamelCase		:							List[Any]		 =						0




      def 					_snake_case     (    self:	str    ):
       __lowerCamelCase		:							Tuple		 =						AutoImageProcessor.from_pretrained('openai/clip-vit-base-patch32'    )
       self.assertIsInstance(a						,				a    )




      def 					_snake_case     (    self:	Optional[Any]    ):
       with tempfile.TemporaryDirectory() as tmpdirname:
        __lowerCamelCase		:							str		 =						Path(a    ) / 'preprocessor_config.json'
        __lowerCamelCase		:							Optional[int]		 =						Path(a    ) / 'config.json'
        json.dump(
            {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}						,				open(a						,				'w'    )						,				)
        json.dump({'model_type': 'clip'}						,				open(a						,				'w'    )    )

        __lowerCamelCase		:							str		 =						AutoImageProcessor.from_pretrained(a    )
        self.assertIsInstance(a						,				a    )




      def 					_snake_case     (    self:	Tuple    ):
       # Ensure we can load the image processor from the feature extractor config
       with tempfile.TemporaryDirectory() as tmpdirname:
        __lowerCamelCase		:							Tuple		 =						Path(a    ) / 'preprocessor_config.json'
        __lowerCamelCase		:							Any		 =						Path(a    ) / 'config.json'
        json.dump(
            {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'}						,				open(a						,				'w'    )						,				)
        json.dump({'model_type': 'clip'}						,				open(a						,				'w'    )    )

        __lowerCamelCase		:							Any		 =						AutoImageProcessor.from_pretrained(a    )
        self.assertIsInstance(a						,				a    )




      def 					_snake_case     (    self:	Union[str, Any]    ):
       with tempfile.TemporaryDirectory() as tmpdirname:
        __lowerCamelCase		:							Dict		 =						CLIPConfig()

        # Create a dummy config file with image_proceesor_type
        __lowerCamelCase		:							Optional[Any]		 =						Path(a    ) / 'preprocessor_config.json'
        __lowerCamelCase		:							Union[str, Any]		 =						Path(a    ) / 'config.json'
        json.dump(
            {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}						,				open(a						,				'w'    )						,				)
        json.dump({'model_type': 'clip'}						,				open(a						,				'w'    )    )

        # remove image_processor_type to make sure config.json alone is enough to load image processor locally
        __lowerCamelCase		:							Tuple		 =						AutoImageProcessor.from_pretrained(a    ).to_dict()

        config_dict.pop('image_processor_type'    )
        __lowerCamelCase		:							int		 =						CLIPImageProcessor(**a    )

        # save in new folder
        model_config.save_pretrained(a    )
        config.save_pretrained(a    )

        __lowerCamelCase		:							List[str]		 =						AutoImageProcessor.from_pretrained(a    )

        # make sure private variable is not incorrectly saved
        __lowerCamelCase		:							Tuple		 =						json.loads(config.to_json_string()    )
        self.assertTrue('_processor_class' not in dict_as_saved    )

       self.assertIsInstance(a						,				a    )




      def 					_snake_case     (    self:	List[Any]    ):
       with tempfile.TemporaryDirectory() as tmpdirname:
        __lowerCamelCase		:							List[Any]		 =						Path(a    ) / 'preprocessor_config.json'
        json.dump(
            {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}						,				open(a						,				'w'    )						,				)

        __lowerCamelCase		:							str		 =						AutoImageProcessor.from_pretrained(a    )
        self.assertIsInstance(a						,				a    )




      def 					_snake_case     (    self:	str    ):
       with self.assertRaisesRegex(
           a						,				'clip-base is not a local folder and is not a valid model identifier'    ):
        __lowerCamelCase		:							Dict		 =						AutoImageProcessor.from_pretrained('clip-base'    )




      def 					_snake_case     (    self:	int    ):
       with self.assertRaisesRegex(
           a						,				R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'    ):
        __lowerCamelCase		:							Optional[Any]		 =						AutoImageProcessor.from_pretrained(a						,				revision='aaaaaa'    )




      def 					_snake_case     (    self:	Any    ):
       with self.assertRaisesRegex(
           a						,				'hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'						,				):
        __lowerCamelCase		:							Tuple		 =						AutoImageProcessor.from_pretrained('hf-internal-testing/config-no-model'    )




      def 					_snake_case     (    self:	str    ):
       # If remote code is not set, we will time out when asking whether to load the model.
       with self.assertRaises(a    ):
        __lowerCamelCase		:							Union[str, Any]		 =						AutoImageProcessor.from_pretrained('hf-internal-testing/test_dynamic_image_processor'    )
       # If remote code is disabled, we can't load this config.
       with self.assertRaises(a    ):
        __lowerCamelCase		:							Dict		 =						AutoImageProcessor.from_pretrained(
            'hf-internal-testing/test_dynamic_image_processor'						,				trust_remote_code=a    )

       __lowerCamelCase		:							Tuple		 =						AutoImageProcessor.from_pretrained(
           'hf-internal-testing/test_dynamic_image_processor'						,				trust_remote_code=a    )
       self.assertEqual(image_processor.__class__.__name__						,				'NewImageProcessor'    )

       # Test image processor can be reloaded.
       with tempfile.TemporaryDirectory() as tmp_dir:
        image_processor.save_pretrained(a    )
        __lowerCamelCase		:							Any		 =						AutoImageProcessor.from_pretrained(a						,				trust_remote_code=a    )
       self.assertEqual(reloaded_image_processor.__class__.__name__						,				'NewImageProcessor'    )




      def 					_snake_case     (    self:	Any    ):
       try:
        AutoConfig.register('custom'						,				a    )
        AutoImageProcessor.register(a						,				a    )
        # Trying to register something existing in the Transformers library will raise an error
        with self.assertRaises(a    ):
         AutoImageProcessor.register(a						,				a    )

        with tempfile.TemporaryDirectory() as tmpdirname:
         __lowerCamelCase		:							Tuple		 =						Path(a    ) / 'preprocessor_config.json'
         __lowerCamelCase		:							Any		 =						Path(a    ) / 'config.json'
         json.dump(
             {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'}						,				open(a						,				'w'    )						,				)
         json.dump({'model_type': 'clip'}						,				open(a						,				'w'    )    )

         __lowerCamelCase		:							int		 =						CustomImageProcessor.from_pretrained(a    )

        # Now that the config is registered, it can be used as any other config with the auto-API
        with tempfile.TemporaryDirectory() as tmp_dir:
         image_processor.save_pretrained(a    )
         __lowerCamelCase		:							List[str]		 =						AutoImageProcessor.from_pretrained(a    )
         self.assertIsInstance(a						,				a    )

       finally:
        if "custom" in CONFIG_MAPPING._extra_content:
         del CONFIG_MAPPING._extra_content["custom"]
        if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
         del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]




      def 					_snake_case     (    self:	List[Any]    ):


       class 	A_ (	__UpperCamelCase   ):




             '''simple docstring'''


             __snake_case 	=       True

       try:
        AutoConfig.register('custom'						,				a    )
        AutoImageProcessor.register(a						,				a    )
        # If remote code is not set, the default is to use local
        __lowerCamelCase		:							List[str]		 =						AutoImageProcessor.from_pretrained('hf-internal-testing/test_dynamic_image_processor'    )
        self.assertEqual(image_processor.__class__.__name__						,				'NewImageProcessor'    )
        self.assertTrue(image_processor.is_local    )

        # If remote code is disabled, we load the local one.
        __lowerCamelCase		:							str		 =						AutoImageProcessor.from_pretrained(
            'hf-internal-testing/test_dynamic_image_processor'						,				trust_remote_code=a    )
        self.assertEqual(image_processor.__class__.__name__						,				'NewImageProcessor'    )
        self.assertTrue(image_processor.is_local    )

        # If remote is enabled, we load from the Hub
        __lowerCamelCase		:							List[Any]		 =						AutoImageProcessor.from_pretrained(
            'hf-internal-testing/test_dynamic_image_processor'						,				trust_remote_code=a    )
        self.assertEqual(image_processor.__class__.__name__						,				'NewImageProcessor'    )
        self.assertTrue(not hasattr(a						,				'is_local'    )    )

       finally:
        if "custom" in CONFIG_MAPPING._extra_content:
         del CONFIG_MAPPING._extra_content["custom"]
        if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
         del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]





import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
       import tensorflow as tf
       from tensorflow.python.eager import context
       from tensorflow.python.framework import ops

       from transformers import GradientAccumulator, create_optimizer


@require_tf
class 	A_ (	unittest.TestCase   ):




      '''simple docstring'''


      def 					_snake_case     (    self:	Optional[int]						,				a:	int						,				a:	List[Any]						,				a:	Any    ):
       self.assertEqual(len(a    )						,				len(a    )    )
       for a, b in zip(a						,				a    ):
        self.assertAlmostEqual(a						,				a						,				delta=a    )




      def 					_snake_case     (    self:	Union[str, Any]    ):
       __lowerCamelCase		:							Union[str, Any]		 =						GradientAccumulator()
       accumulator([tf.constant([1.0, 2.0]    )]    )
       accumulator([tf.constant([-2.0, 1.0]    )]    )
       accumulator([tf.constant([-1.0, 2.0]    )]    )
       with self.assertRaises(a    ):
        accumulator([tf.constant([1.0, 1.0]    ), tf.constant([2.0, 2.0]    )]    )
       self.assertEqual(accumulator.step						,				3    )
       self.assertEqual(len(accumulator.gradients    )						,				1    )
       self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist()						,				[-2.0, 5.0]						,				tol=1e-2    )
       accumulator.reset()
       self.assertEqual(accumulator.step						,				0    )
       self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist()						,				[0.0, 0.0]						,				tol=1e-2    )




      def 					_snake_case     (    self:	str    ):
       __lowerCamelCase		:							Any		 =						None
       ops.enable_eager_execution_internal()
       __lowerCamelCase		:							List[str]		 =						tf.config.list_physical_devices('CPU'    )
       if len(a    ) == 1:
        tf.config.set_logical_device_configuration(
            physical_devices[0]						,				[tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration()]    )
       __lowerCamelCase		:							Dict		 =						tf.config.list_logical_devices(device_type='CPU'    )
       __lowerCamelCase		:							List[Any]		 =						tf.distribute.MirroredStrategy(devices=devices[:2]    )

       with strategy.scope():
        __lowerCamelCase		:							Optional[Any]		 =						GradientAccumulator()
        __lowerCamelCase		:							int		 =						tf.Variable([4.0, 3.0]    )
        __lowerCamelCase ,					__lowerCamelCase		:							Any		 =						create_optimizer(5e-5						,				10						,				5    )
        __lowerCamelCase		:							Any		 =						tf.Variable([0.0, 0.0]						,				trainable=a    )

       def accumulate_on_replica(a:	str    ):
        accumulator([gradient]    )

       def apply_on_replica():
        optimizer.apply_gradients(list(zip(accumulator.gradients						,				[variable]    )    )    )

       @tf.function
       def accumulate(a:	List[Any]						,				a:	List[str]    ):
        with strategy.scope():
         __lowerCamelCase		:							Optional[int]		 =						strategy.experimental_local_results(a    )
         local_variables[0].assign(a    )
         local_variables[1].assign(a    )
         strategy.run(a						,				args=(gradient_placeholder,)    )

       @tf.function
       def apply_grad():
        with strategy.scope():
         strategy.run(a    )

       def _check_local_values(a:	str						,				a:	Union[str, Any]    ):
        __lowerCamelCase		:							Optional[Any]		 =						strategy.experimental_local_results(accumulator._gradients[0]    )
        self.assertListAlmostEqual(values[0].value()						,				a						,				tol=1e-2    )
        self.assertListAlmostEqual(values[1].value()						,				a						,				tol=1e-2    )

       accumulate([1.0, 2.0]						,				[-1.0, 1.0]    )
       accumulate([3.0, -1.0]						,				[-1.0, -1.0]    )
       accumulate([-2.0, 2.0]						,				[3.0, -2.0]    )
       self.assertEqual(accumulator.step						,				3    )
       _check_local_values([2.0, 3.0]						,				[1.0, -2.0]    )
       apply_grad()
       self.assertListAlmostEqual(variable.value()						,				[4.0, 3.0]						,				tol=1e-2    )
       accumulator.reset()
       self.assertEqual(accumulator.step						,				0    )
       _check_local_values([0.0, 0.0]						,				[0.0, 0.0]    )

