





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


__UpperCamelCase:	Dict = {
    """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       SCREAMING_SNAKE_CASE__  (       _lowercase     : List[str]   ,    _lowercase     : List[str]   ,    _lowercase     : Optional[Any]   ,    _lowercase     : Optional[int]=None			)   ->							str:




		'''simple docstring'''
		# Initialise PyTorch model
		lowercase__					:      int      =		XLNetConfig.from_json_file(_lowercase			)

		lowercase__					:      List[Any]      =		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}"""			)
				lowercase__					:      Union[str, Any]      =		finetuning_task
				lowercase__					:      Optional[Any]      =		GLUE_TASKS_NUM_LABELS[finetuning_task]
				lowercase__					:      Any      =		XLNetForSequenceClassification(_lowercase			)
		elif "squad" in finetuning_task:
				lowercase__					:      List[str]      =		finetuning_task
				lowercase__					:      Tuple      =		XLNetForQuestionAnswering(_lowercase			)
		else:
				lowercase__					:      Union[str, Any]      =		XLNetLMHeadModel(_lowercase			)

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

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


if __name__ == "__main__":
			__UpperCamelCase:	Optional[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(
			    """--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""",
			)
			__UpperCamelCase:	int = 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
			)









__UpperCamelCase:	str = {0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []}

__UpperCamelCase:	Tuple = {0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]}


def       SCREAMING_SNAKE_CASE__  (       _lowercase     : dict[int, list[int]]   ,    _lowercase     : int   ,    _lowercase     : list[bool]			)   ->							list[int]:




		'''simple docstring'''
		lowercase__					:      str      =		True
		lowercase__					:      int      =		[]

		for neighbour in graph[vert]:
				if not visited[neighbour]:
						order += topology_sort(_lowercase   ,    _lowercase   ,    _lowercase			)

		order.append(_lowercase			)

		return order


def       SCREAMING_SNAKE_CASE__  (       _lowercase     : dict[int, list[int]]   ,    _lowercase     : int   ,    _lowercase     : list[bool]			)   ->							list[int]:




		'''simple docstring'''
		lowercase__					:      Union[str, Any]      =		True
		lowercase__					:      Optional[Any]      =		[vert]

		for neighbour in reversed_graph[vert]:
				if not visited[neighbour]:
						component += find_components(_lowercase   ,    _lowercase   ,    _lowercase			)

		return component





def       SCREAMING_SNAKE_CASE__  (       _lowercase     : dict[int, list[int]]			)   ->							list[list[int]]:




		'''simple docstring'''
		lowercase__					:      Optional[int]      =		len(_lowercase			) * [False]
		lowercase__					:      dict[int, list[int]]      =		{vert: [] for vert in range(len(_lowercase			)			)}

		for vert, neighbours in graph.items():
				for neighbour in neighbours:
						reversed_graph[neighbour].append(_lowercase			)

		lowercase__					:      Dict      =		[]
		for i, was_visited in enumerate(_lowercase			):
				if not was_visited:
						order += topology_sort(_lowercase   ,    _lowercase   ,    _lowercase			)

		lowercase__					:      Dict      =		[]
		lowercase__					:      Optional[Any]      =		len(_lowercase			) * [False]

		for i in range(len(_lowercase			)			):
				lowercase__					:      List[Any]      =		order[len(_lowercase			) - i - 1]
				if not visited[vert]:
						lowercase__					:      Tuple      =		find_components(_lowercase   ,    _lowercase   ,    _lowercase			)
						components_list.append(_lowercase			)

		return components_list


