






'''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        lowercase__					( __UpperCamelCase						,		__UpperCamelCase						,		__UpperCamelCase						,		__UpperCamelCase=None       )->					Optional[int]:
      # Initialise PyTorch model
      UpperCamelCase							    =   XLNetConfig.from_json_file(__UpperCamelCase       )

      UpperCamelCase							    =   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							    =   finetuning_task
            UpperCamelCase							    =   GLUE_TASKS_NUM_LABELS[finetuning_task]
            UpperCamelCase							    =   XLNetForSequenceClassification(__UpperCamelCase       )
      elif "squad" in finetuning_task:
            UpperCamelCase							    =   finetuning_task
            UpperCamelCase							    =   XLNetForQuestionAnswering(__UpperCamelCase       )
      else:
            UpperCamelCase							    =   XLNetLMHeadModel(__UpperCamelCase       )

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

      # Save pytorch-model
      UpperCamelCase							    =   os.path.join(__UpperCamelCase						,		__UpperCamelCase       )
      UpperCamelCase							    =   os.path.join(__UpperCamelCase						,		__UpperCamelCase       )
      print(F"Save PyTorch model to {os.path.abspath(__UpperCamelCase       )}"       )
      torch.save(model.state_dict()						,		__UpperCamelCase       )
      print(F"Save configuration file to {os.path.abspath(__UpperCamelCase       )}"       )
      with open(__UpperCamelCase						,		"""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
     )






# flake8: noqa
# Lint as: python3

from typing import Dict, List, Optional, Type

from .. import config
from ..utils import logging
from .formatting import (
    ArrowFormatter,
    CustomFormatter,
    Formatter,
    PandasFormatter,
    PythonFormatter,
    TensorFormatter,
    format_table,
    query_table,
)
from .np_formatter import NumpyFormatter


a__		     =     logging.get_logger(__name__)

a__		     =     {}
a__		     =     {}
a__		     =     {}





def 				A__ (snake_case   :		type		,	snake_case   :		Optional[str]		,	snake_case   :		Optional[List[str]] = None		,	)     ->						str:
					__UpperCamelCase					:	int             =      aliases if aliases is not None else []
					if format_type in _FORMAT_TYPES:
										logger.warning(
										    F'''Overwriting format type \'{format_type}\' ({_FORMAT_TYPES[format_type].__name__} -> {formatter_cls.__name__})'''				)
					__UpperCamelCase					:	Optional[Any]             =      formatter_cls
					for alias in set(aliases + [format_type]				):
										if alias in _FORMAT_TYPES_ALIASES:
															logger.warning(
															    F'''Overwriting format type alias \'{alias}\' ({_FORMAT_TYPES_ALIASES[alias]} -> {format_type})'''				)
										__UpperCamelCase					:	Optional[int]             =      format_type





def 				A__ (snake_case   :		Exception		,	snake_case   :		Optional[str]		,	snake_case   :		Optional[List[str]] = None				)     ->						Tuple:
					__UpperCamelCase					:	List[Any]             =      aliases if aliases is not None else []
					for alias in set(aliases + [format_type]				):
										__UpperCamelCase					:	Optional[int]             =      unavailable_error


# Here we define all the available formatting functions that can be used by `Dataset.set_format`
_register_formatter(PythonFormatter, None, aliases=['''python'''])
_register_formatter(ArrowFormatter, '''arrow''', aliases=['''pa''', '''pyarrow'''])
_register_formatter(NumpyFormatter, '''numpy''', aliases=['''np'''])
_register_formatter(PandasFormatter, '''pandas''', aliases=['''pd'''])
_register_formatter(CustomFormatter, '''custom''')

if config.TORCH_AVAILABLE:
	from .torch_formatter import TorchFormatter

	_register_formatter(TorchFormatter, '''torch''', aliases=['''pt''', '''pytorch'''])
else:
	a__		     =     ValueError('''PyTorch needs to be installed to be able to return PyTorch tensors.''')
	_register_unavailable_formatter(_torch_error, '''torch''', aliases=['''pt''', '''pytorch'''])

if config.TF_AVAILABLE:
	from .tf_formatter import TFFormatter

	_register_formatter(TFFormatter, '''tensorflow''', aliases=['''tf'''])
else:
	a__		     =     ValueError('''Tensorflow needs to be installed to be able to return Tensorflow tensors.''')
	_register_unavailable_formatter(_tf_error, '''tensorflow''', aliases=['''tf'''])

if config.JAX_AVAILABLE:
	from .jax_formatter import JaxFormatter

	_register_formatter(JaxFormatter, '''jax''', aliases=[])
else:
	a__		     =     ValueError('''JAX needs to be installed to be able to return JAX arrays.''')
	_register_unavailable_formatter(_jax_error, '''jax''', aliases=[])





def 				A__ (snake_case   :		Optional[str]				)     ->						Optional[str]:
					if format_type in _FORMAT_TYPES_ALIASES:
										return _FORMAT_TYPES_ALIASES[format_type]
					else:
										return format_type







def 				A__ (snake_case   :		Optional[str]		,	**snake_case   :		List[str]				)     ->						Formatter:
					__UpperCamelCase					:	int             =      get_format_type_from_alias(snake_case				)
					if format_type in _FORMAT_TYPES:
										return _FORMAT_TYPES[format_type](**snake_case				)
					if format_type in _FORMAT_TYPES_ALIASES_UNAVAILABLE:
										raise _FORMAT_TYPES_ALIASES_UNAVAILABLE[format_type]
					else:
										raise ValueError(
										    F'''Return type should be None or selected in {list(type for type in _FORMAT_TYPES.keys() if type != None				)}, but got \'{format_type}\''''				)






