From 44cc3e2299582a5ea172368df59a943926c2a0e7 Mon Sep 17 00:00:00 2001 From: CarloLucibello Date: Sun, 29 Nov 2026 19:09:29 +0100 Subject: [PATCH 1/2] 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 --- GNNGraphs/src/gnngraph.jl | 7 +++---- GNNGraphs/test/enzyme.jl | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) 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/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 From 14a71696d6414d97f7a050f7df53efc30ab8a348 Mon Sep 17 00:00:00 2001 From: CarloLucibello Date: Sun, 29 Nov 2026 19:30:18 +0100 Subject: [PATCH 2/2] 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 --- GNNGraphs/src/query.jl | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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]