




import copy
import os
from typing import Union

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


_snake_case            = logging.get_logger(__name__)

_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							(     UpperCamelCase__	):
     _a											=		"altclip_text_model"

     def __init__(						self	,  _a=25_0002	,  _a=1024	,  _a=24	,  _a=16	,  _a=4096	,  _a="gelu"	,  _a=0.1	,  _a=0.1	,  _a=514	,  _a=1	,  _a=0.02	,  _a=0.02	,  _a=1e-05	,  _a=1	,  _a=0	,  _a=2	,  _a="absolute"	,  _a=True	,  _a=768	,  **_a	,  )		->							Optional[Any]:
          super().__init__(pad_token_id=_a	,  bos_token_id=_a	,  eos_token_id=_a	,  **_a       )

          _A							:  Optional[int]    =		vocab_size
          _A							:  List[str]    =		hidden_size
          _A							:  Optional[int]    =		num_hidden_layers
          _A							:  str    =		num_attention_heads
          _A							:  Dict    =		hidden_act
          _A							:  Optional[Any]    =		intermediate_size
          _A							:  Dict    =		hidden_dropout_prob
          _A							:  Optional[Any]    =		attention_probs_dropout_prob
          _A							:  str    =		max_position_embeddings
          _A							:  Optional[int]    =		type_vocab_size
          _A							:  Optional[int]    =		initializer_range
          _A							:  Any    =		initializer_factor
          _A							:  List[Any]    =		layer_norm_eps
          _A							:  Dict    =		position_embedding_type
          _A							:  int    =		use_cache
          _A							:  Dict    =		project_dim







class 		lowercase							(     UpperCamelCase__	):
     _a											=		"altclip_vision_model"

     def __init__(						self	,  _a=768	,  _a=3072	,  _a=512	,  _a=12	,  _a=12	,  _a=3	,  _a=224	,  _a=32	,  _a="quick_gelu"	,  _a=1e-5	,  _a=0.0	,  _a=0.02	,  _a=1.0	,  **_a	,  )		->							Any:
          super().__init__(**_a       )

          _A							:  Tuple    =		hidden_size
          _A							:  Optional[int]    =		intermediate_size
          _A							:  int    =		projection_dim
          _A							:  Tuple    =		num_hidden_layers
          _A							:  str    =		num_attention_heads
          _A							:  Tuple    =		num_channels
          _A							:  Union[str, Any]    =		patch_size
          _A							:  List[str]    =		image_size
          _A							:  Tuple    =		initializer_range
          _A							:  Optional[int]    =		initializer_factor
          _A							:  str    =		attention_dropout
          _A							:  Dict    =		layer_norm_eps
          _A							:  Dict    =		hidden_act



     @classmethod
     def       a__			(						cls	,  _a	,  **_a       )		->							"PretrainedConfig":
          cls._set_token_in_kwargs(_a       )

          _A							:  int    =		cls.get_config_dict(_a	,  **_a       )

          # get the vision config dict if we are loading from AltCLIPConfig
          if config_dict.get("""model_type"""       ) == "altclip":
               _A							:  str    =		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 		lowercase							(     UpperCamelCase__	):
     _a											=		"altclip"
     _a											=		True

     def __init__(						self	,  _a=None	,  _a=None	,  _a=768	,  _a=2.6592	,  **_a       )		->							int:
          # If `_config_dict` exist, we use them for the backward compatibility.
          # We pop out these 2 attributes before calling `super().__init__` to avoid them being saved (which causes a lot
          # of confusion!).
          _A							:  Union[str, Any]    =		kwargs.pop("""text_config_dict"""	,  _a       )
          _A							:  Optional[int]    =		kwargs.pop("""vision_config_dict"""	,  _a       )

          super().__init__(**_a       )

          # 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:
                    _A							:  Optional[int]    =		{}

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

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

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

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

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

          if vision_config is None:
               _A							:  List[str]    =		{}
               logger.info("""`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values."""       )

          _A							:  Tuple    =		AltCLIPTextConfig(**_a       )
          _A							:  List[Any]    =		AltCLIPVisionConfig(**_a       )

          _A							:  Optional[Any]    =		projection_dim
          _A							:  Tuple    =		logit_scale_init_value
          _A							:  Dict    =		1.0

     @classmethod
     def       a__			(						cls	,  _a	,  _a	,  **_a       )		->							Tuple:
          return cls(text_config=text_config.to_dict()	,  vision_config=vision_config.to_dict()	,  **_a       )



     def       a__			(						self       )		->							Tuple:
          _A							:  List[Any]    =		copy.deepcopy(self.__dict__       )
          _A							:  List[Any]    =		self.text_config.to_dict()
          _A							:  Dict    =		self.vision_config.to_dict()
          _A							:  List[Any]    =		self.__class__.model_type
          return output







import os
import re
import shutil
import sys
import tempfile
import unittest

import black


_snake_case            = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, "utils"))

import check_copies  # noqa: E402


# This is the reference code that will be used in the tests.
# If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated.
_snake_case            = "    def __init__(self, config):\n        super().__init__()\n        self.transform = BertPredictionHeadTransform(config)\n\n        # The output weights are the same as the input embeddings, but there is\n        # an output-only bias for each token.\n        self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False)\n\n        self.bias = nn.Parameter(torch.zeros(config.vocab_size))\n\n        # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings`\n        self.decoder.bias = self.bias\n\n    def forward(self, hidden_states):\n        hidden_states = self.transform(hidden_states)\n        hidden_states = self.decoder(hidden_states)\n        return hidden_states\n"







