Papers
Topics
Authors
Recent
Search
2000 character limit reached

TopKSim: Differentiable Top-K Averaging

Updated 30 January 2026
  • Top-K Averaging (TopKSim) is a class of differentiable operators that approximates hard top-k selection using continuous relaxations.
  • It employs tournament-style and LapSum-based methods to transform discrete ranking into a smooth, gradient-friendly process.
  • TopKSim is applied in retrieval, ranking, and set aggregation tasks, demonstrating state-of-the-art efficiency and accuracy on benchmarks.

Top-K Averaging (TopKSim) refers to a class of differentiable operators designed to aggregate the top-kk elements of a set of vectors according to a learned, input-specific score. In classical settings, selecting and averaging the top-kk elements is non-differentiable due to hard thresholding and discrete sorting operations, making it incompatible with gradient-based optimization. Modern TopKSim operators use continuous relaxations of the top-kk selection process, enabling end-to-end learning in neural architectures for retrieval, ranking, and set-aggregation tasks. Representative methods include the Successive Halving Top-kk Operator (Pietruszka et al., 2020) and the LapSum framework (Struski et al., 8 Mar 2025), both of which have been shown to offer efficient, scalable, and accurate approximations of the hard top-kk average.

1. Mathematical Formulation of TopKSim

Given a matrix of representations E=[E1;E2;… ;En]∈Rn×dE = [E_1;E_2;\dots;E_n] \in \mathbb{R}^{n \times d} and a corresponding score vector v=(v1,…,vn)∈Rnv = (v_1, \ldots, v_n) \in \mathbb{R}^n, the classical (hard) top-kk averaging operator is defined by

y=1k∑i∈TopKEi,y = \frac{1}{k} \sum_{i \in \mathrm{TopK}} E_i,

where TopK\mathrm{TopK} denotes the indices of the kk0 largest values in kk1. This operation reduces a set to its kk2 most "important" representatives based on scoring, and takes their unweighted mean.

The hard top-kk3 mask kk4 has kk5 for those kk6 corresponding to the kk7-largest kk8 and kk9 otherwise, with kk0. TopKSim relaxes kk1 to a continuous, differentiable vector kk2, maintaining kk3, and forms the soft average,

kk4

As the temperature or sharpness parameter of the relaxation tends to its limiting value (e.g., kk5 or kk6), kk7 approaches kk8 and kk9.

2. Differentiable Relaxations of Top-kk0 Selection

Tournament-Style (Successive Halving) Relaxation

The Successive Halving Top-kk1 Operator (Pietruszka et al., 2020) replaces the discrete top-kk2 selection with a differentiable, tournament-based elimination process. The method:

  • Iteratively pairs candidates in kk3, computes a boosted two-element softmax for each pair,
  • Merges features and scores according to softmax weights,
  • Halves the number of active candidates at each round, repeating until kk4 survivors remain,
  • Tracks soft contributions via a sequence of merge matrices, yielding a continuous selection mask kk5.

The mask is formally given as

kk6

where kk7 accumulates the effects of the per-round pairwise soft eliminations. Differentiability is preserved via standard softmax gradient propagation; the entire procedure is compatible with end-to-end backpropagation.

LapSum-Based (Closed-Form) Relaxation

LapSum (Struski et al., 8 Mar 2025) provides a general mechanism for differentiable order-statistics using the sum of Laplace cumulative distribution functions (CDFs). For a (sorted) score vector kk8 and scale parameter kk9, define

kk0

where kk1 is the CDF of a Laplace distribution scaled by kk2.

Soft top-kk3 proceeds by:

  1. Identifying kk4, the unique "threshold" such that the relaxed selection sums to kk5,
  2. Setting kk6, giving a soft mask kk7 with kk8,
  3. Forming the normalized averaging weights kk9 and soft mean E=[E1;E2;… ;En]∈Rn×dE = [E_1;E_2;\dots;E_n] \in \mathbb{R}^{n \times d}0.

