Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Inversion strategies

Bisection against Newton, the safeguarded hybrid, the gradient of a root, and a whole batch at once

04 · Inversion strategies

Open In Colab

Every smooth marginal of this part inverts the same way: given a level τ\tau, solve the root problem (4) for xx. Part 0, notebook 02 solved it by bisection and showed that sample pays for it. This notebook asks how to solve it well, how to differentiate the answer, and how to run it on a whole batch.

Python implementation: CPython
Python version       : 3.13.12
IPython version      : 9.12.0

jax             : 0.12.1
numpy           : 2.4.3
matplotlib      : 3.11.2
optimistix      : 0.1.0
gauss_flows_maps: 0.3.0

Compiler    : Clang 21.1.4 
OS          : Linux
Release     : 6.8.0-1044-azure
Machine     : x86_64
Processor   : x86_64
CPU cores   : 16
Architecture: 64bit

We reuse the three-component mixture CDF of notebook 02, (2), with weights (0.4,0.35,0.25)(0.4, 0.35, 0.25), means (−1.5,0.3,2.0)(-1.5, 0.3, 2.0) and scales (0.4,0.6,0.5)(0.4, 0.6, 0.5). Its derivative F′(x)F'(x) is the mixture density, which Newton’s method needs and bisection does not.

1. Bisection against Newton

Bisection halves a bracket around the root at every step, so its error bound (5) falls by a factor of two per step: linear convergence, about one bit of the answer per step. It needs only the sign of F(x)−τF(x) - \tau, and for an increasing FF it cannot fail.

Newton’s method uses the slope as well. From the iterate x(n)x^{(n)} it follows the tangent of FF to the level τ\tau,

x(n+1)=x(n)−(F(x(n))−τ)/F′(x(n))x^{(n+1)} = x^{(n)} - (F(x^{(n)}) - \tau) / F'(x^{(n)})

and near a root with F′(x⋆)>0F'(x^\star) > 0 the error roughly squares at every step,

∣x(n+1)−x⋆∣≤C∣x(n)−x⋆∣2\vert x^{(n+1)} - x^\star \vert \le C \vert x^{(n)} - x^\star \vert^2

