TL;DR
SoftJAX and SoftTorch are open-source, feature-complete libraries that provide drop-in, differentiable relaxations for "hard" non-differentiable operations like sorting, ranking, and thresholding in JAX and PyTorch. By unifying a fragmented landscape of mathematical workarounds into a single framework, these libraries enable seamless gradient-based optimization across physics simulations, graphics pipelines, and machine learning models.
What Problem Does This Paper Address?
Modern automatic differentiation (AD) frameworks like JAX and PyTorch are excellent at computing gradients for continuous mathematical functions. However, they fail when encountering discrete or "hard" operations—such as sorting, ranking, maximum selection, thresholding, and Boolean logic. These operations yield zero or undefined gradients, effectively breaking the chain rule and halting gradient-based optimization.
While researchers have proposed various "soft" mathematical relaxations over the years to circumvent this issue, the implementations remain highly fragmented across isolated codebases. This makes it incredibly difficult for practitioners to combine, compare, or deploy these techniques out of the box. This paper introduces SoftJAX and SoftTorch to bridge this gap, offering a unified, extensible toolbox of soft operators.
Methods
The libraries provide soft surrogates that approximate hard functions but remain differentiable, alongside full support for Straight-Through Estimation (STE).
The authors categorize their implementations into two main types:
- Elementwise Operators: Built by relaxing the discontinuous Heaviside step function using sigmoidal curves. Users can choose between standard exponential sigmoids (
smoothmode) or piecewise polynomial approximations (c0,c1,c2modes) to guarantee specific levels of differentiability. These form the basis for soft versions ofsign,abs,round,relu, andclip. - Axiswise Operators: Designed for multi-element operations like
sort,rank,top_k, andargmax. The libraries implement four distinct algorithmic families for these: - Optimal Transport (OT)-based: Formulating sorting as an optimal transport problem between inputs and sorted anchor points.
- Unit Simplex Projections: Approximations like SoftSort and NeuralSort that project values onto a simplex.
- Permutahedron Projections: Directly softening value-space operators via projections onto the permutahedron (e.g., FastSoftSort). The authors also introduce a novel variant called SmoothSort, which adds entropic regularization to a dual formulation to achieve $C^\infty$ smoothness and dense Jacobians.
- Sorting Networks: Replacing hard compare-and-swap operations in a bitonic sorting network with soft sigmoidal swaps.
Datasets
The paper does not use traditional machine learning datasets (like ImageNet or GLUE). Instead, the libraries are evaluated using:
- Synthetic Benchmarks: Arrays of varying sizes (from $2^2$ to $2^{12}$ elements) to evaluate runtime, JIT compilation time, and peak memory consumption.
- Physical Simulation Case Study: A mesh-mesh collision detection subroutine from MuJoCo XLA (MJX), a rigid-body physics simulator.
Key Insights and Ideas
- The "STE Pitfall": The authors identify a subtle, previously undocumented issue when multiple functions wrapped in Straight-Through Estimators interact multiplicatively (e.g.,
relu(x) * relu(y)). Standard AD applies the product rule, which re-introduces the hard, non-differentiable functions as multiplicative gates, causing gradients to vanish anyway. The authors solve this by applying STE to the composite function rather than individual primitives. - Algorithmic Trade-offs Unified: No single softening method rules them all. Sorting networks are incredibly fast but don't scale perfectly in memory for certain operations; permutahedron projections are highly memory-efficient but can be computationally slower. By unifying them, users can easily swap methods based on their specific hardware or bottleneck constraints.
Key Results
- Performance Benchmarks: For an array size of $n = 4096$, the sorting network-based approach proved to be the fastest soft method, taking just 1.0 ms (only $\sim$3.8× slower than the non-differentiable hard baseline).
- Memory Efficiency: FastSoftSort demonstrated excellent memory scaling, requiring only 420 KB of peak memory at $n = 4096$ because it avoids materializing an explicit $n \times n$ permutation matrix.
- Collision Detection Case Study: In the MuJoCo XLA mesh-mesh collision experiment, replacing hard primitives with SoftJAX operators successfully transformed a zero-gradient landscape into smooth, informative, non-zero gradients across all polygon vertices, enabling stable gradient-based optimization of physical rollouts.
Limitations and Future Work
While highly versatile, some methods hit scalability bottlenecks. For instance, optimal transport and unit-simplex methods inherently involve $O(n^2)$ operations or cost matrices, which can become restrictive for massive arrays (though SoftJAX mitigates memory overhead using lax.scan). Additionally, the authors' novel SmoothSort requires an $O(n^2)$ preprocessing step and relies on L-BFGS optimization, making it slower than standard sparse permutahedron methods.
Finally, this version of the library deliberately focuses on deterministic relaxations, leaving stochastic relaxations (such as perturbed optimizers or Gumbel-Softmax variants) as an avenue for future expansion.
Summary updated