





import math
from typing import Optional

import numpy as np
import torch
from torch import nn

from .activations import get_activation







def 						_lowercase       (     __lowerCamelCase			:					torch.Tensor		,__lowerCamelCase			:					int		,__lowerCamelCase			:					bool = False		,__lowerCamelCase			:					float = 1		,__lowerCamelCase			:					float = 1		,__lowerCamelCase			:					int = 10000		,)  ->   str:





     '''simple docstring'''





     assert len(timesteps.shape			) == 1, "Timesteps should be a 1d-array"

     UpperCamelCase__							:  Any								=     embedding_dim // 2
     UpperCamelCase__							:  Any								=     -math.log(__lowerCamelCase			) * torch.arange(
         start=0		,end=__lowerCamelCase		,dtype=torch.floataa		,device=timesteps.device			)
     UpperCamelCase__							:  Dict								=     exponent / (half_dim - downscale_freq_shift)

     UpperCamelCase__							:  str								=     torch.exp(__lowerCamelCase			)
     UpperCamelCase__							:  List[Any]								=     timesteps[:, None].float() * emb[None, :]

     # scale embeddings
     UpperCamelCase__							:  Union[str, Any]								=     scale * emb

     # concat sine and cosine embeddings
     UpperCamelCase__							:  Optional[int]								=     torch.cat([torch.sin(__lowerCamelCase			), torch.cos(__lowerCamelCase			)]		,dim=-1			)

     # flip sine and cosine embeddings
     if flip_sin_to_cos:
          UpperCamelCase__							:  Tuple								=     torch.cat([emb[:, half_dim:], emb[:, :half_dim]]		,dim=-1			)

     # zero pad
     if embedding_dim % 2 == 1:
          UpperCamelCase__							:  str								=     torch.nn.functional.pad(__lowerCamelCase		,(0, 1, 0, 0)			)
     return emb







def 						_lowercase       (     __lowerCamelCase			:					List[Any]		,__lowerCamelCase			:					Any		,__lowerCamelCase			:					str=False		,__lowerCamelCase			:					str=0			)  ->   Optional[Any]:





     '''simple docstring'''





     UpperCamelCase__							:  Tuple								=     np.arange(__lowerCamelCase		,dtype=np.floataa			)
     UpperCamelCase__							:  Dict								=     np.arange(__lowerCamelCase		,dtype=np.floataa			)
     UpperCamelCase__							:  List[Any]								=     np.meshgrid(__lowerCamelCase		,__lowerCamelCase			)  # here w goes first
     UpperCamelCase__							:  Tuple								=     np.stack(__lowerCamelCase		,axis=0			)

     UpperCamelCase__							:  int								=     grid.reshape([2, 1, grid_size, grid_size]			)
     UpperCamelCase__							:  Optional[Any]								=     get_ad_sincos_pos_embed_from_grid(__lowerCamelCase		,__lowerCamelCase			)
     if cls_token and extra_tokens > 0:
          UpperCamelCase__							:  List[str]								=     np.concatenate([np.zeros([extra_tokens, embed_dim]			), pos_embed]		,axis=0			)
     return pos_embed







