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"""
      __UpperCAmelCase											=   42
  
  
  
  class 					UpperCamelCase__   (							a_       ,  a_):
  
  
  
  
  
  
  
      """simple docstring"""
  
  
  
  
  
  
  
      @register_to_config
      def __init__(		self   :  Tuple							,						UpperCamelCase_   :  Optional[Union[int, Tuple[int, int]]] = None							,						UpperCamelCase_   :  int = 3							,						UpperCamelCase_   :  int = 3							,						UpperCamelCase_   :  bool = False							,						UpperCamelCase_   :  str = "positional"							,						UpperCamelCase_   :  int = 0							,						UpperCamelCase_   :  bool = True							,						UpperCamelCase_   :  Tuple[str] = ("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D")							,						UpperCamelCase_   :  Tuple[str] = ("AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "UpBlock2D")							,						UpperCamelCase_   :  Tuple[int] = (2_2_4, 4_4_8, 6_7_2, 8_9_6)							,						UpperCamelCase_   :  int = 2							,						UpperCamelCase_   :  float = 1							,						UpperCamelCase_   :  int = 1							,						UpperCamelCase_   :  str = "conv"							,						UpperCamelCase_   :  str = "conv"							,						UpperCamelCase_   :  str = "silu"							,						UpperCamelCase_   :  Optional[int] = 8							,						UpperCamelCase_   :  int = 3_2							,						UpperCamelCase_   :  float = 1e-5							,						UpperCamelCase_   :  str = "default"							,						UpperCamelCase_   :  bool = True							,						UpperCamelCase_   :  Optional[str] = None							,						UpperCamelCase_   :  Optional[int] = None							,						):
  
  
  
  
  
             '''simple docstring'''
  
             super().__init__()
  
             __magic_name__										=						sample_size
             __magic_name__										=						block_out_channels[0] * 4
  
             # Check inputs
             if len(UpperCamelCase_   ) != len(UpperCamelCase_   ):
                    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(UpperCamelCase_   ) != len(UpperCamelCase_   ):
                    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
             __magic_name__										=						nn.Convad(UpperCamelCase_							,						block_out_channels[0]							,						kernel_size=3							,						padding=(1, 1)   )
  
             # time
             if time_embedding_type == "fourier":
                    __magic_name__										=						GaussianFourierProjection(embedding_size=block_out_channels[0]							,						scale=1_6   )
                    __magic_name__										=						2 * block_out_channels[0]
             elif time_embedding_type == "positional":
                    __magic_name__										=						Timesteps(block_out_channels[0]							,						UpperCamelCase_							,						UpperCamelCase_   )
                    __magic_name__										=						block_out_channels[0]
  
             __magic_name__										=						TimestepEmbedding(UpperCamelCase_							,						UpperCamelCase_   )
  
             # class embedding
             if class_embed_type is None and num_class_embeds is not None:
                    __magic_name__										=						nn.Embedding(UpperCamelCase_							,						UpperCamelCase_   )
             elif class_embed_type == "timestep":
                    __magic_name__										=						TimestepEmbedding(UpperCamelCase_							,						UpperCamelCase_   )
             elif class_embed_type == "identity":
                    __magic_name__										=						nn.Identity(UpperCamelCase_							,						UpperCamelCase_   )
             else:
                    __magic_name__										=						None
  
             __magic_name__										=						nn.ModuleList([]   )
             __magic_name__										=						None
             __magic_name__										=						nn.ModuleList([]   )
  
             # down
             __magic_name__										=						block_out_channels[0]
             for i, down_block_type in enumerate(UpperCamelCase_   ):
                    __magic_name__										=						output_channel
                    __magic_name__										=						block_out_channels[i]
                    __magic_name__										=						i == len(UpperCamelCase_   ) - 1
  
                    __magic_name__										=						get_down_block(
                        UpperCamelCase_							,						num_layers=UpperCamelCase_							,						in_channels=UpperCamelCase_							,						out_channels=UpperCamelCase_							,						temb_channels=UpperCamelCase_							,						add_downsample=not is_final_block							,						resnet_eps=UpperCamelCase_							,						resnet_act_fn=UpperCamelCase_							,						resnet_groups=UpperCamelCase_							,						attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel							,						downsample_padding=UpperCamelCase_							,						resnet_time_scale_shift=UpperCamelCase_							,						downsample_type=UpperCamelCase_							,						)
                    self.down_blocks.append(UpperCamelCase_   )
  
             # mid
             __magic_name__										=						UNetMidBlockaD(
                 in_channels=block_out_channels[-1]							,						temb_channels=UpperCamelCase_							,						resnet_eps=UpperCamelCase_							,						resnet_act_fn=UpperCamelCase_							,						output_scale_factor=UpperCamelCase_							,						resnet_time_scale_shift=UpperCamelCase_							,						attention_head_dim=attention_head_dim if attention_head_dim is not None else block_out_channels[-1]							,						resnet_groups=UpperCamelCase_							,						add_attention=UpperCamelCase_							,						)
  
             # up
             __magic_name__										=						list(reversed(UpperCamelCase_   )   )
             __magic_name__										=						reversed_block_out_channels[0]
             for i, up_block_type in enumerate(UpperCamelCase_   ):
                    __magic_name__										=						output_channel
                    __magic_name__										=						reversed_block_out_channels[i]
                    __magic_name__										=						reversed_block_out_channels[min(i + 1							,						len(UpperCamelCase_   ) - 1   )]
  
                    __magic_name__										=						i == len(UpperCamelCase_   ) - 1
  
                    __magic_name__										=						get_up_block(
                        UpperCamelCase_							,						num_layers=layers_per_block + 1							,						in_channels=UpperCamelCase_							,						out_channels=UpperCamelCase_							,						prev_output_channel=UpperCamelCase_							,						temb_channels=UpperCamelCase_							,						add_upsample=not is_final_block							,						resnet_eps=UpperCamelCase_							,						resnet_act_fn=UpperCamelCase_							,						resnet_groups=UpperCamelCase_							,						attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel							,						resnet_time_scale_shift=UpperCamelCase_							,						upsample_type=UpperCamelCase_							,						)
                    self.up_blocks.append(UpperCamelCase_   )
                    __magic_name__										=						output_channel
  
             # out
             __magic_name__										=						norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4							,						3_2   )
             __magic_name__										=						nn.GroupNorm(num_channels=block_out_channels[0]							,						num_groups=UpperCamelCase_							,						eps=UpperCamelCase_   )
             __magic_name__										=						nn.SiLU()
             __magic_name__										=						nn.Convad(block_out_channels[0]							,						UpperCamelCase_							,						kernel_size=3							,						padding=1   )
  
  
  
  
  
  
  
      def 							a__ (		self   :  List[Any]							,						UpperCamelCase_   :  torch.FloatTensor							,						UpperCamelCase_   :  Union[torch.Tensor, float, int]							,						UpperCamelCase_   :  Optional[torch.Tensor] = None							,						UpperCamelCase_   :  bool = True							,						):
  
  
  
  
  
             '''simple docstring'''
  
             if self.config.center_input_sample:
                    __magic_name__										=						2 * sample - 1.0
  
             # 1. time
             __magic_name__										=						timestep
             if not torch.is_tensor(UpperCamelCase_   ):
                    __magic_name__										=						torch.tensor([timesteps]							,						dtype=torch.long							,						device=sample.device   )
             elif torch.is_tensor(UpperCamelCase_   ) and len(timesteps.shape   ) == 0:
                    __magic_name__										=						timesteps[None].to(sample.device   )
  
             # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
             __magic_name__										=						timesteps * torch.ones(sample.shape[0]							,						dtype=timesteps.dtype							,						device=timesteps.device   )
  
             __magic_name__										=						self.time_proj(UpperCamelCase_   )
  
             # 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.
             __magic_name__										=						t_emb.to(dtype=self.dtype   )
             __magic_name__										=						self.time_embedding(UpperCamelCase_   )
  
             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":
                           __magic_name__										=						self.time_proj(UpperCamelCase_   )
  
                    __magic_name__										=						self.class_embedding(UpperCamelCase_   ).to(dtype=self.dtype   )
                    __magic_name__										=						emb + class_emb
  
             # 2. pre-process
             __magic_name__										=						sample
             __magic_name__										=						self.conv_in(UpperCamelCase_   )
  
             # 3. down
             __magic_name__										=						(sample,)
             for downsample_block in self.down_blocks:
                    if hasattr(UpperCamelCase_							,						'skip_conv'   ):
                           __magic_name__							,     __magic_name__							,     __magic_name__										=						downsample_block(
                               hidden_states=UpperCamelCase_							,						temb=UpperCamelCase_							,						skip_sample=UpperCamelCase_   )
                    else:
                           __magic_name__							,     __magic_name__										=						downsample_block(hidden_states=UpperCamelCase_							,						temb=UpperCamelCase_   )
  
                    down_block_res_samples += res_samples
  
             # 4. mid
             __magic_name__										=						self.mid_block(UpperCamelCase_							,						UpperCamelCase_   )
  
             # 5. up
             __magic_name__										=						None
             for upsample_block in self.up_blocks:
                    __magic_name__										=						down_block_res_samples[-len(upsample_block.resnets   ) :]
                    __magic_name__										=						down_block_res_samples[: -len(upsample_block.resnets   )]
  
                    if hasattr(UpperCamelCase_							,						'skip_conv'   ):
                           __magic_name__							,     __magic_name__										=						upsample_block(UpperCamelCase_							,						UpperCamelCase_							,						UpperCamelCase_							,						UpperCamelCase_   )
                    else:
                           __magic_name__										=						upsample_block(UpperCamelCase_							,						UpperCamelCase_							,						UpperCamelCase_   )
  
          # 6. post-process
             __magic_name__										=						self.conv_norm_out(UpperCamelCase_   )
             __magic_name__										=						self.conv_act(UpperCamelCase_   )
             __magic_name__										=						self.conv_out(UpperCamelCase_   )
  
             if skip_sample is not None:
                    sample += skip_sample
  
             if self.config.time_embedding_type == "fourier":
                    __magic_name__										=						timesteps.reshape((sample.shape[0], *([1] * len(sample.shape[1:]   )))   )
                    __magic_name__										=						sample / timesteps
  
             if not return_dict:
                    return (sample,)
  
             return UNetaDOutput(sample=UpperCamelCase_   )
style_context: 
  
  
  
  
  
  
  """simple docstring"""
  import numpy as np
  
  
  def        A	(							__snake_case:   Any   ,    __snake_case:   List[str]   ,    __snake_case:   Tuple     )						->		Dict:
  
         """simple docstring"""
  
  
  
  
  
         __magic_name__										=						n_draws * class_counts / class_counts.sum()
         # floored means we don't overshoot n_samples, but probably undershoot
         __magic_name__										=						np.floor(__snake_case     )
         # we add samples according to how much "left over" probability
         # they had, until we arrive at n_samples
         __magic_name__										=						int(n_draws - floored.sum()     )
         if need_to_add > 0:
                __magic_name__										=						continuous - floored
                __magic_name__										=						np.sort(np.unique(__snake_case     )     )[::-1]
                # add according to remainder, but break ties
                # randomly to avoid biases
                for value in values:
                       ((__magic_name__)							,     )										=						np.where(remainder == value     )
                       # if we need_to_add less than what's in inds
                       # we draw randomly from them.
                       # if we need to add more, we add them all and
                       # go to the next value
                       __magic_name__										=						min(len(__snake_case     )   ,    __snake_case     )
                       __magic_name__										=						rng.choice(__snake_case   ,    size=__snake_case   ,    replace=__snake_case     )
                       floored[inds] += 1
                       need_to_add -= add_now
                       if need_to_add == 0:
                              break
         return floored.astype(np.intaa     )
  
  
  
  
  
  
  def        A	(							__snake_case:   Any   ,    __snake_case:   str   ,    __snake_case:   int   ,    __snake_case:   Optional[Any]   ,    __snake_case:   Any=1_0     )						->		Any:
  
         """simple docstring"""
  
  
  
  
  
         __magic_name__							,     __magic_name__										=						np.unique(__snake_case   ,    return_inverse=__snake_case     )
         __magic_name__										=						classes.shape[0]
         __magic_name__										=						np.bincount(__snake_case     )
         if np.min(__snake_case     ) < 2:
                raise ValueError('Minimum class count error'     )
         if n_train < n_classes:
                raise ValueError(
                    'The train_size = %d should be greater or ' 'equal to the number of classes = %d' % (n_train, n_classes)     )
         if n_test < n_classes:
                raise ValueError(
                    'The test_size = %d should be greater or ' 'equal to the number of classes = %d' % (n_test, n_classes)     )
         __magic_name__										=						np.split(np.argsort(__snake_case   ,    kind='mergesort'     )   ,    np.cumsum(__snake_case     )[:-1]     )
         for _ in range(__snake_case     ):
                __magic_name__										=						approximate_mode(__snake_case   ,    __snake_case   ,    __snake_case     )
                __magic_name__										=						class_counts - n_i
                __magic_name__										=						approximate_mode(__snake_case   ,    __snake_case   ,    __snake_case     )
  
                __magic_name__										=						[]
                __magic_name__										=						[]
  
                for i in range(__snake_case     ):
                       __magic_name__										=						rng.permutation(class_counts[i]     )
                       __magic_name__										=						class_indices[i].take(__snake_case   ,    mode='clip'     )
                       train.extend(perm_indices_class_i[: n_i[i]]     )
                       test.extend(perm_indices_class_i[n_i[i] : n_i[i] + t_i[i]]     )
                __magic_name__										=						rng.permutation(__snake_case     )
                __magic_name__										=						rng.permutation(__snake_case     )
  
                yield train, test