On the Correctness of Automatic Differentiation for Neural Networks with Machine-Representable Parameters

Wonyeol Lee, Sejun Park, Alex Aiken

TL;DR

While theoretical proofs suggest Automatic Differentiation (AD) is "almost always" correct over real numbers, this paper proves it remains highly reliable on actual computers using discrete floating-point numbers. The authors show that for neural networks with bias parameters, AD is guaranteed to be correct wherever the network is differentiable and provides a valid generalized derivative even where it isn't.

What Problem Does This Paper Address?

Automatic Differentiation (AD) is the engine under the hood of every major deep learning framework (like PyTorch, TensorFlow, and JAX). Mathematically, we know that for functions using non-differentiable parts like ReLU, AD is correct "almost everywhere"—meaning the set of points where it fails has a Lebesgue measure of zero.

However, computers don't work with the infinite continuum of real numbers ($\mathbb{R}$). They work with machine-representable numbers ($\mathbb{M}$), like 32-bit floating-point numbers. Because $\mathbb{M}$ is a finite, discrete set, it technically has a Lebesgue measure of zero itself. This creates a theoretical crisis: if the set of "bad" points is measure-zero and the set of all possible computer inputs is also measure-zero, could AD be wrong on every single input a computer can actually represent?

The authors tackle this by analyzing the "Incorrect Set" (where the function is differentiable but AD gives the wrong answer) and the "Non-differentiable Set" (where the function has no standard derivative) specifically within the space of machine-representable parameters.

Methods

The paper employs rigorous mathematical analysis and proofs to characterize the behavior of AD in neural networks. The authors define a neural network as a composition of alternating analytic pre-activation functions (like fully-connected or convolutional layers) and pointwise continuous activation functions (like ReLU, LeakyReLU, or Sigmoid).

They split their analysis into two main scenarios:

  1. Networks with bias parameters at every layer: A common setup in practice.
  2. General networks: Networks that might lack bias parameters or use "well-structured biaffine" layers.

The study focuses on extended derivatives—the values AD systems choose to return at non-differentiable points (e.g., returning 0 as the "derivative" of ReLU at $x=0$).

Datasets

As this is a theoretical paper focused on mathematical proofs, no specific datasets were used for training or evaluation.

However, the authors discuss their results in the context of prior empirical work (Bertoin et al., 2021) that used datasets like MNIST, CIFAR-10, SVHN, and ImageNet to estimate the density of "bifurcation zones" (areas where AD choice matters). The authors use their theory to explain why those empirical studies found such low error rates when using high-precision (64-bit) numbers.

Key Insights and Ideas

  • The Power of Biases: The most striking finding is that having bias parameters at every layer makes the "Incorrect Set" completely empty. In other words, if your network has biases and is differentiable at a certain parameter setting, AD is guaranteed to compute the mathematically perfect derivative, even on a computer.
  • Efficiently Deciding Differentiability: Generally, determining if a neural network is non-differentiable at a point is an NP-hard problem. However, the authors show that for networks with biases, this becomes computationally easy. You can check it efficiently using a single pass of reverse-mode AD.
  • The "Cheap Subgradient" Principle: The paper proves that for networks with biases, AD always computes a Clarke subderivative (a generalized notion of a derivative) even at points where the network isn't differentiable. This provides a formal theoretical foundation for why training still works even when we hit the "corners" of functions like ReLU.
  • Density Bounds: For general networks, the authors provide a tight bound on the density of "bad" points. The probability of hitting a point where AD fails is roughly $n/|\mathbb{M}|$, where $n$ is the number of neurons and $|\mathbb{M}|$ is the total number of representable values (e.g., $2^{32}$).

Key Results

  • Theorem 3.2 (Correctness): For networks with bias parameters, the Incorrect Set is empty ($|inc_{\Omega}(z_L)| = 0$). AD is always correct on differentiable points.
  • Theorem 3.3 (Density): The density of the non-differentiable set is bounded by the total number of non-differentiable points in the activation functions divided by the size of the machine-representable set. For 32-bit floats, this density is incredibly low (roughly $1$ in $4$ billion per neuron).
  • Theorem 3.6 (Clarke Subderivative): If AD uses "consistent" extended derivatives (like the standard 0 or 1 for ReLU at zero), it is guaranteed to return a valid Clarke subderivative everywhere.

Limitations and Future Work

  • Pointwise Activations Only: The current proofs apply to pointwise activations. They do not yet cover operations like MaxPool or residual connections that bypass non-analytic functions.
  • Rounding Errors: The analysis assumes AD computes the chain rule perfectly without additional floating-point rounding errors during the intermediate steps of the derivative calculation itself.
  • Layer Types: Some common layers, like BatchNorm or Attention without explicit biases, do not fit the "bias parameter" definition used in the strongest proofs, though the authors provide some extensions for "well-structured biaffine" functions.
  • Future Directions: The authors suggest extending this work to cover rounding errors during AD computation and expanding the theory to cover a wider variety of non-pointwise layers.

Summary updated

Abstract

Recent work has shown that forward- and reverse- mode automatic differentiation (AD) over the reals is almost always correct in a mathematically precise sense. However, actual programs work with machine-representable numbers (e.g., floating-point numbers), not reals. In this paper, we study the correctness of AD when the parameter space of a neural network consists solely of machine-representable numbers. In particular, we analyze two sets of parameters on which AD can be incorrect: the incorrect set on which the network is differentiable but AD does not compute its derivative, and the non-differentiable set on which the network is non-differentiable. For a neural network with bias parameters, we first prove that the incorrect set is always empty. We then prove a tight bound on the size of the non-differentiable set, which is linear in the number of non-differentiabilities in activation functions, and give a simple necessary and sufficient condition for a parameter to be in this set. We further prove that AD always computes a Clarke subderivative even on the non-differentiable set. We also extend these results to neural networks possibly without bias parameters.
BibTeX
@inproceedings{lee2023correctness,
  author = {Wonyeol Lee and Sejun Park and Alex Aiken},
  title = {{On the Correctness of Automatic Differentiation for Neural Networks with Machine-Representable Parameters}},
  booktitle = {International Conference on Machine Learning},
  year = {2023},
  url = {https://openreview.net/forum?id=Y0bPRkGJgP},
}
Figure