Skip to content

Commit e7f5fce

Browse files
duc0facebook-github-bot
authored andcommitted
nomnigraph - easy - expose inducesEdges and addNode to python's NNSubgraph (pytorch#14074)
Summary: Pull Request resolved: pytorch#14074 expose inducesEdges and addNode to python's NNSubgraph. This make it easy to manually construct a NNSubgraph in python Reviewed By: bwasti Differential Revision: D13092885 fbshipit-source-id: a94ed0b318162e27e3a4b5a4954eb6d169da7405
1 parent 7b0f674 commit e7f5fce

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

caffe2/python/nomnigraph_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ def test_edges_simple(self):
9696
dfg.createEdge(op, w)
9797
dfg.createEdge(x, op)
9898

99+
# subgraph
100+
sg = ng.NNSubgraph()
101+
sg.addNode(x)
102+
sg.addNode(op)
103+
sg.induceEdges()
104+
assert len(sg) == 2
105+
99106
@given(size=st.sampled_from([10, 50]))
100107
def test_edges_complex(self, size):
101108
random.seed(1337)

caffe2/python/pybind_state_nomni.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,14 @@ void addNomnigraphMethods(pybind11::module& m) {
382382

383383
// Subgraph matching API
384384
py::class_<NNSubgraph> nnsubgraph(m, "NNSubgraph");
385-
nnsubgraph.def("__len__", [](NNSubgraph& s) { return s.getNodes().size(); })
385+
nnsubgraph.def(py::init<>())
386+
.def("__len__", [](NNSubgraph& s) { return s.getNodes().size(); })
387+
.def(
388+
"addNode",
389+
[](NNSubgraph* sg, NNGraph::NodeRef node) { sg->addNode(node); })
390+
.def(
391+
"induceEdges",
392+
[](NNSubgraph* sg) { nom::algorithm::induceEdges(sg); })
386393
.def_property_readonly(
387394
"nodes",
388395
[](NNSubgraph& s) {

0 commit comments

Comments
 (0)