The entire procedure—forward (TopKSim) and backward (gradient)—is E=[E1;E2;… ;En]∈Rn×dE = [E_1;E_2;\dots;E_n] \in \mathbb{R}^{n \times d}1 in time and E=[E1;E2;… ;En]∈Rn×dE = [E_1;E_2;\dots;E_n] \in \mathbb{R}^{n \times d}2 in memory.

3. Computational Complexity and Implementation

Both tournament-style and LapSum-based TopKSim methods are designed for efficiency.

  • Tournament-Style (Successive Halving): Each round pairs and softmaxes E=[E1;E2;… ;En]∈Rn×dE = [E_1;E_2;\dots;E_n] \in \mathbb{R}^{n \times d}3 candidates, requiring E=[E1;E2;… ;En]∈Rn×dE = [E_1;E_2;\dots;E_n] \in \mathbb{R}^{n \times d}4 rounds. Total cost is E=[E1;E2;… ;En]∈Rn×dE = [E_1;E_2;\dots;E_n] \in \mathbb{R}^{n \times d}5 arithmetical operations plus optional E=[E1;E2;… ;En]∈Rn×dE = [E_1;E_2;\dots;E_n] \in \mathbb{R}^{n \times d}6 for per-round sorts. Compared to prior iterative softmax relaxations (E=[E1;E2;… ;En]∈Rn×dE = [E_1;E_2;\dots;E_n] \in \mathbb{R}^{n \times d}7), this is substantially faster for moderate E=[E1;E2;… ;En]∈Rn×dE = [E_1;E_2;\dots;E_n] \in \mathbb{R}^{n \times d}8 (Pietruszka et al., 2020).
  • LapSum: LapSum requires a single E=[E1;E2;… ;En]∈Rn×dE = [E_1;E_2;\dots;E_n] \in \mathbb{R}^{n \times d}9 presort, v=(v1,…,vn)∈Rnv = (v_1, \ldots, v_n) \in \mathbb{R}^n0 forward/backward passes, and v=(v1,…,vn)∈Rnv = (v_1, \ldots, v_n) \in \mathbb{R}^n1 search/inversion steps with all operations amenable to parallelization. Implementations are concise in C++/CUDA, as primitives are simple scans and pointwise updates (Struski et al., 8 Mar 2025).

This efficiency makes both approaches scalable to large values of v=(v1,…,vn)∈Rnv = (v_1, \ldots, v_n) \in \mathbb{R}^n2 and v=(v1,…,vn)∈Rnv = (v_1, \ldots, v_n) \in \mathbb{R}^n3, overcoming practical limitations of earlier differentiable sorting, ranking, or selection layers.

4. Gradient Backpropagation and End-to-End Training

TopKSim layers, as constructed using either Successive Halving or LapSum, support fully differentiable backpropagation.

  • Tournament-Style: Gradients flow through each boosted-softmax and merge stage, with per-pair derivatives following standard softmax calculus, and the complete gradient obtained via the chain rule.
  • LapSum: The derivative of the output mean v=(v1,…,vn)∈Rnv = (v_1, \ldots, v_n) \in \mathbb{R}^n4 w.r.t. the inputs v=(v1,…,vn)∈Rnv = (v_1, \ldots, v_n) \in \mathbb{R}^n5 is a simple weighted average gradient. For gradients w.r.t. the scores v=(v1,…,vn)∈Rnv = (v_1, \ldots, v_n) \in \mathbb{R}^n6, Jacobians are computed analytically in closed-form based on implicit function theorem calculus, requiring only quantities already computed in the forward pass.

This property is essential for integrating TopKSim modules into neural models for learning similarity, aggregation, and retrieval functions.

5. Empirical Performance and Comparison with Prior Methods

