






'''simple docstring'''







from dataclasses import dataclass
from typing import Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps
from .modeling_utils import ModelMixin
from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block





@dataclass
class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_       ):
			snake_case__  =				42





class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_	,   SCREAMING_SNAKE_CASE_       ):






			@register_to_config
			def __init__( self      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			int = 6_5536  ,						__SCREAMING_SNAKE_CASE      :			Optional[int] = None  ,						__SCREAMING_SNAKE_CASE      :			int = 2  ,						__SCREAMING_SNAKE_CASE      :			int = 2  ,						__SCREAMING_SNAKE_CASE      :			int = 0  ,						__SCREAMING_SNAKE_CASE      :			str = "fourier"  ,						__SCREAMING_SNAKE_CASE      :			bool = True  ,						__SCREAMING_SNAKE_CASE      :			bool = False  ,						__SCREAMING_SNAKE_CASE      :			float = 0.0  ,						__SCREAMING_SNAKE_CASE      :			Tuple[str] = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D")  ,						__SCREAMING_SNAKE_CASE      :			Tuple[str] = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip")  ,						__SCREAMING_SNAKE_CASE      :			Tuple[str] = "UNetMidBlock1D"  ,						__SCREAMING_SNAKE_CASE      :			str = None  ,						__SCREAMING_SNAKE_CASE      :			Tuple[int] = (32, 32, 64)  ,						__SCREAMING_SNAKE_CASE      :			str = None  ,						__SCREAMING_SNAKE_CASE      :			int = 8  ,						__SCREAMING_SNAKE_CASE      :			int = 1  ,						__SCREAMING_SNAKE_CASE      :			bool = False  ,						)			->			List[str]:
							super().__init__()
							a_		:  str											=      sample_size

							# time
							if time_embedding_type == "fourier":
											a_		:  List[str]											=      GaussianFourierProjection(
											    embedding_size=8  ,						set_W_to_weight=__SCREAMING_SNAKE_CASE  ,						log=__SCREAMING_SNAKE_CASE  ,						flip_sin_to_cos=__SCREAMING_SNAKE_CASE					)
											a_		:  Any											=      2 * block_out_channels[0]
							elif time_embedding_type == "positional":
											a_		:  Dict											=      Timesteps(
											    block_out_channels[0]  ,						flip_sin_to_cos=__SCREAMING_SNAKE_CASE  ,						downscale_freq_shift=__SCREAMING_SNAKE_CASE					)
											a_		:  str											=      block_out_channels[0]

							if use_timestep_embedding:
											a_		:  str											=      block_out_channels[0] * 4
											a_		:  Any											=      TimestepEmbedding(
											    in_channels=__SCREAMING_SNAKE_CASE  ,						time_embed_dim=__SCREAMING_SNAKE_CASE  ,						act_fn=__SCREAMING_SNAKE_CASE  ,						out_dim=block_out_channels[0]  ,						)

							a_		:  Dict											=      nn.ModuleList([]					)
							a_		:  List[Any]											=      None
							a_		:  Optional[int]											=      nn.ModuleList([]					)
							a_		:  Union[str, Any]											=      None

							# down
							a_		:  str											=      in_channels
							for i, down_block_type in enumerate(__SCREAMING_SNAKE_CASE					):
											a_		:  List[Any]											=      output_channel
											a_		:  Dict											=      block_out_channels[i]

											if i == 0:
															input_channel += extra_in_channels

											a_		:  Union[str, Any]											=      i == len(__SCREAMING_SNAKE_CASE					) - 1

											a_		:  Optional[Any]											=      get_down_block(
											    __SCREAMING_SNAKE_CASE  ,						num_layers=__SCREAMING_SNAKE_CASE  ,						in_channels=__SCREAMING_SNAKE_CASE  ,						out_channels=__SCREAMING_SNAKE_CASE  ,						temb_channels=block_out_channels[0]  ,						add_downsample=not is_final_block or downsample_each_block  ,						)
											self.down_blocks.append(__SCREAMING_SNAKE_CASE					)

							# mid
							a_		:  Optional[Any]											=      get_mid_block(
							    __SCREAMING_SNAKE_CASE  ,						in_channels=block_out_channels[-1]  ,						mid_channels=block_out_channels[-1]  ,						out_channels=block_out_channels[-1]  ,						embed_dim=block_out_channels[0]  ,						num_layers=__SCREAMING_SNAKE_CASE  ,						add_downsample=__SCREAMING_SNAKE_CASE  ,						)

							# up
							a_		:  str											=      list(reversed(__SCREAMING_SNAKE_CASE					)					)
							a_		:  Union[str, Any]											=      reversed_block_out_channels[0]
							if out_block_type is None:
											a_		:  List[str]											=      out_channels
							else:
											a_		:  int											=      block_out_channels[0]

							for i, up_block_type in enumerate(__SCREAMING_SNAKE_CASE					):
											a_		:  Union[str, Any]											=      output_channel
											a_		:  Optional[Any]											=      (
											    reversed_block_out_channels[i + 1] if i < len(__SCREAMING_SNAKE_CASE					) - 1 else final_upsample_channels
											)

											a_		:  Any											=      i == len(__SCREAMING_SNAKE_CASE					) - 1

											a_		:  int											=      get_up_block(
											    __SCREAMING_SNAKE_CASE  ,						num_layers=__SCREAMING_SNAKE_CASE  ,						in_channels=__SCREAMING_SNAKE_CASE  ,						out_channels=__SCREAMING_SNAKE_CASE  ,						temb_channels=block_out_channels[0]  ,						add_upsample=not is_final_block  ,						)
											self.up_blocks.append(__SCREAMING_SNAKE_CASE					)
											a_		:  str											=      output_channel

							# out
							a_		:  Dict											=      norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4  ,						32					)
							a_		:  int											=      get_out_block(
							    out_block_type=__SCREAMING_SNAKE_CASE  ,						num_groups_out=__SCREAMING_SNAKE_CASE  ,						embed_dim=block_out_channels[0]  ,						out_channels=__SCREAMING_SNAKE_CASE  ,						act_fn=__SCREAMING_SNAKE_CASE  ,						fc_dim=block_out_channels[-1] // 4  ,						)







			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			torch.FloatTensor  ,						__SCREAMING_SNAKE_CASE      :			Union[torch.Tensor, float, int]  ,						__SCREAMING_SNAKE_CASE      :			bool = True  ,						)			->			Union[UNetaDOutput, Tuple]:
							a_		:  List[str]											=      timestep
							if not torch.is_tensor(__SCREAMING_SNAKE_CASE					):
											a_		:  Any											=      torch.tensor([timesteps]  ,						dtype=torch.long  ,						device=sample.device					)
							elif torch.is_tensor(__SCREAMING_SNAKE_CASE					) and len(timesteps.shape					) == 0:
											a_		:  Tuple											=      timesteps[None].to(sample.device					)

							a_		:  Any											=      self.time_proj(__SCREAMING_SNAKE_CASE					)
							if self.config.use_timestep_embedding:
											a_		:  List[str]											=      self.time_mlp(__SCREAMING_SNAKE_CASE					)
							else:
											a_		:  Any											=      timestep_embed[..., None]
											a_		:  str											=      timestep_embed.repeat([1, 1, sample.shape[2]]					).to(sample.dtype					)
											a_		:  Any											=      timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:])					)

							# 2. down
							a_		:  Tuple											=      ()
							for downsample_block in self.down_blocks:
											a_				,			a_		:  Any											=      downsample_block(hidden_states=__SCREAMING_SNAKE_CASE  ,						temb=__SCREAMING_SNAKE_CASE					)
											down_block_res_samples += res_samples

							# 3. mid
							if self.mid_block:
											a_		:  Tuple											=      self.mid_block(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)

							# 4. up
							for i, upsample_block in enumerate(self.up_blocks					):
											a_		:  Optional[Any]											=      down_block_res_samples[-1:]
											a_		:  Any											=      down_block_res_samples[:-1]
											a_		:  Optional[Any]											=      upsample_block(__SCREAMING_SNAKE_CASE  ,						res_hidden_states_tuple=__SCREAMING_SNAKE_CASE  ,						temb=__SCREAMING_SNAKE_CASE					)

							# 5. post-process
							if self.out_block:
											a_		:  List[Any]											=      self.out_block(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)

							if not return_dict:
											return (sample,)

							return UNetaDOutput(sample=__SCREAMING_SNAKE_CASE					)












