Metadata-Version: 2.5
Name: quasimetric-attention
Version: 0.0.4
Summary: Quasimetric Attention - PyTorch
Project-URL: Homepage, https://pypi.org/project/quasimetric-attention/
Project-URL: Repository, https://github.com/lucidrains/quasimetric-attention
Author-email: Phil Wang <lucidrains@gmail.com>
License: MIT License
        
        Copyright (c) 2026 Phil Wang
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: artificial intelligence,attention mechanism,deep learning,quasimetric
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.10
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Requires-Dist: einops>=0.8.1
Requires-Dist: einx>=0.4.0
Requires-Dist: torch-einops-utils>=0.1.25
Requires-Dist: torch>=2.5
Provides-Extra: test
Requires-Dist: pytest; extra == 'test'
Description-Content-Type: text/markdown

## 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

```shell
$ pip install quasimetric-attention
```

## Usage

```python
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.

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

## Citations

```bibtex
@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},
}
```

```bibtex
@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},
}
```

```bibtex
@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},
}
```
