arXiv:2611.08637math.OCNov 2026

Manuscript Offprint

arXiv:2611.08637

Branch-Extended Automatic Differentiation

Jan Hückelheim, Jeffrey Larson, Matt Menickelly

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

We present branch-extended automatic differentiation (BEAD), a software package that provides information about inactive branches of piecewise-differentiable functions, intended for use in nonsmooth optimization algorithms. BEAD is implemented in Python, building on the popular JAX framework. For supported nonsmooth operations such as abs and max, BEAD identifies alternative branches within tolerance from the given input, and computes gradients of the resulting branches extended to the given input. Such information has been previously used for robust optimization methods, but has required error-prone and tedious manual differentiation, unlike BEAD, which is fully automated for functions written in NumPy-like JAX code. Our experiments show that our approach is as accurate as manually written derivative code, and offers more flexibility in modifying the function and choosing between branch-discovery rules. While we were originally motivated by the manifold sampling algorithm for nonsmooth composite objectives, we also integrate BEAD branch-extension gradients into a nonsmooth BFGS-SQP solver. On two small nonsmooth test problems, the resulting path-aware search resolves failures or substantially reduces the number of evaluations required by the BFGS-SQP solver.

Keywords: Automatic Differentiation, Branch Extensions, Nonsmooth Optimization, Piecewise-Smooth Functions

1 Introduction

Automatic differentiation (AD) applies the chain rule of calculus to functions expressed as computer programs and is a standard way to obtain derivatives without manually writing or maintaining separate derivative code [1]. For a smooth program, differentiating its execution trace produces the derivative of the represented function (with some caveats in the presence of roundoff, iterative solvers, and other implementation details, see [2] for an overview). Scientific codes, however, often involve continuous but nonsmooth operations such as an absolute value, a pointwise minimum or a pointwise maximum. When such operations appear as primitives in a function, the function typically becomes piecewise smooth; derivatives do not exist in the mathematical sense at inputs that lie exactly on a kink in the piecewise smooth structure. In a computer program, such nonexistence is often represented at a low abstraction level as a branch that switches control flow at such inputs. At higher abstraction levels, this nonexistence occurs at branch points, an intermediate (set of) value(s) at which a primitive enters one of several mathematically valid branches in defining the primal function.

*jhuckelheim@anl.gov

†jmlarson@anl.gov

‡mmenickelly@anl.gov

As a motivating example of branches and branch points, consider an implementation of the maximum of two scalars aa and bb:

def max(a, b): if (a>=b) return a else return b

This code consistently returns aa when a=ba = b, an arbitrary branch: an equally valid implementation returns bb instead:

def max(a, b): if (a<=b) return b else return a

For every instance where the max primitive appears in a program, the forward execution consistently follows one of two branches. Conventional AD evaluates the derivative of the primal function determined by the corresponding execution trace. However, users of the derivative program, especially practitioners of optimization, may want information about alternative branches not taken by the implementation. We will clarify our intentions later, but note that the execution trace associated with alternative branches may have vastly different derivatives [3, 4]. A user may also want derivatives corresponding to alternative branches that were nearly followed within a prescribed tolerance; that is, for small eps > 0, replacing the first implementation of the max primitive with

def max(a, b): if (a + eps >=b) return a else return b

would have admitted an alternative branch for the same function input.

To overcome this limitation of conventional AD, we introduce branch-extended automatic differentiation (BEAD). At a given function input, BEAD records the branch taken at supported nonsmooth primitives such as abs and max, identifies alternative branches within a prescribed tolerance, and replays the program once per path through branches—an assignment of one branch at every branch point—thereby locking those branches. Each replay defines a smooth branch extension at the query input, differentiable by ordinary JAX AD [5]. The output is a set of branch-extension gradients rather than only the gradient of the default path. BEAD returns tolerance- and program-dependent branch-extension gradients; these need not form a classical generalized derivative, as discussed in Section 2.2.

Our implementation and test cases are mostly motivated by manifold sampling, a derivative-free optimization method that relies on composite objective functions of a certain form, discussed further in Section 2. However, BEAD's applicability is not specific to composite objectives or derivative-free optimization, and we demonstrate some preliminary results with scalar-valued nonsmooth functions in the context of the constrained nonsmooth solver PyGRANSO [6, 7] in Section 4.2. BEAD supports both local tolerances on intermediate values and input-scaled tolerances based on first-order input sensitivities, as described in Section 3.2.

The contributions of this work are a record-and-replay formulation of BEAD, a JAX implementation for array operations that commonly appear in definitions of piecewise smooth functions, implementations of two branch-discovery rules, and an evaluation against manual implementations of branch-extension gradients from past work.

References (31)