
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     ,A__):
            super().__init__()
            lowercase					=		model
            lowercase					=		2
            lowercase					=		nn.Linear(self.model.config.hidden_size     ,self.num_labels)





       def 				A__   (       self):
            pass



def  UpperCamelCase					(							lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__	):
     '''simple docstring'''
     lowercase					=		LongformerModel.from_pretrained(lowerCamelCase__	)
     lowercase					=		LightningModel(lowerCamelCase__	)

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

     # init longformer question answering model
     lowercase					=		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__":
       lowercase__	: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."
       )
       lowercase__	:Optional[Any]										=					parser.parse_args()
       convert_longformer_qa_checkpoint_to_pytorch(
           args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path
       )






import math
import time
from typing import Dict, List, Optional

from torch.utils.data import Dataset

from transformers import SeqaSeqTrainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
							import torch_xla.core.xla_model as xm
							import torch_xla.debug.metrics as met







class        lowercase	(      SCREAMING_SNAKE_CASE__	):




							def __init__(       self     ,*A__     ,A__=None     ,A__=None     ,**A__):
												super().__init__(*A__     ,**A__)
												lowercase					=		eval_examples
												lowercase					=		post_process_function




							def 				A__   (       self     ,A__ = None     ,A__=None     ,A__ = None     ,A__ = "eval"     ,**A__     ,):
												lowercase					=		gen_kwargs.copy()
												lowercase					=		(
												    gen_kwargs['''max_length'''] if gen_kwargs.get('''max_length''') is not None else self.args.generation_max_length
												)
												lowercase					=		(
												    gen_kwargs['''num_beams'''] if gen_kwargs.get('''num_beams''') is not None else self.args.generation_num_beams
												)
												lowercase					=		gen_kwargs

												lowercase					=		self.eval_dataset if eval_dataset is None else eval_dataset
												lowercase					=		self.get_eval_dataloader(A__)
												lowercase					=		self.eval_examples if eval_examples is None else eval_examples

												# Temporarily disable metric computation, we will do it in the loop here.
												lowercase					=		self.compute_metrics
												lowercase					=		None
												lowercase					=		time.time()
												lowercase					=		self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
												try:
																	lowercase					=		eval_loop(
																	    A__     ,description='''Evaluation'''     ,prediction_loss_only=True if compute_metrics is None else None     ,ignore_keys=A__     ,metric_key_prefix=A__     ,)
												finally:
																	lowercase					=		compute_metrics
												lowercase					=		self.args.eval_batch_size * self.args.world_size
												if f'{metric_key_prefix}_jit_compilation_time' in output.metrics:
																	start_time += output.metrics[f'{metric_key_prefix}_jit_compilation_time']
												output.metrics.update(
												    speed_metrics(
												        A__     ,A__     ,num_samples=output.num_samples     ,num_steps=math.ceil(output.num_samples / total_batch_size)     ,))

												if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
																	# Only the main node write the results by default
																	lowercase					=		self.post_process_function(A__     ,A__     ,A__)
																	lowercase					=		self.compute_metrics(A__)

																	# Prefix all keys with metric_key_prefix + '_'
																	for key in list(metrics.keys()):
																						if not key.startswith(f'{metric_key_prefix}_'):
																											lowercase					=		metrics.pop(A__)

																	metrics.update(output.metrics)
												else:
																	lowercase					=		output.metrics

												if self.args.should_log:
																	# Only the main node log the results by default
																	self.log(A__)

												if self.args.tpu_metrics_debug or self.args.debug:
																	# tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
																	xm.master_print(met.metrics_report())

												lowercase					=		self.callback_handler.on_evaluate(self.args     ,self.state     ,self.control     ,A__)
												return metrics





							def 				A__   (       self     ,A__     ,A__     ,A__=None     ,A__ = "test"     ,**A__):
												lowercase					=		gen_kwargs.copy()

												lowercase					=		self.get_test_dataloader(A__)

												# Temporarily disable metric computation, we will do it in the loop here.
												lowercase					=		self.compute_metrics
												lowercase					=		None
												lowercase					=		time.time()
												lowercase					=		self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
												try:
																	lowercase					=		eval_loop(
																	    A__     ,description='''Prediction'''     ,prediction_loss_only=True if compute_metrics is None else None     ,ignore_keys=A__     ,metric_key_prefix=A__     ,)
												finally:
																	lowercase					=		compute_metrics

												lowercase					=		self.args.eval_batch_size * self.args.world_size
												if f'{metric_key_prefix}_jit_compilation_time' in output.metrics:
																	start_time += output.metrics[f'{metric_key_prefix}_jit_compilation_time']
												output.metrics.update(
												    speed_metrics(
												        A__     ,A__     ,num_samples=output.num_samples     ,num_steps=math.ceil(output.num_samples / total_batch_size)     ,))
												if self.post_process_function is None or self.compute_metrics is None:
																	return output

												lowercase					=		self.post_process_function(A__     ,A__     ,A__     ,'''predict''')
												lowercase					=		self.compute_metrics(A__)

												# Prefix all keys with metric_key_prefix + '_'
												for key in list(metrics.keys()):
																	if not key.startswith(f'{metric_key_prefix}_'):
																						lowercase					=		metrics.pop(A__)
												metrics.update(output.metrics)
												return PredictionOutput(predictions=predictions.predictions     ,label_ids=predictions.label_ids     ,metrics=A__)




