

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


_SCREAMING_SNAKE_CASE					:  Optional[int]             =      get_tests_dir('''fixtures''')
_SCREAMING_SNAKE_CASE					:  int             =      get_tests_dir('''fixtures/dummy_feature_extractor_config.json''')
_SCREAMING_SNAKE_CASE					:  Tuple             =      get_tests_dir('''fixtures/dummy-config.json''')






class 	UpperCAmelCase__		(	unittest.TestCase	):






  """simple docstring"""


  def 				lowercase_		(							self						:		Tuple )		->							Optional[int]:
       SCREAMING_SNAKE_CASE__   =   0






  def 				lowercase_		(							self						:		str )		->							Any:
       SCREAMING_SNAKE_CASE__   =   AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h''' )
       self.assertIsInstance(__lowerCamelCase     ,       __lowerCamelCase )






  def 				lowercase_		(							self						:		Any )		->							int:
       SCREAMING_SNAKE_CASE__   =   AutoFeatureExtractor.from_pretrained(__lowerCamelCase )
       self.assertIsInstance(__lowerCamelCase     ,       __lowerCamelCase )






  def 				lowercase_		(							self						:		Tuple )		->							int:
       with tempfile.TemporaryDirectory() as tmpdirname:
            SCREAMING_SNAKE_CASE__   =   WavaVecaConfig()

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

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

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

            SCREAMING_SNAKE_CASE__   =   AutoFeatureExtractor.from_pretrained(__lowerCamelCase )

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

       self.assertIsInstance(__lowerCamelCase     ,       __lowerCamelCase )






  def 				lowercase_		(							self						:		Optional[int] )		->							Union[str, Any]:
       SCREAMING_SNAKE_CASE__   =   AutoFeatureExtractor.from_pretrained(__lowerCamelCase )
       self.assertIsInstance(__lowerCamelCase     ,       __lowerCamelCase )






  def 				lowercase_		(							self						:		List[str] )		->							Tuple:
       with self.assertRaisesRegex(
           __lowerCamelCase     ,       '''bert-base is not a local folder and is not a valid model identifier''' ):
            SCREAMING_SNAKE_CASE__   =   AutoFeatureExtractor.from_pretrained('''bert-base''' )






  def 				lowercase_		(							self						:		List[str] )		->							int:
       with self.assertRaisesRegex(
           __lowerCamelCase     ,       r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ):
            SCREAMING_SNAKE_CASE__   =   AutoFeatureExtractor.from_pretrained(__lowerCamelCase     ,       revision='''aaaaaa''' )






  def 				lowercase_		(							self						:		Dict )		->							Dict:
       with self.assertRaisesRegex(
           __lowerCamelCase     ,       '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''     ,       ):
            SCREAMING_SNAKE_CASE__   =   AutoFeatureExtractor.from_pretrained('''hf-internal-testing/config-no-model''' )






  def 				lowercase_		(							self						:		List[Any] )		->							List[str]:
       # If remote code is not set, we will time out when asking whether to load the model.
       with self.assertRaises(__lowerCamelCase ):
            SCREAMING_SNAKE_CASE__   =   AutoFeatureExtractor.from_pretrained(
                '''hf-internal-testing/test_dynamic_feature_extractor''' )
       # If remote code is disabled, we can't load this config.
       with self.assertRaises(__lowerCamelCase ):
            SCREAMING_SNAKE_CASE__   =   AutoFeatureExtractor.from_pretrained(
                '''hf-internal-testing/test_dynamic_feature_extractor'''     ,       trust_remote_code=__lowerCamelCase )

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

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






  def 				lowercase_		(							self						:		List[Any] )		->							Tuple:
       try:
            AutoConfig.register('''custom'''     ,       __lowerCamelCase )
            AutoFeatureExtractor.register(__lowerCamelCase     ,       __lowerCamelCase )
            # Trying to register something existing in the Transformers library will raise an error
            with self.assertRaises(__lowerCamelCase ):
                 AutoFeatureExtractor.register(__lowerCamelCase     ,       __lowerCamelCase )

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

       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 				lowercase_		(							self						:		Optional[Any] )		->							int:






       class 	UpperCAmelCase__		(	A__	):






         """simple docstring"""


         a									= True

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

            # If remote is enabled, we load from the Hub
            SCREAMING_SNAKE_CASE__   =   AutoFeatureExtractor.from_pretrained(
                '''hf-internal-testing/test_dynamic_feature_extractor'''     ,       trust_remote_code=__lowerCamelCase )
            self.assertEqual(feature_extractor.__class__.__name__     ,       '''NewFeatureExtractor''' )
            self.assertTrue(not hasattr(__lowerCamelCase     ,       '''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 __future__ import annotations

import time
from collections.abc import Sequence
from random import randint

from matplotlib import pyplot as plt
def     UpperCAmelCase_       (						_A				,		_A				,		_A    ):





     '''simple docstring'''




     if not arr:
          return None, None, 0
     if low == high:
          return low, high, arr[low]

     SCREAMING_SNAKE_CASE__   =   (low + high) // 2
     SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   max_subarray(_A				,		_A				,		_A    )
     SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   max_subarray(_A				,		mid + 1				,		_A    )
     SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   max_cross_sum(_A				,		_A				,		_A				,		_A    )
     if left_sum >= right_sum and left_sum >= cross_sum:
          return left_low, left_high, left_sum
     elif right_sum >= left_sum and right_sum >= cross_sum:
          return right_low, right_high, right_sum
     return cross_left, cross_right, cross_sum
def     UpperCAmelCase_       (						_A				,		_A				,		_A				,		_A    ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   float('''-inf'''    ), -1
     SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   float('''-inf'''    ), -1

     SCREAMING_SNAKE_CASE__   =   0
     for i in range(_A				,		low - 1				,		-1    ):
          summ += arr[i]
          if summ > left_sum:
               SCREAMING_SNAKE_CASE__   =   summ
               SCREAMING_SNAKE_CASE__   =   i

     SCREAMING_SNAKE_CASE__   =   0
     for i in range(mid + 1				,		high + 1    ):
          summ += arr[i]
          if summ > right_sum:
               SCREAMING_SNAKE_CASE__   =   summ
               SCREAMING_SNAKE_CASE__   =   i

     return max_left, max_right, (left_sum + right_sum)
def     UpperCAmelCase_       (						_A    ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__   =   [randint(1				,		_A    ) for _ in range(_A    )]
     SCREAMING_SNAKE_CASE__   =   time.time()
     max_subarray(_A				,		0				,		input_size - 1    )
     SCREAMING_SNAKE_CASE__   =   time.time()
     return end - start
def     UpperCAmelCase_       (						):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__   =   [10, 1_00, 10_00, 1_00_00, 5_00_00, 10_00_00, 20_00_00, 30_00_00, 40_00_00, 50_00_00]
     SCREAMING_SNAKE_CASE__   =   [time_max_subarray(_A    ) for input_size in input_sizes]
     print('''No of Inputs\t\tTime Taken'''    )
     for input_size, runtime in zip(_A				,		_A    ):
          print(_A				,		'''\t\t'''				,		_A    )
     plt.plot(_A				,		_A    )
     plt.xlabel('''Number of Inputs'''    )
     plt.ylabel('''Time taken in seconds'''    )
     plt.show()


if __name__ == "__main__":
       from doctest import testmod

       testmod()


