




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


lowerCamelCase__			:       str         =							{
    'configuration_altclip': [
        'ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'AltCLIPConfig',
        'AltCLIPTextConfig',
        'AltCLIPVisionConfig',
    ],
    'processing_altclip': ['AltCLIPProcessor'],
}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   lowerCamelCase__			:       Union[str, Any]         =							[
       'ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST',
       'AltCLIPPreTrainedModel',
       'AltCLIPModel',
       'AltCLIPTextModel',
       'AltCLIPVisionModel',
   ]


if TYPE_CHECKING:
   from .configuration_altclip import (
       ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
       AltCLIPConfig,
       AltCLIPTextConfig,
       AltCLIPVisionConfig,
   )
   from .processing_altclip import AltCLIPProcessor

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_altclip import (
          ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
          AltCLIPModel,
          AltCLIPPreTrainedModel,
          AltCLIPTextModel,
          AltCLIPVisionModel,
      )


else:
   import sys

   lowerCamelCase__			:       List[Any]         =							_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





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

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


lowerCamelCase__			:       List[Any]         =							logging.get_logger(__name__)  # pylint: disable=invalid-name





class 	lowerCamelCase_							(				_SCREAMING_SNAKE_CASE ):


 '''simple docstring'''







 def __init__(							self  :		List[Any]    ,    _lowerCAmelCase  :		Dict    ,    _lowerCAmelCase  :		Tuple=768						):
       super().__init__(_lowerCAmelCase						)
       SCREAMING_SNAKE_CASE_     						=      proj_size

       SCREAMING_SNAKE_CASE_     						=      CLIPVisionModel(_lowerCAmelCase						)
       SCREAMING_SNAKE_CASE_     						=      PaintByExampleMapper(_lowerCAmelCase						)
       SCREAMING_SNAKE_CASE_     						=      nn.LayerNorm(config.hidden_size						)
       SCREAMING_SNAKE_CASE_     						=      nn.Linear(config.hidden_size    ,    self.proj_size						)

       # uncondition for scaling
       SCREAMING_SNAKE_CASE_     						=      nn.Parameter(torch.randn((1, 1, self.proj_size)						)						)




 def 				lowerCAmelCase_  (							self  :		Any    ,    _lowerCAmelCase  :		int    ,    _lowerCAmelCase  :		int=False						):
       SCREAMING_SNAKE_CASE_     						=      self.model(pixel_values=_lowerCAmelCase						)
       SCREAMING_SNAKE_CASE_     						=      clip_output.pooler_output
       SCREAMING_SNAKE_CASE_     						=      self.mapper(latent_states[:, None]						)
       SCREAMING_SNAKE_CASE_     						=      self.final_layer_norm(_lowerCAmelCase						)
       SCREAMING_SNAKE_CASE_     						=      self.proj_out(_lowerCAmelCase						)
       if return_uncond_vector:
             return latent_states, self.uncond_vector

       return latent_states





class 	lowerCamelCase_							(				nn.Module ):


 '''simple docstring'''







 def __init__(							self  :		int    ,    _lowerCAmelCase  :		Optional[Any]						):
       super().__init__()
       SCREAMING_SNAKE_CASE_     						=      (config.num_hidden_layers + 1) // 5
       SCREAMING_SNAKE_CASE_     						=      config.hidden_size
       SCREAMING_SNAKE_CASE_     						=      1
       SCREAMING_SNAKE_CASE_     						=      nn.ModuleList(
           [
               BasicTransformerBlock(_lowerCAmelCase    ,    _lowerCAmelCase    ,    _lowerCAmelCase    ,    activation_fn='gelu'    ,    attention_bias=_lowerCAmelCase						)
               for _ in range(_lowerCAmelCase						)
           ]						)




 def 				lowerCAmelCase_  (							self  :		Union[str, Any]    ,    _lowerCAmelCase  :		Optional[Any]						):
       for block in self.blocks:
             SCREAMING_SNAKE_CASE_     						=      block(_lowerCAmelCase						)

       return hidden_states