"""simple docstring"""



import copy
import os
from typing import Union

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


SCREAMING_SNAKE_CASE__:Optional[Any]									=			logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__:Tuple									=			{
    """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_       ):
 _snake_case       :					str							  =						"""altclip_text_model"""


 def __init__( self    ,		lowerCamelCase=250002    ,		lowerCamelCase=1024    ,		lowerCamelCase=24    ,		lowerCamelCase=16    ,		lowerCamelCase=4096    ,		lowerCamelCase="gelu"    ,		lowerCamelCase=0.1    ,		lowerCamelCase=0.1    ,		lowerCamelCase=514    ,		lowerCamelCase=1    ,		lowerCamelCase=0.02    ,		lowerCamelCase=0.02    ,		lowerCamelCase=1E-05    ,		lowerCamelCase=1    ,		lowerCamelCase=0    ,		lowerCamelCase=2    ,		lowerCamelCase="absolute"    ,		lowerCamelCase=True    ,		lowerCamelCase=768    ,		**lowerCamelCase    ,		):
        super().__init__(pad_token_id=lowerCamelCase    ,		bos_token_id=lowerCamelCase    ,		eos_token_id=lowerCamelCase    ,		**lowerCamelCase						)

        __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       =    initializer_factor
        __a       =    layer_norm_eps
        __a       =    position_embedding_type
        __a       =    use_cache
        __a       =    project_dim






class  snake_case__      (       snake_case_       ):
 _snake_case       :					Union[str, Any]							  =						"""altclip_vision_model"""


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

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






 @classmethod
 def 						a__   ( cls    ,		lowerCamelCase    ,		**lowerCamelCase						):
        cls._set_token_in_kwargs(lowerCamelCase						)

        __a ,   __a       =    cls.get_config_dict(lowerCamelCase    ,		**lowerCamelCase						)

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






class  snake_case__      (       snake_case_       ):
 _snake_case       :					Dict							  =						"""altclip"""
 _snake_case       :					Union[str, Any]							  =						True


 def __init__( self    ,		lowerCamelCase=None    ,		lowerCamelCase=None    ,		lowerCamelCase=768    ,		lowerCamelCase=2.6592    ,		**lowerCamelCase						):
        # 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       =    kwargs.pop("text_config_dict"    ,		lowerCamelCase						)
        __a       =    kwargs.pop("vision_config_dict"    ,		lowerCamelCase						)

        super().__init__(**lowerCamelCase						)

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

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

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

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

            # 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       =    {}
               logger.info("`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values."						)

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

        __a       =    AltCLIPTextConfig(**lowerCamelCase						)
        __a       =    AltCLIPVisionConfig(**lowerCamelCase						)

        __a       =    projection_dim
        __a       =    logit_scale_init_value
        __a       =    1.0


 @classmethod
 def 						a__   ( cls    ,		lowerCamelCase    ,		lowerCamelCase    ,		**lowerCamelCase						):
        return cls(text_config=text_config.to_dict()    ,		vision_config=vision_config.to_dict()    ,		**lowerCamelCase						)






 def 						a__   ( self						):
        __a       =    copy.deepcopy(self.__dict__						)
        __a       =    self.text_config.to_dict()
        __a       =    self.vision_config.to_dict()
        __a       =    self.__class__.model_type
        return output

"""simple docstring"""



import unittest

import torch
from torch import nn

from accelerate.test_utils import require_cuda
from accelerate.utils.memory import find_executable_batch_size, release_memory



def 	_lowerCamelCase(	):
       raise RuntimeError("CUDA out of memory."				)






class  snake_case__      (       nn.Module       ):


 def __init__( self						):
        super().__init__()
        __a       =    nn.Linear(3    ,		4						)
        __a       =    nn.BatchNormad(4						)
        __a       =    nn.Linear(4    ,		5						)






 def 						a__   ( self    ,		lowerCamelCase						):
        return self.lineara(self.batchnorm(self.lineara(lowerCamelCase						)						)						)






class  snake_case__      (       unittest.TestCase       ):


 def 						a__   ( self						):
        __a       =    []

        @find_executable_batch_size(starting_batch_size=128						)
        def mock_training_loop_function(lowerCamelCase						):
               nonlocal batch_sizes
               batch_sizes.append(lowerCamelCase						)
               if batch_size != 8:
                      raise_fake_out_of_memory()

        mock_training_loop_function()
        self.assertListEqual(lowerCamelCase    ,		[128, 64, 32, 16, 8]						)


 def 						a__   ( self						):
        __a       =    []

        @find_executable_batch_size(starting_batch_size=128						)
        def mock_training_loop_function(lowerCamelCase    ,		lowerCamelCase						):
               nonlocal batch_sizes
               batch_sizes.append(lowerCamelCase						)
               if batch_size != 8:
                      raise_fake_out_of_memory()
               return batch_size, arga

        __a ,   __a       =    mock_training_loop_function("hello"						)
        self.assertListEqual(lowerCamelCase    ,		[128, 64, 32, 16, 8]						)
        self.assertListEqual([bs, arga]    ,		[8, "hello"]						)


 def 						a__   ( self						):
        @find_executable_batch_size(starting_batch_size=0						)
        def mock_training_loop_function(lowerCamelCase						):
               pass

        with self.assertRaises(lowerCamelCase						) as cm:
               mock_training_loop_function()
               self.assertIn("No executable batch size found, reached zero."    ,		cm.exception.args[0]						)


 def 						a__   ( self						):
        @find_executable_batch_size(starting_batch_size=16						)
        def mock_training_loop_function(lowerCamelCase						):
               if batch_size > 0:
                      raise_fake_out_of_memory()
               pass

        with self.assertRaises(lowerCamelCase						) as cm:
               mock_training_loop_function()
               self.assertIn("No executable batch size found, reached zero."    ,		cm.exception.args[0]						)


 def 						a__   ( self						):
        @find_executable_batch_size(starting_batch_size=128						)
        def mock_training_loop_function(lowerCamelCase    ,		lowerCamelCase    ,		lowerCamelCase						):
               if batch_size != 8:
                      raise raise_fake_out_of_memory()

        with self.assertRaises(lowerCamelCase						) as cm:
               mock_training_loop_function(128    ,		"hello"    ,		"world"						)
               self.assertIn("Batch size was passed into `f`"    ,		cm.exception.args[0]						)
               self.assertIn("`f(arg1='hello', arg2='world')"    ,		cm.exception.args[0]						)


 def 						a__   ( self						):
        @find_executable_batch_size(starting_batch_size=16						)
        def mock_training_loop_function(lowerCamelCase						):
               raise ValueError("Oops, we had an error!"						)

        with self.assertRaises(lowerCamelCase						) as cm:
               mock_training_loop_function()
               self.assertIn("Oops, we had an error!"    ,		cm.exception.args[0]						)






 @require_cuda
 def 						a__   ( self						):
        __a       =    torch.cuda.memory_allocated()
        __a       =    ModelForTest()
        model.cuda()
        self.assertGreater(torch.cuda.memory_allocated()    ,		lowerCamelCase						)
        __a       =    release_memory(lowerCamelCase						)
        self.assertEqual(torch.cuda.memory_allocated()    ,		lowerCamelCase						)
