



# Copyright 2023 The HuggingFace Inc. 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 copy
import importlib.metadata
import json
import os
from dataclasses import dataclass
from typing import Any, Dict, Union

from packaging import version

from ..utils import is_torch_available, logging


if is_torch_available():
     import torch


a_      :      Optional[int]    =     logging.get_logger(__name__)
@dataclass
class    __UpperCamelCase			:

 """simple docstring"""



 def __init__(	self		,				SCREAMING_SNAKE_CASE=False		,				SCREAMING_SNAKE_CASE=False		,				SCREAMING_SNAKE_CASE=6.0		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=False		,				SCREAMING_SNAKE_CASE=False		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE="fp4"		,				SCREAMING_SNAKE_CASE=False		,				**SCREAMING_SNAKE_CASE		,				) ->			Dict:
  a__												= load_in_abit
  a__												= load_in_abit
  a__												= llm_inta_threshold
  a__												= llm_inta_skip_modules
  a__												= llm_inta_enable_fpaa_cpu_offload
  a__												= llm_inta_has_fpaa_weight
  a__												= bnb_abit_quant_type
  a__												= bnb_abit_use_double_quant

  if bnb_abit_compute_dtype is None:
   a__												= torch.floataa
  elif isinstance(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      ):
   a__												= getattr(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )
  elif isinstance(SCREAMING_SNAKE_CASE		,				torch.dtype      ):
   a__												= bnb_abit_compute_dtype
  else:
   raise ValueError('''bnb_4bit_compute_dtype must be a string or a torch.dtype'''      )

  self.post_init()

 def 						_UpperCAmelCase       (	self      ) ->			Tuple:
  if not isinstance(self.llm_inta_threshold		,				SCREAMING_SNAKE_CASE      ):
   raise ValueError('''llm_int8_threshold must be a float'''      )

  if self.llm_inta_skip_modules is not None and not isinstance(self.llm_inta_skip_modules		,				SCREAMING_SNAKE_CASE      ):
   raise ValueError('''llm_int8_skip_modules must be a list of strings'''      )
  if not isinstance(self.llm_inta_enable_fpaa_cpu_offload		,				SCREAMING_SNAKE_CASE      ):
   raise ValueError('''llm_int8_enable_fp32_cpu_offload must be a boolean'''      )

  if not isinstance(self.llm_inta_has_fpaa_weight		,				SCREAMING_SNAKE_CASE      ):
   raise ValueError('''llm_int8_has_fp16_weight must be a boolean'''      )

  if self.bnb_abit_compute_dtype is not None and not isinstance(self.bnb_abit_compute_dtype		,				torch.dtype      ):
   raise ValueError('''bnb_4bit_compute_dtype must be torch.dtype'''      )

  if not isinstance(self.bnb_abit_quant_type		,				SCREAMING_SNAKE_CASE      ):
   raise ValueError('''bnb_4bit_quant_type must be a string'''      )

  if not isinstance(self.bnb_abit_use_double_quant		,				SCREAMING_SNAKE_CASE      ):
   raise ValueError('''bnb_4bit_use_double_quant must be a boolean'''      )

  if self.load_in_abit and not version.parse(importlib.metadata.version('''bitsandbytes'''      )      ) >= version.parse(
      '''0.39.0'''      ):
   raise ValueError(
       '''4 bit quantization requires bitsandbytes>=0.39.0 - please upgrade your bitsandbytes version'''      )

 def 						_UpperCAmelCase       (	self      ) ->			Tuple:
  return self.load_in_abit or self.load_in_abit

 def 						_UpperCAmelCase       (	self      ) ->			Any:
  if self.load_in_abit:
   return "llm_int8"
  elif self.load_in_abit and self.bnb_abit_quant_type == "fp4":
   return "fp4"
  elif self.load_in_abit and self.bnb_abit_quant_type == "nf4":
   return "nf4"
  else:
   return None

 @classmethod
 def 						_UpperCAmelCase       (	cls		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      ) ->			List[str]:
  a__												= cls(**SCREAMING_SNAKE_CASE      )

  a__												= []
  for key, value in kwargs.items():
   if hasattr(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      ):
    setattr(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )
    to_remove.append(SCREAMING_SNAKE_CASE      )
  for key in to_remove:
   kwargs.pop(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )

  if return_unused_kwargs:
   return config, kwargs
  else:
   return config

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE      ) ->			List[str]:
  with open(SCREAMING_SNAKE_CASE		,				'''w'''		,				encoding='''utf-8'''      ) as writer:
   a__												= self.to_dict()
   a__												= json.dumps(SCREAMING_SNAKE_CASE		,				indent=2		,				sort_keys=SCREAMING_SNAKE_CASE      ) + '''\n'''

   writer.write(SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self      ) ->			Dict[str, Any]:
  a__												= copy.deepcopy(self.__dict__      )
  a__												= str(output['''bnb_4bit_compute_dtype''']      ).split('''.'''      )[1]

  return output

 def __repr__(	self      ) ->			Optional[int]:
  return f"{self.__class__.__name__} {self.to_json_string()}"

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE = True      ) ->			str:
  if use_diff is True:
   a__												= self.to_diff_dict()
  else:
   a__												= self.to_dict()
  return json.dumps(SCREAMING_SNAKE_CASE		,				indent=2		,				sort_keys=SCREAMING_SNAKE_CASE      ) + "\n"



 def 						_UpperCAmelCase       (	self      ) ->			Dict[str, Any]:
  a__												= self.to_dict()

  # get the default config dict
  a__												= BitsAndBytesConfig().to_dict()

  a__												= {}

  # only serialize values that differ from the default config
  for key, value in config_dict.items():
   if value != default_config_dict[key]:
    a__												= value

  return serializable_config_dict










