





"""simple docstring"""
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


__UpperCamelCase     :						int						       =  logging.getLogger(__name__)







class       a							(   a__  ):



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







  """simple docstring"""






  super().__init__(
      _snake_case				,       question_encoder_tokenizer=_snake_case				,       generator_tokenizer=_snake_case				,       index=_snake_case				,       init_retrieval=_snake_case				,       )
  lowerCAmelCase    				= None



 def    UpperCamelCase__  (     self				,       _snake_case     ):







  """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=_snake_case				,       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				,       _snake_case				,       _snake_case				,       _snake_case=torch.floataa     ):







  """simple docstring"""






  lowerCAmelCase    				= torch.empty(_snake_case				,       dtype=_snake_case     )
  dist.scatter(_snake_case				,       src=0				,       scatter_list=_snake_case				,       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'     ))				,       _snake_case     )
  return ifname



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case     ):







  """simple docstring"""






  if not dist.is_initialized():
   lowerCAmelCase				,lowerCAmelCase    				= self._main_retrieve(_snake_case				,       _snake_case     )
   return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(_snake_case     )

  # 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(_snake_case     )]
  dist.gather(torch.tensor(_snake_case     )				,       dst=0				,       gather_list=_snake_case				,       group=self.process_group     )

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

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







import argparse
import os

import torch

from transformers import (
    XLNetConfig,
    XLNetForQuestionAnswering,
    XLNetForSequenceClassification,
    XLNetLMHeadModel,
    load_tf_weights_in_xlnet,
)
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


__A							={
    '''cola''': 2,
    '''mnli''': 3,
    '''mrpc''': 2,
    '''sst-2''': 2,
    '''sts-b''': 1,
    '''qqp''': 2,
    '''qnli''': 2,
    '''rte''': 2,
    '''wnli''': 2,
}


logging.set_verbosity_info()





def      lowerCamelCase_					(							lowerCamelCase__      ,					lowerCamelCase__      ,					lowerCamelCase__      ,					lowerCamelCase__=None       ):
				# Initialise PyTorch model
				lowerCamelCase_         =     XLNetConfig.from_json_file(lowerCamelCase__       )

				lowerCamelCase_         =     finetuning_task.lower() if finetuning_task is not None else ""
				if finetuning_task in GLUE_TASKS_NUM_LABELS:
								print(F'Building PyTorch XLNetForSequenceClassification model from configuration: {config}'       )
								lowerCamelCase_         =     finetuning_task
								lowerCamelCase_         =     GLUE_TASKS_NUM_LABELS[finetuning_task]
								lowerCamelCase_         =     XLNetForSequenceClassification(lowerCamelCase__       )
				elif "squad" in finetuning_task:
								lowerCamelCase_         =     finetuning_task
								lowerCamelCase_         =     XLNetForQuestionAnswering(lowerCamelCase__       )
				else:
								lowerCamelCase_         =     XLNetLMHeadModel(lowerCamelCase__       )

				# Load weights from tf checkpoint
				load_tf_weights_in_xlnet(lowerCamelCase__      ,					lowerCamelCase__      ,					lowerCamelCase__       )

				# Save pytorch-model
				lowerCamelCase_         =     os.path.join(lowerCamelCase__      ,					lowerCamelCase__       )
				lowerCamelCase_         =     os.path.join(lowerCamelCase__      ,					lowerCamelCase__       )
				print(F'Save PyTorch model to {os.path.abspath(lowerCamelCase__       )}'       )
				torch.save(model.state_dict()      ,					lowerCamelCase__       )
				print(F'Save configuration file to {os.path.abspath(lowerCamelCase__       )}'       )
				with open(lowerCamelCase__      ,					"w"      ,					encoding="utf-8"       ) as f:
								f.write(config.to_json_string()       )


if __name__ == "__main__":
			__A							=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(
			    '''--xlnet_config_file''',
			    default=None,
			    type=str,
			    required=True,
			    help=(
			        '''The config json file corresponding to the pre-trained XLNet model. \n'''
			        '''This specifies the model architecture.'''
			    ),
			)
			parser.add_argument(
			    '''--pytorch_dump_folder_path''',
			    default=None,
			    type=str,
			    required=True,
			    help='''Path to the folder to store the PyTorch model or dataset/vocab.''',
			)
			parser.add_argument(
			    '''--finetuning_task''',
			    default=None,
			    type=str,
			    help='''Name of a task on which the XLNet TensorFlow model was fine-tuned''',
			)
			__A							=parser.parse_args()
			print(args)

			convert_xlnet_checkpoint_to_pytorch(
			    args.tf_checkpoint_path, args.xlnet_config_file, args.pytorch_dump_folder_path, args.finetuning_task
			)


