Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not cache _alpha in GATv2Conv #8753

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions test/nn/conv/test_gatv2_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def forward(
assert result[1][0].size() == (2, 7)
assert result[1][1].size() == (7, 2)
assert result[1][1].min() >= 0 and result[1][1].max() <= 1
assert conv._alpha is None

if torch_geometric.typing.WITH_PT113:
# PyTorch < 1.13 does not support multi-dimensional CSR values :(
Expand All @@ -66,7 +65,6 @@ def forward(
result = conv(x1, adj2.t(), return_attention_weights=True)
assert torch.allclose(result[0], out, atol=1e-6)
assert result[1].sizes() == [4, 4, 2] and result[1].nnz() == 7
assert conv._alpha is None

if is_full_test():

Expand All @@ -88,7 +86,6 @@ def forward(
assert result[1][0].size() == (2, 7)
assert result[1][1].size() == (7, 2)
assert result[1][1].min() >= 0 and result[1][1].max() <= 1
assert conv._alpha is None

if torch_geometric.typing.WITH_TORCH_SPARSE:

Expand All @@ -109,7 +106,6 @@ def forward(
result = jit(x1, adj2.t())
assert torch.allclose(result[0], out, atol=1e-6)
assert result[1].sizes() == [4, 4, 2] and result[1].nnz() == 7
assert conv._alpha is None

# Test bipartite message passing:
adj1 = to_torch_csc_tensor(edge_index, size=(4, 2))
Expand Down
24 changes: 11 additions & 13 deletions torch_geometric/nn/conv/gatv2_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ class GATv2Conv(MessagePassing):
or :math:`((|\mathcal{V_t}|, H * F_{out}), ((2, |\mathcal{E}|),
(|\mathcal{E}|, H)))` if bipartite
"""
_alpha: OptTensor

def __init__(
self,
in_channels: Union[int, Tuple[int, int]],
Expand Down Expand Up @@ -191,8 +189,6 @@ def __init__(
else:
self.register_parameter('bias', None)

self._alpha = None

self.reset_parameters()

def reset_parameters(self):
Expand Down Expand Up @@ -298,12 +294,12 @@ def forward( # noqa: F811
"simultaneously is currently not yet supported for "
"'edge_index' in a 'SparseTensor' form")

# propagate_type: (x: PairTensor, edge_attr: OptTensor)
out = self.propagate(edge_index, x=(x_l, x_r), edge_attr=edge_attr)
# edge_updater_type: (x: PairTensor, edge_attr: OptTensor)
alpha = self.edge_updater(edge_index, x=(x_l, x_r),
edge_attr=edge_attr)

alpha = self._alpha
assert alpha is not None
self._alpha = None
# propagate_type: (x: PairTensor, alpha: Tensor)
out = self.propagate(edge_index, x=(x_l, x_r), alpha=alpha)

if self.concat:
out = out.view(-1, self.heads * self.out_channels)
Expand All @@ -326,9 +322,9 @@ def forward( # noqa: F811
else:
return out

def message(self, x_j: Tensor, x_i: Tensor, edge_attr: OptTensor,
index: Tensor, ptr: OptTensor,
size_i: Optional[int]) -> Tensor:
def edge_update(self, x_j: Tensor, x_i: Tensor, edge_attr: OptTensor,
index: Tensor, ptr: OptTensor,
size_i: Optional[int]) -> Tensor:
x = x_i + x_j

if edge_attr is not None:
Expand All @@ -342,8 +338,10 @@ def message(self, x_j: Tensor, x_i: Tensor, edge_attr: OptTensor,
x = F.leaky_relu(x, self.negative_slope)
alpha = (x * self.att).sum(dim=-1)
alpha = softmax(alpha, index, ptr, size_i)
self._alpha = alpha
alpha = F.dropout(alpha, p=self.dropout, training=self.training)
return alpha

def message(self, x_j: Tensor, alpha: Tensor) -> Tensor:
return x_j * alpha.unsqueeze(-1)

def __repr__(self) -> str:
Expand Down
Loading