



'''simple docstring'''

import argparse

import pytorch_lightning as pl
import torch
from torch import nn

from transformers import LongformerForQuestionAnswering, LongformerModel



class 		lowerCamelCase__      (						pl.LightningModule     ):




					'''simple docstring'''



					def __init__(  self				:	List[str]     ,  UpperCamelCase_				:	List[Any]					)  ->       str:



						'''simple docstring'''





						super().__init__()
						_lowercase					:       int       =		model
						_lowercase					:       Optional[int]       =		2
						_lowercase					:       str       =		nn.Linear(self.model.config.hidden_size     ,  self.num_labels					)




					def 						__UpperCAmelCase     (  self				:	Dict					)  ->       Tuple:



						'''simple docstring'''





						pass







def 						__UpperCamelCase				(							_lowercase,				_lowercase,				_lowercase					) ->			int:
	_lowercase					:       str       =		LongformerModel.from_pretrained(lowerCamelCase__					)
	_lowercase					:       int       =		LightningModel(lowerCamelCase__					)

	_lowercase					:       Dict       =		torch.load(lowerCamelCase__,				map_location=torch.device('cpu'					)					)
	lightning_model.load_state_dict(ckpt['state_dict']					)

	# init longformer question answering model
	_lowercase					:       Tuple       =		LongformerForQuestionAnswering.from_pretrained(lowerCamelCase__					)

	# 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(lowerCamelCase__					)

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


if __name__ == "__main__":
						_A				:    Tuple     						=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				:    List[str]     						=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'''

_A				:    Dict     						='''
# Transformers installation
! pip install transformers datasets
# To install from source instead of the last release, comment the command above and uncomment the following one.
# ! pip install git+https://github.com/huggingface/transformers.git
'''

_A				:    Dict     						=[{'''type''': '''code''', '''content''': INSTALL_CONTENT}]
_A				:    Dict     						={
    '''{processor_class}''': '''FakeProcessorClass''',
    '''{model_class}''': '''FakeModelClass''',
    '''{object_class}''': '''FakeObjectClass''',
}
