






"""simple docstring"""







import copy
import os
from typing import Union

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


lowerCAmelCase_						:     Union[str, Any]							  =   logging.get_logger(__name__)

lowerCAmelCase_						:     List[Any]							  =   {
    'BAAI/AltCLIP': 'https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json',
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}





class 	UpperCamelCase_			(							__SCREAMING_SNAKE_CASE	):
  _A							:       Dict					=						'altclip_text_model'



  def __init__(   self			,		snake_case__=25_00_02			,		snake_case__=10_24			,		snake_case__=24			,		snake_case__=16			,		snake_case__=40_96			,		snake_case__="gelu"			,		snake_case__=0.1			,		snake_case__=0.1			,		snake_case__=5_14			,		snake_case__=1			,		snake_case__=0.02			,		snake_case__=0.02			,		snake_case__=1e-05			,		snake_case__=1			,		snake_case__=0			,		snake_case__=2			,		snake_case__="absolute"			,		snake_case__=True			,		snake_case__=7_68			,		**snake_case__			,		)      ->      Dict:



         """simple docstring"""





         super().__init__(pad_token_id=lowerCAmelCase__			,		bos_token_id=lowerCAmelCase__			,		eos_token_id=lowerCAmelCase__			,		**lowerCAmelCase__					)

         UpperCAmelCase						     =      vocab_size
         UpperCAmelCase						     =      hidden_size
         UpperCAmelCase						     =      num_hidden_layers
         UpperCAmelCase						     =      num_attention_heads
         UpperCAmelCase						     =      hidden_act
         UpperCAmelCase						     =      intermediate_size
         UpperCAmelCase						     =      hidden_dropout_prob
         UpperCAmelCase						     =      attention_probs_dropout_prob
         UpperCAmelCase						     =      max_position_embeddings
         UpperCAmelCase						     =      type_vocab_size
         UpperCAmelCase						     =      initializer_range
         UpperCAmelCase						     =      initializer_factor
         UpperCAmelCase						     =      layer_norm_eps
         UpperCAmelCase						     =      position_embedding_type
         UpperCAmelCase						     =      use_cache
         UpperCAmelCase						     =      project_dim





class 	UpperCamelCase_			(							__SCREAMING_SNAKE_CASE	):
  _A							:       List[Any]					=						'altclip_vision_model'



  def __init__(   self			,		snake_case__=7_68			,		snake_case__=30_72			,		snake_case__=5_12			,		snake_case__=12			,		snake_case__=12			,		snake_case__=3			,		snake_case__=2_24			,		snake_case__=32			,		snake_case__="quick_gelu"			,		snake_case__=1e-5			,		snake_case__=0.0			,		snake_case__=0.02			,		snake_case__=1.0			,		**snake_case__			,		)      ->      Dict:



         """simple docstring"""





         super().__init__(**lowerCAmelCase__					)

         UpperCAmelCase						     =      hidden_size
         UpperCAmelCase						     =      intermediate_size
         UpperCAmelCase						     =      projection_dim
         UpperCAmelCase						     =      num_hidden_layers
         UpperCAmelCase						     =      num_attention_heads
         UpperCAmelCase						     =      num_channels
         UpperCAmelCase						     =      patch_size
         UpperCAmelCase						     =      image_size
         UpperCAmelCase						     =      initializer_range
         UpperCAmelCase						     =      initializer_factor
         UpperCAmelCase						     =      attention_dropout
         UpperCAmelCase						     =      layer_norm_eps
         UpperCAmelCase						     =      hidden_act



  @classmethod
  def 		UpperCamelCase_					(   cls			,		snake_case__			,		**snake_case__					)      ->      List[str]:



         """simple docstring"""





         cls._set_token_in_kwargs(lowerCAmelCase__					)

         UpperCAmelCase						     =      cls.get_config_dict(lowerCAmelCase__			,		**lowerCAmelCase__					)

         # get the vision config dict if we are loading from AltCLIPConfig
         if config_dict.get("""model_type"""					) == "altclip":
                UpperCAmelCase						     =      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(lowerCAmelCase__			,		**lowerCAmelCase__					)





