Skip to content

Commit 0846e87

Browse files
authored
Fix Zygote gradient through add_edges (#729)
* Fix Zygote gradient through add_edges * Fix sampling and edge-weight differentiation tests * Avoid differentiating empty graph metadata * Test gradients through added edge metadata * Document pending GNNGraphs fixes * Fix GNNLux MegNetConv state propagation
1 parent 3b4ed42 commit 0846e87

18 files changed

Lines changed: 975 additions & 9 deletions

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*.swo
77
Manifest.toml
88
.vscode
9+
.tools/
10+
.julia-depot/
911
LocalPreferences.toml
1012
.DS_Store
1113
test.jl

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
1010
and the packages adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1111
Entries link to the pull request that introduced them.
1212

13+
## GNNGraphs.jl 1.6.1-DEV
14+
15+
**Fixed**
16+
- Fixed Zygote differentiation through `add_edges` with non-empty edge metadata by iterating over a collected vector of metadata keys. `sample_neighbors(...; replace=true)` now skips zero-degree nodes instead of trying to sample from an empty neighbor list ([#729]).
17+
1318
## GNNGraphs.jl 1.6.0 - 2026-11-04
1419

1520
**Added**
@@ -250,5 +255,6 @@ Lux implementations of the graph convolutional, pooling, and temporal layers
250255
[#717]: https://github.com/JuliaGraphs/GraphNeuralNetworks.jl/pull/717
251256
[#722]: https://github.com/JuliaGraphs/GraphNeuralNetworks.jl/pull/722
252257
[#718]: https://github.com/JuliaGraphs/GraphNeuralNetworks.jl/pull/718
258+
[#729]: https://github.com/JuliaGraphs/GraphNeuralNetworks.jl/pull/729
253259
[#728]: https://github.com/JuliaGraphs/GraphNeuralNetworks.jl/pull/728
254260
[FluxML/Zygote.jl#1662]: https://github.com/FluxML/Zygote.jl/issues/1662

‎DEVELOPMENT.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Local development setup
2+
3+
Run PowerShell from the repository root:
4+
5+
```powershell
6+
.\scripts\setup-dev.ps1
7+
```
8+
9+
The setup installs Julia 1.13.3 as a portable copy under `.tools`, verifies its
10+
SHA-256 checksum, then installs the workspace, package, test, docs, and
11+
JuliaFormatter environments. Julia packages, compiled caches, and artifacts
12+
stay in `.julia-depot`. Neither setup nor the launcher edits the machine PATH or
13+
uses the global Julia depot. Both directories are ignored by Git.
14+
15+
Use the repo-local Julia launcher for subsequent work:
16+
17+
```powershell
18+
.\scripts\julia.ps1 --version
19+
.\scripts\julia.ps1 --project=GNNGraphs -e 'using Pkg; Pkg.test("GNNGraphs")'
20+
.\scripts\julia.ps1 --project=GraphNeuralNetworks -e 'using Pkg; Pkg.test("GraphNeuralNetworks")'
21+
.\scripts\julia.ps1 --project=GNNGraphs/docs GNNGraphs/docs/make.jl
22+
.\scripts\julia.ps1 --project=.tools/devtools -e 'using JuliaFormatter; format(".")'
23+
```
24+
25+
The full package test environments are installed, but setup does not execute
26+
the test suites. GPU backends remain opt-in; install and select the backend
27+
required by the machine before running GPU-tagged tests.

‎GNNGraphs/Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "GNNGraphs"
22
uuid = "aed8fd31-079b-4b5a-b342-a13352159b8c"
3-
version = "1.6.0"
3+
version = "1.6.1-DEV"
44
authors = ["Carlo Lucibello and contributors"]
55

66
[workspace]

‎GNNGraphs/src/datastore.jl‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,11 @@ function cat_features(ds1::DataStore, ds2::DataStore)
172172
n1, n2 = getn(ds1), getn(ds2)
173173
n1 = n1 >= 0 ? n1 : 1
174174
n2 = n2 >= 0 ? n2 : 1
175-
return DataStore(n1 + n2, cat_features(getdata(ds1), getdata(ds2)))
175+
data1, data2 = getdata(ds1), getdata(ds2)
176+
if isempty(data1) && isempty(data2)
177+
return CRC.ignore_derivatives(() -> DataStore(n1 + n2, Dict{Symbol, Any}()))
178+
end
179+
return DataStore(n1 + n2, cat_features(data1, data2))
176180
end
177181

178182
function cat_features(dss::AbstractVector{DataStore}; kws...)

‎GNNGraphs/src/sampling.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ function sample_neighbors(g::GNNGraph{<:COO_T}, nodes, K = -1;
177177
@assert dir ∈ (:in, :out)
178178
_, eidlist = adjacency_list(g, nodes; dir, with_eid = true)
179179
for i in 1:length(eidlist)
180+
isempty(eidlist[i]) && continue
180181
if replace
181182
k = K > 0 ? K : length(eidlist[i])
182183
else

‎GNNGraphs/src/utils.jl‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ function cat_features(x1::NamedTuple, x2::NamedTuple)
6565
end
6666

6767
function cat_features(x1::Dict{Symbol, T}, x2::Dict{Symbol, T}) where {T}
68-
sort(collect(keys(x1))) == sort(collect(keys(x2))) ||
68+
keys1, keys2 = CRC.ignore_derivatives() do
69+
sort(collect(keys(x1))), sort(collect(keys(x2)))
70+
end
71+
keys1 == keys2 ||
6972
@error "cannot concatenate feature data with different keys"
7073

71-
return Dict{Symbol, T}([k => cat_features(x1[k], x2[k]) for k in keys(x1)]...)
74+
return Dict{Symbol, T}([k => cat_features(x1[k], x2[k]) for k in keys1]...)
7275
end
7376

7477
function cat_features(x::Dict)
@@ -391,4 +394,4 @@ function color_refinement(g::GNNGraph, x0::AbstractVector{<:Integer})
391394
end
392395
num_colors = length(union(x))
393396
return x, num_colors, niters
394-
end
397+
end

‎GNNGraphs/test/sampling.jl‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
g = rand_graph(10, 40, bidirected = false, graph_type = GRAPH_T)
2626
sg = sample_neighbors(g, nodes, K; dir, replace = true)
2727
@test sg.num_nodes == 10
28-
@test sg.num_edges == sum(K for i in nodes)
29-
@test size(sg.edata.EID) == (sg.num_edges,)
3028
adjlist = adjacency_list(g; dir)
29+
@test sg.num_edges == sum(isempty(adjlist[i]) ? 0 : K for i in nodes)
30+
@test size(sg.edata.EID) == (sg.num_edges,)
3131
s, t = edge_index(sg)
3232
@test all(s .∈ Ref(nodes))
3333
for i in nodes
@@ -47,6 +47,24 @@
4747
@test sg.ndata.x1 == g.ndata.x1[sg.ndata.NID]
4848
@test length(union(sg.ndata.NID)) == length(sg.ndata.NID)
4949
end
50+
51+
@testset "zero-degree nodes with replacement" begin
52+
g = GNNGraph(([1], [2]); num_nodes = 3)
53+
54+
for (dir, node) in ((:in, 1), (:out, 2))
55+
sg = sample_neighbors(g, [node], 2; dir, replace = true)
56+
@test sg.num_nodes == 3
57+
@test sg.num_edges == 0
58+
@test isempty(sg.edata.EID)
59+
@test edge_index(sg) == (Int[], Int[])
60+
end
61+
62+
for (dir, node) in ((:in, 2), (:out, 1))
63+
sg = sample_neighbors(g, [node], 2; dir, replace = true)
64+
@test sg.num_edges == 2
65+
@test sg.edata.EID == [1, 1]
66+
end
67+
end
5068
end
5169

5270
@testitem "induced_subgraph" setup=[GraphsTestModule] begin

‎GNNGraphs/test/transform.jl‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,38 @@ end
295295
gnew = add_edges(g, (snew, tnew, wnew))
296296
@test get_edge_weight(gnew) == [w; wnew]
297297
end
298+
299+
@testset "differentiate through added edge weights" begin
300+
s = [1, 2]
301+
t = [2, 3]
302+
w = Float32[0.5, 0.7]
303+
g = GNNGraph((s, t, w))
304+
snew = [1, 2]
305+
tnew = [2, 3]
306+
wnew = Float32[0.1, 0.2]
307+
308+
loss_fn(wnew) = sum(get_edge_weight(add_edges(g, (snew, tnew, wnew))))
309+
gnew = add_edges(g, (snew, tnew, wnew))
310+
311+
@test gnew.num_edges == 4
312+
@test edge_index(gnew) == ([1, 2, 1, 2], [2, 3, 2, 3])
313+
@test get_edge_weight(gnew) == Float32[0.5, 0.7, 0.1, 0.2]
314+
@test gradient(loss_fn, wnew)[1] == ones(Float32, 2)
315+
end
316+
317+
@testset "differentiate through added edge data" begin
318+
s = [1, 2]
319+
t = [2, 3]
320+
edata = (x = reshape(Float32[0.5, 0.7], 1, :),)
321+
g = GNNGraph((s, t); edata)
322+
snew = [1, 2]
323+
tnew = [2, 3]
324+
xnew = reshape(Float32[0.1, 0.2], 1, :)
325+
326+
loss_fn(xnew) = sum(add_edges(g, (snew, tnew); edata = (x = xnew,)).edata.x)
327+
328+
@test gradient(loss_fn, xnew)[1] == ones(Float32, size(xnew))
329+
end
298330
end
299331
end
300332
end

‎GNNLux/reproduce_issue_732.jl‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Random
2+
using GNNLux
3+
using Lux
4+
using LuxCore
5+
6+
# Small directed graph with two nodes and two edges.
7+
g = GNNGraph([1, 2], [2, 1])
8+
in_dims, out_dims = 3, 4
9+
x = randn(MersenneTwister(1), Float32, in_dims, g.num_nodes)
10+
e = randn(MersenneTwister(2), Float32, in_dims, g.num_edges)
11+
12+
# MEGNet's edge update receives source node, destination node, and edge
13+
# features (3 * in_dims); BatchNorm keeps running statistics in its state.
14+
ϕe = Chain(Dense(3in_dims => out_dims), BatchNorm(out_dims))
15+
ϕv = Chain(Dense(in_dims + out_dims => out_dims), Dense(out_dims => out_dims))
16+
l = MEGNetConv(in_dims, out_dims, ϕe, ϕv)
17+
18+
ps, st = LuxCore.setup(MersenneTwister(3), l)
19+
edge_state_before = st.ϕe.layer_2
20+
(x_new, e_new), st_new = l(g, x, e, ps, st)
21+
edge_state_after = st_new.ϕe.layer_2
22+
23+
println("Output shapes: nodes=$(size(x_new)), edges=$(size(e_new))")
24+
println("Input BatchNorm running_mean: ", edge_state_before.running_mean)
25+
println("Returned BatchNorm running_mean: ", edge_state_after.running_mean)
26+
println("State changed: ", edge_state_after.running_mean != edge_state_before.running_mean)
27+
println("Returned full state equals input state: ", st_new == st)

0 commit comments

Comments
 (0)