


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)
A							:					Optional[int]     						=     logging.getLogger()

A							:					Tuple     						=     logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)






class     UpperCamelCase(  lowercase__   ):


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Tuple     ,    SCREAMING_SNAKE_CASE	:		Tuple						)			->	Any:




											'''simple docstring'''

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




											'''simple docstring'''

											__snake_case				=		self.get_auto_remove_tmp_dir()
											__snake_case				=		os.path.join(__lowercase     ,    "output"						)
											__snake_case				=		os.path.join(__lowercase     ,    "data"						)
											self._create_dummy_data(data_dir=__lowercase						)

											__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"						)

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

											__snake_case				=		os.path.join(__lowercase     ,    "metrics.json"						)
											with open(__lowercase						) as f:
																	__snake_case				=		json.load(__lowercase						)
											return result


					@require_torch_gpu
					def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[Any]						)			->	int:




											'''simple docstring'''

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


					@require_torch_multi_gpu
					def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[Any]						)			->	str:




											'''simple docstring'''

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


					@require_torch_gpu
					@require_ray
					def 							SCREAMING_SNAKE_CASE_  (						self	:		Tuple						)			->	Optional[int]:




											'''simple docstring'''

											__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 							SCREAMING_SNAKE_CASE_  (						self	:		Tuple						)			->	int:




											'''simple docstring'''

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









