Precision + Numerics Stabilizer (bf16/fp16 Done Right)

Detect NaNs and Inf in PyTorch mixed-precision training with failure snapshots.

Updated Feb 28, 2026
One-click install
npx skills add https://github.com/sovr610/refffiy --skill precision-numerics-stabilizer-bf16-fp16-done-right
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Precision + Numerics Stabilizer (bf16/fp16 Done Right)
Source: https://github.com/sovr610/refffiy/tree/main/brain-ai-dev/skills/precision-numerics-stabilizer
Command: npx skills add https://github.com/sovr610/refffiy --skill precision-numerics-stabilizer-bf16-fp16-done-right

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires torch, numpy, pytest, and includes scripts (resource) and references (resource) and assets (resource) components.

What problem does it solve?

Add a precision layer and numerics instrumentation that makes mixed-precision training safe to run unattended by detecting NaNs/Inf, monitoring gradient norms, and providing reproducible failure snapshots.

Core Features & Use Cases

  • Automatic detection of NaNs, Inf, and gradient overflow with auto-abort or debug snapshots.
  • GradScaler integration for fp16 with automatic scale management and safe unscale/clip ordering.
  • Flexible precision modes: bf16, fp16, or fp32 with autocast constraints and master-weights-in-fp32 invariant.
  • Numerics sentinel: gradient norms, logit monitoring, activation checks, and weight non-finite checks.
  • Snapshotting: 7-file failure snapshots with atomic writes for reproducibility, diagnostic data, and repro scripts.

Quick Start

Integrate this stabilizer into your PyTorch training workflow and run your loop to enable automatic NaN detection, mixed-precision safety, and reproducible failure snapshots.

Frequently Asked Questions about Precision + Numerics Stabilizer (bf16/fp16 Done Right)

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent NaNs and gradient overflow during mixed-precision training in PyTorch?

Preventing NaNs during mixed-precision training requires a numerics pipeline with AMP autocast and GradScaler. This stabilizer automatically detects non-finite values, manages gradient scaling, and logs failure snapshots to ensure safe unattended fp16 and bf16 workflows.

What is the best way to configure GradScaler for fp16 training loops?

Configuring GradScaler for fp16 training loops involves automatic scale management and safe unscale/clip ordering. This Skill integrates GradScaler directly into the PyTorch workflow to dynamically adjust scaling factors and prevent gradient underflow or overflow.

Does PyTorch autocast support switching between bf16 and fp16 modes safely?

PyTorch autocast safely supports flexible bf16, fp16, and fp32 precision modes when paired with a numerics sentinel. This Skill enforces master-weights-in-fp32 invariants and applies autocast constraints to maintain stability across CPUs and GPUs.

How to capture reproducible snapshots when a PyTorch training run crashes?

Capturing reproducible snapshots when a training run crashes involves saving diagnostic data with atomic writes. This Skill generates 7-file failure snapshots containing repro scripts and numerics logs to investigate NaNs, Inf values, and gradient anomalies.

Why does my loss spike or output NaN when using AMP autocast?

Loss spikes or NaN outputs during AMP autocast often stem from gradient overflow or insufficient scaling. This Skill monitors gradient norms, logits, and activation checks to detect non-finite values and automatically abort or snapshot the failure for debugging.

Can I run unattended mixed-precision training without risking silent numerical failures?

Running unattended mixed-precision training safely requires continuous numerics instrumentation to detect non-finite values. This Skill provides a configurable precision pipeline with automatic NaN detection and gradient norm monitoring to prevent silent failures.