

import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel

from diffusers import DDIMScheduler, LDMPipeline, UNetaDModel, VQModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()
class  _A		(  unittest.TestCase):

  @property
  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        torch.manual_seed(0					)
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     UNetaDModel(
            block_out_channels=(32, 64) ,					layers_per_block=2 ,					sample_size=32 ,					in_channels=3 ,					out_channels=3 ,					down_block_types=('DownBlock2D', 'AttnDownBlock2D') ,					up_block_types=('AttnUpBlock2D', 'UpBlock2D') ,					)
        return model

  @property
  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        torch.manual_seed(0					)
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     VQModel(
            block_out_channels=[32, 64] ,					in_channels=3 ,					out_channels=3 ,					down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] ,					up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] ,					latent_channels=3 ,					)
        return model

  @property
  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        torch.manual_seed(0					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     CLIPTextConfig(
            bos_token_id=0 ,					eos_token_id=2 ,					hidden_size=32 ,					intermediate_size=37 ,					layer_norm_eps=1e-05 ,					num_attention_heads=4 ,					num_hidden_layers=5 ,					pad_token_id=1 ,					vocab_size=1000 ,					)
        return CLIPTextModel(_SCREAMING_SNAKE_CASE					)



  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Dict           =     self.dummy_uncond_unet
        SCREAMING_SNAKE_CASE_				:  str           =     DDIMScheduler()
        SCREAMING_SNAKE_CASE_				:  List[str]           =     self.dummy_vq_model

        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     LDMPipeline(unet=_SCREAMING_SNAKE_CASE ,					vqvae=_SCREAMING_SNAKE_CASE ,					scheduler=_SCREAMING_SNAKE_CASE					)
        ldm.to(_SCREAMING_SNAKE_CASE					)
        ldm.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE					)

        SCREAMING_SNAKE_CASE_				:  int           =     torch.manual_seed(0					)
        SCREAMING_SNAKE_CASE_				:  List[str]           =     ldm(generator=_SCREAMING_SNAKE_CASE ,					num_inference_steps=2 ,					output_type='numpy'					).images

        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     torch.manual_seed(0					)
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     ldm(generator=_SCREAMING_SNAKE_CASE ,					num_inference_steps=2 ,					output_type='numpy' ,					return_dict=_SCREAMING_SNAKE_CASE					)[0]

        SCREAMING_SNAKE_CASE_				:  List[Any]           =     image[0, -3:, -3:, -1]
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     image_from_tuple[0, -3:, -3:, -1]

        assert image.shape == (1, 64, 64, 3)
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     np.array([0.8512, 0.818, 0.6411, 0.6808, 0.4465, 0.5618, 0.46, 0.6231, 0.5172]					)
        SCREAMING_SNAKE_CASE_				:  Any           =     1e-2 if torch_device != 'mps' else 3e-2

        assert np.abs(image_slice.flatten() - expected_slice					).max() < tolerance
        assert np.abs(image_from_tuple_slice.flatten() - expected_slice					).max() < tolerance

@slow
@require_torch
class  _A		(  unittest.TestCase):

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[str]           =     LDMPipeline.from_pretrained('CompVis/ldm-celebahq-256'					)
        ldm.to(_SCREAMING_SNAKE_CASE					)
        ldm.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE					)

        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     torch.manual_seed(0					)
        SCREAMING_SNAKE_CASE_				:  Any           =     ldm(generator=_SCREAMING_SNAKE_CASE ,					num_inference_steps=5 ,					output_type='numpy'					).images

        SCREAMING_SNAKE_CASE_				:  str           =     image[0, -3:, -3:, -1]

        assert image.shape == (1, 256, 256, 3)
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     np.array([0.4399, 0.44975, 0.46825, 0.474, 0.4359, 0.4581, 0.45095, 0.4341, 0.4447]					)
        SCREAMING_SNAKE_CASE_				:  int           =     1e-2 if torch_device != 'mps' else 3e-2

        assert np.abs(image_slice.flatten() - expected_slice					).max() < tolerance









import copy
import os
from typing import Union

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


lowerCAmelCase     :   Optional[int]						     =  logging.get_logger(__name__)

