
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)
__lowerCAmelCase					   =       logging.getLogger()

__lowerCAmelCase					   =       logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)





class     lowerCamelCase      (       __lowerCamelCase					):







	def  snake_case__     (     self      :Any   ,     lowercase      :Optional[int]			)		->      List[str]:







				"""simple docstring"""






				os.makedirs(lowercase   ,     exist_ok=lowercase			)
				SCREAMING_SNAKE_CASE     						= {'''source''': '''What is love ?''', '''target''': '''life'''}
				SCREAMING_SNAKE_CASE     						= {'''train''': 1_2, '''val''': 2, '''test''': 2}
				for split in ["train", "test", "val"]:
							for field in ["source", "target"]:
										SCREAMING_SNAKE_CASE     						= '''\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[int]   ,     lowercase      :int   ,     lowercase      :str = "pytorch"			)		->      str:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE     						= self.get_auto_remove_tmp_dir()
				SCREAMING_SNAKE_CASE     						= os.path.join(lowercase   ,     '''output'''			)
				SCREAMING_SNAKE_CASE     						= os.path.join(lowercase   ,     '''data'''			)
				self._create_dummy_data(data_dir=lowercase			)

				SCREAMING_SNAKE_CASE     						= f"""
                --data_dir {data_dir} \
                --output_dir {output_dir} \
                --model_name_or_path facebook/rag-sequence-base \
                --model_type rag_sequence \
                --do_train \
                --do_predict \
                --n_val -1 \
                --val_check_interval 1.0 \
                --train_batch_size 2 \
                --eval_batch_size 1 \
                --max_source_length 25 \
                --max_target_length 25 \
                --val_max_target_length 25 \
                --test_max_target_length 25 \
                --label_smoothing 0.1 \
                --dropout 0.1 \
                --attention_dropout 0.1 \
                --weight_decay 0.001 \
                --adam_epsilon 1e-08 \
                --max_grad_norm 0.1 \
                --lr_scheduler polynomial \
                --learning_rate 3e-04 \
                --num_train_epochs 1 \
                --warmup_steps 4 \
                --gradient_accumulation_steps 1 \
                --distributed-port 8787 \
                --use_dummy_dataset 1 \
                --distributed_retriever {distributed_retriever} \
            """.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'''			)

				SCREAMING_SNAKE_CASE     						= [sys.executable, str(Path(finetune_rag.__file__			).resolve()			)] + testargs
				execute_subprocess_async(lowercase   ,     env=self.get_env()			)

				SCREAMING_SNAKE_CASE     						= os.path.join(lowercase   ,     '''metrics.json'''			)
				with open(lowercase			) as f:
							SCREAMING_SNAKE_CASE     						= json.load(lowercase			)
				return result







	@require_torch_gpu
	def  snake_case__     (     self      :Union[str, Any]			)		->      Dict:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE     						= self._run_finetune(gpus=1			)
				self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']   ,     0.2			)







	@require_torch_multi_gpu
	def  snake_case__     (     self      :List[Any]			)		->      Union[str, Any]:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE     						= self._run_finetune(gpus=2			)
				self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']   ,     0.2			)







	@require_torch_gpu
	@require_ray
	def  snake_case__     (     self      :Any			)		->      List[str]:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE     						= 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]			)		->      Tuple:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE     						= self._run_finetune(gpus=1   ,     distributed_retriever='''ray'''			)
				self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']   ,     0.2			)

import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin





