poly-attention 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- poly_attention-0.0.1/.gitignore +220 -0
- poly_attention-0.0.1/LICENSE +21 -0
- poly_attention-0.0.1/PKG-INFO +83 -0
- poly_attention-0.0.1/README.md +38 -0
- poly_attention-0.0.1/poly_attention/__init__.py +1 -0
- poly_attention-0.0.1/poly_attention/poly_attention.py +123 -0
- poly_attention-0.0.1/pyproject.toml +67 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
uv_publish.sh
|
|
2
|
+
|
|
3
|
+
# Byte-compiled / optimized / DLL files
|
|
4
|
+
__pycache__/
|
|
5
|
+
*.py[codz]
|
|
6
|
+
*$py.class
|
|
7
|
+
|
|
8
|
+
# C extensions
|
|
9
|
+
*.so
|
|
10
|
+
|
|
11
|
+
# Distribution / packaging
|
|
12
|
+
.Python
|
|
13
|
+
build/
|
|
14
|
+
develop-eggs/
|
|
15
|
+
dist/
|
|
16
|
+
downloads/
|
|
17
|
+
eggs/
|
|
18
|
+
.eggs/
|
|
19
|
+
lib/
|
|
20
|
+
lib64/
|
|
21
|
+
parts/
|
|
22
|
+
sdist/
|
|
23
|
+
var/
|
|
24
|
+
wheels/
|
|
25
|
+
share/python-wheels/
|
|
26
|
+
*.egg-info/
|
|
27
|
+
.installed.cfg
|
|
28
|
+
*.egg
|
|
29
|
+
MANIFEST
|
|
30
|
+
|
|
31
|
+
# PyInstaller
|
|
32
|
+
# Usually these files are written by a python script from a template
|
|
33
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
34
|
+
*.manifest
|
|
35
|
+
*.spec
|
|
36
|
+
|
|
37
|
+
# Installer logs
|
|
38
|
+
pip-log.txt
|
|
39
|
+
pip-delete-this-directory.txt
|
|
40
|
+
|
|
41
|
+
# Unit test / coverage reports
|
|
42
|
+
htmlcov/
|
|
43
|
+
.tox/
|
|
44
|
+
.nox/
|
|
45
|
+
.coverage
|
|
46
|
+
.coverage.*
|
|
47
|
+
.cache
|
|
48
|
+
nosetests.xml
|
|
49
|
+
coverage.xml
|
|
50
|
+
*.cover
|
|
51
|
+
*.py.cover
|
|
52
|
+
.hypothesis/
|
|
53
|
+
.pytest_cache/
|
|
54
|
+
cover/
|
|
55
|
+
|
|
56
|
+
# Translations
|
|
57
|
+
*.mo
|
|
58
|
+
*.pot
|
|
59
|
+
|
|
60
|
+
# Django stuff:
|
|
61
|
+
*.log
|
|
62
|
+
local_settings.py
|
|
63
|
+
db.sqlite3
|
|
64
|
+
db.sqlite3-journal
|
|
65
|
+
|
|
66
|
+
# Flask stuff:
|
|
67
|
+
instance/
|
|
68
|
+
.webassets-cache
|
|
69
|
+
|
|
70
|
+
# Scrapy stuff:
|
|
71
|
+
.scrapy
|
|
72
|
+
|
|
73
|
+
# Sphinx documentation
|
|
74
|
+
docs/_build/
|
|
75
|
+
|
|
76
|
+
# PyBuilder
|
|
77
|
+
.pybuilder/
|
|
78
|
+
target/
|
|
79
|
+
|
|
80
|
+
# Jupyter Notebook
|
|
81
|
+
.ipynb_checkpoints
|
|
82
|
+
|
|
83
|
+
# IPython
|
|
84
|
+
profile_default/
|
|
85
|
+
ipython_config.py
|
|
86
|
+
|
|
87
|
+
# pyenv
|
|
88
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
89
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
90
|
+
# .python-version
|
|
91
|
+
|
|
92
|
+
# pipenv
|
|
93
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
94
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
95
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
96
|
+
# install all needed dependencies.
|
|
97
|
+
# Pipfile.lock
|
|
98
|
+
|
|
99
|
+
# UV
|
|
100
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
101
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
102
|
+
# commonly ignored for libraries.
|
|
103
|
+
# uv.lock
|
|
104
|
+
|
|
105
|
+
# poetry
|
|
106
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
107
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
108
|
+
# commonly ignored for libraries.
|
|
109
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
110
|
+
# poetry.lock
|
|
111
|
+
# poetry.toml
|
|
112
|
+
|
|
113
|
+
# pdm
|
|
114
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
115
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
116
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
117
|
+
# pdm.lock
|
|
118
|
+
# pdm.toml
|
|
119
|
+
.pdm-python
|
|
120
|
+
.pdm-build/
|
|
121
|
+
|
|
122
|
+
# pixi
|
|
123
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
124
|
+
# pixi.lock
|
|
125
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
126
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
127
|
+
.pixi
|
|
128
|
+
|
|
129
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
130
|
+
__pypackages__/
|
|
131
|
+
|
|
132
|
+
# Celery stuff
|
|
133
|
+
celerybeat-schedule
|
|
134
|
+
celerybeat.pid
|
|
135
|
+
|
|
136
|
+
# Redis
|
|
137
|
+
*.rdb
|
|
138
|
+
*.aof
|
|
139
|
+
*.pid
|
|
140
|
+
|
|
141
|
+
# RabbitMQ
|
|
142
|
+
mnesia/
|
|
143
|
+
rabbitmq/
|
|
144
|
+
rabbitmq-data/
|
|
145
|
+
|
|
146
|
+
# ActiveMQ
|
|
147
|
+
activemq-data/
|
|
148
|
+
|
|
149
|
+
# SageMath parsed files
|
|
150
|
+
*.sage.py
|
|
151
|
+
|
|
152
|
+
# Environments
|
|
153
|
+
.env
|
|
154
|
+
.envrc
|
|
155
|
+
.venv
|
|
156
|
+
env/
|
|
157
|
+
venv/
|
|
158
|
+
ENV/
|
|
159
|
+
env.bak/
|
|
160
|
+
venv.bak/
|
|
161
|
+
|
|
162
|
+
# Spyder project settings
|
|
163
|
+
.spyderproject
|
|
164
|
+
.spyproject
|
|
165
|
+
|
|
166
|
+
# Rope project settings
|
|
167
|
+
.ropeproject
|
|
168
|
+
|
|
169
|
+
# mkdocs documentation
|
|
170
|
+
/site
|
|
171
|
+
|
|
172
|
+
# mypy
|
|
173
|
+
.mypy_cache/
|
|
174
|
+
.dmypy.json
|
|
175
|
+
dmypy.json
|
|
176
|
+
|
|
177
|
+
# Pyre type checker
|
|
178
|
+
.pyre/
|
|
179
|
+
|
|
180
|
+
# pytype static type analyzer
|
|
181
|
+
.pytype/
|
|
182
|
+
|
|
183
|
+
# Cython debug symbols
|
|
184
|
+
cython_debug/
|
|
185
|
+
|
|
186
|
+
# PyCharm
|
|
187
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
188
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
189
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
190
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
191
|
+
# .idea/
|
|
192
|
+
|
|
193
|
+
# Abstra
|
|
194
|
+
# Abstra is an AI-powered process automation framework.
|
|
195
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
196
|
+
# Learn more at https://abstra.io/docs
|
|
197
|
+
.abstra/
|
|
198
|
+
|
|
199
|
+
# Visual Studio Code
|
|
200
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
201
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
202
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
203
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
204
|
+
# .vscode/
|
|
205
|
+
# Temporary file for partial code execution
|
|
206
|
+
tempCodeRunnerFile.py
|
|
207
|
+
|
|
208
|
+
# Ruff stuff:
|
|
209
|
+
.ruff_cache/
|
|
210
|
+
|
|
211
|
+
# PyPI configuration file
|
|
212
|
+
.pypirc
|
|
213
|
+
|
|
214
|
+
# Marimo
|
|
215
|
+
marimo/_static/
|
|
216
|
+
marimo/_lsp/
|
|
217
|
+
__marimo__/
|
|
218
|
+
|
|
219
|
+
# Streamlit
|
|
220
|
+
.streamlit/secrets.toml
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Phil Wang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: poly-attention
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: PolyAttention
|
|
5
|
+
Project-URL: Homepage, https://pypi.org/project/poly-attention/
|
|
6
|
+
Project-URL: Repository, https://github.com/lucidrains/poly-attention
|
|
7
|
+
Author-email: Phil Wang <lucidrains@gmail.com>
|
|
8
|
+
License: MIT License
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2026 Phil Wang
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Keywords: artificial intelligence,attention mechanism,deep learning
|
|
31
|
+
Classifier: Development Status :: 4 - Beta
|
|
32
|
+
Classifier: Intended Audience :: Developers
|
|
33
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
34
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
35
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
36
|
+
Requires-Python: >=3.10
|
|
37
|
+
Requires-Dist: einops>=0.8.1
|
|
38
|
+
Requires-Dist: einx>=0.3.0
|
|
39
|
+
Requires-Dist: torch>=2.5
|
|
40
|
+
Requires-Dist: x-transformers>=2.21.4
|
|
41
|
+
Provides-Extra: examples
|
|
42
|
+
Provides-Extra: test
|
|
43
|
+
Requires-Dist: pytest; extra == 'test'
|
|
44
|
+
Description-Content-Type: text/markdown
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
## Poly Attention (wip)
|
|
48
|
+
|
|
49
|
+
Implementation of <a href="https://arxiv.org/abs/2602.02422">Poly-Attention</a>, a general scheme for higher-order self-attention
|
|
50
|
+
|
|
51
|
+
## Install
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
$ pip install poly-attention
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
import torch
|
|
61
|
+
from poly_attention import PolyAttention
|
|
62
|
+
|
|
63
|
+
attn = PolyAttention(
|
|
64
|
+
dim = 512,
|
|
65
|
+
heads = 8,
|
|
66
|
+
dim_head = 64,
|
|
67
|
+
causal = False
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
x = torch.randn(1, 1024, 512)
|
|
71
|
+
out = attn(x) # (1, 1024, 512)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Citations
|
|
75
|
+
|
|
76
|
+
```bibtex
|
|
77
|
+
@inproceedings{chakrabarti2026poly,
|
|
78
|
+
title = {Poly-attention: a general scheme for higher-order self-attention},
|
|
79
|
+
author = {Chakrabarti, Sayak and Pitassi, Toniann and Alman, Josh},
|
|
80
|
+
booktitle = {International Conference on Learning Representations (ICLR)},
|
|
81
|
+
year = {2026}
|
|
82
|
+
}
|
|
83
|
+
```
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
|
|
2
|
+
## Poly Attention (wip)
|
|
3
|
+
|
|
4
|
+
Implementation of <a href="https://arxiv.org/abs/2602.02422">Poly-Attention</a>, a general scheme for higher-order self-attention
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
$ pip install poly-attention
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
import torch
|
|
16
|
+
from poly_attention import PolyAttention
|
|
17
|
+
|
|
18
|
+
attn = PolyAttention(
|
|
19
|
+
dim = 512,
|
|
20
|
+
heads = 8,
|
|
21
|
+
dim_head = 64,
|
|
22
|
+
causal = False
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
x = torch.randn(1, 1024, 512)
|
|
26
|
+
out = attn(x) # (1, 1024, 512)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Citations
|
|
30
|
+
|
|
31
|
+
```bibtex
|
|
32
|
+
@inproceedings{chakrabarti2026poly,
|
|
33
|
+
title = {Poly-attention: a general scheme for higher-order self-attention},
|
|
34
|
+
author = {Chakrabarti, Sayak and Pitassi, Toniann and Alman, Josh},
|
|
35
|
+
booktitle = {International Conference on Learning Representations (ICLR)},
|
|
36
|
+
year = {2026}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from poly_attention.poly_attention import PolyAttention
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# Chakrabarti et al. https://arxiv.org/abs/2602.02422
|
|
2
|
+
|
|
3
|
+
from functools import partial
|
|
4
|
+
|
|
5
|
+
import torch
|
|
6
|
+
from torch import nn, einsum
|
|
7
|
+
from torch.nn import Module, RMSNorm
|
|
8
|
+
import torch.nn.functional as F
|
|
9
|
+
|
|
10
|
+
import einx
|
|
11
|
+
from einops import rearrange
|
|
12
|
+
from einops.layers.torch import Rearrange
|
|
13
|
+
|
|
14
|
+
# constants
|
|
15
|
+
|
|
16
|
+
LinearNoBias = partial(nn.Linear, bias=False)
|
|
17
|
+
|
|
18
|
+
# helper functions
|
|
19
|
+
|
|
20
|
+
def exists(val):
|
|
21
|
+
return val is not None
|
|
22
|
+
|
|
23
|
+
def default(val, d):
|
|
24
|
+
return val if exists(val) else d
|
|
25
|
+
|
|
26
|
+
def divisible_by(num, den):
|
|
27
|
+
return (num % den) == 0
|
|
28
|
+
|
|
29
|
+
def softclamp(x, c):
|
|
30
|
+
return c * torch.tanh(x / c)
|
|
31
|
+
|
|
32
|
+
# poly attention
|
|
33
|
+
|
|
34
|
+
class PolyAttention(Module):
|
|
35
|
+
def __init__(
|
|
36
|
+
self,
|
|
37
|
+
dim,
|
|
38
|
+
heads = 8,
|
|
39
|
+
dim_head = 64,
|
|
40
|
+
causal = False,
|
|
41
|
+
softclamp_value = 20.,
|
|
42
|
+
eps = 1e-9
|
|
43
|
+
):
|
|
44
|
+
super().__init__()
|
|
45
|
+
dim_inner = dim_head * heads
|
|
46
|
+
|
|
47
|
+
self.causal = causal
|
|
48
|
+
self.softclamp_value = softclamp_value
|
|
49
|
+
self.scale = dim_head ** -0.5
|
|
50
|
+
self.eps = eps
|
|
51
|
+
|
|
52
|
+
self.split_heads = Rearrange('b n (h d) -> b h n d', h = heads)
|
|
53
|
+
self.merge_heads = Rearrange('b h n d -> b n (h d)')
|
|
54
|
+
|
|
55
|
+
self.to_q_v = LinearNoBias(dim, dim_inner * 5)
|
|
56
|
+
|
|
57
|
+
self.q1_norm = RMSNorm(dim_head)
|
|
58
|
+
self.q2_norm = RMSNorm(dim_head)
|
|
59
|
+
self.q3_norm = RMSNorm(dim_head)
|
|
60
|
+
|
|
61
|
+
self.to_out = nn.Linear(dim_inner, dim)
|
|
62
|
+
|
|
63
|
+
self._reset_parameters()
|
|
64
|
+
|
|
65
|
+
def _reset_parameters(self):
|
|
66
|
+
# small initialization is crucial to prevent exp() overflow
|
|
67
|
+
nn.init.normal_(self.to_q_v.weight, mean = 0.0, std = 0.01)
|
|
68
|
+
nn.init.normal_(self.to_out.weight, mean = 0.0, std = 0.01)
|
|
69
|
+
nn.init.zeros_(self.to_out.bias)
|
|
70
|
+
|
|
71
|
+
def forward(self, x, mask = None):
|
|
72
|
+
device = x.device
|
|
73
|
+
|
|
74
|
+
q1, q2, q3, v2, v3 = map(self.split_heads, self.to_q_v(x).chunk(5, dim = -1))
|
|
75
|
+
|
|
76
|
+
q1 = self.q1_norm(q1)
|
|
77
|
+
q2 = self.q2_norm(q2)
|
|
78
|
+
q3 = self.q3_norm(q3)
|
|
79
|
+
|
|
80
|
+
q_left = torch.stack((q1, q2))
|
|
81
|
+
q_right = torch.stack((q2, q3))
|
|
82
|
+
|
|
83
|
+
# unscaled exp values
|
|
84
|
+
|
|
85
|
+
scores = einsum('... i d, ... j d -> ... i j', q_left, q_right) * self.scale
|
|
86
|
+
scores = softclamp(scores, self.softclamp_value)
|
|
87
|
+
|
|
88
|
+
exp_scores = scores.exp()
|
|
89
|
+
|
|
90
|
+
# causal masking
|
|
91
|
+
|
|
92
|
+
if self.causal:
|
|
93
|
+
i, j = exp_scores.shape[-2:]
|
|
94
|
+
causal_mask = torch.ones((i, j), device = device, dtype = torch.bool).triu(1)
|
|
95
|
+
exp_scores = exp_scores.masked_fill(causal_mask, 0.)
|
|
96
|
+
|
|
97
|
+
# padding masking
|
|
98
|
+
|
|
99
|
+
if exists(mask):
|
|
100
|
+
exp_scores = einx.where('b j, c b h i j, -> c b h i j', mask, exp_scores, 0.)
|
|
101
|
+
|
|
102
|
+
exp_scores12, exp_scores23 = exp_scores
|
|
103
|
+
|
|
104
|
+
# aggregate
|
|
105
|
+
|
|
106
|
+
exp_scores23_v3 = einsum('b h i j, b h j d -> b h i d', exp_scores23, v3)
|
|
107
|
+
unnormalized_out = einsum('b h i j, b h j d -> b h i d', exp_scores12, exp_scores23_v3)
|
|
108
|
+
|
|
109
|
+
# elementwise multiply the root values (v2) with the aggregated messages from the rest of the tree
|
|
110
|
+
|
|
111
|
+
out = v2 * unnormalized_out
|
|
112
|
+
|
|
113
|
+
# normalize
|
|
114
|
+
|
|
115
|
+
exp_scores23_sum = exp_scores23.sum(dim = -1, keepdim = True)
|
|
116
|
+
|
|
117
|
+
denominator = einsum('b h i j, b h j d -> b h i d', exp_scores12, exp_scores23_sum)
|
|
118
|
+
|
|
119
|
+
out = unnormalized_out / denominator.clamp(min = self.eps)
|
|
120
|
+
|
|
121
|
+
# combine heads
|
|
122
|
+
|
|
123
|
+
return self.to_out(self.merge_heads(out))
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "poly-attention"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "PolyAttention"
|
|
5
|
+
authors = [
|
|
6
|
+
{ name = "Phil Wang", email = "lucidrains@gmail.com" }
|
|
7
|
+
]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">= 3.10"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
keywords = [
|
|
12
|
+
'artificial intelligence',
|
|
13
|
+
'deep learning',
|
|
14
|
+
'attention mechanism',
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
classifiers=[
|
|
18
|
+
'Development Status :: 4 - Beta',
|
|
19
|
+
'Intended Audience :: Developers',
|
|
20
|
+
'Topic :: Scientific/Engineering :: Artificial Intelligence',
|
|
21
|
+
'License :: OSI Approved :: MIT License',
|
|
22
|
+
'Programming Language :: Python :: 3.10',
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
dependencies = [
|
|
26
|
+
"einx>=0.3.0",
|
|
27
|
+
"einops>=0.8.1",
|
|
28
|
+
"torch>=2.5",
|
|
29
|
+
"x-transformers>=2.21.4",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[project.urls]
|
|
33
|
+
Homepage = "https://pypi.org/project/poly-attention/"
|
|
34
|
+
Repository = "https://github.com/lucidrains/poly-attention"
|
|
35
|
+
|
|
36
|
+
[project.optional-dependencies]
|
|
37
|
+
examples = []
|
|
38
|
+
test = [
|
|
39
|
+
"pytest"
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[tool.pytest.ini_options]
|
|
43
|
+
pythonpath = [
|
|
44
|
+
"."
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
[build-system]
|
|
48
|
+
requires = ["hatchling"]
|
|
49
|
+
build-backend = "hatchling.build"
|
|
50
|
+
|
|
51
|
+
[tool.rye]
|
|
52
|
+
managed = true
|
|
53
|
+
dev-dependencies = []
|
|
54
|
+
|
|
55
|
+
[tool.hatch.metadata]
|
|
56
|
+
allow-direct-references = true
|
|
57
|
+
|
|
58
|
+
[tool.hatch.build]
|
|
59
|
+
include = [
|
|
60
|
+
"poly_attention",
|
|
61
|
+
"pyproject.toml",
|
|
62
|
+
"README.md",
|
|
63
|
+
"LICENSE"
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
[tool.hatch.build.targets.wheel]
|
|
67
|
+
packages = ["poly_attention"]
|