Skip to content

Commit 1d00c97

Browse files
committed
Keep Reactant graph indices dynamic in GCNConv
# Conflicts: # CHANGELOG.md # GNNGraphs/Project.toml # GNNlib/Project.toml
1 parent 2b3f6f0 commit 1d00c97

8 files changed

Lines changed: 75 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
1010
and the packages adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1111
Entries link to the pull request that introduced them.
1212

13+
## GNNGraphs.jl — Unreleased (towards 1.7.0)
14+
15+
**Fixed**
16+
- Reactant traced scalar types are now converted to their underlying numeric type before graph-side degree and COO adjacency allocation. COO graph indices can also remain traced device inputs during compiled GNNLux model calls. Reactant remains an optional GNNGraphs integration ([#710]).
17+
1318
## GNNGraphs.jl 1.6.0 - 2026-11-04
1419

1520
**Added**
1621
- 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]).
1722

1823
**Fixed**
19-
- Reactant traced scalar types are now converted to their underlying numeric type before graph-side degree and COO adjacency allocation. Reactant remains an optional GNNGraphs integration ([#710]).
2024
- Fixed `scaled_laplacian` (and so `ChebConv`) crashing with a segmentation fault on AMDGPU graphs: the eigensolver's start vector was created on the CPU, so CPU BLAS was handed GPU memory. It is now created on the same device as the graph ([#722]).
2125
- 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]).
2226
- 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]).
@@ -26,6 +30,11 @@ Entries link to the pull request that introduced them.
2630
**Fixed**
2731
- `GNNChain` now returns its state with the same structure it receives, `(; layers = (; layer_1, ...))`, as the Lux layer interface requires. It previously returned the per-layer states without the `layers` wrapper, so feeding the returned state back into the chain (as `Lux.Training.single_train_step!` does) failed with a `FieldError`. This is breaking for code that worked around the bug by re-wrapping the state as `(; layers = st)`, as the docs examples did: drop the wrapper.
2832

