





'''simple docstring'''

import math
from typing import Optional

import numpy as np
import torch
from torch import nn

from .activations import get_activation


def 		__UpperCamelCase		(       _UpperCAmelCase,    _UpperCAmelCase,    _UpperCAmelCase = False,    _UpperCAmelCase = 1,    _UpperCAmelCase = 1,    _UpperCAmelCase = 10000,    ):
		assert len(timesteps.shape   ) == 1, "Timesteps should be a 1d-array"

		__UpperCAmelCase     :				Optional[Any]					   =			embedding_dim // 2
		__UpperCAmelCase     :				str					   =			-math.log(_UpperCAmelCase   ) * torch.arange(
		    start=0,    end=_UpperCAmelCase,    dtype=torch.floataa,    device=timesteps.device   )
		__UpperCAmelCase     :				Optional[int]					   =			exponent / (half_dim - downscale_freq_shift)

		__UpperCAmelCase     :				List[str]					   =			torch.exp(_UpperCAmelCase   )
		__UpperCAmelCase     :				Optional[int]					   =			timesteps[:, None].float() * emb[None, :]

		# scale embeddings
		__UpperCAmelCase     :				int					   =			scale * emb

		# concat sine and cosine embeddings
		__UpperCAmelCase     :				Dict					   =			torch.cat([torch.sin(_UpperCAmelCase   ), torch.cos(_UpperCAmelCase   )],    dim=-1   )

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

		# zero pad
		if embedding_dim % 2 == 1:
				__UpperCAmelCase     :				List[Any]					   =			torch.nn.functional.pad(_UpperCAmelCase,    (0, 1, 0, 0)   )
		return emb


def 		__UpperCamelCase		(       _UpperCAmelCase,    _UpperCAmelCase,    _UpperCAmelCase=False,    _UpperCAmelCase=0   ):
		__UpperCAmelCase     :				Dict					   =			np.arange(_UpperCAmelCase,    dtype=np.floataa   )
		__UpperCAmelCase     :				str					   =			np.arange(_UpperCAmelCase,    dtype=np.floataa   )
		__UpperCAmelCase     :				Optional[int]					   =			np.meshgrid(_UpperCAmelCase,    _UpperCAmelCase   )  # here w goes first
		__UpperCAmelCase     :				Optional[int]					   =			np.stack(_UpperCAmelCase,    axis=0   )

		__UpperCAmelCase     :				Dict					   =			grid.reshape([2, 1, grid_size, grid_size]   )
		__UpperCAmelCase     :				Tuple					   =			get_ad_sincos_pos_embed_from_grid(_UpperCAmelCase,    _UpperCAmelCase   )
		if cls_token and extra_tokens > 0:
				__UpperCAmelCase     :				Optional[int]					   =			np.concatenate([np.zeros([extra_tokens, embed_dim]   ), pos_embed],    axis=0   )
		return pos_embed


