
'''simple docstring'''





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


SCREAMING_SNAKE_CASE     =       {
    """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 snake_case_     (					lowercase__   ,       lowercase__   ,       lowercase__   ,       lowercase__=None		):
  # Initialise PyTorch model
  UpperCAmelCase__      :					Union[str, Any]				       =							XLNetConfig.from_json_file(lowercase__		)

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

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

  # Save pytorch-model
  UpperCAmelCase__      :					Union[str, Any]				       =							os.path.join(lowercase__   ,       lowercase__		)
  UpperCAmelCase__      :					Tuple				       =							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__":
      SCREAMING_SNAKE_CASE     =       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""",
      )
      SCREAMING_SNAKE_CASE     =       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
      )





'''simple docstring'''





from ..utils import DummyObject, requires_backends






class 							UpperCAmelCase_							(    metaclass=A				):

 '''simple docstring'''
 lowercase_					:    Optional[int]				=							["torch", "transformers", "onnx"]







 def __init__(					self		:  Tuple			,				*snake_case__		:  str			,				**snake_case__		:  List[str] ):



   '''simple docstring'''




   requires_backends(self			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  Optional[int]			,				*snake_case__		:  Any			,				**snake_case__		:  List[Any] ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  List[str]			,				*snake_case__		:  Any			,				**snake_case__		:  Tuple ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )






class 							UpperCAmelCase_							(    metaclass=A				):

 '''simple docstring'''
 lowercase_					:    Tuple				=							["torch", "transformers", "onnx"]







 def __init__(					self		:  List[str]			,				*snake_case__		:  List[Any]			,				**snake_case__		:  Any ):



   '''simple docstring'''




   requires_backends(self			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  Dict			,				*snake_case__		:  Any			,				**snake_case__		:  Optional[int] ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  int			,				*snake_case__		:  Dict			,				**snake_case__		:  List[Any] ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )






class 							UpperCAmelCase_							(    metaclass=A				):

 '''simple docstring'''
 lowercase_					:    Optional[int]				=							["torch", "transformers", "onnx"]







 def __init__(					self		:  str			,				*snake_case__		:  Optional[int]			,				**snake_case__		:  str ):



   '''simple docstring'''




   requires_backends(self			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  Any			,				*snake_case__		:  List[str]			,				**snake_case__		:  Dict ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  str			,				*snake_case__		:  Dict			,				**snake_case__		:  Dict ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )






class 							UpperCAmelCase_							(    metaclass=A				):

 '''simple docstring'''
 lowercase_					:    int				=							["torch", "transformers", "onnx"]







 def __init__(					self		:  Any			,				*snake_case__		:  List[str]			,				**snake_case__		:  str ):



   '''simple docstring'''




   requires_backends(self			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  List[str]			,				*snake_case__		:  List[str]			,				**snake_case__		:  Optional[Any] ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  Optional[int]			,				*snake_case__		:  List[Any]			,				**snake_case__		:  int ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )






class 							UpperCAmelCase_							(    metaclass=A				):

 '''simple docstring'''
 lowercase_					:    Dict				=							["torch", "transformers", "onnx"]







 def __init__(					self		:  Any			,				*snake_case__		:  Optional[int]			,				**snake_case__		:  Any ):



   '''simple docstring'''




   requires_backends(self			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  Union[str, Any]			,				*snake_case__		:  str			,				**snake_case__		:  Optional[Any] ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  int			,				*snake_case__		:  int			,				**snake_case__		:  Tuple ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )






class 							UpperCAmelCase_							(    metaclass=A				):

 '''simple docstring'''
 lowercase_					:    Dict				=							["torch", "transformers", "onnx"]







 def __init__(					self		:  List[str]			,				*snake_case__		:  Tuple			,				**snake_case__		:  int ):



   '''simple docstring'''




   requires_backends(self			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  Optional[int]			,				*snake_case__		:  Tuple			,				**snake_case__		:  Dict ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  List[str]			,				*snake_case__		:  Dict			,				**snake_case__		:  List[Any] ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )



