






"""simple docstring"""



import argparse

import pytorch_lightning as pl
import torch
from torch import nn

from transformers import LongformerForQuestionAnswering, LongformerModel






class 							__SCREAMING_SNAKE_CASE						(				pl.LightningModule		):


							def __init__(			self      :						Union[str, Any]			,    snake_case      :						Optional[Any]							):
													'''simple docstring'''





													super().__init__()
													A__						:  Union[str, Any]		  =  model
													A__						:  Any		  =  2
													A__						:  Tuple		  =  nn.Linear(self.model.config.hidden_size			,    self.num_labels							)


							def 						_UpperCamelCase       (			self      :						Tuple							):
													'''simple docstring'''





													pass




def      _lowerCAmelCase		(							UpperCAmelCase__       :					str,						UpperCAmelCase__       :					str,						UpperCAmelCase__       :					str      )		->Union[str, Any]:
						# load longformer model from model identifier
						A__						:  int		  =  LongformerModel.from_pretrained(UpperCAmelCase__      )
						A__						:  Any		  =  LightningModel(UpperCAmelCase__      )

						A__						:  int		  =  torch.load(UpperCAmelCase__,						map_location=torch.device("""cpu"""      )      )
						lightning_model.load_state_dict(ckpt["""state_dict"""]      )

						# init longformer question answering model
						A__						:  Optional[Any]		  =  LongformerForQuestionAnswering.from_pretrained(UpperCAmelCase__      )

						# transfer weights
						longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict()      )
						longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict()      )
						longformer_for_qa.eval()

						# save model
						longformer_for_qa.save_pretrained(UpperCAmelCase__      )

						print(f'Conversion successful. Model saved under {pytorch_dump_folder_path}'      )


if __name__ == "__main__":
						A_    =							argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    '''--longformer_model''',
						    default=None,
						    type=str,
						    required=True,
						    help='''model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.''',
						)
						parser.add_argument(
						    '''--longformer_question_answering_ckpt_path''',
						    default=None,
						    type=str,
						    required=True,
						    help='''Path the official PyTorch Lightning Checkpoint.''',
						)
						parser.add_argument(
						    '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.'''
						)
						A_    =							parser.parse_args()
						convert_longformer_qa_checkpoint_to_pytorch(
						    args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path
						)














"""simple docstring"""



def      _lowerCAmelCase		(							UpperCAmelCase__       :					str,						UpperCAmelCase__       :					List[Any]      )		->List[Any]:
						A__						:  Union[str, Any]		  =  [1]
						for i in range(2,						UpperCAmelCase__      ):
												factorials.append(factorials[-1] * i      )
						assert 0 <= k < factorials[-1] * n, "k out of bounds"

						A__						:  Optional[int]		  =  []
						A__						:  List[str]		  =  list(range(UpperCAmelCase__      )      )

						# Find permutation
						while factorials:
												A__						:  Optional[int]		  =  factorials.pop()
												A__  ,  A__						:  Optional[int]		  =  divmod(UpperCAmelCase__,						UpperCAmelCase__      )
												permutation.append(elements[number]      )
												elements.remove(elements[number]      )
						permutation.append(elements[0]      )

						return permutation


if __name__ == "__main__":
						import doctest

						doctest.testmod()






