"""Shared safety scaffolding for the opt-in fused-kernel installers (CCE, custom RMSNorm). Both run a numeric parity check on the live GPU before patching and only patch if it matches eager within tolerance — correctness over speed. The check must not perturb the trainer's global RNG (it runs after the model is loaded, inside the trainer's RNG stream), so it seeds under a local fork. """ from __future__ import annotations from collections.abc import Callable def run_gpu_self_test(body: Callable[[], bool], *, seed: int = 0) -> bool: """Run ``body`` (a numeric-parity check returning bool) on the live GPU under a forked RNG. Returns False when CUDA is unavailable. Seeds with ``seed`` inside ``torch.random.fork_rng`` so seeding the check doesn't disturb the trainer's global CPU/CUDA RNG stream. """ import torch if not torch.cuda.is_available(): return False with torch.random.fork_rng(devices=[torch.cuda.current_device()]): torch.manual_seed(seed) return body()