open source · MIT

clarke-ad

Automatic differentiation for nonsmooth functions.
abs, max, min, floor — done right.

// Standard AD gives 0 at x=0 for |x|  ← WRONG
// Clarke-AD gives the correct subgradient set
∂|x| at x=0 → [-1, 1]  ✓

Why clarke-ad

Honest at kinks

Standard AD silently gives wrong derivatives at abs, max, floor. Clarke-AD returns the correct subgradient set.

Branch tracking

Records which branch each nonsmooth function takes. When a kink is crossed, the subgradient becomes a convex set.

Optimization ready

Subgradient methods, proximal operators, Tikhonov regularization — all work with nonsmooth objectives.

Learn: What is clarke-ad?

The Problem

Standard automatic differentiation (AD) computes derivatives by propagating dual numbers through a computation graph. It works perfectly for smooth functions. But many real-world functions are nonsmooth:

  • |x| — absolute value has a kink at x=0
  • max(x, 0) — ReLU activation in neural networks
  • floor(x) — quantization, integer constraints
  • sign(x) — contact mechanics, friction

At these kinks, standard AD returns wrong answers — usually 0 or an arbitrary one-sided derivative. This breaks gradient-based optimization.

The Solution: Clarke Subgradients

In 1975, Frank Clarke introduced generalized gradients for nonsmooth analysis. Instead of a single derivative at a kink, we get a convex set of possible subgradients:

∂f(x₀) = conv{lim ∇f(xᵢ) : xᵢ → x₀, xᵢ ∉ Ωf}

At smooth points: ∂f(x₀) = {f'(x₀)} (just the derivative)
At kinks: ∂f(x₀) is a convex set (an interval in 1D)

Key subgradients

functionsubgradient at x₀explanation
|x| at x=0[-1, 1]left = -1, right = +1
max(x, 0) at x=0[0, 1]ReLU kink
min(x, 0) at x=0[-1, 0]flipped ReLU
floor(x) at integer[-∞, ∞]distributional

Who is this for?

Robotics engineers

Contact/impact mechanics use sign and abs. Standard AD gives wrong forces. Clarke-AD gives correct subgradients for stable simulation.

ML researchers

ReLU networks, hinge loss, quantization — all nonsmooth. Clarke-AD gives honest gradients at kinks for better training.

Control engineers

Saturation, thresholds, argmin compositions are everywhere in control. Clarke-AD handles them correctly.

Optimization developers

Subgradient methods, proximal operators, bundle methods — all need correct subgradients. Clarke-AD provides them.

How to use it

Rust library: Use clarke-core for dual numbers, clarke-nonsmooth for kink detection.

use clarke_nonsmooth::subgrad_abs;
let s = subgrad_abs(0.0); // [-1, 1]

Web demo: Open the demo, see subgradients computed live in WASM.

Optimization: Use clarke-opt for subgradient and proximal methods.

How it works

The Clarke subgradient at a kink is the convex hull of left and right limits:

∂f(x₀) = conv{∇f(x₀⁻), ∇f(x₀⁺)}

For |x| at x=0:
  ∂|x|(0) = conv{-1, +1} = [-1, 1]

For composed functions, branch tracking records which path each operation takes.

Install

curl
cargo
wasm
curl -sSf https://clarke.jesed.dev/install.sh | sh
cargo install clarke-ad --git https://github.com/jesedv/clarke-ad.git
wasm-pack build crates/clarke-wasm --target web
cd ui && npm install && npm run dev