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
38 changes: 38 additions & 0 deletions .github/workflows/test_GNNLux_Reactant.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: GNNLux Reactant

on:
pull_request:
branches:
- master
push:
branches:
- master

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/master' }}

permissions:
contents: read

jobs:
test:
name: Julia 1.11 - Reactant CPU
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: julia-actions/setup-julia@v3
with:
version: '1.11'
- uses: julia-actions/cache@v3
- name: Install Reactant test dependencies
run: julia --project=GNNLux/test/reactant -e 'using Pkg; Pkg.instantiate()'
- name: Run optional Reactant integration tests
run: julia --code-coverage=all --project=GNNLux/test/reactant GNNLux/test/reactant/runtests.jl
- uses: julia-actions/julia-processcoverage@v1
with:
directories: GNNLux/src,GNNlib/src,GNNlib/ext,GNNGraphs/src,GNNGraphs/ext
- uses: codecov/codecov-action@v7
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: lcov.info
5 changes: 4 additions & 1 deletion GNNGraphs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "GNNGraphs"
uuid = "aed8fd31-079b-4b5a-b342-a13352159b8c"
version = "1.6.1-DEV"
version = "1.7.0-DEV"
authors = ["Carlo Lucibello and contributors"]

[workspace]
Expand All @@ -25,12 +25,14 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
EnzymeCore = "f151be2c-9106-41f4-ab19-57ee4f262869"
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
Reactant = "3c362404-f566-11ee-1572-e11a4b42c853"
SimpleWeightedGraphs = "47aef6b3-ad0c-573a-a1e2-d07658019622"

[extensions]
GNNGraphsCUDAExt = "CUDA"
GNNGraphsEnzymeCoreExt = "EnzymeCore"
GNNGraphsMooncakeExt = "Mooncake"
GNNGraphsReactantExt = "Reactant"
GNNGraphsSimpleWeightedGraphsExt = "SimpleWeightedGraphs"

[compat]
Expand All @@ -46,6 +48,7 @@ MLUtils = "0.4"
Mooncake = "0.5.24"
NNlib = "0.9"
NearestNeighbors = "0.4"
Reactant = "0.2"
Random = "1"
SimpleWeightedGraphs = "1.4.0"
SparseArrays = "1"
Expand Down
10 changes: 10 additions & 0 deletions GNNGraphs/ext/GNNGraphsReactantExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module GNNGraphsReactantExt

using GNNGraphs
using Reactant: TracedRNumber

GNNGraphs._check_index_eltype(::Type{<:TracedRNumber{T}}) where {T <: Integer} = true

GNNGraphs._graph_scalar_eltype(::Type{TracedRNumber{T}}) where {T} = T

end
10 changes: 8 additions & 2 deletions GNNGraphs/src/abstracttypes.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@

const COO_T = Tuple{T, T, V} where {T <: AbstractVector{<:Integer}, V <: Union{Nothing, AbstractVector}}
# COO index arrays may have non-integer wrapper eltypes while traced by a compiler
# such as Reactant. `_check_index_eltype` validates that the wrapped type is integral.
const COO_T = Tuple{T, T, V} where {T <: AbstractVector, V <: Union{Nothing, AbstractVector}}
const ADJLIST_T = AbstractVector{T} where {T <: AbstractVector{<:Integer}}
const ADJMAT_T = AbstractMatrix
const SPARSE_T = AbstractSparseMatrix # subset of ADJMAT_T

const AVecI = AbstractVector{<:Integer}
# Graph indicator arrays may likewise be traced arrays whose eltype wraps integers.
const AVecI = AbstractVector

_check_index_eltype(::Type{T}) where {T} =
T <: Integer || throw(ArgumentError("graph indices must be integers, got eltype $T"))

# All concrete graph types should be subtypes of AbstractGNNGraph{T}.
# GNNGraph and GNNHeteroGraph are the two concrete types.
Expand Down
11 changes: 11 additions & 0 deletions GNNGraphs/src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ function to_coo(coo::COO_T; dir = :out, num_nodes = nothing, weighted = true,
hetero = false)
s, t, val = coo

_check_index_eltype(eltype(s))
_check_index_eltype(eltype(t))

