

import argparse

import pytorch_lightning as pl
import torch
from torch import nn

from transformers import LongformerForQuestionAnswering, LongformerModel



class   lowercase_   (  pl.LightningModule					):



      def __init__(      self				:    List[str]      , A__				:    Union[str, Any]		)   ->					Any:
        super().__init__()
        _snake_case						=							model
        _snake_case						=							2
        _snake_case						=							nn.Linear(self.model.config.hidden_size      , self.num_labels		)







      def 						UpperCamelCase_		(      self				:    Optional[Any]		)   ->					Optional[Any]:
        pass



def 				snake_case_(_UpperCamelCase			,	_UpperCamelCase			,	_UpperCamelCase					)							->     Dict:



  """simple docstring"""






  _snake_case						=							LongformerModel.from_pretrained(_UpperCamelCase					)
  _snake_case						=							LightningModel(_UpperCamelCase					)

  _snake_case						=							torch.load(_UpperCamelCase			,	map_location=torch.device('''cpu'''					)					)
  lightning_model.load_state_dict(ckpt['''state_dict''']					)

  # init longformer question answering model
  _snake_case						=							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
 )




import argparse

import torch

from ...utils import logging
from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert


logging.set_verbosity_info()



def 				snake_case_(_UpperCamelCase			,	_UpperCamelCase			,	_UpperCamelCase					)							->     Optional[Any]:



  """simple docstring"""






  _snake_case						=							AlbertConfig.from_json_file(_UpperCamelCase					)
  print(F"""Building PyTorch model from configuration: {config}"""					)
  _snake_case						=							AlbertForPreTraining(_UpperCamelCase					)

  # Load weights from tf checkpoint
  load_tf_weights_in_albert(_UpperCamelCase			,	_UpperCamelCase			,	_UpperCamelCase					)

  # Save pytorch-model
  print(F"""Save PyTorch model to {pytorch_dump_path}"""					)
  torch.save(model.state_dict()			,	_UpperCamelCase					)


if __name__ == "__main__":
 __A      		=     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(
     '''--albert_config_file''',
     default=None,
     type=str,
     required=True,
     help=(
         '''The config json file corresponding to the pre-trained ALBERT model. \n'''
         '''This specifies the model architecture.'''
     ),
 )
 parser.add_argument(
     '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.'''
 )
 __A      		=     parser.parse_args()
 convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)

