PerformerAttention
¶
PerformerAttention(
channels: int,
heads: int,
head_channels: Optional[int] = 64,
kernel: Callable[[array], array] = relu,
qkv_bias: bool = False,
attn_out_bias: bool = True,
dropout: float = 0.0,
)
Bases: Module
The linear scaled attention mechanism from the "Rethinking Attention with Performers" paper, as in PyG.
Queries and keys are mapped by a fixed random orthogonal projection
followed by kernel (plus \(10^{-3}\)), which makes the attention
linear in the number of nodes. The projection matrix is a frozen
parameter that can be redrawn with redraw_projection_matrix().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channels
|
int
|
Size of each input sample. |
required |
heads
|
int
|
Number of attention heads. |
required |
head_channels
|
Optional[int]
|
Size of each attention head. Default: |
64
|
kernel
|
Callable[[array], array]
|
Kernel of the generalized attention. Default: |
relu
|
qkv_bias
|
bool
|
Whether the query, key and value projections have a bias.
Default: |
False
|
attn_out_bias
|
bool
|
Whether the output projection has a bias.
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 PerformerAttention
attn = PerformerAttention(channels=16, heads=2, head_channels=8)
x = mx.random.normal((2, 5, 16))
mask = mx.ones((2, 5), dtype=mx.bool_)
>>> attn(x, mask).shape
(2, 5, 16)
Methods:
| Name | Description |
|---|---|
redraw_projection_matrix |
Draws a new random projection matrix, as recommended in the paper |
__call__ |
Computes the attention. |
Source code in mlx_graphs/nn/attention/performer.py
redraw_projection_matrix
¶
Draws a new random projection matrix, as recommended in the paper to improve the approximation of the attention.
__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/performer.py
from mlx_graphs.nn.attention import PerformerAttention · mlx_graphs/nn/attention/performer.py