


'''simple docstring'''






from typing import Dict, List, Optional

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


__UpperCAmelCase       =							logging.get_logger(__name__)


__UpperCAmelCase       =							{
    '''nielsr/canine-s''': 2_048,
}

# Unicode defines 1,114,112 total “codepoints”
__UpperCAmelCase       =							1_114_112

# Below: Constants defining canonical codepoints for special, pseudo-characters.
# Copied from https://github.com/google-research/language/blob/master/language/canine/special_codepoints.py
__UpperCAmelCase       =							0

__UpperCAmelCase       =							0xE000
__UpperCAmelCase       =							0xE001
__UpperCAmelCase       =							0xE002
__UpperCAmelCase       =							0xE003
__UpperCAmelCase       =							0xE004

# Maps special codepoints to human-readable names.
__UpperCAmelCase       =							{
    # Special symbols are represented using codepoints values that are valid,
    # but designated as "Private Use", meaning that they will never be assigned
    # characters by the Unicode Consortium, and are thus safe for use here.
    #
    # NOTE: Do *NOT* add any sort of [UNK_CHAR] here. They are explicitly
    # excluded and should fail with a hard error.
    CLS: "[CLS]",
    SEP: "[SEP]",
    BOS: "[BOS]",
    MASK: "[MASK]",
    PAD: "[PAD]",
    RESERVED: "[RESERVED]",
}

# Maps special codepoint human-readable names to their codepoint values.
__UpperCAmelCase       =							{name: codepoint for codepoint, name in SPECIAL_CODEPOINTS.items()}




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Optional[int]     						=				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES







							def __init__(		self ,   lowerCamelCase_=chr(lowerCamelCase_       ) ,   lowerCamelCase_=chr(lowerCamelCase_       ) ,   lowerCamelCase_=chr(lowerCamelCase_       ) ,   lowerCamelCase_=chr(lowerCamelCase_       ) ,   lowerCamelCase_=chr(lowerCamelCase_       ) ,   lowerCamelCase_=chr(lowerCamelCase_       ) ,   lowerCamelCase_=False ,   lowerCamelCase_=20_48 ,   **lowerCamelCase_ ,   )	->							List[str]:
														lowerCAmelCase__	       =     AddedToken(lowerCamelCase_ ,   lstrip=lowerCamelCase_ ,   rstrip=lowerCamelCase_       ) if isinstance(lowerCamelCase_ ,   lowerCamelCase_       ) else bos_token
														lowerCAmelCase__	       =     AddedToken(lowerCamelCase_ ,   lstrip=lowerCamelCase_ ,   rstrip=lowerCamelCase_       ) if isinstance(lowerCamelCase_ ,   lowerCamelCase_       ) else eos_token
														lowerCAmelCase__	       =     AddedToken(lowerCamelCase_ ,   lstrip=lowerCamelCase_ ,   rstrip=lowerCamelCase_       ) if isinstance(lowerCamelCase_ ,   lowerCamelCase_       ) else sep_token
														lowerCAmelCase__	       =     AddedToken(lowerCamelCase_ ,   lstrip=lowerCamelCase_ ,   rstrip=lowerCamelCase_       ) if isinstance(lowerCamelCase_ ,   lowerCamelCase_       ) else cls_token
														lowerCAmelCase__	       =     AddedToken(lowerCamelCase_ ,   lstrip=lowerCamelCase_ ,   rstrip=lowerCamelCase_       ) if isinstance(lowerCamelCase_ ,   lowerCamelCase_       ) else pad_token

														# Mask token behave like a normal word, i.e. include the space before it
														lowerCAmelCase__	       =     AddedToken(lowerCamelCase_ ,   lstrip=lowerCamelCase_ ,   rstrip=lowerCamelCase_       ) if isinstance(lowerCamelCase_ ,   lowerCamelCase_       ) else mask_token

														super().__init__(
														    bos_token=lowerCamelCase_ ,   eos_token=lowerCamelCase_ ,   sep_token=lowerCamelCase_ ,   cls_token=lowerCamelCase_ ,   pad_token=lowerCamelCase_ ,   mask_token=lowerCamelCase_ ,   add_prefix_space=lowerCamelCase_ ,   model_max_length=lowerCamelCase_ ,   **lowerCamelCase_ ,   )

														# Creates a mapping for looking up the IDs of special symbols.
														lowerCAmelCase__	       =     {}
														for codepoint, name in SPECIAL_CODEPOINTS.items():
																					lowerCAmelCase__	       =     codepoint

														# Creates a mapping for looking up the string forms of special symbol IDs.
														lowerCAmelCase__	       =     {
														    codepoint: name for name, codepoint in self._special_codepoints.items()
														}

														lowerCAmelCase__	       =     UNICODE_VOCAB_SIZE
														lowerCAmelCase__	       =     len(self._special_codepoints       )







							@property
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							int:
														return self._unicode_vocab_size







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_       )	->							List[str]:
														return list(lowerCamelCase_       )







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_       )	->							int:
														try:
																					return ord(lowerCamelCase_       )
														except TypeError:
																					raise ValueError(F"""invalid token: '{token}'"""       )







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_       )	->							str:
														try:
																					if index in SPECIAL_CODEPOINTS:
																												return SPECIAL_CODEPOINTS[index]
																					return chr(lowerCamelCase_       )
														except TypeError:
																					raise ValueError(F"""invalid id: {index}"""       )







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_       )	->							Tuple:
														return "".join(lowerCamelCase_       )







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_ ,   lowerCamelCase_ = None       )	->							List[int]:
														lowerCAmelCase__	       =     [self.sep_token_id]
														lowerCAmelCase__	       =     [self.cls_token_id]

														lowerCAmelCase__	       =     cls + token_ids_a + sep
														if token_ids_a is not None:
																					result += token_ids_a + sep
														return result







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_ ,   lowerCamelCase_ = None ,   lowerCamelCase_ = False       )	->							List[int]:
														if already_has_special_tokens:
																					return super().get_special_tokens_mask(
																					    token_ids_a=lowerCamelCase_ ,   token_ids_a=lowerCamelCase_ ,   already_has_special_tokens=lowerCamelCase_       )

														lowerCAmelCase__	       =     [1] + ([0] * len(lowerCamelCase_       )) + [1]
														if token_ids_a is not None:
																					result += ([0] * len(lowerCamelCase_       )) + [1]
														return result







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_ ,   lowerCamelCase_ = None       )	->							List[int]:
														lowerCAmelCase__	       =     [self.sep_token_id]
														lowerCAmelCase__	       =     [self.cls_token_id]

														lowerCAmelCase__	       =     len(cls + token_ids_a + sep       ) * [0]
														if token_ids_a is not None:
																					result += len(token_ids_a + sep       ) * [1]
														return result







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_ ,   lowerCamelCase_ = None       )	->							List[Any]:
														return ()



