forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This started off as a minor fix based on Adam's question, "why is printing a graph not const" and snowballed into a giant yak shaving exercise. - The Graph and Node APIs now uniformly enforce deep constness; e.g., if you get a const Node* or const Graph*, it is not possible to get a non-const Node*/Graph* somewhere else in the graph (even though the member variables of these are non-const. Hooray for private access specifier.) - A big pile of functions got const versions, most notably the printing functions, and functions for accessing inputs(). - REALLY IMPORTANT, BC-BREAKING CHANGE: inputs() now returns a COPY of the inputs, rather than a reference to the underlying. I was forced to do this because there is no way to portably turn a std::vector<Node*> into a std::vector<const Node*>, which is necessary to provide a const-correct version of inputs() that enforces deep const-correctness. I then justified this choice to myself with the observation that outputs() returned a copy (by necessity), so this makes the API more uniform. But making this change uncovered two very subtle bugs: 1. If you change functions from returning a reference to returning a copy, the idiom node->inputs().begin() is no longer valid, because the memory the iterator points to immediately becomes invalid. THIS SUCKS. Honestly, we should add a lint rule rejecting calling begin()/end() on temporaries because this is very dangerous. To excise this pattern from the codebase, I added begin() and end() methods to Graph, so that we got rid of the graph->nodes().begin() idiom, which happens to be sound, despite not returning a reference, because graph_node_list is a non-owning reference. 2. pybind11 doesn't handle std::vector<Node*> cast out of the box. Fortunately, I found a simple fix in the GitHub issues tracker that involved adding an extra type converter. And yes, this does mean that outputs() in Python never worked correctly. - New const_graph_node_list, which is a graph_node_list that gives you const Node* There are some more miscellaneous improvements: - Applied CR comment fixes on export.cpp; using replaceInput, and renaming variables for clarity. - assertValidInput helper method added, and applied to replaceInput - Use an explicit function to print THPObjectPtr, otherwise we get the wrong overload. Signed-off-by: Edward Z. Yang <ezyang@fb.com>
- Loading branch information
Showing
13 changed files
with
173 additions
and
78 deletions.
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
graph(%1 : Double(2, 2)) { | ||
%3 : Double(2, 2), %4 : Handle = ^Dropout(0.6, True, False)(%1), uses = [[%0.i0], []]; | ||
return (%3); | ||
} |
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
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
Oops, something went wrong.