



'''simple docstring'''






import argparse

import torch
from torch import nn

from transformers import PLBartConfig, PLBartForConditionalGeneration, PLBartForSequenceClassification


def 			snake_case_					( _lowerCAmelCase  :						Any  )				->       Union[str, Any]:
							UpperCAmelCase			:	Any										=		[
							    '''encoder.version''',
							    '''decoder.version''',
							    '''model.encoder.version''',
							    '''model.decoder.version''',
							    '''_float_tensor''',
							    '''decoder.output_projection.weight''',
							]
							for k in ignore_keys:
														state_dict.pop(_lowerCAmelCase       ,				_lowerCAmelCase  )


def 			snake_case_					( _lowerCAmelCase  :						Tuple  )				->       Any:
							UpperCAmelCase							,		UpperCAmelCase			:	str										=		emb.weight.shape
							UpperCAmelCase			:	Union[str, Any]										=		nn.Linear(_lowerCAmelCase       ,				_lowerCAmelCase       ,				bias=_lowerCAmelCase  )
							UpperCAmelCase			:	Tuple										=		emb.weight.data
							return lin_layer


def 			snake_case_					( _lowerCAmelCase  :						Dict       ,				_lowerCAmelCase  :						Optional[Any]="uclanlp/plbart-base"       ,				_lowerCAmelCase  :						List[str]=False       ,				_lowerCAmelCase  :						Optional[int]=False  )				->       Tuple:
							UpperCAmelCase			:	Any										=		torch.load(_lowerCAmelCase       ,				map_location='''cpu'''  )['''model''']
							remove_ignore_keys_(_lowerCAmelCase  )
							UpperCAmelCase			:	int										=		state_dict['''encoder.embed_tokens.weight'''].shape[0]

							UpperCAmelCase			:	str										=		PLBartConfig.from_pretrained(_lowerCAmelCase       ,				vocab_size=_lowerCAmelCase  )

							UpperCAmelCase			:	Dict										=		state_dict['''decoder.embed_tokens.weight''']
							if not classification:
														UpperCAmelCase			:	Optional[int]										=		PLBartForConditionalGeneration(_lowerCAmelCase  )
														model.model.load_state_dict(_lowerCAmelCase  )
														if finetuned:
																					UpperCAmelCase			:	Optional[Any]										=		make_linear_from_emb(model.model.shared  )

							else:
														UpperCAmelCase			:	int										=		{}
														for key, value in state_dict.copy().items():
																					if key.startswith('''classification_heads.sentence_classification_head'''  ):
																												UpperCAmelCase			:	List[Any]										=		value
																												state_dict.pop(_lowerCAmelCase  )
														UpperCAmelCase			:	Any										=		PLBartForSequenceClassification(_lowerCAmelCase  )
														model.model.load_state_dict(_lowerCAmelCase  )
														model.classification_head.load_state_dict(_lowerCAmelCase  )

							return model


if __name__ == "__main__":
						UpperCamelCase__:       Tuple							     =      argparse.ArgumentParser()
						# Required parameters
						parser.add_argument("fairseq_path", type=str, help="model.pt on local filesystem.")
						parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
						parser.add_argument(
						    "--hf_config",
						    default="uclanlp/plbart-base",
						    type=str,
						    help="Which huggingface architecture to use: plbart-base",
						)
						parser.add_argument("--finetuned", action="store_true", help="whether the model is a fine-tuned checkpoint")
						parser.add_argument(
						    "--classification", action="store_true", help="whether the model is a classification checkpoint"
						)
						UpperCamelCase__:       List[str]							     =      parser.parse_args()
						UpperCamelCase__:       Union[str, Any]							     =      convert_fairseq_plbart_checkpoint_from_disk(
						    args.fairseq_path,
						    hf_config_path=args.hf_config,
						    finetuned=args.finetuned,
						    classification=args.classification,
						)
						model.save_pretrained(args.pytorch_dump_folder_path)










'''simple docstring'''






import importlib.metadata
import subprocess
import sys


def 			snake_case_					( _lowerCAmelCase  :						bool = False  )				->       str:
							UpperCAmelCase			:	List[str]										=		False
							if "IPython" in sys.modules:
														UpperCAmelCase			:	Optional[Any]										=		'''google.colab''' in str(sys.modules['''IPython'''].get_ipython()  )

							if in_colab:
														if upgrade:
																					UpperCAmelCase			:	List[Any]										=		['''pip''', '''install''', '''-U''', '''torch''']
																					subprocess.run(_lowerCAmelCase       ,				check=_lowerCAmelCase  )
														# get the current version of torch
														UpperCAmelCase			:	Dict										=		importlib.metadata.version('''torch'''  )
														UpperCAmelCase			:	int										=		torch_version[: torch_version.rindex('''.'''  )]
														UpperCAmelCase			:	Any										=		f"""https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-{torch_version_trunc}-cp37-cp37m-linux_x86_64.whl"""
														UpperCAmelCase			:	List[str]										=		['''pip''', '''install''', xla_wheel]
														subprocess.run(_lowerCAmelCase       ,				check=_lowerCAmelCase  )
							else:
														raise RuntimeError('''`install_xla` utility works only on google colab.'''  )





