




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

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


__lowerCAmelCase		:				Union[str, Any]       			=		logging.get_logger(__name__)  # pylint: disable=invalid-name




class     snake_case__							(_UpperCamelCase   ):
				"""simple docstring"""



				def __init__(      self				:     List[Any]					,      __lowerCamelCase				:     Tuple					,      __lowerCamelCase				:     List[str]=7_68							)					->     Any:
							super().__init__(__lowerCamelCase							)
							a    =			proj_size

							a    =			CLIPVisionModel(__lowerCamelCase							)
							a    =			PaintByExampleMapper(__lowerCamelCase							)
							a    =			nn.LayerNorm(config.hidden_size							)
							a    =			nn.Linear(config.hidden_size					,      self.proj_size							)

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





				def 							__UpperCAmelCase							(      self				:     Union[str, Any]					,      __lowerCamelCase				:     Tuple					,      __lowerCamelCase				:     List[str]=False							)					->     Tuple:
							a    =			self.model(pixel_values=__lowerCamelCase							)
							a    =			clip_output.pooler_output
							a    =			self.mapper(latent_states[:, None]							)
							a    =			self.final_layer_norm(__lowerCamelCase							)
							a    =			self.proj_out(__lowerCamelCase							)
							if return_uncond_vector:
										return latent_states, self.uncond_vector

							return latent_states




class     snake_case__							(nn.Module   ):
				"""simple docstring"""



				def __init__(      self				:     Dict					,      __lowerCamelCase				:     List[Any]							)					->     Any:
							super().__init__()
							a    =			(config.num_hidden_layers + 1) // 5
							a    =			config.hidden_size
							a    =			1
							a    =			nn.ModuleList(
							    [
							        BasicTransformerBlock(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      activation_fn="gelu"					,      attention_bias=__lowerCamelCase							)
							        for _ in range(__lowerCamelCase							)
							    ]							)





				def 							__UpperCAmelCase							(      self				:     Tuple					,      __lowerCamelCase				:     int							)					->     Any:
							for block in self.blocks:
										a    =			block(__lowerCamelCase							)

							return hidden_states














"""simple docstring"""



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


__A		:       List[Any]  				=		{
    '''configuration_mega''': ['''MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MegaConfig''', '''MegaOnnxConfig'''],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__A		:       List[str]  				=		[
						    '''MEGA_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''MegaForCausalLM''',
						    '''MegaForMaskedLM''',
						    '''MegaForMultipleChoice''',
						    '''MegaForQuestionAnswering''',
						    '''MegaForSequenceClassification''',
						    '''MegaForTokenClassification''',
						    '''MegaModel''',
						    '''MegaPreTrainedModel''',
						]

if TYPE_CHECKING:
						from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_mega import (
												    MEGA_PRETRAINED_MODEL_ARCHIVE_LIST,
												    MegaForCausalLM,
												    MegaForMaskedLM,
												    MegaForMultipleChoice,
												    MegaForQuestionAnswering,
												    MegaForSequenceClassification,
												    MegaForTokenClassification,
												    MegaModel,
												    MegaPreTrainedModel,
												)

else:
						import sys

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



