Unit 8.5 · about 4 h
225 / 324Overflow, underflow and finding the first invalid value
A NaN loss names no culprit. The only cheap evidence is the first invalid value and the operation that made it, and both need instrumentation in place before the failure.
- measure
You can now
- Explain why mathematically identical floating-point computations may differ bitwise across batches, slices and backends.
- Enable anomaly detection to name the first backward function that produces
NaN, with its forward traceback. - Place
isfinitechecks so an invalid value is caught at creation, not ten steps later.
Covers
- Nonassociativity, overflow and underflow
- First-invalid-value instrumentation
Study, in this order
3 steps
- Nonassociativity, batches, slices and extremal valuesPyTorch - checkpointing, numerical accuracy and CUDA semantics
Opening paragraphs on limited accuracy and nonassociativity; Batched computations or slice computations with the
bmmandsumexamples; Extremal values with thenormoverflow example and theisfiniteadvice - Anomaly detection referencePyTorch - checkpointing, numerical accuracy and CUDA semantics
detect_anomalyandset_detect_anomaly: forward traceback of the failing operation plus thecheck_nanerror onNaNgenerated in backward; debug-only slowdown warning - Debugging APIs stay off in timed runsPyTorch - reliability and performance recipes
General optimizations, Disable debugging APIs: anomaly detection, profiler hooks and
gradcheckoff for regular training
Focus on
- Floating point addition and multiplication are not associative. Batching, slicing and backend choice change reduction order, so bitwise identity is never promised.
- Overflow can strike intermediates while the final value is representable. The
fp32norm of[1e20, 1e20]isinfwhile thefp64norm is finite. - Anomaly detection answers which backward made
NaN, not which forward looked odd.check_nanfires onNaNgenerated inside backward. - Detection and profiling both slow execution. Neither belongs in a timed run or a production loop.
Reproduce closed-book
torch.tensor([1e20, 1e20]).norm() gives inf while the double version is finite. Then run (x / x).backward() at x = 0 under autograd.detect_anomaly() and record the exact error naming DivBackward0 and its NaN output.Traps
- Asserting bitwise equality across batch sizes, slices or devices. The docs promise nothing here; compare with a tolerance.
- Expecting anomaly detection to flag
NaNflowing from forward. Log of a negative isNaNforward with a finite grad of -1, andcheck_nanstays silent because backward generated nothing invalid. - Leaving anomaly detection or the profiler on while timing. Both add overhead, so a slow profiled run proves nothing about the clean run.
Practice
TrenTorch: FP16/BF16 Representable Range
Implement cast_to_dtype, detect_underflow and detect_overflow with NumPy float16
In float16, magnitudes below about 3e-8 round to 0 and values from 65,520 up overflow to inf. The section mini-project then adds first-invalid capture and seeded NaN diagnosis with fixed-batch replay.
Self-test
-
Under
detect_anomaly,(x / x).backward()atx = 0namesDivBackward0. What does that name identify?The backward function whose output wentNaN, hence the forward division. The mode also prints the traceback of that forward call. -
torch.logof -1.0 givesNaNforward and grad -1.0 with no anomaly error. Why silent?check_nanfires onNaNgenerated by backward computation. Here backward computed the finite value \(1/x\), so there is nothing to report. -
(A@B)[0]equaledA[0]@B[0]bitwise on your machine. May a test assert==across batchings?No. Bitwise identity across batched and unbatched paths is not guaranteed, so assertallclosewith an explicit tolerance. -
Times look bad with anomaly detection and the profiler enabled. What is the first correction?
Disable both for timed runs. Both are debug-only overhead, so their timings do not describe the clean run.
Completion criterion
Stop when
You can produce the named DivBackward0 error on demand, and every equality in your checks carries an explicit tolerance.
Leads to
- 3.8 Diagnosing failed runs: divergence, stale state, slices and seeds Promote first-invalid evidence into full divergence diagnosis.
- 8.9 Profiling training phases and verifying optimizations Record invalid events alongside phase timings in profiles.
Comes after
- 5.6 Floating point, rounding, overflow and cancellationUse constructed failure mechanisms to interpret non-finite diagnostics.
- 5.8 Residuals, tolerances and iterative-solver errorReport convergence and numerical failure as separate statuses.
- 3.8 Diagnosing failed runs: divergence, stale state, slices and seedsTrace the first invalid value with framework instrumentation.
- 8.4 Precision formats, autocast and gradient scalingTrace the overflow and underflow that scaling is designed to prevent.
Resources for this unit
- PyTorch - reliability and performance recipes Use the Profiler, Automatic Mixed Precision, model loading and Benchmark recipes, then the Performance Tuning Guide. Apply each to one compact model from an earlier section and keep a CPU fallback when CUDA is unavailable.
- PyTorch - checkpointing, numerical accuracy and CUDA semantics Use the official user-guide entries for torch.utils.checkpoint, numerical accuracy, CUDA semantics, reproducibility and serialization. Predict memory categories before measuring and define resume equivalence before running.
Also in this section
- PyTorch - compiler troubleshooting Read graph breaks, guards, recompilations and logging. Compile one stable fragment, intentionally trigger a graph break or recompilation, inspect it, and compare eager and compiled outputs and gradients.
- Deep Learning Training Debugger Use the failure-injection, measurement and evidence rules as the deeper standard. The full Tier 2 project is a later route and is not added to this stage milestone.
- TensorTonic - mixed-precision memory and transformer FLOPs Solve only Mixed-Precision Training Memory Accountant and Transformer Training FLOP Estimator. Reconcile both with the compact model's profiler and document every included and excluded term.
- Learn PyTorch - profiler walkthrough Use notebook 10 only if the official profiler route remains unclear. Transfer the relevant annotations into the compact model instead of keeping the tutorial code.
- TrenTorch - precision, checkpointing and resume Solve Loss Scaling, FP16/BF16 Representable Range, Gradient Checkpointing and Resume-from-checkpoint in NumPy, then reproduce each behavior with the PyTorch API in the compact model.
- Python os - `os.replace` Read
os.replacefor the atomic-rename step of checkpoint writes: save to a temp file in the same directory,flushandfsync, then replace the final name. - Hands-On Machine Learning with Scikit-Learn and PyTorch Read Appendix B, Reduced Precision Models and Mixed-Precision Training, for why
float16training underflows and whatfp32primary weights and loss scaling fix.