Numerical behaviour¶
This page states what Optora guarantees for each floating-point dtype and what happens with non-finite inputs. It covers every ambiguity set and every divergence. Each row below is backed by the tests it names.
Dtype support¶
Every routine computes in a working dtype, which is the type promotion of
its inputs raised to at least float32
(working_dtype). The result is returned in the
plain promotion of the inputs, without that floor, which is what any
PyTorch operation on them would return. For an ambiguity set the inputs are
loss and nominal. For a divergence they are p and q.
| Input dtype | Computed in | Returned as | Accuracy | Tested by |
|---|---|---|---|---|
float64 |
float64 |
float64 |
The reference precision. It agrees with the independent LP and closed-form references within an absolute tolerance of \(10^{-9}\). | tests/dro/test_oracles.py |
float32 |
float32 |
float32 |
The result is within \(128\,\varepsilon \cdot \max(\lvert v \rvert,\ \max \ell - \min \ell)\) of a float64 solve of the same inputs, where \(v\) is the worst case. Typical errors are a few \(\varepsilon\). MMDAmbiguitySet adds the condition number of its Gram matrix to the 128, as described in the exceptions below. |
tests/dro/test_ambiguity_set_device_dtype.py |
bfloat16 |
float32 |
bfloat16 |
It is exactly the float32 result rounded once. | tests/dro/test_ambiguity_set_device_dtype.py, tests/divergences/test_divergence_device_dtype.py |
float16 |
float32 |
float16 |
It is exactly the float32 result rounded once. | as bfloat16 |
| mixed | the promotion, at least float32 |
the promotion | Same as its working dtype. | as bfloat16 |
Here \(\varepsilon\) is the machine epsilon of the input dtype, which is \(2^{-23} \approx 1.2 \times 10^{-7}\) for float32.
Why half precision is computed in float32
Computing a worst case involves long reductions, a bisection whose trip
count is set by the dtype, and differences of nearly equal sums. An
8-bit (bfloat16) or 11-bit (float16) significand loses most of the
accuracy of these steps. When half precision was computed natively,
the result was off by up to about \(2 \times 10^4\) units in the last
place (Wasserstein, float16) and violated the bound
\(\mathbb{E}_{\mathrm{nominal}}[\ell] \le v \le \max \ell\). Computing in
float32 and rounding once leaves only the final rounding error. This is
the same policy that
torch.autocast applies to
logsumexp, softmax and cumsum.
The following guarantees are also tested.
- Gradients. The gradient with respect to a half-precision
losshas the dtype ofloss. It is exactly the float32 gradient rounded once. - Autocast.
torch.autocastdoes not change a worst case. The working dtype already decides the precision. - Half-precision sets. A set whose buffers were cast with
.to(torch.bfloat16)solves in float32 from those rounded buffers. A float32 set given a half-precisionlossreturns float32.
The figure below measures the float32 row when this site is built. It uses a random support of 256 scenarios and evaluates every set at seven fractions of its saturation radius.
Three documented exceptions
- Mass far below \(\varepsilon\). When the worst case concentrates on
scenarios with tiny nominal mass, for example \(10^{-20}\) in float32,
the tilt \(t\) lies near the upper end of its bracket \([0, 1)\). The
tilt rate is then resolved only to a relative
\(\varepsilon / (1 - t)\). KL and \(\chi^2\) errors grow to about
\(10^{-4}\) relative (tested at \(2048\,\varepsilon\) in
tests/dro/test_tilt_robustness.py). PhiAmbiguitySetwith adual_solver. Its accuracy is set by that solver, in every dtype. Without one, which is the default, it is solved exactly and held to the same bounds as the other tilted sets.MMDAmbiguitySetin float32. Its float32 error is that of factoring the Gram matrix \(K\) in float32, so the bound is \((128 + \kappa(K))\,\varepsilon \cdot \max(\lvert v \rvert,\ \max \ell - \min \ell)\), with \(\kappa(K)\) the ratio of its largest to its smallest eigenvalue. A tighter duality gap does not reduce this error, which is the same for gaps from \(10^{-3}\) to \(10^{-10}\). Over 428 test problems the error stayed below half of that bound. Support points that nearly coincide make \(\kappa(K)\) large, however, and at a small radius the error can then exceed the robust excess \(v - \mathbb{E}_{\mathrm{nominal}}[\ell]\) itself. Such balls should be solved in float64.
ambiguity_set = MMDAmbiguitySet(nominal.double(), gram.double(), radius)
robust_loss = ambiguity_set.worst_case_expectation(loss.double())
The gradient reaches a float32 loss through the cast. Forcing
float64 inside the set would break the precision policy above and
fail on accelerators without float64 support.
Second derivatives in float32 carry one more caveat. The Hessian is piecewise constant and jumps where the maximizer's support changes, and a converged float32 solve distinguishes a scenario's mass from its multiplier only down to the square root of its duality-gap floor over the support size, about \(10^{-3}\) to \(10^{-4}\). A scenario whose mass is near that level can fall on either side of the jump, and the Hessian then differs from float64 by order one in that direction.
Supported loss range¶
A loss must be finite, and its spread \(\max \ell - \min \ell\) must be finite
in the working dtype. In float32 that means \(\lvert \ell \rvert\) below about
\(1.7 \times 10^{38}\). A half-precision loss always qualifies, since it is
solved in float32. WassersteinAmbiguitySet also needs the spread divided
by the smallest positive cost difference to stay finite.
Non-finite inputs and validation¶
Metadata checks need no host synchronization and always run. Checks that
read tensor values back to the host block until the device has produced
them, so they run only with validate=True, in the spirit of the
validate_args flag of torch.distributions.
| Input | Default (validate=False) |
validate=True |
|---|---|---|
nan or \(\pm\infty\) entry in loss |
That batch element is nan or infinite, never a finite number. Every other batch element is unchanged. |
ValueError on every call, at one host synchronization per call. |
Negative or nan float radius |
ValueError |
ValueError |
Infinite float radius |
Allowed. The worst case is the largest reachable loss. | Allowed. |
Negative or nan tensor radius |
Not checked. The result is meaningless. | ValueError at construction. |
Invalid nominal |
Not checked. | ValueError at construction. |
These cases are tested by tests/dro/test_non_finite_losses.py and
tests/core/test_radius_and_loss_validation.py.
Turn validate=True on while developing a model. Turn it off for training
on an accelerator, where a host synchronization per step reduces
throughput.
Stability of the KL tilt¶
KLAmbiguitySet finds its worst case \(q_\beta \propto
\mathrm{nominal} \cdot e^{-\beta\,\mathrm{gap}}\) by bisecting the tilt rate
\(\beta\). The gap \(\mathrm{gap} = (\max \ell - \ell)/(\max \ell - \min \ell)\)
lies in \([0, 1]\) and is zero on the highest loss. Three choices keep every
step finite and accurate.
- Shifted exponent. The largest exponent is exactly zero, which is the log-sum-exp shift, so no weight overflows however large \(\beta\) becomes.
- Log-domain normalization. \(\log Z = \operatorname{logsumexp}(\log \mathrm{nominal} - \beta\,\mathrm{gap})\). A scenario whose nominal mass and weight would underflow as a product still gets its correct tilted mass.
-
Two forms of the divergence. The divergence has two algebraically equal forms, each accurate in its own regime,
\[ D_{\mathrm{KL}}(q_\beta \,\|\, \mathrm{nominal}) = \begin{cases} -\log\!\big(1 + \mathbb{E}_{\mathrm{nominal}}[e^{c} - 1]\big), & D \le 1, \\[4pt] -\beta\,\mathbb{E}_{q_\beta}[\mathrm{gap}] - \log Z, & D > 1, \end{cases} \qquad c = -\beta\big(\mathrm{gap} - \mathbb{E}_{q_\beta}[\mathrm{gap}]\big). \]The first form, read through
expm1andlog1p, resolves a vanishing radius without cancellation. The second takes over for large divergences. There the first would represent \(e^{-D}\) as one plus a sum near \(-1\), which rounds to exactly \(-1\) once \(D\) exceeds about 16 in float32 or 36 in float64. The second form also takes over when the largest centered exponent \(\beta\,\mathbb{E}_{q_\beta}[\mathrm{gap}]\) exceeds half of the logarithmic range of the dtype, beyond whichexpm1of the centered exponents could overflow. That requires a highest-loss nominal mass below \(e^{-44}\) in float32.Choosing the form adds no work per bisection step, because both forms read quantities that the tilt has already computed.