Murli-V1.5-Code / utils /checkpoint.py
ViuAI's picture
Audit fixes: optimizer-step checkpoints, train/infer prosody parity, spoken numbers, narrate.py, 6/6 tests pass
cc5e704 verified
Raw History Blame Contribute Delete
475 Bytes
"""Atomic writes shared by checkpoints and feature caches."""
import os
import tempfile
from pathlib import Path
import torch
def atomic_save(payload, destination):
destination = Path(destination)
with tempfile.NamedTemporaryFile(dir=destination.parent, delete=False) as stream:
temporary = stream.name
try:
torch.save(payload, temporary)
os.replace(temporary, destination)
finally:
Path(temporary).unlink(missing_ok=True)