-
Notifications
You must be signed in to change notification settings - Fork 47
/
utils.jl
230 lines (190 loc) · 6.83 KB
/
utils.jl
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
function check_num_nodes(g::GNNGraph, x::AbstractArray)
@assert g.num_nodes == size(x, ndims(x))
end
function check_num_edges(g::GNNGraph, e::AbstractArray)
@assert g.num_edges == size(e, ndims(e))
end
sort_edge_index(eindex::Tuple) = sort_edge_index(eindex...)
function sort_edge_index(u, v)
uv = collect(zip(u, v))
p = sortperm(uv) # isless lexicographically defined for tuples
return u[p], v[p]
end
cat_features(x1::Nothing, x2::Nothing) = nothing
cat_features(x1::AbstractArray, x2::AbstractArray) = cat(x1, x2, dims=ndims(x1))
cat_features(x1::Union{Number, AbstractVector}, x2::Union{Number, AbstractVector}) =
cat(x1, x2, dims=1)
function cat_features(x1::NamedTuple, x2::NamedTuple)
sort(collect(keys(x1))) == sort(collect(keys(x2))) ||
@error "cannot concatenate feature data with different keys"
NamedTuple(k => cat_features(getfield(x1,k), getfield(x2,k)) for k in keys(x1))
end
# Turns generic type into named tuple
normalize_graphdata(data::Nothing; kws...) = NamedTuple()
normalize_graphdata(data; default_name::Symbol, kws...) =
normalize_graphdata(NamedTuple{(default_name,)}((data,)); default_name, kws...)
function normalize_graphdata(data::NamedTuple; default_name, n, duplicate_if_needed=false)
# This had to workaround two Zygote bugs with NamedTuples
# https://github.com/FluxML/Zygote.jl/issues/1071
# https://github.com/FluxML/Zygote.jl/issues/1072
if n == 1
# If last array dimension is not 1, add a new dimension.
# This is mostly usefule to reshape globale feature vectors
# of size D to Dx1 matrices.
function unsqz(v)
if v isa AbstractArray && size(v)[end] != 1
v = reshape(v, size(v)..., 1)
end
v
end
data = NamedTuple{keys(data)}(unsqz.(values(data)))
end
sz = map(x -> x isa AbstractArray ? size(x)[end] : 0, data)
if duplicate_if_needed
# Used to copy edge features on reverse edges
@assert all(s -> s == 0 || s == n || s == n÷2, sz)
function duplicate(v)
if v isa AbstractArray && size(v)[end] == n÷2
v = cat(v, v, dims=ndims(v))
end
v
end
data = NamedTuple{keys(data)}(duplicate.(values(data)))
else
@assert all(s -> s == 0 || s == n, sz)
end
return data
end
ones_like(x::AbstractArray, T=eltype(x), sz=size(x)) = fill!(similar(x, T, sz), 1)
ones_like(x::SparseMatrixCSC, T=eltype(x), sz=size(x)) = ones(T, sz)
ones_like(x::CUMAT_T, T=eltype(x), sz=size(x)) = CUDA.ones(T, sz)
ofeltype(x, y) = convert(float(eltype(x)), y)
# Considers the src a zero dimensional object.
# Useful for implementing `StatsBase.counts`, `degree`, etc...
# function NNlib.scatter!(op, dst::AbstractArray, src::Number, idx::AbstractArray)
# for k in CartesianIndices(idx)
# # dst_v = NNlib._view(dst, idx[k])
# # dst_v .= (op).(dst_v, src)
# dst[idx[k]] .= (op).(dst[idx[k]], src)
# end
# dst
# end
# 10 time faster than the generic version above.
# All the speedup comes from not broadcasting `op`, i dunno why.
function NNlib.scatter!(op, dst::AbstractVector, src::Number, idx::AbstractVector{<:Integer})
for i in idx
dst[i] = op(dst[i], src)
end
end
# NNlib._view(X, k) = view(X, k...)
# NNlib._view(X, k::Union{Integer, CartesianIndex}) = view(X, k)
# Considers src as a zero dimensional object to be scattered
# function NNlib.scatter(op,
# src::Tsrc,
# idx::AbstractArray{Tidx,Nidx};
# init = nothing, dstsize = nothing) where {Tsrc<:Number,Tidx,Nidx}
# dstsz = isnothing(dstsize) ? maximum_dims(idx) : dstsize
# dst = similar(src, Tsrc, dstsz)
# xinit = isnothing(init) ? scatter_empty(op, Tsrc) : init
# fill!(dst, xinit)
# scatter!(op, dst, src, idx)
# end
function scatter_scalar_kernel!(op, dst, src, idx)
index = threadIdx().x + (blockIdx().x - 1) * blockDim().x
@inbounds if index <= length(idx)
CUDA.@atomic dst[idx[index]...] = op(dst[idx[index]...], src)
end
return nothing
end
function NNlib.scatter!(op, dst::AnyCuArray, src::Number, idx::AnyCuArray)
max_idx = length(idx)
args = op, dst, src, idx
kernel = @cuda launch=false scatter_scalar_kernel!(args...)
config = launch_configuration(kernel.fun; max_threads=256)
threads = min(max_idx, config.threads)
blocks = cld(max_idx, threads)
kernel(args...; threads=threads, blocks=blocks)
return dst
end
"""
reduce_nodes(aggr, g, x)
For a batched graph `g`, return the graph-wise aggregation of the node
features `x`. The aggregation operator `aggr` can be `+`, `mean`, `max`, or `min`.
The returned array will have last dimension `g.num_graphs`.
"""
function reduce_nodes(aggr, g::GNNGraph, x)
@assert size(x)[end] == g.num_nodes
indexes = graph_indicator(g)
return NNlib.scatter(aggr, x, indexes)
end
"""
reduce_edges(aggr, g, e)
For a batched graph `g`, return the graph-wise aggregation of the edge
features `e`. The aggregation operator `aggr` can be `+`, `mean`, `max`, or `min`.
The returned array will have last dimension `g.num_graphs`.
"""
function reduce_edges(aggr, g::GNNGraph, e)
@assert size(e)[end] == g.num_edges
s, t = edge_index(g)
indexes = graph_indicator(g)[s]
return NNlib.scatter(aggr, e, indexes)
end
"""
softmax_nodes(g, x)
Graph-wise softmax of the node features `x`.
"""
function softmax_nodes(g::GNNGraph, x)
@assert size(x)[end] == g.num_nodes
gi = graph_indicator(g)
max_ = gather(scatter(max, x, gi), gi)
num = exp.(x .- max_)
den = reduce_nodes(+, g, num)
den = gather(den, gi)
return num ./ den
end
"""
softmax_edges(g, e)
Graph-wise softmax of the edge features `e`.
"""
function softmax_edges(g::GNNGraph, e)
@assert size(e)[end] == g.num_edges
gi = graph_indicator(g, edges=true)
max_ = gather(scatter(max, e, gi), gi)
num = exp.(e .- max_)
den = reduce_edges(+, g, num)
den = gather(den, gi)
return num ./ den
end
"""
broadcast_nodes(g, x)
Graph-wise broadcast array `x` of size `(*, g.num_graphs)`
to size `(*, g.num_nodes)`.
"""
function broadcast_nodes(g::GNNGraph, x)
@assert size(x)[end] == g.num_graphs
gi = graph_indicator(g)
return gather(x, gi)
end
"""
broadcast_edges(g, x)
Graph-wise broadcast array `x` of size `(*, g.num_graphs)`
to size `(*, g.num_edges)`.
"""
function broadcast_edges(g::GNNGraph, x)
@assert size(x)[end] == g.num_graphs
gi = graph_indicator(g, edges=true)
return gather(x, gi)
end
function graph_indicator(g; edges=false)
if isnothing(g.graph_indicator)
gi = ones_like(edge_index(g)[1], Int, g.num_nodes)
else
gi = g.graph_indicator
end
if edges
s, t = edge_index(g)
return gi[s]
else
return gi
end
end