





import copy
import os
from typing import Union

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


_lowerCamelCase   				=			logging.get_logger(__name__)

_lowerCamelCase   				=			{
    '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   UpperCamelCase_       (     UpperCamelCase__    ):
							lowerCamelCase_           =					"blip_text_model"


							def __init__(							self    :Union[str, Any]							,				__A    :int=3_0524							,				__A    :List[Any]=768							,				__A    :Optional[int]=768							,				__A    :str=3072							,				__A    :Tuple=768							,				__A    :Any=12							,				__A    :Dict=8							,				__A    :Union[str, Any]=512							,				__A    :int="gelu"							,				__A    :int=1E-12							,				__A    :Tuple=0.0							,				__A    :Optional[int]=0.0							,				__A    :Optional[int]=0.0_2							,				__A    :Union[str, Any]=3_0522							,				__A    :List[Any]=2							,				__A    :str=0							,				__A    :int=102							,				__A    :Optional[int]=True							,				__A    :List[Any]=True							,				**__A    :Optional[int]							,				)   ->   Optional[int]:



									"""simple docstring"""



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

									SCREAMING_SNAKE_CASE__			=      vocab_size
									SCREAMING_SNAKE_CASE__			=      hidden_size
									SCREAMING_SNAKE_CASE__			=      encoder_hidden_size
									SCREAMING_SNAKE_CASE__			=      intermediate_size
									SCREAMING_SNAKE_CASE__			=      projection_dim
									SCREAMING_SNAKE_CASE__			=      hidden_dropout_prob
									SCREAMING_SNAKE_CASE__			=      num_hidden_layers
									SCREAMING_SNAKE_CASE__			=      num_attention_heads
									SCREAMING_SNAKE_CASE__			=      max_position_embeddings
									SCREAMING_SNAKE_CASE__			=      layer_norm_eps
									SCREAMING_SNAKE_CASE__			=      hidden_act
									SCREAMING_SNAKE_CASE__			=      initializer_range
									SCREAMING_SNAKE_CASE__			=      attention_probs_dropout_prob
									SCREAMING_SNAKE_CASE__			=      is_decoder
									SCREAMING_SNAKE_CASE__			=      use_cache


							@classmethod
							def      _snake_case  (							cls    :Union[str, Any]							,				__A    :Union[str, os.PathLike]							,				**__A    :Union[str, Any]		)   ->   "PretrainedConfig":



									"""simple docstring"""



									cls._set_token_in_kwargs(__A		)

									SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      cls.get_config_dict(__A							,				**__A		)

									# get the text config dict if we are loading from BlipConfig
									if config_dict.get("""model_type"""		) == "blip":
											SCREAMING_SNAKE_CASE__			=      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(__A							,				**__A		)




class   UpperCamelCase_       (     UpperCamelCase__    ):
							lowerCamelCase_           =					"blip_vision_model"


							def __init__(							self    :str							,				__A    :List[str]=768							,				__A    :Any=3072							,				__A    :str=512							,				__A    :int=12							,				__A    :List[str]=12							,				__A    :Any=384							,				__A    :Optional[Any]=16							,				__A    :Union[str, Any]="gelu"							,				__A    :List[str]=1E-5							,				__A    :Any=0.0							,				__A    :Any=1E-10							,				**__A    :Optional[Any]							,				)   ->   Optional[int]:



									"""simple docstring"""



									super().__init__(**__A		)

									SCREAMING_SNAKE_CASE__			=      hidden_size
									SCREAMING_SNAKE_CASE__			=      intermediate_size
									SCREAMING_SNAKE_CASE__			=      projection_dim
									SCREAMING_SNAKE_CASE__			=      num_hidden_layers
									SCREAMING_SNAKE_CASE__			=      num_attention_heads
									SCREAMING_SNAKE_CASE__			=      patch_size
									SCREAMING_SNAKE_CASE__			=      image_size
									SCREAMING_SNAKE_CASE__			=      initializer_range
									SCREAMING_SNAKE_CASE__			=      attention_dropout
									SCREAMING_SNAKE_CASE__			=      layer_norm_eps
									SCREAMING_SNAKE_CASE__			=      hidden_act


							@classmethod
							def      _snake_case  (							cls    :Optional[int]							,				__A    :Union[str, os.PathLike]							,				**__A    :Union[str, Any]		)   ->   "PretrainedConfig":



									"""simple docstring"""



									cls._set_token_in_kwargs(__A		)

									SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      cls.get_config_dict(__A							,				**__A		)

									# get the vision config dict if we are loading from BlipConfig
									if config_dict.get("""model_type"""		) == "blip":
											SCREAMING_SNAKE_CASE__			=      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(__A							,				**__A		)




class   UpperCamelCase_       (     UpperCamelCase__    ):
							lowerCamelCase_           =					"blip"
							lowerCamelCase_           =					True


							def __init__(							self    :Tuple							,				__A    :List[str]=None							,				__A    :Any=None							,				__A    :Tuple=512							,				__A    :List[Any]=2.6_5_9_2							,				__A    :Any=256							,				**__A    :List[str]							,				)   ->   List[str]:



									"""simple docstring"""



									super().__init__(**__A		)

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

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

									SCREAMING_SNAKE_CASE__			=      BlipTextConfig(**__A		)
									SCREAMING_SNAKE_CASE__			=      BlipVisionConfig(**__A		)

									SCREAMING_SNAKE_CASE__			=      self.vision_config.hidden_size

									SCREAMING_SNAKE_CASE__			=      projection_dim
									SCREAMING_SNAKE_CASE__			=      logit_scale_init_value
									SCREAMING_SNAKE_CASE__			=      1.0
									SCREAMING_SNAKE_CASE__			=      0.0_2
									SCREAMING_SNAKE_CASE__			=      image_text_hidden_size


							@classmethod
							def      _snake_case  (							cls    :int							,				__A    :BlipTextConfig							,				__A    :BlipVisionConfig							,				**__A    :Any		)   ->   List[Any]:



									"""simple docstring"""



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


							def      _snake_case  (							self    :List[str]		)   ->   int:



									"""simple docstring"""



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






import argparse
import os
import pickle
import sys

import torch

from transformers import TransfoXLConfig, TransfoXLLMHeadModel, load_tf_weights_in_transfo_xl
from transformers.models.transfo_xl import tokenization_transfo_xl as data_utils
from transformers.models.transfo_xl.tokenization_transfo_xl import CORPUS_NAME, VOCAB_FILES_NAMES
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()

# We do this to be able to load python 2 datasets pickles
# See e.g. https://stackoverflow.com/questions/2121874/python-pickling-after-changing-a-modules-directory/2121918#2121918
_lowerCamelCase   				=			data_utils.TransfoXLTokenizer
_lowerCamelCase   				=			data_utils.TransfoXLCorpus
_lowerCamelCase   				=			data_utils
_lowerCamelCase   				=			data_utils






def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							Dict					,   UpperCamelCase__:							Any					,   UpperCamelCase__:							Any					,   UpperCamelCase__:							Tuple    ):
		if transfo_xl_dataset_file:
				# Convert a pre-processed corpus (see original TensorFlow repo)
				with open(UpperCamelCase__					,   """rb"""    ) as fp:
						SCREAMING_SNAKE_CASE__			=      pickle.load(UpperCamelCase__					,   encoding="""latin1"""    )
				# Save vocabulary and dataset cache as Dictionaries (should be better than pickles for the long-term)
				SCREAMING_SNAKE_CASE__			=      pytorch_dump_folder_path + """/""" + VOCAB_FILES_NAMES["""pretrained_vocab_file"""]
				print(f'''Save vocabulary to {pytorch_vocab_dump_path}'''    )
				SCREAMING_SNAKE_CASE__			=      corpus.vocab.__dict__
				torch.save(UpperCamelCase__					,   UpperCamelCase__    )

				SCREAMING_SNAKE_CASE__			=      corpus.__dict__
				corpus_dict_no_vocab.pop("""vocab"""					,   UpperCamelCase__    )
				SCREAMING_SNAKE_CASE__			=      pytorch_dump_folder_path + """/""" + CORPUS_NAME
				print(f'''Save dataset to {pytorch_dataset_dump_path}'''    )
				torch.save(UpperCamelCase__					,   UpperCamelCase__    )

		if tf_checkpoint_path:
				# Convert a pre-trained TensorFlow model
				SCREAMING_SNAKE_CASE__			=      os.path.abspath(UpperCamelCase__    )
				SCREAMING_SNAKE_CASE__			=      os.path.abspath(UpperCamelCase__    )

				print(f'''Converting Transformer XL checkpoint from {tf_path} with config at {config_path}.'''    )
				# Initialise PyTorch model
				if transfo_xl_config_file == "":
						SCREAMING_SNAKE_CASE__			=      TransfoXLConfig()
				else:
						SCREAMING_SNAKE_CASE__			=      TransfoXLConfig.from_json_file(UpperCamelCase__    )
				print(f'''Building PyTorch model from configuration: {config}'''    )
				SCREAMING_SNAKE_CASE__			=      TransfoXLLMHeadModel(UpperCamelCase__    )

				SCREAMING_SNAKE_CASE__			=      load_tf_weights_in_transfo_xl(UpperCamelCase__					,   UpperCamelCase__					,   UpperCamelCase__    )
				# Save pytorch-model
				SCREAMING_SNAKE_CASE__			=      os.path.join(UpperCamelCase__					,   UpperCamelCase__    )
				SCREAMING_SNAKE_CASE__			=      os.path.join(UpperCamelCase__					,   UpperCamelCase__    )
				print(f'''Save PyTorch model to {os.path.abspath(UpperCamelCase__    )}'''    )
				torch.save(model.state_dict()					,   UpperCamelCase__    )
				print(f'''Save configuration file to {os.path.abspath(UpperCamelCase__    )}'''    )
				with open(UpperCamelCase__					,   """w"""					,   encoding="""utf-8"""    ) as f:
						f.write(config.to_json_string()    )


if __name__ == "__main__":
	_lowerCamelCase   				=			argparse.ArgumentParser()
	parser.add_argument(
	    '--pytorch_dump_folder_path',
	    default=None,
	    type=str,
	    required=True,
	    help='Path to the folder to store the PyTorch model or dataset/vocab.',
	)
	parser.add_argument(
	    '--tf_checkpoint_path',
	    default='',
	    type=str,
	    help='An optional path to a TensorFlow checkpoint path to be converted.',
	)
	parser.add_argument(
	    '--transfo_xl_config_file',
	    default='',
	    type=str,
	    help=(
	        'An optional config json file corresponding to the pre-trained BERT model. \n'
	        'This specifies the model architecture.'
	    ),
	)
	parser.add_argument(
	    '--transfo_xl_dataset_file',
	    default='',
	    type=str,
	    help='An optional dataset file to be converted in a vocabulary.',
	)
	_lowerCamelCase   				=			parser.parse_args()
	convert_transfo_xl_checkpoint_to_pytorch(
	    args.tf_checkpoint_path,
	    args.transfo_xl_config_file,
	    args.pytorch_dump_folder_path,
	    args.transfo_xl_dataset_file,
	)