where CC is about ∣F′′(x⋆)∣/(2F′(x⋆))\vert F''(x^\star) \vert / (2 F'(x^\star)). Quadratic convergence doubles the number of correct digits per step. The catch is the word “near”: (1) has no bracket, and where the density F′F' is tiny, in a flat tail of the CDF, one tangent step can throw the iterate far away.

We record the residual ∣F(x(n))−τ∣\vert F(x^{(n)}) - \tau \vert of both methods at τ=0.7\tau = 0.7, and of Newton aiming at the extreme level τ=0.999\tau = 0.999 from the start x(0)=−8x^{(0)} = -8, deep in the left tail.

Newton’s path also records each iterate, so we can see where a bad start sends it.

We count the steps each method needs to bring the residual below 10-12.

τ = 0.7, bisection reaches a residual below 1e-12 at step 41
τ = 0.7, Newton    reaches a residual below 1e-12 at step 5
Newton residuals: 2e-01, 3e-03, 3e-05, 3e-09, 1e-16, 1e-16
τ = 0.999 from x⁽⁰⁾ = -8, Newton iterates: -8, 1.54e+42, -inf, nan, nan, nan, nan, nan
<Figure size 1200x420 with 2 Axes>

Figure 1:Left: at τ=0.7\tau = 0.7 Newton’s residual (green) falls to machine precision in a handful of steps, while bisection (blue) gains about one bit per step. Right: Newton aiming at τ=0.999\tau = 0.999 from x(0)=−8x^{(0)} = -8, where the density is almost zero; the first tangent step overshoots to about 1042 and the iteration ends in nan (shaded).

The two panels are the whole trade-off. Bisection is slow and certain; Newton is fast and conditional on a good start. A good start is exactly what a flow cannot promise, because its sampling direction feeds the inverse every level of (0,1)(0, 1), extreme ones included.

2. The safeguarded hybrid

The standard fix keeps both: a bisection bracket [an,bn][a_n, b_n] that always contains the root, and a Newton proposal x~(n+1)\tilde{x}^{(n+1)}, the right-hand side of (1), that is accepted only if it lands strictly inside the bracket Brent, 1973:

x(n+1)={x~(n+1)if an<x~(n+1)<bn(an+bn)/2otherwisex^{(n+1)} = \begin{cases} \tilde{x}^{(n+1)} & \text{if } a_n < \tilde{x}^{(n+1)} < b_n \\ (a_n + b_n) / 2 & \text{otherwise} \end{cases}

After each step the bracket shrinks to the side of x(n+1)x^{(n+1)} where F(x)−τF(x) - \tau changes sign. The bracket always contains the root, so the hybrid cannot diverge, and once the iterate is near the root every proposal is accepted and convergence is quadratic. The rule does not promise bisection’s rate, though: a Newton step that lands inside the bracket need not halve it, so on a CDF with a long flat stretch, such as F(x)=x10F(x) = x^{10} on [0,1][0, 1] at τ=10−12\tau = 10^{-12}, accepted Newton steps creep towards the root and the hybrid takes about twice as many evaluations as bisection. Brent’s method Brent, 1973 closes this gap with a sufficient-progress check that bisects whenever the interpolation steps stop shrinking the bracket fast enough.

The hybrid then tries Newton at every step and falls back to the midpoint whenever the proposal leaves the bracket, for at most 40 steps or until the residual is below 10-14.

safeguarded Newton, τ = 0.999 from x⁽⁰⁾ = -8: residual 1.1e-16
after 10 steps, step kinds (B = bisection, N = Newton): BNNNNNNNNN

gauss_flows ships a derivative-free member of the same family. monotone_inverse defaults to Chandrupatla Chandrupatla, 1997, an optimistix solver that keeps a bracket like bisection but proposes inverse quadratic interpolation steps through the last three iterates instead of Newton steps, and falls back to bisection whenever the interpolation is not trustworthy. We count the steps each optimistix solver takes to a tolerance of 10-12 at the easy level and at the hard one.

We run both solvers at the easy level and at the hard one.

   Bisection  τ = 0.7    steps  44  residual 9.5e-14
   Bisection  τ = 0.999  steps  43  residual 2.1e-15
Chandrupatla  τ = 0.7    steps  11  residual 1.1e-16
Chandrupatla  τ = 0.999  steps  14  residual 1.1e-16
<Figure size 800x420 with 1 Axes>

Figure 2:The level τ=0.999\tau = 0.999 and the start x(0)=−8x^{(0)} = -8 that sent plain Newton to nan in Figure 1. The safeguarded hybrid (3) (purple) stays finite: its first proposal falls outside the bracket and is replaced by a bisection step, after which every Newton step is accepted and the residual collapses within ten steps. Bisection (dashed) is shown for reference.

3. Differentiating the inverse

Training a flow through its sampling direction, or fitting a marginal whose inverse sits inside a loss, needs the derivative of the root x⋆x^\star with respect to the level τ\tau and to the parameters θ\theta of FF (its weights, means and scales). The root satisfies F(x⋆;θ)=τF(x^\star; \theta) = \tau for every τ\tau and θ\theta, and differentiating that identity gives the implicit-function gradients Blondel et al., 2022,

∂x⋆/∂τ=1/F′(x⋆)\partial x^\star / \partial \tau = 1 / F'(x^\star)
∂x⋆/∂θ=−∂θF(x⋆;θ)/F′(x⋆)\partial x^\star / \partial \theta = -\partial_\theta F(x^\star; \theta) / F'(x^\star)
Why

Differentiate F(x⋆(τ,θ);θ)=τF(x^\star(\tau, \theta); \theta) = \tau with the chain rule. In τ\tau, F′(x⋆)∂x⋆/∂τ=1F'(x^\star) \partial x^\star / \partial \tau = 1, which is (4). In θ\theta, F′(x⋆)∂x⋆/∂θ+∂θF(x⋆;θ)=0F'(x^\star) \partial x^\star / \partial \theta + \partial_\theta F(x^\star; \theta) = 0, which is (5). Both need only the root itself, not the path the solver took to it.

How a program reaches these numbers depends on the solver, and there are four common routes.

RouteWhat is differentiatedGradientMemory
unroll bisectionevery where(F(mid) < τ) stepzero, wronggrows with the steps
unroll Newtonevery step (1)correctgrows with the steps
one stepone Newton step from the detached root (6)correctconstant
implicit adjointthe identity F(x⋆;θ)=τF(x^\star; \theta) = \tau, by a linear solvecorrectconstant

Unrolled bisection fails because its output depends on τ\tau and θ\theta only through comparisons, which are piecewise constant, so automatic differentiation sees a derivative of zero everywhere. The one-step route repairs it cheaply. Write sg\mathrm{sg} for the stop-gradient operator, which passes a value through and blocks its derivative, and take one Newton step from the detached root:

x^=sg(x⋆)−(F(sg(x⋆))−τ)/F′(sg(x⋆))\hat{x} = \mathrm{sg}(x^\star) - (F(\mathrm{sg}(x^\star)) - \tau) / F'(\mathrm{sg}(x^\star))
Why one step is enough

At the root F(x⋆)=τF(x^\star) = \tau, so x^=x⋆\hat{x} = x^\star to solver precision and the value is unchanged. Its derivative in τ\tau is 1/F′(x⋆)1 / F'(x^\star), which is (4). Its derivative in θ\theta is −∂θF(x⋆;θ)/F′(x⋆)-\partial_\theta F(x^\star; \theta) / F'(x^\star) plus a term proportional to the residual F(x⋆)−τF(x^\star) - \tau, which vanishes at the root, so it is (5).

We differentiate all four routes in τ\tau on a grid of levels and compare with (4).

The two unrolled routes trace every step of their loop, so automatic differentiation follows each comparison or tangent step.

We differentiate each route at 40 levels and keep the implicit-function value as the reference.

unrolled bisection: max |∂x⋆/∂τ - 1/F'(x⋆)| = 1.0e+01, max |∂x⋆/∂τ| = 0.00
   unrolled Newton: max |∂x⋆/∂τ - 1/F'(x⋆)| = 6.0e-12, max |∂x⋆/∂τ| = 9.99
          one step: max |∂x⋆/∂τ - 1/F'(x⋆)| = 0.0e+00, max |∂x⋆/∂τ| = 9.99
  implicit adjoint: max |∂x⋆/∂τ - 1/F'(x⋆)| = 3.6e-15, max |∂x⋆/∂τ| = 9.99
<Figure size 800x440 with 1 Axes>

Figure 3:The derivative of the inverse in the level, ∂x⋆/∂τ\partial x^\star / \partial \tau, by each route, against the implicit-function value (4) (black). The adjoint, the one-step route and unrolled Newton lie on the curve; unrolled bisection (red, dashed) is zero everywhere.

Unrolled Newton is correct here but keeps every iterate for the backward pass, and it inherits Newton’s fragility from section 1. The one-step and adjoint routes cost one evaluation of F′F' at the root whatever the number of solver steps, which is why libraries use them.

What gauss_flows does today

Both inverses in gauss_flows differentiate correctly. MixtureGaussianCDF.inverse brackets the root by bisection and then takes the one step (6) from the detached root; monotone_inverse runs Chandrupatla through optimistix.root_find with the implicit adjoint. Older versions returned the bare unrolled bisection and so a zero gradient, which gauss_flows#111 reported and fixed. We check both: the level derivative of a fitted MixtureGaussianCDF, against the reciprocal of its forward derivative, and the derivative of a monotone_inverse root in the means μm\mu_m, against (5).

We invert one latent value z=0.7z = 0.7 and compare the derivative through the inverse with the reciprocal of the forward derivative at the root.

MixtureGaussianCDF: ∂x/∂z through .inverse  = 1.161899
                    1 / (∂z/∂x) at the root = 1.161899

The same inverse must also pass gradients to every parameter θ\theta of the mixture.

                    every parameter gradient nonzero: True

For monotone_inverse we differentiate the root in the means μm\mu_m and compare with the implicit-function value.

monotone_inverse:   ∂x⋆/∂μ = [0.       0.880307 0.119693]
eq-inv-dtheta:      ∂x⋆/∂μ = [0.       0.880307 0.119693]

The two pairs agree to the printed digits. The derivative in the means also reads naturally: at τ=0.7\tau = 0.7 the root sits near the second component, so moving its mean moves the root most, and the first component, far to the left, barely matters.

4. A whole batch at once

A flow inverts every coordinate of every sample in one call. We write the scalar solve once and jax.vmap it, which traces it once and compiles the batch into one kernel, and compare with a Python loop that calls the same compiled scalar solve once per level. monotone_inverse needs no vmap at all, since it takes an elementwise FF and an array of levels.

Each solve is compiled once before we time it.

vmap, one kernel:             4.09 µs per level
monotone_inverse, array:      1.84 µs per level
Python loop, per element:  1392.55 µs per level
the loop is 340 times slower per level than vmap
batched accuracy: max |F(x⋆) - τ| = 2.7e-13
<Figure size 700x400 with 1 Axes>

Figure 4:Time per inversion on this machine, log scale. The vmapped solve and monotone_inverse on an array each run as one compiled kernel; the Python loop pays a dispatch per level for the same compiled scalar solve. The absolute numbers depend on the hardware; the gap does not go away.

The loop is slower per level by the factor printed above, for exactly the same arithmetic. A marginal layer on DxD_x coordinates does the same thing with one more batch axis, so the inverse of a whole flow layer is still one kernel.

Recap

ConceptEquationIn code
the root problem and bisection(4), (5)optimistix.Bisection
Newton, quadratic near the root(1), (2)Figure 1
the safeguarded hybrid(3)Chandrupatla, monotone_inverse
gradient of the root(4), (5)optimistix.ImplicitAdjoint
one Newton step from the detached root(6)MixtureGaussianCDF.inverse
a batch in one kernelnonejax.vmap, monotone_inverse on an array

Next. Part 1 can now build, fit, train and invert a single one-dimensional marginal. Part 2 puts rotations between the marginals, so that a stack of them can remove dependence between coordinates, the second half of the layer (3).

References
  1. Brent, R. P. (1973). Algorithms for Minimization Without Derivatives. Prentice-Hall.
  2. Chandrupatla, T. R. (1997). A new hybrid quadratic/bisection algorithm for finding the zero of a nonlinear function without using derivatives. Advances in Engineering Software, 28(3), 145–149. 10.1016/S0965-9978(96)00051-8
  3. Blondel, M., Berthet, Q., Cuturi, M., Frostig, R., Hoyer, S., Llinares-López, F., Pedregosa, F., & Vert, J.-P. (2022). Efficient and Modular Implicit Differentiation. Advances in Neural Information Processing Systems (NeurIPS).