



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

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


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

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


a_								=       get_tests_dir("""fixtures""")
a_								=       get_tests_dir("""fixtures/dummy_feature_extractor_config.json""")
a_								=       get_tests_dir("""fixtures/dummy-config.json""")



class 			__lowerCAmelCase  (						unittest.TestCase ):




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     0




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h'''					)
    self.assertIsInstance(__UpperCAmelCase						,					__UpperCAmelCase					)




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained(__UpperCAmelCase					)
    self.assertIsInstance(__UpperCAmelCase						,					__UpperCAmelCase					)




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    with tempfile.TemporaryDirectory() as tmpdirname:
       __lowerCamelCase    				=     WavaVecaConfig()

       # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
       __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained(__UpperCAmelCase					).to_dict()

       config_dict.pop('''feature_extractor_type'''					)
       __lowerCamelCase    				=     WavaVecaFeatureExtractor(**__UpperCAmelCase					)

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

       __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained(__UpperCAmelCase					)

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

    self.assertIsInstance(__UpperCAmelCase						,					__UpperCAmelCase					)




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained(__UpperCAmelCase					)
    self.assertIsInstance(__UpperCAmelCase						,					__UpperCAmelCase					)




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    with self.assertRaisesRegex(
        __UpperCAmelCase						,					'''bert-base is not a local folder and is not a valid model identifier'''					):
       __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained('''bert-base'''					)




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    with self.assertRaisesRegex(
        __UpperCAmelCase						,					r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''					):
       __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained(__UpperCAmelCase						,					revision='''aaaaaa'''					)




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    with self.assertRaisesRegex(
        __UpperCAmelCase						,					'''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''						,					):
       __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained('''hf-internal-testing/config-no-model'''					)




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    # If remote code is not set, we will time out when asking whether to load the model.
    with self.assertRaises(__UpperCAmelCase					):
       __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained(
           '''hf-internal-testing/test_dynamic_feature_extractor'''					)
    # If remote code is disabled, we can't load this config.
    with self.assertRaises(__UpperCAmelCase					):
       __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained(
           '''hf-internal-testing/test_dynamic_feature_extractor'''						,					trust_remote_code=__UpperCAmelCase					)

    __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained(
        '''hf-internal-testing/test_dynamic_feature_extractor'''						,					trust_remote_code=__UpperCAmelCase					)
    self.assertEqual(feature_extractor.__class__.__name__						,					'''NewFeatureExtractor'''					)

    # Test feature extractor can be reloaded.
    with tempfile.TemporaryDirectory() as tmp_dir:
       feature_extractor.save_pretrained(__UpperCAmelCase					)
       __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained(__UpperCAmelCase						,					trust_remote_code=__UpperCAmelCase					)
    self.assertEqual(reloaded_feature_extractor.__class__.__name__						,					'''NewFeatureExtractor'''					)




 def 							lowerCamelCase   (    self					):



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

       # Now that the config is registered, it can be used as any other config with the auto-API
       __lowerCamelCase    				=     CustomFeatureExtractor.from_pretrained(__UpperCAmelCase					)
       with tempfile.TemporaryDirectory() as tmp_dir:
          feature_extractor.save_pretrained(__UpperCAmelCase					)
          __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained(__UpperCAmelCase					)
          self.assertIsInstance(__UpperCAmelCase						,					__UpperCAmelCase					)

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




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''



    class 			__lowerCAmelCase  (						lowerCAmelCase__ ):
     lowerCAmelCase__							       =	True

    try:
       AutoConfig.register('''custom'''						,					__UpperCAmelCase					)
       AutoFeatureExtractor.register(__UpperCAmelCase						,					__UpperCAmelCase					)
       # If remote code is not set, the default is to use local
       __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained(
           '''hf-internal-testing/test_dynamic_feature_extractor'''					)
       self.assertEqual(feature_extractor.__class__.__name__						,					'''NewFeatureExtractor'''					)
       self.assertTrue(feature_extractor.is_local					)

       # If remote code is disabled, we load the local one.
       __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained(
           '''hf-internal-testing/test_dynamic_feature_extractor'''						,					trust_remote_code=__UpperCAmelCase					)
       self.assertEqual(feature_extractor.__class__.__name__						,					'''NewFeatureExtractor'''					)
       self.assertTrue(feature_extractor.is_local					)

       # If remote is enabled, we load from the Hub
       __lowerCamelCase    				=     AutoFeatureExtractor.from_pretrained(
           '''hf-internal-testing/test_dynamic_feature_extractor'''						,					trust_remote_code=__UpperCAmelCase					)
       self.assertEqual(feature_extractor.__class__.__name__						,					'''NewFeatureExtractor'''					)
       self.assertTrue(not hasattr(__UpperCAmelCase						,					'''is_local'''					)					)

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











