Skip to content

Commit f9a8cbd

Browse files
Fix Enzyme through graph transforms (#756)
* Fix Enzyme through graph transforms on Julia 1.13 #726 added a `graph_indicator` eltype check to the positional `GNNGraph` constructor that every transform (`add_self_loops`, `set_edge_weight`, `_to_coo_graph`, ...) calls. On Julia 1.13 Enzyme cannot root the pointer phi that branch produces, so gradients through GCNConv, GATConv & co. fail with an EnzymeRuntimeException. Callers pass an already validated indicator and the keyword constructor keeps the check, so drop it here. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * Make edge_index type-stable on Julia 1.10 `g.graph[1:2]` slices a heterogeneous tuple, which Julia 1.10 does through a Union-typed vector. With active edge weights Enzyme then fails with an IllegalTypeAnalysisException, as the new transform test showed on 1.10. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 9789546 commit f9a8cbd

3 files changed

Lines changed: 37 additions & 7 deletions

File tree

‎GNNGraphs/src/gnngraph.jl‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ struct GNNGraph{T <: Union{COO_T, ADJMAT_T}} <: AbstractGNNGraph{T}
116116
is_coalesced::Bool # only for :coo, true if the graph is coalesced, i.e., no multi edges and indices ordered by target, then source
117117
end
118118

119-
# GNNGraph constructor setting the is_coalesced field to false
119+
# GNNGraph constructor setting the is_coalesced field to false.
120+
# Callers pass an already validated `graph_indicator`; checking it here breaks Enzyme
121+
# on Julia 1.13 (see test/enzyme.jl).
120122
function GNNGraph(graph::T,
121123
num_nodes::Int,
122124
num_edges::Int,
@@ -125,9 +127,6 @@ function GNNGraph(graph::T,
125127
ndata::DataStore,
126128
edata::DataStore,
127129
gdata::DataStore) where {T <: Union{COO_T, ADJMAT_T}}
128-
if !isnothing(graph_indicator)
129-
_check_index_eltype(eltype(graph_indicator))
130-
end
131130
return GNNGraph{T}(graph, num_nodes, num_edges, num_graphs,
132131
graph_indicator, ndata, edata, gdata, false)
133132
end

‎GNNGraphs/src/query.jl‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11

2+
# Not `g.graph[1:2]`: slicing a heterogeneous tuple is type-unstable on Julia 1.10,
3+
# which breaks Enzyme when the edge weights are active.
24
"""
35
edge_index(g::GNNGraph)
46
5-
Return a tuple containing two vectors, respectively storing
7+
Return a tuple containing two vectors, respectively storing
68
the source and target nodes for each edges in `g`.
79
810
```julia
911
s, t = edge_index(g)
1012
```
1113
"""
12-
edge_index(g::GNNGraph{<:COO_T}) = g.graph[1:2]
14+
edge_index(g::GNNGraph{<:COO_T}) = (g.graph[1], g.graph[2])
1315

14-
edge_index(g::GNNGraph{<:ADJMAT_T}) = to_coo(g.graph, num_nodes = g.num_nodes)[1][1:2]
16+
function edge_index(g::GNNGraph{<:ADJMAT_T})
17+
s, t, _ = to_coo(g.graph, num_nodes = g.num_nodes)[1]
18+
return s, t
19+
end
1520

1621
get_edge_weight(g::GNNGraph{<:COO_T}) = g.graph[3]
1722

‎GNNGraphs/test/enzyme.jl‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,29 @@ end
4141
gfd = ngradient(x -> loss(x, g), x)[1]
4242
@test grad ≈ gfd rtol = 1e-4
4343
end
44+
45+
@testitem "Enzyme differentiates through graph transforms" setup=[GraphsTestModule] begin
46+
using .GraphsTestModule
47+
using Enzyme: Enzyme, Const, Reverse
48+
import NNlib
49+
50+
# Transforms rebuild the graph through the positional `GNNGraph` constructor,
51+
# where a branch on `graph_indicator` made Enzyme fail on Julia 1.13.
52+
rng = MersenneTwister(17)
53+
function loss(x, w, g)
54+
g = add_self_loops(set_edge_weight(g, w))
55+
s, _ = edge_index(g)
56+
return sum(abs2, NNlib.gather(x, s) .* get_edge_weight(g)')
57+
end
58+
for graph_type in (:coo, :sparse)
59+
g = rand_graph(rng, 6, 10; graph_type)
60+
x = randn(rng, 3, g.num_nodes)
61+
w = rand(rng, g.num_edges)
62+
# Runtime activity, as Flux's Enzyme backend uses: the rebuilt graph holds
63+
# both the active `w` and constant fields of `g`.
64+
grad = Enzyme.gradient(Enzyme.set_runtime_activity(Reverse), loss, x, w, Const(g))
65+
gfd = ngradient((x, w) -> loss(x, w, g), x, w)
66+
@test grad[1] ≈ gfd[1] rtol = 1e-6
67+
@test grad[2] ≈ gfd[2] rtol = 1e-6
68+
end
69+
end

0 commit comments

Comments
 (0)