33+
## GNNlib.jl — Unreleased (towards 1.5.0)
34+
35+
**Fixed**
36+
- Reactant compiled message passing now uses gather/scatter with traced COO indices instead of building a sparse adjacency matrix, allowing one compiled GCNConv to run on different graphs with the same shapes ([#710]).
37+
2938
## GNNlib.jl 1.4.1 - 2026-11-04
3039

3140
**Fixed**

‎GNNGraphs/Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "GNNGraphs"
22
uuid = "aed8fd31-079b-4b5a-b342-a13352159b8c"
3-
version = "1.6.0"
3+
version = "1.7.0-DEV"
44
authors = ["Carlo Lucibello and contributors"]
55

66
[workspace]

‎GNNGraphs/src/abstracttypes.jl‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11

2-
const COO_T = Tuple{T, T, V} where {T <: AbstractVector{<:Integer}, V <: Union{Nothing, AbstractVector}}
2+
# COO index arrays may have non-integer wrapper eltypes while traced by a compiler
3+
# such as Reactant. Constructors still validate ordinary graph indices.
4+
const COO_T = Tuple{T, T, V} where {T <: AbstractVector, V <: Union{Nothing, AbstractVector}}
35
const ADJLIST_T = AbstractVector{T} where {T <: AbstractVector{<:Integer}}
46
const ADJMAT_T = AbstractMatrix
57
const SPARSE_T = AbstractSparseMatrix # subset of ADJMAT_T
68

7-
const AVecI = AbstractVector{<:Integer}
9+
# Graph indicator arrays may likewise be traced arrays whose eltype wraps integers.
10+
const AVecI = AbstractVector
811

912
# All concrete graph types should be subtypes of AbstractGNNGraph{T}.
1013
# GNNGraph and GNNHeteroGraph are the two concrete types.

‎GNNGraphs/src/gatherscatter.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ _scatter(aggr, src::Dict, idx, n) = Dict([k => _scatter(aggr, v, idx, n) for (k,
1111

1212
function _scatter(aggr,
1313
src::AbstractArray,
14-
idx::AbstractVector{<:Integer},
14+
idx::AbstractVector,
1515
n::Integer)
1616
dstsize = (size(src)[1:(end - 1)]..., n)
1717
return NNlib.scatter(aggr, src, idx; dstsize)

‎GNNLux/test/reactant/runtests.jl‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,30 @@ end
4747
@test size(y_d) == (2, 6)
4848
@test Array(y_d) ≈ y_cpu rtol = 1.0f-4 atol = 1.0f-5
4949
end
50+
51+
@testset "Reactant GCNConv keeps device graph indices dynamic" begin
52+
rng = Xoshiro(42)
53+
model = GCNConv(3 => 2, relu)
54+
ps, st = Lux.setup(rng, model)
55+
g1 = test_graph()
56+
g2 = GNNGraph(
57+
([1, 1, 1, 2, 2, 3], [4, 5, 6, 5, 6, 6]);
58+
num_nodes = 6,
59+
)
60+
x = randn(rng, Float32, 3, 6)
61+
f(g, x, ps, st) = first(model(g, x, ps, st))
62+
63+
dev = reactant_device()
64+
ps_d, st_d, x_d, g1_d, g2_d = (ps, st, x, g1, g2) |> dev
65+
compiled = @compile f(g1_d, x_d, ps_d, st_d)
66+
67+
y1_cpu = f(g1, x, ps, st)
68+
y2_cpu = f(g2, x, ps, st)
69+
y1_compiled = Array(compiled(g1_d, x_d, ps_d, st_d))
70+
y2_compiled = Array(compiled(g2_d, x_d, ps_d, st_d))
71+
72+
@test size(y1_compiled) == (2, 6)
73+
@test y1_compiled ≈ y1_cpu rtol = 1.0f-4 atol = 1.0f-5
74+
@test size(y2_compiled) == (2, 6)
75+
@test y2_compiled ≈ y2_cpu rtol = 1.0f-4 atol = 1.0f-5
76+
end

‎GNNlib/Project.toml‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "GNNlib"
22
uuid = "a6a84749-d869-43f8-aacc-be26a1996e48"
33
authors = ["Carlo Lucibello and contributors"]
4-
version = "1.4.1"
4+
version = "1.5.0-DEV"
55

66
[workspace]
77
projects = ["test", "docs", "../GNNGraphs"]
@@ -21,24 +21,27 @@ AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
2121
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
2222
Metal = "dde4c033-4e86-420c-a63e-0dd931031962"
2323
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
24+
Reactant = "3c362404-f566-11ee-1572-e11a4b42c853"
2425

2526
[extensions]
2627
GNNlibAMDGPUExt = "AMDGPU"
2728
GNNlibCUDAExt = "CUDA"
2829
GNNlibMetalExt = "Metal"
2930
GNNlibMooncakeExt = "Mooncake"
31+
GNNlibReactantExt = "Reactant"
3032

3133
[compat]
3234
AMDGPU = "1, 2.7"
3335
CUDA = "5, 6"
3436
ChainRulesCore = "1.24"
3537
DataStructures = "0.18, 0.19"
36-
GNNGraphs = "1.6"
38+
GNNGraphs = "1.7"
3739
LinearAlgebra = "1"
3840
Metal = "1.0"
3941
MLUtils = "0.4"
4042
Mooncake = "0.5"
4143
NNlib = "0.9"
4244
Random = "1"
45+
Reactant = "0.2"
4346
Statistics = "1"
4447
julia = "1.10"

‎GNNlib/ext/GNNlibReactantExt.jl‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module GNNlibReactantExt
2+
3+
using Reactant: AnyTracedRMatrix
4+
using GNNlib: GNNlib, propagate, copy_xj, e_mul_xj, w_mul_xj
5+
using GNNGraphs: GNNGraph, COO_T, get_edge_weight
6+
7+
# The generic fast paths construct a sparse adjacency matrix, which Reactant
8+
# cannot trace when its indices are dynamic. Gather/scatter keeps graph indices
9+
# as inputs to the compiled program.
10+
function GNNlib.propagate(::typeof(copy_xj), g::GNNGraph{<:COO_T}, ::typeof(+),
11+
xi, xj::AnyTracedRMatrix, e)
12+
propagate((xi, xj, e) -> copy_xj(xi, xj, e), g, +, xi, xj, e)
13+
end
14+
15+
function GNNlib.propagate(::typeof(e_mul_xj), g::GNNGraph{<:COO_T}, ::typeof(+),
16+
xi, xj::AnyTracedRMatrix, e::AbstractVector)
17+
propagate((xi, xj, e) -> e_mul_xj(xi, xj, e), g, +, xi, xj, e)
18+
end
19+
20+
function GNNlib.propagate(::typeof(w_mul_xj), g::GNNGraph{<:COO_T}, ::typeof(+),
21+
xi, xj::AnyTracedRMatrix, e::Nothing)
22+
propagate((xi, xj, e) -> w_mul_xj(xi, xj, e), g, +, xi, xj, get_edge_weight(g))
23+
end
24+
25+
end # module

‎GNNlib/src/GNNlib.jl‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using ChainRulesCore: @non_differentiable, ignore_derivatives
1010
using GNNGraphs
1111
using .GNNGraphs: COO_T, ADJMAT_T, SPARSE_T,
1212
check_num_nodes, check_num_edges,
13-
_to_coo_graph,
13+
_to_coo_graph, _scatter,
1414
EType, NType # for heteroconvs
1515

1616
include("utils.jl")
@@ -77,4 +77,3 @@ export global_pool,
7777
# include("layers/heteroconv.jl") # no functional part at the moment
7878

7979
end #module
80-

0 commit comments

Comments
 (0)