In standard deep learning, we assume that every operation in a model's pipeline must be "smooth." To train a network via gradient descent, we need to know how a tiny nudge in the input affects the output. However, some of the most fundamental operations in computer science—sorting, ranking, and picking the "top-$k$" elements—are notoriously "jagged."
As noted in LapSum, these operations are piecewise constant. If you have a list of scores and you change one by a tiny fraction, the sorted order usually stays exactly the same. When the order finally does change, it jumps discontinuously. This creates a "zero-gradient" problem: the derivative is zero almost everywhere and undefined at the jumps, providing no useful signal for backpropagation.
Research into differentiable sorting and ranking aims to bridge this gap, allowing models to learn to order, prioritize, and select data as part of an end-to-end trainable architecture.
The Permutation Matrix Perspective
The core intuition behind making sorting differentiable is to move from discrete permutations to "soft" assignments. Normally, sorting a vector can be viewed as multiplying it by a permutation matrix—a grid of 0s and 1s where each row and column sums to one.
To make this differentiable, researchers like marco cuturi, Olivier Teboul, and Jean-Philippe Vert reframed sorting as an Optimal Transport problem. In Differentiable Ranking and Sorting using Optimal Transport, they show that sorting is equivalent to finding the most efficient way to transport the values of an input vector to a set of sorted target positions. By applying entropic regularization and solving this with the Sinkhorn algorithm, they produce a "doubly stochastic" matrix—a soft permutation where values can be "partially" in multiple ranks at once. This matrix is fully differentiable, allowing gradients to flow back to the original scores.
Continuous Relaxations and Sorting Networks
While Optimal Transport provides a robust mathematical foundation, other researchers have sought simpler or more scalable approximations.
- SoftSort: Julian Martin Eisenschlos and Sebastian Prillo introduced SoftSort, a continuous relaxation of the
argsortoperator. It replaces the discrete index selection with a softmax-based weighted average of ranks, making it significantly faster and easier to implement than earlier methods. - Differentiable Sorting Networks: Instead of relaxing the entire sorting operation at once, Felix Petersen and Christian Borgelt looked at the internal logic of sorting algorithms like Bitonic Sort. In Differentiable Sorting Networks, they replace the discrete
minandmaxoperators within the sorting network with smooth approximations. This allows the model to learn from ranking supervision even for very large sets of elements. - Neural Proxies: A different approach, taken by Martin Engilberge and colleagues in SoDeep, is to train a separate "proxy" neural network to learn the sorting function itself. This proxy acts as a differentiable surrogate that can be used to optimize non-differentiable metrics like mean Average Precision (mAP).
Why Differentiable Sorting Matters
By making sorting "visible" to the optimizer, these techniques unlock several new capabilities in deep learning:
1. Learning to Attend and Route Standard Transformers have quadratic complexity because every token attends to every other token. Yi Tay and colleagues used Sparse Sinkhorn Attention to let a model learn how to permute and block its own input sequence. By sorting blocks before applying attention, the model achieves a quasi-global receptive field with much lower memory costs.
2. Rank-Based Objectives Many real-world tasks care about the order of predictions rather than their absolute values. Felix Petersen demonstrated in Differentiable Top-k Classification Learning that training with a differentiable top-$k$ loss can improve both top-$k$ and top-1 accuracy by allowing the model to refine the relative order of its predictions directly. Similarly, XicorAttention uses differentiable sorting to incorporate Chatterjee’s rank correlation into time-series Transformers, better capturing nonlinear patterns.
3. Sparse Selection and Routing In Mixture of Experts (MoE) or dynamic pruning, models must choose the "best" $k$ experts or weights. Michael Eli Sander, Mathieu Blondel, and others have developed Fast, Differentiable and Sparse Top-k operators. These allow the model to make sparse, discrete-like selections while maintaining a valid gradient signal for the routing logic.
4. Structural and Combinatorial Learning From auto-labelling markers in motion capture (Ali Etemad et al., CGI 2019) to solving the Traveling Salesman Problem (Yimeng Min and Carla P Gomes, Structure As Search), differentiable permutations allow neural networks to explore combinatorial spaces. Instead of using expensive search algorithms, the network learns to output valid, structured permutations directly through standard backpropagation.