Abstract: The sorting operation is one of the most commonly used building blocks in computer programming. In machine learning, it is often used for robust statistics. However, seen as a function, it is piecewise linear and as a result includes many kinks where it is non-differentiable. More problematic is the related ranking operator, often used for order statistics and ranking metrics. It is a piecewise constant function, meaning that its derivatives are null or undefined. While numerous works have proposed differentiable proxies to sorting and ranking, they do not achieve the $O(n \log n)$ time complexity one would expect from sorting and ranking operations. In this paper, we propose the first differentiable sorting and ranking operators with $O(n \log n)$ time and $O(n)$ space complexity. Our proposal in addition enjoys exact computation and differentiation. We achieve this feat by constructing differentiable operators as projections onto the permutahedron, the convex hull of permutations, and using a reduction to isotonic optimization. Empirically, we confirm that our approach is an order of magnitude faster than existing approaches and showcase two novel applications: differentiable Spearman's rank correlation coefficient and least trimmed squares.
Introduction
In deep learning, the ability to compute gradients via backpropagation is essential. This requirement has historically excluded fundamental operations like sorting and ranking. Standard sorting is piecewise linear, possessing non-differentiable "kinks," while ranking is piecewise constant, resulting in zero derivatives almost everywhere. Previous attempts to create "soft" differentiable versions of these operations often suffered from high computational complexity, typically scaling quadratically ($O(n^2)$) or cubically ($O(n^3)$).
This work introduces the first differentiable sorting and ranking operators that achieve $O(n \log n)$ time complexity and $O(n)$ space complexity, matching the efficiency of standard non-differentiable algorithms.
Methodology: Projections onto the Permutahedron
The core of the proposed method lies in casting sorting and ranking as optimization problems over the permutahedron. The permutahedron is a convex polytope in $\mathbb{R}^n$ whose vertices are all possible permutations of a given vector.
To ensure differentiability, the authors apply strongly convex regularization (either quadratic or entropic) to the linear program. A key theoretical contribution is the proof that projecting onto the permutahedron is equivalent to isotonic optimization—a constrained optimization problem where the output must maintain a monotonic order. By leveraging the Pool Adjacent Violators (PAV) algorithm, the projection can be computed in $O(n)$ time after an initial $O(n \log n)$ sort. Furthermore, the authors derived an efficient $O(n)$ method for computing the Jacobian-vector product, enabling fast backpropagation.
Experimental Evaluation
The efficiency and utility of these operators were evaluated across several tasks:
- Computational Speed: The proposed method demonstrated an order of magnitude speedup compared to Optimal Transport (OT) based approaches. It scaled gracefully to vectors of size 5,000, where competing methods failed due to memory constraints.
- Image Classification (Soft Top-k): On CIFAR-100, the soft rank formulations achieved state-of-the-art accuracy while being significantly faster to train than previous soft loss functions.
- Label Ranking: Using a differentiable Spearman’s rank correlation improved performance on 15 out of 21 benchmark datasets.
- Robust Regression: The "Soft Least Trimmed Squares" (LTS) method was tested on datasets like
HousingandBodyfat. By sorting losses and ignoring outliers, the model matched the performance of specialized robust losses like Huber loss.
Insights and Discussion
The proposed operators allow for a smooth interpolation between "hard" sorting/ranking and a simple average of input values, controlled by a regularization parameter $\epsilon$.
As $\epsilon$ increases, the "soft" values converge, providing a tunable trade-off between the precision of the sort and the smoothness of the gradient. This framework not only matches theoretical complexity limits but also provides exact solutions rather than iterative approximations. While the current implementation relies on CPU-based PAV, the $O(n \log n)$ complexity ensures it remains viable for large-scale deep learning.
Summary updated