



"""simple docstring"""

import json
import logging
import os
import sys
from pathlib import Path

import finetune_rag

from transformers.file_utils import is_apex_available
from transformers.testing_utils import (
    TestCasePlus,
    execute_subprocess_async,
    require_ray,
    require_torch_gpu,
    require_torch_multi_gpu,
)


logging.basicConfig(level=logging.DEBUG)
lowercase__					:						Optional[int]												=			logging.getLogger()

lowercase__					:						Any												=			logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)




class  _UpperCAmelCase (    lowerCAmelCase__):

 def 		_snake_case      (  self	: List[Any]						,  lowercase_	: str       ):
     os.makedirs(lowercase_						,  exist_ok=lowercase_       )
     snake_case_				:  str       =			{'''source''': '''What is love ?''', '''target''': '''life'''}
     snake_case_				:  Optional[Any]       =			{'''train''': 12, '''val''': 2, '''test''': 2}
     for split in ["train", "test", "val"]:
         for field in ["source", "target"]:
             snake_case_				:  List[Any]       =			'''\n'''.join([contents[field]] * n_lines[split]       )
             with open(os.path.join(lowercase_						,  f"{split}.{field}"       )						,  '''w'''       ) as f:
                 f.write(lowercase_       )

 def 		_snake_case      (  self	: Optional[Any]						,  lowercase_	: int						,  lowercase_	: str = "pytorch"       ):
     snake_case_				:  str       =			self.get_auto_remove_tmp_dir()
     snake_case_				:  List[Any]       =			os.path.join(lowercase_						,  '''output'''       )
     snake_case_				:  List[str]       =			os.path.join(lowercase_						,  '''data'''       )
     self._create_dummy_data(data_dir=lowercase_       )

     snake_case_				:  Any       =			f"\n                --data_dir {data_dir} \\n                --output_dir {output_dir} \\n                --model_name_or_path facebook/rag-sequence-base \\n                --model_type rag_sequence \\n                --do_train \\n                --do_predict \\n                --n_val -1 \\n                --val_check_interval 1.0 \\n                --train_batch_size 2 \\n                --eval_batch_size 1 \\n                --max_source_length 25 \\n                --max_target_length 25 \\n                --val_max_target_length 25 \\n                --test_max_target_length 25 \\n                --label_smoothing 0.1 \\n                --dropout 0.1 \\n                --attention_dropout 0.1 \\n                --weight_decay 0.001 \\n                --adam_epsilon 1e-08 \\n                --max_grad_norm 0.1 \\n                --lr_scheduler polynomial \\n                --learning_rate 3e-04 \\n                --num_train_epochs 1 \\n                --warmup_steps 4 \\n                --gradient_accumulation_steps 1 \\n                --distributed-port 8787 \\n                --use_dummy_dataset 1 \\n                --distributed_retriever {distributed_retriever} \\n            ".split()

     if gpus > 0:
         testargs.append(f"--gpus={gpus}"       )
         if is_apex_available():
             testargs.append('''--fp16'''       )
     else:
         testargs.append('''--gpus=0'''       )
         testargs.append('''--distributed_backend=ddp_cpu'''       )
         testargs.append('''--num_processes=2'''       )

     snake_case_				:  int       =			[sys.executable, str(Path(finetune_rag.__file__       ).resolve()       )] + testargs
     execute_subprocess_async(lowercase_						,  env=self.get_env()       )

     snake_case_				:  List[str]       =			os.path.join(lowercase_						,  '''metrics.json'''       )
     with open(lowercase_       ) as f:
         snake_case_				:  Optional[int]       =			json.load(lowercase_       )
     return result

 @require_torch_gpu
 def 		_snake_case      (  self	: Any       ):
     snake_case_				:  str       =			self._run_finetune(gpus=1       )
     self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']						,  0.2       )

 @require_torch_multi_gpu
 def 		_snake_case      (  self	: Union[str, Any]       ):
     snake_case_				:  List[Any]       =			self._run_finetune(gpus=2       )
     self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']						,  0.2       )

 @require_torch_gpu
 @require_ray
 def 		_snake_case      (  self	: List[str]       ):
     snake_case_				:  str       =			self._run_finetune(gpus=1						,  distributed_retriever='''ray'''       )
     self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']						,  0.2       )






 @require_torch_multi_gpu
 @require_ray
 def 		_snake_case      (  self	: Union[str, Any]       ):
     snake_case_				:  Tuple       =			self._run_finetune(gpus=1						,  distributed_retriever='''ray'''       )
     self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']						,  0.2       )










"""simple docstring"""

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


lowercase__					:						Tuple												=			get_tests_dir('''fixtures''')
lowercase__					:						List[str]												=			get_tests_dir('''fixtures/dummy_feature_extractor_config.json''')
lowercase__					:						Dict												=			get_tests_dir('''fixtures/dummy-config.json''')




class  _UpperCAmelCase (    unittest.TestCase):

 def 		_snake_case      (  self	: Tuple       ):
     snake_case_				:  Optional[Any]       =			0

 def 		_snake_case      (  self	: int       ):
     snake_case_				:  str       =			AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h'''       )
     self.assertIsInstance(lowercase_						,  lowercase_       )

 def 		_snake_case      (  self	: Optional[Any]       ):
     snake_case_				:  Dict       =			AutoFeatureExtractor.from_pretrained(lowercase_       )
     self.assertIsInstance(lowercase_						,  lowercase_       )

 def 		_snake_case      (  self	: int       ):
     with tempfile.TemporaryDirectory() as tmpdirname:
         snake_case_				:  List[Any]       =			WavaVecaConfig()

         # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
         snake_case_				:  Optional[Any]       =			AutoFeatureExtractor.from_pretrained(lowercase_       ).to_dict()

         config_dict.pop('''feature_extractor_type'''       )
         snake_case_				:  Optional[Any]       =			WavaVecaFeatureExtractor(**lowercase_       )

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

         snake_case_				:  List[Any]       =			AutoFeatureExtractor.from_pretrained(lowercase_       )

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

     self.assertIsInstance(lowercase_						,  lowercase_       )

 def 		_snake_case      (  self	: Optional[Any]       ):
     snake_case_				:  str       =			AutoFeatureExtractor.from_pretrained(lowercase_       )
     self.assertIsInstance(lowercase_						,  lowercase_       )

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

 def 		_snake_case      (  self	: Any       ):
     with self.assertRaisesRegex(
         lowercase_						,  r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''       ):
         snake_case_				:  Dict       =			AutoFeatureExtractor.from_pretrained(lowercase_						,  revision='''aaaaaa'''       )

 def 		_snake_case      (  self	: Optional[Any]       ):
     with self.assertRaisesRegex(
         lowercase_						,  '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''						,  ):
         snake_case_				:  Dict       =			AutoFeatureExtractor.from_pretrained('''hf-internal-testing/config-no-model'''       )

 def 		_snake_case      (  self	: Optional[int]       ):
     # If remote code is not set, we will time out when asking whether to load the model.
     with self.assertRaises(lowercase_       ):
         snake_case_				:  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(lowercase_       ):
         snake_case_				:  str       =			AutoFeatureExtractor.from_pretrained(
             '''hf-internal-testing/test_dynamic_feature_extractor'''						,  trust_remote_code=lowercase_       )

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

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

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

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

     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	: Union[str, Any]       ):




     class  _UpperCAmelCase (    lowerCAmelCase__):
      _lowerCAmelCase     :    str	      =	True

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

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





