



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


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



class   UpperCAmelCase_    (       unittest.TestCase):







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							str    =    0







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[int]    =    AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base-960h")
														self.assertIsInstance(__a,			__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[Any]    =    AutoFeatureExtractor.from_pretrained(__a)
														self.assertIsInstance(__a,			__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														with tempfile.TemporaryDirectory() as tmpdirname:
																					_lowerCAmelCase     :							Dict    =    WavaVecaConfig()

																					# remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
																					_lowerCAmelCase     :							int    =    AutoFeatureExtractor.from_pretrained(__a).to_dict()

																					config_dict.pop("feature_extractor_type")
																					_lowerCAmelCase     :							List[Any]    =    WavaVecaFeatureExtractor(**__a)

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

																					_lowerCAmelCase     :							List[Any]    =    AutoFeatureExtractor.from_pretrained(__a)

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

														self.assertIsInstance(__a,			__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							int    =    AutoFeatureExtractor.from_pretrained(__a)
														self.assertIsInstance(__a,			__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														with self.assertRaisesRegex(
														    __a,			"bert-base is not a local folder and is not a valid model identifier"):
																					_lowerCAmelCase     :							Dict    =    AutoFeatureExtractor.from_pretrained("bert-base")







							def    snake_case__      (   self):

														'''simple docstring'''

														with self.assertRaisesRegex(
														    __a,			R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"):
																					_lowerCAmelCase     :							List[Any]    =    AutoFeatureExtractor.from_pretrained(__a,			revision="aaaaaa")







							def    snake_case__      (   self):

														'''simple docstring'''

														with self.assertRaisesRegex(
														    __a,			"hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.",			):
																					_lowerCAmelCase     :							Optional[int]    =    AutoFeatureExtractor.from_pretrained("hf-internal-testing/config-no-model")







							def    snake_case__      (   self):

														'''simple docstring'''

														with self.assertRaises(__a):
																					_lowerCAmelCase     :							List[str]    =    AutoFeatureExtractor.from_pretrained(
																					    "hf-internal-testing/test_dynamic_feature_extractor")
														# If remote code is disabled, we can't load this config.
														with self.assertRaises(__a):
																					_lowerCAmelCase     :							List[Any]    =    AutoFeatureExtractor.from_pretrained(
																					    "hf-internal-testing/test_dynamic_feature_extractor",			trust_remote_code=__a)

														_lowerCAmelCase     :							List[str]    =    AutoFeatureExtractor.from_pretrained(
														    "hf-internal-testing/test_dynamic_feature_extractor",			trust_remote_code=__a)
														self.assertEqual(feature_extractor.__class__.__name__,			"NewFeatureExtractor")

														# Test feature extractor can be reloaded.
														with tempfile.TemporaryDirectory() as tmp_dir:
																					feature_extractor.save_pretrained(__a)
																					_lowerCAmelCase     :							Optional[int]    =    AutoFeatureExtractor.from_pretrained(__a,			trust_remote_code=__a)
														self.assertEqual(reloaded_feature_extractor.__class__.__name__,			"NewFeatureExtractor")







							def    snake_case__      (   self):

														'''simple docstring'''

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

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

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

														'''simple docstring'''

														class   UpperCAmelCase_    (       a):
																					lowerCamelCase__   			=		True

														try:
																					AutoConfig.register("custom",			__a)
																					AutoFeatureExtractor.register(__a,			__a)
																					# If remote code is not set, the default is to use local
																					_lowerCAmelCase     :							Union[str, Any]    =    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     :							int    =    AutoFeatureExtractor.from_pretrained(
																					    "hf-internal-testing/test_dynamic_feature_extractor",			trust_remote_code=__a)
																					self.assertEqual(feature_extractor.__class__.__name__,			"NewFeatureExtractor")
																					self.assertTrue(feature_extractor.is_local)

																					# If remote is enabled, we load from the Hub
																					_lowerCAmelCase     :							str    =    AutoFeatureExtractor.from_pretrained(
																					    "hf-internal-testing/test_dynamic_feature_extractor",			trust_remote_code=__a)
																					self.assertEqual(feature_extractor.__class__.__name__,			"NewFeatureExtractor")
																					self.assertTrue(not hasattr(__a,			"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]






import os

import pytest
from attr import dataclass


_UpperCAmelCase		:  List[str]	=   "us-east-1"  # defaults region




@dataclass
class       __lowerCAmelCase				:
       _a           =  42
       _a           =  '''arn:aws:iam::558105141721:role/sagemaker_execution_role'''
       _a           =  {
           '''task_name''': '''mnli''',
           '''per_device_train_batch_size''': 16,
           '''per_device_eval_batch_size''': 16,
           '''do_train''': True,
           '''do_eval''': True,
           '''do_predict''': True,
           '''output_dir''': '''/opt/ml/model''',
           '''overwrite_output_dir''': True,
           '''max_steps''': 500,
           '''save_steps''': 5500,
       }
       _a           =  {**hyperparameters, '''max_steps''': 1000}






       @property
       def     SCREAMING_SNAKE_CASE			(      self:    str ):
         if self.framework == "pytorch":
           return [
               {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"},
               {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"},
               {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"},
           ]
         else:
           return [
               {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"},
               {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"},
               {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"},
           ]






       @property
       def     SCREAMING_SNAKE_CASE			(      self:    Dict ):
         return F"{self.framework}-transfromers-test"






       @property
       def     SCREAMING_SNAKE_CASE			(      self:    Any ):
         return F"./tests/sagemaker/scripts/{self.framework}"






       @property
       def     SCREAMING_SNAKE_CASE			(      self:    Optional[int] ):
         if self.framework == "pytorch":
           return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04"
         else:
           return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04"







@pytest.fixture(scope="class"      )
def 					UpperCAmelCase__     (   lowerCamelCase      ):
  lowercase			:Union[str, Any]							=  SageMakerTestEnvironment(framework=request.cls.framework      )



