code: def main():
      args = parse_args()
      logging_dir = Path(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,
      )
      if args.report_to == "wandb":
          if not is_wandb_available():
              raise ImportError(
                  "Make sure to install wandb if you want to use it for logging during training."
              )
          import wandb
  
      # 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, image_processor, tokenizer and models.
      noise_scheduler = DDPMScheduler(
          beta_schedule="squaredcos_cap_v2", prediction_type="sample"
      )
      image_processor = CLIPImageProcessor.from_pretrained(
          args.pretrained_prior_model_name_or_path,
          subfolder="image_processor",
      )
      tokenizer = CLIPTokenizer.from_pretrained(
          args.pretrained_prior_model_name_or_path,
          subfolder="tokenizer",
      )
      image_encoder = CLIPVisionModelWithProjection.from_pretrained(
          args.pretrained_prior_model_name_or_path,
          subfolder="image_encoder",
      )
      text_encoder = CLIPTextModelWithProjection.from_pretrained(
          args.pretrained_prior_model_name_or_path,
          subfolder="text_encoder",
      )
      prior = PriorTransformer.from_pretrained(
          args.pretrained_prior_model_name_or_path, subfolder="prior"
      )
      # freeze parameters of models to save more memory
      image_encoder.requires_grad_(False)
      prior.requires_grad_(False)
      text_encoder.requires_grad_(False)
      weight_dtype = torch.float32
      if accelerator.mixed_precision == "fp16":
          weight_dtype = torch.float16
      elif accelerator.mixed_precision == "bf16":
          weight_dtype = torch.bfloat16
  
      # Move image_encoder, text_encoder and prior to device and cast to weight_dtype
      prior.to(accelerator.device, dtype=weight_dtype)
      image_encoder.to(accelerator.device, dtype=weight_dtype)
      text_encoder.to(accelerator.device, dtype=weight_dtype)
      lora_attn_procs = {}
      for name in prior.attn_processors.keys():
          lora_attn_procs[name] = LoRAAttnProcessor(
              hidden_size=2048, rank=args.rank
          )
  
      prior.set_attn_processor(lora_attn_procs)
      lora_layers = AttnProcsLayers(prior.attn_processors)
  
      if args.allow_tf32:
          torch.backends.cuda.matmul.allow_tf32 = True
  
      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(
          lora_layers.parameters(),
          lr=args.learning_rate,
          betas=(args.adam_beta1, args.adam_beta2),
          weight_decay=args.adam_weight_decay,
          eps=args.adam_epsilon,
      )
  
      # 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",
          )
          text_input_ids = inputs.input_ids
          text_mask = inputs.attention_mask.bool()
          return text_input_ids, text_mask
  
      def preprocess_train(examples):
          images = [
              image.convert("RGB") for image in examples[image_column]
          ]
          examples["clip_pixel_values"] = image_processor(
              images, return_tensors="pt"
          ).pixel_values
          (
              examples["text_input_ids"],
              examples["text_mask"],
          ) = 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):
          clip_pixel_values = torch.stack(
              [example["clip_pixel_values"] for example in examples]
          )
          clip_pixel_values = clip_pixel_values.to(
              memory_format=torch.contiguous_format
          ).float()
          text_input_ids = torch.stack(
              [example["text_input_ids"] for example in examples]
          )
          text_mask = torch.stack(
              [example["text_mask"] for example in examples]
          )
          return {
              "clip_pixel_values": clip_pixel_values,
              "text_input_ids": text_input_ids,
              "text_mask": text_mask,
          }
  
      # 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,
      )
      clip_mean = prior.clip_mean.clone()
      clip_std = prior.clip_std.clone()
      (
          lora_layers,
          optimizer,
          train_dataloader,
          lr_scheduler,
      ) = accelerator.prepare(
          lora_layers, optimizer, train_dataloader, lr_scheduler
      )
  
      # 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:
          accelerator.init_trackers(
              "text2image-fine-tune", config=vars(args)
          )
  
      # 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
              initial_global_step = 0
          else:
              accelerator.print(f"Resuming from checkpoint {path}")
              accelerator.load_state(
                  os.path.join(args.output_dir, path)
              )
              global_step = int(path.split("-")[1])
  
              initial_global_step = global_step
              first_epoch = global_step // num_update_steps_per_epoch
  
      else:
          initial_global_step = 0
  
      progress_bar = tqdm(
          range(0, args.max_train_steps),
          initial=initial_global_step,
          desc="Steps",
          # Only show the progress bar once on each machine.
          disable=not accelerator.is_local_main_process,
      )
  
      clip_mean = clip_mean.to(weight_dtype).to(accelerator.device)
      clip_std = clip_std.to(weight_dtype).to(accelerator.device)
  
      for epoch in range(first_epoch, args.num_train_epochs):
          prior.train()
          train_loss = 0.0
          for step, batch in enumerate(train_dataloader):
              with accelerator.accumulate(prior):
                  # Convert images to latent space
                  text_input_ids, text_mask, clip_images = (
                      batch["text_input_ids"],
                      batch["text_mask"],
                      batch["clip_pixel_values"].to(weight_dtype),
                  )
                  with torch.no_grad():
                      text_encoder_output = text_encoder(text_input_ids)
                      prompt_embeds = text_encoder_output.text_embeds
                      text_encoder_hidden_states = (
                          text_encoder_output.last_hidden_state
                      )
  
                      image_embeds = image_encoder(
                          clip_images
                      ).image_embeds
                      # Sample noise that we'll add to the image_embeds
                      noise = torch.randn_like(image_embeds)
                      bsz = image_embeds.shape[0]
                      # Sample a random timestep for each image
                      timesteps = torch.randint(
                          0,
                          noise_scheduler.config.num_train_timesteps,
                          (bsz,),
                          device=image_embeds.device,
                      )
                      timesteps = timesteps.long()
                      image_embeds = (
                          image_embeds - clip_mean
                      ) / clip_std
                      noisy_latents = noise_scheduler.add_noise(
                          image_embeds, noise, timesteps
                      )
  
                      target = image_embeds
  
                  # Predict the noise residual and compute loss
                  model_pred = prior(
                      noisy_latents,
                      timestep=timesteps,
                      proj_embedding=prompt_embeds,
                      encoder_hidden_states=text_encoder_hidden_states,
                      attention_mask=text_mask,
                  ).predicted_image_embedding
  
                  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(noise_scheduler, timesteps)
                      if (
                          noise_scheduler.config.prediction_type
                          == "v_prediction"
                      ):
                          # Velocity objective requires that we add one to SNR values before we divide by them.
                          snr = snr + 1
                      mse_loss_weights = (
                          torch.stack(
                              [
                                  snr,
                                  args.snr_gamma
                                  * torch.ones_like(timesteps),
                              ],
                              dim=1,
                          ).min(dim=1)[0]
                          / snr
                      )
  
                      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_(
                          prior.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:
                  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:
                          # _before_ saving state, check if this save would set us over the `checkpoints_total_limit`
                          if args.checkpoints_total_limit is not None:
                              checkpoints = os.listdir(args.output_dir)
                              checkpoints = [
                                  d
                                  for d in checkpoints
                                  if d.startswith("checkpoint")
                              ]
                              checkpoints = sorted(
                                  checkpoints,
                                  key=lambda x: int(x.split("-")[1]),
                              )
  
                              # before we save the new checkpoint, we need to have at _most_ `checkpoints_total_limit - 1` checkpoints
                              if (
                                  len(checkpoints)
                                  >= args.checkpoints_total_limit
                              ):
                                  num_to_remove = (
                                      len(checkpoints)
                                      - args.checkpoints_total_limit
                                      + 1
                                  )
                                  removing_checkpoints = checkpoints[
                                      0:num_to_remove
                                  ]
  
                                  logger.info(
                                      f"{len(checkpoints)} checkpoints already exist, removing {len(removing_checkpoints)} checkpoints"
                                  )
                                  logger.info(
                                      f"removing checkpoints: {', '.join(removing_checkpoints)}"
                                  )
  
                                  for (
                                      removing_checkpoint
                                  ) in removing_checkpoints:
                                      removing_checkpoint = (
                                          os.path.join(
                                              args.output_dir,
                                              removing_checkpoint,
                                          )
                                      )
                                      shutil.rmtree(removing_checkpoint)
  
                          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_prompt is not None
                  and epoch % args.validation_epochs == 0
              ):
                  logger.info(
                      f"Running validation... \n Generating {args.num_validation_images} images with prompt:"
                      f" {args.validation_prompt}."
                  )
                  # create pipeline
                  pipeline = AutoPipelineForText2Image.from_pretrained(
                      args.pretrained_decoder_model_name_or_path,
                      prior_prior=accelerator.unwrap_model(prior),
                      torch_dtype=weight_dtype,
                  )
                  pipeline = pipeline.to(accelerator.device)
                  pipeline.set_progress_bar_config(disable=True)
  
                  # run inference
                  generator = torch.Generator(device=accelerator.device)
                  if args.seed is not None:
                      generator = generator.manual_seed(args.seed)
                  images = []
                  for _ in range(args.num_validation_images):
                      images.append(
                          pipeline(
                              args.validation_prompt,
                              num_inference_steps=30,
                              generator=generator,
                          ).images[0]
                      )
  
                  for tracker in accelerator.trackers:
                      if tracker.name == "tensorboard":
                          np_images = np.stack(
                              [np.asarray(img) for img in images]
                          )
                          tracker.writer.add_images(
                              "validation",
                              np_images,
                              epoch,
                              dataformats="NHWC",
                          )
                      if tracker.name == "wandb":
                          tracker.log(
                              {
                                  "validation": [
                                      wandb.Image(
                                          image,
                                          caption=f"{i}: {args.validation_prompt}",
                                      )
                                      for i, image in enumerate(images)
                                  ]
                              }
                          )
  
                  del pipeline
                  torch.cuda.empty_cache()
  
      # Save the lora layers
      accelerator.wait_for_everyone()
      if accelerator.is_main_process:
          prior = prior.to(torch.float32)
          prior.save_attn_procs(args.output_dir)
  
          if args.push_to_hub:
              save_model_card(
                  repo_id,
                  images=images,
                  base_model=args.pretrained_prior_model_name_or_path,
                  dataset_name=args.dataset_name,
                  repo_folder=args.output_dir,
              )
              upload_folder(
                  repo_id=repo_id,
                  folder_path=args.output_dir,
                  commit_message="End of training",
                  ignore_patterns=["step_*", "epoch_*"],
              )
  
      # Final inference
      # Load previous pipeline
      pipeline = AutoPipelineForText2Image.from_pretrained(
          args.pretrained_decoder_model_name_or_path,
          torch_dtype=weight_dtype,
      )
      pipeline = pipeline.to(accelerator.device)
  
      # load attention processors
      pipeline.prior_prior.load_attn_procs(args.output_dir)
  
      # run inference
      generator = torch.Generator(device=accelerator.device)
      if args.seed is not None:
          generator = generator.manual_seed(args.seed)
      images = []
      for _ in range(args.num_validation_images):
          images.append(
              pipeline(
                  args.validation_prompt,
                  num_inference_steps=30,
                  generator=generator,
              ).images[0]
          )
  
      if accelerator.is_main_process:
          for tracker in accelerator.trackers:
              if len(images) != 0:
                  if tracker.name == "tensorboard":
                      np_images = np.stack(
                          [np.asarray(img) for img in images]
                      )
                      tracker.writer.add_images(
                          "test", np_images, epoch, dataformats="NHWC"
                      )
                  if tracker.name == "wandb":
                      tracker.log(
                          {
                              "test": [
                                  wandb.Image(
                                      image,
                                      caption=f"{i}: {args.validation_prompt}",
                                  )
                                  for i, image in enumerate(images)
                              ]
                          }
                      )
  
      accelerator.end_training()
  
imports: import argparse
  import logging
  import math
  import os
  import random
  import shutil
  from pathlib import Path
  import datasets
  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.utils import ProjectConfiguration, set_seed
  from datasets import load_dataset
  from huggingface_hub import create_repo, upload_folder
  from tqdm import tqdm
  from transformers import CLIPImageProcessor, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionModelWithProjection
  import diffusers
  from diffusers import AutoPipelineForText2Image, DDPMScheduler, PriorTransformer
  from diffusers.loaders import AttnProcsLayers
  from diffusers.models.attention_processor import LoRAAttnProcessor
  from diffusers.optimization import get_scheduler
  from diffusers.training_utils import compute_snr
  from diffusers.utils import check_min_version, is_wandb_available
label_desc: Use image node_id 3 for calling a global function with example usage: main() and returns: text_input_ids,  text_mask, examples, dict