





'''simple docstring'''



import copy
import os
from typing import Union

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


_SCREAMING_SNAKE_CASE					=    logging.get_logger(__name__)

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






class 							__lowercase       (      lowerCAmelCase__						):







	'''simple docstring'''

	a    :				List[Any]											=       "altclip_text_model"


	def __init__(self     ,_lowerCamelCase=250002     ,_lowerCamelCase=1024     ,_lowerCamelCase=24     ,_lowerCamelCase=16     ,_lowerCamelCase=4096     ,_lowerCamelCase="gelu"     ,_lowerCamelCase=0.1     ,_lowerCamelCase=0.1     ,_lowerCamelCase=514     ,_lowerCamelCase=1     ,_lowerCamelCase=0.0_2     ,_lowerCamelCase=0.0_2     ,_lowerCamelCase=1E-0_5     ,_lowerCamelCase=1     ,_lowerCamelCase=0     ,_lowerCamelCase=2     ,_lowerCamelCase="absolute"     ,_lowerCamelCase=True     ,_lowerCamelCase=768     ,**_lowerCamelCase     ,)							->							Any:







				'''simple docstring'''





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

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






class 							__lowercase       (      lowerCAmelCase__						):







	'''simple docstring'''

	a    :				Tuple											=       "altclip_vision_model"


	def __init__(self     ,_lowerCamelCase=768     ,_lowerCamelCase=3072     ,_lowerCamelCase=512     ,_lowerCamelCase=12     ,_lowerCamelCase=12     ,_lowerCamelCase=3     ,_lowerCamelCase=224     ,_lowerCamelCase=32     ,_lowerCamelCase="quick_gelu"     ,_lowerCamelCase=1E-5     ,_lowerCamelCase=0.0     ,_lowerCamelCase=0.0_2     ,_lowerCamelCase=1.0     ,**_lowerCamelCase     ,)							->							str:







				'''simple docstring'''





				super().__init__(**_lowerCamelCase )

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


	@classmethod
	def 						_UpperCAmelCase     (cls     ,_lowerCamelCase     ,**_lowerCamelCase )							->							"PretrainedConfig":







				'''simple docstring'''





				cls._set_token_in_kwargs(_lowerCamelCase )

				__lowercase						,		__lowercase										=							cls.get_config_dict(_lowerCamelCase     ,**_lowerCamelCase )

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







	'''simple docstring'''

	a    :				Optional[Any]											=       "altclip"
	a    :				Union[str, Any]											=       True


	def __init__(self     ,_lowerCamelCase=None     ,_lowerCamelCase=None     ,_lowerCamelCase=768     ,_lowerCamelCase=2.6_5_9_2     ,**_lowerCamelCase )							->							List[Any]:







				'''simple docstring'''





				__lowercase										=							kwargs.pop('''text_config_dict'''     ,_lowerCamelCase )
				__lowercase										=							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:
										__lowercase										=							{}

							# This is the complete result when using `text_config_dict`.
							__lowercase										=							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:
																__lowercase										=							(
																    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:
																__lowercase										=							(
																    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:
										__lowercase										=							{}

							# This is the complete result when using `vision_config_dict`.
							__lowercase										=							AltCLIPVisionConfig(**_lowerCamelCase ).to_dict()
							# convert keys to string instead of integer
							if "id2label" in _vision_config_dict:
										__lowercase										=							{
										    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:
																__lowercase										=							(
																    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:
																__lowercase										=							(
																    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:
							__lowercase										=							{}
							logger.info('''`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values.''' )

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

				__lowercase										=							AltCLIPTextConfig(**_lowerCamelCase )
				__lowercase										=							AltCLIPVisionConfig(**_lowerCamelCase )

				__lowercase										=							projection_dim
				__lowercase										=							logit_scale_init_value
				__lowercase										=							1.0


	@classmethod
	def 						_UpperCAmelCase     (cls     ,_lowerCamelCase     ,_lowerCamelCase     ,**_lowerCamelCase )							->							List[Any]:







				'''simple docstring'''





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


	def 						_UpperCAmelCase     (self )							->							List[Any]:







				'''simple docstring'''





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












'''simple docstring'''



import argparse
from pathlib import Path

import torch

from transformers import OPTConfig, OPTModel
from transformers.utils import logging


logging.set_verbosity_info()
_SCREAMING_SNAKE_CASE					=    logging.get_logger(__name__)




def    _lowerCAmelCase						(				lowerCamelCase_			:  int				):
			__lowercase										=							torch.load(lowerCamelCase_		,       map_location='''cpu'''				)
			if "model" in sd.keys():
						__lowercase										=							torch.load(lowerCamelCase_		,       map_location='''cpu'''				)['''model''']

			# pop unnecessary weights
			__lowercase										=							[
			    '''decoder.version''',
			    '''decoder.output_projection.weight''',
			]
			for key in keys_to_delete:
						if key in sd:
									sd.pop(lowerCamelCase_				)

			__lowercase										=							{
			    '''decoder.project_in_dim.weight''': '''decoder.project_in.weight''',
			    '''decoder.project_out_dim.weight''': '''decoder.project_out.weight''',
			    '''decoder.layer_norm.weight''': '''decoder.final_layer_norm.weight''',
			    '''decoder.layer_norm.bias''': '''decoder.final_layer_norm.bias''',
			}
			for old_key, new_key in keys_to_rename.items():
						if old_key in sd:
									__lowercase										=							sd.pop(lowerCamelCase_				)

			__lowercase										=							list(sd.keys()				)
			for key in keys:
						if ".qkv_proj." in key:
									__lowercase										=							sd[key]
									# We split QKV in separate Q,K,V

									__lowercase										=							key.replace('''.qkv_proj.'''		,       '''.q_proj.'''				)
									__lowercase										=							key.replace('''.qkv_proj.'''		,       '''.k_proj.'''				)
									__lowercase										=							key.replace('''.qkv_proj.'''		,       '''.v_proj.'''				)

									__lowercase										=							value.shape[0]
									assert depth % 3 == 0
									# `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming:
									# https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97
									__lowercase						,		__lowercase						,		__lowercase										=							torch.split(lowerCamelCase_		,       depth // 3		,       dim=0				)

									__lowercase										=							q
									__lowercase										=							k
									__lowercase										=							v
									del sd[key]

			return sd




@torch.no_grad()
def    _lowerCAmelCase						(				lowerCamelCase_			:  Union[str, Any]		,       lowerCamelCase_			:  Tuple		,       lowerCamelCase_			:  Union[str, Any]=None				):
			__lowercase										=							load_checkpoint(lowerCamelCase_				)

			if config is not None:
						__lowercase										=							OPTConfig.from_pretrained(lowerCamelCase_				)
			else:
						__lowercase										=							OPTConfig()

			__lowercase										=							OPTModel(lowerCamelCase_				).half().eval()
			model.load_state_dict(lowerCamelCase_				)

			# Check results
			Path(lowerCamelCase_				).mkdir(exist_ok=lowerCamelCase_				)
			model.save_pretrained(lowerCamelCase_				)


if __name__ == "__main__":
	_SCREAMING_SNAKE_CASE					=    argparse.ArgumentParser()
	# Required parameters
	parser.add_argument(
	    '''--fairseq_path''',
	    type=str,
	    help=(
	        '''path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:'''
	        ''' https://huggingface.co/models?other=opt_metasq'''
	    ),
	)
	parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''')
	parser.add_argument('''--hf_config''', default=None, type=str, help='''Define HF config.''')
	_SCREAMING_SNAKE_CASE					=    parser.parse_args()
	convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)





