-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PASS] PrintGraphIR, SimplifyBatchNormInference (#19)
- Loading branch information
Showing
19 changed files
with
650 additions
and
24 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/*! | ||
* Copyright (c) 2017 by Contributors | ||
* \file graph_deep_compare.cc | ||
* \brief Deep compare two graph structure | ||
*/ | ||
#include <nnvm/graph.h> | ||
#include <nnvm/op_attr_types.h> | ||
#include <nnvm/compiler/packed_func_ext.h> | ||
#include <tvm/runtime/packed_func.h> | ||
#include "./node_attr.h" | ||
|
||
namespace nnvm { | ||
namespace compiler { | ||
|
||
// deep compare the graph structure | ||
// not considering the graph attributes | ||
// return non-empty error message if the graph mismatch. | ||
// the comparator won't match name of intermediate node. | ||
std::string DeepCompare(Graph a, Graph b) { | ||
const IndexedGraph& idxa = a.indexed_graph(); | ||
const IndexedGraph& idxb = b.indexed_graph(); | ||
std::ostringstream err; | ||
if (idxa.num_nodes() != idxb.num_nodes()) { | ||
err << "Number of nodes mismatch"; | ||
return err.str(); | ||
} | ||
if (idxa.num_node_entries() != idxb.num_node_entries()) { | ||
err << "Number of node entry mismatch"; | ||
return err.str(); | ||
} | ||
if (idxa.outputs().size() != idxb.outputs().size()) { | ||
err << "Number of outputs mismatch"; | ||
return err.str(); | ||
} | ||
for (size_t i = 0; i < idxa.outputs().size(); ++i) { | ||
if (idxa.outputs()[i].node_id != idxb.outputs()[i].node_id || | ||
idxa.outputs()[i].index != idxb.outputs()[i].index) { | ||
err << "Output entry mismatch"; | ||
return err.str(); | ||
} | ||
} | ||
if (idxa.input_nodes().size() != idxb.input_nodes().size()) { | ||
err << "Number of inputs mismatch"; | ||
return err.str(); | ||
} | ||
|
||
for (uint32_t nid = 0; nid < idxa.num_nodes(); ++nid) { | ||
const IndexedGraph::Node& anode = idxa[nid]; | ||
const IndexedGraph::Node& bnode = idxb[nid]; | ||
if (anode.source->op() != bnode.source->op()) { | ||
err << "Node mismatch "; | ||
return err.str(); | ||
} | ||
AttrDict adict = GetAttrDict(anode.source->attrs); | ||
AttrDict bdict = GetAttrDict(bnode.source->attrs); | ||
|
||
auto fmatch = [&err, &anode](const AttrDict& adict, const AttrDict& bdict) { | ||
for (const auto& kv : adict) { | ||
auto it = bdict.find(kv.first); | ||
if (it != bdict.end()) { | ||
if (it->second != kv.second) { | ||
err << "Node attr mismatch, op=" << anode.source->attrs.name | ||
<< " attr_key=" << kv.first << " " << it->second | ||
<< " v.s. " << kv.second; | ||
return false; | ||
} | ||
} else { | ||
err << "One attr_key=" << kv.first << " is missing in another " | ||
<< "op=" << anode.source->attrs.name; | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
if (!fmatch(adict, bdict)) return err.str(); | ||
if (adict.size() != bdict.size()) { | ||
CHECK(!fmatch(bdict, adict)); | ||
return err.str(); | ||
} | ||
if (anode.inputs.size() != bnode.inputs.size()) { | ||
err << "Node input mismatch, op=" << anode.source->attrs.name; | ||
return err.str(); | ||
} | ||
if (anode.control_deps.size() != bnode.control_deps.size()) { | ||
err << "Node control_deps mistach, op=" << anode.source->attrs.name; | ||
return err.str(); | ||
} | ||
for (size_t i = 0; i < anode.inputs.size(); ++i) { | ||
const IndexedGraph::NodeEntry& ae = anode.inputs[i]; | ||
const IndexedGraph::NodeEntry& be = bnode.inputs[i]; | ||
if (ae.node_id != be.node_id || | ||
ae.index != be.index || | ||
ae.version != be.version) { | ||
err << "Node input mismatch on, op=" << anode.source->attrs.name; | ||
return err.str(); | ||
} | ||
} | ||
for (size_t i = 0; i < anode.control_deps.size(); ++i) { | ||
if (anode.control_deps[i] != bnode.control_deps[i]) { | ||
err << "Node control_dep mismatch on, op=" << anode.source->attrs.name; | ||
return err.str(); | ||
} | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("nnvm.graph.DeepCompare") | ||
.set_body([](tvm::runtime::TVMArgs args, tvm::runtime::TVMRetValue *rv) { | ||
*rv = DeepCompare(args[0], args[1]); | ||
}); | ||
} // namespace compiler | ||
} // namespace nnvm |
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,125 @@ | ||
/*! | ||
* Copyright (c) 2017 by Contributors | ||
* \file graph_transform.h | ||
* \brief A mutator class that does local pattern matching and mutates a node. | ||
*/ | ||
#ifndef NNVM_COMPILER_GRAPH_TRANSFORM_H_ | ||
#define NNVM_COMPILER_GRAPH_TRANSFORM_H_ | ||
|
||
#include <nnvm/graph.h> | ||
#include <vector> | ||
|
||
namespace nnvm { | ||
namespace compiler { | ||
|
||
/*! | ||
* \brief Transform the graph to build a new Graph, in post DFS order. | ||
* | ||
* Automatically copies node when some of its children or control_deps changed. | ||
* This function won't be called in Variable. | ||
* | ||
* \param graph The original graph | ||
* | ||
* \param ftransform Function of (int nid, const Node* node, std::vector<NodeEntry>* out) -> bool | ||
* | ||
* If empty vector is returned, it means original entries should be kept. | ||
* | ||
* \tparam FTransform The transformation function. | ||
*/ | ||
template<typename FTransform> | ||
Graph GraphTransform(Graph graph, FTransform ftransform) { | ||
const IndexedGraph& idx = graph.indexed_graph(); | ||
// new nodes | ||
std::vector<NodeEntry> new_entry_map(idx.num_node_entries()); | ||
std::vector<bool> updated(idx.num_node_entries(), false); | ||
|
||
// setup inputs and placeholder. | ||
for (uint32_t nid = 0; nid < idx.num_nodes(); ++nid) { | ||
const auto& inode = idx[nid]; | ||
if (inode.source->is_variable()) continue; | ||
bool need_copy = false; | ||
for (const IndexedGraph::NodeEntry& e : inode.inputs) { | ||
if (updated[idx.entry_id(e)]) { | ||
need_copy = true; break; | ||
} | ||
} | ||
if (!need_copy) { | ||
for (const uint32_t cid : inode.control_deps) { | ||
const auto& cnode = idx[cid]; | ||
for (uint32_t i = 0 ; i < cnode.source->num_outputs(); ++i) { | ||
if (updated[idx.entry_id(cid, i)]) { | ||
need_copy = true; | ||
} | ||
} | ||
if (need_copy) break; | ||
} | ||
} | ||
|
||
if (!need_copy) { | ||
std::vector<NodeEntry> ret; | ||
if (ftransform(nid, inode.source, &ret)) { | ||
CHECK_EQ(ret.size(), static_cast<size_t>(inode.source->num_outputs())); | ||
for (uint32_t i = 0 ; i < inode.source->num_outputs(); ++i) { | ||
updated[idx.entry_id(nid, i)] = true; | ||
new_entry_map[idx.entry_id(nid, i)] = ret[i]; | ||
} | ||
} | ||
} else { | ||
NodePtr node = Node::Create(); | ||
node->attrs = inode.source->attrs; | ||
for (size_t i = 0; i < inode.inputs.size(); ++i) { | ||
const IndexedGraph::NodeEntry& e = inode.inputs[i]; | ||
if (updated[idx.entry_id(e)]) { | ||
node->inputs.push_back(new_entry_map[idx.entry_id(e)]); | ||
} else { | ||
node->inputs.push_back(inode.source->inputs[i]); | ||
} | ||
} | ||
for (size_t i = 0; i < inode.control_deps.size(); ++i) { | ||
const uint32_t cid = inode.control_deps[i]; | ||
const auto& cnode = idx[cid]; | ||
CHECK_NE(cnode.source->num_outputs(), 0U); | ||
NodePtr selected_ptr; | ||
for (uint32_t j = 0 ; j < cnode.source->num_outputs(); ++j) { | ||
NodePtr cptr = updated[idx.entry_id(cid, j)] ? | ||
new_entry_map[idx.entry_id(cid, j)].node : inode.source->control_deps[i]; | ||
if (selected_ptr == nullptr) { | ||
selected_ptr = std::move(cptr); | ||
} else { | ||
CHECK(selected_ptr.get() == cptr.get()) | ||
<< "Control dependency node changed to more than one node"; | ||
} | ||
} | ||
node->control_deps.push_back(selected_ptr); | ||
} | ||
std::vector<NodeEntry> ret; | ||
if (ftransform(nid, node.get(), &ret)) { | ||
CHECK_EQ(ret.size(), static_cast<size_t>(inode.source->num_outputs())); | ||
for (uint32_t i = 0 ; i < inode.source->num_outputs(); ++i) { | ||
updated[idx.entry_id(nid, i)] = true; | ||
new_entry_map[idx.entry_id(nid, i)] = ret[i]; | ||
} | ||
} else { | ||
for (uint32_t i = 0 ; i < inode.source->num_outputs(); ++i) { | ||
updated[idx.entry_id(nid, i)] = true; | ||
new_entry_map[idx.entry_id(nid, i)] = NodeEntry{node, i, 0}; | ||
} | ||
} | ||
} | ||
} | ||
Graph ret; | ||
for (size_t i = 0; i < idx.outputs().size(); ++i) { | ||
const IndexedGraph::NodeEntry& e = idx.outputs()[i]; | ||
if (updated[idx.entry_id(e)]) { | ||
ret.outputs.push_back(new_entry_map[idx.entry_id(e)]); | ||
} else { | ||
ret.outputs.push_back(graph.outputs[i]); | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
} // namespace compiler | ||
} // namespace nnvm | ||
|
||
#endif // NNVM_COMPILER_GRAPH_TRANSFORM_H_ |
File renamed without changes.
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,34 @@ | ||
/*! | ||
* Copyright (c) 2017 by Contributors | ||
* \file node_attr.h | ||
* \brief utility to access node attributes | ||
*/ | ||
#ifndef NNVM_COMPILER_NODE_ATTR_H_ | ||
#define NNVM_COMPILER_NODE_ATTR_H_ | ||
|
||
#include <nnvm/op.h> | ||
#include <nnvm/compiler/op_attr_types.h> | ||
#include <unordered_map> | ||
#include <string> | ||
|
||
namespace nnvm { | ||
namespace compiler { | ||
|
||
using AttrDict = std::unordered_map<std::string, std::string>; | ||
/*! | ||
* \brief Get canonicalized attr dict from node | ||
* \param attrs The node attrs | ||
* \return The attribute dict | ||
*/ | ||
inline AttrDict GetAttrDict(const NodeAttrs& attrs) { | ||
static auto& fgetdict = nnvm::Op::GetAttr<FGetAttrDict>("FGetAttrDict"); | ||
if (fgetdict.count(attrs.op)) { | ||
return fgetdict[attrs.op](attrs); | ||
} else { | ||
return attrs.dict; | ||
} | ||
} | ||
|
||
} // namespace compiler | ||
} // namespace nnvm | ||
#endif // NNVM_COMPILER_NODE_ATTR_H_ |
Oops, something went wrong.