eniairaph07's picture
Add validated Transformers runtime and concise generation example
7218492 verified
Raw History Blame Contribute Delete
1.15 kB
"""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