






import logging
import os
from typing import List, Tuple

import numpy as np
import psutil
import torch
import torch.distributed as dist

from transformers import RagRetriever


__snake_case    :    List[Any]         =     logging.getLogger(__name__)


class     __SCREAMING_SNAKE_CASE					(   __lowercase):






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






												"""simple docstring"""
												super().__init__(
												    _UpperCamelCase     ,						question_encoder_tokenizer=_UpperCamelCase     ,						generator_tokenizer=_UpperCamelCase     ,						index=_UpperCamelCase     ,						init_retrieval=_UpperCamelCase     ,						)
												lowerCAmelCase__    						=						None






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase   ):






												"""simple docstring"""
												logger.info('initializing retrieval'   )

												# initializing a separate process group for retrieval as the default
												# nccl backend doesn't support gather/scatter operations while gloo
												# is too slow to replace nccl for the core gpu communication
												if dist.is_initialized():
																		logger.info('dist initialized'   )
																		# needs to be set manually
																		lowerCAmelCase__    						=						self._infer_socket_ifname()
																		# avoid clash with the NCCL port
																		lowerCAmelCase__    						=						str(distributed_port + 1   )
																		lowerCAmelCase__    						=						dist.new_group(ranks=_UpperCamelCase     ,						backend='gloo'   )

												# initialize retriever only on the main worker
												if not dist.is_initialized() or self._is_main():
																		logger.info('dist not initialized / main'   )
																		self.index.init_index()

												# all processes wait untill the retriever is initialized by the main process
												if dist.is_initialized():
																		torch.distributed.barrier(group=self.process_group   )






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												return dist.get_rank(group=self.process_group   ) == 0






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase=torch.floataa   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						torch.empty(_UpperCamelCase     ,						dtype=_UpperCamelCase   )
												dist.scatter(_UpperCamelCase     ,						src=0     ,						scatter_list=_UpperCamelCase     ,						group=self.process_group   )
												return target_tensor






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						psutil.net_if_addrs()
												# a hacky way to deal with varying network interface names
												lowerCAmelCase__    						=						next((addr for addr in addrs if addr.startswith('e'   ))     ,						_UpperCamelCase   )
												return ifname







						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase   ):






												"""simple docstring"""

												# single GPU training
												if not dist.is_initialized():
																		lowerCAmelCase__						,       lowerCAmelCase__    						=						self._main_retrieve(_UpperCamelCase     ,						_UpperCamelCase   )
																		return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(_UpperCamelCase   )

												# distributed training
												lowerCAmelCase__    						=						dist.get_world_size(group=self.process_group   )

												# gather logic
												lowerCAmelCase__    						=						None
												if self._is_main():
																		lowerCAmelCase__    						=						[torch.empty(question_hidden_states.shape     ,						dtype=torch.floataa   ) for _ in range(_UpperCamelCase   )]
												dist.gather(torch.tensor(_UpperCamelCase   )     ,						dst=0     ,						gather_list=_UpperCamelCase     ,						group=self.process_group   )

												# scatter logic
												lowerCAmelCase__    						=						question_hidden_states.shape[0]
												lowerCAmelCase__    						=						[]
												lowerCAmelCase__    						=						[]
												if self._is_main():
																		assert len(_UpperCamelCase   ) == world_size
																		lowerCAmelCase__						,       lowerCAmelCase__    						=						self._main_retrieve(torch.cat(_UpperCamelCase   ).numpy()     ,						_UpperCamelCase   )
																		lowerCAmelCase__						,       lowerCAmelCase__    						=						torch.tensor(_UpperCamelCase   ), torch.tensor(_UpperCamelCase   )
																		lowerCAmelCase__    						=						self._chunk_tensor(_UpperCamelCase     ,						_UpperCamelCase   )
																		lowerCAmelCase__    						=						self._chunk_tensor(_UpperCamelCase     ,						_UpperCamelCase   )
												lowerCAmelCase__    						=						self._scattered(_UpperCamelCase     ,						[n_queries, n_docs]     ,						target_type=torch.intaa   )
												lowerCAmelCase__    						=						self._scattered(_UpperCamelCase     ,						[n_queries, n_docs, question_hidden_states.shape[1]]   )

												return retrieved_doc_embeds.numpy(), doc_ids.numpy(), self.index.get_doc_dicts(_UpperCamelCase   )













import argparse

import torch

from transformers import BertConfig, BertForPreTraining, load_tf_weights_in_bert
from transformers.utils import logging


logging.set_verbosity_info()







def 	_UpperCamelCase   (    UpperCamelCase_       :					Dict    ,       UpperCamelCase_       :					str    ,       UpperCamelCase_       :					Optional[Any]		)  ->   Any:

						"""simple docstring"""



						lowerCAmelCase__    						=						BertConfig.from_json_file(UpperCamelCase_		)
						print(F"Building PyTorch model from configuration: {config}"		)
						lowerCAmelCase__    						=						BertForPreTraining(UpperCamelCase_		)

						# Load weights from tf checkpoint
						load_tf_weights_in_bert(UpperCamelCase_    ,       UpperCamelCase_    ,       UpperCamelCase_		)

						# Save pytorch-model
						print(F"Save PyTorch model to {pytorch_dump_path}"		)
						torch.save(model.state_dict()    ,       UpperCamelCase_		)


if __name__ == "__main__":
					__snake_case    :    Union[str, Any]         =     argparse.ArgumentParser()
					# Required parameters
					parser.add_argument(
					    """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path."""
					)
					parser.add_argument(
					    """--bert_config_file""",
					    default=None,
					    type=str,
					    required=True,
					    help=(
					        """The config json file corresponding to the pre-trained BERT model. \n"""
					        """This specifies the model architecture."""
					    ),
					)
					parser.add_argument(
					    """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
					)
					__snake_case    :    List[str]         =     parser.parse_args()
					convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)