'''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 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       torch.FloatTensor




class 		a__    (							a__    ,    a__  ):





							'''simple docstring'''






							@register_to_config
							def __init__(		self ,   lowerCamelCase_ = 6_55_36 ,   lowerCamelCase_ = None ,   lowerCamelCase_ = 2 ,   lowerCamelCase_ = 2 ,   lowerCamelCase_ = 0 ,   lowerCamelCase_ = "fourier" ,   lowerCamelCase_ = True ,   lowerCamelCase_ = False ,   lowerCamelCase_ = 0.0 ,   lowerCamelCase_ = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") ,   lowerCamelCase_ = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") ,   lowerCamelCase_ = "UNetMidBlock1D" ,   lowerCamelCase_ = None ,   lowerCamelCase_ = (32, 32, 64) ,   lowerCamelCase_ = None ,   lowerCamelCase_ = 8 ,   lowerCamelCase_ = 1 ,   lowerCamelCase_ = False ,   )	->							Optional[int]:
														super().__init__()
														lowerCAmelCase__	       =     sample_size

														# time
														if time_embedding_type == "fourier":
																					lowerCAmelCase__	       =     GaussianFourierProjection(
																					    embedding_size=8 ,   set_W_to_weight=lowerCamelCase_ ,   log=lowerCamelCase_ ,   flip_sin_to_cos=lowerCamelCase_       )
																					lowerCAmelCase__	       =     2 * block_out_channels[0]
														elif time_embedding_type == "positional":
																					lowerCAmelCase__	       =     Timesteps(
																					    block_out_channels[0] ,   flip_sin_to_cos=lowerCamelCase_ ,   downscale_freq_shift=lowerCamelCase_       )
																					lowerCAmelCase__	       =     block_out_channels[0]

														if use_timestep_embedding:
																					lowerCAmelCase__	       =     block_out_channels[0] * 4
																					lowerCAmelCase__	       =     TimestepEmbedding(
																					    in_channels=lowerCamelCase_ ,   time_embed_dim=lowerCamelCase_ ,   act_fn=lowerCamelCase_ ,   out_dim=block_out_channels[0] ,   )

														lowerCAmelCase__	       =     nn.ModuleList([]       )
														lowerCAmelCase__	       =     None
														lowerCAmelCase__	       =     nn.ModuleList([]       )
														lowerCAmelCase__	       =     None

														# down
														lowerCAmelCase__	       =     in_channels
														for i, down_block_type in enumerate(lowerCamelCase_       ):
																					lowerCAmelCase__	       =     output_channel
																					lowerCAmelCase__	       =     block_out_channels[i]

																					if i == 0:
																												input_channel += extra_in_channels

																					lowerCAmelCase__	       =     i == len(lowerCamelCase_       ) - 1

																					lowerCAmelCase__	       =     get_down_block(
																					    lowerCamelCase_ ,   num_layers=lowerCamelCase_ ,   in_channels=lowerCamelCase_ ,   out_channels=lowerCamelCase_ ,   temb_channels=block_out_channels[0] ,   add_downsample=not is_final_block or downsample_each_block ,   )
																					self.down_blocks.append(lowerCamelCase_       )

														# mid
														lowerCAmelCase__	       =     get_mid_block(
														    lowerCamelCase_ ,   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=lowerCamelCase_ ,   add_downsample=lowerCamelCase_ ,   )

														# up
														lowerCAmelCase__	       =     list(reversed(lowerCamelCase_       )       )
														lowerCAmelCase__	       =     reversed_block_out_channels[0]
														if out_block_type is None:
																					lowerCAmelCase__	       =     out_channels
														else:
																					lowerCAmelCase__	       =     block_out_channels[0]

														for i, up_block_type in enumerate(lowerCamelCase_       ):
																					lowerCAmelCase__	       =     output_channel
																					lowerCAmelCase__	       =     (
																					    reversed_block_out_channels[i + 1] if i < len(lowerCamelCase_       ) - 1 else final_upsample_channels
																					)

																					lowerCAmelCase__	       =     i == len(lowerCamelCase_       ) - 1

																					lowerCAmelCase__	       =     get_up_block(
																					    lowerCamelCase_ ,   num_layers=lowerCamelCase_ ,   in_channels=lowerCamelCase_ ,   out_channels=lowerCamelCase_ ,   temb_channels=block_out_channels[0] ,   add_upsample=not is_final_block ,   )
																					self.up_blocks.append(lowerCamelCase_       )
																					lowerCAmelCase__	       =     output_channel

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







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_ ,   lowerCamelCase_ ,   lowerCamelCase_ = True ,   )	->							Union[UNetaDOutput, Tuple]:
														lowerCAmelCase__	       =     timestep
														if not torch.is_tensor(lowerCamelCase_       ):
																					lowerCAmelCase__	       =     torch.tensor([timesteps] ,   dtype=torch.long ,   device=sample.device       )
														elif torch.is_tensor(lowerCamelCase_       ) and len(timesteps.shape       ) == 0:
																					lowerCAmelCase__	       =     timesteps[None].to(sample.device       )

														lowerCAmelCase__	       =     self.time_proj(lowerCamelCase_       )
														if self.config.use_timestep_embedding:
																					lowerCAmelCase__	       =     self.time_mlp(lowerCamelCase_       )
														else:
																					lowerCAmelCase__	       =     timestep_embed[..., None]
																					lowerCAmelCase__	       =     timestep_embed.repeat([1, 1, sample.shape[2]]       ).to(sample.dtype       )
																					lowerCAmelCase__	       =     timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:])       )

														# 2. down
														lowerCAmelCase__	       =     ()
														for downsample_block in self.down_blocks:
																					lowerCAmelCase__       , lowerCAmelCase__	       =     downsample_block(hidden_states=lowerCamelCase_ ,   temb=lowerCamelCase_       )
																					down_block_res_samples += res_samples

														# 3. mid
														if self.mid_block:
																					lowerCAmelCase__	       =     self.mid_block(lowerCamelCase_ ,   lowerCamelCase_       )

														# 4. up
														for i, upsample_block in enumerate(self.up_blocks       ):
																					lowerCAmelCase__	       =     down_block_res_samples[-1:]
																					lowerCAmelCase__	       =     down_block_res_samples[:-1]
																					lowerCAmelCase__	       =     upsample_block(lowerCamelCase_ ,   res_hidden_states_tuple=lowerCamelCase_ ,   temb=lowerCamelCase_       )

														# 5. post-process
														if self.out_block:
																					lowerCAmelCase__	       =     self.out_block(lowerCamelCase_ ,   lowerCamelCase_       )

														if not return_dict:
																					return (sample,)

														return UNetaDOutput(sample=lowerCamelCase_       )