


"""simple docstring"""
import logging
import random

import ray

from transformers import RagConfig, RagRetriever, RagTokenizer
from transformers.models.rag.retrieval_rag import CustomHFIndex


SCREAMING_SNAKE_CASE :    Tuple									=				logging.getLogger(__name__)



class 			_UpperCAmelCase :






		'''simple docstring'''

		def __init__(self					):





			'''simple docstring'''

			__snake_case	:						List[str]						=				False



		def 				SCREAMING_SNAKE_CASE  (self				,       a_				,       a_				,       a_				,       a_					):





			'''simple docstring'''

			if not self.initialized:
				__snake_case	:						int						=				RagRetriever(
				    a_				,       question_encoder_tokenizer=a_				,       generator_tokenizer=a_				,       index=a_				,       init_retrieval=a_				,       )
				__snake_case	:						str						=				True



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			self.retriever.index.init_index()



		def 				SCREAMING_SNAKE_CASE  (self				,       a_				,       a_					):





			'''simple docstring'''

			__snake_case	,							__snake_case	:						List[Any]						=				self.retriever._main_retrieve(a_				,       a_					)
			return doc_ids, retrieved_doc_embeds



class 			_UpperCAmelCase (							__snake_case				):






		'''simple docstring'''

		def __init__(self				,       a_				,       a_				,       a_				,       a_				,       a_=None					):





			'''simple docstring'''

			if index is not None and index.is_initialized() and len(a_					) > 0:
				raise ValueError(
				    '''When using Ray for distributed fine-tuning, '''
				    '''you\'ll need to provide the paths instead, '''
				    '''as the dataset and the index are loaded '''
				    '''separately. More info in examples/rag/use_own_knowledge_dataset.py '''					)
			super().__init__(
			    a_				,       question_encoder_tokenizer=a_				,       generator_tokenizer=a_				,       index=a_				,       init_retrieval=a_				,       )
			__snake_case	:						Tuple						=				retrieval_workers
			if len(self.retrieval_workers					) > 0:
				ray.get(
				    [
				        worker.create_rag_retriever.remote(a_				,       a_				,       a_				,       a_					)
				        for worker in self.retrieval_workers
				    ]					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			logger.info('''initializing retrieval'''					)

			if len(self.retrieval_workers					) > 0:
				ray.get([worker.init_retrieval.remote() for worker in self.retrieval_workers]					)
			else:
				# Non-distributed training. Load index into this same process.
				self.index.init_index()



		def 				SCREAMING_SNAKE_CASE  (self				,       a_				,       a_					):





			'''simple docstring'''

			if len(self.retrieval_workers					) > 0:
				# Select a random retrieval actor.
				__snake_case	:						Tuple						=				self.retrieval_workers[random.randint(0				,       len(self.retrieval_workers					) - 1					)]
				__snake_case	,							__snake_case	:						int						=				ray.get(random_worker.retrieve.remote(a_				,       a_					)					)
			else:
				__snake_case	,							__snake_case	:						Optional[int]						=				self._main_retrieve(a_				,       a_					)
			return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(a_					)



		@classmethod
		def 				SCREAMING_SNAKE_CASE  (cls				,       a_				,       a_=None				,       **a_					):





			'''simple docstring'''

			return super(a_				,       cls					).get_tokenizers(a_				,       a_				,       **a_					)



		@classmethod
		def 				SCREAMING_SNAKE_CASE  (cls				,       a_				,       a_				,       a_=None				,       **a_					):





			'''simple docstring'''

			__snake_case	:						Optional[Any]						=				kwargs.pop('''config'''				,       a_					) or RagConfig.from_pretrained(a_				,       **a_					)
			__snake_case	:						Any						=				RagTokenizer.from_pretrained(a_				,       config=a_					)
			__snake_case	:						Union[str, Any]						=				rag_tokenizer.question_encoder
			__snake_case	:						Tuple						=				rag_tokenizer.generator
			if indexed_dataset is not None:
				__snake_case	:						Union[str, Any]						=				'''custom'''
				__snake_case	:						Optional[int]						=				CustomHFIndex(config.retrieval_vector_size				,       a_					)
			else:
				__snake_case	:						Dict						=				cls._build_index(a_					)
			return cls(
			    a_				,       question_encoder_tokenizer=a_				,       generator_tokenizer=a_				,       retrieval_workers=a_				,       index=a_				,       )









"""simple docstring"""
import argparse

import torch

from transformers import OpenAIGPTConfig, OpenAIGPTModel, load_tf_weights_in_openai_gpt
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()






def 		lowercase							(     _snake_case						:      List[Any]     ,   _snake_case						:      Tuple     ,   _snake_case						:      int     )     ->List[Any]:
	"""simple docstring"""





	if openai_config_file == "":
		__snake_case	:						Dict						=				OpenAIGPTConfig()
	else:
		__snake_case	:						int						=				OpenAIGPTConfig.from_json_file(_snake_case     )
	__snake_case	:						Tuple						=				OpenAIGPTModel(_snake_case     )

	# Load weights from numpy
	load_tf_weights_in_openai_gpt(_snake_case     ,   _snake_case     ,   _snake_case     )

	# Save pytorch-model
	__snake_case	:						str						=				pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME
	__snake_case	:						Optional[Any]						=				pytorch_dump_folder_path + '''/''' + CONFIG_NAME
	print(f"""Save PyTorch model to {pytorch_weights_dump_path}"""     )
	torch.save(model.state_dict()     ,   _snake_case     )
	print(f"""Save configuration file to {pytorch_config_dump_path}"""     )
	with open(_snake_case     ,   '''w'''     ,   encoding='''utf-8'''     ) as f:
		f.write(config.to_json_string()     )


if __name__ == "__main__":
			SCREAMING_SNAKE_CASE :    Any									=				argparse.ArgumentParser()
			# Required parameters
			parser.add_argument(
			    """--openai_checkpoint_folder_path""",
			    default=None,
			    type=str,
			    required=True,
			    help="""Path to the TensorFlow checkpoint path.""",
			)
			parser.add_argument(
			    """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
			)
			parser.add_argument(
			    """--openai_config_file""",
			    default="""""",
			    type=str,
			    help=(
			        """An optional config json file corresponding to the pre-trained OpenAI model. \n"""
			        """This specifies the model architecture."""
			    ),
			)
			SCREAMING_SNAKE_CASE :    Optional[int]									=				parser.parse_args()
			convert_openai_checkpoint_to_pytorch(
			    args.openai_checkpoint_folder_path, args.openai_config_file, args.pytorch_dump_folder_path
			)





