Skip to main content
Back

Tübingen Researchers Fix AI's Gradient Dead Zone With SoftJAX and SoftTorch

SoftJAX and SoftTorch unify scattered gradient relaxation techniques into drop-in replacements for non-differentiable JAX and PyTorch operations

Pro

  • What: SoftJAX and SoftTorch are new open-source libraries providing differentiable drop-in replacements for non-differentiable JAX and PyTorch operations.
  • Problem solved: Operations like sort, argmax, round, and Boolean logic return zero/undefined gradients, breaking gradient-based optimization pipelines.
  • Key feature: Unified interface across 5 sorting algorithms, 4 smoothness modes, and full straight-through estimation support for 30+ operators.
  • Novel contribution: A new SmoothSort algorithm achieving C-infinity differentiability, plus the first explicit documentation and fix of the "STE pitfall" in composite functions.
  • Performance: Sorting network method runs at ~3.8x the hard baseline; FastSoftSort uses only 420 kB memory at n=4096 with linear scaling.
  • Code: Available at github.com/a-paulus/softjax and github.com/a-paulus/softtorch, installable via pip, Python 3.11+.

Gradient-based optimization is the engine behind modern machine learning, but it has a dirty secret: a surprising number of everyday operations simply break it. Functions like sort, argmax, round, and Boolean comparisons all return zero or undefined gradients, leaving optimizers with nothing to work with. Researchers have spent years inventing workarounds, but those fixes have been scattered across dozens of papers and one-off implementations. A new paper from researchers at the University of Tübingen and the Max Planck Institute consolidates all of it into two production-ready libraries: SoftJAX and SoftTorch.

The gradient dead zone

Automatic differentiation frameworks like JAX and PyTorch have enabled gradient-based optimization for a wide range of scientific fields, yet many "hard" primitives such as thresholding, Boolean logic, discrete indexing, and sorting operations yield zero or undefined gradients that are not useful for optimization. This is not a niche problem. It shows up constantly in practice:

  • Differentiable rendering using NeRFs and Gaussian splatting relies on soft rasterization to get gradients through discrete pixel assignments
  • Differentiable simulation in physics engines like MuJoCo XLA hits walls when collision detection uses hard comparisons and argmax
  • Structured prediction tasks like learning-to-rank need gradients through sort and top-k
  • Combinatorial optimization layers embedded in neural networks require smooth relaxations of discrete decisions

The classic example everyone knows is the dying ReLU problem: ReLU's zero gradient for negative inputs kills neurons during training, which is why SiLU and Softplus exist as smooth replacements. While numerous "soft" relaxations have been proposed that provide informative gradients, the respective implementations are fragmented across projects, making them difficult to combine and compare. Every team reinvents the wheel, and comparing methods across papers is nearly impossible without reimplementing everything from scratch.

What "soft" actually means

A soft surrogate is a differentiable approximation of a hard function, parameterized by a softness value tau (τ). The key properties are: it must be continuous and differentiable almost everywhere, it must provide non-zero gradients over the relevant domain, and it must recover the original hard function as τ approaches zero. Think of τ as a dial: turn it up for better gradient flow, turn it down to stay faithful to the original function's behavior.

The second core concept is straight-through estimation (STE). Sometimes you want the hard function in the forward pass (so your simulation stays physically valid) but a soft gradient in the backward pass (so optimization can proceed). STE does exactly this: it runs the hard function forward, but substitutes the soft function's gradient during backpropagation. The paper also identifies and fixes a subtle "STE pitfall" that has never been explicitly documented before: when two STE-wrapped functions interact multiplicatively, the product rule can reintroduce the original hard function as a multiplicative gate, causing gradients to vanish again. The fix is to apply STE to the composite function rather than each primitive individually.

A unified library, not a collection of hacks

SoftJAX and SoftTorch are open-source, feature-complete libraries for soft differentiable programming that provide a variety of soft functions as drop-in replacements for their hard JAX and PyTorch counterparts, including elementwise operators such as clip or abs, utility methods for manipulating Booleans and indices via fuzzy logic, and axiswise operators such as sort or rank based on optimal transport or permutahedron projections, with full support for straight-through gradient estimation.

The full operator set covers four categories:

  • Elementwise: abs, relu, clip, sign, round, heaviside
  • Axiswise: sort, argsort, max, min, top_k, rank, median, quantile and their arg-variants
  • Logical: logical_and, logical_or, any, all via fuzzy logic (soft Booleans as probabilities)
  • Selection: where, take_along_axis, dynamic_index_in_dim using soft probability distributions over indices instead of hard integer indices

All operators share a consistent interface: a mode argument controls smoothness class (smooth for C-infinity, c0/c1/c2 for progressively smoother piecewise polynomials), and a softness argument controls the τ parameter. The library is designed to require minimal user effort, by simply replacing the non-differentiable JAX function with the SoftJAX counterpart.

