"""simple docstring"""






import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


lowerCAmelCase__						:						str		  =			logging.get_logger(__name__)

lowerCAmelCase__						:						str		  =			{
    'Salesforce/instruct-blip-flan-t5': 'https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json',
}
class        snake_case (				__UpperCAmelCase       ):




		"""simple docstring"""

		snake_case__       					=				"instructblip_vision_model"







		def __init__(       self     :			List[str]					,lowerCamelCase__     :			int=1_408					,lowerCamelCase__     :			Union[str, Any]=6_144					,lowerCamelCase__     :			int=39					,lowerCamelCase__     :			Union[str, Any]=16					,lowerCamelCase__     :			Dict=224					,lowerCamelCase__     :			str=14					,lowerCamelCase__     :			Optional[int]="gelu"					,lowerCamelCase__     :			Union[str, Any]=1e-6					,lowerCamelCase__     :			List[str]=0.0					,lowerCamelCase__     :			Optional[int]=1e-10					,lowerCamelCase__     :			Dict=True					,**lowerCamelCase__     :			Dict					,):
						super().__init__(**lowerCamelCase__    )

						UpperCAmelCase__    =							hidden_size
						UpperCAmelCase__    =							intermediate_size
						UpperCAmelCase__    =							num_hidden_layers
						UpperCAmelCase__    =							num_attention_heads
						UpperCAmelCase__    =							patch_size
						UpperCAmelCase__    =							image_size
						UpperCAmelCase__    =							initializer_range
						UpperCAmelCase__    =							attention_dropout
						UpperCAmelCase__    =							layer_norm_eps
						UpperCAmelCase__    =							hidden_act
						UpperCAmelCase__    =							qkv_bias







		@classmethod
		def 	__lowerCAmelCase		(       cls     :			Optional[Any]					,lowerCamelCase__     :			Union[str, os.PathLike]					,**lowerCamelCase__     :			List[Any]    ):
						cls._set_token_in_kwargs(lowerCamelCase__    )

						UpperCAmelCase__   ,			UpperCAmelCase__    =							cls.get_config_dict(lowerCamelCase__					,**lowerCamelCase__    )

						# get the vision config dict if we are loading from InstructBlipConfig
						if config_dict.get('model_type'    ) == "instructblip":
										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        snake_case (				__UpperCAmelCase       ):




		"""simple docstring"""

		snake_case__       					=				"instructblip_qformer"







		def __init__(       self     :			List[str]					,lowerCamelCase__     :			Tuple=30_522					,lowerCamelCase__     :			Union[str, Any]=768					,lowerCamelCase__     :			List[Any]=12					,lowerCamelCase__     :			Tuple=12					,lowerCamelCase__     :			int=3_072					,lowerCamelCase__     :			List[Any]="gelu"					,lowerCamelCase__     :			Dict=0.1					,lowerCamelCase__     :			Optional[Any]=0.1					,lowerCamelCase__     :			str=512					,lowerCamelCase__     :			Tuple=0.0_2					,lowerCamelCase__     :			List[Any]=1e-12					,lowerCamelCase__     :			Tuple=0					,lowerCamelCase__     :			List[str]="absolute"					,lowerCamelCase__     :			Any=2					,lowerCamelCase__     :			int=1_408					,**lowerCamelCase__     :			int					,):
						super().__init__(pad_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__    =							initializer_range
						UpperCAmelCase__    =							layer_norm_eps
						UpperCAmelCase__    =							position_embedding_type
						UpperCAmelCase__    =							cross_attention_frequency
						UpperCAmelCase__    =							encoder_hidden_size







		@classmethod
		def 	__lowerCAmelCase		(       cls     :			Union[str, Any]					,lowerCamelCase__     :			Union[str, os.PathLike]					,**lowerCamelCase__     :			Union[str, Any]    ):
						cls._set_token_in_kwargs(lowerCamelCase__    )

						UpperCAmelCase__   ,			UpperCAmelCase__    =							cls.get_config_dict(lowerCamelCase__					,**lowerCamelCase__    )

						# get the qformer config dict if we are loading from InstructBlipConfig
						if config_dict.get('model_type'    ) == "instructblip":
										UpperCAmelCase__    =							config_dict['qformer_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        snake_case (				__UpperCAmelCase       ):




		"""simple docstring"""

		snake_case__       					=				"instructblip"
		snake_case__       					=				True







		def __init__(       self     :			int					,lowerCamelCase__     :			str=None					,lowerCamelCase__     :			Dict=None					,lowerCamelCase__     :			Tuple=None					,lowerCamelCase__     :			str=32					,**lowerCamelCase__     :			Tuple    ):
						super().__init__(**lowerCamelCase__    )

						if vision_config is None:
										UpperCAmelCase__    =							{}
										logger.info('vision_config is None. initializing the InstructBlipVisionConfig with default values.'    )

						if qformer_config is None:
										UpperCAmelCase__    =							{}
										logger.info('qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.'    )

						if text_config is None:
										UpperCAmelCase__    =							{}
										logger.info('text_config is None. Initializing the text config with default values (`OPTConfig`).'    )

						UpperCAmelCase__    =							InstructBlipVisionConfig(**lowerCamelCase__    )
						UpperCAmelCase__    =							InstructBlipQFormerConfig(**lowerCamelCase__    )
						UpperCAmelCase__    =							text_config['model_type'] if 'model_type' in text_config else 'opt'
						UpperCAmelCase__    =							CONFIG_MAPPING[text_model_type](**lowerCamelCase__    )

						UpperCAmelCase__    =							self.text_config.tie_word_embeddings
						UpperCAmelCase__    =							self.text_config.is_encoder_decoder

						UpperCAmelCase__    =							num_query_tokens
						UpperCAmelCase__    =							self.vision_config.hidden_size
						UpperCAmelCase__    =							self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
						UpperCAmelCase__    =							1.0
						UpperCAmelCase__    =							0.0_2







		@classmethod
		def 	__lowerCAmelCase		(       cls     :			Tuple					,lowerCamelCase__     :			InstructBlipVisionConfig					,lowerCamelCase__     :			InstructBlipQFormerConfig					,lowerCamelCase__     :			PretrainedConfig					,**lowerCamelCase__     :			int					,):
						return cls(
						    vision_config=vision_config.to_dict()					,qformer_config=qformer_config.to_dict()					,text_config=text_config.to_dict()					,**lowerCamelCase__					,)







		def 	__lowerCAmelCase		(       self     :			List[str]    ):
						UpperCAmelCase__    =							copy.deepcopy(self.__dict__    )
						UpperCAmelCase__    =							self.vision_config.to_dict()
						UpperCAmelCase__    =							self.qformer_config.to_dict()
						UpperCAmelCase__    =							self.text_config.to_dict()
						UpperCAmelCase__    =							self.__class__.model_type
						return output




"""simple docstring"""






# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os

from accelerate.utils import ComputeEnvironment

from .cluster import get_cluster_input
from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file  # noqa: F401
from .config_utils import _ask_field, _ask_options, _convert_compute_environment  # noqa: F401
from .sagemaker import get_sagemaker_input


lowerCAmelCase__						:						Dict		  =			'Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine'



def 						a_		(      ):
				UpperCAmelCase__    =							_ask_options(
				    'In which compute environment are you running?'      ,    ['This machine', 'AWS (Amazon SageMaker)']      ,    _convert_compute_environment      ,    )
				if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
								UpperCAmelCase__    =							get_sagemaker_input()
				else:
								UpperCAmelCase__    =							get_cluster_input()
				return config



def 						a_		(      lowerCamelCase=None ):
				if subparsers is not None:
								UpperCAmelCase__    =							subparsers.add_parser('config'      ,    description=lowerCamelCase )
				else:
								UpperCAmelCase__    =							argparse.ArgumentParser('Accelerate config command'      ,    description=lowerCamelCase )

				parser.add_argument(
				    '--config_file'      ,    default=lowerCamelCase      ,    help=(
				        'The path to use to store the config file. Will default to a file named default_config.yaml in the cache '
				        'location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have '
				        'such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed '
				        'with \'huggingface\'.'
				    )      ,    )

				if subparsers is not None:
								parser.set_defaults(func=lowerCamelCase )
				return parser



def 						a_		(      lowerCamelCase ):
				UpperCAmelCase__    =							get_user_input()
				if args.config_file is not None:
								UpperCAmelCase__    =							args.config_file
				else:
								if not os.path.isdir(lowerCamelCase ):
												os.makedirs(lowerCamelCase )
								UpperCAmelCase__    =							default_yaml_config_file

				if config_file.endswith('.json' ):
								config.to_json_file(lowerCamelCase )
				else:
								config.to_yaml_file(lowerCamelCase )
				print(f'''accelerate configuration saved at {config_file}''' )



def 						a_		(      ):
				UpperCAmelCase__    =							config_command_parser()
				UpperCAmelCase__    =							parser.parse_args()
				config_command(lowerCamelCase )


if __name__ == "__main__":
						main()



