



'''simple docstring'''



import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin






class 					UpperCAmelCase__	(		UpperCamelCase__							,       UpperCamelCase__ ):



					@register_to_config
					def __init__(    self			,				*,
					    UpperCamelCase = 4			,				UpperCamelCase = 768			,				UpperCamelCase			,				UpperCamelCase			,				)     ->			Optional[Any]:
						super().__init__()

						__lowerCAmelCase												=  nn.Parameter(torch.zeros(UpperCamelCase     )     )

						# parameters for additional clip time embeddings
						__lowerCAmelCase												=  nn.Linear(UpperCamelCase			,				UpperCamelCase     )
						__lowerCAmelCase												=  nn.Linear(UpperCamelCase			,				UpperCamelCase     )

						# parameters for encoder hidden states
						__lowerCAmelCase												=  clip_extra_context_tokens
						__lowerCAmelCase												=  nn.Linear(
						    UpperCamelCase			,				self.clip_extra_context_tokens * cross_attention_dim     )
						__lowerCAmelCase												=  nn.Linear(UpperCamelCase			,				UpperCamelCase     )
						__lowerCAmelCase												=  nn.LayerNorm(UpperCamelCase     )



					def        UpperCAmelCase_ (    self			,				*, UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			List[Any]:
						if do_classifier_free_guidance:
							# Add the classifier free guidance embeddings to the image embeddings
							__lowerCAmelCase												=  image_embeddings.shape[0]
							__lowerCAmelCase												=  self.learned_classifier_free_guidance_embeddings.unsqueeze(0     )
							__lowerCAmelCase												=  classifier_free_guidance_embeddings.expand(
							    UpperCamelCase			,				-1     )
							__lowerCAmelCase												=  torch.cat([classifier_free_guidance_embeddings, image_embeddings]			,				dim=0     )

						# The image embeddings batch size and the text embeddings batch size are equal
						assert image_embeddings.shape[0] == prompt_embeds.shape[0]

						__lowerCAmelCase												=  prompt_embeds.shape[0]

						# "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
						# adding CLIP embeddings to the existing timestep embedding, ...
						__lowerCAmelCase												=  self.embedding_proj(UpperCamelCase     )
						__lowerCAmelCase												=  self.clip_image_embeddings_project_to_time_embeddings(UpperCamelCase     )
						__lowerCAmelCase												=  time_projected_image_embeddings + time_projected_prompt_embeds

						# ... and by projecting CLIP embeddings into four
						# extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
						__lowerCAmelCase												=  self.clip_extra_context_tokens_proj(UpperCamelCase     )
						__lowerCAmelCase												=  clip_extra_context_tokens.reshape(UpperCamelCase			,				-1			,				self.clip_extra_context_tokens     )
						__lowerCAmelCase												=  clip_extra_context_tokens.permute(0			,				2			,				1     )

						__lowerCAmelCase												=  self.encoder_hidden_states_proj(UpperCamelCase     )
						__lowerCAmelCase												=  self.text_encoder_hidden_states_norm(UpperCamelCase     )
						__lowerCAmelCase												=  torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]			,				dim=1     )

						return text_encoder_hidden_states, additive_clip_time_embeddings




'''simple docstring'''



from argparse import ArgumentParser

from . import BaseTransformersCLICommand



def   __lowerCAmelCase					(							lowerCamelCase   :						List[str]					):






	'''simple docstring'''




	return DownloadCommand(args.model				,		args.cache_dir				,		args.force				,		args.trust_remote_code					)






class 					UpperCAmelCase__	(		UpperCamelCase__ ):



					@staticmethod
					def        UpperCAmelCase_ (    UpperCamelCase     )     ->			Tuple:
						__lowerCAmelCase												=  parser.add_parser("download"     )
						download_parser.add_argument(
						    "--cache-dir"			,				type=UpperCamelCase			,				default=UpperCamelCase			,				help="Path to location to store the models"     )
						download_parser.add_argument(
						    "--force"			,				action="store_true"			,				help="Force the model to be download even if already in cache-dir"     )
						download_parser.add_argument(
						    "--trust-remote-code"			,				action="store_true"			,				help="Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you've reviewed the code as it will execute on your local machine"			,				)
						download_parser.add_argument("model"			,				type=UpperCamelCase			,				help="Name of the model to download"     )
						download_parser.set_defaults(func=UpperCamelCase     )



					def __init__(    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			List[str]:
						__lowerCAmelCase												=  model
						__lowerCAmelCase												=  cache
						__lowerCAmelCase												=  force
						__lowerCAmelCase												=  trust_remote_code



					def        UpperCAmelCase_ (    self     )     ->			Any:
						from ..models.auto import AutoModel, AutoTokenizer

						AutoModel.from_pretrained(
						    self._model			,				cache_dir=self._cache			,				force_download=self._force			,				trust_remote_code=self._trust_remote_code     )
						AutoTokenizer.from_pretrained(
						    self._model			,				cache_dir=self._cache			,				force_download=self._force			,				trust_remote_code=self._trust_remote_code     )