Datasets:
Formats:
csv
Languages:
English
Size:
1K - 10K
Tags:
arxiv-artifact
reproducibility
research-artifact
computer-science
computer-logic
formal-methods
License:
| from __future__ import annotations | |
| import time | |
| from dataclasses import dataclass | |
| from typing import Iterable | |
| import numpy as np | |
| from .schema import Window | |
| class EngineInfo: | |
| requested: str | |
| actual: str | |
| torch_available: bool | |
| cuda_available: bool | |
| device_name: str | None = None | |
| def resolve_engine(requested: str = "auto") -> EngineInfo: | |
| requested = requested.lower() | |
| try: | |
| import torch | |
| cuda = bool(torch.cuda.is_available()) | |
| name = torch.cuda.get_device_name(0) if cuda else None | |
| if requested in {"cuda", "gpu"} and cuda: | |
| return EngineInfo(requested=requested, actual="cuda", torch_available=True, cuda_available=True, device_name=name) | |
| if requested == "auto" and cuda: | |
| return EngineInfo(requested=requested, actual="cuda", torch_available=True, cuda_available=True, device_name=name) | |
| if requested in {"cuda", "gpu"} and not cuda: | |
| return EngineInfo(requested=requested, actual="cuda-requested-fell-back", torch_available=True, cuda_available=False) | |
| return EngineInfo(requested=requested, actual="cpu", torch_available=True, cuda_available=cuda, device_name=name) | |
| except Exception: | |
| if requested in {"cuda", "gpu"}: | |
| return EngineInfo(requested=requested, actual="cuda-requested-fell-back", torch_available=False, cuda_available=False) | |
| return EngineInfo(requested=requested, actual="cpu", torch_available=False, cuda_available=False) | |
| def atom_matrix(windows: list[Window], atoms: Iterable[str], engine: EngineInfo): | |
| atom_list = list(atoms) | |
| matrix = np.zeros((len(windows), len(atom_list)), dtype=bool) | |
| for row, window in enumerate(windows): | |
| present = window.atom_set() | |
| for col, atom in enumerate(atom_list): | |
| matrix[row, col] = atom in present | |
| if engine.actual == "cuda": | |
| import torch | |
| return torch.as_tensor(matrix, device="cuda"), atom_list | |
| return matrix, atom_list | |
| def within_radius(mask, radius: int): | |
| if radius <= 0: | |
| return mask | |
| if hasattr(mask, "device") and str(mask.device).startswith("cuda"): | |
| import torch | |
| out = mask.clone() | |
| for shift in range(1, radius + 1): | |
| out[shift:] |= mask[:-shift] | |
| out[:-shift] |= mask[shift:] | |
| return out | |
| out = mask.copy() | |
| for shift in range(1, radius + 1): | |
| out[shift:] |= mask[:-shift] | |
| out[:-shift] |= mask[shift:] | |
| return out | |
| def to_numpy_bool(mask) -> np.ndarray: | |
| if hasattr(mask, "detach"): | |
| return mask.detach().cpu().numpy().astype(bool) | |
| return np.asarray(mask, dtype=bool) | |
| def stress_check(windows: list[Window], engine: EngineInfo, repeats: int = 1) -> dict[str, float | int | str]: | |
| atoms = sorted({atom for window in windows for atom in window.atom_set()}) | |
| start = time.time() | |
| matrix, _ = atom_matrix(windows, atoms, engine) | |
| checksum = 0 | |
| if engine.actual == "cuda": | |
| import torch | |
| work = matrix | |
| for _ in range(max(1, repeats)): | |
| work = torch.logical_xor(within_radius(work, 1), torch.roll(work, shifts=1, dims=0)) | |
| checksum += int(work.sum().detach().cpu().item()) | |
| torch.cuda.synchronize() | |
| else: | |
| work = matrix | |
| for _ in range(max(1, repeats)): | |
| work = np.logical_xor(within_radius(work, 1), np.roll(work, shift=1, axis=0)) | |
| checksum += int(work.sum()) | |
| elapsed = time.time() - start | |
| return { | |
| "engine": engine.actual, | |
| "atom_count": len(atoms), | |
| "window_count": len(windows), | |
| "repeats": repeats, | |
| "checksum": checksum, | |
| "elapsed_seconds": elapsed, | |
| } | |