code: def main():
      args = parse_args()
  
      if args.non_ema_revision is not None:
          deprecate(
              "non_ema_revision!=None",
              "0.15.0",
              message=(
                  "Downloading 'non_ema' weights from revision branches of the Hub is deprecated. Please make sure to"
                  " use `--variant=non_ema` instead."
              ),
          )
      logging_dir = os.path.join(args.output_dir, args.logging_dir)
  
      accelerator_project_config = ProjectConfiguration(
          total_limit=args.checkpoints_total_limit,
          project_dir=args.output_dir,
          logging_dir=logging_dir,
      )
  
      accelerator = Accelerator(
          gradient_accumulation_steps=args.gradient_accumulation_steps,
          mixed_precision=args.mixed_precision,
          log_with=args.report_to,
          project_config=accelerator_project_config,
      )
  
      # Make one log on every process with the configuration for debugging.
      logging.basicConfig(
          format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
          datefmt="%m/%d/%Y %H:%M:%S",
          level=logging.INFO,
      )
      logger.info(accelerator.state, main_process_only=False)
      if accelerator.is_local_main_process:
          datasets.utils.logging.set_verbosity_warning()
          transformers.utils.logging.set_verbosity_warning()
          diffusers.utils.logging.set_verbosity_info()
      else:
          datasets.utils.logging.set_verbosity_error()
          transformers.utils.logging.set_verbosity_error()
          diffusers.utils.logging.set_verbosity_error()
  
      # If passed along, set the training seed now.
      if args.seed is not None:
          set_seed(args.seed)
  
      # Handle the repository creation
      if accelerator.is_main_process:
          if args.output_dir is not None:
              os.makedirs(args.output_dir, exist_ok=True)
  
          if args.push_to_hub:
              repo_id = create_repo(
                  repo_id=args.hub_model_id
                  or Path(args.output_dir).name,
                  exist_ok=True,
                  token=args.hub_token,
              ).repo_id
  
      # Load scheduler, tokenizer and models.
      noise_scheduler = DDPMScheduler.from_pretrained(
          args.pretrained_model_name_or_path, subfolder="scheduler"
      )
      tokenizer = CLIPTokenizer.from_pretrained(
          args.pretrained_model_name_or_path,
          subfolder="tokenizer",
          revision=args.revision,
      )
  
      def deepspeed_zero_init_disabled_context_manager():
          """
          returns either a context list that includes one that will disable zero.Init or an empty context list
          """
          deepspeed_plugin = (
              AcceleratorState().deepspeed_plugin
              if accelerate.state.is_initialized()
              else None
          )
          if deepspeed_plugin is None:
              return []
  
          return [
              deepspeed_plugin.zero3_init_context_manager(enable=False)
          ]
  
      # Currently Accelerate doesn't know how to handle multiple models under Deepspeed ZeRO stage 3.
      # For this to work properly all models must be run through `accelerate.prepare`. But accelerate
      # will try to assign the same optimizer with the same weights to all models during
      # `deepspeed.initialize`, which of course doesn't work.
      #
      # For now the following workaround will partially support Deepspeed ZeRO-3, by excluding the 2
      # frozen models from being partitioned during `zero.Init` which gets called during
      # `from_pretrained` So CLIPTextModel and AutoencoderKL will not enjoy the parameter sharding
      # across multiple gpus and only UNet2DConditionModel will get ZeRO sharded.
      with ContextManagers(
          deepspeed_zero_init_disabled_context_manager()
      ):
          text_encoder = CLIPTextModel.from_pretrained(
              args.pretrained_model_name_or_path,
              subfolder="text_encoder",
              revision=args.revision,
          )
          vae = AutoencoderKL.from_pretrained(
              args.pretrained_model_name_or_path,
              subfolder="vae",
              revision=args.revision,
          )
  
      unet = UNet2DConditionModel.from_pretrained(
          args.pretrained_model_name_or_path,
          subfolder="unet",
          revision=args.non_ema_revision,
      )
  
      # Freeze vae and text_encoder
      vae.requires_grad_(False)
      text_encoder.requires_grad_(False)
  
      # Create EMA for the unet.
      if args.use_ema:
          ema_unet = UNet2DConditionModel.from_pretrained(
              args.pretrained_model_name_or_path,
              subfolder="unet",
              revision=args.revision,
          )
          ema_unet = EMAModel(
              ema_unet.parameters(),
              model_cls=UNet2DConditionModel,
              model_config=ema_unet.config,
          )
  
      if args.enable_xformers_memory_efficient_attention:
          if is_xformers_available():
              import xformers
  
              xformers_version = version.parse(xformers.__version__)
              if xformers_version == version.parse("0.0.16"):
                  logger.warn(
                      "xFormers 0.0.16 cannot be used for training in some GPUs. If you observe problems during training, please update xFormers to at least 0.0.17. See https://huggingface.co/docs/diffusers/main/en/optimization/xformers for more details."
                  )
              unet.enable_xformers_memory_efficient_attention()
          else:
              raise ValueError(
                  "xformers is not available. Make sure it is installed correctly"
              )
  
      def compute_snr(timesteps):
          """
          Computes SNR as per https://github.com/TiankaiHang/Min-SNR-Diffusion-Training/blob/521b624bd70c67cee4bdf49225915f5945a872e3/guided_diffusion/gaussian_diffusion.py#L847-L849
          """
          alphas_cumprod = noise_scheduler.alphas_cumprod
          sqrt_alphas_cumprod = alphas_cumprod**0.5
          sqrt_one_minus_alphas_cumprod = (1.0 - alphas_cumprod) ** 0.5
  
          # Expand the tensors.
          # Adapted from https://github.com/TiankaiHang/Min-SNR-Diffusion-Training/blob/521b624bd70c67cee4bdf49225915f5945a872e3/guided_diffusion/gaussian_diffusion.py#L1026
          sqrt_alphas_cumprod = sqrt_alphas_cumprod.to(
              device=timesteps.device
          )[timesteps].float()
          while len(sqrt_alphas_cumprod.shape) < len(timesteps.shape):
              sqrt_alphas_cumprod = sqrt_alphas_cumprod[..., None]
          alpha = sqrt_alphas_cumprod.expand(timesteps.shape)
  
          sqrt_one_minus_alphas_cumprod = (
              sqrt_one_minus_alphas_cumprod.to(device=timesteps.device)[
                  timesteps
              ].float()
          )
          while len(sqrt_one_minus_alphas_cumprod.shape) < len(
              timesteps.shape
          ):
              sqrt_one_minus_alphas_cumprod = (
                  sqrt_one_minus_alphas_cumprod[..., None]
              )
          sigma = sqrt_one_minus_alphas_cumprod.expand(timesteps.shape)
  
          # Compute SNR.
          snr = (alpha / sigma) ** 2
          return snr
  
      # `accelerate` 0.16.0 will have better support for customized saving
      if version.parse(accelerate.__version__) >= version.parse(
          "0.16.0"
      ):
          # create custom saving & loading hooks so that `accelerator.save_state(...)` serializes in a nice format
          def save_model_hook(models, weights, output_dir):
              if args.use_ema:
                  ema_unet.save_pretrained(
                      os.path.join(output_dir, "unet_ema")
                  )
  
              for i, model in enumerate(models):
                  model.save_pretrained(
                      os.path.join(output_dir, "unet")
                  )
  
                  # make sure to pop weight so that corresponding model is not saved again
                  weights.pop()
  
          def load_model_hook(models, input_dir):
              if args.use_ema:
                  load_model = EMAModel.from_pretrained(
                      os.path.join(input_dir, "unet_ema"),
                      UNet2DConditionModel,
                  )
                  ema_unet.load_state_dict(load_model.state_dict())
                  ema_unet.to(accelerator.device)
                  del load_model
  
              for i in range(len(models)):
                  # pop models so that they are not loaded again
                  model = models.pop()
  
                  # load diffusers style into model
                  load_model = UNet2DConditionModel.from_pretrained(
                      input_dir, subfolder="unet"
                  )
                  model.register_to_config(**load_model.config)
  
                  model.load_state_dict(load_model.state_dict())
                  del load_model
  
          accelerator.register_save_state_pre_hook(save_model_hook)
          accelerator.register_load_state_pre_hook(load_model_hook)
  
      if args.gradient_checkpointing:
          unet.enable_gradient_checkpointing()
  
      # Enable TF32 for faster training on Ampere GPUs,
      # cf https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices
      if args.allow_tf32:
          torch.backends.cuda.matmul.allow_tf32 = True
  
      if args.scale_lr:
          args.learning_rate = (
              args.learning_rate
              * args.gradient_accumulation_steps
              * args.train_batch_size
              * accelerator.num_processes
          )
  
      # Initialize the optimizer
      if args.use_8bit_adam:
          try:
              import bitsandbytes as bnb
          except ImportError:
              raise ImportError(
                  "Please install bitsandbytes to use 8-bit Adam. You can do so by running `pip install bitsandbytes`"
              )
  
          optimizer_cls = bnb.optim.AdamW8bit
      else:
          optimizer_cls = torch.optim.AdamW
  
      optimizer = optimizer_cls(
          unet.parameters(),
          lr=args.learning_rate,
          betas=(args.adam_beta1, args.adam_beta2),
          weight_decay=args.adam_weight_decay,
          eps=args.adam_epsilon,
      )
  
      if args.ort:
          from onnxruntime.training.optim.fp16_optimizer import (
              FP16_Optimizer as ORT_FP16_Optimizer,
          )
  
          optimizer = ORT_FP16_Optimizer(optimizer)
  
      # Get the datasets: you can either provide your own training and evaluation files (see below)
      # or specify a Dataset from the hub (the dataset will be downloaded automatically from the datasets Hub).
  
      # In distributed training, the load_dataset function guarantees that only one local process can concurrently
      # download the dataset.
      if args.dataset_name is not None:
          # Downloading and loading a dataset from the hub.
          dataset = load_dataset(
              args.dataset_name,
              args.dataset_config_name,
              cache_dir=args.cache_dir,
          )
      else:
          data_files = {}
          if args.train_data_dir is not None:
              data_files["train"] = os.path.join(
                  args.train_data_dir, "**"
              )
          dataset = load_dataset(
              "imagefolder",
              data_files=data_files,
              cache_dir=args.cache_dir,
          )
          # See more about loading custom images at
          # https://huggingface.co/docs/datasets/v2.4.0/en/image_load#imagefolder
  
      # Preprocessing the datasets.
      # We need to tokenize inputs and targets.
      column_names = dataset["train"].column_names
  
      # 6. Get the column names for input/target.
      dataset_columns = DATASET_NAME_MAPPING.get(
          args.dataset_name, None
      )
      if args.image_column is None:
          image_column = (
              dataset_columns[0]
              if dataset_columns is not None
              else column_names[0]
          )
      else:
          image_column = args.image_column
          if image_column not in column_names:
              raise ValueError(
                  f"--image_column' value '{args.image_column}' needs to be one of: {', '.join(column_names)}"
              )
      if args.caption_column is None:
          caption_column = (
              dataset_columns[1]
              if dataset_columns is not None
              else column_names[1]
          )
      else:
          caption_column = args.caption_column
          if caption_column not in column_names:
              raise ValueError(
                  f"--caption_column' value '{args.caption_column}' needs to be one of: {', '.join(column_names)}"
              )
  
      # Preprocessing the datasets.
      # We need to tokenize input captions and transform the images.
      def tokenize_captions(examples, is_train=True):
          captions = []
          for caption in examples[caption_column]:
              if isinstance(caption, str):
                  captions.append(caption)
              elif isinstance(caption, (list, np.ndarray)):
                  # take a random caption if there are multiple
                  captions.append(
                      random.choice(caption) if is_train else caption[0]
                  )
              else:
                  raise ValueError(
                      f"Caption column `{caption_column}` should contain either strings or lists of strings."
                  )
          inputs = tokenizer(
              captions,
              max_length=tokenizer.model_max_length,
              padding="max_length",
              truncation=True,
              return_tensors="pt",
          )
          return inputs.input_ids
  
      # Preprocessing the datasets.
      train_transforms = transforms.Compose(
          [
              transforms.Resize(
                  args.resolution,
                  interpolation=transforms.InterpolationMode.BILINEAR,
              ),
              transforms.CenterCrop(args.resolution)
              if args.center_crop
              else transforms.RandomCrop(args.resolution),
              transforms.RandomHorizontalFlip()
              if args.random_flip
              else transforms.Lambda(lambda x: x),
              transforms.ToTensor(),
              transforms.Normalize([0.5], [0.5]),
          ]
      )
  
      def preprocess_train(examples):
          images = [
              image.convert("RGB") for image in examples[image_column]
          ]
          examples["pixel_values"] = [
              train_transforms(image) for image in images
          ]
          examples["input_ids"] = tokenize_captions(examples)
          return examples
  
      with accelerator.main_process_first():
          if args.max_train_samples is not None:
              dataset["train"] = (
                  dataset["train"]
                  .shuffle(seed=args.seed)
                  .select(range(args.max_train_samples))
              )
          # Set the training transforms
          train_dataset = dataset["train"].with_transform(
              preprocess_train
          )
  
      def collate_fn(examples):
          pixel_values = torch.stack(
              [example["pixel_values"] for example in examples]
          )
          pixel_values = pixel_values.to(
              memory_format=torch.contiguous_format
          ).float()
          input_ids = torch.stack(
              [example["input_ids"] for example in examples]
          )
          return {"pixel_values": pixel_values, "input_ids": input_ids}
  
      # DataLoaders creation:
      train_dataloader = torch.utils.data.DataLoader(
          train_dataset,
          shuffle=True,
          collate_fn=collate_fn,
          batch_size=args.train_batch_size,
          num_workers=args.dataloader_num_workers,
      )
  
      # Scheduler and math around the number of training steps.
      overrode_max_train_steps = False
      num_update_steps_per_epoch = math.ceil(
          len(train_dataloader) / args.gradient_accumulation_steps
      )
      if args.max_train_steps is None:
          args.max_train_steps = (
              args.num_train_epochs * num_update_steps_per_epoch
          )
          overrode_max_train_steps = True
  
      lr_scheduler = get_scheduler(
          args.lr_scheduler,
          optimizer=optimizer,
          num_warmup_steps=args.lr_warmup_steps
          * args.gradient_accumulation_steps,
          num_training_steps=args.max_train_steps
          * args.gradient_accumulation_steps,
      )
  
      # Prepare everything with our `accelerator`.
      (
          unet,
          optimizer,
          train_dataloader,
          lr_scheduler,
      ) = accelerator.prepare(
          unet, optimizer, train_dataloader, lr_scheduler
      )
  
      if args.use_ema:
          ema_unet.to(accelerator.device)
  
      if args.ort:
          from onnxruntime.training.ortmodule import ORTModule
  
          vae = ORTModule(vae)
          text_encoder = ORTModule(text_encoder)
          unet = ORTModule(unet)
  
      # For mixed precision training we cast the text_encoder and vae weights to half-precision
      # as these models are only used for inference, keeping weights in full precision is not required.
      weight_dtype = torch.float32
      if accelerator.mixed_precision == "fp16":
          weight_dtype = torch.float16
      elif accelerator.mixed_precision == "bf16":
          weight_dtype = torch.bfloat16
  
      # Move text_encode and vae to gpu and cast to weight_dtype
      text_encoder.to(accelerator.device, dtype=weight_dtype)
      vae.to(accelerator.device, dtype=weight_dtype)
  
      # We need to recalculate our total training steps as the size of the training dataloader may have changed.
      num_update_steps_per_epoch = math.ceil(
          len(train_dataloader) / args.gradient_accumulation_steps
      )
      if overrode_max_train_steps:
          args.max_train_steps = (
              args.num_train_epochs * num_update_steps_per_epoch
          )
      # Afterwards we recalculate our number of training epochs
      args.num_train_epochs = math.ceil(
          args.max_train_steps / num_update_steps_per_epoch
      )
  
      # We need to initialize the trackers we use, and also store our configuration.
      # The trackers initializes automatically on the main process.
      if accelerator.is_main_process:
          tracker_config = dict(vars(args))
          tracker_config.pop("validation_prompts")
          accelerator.init_trackers(
              args.tracker_project_name, tracker_config
          )
  
      # Train!
      total_batch_size = (
          args.train_batch_size
          * accelerator.num_processes
          * args.gradient_accumulation_steps
      )
  
      logger.info("***** Running training *****")
      logger.info(f"  Num examples = {len(train_dataset)}")
      logger.info(f"  Num Epochs = {args.num_train_epochs}")
      logger.info(
          f"  Instantaneous batch size per device = {args.train_batch_size}"
      )
      logger.info(
          f"  Total train batch size (w. parallel, distributed & accumulation) = {total_batch_size}"
      )
      logger.info(
          f"  Gradient Accumulation steps = {args.gradient_accumulation_steps}"
      )
      logger.info(
          f"  Total optimization steps = {args.max_train_steps}"
      )
      global_step = 0
      first_epoch = 0
  
      # Potentially load in the weights and states from a previous save
      if args.resume_from_checkpoint:
          if args.resume_from_checkpoint != "latest":
              path = os.path.basename(args.resume_from_checkpoint)
          else:
              # Get the most recent checkpoint
              dirs = os.listdir(args.output_dir)
              dirs = [d for d in dirs if d.startswith("checkpoint")]
              dirs = sorted(dirs, key=lambda x: int(x.split("-")[1]))
              path = dirs[-1] if len(dirs) > 0 else None
  
          if path is None:
              accelerator.print(
                  f"Checkpoint '{args.resume_from_checkpoint}' does not exist. Starting a new training run."
              )
              args.resume_from_checkpoint = None
          else:
              accelerator.print(f"Resuming from checkpoint {path}")
              accelerator.load_state(
                  os.path.join(args.output_dir, path)
              )
              global_step = int(path.split("-")[1])
  
              resume_global_step = (
                  global_step * args.gradient_accumulation_steps
              )
              first_epoch = global_step // num_update_steps_per_epoch
              resume_step = resume_global_step % (
                  num_update_steps_per_epoch
                  * args.gradient_accumulation_steps
              )
  
      # Only show the progress bar once on each machine.
      progress_bar = tqdm(
          range(global_step, args.max_train_steps),
          disable=not accelerator.is_local_main_process,
      )
      progress_bar.set_description("Steps")
  
      for epoch in range(first_epoch, args.num_train_epochs):
          unet.train()
          train_loss = 0.0
          for step, batch in enumerate(train_dataloader):
              # Skip steps until we reach the resumed step
              if (
                  args.resume_from_checkpoint
                  and epoch == first_epoch
                  and step < resume_step
              ):
                  if step % args.gradient_accumulation_steps == 0:
                      progress_bar.update(1)
                  continue
  
              with accelerator.accumulate(unet):
                  # Convert images to latent space
                  latents = vae.encode(
                      batch["pixel_values"].to(weight_dtype)
                  ).latent_dist.sample()
                  latents = latents * vae.config.scaling_factor
  
                  # Sample noise that we'll add to the latents
                  noise = torch.randn_like(latents)
                  if args.noise_offset:
                      # https://www.crosslabs.org//blog/diffusion-with-offset-noise
                      noise += args.noise_offset * torch.randn(
                          (latents.shape[0], latents.shape[1], 1, 1),
                          device=latents.device,
                      )
                  if args.input_perturbation:
                      new_noise = (
                          noise
                          + args.input_perturbation
                          * torch.randn_like(noise)
                      )
                  bsz = latents.shape[0]
                  # Sample a random timestep for each image
                  timesteps = torch.randint(
                      0,
                      noise_scheduler.config.num_train_timesteps,
                      (bsz,),
                      device=latents.device,
                  )
                  timesteps = timesteps.long()
  
                  # Add noise to the latents according to the noise magnitude at each timestep
                  # (this is the forward diffusion process)
                  if args.input_perturbation:
                      noisy_latents = noise_scheduler.add_noise(
                          latents, new_noise, timesteps
                      )
                  else:
                      noisy_latents = noise_scheduler.add_noise(
                          latents, noise, timesteps
                      )
  
                  # Get the text embedding for conditioning
                  encoder_hidden_states = text_encoder(
                      batch["input_ids"]
                  )[0]
  
                  # Get the target for loss depending on the prediction type
                  if args.prediction_type is not None:
                      # set prediction_type of scheduler if defined
                      noise_scheduler.register_to_config(
                          prediction_type=args.prediction_type
                      )
  
                  if (
                      noise_scheduler.config.prediction_type
                      == "epsilon"
                  ):
                      target = noise
                  elif (
                      noise_scheduler.config.prediction_type
                      == "v_prediction"
                  ):
                      target = noise_scheduler.get_velocity(
                          latents, noise, timesteps
                      )
                  else:
                      raise ValueError(
                          f"Unknown prediction type {noise_scheduler.config.prediction_type}"
                      )
  
                  # Predict the noise residual and compute loss
                  model_pred = unet(
                      noisy_latents, timesteps, encoder_hidden_states
                  ).sample
  
                  if args.snr_gamma is None:
                      loss = F.mse_loss(
                          model_pred.float(),
                          target.float(),
                          reduction="mean",
                      )
                  else:
                      # Compute loss-weights as per Section 3.4 of https://arxiv.org/abs/2303.09556.
                      # Since we predict the noise instead of x_0, the original formulation is slightly changed.
                      # This is discussed in Section 4.2 of the same paper.
                      snr = compute_snr(timesteps)
                      mse_loss_weights = (
                          torch.stack(
                              [
                                  snr,
                                  args.snr_gamma
                                  * torch.ones_like(timesteps),
                              ],
                              dim=1,
                          ).min(dim=1)[0]
                          / snr
                      )
                      # We first calculate the original loss. Then we mean over the non-batch dimensions and
                      # rebalance the sample-wise losses with their respective loss weights.
                      # Finally, we take the mean of the rebalanced loss.
                      loss = F.mse_loss(
                          model_pred.float(),
                          target.float(),
                          reduction="none",
                      )
                      loss = (
                          loss.mean(dim=list(range(1, len(loss.shape))))
                          * mse_loss_weights
                      )
                      loss = loss.mean()
  
                  # Gather the losses across all processes for logging (if we use distributed training).
                  avg_loss = accelerator.gather(
                      loss.repeat(args.train_batch_size)
                  ).mean()
                  train_loss += (
                      avg_loss.item() / args.gradient_accumulation_steps
                  )
  
                  # Backpropagate
                  accelerator.backward(loss)
                  if accelerator.sync_gradients:
                      accelerator.clip_grad_norm_(
                          unet.parameters(), args.max_grad_norm
                      )
                  optimizer.step()
                  lr_scheduler.step()
                  optimizer.zero_grad()
  
              # Checks if the accelerator has performed an optimization step behind the scenes
              if accelerator.sync_gradients:
                  if args.use_ema:
                      ema_unet.step(unet.parameters())
                  progress_bar.update(1)
                  global_step += 1
                  accelerator.log(
                      {"train_loss": train_loss}, step=global_step
                  )
                  train_loss = 0.0
  
                  if global_step % args.checkpointing_steps == 0:
                      if accelerator.is_main_process:
                          save_path = os.path.join(
                              args.output_dir,
                              f"checkpoint-{global_step}",
                          )
                          accelerator.save_state(save_path)
                          logger.info(f"Saved state to {save_path}")
  
              logs = {
                  "step_loss": loss.detach().item(),
                  "lr": lr_scheduler.get_last_lr()[0],
              }
              progress_bar.set_postfix(**logs)
  
              if global_step >= args.max_train_steps:
                  break
  
          if accelerator.is_main_process:
              if (
                  args.validation_prompts is not None
                  and epoch % args.validation_epochs == 0
              ):
                  if args.use_ema:
                      # Store the UNet parameters temporarily and load the EMA parameters to perform inference.
                      ema_unet.store(unet.parameters())
                      ema_unet.copy_to(unet.parameters())
                  log_validation(
                      vae,
                      text_encoder,
                      tokenizer,
                      unet,
                      args,
                      accelerator,
                      weight_dtype,
                      global_step,
                  )
                  if args.use_ema:
                      # Switch back to the original UNet parameters.
                      ema_unet.restore(unet.parameters())
  
      # Create the pipeline using the trained modules and save it.
      accelerator.wait_for_everyone()
      if accelerator.is_main_process:
          if args.ort:
              # Get the most recent checkpoint
              dirs = os.listdir(args.output_dir)
              dirs = [d for d in dirs if d.startswith("checkpoint")]
              dirs = sorted(dirs, key=lambda x: int(x.split("-")[1]))
              checkpoint_dir = dirs[-1] if len(dirs) > 0 else None
  
              # reload PyTorch model with weights trained via ORT Training
              # this is required since ORTModule object cannot be passed into StableDiffusionPipeline
              root_dir = Path(__file__).resolve().parent
              checkpoint_dir = (
                  root_dir / args.output_dir / checkpoint_dir
              )
              unet = UNet2DConditionModel.from_pretrained(
                  str(checkpoint_dir), subfolder="unet"
              )
  
              # reload pre-trained text_encoder and vae
              text_encoder = CLIPTextModel.from_pretrained(
                  args.pretrained_model_name_or_path,
                  subfolder="text_encoder",
                  revision=args.revision,
              )
              vae = AutoencoderKL.from_pretrained(
                  args.pretrained_model_name_or_path,
                  subfolder="vae",
                  revision=args.revision,
              )
          else:
              unet = accelerator.unwrap_model(unet)
              if args.use_ema:
                  ema_unet.copy_to(unet.parameters())
  
          pipeline = StableDiffusionPipeline.from_pretrained(
              args.pretrained_model_name_or_path,
              text_encoder=text_encoder,
              vae=vae,
              unet=unet,
              revision=args.revision,
              torch_dtype=torch.float16,
          )
          pipeline.save_pretrained(args.output_dir)
  
          if args.push_to_hub:
              upload_folder(
                  repo_id=repo_id,
                  folder_path=args.output_dir,
                  commit_message="End of training",
                  ignore_patterns=["step_*", "epoch_*"],
              )
  
      accelerator.end_training()
  
imports: import argparse
  import logging
  import math
  import os
  import random
  from pathlib import Path
  import accelerate
  import datasets
  import diffusers
  import numpy
  import torch
  import torch.nn.functional
  import torch.utils.checkpoint
  import transformers
  from accelerate import Accelerator
  from accelerate.logging import get_logger
  from accelerate.state import AcceleratorState
  from accelerate.utils import ProjectConfiguration, set_seed
  from datasets import load_dataset
  from diffusers import AutoencoderKL, DDPMScheduler, StableDiffusionPipeline, UNet2DConditionModel
  from diffusers.optimization import get_scheduler
  from diffusers.training_utils import EMAModel
  from diffusers.utils import check_min_version, deprecate, is_wandb_available
  from diffusers.utils.import_utils import is_xformers_available
  from huggingface_hub import create_repo, upload_folder
  from packaging import version
  from torchvision import transforms
  from tqdm.auto import tqdm
  from transformers import CLIPTextModel, CLIPTokenizer
  from transformers.utils import ContextManagers
label_desc: Use image node_id 3 for calling a global function with example usage: main() and returns: list, snr, inputs, examples, dict, list