



'''simple docstring'''





import argparse
import os

import torch
from torchvision.datasets.utils import download_url

from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, TransformeraDModel


_UpperCamelCase							       =		{512: 'DiT-XL-2-512x512.pt', 256: 'DiT-XL-2-256x256.pt'}

def  a_  (							_lowerCAmelCase )   ->   Union[str, Any]:
  __lowerCamelCase : Tuple					 =				F'pretrained_models/{model_name}'
  if not os.path.isfile(_lowerCAmelCase ):
    os.makedirs('pretrained_models' ,exist_ok=_lowerCAmelCase )
    __lowerCamelCase : Dict					 =				F'https://dl.fbaipublicfiles.com/DiT/models/{model_name}'
    download_url(_lowerCAmelCase ,'pretrained_models' )
  __lowerCamelCase : str					 =				torch.load(_lowerCAmelCase ,map_location=lambda _lowerCAmelCase ,_lowerCAmelCase : storage )
  return model

def  a_  (							_lowerCAmelCase )   ->   Dict:
  __lowerCamelCase : Tuple					 =				download_model(pretrained_models[args.image_size] )

  __lowerCamelCase : List[str]					 =				state_dict['x_embedder.proj.weight']
  __lowerCamelCase : Union[str, Any]					 =				state_dict['x_embedder.proj.bias']
  state_dict.pop('x_embedder.proj.weight' )
  state_dict.pop('x_embedder.proj.bias' )

  for depth in range(28 ):
    __lowerCamelCase : Optional[int]					 =				state_dict[
        't_embedder.mlp.0.weight'
    ]
    __lowerCamelCase : List[Any]					 =				state_dict[
        't_embedder.mlp.0.bias'
    ]
    __lowerCamelCase : int					 =				state_dict[
        't_embedder.mlp.2.weight'
    ]
    __lowerCamelCase : int					 =				state_dict[
        't_embedder.mlp.2.bias'
    ]
    __lowerCamelCase : List[str]					 =				state_dict[
        'y_embedder.embedding_table.weight'
    ]

    __lowerCamelCase : Optional[int]					 =				state_dict[
        F'blocks.{depth}.adaLN_modulation.1.weight'
    ]
    __lowerCamelCase : str					 =				state_dict[
        F'blocks.{depth}.adaLN_modulation.1.bias'
    ]

    __lowerCamelCase ,__lowerCamelCase ,__lowerCamelCase : str					 =				torch.chunk(state_dict[F'blocks.{depth}.attn.qkv.weight'] ,3 ,dim=0 )
    __lowerCamelCase ,__lowerCamelCase ,__lowerCamelCase : Union[str, Any]					 =				torch.chunk(state_dict[F'blocks.{depth}.attn.qkv.bias'] ,3 ,dim=0 )

    __lowerCamelCase : Dict					 =				q
    __lowerCamelCase : Union[str, Any]					 =				q_bias
    __lowerCamelCase : Dict					 =				k
    __lowerCamelCase : Any					 =				k_bias
    __lowerCamelCase : List[str]					 =				v
    __lowerCamelCase : str					 =				v_bias

    __lowerCamelCase : List[str]					 =				state_dict[
        F'blocks.{depth}.attn.proj.weight'
    ]
    __lowerCamelCase : Optional[int]					 =				state_dict[F'blocks.{depth}.attn.proj.bias']

    __lowerCamelCase : List[Any]					 =				state_dict[F'blocks.{depth}.mlp.fc1.weight']
    __lowerCamelCase : Optional[Any]					 =				state_dict[F'blocks.{depth}.mlp.fc1.bias']
    __lowerCamelCase : Union[str, Any]					 =				state_dict[F'blocks.{depth}.mlp.fc2.weight']
    __lowerCamelCase : str					 =				state_dict[F'blocks.{depth}.mlp.fc2.bias']

    state_dict.pop(F'blocks.{depth}.attn.qkv.weight' )
    state_dict.pop(F'blocks.{depth}.attn.qkv.bias' )
    state_dict.pop(F'blocks.{depth}.attn.proj.weight' )
    state_dict.pop(F'blocks.{depth}.attn.proj.bias' )
    state_dict.pop(F'blocks.{depth}.mlp.fc1.weight' )
    state_dict.pop(F'blocks.{depth}.mlp.fc1.bias' )
    state_dict.pop(F'blocks.{depth}.mlp.fc2.weight' )
    state_dict.pop(F'blocks.{depth}.mlp.fc2.bias' )
    state_dict.pop(F'blocks.{depth}.adaLN_modulation.1.weight' )
    state_dict.pop(F'blocks.{depth}.adaLN_modulation.1.bias' )

  state_dict.pop('t_embedder.mlp.0.weight' )
  state_dict.pop('t_embedder.mlp.0.bias' )
  state_dict.pop('t_embedder.mlp.2.weight' )
  state_dict.pop('t_embedder.mlp.2.bias' )
  state_dict.pop('y_embedder.embedding_table.weight' )

  __lowerCamelCase : List[Any]					 =				state_dict['final_layer.adaLN_modulation.1.weight']
  __lowerCamelCase : int					 =				state_dict['final_layer.adaLN_modulation.1.bias']
  __lowerCamelCase : Dict					 =				state_dict['final_layer.linear.weight']
  __lowerCamelCase : Optional[int]					 =				state_dict['final_layer.linear.bias']

  state_dict.pop('final_layer.linear.weight' )
  state_dict.pop('final_layer.linear.bias' )
  state_dict.pop('final_layer.adaLN_modulation.1.weight' )
  state_dict.pop('final_layer.adaLN_modulation.1.bias' )

  # DiT XL/2
  __lowerCamelCase : List[Any]					 =				TransformeraDModel(
      sample_size=args.image_size // 8 ,num_layers=28 ,attention_head_dim=72 ,in_channels=4 ,out_channels=8 ,patch_size=2 ,attention_bias=_lowerCAmelCase ,num_attention_heads=16 ,activation_fn='gelu-approximate' ,num_embeds_ada_norm=1000 ,norm_type='ada_norm_zero' ,norm_elementwise_affine=_lowerCAmelCase ,)
  transformer.load_state_dict(_lowerCAmelCase ,strict=_lowerCAmelCase )

  __lowerCamelCase : List[str]					 =				DDIMScheduler(
      num_train_timesteps=1000 ,beta_schedule='linear' ,prediction_type='epsilon' ,clip_sample=_lowerCAmelCase ,)

  __lowerCamelCase : Union[str, Any]					 =				AutoencoderKL.from_pretrained(args.vae_model )

  __lowerCamelCase : Any					 =				DiTPipeline(transformer=_lowerCAmelCase ,vae=_lowerCAmelCase ,scheduler=_lowerCAmelCase )

  if args.save:
    pipeline.save_pretrained(args.checkpoint_path )


if __name__ == "__main__":
   _UpperCamelCase							       =		argparse.ArgumentParser()

   parser.add_argument(
       '--image_size',
       default=256,
       type=int,
       required=False,
       help='Image size of pretrained model, either 256 or 512.',
   )
   parser.add_argument(
       '--vae_model',
       default='stabilityai/sd-vae-ft-ema',
       type=str,
       required=False,
       help='Path to pretrained VAE model, either stabilityai/sd-vae-ft-mse or stabilityai/sd-vae-ft-ema.',
   )
   parser.add_argument(
       '--save', default=True, type=bool, required=False, help='Whether to save the converted pipeline or not.'
   )
   parser.add_argument(
       '--checkpoint_path', default=None, type=str, required=True, help='Path to the output pipeline.'
   )

   _UpperCamelCase							       =		parser.parse_args()
   main(args)











'''simple docstring'''





from __future__ import annotations

# Define the Gravitational Constant G and the function
_UpperCamelCase							       =		6.6_743e-11  # unit of G : m^3 * kg^-1 * s^-2

def  a_  (							_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase )   ->   dict[str, float]:
  __lowerCamelCase : Dict					 =				mass_a * mass_a

  if (force, mass_a, mass_a, distance).count(0 ) != 1:
    raise ValueError('One and only one argument must be 0' )
  if force < 0:
    raise ValueError('Gravitational force can not be negative' )
  if distance < 0:
    raise ValueError('Distance can not be negative' )
  if mass_a < 0 or mass_a < 0:
    raise ValueError('Mass can not be negative' )
  if force == 0:
    __lowerCamelCase : Optional[Any]					 =				GRAVITATIONAL_CONSTANT * product_of_mass / (distance**2)
    return {"force": force}
  elif mass_a == 0:
    __lowerCamelCase : Tuple					 =				(force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_a)
    return {"mass_1": mass_a}
  elif mass_a == 0:
    __lowerCamelCase : str					 =				(force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_a)
    return {"mass_2": mass_a}
  elif distance == 0:
    __lowerCamelCase : int					 =				(GRAVITATIONAL_CONSTANT * product_of_mass / (force)) ** 0.5
    return {"distance": distance}
  raise ValueError('One and only one argument must be 0' )


# Run doctest
if __name__ == "__main__":
   import doctest

   doctest.testmod()






