Skip to main content

Quasimetric Attention

Implementation of Quasimetric Attention in PyTorch, where query-key dot product is replaced with a learned quasimetric distance function with symmetric and asymmetric components.

Install

$ pip install quasimetric-attention

Usage

import torch
from quasimetric_attention import QuasimetricAttention

attn = QuasimetricAttention(
    dim = 512,
    dim_head = 64,
    heads = 8,
    prenorm = True
)

tokens = torch.randn(2, 1024, 512)

out = attn(tokens) + tokens
assert out.shape == tokens.shape

By default, mask_self = True blocks each token from attending to its own position, null_sink = True adds a learned null key (symmetric and asymmetric) and null value, prepended as a no-op sink that tokens can always attend to (which also prevents nan when every real key is masked), and gate = True applies a learned per-head sigmoid gate to the values. Each can be turned off.

attn = QuasimetricAttention(
    dim = 512,
    dim_head = 64,
    heads = 8,
    mask_self = False,
    null_sink = False,
    gate = False,
    prenorm = True
)

You can also enable a straight-through estimator (STE) on the asymmetric distance function with a smooth softplus logsumexp backwards pass (with temperature) for dense gradient propagation across all coordinate dimensions:

attn = QuasimetricAttention(
    dim = 512,
    dim_head = 64,
    asym_straight_through = True,
    asym_temperature = 0.5
)

Citations

@misc{liu2023metricresidualnetworkssample,
    title   = {Metric Residual Networks for Sample Efficient Goal-Conditioned Reinforcement Learning},
    author  = {Bo Liu and Yihao Feng and Qiang Liu and Peter Stone},
    year    = {2023},
    eprint  = {2208.08133},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG},
    url     = {https://arxiv.org/abs/2208.08133},
}
@misc{zheng2026multistepquasimetriclearningscalable,
    title   = {Multistep Quasimetric Learning for Scalable Goal-conditioned Reinforcement Learning},
    author  = {Bill Chunyuan Zheng and Vivek Myers and Benjamin Eysenbach and Sergey Levine},
    year    = {2026},
    eprint  = {2511.07730},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG},
    url     = {https://arxiv.org/abs/2511.07730},
}
@misc{wang2022improvedrepresentationasymmetricaldistances,
    title   = {Improved Representation of Asymmetrical Distances with Interval Quasimetric Embeddings},
    author  = {Tongzhou Wang and Phillip Isola},
    year    = {2022},
    eprint  = {2211.15120},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG},
    url     = {https://arxiv.org/abs/2211.15120},
}

Metadata

Release files for quasimetric-attention 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for quasimetric-attention 0.0.5
File Size Uploaded
quasimetric_attention-0.0.5.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for quasimetric-attention 0.0.5
File Interpreter ABI Platform
quasimetric_attention-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 12.9 kB

Release files / quasimetric_attention-0.0.5.tar.gz

Download URL quasimetric_attention-0.0.5.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1209bf64a922ac8aede48b04c40642f5f5b85406b3c93d02af6ba362e9198fae
BLAKE2b-256 checksum
How to use checksums
78bdd37268133abca1c8700499d6deb0de93671c8c5df68bb195610045b9a4b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Release files / quasimetric_attention-0.0.5-py3-none-any.whl

Download URL quasimetric_attention-0.0.5-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6d5cebac23b75bc8f24415108eeec84669a722974cb5fbcc4829cd362e1c3fbe
BLAKE2b-256 checksum
How to use checksums
3a31bb4b8150cd09a26735b961e5ae0006e6240b3992e680dd9e71bb1cf5a2aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page