File size: 1,145 Bytes
7218492
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""Stable, byte-verified CUDA input paths across source and wheel installations."""
import hashlib,os,tempfile
from pathlib import Path


def stable_source(path,cache_root=None):
    path=Path(path)
    if path.suffix!='.cu':raise ValueError('CUDA source required')
    raw=path.read_bytes();digest=hashlib.sha256(raw).hexdigest()
    root=Path(cache_root) if cache_root is not None else Path(os.environ.get('TORCH_EXTENSIONS_DIR',str(Path.home()/'.cache/torch_extensions')))/'fidel_sources'
    folder=root/digest;folder.mkdir(parents=True,exist_ok=True);target=folder/path.name
    if not target.exists():
        with tempfile.NamedTemporaryFile(dir=folder,delete=False) as stream:
            temporary=Path(stream.name);stream.write(raw);stream.flush();os.fsync(stream.fileno())
        try:
            try:os.link(temporary,target)
            except FileExistsError:pass
        finally:temporary.unlink()
    if hashlib.sha256(target.read_bytes()).hexdigest()!=digest:
        raise RuntimeError('Cached CUDA source bytes differ; refusing compilation')
    os.utime(target,(946684800,946684800))
    return target