





"""simple docstring"""




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  _UpperCAmelCase  (   unittest.TestCase		):



      '''simple docstring'''

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Any										=  0

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Tuple										=  AutoImageProcessor.from_pretrained('openai/clip-vit-base-patch32'  )
        self.assertIsInstance(snake_case_					,      snake_case_  )

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        with tempfile.TemporaryDirectory() as tmpdirname:
          A_  :       Dict										=  Path(snake_case_  ) / 'preprocessor_config.json'
          A_  :       List[Any]										=  Path(snake_case_  ) / 'config.json'
          json.dump(
              {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}					,      open(snake_case_					,      'w'  )					,      )
          json.dump({'model_type': 'clip'}					,      open(snake_case_					,      'w'  )  )

          A_  :       List[Any]										=  AutoImageProcessor.from_pretrained(snake_case_  )
          self.assertIsInstance(snake_case_					,      snake_case_  )

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        with tempfile.TemporaryDirectory() as tmpdirname:
          A_  :       Optional[int]										=  Path(snake_case_  ) / 'preprocessor_config.json'
          A_  :       str										=  Path(snake_case_  ) / 'config.json'
          json.dump(
              {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'}					,      open(snake_case_					,      'w'  )					,      )
          json.dump({'model_type': 'clip'}					,      open(snake_case_					,      'w'  )  )

          A_  :       Optional[int]										=  AutoImageProcessor.from_pretrained(snake_case_  )
          self.assertIsInstance(snake_case_					,      snake_case_  )

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        with tempfile.TemporaryDirectory() as tmpdirname:
          A_  :       str										=  CLIPConfig()

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

          # remove image_processor_type to make sure config.json alone is enough to load image processor locally
          A_  :       Optional[Any]										=  AutoImageProcessor.from_pretrained(snake_case_  ).to_dict()

          config_dict.pop('image_processor_type'  )
          A_  :       Optional[Any]										=  CLIPImageProcessor(**snake_case_  )

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

          A_  :       Optional[Any]										=  AutoImageProcessor.from_pretrained(snake_case_  )

          # make sure private variable is not incorrectly saved
          A_  :       List[Any]										=  json.loads(config.to_json_string()  )
          self.assertTrue('_processor_class' not in dict_as_saved  )

        self.assertIsInstance(snake_case_					,      snake_case_  )

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        with tempfile.TemporaryDirectory() as tmpdirname:
          A_  :       Tuple										=  Path(snake_case_  ) / 'preprocessor_config.json'
          json.dump(
              {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}					,      open(snake_case_					,      'w'  )					,      )

          A_  :       Any										=  AutoImageProcessor.from_pretrained(snake_case_  )
          self.assertIsInstance(snake_case_					,      snake_case_  )

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        with self.assertRaisesRegex(
            snake_case_					,      'clip-base is not a local folder and is not a valid model identifier'  ):
          A_  :       Union[str, Any]										=  AutoImageProcessor.from_pretrained('clip-base'  )

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

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

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

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

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        with self.assertRaises(snake_case_  ):
          A_  :       List[str]										=  AutoImageProcessor.from_pretrained('hf-internal-testing/test_dynamic_image_processor'  )
        # If remote code is disabled, we can't load this config.
        with self.assertRaises(snake_case_  ):
          A_  :       List[str]										=  AutoImageProcessor.from_pretrained(
              'hf-internal-testing/test_dynamic_image_processor'					,      trust_remote_code=snake_case_  )

        A_  :       List[str]										=  AutoImageProcessor.from_pretrained(
            'hf-internal-testing/test_dynamic_image_processor'					,      trust_remote_code=snake_case_  )
        self.assertEqual(image_processor.__class__.__name__					,      'NewImageProcessor'  )

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

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

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

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

            A_  :       List[str]										=  CustomImageProcessor.from_pretrained(snake_case_  )

          # 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(snake_case_  )
            A_  :       Any										=  AutoImageProcessor.from_pretrained(snake_case_  )
            self.assertIsInstance(snake_case_					,      snake_case_  )

        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 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        class  _UpperCAmelCase  (   UpperCAmelCase__		):



              '''simple docstring'''
              lowercase_      : List[str]	    =   True

        try:
          AutoConfig.register('custom'					,      snake_case_  )
          AutoImageProcessor.register(snake_case_					,      snake_case_  )
          # If remote code is not set, the default is to use local
          A_  :       List[Any]										=  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.
          A_  :       Optional[Any]										=  AutoImageProcessor.from_pretrained(
              'hf-internal-testing/test_dynamic_image_processor'					,      trust_remote_code=snake_case_  )
          self.assertEqual(image_processor.__class__.__name__					,      'NewImageProcessor'  )
          self.assertTrue(image_processor.is_local  )

          # If remote is enabled, we load from the Hub
          A_  :       Optional[Any]										=  AutoImageProcessor.from_pretrained(
              'hf-internal-testing/test_dynamic_image_processor'					,      trust_remote_code=snake_case_  )
          self.assertEqual(image_processor.__class__.__name__					,      'NewImageProcessor'  )
          self.assertTrue(not hasattr(snake_case_					,      '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]






"""simple docstring"""




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCamelCase_     :						str						=					logging.get_logger(__name__)

lowerCamelCase_     :						Any						=					{
    'facebook/xmod-base': 'https://huggingface.co/facebook/xmod-base/resolve/main/config.json',
    'facebook/xmod-large-prenorm': 'https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json',
    'facebook/xmod-base-13-125k': 'https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json',
    'facebook/xmod-base-30-125k': 'https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json',
    'facebook/xmod-base-30-195k': 'https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json',
    'facebook/xmod-base-60-125k': 'https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json',
    'facebook/xmod-base-60-265k': 'https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json',
    'facebook/xmod-base-75-125k': 'https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json',
    'facebook/xmod-base-75-269k': 'https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json',
}

class  _UpperCAmelCase  (   UpperCAmelCase__		):



      '''simple docstring'''
      lowercase_      : Tuple	    =   """xmod"""

      def __init__(     self					,      snake_case_=3_0_5_2_2					,      snake_case_=7_6_8					,      snake_case_=1_2					,      snake_case_=1_2					,      snake_case_=3_0_7_2					,      snake_case_="gelu"					,      snake_case_=0.1					,      snake_case_=0.1					,      snake_case_=5_1_2					,      snake_case_=2					,      snake_case_=0.02					,      snake_case_=1E-12					,      snake_case_=1					,      snake_case_=0					,      snake_case_=2					,      snake_case_="absolute"					,      snake_case_=True					,      snake_case_=None					,      snake_case_=False					,      snake_case_=2					,      snake_case_=False					,      snake_case_=True					,      snake_case_=True					,      snake_case_=("en_XX",)					,      snake_case_=None					,      **snake_case_					,      ):

        """simple docstring"""

        super().__init__(pad_token_id=snake_case_					,      bos_token_id=snake_case_					,      eos_token_id=snake_case_					,      **snake_case_  )

        A_  :       Union[str, Any]										=  vocab_size
        A_  :       Any										=  hidden_size
        A_  :       List[str]										=  num_hidden_layers
        A_  :       Tuple										=  num_attention_heads
        A_  :       int										=  hidden_act
        A_  :       Any										=  intermediate_size
        A_  :       Any										=  hidden_dropout_prob
        A_  :       Dict										=  attention_probs_dropout_prob
        A_  :       Union[str, Any]										=  max_position_embeddings
        A_  :       List[Any]										=  type_vocab_size
        A_  :       List[str]										=  initializer_range
        A_  :       Any										=  layer_norm_eps
        A_  :       Optional[Any]										=  position_embedding_type
        A_  :       int										=  use_cache
        A_  :       Dict										=  classifier_dropout
        A_  :       int										=  pre_norm
        A_  :       Optional[Any]										=  adapter_reduction_factor
        A_  :       List[Any]										=  adapter_layer_norm
        A_  :       int										=  adapter_reuse_layer_norm
        A_  :       Dict										=  ln_before_adapter
        A_  :       List[str]										=  list(snake_case_  )
        A_  :       Union[str, Any]										=  default_language


class  _UpperCAmelCase  (   UpperCAmelCase__		):



      '''simple docstring'''

      @property
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        if self.task == "multiple-choice":
          A_  :       Dict										=  {0: 'batch', 1: 'choice', 2: 'sequence'}
        else:
          A_  :       int										=  {0: 'batch', 1: 'sequence'}
        return OrderedDict(
            [
                ('input_ids', dynamic_axis),
                ('attention_mask', dynamic_axis),
            ]  )