-
Notifications
You must be signed in to change notification settings - Fork 47
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
Add heterogeneous add_self_loop support #345
Merged
CarloLucibello
merged 6 commits into
JuliaGraphs:master
from
AarSeBail:add_self_loops-heterograph
Jan 7, 2024
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
16cdec9
Add heterogeneous add_self_loop support
AarSeBail 227c35d
Merge pull request #1 from CarloLucibello/master
AarSeBail 5a83b82
Merge branch 'CarloLucibello:master' into add_self_loops-heterograph
AarSeBail 71aac4f
Fixed array types for add_self_loop on GNNHeteroGraph
AarSeBail 4a896cd
Merge branch 'add_self_loops-heterograph' of https://github.com/AarSe…
AarSeBail f1b2b77
Remove debugging info
AarSeBail File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,62 @@ function add_self_loops(g::GNNGraph{<:ADJMAT_T}) | |
g.ndata, g.edata, g.gdata) | ||
end | ||
|
||
""" | ||
add_self_loops(g::GNNHeteroGraph, edge_t::EType) | ||
|
||
Return a graph with the same features as `g` | ||
but also adding self-loops of the specified type, edge_t | ||
|
||
Nodes with already existing self-loops of type edge_t will obtain a second self-loop of type edge_t. | ||
|
||
If the graphs has edge weights for edges of type edge_t, the new edges will have weight 1. | ||
""" | ||
function add_self_loops(g::GNNHeteroGraph{<:COO_T}, edge_t::EType) | ||
function get_edge_weight_nullable(g::GNNHeteroGraph{<:COO_T}, edge_t::EType) | ||
get(g.graph, edge_t, (nothing, nothing, nothing))[3] | ||
end | ||
|
||
src_t, _, tgt_t = edge_t | ||
(src_t === tgt_t) || | ||
@error "cannot add a self-loop with different source and target types" | ||
|
||
n = get(g.num_nodes, src_t, 0) | ||
|
||
# By avoiding using haskey, this only calls ht_keyindex once instead of twice | ||
if (x = get(g.graph, edge_t, nothing)) !== nothing | ||
s, t = x[1:2] | ||
nodes = convert(typeof(s), [1:n;]) | ||
s = [s; nodes] | ||
t = [t; nodes] | ||
else | ||
nodes = [1:n;] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This creates a cpu array even if the rest of graph lives on gpu. We should create a new array similar to one in the existing relations. |
||
s = nodes | ||
t = nodes | ||
end | ||
|
||
graph = g.graph |> copy | ||
ew = get_edge_weight_nullable(g, edge_t) | ||
|
||
if ew !== nothing | ||
ew = [ew; fill!(similar(ew, n), 1)] | ||
end | ||
|
||
graph[edge_t] = (s, t, ew) | ||
edata = g.edata |> copy | ||
ndata = g.ndata |> copy | ||
ntypes = g.ntypes |> copy | ||
etypes = g.etypes |> copy | ||
num_nodes = g.num_nodes |> copy | ||
num_edges = g.num_edges |> copy | ||
num_edges[edge_t] = length(get(graph, edge_t, ([],[]))[1]) | ||
|
||
return GNNHeteroGraph(graph, | ||
num_nodes, num_edges, g.num_graphs, | ||
g.graph_indicator, | ||
ndata, edata, g.gdata, | ||
ntypes, etypes) | ||
end | ||
|
||
""" | ||
remove_self_loops(g::GNNGraph) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These kinds of performance concerns for dictionary queries are irrelevant. The heavy operations are the copies and the concatenations. Therefore the concern here should be to make the code has readable as possible, which means using haskey in my opinion.