Empirically, LapSum-based soft top-v=(v1,…,vn)∈Rnv = (v_1, \ldots, v_n) \in \mathbb{R}^n7/TopKSim achieves state-of-the-art accuracy, speed, and memory use in large-scale ranking and aggregation tasks.

  • On benchmarks such as CIFAR-100 (ResNet-18) and ImageNet-1K/21K-P (ResNeXt-101), LapSum consistently matches or surpasses prior soft-sort and soft-permute methods (e.g., NeuralSort, SoftSort, SinkhornSort) in Top-1 and Top-5 accuracy, especially as class count increases.
  • For large v=(v1,…,vn)∈Rnv = (v_1, \ldots, v_n) \in \mathbb{R}^n8 and v=(v1,…,vn)∈Rnv = (v_1, \ldots, v_n) \in \mathbb{R}^n9, LapSum is 2–5× faster than existing alternatives and remains kk0 in memory, even as competitors become infeasible.
  • The total weight error kk1 is at machine precision, addressing a common deficiency in other relaxations (Struski et al., 8 Mar 2025).
  • The Successive Halving operator (Pietruszka et al., 2020) exhibits higher normalized Chamfer cosine similarity (nCCS) than iterative baselines for all tested kk2, with accuracy degrading only slowly as kk3 increases and provably converging to the hard top-kk4 as kk5.

6. Integration and Applications in Retrieval and Set Aggregation

TopKSim layers are directly usable in neural architectures requiring soft top-kk6 aggregation for retrieval, similarity, or set representation tasks.

  • For retrieval, TopKSim provides a continuous relaxation of "best match" or "top-k" pooling, which can be embedded into ranking losses or metric learning pipelines.
  • Set aggregation tasks benefit from TopKSim by obtaining permutation-invariant, differentiable summaries of salient elements.
  • The sharpness or temperature parameter (e.g., kk7 for Successive Halving, kk8 for LapSum) can be tuned or annealed to control the bias-variance tradeoff between faithful top-kk9 emulation and smooth optimization.

Table 1 summarizes key computational features of major TopKSim methods:

Method Forward/Backward Cost Memory Notes
Successive Halving Top-k (Pietruszka et al., 2020) y=1k∑i∈TopKEi,y = \frac{1}{k} \sum_{i \in \mathrm{TopK}} E_i,0–y=1k∑i∈TopKEi,y = \frac{1}{k} \sum_{i \in \mathrm{TopK}} E_i,1 y=1k∑i∈TopKEi,y = \frac{1}{k} \sum_{i \in \mathrm{TopK}} E_i,2 Chain of soft tournament merges
LapSum (Struski et al., 8 Mar 2025) y=1k∑i∈TopKEi,y = \frac{1}{k} \sum_{i \in \mathrm{TopK}} E_i,3 y=1k∑i∈TopKEi,y = \frac{1}{k} \sum_{i \in \mathrm{TopK}} E_i,4 Closed-form, invertible soft threshold
Iterated Softmax (prior art) y=1k∑i∈TopKEi,y = \frac{1}{k} \sum_{i \in \mathrm{TopK}} E_i,5 y=1k∑i∈TopKEi,y = \frac{1}{k} \sum_{i \in \mathrm{TopK}} E_i,6 y=1k∑i∈TopKEi,y = \frac{1}{k} \sum_{i \in \mathrm{TopK}} E_i,7 global softmax passes needed

Both frameworks enable practical, fully differentiable top-y=1k∑i∈TopKEi,y = \frac{1}{k} \sum_{i \in \mathrm{TopK}} E_i,8 aggregation for large datasets and architectures.

7. Summary and Outlook

TopKSim denotes a family of differentiable top-y=1k∑i∈TopKEi,y = \frac{1}{k} \sum_{i \in \mathrm{TopK}} E_i,9 averaging operators, with representative realizations including the Successive Halving operator (Pietruszka et al., 2020) and the LapSum methodology (Struski et al., 8 Mar 2025). These methods provide mathematically principled, computationally efficient, and empirically robust means to approximate hard top-TopK\mathrm{TopK}0 selection and averaging in neural applications requiring optimization through subset selection. Their modularity and compatibility with standard autodiff allow seamless integration into ranking, retrieval, and robust permutation-invariant architectures. A plausible implication is that continued developments in closed-form, scalable, and accurate soft order-statistics will further broaden the range of tasks amenable to differentiable subset selection.

Definition Search Book Streamline Icon: https://streamlinehq.com
References (2)

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to Top-K Averaging (TopKSim).