import copy
import os
from typing import TYPE_CHECKING, List, Union


if TYPE_CHECKING:
     pass

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


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

a_      :      Optional[Any]    =     {
    'kakaobrain/align-base': 'https://huggingface.co/kakaobrain/align-base/resolve/main/config.json',
}
class    __UpperCamelCase			(       _lowercase				):

 """simple docstring"""



 _lowercase   :    int      						=					'''align_text_model'''

 def __init__(	self		,				SCREAMING_SNAKE_CASE=3_0_5_2_2		,				SCREAMING_SNAKE_CASE=7_6_8		,				SCREAMING_SNAKE_CASE=1_2		,				SCREAMING_SNAKE_CASE=1_2		,				SCREAMING_SNAKE_CASE=3_0_7_2		,				SCREAMING_SNAKE_CASE="gelu"		,				SCREAMING_SNAKE_CASE=0.1		,				SCREAMING_SNAKE_CASE=0.1		,				SCREAMING_SNAKE_CASE=5_1_2		,				SCREAMING_SNAKE_CASE=2		,				SCREAMING_SNAKE_CASE=0.02		,				SCREAMING_SNAKE_CASE=1e-1_2		,				SCREAMING_SNAKE_CASE=0		,				SCREAMING_SNAKE_CASE="absolute"		,				SCREAMING_SNAKE_CASE=True		,				**SCREAMING_SNAKE_CASE		,				) ->			List[str]:
  super().__init__(**SCREAMING_SNAKE_CASE      )

  a__												= vocab_size
  a__												= hidden_size
  a__												= num_hidden_layers
  a__												= num_attention_heads
  a__												= hidden_act
  a__												= intermediate_size
  a__												= hidden_dropout_prob
  a__												= attention_probs_dropout_prob
  a__												= max_position_embeddings
  a__												= type_vocab_size
  a__												= initializer_range
  a__												= layer_norm_eps
  a__												= position_embedding_type
  a__												= use_cache
  a__												= pad_token_id



 @classmethod
 def 						_UpperCAmelCase       (	cls		,				SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      ) ->			"PretrainedConfig":
  cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE      )

  a__							,			a__												= cls.get_config_dict(SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )

  # get the text config dict if we are loading from AlignConfig
  if config_dict.get('''model_type'''      ) == "align":
   a__												= 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(SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )





