Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for quadratic batching in #99 #100

Closed
wants to merge 5 commits into from
Closed
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
29 changes: 27 additions & 2 deletions src/GNNGraphs/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,33 @@ julia> g12.ndata.x
1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
```
"""
Flux.batch(gs::Vector{<:GNNGraph}) = blockdiag(gs...)

function Flux.batch(gs::Vector{<:GNNGraph})
nodes = [g.num_nodes for g in gs]

if all(y -> isa(y, COO_T), [g.graph for g in gs] )
edge_indices = [edge_index(g) for g in gs]
nodesum = cumsum([0, nodes...])[1:end-1]
s = cat_features([ei[1] .+ nodesum[ii] for (ii, ei) in enumerate(edge_indices)])
t = cat_features([ei[2] .+ nodesum[ii] for (ii, ei) in enumerate(edge_indices)])
w = reduce(vcat, [get_edge_weight(g) for g in gs])
w = w isa Vector{Nothing} ? nothing : w
graph = (s, t, w)
graph_indicator = vcat([ones_like(ei[1],Int,nodes[ii]) .+ (ii - 1) for (ii,ei) in enumerate(edge_indices)]...)
elseif all(y -> isa(y, ADJMAT_T), [g.graph for g in gs] )
graph = blockdiag([g.graph for g in gs]...)
Copy link
Member

@CarloLucibello CarloLucibello Jan 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this blockdiag method does not exist for dense matrices.
Maybe we can use dispatch as follows:

Flux.batch(gs::Vector{<:GNNGraph{<:COO_T}) = ...  # specialized method
Flux.batch(gs::Vector{<:GNNGraph{<:SPARSE_T}) = ...  # specialized method
Flux.batch(gs::Vector{<:GNNGraph{<:SPARSE_T}) = blockdiag(gs...)  # old slow fallback

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comments! I'll work on the dispatching

graph_indicator = vcat([ones_like(graph,Int,nodes[ii]) .+ (ii - 1) for ii in 1:length(nodes)]...)
end

GNNGraph(graph,
sum(nodes),
sum([g.num_edges for g in gs]),
sum([g.num_graphs for g in gs]),
graph_indicator,
cat_features([g.ndata for g in gs]),
cat_features([g.edata for g in gs]),
cat_features([g.gdata for g in gs]),
)
end

"""
unbatch(g::GNNGraph)
Expand Down
12 changes: 12 additions & 0 deletions src/GNNGraphs/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ function cat_features(x1::NamedTuple, x2::NamedTuple)
NamedTuple(k => cat_features(getfield(x1,k), getfield(x2,k)) for k in keys(x1))
end

function cat_features(xs::Vector{<:NamedTuple})
symbols = [sort(collect(keys(x))) for x in xs]
all(y->y==symbols[1], symbols) || @error "cannot concatenate feature data with different keys"
length(xs) == 1 && return xs[1]

# concatenate
syms = symbols[1]
NamedTuple(
k => cat_features([x[k] for x in xs]) for (ii,k) in enumerate(syms)
)
end

# Turns generic type into named tuple
normalize_graphdata(data::Nothing; kws...) = NamedTuple()

Expand Down
1 change: 1 addition & 0 deletions test/GNNGraphs/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

g12 = Flux.batch([g1, g2])
g12b = blockdiag(g1, g2)
@test g12 == g12b

g123 = Flux.batch([g1, g2, g3])
@test g123.graph_indicator == [fill(1, 10); fill(2, 4); fill(3, 7)]
Expand Down