code: def _safe_save_checkpoint(
      state: tp.Any, checkpoint_path: Path, is_sharded: bool = False
  ) -> None:
      """Save checkpoints in a safe manner even with when sharded checkpoints across nodes."""
  
      def _barrier_if_sharded():
          if is_sharded:
              flashy.distrib.barrier()
  
      if flashy.distrib.is_rank_zero():
          token = Path(str(checkpoint_path) + ".tmp.done")
          if token.exists():
              token.unlink()
      _barrier_if_sharded()
      with flashy.utils.write_and_rename(checkpoint_path) as f:
          torch.save(state, f)
          _barrier_if_sharded()
          if flashy.distrib.is_rank_zero():
              token.touch()
          _barrier_if_sharded()
      _barrier_if_sharded()
      if flashy.distrib.rank() == 0:
          token.unlink()
  
file_path: audiocraft/audiocraft/utils/checkpoint.py
imports: from enum import Enum
  import logging
  from pathlib import Path
  import re
  import typing
  import flashy
  import torch
  from ..environment import AudioCraftEnvironment
label_desc: Use image node_id 8 for calling a global function with example usage: _safe_save_checkpoint(state, checkpoint_path, is_sharded) without return types