

import copy
import os
from typing import Union

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


UpperCamelCase__		 =     logging.get_logger(__name__)

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

class     __snake_case    (				snake_case__  ):
       """simple docstring"""







       __SCREAMING_SNAKE_CASE				    =							"altclip_text_model"


       def __init__(		self      , _UpperCamelCase=25_00_02      , _UpperCamelCase=10_24      , _UpperCamelCase=24      , _UpperCamelCase=16      , _UpperCamelCase=40_96      , _UpperCamelCase="gelu"      , _UpperCamelCase=0.1      , _UpperCamelCase=0.1      , _UpperCamelCase=5_14      , _UpperCamelCase=1      , _UpperCamelCase=0.02      , _UpperCamelCase=0.02      , _UpperCamelCase=1E-05      , _UpperCamelCase=1      , _UpperCamelCase=0      , _UpperCamelCase=2      , _UpperCamelCase="absolute"      , _UpperCamelCase=True      , _UpperCamelCase=7_68      , **_UpperCamelCase      , )      ->							List[str]:






              """simple docstring"""


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

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






class     __snake_case    (				snake_case__  ):
       """simple docstring"""







       __SCREAMING_SNAKE_CASE				    =							"altclip_vision_model"


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






              """simple docstring"""


              super().__init__(**_UpperCamelCase   )

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






       @classmethod
       def 			a		(		cls      , _UpperCamelCase      , **_UpperCamelCase   )      ->							"PretrainedConfig":






              """simple docstring"""


              cls._set_token_in_kwargs(_UpperCamelCase   )

              __snake_case	,    __snake_case       	=							cls.get_config_dict(_UpperCamelCase      , **_UpperCamelCase   )

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






class     __snake_case    (				snake_case__  ):
       """simple docstring"""







       __SCREAMING_SNAKE_CASE				    =							"altclip"
       __SCREAMING_SNAKE_CASE				    =							True


       def __init__(		self      , _UpperCamelCase=None      , _UpperCamelCase=None      , _UpperCamelCase=7_68      , _UpperCamelCase=2.6592      , **_UpperCamelCase   )      ->							int:






              """simple docstring"""


              __snake_case       	=							kwargs.pop("""text_config_dict"""      , _UpperCamelCase   )
              __snake_case       	=							kwargs.pop("""vision_config_dict"""      , _UpperCamelCase   )

              super().__init__(**_UpperCamelCase   )

              # 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:
                            __snake_case       	=							{}

                     # This is the complete result when using `text_config_dict`.
                     __snake_case       	=							AltCLIPTextConfig(**_UpperCamelCase   ).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:
                                          __snake_case       	=							(
                                              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:
                                          __snake_case       	=							(
                                              F'`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The '
                                              F'value `text_config["{key}"]` will be overriden.'
                                          )
                                   logger.warning(_UpperCamelCase   )

            # 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:
                            __snake_case       	=							{}

                     # This is the complete result when using `vision_config_dict`.
                     __snake_case       	=							AltCLIPVisionConfig(**_UpperCamelCase   ).to_dict()
                     # convert keys to string instead of integer
                     if "id2label" in _vision_config_dict:
                            __snake_case       	=							{
                                str(_UpperCamelCase   ): 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:
                                          __snake_case       	=							(
                                              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:
                                          __snake_case       	=							(
                                              F'`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. '
                                              F'The value `vision_config["{key}"]` will be overriden.'
                                          )
                                   logger.warning(_UpperCamelCase   )

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

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

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

              __snake_case       	=							AltCLIPTextConfig(**_UpperCamelCase   )
              __snake_case       	=							AltCLIPVisionConfig(**_UpperCamelCase   )

              __snake_case       	=							projection_dim
              __snake_case       	=							logit_scale_init_value
              __snake_case       	=							1.0


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






              """simple docstring"""


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






       def 			a		(		self   )      ->							int:






              """simple docstring"""


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




# 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.

from pathlib import Path

import torch

from ...utils import is_npu_available, is_xpu_available
from .config_args import ClusterConfig, default_json_config_file
from .config_utils import SubcommandHelpFormatter


UpperCamelCase__		 =     '''Create a default config file for Accelerate with only a few flags set.'''







def 						lowerCamelCase__      ( __A    :List[Any]="no"	,__A    :str = default_json_config_file	,__A    :bool = False		):


       """simple docstring"""




       __snake_case       	=							Path(__A		)
       path.parent.mkdir(parents=__A	,exist_ok=__A		)
       if path.exists():
              print(
                  F'Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.'		)
              return False
       __snake_case       	=							mixed_precision.lower()
       if mixed_precision not in ["no", "fp16", "bf16", "fp8"]:
              raise ValueError(
                  F'`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}'		)
       __snake_case       	=							{
           """compute_environment""": """LOCAL_MACHINE""",
           """mixed_precision""": mixed_precision,
       }
       if torch.cuda.is_available():
              __snake_case       	=							torch.cuda.device_count()
              __snake_case       	=							num_gpus
              __snake_case       	=							False
              if num_gpus > 1:
                     __snake_case       	=							"""MULTI_GPU"""
              else:
                     __snake_case       	=							"""NO"""
       elif is_xpu_available() and use_xpu:
              __snake_case       	=							torch.xpu.device_count()
              __snake_case       	=							num_xpus
              __snake_case       	=							False
              if num_xpus > 1:
                     __snake_case       	=							"""MULTI_XPU"""
              else:
                     __snake_case       	=							"""NO"""
       elif is_npu_available():
              __snake_case       	=							torch.npu.device_count()
              __snake_case       	=							num_npus
              __snake_case       	=							False
              if num_npus > 1:
                     __snake_case       	=							"""MULTI_NPU"""
              else:
                     __snake_case       	=							"""NO"""
       else:
              __snake_case       	=							0
              __snake_case       	=							True
              __snake_case       	=							1
              __snake_case       	=							"""NO"""
       __snake_case       	=							ClusterConfig(**__A		)
       config.to_json_file(__A		)
       return path







def 						lowerCamelCase__      ( __A    :Dict	,__A    :List[Any]		):


       """simple docstring"""




       __snake_case       	=							parser.add_parser("""default"""	,parents=__A	,help=__A	,formatter_class=__A		)
       parser.add_argument(
           """--config_file"""	,default=__A	,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'."""
           )	,dest="""save_location"""	,)

       parser.add_argument(
           """--mixed_precision"""	,choices=["""no""", """fp16""", """bf16"""]	,type=__A	,help="""Whether or not to use mixed precision training. """
           """Choose between FP16 and BF16 (bfloat16) training. """
           """BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later."""	,default="""no"""	,)
       parser.set_defaults(func=__A		)
       return parser







def 						lowerCamelCase__      ( __A    :Optional[Any]		):


       """simple docstring"""




       __snake_case       	=							write_basic_config(args.mixed_precision	,args.save_location		)
       if config_file:
              print(F'accelerate configuration saved at {config_file}'		)

