Fast Differentiable Sorting and Ranking

Mathieu Blondel, Olivier Teboul, Quentin Berthet, Josip Djolonga

Introduction

Sorting and ranking are fundamental operations in computer science, yet they remain notoriously difficult to integrate into deep learning architectures. This paper introduces the first differentiable sorting and ranking operators that achieve $O(n \log n)$ time and $O(n)$ space complexity. By framing sorting as a projection onto a geometric structure called a "permutahedron" and reducing the problem to isotonic optimization, the authors enable efficient, exact gradients for operations that were previously either non-differentiable or computationally prohibitive.

The Challenge of Differentiable Sorting

In the context of gradient-based optimization, standard sorting and ranking are pathological:

  • Sorting is piecewise linear; it has "kinks" where the derivative is undefined.
  • Ranking is piecewise constant, meaning its derivatives are zero almost everywhere.

If a practitioner wants to use a rank-based metric (like Spearman’s correlation) or a sort-based loss (like top-k accuracy) as a training objective, standard backpropagation fails because the gradients provide no useful information. While previous research has proposed "soft" differentiable proxies (often based on Sinkhorn iterations), these typically suffer from high computational costs—scaling at $O(n^2)$ or even $O(n^3)$—making them unusable for large-scale problems.

A Geometric Solution: The Permutahedron

The authors bridge the gap between discrete sorting and continuous optimization using the permutahedron, a convex polytope whose vertices correspond to the permutations of a vector.

  1. Linear Programming Formulation: They demonstrate that sorting and ranking can be expressed as linear programs over the permutahedron.
  2. Regularization: To make these operators differentiable, they add strongly convex regularization (either quadratic or entropic). This transforms the linear program into a continuous projection onto the permutahedron.
Illustration of the permutahedron and regularization paths for soft ranks.
  1. The Isotonic Shortcut: The key technical breakthrough of the paper is proving that this projection can be reduced to isotonic optimization (regression under monotonic constraints).
  2. Efficiency: Isotonic regression can be solved in $O(n)$ time using the Pool Adjacent Violators (PAV) algorithm. Combined with an initial $O(n \log n)$ sort of the input, the entire differentiable operator runs in $O(n \log n)$ time.

The "Softness" Trade-off

Unlike methods that approximate the permutation matrix, this approach provides exact computation and differentiation for the regularized objective. The regularization parameter $\epsilon$ allows practitioners to tune the operator's behavior smoothly.

As $\epsilon \to 0$, the operator recovers the "hard" (exact) sort or rank. As $\epsilon \to \infty$, the output collapses to a simple mean, creating a smoother landscape that is easier to optimize.

Effect of the regularization parameter on soft sorting and soft ranking. As regularization increases, values converge toward the mean.

Key Results and Applications

The authors evaluated their method across several domains, demonstrating significant improvements in both scalability and performance:

  • Speed and Memory: The $O(n \log n)$ complexity is a game-changer. While $O(n^2)$ methods quickly run out of GPU memory at sequence lengths of $n=2000$, this method scales easily to $n=5000$ and beyond, running an order of magnitude faster than Optimal Transport-based methods.
  • Image Classification: Using CIFAR-10 and CIFAR-100, the authors tested a soft top-k classification loss. The soft rank operators achieved accuracy comparable to state-of-the-art differentiable proxies but with vastly superior scaling.
  • Label Ranking: Evaluated across 21 different datasets, the method successfully optimized a differentiable Spearman’s rank correlation coefficient.
  • Robust Regression: Using datasets like Housing and Bodyfat, the authors tested "soft least trimmed squares." The differentiable sorting effectively ignored outliers, outperforming standard Ridge regression in noisy environments.

Limitations and Future Work

While the theoretical complexity is strictly superior, the authors' implementation of the PAV algorithm currently runs on the CPU. Because modern deep learning frameworks are highly optimized for GPU matrix operations, $O(n^2)$ "all-pairs" methods can still be faster in practice for very small arrays (e.g., $n < 100$). Developing a highly parallelized GPU version of the PAV algorithm is a clear next step.

Additionally, the "softness" parameter $\epsilon$ requires hyperparameter tuning, particularly for tasks like top-k classification. Despite these minor limitations, this work opens the door for differentiable versions of many other algorithms that rely on sorting, such as dynamic time warping and robust clustering.

Summary updated

BibTeX
@inproceedings{blondel2020fast,
  author = {Mathieu Blondel and Olivier Teboul and Quentin Berthet and Josip Djolonga},
  title = {{Fast Differentiable Sorting and Ranking}},
  booktitle = {International Conference on Machine Learning},
  year = {2020},
}
Figure