






import argparse

import pytorch_lightning as pl
import torch
from torch import nn

from transformers import LongformerForQuestionAnswering, LongformerModel


class   __SCREAMING_SNAKE_CASE(	pl.LightningModule	):

							'''simple docstring'''





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


							def lowerCAmelCase_ (     self:      Union[str, Any]						)		->		Optional[int]:
											pass


def      a_ (      _A      ,   _A      ,   _A  )			->			str:







				"""simple docstring"""
				snake_case__									=     LongformerModel.from_pretrained(_A  )
				snake_case__									=     LightningModel(_A  )

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

				# init longformer question answering model
				snake_case__									=     LongformerForQuestionAnswering.from_pretrained(_A  )

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

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


if __name__ == "__main__":
							__UpperCamelCase					:		Dict	  =						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."""
							)
							__UpperCamelCase					:		str	  =						parser.parse_args()
							convert_longformer_qa_checkpoint_to_pytorch(
							    args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path
							)












def      a_ (      _A      ,   _A  )			->			str:







				"""simple docstring"""
				if number < 0 or shift_amount < 0:
								raise ValueError('both inputs must be positive integers'  )

				snake_case__									=     str(bin(_A  )  )
				binary_number += "0" * shift_amount
				return binary_number
def      a_ (      _A      ,   _A  )			->			str:







				"""simple docstring"""
				if number < 0 or shift_amount < 0:
								raise ValueError('both inputs must be positive integers'  )

				snake_case__									=     str(bin(_A  )  )[2:]
				if shift_amount >= len(_A  ):
								return "0b0"
				snake_case__									=     binary_number[: len(_A  ) - shift_amount]
				return "0b" + shifted_binary_number
def      a_ (      _A      ,   _A  )			->			str:







				"""simple docstring"""
				if number >= 0:  # Get binary representation of positive number
								snake_case__									=     '0' + str(bin(_A  )  ).strip('-'  )[2:]
				else:  # Get binary (2's complement) representation of negative number
								snake_case__									=     len(bin(_A  )[3:]  )  # Find 2's complement of number
								snake_case__									=     bin(abs(_A  ) - (1 << binary_number_length)  )[3:]
								snake_case__									=     (
								    '1' + '0' * (binary_number_length - len(_A  )) + binary_number
								)

				if shift_amount >= len(_A  ):
								return "0b" + binary_number[0] * len(_A  )
				return (
				    "0b"
				    + binary_number[0] * shift_amount
				    + binary_number[: len(_A  ) - shift_amount]
				)


if __name__ == "__main__":
							import doctest

							doctest.testmod()