lowerCAmelCase     :   str						     =  {
    'microsoft/git-base': 'https://huggingface.co/microsoft/git-base/resolve/main/config.json',
}
class  _A		(  __magic_name__):
  SCREAMING_SNAKE_CASE    :      Optional[int]       =  '''git_vision_model'''

  def __init__(				self ,					_SCREAMING_SNAKE_CASE=768 ,					_SCREAMING_SNAKE_CASE=3072 ,					_SCREAMING_SNAKE_CASE=12 ,					_SCREAMING_SNAKE_CASE=12 ,					_SCREAMING_SNAKE_CASE=3 ,					_SCREAMING_SNAKE_CASE=224 ,					_SCREAMING_SNAKE_CASE=16 ,					_SCREAMING_SNAKE_CASE="quick_gelu" ,					_SCREAMING_SNAKE_CASE=1e-5 ,					_SCREAMING_SNAKE_CASE=0.0 ,					_SCREAMING_SNAKE_CASE=0.02 ,					**_SCREAMING_SNAKE_CASE ,					):





        """simple docstring"""







        super().__init__(**_SCREAMING_SNAKE_CASE					)

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



  @classmethod
  def  UpperCAmelCase    (				cls ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        cls._set_token_in_kwargs(_SCREAMING_SNAKE_CASE					)

        SCREAMING_SNAKE_CASE_       ,						SCREAMING_SNAKE_CASE_				:  str           =     cls.get_config_dict(_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					)

        # get the vision config dict if we are loading from GITConfig
        if config_dict.get('model_type'					) == "git":
              SCREAMING_SNAKE_CASE_				:  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(_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					)

class  _A		(  __magic_name__):
  SCREAMING_SNAKE_CASE    :      Optional[Any]       =  '''git'''

  def __init__(				self ,					_SCREAMING_SNAKE_CASE=None ,					_SCREAMING_SNAKE_CASE=3_0522 ,					_SCREAMING_SNAKE_CASE=768 ,					_SCREAMING_SNAKE_CASE=6 ,					_SCREAMING_SNAKE_CASE=12 ,					_SCREAMING_SNAKE_CASE=3072 ,					_SCREAMING_SNAKE_CASE="gelu" ,					_SCREAMING_SNAKE_CASE=0.1 ,					_SCREAMING_SNAKE_CASE=0.1 ,					_SCREAMING_SNAKE_CASE=1024 ,					_SCREAMING_SNAKE_CASE=0.02 ,					_SCREAMING_SNAKE_CASE=1e-12 ,					_SCREAMING_SNAKE_CASE=0 ,					_SCREAMING_SNAKE_CASE="absolute" ,					_SCREAMING_SNAKE_CASE=True ,					_SCREAMING_SNAKE_CASE=False ,					_SCREAMING_SNAKE_CASE=101 ,					_SCREAMING_SNAKE_CASE=102 ,					_SCREAMING_SNAKE_CASE=None ,					**_SCREAMING_SNAKE_CASE ,					):





        """simple docstring"""







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

        if vision_config is None:
              SCREAMING_SNAKE_CASE_				:  Any           =     {}
              logger.info('vision_config is None. initializing the GitVisionConfig with default values.'					)

        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     GitVisionConfig(**_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     vocab_size
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     hidden_size
        SCREAMING_SNAKE_CASE_				:  Dict           =     num_hidden_layers
        SCREAMING_SNAKE_CASE_				:  List[str]           =     num_attention_heads
        SCREAMING_SNAKE_CASE_				:  List[str]           =     hidden_act
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     intermediate_size
        SCREAMING_SNAKE_CASE_				:  Any           =     hidden_dropout_prob
        SCREAMING_SNAKE_CASE_				:  Tuple           =     attention_probs_dropout_prob
        SCREAMING_SNAKE_CASE_				:  int           =     max_position_embeddings
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     initializer_range
        SCREAMING_SNAKE_CASE_				:  int           =     layer_norm_eps
        SCREAMING_SNAKE_CASE_				:  List[str]           =     position_embedding_type
        SCREAMING_SNAKE_CASE_				:  List[str]           =     use_cache
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     tie_word_embeddings
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     num_image_with_embedding

        SCREAMING_SNAKE_CASE_				:  Tuple           =     bos_token_id
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     eos_token_id



  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[str]           =     copy.deepcopy(self.__dict__					)
        SCREAMING_SNAKE_CASE_				:  List[str]           =     self.vision_config.to_dict()
        SCREAMING_SNAKE_CASE_				:  int           =     self.__class__.model_type
        return output