class    __UpperCamelCase			(       _lowercase				):

 """simple docstring"""



 _lowercase   :    str      						=					'''align_vision_model'''

 def __init__(	self		,				SCREAMING_SNAKE_CASE = 3		,				SCREAMING_SNAKE_CASE = 6_0_0		,				SCREAMING_SNAKE_CASE = 2.0		,				SCREAMING_SNAKE_CASE = 3.1		,				SCREAMING_SNAKE_CASE = 8		,				SCREAMING_SNAKE_CASE = [3, 3, 5, 3, 5, 5, 3]		,				SCREAMING_SNAKE_CASE = [3_2, 1_6, 2_4, 4_0, 8_0, 1_1_2, 1_9_2]		,				SCREAMING_SNAKE_CASE = [1_6, 2_4, 4_0, 8_0, 1_1_2, 1_9_2, 3_2_0]		,				SCREAMING_SNAKE_CASE = []		,				SCREAMING_SNAKE_CASE = [1, 2, 2, 2, 1, 2, 1]		,				SCREAMING_SNAKE_CASE = [1, 2, 2, 3, 3, 4, 1]		,				SCREAMING_SNAKE_CASE = [1, 6, 6, 6, 6, 6, 6]		,				SCREAMING_SNAKE_CASE = 0.25		,				SCREAMING_SNAKE_CASE = "swish"		,				SCREAMING_SNAKE_CASE = 2_5_6_0		,				SCREAMING_SNAKE_CASE = "mean"		,				SCREAMING_SNAKE_CASE = 0.02		,				SCREAMING_SNAKE_CASE = 0.0_01		,				SCREAMING_SNAKE_CASE = 0.99		,				SCREAMING_SNAKE_CASE = 0.2		,				**SCREAMING_SNAKE_CASE		,				) ->			str:
  super().__init__(**SCREAMING_SNAKE_CASE      )

  a__												= num_channels
  a__												= image_size
  a__												= width_coefficient
  a__												= depth_coefficient
  a__												= depth_divisor
  a__												= kernel_sizes
  a__												= in_channels
  a__												= out_channels
  a__												= depthwise_padding
  a__												= strides
  a__												= num_block_repeats
  a__												= expand_ratios
  a__												= squeeze_expansion_ratio
  a__												= hidden_act
  a__												= hidden_dim
  a__												= pooling_type
  a__												= initializer_range
  a__												= batch_norm_eps
  a__												= batch_norm_momentum
  a__												= drop_connect_rate
  a__												= sum(SCREAMING_SNAKE_CASE      ) * 4



 @classmethod
 def 						_UpperCAmelCase       (	cls		,				SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      ) ->			"PretrainedConfig":
  cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE      )

  a__							,			a__												= cls.get_config_dict(SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )

  # get the vision config dict if we are loading from AlignConfig
  if config_dict.get('''model_type'''      ) == "align":
   a__												= 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(SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )





class    __UpperCamelCase			(       _lowercase				):

 """simple docstring"""



 _lowercase   :    Dict      						=					'''align'''
 _lowercase   :    Optional[Any]      						=					True

 def __init__(	self		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=6_4_0		,				SCREAMING_SNAKE_CASE=1.0		,				SCREAMING_SNAKE_CASE=0.02		,				**SCREAMING_SNAKE_CASE		,				) ->			List[str]:
  super().__init__(**SCREAMING_SNAKE_CASE      )

  if text_config is None:
   a__												= {}
   logger.info('''text_config is None. Initializing the AlignTextConfig with default values.'''      )

  if vision_config is None:
   a__												= {}
   logger.info('''vision_config is None. Initializing the AlignVisionConfig with default values.'''      )

  a__												= AlignTextConfig(**SCREAMING_SNAKE_CASE      )
  a__												= AlignVisionConfig(**SCREAMING_SNAKE_CASE      )

  a__												= projection_dim
  a__												= temperature_init_value
  a__												= initializer_range

 @classmethod
 def 						_UpperCAmelCase       (	cls		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      ) ->			Optional[int]:
  return cls(text_config=text_config.to_dict()		,				vision_config=vision_config.to_dict()		,				**SCREAMING_SNAKE_CASE      )



 def 						_UpperCAmelCase       (	self      ) ->			int:
  a__												= copy.deepcopy(self.__dict__      )
  a__												= self.text_config.to_dict()
  a__												= self.vision_config.to_dict()
  a__												= self.__class__.model_type
  return output





