Skip to content

Commit ec7f620

Browse files
authored
Add GNNGraphsEnzymeCoreExt marking scaled_laplacian inactive for Enzyme (#706)
Signed-off-by: Parvm1102 <parvmittal31757@gmail.com>
1 parent 486fa54 commit ec7f620

4 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Entries link to the pull request that introduced them.
1616
- Added a `Mooncake` package extension, so that Mooncake can differentiate on CUDA through `add_self_loops` and through adjacency-matrix graphs. Float edge weights of adjacency-matrix graphs stay differentiable ([#704]).
1717

1818
**Fixed**
19+
- Added an `EnzymeCore` package extension marking `scaled_laplacian` inactive for Enzyme, matching its existing `@non_differentiable` ChainRules declaration. Enzyme previously differentiated the Krylov eigensolve inside it and failed; this unblocks `ChebConv`, `GConvGRUCell` and `GConvLSTMCell` ([#706]).
1920
- 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]).
2021

2122
## GNNLux.jl — Unreleased (towards 0.2.0)
@@ -234,4 +235,5 @@ Lux implementations of the graph convolutional, pooling, and temporal layers
234235
[#696]: https://github.com/JuliaGraphs/GraphNeuralNetworks.jl/pull/696
235236
[#703]: https://github.com/JuliaGraphs/GraphNeuralNetworks.jl/pull/703
236237
[#704]: https://github.com/JuliaGraphs/GraphNeuralNetworks.jl/pull/704
238+
[#706]: https://github.com/JuliaGraphs/GraphNeuralNetworks.jl/pull/706
237239
[FluxML/Zygote.jl#1662]: https://github.com/FluxML/Zygote.jl/issues/1662

‎GNNGraphs/Project.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
2323

2424
[weakdeps]
2525
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
26+
EnzymeCore = "f151be2c-9106-41f4-ab19-57ee4f262869"
2627
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
2728
SimpleWeightedGraphs = "47aef6b3-ad0c-573a-a1e2-d07658019622"
2829

2930
[extensions]
3031
GNNGraphsCUDAExt = "CUDA"
32+
GNNGraphsEnzymeCoreExt = "EnzymeCore"
3133
GNNGraphsMooncakeExt = "Mooncake"
3234
GNNGraphsSimpleWeightedGraphsExt = "SimpleWeightedGraphs"
3335

3436
[compat]
3537
CUDA = "5, 6"
3638
ChainRulesCore = "1"
39+
EnzymeCore = "0.8"
3740
Functors = "0.5"
3841
Graphs = "1.4"
3942
KrylovKit = "0.8, 0.9, 0.10"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module GNNGraphsEnzymeCoreExt
2+
3+
using GNNGraphs
4+
import EnzymeCore
5+
6+
# `scaled_laplacian` is `@non_differentiable` for ChainRules (see query.jl), but Enzyme
7+
# does not read those declarations and would differentiate its Krylov eigensolve.
8+
# It depends only on the graph, never on the node features, so no gradient is lost.
9+
EnzymeCore.EnzymeRules.inactive(::typeof(GNNGraphs.scaled_laplacian), args...) = nothing
10+
11+
end # module

‎GNNGraphs/test/enzyme.jl‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,21 @@
2323
Enzyme.autodiff(Reverse, loss_w, Active, Duplicated(g, dg))
2424
@test nonzeros(dg.graph) ≈ 2 .* nonzeros(S)
2525
end
26+
27+
@testitem "Enzyme treats scaled_laplacian as constant" setup=[GraphsTestModule] begin
28+
using .GraphsTestModule
29+
using Enzyme: Enzyme, Const, Reverse
30+
31+
# Without GNNGraphsEnzymeCoreExt Enzyme differentiates the Krylov eigensolve
32+
# inside `scaled_laplacian` and fails; ChainRules already treats it as constant.
33+
# Bidirected cycle: symmetric and without isolated nodes.
34+
src = [1, 2, 3, 4, 5, 6, 7, 8]
35+
dst = [2, 3, 4, 5, 6, 7, 8, 1]
36+
g = GNNGraph(vcat(src, dst), vcat(dst, src))
37+
x = randn(MersenneTwister(17), Float32, 3, g.num_nodes)
38+
loss(x, g) = sum(abs2, x * scaled_laplacian(g, Float32))
39+
40+
grad = Enzyme.gradient(Reverse, loss, x, Const(g))[1]
41+
gfd = ngradient(x -> loss(x, g), x)[1]
42+
@test grad ≈ gfd rtol = 1e-4
43+
end

0 commit comments

Comments
 (0)