Skip to content

Commit

Permalink
[caffe2][nomnigraph] Add optimize function to opt:: namespace that ta…
Browse files Browse the repository at this point in the history
…kes in a level and optimizes the graph/workspace accordingly. Adding it to predictor and speed_benchmark arguments (#7558)
  • Loading branch information
bwasti authored May 15, 2018
1 parent 469c6c8 commit 4b6c884
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 9 deletions.
6 changes: 6 additions & 0 deletions binaries/speed_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "caffe2/core/init.h"
#include "caffe2/core/logging.h"
#include "caffe2/core/operator.h"
#include "caffe2/opt/optimizer.h"
#include "caffe2/proto/caffe2.pb.h"
#include "caffe2/utils/proto_utils.h"
#include "caffe2/utils/string_utils.h"
Expand Down Expand Up @@ -63,6 +64,7 @@ CAFFE2_DEFINE_string(
"folder must already exist in the file system.");
CAFFE2_DEFINE_int(warmup, 0, "The number of iterations to warm up.");
CAFFE2_DEFINE_int(iter, 10, "The number of iterations to run.");
CAFFE2_DEFINE_int(opt, 0, "The level of optimization to run automatically.");
CAFFE2_DEFINE_bool(
run_individual,
false,
Expand Down Expand Up @@ -170,6 +172,10 @@ int main(int argc, char** argv) {
->set_s(caffe2::FLAGS_algo);
}
}
if (caffe2::FLAGS_opt) {
net_def = caffe2::opt::optimize(net_def, workspace.get(), caffe2::FLAGS_opt);
}

caffe2::NetBase* net = workspace->CreateNet(net_def);
CHECK_NOTNULL(net);
CAFFE_ENFORCE(net->Run());
Expand Down
10 changes: 9 additions & 1 deletion caffe2/core/predictor.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "caffe2/core/predictor.h"
#include "caffe2/opt/optimizer.h"

#include <unordered_set>

Expand Down Expand Up @@ -81,12 +82,18 @@ Predictor::Predictor(
const NetDef& init_net,
const NetDef& run_net,
Workspace* parent,
bool run_init)
bool run_init,
int optimization)
: run_net_(run_net), ws_(parent) {

if (run_init) {
CAFFE_ENFORCE(ws_.RunNetOnce(init_net));
}

if (optimization) {
run_net_ = opt::optimize(run_net_, &ws_, optimization);
}

// real model inputs can be fed later in run* functions
const auto& initialized_vec = ws_.Blobs();
const std::unordered_set<std::string> initialized{initialized_vec.begin(),
Expand All @@ -97,6 +104,7 @@ Predictor::Predictor(
blob->template GetMutable<TensorCPU>();
}
}

CAFFE_ENFORCE(ws_.CreateNet(run_net));
}

Expand Down
3 changes: 2 additions & 1 deletion caffe2/core/predictor.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class Predictor {
const NetDef& init_net,
const NetDef& run_net,
Workspace* parent = nullptr,
bool run_init = true);
bool run_init = true,
int optimization = 1);

~Predictor() {}

Expand Down
6 changes: 3 additions & 3 deletions caffe2/opt/converter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef NOM_CONVERTERS_CAFFE2_H
#define NOM_CONVERTERS_CAFFE2_H
#ifndef CAFFE2_OPT_CONVERTER_H
#define CAFFE2_OPT_CONVERTER_H

#include "nomnigraph/Graph/Graph.h"
#include "nomnigraph/Representations/ControlFlow.h"
Expand Down Expand Up @@ -55,4 +55,4 @@ std::unique_ptr<nom::repr::NeuralNetOperator> convertToOperatorDef(caffe2::Opera
} // namespace caffe2


#endif // NOM_CONVERTERS_CAFFE2_H
#endif // CAFFE2_OPT_CONVERTER_H
2 changes: 0 additions & 2 deletions caffe2/opt/fusion.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
#ifndef CAFFE2_OPT_FUSION_H_
#define CAFFE2_OPT_FUSION_H_

#include "caffe2/core/common.h"
#include "caffe2/core/workspace.h"
#include "caffe2/proto/caffe2.pb.h"
#include "nomnigraph/Representations/NeuralNet.h"

namespace caffe2 {
Expand Down
2 changes: 0 additions & 2 deletions caffe2/opt/mobile.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef CAFFE2_OPT_MOBILE_H_
#define CAFFE2_OPT_MOBILE_H_

#include "caffe2/core/common.h"
#include "caffe2/proto/caffe2.pb.h"
#include "nomnigraph/Representations/NeuralNet.h"

namespace caffe2 {
Expand Down
48 changes: 48 additions & 0 deletions caffe2/opt/optimizer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "caffe2/opt/optimizer.h"

#include "caffe2/opt/converter.h"
#include "caffe2/opt/mobile.h"
#include "caffe2/opt/fusion.h"

namespace caffe2 {
namespace opt {

void workspaceOptimizations(nom::repr::NNModule* nn, Workspace* ws, int level) {
switch (level) {
case 1:
opt::fuseConvBN(nn, ws);
case 0:
default:
break;
}
}

void graphOptimzations(nom::repr::NNModule* nn, int level) {
switch (level) {
case 1:
#ifdef USE_NNPACK
opt::addNNPACK(nn, false);
opt::fuseNNPACKConvRelu(nn);
#endif
case 0:
default:
break;
}
}

NetDef optimize(NetDef net, Workspace* ws, int level) {
auto nn = convertToNNModule(net);
graphOptimzations(&nn, level);
workspaceOptimizations(&nn, ws, level);
return convertToCaffe2Proto(nn, net);
}

NetDef optimize(NetDef net, int level) {
auto nn = convertToNNModule(net);
graphOptimzations(&nn, level);
return convertToCaffe2Proto(nn, net);
}

} // namespace opt
} // namespace caffe2

17 changes: 17 additions & 0 deletions caffe2/opt/optimizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef CAFFE2_OPT_OPTIMIZER_H
#define CAFFE2_OPT_OPTIMIZER_H

#include "caffe2/core/common.h"
#include "caffe2/proto/caffe2.pb.h"
#include "caffe2/core/workspace.h"

namespace caffe2 {
namespace opt {

NetDef optimize(NetDef net, Workspace* ws, int level = 1);
NetDef optimize(NetDef net, int level = 1);

} // namespace opt
} // namespace caffe2

#endif // CAFFE2_OPT_OPTIMIZER_H

0 comments on commit 4b6c884

Please sign in to comment.