---
title: Injective Linear Attention (InLine)
url: https://www.emergentmind.com/topics/injective-linear-attention-inline
type: topic
---

# Injective Linear Attention (InLine)

Injective Linear Attention (InLine) is an attention mechanism designed to reconcile the performance gap between conventional Softmax attention and linear attention, particularly in large-scale vision transformer models. InLine achieves linear computational complexity while restoring crucial properties—injectivity and effective local modeling—that underlie the superior expressiveness of Softmax attention. Its core theoretical and algorithmic innovations enable it to outperform both standard Softmax and prior linear attention methods in diverse visual tasks, all while maintaining efficient scaling for high-resolution input [2412.06590].

## 1. Distinctions Between Softmax and Linear Attention

Both Softmax and linear attention mechanisms compute context-sensitive outputs from queries $Q \in \mathbb{R}^{N \times d}$, keys $K \in \mathbb{R}^{N \times d}$, and values $V \in \mathbb{R}^{N \times C}$, outputting $O \in \mathbb{R}^{N \times C}$. They diverge sharply in algorithmic complexity and representational power:

- **Softmax attention** computes the full $N \times N$ similarity matrix, requiring $\mathcal{O}(N^2 C)$ computation:
  $$
  S_i = \mathrm{softmax}(Q_i K^\top), \quad O_i^S = S_i^\top V.
  $$
  This formulation enables strong capture of both local and long-range dependencies but is computationally prohibitive as $N$ increases.

- **Linear attention** reduces complexity to $\mathcal{O}(NC)$ by substituting the exponential kernel with a feature map $\phi(\cdot)$:
  $$
  L_i = \left[ \frac{\phi(Q_i)^\top \phi(K_j)}{\sum_{t} \phi(Q_i)^\top \phi(K_t)} \right]_{j=1}^N, \quad O_i^L = L_i^\top V.
  $$
  The associativity property allows kernel value aggregation in linear time, but empirical results show consistent under-performance compared to Softmax attention in vision tasks.

## 2. Injectivity in Attention Functions

Injectivity, defined as $f: A \to B$ such that $x \neq y \implies f(x) \neq f(y)$, is crucial for ensuring that distinct queries yield distinct attention weight distributions. For fixed keys $K$:

- **Softmax mapping**: $S_K(q) = \mathrm{softmax}(q^\top K)$ is injective, as $S_K(p) \neq S_K(q)$ for $p \neq q$.
- **Linear mapping**: $L_K(q)_j \propto \phi(q)^\top \phi(K_j)$ may fail to distinguish collinear or scaling-equivalent queries, leading to non-injectivity.

This property is central in preventing semantic confusion during attention computation, which can otherwise result if different queries receive identical attention weights.

## 3. Non-Injectivity of Vanilla Linear Attention

Proposition 2 establishes that standard linear attention is not injective for any continuous $\phi: \mathbb{R}^d \rightarrow \mathbb{R}^d$:

- If $\phi$ is non-injective, queries $p \neq q$ with $\phi(p) = \phi(q)$ trivially map to identical outputs: $L_K(p) = L_K(q)$.
- If $\phi$ is injective, invariance of domain implies there exist $p \neq q$ and $\alpha \neq 0$ such that $\phi(q) = \alpha \phi(p)$, resulting in $L_K(q) = L_K(p)$ after kernel normalization.

Consequently, vanilla linear attention "collapses" the attention behavior of collinear queries, significantly reducing its discriminative capacity relative to Softmax attention [2412.06590].

## 4. The InLine Mechanism: Injective Linear Attention

InLine resolves the injectivity limitation via a novel normalization scheme that replaces divisive normalization with a subtractive form, guaranteeing that $\sum_j w_{i,j} = 1$ and enforcing injectivity under mild rank conditions:

\[
w_{i, j} = \phi(Q_i)^\top \phi(K_j) - \frac{1}{N}\sum_{s=1}^N \phi(Q_i)^\top \phi(K_s) + \frac{1}{N}
\]
\[
O_i^I = \sum_{j=1}^N w_{i,j} V_j
\]

Under full-rank assumptions for $\phi(K)$ and the augmented matrix $[\phi(K), 1 ]$, the resulting attention map $InL_K: \mathbb{R}^d \rightarrow \mathbb{R}^N$ is injective. The InLine computation is efficiently realized in linear time per token:

