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



    def    _lowercase		(self       :       List[str]					):
         UpperCAmelCase_			       =  0



    def    _lowercase		(self       :       Tuple					):
         UpperCAmelCase_			       =  AutoImageProcessor.from_pretrained("openai/clip-vit-base-patch32"					)
         self.assertIsInstance(__a  ,     __a					)



    def    _lowercase		(self       :       str					):
         with tempfile.TemporaryDirectory() as tmpdirname:
              UpperCAmelCase_			       =  Path(__a					) / "preprocessor_config.json"
              UpperCAmelCase_			       =  Path(__a					) / "config.json"
              json.dump(
                  {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}  ,     open(__a  ,     "w"					)  ,     )
              json.dump({"model_type": "clip"}  ,     open(__a  ,     "w"					)					)

              UpperCAmelCase_			       =  AutoImageProcessor.from_pretrained(__a					)
              self.assertIsInstance(__a  ,     __a					)



    def    _lowercase		(self       :       Dict					):
         # Ensure we can load the image processor from the feature extractor config
         with tempfile.TemporaryDirectory() as tmpdirname:
              UpperCAmelCase_			       =  Path(__a					) / "preprocessor_config.json"
              UpperCAmelCase_			       =  Path(__a					) / "config.json"
              json.dump(
                  {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"}  ,     open(__a  ,     "w"					)  ,     )
              json.dump({"model_type": "clip"}  ,     open(__a  ,     "w"					)					)

              UpperCAmelCase_			       =  AutoImageProcessor.from_pretrained(__a					)
              self.assertIsInstance(__a  ,     __a					)



    def    _lowercase		(self       :       List[str]					):
         with tempfile.TemporaryDirectory() as tmpdirname:
              UpperCAmelCase_			       =  CLIPConfig()

              # Create a dummy config file with image_proceesor_type
              UpperCAmelCase_			       =  Path(__a					) / "preprocessor_config.json"
              UpperCAmelCase_			       =  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
              UpperCAmelCase_			       =  AutoImageProcessor.from_pretrained(__a					).to_dict()

              config_dict.pop("image_processor_type"					)
              UpperCAmelCase_			       =  CLIPImageProcessor(**__a					)

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

              UpperCAmelCase_			       =  AutoImageProcessor.from_pretrained(__a					)

              # make sure private variable is not incorrectly saved
              UpperCAmelCase_			       =  json.loads(config.to_json_string()					)
              self.assertTrue("_processor_class" not in dict_as_saved					)

         self.assertIsInstance(__a  ,     __a					)



    def    _lowercase		(self       :       int					):
         with tempfile.TemporaryDirectory() as tmpdirname:
              UpperCAmelCase_			       =  Path(__a					) / "preprocessor_config.json"
              json.dump(
                  {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}  ,     open(__a  ,     "w"					)  ,     )

              UpperCAmelCase_			       =  AutoImageProcessor.from_pretrained(__a					)
              self.assertIsInstance(__a  ,     __a					)



    def    _lowercase		(self       :       Tuple					):
         with self.assertRaisesRegex(
             __a  ,     "clip-base is not a local folder and is not a valid model identifier"					):
              UpperCAmelCase_			       =  AutoImageProcessor.from_pretrained("clip-base"					)



    def    _lowercase		(self       :       Optional[int]					):
         with self.assertRaisesRegex(
             __a  ,     r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"					):
              UpperCAmelCase_			       =  AutoImageProcessor.from_pretrained(__a  ,     revision="aaaaaa"					)



    def    _lowercase		(self       :       Union[str, Any]					):
         with self.assertRaisesRegex(
             __a  ,     "hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."  ,     ):
              UpperCAmelCase_			       =  AutoImageProcessor.from_pretrained("hf-internal-testing/config-no-model"					)



    def    _lowercase		(self       :       List[Any]					):
         # If remote code is not set, we will time out when asking whether to load the model.
         with self.assertRaises(__a					):
              UpperCAmelCase_			       =  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					):
              UpperCAmelCase_			       =  AutoImageProcessor.from_pretrained(
                  "hf-internal-testing/test_dynamic_image_processor"  ,     trust_remote_code=__a					)

         UpperCAmelCase_			       =  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					)
              UpperCAmelCase_			       =  AutoImageProcessor.from_pretrained(__a  ,     trust_remote_code=__a					)
         self.assertEqual(reloaded_image_processor.__class__.__name__  ,     "NewImageProcessor"					)



    def    _lowercase		(self       :       Optional[int]					):
         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:
                   UpperCAmelCase_			       =  Path(__a					) / "preprocessor_config.json"
                   UpperCAmelCase_			       =  Path(__a					) / "config.json"
                   json.dump(
                       {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"}  ,     open(__a  ,     "w"					)  ,     )
                   json.dump({"model_type": "clip"}  ,     open(__a  ,     "w"					)					)

                   UpperCAmelCase_			       =  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					)
                   UpperCAmelCase_			       =  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    _lowercase		(self       :       Optional[int]					):




         class 			__A (					UpperCamelCase__      ):
             a__			:		str			  =    True

         try:
              AutoConfig.register("custom"  ,     __a					)
              AutoImageProcessor.register(__a  ,     __a					)
              # If remote code is not set, the default is to use local
              UpperCAmelCase_			       =  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.
              UpperCAmelCase_			       =  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
              UpperCAmelCase_			       =  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]











"""simple docstring"""




import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


a		:					List[Any]	      =     logging.get_logger(__name__)

a		:					List[Any]	      =     {
    '''microsoft/unispeech-sat-base-100h-libri-ft''': (
        '''https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json'''
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}


class 			__UpperCamelCase							(		a__		):
							lowerCamelCase  :   Tuple    ="""unispeech-sat"""






							def __init__(  self ,   lowerCAmelCase__=32 ,   lowerCAmelCase__=768 ,   lowerCAmelCase__=12 ,   lowerCAmelCase__=12 ,   lowerCAmelCase__=3072 ,   lowerCAmelCase__="gelu" ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=0.0 ,   lowerCAmelCase__=0.0 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=0.02 ,   lowerCAmelCase__=1E-5 ,   lowerCAmelCase__="group" ,   lowerCAmelCase__="gelu" ,   lowerCAmelCase__=(512, 512, 512, 512, 512, 512, 512) ,   lowerCAmelCase__=(5, 2, 2, 2, 2, 2, 2) ,   lowerCAmelCase__=(10, 3, 3, 3, 3, 2, 2) ,   lowerCAmelCase__=False ,   lowerCAmelCase__=128 ,   lowerCAmelCase__=16 ,   lowerCAmelCase__=False ,   lowerCAmelCase__=True ,   lowerCAmelCase__=0.05 ,   lowerCAmelCase__=10 ,   lowerCAmelCase__=2 ,   lowerCAmelCase__=0.0 ,   lowerCAmelCase__=10 ,   lowerCAmelCase__=0 ,   lowerCAmelCase__=320 ,   lowerCAmelCase__=2 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__=100 ,   lowerCAmelCase__=256 ,   lowerCAmelCase__=256 ,   lowerCAmelCase__=0.1 ,   lowerCAmelCase__="mean" ,   lowerCAmelCase__=False ,   lowerCAmelCase__=False ,   lowerCAmelCase__=256 ,   lowerCAmelCase__=(512, 512, 512, 512, 1500) ,   lowerCAmelCase__=(5, 3, 3, 1, 1) ,   lowerCAmelCase__=(1, 2, 3, 1, 1) ,   lowerCAmelCase__=512 ,   lowerCAmelCase__=0 ,   lowerCAmelCase__=1 ,   lowerCAmelCase__=2 ,   lowerCAmelCase__=504 ,   **lowerCAmelCase__ ,   )      ->					List[str]:
								super().__init__(**lowerCAmelCase__ ,   pad_token_id=lowerCAmelCase__ ,   bos_token_id=lowerCAmelCase__ ,   eos_token_id=lowerCAmelCase__ )
								a :						Any      =							hidden_size
								a :						Tuple      =							feat_extract_norm
								a :						Tuple      =							feat_extract_activation
								a :						Dict      =							list(lowerCAmelCase__ )
								a :						int      =							list(lowerCAmelCase__ )
								a :						Optional[Any]      =							list(lowerCAmelCase__ )
								a :						int      =							conv_bias
								a :						str      =							num_conv_pos_embeddings
								a :						Dict      =							num_conv_pos_embedding_groups
								a :						Optional[int]      =							len(self.conv_dim )
								a :						int      =							num_hidden_layers
								a :						Any      =							intermediate_size
								a :						Any      =							hidden_act
								a :						List[Any]      =							num_attention_heads
								a :						Any      =							hidden_dropout
								a :						Union[str, Any]      =							attention_dropout
								a :						Tuple      =							activation_dropout
								a :						Dict      =							feat_proj_dropout
								a :						Optional[Any]      =							final_dropout
								a :						Union[str, Any]      =							layerdrop
								a :						str      =							layer_norm_eps
								a :						Optional[int]      =							initializer_range
								a :						Optional[int]      =							vocab_size
								a :						str      =							num_clusters
								a :						Any      =							do_stable_layer_norm
								a :						str      =							use_weighted_layer_sum

								if (
								    (len(self.conv_stride ) != self.num_feat_extract_layers)
								    or (len(self.conv_kernel ) != self.num_feat_extract_layers)
								    or (len(self.conv_dim ) != self.num_feat_extract_layers)
								):
									raise ValueError(
									    "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="
									    " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="
									    f""" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,"""
									    f""" `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" )

								# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
								a :						List[Any]      =							apply_spec_augment
								a :						int      =							mask_time_prob
								a :						Optional[int]      =							mask_time_length
								a :						Dict      =							mask_time_min_masks
								a :						Optional[int]      =							mask_feature_prob
								a :						List[str]      =							mask_feature_length
								a :						Dict      =							mask_feature_min_masks

								# parameters for pretraining with codevector quantized representations
								a :						List[str]      =							num_codevectors_per_group
								a :						List[Any]      =							num_codevector_groups
								a :						Tuple      =							contrastive_logits_temperature
								a :						int      =							feat_quantizer_dropout
								a :						Optional[Any]      =							num_negatives
								a :						Optional[int]      =							codevector_dim
								a :						Tuple      =							proj_codevector_dim
								a :						Optional[int]      =							diversity_loss_weight

								# ctc loss
								a :						Dict      =							ctc_loss_reduction
								a :						Optional[Any]      =							ctc_zero_infinity

								# SequenceClassification-specific parameter. Feel free to ignore for other classes.
								a :						Dict      =							classifier_proj_size

								# XVector-specific parameters. Feel free to ignore for other classes.
								a :						Union[str, Any]      =							list(lowerCAmelCase__ )
								a :						List[str]      =							list(lowerCAmelCase__ )
								a :						Tuple      =							list(lowerCAmelCase__ )
								a :						Optional[int]      =							xvector_output_dim






							@property
							def        __a  (  self )      ->					Any:
								return functools.reduce(operator.mul ,   self.conv_stride ,   1 )


