diff --git a/GNNGraphs/src/gnngraph.jl b/GNNGraphs/src/gnngraph.jl index b24322e14..3798bdf18 100644 --- a/GNNGraphs/src/gnngraph.jl +++ b/GNNGraphs/src/gnngraph.jl @@ -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, @@ -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 diff --git a/GNNGraphs/src/query.jl b/GNNGraphs/src/query.jl index db8b4ac98..608a62521 100644 --- a/GNNGraphs/src/query.jl +++ b/GNNGraphs/src/query.jl @@ -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] diff --git a/GNNGraphs/test/enzyme.jl b/GNNGraphs/test/enzyme.jl index 9b4a5e08e..a34e15712 100644 --- a/GNNGraphs/test/enzyme.jl +++ b/GNNGraphs/test/enzyme.jl @@ -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