Murli-V1.6-Code / utils /checkpoint.py
ViuAI's picture
Speed optimization: eliminate ConvNeXt GRN FP32 tensor copy + prefetch_factor=4 + float32_matmul_precision=high
7967323 verified
Raw History Blame Contribute Delete
533 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)
destination.parent.mkdir(parents=True, exist_ok=True)
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)