code: 
  
  
  
  
  
  
  import argparse
  import os
  
  import torch
  from torchvision.datasets.utils import download_url
  
  from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, TransformeraDModel
  
  
  __A  							=  {5_12: "DiT-XL-2-512x512.pt", 2_56: "DiT-XL-2-256x256.pt"}
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	List[str]		)  ->							Optional[Any]:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         __UpperCAmelCase    							=F'''pretrained_models/{model_name}'''
         if not os.path.isfile(A_		):
                os.makedirs("""pretrained_models"""     ,			exist_ok=A_		)
                __UpperCAmelCase    							=F'''https://dl.fbaipublicfiles.com/DiT/models/{model_name}'''
                download_url(A_     ,			"""pretrained_models"""		)
         __UpperCAmelCase    							=torch.load(A_     ,			map_location=lambda A_     ,			A_		: storage		)
         return model
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	List[str]		)  ->							int:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         __UpperCAmelCase    							=download_model(pretrained_models[args.image_size]		)
  
         __UpperCAmelCase    							=state_dict["""x_embedder.proj.weight"""]
         __UpperCAmelCase    							=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		):
                __UpperCAmelCase    							=state_dict[
                    """t_embedder.mlp.0.weight"""
                ]
                __UpperCAmelCase    							=state_dict[
                    """t_embedder.mlp.0.bias"""
                ]
                __UpperCAmelCase    							=state_dict[
                    """t_embedder.mlp.2.weight"""
                ]
                __UpperCAmelCase    							=state_dict[
                    """t_embedder.mlp.2.bias"""
                ]
                __UpperCAmelCase    							=state_dict[
                    """y_embedder.embedding_table.weight"""
                ]
  
                __UpperCAmelCase    							=state_dict[
                    F'''blocks.{depth}.adaLN_modulation.1.weight'''
                ]
                __UpperCAmelCase    							=state_dict[
                    F'''blocks.{depth}.adaLN_modulation.1.bias'''
                ]
  
                __UpperCAmelCase							,   __UpperCAmelCase							,   __UpperCAmelCase    							=torch.chunk(state_dict[F'''blocks.{depth}.attn.qkv.weight''']     ,			3     ,			dim=0		)
                __UpperCAmelCase							,   __UpperCAmelCase							,   __UpperCAmelCase    							=torch.chunk(state_dict[F'''blocks.{depth}.attn.qkv.bias''']     ,			3     ,			dim=0		)
  
                __UpperCAmelCase    							=q
                __UpperCAmelCase    							=q_bias
                __UpperCAmelCase    							=k
                __UpperCAmelCase    							=k_bias
                __UpperCAmelCase    							=v
                __UpperCAmelCase    							=v_bias
  
                __UpperCAmelCase    							=state_dict[
                    F'''blocks.{depth}.attn.proj.weight'''
                ]
                __UpperCAmelCase    							=state_dict[F'''blocks.{depth}.attn.proj.bias''']
  
                __UpperCAmelCase    							=state_dict[F'''blocks.{depth}.mlp.fc1.weight''']
                __UpperCAmelCase    							=state_dict[F'''blocks.{depth}.mlp.fc1.bias''']
                __UpperCAmelCase    							=state_dict[F'''blocks.{depth}.mlp.fc2.weight''']
                __UpperCAmelCase    							=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"""		)
  
         __UpperCAmelCase    							=state_dict["""final_layer.adaLN_modulation.1.weight"""]
         __UpperCAmelCase    							=state_dict["""final_layer.adaLN_modulation.1.bias"""]
         __UpperCAmelCase    							=state_dict["""final_layer.linear.weight"""]
         __UpperCAmelCase    							=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
         __UpperCAmelCase    							=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=A_     ,			num_attention_heads=16     ,			activation_fn="""gelu-approximate"""     ,			num_embeds_ada_norm=1000     ,			norm_type="""ada_norm_zero"""     ,			norm_elementwise_affine=A_     ,			)
         transformer.load_state_dict(A_     ,			strict=A_		)
  
         __UpperCAmelCase    							=DDIMScheduler(
             num_train_timesteps=1000     ,			beta_schedule="""linear"""     ,			prediction_type="""epsilon"""     ,			clip_sample=A_     ,			)
  
         __UpperCAmelCase    							=AutoencoderKL.from_pretrained(args.vae_model		)
  
         __UpperCAmelCase    							=DiTPipeline(transformer=A_     ,			vae=A_     ,			scheduler=A_		)
  
         if args.save:
                pipeline.save_pretrained(args.checkpoint_path		)
  
  
  if __name__ == "__main__":
     __A  							=  argparse.ArgumentParser()
  
     parser.add_argument(
         "--image_size",
         default=2_56,
         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."
     )
  
     __A  							=  parser.parse_args()
     main(args)
  
  
style_context: 
  
  
  
  
  
  
  import math as m
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	Tuple     ,			A_:	str     ,			A_:	Union[str, Any]=0.0_0_1		)  ->							List[str]:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         return (f(a + h		) - f(a - h		)) / (2 * h)
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	str     ,			A_:	Union[str, Any]=0     ,			A_:	List[Any]=100     ,			A_:	Optional[Any]=0.0_0_0_1     ,			A_:	Tuple=1e-6     ,			A_:	Union[str, Any]=False		)  ->							Union[str, Any]:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         __UpperCAmelCase    							=xa  # set the initial guess
         __UpperCAmelCase    							=[a]
         __UpperCAmelCase    							=abs(f(A_		)		)
         __UpperCAmelCase    							=lambda A_		: calc_derivative(A_     ,			A_     ,			h=A_		)  # noqa: E731  Derivative of f(x)
         for _ in range(A_		):
                if fa(A_		) == 0:
                       raise ValueError("""No converging solution found"""		)
                __UpperCAmelCase    							=a - f(A_		) / fa(A_		)  # Calculate the next estimate
                if logsteps:
                       steps.append(A_		)
                if error < maxerror:
                       break
         else:
                raise ValueError("""Iteration limit reached, no converging solution found"""		)
         if logsteps:
                # If logstep is true, then log intermediate steps
                return a, error, steps
         return a, error
  
  
  if __name__ == "__main__":
     from matplotlib import pyplot as plt
  
     __A  							=  lambda x: m.tanh(x) ** 2 - m.exp(3 * x)  # noqa: E731
     __A							,    __A							,    __A  							=  newton_raphson(
         f, xa=10, maxiter=10_00, step=1E-6, logsteps=True
     )
     plt.plot([abs(f(x)) for x in steps])
     plt.xlabel("step")
     plt.ylabel("error")
     plt.show()
     print(F"""solution = {{{solution:f}}}, error = {{{error:f}}}""")
  
  
  
  
  