SGFormerAttention
¶
SGFormerAttention(
channels: int,
heads: int = 1,
head_channels: Optional[int] = 64,
qkv_bias: bool = False,
)
Bases: Module
The simple global attention mechanism from the "SGFormer: Simplifying and Empowering Transformers for Large-Graph Representations" paper, as in PyG.
Queries and keys are \(\ell_2\)-normalized and the all-pair attention is computed in linear time:
The outputs of the heads are averaged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channels
|
int
|
Size of each input sample. |
required |
heads
|
int
|
Number of attention heads. Default: |
1
|
head_channels
|
Optional[int]
|
Size of each attention head. Default: |
64
|
qkv_bias
|
bool
|
Whether the query, key and value projections have a bias.
Default: |
False
|
Example:
import mlx.core as mx
from mlx_graphs.nn.attention import SGFormerAttention
attn = SGFormerAttention(channels=16, heads=2, head_channels=8)
x = mx.random.normal((2, 5, 16))
>>> attn(x).shape
(2, 5, 8)
Methods:
| Name | Description |
|---|---|
__call__ |
Computes the attention. |
Source code in mlx_graphs/nn/attention/sgformer.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/sgformer.py
from mlx_graphs.nn.attention import SGFormerAttention · mlx_graphs/nn/attention/sgformer.py