---
title: Log-Spaced Continuous Position Bias
url: https://www.emergentmind.com/topics/log-spaced-continuous-position-bias-log-cpb
type: topic
---

# Log-Spaced Continuous Position Bias

Log-Spaced Continuous Position Bias (Log-CPB) is a method introduced for encoding relative spatial relations between tokens in window-based vision Transformers. Unlike static tabular position biases, Log-CPB employs a small neural meta-network that accepts log-spaced, continuous distance coordinates, enabling seamless and stable extrapolation across window sizes—crucial for transferring pre-trained models to higher resolutions or larger windows without performance degradation. It was proposed in the context of Swin Transformer V2 to address the challenges associated with resolution gaps between pre-training and downstream fine-tuning [2111.09883].

## 1. Motivation and Conceptual Framework

Window-based vision Transformer architectures, such as Swin Transformer, require an inductive bias that reflects the spatial proximity of image patches. In the original Swin Transformer, this was realized as a learned bias $B_{ij}$ for each pair of patches at grid distance $(\Delta x, \Delta y)$, stored as a discrete lookup table of size $(2M-1)\times(2M-1)$ for window size $M$. Transitioning to different window sizes at downstream tasks (e.g., during fine-tuning or deployment at higher resolutions) necessitated ad hoc resizing (e.g., via bicubic interpolation), which often led to suboptimal generalization and substantial performance drops at extreme window scales.

Log-CPB replaces this table with a two-layer MLP $\mathcal G$, which takes as input log-spaced, continuous relative coordinates, enabling smooth extrapolation far beyond the window sizes used at pre-training. The log-spacing transformation ensures stability and consistent scaling even across large relative distances, due to the slow growth of the logarithm function.

## 2. Mathematical Formulation

Let $M$ denote the window width or height ($M \times M$ patches per window). For any two patches within a window, the grid offset is $(\Delta x, \Delta y) \in \{-(M-1), ..., M-1\}^2$.

**Log-Spaced Coordinate Transformation:**
\[
\hat\Delta x = \mathrm{sign}(\Delta x)\,\log\bigl(1 + |\Delta x|\bigr), \qquad
\hat\Delta y = \mathrm{sign}(\Delta y)\,\log\bigl(1 + |\Delta y|\bigr)
\]

**Meta-Network Bias Prediction:**
\[
B(\Delta x, \Delta y) = \mathcal G(\hat\Delta x, \hat\Delta y)
\]
Here, $\mathcal G: \mathbb R^2 \rightarrow \mathbb R$ is a 2-layer MLP with hidden dimension $d_{\mathrm{hid}}$ (commonly 128 or 256), using ReLU activation:
\[
\mathbf u = 
\begin{bmatrix}
\hat\Delta x \\
\hat\Delta y
\end{bmatrix} \in \mathbb R^2, \qquad
\mathbf h = \mathrm{ReLU}(W_1 \mathbf u + b_1) \in \mathbb R^{d_{\mathrm{hid}}}
\]
\[
B(\Delta x, \Delta y) = W_2 \mathbf h + b_2
\]
with $W_1 \in \mathbb R^{d_{\mathrm{hid}} \times 2}$, $W_2 \in \mathbb R^{1 \times d_{\mathrm{hid}}}$. For multi-head attention, $\mathcal G$ either outputs an $H$-vector per head or separate MLPs per head.

**Integration into Attention Mechanism:**
\[
\mathrm{Sim}(q_i, k_j) = \frac{\cos(q_i, k_j)}{\tau} + B_{ij}
\]
or in scaled dot-product attention:
\[
\mathrm{Attention}(Q, K, V) = \mathrm{Softmax}\left(\frac{QK^\top}{\sqrt d} + B\right)V
\]
$B \in \mathbb R^{M^2 \times M^2}$ with $B_{ij} = B(\Delta x_{ij}, \Delta y_{ij})$.

## 3. Integration within Transformer Modules

