






"""simple docstring"""






from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase_     :				int         =				logging.get_logger(__name__)

UpperCAmelCase_     :				Dict         =				{
    """microsoft/trocr-base-handwritten""": (
        """https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json"""
    ),
    # See all TrOCR models at https://huggingface.co/models?filter=trocr
}




class 							lowerCAmelCase__       (    UpperCAmelCase__				):






      '''simple docstring'''

      __UpperCamelCase							=						"trocr"
      __UpperCamelCase							=						["past_key_values"]
      __UpperCamelCase							=						{
          "num_attention_heads": "decoder_attention_heads",
          "hidden_size": "d_model",
          "num_hidden_layers": "decoder_layers",
      }
      def __init__(		self  :						Any		,			lowercase_  :						str=50265		,			lowercase_  :						List[Any]=1024		,			lowercase_  :						List[str]=12		,			lowercase_  :						List[str]=16		,			lowercase_  :						List[str]=4096		,			lowercase_  :						int="gelu"		,			lowercase_  :						Tuple=512		,			lowercase_  :						int=0.1		,			lowercase_  :						Any=0.0		,			lowercase_  :						Dict=0.0		,			lowercase_  :						List[str]=2		,			lowercase_  :						Optional[int]=0.02		,			lowercase_  :						Dict=0.0		,			lowercase_  :						Any=True		,			lowercase_  :						List[str]=False		,			lowercase_  :						Any=True		,			lowercase_  :						Union[str, Any]=True		,			lowercase_  :						int=1		,			lowercase_  :						Union[str, Any]=0		,			lowercase_  :						str=2		,			**lowercase_  :						Tuple		,			):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Dict							=	vocab_size
          SCREAMING_SNAKE_CASE_   :						List[str]							=	d_model
          SCREAMING_SNAKE_CASE_   :						Any							=	decoder_layers
          SCREAMING_SNAKE_CASE_   :						Tuple							=	decoder_attention_heads
          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	decoder_ffn_dim
          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	activation_function
          SCREAMING_SNAKE_CASE_   :						Tuple							=	max_position_embeddings
          SCREAMING_SNAKE_CASE_   :						str							=	dropout
          SCREAMING_SNAKE_CASE_   :						List[Any]							=	attention_dropout
          SCREAMING_SNAKE_CASE_   :						List[str]							=	activation_dropout
          SCREAMING_SNAKE_CASE_   :						Tuple							=	init_std
          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	decoder_layerdrop
          SCREAMING_SNAKE_CASE_   :						str							=	use_cache
          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	scale_embedding
          SCREAMING_SNAKE_CASE_   :						Any							=	use_learned_position_embeddings
          SCREAMING_SNAKE_CASE_   :						str							=	layernorm_embedding

          super().__init__(
              pad_token_id=lowercase_		,			bos_token_id=lowercase_		,			eos_token_id=lowercase_		,			decoder_start_token_id=lowercase_		,			**lowercase_		,			)










"""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_     :				int         =				{
    """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       _A    (__a					,       __a					,       __a					,       __a=None							)				-> int:
    """simple docstring"""






    SCREAMING_SNAKE_CASE_   :						str							=	XLNetConfig.from_json_file(__a							)

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

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

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


if __name__ == "__main__":
  UpperCAmelCase_     :				str         =				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
  )


