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       a_  (       snake_case_ ):
  
      '''simple docstring'''
      UpperCamelCase									=				42
  
  
  
  
  class       a_  (       snake_case_   ,					snake_case_ ):
  
      '''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 = (224, 448, 672, 896)				,	A = 2				,	A = 1				,	A = 1				,	A = "conv"				,	A = "conv"				,	A = "silu"				,	A = 8				,	A = 32				,	A = 1e-5				,	A = "default"				,	A = True				,	A = None				,	A = None				,	)	->     Optional[Any]:
          super().__init__()
  
          _SCREAMING_SNAKE_CASE             =    sample_size
          _SCREAMING_SNAKE_CASE             =    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
          _SCREAMING_SNAKE_CASE             =    nn.Convad(A				,	block_out_channels[0]				,	kernel_size=3				,	padding=(1, 1) )
  
          # time
          if time_embedding_type == "fourier":
              _SCREAMING_SNAKE_CASE             =    GaussianFourierProjection(embedding_size=block_out_channels[0]				,	scale=16 )
              _SCREAMING_SNAKE_CASE             =    2 * block_out_channels[0]
          elif time_embedding_type == "positional":
              _SCREAMING_SNAKE_CASE             =    Timesteps(block_out_channels[0]				,	A				,	A )
              _SCREAMING_SNAKE_CASE             =    block_out_channels[0]
  
          _SCREAMING_SNAKE_CASE             =    TimestepEmbedding(A				,	A )
  
          # class embedding
          if class_embed_type is None and num_class_embeds is not None:
              _SCREAMING_SNAKE_CASE             =    nn.Embedding(A				,	A )
          elif class_embed_type == "timestep":
              _SCREAMING_SNAKE_CASE             =    TimestepEmbedding(A				,	A )
          elif class_embed_type == "identity":
              _SCREAMING_SNAKE_CASE             =    nn.Identity(A				,	A )
          else:
              _SCREAMING_SNAKE_CASE             =    None
  
          _SCREAMING_SNAKE_CASE             =    nn.ModuleList([] )
          _SCREAMING_SNAKE_CASE             =    None
          _SCREAMING_SNAKE_CASE             =    nn.ModuleList([] )
  
          # down
          _SCREAMING_SNAKE_CASE             =    block_out_channels[0]
          for i, down_block_type in enumerate(A ):
              _SCREAMING_SNAKE_CASE             =    output_channel
              _SCREAMING_SNAKE_CASE             =    block_out_channels[i]
              _SCREAMING_SNAKE_CASE             =    i == len(A ) - 1
  
              _SCREAMING_SNAKE_CASE             =    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
          _SCREAMING_SNAKE_CASE             =    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
          _SCREAMING_SNAKE_CASE             =    list(reversed(A ) )
          _SCREAMING_SNAKE_CASE             =    reversed_block_out_channels[0]
          for i, up_block_type in enumerate(A ):
              _SCREAMING_SNAKE_CASE             =    output_channel
              _SCREAMING_SNAKE_CASE             =    reversed_block_out_channels[i]
              _SCREAMING_SNAKE_CASE             =    reversed_block_out_channels[min(i + 1				,	len(A ) - 1 )]
  
              _SCREAMING_SNAKE_CASE             =    i == len(A ) - 1
  
              _SCREAMING_SNAKE_CASE             =    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 )
              _SCREAMING_SNAKE_CASE             =    output_channel
  
          # out
          _SCREAMING_SNAKE_CASE             =    norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4				,	32 )
          _SCREAMING_SNAKE_CASE             =    nn.GroupNorm(num_channels=block_out_channels[0]				,	num_groups=A				,	eps=A )
          _SCREAMING_SNAKE_CASE             =    nn.SiLU()
          _SCREAMING_SNAKE_CASE             =    nn.Convad(block_out_channels[0]				,	A				,	kernel_size=3				,	padding=1 )
  
  
  
  
  
      def     snake_case_(	self				,	A				,	A				,	A = None				,	A = True				,	)	->     Union[UNetaDOutput, Tuple]:
          # 0. center input if necessary
          if self.config.center_input_sample:
              _SCREAMING_SNAKE_CASE             =    2 * sample - 1.0
  
          # 1. time
          _SCREAMING_SNAKE_CASE             =    timestep
          if not torch.is_tensor(A ):
              _SCREAMING_SNAKE_CASE             =    torch.tensor([timesteps]				,	dtype=torch.long				,	device=sample.device )
          elif torch.is_tensor(A ) and len(timesteps.shape ) == 0:
              _SCREAMING_SNAKE_CASE             =    timesteps[None].to(sample.device )
  
          # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
          _SCREAMING_SNAKE_CASE             =    timesteps * torch.ones(sample.shape[0]				,	dtype=timesteps.dtype				,	device=timesteps.device )
  
          _SCREAMING_SNAKE_CASE             =    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.
          _SCREAMING_SNAKE_CASE             =    t_emb.to(dtype=self.dtype )
          _SCREAMING_SNAKE_CASE             =    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":
                  _SCREAMING_SNAKE_CASE             =    self.time_proj(A )
  
              _SCREAMING_SNAKE_CASE             =    self.class_embedding(A ).to(dtype=self.dtype )
              _SCREAMING_SNAKE_CASE             =    emb + class_emb
  
          # 2. pre-process
          _SCREAMING_SNAKE_CASE             =    sample
          _SCREAMING_SNAKE_CASE             =    self.conv_in(A )
  
          # 3. down
          _SCREAMING_SNAKE_CASE             =    (sample,)
          for downsample_block in self.down_blocks:
              if hasattr(A				,	"""skip_conv""" ):
                  _SCREAMING_SNAKE_CASE       , _SCREAMING_SNAKE_CASE       , _SCREAMING_SNAKE_CASE             =    downsample_block(
                      hidden_states=A				,	temb=A				,	skip_sample=A )
              else:
                  _SCREAMING_SNAKE_CASE       , _SCREAMING_SNAKE_CASE             =    downsample_block(hidden_states=A				,	temb=A )
  
              down_block_res_samples += res_samples
  
          # 4. mid
          _SCREAMING_SNAKE_CASE             =    self.mid_block(A				,	A )
  
          # 5. up
          _SCREAMING_SNAKE_CASE             =    None
          for upsample_block in self.up_blocks:
              _SCREAMING_SNAKE_CASE             =    down_block_res_samples[-len(upsample_block.resnets ) :]
              _SCREAMING_SNAKE_CASE             =    down_block_res_samples[: -len(upsample_block.resnets )]
  
              if hasattr(A				,	"""skip_conv""" ):
                  _SCREAMING_SNAKE_CASE       , _SCREAMING_SNAKE_CASE             =    upsample_block(A				,	A				,	A				,	A )
              else:
                  _SCREAMING_SNAKE_CASE             =    upsample_block(A				,	A				,	A )
  
          # 6. post-process
          _SCREAMING_SNAKE_CASE             =    self.conv_norm_out(A )
          _SCREAMING_SNAKE_CASE             =    self.conv_act(A )
          _SCREAMING_SNAKE_CASE             =    self.conv_out(A )
  
          if skip_sample is not None:
              sample += skip_sample
  
          if self.config.time_embedding_type == "fourier":
              _SCREAMING_SNAKE_CASE             =    timesteps.reshape((sample.shape[0], *([1] * len(sample.shape[1:] ))) )
              _SCREAMING_SNAKE_CASE             =    sample / timesteps
  
          if not return_dict:
              return (sample,)
  
          return UNetaDOutput(sample=A )
  
  
  
  
style_context: 
  
  
  
  
  
  '''simple docstring'''
  
  import json
  import os
  import tempfile
  import unittest
  
  from transformers.modelcard import ModelCard
  
  
  class       a_  (       unittest.TestCase ):
  
      '''simple docstring'''
  
  
      def     snake_case_(	self )	->     int:
          _SCREAMING_SNAKE_CASE             =    {
              """model_details""": {
                  """Organization""": """testing""",
                  """Model date""": """today""",
                  """Model version""": """v2.1, Developed by Test Corp in 2019.""",
                  """Architecture""": """Convolutional Neural Network.""",
              },
              """metrics""": """BLEU and ROUGE-1""",
              """evaluation_data""": {
                  """Datasets""": {"""BLEU""": """My-great-dataset-v1""", """ROUGE-1""": """My-short-dataset-v2.1"""},
                  """Preprocessing""": """See details on https://arxiv.org/pdf/1810.03993.pdf""",
              },
              """training_data""": {
                  """Dataset""": """English Wikipedia dump dated 2018-12-01""",
                  """Preprocessing""": (
                      """Using SentencePiece vocabulary of size 52k tokens. See details on"""
                      """ https://arxiv.org/pdf/1810.03993.pdf"""
                  ),
              },
              """quantitative_analyses""": {"""BLEU""": 55.1, """ROUGE-1""": 76},
          }
  
  
      def     snake_case_(	self )	->     Tuple:
          _SCREAMING_SNAKE_CASE             =    ModelCard.from_dict(self.inputs_dict )
          self.assertTrue(hasattr(A				,	"""model_details""" ) )
          self.assertTrue(hasattr(A				,	"""intended_use""" ) )
          self.assertTrue(hasattr(A				,	"""factors""" ) )
          self.assertTrue(hasattr(A				,	"""metrics""" ) )
          self.assertTrue(hasattr(A				,	"""evaluation_data""" ) )
          self.assertTrue(hasattr(A				,	"""training_data""" ) )
          self.assertTrue(hasattr(A				,	"""quantitative_analyses""" ) )
          self.assertTrue(hasattr(A				,	"""ethical_considerations""" ) )
          self.assertTrue(hasattr(A				,	"""caveats_and_recommendations""" ) )
  
  
      def     snake_case_(	self )	->     List[str]:
          _SCREAMING_SNAKE_CASE             =    ModelCard.from_dict(self.inputs_dict )
          _SCREAMING_SNAKE_CASE             =    json.loads(modelcard.to_json_string() )
          for key, value in self.inputs_dict.items():
              self.assertEqual(obj[key]				,	A )
  
  
      def     snake_case_(	self )	->     Any:
          _SCREAMING_SNAKE_CASE             =    ModelCard.from_dict(self.inputs_dict )
  
          with tempfile.TemporaryDirectory() as tmpdirname:
              _SCREAMING_SNAKE_CASE             =    os.path.join(A				,	"""modelcard.json""" )
              model_card_first.to_json_file(A )
              _SCREAMING_SNAKE_CASE             =    ModelCard.from_json_file(A )
  
          self.assertEqual(model_card_second.to_dict()				,	model_card_first.to_dict() )
  
  
  
  
  
      def     snake_case_(	self )	->     List[Any]:
          _SCREAMING_SNAKE_CASE             =    ModelCard.from_dict(self.inputs_dict )
  
          with tempfile.TemporaryDirectory() as tmpdirname:
              model_card_first.save_pretrained(A )
              _SCREAMING_SNAKE_CASE             =    ModelCard.from_pretrained(A )
  
          self.assertEqual(model_card_second.to_dict()				,	model_card_first.to_dict() )
  
  
  
  