




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 UNetMidBlockaD, get_down_block, get_up_block







@dataclass
class __snake_case     (     a						):
   UpperCAmelCase__    :       torch.FloatTensor







class __snake_case     (     a					,    a						):







   @register_to_config
   def __init__( self						:		List[Any]   ,		_snake_case						:		Optional[Union[int, Tuple[int, int]]] = None   ,		_snake_case						:		int = 3   ,		_snake_case						:		int = 3   ,		_snake_case						:		bool = False   ,		_snake_case						:		str = "positional"   ,		_snake_case						:		int = 0   ,		_snake_case						:		bool = True   ,		_snake_case						:		Tuple[str] = ("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D")   ,		_snake_case						:		Tuple[str] = ("AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "UpBlock2D")   ,		_snake_case						:		Tuple[int] = (224, 448, 672, 896)   ,		_snake_case						:		int = 2   ,		_snake_case						:		float = 1   ,		_snake_case						:		int = 1   ,		_snake_case						:		str = "conv"   ,		_snake_case						:		str = "conv"   ,		_snake_case						:		str = "silu"   ,		_snake_case						:		Optional[int] = 8   ,		_snake_case						:		int = 32   ,		_snake_case						:		float = 1e-5   ,		_snake_case						:		str = "default"   ,		_snake_case						:		bool = True   ,		_snake_case						:		Optional[str] = None   ,		_snake_case						:		Optional[int] = None   ,		):

        """simple docstring"""


        super().__init__()

        UpperCAmelCase_		=				sample_size
        UpperCAmelCase_		=				block_out_channels[0] * 4

        # Check inputs
        if len(_snake_case) != len(_snake_case):
             raise ValueError(
                 F"""Must provide the same number of `down_block_types` as `up_block_types`. `down_block_types`: {down_block_types}. `up_block_types`: {up_block_types}.""")

        if len(_snake_case) != len(_snake_case):
             raise ValueError(
                 F"""Must provide the same number of `block_out_channels` as `down_block_types`. `block_out_channels`: {block_out_channels}. `down_block_types`: {down_block_types}.""")

        # input
        UpperCAmelCase_		=				nn.Convad(_snake_case   ,		block_out_channels[0]   ,		kernel_size=3   ,		padding=(1, 1))

        # time
        if time_embedding_type == "fourier":
             UpperCAmelCase_		=				GaussianFourierProjection(embedding_size=block_out_channels[0]   ,		scale=16)
             UpperCAmelCase_		=				2 * block_out_channels[0]
        elif time_embedding_type == "positional":
             UpperCAmelCase_		=				Timesteps(block_out_channels[0]   ,		_snake_case   ,		_snake_case)
             UpperCAmelCase_		=				block_out_channels[0]

        UpperCAmelCase_		=				TimestepEmbedding(_snake_case   ,		_snake_case)

        # class embedding
        if class_embed_type is None and num_class_embeds is not None:
             UpperCAmelCase_		=				nn.Embedding(_snake_case   ,		_snake_case)
        elif class_embed_type == "timestep":
             UpperCAmelCase_		=				TimestepEmbedding(_snake_case   ,		_snake_case)
        elif class_embed_type == "identity":
             UpperCAmelCase_		=				nn.Identity(_snake_case   ,		_snake_case)
        else:
             UpperCAmelCase_		=				None

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

        # down
        UpperCAmelCase_		=				block_out_channels[0]
        for i, down_block_type in enumerate(_snake_case):
             UpperCAmelCase_		=				output_channel
             UpperCAmelCase_		=				block_out_channels[i]
             UpperCAmelCase_		=				i == len(_snake_case) - 1

             UpperCAmelCase_		=				get_down_block(
                 _snake_case   ,		num_layers=_snake_case   ,		in_channels=_snake_case   ,		out_channels=_snake_case   ,		temb_channels=_snake_case   ,		add_downsample=not is_final_block   ,		resnet_eps=_snake_case   ,		resnet_act_fn=_snake_case   ,		resnet_groups=_snake_case   ,		attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel   ,		downsample_padding=_snake_case   ,		resnet_time_scale_shift=_snake_case   ,		downsample_type=_snake_case   ,		)
             self.down_blocks.append(_snake_case)

        # mid
        UpperCAmelCase_		=				UNetMidBlockaD(
            in_channels=block_out_channels[-1]   ,		temb_channels=_snake_case   ,		resnet_eps=_snake_case   ,		resnet_act_fn=_snake_case   ,		output_scale_factor=_snake_case   ,		resnet_time_scale_shift=_snake_case   ,		attention_head_dim=attention_head_dim if attention_head_dim is not None else block_out_channels[-1]   ,		resnet_groups=_snake_case   ,		add_attention=_snake_case   ,		)

        # up
        UpperCAmelCase_		=				list(reversed(_snake_case))
        UpperCAmelCase_		=				reversed_block_out_channels[0]
        for i, up_block_type in enumerate(_snake_case):
             UpperCAmelCase_		=				output_channel
             UpperCAmelCase_		=				reversed_block_out_channels[i]
             UpperCAmelCase_		=				reversed_block_out_channels[min(i + 1   ,		len(_snake_case) - 1)]

             UpperCAmelCase_		=				i == len(_snake_case) - 1

             UpperCAmelCase_		=				get_up_block(
                 _snake_case   ,		num_layers=layers_per_block + 1   ,		in_channels=_snake_case   ,		out_channels=_snake_case   ,		prev_output_channel=_snake_case   ,		temb_channels=_snake_case   ,		add_upsample=not is_final_block   ,		resnet_eps=_snake_case   ,		resnet_act_fn=_snake_case   ,		resnet_groups=_snake_case   ,		attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel   ,		resnet_time_scale_shift=_snake_case   ,		upsample_type=_snake_case   ,		)
             self.up_blocks.append(_snake_case)
             UpperCAmelCase_		=				output_channel

        # out
        UpperCAmelCase_		=				norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4   ,		32)
        UpperCAmelCase_		=				nn.GroupNorm(num_channels=block_out_channels[0]   ,		num_groups=_snake_case   ,		eps=_snake_case)
        UpperCAmelCase_		=				nn.SiLU()
        UpperCAmelCase_		=				nn.Convad(block_out_channels[0]   ,		_snake_case   ,		kernel_size=3   ,		padding=1)







   def lowerCamelCase		( self						:		str   ,		_snake_case						:		torch.FloatTensor   ,		_snake_case						:		Union[torch.Tensor, float, int]   ,		_snake_case						:		Optional[torch.Tensor] = None   ,		_snake_case						:		bool = True   ,		):

        """simple docstring"""


        if self.config.center_input_sample:
             UpperCAmelCase_		=				2 * sample - 1.0

        # 1. time
        UpperCAmelCase_		=				timestep
        if not torch.is_tensor(_snake_case):
             UpperCAmelCase_		=				torch.tensor([timesteps]   ,		dtype=torch.long   ,		device=sample.device)
        elif torch.is_tensor(_snake_case) and len(timesteps.shape) == 0:
             UpperCAmelCase_		=				timesteps[None].to(sample.device)

        # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
        UpperCAmelCase_		=				timesteps * torch.ones(sample.shape[0]   ,		dtype=timesteps.dtype   ,		device=timesteps.device)

        UpperCAmelCase_		=				self.time_proj(_snake_case)

        # timesteps does not contain any weights and will always return f32 tensors
        # but time_embedding might actually be running in fp16. so we need to cast here.
        # there might be better ways to encapsulate this.
        UpperCAmelCase_		=				t_emb.to(dtype=self.dtype)
        UpperCAmelCase_		=				self.time_embedding(_snake_case)

        if self.class_embedding is not None:
             if class_labels is None:
                  raise ValueError('''class_labels should be provided when doing class conditioning''')

             if self.config.class_embed_type == "timestep":
                  UpperCAmelCase_		=				self.time_proj(_snake_case)

             UpperCAmelCase_		=				self.class_embedding(_snake_case).to(dtype=self.dtype)
             UpperCAmelCase_		=				emb + class_emb

        # 2. pre-process
        UpperCAmelCase_		=				sample
        UpperCAmelCase_		=				self.conv_in(_snake_case)

        # 3. down
        UpperCAmelCase_		=				(sample,)
        for downsample_block in self.down_blocks:
             if hasattr(_snake_case   ,		'''skip_conv'''):
                  UpperCAmelCase_	,			UpperCAmelCase_	,			UpperCAmelCase_		=				downsample_block(
                      hidden_states=_snake_case   ,		temb=_snake_case   ,		skip_sample=_snake_case)
             else:
                  UpperCAmelCase_	,			UpperCAmelCase_		=				downsample_block(hidden_states=_snake_case   ,		temb=_snake_case)

             down_block_res_samples += res_samples

        # 4. mid
        UpperCAmelCase_		=				self.mid_block(_snake_case   ,		_snake_case)

        # 5. up
        UpperCAmelCase_		=				None
        for upsample_block in self.up_blocks:
             UpperCAmelCase_		=				down_block_res_samples[-len(upsample_block.resnets) :]
             UpperCAmelCase_		=				down_block_res_samples[: -len(upsample_block.resnets)]

             if hasattr(_snake_case   ,		'''skip_conv'''):
                  UpperCAmelCase_	,			UpperCAmelCase_		=				upsample_block(_snake_case   ,		_snake_case   ,		_snake_case   ,		_snake_case)
             else:
                  UpperCAmelCase_		=				upsample_block(_snake_case   ,		_snake_case   ,		_snake_case)

        # 6. post-process
        UpperCAmelCase_		=				self.conv_norm_out(_snake_case)
        UpperCAmelCase_		=				self.conv_act(_snake_case)
        UpperCAmelCase_		=				self.conv_out(_snake_case)

        if skip_sample is not None:
             sample += skip_sample

        if self.config.time_embedding_type == "fourier":
             UpperCAmelCase_		=				timesteps.reshape((sample.shape[0], *([1] * len(sample.shape[1:]))))
             UpperCAmelCase_		=				sample / timesteps

        if not return_dict:
             return (sample,)

        return UNetaDOutput(sample=_snake_case)












import os
from typing import List, Union

import tensorflow as tf
from tensorflow_text import BertTokenizer as BertTokenizerLayer
from tensorflow_text import FastBertTokenizer, ShrinkLongestTrimmer, case_fold_utfa, combine_segments, pad_model_inputs

from .tokenization_bert import BertTokenizer







class __snake_case     (     tf.keras.layers.Layer						):







   def __init__( self						:		Optional[int]   ,		_snake_case						:		List   ,		_snake_case						:		bool   ,		_snake_case						:		int = None   ,		_snake_case						:		int = None   ,		_snake_case						:		int = None   ,		_snake_case						:		str = "longest"   ,		_snake_case						:		bool = True   ,		_snake_case						:		int = 512   ,		_snake_case						:		int = None   ,		_snake_case						:		bool = True   ,		_snake_case						:		bool = True   ,		_snake_case						:		bool = True   ,		**_snake_case						:		Any   ,		):

        """simple docstring"""


        super().__init__()
        if use_fast_bert_tokenizer:
             UpperCAmelCase_		=				FastBertTokenizer(
                 _snake_case   ,		token_out_type=tf.intaa   ,		lower_case_nfd_strip_accents=_snake_case   ,		**_snake_case)
        else:
             UpperCAmelCase_		=				tf.lookup.StaticVocabularyTable(
                 tf.lookup.KeyValueTensorInitializer(
                     keys=_snake_case   ,		key_dtype=tf.string   ,		values=tf.range(tf.size(_snake_case   ,		out_type=tf.intaa)   ,		dtype=tf.intaa)   ,		value_dtype=tf.intaa   ,		)   ,		num_oov_buckets=1   ,		)
             UpperCAmelCase_		=				BertTokenizerLayer(
                 _snake_case   ,		token_out_type=tf.intaa   ,		lower_case=_snake_case   ,		**_snake_case)

        UpperCAmelCase_		=				vocab_list
        UpperCAmelCase_		=				do_lower_case
        UpperCAmelCase_		=				cls_token_id or vocab_list.index('''[CLS]''')
        UpperCAmelCase_		=				sep_token_id or vocab_list.index('''[SEP]''')
        UpperCAmelCase_		=				pad_token_id or vocab_list.index('''[PAD]''')
        UpperCAmelCase_		=				ShrinkLongestTrimmer(max_length - 3   ,		axis=1)  # Allow room for special tokens
        UpperCAmelCase_		=				max_length
        UpperCAmelCase_		=				padding
        UpperCAmelCase_		=				truncation
        UpperCAmelCase_		=				pad_to_multiple_of
        UpperCAmelCase_		=				return_token_type_ids
        UpperCAmelCase_		=				return_attention_mask







   @classmethod
   def lowerCamelCase		( cls						:		Optional[Any]   ,		_snake_case						:		"PreTrainedTokenizerBase"   ,		**_snake_case						:		str):  # noqa: F821

        """simple docstring"""


        UpperCAmelCase_		=				kwargs.pop('''do_lower_case'''   ,		_snake_case)
        UpperCAmelCase_		=				tokenizer.do_lower_case if do_lower_case is None else do_lower_case
        UpperCAmelCase_		=				kwargs.pop('''cls_token_id'''   ,		_snake_case)
        UpperCAmelCase_		=				tokenizer.cls_token_id if cls_token_id is None else cls_token_id
        UpperCAmelCase_		=				kwargs.pop('''sep_token_id'''   ,		_snake_case)
        UpperCAmelCase_		=				tokenizer.sep_token_id if sep_token_id is None else sep_token_id
        UpperCAmelCase_		=				kwargs.pop('''pad_token_id'''   ,		_snake_case)
        UpperCAmelCase_		=				tokenizer.pad_token_id if pad_token_id is None else pad_token_id

        UpperCAmelCase_		=				tokenizer.get_vocab()
        UpperCAmelCase_		=				sorted([(wordpiece, idx) for wordpiece, idx in vocab.items()]   ,		key=lambda _snake_case: x[1])
        UpperCAmelCase_		=				[entry[0] for entry in vocab]
        return cls(
            vocab_list=_snake_case   ,		do_lower_case=_snake_case   ,		cls_token_id=_snake_case   ,		sep_token_id=_snake_case   ,		pad_token_id=_snake_case   ,		**_snake_case   ,		)







   @classmethod
   def lowerCamelCase		( cls						:		List[str]   ,		_snake_case						:		Union[str, os.PathLike]   ,		*_snake_case						:		Union[str, Any]   ,		**_snake_case						:		Union[str, Any]):

        """simple docstring"""


        try:
             UpperCAmelCase_		=				BertTokenizer.from_pretrained(_snake_case   ,		*_snake_case   ,		**_snake_case)
        except:  # noqa: E722
             from .tokenization_bert_fast import BertTokenizerFast

             UpperCAmelCase_		=				BertTokenizerFast.from_pretrained(_snake_case   ,		*_snake_case   ,		**_snake_case)
        return cls.from_tokenizer(_snake_case   ,		**_snake_case)







   def lowerCamelCase		( self						:		int   ,		_snake_case						:		str):

        """simple docstring"""


        if self.do_lower_case:
             UpperCAmelCase_		=				case_fold_utfa(_snake_case)
        UpperCAmelCase_		=				self.tf_tokenizer.tokenize(_snake_case)
        return tokens.merge_dims(1   ,		-1)







   def lowerCamelCase		( self						:		int   ,		_snake_case						:		str   ,		_snake_case						:		str=None   ,		_snake_case						:		Union[str, Any]=None   ,		_snake_case						:		str=None   ,		_snake_case						:		int=None   ,		_snake_case						:		Optional[int]=None   ,		_snake_case						:		Optional[Any]=None   ,		_snake_case						:		List[str]=None   ,		):

        """simple docstring"""


        if padding is None:
             UpperCAmelCase_		=				self.padding
        if padding not in ("longest", "max_length"):
             raise ValueError('''Padding must be either \'longest\' or \'max_length\'!''')
        if max_length is not None and text_pair is not None:
             # Because we have to instantiate a Trimmer to do it properly
             raise ValueError('''max_length cannot be overridden at call time when truncating paired texts!''')
        if max_length is None:
             UpperCAmelCase_		=				self.max_length
        if truncation is None:
             UpperCAmelCase_		=				self.truncation
        if pad_to_multiple_of is None:
             UpperCAmelCase_		=				self.pad_to_multiple_of
        if return_token_type_ids is None:
             UpperCAmelCase_		=				self.return_token_type_ids
        if return_attention_mask is None:
             UpperCAmelCase_		=				self.return_attention_mask
        if not isinstance(_snake_case   ,		tf.Tensor):
             UpperCAmelCase_		=				tf.convert_to_tensor(_snake_case)
        if text_pair is not None and not isinstance(_snake_case   ,		tf.Tensor):
             UpperCAmelCase_		=				tf.convert_to_tensor(_snake_case)
        if text_pair is not None:
             if text.shape.rank > 1:
                  raise ValueError('''text argument should not be multidimensional when a text pair is supplied!''')
             if text_pair.shape.rank > 1:
                  raise ValueError('''text_pair should not be multidimensional!''')
        if text.shape.rank == 2:
             UpperCAmelCase_	,			UpperCAmelCase_		=				text[:, 0], text[:, 1]
        UpperCAmelCase_		=				self.unpaired_tokenize(_snake_case)
        if text_pair is None:  # Unpaired text
             if truncation:
                  UpperCAmelCase_		=				text[:, : max_length - 2]  # Allow room for special tokens
             UpperCAmelCase_	,			UpperCAmelCase_		=				combine_segments(
                 (text,)   ,		start_of_sequence_id=self.cls_token_id   ,		end_of_segment_id=self.sep_token_id)
        else:  # Paired text
             UpperCAmelCase_		=				self.unpaired_tokenize(_snake_case)
             if truncation:
                  UpperCAmelCase_	,			UpperCAmelCase_		=				self.paired_trimmer.trim([text, text_pair])
             UpperCAmelCase_	,			UpperCAmelCase_		=				combine_segments(
                 (text, text_pair)   ,		start_of_sequence_id=self.cls_token_id   ,		end_of_segment_id=self.sep_token_id)
        if padding == "longest":
             UpperCAmelCase_		=				input_ids.bounding_shape(axis=1)
             if pad_to_multiple_of is not None:
                  # No ceiling division in tensorflow, so we negate floordiv instead
                  UpperCAmelCase_		=				pad_to_multiple_of * (-tf.math.floordiv(-pad_length   ,		_snake_case))
        else:
             UpperCAmelCase_		=				max_length

        UpperCAmelCase_	,			UpperCAmelCase_		=				pad_model_inputs(_snake_case   ,		max_seq_length=_snake_case   ,		pad_value=self.pad_token_id)
        UpperCAmelCase_		=				{'''input_ids''': input_ids}
        if return_attention_mask:
             UpperCAmelCase_		=				attention_mask
        if return_token_type_ids:
             UpperCAmelCase_	,			UpperCAmelCase_		=				pad_model_inputs(
                 _snake_case   ,		max_seq_length=_snake_case   ,		pad_value=self.pad_token_id)
             UpperCAmelCase_		=				token_type_ids
        return output







   def lowerCamelCase		( self						:		Optional[Any]):

        """simple docstring"""


        return {
            "vocab_list": self.vocab_list,
            "do_lower_case": self.do_lower_case,
            "cls_token_id": self.cls_token_id,
            "sep_token_id": self.sep_token_id,
            "pad_token_id": self.pad_token_id,
        }