Construction of the position bias matrix $B$ is performed for each forward pass, or pre-computed and cached at test-time. The process involves the following steps:
1. For each patch pair $(i, j)$, compute $(\Delta x_{ij}, \Delta y_{ij})$.
2. Apply the log-spaced transform to obtain $(\hat\Delta x, \hat\Delta y)$.
3. Pass the result through the 2-layer MLP $\mathcal G$ to compute $B_{ij}$.
4. Add the resulting $B$ to the unnormalized attention logits prior to softmax.

At inference, $B$ can be stored as a fixed parameter tensor with shape $(H, M^2, M^2)$, eliminating further MLP computations. No modifications are required elsewhere in the Transformer block.

## 4. Hyperparameters, Design Choices, and Ablation

The primary hyperparameters and design decisions include:
- **MLP Depth:** 2 layers
- **Hidden Dimension ($d_{\mathrm{hid}}$):** Typically 128 or 256; experiments demonstrate 256 is effective.
- **Activation:** ReLU
- **Coordinate Transform:** $\log(1+|\Delta|)$ with sign preserved, no additional scaling.
- **Number of Heads ($H$):** Equal to attention heads; $\mathcal G$ may be shared or distinct across heads.
- **Bias Output:** Either vector-valued for all heads jointly or per-head small MLPs.

**Ablations:**
- Replacing log-spaced CPB with linear-spaced CPB (using raw $(\Delta x, \Delta y)$ as input) or the original table leads to inferior scaling. Log-CPB outperforms linear CPB by 0.3–0.6% top-1 accuracy on ImageNet and improves box/mask mAP and mIoU by up to 3.3 points under large window-size transitions.

## 5. Empirical Performance and Scaling Results

With models such as Swin-T trained at $256^2$ resolution using $8 \times 8$ windows:
- The original scheme’s top-1 accuracy on ImageNet-V2 falls from 81.7% at window size $M = 8$ to 68.7% at $M = 24$.
- Log-CPB maintains performance across this transition, dropping only from 81.8% to 79.1% without fine-tuning.
- With fine-tuning on larger window sizes, Log-CPB achieves up to 84.2% top-1, a 1.0% gain over linear CPB and up to 1.0% over the original parameterization.

In dense prediction tasks (COCO, ADE20K), Log-CPB yields 0.3–0.9 higher box AP and 1.5–3.3 higher mIoU when doubling window size, supporting its efficacy in extreme regime shifts.

## 6. Implementation Characteristics and Computational Complexity

**Complexity:** 
- A naive approach recomputing $B$ for all $M^4$ possible patch-pairs is prohibitive.
- Efficiently, one precomputes a $(2M-1)\times(2M-1)$ table of unique $(\hat\Delta x, \hat\Delta y)$, resulting in $O(M^2)$ MLP calls, then indexes into this table to assemble the $B$ matrix, amortizing computation.
- At inference, the bias tensor for all heads and patch pairs can be statically stored, removing further computational overhead.

**Memory:** 
- The overhead is identical to the original tabular approach, requiring a $(H \times M^2 \times M^2)$ bias tensor.
- Additional parameters due to $\mathcal G$ are negligible (on the order of a few thousand floats).

**Stability and Training:**
- The log transformation keeps MLP input ranges stable; $|\log(1+|\Delta|)|$ grows slowly and remains in roughly $[0,3]$ for $|\Delta| \leq 1,000$.
- Training uses standard weight initialization (Xavier or Kaiming); no altered schedule is needed.
- When sharing an MLP, vector-valued outputs for all heads can be efficiently computed; per-head MLPs can be batched.

A plausible implication is that this approach enables immediate and robust scaling of window-based vision Transformer models across a wide range of image resolutions and spatial granularity, with no need for interpolation or retuning of position biases [2111.09883].

Source: https://www.emergentmind.com/topics/log-spaced-continuous-position-bias-log-cpb