1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
#include "caffe2/core/transform.h"
#include "caffe2/core/common.h"
#include "caffe2/core/logging.h"
#include "caffe2/core/net.h"
#include "caffe2/core/timer.h"
#include "caffe2/proto/caffe2_pb.h"
namespace caffe2 {
using transform::Graph;
C10_DEFINE_REGISTRY(TransformRegistry, Transform);
std::vector<std::vector<int>> Transform::PatternMatch(const Graph& graph) {
// checks if the node at index i is matched already or not
std::vector<bool> matched(graph.size(), false);
// stores matches, which are ordered subgraphs of G
std::vector<std::vector<int>> matches;
// Consider every possible node as the starting point.
for (int idx = 0; idx < (int)graph.size(); ++idx) {
// The current working subgraph. We will try to add new nodes to this,
// when invoking the PatternRule.
std::vector<int> subgraph;
// The largest "validated" subgraph found so far.
// This will be mutated by PatternMatchHelper.
std::vector<int> best_subgraph;
// Only begin to match if the start node is accepted.
if (!matched.at(idx) && PatternRule(graph, subgraph, idx)) {
subgraph.push_back(idx);
PatternMatchHelper(graph, matched, &subgraph, &best_subgraph);
subgraph.pop_back();
}
if (best_subgraph.size() > 0) { // match found
matches.push_back(best_subgraph);
for (const auto& x : best_subgraph) {
matched[x] = true;
}
}
}
return matches;
}
void Transform::TryNeighbors(
const Graph& graph,
const std::map<int, std::vector<string>>& neighbors,
const std::vector<bool>& matched,
std::vector<int>* subgraph_ptr,
std::vector<int>* best_subgraph_ptr) {
auto& subgraph = *subgraph_ptr;
for (const auto& edge : neighbors) {
int j = edge.first;
if (std::find(subgraph.begin(), subgraph.end(), j) == subgraph.end()) {
if (!matched.at(j) && PatternRule(graph, subgraph, j)) {
subgraph.push_back(j);
PatternMatchHelper(graph, matched, subgraph_ptr, best_subgraph_ptr);
subgraph.pop_back();
}
}
}
}
void Transform::PatternMatchHelper(
const Graph& graph,
const std::vector<bool>& matched,
std::vector<int>* subgraph_ptr,
std::vector<int>* best_subgraph_ptr) {
CHECK(subgraph_ptr);
auto& subgraph = *subgraph_ptr;
CHECK(best_subgraph_ptr);
auto& best_subgraph = *best_subgraph_ptr;
// If the current subgraph is valid, and the largest we've seen so far,
// make it the best_subgraph.
if (ValidatorRule(graph, subgraph) &&
subgraph.size() > best_subgraph.size()) {
best_subgraph = subgraph;
}
size_t size_before = subgraph.size();
if (pattern_match_type_ == CONNECTED_SUBGRAPH) {
// Connected Component Order Pattern Matching
// We want to match subgraphs which are connected ConnectedComponents
// Try adding each parent and child of every node in the subgraph,
// and see if we can accept it.
for (size_t i = 0; i < subgraph.size(); i++) {
int x = subgraph[i];
TryNeighbors(
graph,
graph.node(x).children,
matched,
subgraph_ptr,
best_subgraph_ptr);
CAFFE_ENFORCE(
size_before == subgraph.size(),
"Subgraph size should not change after returning from recursive call.");
TryNeighbors(
graph,
graph.node(x).parents,
matched,
subgraph_ptr,
best_subgraph_ptr);
CAFFE_ENFORCE(
size_before == subgraph.size(),
"Subgraph size should not change after returning from recursive call.");
}
} else if (pattern_match_type_ == SORTED_WRT_EXECUTION_ORDER) {
// Sorted Execution Order Pattern matching
// We want to be able to match subgraphs in sorted execution order
// We can safely assume our subgraph is already sorted.
// This means, we only need to consider nodes that come after the LAST
// node in our current subgraph.
// Thus, we simply iterate over the nodes that come AFTER the last node of
// our current subgraph.
size_t start_idx = 0;
if (subgraph.size() > 0) {
start_idx = subgraph.back() + 1;
}
for (size_t i = start_idx; i < graph.size(); i++) {
if (!matched.at(i) && PatternRule(graph, subgraph, i)) {
subgraph.push_back(i);
PatternMatchHelper(graph, matched, subgraph_ptr, best_subgraph_ptr);
subgraph.pop_back();
}
}
} else if (pattern_match_type_ == GENERAL) {
// General Pattern matching
// We want to be able to match any ordered subgraph
// For every current subgraph, we consider all nodes to be
// the next candidate node, as long as it isn't already matched.
for (size_t i = 0; i < graph.size(); i++) {
if (std::find(subgraph.begin(), subgraph.end(), i) == subgraph.end()) {
// Then we try appending it to the subgraph.
if (!matched.at(i) && PatternRule(graph, subgraph, i)) {
subgraph.push_back(i);
PatternMatchHelper(graph, matched, subgraph_ptr, best_subgraph_ptr);
subgraph.pop_back();
}
}
}
} else {
CAFFE_NOT_IMPLEMENTED;
}
}
void Transform::ReplacePattern(
const std::vector<vector<int>>& matches,
Graph* graph) {
for (const auto& match : matches) {
// Make sure each matched node is still active (not overwritten)
bool is_match_active = true;
for (int idx : match) {
if (!graph->is_node_active(idx)) {
is_match_active = false;
}
}
// Simply try to apply the replace rule upon every match.
if (is_match_active && !ReplaceRule(match, graph)) {
CAFFE_THROW("Replace failed!");
}
}
}
// The simple interface - performs the transformation upon a NetDef, and returns
// the result.
NetDef Transform::ApplyTo(const NetDef& orig_net) {
Graph g(orig_net);
const auto matches = PatternMatch(g);
ReplacePattern(matches, &g);
return g.GetNetDef();
}
// Create a Transform object
unique_ptr<Transform> CreateTransform(string key) {
auto t = TransformRegistry()->Create(key);
CAFFE_ENFORCE(t != nullptr, "Transform not found in registry: ", key);
return t;
}
// Create a Transform object from registry,
// and immediately apply it to a Netdef.
NetDef ApplyTransform(const string& key, const NetDef& netdef) {
auto t = CreateTransform(key);
return t->ApplyTo(netdef);
}
double average_net_run_duration(
const NetDef& netdef,
const NetDef& init_netdef,
const int warmup_runs,
const int main_runs) {
Workspace ws;
if (init_netdef.op_size() > 0) {
std::unique_ptr<NetBase> init_net(CreateNet(init_netdef, &ws));
CHECK(init_net);
CAFFE_ENFORCE(init_net->Run(), "Init run has failed!");
} else {
// If a proper init_net is not provided, then this is the best we can do.
// NOLINTNEXTLINE(performance-for-range-copy)
for (auto inp : netdef.external_input()) {
ws.CreateBlob(inp);
}
}
std::unique_ptr<NetBase> net(CreateNet(netdef, &ws));
CHECK(net);
CAFFE_ENFORCE(
warmup_runs >= 0,
"Number of warm up runs should be non negative, provided ",
warmup_runs,
".");
for (int i = 0; i < warmup_runs; i++) {
CAFFE_ENFORCE(net->Run(), "Warmup run ", i, " has failed.");
}
CAFFE_ENFORCE(
main_runs > 0,
"Number of main runs should be positive, provided ",
main_runs,
".");
Timer timer;
for (int i = 0; i < main_runs; i++) {
CAFFE_ENFORCE(net->Run(), "Main run ", i, " has failed.");
}
return timer.MilliSeconds();
}
// Create a Transform object from registry, apply it to a NetDef.
// Will only return the transformed net if it is faster than the old net.
// This will run the init net first, will run the two nets warmup_runs times.
// Then, we will take the average time of main_runs runs, and only keep the
// transformed net if it is faster by a factor of improvement_threshold.
NetDef ApplyTransformIfFaster(
const string& key,
const NetDef& netdef,
const NetDef& init_netdef,
const int warmup_runs,
const int main_runs,
const double improvement_threshold) {
NetDef transformed_netdef = ApplyTransform(key, netdef);
double original_net_time =
average_net_run_duration(netdef, init_netdef, warmup_runs, main_runs);
double new_net_time = average_net_run_duration(
transformed_netdef, init_netdef, warmup_runs, main_runs);
if (original_net_time > improvement_threshold * new_net_time) {
return transformed_netdef;
}
return netdef;
}
} // namespace Caffe2
|