




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__  (     _lowercase					,		_lowercase					,		_lowercase					,		_lowercase=None ):

     '''simple docstring'''


     UpperCAmelCase_     :				Dict  =     XLNetConfig.from_json_file(_lowercase )

     UpperCAmelCase_     :				Optional[int]  =     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[int]  =     finetuning_task
          UpperCAmelCase_     :				Optional[Any]  =     GLUE_TASKS_NUM_LABELS[finetuning_task]
          UpperCAmelCase_     :				List[str]  =     XLNetForSequenceClassification(_lowercase )
     elif "squad" in finetuning_task:
          UpperCAmelCase_     :				Dict  =     finetuning_task
          UpperCAmelCase_     :				List[str]  =     XLNetForQuestionAnswering(_lowercase )
     else:
          UpperCAmelCase_     :				List[str]  =     XLNetLMHeadModel(_lowercase )

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

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





from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


if TYPE_CHECKING:
     from ... import FeatureExtractionMixin, TensorType

__a							     =					logging.get_logger(__name__)

__a							     =					{
    'openai/imagegpt-small': '',
    'openai/imagegpt-medium': '',
    'openai/imagegpt-large': '',
}

class     __a(	_a  ):






    """simple docstring"""





    lowerCAmelCase         = '''imagegpt'''
    lowerCAmelCase         = ['''past_key_values''']
    lowerCAmelCase         = {
        '''hidden_size''': '''n_embd''',
        '''max_position_embeddings''': '''n_positions''',
        '''num_attention_heads''': '''n_head''',
        '''num_hidden_layers''': '''n_layer''',
    }





    def __init__( self  ,_SCREAMING_SNAKE_CASE=512 + 1  ,_SCREAMING_SNAKE_CASE=32 * 32  ,_SCREAMING_SNAKE_CASE=512  ,_SCREAMING_SNAKE_CASE=24  ,_SCREAMING_SNAKE_CASE=8  ,_SCREAMING_SNAKE_CASE=None  ,_SCREAMING_SNAKE_CASE="quick_gelu"  ,_SCREAMING_SNAKE_CASE=0.1  ,_SCREAMING_SNAKE_CASE=0.1  ,_SCREAMING_SNAKE_CASE=0.1  ,_SCREAMING_SNAKE_CASE=1e-5  ,_SCREAMING_SNAKE_CASE=0.02  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=False  ,_SCREAMING_SNAKE_CASE=False  ,_SCREAMING_SNAKE_CASE=False  ,**_SCREAMING_SNAKE_CASE  ,)					->						Optional[int]:
         UpperCAmelCase_     :				Optional[int]  =     vocab_size
         UpperCAmelCase_     :				Union[str, Any]  =     n_positions
         UpperCAmelCase_     :				Union[str, Any]  =     n_embd
         UpperCAmelCase_     :				Any  =     n_layer
         UpperCAmelCase_     :				Optional[Any]  =     n_head
         UpperCAmelCase_     :				Union[str, Any]  =     n_inner
         UpperCAmelCase_     :				List[Any]  =     activation_function
         UpperCAmelCase_     :				List[str]  =     resid_pdrop
         UpperCAmelCase_     :				str  =     embd_pdrop
         UpperCAmelCase_     :				Optional[Any]  =     attn_pdrop
         UpperCAmelCase_     :				Dict  =     layer_norm_epsilon
         UpperCAmelCase_     :				Union[str, Any]  =     initializer_range
         UpperCAmelCase_     :				Dict  =     scale_attn_weights
         UpperCAmelCase_     :				Any  =     use_cache
         UpperCAmelCase_     :				List[str]  =     scale_attn_by_inverse_layer_idx
         UpperCAmelCase_     :				Tuple  =     reorder_and_upcast_attn
         UpperCAmelCase_     :				int  =     tie_word_embeddings

         super().__init__(tie_word_embeddings=_SCREAMING_SNAKE_CASE  ,**_SCREAMING_SNAKE_CASE    )


class     __a(	_a  ):






    """simple docstring"""





    @property
    def 				a__ ( self    )					->						Mapping[str, Mapping[int, str]]:
         return OrderedDict(
             [
                 ('''input_ids''', {0: '''batch''', 1: '''sequence'''}),
             ]    )





    def 				a__ ( self  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE = 1  ,_SCREAMING_SNAKE_CASE = -1  ,_SCREAMING_SNAKE_CASE = False  ,_SCREAMING_SNAKE_CASE = None  ,_SCREAMING_SNAKE_CASE = 3  ,_SCREAMING_SNAKE_CASE = 32  ,_SCREAMING_SNAKE_CASE = 32  ,)					->						Mapping[str, Any]:
         UpperCAmelCase_     :				Any  =     self._generate_dummy_images(_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE    )
         UpperCAmelCase_     :				Optional[int]  =     dict(preprocessor(images=_SCREAMING_SNAKE_CASE  ,return_tensors=_SCREAMING_SNAKE_CASE    )    )

         return inputs