File size: 1,032 Bytes
9e366ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""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()