def 		__UpperCamelCase		(       _UpperCAmelCase,    _UpperCAmelCase   ):


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

		# use half of dimensions to encode grid_h
		__UpperCAmelCase     :				Optional[int]					   =			get_ad_sincos_pos_embed_from_grid(embed_dim // 2,    grid[0]   )  # (H*W, D/2)
		__UpperCAmelCase     :				Tuple					   =			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 		__UpperCamelCase		(       _UpperCAmelCase,    _UpperCAmelCase   ):


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

		__UpperCAmelCase     :				Union[str, Any]					   =			np.arange(embed_dim // 2,    dtype=np.floataa   )
		omega /= embed_dim / 2.0
		__UpperCAmelCase     :				Dict					   =			1.0 / 10000**omega  # (D/2,)

		__UpperCAmelCase     :				int					   =			pos.reshape(-1   )  # (M,)
		__UpperCAmelCase     :				Optional[Any]					   =			np.einsum("m,d->md",    _UpperCAmelCase,    _UpperCAmelCase   )  # (M, D/2), outer product

		__UpperCAmelCase     :				Union[str, Any]					   =			np.sin(_UpperCAmelCase   )  # (M, D/2)
		__UpperCAmelCase     :				str					   =			np.cos(_UpperCAmelCase   )  # (M, D/2)

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


class 			SCREAMING_SNAKE_CASE__	(					nn.Module						):







					"""simple docstring"""






					def __init__(					self    :	str						,     UpperCAmelCase_    :	Tuple=224						,     UpperCAmelCase_    :	List[str]=224						,     UpperCAmelCase_    :	int=16						,     UpperCAmelCase_    :	Dict=3						,     UpperCAmelCase_    :	int=768						,     UpperCAmelCase_    :	List[Any]=False						,     UpperCAmelCase_    :	Any=True						,     UpperCAmelCase_    :	Union[str, Any]=True						,     ):







							"""simple docstring"""
							super().__init__()

							__UpperCAmelCase     :				Dict					   =			(height // patch_size) * (width // patch_size)
							__UpperCAmelCase     :				Dict					   =			flatten
							__UpperCAmelCase     :				Dict					   =			layer_norm

							__UpperCAmelCase     :				Tuple					   =			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     :				str					   =			None

							__UpperCAmelCase     :				Optional[int]					   =			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 						lowerCamelCase_					(					self    :	Any						,     UpperCAmelCase_    :	int						):







							"""simple docstring"""
							__UpperCAmelCase     :				Optional[Any]					   =			self.proj(lowerCAmelCase_						)
							if self.flatten:
									__UpperCAmelCase     :				Any					   =			latent.flatten(2						).transpose(1						,     2						)  # BCHW -> BNC
							if self.layer_norm:
									__UpperCAmelCase     :				Any					   =			self.norm(lowerCAmelCase_						)
							return latent + self.pos_embed






class 			SCREAMING_SNAKE_CASE__	(					nn.Module						):







					"""simple docstring"""






					def __init__(					self    :	List[str]						,     UpperCAmelCase_    :	int						,     UpperCAmelCase_    :	int						,     UpperCAmelCase_    :	str = "silu"						,     UpperCAmelCase_    :	int = None						,     UpperCAmelCase_    :	Optional[str] = None						,     UpperCAmelCase_    :	Any=None						,     ):







							"""simple docstring"""
							super().__init__()

							__UpperCAmelCase     :				Tuple					   =			nn.Linear(lowerCAmelCase_						,     lowerCAmelCase_						)

							if cond_proj_dim is not None:
									__UpperCAmelCase     :				Dict					   =			nn.Linear(lowerCAmelCase_						,     lowerCAmelCase_						,     bias=lowerCAmelCase_						)
							else:
									__UpperCAmelCase     :				Any					   =			None

							__UpperCAmelCase     :				Union[str, Any]					   =			get_activation(lowerCAmelCase_						)

							if out_dim is not None:
									__UpperCAmelCase     :				Optional[int]					   =			out_dim
							else:
									__UpperCAmelCase     :				List[Any]					   =			time_embed_dim
							__UpperCAmelCase     :				Optional[Any]					   =			nn.Linear(lowerCAmelCase_						,     lowerCAmelCase_						)

							if post_act_fn is None:
									__UpperCAmelCase     :				List[Any]					   =			None
							else:
									__UpperCAmelCase     :				List[str]					   =			get_activation(lowerCAmelCase_						)






					def 						lowerCamelCase_					(					self    :	List[str]						,     UpperCAmelCase_    :	Any						,     UpperCAmelCase_    :	Dict=None						):







							"""simple docstring"""


							if condition is not None:
									__UpperCAmelCase     :				Optional[int]					   =			sample + self.cond_proj(lowerCAmelCase_						)
							__UpperCAmelCase     :				Optional[int]					   =			self.linear_a(lowerCAmelCase_						)

							if self.act is not None:
									__UpperCAmelCase     :				Union[str, Any]					   =			self.act(lowerCAmelCase_						)

							__UpperCAmelCase     :				Tuple					   =			self.linear_a(lowerCAmelCase_						)

							if self.post_act is not None:
									__UpperCAmelCase     :				Any					   =			self.post_act(lowerCAmelCase_						)
							return sample






class 			SCREAMING_SNAKE_CASE__	(					nn.Module						):







					"""simple docstring"""






					def __init__(					self    :	Union[str, Any]						,     UpperCAmelCase_    :	int						,     UpperCAmelCase_    :	bool						,     UpperCAmelCase_    :	float						):







							"""simple docstring"""
							super().__init__()
							__UpperCAmelCase     :				Union[str, Any]					   =			num_channels
							__UpperCAmelCase     :				Optional[int]					   =			flip_sin_to_cos
							__UpperCAmelCase     :				List[str]					   =			downscale_freq_shift






					def 						lowerCamelCase_					(					self    :	int						,     UpperCAmelCase_    :	str						):







							"""simple docstring"""
							__UpperCAmelCase     :				Optional[Any]					   =			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 			SCREAMING_SNAKE_CASE__	(					nn.Module						):







					"""simple docstring"""






					def __init__(					self    :	List[str]						,     UpperCAmelCase_    :	int = 256						,     UpperCAmelCase_    :	float = 1.0						,     UpperCAmelCase_    :	List[str]=True						,     UpperCAmelCase_    :	Tuple=True						,     UpperCAmelCase_    :	Optional[int]=False						):







							"""simple docstring"""
							super().__init__()
							__UpperCAmelCase     :				Optional[Any]					   =			nn.Parameter(torch.randn(lowerCAmelCase_						) * scale						,     requires_grad=lowerCAmelCase_						)
							__UpperCAmelCase     :				Optional[int]					   =			log
							__UpperCAmelCase     :				Optional[Any]					   =			flip_sin_to_cos

							if set_W_to_weight:
									# to delete later
									__UpperCAmelCase     :				Optional[int]					   =			nn.Parameter(torch.randn(lowerCAmelCase_						) * scale						,     requires_grad=lowerCAmelCase_						)

									__UpperCAmelCase     :				str					   =			self.W






					def 						lowerCamelCase_					(					self    :	Optional[Any]						,     UpperCAmelCase_    :	Optional[int]						):







							"""simple docstring"""


							if self.log:
									__UpperCAmelCase     :				str					   =			torch.log(lowerCAmelCase_						)

							__UpperCAmelCase     :				Any					   =			x[:, None] * self.weight[None, :] * 2 * np.pi

							if self.flip_sin_to_cos:
									__UpperCAmelCase     :				str					   =			torch.cat([torch.cos(lowerCAmelCase_						), torch.sin(lowerCAmelCase_						)]						,     dim=-1						)
							else:
									__UpperCAmelCase     :				Optional[Any]					   =			torch.cat([torch.sin(lowerCAmelCase_						), torch.cos(lowerCAmelCase_						)]						,     dim=-1						)
							return out






class 			SCREAMING_SNAKE_CASE__	(					nn.Module						):







					"""simple docstring"""






					def __init__(					self    :	Optional[int]						,     UpperCAmelCase_    :	int						,     UpperCAmelCase_    :	int						,     UpperCAmelCase_    :	int						,     UpperCAmelCase_    :	int						,     ):







							"""simple docstring"""
							super().__init__()

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

							__UpperCAmelCase     :				Optional[int]					   =			nn.Embedding(self.num_embed						,     lowerCAmelCase_						)
							__UpperCAmelCase     :				List[Any]					   =			nn.Embedding(self.height						,     lowerCAmelCase_						)
							__UpperCAmelCase     :				List[str]					   =			nn.Embedding(self.width						,     lowerCAmelCase_						)






					def 						lowerCamelCase_					(					self    :	str						,     UpperCAmelCase_    :	Any						):







							"""simple docstring"""
							__UpperCAmelCase     :				int					   =			self.emb(lowerCAmelCase_						)

							__UpperCAmelCase     :				Tuple					   =			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     :				Union[str, 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     :				Dict					   =			width_emb.unsqueeze(1						)

							__UpperCAmelCase     :				Optional[Any]					   =			height_emb + width_emb

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

							__UpperCAmelCase     :				Tuple					   =			emb + pos_emb[:, : emb.shape[1], :]

							return emb






class 			SCREAMING_SNAKE_CASE__	(					nn.Module						):







					"""simple docstring"""






					def __init__(					self    :	Any						,     UpperCAmelCase_    :	str						,     UpperCAmelCase_    :	List[Any]						,     UpperCAmelCase_    :	Tuple						):







							"""simple docstring"""
							super().__init__()
							__UpperCAmelCase     :				int					   =			dropout_prob > 0
							__UpperCAmelCase     :				Optional[int]					   =			nn.Embedding(num_classes + use_cfg_embedding						,     lowerCAmelCase_						)
							__UpperCAmelCase     :				Tuple					   =			num_classes
							__UpperCAmelCase     :				Any					   =			dropout_prob
					def 						lowerCamelCase_					(					self    :	Optional[Any]						,     UpperCAmelCase_    :	Tuple						,     UpperCAmelCase_    :	List[str]=None						):







							"""simple docstring"""


							if force_drop_ids is None:
									__UpperCAmelCase     :				Union[str, Any]					   =			torch.rand(labels.shape[0]						,     device=labels.device						) < self.dropout_prob
							else:
									__UpperCAmelCase     :				Union[str, Any]					   =			torch.tensor(force_drop_ids == 1						)
							__UpperCAmelCase     :				str					   =			torch.where(lowerCAmelCase_						,     self.num_classes						,     lowerCAmelCase_						)
							return labels






					def 						lowerCamelCase_					(					self    :	int						,     UpperCAmelCase_    :	torch.LongTensor						,     UpperCAmelCase_    :	Dict=None						):







							"""simple docstring"""
							__UpperCAmelCase     :				List[Any]					   =			self.dropout_prob > 0
							if (self.training and use_dropout) or (force_drop_ids is not None):
									__UpperCAmelCase     :				Optional[int]					   =			self.token_drop(lowerCAmelCase_						,     lowerCAmelCase_						)
							__UpperCAmelCase     :				List[Any]					   =			self.embedding_table(lowerCAmelCase_						)
							return embeddings






class 			SCREAMING_SNAKE_CASE__	(					nn.Module						):







					"""simple docstring"""






					def __init__(					self    :	Dict						,     UpperCAmelCase_    :	int = 1_024						,     UpperCAmelCase_    :	int = 768						,     UpperCAmelCase_    :	int = 768						,     UpperCAmelCase_    :	int = 10						,     ):







							"""simple docstring"""
							super().__init__()

							__UpperCAmelCase     :				str					   =			num_image_text_embeds
							__UpperCAmelCase     :				int					   =			nn.Linear(lowerCAmelCase_						,     self.num_image_text_embeds * cross_attention_dim						)
							__UpperCAmelCase     :				Dict					   =			nn.Linear(lowerCAmelCase_						,     lowerCAmelCase_						)






					def 						lowerCamelCase_					(					self    :	Tuple						,     UpperCAmelCase_    :	torch.FloatTensor						,     UpperCAmelCase_    :	torch.FloatTensor						):







							"""simple docstring"""
							__UpperCAmelCase     :				str					   =			text_embeds.shape[0]

							# image
							__UpperCAmelCase     :				int					   =			self.image_embeds(lowerCAmelCase_						)
							__UpperCAmelCase     :				int					   =			image_text_embeds.reshape(lowerCAmelCase_						,     self.num_image_text_embeds						,     -1						)

							# text
							__UpperCAmelCase     :				List[Any]					   =			self.text_proj(lowerCAmelCase_						)

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






class 			SCREAMING_SNAKE_CASE__	(					nn.Module						):







					"""simple docstring"""






					def __init__(					self    :	Union[str, Any]						,     UpperCAmelCase_    :	int = 768						,     UpperCAmelCase_    :	int = 768						,     UpperCAmelCase_    :	int = 32						,     ):







							"""simple docstring"""
							super().__init__()

							__UpperCAmelCase     :				Dict					   =			num_image_text_embeds
							__UpperCAmelCase     :				Tuple					   =			nn.Linear(lowerCAmelCase_						,     self.num_image_text_embeds * cross_attention_dim						)
							__UpperCAmelCase     :				str					   =			nn.LayerNorm(lowerCAmelCase_						)






					def 						lowerCamelCase_					(					self    :	int						,     UpperCAmelCase_    :	torch.FloatTensor						):







							"""simple docstring"""
							__UpperCAmelCase     :				Tuple					   =			image_embeds.shape[0]

							# image
							__UpperCAmelCase     :				Dict					   =			self.image_embeds(lowerCAmelCase_						)
							__UpperCAmelCase     :				str					   =			image_embeds.reshape(lowerCAmelCase_						,     self.num_image_text_embeds						,     -1						)
							__UpperCAmelCase     :				Any					   =			self.norm(lowerCAmelCase_						)
							return image_embeds






class 			SCREAMING_SNAKE_CASE__	(					nn.Module						):







					"""simple docstring"""






					def __init__(					self    :	Optional[Any]						,     UpperCAmelCase_    :	List[Any]						,     UpperCAmelCase_    :	Tuple						,     UpperCAmelCase_    :	int=0.1						):







							"""simple docstring"""
							super().__init__()

							__UpperCAmelCase     :				List[Any]					   =			Timesteps(num_channels=256						,     flip_sin_to_cos=lowerCAmelCase_						,     downscale_freq_shift=1						)
							__UpperCAmelCase     :				Tuple					   =			TimestepEmbedding(in_channels=256						,     time_embed_dim=lowerCAmelCase_						)
							__UpperCAmelCase     :				int					   =			LabelEmbedding(lowerCAmelCase_						,     lowerCAmelCase_						,     lowerCAmelCase_						)






					def 						lowerCamelCase_					(					self    :	int						,     UpperCAmelCase_    :	Optional[int]						,     UpperCAmelCase_    :	Union[str, Any]						,     UpperCAmelCase_    :	Any=None						):







							"""simple docstring"""
							__UpperCAmelCase     :				int					   =			self.time_proj(lowerCAmelCase_						)
							__UpperCAmelCase     :				Optional[int]					   =			self.timestep_embedder(timesteps_proj.to(dtype=lowerCAmelCase_						)						)  # (N, D)

							__UpperCAmelCase     :				List[str]					   =			self.class_embedder(lowerCAmelCase_						)  # (N, D)

							__UpperCAmelCase     :				Any					   =			timesteps_emb + class_labels  # (N, D)

							return conditioning






class 			SCREAMING_SNAKE_CASE__	(					nn.Module						):







					"""simple docstring"""






					def __init__(					self    :	List[Any]						,     UpperCAmelCase_    :	int						,     UpperCAmelCase_    :	int						,     UpperCAmelCase_    :	int = 64						):







							"""simple docstring"""
							super().__init__()
							__UpperCAmelCase     :				Dict					   =			nn.LayerNorm(lowerCAmelCase_						)
							__UpperCAmelCase     :				List[Any]					   =			AttentionPooling(lowerCAmelCase_						,     lowerCAmelCase_						)
							__UpperCAmelCase     :				Dict					   =			nn.Linear(lowerCAmelCase_						,     lowerCAmelCase_						)
							__UpperCAmelCase     :				int					   =			nn.LayerNorm(lowerCAmelCase_						)






					def 						lowerCamelCase_					(					self    :	int						,     UpperCAmelCase_    :	Any						):







							"""simple docstring"""
							__UpperCAmelCase     :				Dict					   =			self.norma(lowerCAmelCase_						)
							__UpperCAmelCase     :				Optional[int]					   =			self.pool(lowerCAmelCase_						)
							__UpperCAmelCase     :				int					   =			self.proj(lowerCAmelCase_						)
							__UpperCAmelCase     :				Union[str, Any]					   =			self.norma(lowerCAmelCase_						)
							return hidden_states






class 			SCREAMING_SNAKE_CASE__	(					nn.Module						):







					"""simple docstring"""






					def __init__(					self    :	Tuple						,     UpperCAmelCase_    :	int = 768						,     UpperCAmelCase_    :	int = 768						,     UpperCAmelCase_    :	int = 1_536						):







							"""simple docstring"""
							super().__init__()
							__UpperCAmelCase     :				Optional[Any]					   =			nn.Linear(lowerCAmelCase_						,     lowerCAmelCase_						)
							__UpperCAmelCase     :				int					   =			nn.LayerNorm(lowerCAmelCase_						)
							__UpperCAmelCase     :				Any					   =			nn.Linear(lowerCAmelCase_						,     lowerCAmelCase_						)






					def 						lowerCamelCase_					(					self    :	Optional[Any]						,     UpperCAmelCase_    :	torch.FloatTensor						,     UpperCAmelCase_    :	torch.FloatTensor						):







							"""simple docstring"""
							__UpperCAmelCase     :				List[Any]					   =			self.text_proj(lowerCAmelCase_						)
							__UpperCAmelCase     :				Optional[int]					   =			self.text_norm(lowerCAmelCase_						)

							# image
							__UpperCAmelCase     :				Dict					   =			self.image_proj(lowerCAmelCase_						)

							return time_image_embeds + time_text_embeds






class 			SCREAMING_SNAKE_CASE__	(					nn.Module						):







					"""simple docstring"""






					def __init__(					self    :	Optional[int]						,     UpperCAmelCase_    :	int = 768						,     UpperCAmelCase_    :	int = 1_536						):







							"""simple docstring"""
							super().__init__()
							__UpperCAmelCase     :				Optional[Any]					   =			nn.Linear(lowerCAmelCase_						,     lowerCAmelCase_						)
							__UpperCAmelCase     :				Dict					   =			nn.LayerNorm(lowerCAmelCase_						)






					def 						lowerCamelCase_					(					self    :	Any						,     UpperCAmelCase_    :	torch.FloatTensor						):







							"""simple docstring"""
							__UpperCAmelCase     :				Dict					   =			self.image_proj(lowerCAmelCase_						)
							__UpperCAmelCase     :				List[Any]					   =			self.image_norm(lowerCAmelCase_						)
							return time_image_embeds






class 			SCREAMING_SNAKE_CASE__	(					nn.Module						):







					"""simple docstring"""






					def __init__(					self    :	List[str]						,     UpperCAmelCase_    :	int = 768						,     UpperCAmelCase_    :	int = 1_536						):







							"""simple docstring"""
							super().__init__()
							__UpperCAmelCase     :				int					   =			nn.Linear(lowerCAmelCase_						,     lowerCAmelCase_						)
							__UpperCAmelCase     :				Dict					   =			nn.LayerNorm(lowerCAmelCase_						)
							__UpperCAmelCase     :				str					   =			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						,     256						,     3						,     padding=1						,     stride=2						)						,     nn.SiLU()						,     nn.Convad(256						,     4						,     3						,     padding=1						)						,     )






					def 						lowerCamelCase_					(					self    :	Tuple						,     UpperCAmelCase_    :	torch.FloatTensor						,     UpperCAmelCase_    :	torch.FloatTensor						):







							"""simple docstring"""
							__UpperCAmelCase     :				Tuple					   =			self.image_proj(lowerCAmelCase_						)
							__UpperCAmelCase     :				Union[str, Any]					   =			self.image_norm(lowerCAmelCase_						)
							__UpperCAmelCase     :				Tuple					   =			self.input_hint_block(lowerCAmelCase_						)
							return time_image_embeds, hint






class 			SCREAMING_SNAKE_CASE__	(					nn.Module						):







					"""simple docstring"""






					def __init__(					self    :	List[str]						,     UpperCAmelCase_    :	Optional[int]						,     UpperCAmelCase_    :	Optional[int]						,     UpperCAmelCase_    :	Optional[Any]=None						):







							"""simple docstring"""
							super().__init__()
							__UpperCAmelCase     :				List[str]					   =			dtype
							__UpperCAmelCase     :				List[str]					   =			nn.Parameter(torch.randn(1						,     lowerCAmelCase_						) / embed_dim**0.5						)
							__UpperCAmelCase     :				Tuple					   =			nn.Linear(lowerCAmelCase_						,     lowerCAmelCase_						,     dtype=self.dtype						)
							__UpperCAmelCase     :				Tuple					   =			nn.Linear(lowerCAmelCase_						,     lowerCAmelCase_						,     dtype=self.dtype						)
							__UpperCAmelCase     :				List[Any]					   =			nn.Linear(lowerCAmelCase_						,     lowerCAmelCase_						,     dtype=self.dtype						)
							__UpperCAmelCase     :				List[str]					   =			num_heads
							__UpperCAmelCase     :				Union[str, Any]					   =			embed_dim // self.num_heads






					def 						lowerCamelCase_					(					self    :	Optional[Any]						,     UpperCAmelCase_    :	Optional[int]						):







							"""simple docstring"""
							__UpperCAmelCase   ,       __UpperCAmelCase   ,       __UpperCAmelCase     :				Any					   =			x.size()

							def shape(UpperCAmelCase_    :	Tuple						):
									# (bs, length, width) --> (bs, length, n_heads, dim_per_head)
									__UpperCAmelCase     :				Dict					   =			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     :				Optional[Any]					   =			x.transpose(1						,     2						)
									# (bs, n_heads, length, dim_per_head) --> (bs*n_heads, length, dim_per_head)
									__UpperCAmelCase     :				Optional[int]					   =			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     :				Tuple					   =			x.transpose(1						,     2						)
									return x

							__UpperCAmelCase     :				List[str]					   =			x.mean(dim=1						,     keepdim=lowerCAmelCase_						) + self.positional_embedding.to(x.dtype						)
							__UpperCAmelCase     :				Dict					   =			torch.cat([class_token, x]						,     dim=1						)  # (bs, length+1, width)

							# (bs*n_heads, class_token_length, dim_per_head)
							__UpperCAmelCase     :				int					   =			shape(self.q_proj(lowerCAmelCase_						)						)
							# (bs*n_heads, length+class_token_length, dim_per_head)
							__UpperCAmelCase     :				Union[str, Any]					   =			shape(self.k_proj(lowerCAmelCase_						)						)
							__UpperCAmelCase     :				Dict					   =			shape(self.v_proj(lowerCAmelCase_						)						)

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

							# (bs*n_heads, dim_per_head, class_token_length)
							__UpperCAmelCase     :				Optional[int]					   =			torch.einsum("bts,bcs->bct"						,     lowerCAmelCase_						,     lowerCAmelCase_						)

							# (bs, length+1, width)
							__UpperCAmelCase     :				Optional[Any]					   =			a.reshape(lowerCAmelCase_						,     -1						,     1						).transpose(1						,     2						)

							return a[:, 0, :]  # cls_token










'''simple docstring'''


from typing import List

from packaging import version
from sklearn.metrics import fa_score

import datasets
from datasets.config import PY_VERSION


if PY_VERSION < version.parse('3.8'):
				import importlib_metadata
else:
				import importlib.metadata as importlib_metadata


# TODO(Patrick/Anton)
_snake_case      :      Optional[int]   							=		'\\n'

_snake_case      :      Optional[Any]   							=		'\\n    XTREME-S is a benchmark to evaluate universal cross-lingual speech representations in many languages.\n    XTREME-S covers four task families: speech recognition, classification, speech-to-text translation and retrieval.\n'

_snake_case      :      Union[str, Any]   							=		'\nCompute XTREME-S evaluation metric associated to each XTREME-S dataset.\nArgs:\n    predictions: list of predictions to score.\n        Each translation should be tokenized into a list of tokens.\n    references: list of lists of references for each translation.\n        Each reference should be tokenized into a list of tokens.\n    bleu_kwargs: optional dict of keywords to be passed when computing \'bleu\'.\n        Keywords include Dict can be one of \'smooth_method\', \'smooth_value\', \'force\', \'lowercase\',\n        \'tokenize\', \'use_effective_order\'.\n    wer_kwargs: optional dict of keywords to be passed when computing \'wer\' and \'cer\'.\n        Keywords include \'concatenate_texts\'.\nReturns: depending on the XTREME-S task, one or several of:\n    "accuracy": Accuracy - for \'fleurs-lang_id\', \'minds14\'\n    "f1": F1 score - for \'minds14\'\n    "wer": Word error rate - for \'mls\', \'fleurs-asr\', \'voxpopuli\', \'babel\'\n    "cer": Character error rate - for \'mls\', \'fleurs-asr\', \'voxpopuli\', \'babel\'\n    "bleu": BLEU score according to the `sacrebleu` metric - for \'covost2\'\nExamples:\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'mls\')  # \'mls\', \'voxpopuli\', \'fleurs-asr\' or \'babel\'\n    >>> references = ["it is sunny here", "paper and pen are essentials"]\n    >>> predictions = ["it\'s sunny", "paper pen are essential"]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'wer\': 0.56, \'cer\': 0.27}\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'covost2\')\n    >>> references = ["bonjour paris", "il est necessaire de faire du sport de temps en temp"]\n    >>> predictions = ["bonjour paris", "il est important de faire du sport souvent"]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'bleu\': 31.65}\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'fleurs-lang_id\')\n    >>> references = [0, 1, 0, 0, 1]\n    >>> predictions = [0, 1, 1, 0, 0]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'accuracy\': 0.6}\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'minds14\')\n    >>> references = [0, 1, 0, 0, 1]\n    >>> predictions = [0, 1, 1, 0, 0]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'f1\': 0.58, \'accuracy\': 0.6}\n'

_snake_case      :      Dict   							=		['fleurs-asr', 'mls', 'voxpopuli', 'babel', 'covost2', 'fleurs-lang_id', 'minds14']
_snake_case      :      List[str]   							=		''

try:
				from jiwer import transforms as tr

				_snake_case      :      Optional[Any]   							=		True
except ImportError:
				_snake_case      :      Any   							=		False

if _jiwer_available and version.parse(importlib_metadata.version('jiwer')) < version.parse('2.3.0'):

				class 			A    (		tr.AbstractTransform    ):







											def __init__(  self       :  Any							,			lowerCAmelCase_       :  str = " "					)			->							Dict:


																		"""simple docstring"""


																		_a	     =				sentence_delimiter







											def    __lowerCAmelCase			(  self       :  Union[str, Any]							,			lowerCAmelCase_       :  str					)			->							Tuple:


																		"""simple docstring"""


																		return list(lowerCAmelCase_					)







											def    __lowerCAmelCase			(  self       :  Optional[int]							,			lowerCAmelCase_       :  List[str]					)			->							List[str]:


																		"""simple docstring"""


																		_a	     =				[]
																		for sent_idx, sentence in enumerate(lowerCAmelCase_					):
																									chars.extend(self.process_string(lowerCAmelCase_					)					)
																									if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(lowerCAmelCase_					) - 1:
																																chars.append(self.sentence_delimiter					)
																		return chars



				_snake_case      :      List[Any]   							=		tr.Compose(
				    [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)]
				)
elif _jiwer_available:
				_snake_case      :      Dict   							=		tr.Compose(
				    [
				        tr.RemoveMultipleSpaces(),
				        tr.Strip(),
				        tr.ReduceToSingleSentence(SENTENCE_DELIMITER),
				        tr.ReduceToListOfListOfChars(),
				    ]
				)
else:
				_snake_case      :      Dict   							=		None
def 		snake_case_    (UpperCamelCase       :  Optional[Any] ,						UpperCamelCase       :  List[str]      ):





							'''simple docstring'''


							return float((preds == labels).mean()      )
def 		snake_case_    (UpperCamelCase       :  Optional[int] ,						UpperCamelCase       :  Optional[int]      ):





							'''simple docstring'''


							return {
							    "f1": float(fa_score(y_true=UpperCamelCase ,						y_pred=UpperCamelCase ,						average='''macro'''      )      ),
							    "accuracy": simple_accuracy(UpperCamelCase ,						UpperCamelCase      ),
							}
def 		snake_case_    (UpperCamelCase       :  Optional[int] ,						UpperCamelCase       :  Any ,						UpperCamelCase       :  Optional[Any]="exp" ,						UpperCamelCase       :  Optional[int]=None ,						UpperCamelCase       :  Tuple=False ,						UpperCamelCase       :  Dict=False ,						UpperCamelCase       :  List[str]=None ,						UpperCamelCase       :  Tuple=False ,						):





							'''simple docstring'''


							_a	     =				[[label] for label in labels]
							_a	     =				list(UpperCamelCase      )
							try:
														import sacrebleu as scb
							except ImportError:
														raise ValueError(
														    '''sacrebleu has to be installed in order to apply the bleu metric for covost2.'''
														    '''You can install it via `pip install sacrebleu`.'''      )

							if version.parse(scb.__version__      ) < version.parse('''1.4.12'''      ):
														raise ImportWarning(
														    '''To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn\'t match this condition.\n'''
														    '''You can install it with `pip install "sacrebleu>=1.4.12"`.'''      )

							_a	     =				len(labels[0]      )
							if any(len(UpperCamelCase      ) != references_per_prediction for refs in labels      ):
														raise ValueError('''Sacrebleu requires the same number of references for each prediction'''      )
							_a	     =				[[refs[i] for refs in labels] for i in range(UpperCamelCase      )]
							_a	     =				scb.corpus_bleu(
							    UpperCamelCase ,						UpperCamelCase ,						smooth_method=UpperCamelCase ,						smooth_value=UpperCamelCase ,						force=UpperCamelCase ,						lowercase=UpperCamelCase ,						use_effective_order=UpperCamelCase ,						**({'''tokenize''': tokenize} if tokenize else {}) ,						)
							return {"bleu": output.score}
def 		snake_case_    (UpperCamelCase       :  Optional[Any] ,						UpperCamelCase       :  Dict ,						UpperCamelCase       :  str ,						UpperCamelCase       :  List[Any]      ):





							'''simple docstring'''


							try:
														from jiwer import compute_measures
							except ImportError:
														raise ValueError(
														    f'jiwer has to be installed in order to apply the wer metric for {config_name}.'
														    '''You can install it via `pip install jiwer`.'''      )

							if concatenate_texts:
														_a	     =				compute_measures(UpperCamelCase ,						UpperCamelCase      )['''wer''']

														_a	     =				compute_measures(UpperCamelCase ,						UpperCamelCase ,						truth_transform=UpperCamelCase ,						hypothesis_transform=UpperCamelCase      )['''wer''']
														return {"wer": wer, "cer": cer}
							else:

														def compute_score(UpperCamelCase       :  Tuple ,						UpperCamelCase       :  Optional[int] ,						UpperCamelCase       :  Dict="wer"      ):
																					_a	     =				0
																					_a	     =				0
																					for prediction, reference in zip(UpperCamelCase ,						UpperCamelCase      ):
																												if score_type == "wer":
																																			_a	     =				compute_measures(UpperCamelCase ,						UpperCamelCase      )
																												elif score_type == "cer":
																																			_a	     =				compute_measures(
																																			    UpperCamelCase ,						UpperCamelCase ,						truth_transform=UpperCamelCase ,						hypothesis_transform=UpperCamelCase      )
																												incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
																												total += measures["substitutions"] + measures["deletions"] + measures["hits"]
																					return incorrect / total

														return {"wer": compute_score(UpperCamelCase ,						UpperCamelCase ,						'''wer'''      ), "cer": compute_score(UpperCamelCase ,						UpperCamelCase ,						'''cer'''      )}

@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION     ,_KWARGS_DESCRIPTION    )
class 			A    (		datasets.Metric    ):







							def    __lowerCAmelCase			(  self       :  Tuple					)			->							Dict:


														"""simple docstring"""


														if self.config_name not in _CONFIG_NAMES:
																					raise KeyError(F'You should supply a configuration name selected in {_CONFIG_NAMES}'					)

														_a	     =				'''int64''' if self.config_name in ['''fleurs-lang_id''', '''minds14'''] else '''string'''

														return datasets.MetricInfo(
														    description=_DESCRIPTION							,			citation=_CITATION							,			inputs_description=_KWARGS_DESCRIPTION							,			features=datasets.Features(
														        {'''predictions''': datasets.Value(lowerCAmelCase_					), '''references''': datasets.Value(lowerCAmelCase_					)}					)							,			codebase_urls=[]							,			reference_urls=[]							,			format='''numpy'''							,			)







							def    __lowerCAmelCase			(  self       :  Optional[int]							,			lowerCAmelCase_       :  Union[str, Any]							,			lowerCAmelCase_       :  Optional[Any]							,			lowerCAmelCase_       :  List[Any]=None							,			lowerCAmelCase_       :  int=None					)			->							Tuple:


														"""simple docstring"""


														_a	     =				bleu_kwargs if bleu_kwargs is not None else {}
														_a	     =				wer_kwargs if wer_kwargs is not None else {}

														if self.config_name == "fleurs-lang_id":
																					return {"accuracy": simple_accuracy(lowerCAmelCase_							,			lowerCAmelCase_					)}
														elif self.config_name == "minds14":
																					return fa_and_simple_accuracy(lowerCAmelCase_							,			lowerCAmelCase_					)
														elif self.config_name == "covost2":
																					_a	     =				bleu_kwargs.pop('''smooth_method'''							,			'''exp'''					)
																					_a	     =				bleu_kwargs.pop('''smooth_value'''							,			lowerCAmelCase_					)
																					_a	     =				bleu_kwargs.pop('''force'''							,			lowerCAmelCase_					)
																					_a	     =				bleu_kwargs.pop('''lowercase'''							,			lowerCAmelCase_					)
																					_a	     =				bleu_kwargs.pop('''tokenize'''							,			lowerCAmelCase_					)
																					_a	     =				bleu_kwargs.pop('''use_effective_order'''							,			lowerCAmelCase_					)
																					return bleu(
																					    preds=lowerCAmelCase_							,			labels=lowerCAmelCase_							,			smooth_method=lowerCAmelCase_							,			smooth_value=lowerCAmelCase_							,			force=lowerCAmelCase_							,			lowercase=lowerCAmelCase_							,			tokenize=lowerCAmelCase_							,			use_effective_order=lowerCAmelCase_							,			)
														elif self.config_name in ["fleurs-asr", "mls", "voxpopuli", "babel"]:
																					_a	     =				wer_kwargs.pop('''concatenate_texts'''							,			lowerCAmelCase_					)
																					return wer_and_cer(lowerCAmelCase_							,			lowerCAmelCase_							,			lowerCAmelCase_							,			self.config_name					)
														else:
																					raise KeyError(F'You should supply a configuration name selected in {_CONFIG_NAMES}'					)

