|
| 1 | +#include "caffe2/contrib/transform/transform.h" |
| 2 | + |
| 3 | +#include "caffe2/core/common.h" |
| 4 | +#include "caffe2/core/logging.h" |
| 5 | +#include "caffe2/core/net.h" |
| 6 | +#include "caffe2/proto/caffe2.pb.h" |
| 7 | + |
| 8 | +namespace caffe2 { |
| 9 | + |
| 10 | +using transform::Graph; |
| 11 | + |
| 12 | +CAFFE_DEFINE_REGISTRY(TransformRegistry, Transform); |
| 13 | + |
| 14 | +std::vector<std::vector<int>> Transform::PatternMatch(const Graph& graph) { |
| 15 | + std::vector<std::vector<int>> matches; |
| 16 | + |
| 17 | + // Consider every possible node as the starting point. |
| 18 | + for (int idx = 0; idx < graph.size(); ++idx) { |
| 19 | + // The current working subgraph. We will try to add new nodes to this, |
| 20 | + // when invoking the PatternRule. |
| 21 | + std::vector<int> subgraph; |
| 22 | + |
| 23 | + // The largest "validated" subgraph found so far. |
| 24 | + // This will be mutated by PatternMatchHelper. |
| 25 | + std::vector<int> best_subgraph; |
| 26 | + |
| 27 | + // Only begin to match if the start node is accepted. |
| 28 | + if (PatternRule(graph, subgraph, idx)) { |
| 29 | + subgraph.push_back(idx); |
| 30 | + PatternMatchHelper(graph, &subgraph, &best_subgraph); |
| 31 | + subgraph.pop_back(); |
| 32 | + } |
| 33 | + if (best_subgraph.size() > 0) { // match found |
| 34 | + matches.push_back(best_subgraph); |
| 35 | + } |
| 36 | + } |
| 37 | + return matches; |
| 38 | +} |
| 39 | + |
| 40 | +void Transform::TryNeighbors( |
| 41 | + const Graph& graph, |
| 42 | + const std::map<int, string>& neighbors, |
| 43 | + std::vector<int>* subgraph_ptr, |
| 44 | + std::vector<int>* best_subgraph_ptr) { |
| 45 | + auto& subgraph = *subgraph_ptr; |
| 46 | + for (const auto& edge : neighbors) { |
| 47 | + int j = edge.first; |
| 48 | + if (std::find(subgraph.begin(), subgraph.end(), j) == subgraph.end()) { |
| 49 | + if (PatternRule(graph, subgraph, j)) { |
| 50 | + subgraph.push_back(j); |
| 51 | + PatternMatchHelper(graph, subgraph_ptr, best_subgraph_ptr); |
| 52 | + subgraph.pop_back(); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +void Transform::PatternMatchHelper( |
| 59 | + const Graph& graph, |
| 60 | + std::vector<int>* subgraph_ptr, |
| 61 | + std::vector<int>* best_subgraph_ptr) { |
| 62 | + CHECK(subgraph_ptr); |
| 63 | + auto& subgraph = *subgraph_ptr; |
| 64 | + CHECK(best_subgraph_ptr); |
| 65 | + auto& best_subgraph = *best_subgraph_ptr; |
| 66 | + |
| 67 | + // If the current subgraph is valid, and the largest we've seen so far, |
| 68 | + // make it the best_subgraph. |
| 69 | + if (ValidatorRule(graph, subgraph) && |
| 70 | + subgraph.size() > best_subgraph.size()) { |
| 71 | + best_subgraph = subgraph; |
| 72 | + } |
| 73 | + |
| 74 | + // Try adding each parent and child of every node in the subgraph, |
| 75 | + // and see if we can accept it. |
| 76 | + for (int i : subgraph) { |
| 77 | + TryNeighbors( |
| 78 | + graph, graph.node(i).children, subgraph_ptr, best_subgraph_ptr); |
| 79 | + TryNeighbors(graph, graph.node(i).parents, subgraph_ptr, best_subgraph_ptr); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void Transform::ReplacePattern( |
| 84 | + const std::vector<vector<int>>& matches, |
| 85 | + Graph* graph) { |
| 86 | + // Simply try to apply the replace rule upon every match. |
| 87 | + for (const auto& match : matches) { |
| 88 | + if (!ReplaceRule(match, graph)) { |
| 89 | + CAFFE_THROW("Replace failed!"); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// The simple interface - performs the transformation upon a NetDef, and returns |
| 95 | +// the result. |
| 96 | +NetDef Transform::ApplyTo(const NetDef& orig_net) { |
| 97 | + Graph g(orig_net); |
| 98 | + const auto matches = PatternMatch(g); |
| 99 | + ReplacePattern(matches, &g); |
| 100 | + return g.GetNetDef(); |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace Caffe2 |
0 commit comments