


import torch
from torch import nn
from transformers import CLIPPreTrainedModel, CLIPVisionModel

from ...models.attention import BasicTransformerBlock
from ...utils import logging


__UpperCamelCase				:  Dict      					=							logging.get_logger(__name__)  # pylint: disable=invalid-name





class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    def __init__(	self				:   Tuple       ,   __snake_case				:   Dict       ,   __snake_case				:   int=768	):





       '''simple docstring'''
       super().__init__(__snake_case	)
       UpperCAmelCase_							:      Optional[Any]						=   proj_size

       UpperCAmelCase_							:      Tuple						=   CLIPVisionModel(__snake_case	)
       UpperCAmelCase_							:      List[Any]						=   PaintByExampleMapper(__snake_case	)
       UpperCAmelCase_							:      Optional[int]						=   nn.LayerNorm(config.hidden_size	)
       UpperCAmelCase_							:      Optional[Any]						=   nn.Linear(config.hidden_size       ,   self.proj_size	)

       # uncondition for scaling
       UpperCAmelCase_							:      List[Any]						=   nn.Parameter(torch.randn((1, 1, self.proj_size)	)	)





    def 					_lowerCamelCase (	self				:   str       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   str=False	):





       '''simple docstring'''
       UpperCAmelCase_							:      Union[str, Any]						=   self.model(pixel_values=__snake_case	)
       UpperCAmelCase_							:      int						=   clip_output.pooler_output
       UpperCAmelCase_							:      Optional[Any]						=   self.mapper(latent_states[:, None]	)
       UpperCAmelCase_							:      List[str]						=   self.final_layer_norm(__snake_case	)
       UpperCAmelCase_							:      Union[str, Any]						=   self.proj_out(__snake_case	)
       if return_uncond_vector:
          return latent_states, self.uncond_vector

       return latent_states





class  lowerCAmelCase__(    nn.Module						):






    '''simple docstring'''





    def __init__(	self				:   int       ,   __snake_case				:   Dict	):





       '''simple docstring'''
       super().__init__()
       UpperCAmelCase_							:      List[Any]						=   (config.num_hidden_layers + 1) // 5
       UpperCAmelCase_							:      str						=   config.hidden_size
       UpperCAmelCase_							:      Union[str, Any]						=   1
       UpperCAmelCase_							:      int						=   nn.ModuleList(
           [
               BasicTransformerBlock(__snake_case       ,   __snake_case       ,   __snake_case       ,   activation_fn='''gelu'''       ,   attention_bias=__snake_case	)
               for _ in range(__snake_case	)
           ]	)





    def 					_lowerCamelCase (	self				:   Optional[Any]       ,   __snake_case				:   Tuple	):





       '''simple docstring'''
       for block in self.blocks:
          UpperCAmelCase_							:      Tuple						=   block(__snake_case	)

       return hidden_states



import copy

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


__UpperCamelCase				:  Tuple      					=							logging.get_logger(__name__)





class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    A_  :     Union[str, Any]			= 'encoder-decoder'
    A_  :     Optional[int]			= True





    def __init__(	self				:   Dict       ,   **__snake_case				:   Union[str, Any]	):





       '''simple docstring'''
       super().__init__(**__snake_case	)
       assert (
           "encoder" in kwargs and "decoder" in kwargs
       ), "Config has to be initialized with encoder and decoder config"
       UpperCAmelCase_							:      int						=   kwargs.pop('''encoder'''	)
       UpperCAmelCase_							:      List[Any]						=   encoder_config.pop('''model_type'''	)
       UpperCAmelCase_							:      int						=   kwargs.pop('''decoder'''	)
       UpperCAmelCase_							:      Union[str, Any]						=   decoder_config.pop('''model_type'''	)

       from ..auto.configuration_auto import AutoConfig

       UpperCAmelCase_							:      Optional[int]						=   AutoConfig.for_model(__snake_case       ,   **__snake_case	)
       UpperCAmelCase_							:      Any						=   AutoConfig.for_model(__snake_case       ,   **__snake_case	)
       UpperCAmelCase_							:      List[Any]						=   True





    @classmethod
    def 					_lowerCamelCase (	cls				:   Optional[Any]       ,   __snake_case				:   PretrainedConfig       ,   __snake_case				:   PretrainedConfig       ,   **__snake_case				:   Union[str, Any]	):





       '''simple docstring'''
       logger.info('''Set `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'''	)
       UpperCAmelCase_							:      Dict						=   True
       UpperCAmelCase_							:      str						=   True

       return cls(encoder=encoder_config.to_dict()       ,   decoder=decoder_config.to_dict()       ,   **__snake_case	)





    def 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       UpperCAmelCase_							:      int						=   copy.deepcopy(self.__dict__	)
       UpperCAmelCase_							:      Tuple						=   self.encoder.to_dict()
       UpperCAmelCase_							:      Tuple						=   self.decoder.to_dict()
       UpperCAmelCase_							:      Tuple						=   self.__class__.model_type
       return output