




'''simple docstring'''

# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from pathlib import Path

import torch

from ...utils import is_npu_available, is_xpu_available
from .config_args import ClusterConfig, default_json_config_file
from .config_utils import SubcommandHelpFormatter


a       :						List[str]								=		'''Create a default config file for Accelerate with only a few flags set.'''


def __UpperCAmelCase	(   _UpperCAmelCase		:   Any="no"		,				_UpperCAmelCase		:   str = default_json_config_file		,				_UpperCAmelCase		:   bool = False						)      ->      Tuple:
			__snake_case			=		Path(_UpperCAmelCase						)
			path.parent.mkdir(parents=_UpperCAmelCase		,				exist_ok=_UpperCAmelCase						)
			if path.exists():
						print(
						    F'''Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.'''						)
						return False
			__snake_case			=		mixed_precision.lower()
			if mixed_precision not in ["no", "fp16", "bf16", "fp8"]:
						raise ValueError(
						    F'''`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}'''						)
			__snake_case			=		{
			    "compute_environment": "LOCAL_MACHINE",
			    "mixed_precision": mixed_precision,
			}
			if torch.cuda.is_available():
						__snake_case			=		torch.cuda.device_count()
						__snake_case			=		num_gpus
						__snake_case			=		False
						if num_gpus > 1:
									__snake_case			=		"MULTI_GPU"
						else:
									__snake_case			=		"NO"
			elif is_xpu_available() and use_xpu:
						__snake_case			=		torch.xpu.device_count()
						__snake_case			=		num_xpus
						__snake_case			=		False
						if num_xpus > 1:
									__snake_case			=		"MULTI_XPU"
						else:
									__snake_case			=		"NO"
			elif is_npu_available():
						__snake_case			=		torch.npu.device_count()
						__snake_case			=		num_npus
						__snake_case			=		False
						if num_npus > 1:
									__snake_case			=		"MULTI_NPU"
						else:
									__snake_case			=		"NO"
			else:
						__snake_case			=		0
						__snake_case			=		True
						__snake_case			=		1
						__snake_case			=		"NO"
			__snake_case			=		ClusterConfig(**_UpperCAmelCase						)
			config.to_json_file(_UpperCAmelCase						)
			return path


def __UpperCAmelCase	(   _UpperCAmelCase		:   Optional[int]		,				_UpperCAmelCase		:   List[str]						)      ->      List[Any]:
			__snake_case			=		parser.add_parser("default"		,				parents=_UpperCAmelCase		,				help=_UpperCAmelCase		,				formatter_class=_UpperCAmelCase						)
			parser.add_argument(
			    "--config_file"		,				default=_UpperCAmelCase		,				help=(
			        "The path to use to store the config file. Will default to a file named default_config.yaml in the cache "
			        "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have "
			        "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed "
			        "with 'huggingface'."
			    )		,				dest="save_location"		,				)

			parser.add_argument(
			    "--mixed_precision"		,				choices=["no", "fp16", "bf16"]		,				type=_UpperCAmelCase		,				help="Whether or not to use mixed precision training. "
			    "Choose between FP16 and BF16 (bfloat16) training. "
			    "BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later."		,				default="no"		,				)
			parser.set_defaults(func=_UpperCAmelCase						)
			return parser






def __UpperCAmelCase	(   _UpperCAmelCase		:   List[Any]						)      ->      List[str]:
			__snake_case			=		write_basic_config(args.mixed_precision		,				args.save_location						)
			if config_file:
						print(F'''accelerate configuration saved at {config_file}'''						)







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


lowercase__											={
    '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      UpperCamelCase_				(      A__  ,      A__  ,      A__  ,      A__=None						):
	# Initialise PyTorch model
	a_      				=						XLNetConfig.from_json_file(A__						)

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

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

	# Save pytorch-model
	a_      				=						os.path.join(A__  ,      A__						)
	a_      				=						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__":
		lowercase__											=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',
		)
		lowercase__											=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
		)