'''simple docstring'''







import re
import string

import numpy as np

import datasets


__lowerCAmelCase       		=      '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n'

__lowerCAmelCase       		=      '\nArgs:\n    predictions: List of predicted texts.\n    references: List of reference texts.\n    regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n        ignore when calculating the exact matches. Note: these regexes are removed\n        from the input data before the changes based on the options below (e.g. ignore_case,\n        ignore_punctuation, ignore_numbers) are applied.\n    ignore_case: Boolean, defaults to False. If true, turns everything\n        to lowercase so that capitalization differences are ignored.\n    ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\n    ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\nReturns:\n    exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results["exact_match"], 1))\n    25.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results["exact_match"], 1))\n    50.0\n\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results["exact_match"], 1))\n    75.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n    >>> print(round(results["exact_match"], 1))\n    100.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n    >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results["exact_match"], 1))\n    33.3\n\n'

__lowerCAmelCase       		=      '\n'





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






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			int:
							return datasets.MetricInfo(
							    description=_DESCRIPTION  ,						citation=_CITATION  ,						inputs_description=_KWARGS_DESCRIPTION  ,						features=datasets.Features(
							        {
							            '''predictions''': datasets.Value('''string'''  ,						id='''sequence'''					),
							            '''references''': datasets.Value('''string'''  ,						id='''sequence'''					),
							        }					)  ,						reference_urls=[]  ,						)







			def 			SCREAMING_SNAKE_CASE      ( self      :			str  ,						__SCREAMING_SNAKE_CASE      :			List[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Dict=None  ,						__SCREAMING_SNAKE_CASE      :			int=False  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=False  ,						__SCREAMING_SNAKE_CASE      :			Dict=False  ,						)			->			str:
							if regexes_to_ignore is not None:
											for s in regexes_to_ignore:
															a_		:  Optional[Any]											=      np.array([re.sub(__SCREAMING_SNAKE_CASE  ,						''''''  ,						__SCREAMING_SNAKE_CASE					) for x in predictions]					)
															a_		:  int											=      np.array([re.sub(__SCREAMING_SNAKE_CASE  ,						''''''  ,						__SCREAMING_SNAKE_CASE					) for x in references]					)
							else:
											a_		:  List[str]											=      np.asarray(__SCREAMING_SNAKE_CASE					)
											a_		:  Any											=      np.asarray(__SCREAMING_SNAKE_CASE					)

							if ignore_case:
											a_		:  List[str]											=      np.char.lower(__SCREAMING_SNAKE_CASE					)
											a_		:  List[Any]											=      np.char.lower(__SCREAMING_SNAKE_CASE					)

							if ignore_punctuation:
											a_		:  Any											=      string.punctuation.maketrans(''''''  ,						''''''  ,						string.punctuation					)
											a_		:  Union[str, Any]											=      np.char.translate(__SCREAMING_SNAKE_CASE  ,						table=__SCREAMING_SNAKE_CASE					)
											a_		:  int											=      np.char.translate(__SCREAMING_SNAKE_CASE  ,						table=__SCREAMING_SNAKE_CASE					)

							if ignore_numbers:
											a_		:  int											=      string.digits.maketrans(''''''  ,						''''''  ,						string.digits					)
											a_		:  Optional[int]											=      np.char.translate(__SCREAMING_SNAKE_CASE  ,						table=__SCREAMING_SNAKE_CASE					)
											a_		:  Dict											=      np.char.translate(__SCREAMING_SNAKE_CASE  ,						table=__SCREAMING_SNAKE_CASE					)

							a_		:  Optional[Any]											=      predictions == references

							return {"exact_match": np.mean(__SCREAMING_SNAKE_CASE					) * 100}




