File size: 533 Bytes
870eedf
 
 
 
 
 
 
 
 
 
7967323
870eedf
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"""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)