if isnothing(num_nodes)
ns = maximum(s)
nt = maximum(t)
Expand Down Expand Up @@ -146,6 +149,7 @@ function to_dense(A::ADJMAT_T, T = nothing; dir = :out, num_nodes = nothing,
weighted = true)
@assert dir ∈ [:out, :in]
T = T === nothing ? eltype(A) : T
T = _graph_scalar_eltype(T)
num_nodes = size(A, 1)
@assert num_nodes == size(A, 2)
# @assert all(x -> (x == 1) || (x == 0), A)
Expand Down Expand Up @@ -186,10 +190,13 @@ function to_dense(coo::COO_T, T = nothing; dir = :out, num_nodes = nothing, weig
# `dir` will be ignored since the input `coo` is always in source -> target format.
# The output will always be a adjmat in :out format (e.g. A[i,j] denotes from i to j)
s, t, val = coo
_check_index_eltype(eltype(s))
_check_index_eltype(eltype(t))
n::Int = isnothing(num_nodes) ? max(maximum(s), maximum(t)) : num_nodes
if T === nothing
T = isnothing(val) ? eltype(s) : eltype(val)
end
T = _graph_scalar_eltype(T)
if val === nothing || !weighted
val = ones_like(s, T)
end
Expand All @@ -216,6 +223,7 @@ function to_sparse(A::ADJMAT_T, T = nothing; dir = :out, num_nodes = nothing,
num_nodes = size(A, 1)
@assert num_nodes == size(A, 2)
T = T === nothing ? eltype(A) : T
T = _graph_scalar_eltype(T)
num_edges = A isa AbstractSparseMatrix ? nnz(A) : count(!=(0), A)
if dir == :in
A = A'
Expand All @@ -241,7 +249,10 @@ end
function to_sparse(coo::COO_T, T = nothing; dir = :out, num_nodes = nothing,
weighted = true)
s, t, eweight = coo
_check_index_eltype(eltype(s))
_check_index_eltype(eltype(t))
T = T === nothing ? (eweight === nothing ? eltype(s) : eltype(eweight)) : T
T = _graph_scalar_eltype(T)

if eweight === nothing || !weighted
eweight = fill!(similar(s, T), 1)
Expand Down
2 changes: 1 addition & 1 deletion GNNGraphs/src/gatherscatter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ _scatter(aggr, src::Dict, idx, n) = Dict([k => _scatter(aggr, v, idx, n) for (k,

function _scatter(aggr,
src::AbstractArray,
idx::AbstractVector{<:Integer},
idx::AbstractVector,
n::Integer)
dstsize = (size(src)[1:(end - 1)]..., n)
return NNlib.scatter(aggr, src, idx; dstsize)
Expand Down
6 changes: 6 additions & 0 deletions GNNGraphs/src/gnngraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ 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 Expand Up @@ -154,6 +157,9 @@ function GNNGraph(data::D;
graph, num_nodes, num_edges = to_sparse(data; num_nodes, dir)
end

if !isnothing(graph_indicator)
_check_index_eltype(eltype(graph_indicator))
end
num_graphs = !isnothing(graph_indicator) ? maximum(graph_indicator) : 1

ndata = normalize_graphdata(ndata, default_name = :x, n = num_nodes)
Expand Down
4 changes: 4 additions & 0 deletions GNNGraphs/src/gnnheterograph/gnnheterograph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ function GNNHeteroGraph(data::EDict;
@assert dir ∈ [:in, :out]
@assert graph_type==:coo "only :coo graph_type is supported for now"

if !isnothing(graph_indicator)
foreach(gi -> _check_index_eltype(eltype(gi)), values(graph_indicator))
end

if num_nodes !== nothing
num_nodes = Dict(num_nodes)
end
Expand Down
2 changes: 2 additions & 0 deletions GNNGraphs/src/gnnheterograph/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ function add_edges(g::GNNHeteroGraph{<:COO_T},
edata = nothing,
num_nodes = Dict{Symbol,Int}())
edge_t, (snew, tnew, wnew) = data
_check_index_eltype(eltype(snew))
_check_index_eltype(eltype(tnew))
@assert length(snew) == length(tnew)
if length(snew) == 0
return g
Expand Down
6 changes: 6 additions & 0 deletions GNNGraphs/src/query.jl
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ function Graphs.degree(g::GNNGraph{<:COO_T}, T::TT = nothing; dir = :out,
else
T
end
T = _graph_scalar_eltype(T)
return _degree((s, t), T, dir, ew, g.num_nodes)
end

Expand All @@ -371,10 +372,15 @@ function Graphs.degree(g::GNNGraph{<:ADJMAT_T}, T::TT = nothing; dir = :out,
T = Nt
end
end
T = _graph_scalar_eltype(T)
A = adjacency_matrix(g)
return _degree(A, T, dir, edge_weight, g.num_nodes)
end

# Backends can specialize this hook when their scalar wrapper type should not be
# used as the element type of ordinary graph-side allocations.
_graph_scalar_eltype(::Type{T}) where {T} = T

function _degree((s, t)::Tuple, T::Type, dir::Symbol, edge_weight::Nothing, num_nodes::Int)
_degree((s, t), T, dir, ones_like(s, T), num_nodes)
end
Expand Down
2 changes: 2 additions & 0 deletions GNNGraphs/src/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ add_edges(g, data::Tuple{<:AbstractVector, <:AbstractVector}; kws...) = add_edge

function add_edges(g::GNNGraph{<:COO_T}, data::COO_T; edata = nothing)
snew, tnew, wnew = data
_check_index_eltype(eltype(snew))
_check_index_eltype(eltype(tnew))
@assert length(snew) == length(tnew)
@assert isnothing(wnew) || length(wnew) == length(snew)
if length(snew) == 0
Expand Down
21 changes: 21 additions & 0 deletions GNNGraphs/test/gnngraph.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
# TODO test that the graph type is preserved
# when constructing a GNNGraph from another

@testitem "Constructor: COO indices must be integers" setup=[GraphsTestModule] begin
@test GNNGraph(([1, 2], [2, 1]); num_nodes = 2).num_edges == 2
@test_throws ArgumentError GNNGraph(([1.0, 2.0], [2.0, 1.0]); num_nodes = 2)
@test_throws ArgumentError GNNGraph(([1.5, 2.0], [2.0, 1.0]); num_nodes = 2)
@test_throws ArgumentError GNNGraph((["1", "2"], ["2", "1"]); num_nodes = 2)
@test_throws ArgumentError GNNGraph(([1.0, 2.0], [2.0, 1.0]); num_nodes = 2, graph_type = :sparse)
@test_throws ArgumentError GNNGraph(([1.0, 2.0], [2.0, 1.0]); num_nodes = 2, graph_type = :dense)

g = GNNGraph(([1, 2], [2, 1]); num_nodes = 2)
@test_throws ArgumentError add_edges(g, [1.0], [2.0])
hg = GNNHeteroGraph((:A, :e, :B) => ([1, 2], [2, 1]))
@test_throws ArgumentError add_edges(hg, (:A, :e, :B), [1.0], [2.0])

@test_throws ArgumentError GNNGraph(
([1, 2], [2, 1]); num_nodes = 2, graph_indicator = [1.0, 1.0]
)
@test_throws ArgumentError GNNGraph(
([1, 2], [2, 1]); num_nodes = 2, graph_indicator = ["1", "1"]
)
end

@testitem "Constructor: adjacency matrix" setup=[GraphsTestModule] begin
using .GraphsTestModule
for GRAPH_T in GRAPH_TYPES
Expand Down
2 changes: 1 addition & 1 deletion GNNLux/test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
GNNLux = {path = ".."}

[compat]
LuxTestUtils = "2.3.2, 3.0"
LuxTestUtils = "3"
NNlib = "0.9.38"
StableRNGs = "1.0.4"
4 changes: 2 additions & 2 deletions GNNLux/test/layers/temporalconv.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testitem "layers/temporalconv" setup=[TestModuleLux] begin
using .TestModuleLux
using LuxCore, NNlib
using LuxTestUtils: test_gradients, AutoTracker, AutoForwardDiff, AutoEnzyme, AutoMooncake
using LuxTestUtils: test_gradients, AutoForwardDiff, AutoEnzyme

rng = StableRNG(1234)
n, m = 10, 40
Expand All @@ -11,7 +11,7 @@
X = randn(rng, Float32, in_dims, timesteps, n) # a whole sequence
tg = TemporalSnapshotsGNNGraph([rand_graph(rng, n, m) for _ in 1:timesteps])
tX = [randn(rng, Float32, in_dims, n) for _ in 1:timesteps]
skipb = [AutoForwardDiff(), AutoEnzyme(), AutoMooncake()]
skipb = [AutoForwardDiff(), AutoEnzyme()]

sig = NNlib.sigmoid_fast
tanhf = NNlib.tanh_fast
Expand Down
17 changes: 17 additions & 0 deletions GNNLux/test/reactant/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[deps]
GNNGraphs = "aed8fd31-079b-4b5a-b342-a13352159b8c"
GNNLux = "e8545f4d-a905-48ac-a8c4-ca114b98986d"
GNNlib = "a6a84749-d869-43f8-aacc-be26a1996e48"
Lux = "b2108857-7c20-44ae-9111-449ecde12c47"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Reactant = "3c362404-f566-11ee-1572-e11a4b42c853"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[sources]
GNNGraphs = {path = "../../../GNNGraphs"}
GNNLux = {path = "../.."}
GNNlib = {path = "../../../GNNlib"}

[compat]
Reactant = "0.2"
julia = "1.11"
76 changes: 76 additions & 0 deletions GNNLux/test/reactant/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Optional integration tests; the regular GNNLux test project does not depend on
# Reactant. From the repository root, instantiate and run with:
# julia --project=GNNLux/test/reactant -e 'using Pkg; Pkg.instantiate()'
# julia --project=GNNLux/test/reactant GNNLux/test/reactant/runtests.jl

using GNNGraphs
using GNNLux
using Lux
using Random
using Reactant
using Test

degree_for_features(g, x) = GNNGraphs.degree(g, eltype(x); dir = :in, edge_weight = false)

function test_graph()
return GNNGraph(
([1, 2, 3, 4, 5, 6], [2, 3, 1, 5, 6, 4]);
num_nodes = 6,
)
end

@testset "Reactant degree uses feature numeric type" begin
g = test_graph()
x = randn(Xoshiro(42), Float32, 3, 6)
x_d = x |> reactant_device()

compiled_degree = @compile degree_for_features(g, x_d)
degree_d = compiled_degree(g, x_d)

@test Array(degree_d) == ones(Float32, 6)
end

@testset "Reactant GCNConv compiles, executes, and matches CPU" begin
rng = Xoshiro(42)
model = GCNConv(3 => 2, relu)
ps, st = Lux.setup(rng, model)
g = test_graph()
x = randn(rng, Float32, 3, 6)
y_cpu, _ = model(g, x, ps, st)

dev = reactant_device()
ps_d, st_d = (ps, st) |> dev
x_d = x |> dev
compiled = @compile model(g, x_d, ps_d, st_d)
y_d, _ = compiled(g, x_d, ps_d, st_d)

@test size(y_d) == (2, 6)
@test Array(y_d) ≈ y_cpu rtol = 1.0f-4 atol = 1.0f-5
end

@testset "Reactant GCNConv keeps device graph indices dynamic" begin
rng = Xoshiro(42)
model = GCNConv(3 => 2, relu)
ps, st = Lux.setup(rng, model)
g1 = test_graph()
g2 = GNNGraph(
([1, 1, 1, 2, 2, 3], [4, 5, 6, 5, 6, 6]);
num_nodes = 6,
)
x = randn(rng, Float32, 3, 6)
f(g, x, ps, st) = first(model(g, x, ps, st))

dev = reactant_device()
ps_d, st_d, x_d, g1_d, g2_d = (ps, st, x, g1, g2) |> dev
compiled = @compile f(g1_d, x_d, ps_d, st_d)

y1_cpu = f(g1, x, ps, st)
y2_cpu = f(g2, x, ps, st)
y1_compiled = Array(compiled(g1_d, x_d, ps_d, st_d))
y2_compiled = Array(compiled(g2_d, x_d, ps_d, st_d))

@test size(y1_compiled) == (2, 6)
@test y1_compiled ≈ y1_cpu rtol = 1.0f-4 atol = 1.0f-5
@test size(y2_compiled) == (2, 6)
@test y2_compiled ≈ y2_cpu rtol = 1.0f-4 atol = 1.0f-5
end
4 changes: 2 additions & 2 deletions GNNLux/test/test_module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ using Reexport: @reexport
@reexport using StableRNGs
@reexport using Random, Statistics

using LuxTestUtils: test_gradients, AutoTracker, AutoForwardDiff, AutoEnzyme, AutoMooncake
using LuxTestUtils: test_gradients, AutoForwardDiff, AutoEnzyme

export test_lux_layer

Expand Down Expand Up @@ -71,7 +71,7 @@ function test_lux_layer(rng::AbstractRNG, l, g::GNNGraph, x;
else
loss = (x, ps) -> mean(first(l(g, x, ps, st)))
end
test_gradients(loss, x, ps; atol, rtol, skip_backends=[AutoForwardDiff(), AutoEnzyme(), AutoMooncake()])
test_gradients(loss, x, ps; atol, rtol, skip_backends=[AutoForwardDiff(), AutoEnzyme()])
end

end
Loading