class     lowerCamelCase      (       __lowerCamelCase					):
	UpperCamelCase_  :					Optional[Any]    		=	'MCTCTFeatureExtractor'
	UpperCamelCase_  :					List[Any]    		=	'AutoTokenizer'







	def __init__(     self      :Tuple   ,     lowercase      :List[str]   ,     lowercase      :Dict			)		->      Tuple:







				"""simple docstring"""






				super().__init__(lowercase   ,     lowercase			)
				SCREAMING_SNAKE_CASE     						= self.feature_extractor
				SCREAMING_SNAKE_CASE     						= False







	def __call__(     self      :Union[str, Any]   ,     *lowercase      :Union[str, Any]   ,     **lowercase      :str			)		->      int:







				"""simple docstring"""






				if self._in_target_context_manager:
							return self.current_processor(*lowercase   ,     **lowercase			)

				if "raw_speech" in kwargs:
							warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'''			)
							SCREAMING_SNAKE_CASE     						= kwargs.pop('''raw_speech'''			)
				else:
							SCREAMING_SNAKE_CASE     						= kwargs.pop('''audio'''   ,     lowercase			)
				SCREAMING_SNAKE_CASE     						= kwargs.pop('''sampling_rate'''   ,     lowercase			)
				SCREAMING_SNAKE_CASE     						= kwargs.pop('''text'''   ,     lowercase			)
				if len(lowercase			) > 0:
							SCREAMING_SNAKE_CASE     						= args[0]
							SCREAMING_SNAKE_CASE     						= args[1:]

				if audio is None and text is None:
							raise ValueError('''You need to specify either an `audio` or `text` input to process.'''			)

				if audio is not None:
							SCREAMING_SNAKE_CASE     						= self.feature_extractor(lowercase   ,     *lowercase   ,     sampling_rate=lowercase   ,     **lowercase			)
				if text is not None:
							SCREAMING_SNAKE_CASE     						= self.tokenizer(lowercase   ,     **lowercase			)

				if text is None:
							return inputs
				elif audio is None:
							return encodings
				else:
							SCREAMING_SNAKE_CASE     						= encodings['''input_ids''']
							return inputs







	def  snake_case__     (     self      :Dict   ,     *lowercase      :Union[str, Any]   ,     **lowercase      :List[str]			)		->      Union[str, Any]:







				"""simple docstring"""






				return self.tokenizer.batch_decode(*lowercase   ,     **lowercase			)







	def  snake_case__     (     self      :List[Any]   ,     *lowercase      :List[Any]   ,     **lowercase      :List[str]			)		->      int:







				"""simple docstring"""






				if self._in_target_context_manager:
							return self.current_processor.pad(*lowercase   ,     **lowercase			)

				SCREAMING_SNAKE_CASE     						= kwargs.pop('''input_features'''   ,     lowercase			)
				SCREAMING_SNAKE_CASE     						= kwargs.pop('''labels'''   ,     lowercase			)
				if len(lowercase			) > 0:
							SCREAMING_SNAKE_CASE     						= args[0]
							SCREAMING_SNAKE_CASE     						= args[1:]

				if input_features is not None:
							SCREAMING_SNAKE_CASE     						= self.feature_extractor.pad(lowercase   ,     *lowercase   ,     **lowercase			)
				if labels is not None:
							SCREAMING_SNAKE_CASE     						= self.tokenizer.pad(lowercase   ,     **lowercase			)

				if labels is None:
							return input_features
				elif input_features is None:
							return labels
				else:
							SCREAMING_SNAKE_CASE     						= labels['''input_ids''']
							return input_features







	def  snake_case__     (     self      :Dict   ,     *lowercase      :List[str]   ,     **lowercase      :Union[str, Any]			)		->      List[Any]:







				"""simple docstring"""






				return self.tokenizer.decode(*lowercase   ,     **lowercase			)







	@contextmanager
	def  snake_case__     (     self      :str			)		->      Union[str, Any]:







				"""simple docstring"""






				warnings.warn(
				    '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '''
				    '''labels by using the argument `text` of the regular `__call__` method (either in the same call as '''
				    '''your audio inputs, or in a separate call.'''			)
				SCREAMING_SNAKE_CASE     						= True
				SCREAMING_SNAKE_CASE     						= self.tokenizer
				yield
				SCREAMING_SNAKE_CASE     						= self.feature_extractor
				SCREAMING_SNAKE_CASE     						= False