






'''simple docstring'''
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
	import torch


class        _UpperCAmelCase						(		TensorFormatter[Mapping, 'torch.Tensor', Mapping]			):




				def __init__(			self  :       Union[str, Any]  ,     a  :       int=None  ,     **a  :       str			):






							'''simple docstring'''






							super().__init__(features=a			)
							lowercase_	:    List[str]           =     torch_tensor_kwargs
							import torch  # noqa import torch at initialization




				def 				lowerCAmelCase__				(			self  :       str  ,     a  :       Dict			):






							'''simple docstring'''






							import torch

							if isinstance(a  ,     a			) and column:
										if all(
										    isinstance(a  ,     torch.Tensor			) and x.shape == column[0].shape and x.dtype == column[0].dtype
										    for x in column			):
													return torch.stack(a			)
							return column




				def 				lowerCAmelCase__				(			self  :       Optional[Any]  ,     a  :       Union[str, Any]			):






							'''simple docstring'''






							import torch

							if isinstance(a  ,     (str, bytes, type(a			))			):
										return value
							elif isinstance(a  ,     (np.character, np.ndarray)			) and np.issubdtype(value.dtype  ,     np.character			):
										return value.tolist()

							lowercase_	:    Optional[int]           =     {}

							if isinstance(a  ,     (np.number, np.ndarray)			) and np.issubdtype(value.dtype  ,     np.integer			):
										lowercase_	:    List[Any]           =     {"dtype": torch.intaa}
							elif isinstance(a  ,     (np.number, np.ndarray)			) and np.issubdtype(value.dtype  ,     np.floating			):
										lowercase_	:    List[Any]           =     {"dtype": torch.floataa}
							elif config.PIL_AVAILABLE and "PIL" in sys.modules:
										import PIL.Image

										if isinstance(a  ,     PIL.Image.Image			):
													lowercase_	:    Dict           =     np.asarray(a			)
							return torch.tensor(a  ,     **{**default_dtype, **self.torch_tensor_kwargs}			)




				def 				lowerCAmelCase__				(			self  :       Optional[int]  ,     a  :       Dict			):






							'''simple docstring'''






							import torch

							# support for torch, tf, jax etc.
							if hasattr(a  ,     "__array__"			) and not isinstance(a  ,     torch.Tensor			):
										lowercase_	:    Tuple           =     data_struct.__array__()
							# support for nested types like struct of list of struct
							if isinstance(a  ,     np.ndarray			):
										if data_struct.dtype == object:  # torch tensors cannot be instantied from an array of objects
													return self._consolidate([self.recursive_tensorize(a			) for substruct in data_struct]			)
							elif isinstance(a  ,     (list, tuple)			):
										return self._consolidate([self.recursive_tensorize(a			) for substruct in data_struct]			)
							return self._tensorize(a			)




				def 				lowerCAmelCase__				(			self  :       Optional[int]  ,     a  :       dict			):






							'''simple docstring'''






							return map_nested(self._recursive_tensorize  ,     a  ,     map_list=a			)




				def 				lowerCAmelCase__				(			self  :       Tuple  ,     a  :       pa.Table			):






							'''simple docstring'''






							lowercase_	:    Optional[Any]           =     self.numpy_arrow_extractor().extract_row(a			)
							lowercase_	:    Union[str, Any]           =     self.python_features_decoder.decode_row(a			)
							return self.recursive_tensorize(a			)




				def 				lowerCAmelCase__				(			self  :       str  ,     a  :       pa.Table			):






							'''simple docstring'''






							lowercase_	:    List[str]           =     self.numpy_arrow_extractor().extract_column(a			)
							lowercase_	:    Any           =     self.python_features_decoder.decode_column(a  ,     pa_table.column_names[0]			)
							lowercase_	:    Tuple           =     self.recursive_tensorize(a			)
							lowercase_	:    List[Any]           =     self._consolidate(a			)
							return column





				def 				lowerCAmelCase__				(			self  :       int  ,     a  :       pa.Table			):






							'''simple docstring'''






							lowercase_	:    Any           =     self.numpy_arrow_extractor().extract_batch(a			)
							lowercase_	:    List[Any]           =     self.python_features_decoder.decode_batch(a			)
							lowercase_	:    str           =     self.recursive_tensorize(a			)
							for column_name in batch:
										lowercase_	:    str           =     self._consolidate(batch[column_name]			)
							return batch












'''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


UpperCamelCase__ =			{
    '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					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase=None							):


   """simple docstring"""




   lowercase_	:    Tuple           =     XLNetConfig.from_json_file(_UpperCamelCase							)

   lowercase_	:    Union[str, 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_	:    Dict           =     finetuning_task
      lowercase_	:    Any           =     GLUE_TASKS_NUM_LABELS[finetuning_task]
      lowercase_	:    Any           =     XLNetForSequenceClassification(_UpperCamelCase							)
   elif "squad" in finetuning_task:
      lowercase_	:    Optional[int]           =     finetuning_task
      lowercase_	:    Optional[int]           =     XLNetForQuestionAnswering(_UpperCamelCase							)
   else:
      lowercase_	:    Union[str, Any]           =     XLNetLMHeadModel(_UpperCamelCase							)

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

   # Save pytorch-model
   lowercase_	:    Optional[Any]           =     os.path.join(_UpperCamelCase						,		_UpperCamelCase							)
   lowercase_	:    Dict           =     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__":
 UpperCamelCase__ =			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__ =			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
 )




