Skip to content

Commit 72cfc86

Browse files
committed
Validate traced graph indices without relaxing CPU checks
1 parent 00dbc4c commit 72cfc86

9 files changed

Lines changed: 68 additions & 6 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: GNNLux Reactant
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
push:
8+
branches:
9+
- master
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.ref }}
13+
cancel-in-progress: ${{ github.ref != 'refs/heads/master' }}
14+
15+
permissions:
16+
contents: read
17+
18+
jobs:
19+
test:
20+
name: Julia 1.11 - Reactant CPU
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v7
24+
- uses: julia-actions/setup-julia@v3
25+
with:
26+
version: '1.11'
27+
- uses: julia-actions/cache@v3
28+
- name: Install Reactant test dependencies
29+
run: julia --project=GNNLux/test/reactant -e 'using Pkg; Pkg.instantiate()'
30+
- name: Run optional Reactant integration tests
31+
run: julia --project=GNNLux/test/reactant GNNLux/test/reactant/runtests.jl
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module GNNGraphsReactantExt
22

33
using GNNGraphs
4-
using Reactant
4+
using Reactant: TracedRNumber
55

6-
GNNGraphs._graph_scalar_eltype(::Type{Reactant.TracedRNumber{T}}) where {T} =
7-
Reactant.unwrapped_eltype(Reactant.TracedRNumber{T})
6+
GNNGraphs._check_index_eltype(::Type{<:TracedRNumber{T}}) where {T <: Integer} = true
7+
8+
GNNGraphs._graph_scalar_eltype(::Type{TracedRNumber{T}}) where {T} = T
89

910
end

‎GNNGraphs/src/abstracttypes.jl‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
# COO index arrays may have non-integer wrapper eltypes while traced by a compiler
3-
# such as Reactant. Constructors still validate ordinary graph indices.
3+
# such as Reactant. `_check_index_eltype` validates that the wrapped type is integral.
44
const COO_T = Tuple{T, T, V} where {T <: AbstractVector, V <: Union{Nothing, AbstractVector}}
55
const ADJLIST_T = AbstractVector{T} where {T <: AbstractVector{<:Integer}}
66
const ADJMAT_T = AbstractMatrix
@@ -9,6 +9,9 @@ const SPARSE_T = AbstractSparseMatrix # subset of ADJMAT_T
99
# Graph indicator arrays may likewise be traced arrays whose eltype wraps integers.
1010
const AVecI = AbstractVector
1111

12+
_check_index_eltype(::Type{T}) where {T} =
13+
T <: Integer || throw(ArgumentError("graph indices must be integers, got eltype $T"))
14+
1215
# All concrete graph types should be subtypes of AbstractGNNGraph{T}.
1316
# GNNGraph and GNNHeteroGraph are the two concrete types.
1417
abstract type AbstractGNNGraph{T} <: AbstractGraph{Int} end

‎GNNGraphs/src/convert.jl‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ function to_coo(coo::COO_T; dir = :out, num_nodes = nothing, weighted = true,
3030
hetero = false)
3131
s, t, val = coo
3232

33+
_check_index_eltype(eltype(s))
34+
_check_index_eltype(eltype(t))
35+
3336
if isnothing(num_nodes)
3437
ns = maximum(s)
3538
nt = maximum(t)

‎GNNGraphs/src/gnngraph.jl‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ function GNNGraph(graph::T,
125125
ndata::DataStore,
126126
edata::DataStore,
127127
gdata::DataStore) where {T <: Union{COO_T, ADJMAT_T}}
128+
if !isnothing(graph_indicator)
129+
_check_index_eltype(eltype(graph_indicator))
130+
end
128131
return GNNGraph{T}(graph, num_nodes, num_edges, num_graphs,
129132
graph_indicator, ndata, edata, gdata, false)
130133
end
@@ -154,6 +157,9 @@ function GNNGraph(data::D;
154157
graph, num_nodes, num_edges = to_sparse(data; num_nodes, dir)
155158
end
156159

160+
if !isnothing(graph_indicator)
161+
_check_index_eltype(eltype(graph_indicator))
162+
end
157163
num_graphs = !isnothing(graph_indicator) ? maximum(graph_indicator) : 1
158164

159165
ndata = normalize_graphdata(ndata, default_name = :x, n = num_nodes)

‎GNNGraphs/src/gnnheterograph/gnnheterograph.jl‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ function GNNHeteroGraph(data::EDict;
117117
@assert dir ∈ [:in, :out]
118118
@assert graph_type==:coo "only :coo graph_type is supported for now"
119119

120+
if !isnothing(graph_indicator)
121+
foreach(gi -> _check_index_eltype(eltype(gi)), values(graph_indicator))
122+
end
123+
120124
if num_nodes !== nothing
121125
num_nodes = Dict(num_nodes)
122126
end

‎GNNGraphs/test/gnngraph.jl‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
# TODO test that the graph type is preserved
22
# when constructing a GNNGraph from another
33

4+
@testitem "Constructor: COO indices must be integers" setup=[GraphsTestModule] begin
5+
@test GNNGraph(([1, 2], [2, 1]); num_nodes = 2).num_edges == 2
6+
@test_throws ArgumentError GNNGraph(([1.0, 2.0], [2.0, 1.0]); num_nodes = 2)
7+
@test_throws ArgumentError GNNGraph(([1.5, 2.0], [2.0, 1.0]); num_nodes = 2)
8+
@test_throws ArgumentError GNNGraph((["1", "2"], ["2", "1"]); num_nodes = 2)
9+
10+
@test_throws ArgumentError GNNGraph(
11+
([1, 2], [2, 1]); num_nodes = 2, graph_indicator = [1.0, 1.0]
12+
)
13+
@test_throws ArgumentError GNNGraph(
14+
([1, 2], [2, 1]); num_nodes = 2, graph_indicator = ["1", "1"]
15+
)
16+
end
17+
418
@testitem "Constructor: adjacency matrix" setup=[GraphsTestModule] begin
519
using .GraphsTestModule
620
for GRAPH_T in GRAPH_TYPES

‎GNNLux/test/reactant/Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ GNNlib = {path = "../../../GNNlib"}
1414

1515
[compat]
1616
Reactant = "0.2"
17-
julia = "1.10"
17+
julia = "1.11"

‎GNNlib/src/GNNlib.jl‎

Lines changed: 1 addition & 1 deletion
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, _scatter,
13+
_to_coo_graph,
1414
EType, NType # for heteroconvs
1515

1616
include("utils.jl")

0 commit comments

Comments
 (0)