- Precompute:
  - $M_1 = \sum_{j} \phi(K_j) V_j^\top \in \mathbb{R}^{d \times C}$
  - $m_\phi = \sum_j \phi(K_j) \in \mathbb{R}^d$
  - $m_V = \sum_j V_j \in \mathbb{R}^C$
- Compute outputs:
  \[
  O_i^I = \phi(Q_i)^\top M_1 - \left[\phi(Q_i)^\top m_\phi - 1\right] \frac{1}{N} m_V
  \]
  with $\mathcal{O}(N d^2 ) \approx \mathcal{O}(N C d )$ cost.

## 5. Enhancing Local Modeling

Empirical analyses reveal that Softmax attention applies strong local inductive bias in early layers, critical for visual pattern recognition: observed local selection rates in Softmax attention rise markedly above the $9/197 \approx 4.6\%$ baseline, ranging from 15–30%. Both vanilla linear attention and InLine lack this property by default. To close this gap, InLine incorporates an explicit local residual:

1. Compute global $O_i^I$.
2. Compute overall token mean $\bar x = \mathrm{mean}_{k} x_k$.
3. Feed $\bar x$ through a small MLP predicting $r \in \mathbb{R}^9$.
4. Collect 3×3 neighborhood values $V_j^{N(i)}$.
5. Output:
   $$
   O_i = O_i^I + \sum_{j=1}^9 r_j V_j^{N(i)}
   $$
This operation increases overhead by $\mathcal{O}(9 N d + d^2)$ but preserves the overall linear scaling in sequence length.

## 6. Implementation in Vision Transformers

InLine is implemented by replacing all Softmax attention layers in leading Vision Transformer backbones—such as Swin, DeiT, PVT, and CSWin—with the injective InLine and local residual module:

- **Kernel choices**: $\phi(x) = x$ (identity) or nonnegative variants such as ReLU/exp.
- **Complexity**:
  - Softmax: $\mathcal{O}(N^2 C)$
  - Linear, InLine: $\mathcal{O}(NCd)$ per layer
- **Training protocol** (ImageNet-1K): 300 epochs from scratch, AdamW ($\text{lr}=10^{-3}$), cosine decay, 20-epoch warmup, and standard augmentation (RandAugment, Mixup, CutMix, Erasing). Identical protocols are used for downstream detection/segmentation tasks [2412.06590].

## 7. Empirical Results and Significance

A comprehensive evaluation on vision benchmarks demonstrates that InLine achieves equal or superior accuracy to Softmax attention at substantially reduced computation, outperforming prior linear attention methods in both classification, detection, and segmentation settings:

| Model            | Softmax | Linear-base | InLine (ours) |
|------------------|:-------:|:-----------:|:-------------:|
| DeiT-T (1.2 G)   | 72.2%   | ~70.0%      | 74.5%         |
| PVT-S (3.8 G)    | 79.8%   | ~77.3%      | 82.0%         |
| Swin-T (4.5 G)   | 81.3%   | ~77.3%      | 82.4%         |
| CSWin-T (4.3 G)  | 82.7%   |    —        | 83.2%         |

| Backbone     | APᵇ  | APᵐ  | FLOPs  |
|--------------|:----:|:----:|:------:|
| PVT-S        | 40.4 | 37.8 | 305 G  |
| InLine-PVT-S | 43.4 | 40.1 | 250 G  |

| Backbone     | mIoU  | FLOPs  |
|--------------|:-----:|:------:|
| Swin-T       | 44.51 | 945 G  |
| InLine-Swin-T| 45.57 | 941 G  |

Inference speed is retained as window or image size grows, in contrast to significant throughput degradation observed for Softmax attention with increasing $N$.

## 8. Theoretical and Practical Impact

Softmax attention’s empirical success is attributed to two distinct properties: injectivity (ensuring unique attention mappings for distinct queries) and emergent local bias. Standard linear attention loses both—providing explainable grounds for its lower efficacy in vision models. InLine systematically remedies both deficiencies: it employs a subtraction-based normalization to restore injectivity and supplements the global attention output with a learned local residual. As a result, InLine closes and, in many regimes, reverses the performance gap between linear and Softmax attention at significantly lower computational cost for large input domains [2412.06590].

A plausible implication is that the key determinants of attention quality in vision models are injectivity and explicit local modeling capacity, rather than the specific use of Softmax normalization. This suggests that further variants—enforcing these properties—could push the scalability and accuracy of attention-based architectures even higher in future work.

Source: https://www.emergentmind.com/topics/injective-linear-attention-inline