-
Notifications
You must be signed in to change notification settings - Fork 63
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
Feature/pytorch geometric #216
Merged
ChrisCummins
merged 12 commits into
ChrisCummins:development
from
igabirondo16:feature/pytorch_geometric
May 22, 2024
+257
−1
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
68f886e
Add to_pyg() method
45a758c
Document to_pyg() function
e392140
Update requirements
e11c7d1
Store the text of the nodes, the type edges and the ensure that the c…
80b22c4
Add to_pyg() to the module
9c89f0d
Add tests for to_pyg() method
adfb51f
Include to_pyg() tests
1874e78
Update documentation of to_pyg()
988efda
Add license
37b0fe2
Store the full text attribute of the nodes
2a93fe6
Add tests for different graphs
a066ffb
Refactor code for determining if two graphs are equal
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
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 |
---|---|---|
|
@@ -5,4 +5,5 @@ networkx>=2.4 | |
numpy>=1.19.3 | ||
protobuf>=3.13.0,<4.21.0 | ||
torch>=1.8.0 | ||
torch_geometric==2.4.0 | ||
tqdm>=4.38.0 |
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
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
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 |
---|---|---|
@@ -0,0 +1,148 @@ | ||
# Copyright 2019-2020 the ProGraML authors. | ||
# | ||
# Contact Chris Cummins <[email protected]>. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from concurrent.futures.thread import ThreadPoolExecutor | ||
|
||
import networkx as nx | ||
import pytest | ||
|
||
import programl as pg | ||
from torch_geometric.data import HeteroData | ||
from tests.test_main import main | ||
|
||
pytest_plugins = ["tests.plugins.llvm_program_graph"] | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def graph() -> pg.ProgramGraph: | ||
return pg.from_cpp("int A() { return 0; }") | ||
|
||
@pytest.fixture(scope="session") | ||
def graph2() -> pg.ProgramGraph: | ||
return pg.from_cpp("int B() { return 1; }") | ||
|
||
@pytest.fixture(scope="session") | ||
def graph3() -> pg.ProgramGraph: | ||
return pg.from_cpp("int B(int x) { return x + 1; }") | ||
|
||
def assert_equal_graphs( | ||
graph1: HeteroData, | ||
graph2: HeteroData, | ||
equality: bool = True | ||
): | ||
if equality: | ||
assert graph1['nodes']['full_text'] == graph2['nodes']['full_text'] | ||
|
||
assert graph1['nodes', 'control', 'nodes'].edge_index.equal(graph2['nodes', 'control', 'nodes'].edge_index) | ||
assert graph1['nodes', 'data', 'nodes'].edge_index.equal(graph2['nodes', 'data', 'nodes'].edge_index) | ||
assert graph1['nodes', 'call', 'nodes'].edge_index.equal(graph2['nodes', 'call', 'nodes'].edge_index) | ||
assert graph1['nodes', 'type', 'nodes'].edge_index.equal(graph2['nodes', 'type', 'nodes'].edge_index) | ||
|
||
else: | ||
text_different = graph1['nodes']['full_text'] != graph2['nodes']['full_text'] | ||
|
||
control_edges_different = not graph1['nodes', 'control', 'nodes'].edge_index.equal( | ||
graph2['nodes', 'control', 'nodes'].edge_index | ||
) | ||
data_edges_different = not graph1['nodes', 'data', 'nodes'].edge_index.equal( | ||
graph2['nodes', 'data', 'nodes'].edge_index | ||
) | ||
call_edges_different = not graph1['nodes', 'call', 'nodes'].edge_index.equal( | ||
graph2['nodes', 'call', 'nodes'].edge_index | ||
) | ||
type_edges_different = not graph1['nodes', 'type', 'nodes'].edge_index.equal( | ||
graph2['nodes', 'type', 'nodes'].edge_index | ||
) | ||
|
||
assert ( | ||
text_different | ||
or control_edges_different | ||
or data_edges_different | ||
or call_edges_different | ||
or type_edges_different | ||
) | ||
|
||
def test_to_pyg_simple_graph(graph: pg.ProgramGraph): | ||
graphs = list(pg.to_pyg([graph])) | ||
assert len(graphs) == 1 | ||
assert isinstance(graphs[0], HeteroData) | ||
|
||
def test_to_pyg_simple_graph_single_input(graph: pg.ProgramGraph): | ||
pyg_graph = pg.to_pyg(graph) | ||
assert isinstance(pyg_graph, HeteroData) | ||
|
||
def test_to_pyg_different_two_different_inputs( | ||
graph: pg.ProgramGraph, | ||
graph2: pg.ProgramGraph, | ||
): | ||
pyg_graph = pg.to_pyg(graph) | ||
pyg_graph2 = pg.to_pyg(graph2) | ||
|
||
# Ensure that the graphs are different | ||
assert_equal_graphs(pyg_graph, pyg_graph2, equality=False) | ||
|
||
def test_to_pyg_different_inputs( | ||
graph: pg.ProgramGraph, | ||
graph2: pg.ProgramGraph, | ||
graph3: pg.ProgramGraph | ||
): | ||
pyg_graph = pg.to_pyg(graph) | ||
pyg_graph2 = pg.to_pyg(graph2) | ||
pyg_graph3 = pg.to_pyg(graph3) | ||
|
||
# Ensure that the graphs are different | ||
assert_equal_graphs(pyg_graph, pyg_graph2, equality=False) | ||
assert_equal_graphs(pyg_graph, pyg_graph3, equality=False) | ||
assert_equal_graphs(pyg_graph2, pyg_graph3, equality=False) | ||
|
||
def test_to_pyg_two_inputs(graph: pg.ProgramGraph): | ||
graphs = list(pg.to_pyg([graph, graph])) | ||
assert len(graphs) == 2 | ||
assert_equal_graphs(graphs[0], graphs[1], equality=True) | ||
|
||
def test_to_pyg_generator(graph: pg.ProgramGraph): | ||
graphs = list(pg.to_pyg((graph for _ in range(10)), chunksize=3)) | ||
assert len(graphs) == 10 | ||
for x in graphs[1:]: | ||
assert_equal_graphs(graphs[0], x, equality=True) | ||
|
||
def test_to_pyg_generator_parallel_executor(graph: pg.ProgramGraph): | ||
with ThreadPoolExecutor() as executor: | ||
graphs = list( | ||
pg.to_pyg((graph for _ in range(10)), chunksize=3, executor=executor) | ||
) | ||
assert len(graphs) == 10 | ||
for x in graphs[1:]: | ||
assert_equal_graphs(graphs[0], x, equality=True) | ||
|
||
|
||
def test_to_pyg_smoke_test(llvm_program_graph: pg.ProgramGraph): | ||
graphs = list(pg.to_pyg([llvm_program_graph])) | ||
|
||
num_nodes = len(graphs[0]['nodes']['text']) | ||
num_control_edges = graphs[0]['nodes', 'control', 'nodes'].edge_index.size(1) | ||
num_data_edges = graphs[0]['nodes', 'data', 'nodes'].edge_index.size(1) | ||
num_call_edges = graphs[0]['nodes', 'call', 'nodes'].edge_index.size(1) | ||
num_type_edges = graphs[0]['nodes', 'type', 'nodes'].edge_index.size(1) | ||
num_edges = num_control_edges + num_data_edges + num_call_edges + num_type_edges | ||
|
||
assert len(graphs) == 1 | ||
assert isinstance(graphs[0], HeteroData) | ||
assert num_nodes == len(llvm_program_graph.node) | ||
assert num_edges <= len(llvm_program_graph.edge) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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.
I may have misunderstood, but I believe you could simplify this to just a helper function that runs all your equality checks like:
then in your tests:
would that work?