-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathquery.jl
More file actions
334 lines (288 loc) · 11.5 KB
/
query.jl
File metadata and controls
334 lines (288 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
@testitem "is_bidirected" setup=[GraphsTestModule] begin
using .GraphsTestModule
for GRAPH_T in GRAPH_TYPES
g = rand_graph(10, 20, bidirected = true, graph_type = GRAPH_T)
@test is_bidirected(g)
g = rand_graph(10, 20, bidirected = false, graph_type = GRAPH_T)
@test !is_bidirected(g)
end
end
@testitem "has_multi_edges" setup=[GraphsTestModule] begin
using .GraphsTestModule
for GRAPH_T in GRAPH_TYPES
if GRAPH_T == :coo
s = [1, 1, 2, 3]
t = [2, 2, 2, 4]
g = GNNGraph(s, t, graph_type = GRAPH_T)
@test has_multi_edges(g)
s = [1, 2, 2, 3]
t = [2, 1, 2, 4]
g = GNNGraph(s, t, graph_type = GRAPH_T)
@test !has_multi_edges(g)
end
end
end
@testitem "edges" setup=[GraphsTestModule] begin
using .GraphsTestModule
for GRAPH_T in GRAPH_TYPES
g = rand_graph(4, 10, graph_type = GRAPH_T)
@test edgetype(g) <: Graphs.Edge
for e in edges(g)
@test e isa Graphs.Edge
end
end
end
@testitem "has_isolated_nodes" setup=[GraphsTestModule] begin
using .GraphsTestModule
for GRAPH_T in GRAPH_TYPES
s = [1, 2, 3]
t = [2, 3, 2]
g = GNNGraph(s, t, graph_type = GRAPH_T)
@test has_isolated_nodes(g) == false
@test has_isolated_nodes(g, dir = :in) == true
end
end
@testitem "has_self_loops" setup=[GraphsTestModule] begin
using .GraphsTestModule
for GRAPH_T in GRAPH_TYPES
s = [1, 1, 2, 3]
t = [2, 2, 2, 4]
g = GNNGraph(s, t, graph_type = GRAPH_T)
@test has_self_loops(g)
s = [1, 1, 2, 3]
t = [2, 2, 3, 4]
g = GNNGraph(s, t, graph_type = GRAPH_T)
@test !has_self_loops(g)
end
end
@testitem "degree" setup=[GraphsTestModule] begin
using .GraphsTestModule
using SparseArrays: AbstractSparseMatrix
for GRAPH_T in GRAPH_TYPES
@testset "unweighted" begin
s = [1, 1, 2, 3]
t = [2, 2, 2, 4]
g = GNNGraph(s, t, graph_type = GRAPH_T)
@test degree(g) isa Vector{Int}
@test degree(g) == degree(g; dir = :out) == [2, 1, 1, 0] # default is outdegree
@test degree(g; dir = :in) == [0, 3, 0, 1]
@test degree(g; dir = :both) == [2, 4, 1, 1]
@test eltype(degree(g, Float32)) == Float32
end
@testset "weighted" begin
# weighted degree
s = [1, 1, 2, 3]
t = [2, 2, 2, 4]
eweight = Float32[0.1, 2.1, 1.2, 1]
g = GNNGraph((s, t, eweight), graph_type = GRAPH_T)
@test degree(g) ≈ [2.2, 1.2, 1.0, 0.0]
d = degree(g, edge_weight = false)
if GRAPH_T == :coo
@test d == [2, 1, 1, 0]
else
# Adjacency matrix representation cannot disambiguate multiple edges
# and edge weights
@test d == [1, 1, 1, 0]
end
@test eltype(d) <: Integer
@test degree(g, edge_weight = 2 * eweight) ≈ [4.4, 2.4, 2.0, 0.0] broken = (GRAPH_T != :coo)
@testset "gradient" begin
gw = gradient(eweight) do w
g = GNNGraph((s, t, w), graph_type = GRAPH_T)
sum(degree(g, edge_weight = false))
end[1]
@test gw === nothing
gw = gradient(eweight) do w
g = GNNGraph((s, t, w), graph_type = GRAPH_T)
sum(degree(g, edge_weight = true))
end[1]
@test gw isa AbstractVector{Float32}
@test gw isa Vector{Float32} broken = (GRAPH_T == :sparse)
@test gw ≈ ones(Float32, length(gw))
gw = gradient(eweight) do w
g = GNNGraph((s, t, w), graph_type = GRAPH_T)
sum(degree(g, dir=:both, edge_weight=true))
end[1]
@test gw isa AbstractVector{Float32}
@test gw isa Vector{Float32} broken = (GRAPH_T == :sparse)
@test gw ≈ 2 * ones(Float32, length(gw))
grad = gradient(g) do g
sum(degree(g, edge_weight=false))
end[1]
@test grad === nothing
grad = gradient(g) do g
sum(degree(g, edge_weight=true))
end[1]
if GRAPH_T == :coo
@test grad.graph[3] isa Vector{Float32}
@test grad.graph[3] ≈ ones(Float32, length(gw))
else
if GRAPH_T == :sparse
@test grad.graph isa AbstractSparseMatrix{Float32}
end
@test grad.graph isa AbstractMatrix{Float32}
@test grad.graph ≈ [0.0 1.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 0.0 1.0
0.0 0.0 0.0 0.0]
end
@testset "directed, degree dir=$dir" for dir in [:in, :out, :both]
g = rand_graph(10, 30, bidirected=false)
w = rand(Float32, 30)
s, t = edge_index(g)
grad = gradient(w) do w
g = GNNGraph((s, t, w), graph_type = GRAPH_T)
sum(tanh.(degree(g; dir, edge_weight=true)))
end[1]
ngrad = ngradient(w) do w
g = GNNGraph((s, t, w), graph_type = GRAPH_T)
sum(tanh.(degree(g; dir, edge_weight=true)))
end[1]
@test grad ≈ ngrad
end
end
end
end
end
@testitem "degree GPU" setup=[GraphsTestModule] tags=[:gpu] begin
using .GraphsTestModule
dev = gpu_device(force=true)
for GRAPH_T in GRAPH_TYPES
dev isa MetalDevice && continue # TODO: remove when gather/scatter is implemented for Metal
@testset "unweighted" begin
s = [1, 1, 2, 3]
t = [2, 2, 2, 4]
g = GNNGraph(s, t, graph_type = GRAPH_T)
g_gpu = g |> dev
d = degree(g)
d_gpu = degree(g_gpu)
@test d_gpu isa AbstractVector{Int}
@test get_device(d_gpu) isa AbstractGPUDevice
@test Array(d_gpu) == d
end
@testset "weighted" begin
# weighted degree
s = [1, 1, 2, 3]
t = [2, 2, 2, 4]
eweight = Float32[0.1, 2.1, 1.2, 1]
g = GNNGraph((s, t, eweight), graph_type = GRAPH_T)
g_gpu = g |> dev
d = degree(g)
d_gpu = degree(g_gpu)
@test d_gpu isa AbstractArray{Float32}
@test get_device(d_gpu) isa AbstractGPUDevice
@test Array(d_gpu) ≈ d
end
end
end
@testitem "heterognn, degree" begin
g = GNNHeteroGraph((:A, :to, :B) => ([1,1,2,3], [7,13,5,7]))
@test degree(g, (:A, :to, :B), dir = :out) == [2, 1, 1]
@test degree(g, (:A, :to, :B), dir = :in) == [0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 1]
@test degree(g, (:A, :to, :B)) == [2, 1, 1]
end
@testitem "laplacian_matrix" setup=[GraphsTestModule] begin
using .GraphsTestModule
for GRAPH_T in GRAPH_TYPES
g = rand_graph(10, 30, graph_type = GRAPH_T)
A = adjacency_matrix(g)
D = Diagonal(vec(sum(A, dims = 2)))
L = laplacian_matrix(g)
@test eltype(L) == eltype(g)
@test L ≈ D - A
end
end
@testitem "laplacian_lambda_max" setup=[GraphsTestModule] begin
using .GraphsTestModule
s = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
t = [2, 3, 4, 5, 1, 5, 1, 2, 3, 4]
g = GNNGraph(s, t)
@test laplacian_lambda_max(g) ≈ Float32(1.809017)
data1 = [g for i in 1:5]
gall1 = MLUtils.batch(data1)
@test laplacian_lambda_max(gall1) ≈ [Float32(1.809017) for i in 1:5]
data2 = [rand_graph(10, 20) for i in 1:3]
gall2 = MLUtils.batch(data2)
@test length(laplacian_lambda_max(gall2, add_self_loops=true)) == 3
end
@testitem "adjacency_matrix" setup=[GraphsTestModule] begin
using .GraphsTestModule
for GRAPH_T in GRAPH_TYPES
a = sprand(5, 5, 0.5)
abin = map(x -> x > 0 ? 1 : 0, a)
g = GNNGraph(a, graph_type = GRAPH_T)
A = adjacency_matrix(g, Float32)
@test A ≈ a
@test eltype(A) == Float32
Abin = adjacency_matrix(g, Float32, weighted = false)
@test Abin ≈ abin
@test eltype(Abin) == Float32
@testset "gradient" begin
s = [1, 2, 3]
t = [2, 3, 1]
w = [0.1, 0.1, 0.2]
gw = gradient(w) do w
g = GNNGraph(s, t, w, graph_type = GRAPH_T)
A = adjacency_matrix(g, weighted = false)
sum(A)
end[1]
@test gw === nothing
gw = gradient(w) do w
g = GNNGraph(s, t, w, graph_type = GRAPH_T)
A = adjacency_matrix(g, weighted = true)
sum(A)
end[1]
@test gw == [1, 1, 1]
end
@testset "fmt" begin
Asparse = adjacency_matrix(g, weighted = false, fmt = :sparse)
@test Asparse isa AbstractSparseMatrix
Adense = adjacency_matrix(g, weighted = false, fmt = :dense)
@test Adense isa AbstractMatrix
@test !(Adense isa AbstractSparseMatrix)
@test collect(Asparse) == Adense
end
end
@testset "khop_adj" begin
s = [1, 2, 3]
t = [2, 3, 1]
w = [0.1, 0.1, 0.2]
g = GNNGraph(s, t, w)
@test khop_adj(g, 2) == adjacency_matrix(g) * adjacency_matrix(g)
@test khop_adj(g, 2, Int8; weighted = false) == sparse([0 0 1; 1 0 0; 0 1 0])
@test khop_adj(g, 2, Int8; dir = in, weighted = false) ==
sparse([0 0 1; 1 0 0; 0 1 0]')
@test khop_adj(g, 1) == adjacency_matrix(g)
@test eltype(khop_adj(g, 4)) == Float64
@test eltype(khop_adj(g, 10, Float32)) == Float32
end
end
@testitem "HeteroGraph" setup=[GraphsTestModule] begin
using .GraphsTestModule
@testset "graph_indicator" begin
gs = [rand_heterograph(Dict(:user => 10, :movie => 20, :actor => 30),
Dict((:user,:like,:movie) => 10,
(:actor,:rate,:movie)=>20)) for _ in 1:3]
g = MLUtils.batch(gs)
@test graph_indicator(g) == Dict(:user => [repeat([1], 10); repeat([2], 10); repeat([3], 10)],
:movie => [repeat([1], 20); repeat([2], 20); repeat([3], 20)],
:actor => [repeat([1], 30); repeat([2], 30); repeat([3], 30)])
@test graph_indicator(g, :movie) == [repeat([1], 20); repeat([2], 20); repeat([3], 20)]
end
end
@testitem "get_graph_type" setup=[GraphsTestModule] begin
using .GraphsTestModule
for GRAPH_T in GRAPH_TYPES
g = rand_graph(10, 20, graph_type = GRAPH_T)
@test get_graph_type(g) == GRAPH_T
gsparse = GNNGraph(g, graph_type=:sparse)
@test get_graph_type(gsparse) == :sparse
@test gsparse.graph isa SparseMatrixCSC
gcoo = GNNGraph(g, graph_type=:coo)
@test get_graph_type(gcoo) == :coo
@test gcoo.graph[1:2] isa Tuple{Vector{Int}, Vector{Int}}
gdense = GNNGraph(g, graph_type=:dense)
@test get_graph_type(gdense) == :dense
@test gdense.graph isa Matrix{Int}
end
end