Skip to content

Commit fe045f1

Browse files
authored
Fix Enzyme differentiation of GCNConv/SGConv/TAGConv on adjacency-matrix graphs via a keyword-free COO conversion helper (#703)
* Fix Enzyme crash on dense/sparse adjacency graphs via Enzyme-inactive COO conversion * Restrict Enzyme-inactive to integer structure extraction, keep values differentiable Signed-off-by: Parvm1102 <parvmittal31757@gmail.com> * Simplify Enzyme fix per review: drop EnzymeCore ext and rules, use keyword-free _to_coo_graph --------- Signed-off-by: Parvm1102 <parvmittal31757@gmail.com>
1 parent 3888b1a commit fe045f1

8 files changed

Lines changed: 67 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 16 additions & 5 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 — Unreleased (towards 1.5.2)
14+
15+
**Fixed**
16+
- Enzyme can now differentiate through the adjacency-matrix → COO graph conversion: a new internal keyword-free helper `_to_coo_graph` avoids the union-typed keyword handling of the `GNNGraph(g; graph_type)` constructor and of `to_coo`, which Enzyme's type analysis cannot compile. Together with upstream fixes in Enzyme ≥ 0.13.197 this removes the `EnzymeInternalError` crash on `:dense`/`:sparse` graphs ([#703]).
17+
1318
## GNNLux.jl — Unreleased (towards 0.2.0)
1419

1520
**Added**
@@ -24,19 +29,24 @@ Entries link to the pull request that introduced them.
2429
**Removed**
2530
- Removed the `A3TGCN` layer, mirroring its removal from the Flux frontend ([#696]).
2631

27-
## GNNlib.jl — Unreleased (towards 1.4.0)
28-
29-
**Added**
30-
- Added framework-agnostic forward passes for the recurrent temporal cells — `tgcn`, `gconv_gru`, `gconv_lstm`, `dcgru` — shared by the Flux and Lux frontends ([#696]).
32+
## GNNlib.jl — Unreleased (towards 1.4.1)
3133

3234
**Fixed**
33-
- Worked around a Julia 1.12 code-generation segfault that crashed `CGConv` and `GMMConv` gradients: the layers' `@warn` (in the residual branch) is now wrapped in `ignore_derivatives` to keep the logging macro out of the AD-differentiated code path. Root cause reported upstream as [FluxML/Zygote.jl#1662]; re-enables the previously disabled `CGConv`/`GMMConv` gradient tests ([#695]).
35+
- Fixed `Enzyme.gradient` failing when differentiating `GCNConv`, `SGConv` and `TAGConv` on `:dense`/`:sparse` adjacency graphs: their adjacency-matrix fallbacks now convert via the Enzyme-differentiable `GNNGraphs._to_coo_graph` instead of the keyword `GNNGraph` constructor. Requires GNNGraphs ≥ 1.5.2 and, for Enzyme, Enzyme ≥ 0.13.197 ([#703]).
3436

3537
## GraphNeuralNetworks.jl — Unreleased (towards 1.1.1)
3638

3739
**Changed**
3840
- The recurrent temporal cells now delegate their forward-pass math to shared `GNNlib` functions (requires GNNlib ≥ 1.4); the layer behaviour is unchanged ([#696]).
3941

42+
## GNNlib.jl 1.4.0 — 2026-07-22
43+
44+
**Added**
45+
- Added framework-agnostic forward passes for the recurrent temporal cells — `tgcn`, `gconv_gru`, `gconv_lstm`, `dcgru` — shared by the Flux and Lux frontends ([#696]).
46+
47+
**Fixed**
48+
- Worked around a Julia 1.12 code-generation segfault that crashed `CGConv` and `GMMConv` gradients: the layers' `@warn` (in the residual branch) is now wrapped in `ignore_derivatives` to keep the logging macro out of the AD-differentiated code path. Root cause reported upstream as [FluxML/Zygote.jl#1662]; re-enables the previously disabled `CGConv`/`GMMConv` gradient tests ([#695]).
49+
4050
## GNNGraphs.jl 1.5.1 — 2026-07-22
4151

4252
**Changed**
@@ -219,4 +229,5 @@ Lux implementations of the graph convolutional, pooling, and temporal layers
219229
[#623]: https://github.com/JuliaGraphs/GraphNeuralNetworks.jl/issues/623
220230
[#695]: https://github.com/JuliaGraphs/GraphNeuralNetworks.jl/pull/695
221231
[#696]: https://github.com/JuliaGraphs/GraphNeuralNetworks.jl/pull/696
232+
[#703]: https://github.com/JuliaGraphs/GraphNeuralNetworks.jl/pull/703
222233
[FluxML/Zygote.jl#1662]: https://github.com/FluxML/Zygote.jl/issues/1662

‎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.5.1"
3+
version = "1.5.2-DEV"
44
authors = ["Carlo Lucibello and contributors"]
55

66
[workspace]

‎GNNGraphs/src/convert.jl‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ function to_coo(adj_list::ADJLIST_T; dir = :out, num_nodes = nothing, weighted =
116116
(s, t, nothing), num_nodes, num_edges
117117
end
118118

119+
# Keyword-free equivalent of `GNNGraph(g, graph_type = :coo)`: Enzyme's type analysis
120+
# cannot compile the union-typed keyword bodies of the GNNGraph constructor and `to_coo`.
121+
function _to_coo_graph(g::GNNGraph{<:SPARSE_T})
122+
s, t, v = findnz(g.graph)
123+
return GNNGraph((s, t, v), g.num_nodes, g.num_edges, g.num_graphs,
124+
g.graph_indicator, g.ndata, g.edata, g.gdata)
125+
end
126+
127+
function _to_coo_graph(g::GNNGraph{<:ADJMAT_T})
128+
A = g.graph
129+
s, t, nz = _findnz_idx(A)
130+
v = A[nz]
131+
return GNNGraph((s, t, v), g.num_nodes, g.num_edges, g.num_graphs,
132+
g.graph_indicator, g.ndata, g.edata, g.gdata)
133+
end
134+
119135
### CONVERT TO ADJACENCY MATRIX ################
120136

121137
### DENSE ####################

‎GNNGraphs/test/Project.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[deps]
2+
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
23
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
34
Functors = "d9f16b24-f501-4c13-a1f2-28368ffc5196"
45
GNNGraphs = "aed8fd31-079b-4b5a-b342-a13352159b8c"
@@ -24,5 +25,6 @@ Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
2425
GNNGraphs = {path = ".."}
2526

2627
[compat]
28+
Enzyme = "0.13.197"
2729
NNlib = "0.9.38"
2830
StableRNGs = "1.0.4"

‎GNNGraphs/test/enzyme.jl‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@testitem "Enzyme differentiates through adjacency-matrix → COO conversion" setup=[GraphsTestModule] begin
2+
using .GraphsTestModule
3+
using Enzyme: Enzyme, Const, Reverse, Duplicated, Active
4+
import NNlib
5+
6+
# Differentiating the conversion used by gcn_conv & co. on :dense/:sparse graphs
7+
# used to crash Enzyme (EnzymeAD/Enzyme.jl#3409, fixed in Enzyme 0.13.197).
8+
rng = MersenneTwister(17)
9+
loss(x, g) = sum(abs2, NNlib.gather(x, edge_index(GNNGraphs._to_coo_graph(g))[1]))
10+
for graph_type in (:dense, :sparse)
11+
g = rand_graph(rng, 6, 10; graph_type)
12+
x = randn(rng, Float32, 3, g.num_nodes)
13+
grad = Enzyme.gradient(Reverse, loss, x, Const(g))[1]
14+
gfd = ngradient(x -> loss(x, g), x)[1]
15+
@test grad ≈ gfd rtol = 1e-4
16+
end
17+
18+
# Edge weights stay on the differentiable path through the conversion.
19+
loss_w(g) = sum(abs2, get_edge_weight(GNNGraphs._to_coo_graph(g)))
20+
S = sparse([1, 2, 3, 4, 5], [2, 3, 4, 5, 6], rand(rng, 5), 6, 6)
21+
g = GNNGraph(S, graph_type = :sparse)
22+
dg = Enzyme.make_zero(g)
23+
Enzyme.autodiff(Reverse, loss_w, Active, Duplicated(g, dg))
24+
@test nonzeros(dg.graph) ≈ 2 .* nonzeros(S)
25+
end

‎GNNlib/Project.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "GNNlib"
22
uuid = "a6a84749-d869-43f8-aacc-be26a1996e48"
33
authors = ["Carlo Lucibello and contributors"]
4-
version = "1.4.0"
4+
version = "1.4.1-DEV"
55

66
[workspace]
77
projects = ["test", "docs", "../GNNGraphs"]
@@ -33,7 +33,7 @@ AMDGPU = "1, 2.7"
3333
CUDA = "5, 6"
3434
ChainRulesCore = "1.24"
3535
DataStructures = "0.18, 0.19"
36-
GNNGraphs = "1.4"
36+
GNNGraphs = "1.5.2"
3737
LinearAlgebra = "1"
3838
Metal = "1.0"
3939
MLUtils = "0.4"

‎GNNlib/src/GNNlib.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ using ChainRulesCore: @non_differentiable, ignore_derivatives
1010
using GNNGraphs
1111
using .GNNGraphs: COO_T, ADJMAT_T, SPARSE_T,
1212
check_num_nodes, check_num_edges,
13+
_to_coo_graph,
1314
EType, NType # for heteroconvs
1415

1516
include("utils.jl")

‎GNNlib/src/layers/conv.jl‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ end
7474
# when we also have edge_weight we need to convert the graph to COO
7575
function gcn_conv(l, g::GNNGraph{<:ADJMAT_T}, x, edge_weight::EW, norm_fn::F, conv_weight::CW) where
7676
{EW <: Union{Nothing, AbstractVector}, CW<:Union{Nothing,AbstractMatrix}, F}
77-
g = GNNGraph(g, graph_type = :coo)
77+
g = _to_coo_graph(g)
7878
return gcn_conv(l, g, x, edge_weight, norm_fn, conv_weight)
7979
end
8080

@@ -460,7 +460,7 @@ end
460460
# when we also have edge_weight we need to convert the graph to COO
461461
function sgc_conv(l, g::GNNGraph{<:ADJMAT_T}, x::AbstractMatrix,
462462
edge_weight::AbstractVector)
463-
g = GNNGraph(g; graph_type=:coo)
463+
g = _to_coo_graph(g)
464464
return sgc_conv(l, g, x, edge_weight)
465465
end
466466

@@ -554,7 +554,7 @@ end
554554
# when we also have edge_weight we need to convert the graph to COO
555555
function sg_conv(l, g::GNNGraph{<:ADJMAT_T}, x::AbstractMatrix,
556556
edge_weight::AbstractVector)
557-
g = GNNGraph(g; graph_type=:coo)
557+
g = _to_coo_graph(g)
558558
return sg_conv(l, g, x, edge_weight)
559559
end
560560

@@ -697,7 +697,7 @@ end
697697
# when we also have edge_weight we need to convert the graph to COO
698698
function tag_conv(l, g::GNNGraph{<:ADJMAT_T}, x::AbstractMatrix,
699699
edge_weight::AbstractVector)
700-
g = GNNGraph(g; graph_type = :coo)
700+
g = _to_coo_graph(g)
701701
return l(g, x, edge_weight)
702702
end
703703

0 commit comments

Comments
 (0)