SoftJAX & SoftTorch: Empowering Automatic Differentiation Libraries with Informative Gradients

Anselm Paulus, Andreas René Geist, Vít Musil, Sebastian Hoffmann, Georg Martius

Automatic Differentiation (AD) is the backbone of modern machine learning, allowing frameworks like JAX and PyTorch to optimize complex models via gradient descent. However, AD struggles with "hard" primitives—operations like sorting, ranking, boolean logic, and thresholding. These functions are either discontinuous or have zero gradients almost everywhere, halting gradient-based optimization in its tracks. If a loss function depends on the discrete order of elements in a list, the gradient is zero, and the optimizer cannot improve the model.

While researchers have developed "soft" relaxations to smooth these functions and provide informative gradients, implementations remain fragmented across disparate repositories. This fragmentation makes it difficult to combine techniques or compare performance. To solve this, researchers have introduced SoftJAX and SoftTorch, unified, open-source libraries designed for soft differentiable programming.

The SoftJAX and SoftTorch Solution

The authors introduce feature-complete, drop-in replacements for standard non-differentiable functions, categorized into two main areas:

  1. Elementwise Operators: The libraries relax the Heaviside step function (the foundation for abs, clip, and Boolean logic) using sigmoidal functions. They offer varying degrees of smoothness, from $C^0$ (continuous but not differentiable) to $C^\infty$ (infinitely differentiable), allowing users to tailor the geometry to their specific optimizer.
  2. Axiswise Operators: For complex operations like sort, rank, and top-k, the libraries implement several state-of-the-art algorithms:
  3. Optimal Transport (OT): Frames sorting as moving "mass" from an unsorted list to a sorted "anchor" grid.
  4. Simplex/Permutahedron Projections: Uses geometric projections to find a soft permutation matrix. This includes a novel variant called SmoothSort, which provides $C^\infty$ differentiability for permutahedron-based sorting.
  5. Differentiable Sorting Networks: Replaces the "compare-and-swap" operations in bitonic sorting networks with soft sigmoidal swaps.

Crucially, the libraries fully support Straight-Through Estimation (STE). This allows users to apply the exact "hard" operation during the forward pass—keeping simulations or logic exact—while routing the "soft" informative gradients during the backward pass to update parameters.

Key Insights & Innovations

  • The "STE Pitfall": The authors identify a critical issue where wrapping individual functions in straight-through estimators can cause vanishing gradients when those functions interact multiplicatively. They introduce a "composite" wrapping strategy to mitigate this.
  • Unified Softness: The libraries standardize the softness parameter ($\tau$) across methods. This allows users to easily tune the trade-off between approximation accuracy (closeness to the "hard" function) and gradient smoothness.
  • Sorting Networks for Speed: Benchmarks reveal that Differentiable Sorting Networks are significantly faster than optimization-based methods like OT, making them highly suitable for large-scale GPU applications.

Empirical Results & Applications

The libraries were rigorously evaluated across synthetic benchmarks and practical case studies:

  • Performance & Memory: Sorting networks emerged as the fastest soft method, taking only ~1.0 ms for an array of 4096 elements (just 3.8x slower than the non-differentiable baseline). Meanwhile, FastSoftSort proved the most memory-efficient by avoiding the creation of massive $n \times n$ permutation matrices.
  • Computer Vision: In a Four-digit MNIST Sorting task, soft operators enabled a Convolutional Neural Network (CNN) to achieve nearly 100% accuracy on short sequences, a task where traditional hard training fails to converge.
  • Robotics & Simulation: Applied to MuJoCo XLA (MJX) collision detection, the soft logic provided smooth, non-zero gradients across 3D mesh surfaces, eliminating the gradient "dead zones" present in hard logic.

Limitations & Future Directions

While powerful, soft differentiable programming has constraints. Certain methods, like Optimal Transport, scale quadratically ($O(n^2)$) in memory and time, limiting their use on massive arrays. Additionally, the softness parameter ($\tau$) is rarely a "set and forget" value; it often requires careful scheduling (e.g., decaying over time) to achieve optimal results, adding a hyperparameter to the training pipeline. Finally, the current libraries focus on deterministic relaxations. Incorporating stochastic methods, such as Gumbel-Softmax or perturbed optimizers, remains an exciting avenue for future expansion.

Summary updated

BibTeX
@inproceedings{paulus2026softjax,
  author = {Anselm Paulus and Andreas René Geist and Vít Musil and Sebastian Hoffmann and Georg Martius},
  title = {{SoftJAX \& SoftTorch: Empowering Automatic Differentiation Libraries with Informative Gradients}},
  booktitle = {International Conference on Machine Learning},
  year = {2026},
  url = {https://openreview.net/forum?id=RKHDV40omz},
}
Figure