How sorting becomes differentiable

Sorting is the hardest case and the most instructive. The paper implements five distinct families of differentiable sort, each with different trade-offs:

  • Optimal Transport (OT): Frames sorting as moving probability mass from input values to sorted anchor points. Smooth but expensive at O(Tn²) via Sinkhorn iterations.
  • SoftSort: Approximates OT with a single row-wise simplex projection. Fast closed-form solution, but not fully smooth at ties.
  • NeuralSort: Derives soft permutation matrices from soft absolute differences. Fully smooth when combined with the library's soft abs. Default for sort, rank, quantile, and median.
  • FastSoftSort: Projects sorted ranks onto the permutahedron (the convex hull of all permutations of a vector) in O(n log n). Most memory-efficient, but cannot produce argsort or argrank.
  • SmoothSort (novel): A new variant introduced in this paper that adds entropic regularization to a dual permutahedron formulation, achieving C-infinity smoothness with dense Jacobians.
  • Sorting Networks: Replaces hard compare-and-swap operations in a bitonic sorting network with soft comparisons. Fastest soft method at ~3.8x the hard baseline for n=4096.
3D surface plots comparing gradient landscapes of NeuralSort vs SoftSort methods for the soft rank operator

The elementwise operators are all derived from a single root: the Heaviside step function. By replacing this with a sigmoidal approximation, you get soft versions of sign, abs, round, relu, clip, and all comparison operators. The paper proves formally (Theorem 1) that p-norm regularized projections achieve exactly C0, C1, or C2 smoothness depending on the choice of p, with entropic regularization giving C-infinity.

Benchmarks: speed vs. smoothness

The paper benchmarks all methods on an Nvidia RTX 3060 GPU for a full forward-backward pass of sj.sort:

MethodTime (n=4096)Memory scalingArgsort support
Hard baseline~0.26 msO(n)Yes
Sorting Network1.0 msO(n²) matrix trackedYes
SoftSort16 msO(n²)Yes
NeuralSort37 msO(n²)Yes
FastSoftSortModerate~420 kB at n=4096 (linear)No
SmoothSortSlowestO(n²) preprocessingNo

For most practical use cases, the sorting network is the go-to if you need speed, NeuralSort if you need full smoothness and argsort support, and FastSoftSort if memory is the bottleneck.

A real-world case study: robot collision detection

The paper demonstrates the library on MuJoCo XLA (MJX), one of the most widely used physics simulators in robotics research. MJX's mesh collision detection algorithm selects four polygon vertices using a chain of argmax calls and abs operations. Every one of those is non-differentiable, meaning gradients cannot flow through the collision geometry at all.

Converting the algorithm to SoftJAX required exactly three changes: replace jnp.argmax with sj.argmax, replace array indexing with sj.dynamic_index_in_dim, and replace jnp.abs with sj.abs. The result: smooth, non-zero gradients at all polygon vertices, compared to the hard version which only produces gradients at the currently selected vertices. The STE pitfall fix is also demonstrated here, applying straight-through estimation to the entire composite function rather than each primitive.

Polygon visualization showing soft point selection across vertices with softness parameter color scale

When to use it, and when not to

SoftJAX and SoftTorch are the right tool when you have a pipeline that contains discrete operations and you want to optimize through them end-to-end. Concrete scenarios:

  • Learning to rank with differentiable ranking losses
  • Differentiable physics or simulation with hard contact and collision logic
  • Feature selection or top-k masking inside a neural network
  • Optimizing rule-based systems with Boolean logic and thresholds
  • Robust regression using soft median or quantile losses

The main limitation is cost. Special care needs to be taken when using functions operating on indices, as the library relaxes the notion of an index into a distribution over indices, effectively modifying the shape of returned and accepted values. Axiswise operators like sort and rank can be 4x to 140x slower than their hard counterparts depending on the method chosen, and the O(n²) cost matrix materializes for most methods. For large arrays (n in the tens of thousands), only FastSoftSort and sorting networks are practical. The softness parameter τ also requires tuning: too large and the soft function diverges from the hard one; too small and gradients become numerically unstable.

Getting started

Code is available at github.com/a-paulus/softjax and github.com/a-paulus/softtorch. Both are installable via pip. The library requires Python 3.11+. SoftJAX follows JAX naming conventions; SoftTorch mirrors PyTorch's API (e.g., clip becomes clamp, equal becomes eq). The migration path from existing code is minimal: swap the import and add a softness parameter. For straight-through estimation, wrap the function with sj.st().

The real value here is not any single algorithm. Researchers have had NeuralSort, FastSoftSort, and OT-based ranking for years. What SoftJAX and SoftTorch deliver is a single, consistent, well-tested abstraction layer that makes it trivial to swap methods, tune softness, and combine operators without worrying about implementation details. That kind of infrastructure tends to quietly unlock a lot of research that was previously too painful to attempt.