def rm_checkpoints(
    save_dir: str,
    keep_n_latest: int = 0,
):
    """
    Remove old checkpoints.

    Args:
        save_dir (str): The directory to save the checkpoints.
        keep_n_latest (int): The number of latest checkpoints to keep.
    """
    if keep_n_latest <= 0 or dist.get_rank() != 0:
        return
    files = glob(os.path.join(save_dir, "epoch*-global_step*"))
    files = sorted(
        files, key=lambda s: tuple(map(int, re.search(r"epoch(\d+)-global_step(\d+)", s).groups())), reverse=True
    )
    to_remove = files[keep_n_latest:]
    for f in to_remove:
        # shutil.rmtree(f)
        for item in glob(os.path.join(f, "*")):
            if os.path.isdir(item):
                dir_name = os.path.basename(item)
                if dir_name != "eval":
                    shutil.rmtree(item)
            else:
                os.remove(item)

    Remove old checkpoints.

    Args:
        save_dir (str): The directory to save the checkpoints.
        keep_n_latest (int): The number of latest checkpoints to keep.
    
opensora/utils/ckpt.py