def 						_lowercase       (     __lowerCamelCase			:					Union[str, Any]		,__lowerCamelCase			:					Optional[int]			)  ->   List[Any]:





     '''simple docstring'''





     if embed_dim % 2 != 0:
          raise ValueError('''embed_dim must be divisible by 2'''			)

     # use half of dimensions to encode grid_h
     UpperCamelCase__							:  Union[str, Any]								=     get_ad_sincos_pos_embed_from_grid(embed_dim // 2		,grid[0]			)  # (H*W, D/2)
     UpperCamelCase__							:  int								=     get_ad_sincos_pos_embed_from_grid(embed_dim // 2		,grid[1]			)  # (H*W, D/2)

     UpperCamelCase__							:  Optional[int]								=     np.concatenate([emb_h, emb_w]		,axis=1			)  # (H*W, D)
     return emb







def 						_lowercase       (     __lowerCamelCase			:					List[str]		,__lowerCamelCase			:					Tuple			)  ->   List[str]:





     '''simple docstring'''





     if embed_dim % 2 != 0:
          raise ValueError('''embed_dim must be divisible by 2'''			)

     UpperCamelCase__							:  Tuple								=     np.arange(embed_dim // 2		,dtype=np.floataa			)
     omega /= embed_dim / 2.0
     UpperCamelCase__							:  Tuple								=     1.0 / 10000**omega  # (D/2,)

     UpperCamelCase__							:  Dict								=     pos.reshape(-1			)  # (M,)
     UpperCamelCase__							:  Tuple								=     np.einsum('''m,d->md'''		,__lowerCamelCase		,__lowerCamelCase			)  # (M, D/2), outer product

     UpperCamelCase__							:  List[Any]								=     np.sin(__lowerCamelCase			)  # (M, D/2)
     UpperCamelCase__							:  Optional[int]								=     np.cos(__lowerCamelCase			)  # (M, D/2)

     UpperCamelCase__							:  List[Any]								=     np.concatenate([emb_sin, emb_cos]		,axis=1			)  # (M, D)
     return emb

class  UpperCamelCase__ ( nn.Module			):






      def __init__(     self					:   Dict,		__lowerCamelCase					:   List[Any]=2_24,		__lowerCamelCase					:   Union[str, Any]=2_24,		__lowerCamelCase					:   Optional[Any]=16,		__lowerCamelCase					:   List[Any]=3,		__lowerCamelCase					:   Optional[Any]=7_68,		__lowerCamelCase					:   Tuple=False,		__lowerCamelCase					:   List[str]=True,		__lowerCamelCase					:   Dict=True,		)  -> Optional[int]:
           super().__init__()

           UpperCamelCase__							:  Optional[Any]								=     (height // patch_size) * (width // patch_size)
           UpperCamelCase__							:  Optional[int]								=     flatten
           UpperCamelCase__							:  List[str]								=     layer_norm

           UpperCamelCase__							:  List[Any]								=     nn.Convad(
               __lowerCamelCase,		__lowerCamelCase,		kernel_size=(patch_size, patch_size),		stride=__lowerCamelCase,		bias=__lowerCamelCase   )
           if layer_norm:
                UpperCamelCase__							:  List[str]								=     nn.LayerNorm(__lowerCamelCase,		elementwise_affine=__lowerCamelCase,		eps=1e-6   )
           else:
                UpperCamelCase__							:  Tuple								=     None

           UpperCamelCase__							:  Optional[Any]								=     get_ad_sincos_pos_embed(__lowerCamelCase,		int(num_patches**0.5   )   )
           self.register_buffer('''pos_embed''',		torch.from_numpy(__lowerCamelCase   ).float().unsqueeze(0   ),		persistent=__lowerCamelCase   )






      def 	__lowercase(     self					:   List[str],		__lowerCamelCase					:   Union[str, Any]   )  -> str:
           UpperCamelCase__							:  int								=     self.proj(__lowerCamelCase   )
           if self.flatten:
                UpperCamelCase__							:  Optional[Any]								=     latent.flatten(2   ).transpose(1,		2   )  # BCHW -> BNC
           if self.layer_norm:
                UpperCamelCase__							:  Any								=     self.norm(__lowerCamelCase   )
           return latent + self.pos_embed

class  UpperCamelCase__ ( nn.Module			):






      def __init__(     self					:   int,		__lowerCamelCase					:   int,		__lowerCamelCase					:   int,		__lowerCamelCase					:   str = "silu",		__lowerCamelCase					:   int = None,		__lowerCamelCase					:   Optional[str] = None,		__lowerCamelCase					:   Dict=None,		)  -> str:
           super().__init__()

           UpperCamelCase__							:  List[str]								=     nn.Linear(__lowerCamelCase,		__lowerCamelCase   )

           if cond_proj_dim is not None:
                UpperCamelCase__							:  List[str]								=     nn.Linear(__lowerCamelCase,		__lowerCamelCase,		bias=__lowerCamelCase   )
           else:
                UpperCamelCase__							:  List[str]								=     None

           UpperCamelCase__							:  int								=     get_activation(__lowerCamelCase   )

           if out_dim is not None:
                UpperCamelCase__							:  Tuple								=     out_dim
           else:
                UpperCamelCase__							:  List[Any]								=     time_embed_dim
           UpperCamelCase__							:  Tuple								=     nn.Linear(__lowerCamelCase,		__lowerCamelCase   )

           if post_act_fn is None:
                UpperCamelCase__							:  Any								=     None
           else:
                UpperCamelCase__							:  Any								=     get_activation(__lowerCamelCase   )






      def 	__lowercase(     self					:   Any,		__lowerCamelCase					:   Optional[int],		__lowerCamelCase					:   List[str]=None   )  -> Tuple:
           if condition is not None:
                UpperCamelCase__							:  Union[str, Any]								=     sample + self.cond_proj(__lowerCamelCase   )
           UpperCamelCase__							:  Union[str, Any]								=     self.linear_a(__lowerCamelCase   )

           if self.act is not None:
                UpperCamelCase__							:  Any								=     self.act(__lowerCamelCase   )

           UpperCamelCase__							:  Dict								=     self.linear_a(__lowerCamelCase   )

           if self.post_act is not None:
                UpperCamelCase__							:  Tuple								=     self.post_act(__lowerCamelCase   )
           return sample

class  UpperCamelCase__ ( nn.Module			):






      def __init__(     self					:   Tuple,		__lowerCamelCase					:   int,		__lowerCamelCase					:   bool,		__lowerCamelCase					:   float   )  -> Dict:
           super().__init__()
           UpperCamelCase__							:  int								=     num_channels
           UpperCamelCase__							:  Union[str, Any]								=     flip_sin_to_cos
           UpperCamelCase__							:  List[str]								=     downscale_freq_shift






      def 	__lowercase(     self					:   Any,		__lowerCamelCase					:   Union[str, Any]   )  -> str:
           UpperCamelCase__							:  str								=     get_timestep_embedding(
               __lowerCamelCase,		self.num_channels,		flip_sin_to_cos=self.flip_sin_to_cos,		downscale_freq_shift=self.downscale_freq_shift,		)
           return t_emb

class  UpperCamelCase__ ( nn.Module			):






      def __init__(     self					:   str,		__lowerCamelCase					:   int = 2_56,		__lowerCamelCase					:   float = 1.0,		__lowerCamelCase					:   Optional[int]=True,		__lowerCamelCase					:   List[Any]=True,		__lowerCamelCase					:   Optional[Any]=False   )  -> Optional[int]:
           super().__init__()
           UpperCamelCase__							:  Tuple								=     nn.Parameter(torch.randn(__lowerCamelCase   ) * scale,		requires_grad=__lowerCamelCase   )
           UpperCamelCase__							:  Tuple								=     log
           UpperCamelCase__							:  str								=     flip_sin_to_cos

           if set_W_to_weight:
                # to delete later
                UpperCamelCase__							:  Optional[Any]								=     nn.Parameter(torch.randn(__lowerCamelCase   ) * scale,		requires_grad=__lowerCamelCase   )

                UpperCamelCase__							:  Any								=     self.W






      def 	__lowercase(     self					:   Dict,		__lowerCamelCase					:   Optional[int]   )  -> List[str]:
           if self.log:
                UpperCamelCase__							:  Optional[Any]								=     torch.log(__lowerCamelCase   )

           UpperCamelCase__							:  str								=     x[:, None] * self.weight[None, :] * 2 * np.pi

           if self.flip_sin_to_cos:
                UpperCamelCase__							:  Optional[int]								=     torch.cat([torch.cos(__lowerCamelCase   ), torch.sin(__lowerCamelCase   )],		dim=-1   )
           else:
                UpperCamelCase__							:  List[Any]								=     torch.cat([torch.sin(__lowerCamelCase   ), torch.cos(__lowerCamelCase   )],		dim=-1   )
           return out

class  UpperCamelCase__ ( nn.Module			):






      def __init__(     self					:   Any,		__lowerCamelCase					:   int,		__lowerCamelCase					:   int,		__lowerCamelCase					:   int,		__lowerCamelCase					:   int,		)  -> Union[str, Any]:
           super().__init__()

           UpperCamelCase__							:  Optional[int]								=     height
           UpperCamelCase__							:  int								=     width
           UpperCamelCase__							:  Union[str, Any]								=     num_embed
           UpperCamelCase__							:  Optional[int]								=     embed_dim

           UpperCamelCase__							:  Union[str, Any]								=     nn.Embedding(self.num_embed,		__lowerCamelCase   )
           UpperCamelCase__							:  Any								=     nn.Embedding(self.height,		__lowerCamelCase   )
           UpperCamelCase__							:  Tuple								=     nn.Embedding(self.width,		__lowerCamelCase   )






      def 	__lowercase(     self					:   Optional[int],		__lowerCamelCase					:   Optional[Any]   )  -> Any:
           UpperCamelCase__							:  Dict								=     self.emb(__lowerCamelCase   )

           UpperCamelCase__							:  List[str]								=     self.height_emb(torch.arange(self.height,		device=index.device   ).view(1,		self.height   )   )

           # 1 x H x D -> 1 x H x 1 x D
           UpperCamelCase__							:  Any								=     height_emb.unsqueeze(2   )

           UpperCamelCase__							:  Any								=     self.width_emb(torch.arange(self.width,		device=index.device   ).view(1,		self.width   )   )

           # 1 x W x D -> 1 x 1 x W x D
           UpperCamelCase__							:  str								=     width_emb.unsqueeze(1   )

           UpperCamelCase__							:  Any								=     height_emb + width_emb

           # 1 x H x W x D -> 1 x L xD
           UpperCamelCase__							:  List[str]								=     pos_emb.view(1,		self.height * self.width,		-1   )

           UpperCamelCase__							:  str								=     emb + pos_emb[:, : emb.shape[1], :]

           return emb

class  UpperCamelCase__ ( nn.Module			):






      def __init__(     self					:   Tuple,		__lowerCamelCase					:   Union[str, Any],		__lowerCamelCase					:   Union[str, Any],		__lowerCamelCase					:   List[Any]   )  -> List[str]:
           super().__init__()
           UpperCamelCase__							:  List[Any]								=     dropout_prob > 0
           UpperCamelCase__							:  Tuple								=     nn.Embedding(num_classes + use_cfg_embedding,		__lowerCamelCase   )
           UpperCamelCase__							:  Tuple								=     num_classes
           UpperCamelCase__							:  Union[str, Any]								=     dropout_prob






      def 	__lowercase(     self					:   int,		__lowerCamelCase					:   int,		__lowerCamelCase					:   str=None   )  -> Optional[int]:
           if force_drop_ids is None:
                UpperCamelCase__							:  Dict								=     torch.rand(labels.shape[0],		device=labels.device   ) < self.dropout_prob
           else:
                UpperCamelCase__							:  List[str]								=     torch.tensor(force_drop_ids == 1   )
           UpperCamelCase__							:  Optional[Any]								=     torch.where(__lowerCamelCase,		self.num_classes,		__lowerCamelCase   )
           return labels






      def 	__lowercase(     self					:   Union[str, Any],		__lowerCamelCase					:   torch.LongTensor,		__lowerCamelCase					:   Optional[Any]=None   )  -> List[Any]:
           UpperCamelCase__							:  Tuple								=     self.dropout_prob > 0
           if (self.training and use_dropout) or (force_drop_ids is not None):
                UpperCamelCase__							:  Dict								=     self.token_drop(__lowerCamelCase,		__lowerCamelCase   )
           UpperCamelCase__							:  str								=     self.embedding_table(__lowerCamelCase   )
           return embeddings

class  UpperCamelCase__ ( nn.Module			):






      def __init__(     self					:   str,		__lowerCamelCase					:   int = 10_24,		__lowerCamelCase					:   int = 7_68,		__lowerCamelCase					:   int = 7_68,		__lowerCamelCase					:   int = 10,		)  -> Any:
           super().__init__()

           UpperCamelCase__							:  List[Any]								=     num_image_text_embeds
           UpperCamelCase__							:  Any								=     nn.Linear(__lowerCamelCase,		self.num_image_text_embeds * cross_attention_dim   )
           UpperCamelCase__							:  List[Any]								=     nn.Linear(__lowerCamelCase,		__lowerCamelCase   )






      def 	__lowercase(     self					:   Any,		__lowerCamelCase					:   torch.FloatTensor,		__lowerCamelCase					:   torch.FloatTensor   )  -> Any:
           UpperCamelCase__							:  List[str]								=     text_embeds.shape[0]

           # image
           UpperCamelCase__							:  Tuple								=     self.image_embeds(__lowerCamelCase   )
           UpperCamelCase__							:  int								=     image_text_embeds.reshape(__lowerCamelCase,		self.num_image_text_embeds,		-1   )

           # text
           UpperCamelCase__							:  int								=     self.text_proj(__lowerCamelCase   )

           return torch.cat([image_text_embeds, text_embeds],		dim=1   )

class  UpperCamelCase__ ( nn.Module			):






      def __init__(     self					:   Tuple,		__lowerCamelCase					:   int = 7_68,		__lowerCamelCase					:   int = 7_68,		__lowerCamelCase					:   int = 32,		)  -> Any:
           super().__init__()

           UpperCamelCase__							:  Dict								=     num_image_text_embeds
           UpperCamelCase__							:  Dict								=     nn.Linear(__lowerCamelCase,		self.num_image_text_embeds * cross_attention_dim   )
           UpperCamelCase__							:  List[Any]								=     nn.LayerNorm(__lowerCamelCase   )






      def 	__lowercase(     self					:   Dict,		__lowerCamelCase					:   torch.FloatTensor   )  -> int:
           UpperCamelCase__							:  int								=     image_embeds.shape[0]

           # image
           UpperCamelCase__							:  List[Any]								=     self.image_embeds(__lowerCamelCase   )
           UpperCamelCase__							:  Any								=     image_embeds.reshape(__lowerCamelCase,		self.num_image_text_embeds,		-1   )
           UpperCamelCase__							:  Any								=     self.norm(__lowerCamelCase   )
           return image_embeds

class  UpperCamelCase__ ( nn.Module			):






      def __init__(     self					:   Any,		__lowerCamelCase					:   Union[str, Any],		__lowerCamelCase					:   Tuple,		__lowerCamelCase					:   Optional[int]=0.1   )  -> Union[str, Any]:
           super().__init__()

           UpperCamelCase__							:  Dict								=     Timesteps(num_channels=2_56,		flip_sin_to_cos=__lowerCamelCase,		downscale_freq_shift=1   )
           UpperCamelCase__							:  Dict								=     TimestepEmbedding(in_channels=2_56,		time_embed_dim=__lowerCamelCase   )
           UpperCamelCase__							:  Union[str, Any]								=     LabelEmbedding(__lowerCamelCase,		__lowerCamelCase,		__lowerCamelCase   )






      def 	__lowercase(     self					:   Any,		__lowerCamelCase					:   Union[str, Any],		__lowerCamelCase					:   Optional[int],		__lowerCamelCase					:   int=None   )  -> int:
           UpperCamelCase__							:  List[str]								=     self.time_proj(__lowerCamelCase   )
           UpperCamelCase__							:  Optional[Any]								=     self.timestep_embedder(timesteps_proj.to(dtype=__lowerCamelCase   )   )  # (N, D)

           UpperCamelCase__							:  Tuple								=     self.class_embedder(__lowerCamelCase   )  # (N, D)

           UpperCamelCase__							:  Dict								=     timesteps_emb + class_labels  # (N, D)

           return conditioning

class  UpperCamelCase__ ( nn.Module			):






      def __init__(     self					:   int,		__lowerCamelCase					:   int,		__lowerCamelCase					:   int,		__lowerCamelCase					:   int = 64   )  -> List[Any]:
           super().__init__()
           UpperCamelCase__							:  Union[str, Any]								=     nn.LayerNorm(__lowerCamelCase   )
           UpperCamelCase__							:  Union[str, Any]								=     AttentionPooling(__lowerCamelCase,		__lowerCamelCase   )
           UpperCamelCase__							:  Dict								=     nn.Linear(__lowerCamelCase,		__lowerCamelCase   )
           UpperCamelCase__							:  int								=     nn.LayerNorm(__lowerCamelCase   )






      def 	__lowercase(     self					:   List[str],		__lowerCamelCase					:   int   )  -> List[Any]:
           UpperCamelCase__							:  Optional[Any]								=     self.norma(__lowerCamelCase   )
           UpperCamelCase__							:  Optional[int]								=     self.pool(__lowerCamelCase   )
           UpperCamelCase__							:  Union[str, Any]								=     self.proj(__lowerCamelCase   )
           UpperCamelCase__							:  Optional[Any]								=     self.norma(__lowerCamelCase   )
           return hidden_states

class  UpperCamelCase__ ( nn.Module			):






      def __init__(     self					:   Optional[Any],		__lowerCamelCase					:   int = 7_68,		__lowerCamelCase					:   int = 7_68,		__lowerCamelCase					:   int = 15_36   )  -> Tuple:
           super().__init__()
           UpperCamelCase__							:  Optional[int]								=     nn.Linear(__lowerCamelCase,		__lowerCamelCase   )
           UpperCamelCase__							:  Tuple								=     nn.LayerNorm(__lowerCamelCase   )
           UpperCamelCase__							:  str								=     nn.Linear(__lowerCamelCase,		__lowerCamelCase   )






      def 	__lowercase(     self					:   Optional[int],		__lowerCamelCase					:   torch.FloatTensor,		__lowerCamelCase					:   torch.FloatTensor   )  -> List[Any]:
           # text
           UpperCamelCase__							:  Dict								=     self.text_proj(__lowerCamelCase   )
           UpperCamelCase__							:  int								=     self.text_norm(__lowerCamelCase   )

           # image
           UpperCamelCase__							:  Union[str, Any]								=     self.image_proj(__lowerCamelCase   )

           return time_image_embeds + time_text_embeds

class  UpperCamelCase__ ( nn.Module			):






      def __init__(     self					:   Union[str, Any],		__lowerCamelCase					:   int = 7_68,		__lowerCamelCase					:   int = 15_36   )  -> str:
           super().__init__()
           UpperCamelCase__							:  int								=     nn.Linear(__lowerCamelCase,		__lowerCamelCase   )
           UpperCamelCase__							:  Optional[int]								=     nn.LayerNorm(__lowerCamelCase   )






      def 	__lowercase(     self					:   Union[str, Any],		__lowerCamelCase					:   torch.FloatTensor   )  -> int:
           # image
           UpperCamelCase__							:  Optional[int]								=     self.image_proj(__lowerCamelCase   )
           UpperCamelCase__							:  Any								=     self.image_norm(__lowerCamelCase   )
           return time_image_embeds

class  UpperCamelCase__ ( nn.Module			):






      def __init__(     self					:   int,		__lowerCamelCase					:   int = 7_68,		__lowerCamelCase					:   int = 15_36   )  -> str:
           super().__init__()
           UpperCamelCase__							:  List[str]								=     nn.Linear(__lowerCamelCase,		__lowerCamelCase   )
           UpperCamelCase__							:  Tuple								=     nn.LayerNorm(__lowerCamelCase   )
           UpperCamelCase__							:  List[Any]								=     nn.Sequential(
               nn.Convad(3,		16,		3,		padding=1   ),		nn.SiLU(),		nn.Convad(16,		16,		3,		padding=1   ),		nn.SiLU(),		nn.Convad(16,		32,		3,		padding=1,		stride=2   ),		nn.SiLU(),		nn.Convad(32,		32,		3,		padding=1   ),		nn.SiLU(),		nn.Convad(32,		96,		3,		padding=1,		stride=2   ),		nn.SiLU(),		nn.Convad(96,		96,		3,		padding=1   ),		nn.SiLU(),		nn.Convad(96,		2_56,		3,		padding=1,		stride=2   ),		nn.SiLU(),		nn.Convad(2_56,		4,		3,		padding=1   ),		)






      def 	__lowercase(     self					:   int,		__lowerCamelCase					:   torch.FloatTensor,		__lowerCamelCase					:   torch.FloatTensor   )  -> List[str]:
           # image
           UpperCamelCase__							:  Union[str, Any]								=     self.image_proj(__lowerCamelCase   )
           UpperCamelCase__							:  Dict								=     self.image_norm(__lowerCamelCase   )
           UpperCamelCase__							:  Union[str, Any]								=     self.input_hint_block(__lowerCamelCase   )
           return time_image_embeds, hint

class  UpperCamelCase__ ( nn.Module			):






      def __init__(     self					:   Dict,		__lowerCamelCase					:   Any,		__lowerCamelCase					:   Optional[int],		__lowerCamelCase					:   Union[str, Any]=None   )  -> List[str]:
           super().__init__()
           UpperCamelCase__							:  str								=     dtype
           UpperCamelCase__							:  Dict								=     nn.Parameter(torch.randn(1,		__lowerCamelCase   ) / embed_dim**0.5   )
           UpperCamelCase__							:  Tuple								=     nn.Linear(__lowerCamelCase,		__lowerCamelCase,		dtype=self.dtype   )
           UpperCamelCase__							:  int								=     nn.Linear(__lowerCamelCase,		__lowerCamelCase,		dtype=self.dtype   )
           UpperCamelCase__							:  Dict								=     nn.Linear(__lowerCamelCase,		__lowerCamelCase,		dtype=self.dtype   )
           UpperCamelCase__							:  str								=     num_heads
           UpperCamelCase__							:  Optional[Any]								=     embed_dim // self.num_heads






      def 	__lowercase(     self					:   Optional[Any],		__lowerCamelCase					:   Union[str, Any]   )  -> int:
           UpperCamelCase__							:  List[str]								=     x.size()

           def shape(__lowerCamelCase					:   int   ):
                # (bs, length, width) --> (bs, length, n_heads, dim_per_head)
                UpperCamelCase__							:  Optional[Any]								=     x.view(__lowerCamelCase,		-1,		self.num_heads,		self.dim_per_head   )
                # (bs, length, n_heads, dim_per_head) --> (bs, n_heads, length, dim_per_head)
                UpperCamelCase__							:  List[str]								=     x.transpose(1,		2   )
                # (bs, n_heads, length, dim_per_head) --> (bs*n_heads, length, dim_per_head)
                UpperCamelCase__							:  List[str]								=     x.reshape(bs * self.num_heads,		-1,		self.dim_per_head   )
                # (bs*n_heads, length, dim_per_head) --> (bs*n_heads, dim_per_head, length)
                UpperCamelCase__							:  str								=     x.transpose(1,		2   )
                return x

           UpperCamelCase__							:  Union[str, Any]								=     x.mean(dim=1,		keepdim=__lowerCamelCase   ) + self.positional_embedding.to(x.dtype   )
           UpperCamelCase__							:  List[Any]								=     torch.cat([class_token, x],		dim=1   )  # (bs, length+1, width)

           # (bs*n_heads, class_token_length, dim_per_head)
           UpperCamelCase__							:  Tuple								=     shape(self.q_proj(__lowerCamelCase   )   )
           # (bs*n_heads, length+class_token_length, dim_per_head)
           UpperCamelCase__							:  str								=     shape(self.k_proj(__lowerCamelCase   )   )
           UpperCamelCase__							:  Tuple								=     shape(self.v_proj(__lowerCamelCase   )   )

           # (bs*n_heads, class_token_length, length+class_token_length):
           UpperCamelCase__							:  List[str]								=     1 / math.sqrt(math.sqrt(self.dim_per_head   )   )
           UpperCamelCase__							:  Dict								=     torch.einsum('''bct,bcs->bts''',		q * scale,		k * scale   )  # More stable with f16 than dividing afterwards
           UpperCamelCase__							:  Optional[Any]								=     torch.softmax(weight.float(),		dim=-1   ).type(weight.dtype   )

           # (bs*n_heads, dim_per_head, class_token_length)
           UpperCamelCase__							:  int								=     torch.einsum('''bts,bcs->bct''',		__lowerCamelCase,		__lowerCamelCase   )

           # (bs, length+1, width)
           UpperCamelCase__							:  Any								=     a.reshape(__lowerCamelCase,		-1,		1   ).transpose(1,		2   )

           return a[:, 0, :]  # cls_token








from typing import TYPE_CHECKING

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


_SCREAMING_SNAKE_CASE :					str			=	{
    """configuration_donut_swin""": ["""DONUT_SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DonutSwinConfig"""],
    """processing_donut""": ["""DonutProcessor"""],
}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _SCREAMING_SNAKE_CASE :					List[Any]			=	[
       """DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST""",
       """DonutSwinModel""",
       """DonutSwinPreTrainedModel""",
   ]

try:
   if not is_vision_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _SCREAMING_SNAKE_CASE :					List[Any]			=	["""DonutFeatureExtractor"""]
   _SCREAMING_SNAKE_CASE :					Tuple			=	["""DonutImageProcessor"""]


if TYPE_CHECKING:
   from .configuration_donut_swin import DONUT_SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, DonutSwinConfig
   from .processing_donut import DonutProcessor

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_donut_swin import (
          DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
          DonutSwinModel,
          DonutSwinPreTrainedModel,
      )

   try:
      if not is_vision_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .feature_extraction_donut import DonutFeatureExtractor
      from .image_processing_donut import DonutImageProcessor

else:
   import sys

   _SCREAMING_SNAKE_CASE :					Optional[Any]			=	_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




