

"""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    : Any				 =						{
    '''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    ( A_		,	A_		,	A_		,	A_=None					):
	# Initialise PyTorch model
	lowerCAmelCase__      :		List[str]    							=      XLNetConfig.from_json_file(A_					)

	lowerCAmelCase__      :		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}'					)
		lowerCAmelCase__      :		Union[str, Any]    							=      finetuning_task
		lowerCAmelCase__      :		List[str]    							=      GLUE_TASKS_NUM_LABELS[finetuning_task]
		lowerCAmelCase__      :		int    							=      XLNetForSequenceClassification(A_					)
	elif "squad" in finetuning_task:
		lowerCAmelCase__      :		Union[str, Any]    							=      finetuning_task
		lowerCAmelCase__      :		int    							=      XLNetForQuestionAnswering(A_					)
	else:
		lowerCAmelCase__      :		List[str]    							=      XLNetLMHeadModel(A_					)

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

	# Save pytorch-model
	lowerCAmelCase__      :		Any    							=      os.path.join(A_		,	A_					)
	lowerCAmelCase__      :		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    : Any				 =						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
							)







"""simple docstring"""





from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


__UpperCamelCase    : Tuple				 =						logging.get_logger(__name__)

__UpperCamelCase    : Optional[int]				 =						{
    '''google/bit-50''': '''https://huggingface.co/google/bit-50/resolve/main/config.json''',
}







class 							SCREAMING_SNAKE_CASE   (						a_       ,				a_			):






					"""simple docstring"""



					lowercase__												=			"bit"
					lowercase__												=			["preactivation", "bottleneck"]
					lowercase__												=			["SAME", "VALID"]







					def __init__(			self     :							int  ,lowercase_     :							Optional[int]=3  ,lowercase_     :							int=6_4  ,lowercase_     :							Optional[int]=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8]  ,lowercase_     :							Union[str, Any]=[3, 4, 6, 3]  ,lowercase_     :							List[Any]="preactivation"  ,lowercase_     :							Dict="relu"  ,lowercase_     :							Dict=None  ,lowercase_     :							Optional[Any]=3_2  ,lowercase_     :							Any=0.0  ,lowercase_     :							Optional[Any]=False  ,lowercase_     :							List[Any]=3_2  ,lowercase_     :							Optional[Any]=1  ,lowercase_     :							Optional[int]=None  ,lowercase_     :							int=None  ,**lowercase_     :							List[str]  ,):
						super().__init__(**lowercase_						)
						if layer_type not in self.layer_types:
							raise ValueError(F'layer_type={layer_type} is not one of {",".join(self.layer_types						)}'						)
						if global_padding is not None:
							if global_padding.upper() in self.supported_padding:
								lowerCAmelCase__      :		Optional[Any]    							=      global_padding.upper()
							else:
								raise ValueError(F'Padding strategy {global_padding} not supported'						)
						lowerCAmelCase__      :		int    							=      num_channels
						lowerCAmelCase__      :		int    							=      embedding_size
						lowerCAmelCase__      :		Any    							=      hidden_sizes
						lowerCAmelCase__      :		int    							=      depths
						lowerCAmelCase__      :		Union[str, Any]    							=      layer_type
						lowerCAmelCase__      :		Optional[int]    							=      hidden_act
						lowerCAmelCase__      :		int    							=      global_padding
						lowerCAmelCase__      :		List[Any]    							=      num_groups
						lowerCAmelCase__      :		List[Any]    							=      drop_path_rate
						lowerCAmelCase__      :		Union[str, Any]    							=      embedding_dynamic_padding
						lowerCAmelCase__      :		int    							=      output_stride
						lowerCAmelCase__      :		Optional[Any]    							=      width_factor

						lowerCAmelCase__      :		List[str]    							=      ['''stem'''] + [F'stage{idx}' for idx in range(1  ,len(lowercase_						) + 1						)]
						lowerCAmelCase__							,lowerCAmelCase__      :		Any    							=      get_aligned_output_features_output_indices(
						    out_features=lowercase_  ,out_indices=lowercase_  ,stage_names=self.stage_names						)




