poly-attention 0.0.1__py3-none-any.whl

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.
@@ -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,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,6 @@
1
+ poly_attention/__init__.py,sha256=IZGqAZPTmP2Td66HzQk0M3a6Kk6lExVM-1ReQ2y7jdg,56
2
+ poly_attention/poly_attention.py,sha256=aeLT6gQWsrma5LqU_zSru_nBqs9kcr-7hQIJgAOWjVs,3405
3
+ poly_attention-0.0.1.dist-info/METADATA,sha256=AvPUwBRDvdVrxq5BCvhBh8zwoqpFYtnprJ06O3qPjCE,2883
4
+ poly_attention-0.0.1.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
5
+ poly_attention-0.0.1.dist-info/licenses/LICENSE,sha256=e6AOF7Z8EFdK3IdcL0x0fLw4cY7Q0d0kNR0o0TmBewM,1066
6
+ poly_attention-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.30.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -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.