Quasimetric Attention (wip)
Implementation of Quasimetric Attention in PyTorch, where query-key affinity is replaced with a learned quasimetric distance function decomposed into 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
)
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.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| quasimetric_attention-0.0.4.tar.gz | 6.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| quasimetric_attention-0.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.4 kB
Release files / quasimetric_attention-0.0.4.tar.gz
| Download URL | quasimetric_attention-0.0.4.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
faa679120dde9fb015161a6d71dcd8ec890433a28d007bcf37aa8ea58b5a819d
|
|
BLAKE2b-256 checksum How to use checksums |
a87ad0cdb8ad0f1e6f6df828a89c309ab4ec9699de600c0a8dcdc516759193d0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.17
|
Release files / quasimetric_attention-0.0.4-py3-none-any.whl
| Download URL | quasimetric_attention-0.0.4-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
726eaf3ea55002a7e9831245058d969060a85bd065b0770b3f18d84afc1f0b9b
|
|
BLAKE2b-256 checksum How to use checksums |
d5c0b588274d4e158ed239cd120b582b03249cbc7efe55dcc37af0e0aaa70f47
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.17
|