Skip to content

Commit 5fac4ae

Browse files
Fix GatedGraphConv hidden-state recursion for num_layers > 1
The heterograph change built the messages of every step from the initial padded features, so homogeneous inputs with num_layers > 1 no longer matched the GGNN recursion. Messages again come from the evolving hidden state, unless distinct source features are given (e.g. another node type): those have no hidden state in the relation and stay fixed, padded to `dims`. Source features are now also checked against `dims`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1e642b8 commit 5fac4ae

1 file changed

Lines changed: 13 additions & 22 deletions

File tree

‎GNNlib/src/layers/conv.jl‎

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -215,32 +215,23 @@ end
215215

216216
####################### GatedGraphConv ######################################
217217

218+
function _gated_graph_pad(x, dims)
219+
m, n = size(x)
220+
@assert m <= dims "number of input features must be less or equal to output features."
221+
return m < dims ? vcat(x, zeros_like(x, (dims - m, n))) : x
222+
end
223+
218224
function gated_graph_conv(l, g::AbstractGNNGraph, x)
219225
check_num_nodes(g, x)
220226
xj, xi = expand_srcdst(g, x)
221-
222-
h = xi
223-
m, n = size(h)
224-
@assert m <= l.dims "number of input features must be less or equal to output features."
225-
if m < l.dims
226-
xpad = zeros_like(h, (l.dims - m, n))
227-
h = vcat(h, xpad)
228-
end
229-
230-
if xj !== xi
231-
mj, nj = size(xj)
232-
if mj < l.dims
233-
xpad = zeros_like(xj, (l.dims - mj, nj))
234-
xj = vcat(xj, xpad)
235-
end
236-
else
237-
xj = h
238-
end
239-
227+
h = _gated_graph_pad(xi, l.dims)
228+
# Distinct source features (e.g. another node type) have no hidden state in the recursion.
229+
fixed_src = xj !== xi
230+
hj = fixed_src ? _gated_graph_pad(xj, l.dims) : h
240231
for i in 1:(l.num_layers)
241-
msg = view(l.weight, :, :, i) * xj
242-
msg = propagate(copy_xj, g, l.aggr; xj = msg)
243-
_, h = l.gru(msg, h)
232+
m = view(l.weight, :, :, i) * (fixed_src ? hj : h)
233+
m = propagate(copy_xj, g, l.aggr; xj = m)
234+
_, h = l.gru(m, h)
244235
end
245236
return h
246237
end

0 commit comments

Comments
 (0)