code: def _save_checkpoint(self):
      if self.accelerator.is_main_process:
          if self.global_step % self.config.checkpointing_steps == 0:
              # _before_ saving state, check if this save would set us over the `checkpoints_total_limit`
              if self.config.checkpoints_total_limit is not None:
                  checkpoints = os.listdir(self.config.project_name)
                  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)
                      >= self.config.checkpoints_total_limit
                  ):
                      num_to_remove = (
                          len(checkpoints)
                          - self.config.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(
                              self.config.project_name,
                              removing_checkpoint,
                          )
                          shutil.rmtree(removing_checkpoint)
  
              save_path = os.path.join(
                  self.config.project_name,
                  f"checkpoint-{self.global_step}",
              )
              self.accelerator.save_state(save_path)
              logger.info(f"Saved state to {save_path}")
  
imports: import itertools
  import math
  import os
  import shutil
  import torch
  import torch.nn.functional
  from diffusers import StableDiffusionXLPipeline
  from diffusers.loaders import LoraLoaderMixin, text_encoder_lora_state_dict
  from diffusers.optimization import get_scheduler
  from huggingface_hub import create_repo, upload_folder
  from tqdm import tqdm
  from autotrain import logger
  from autotrain.trainers.dreambooth import utils
label_desc: Use image node_id 8 for calling the Trainer obj's underlying member method code with example usage: obj._save_checkpoint() without return types