class 	UpperCamelCase_			(							__SCREAMING_SNAKE_CASE	):
  _A							:       Dict					=						'altclip'
  _A							:       Optional[int]					=						True



  def __init__(   self			,		snake_case__=None			,		snake_case__=None			,		snake_case__=7_68			,		snake_case__=2.6_592			,		**snake_case__					)      ->      Tuple:



         """simple docstring"""





         UpperCAmelCase						     =      kwargs.pop("""text_config_dict"""			,		lowerCAmelCase__					)
         UpperCAmelCase						     =      kwargs.pop("""vision_config_dict"""			,		lowerCAmelCase__					)

         super().__init__(**lowerCAmelCase__					)

         # Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in
         # `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most
         # cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`.
         if text_config_dict is not None:
                if text_config is None:
                       UpperCAmelCase						     =      {}

                # This is the complete result when using `text_config_dict`.
                UpperCAmelCase						     =      AltCLIPTextConfig(**lowerCAmelCase__					).to_dict()

                # Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different.
                for key, value in _text_config_dict.items():
                       if key in text_config and value != text_config[key] and key not in ["transformers_version"]:
                              # If specified in `text_config_dict`
                              if key in text_config_dict:
                                     UpperCAmelCase						     =      (
                                         f'''`{key}` is found in both `text_config_dict` and `text_config` but with different values. '''
                                         f'''The value `text_config_dict["{key}"]` will be used instead.'''
                                     )
                              # If inferred from default argument values (just to be super careful)
                              else:
                                     UpperCAmelCase						     =      (
                                         f'''`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The '''
                                         f'''value `text_config["{key}"]` will be overriden.'''
                                     )
                              logger.warning(lowerCAmelCase__					)

            # Update all values in `text_config` with the ones in `_text_config_dict`.
                text_config.update(_text_config_dict					)

         if vision_config_dict is not None:
                if vision_config is None:
                       UpperCAmelCase						     =      {}

                # This is the complete result when using `vision_config_dict`.
                UpperCAmelCase						     =      AltCLIPVisionConfig(**lowerCAmelCase__					).to_dict()
                # convert keys to string instead of integer
                if "id2label" in _vision_config_dict:
                       UpperCAmelCase						     =      {
                           str(lowerCAmelCase__					): value for key, value in _vision_config_dict['id2label'].items()
                       }

                # Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different.
                for key, value in _vision_config_dict.items():
                       if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]:
                              # If specified in `vision_config_dict`
                              if key in vision_config_dict:
                                     UpperCAmelCase						     =      (
                                         f'''`{key}` is found in both `vision_config_dict` and `vision_config` but with different '''
                                         f'''values. The value `vision_config_dict["{key}"]` will be used instead.'''
                                     )
                              # If inferred from default argument values (just to be super careful)
                              else:
                                     UpperCAmelCase						     =      (
                                         f'''`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. '''
                                         f'''The value `vision_config["{key}"]` will be overriden.'''
                                     )
                              logger.warning(lowerCAmelCase__					)

            # Update all values in `vision_config` with the ones in `_vision_config_dict`.
                vision_config.update(_vision_config_dict					)

         if text_config is None:
                UpperCAmelCase						     =      {}
                logger.info("""`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values."""					)

         if vision_config is None:
                UpperCAmelCase						     =      {}
                logger.info("""`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values."""					)

         UpperCAmelCase						     =      AltCLIPTextConfig(**lowerCAmelCase__					)
         UpperCAmelCase						     =      AltCLIPVisionConfig(**lowerCAmelCase__					)

         UpperCAmelCase						     =      projection_dim
         UpperCAmelCase						     =      logit_scale_init_value
         UpperCAmelCase						     =      1.0



  @classmethod
  def 		UpperCamelCase_					(   cls			,		snake_case__			,		snake_case__			,		**snake_case__					)      ->      Union[str, Any]:



         """simple docstring"""





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



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



         """simple docstring"""





         UpperCAmelCase						     =      copy.deepcopy(self.__dict__					)
         UpperCAmelCase						     =      self.text_config.to_dict()
         UpperCAmelCase						     =      self.vision_config.to_dict()
         UpperCAmelCase						     =      self.__class__.model_type
         return output




import argparse

import torch
from torch import nn

from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration






def 						a__			(				A__				):
			SCREAMING_SNAKE_CASE_		:     Any					=					[
			    'encoder.version',
			    'decoder.version',
			    'model.encoder.version',
			    'model.decoder.version',
			    'decoder.output_projection.weight',
			    '_float_tensor',
			    'encoder.embed_positions._float_tensor',
			    'decoder.embed_positions._float_tensor',
			]
			for k in ignore_keys:
						state_dict.pop(A__,      A__				)






def 						a__			(				A__				):
			SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     List[Any]					=					emb.weight.shape
			SCREAMING_SNAKE_CASE_		:     List[Any]					=					nn.Linear(A__,      A__,      bias=A__				)
			SCREAMING_SNAKE_CASE_		:     Dict					=					emb.weight.data
			return lin_layer






def 						a__			(				A__				):
			SCREAMING_SNAKE_CASE_		:     List[Any]					=					torch.load(A__,      map_location='cpu'				)
			SCREAMING_SNAKE_CASE_		:     Tuple					=					mam_aaa['args'] or mam_aaa['cfg']['model']
			SCREAMING_SNAKE_CASE_		:     Any					=					mam_aaa['model']
			remove_ignore_keys_(A__				)
			SCREAMING_SNAKE_CASE_		:     List[str]					=					state_dict['encoder.embed_tokens.weight'].shape[0]

			SCREAMING_SNAKE_CASE_		:     Dict					=					MaMaaaConfig(
			    vocab_size=A__,      max_position_embeddings=1_0_2_4,      encoder_layers=args.encoder_layers,      decoder_layers=args.decoder_layers,      encoder_attention_heads=args.encoder_attention_heads,      decoder_attention_heads=args.decoder_attention_heads,      encoder_ffn_dim=args.encoder_ffn_embed_dim,      decoder_ffn_dim=args.decoder_ffn_embed_dim,      d_model=args.encoder_embed_dim,      encoder_layerdrop=args.encoder_layerdrop,      decoder_layerdrop=args.decoder_layerdrop,      dropout=args.dropout,      attention_dropout=args.attention_dropout,      activation_dropout=args.activation_dropout,      activation_function='relu',      )

			SCREAMING_SNAKE_CASE_		:     int					=					state_dict['decoder.embed_tokens.weight']
			SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					MaMaaaForConditionalGeneration(A__				)
			model.model.load_state_dict(A__,      strict=A__				)
			SCREAMING_SNAKE_CASE_		:     int					=					make_linear_from_emb(model.model.shared				)

			return model


if __name__ == "__main__":
				lowerCAmelCase__			:      Optional[int]								=argparse.ArgumentParser()
				# Required parameters
				parser.add_argument('fairseq_path', type=str, help='path to a model.pt on local filesystem.')
				parser.add_argument('pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
				lowerCAmelCase__			:      Optional[Any]								=parser.parse_args()
				lowerCAmelCase__			:      int								=convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß)
				model.save_pretrained(args.pytorch_dump_folder_path)