class 		lowercase							(     unittest.TestCase	):

     def       a__			(						self       )		->							Union[str, Any]:
           _A							:  List[str]    =		tempfile.mkdtemp()
           os.makedirs(os.path.join(self.transformer_dir	,  """models/bert/"""       )       )
           _A							:  str    =		self.transformer_dir
           shutil.copy(
               os.path.join(_a	,  """src/transformers/models/bert/modeling_bert.py"""       )	,  os.path.join(self.transformer_dir	,  """models/bert/modeling_bert.py"""       )	,  )

     def       a__			(						self       )		->							Optional[int]:
           _A							:  List[str]    =		"""src/transformers"""
           shutil.rmtree(self.transformer_dir       )

     def       a__			(						self	,  _a	,  _a	,  _a	,  _a=None       )		->							Optional[Any]:
           _A							:  Optional[Any]    =		comment + F'''\nclass {class_name}(nn.Module):\n''' + class_code
           if overwrite_result is not None:
                 _A							:  List[str]    =		comment + F'''\nclass {class_name}(nn.Module):\n''' + overwrite_result
           _A							:  List[str]    =		black.Mode(target_versions={black.TargetVersion.PYaa}	,  line_length=119       )
           _A							:  Optional[int]    =		black.format_str(_a	,  mode=_a       )
           _A							:  Optional[Any]    =		os.path.join(self.transformer_dir	,  """new_code.py"""       )
           with open(_a	,  """w"""	,  newline="""\n"""       ) as f:
                 f.write(_a       )
           if overwrite_result is None:
                 self.assertTrue(len(check_copies.is_copy_consistent(_a       )       ) == 0       )
           else:
                 check_copies.is_copy_consistent(f.name	,  overwrite=_a       )
                 with open(_a	,  """r"""       ) as f:
                       self.assertTrue(f.read()	,  _a       )

     def       a__			(						self       )		->							str:
           _A							:  Union[str, Any]    =		check_copies.find_code_in_transformers("""models.bert.modeling_bert.BertLMPredictionHead"""       )
           self.assertEqual(_a	,  _a       )

     def       a__			(						self       )		->							int:
           # Base copy consistency
           self.check_copy_consistency(
               """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead"""	,  """BertLMPredictionHead"""	,  REFERENCE_CODE + """\n"""	,  )

           # With no empty line at the end
           self.check_copy_consistency(
               """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead"""	,  """BertLMPredictionHead"""	,  _a	,  )

           # Copy consistency with rename
           self.check_copy_consistency(
               """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel"""	,  """TestModelLMPredictionHead"""	,  re.sub("""Bert"""	,  """TestModel"""	,  _a       )	,  )

           # Copy consistency with a really long name
           _A							:  List[str]    =		"""TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason"""
           self.check_copy_consistency(
               F'''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}'''	,  F'''{long_class_name}LMPredictionHead'''	,  re.sub("""Bert"""	,  _a	,  _a       )	,  )

           # Copy consistency with overwrite
           self.check_copy_consistency(
               """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel"""	,  """TestModelLMPredictionHead"""	,  _a	,  overwrite_result=re.sub("""Bert"""	,  """TestModel"""	,  _a       )	,  )



     def       a__			(						self       )		->							Tuple:
           _A							:  Union[str, Any]    =		check_copies.LOCALIZED_READMES["""README_zh-hans.md"""]

           _A							:  str    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the"""
               """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for"""
               """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong"""
               """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1."""
               """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),"""
               """ released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and"""
               """ lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same"""
               """ method has been applied to compress GPT2 into"""
               """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into"""
               """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),"""
               """ Multilingual BERT into"""
               """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German"""
               """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**"""
               """ (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders"""
               """ as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang"""
               """ Luong, Quoc V. Le, Christopher D. Manning."""
           )
           _A							:  str    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the"""
               """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of"""
               """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian"""
               """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n"""
           )
           _A							:  Any    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the"""
               """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of"""
               """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian"""
               """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1."""
               """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文"""
               """ [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and"""
               """ lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same"""
               """ method has been applied to compress GPT2 into"""
               """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into"""
               """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),"""
               """ Multilingual BERT into"""
               """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German"""
               """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自"""
               """ Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather"""
               """ than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,"""
               """ Christopher D. Manning 发布。\n"""
           )

           _A    ,     _A							:  Tuple    =		check_copies.convert_to_localized_md(
               _a	,  _a	,  localized_readme["""format_model_list"""]       )

           self.assertFalse(_a       )
           self.assertEqual(_a	,  _a       )

           _A    ,     _A							:  List[str]    =		check_copies.convert_to_localized_md(
               _a	,  _a	,  localized_readme["""format_model_list"""]       )

           # Check whether the number of models is equal to README.md after conversion.
           self.assertTrue(_a       )

           _A							:  Tuple    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the"""
               """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for"""
               """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong"""
               """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut."""
           )
           _A							:  Dict    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and"""
               """ the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of"""
               """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian"""
               """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n"""
           )
           _A							:  Optional[Any]    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the"""
               """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of"""
               """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian"""
               """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n"""
           )

           _A    ,     _A							:  Optional[int]    =		check_copies.convert_to_localized_md(
               _a	,  _a	,  localized_readme["""format_model_list"""]       )

           # Check if the model link is synchronized.
           self.assertEqual(_a	,  _a       )

