Caffe2 - C++ API
A deep learning, cross platform ML framework
dead_code_elim.cc
1 #include "caffe2/core/logging.h"
2 #include "caffe2/opt/converter.h"
3 #include "caffe2/opt/passes.h"
4 
5 namespace caffe2 {
6 namespace opt {
7 
8 using namespace nom;
9 using namespace nom::repr;
10 
11 void deadCodeElim(NNModule* nn) {
12  // Iteratively remove unconsumed non-external outputs.
13  bool changed = false;
14  do {
15  changed = false;
16  for (const auto& node : nn->dataFlow.getMutableNodes()) {
17  NOM_REQUIRE_OR_CONT(nn::is<repr::NeuralNetOperator>(node));
18 
19  bool isUsed = false;
20  for (const auto& output : nn::getOutputs(node)) {
21  if (nn::hasConsumer(output) || nn->outputs.count(output)) {
22  isUsed = true;
23  break;
24  }
25  }
26 
27  NOM_REQUIRE_OR_CONT(!isUsed);
28 
29  // No outputs are used, delete them and the node itself.
30  for (const auto& output : nn::getOutputs(node)) {
31  nn->dataFlow.deleteNode(output);
32  }
33  nn->dataFlow.deleteNode(node);
34  changed = true;
35  break;
36  }
37  } while (changed);
38 }
39 
40 REGISTER_OPT_PASS_FROM_FUNC(DeadCodeElim, deadCodeElim);
41 
42 } // namespace opt
43 } // namespace caffe2
Definition: Dot.h:16
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
void deleteNode(NodeRef n)
Deletes a node from the graph.
Definition: Graph.h:460