from typing import Mapping

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


a_								=       logging.get_logger(__name__)

a_								=       {
    """t5-small""": """https://huggingface.co/t5-small/resolve/main/config.json""",
    """t5-base""": """https://huggingface.co/t5-base/resolve/main/config.json""",
    """t5-large""": """https://huggingface.co/t5-large/resolve/main/config.json""",
    """t5-3b""": """https://huggingface.co/t5-3b/resolve/main/config.json""",
    """t5-11b""": """https://huggingface.co/t5-11b/resolve/main/config.json""",
}



class 			__lowerCAmelCase  (						lowerCAmelCase__ ):
 lowerCAmelCase__							       =	"""t5"""
 lowerCAmelCase__							       =	["""past_key_values"""]
 lowerCAmelCase__							       =	{"""hidden_size""": """d_model""", """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers"""}




 def __init__(    self						,					__UpperCAmelCase=32128						,					__UpperCAmelCase=512						,					__UpperCAmelCase=64						,					__UpperCAmelCase=2048						,					__UpperCAmelCase=6						,					__UpperCAmelCase=None						,					__UpperCAmelCase=8						,					__UpperCAmelCase=32						,					__UpperCAmelCase=128						,					__UpperCAmelCase=0.1						,					__UpperCAmelCase=1E-6						,					__UpperCAmelCase=1.0						,					__UpperCAmelCase="relu"						,					__UpperCAmelCase=True						,					__UpperCAmelCase=True						,					__UpperCAmelCase=0						,					__UpperCAmelCase=1						,					**__UpperCAmelCase						,					):



    '''simple docstring'''
    __lowerCamelCase    				=     vocab_size
    __lowerCamelCase    				=     d_model
    __lowerCamelCase    				=     d_kv
    __lowerCamelCase    				=     d_ff
    __lowerCamelCase    				=     num_layers
    __lowerCamelCase    				=     (
        num_decoder_layers if num_decoder_layers is not None else self.num_layers
    )  # default = symmetry
    __lowerCamelCase    				=     num_heads
    __lowerCamelCase    				=     relative_attention_num_buckets
    __lowerCamelCase    				=     relative_attention_max_distance
    __lowerCamelCase    				=     dropout_rate
    __lowerCamelCase    				=     layer_norm_epsilon
    __lowerCamelCase    				=     initializer_factor
    __lowerCamelCase    				=     feed_forward_proj
    __lowerCamelCase    				=     use_cache

    __lowerCamelCase    				=     self.feed_forward_proj.split('''-'''					)
    __lowerCamelCase    				=     act_info[-1]
    __lowerCamelCase    				=     act_info[0] == '''gated'''

    if len(__UpperCAmelCase					) > 1 and act_info[0] != "gated" or len(__UpperCAmelCase					) > 2:
       raise ValueError(
           F"""`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer."""
           '''Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. '''
           '''\'gated-gelu\' or \'relu\''''					)

    # for backwards compatibility
    if feed_forward_proj == "gated-gelu":
       __lowerCamelCase    				=     '''gelu_new'''

    super().__init__(
        pad_token_id=__UpperCAmelCase						,					eos_token_id=__UpperCAmelCase						,					is_encoder_decoder=__UpperCAmelCase						,					**__UpperCAmelCase						,					)



class 			__lowerCAmelCase  (						lowerCAmelCase__ ):




 @property
 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     {
        '''input_ids''': {0: '''batch''', 1: '''encoder_sequence'''},
        '''attention_mask''': {0: '''batch''', 1: '''encoder_sequence'''},
    }
    if self.use_past:
       __lowerCamelCase    				=     '''past_encoder_sequence + sequence'''
       __lowerCamelCase    				=     {0: '''batch'''}
       __lowerCamelCase    				=     {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
    else:
       __lowerCamelCase    				=     {0: '''batch''', 1: '''decoder_sequence'''}
       __lowerCamelCase    				=     {0: '''batch''', 1: '''decoder_sequence'''}

    if self.use_past:
       self.fill_with_past_key_values_(__UpperCAmelCase						,					direction='''inputs'''					)

    return common_inputs




 @property
 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    return 13