class     UpperCamelCase:


					def __init__(						self	:		Any						)			->	Any:




											'''simple docstring'''

											__snake_case				=		0
											__snake_case				=		0
											__snake_case				=		{}


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Dict     ,    SCREAMING_SNAKE_CASE	:		Optional[int]						)			->	Optional[Any]:




											'''simple docstring'''

											if vertex not in self.adjacency:
																	__snake_case				=		{}
																	self.num_vertices += 1


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Union[str, Any]     ,    SCREAMING_SNAKE_CASE	:		Optional[int]     ,    SCREAMING_SNAKE_CASE	:		Optional[int]     ,    SCREAMING_SNAKE_CASE	:		str						)			->	Optional[Any]:




											'''simple docstring'''

											self.add_vertex(SCREAMING_SNAKE_CASE						)
											self.add_vertex(SCREAMING_SNAKE_CASE						)

											if head == tail:
																	return

											__snake_case				=		weight
											__snake_case				=		weight


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[int]						)			->	Tuple:




											'''simple docstring'''

											__snake_case				=		self.get_edges()
											for edge in edges:
																	__snake_case		,					__snake_case		,					__snake_case				=		edge
																	edges.remove((tail, head, weight)						)
											for i in range(len(SCREAMING_SNAKE_CASE						)						):
																	__snake_case				=		list(edges[i]						)

											edges.sort(key=lambda SCREAMING_SNAKE_CASE						: e[2]						)
											for i in range(len(SCREAMING_SNAKE_CASE						) - 1						):
																	if edges[i][2] >= edges[i + 1][2]:
																							__snake_case				=		edges[i][2] + 1
											for edge in edges:
																	__snake_case		,					__snake_case		,					__snake_case				=		edge
																	__snake_case				=		weight
																	__snake_case				=		weight


					def __str__(						self	:		Tuple						)			->	List[Any]:




											'''simple docstring'''

											__snake_case				=		""
											for tail in self.adjacency:
																	for head in self.adjacency[tail]:
																							__snake_case				=		self.adjacency[head][tail]
																							string += f'''{head} -> {tail} == {weight}\n'''
											return string.rstrip("\n"						)


					def 							SCREAMING_SNAKE_CASE_  (						self	:		int						)			->	Optional[Any]:




											'''simple docstring'''

											__snake_case				=		[]
											for tail in self.adjacency:
																	for head in self.adjacency[tail]:
																							output.append((tail, head, self.adjacency[head][tail])						)
											return output


					def 							SCREAMING_SNAKE_CASE_  (						self	:		Any						)			->	List[Any]:




											'''simple docstring'''

											return self.adjacency.keys()


					@staticmethod
					def 							SCREAMING_SNAKE_CASE_  (						SCREAMING_SNAKE_CASE	:		Any=None     ,    SCREAMING_SNAKE_CASE	:		List[Any]=None						)			->	int:




											'''simple docstring'''

											__snake_case				=		Graph()
											if vertices is None:
																	__snake_case				=		[]
											if edges is None:
																	__snake_case				=		[]
											for vertex in vertices:
																	g.add_vertex(SCREAMING_SNAKE_CASE						)
											for edge in edges:
																	g.add_edge(*SCREAMING_SNAKE_CASE						)
											return g






					class     UpperCamelCase:


										def __init__(						self	:		Union[str, Any]						)			->	Union[str, Any]:




																'''simple docstring'''

																__snake_case				=		{}
																__snake_case				=		{}


										def __len__(						self	:		List[str]						)			->	Dict:




																'''simple docstring'''

																return len(self.parent						)


										def 							SCREAMING_SNAKE_CASE_  (						self	:		List[str]     ,    SCREAMING_SNAKE_CASE	:		int						)			->	List[str]:




																'''simple docstring'''

																if item in self.parent:
																						return self.find(SCREAMING_SNAKE_CASE						)

																__snake_case				=		item
																__snake_case				=		0
																return item


										def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[Any]     ,    SCREAMING_SNAKE_CASE	:		List[str]						)			->	Any:




																'''simple docstring'''

																if item not in self.parent:
																						return self.make_set(SCREAMING_SNAKE_CASE						)
																if item != self.parent[item]:
																						__snake_case				=		self.find(self.parent[item]						)
																return self.parent[item]






										def 							SCREAMING_SNAKE_CASE_  (						self	:		Optional[Any]     ,    SCREAMING_SNAKE_CASE	:		int     ,    SCREAMING_SNAKE_CASE	:		Union[str, Any]						)			->	Dict:




																'''simple docstring'''

																__snake_case				=		self.find(SCREAMING_SNAKE_CASE						)
																__snake_case				=		self.find(SCREAMING_SNAKE_CASE						)

																if roota == roota:
																						return roota

																if self.rank[roota] > self.rank[roota]:
																						__snake_case				=		roota
																						return roota

																if self.rank[roota] < self.rank[roota]:
																						__snake_case				=		roota
																						return roota

																if self.rank[roota] == self.rank[roota]:
																						self.rank[roota] += 1
																						__snake_case				=		roota
																						return roota
																return None




					@staticmethod
					def 							SCREAMING_SNAKE_CASE_  (						SCREAMING_SNAKE_CASE	:		str						)			->	Any:




											'''simple docstring'''

											__snake_case				=		graph.num_vertices

											__snake_case				=		Graph.UnionFind()
											__snake_case				=		[]
											while num_components > 1:
																	__snake_case				=		{}
																	for vertex in graph.get_vertices():
																							__snake_case				=		-1

																	__snake_case				=		graph.get_edges()
																	for edge in edges:
																							__snake_case		,					__snake_case		,					__snake_case				=		edge
																							edges.remove((tail, head, weight)						)
																	for edge in edges:
																							__snake_case		,					__snake_case		,					__snake_case				=		edge
																							__snake_case				=		union_find.find(SCREAMING_SNAKE_CASE						)
																							__snake_case				=		union_find.find(SCREAMING_SNAKE_CASE						)
																							if seta != seta:
																													if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
																																			__snake_case				=		[head, tail, weight]

																													if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
																																			__snake_case				=		[head, tail, weight]
																	for vertex in cheap_edge:
																							if cheap_edge[vertex] != -1:
																													__snake_case		,					__snake_case		,					__snake_case				=		cheap_edge[vertex]
																													if union_find.find(SCREAMING_SNAKE_CASE						) != union_find.find(SCREAMING_SNAKE_CASE						):
																																			union_find.union(SCREAMING_SNAKE_CASE     ,    SCREAMING_SNAKE_CASE						)
																																			mst_edges.append(cheap_edge[vertex]						)
																																			__snake_case				=		num_components - 1
											__snake_case				=		Graph.build(edges=SCREAMING_SNAKE_CASE						)
											return mst





