Skip to content
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
7 changes: 3 additions & 4 deletions GNNGraphs/src/gnngraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ struct GNNGraph{T <: Union{COO_T, ADJMAT_T}} <: AbstractGNNGraph{T}
is_coalesced::Bool # only for :coo, true if the graph is coalesced, i.e., no multi edges and indices ordered by target, then source
end

# GNNGraph constructor setting the is_coalesced field to false
# GNNGraph constructor setting the is_coalesced field to false.
# Callers pass an already validated `graph_indicator`; checking it here breaks Enzyme
# on Julia 1.13 (see test/enzyme.jl).
function GNNGraph(graph::T,
num_nodes::Int,
num_edges::Int,
Expand All @@ -125,9 +127,6 @@ function GNNGraph(graph::T,
ndata::DataStore,
edata::DataStore,
gdata::DataStore) where {T <: Union{COO_T, ADJMAT_T}}
if !isnothing(graph_indicator)
_check_index_eltype(eltype(graph_indicator))
end
return GNNGraph{T}(graph, num_nodes, num_edges, num_graphs,
graph_indicator, ndata, edata, gdata, false)
end
Expand Down
11 changes: 8 additions & 3 deletions GNNGraphs/src/query.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@

# Not `g.graph[1:2]`: slicing a heterogeneous tuple is type-unstable on Julia 1.10,
# which breaks Enzyme when the edge weights are active.
"""
edge_index(g::GNNGraph)

Return a tuple containing two vectors, respectively storing
Return a tuple containing two vectors, respectively storing
the source and target nodes for each edges in `g`.

```julia
s, t = edge_index(g)
```
"""
edge_index(g::GNNGraph{<:COO_T}) = g.graph[1:2]
edge_index(g::GNNGraph{<:COO_T}) = (g.graph[1], g.graph[2])

edge_index(g::GNNGraph{<:ADJMAT_T}) = to_coo(g.graph, num_nodes = g.num_nodes)[1][1:2]
function edge_index(g::GNNGraph{<:ADJMAT_T})
s, t, _ = to_coo(g.graph, num_nodes = g.num_nodes)[1]
return s, t
end

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

Expand Down
26 changes: 26 additions & 0 deletions GNNGraphs/test/enzyme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,29 @@ end
gfd = ngradient(x -> loss(x, g), x)[1]
@test grad ≈ gfd rtol = 1e-4
end

@testitem "Enzyme differentiates through graph transforms" setup=[GraphsTestModule] begin
using .GraphsTestModule
using Enzyme: Enzyme, Const, Reverse
import NNlib

# Transforms rebuild the graph through the positional `GNNGraph` constructor,
# where a branch on `graph_indicator` made Enzyme fail on Julia 1.13.
rng = MersenneTwister(17)
function loss(x, w, g)
g = add_self_loops(set_edge_weight(g, w))
s, _ = edge_index(g)
return sum(abs2, NNlib.gather(x, s) .* get_edge_weight(g)')
end
for graph_type in (:coo, :sparse)
g = rand_graph(rng, 6, 10; graph_type)
x = randn(rng, 3, g.num_nodes)
w = rand(rng, g.num_edges)
# Runtime activity, as Flux's Enzyme backend uses: the rebuilt graph holds
# both the active `w` and constant fields of `g`.
grad = Enzyme.gradient(Enzyme.set_runtime_activity(Reverse), loss, x, w, Const(g))
gfd = ngradient((x, w) -> loss(x, w, g), x, w)
@test grad[1] ≈ gfd[1] rtol = 1e-6
@test grad[2] ≈ gfd[2] rtol = 1e-6
end
end