






import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE     :   Any 							=						logging.get_logger(__name__)

SCREAMING_SNAKE_CASE     :   Tuple 							=						{
    'Salesforce/blip-vqa-base': 'https://huggingface.co/Salesforce/blip-vqa-base/resolve/main/config.json',
    'Salesforce/blip-vqa-capfit-large': (
        'https://huggingface.co/Salesforce/blip-vqa-base-capfit/resolve/main/config.json'
    ),
    'Salesforce/blip-image-captioning-base': (
        'https://huggingface.co/Salesforce/blip-image-captioning-base/resolve/main/config.json'
    ),
    'Salesforce/blip-image-captioning-large': (
        'https://huggingface.co/Salesforce/blip-image-captioning-large/resolve/main/config.json'
    ),
    'Salesforce/blip-itm-base-coco': 'https://huggingface.co/Salesforce/blip-itm-base-coco/resolve/main/config.json',
    'Salesforce/blip-itm-large-coco': 'https://huggingface.co/Salesforce/blip-itm-large-coco/resolve/main/config.json',
    'Salesforce/blip-itm-base-flikr': 'https://huggingface.co/Salesforce/blip-itm-base-flikr/resolve/main/config.json',
    'Salesforce/blip-itm-large-flikr': (
        'https://huggingface.co/Salesforce/blip-itm-large-flikr/resolve/main/config.json'
    ),
}
class 							A_    (						__lowercase			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			Optional[int] =					'blip_text_model'


 def __init__(	self							,  _A=30524							,  _A=768							,  _A=768							,  _A=3072							,  _A=768							,  _A=12							,  _A=8							,  _A=512							,  _A="gelu"							,  _A=1e-12							,  _A=0.0							,  _A=0.0							,  _A=0.02							,  _A=30522							,  _A=2							,  _A=0							,  _A=102							,  _A=True							,  _A=True							,  **_A							,  ) ->		List[Any]:

     """simple docstring"""



     super().__init__(
         pad_token_id=UpperCAmelCase__							,  bos_token_id=UpperCAmelCase__							,  eos_token_id=UpperCAmelCase__							,  sep_token_id=UpperCAmelCase__							,  **UpperCAmelCase__							,  )

     _UpperCAmelCase				:      str			  =				vocab_size
     _UpperCAmelCase				:      List[Any]			  =				hidden_size
     _UpperCAmelCase				:      int			  =				encoder_hidden_size
     _UpperCAmelCase				:      Optional[Any]			  =				intermediate_size
     _UpperCAmelCase				:      Optional[int]			  =				projection_dim
     _UpperCAmelCase				:      str			  =				hidden_dropout_prob
     _UpperCAmelCase				:      Dict			  =				num_hidden_layers
     _UpperCAmelCase				:      Dict			  =				num_attention_heads
     _UpperCAmelCase				:      Tuple			  =				max_position_embeddings
     _UpperCAmelCase				:      Optional[int]			  =				layer_norm_eps
     _UpperCAmelCase				:      Optional[Any]			  =				hidden_act
     _UpperCAmelCase				:      Optional[int]			  =				initializer_range
     _UpperCAmelCase				:      Optional[Any]			  =				attention_probs_dropout_prob
     _UpperCAmelCase				:      List[Any]			  =				is_decoder
     _UpperCAmelCase				:      Tuple			  =				use_cache


 @classmethod
 def  snake_case__				(	cls							,  _A							,  **_A) ->		str:

     """simple docstring"""



     cls._set_token_in_kwargs(UpperCAmelCase__)

     _UpperCAmelCase				:      int			  =				cls.get_config_dict(UpperCAmelCase__							,  **UpperCAmelCase__)

     # get the text config dict if we are loading from BlipConfig
     if config_dict.get('''model_type''') == "blip":
         _UpperCAmelCase				:      Optional[Any]			  =				config_dict['''text_config''']

     if "model_type" in config_dict and hasattr(cls							,  '''model_type''') and config_dict["model_type"] != cls.model_type:
         logger.warning(
             f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
             f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''')

     return cls.from_dict(UpperCAmelCase__							,  **UpperCAmelCase__)



class 							A_    (						__lowercase			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			Tuple =					'blip_vision_model'


 def __init__(	self							,  _A=768							,  _A=3072							,  _A=512							,  _A=12							,  _A=12							,  _A=384							,  _A=16							,  _A="gelu"							,  _A=1e-5							,  _A=0.0							,  _A=1e-10							,  **_A							,  ) ->		str:

     """simple docstring"""



     super().__init__(**UpperCAmelCase__)

     _UpperCAmelCase				:      List[str]			  =				hidden_size
     _UpperCAmelCase				:      Union[str, Any]			  =				intermediate_size
     _UpperCAmelCase				:      Tuple			  =				projection_dim
     _UpperCAmelCase				:      Dict			  =				num_hidden_layers
     _UpperCAmelCase				:      str			  =				num_attention_heads
     _UpperCAmelCase				:      Dict			  =				patch_size
     _UpperCAmelCase				:      Optional[Any]			  =				image_size
     _UpperCAmelCase				:      Union[str, Any]			  =				initializer_range
     _UpperCAmelCase				:      Dict			  =				attention_dropout
     _UpperCAmelCase				:      Union[str, Any]			  =				layer_norm_eps
     _UpperCAmelCase				:      Optional[int]			  =				hidden_act


 @classmethod
 def  snake_case__				(	cls							,  _A							,  **_A) ->		Tuple:

     """simple docstring"""



     cls._set_token_in_kwargs(UpperCAmelCase__)

     _UpperCAmelCase				:      Union[str, Any]			  =				cls.get_config_dict(UpperCAmelCase__							,  **UpperCAmelCase__)

     # get the vision config dict if we are loading from BlipConfig
     if config_dict.get('''model_type''') == "blip":
         _UpperCAmelCase				:      Tuple			  =				config_dict['''vision_config''']

     if "model_type" in config_dict and hasattr(cls							,  '''model_type''') and config_dict["model_type"] != cls.model_type:
         logger.warning(
             f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
             f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''')

     return cls.from_dict(UpperCAmelCase__							,  **UpperCAmelCase__)



class 							A_    (						__lowercase			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			List[str] =					'blip'
 _SCREAMING_SNAKE_CASE							:			Tuple =					True


 def __init__(	self							,  _A=None							,  _A=None							,  _A=512							,  _A=2.6592							,  _A=256							,  **_A							,  ) ->		List[Any]:

     """simple docstring"""



     super().__init__(**UpperCAmelCase__)

     if text_config is None:
         _UpperCAmelCase				:      Union[str, Any]			  =				{}
         logger.info('''`text_config` is `None`. Initializing the `BlipTextConfig` with default values.''')

     if vision_config is None:
         _UpperCAmelCase				:      Tuple			  =				{}
         logger.info('''`vision_config` is `None`. Initializing the `BlipVisionConfig` with default values.''')

     _UpperCAmelCase				:      List[Any]			  =				BlipTextConfig(**UpperCAmelCase__)
     _UpperCAmelCase				:      List[str]			  =				BlipVisionConfig(**UpperCAmelCase__)

     _UpperCAmelCase				:      Dict			  =				self.vision_config.hidden_size

     _UpperCAmelCase				:      Any			  =				projection_dim
     _UpperCAmelCase				:      Optional[int]			  =				logit_scale_init_value
     _UpperCAmelCase				:      List[Any]			  =				1.0
     _UpperCAmelCase				:      int			  =				0.02
     _UpperCAmelCase				:      Optional[Any]			  =				image_text_hidden_size


 @classmethod
 def  snake_case__				(	cls							,  _A							,  _A							,  **_A) ->		Optional[Any]:

     """simple docstring"""



     return cls(text_config=text_config.to_dict()							,  vision_config=vision_config.to_dict()							,  **UpperCAmelCase__)


 def  snake_case__				(	self) ->		int:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[int]			  =				copy.deepcopy(self.__dict__)
     _UpperCAmelCase				:      Tuple			  =				self.text_config.to_dict()
     _UpperCAmelCase				:      Optional[int]			  =				self.vision_config.to_dict()
     _UpperCAmelCase				:      List[str]			  =				self.__class__.model_type
     return output












import argparse

import pytorch_lightning as pl
import torch
from torch import nn

from transformers import LongformerForQuestionAnswering, LongformerModel
class 							A_    (						pl.LightningModule			):






 '''simple docstring'''




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

     """simple docstring"""



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


 def  snake_case__				(	self) ->		Optional[Any]:

     """simple docstring"""



     pass



def      _lowerCamelCase	(						__A    :							str		,		__A    :							str		,		__A    :							str )      ->					List[str]:
    # load longformer model from model identifier
    _UpperCAmelCase				:      Optional[Any]			  =				LongformerModel.from_pretrained(__A )
    _UpperCAmelCase				:      Optional[Any]			  =				LightningModel(__A )

    _UpperCAmelCase				:      Optional[int]			  =				torch.load(__A		,		map_location=torch.device('''cpu''' ) )
    lightning_model.load_state_dict(ckpt['''state_dict'''] )

    # init longformer question answering model
    _UpperCAmelCase				:      int			  =				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__":
  SCREAMING_SNAKE_CASE 							=						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.'
  )
  SCREAMING_SNAKE_CASE 							=						parser.parse_args()
  convert_longformer_qa_checkpoint_to_pytorch(
      args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path
  )




