code: 
  
  
  
  """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 UNetMidBlockaD, get_down_block, get_up_block
  @dataclass
  class 							_UpperCAmelCase							(				a   ):
  
  
  
  
  
       '''simple docstring'''
  
  
  
       a__             =42
  
  
  
  class 							_UpperCAmelCase							(				a	,a   ):
  
  
  
  
  
       '''simple docstring'''
  
  
  
       @register_to_config
       def __init__(      self ,			A = None ,			A = 3 ,			A = 3 ,			A = False ,			A = "positional" ,			A = 0 ,			A = True ,			A = ("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D") ,			A = ("AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "UpBlock2D") ,			A = (2_2_4, 4_4_8, 6_7_2, 8_9_6) ,			A = 2 ,			A = 1 ,			A = 1 ,			A = "conv" ,			A = "conv" ,			A = "silu" ,			A = 8 ,			A = 3_2 ,			A = 1E-5 ,			A = "default" ,			A = True ,			A = None ,			A = None ,			)							->      List[str]:
           super().__init__()
  
           _UpperCAmelCase							:						int       							=					sample_size
           _UpperCAmelCase							:						Tuple       							=					block_out_channels[0] * 4
  
           # Check inputs
           if len(A	) != len(A	):
               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(A	) != len(A	):
               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							:						int       							=					nn.Convad(A ,			block_out_channels[0] ,			kernel_size=3 ,			padding=(1, 1)	)
  
           # time
           if time_embedding_type == "fourier":
               _UpperCAmelCase							:						str       							=					GaussianFourierProjection(embedding_size=block_out_channels[0] ,			scale=1_6	)
               _UpperCAmelCase							:						Optional[int]       							=					2 * block_out_channels[0]
           elif time_embedding_type == "positional":
               _UpperCAmelCase							:						List[str]       							=					Timesteps(block_out_channels[0] ,			A ,			A	)
               _UpperCAmelCase							:						List[Any]       							=					block_out_channels[0]
  
           _UpperCAmelCase							:						List[str]       							=					TimestepEmbedding(A ,			A	)
  
           # class embedding
           if class_embed_type is None and num_class_embeds is not None:
               _UpperCAmelCase							:						Optional[int]       							=					nn.Embedding(A ,			A	)
           elif class_embed_type == "timestep":
               _UpperCAmelCase							:						Union[str, Any]       							=					TimestepEmbedding(A ,			A	)
           elif class_embed_type == "identity":
               _UpperCAmelCase							:						int       							=					nn.Identity(A ,			A	)
           else:
               _UpperCAmelCase							:						Dict       							=					None
  
           _UpperCAmelCase							:						Dict       							=					nn.ModuleList([]	)
           _UpperCAmelCase							:						str       							=					None
           _UpperCAmelCase							:						str       							=					nn.ModuleList([]	)
  
           # down
           _UpperCAmelCase							:						List[Any]       							=					block_out_channels[0]
           for i, down_block_type in enumerate(A	):
               _UpperCAmelCase							:						Any       							=					output_channel
               _UpperCAmelCase							:						Dict       							=					block_out_channels[i]
               _UpperCAmelCase							:						Any       							=					i == len(A	) - 1
  
               _UpperCAmelCase							:						Union[str, Any]       							=					get_down_block(
                   A ,			num_layers=A ,			in_channels=A ,			out_channels=A ,			temb_channels=A ,			add_downsample=not is_final_block ,			resnet_eps=A ,			resnet_act_fn=A ,			resnet_groups=A ,			attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel ,			downsample_padding=A ,			resnet_time_scale_shift=A ,			downsample_type=A ,			)
               self.down_blocks.append(A	)
  
           # mid
           _UpperCAmelCase							:						List[str]       							=					UNetMidBlockaD(
               in_channels=block_out_channels[-1] ,			temb_channels=A ,			resnet_eps=A ,			resnet_act_fn=A ,			output_scale_factor=A ,			resnet_time_scale_shift=A ,			attention_head_dim=attention_head_dim if attention_head_dim is not None else block_out_channels[-1] ,			resnet_groups=A ,			add_attention=A ,			)
  
           # up
           _UpperCAmelCase							:						Dict       							=					list(reversed(A	)	)
           _UpperCAmelCase							:						List[Any]       							=					reversed_block_out_channels[0]
           for i, up_block_type in enumerate(A	):
               _UpperCAmelCase							:						Optional[int]       							=					output_channel
               _UpperCAmelCase							:						Optional[int]       							=					reversed_block_out_channels[i]
               _UpperCAmelCase							:						int       							=					reversed_block_out_channels[min(i + 1 ,			len(A	) - 1	)]
  
               _UpperCAmelCase							:						str       							=					i == len(A	) - 1
  
               _UpperCAmelCase							:						Dict       							=					get_up_block(
                   A ,			num_layers=layers_per_block + 1 ,			in_channels=A ,			out_channels=A ,			prev_output_channel=A ,			temb_channels=A ,			add_upsample=not is_final_block ,			resnet_eps=A ,			resnet_act_fn=A ,			resnet_groups=A ,			attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel ,			resnet_time_scale_shift=A ,			upsample_type=A ,			)
               self.up_blocks.append(A	)
               _UpperCAmelCase							:						Union[str, Any]       							=					output_channel
  
           # out
           _UpperCAmelCase							:						Any       							=					norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4 ,			3_2	)
           _UpperCAmelCase							:						Union[str, Any]       							=					nn.GroupNorm(num_channels=block_out_channels[0] ,			num_groups=A ,			eps=A	)
           _UpperCAmelCase							:						List[Any]       							=					nn.SiLU()
           _UpperCAmelCase							:						Any       							=					nn.Convad(block_out_channels[0] ,			A ,			kernel_size=3 ,			padding=1	)
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A ,			A ,			A = None ,			A = True ,			)							->      Union[UNetaDOutput, Tuple]:
           # 0. center input if necessary
           if self.config.center_input_sample:
               _UpperCAmelCase							:						List[str]       							=					2 * sample - 1.0
  
           # 1. time
           _UpperCAmelCase							:						Optional[int]       							=					timestep
           if not torch.is_tensor(A	):
               _UpperCAmelCase							:						Tuple       							=					torch.tensor([timesteps] ,			dtype=torch.long ,			device=sample.device	)
           elif torch.is_tensor(A	) and len(timesteps.shape	) == 0:
               _UpperCAmelCase							:						int       							=					timesteps[None].to(sample.device	)
  
           # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
           _UpperCAmelCase							:						Optional[Any]       							=					timesteps * torch.ones(sample.shape[0] ,			dtype=timesteps.dtype ,			device=timesteps.device	)
  
           _UpperCAmelCase							:						Union[str, Any]       							=					self.time_proj(A	)
  
           # 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							:						List[Any]       							=					t_emb.to(dtype=self.dtype	)
           _UpperCAmelCase							:						Optional[Any]       							=					self.time_embedding(A	)
  
           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							:						List[str]       							=					self.time_proj(A	)
  
               _UpperCAmelCase							:						Optional[Any]       							=					self.class_embedding(A	).to(dtype=self.dtype	)
               _UpperCAmelCase							:						Optional[Any]       							=					emb + class_emb
  
           # 2. pre-process
           _UpperCAmelCase							:						Optional[int]       							=					sample
           _UpperCAmelCase							:						Tuple       							=					self.conv_in(A	)
  
           # 3. down
           _UpperCAmelCase							:						str       							=					(sample,)
           for downsample_block in self.down_blocks:
               if hasattr(A ,			'''skip_conv'''	):
                   _UpperCAmelCase							,       _UpperCAmelCase							,       _UpperCAmelCase							:						List[Any]       							=					downsample_block(
                       hidden_states=A ,			temb=A ,			skip_sample=A	)
               else:
                   _UpperCAmelCase							,       _UpperCAmelCase							:						List[Any]       							=					downsample_block(hidden_states=A ,			temb=A	)
  
               down_block_res_samples += res_samples
  
           # 4. mid
           _UpperCAmelCase							:						List[Any]       							=					self.mid_block(A ,			A	)
  
           # 5. up
           _UpperCAmelCase							:						Union[str, Any]       							=					None
           for upsample_block in self.up_blocks:
               _UpperCAmelCase							:						Dict       							=					down_block_res_samples[-len(upsample_block.resnets	) :]
               _UpperCAmelCase							:						Any       							=					down_block_res_samples[: -len(upsample_block.resnets	)]
  
               if hasattr(A ,			'''skip_conv'''	):
                   _UpperCAmelCase							,       _UpperCAmelCase							:						Any       							=					upsample_block(A ,			A ,			A ,			A	)
               else:
                   _UpperCAmelCase							:						List[Any]       							=					upsample_block(A ,			A ,			A	)
  
          # 6. post-process
           _UpperCAmelCase							:						Dict       							=					self.conv_norm_out(A	)
           _UpperCAmelCase							:						Any       							=					self.conv_act(A	)
           _UpperCAmelCase							:						Optional[Any]       							=					self.conv_out(A	)
  
           if skip_sample is not None:
               sample += skip_sample
  
           if self.config.time_embedding_type == "fourier":
               _UpperCAmelCase							:						Union[str, Any]       							=					timesteps.reshape((sample.shape[0], *([1] * len(sample.shape[1:]	)))	)
               _UpperCAmelCase							:						Union[str, Any]       							=					sample / timesteps
  
           if not return_dict:
               return (sample,)
  
           return UNetaDOutput(sample=A	)
  
  
  
style_context: 
  
  
  
  """simple docstring"""
  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 							_UpperCAmelCase							(				tf.keras.layers.Layer   ):
  
  
  
  
  
       '''simple docstring'''
  
  
  
       def __init__(      self ,			A ,			A ,			A = None ,			A = None ,			A = None ,			A = "longest" ,			A = True ,			A = 5_1_2 ,			A = None ,			A = True ,			A = True ,			A = True ,			**A ,			)							->      Tuple:
           super().__init__()
           if use_fast_bert_tokenizer:
               _UpperCAmelCase							:						Union[str, Any]       							=					FastBertTokenizer(
                   A ,			token_out_type=tf.intaa ,			lower_case_nfd_strip_accents=A ,			**A	)
           else:
               _UpperCAmelCase							:						List[Any]       							=					tf.lookup.StaticVocabularyTable(
                   tf.lookup.KeyValueTensorInitializer(
                       keys=A ,			key_dtype=tf.string ,			values=tf.range(tf.size(A ,			out_type=tf.intaa	) ,			dtype=tf.intaa	) ,			value_dtype=tf.intaa ,			) ,			num_oov_buckets=1 ,			)
               _UpperCAmelCase							:						str       							=					BertTokenizerLayer(
                   A ,			token_out_type=tf.intaa ,			lower_case=A ,			**A	)
  
           _UpperCAmelCase							:						int       							=					vocab_list
           _UpperCAmelCase							:						Dict       							=					do_lower_case
           _UpperCAmelCase							:						Optional[int]       							=					cls_token_id or vocab_list.index('''[CLS]'''	)
           _UpperCAmelCase							:						Any       							=					sep_token_id or vocab_list.index('''[SEP]'''	)
           _UpperCAmelCase							:						List[Any]       							=					pad_token_id or vocab_list.index('''[PAD]'''	)
           _UpperCAmelCase							:						List[str]       							=					ShrinkLongestTrimmer(max_length - 3 ,			axis=1	)  # Allow room for special tokens
           _UpperCAmelCase							:						Tuple       							=					max_length
           _UpperCAmelCase							:						List[Any]       							=					padding
           _UpperCAmelCase							:						List[Any]       							=					truncation
           _UpperCAmelCase							:						Union[str, Any]       							=					pad_to_multiple_of
           _UpperCAmelCase							:						Union[str, Any]       							=					return_token_type_ids
           _UpperCAmelCase							:						Union[str, Any]       							=					return_attention_mask
  
  
  
  
  
       @classmethod
       def 				__lowerCAmelCase			(      cls ,			A ,			**A	)							->      Dict:  # noqa: F821
           _UpperCAmelCase							:						Optional[int]       							=					kwargs.pop('''do_lower_case''' ,			A	)
           _UpperCAmelCase							:						int       							=					tokenizer.do_lower_case if do_lower_case is None else do_lower_case
           _UpperCAmelCase							:						Optional[Any]       							=					kwargs.pop('''cls_token_id''' ,			A	)
           _UpperCAmelCase							:						str       							=					tokenizer.cls_token_id if cls_token_id is None else cls_token_id
           _UpperCAmelCase							:						str       							=					kwargs.pop('''sep_token_id''' ,			A	)
           _UpperCAmelCase							:						int       							=					tokenizer.sep_token_id if sep_token_id is None else sep_token_id
           _UpperCAmelCase							:						List[Any]       							=					kwargs.pop('''pad_token_id''' ,			A	)
           _UpperCAmelCase							:						Optional[Any]       							=					tokenizer.pad_token_id if pad_token_id is None else pad_token_id
  
           _UpperCAmelCase							:						int       							=					tokenizer.get_vocab()
           _UpperCAmelCase							:						List[str]       							=					sorted([(wordpiece, idx) for wordpiece, idx in vocab.items()] ,			key=lambda A	: x[1]	)
           _UpperCAmelCase							:						Dict       							=					[entry[0] for entry in vocab]
           return cls(
               vocab_list=A ,			do_lower_case=A ,			cls_token_id=A ,			sep_token_id=A ,			pad_token_id=A ,			**A ,			)
  
  
  
  
  
       @classmethod
       def 				__lowerCAmelCase			(      cls ,			A ,			*A ,			**A	)							->      int:
           try:
               _UpperCAmelCase							:						int       							=					BertTokenizer.from_pretrained(A ,			*A ,			**A	)
           except:  # noqa: E722
               from .tokenization_bert_fast import BertTokenizerFast
  
               _UpperCAmelCase							:						Dict       							=					BertTokenizerFast.from_pretrained(A ,			*A ,			**A	)
           return cls.from_tokenizer(A ,			**A	)
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A	)							->      Any:
           if self.do_lower_case:
               _UpperCAmelCase							:						int       							=					case_fold_utfa(A	)
           _UpperCAmelCase							:						str       							=					self.tf_tokenizer.tokenize(A	)
           return tokens.merge_dims(1 ,			-1	)
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A ,			A=None ,			A=None ,			A=None ,			A=None ,			A=None ,			A=None ,			A=None ,			)							->      Optional[Any]:
           if padding is None:
               _UpperCAmelCase							:						str       							=					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							:						Union[str, Any]       							=					self.max_length
           if truncation is None:
               _UpperCAmelCase							:						List[Any]       							=					self.truncation
           if pad_to_multiple_of is None:
               _UpperCAmelCase							:						List[Any]       							=					self.pad_to_multiple_of
           if return_token_type_ids is None:
               _UpperCAmelCase							:						List[str]       							=					self.return_token_type_ids
           if return_attention_mask is None:
               _UpperCAmelCase							:						Any       							=					self.return_attention_mask
           if not isinstance(A ,			tf.Tensor	):
               _UpperCAmelCase							:						Dict       							=					tf.convert_to_tensor(A	)
           if text_pair is not None and not isinstance(A ,			tf.Tensor	):
               _UpperCAmelCase							:						str       							=					tf.convert_to_tensor(A	)
           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							:						str       							=					text[:, 0], text[:, 1]
           _UpperCAmelCase							:						Optional[Any]       							=					self.unpaired_tokenize(A	)
           if text_pair is None:  # Unpaired text
               if truncation:
                   _UpperCAmelCase							:						Optional[int]       							=					text[:, : max_length - 2]  # Allow room for special tokens
               _UpperCAmelCase							,       _UpperCAmelCase							:						Any       							=					combine_segments(
                   (text,) ,			start_of_sequence_id=self.cls_token_id ,			end_of_segment_id=self.sep_token_id	)
           else:  # Paired text
               _UpperCAmelCase							:						Tuple       							=					self.unpaired_tokenize(A	)
               if truncation:
                   _UpperCAmelCase							,       _UpperCAmelCase							:						Optional[int]       							=					self.paired_trimmer.trim([text, text_pair]	)
               _UpperCAmelCase							,       _UpperCAmelCase							:						Union[str, Any]       							=					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							:						List[Any]       							=					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							:						Tuple       							=					pad_to_multiple_of * (-tf.math.floordiv(-pad_length ,			A	))
           else:
               _UpperCAmelCase							:						int       							=					max_length
  
           _UpperCAmelCase							,       _UpperCAmelCase							:						Any       							=					pad_model_inputs(A ,			max_seq_length=A ,			pad_value=self.pad_token_id	)
           _UpperCAmelCase							:						Dict       							=					{'''input_ids''': input_ids}
           if return_attention_mask:
               _UpperCAmelCase							:						Optional[int]       							=					attention_mask
           if return_token_type_ids:
               _UpperCAmelCase							,       _UpperCAmelCase							:						Tuple       							=					pad_model_inputs(
                   A ,			max_seq_length=A ,			pad_value=self.pad_token_id	)
               _UpperCAmelCase							:						int       							=					token_type_ids
           return output
  
  
  
  
  
       def 				__lowerCAmelCase			(      self	)							->      List[str]:
           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,
           }
  
  
  