HoPE Positional Encoding
Hybrid Positional Encoding (HoPE).
Implements hyperbolic positional encoding over consecutive dimension pairs with monotonic exponential damping. Unlike RoPE which uses trigonometric rotation (sin/cos), HoPE uses hyperbolic functions (sinh/cosh) combined with an exponential damping factor that decays with position distance. A layer-dependent scale factor modulates the angles, making deeper layers sensitive to longer-range positions.
HoPE is used as the positional encoding for TitanAttention blocks.
- class src.model.embeddings.hope.HoPE(*args: Any, **kwargs: Any)[source]
Bases:
ModuleHybrid Positional Encoding with hyperbolic rotation and exponential damping.
Applies a hyperbolic transformation to each pair of adjacent dimensions. The angle for pair
iat positionpin layerlis:theta_i(p, l) = p * exp(-log(base) * i / (pair_dim - 1)) * (1 + 0.05 * l)
The transformation uses
coshandsinh(instead ofcosandsin) multiplied by an exponential damping factor:damping(p, l) = exp(-damping * (1 + 0.05 * l) * p)
This provides monotonic decay of positional influence with distance while the layer-dependent scaling allows deeper layers to attend over longer ranges.
- Parameters:
head_dim – Dimensionality of each attention head. Must be even for proper pairing.
base – Base for the geometric progression of inverse frequencies. Defaults to
10000.0.damping – Damping coefficient controlling the rate of exponential decay with position. Defaults to
0.01.
- head_dim
Total head dimensionality.
- pair_dim
Number of dimension pairs (
head_dim // 2).
- base
Base frequency for inverse frequency computation.
- damping
Damping coefficient for exponential position decay.
- forward(x: torch.Tensor, logical_layer_idx: int = 0) torch.Tensor[source]
Apply hyperbolic positional encoding.
- Parameters:
x – Input tensor of shape
(batch, heads, seq_len, head_dim).logical_layer_idx – Logical layer index used to compute the layer-dependent angle scaling factor. Defaults to
0.
- Returns:
Tensor of same shape as
xwith hyperbolic positional encoding applied. Ifpair_dim == 0, returnsxunchanged.