Abstract
Automatic differentiation (AD) frameworks such as JAX and PyTorch have enabled gradient-based optimization for a wide range of scientific fields. Yet, many "hard" primitives in these libraries such as thresholding, Boolean logic, discrete indexing, and sorting operations yield zero or undefined gradients that are not useful for optimization. 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. This work introduces SoftJAX and SoftTorch, open-source, feature-complete libraries for soft differentiable programming. These libraries provide a variety of soft functions as drop-in replacements for their hard JAX and PyTorch counterparts. This includes (i) elementwise operators such as clip or abs, (ii) utility methods for manipulating Booleans and indices via fuzzy logic, (iii) axiswise operators such as sort or rank -- based on optimal transport or permutahedron projections, and (iv) offer full support for straight-through gradient estimation. Overall, SoftJAX and SoftTorch make the toolbox of soft relaxations easily accessible to differentiable programming, as demonstrated through benchmarking and a practical case study. Code is available at github.com/a-paulus/softjax and github.com/a-paulus/softtorch.
Introduction
Modern machine learning and scientific computing rely heavily on automatic differentiation (AD) to optimize complex models. However, standard AD frameworks struggle with "hard" programming primitives—operations like sort, argmax, round, and boolean comparisons (x > y). These operations typically have gradients that are zero almost everywhere or undefined, creating a "gradient desert" that halts optimization algorithms.
While various "soft" relaxations (e.g., using a sigmoid as a surrogate for a step function) have been proposed in literature, their implementations remain fragmented across different research codebases. SoftJAX and SoftTorch address this by providing a unified, feature-complete suite of differentiable surrogates as drop-in replacements for standard JAX and PyTorch functions.
Library Architecture and Primitives
The libraries are built on two primary pillars to bridge the gap between discrete logic and differentiability:
- Soft Surrogates: Hard functions are replaced with differentiable approximations ($f_\tau$) governed by a softness parameter $\tau$. As $\tau$ approaches zero, the function recovers its original discrete behavior.
- Straight-Through Estimation (STE): For applications requiring hard behavior in the forward pass (e.g., physical simulations) but soft gradients in the backward pass, the libraries provide robust STE support.
Elementwise and Logical Operators
The libraries offer a wide range of elementwise surrogates, including soft versions of sign, abs, round, relu, and clip. Furthermore, they implement fuzzy logic primitives to handle Boolean comparisons differentiably.
Axiswise Operations
The "heavy lifters" of the library are the axiswise operators like sort and rank. These are implemented using state-of-the-art algorithms, including Optimal Transport (OT), Unit Simplex Projections (NeuralSort, SoftSort), and Permutahedron Projections (FastSoftSort). The libraries also include differentiable bitonic sorting networks optimized for GPU performance.
Key Innovations
The "STE Pitfall" and Composite Gradients
A significant contribution of this work is identifying the "STE Pitfall." Applying the straight-through trick to individual terms in a product (e.g., relu(x) * relu(y)) can still result in zero gradients if one term evaluates to zero. SoftJAX introduces "composite STE" to ensure gradients flow correctly through such multiplicative gates, maintaining informative signals throughout the computation graph.
Smoothness Families and SmoothSort
The libraries provide a family of smoothness modes ($C^0, C^1, C^2, \text{and } C^\infty$), allowing users to select the level of differentiability required by their specific optimization algorithm. Additionally, the authors introduce SmoothSort, a new $C^\infty$ differentiable variant of the permutahedron projection that combines the efficiency of projection methods with the smoothness of OT-based approaches.
Evaluation and Results
The libraries were evaluated through synthetic benchmarks and a practical case study in MuJoCo XLA (MJX).
- Computational Efficiency: Sorting networks proved highly efficient, handling arrays of 4096 elements in approximately 1.0 ms—only ~3.8x slower than non-differentiable baselines.
- Memory Scaling: FastSoftSort demonstrated superior memory efficiency for large-scale inputs by avoiding the quadratic memory cost of $N \times N$ permutation matrices.
- Practical Utility: In the MJX case study, softening a complex collision detection subroutine transformed a non-differentiable process into one with smooth, informative gradients. This allowed for the optimization of vertex positions to precisely control contact areas, a task impossible with standard "hard" primitives.
Conclusion
SoftJAX and SoftTorch bridge the gap between discrete logic and differentiable programming. By unifying disparate soft relaxation techniques into a single, high-performance API, these libraries empower researchers to apply gradient-based optimization to a broader class of problems. Future work will focus on automated hyperparameter tuning for the softness parameter $\tau$ and achieving full feature parity across both JAX and PyTorch versions.
Code available at: github.com/a-paulus/softjax and github.com/a-paulus/softtorch
Summary updated