PolynormerAttention
¶
PolynormerAttention(
channels: int,
heads: int,
head_channels: int = 64,
beta: float = 0.9,
qkv_bias: bool = False,
qk_shared: bool = True,
dropout: float = 0.0,
)
Bases: Module
The polynomial-expressive linear global attention from the "Polynormer: Polynomial-Expressive Graph Transformer in Linear Time" paper, as in PyG.
Sigmoid-kernelized attention is computed in linear time, normalized with a layer norm and gated by \(\mathbf{H} + \beta\) where \(\mathbf{H}\) is a linear transformation of the input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channels
|
int
|
Size of each input sample. |
required |
heads
|
int
|
Number of attention heads. |
required |
head_channels
|
int
|
Size of each attention head. Default: |
64
|
beta
|
float
|
Polynormer \(\beta\). Default: |
0.9
|
qkv_bias
|
bool
|
Whether the query, key and value projections have a bias.
Default: |
False
|
qk_shared
|
bool
|
Whether queries and keys share their weights.
Default: |
True
|
dropout
|
float
|
Dropout probability of the attention output. Default: |
0.0
|
Example:
import mlx.core as mx
from mlx_graphs.nn.attention import PolynormerAttention
attn = PolynormerAttention(channels=16, heads=2, head_channels=8)
x = mx.random.normal((2, 5, 16))
>>> attn(x).shape
(2, 5, 16)
References
Methods:
| Name | Description |
|---|---|
__call__ |
Computes the attention. |
Source code in mlx_graphs/nn/attention/polynormer.py
__call__
¶
__call__(x: array, mask: Optional[array] = None) -> array
Computes the attention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array
|
Node features of shape |
required |
mask
|
Optional[array]
|
Optional boolean mask of shape |
None
|
Returns:
| Type | Description |
|---|---|
array
|
An array of shape |
Source code in mlx_graphs/nn/attention/polynormer.py
from mlx_graphs.nn.attention import PolynormerAttention · mlx_graphs/nn/attention/polynormer.py