Skip to content

Commit f1bfe67

Browse files
Shashank Singhfacebook-github-bot
Shashank Singh
authored andcommitted
Back out "[caffe2] Update blackbox predictor with new constructor" (pytorch#11105)
Summary: Pull Request resolved: pytorch#11105 Reverts: D9516972 See this discussion for context: https://fburl.com/w45hb1oc Reviewed By: highker Differential Revision: D9587931 fbshipit-source-id: 715247929d819dfa88e1d051021e51c5bf0c4835
1 parent 9fae8fc commit f1bfe67

File tree

4 files changed

+13
-109
lines changed

4 files changed

+13
-109
lines changed

caffe2/predictor/predictor_config.cc

+12-46
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace {
1010
// We don't use the getNet() from predictor_utils.cc here because that file
1111
// has additional dependencies that we want to avoid bringing in, to keep the
1212
// binary size as small as possible.
13-
static const NetDef& getNet(const MetaNetDef& def, const std::string& name) {
13+
const NetDef& getNet(const MetaNetDef& def, const std::string& name) {
1414
for (const auto& n : def.nets()) {
1515
if (n.key() == name) {
1616
return n.value();
@@ -19,7 +19,7 @@ static const NetDef& getNet(const MetaNetDef& def, const std::string& name) {
1919
CAFFE_THROW("Net not found: ", name);
2020
}
2121

22-
static const ::google::protobuf::RepeatedPtrField<::std::string>& getBlobs(
22+
const ::google::protobuf::RepeatedPtrField<::std::string>& getBlobs(
2323
const MetaNetDef& def,
2424
const std::string& name) {
2525
for (const auto& b : def.blobs()) {
@@ -30,60 +30,26 @@ static const ::google::protobuf::RepeatedPtrField<::std::string>& getBlobs(
3030
CAFFE_THROW("Blob not found: ", name);
3131
}
3232

33-
static std::string combine(const std::string& str, const std::string& name) {
34-
if (name.empty()) {
35-
return std::string(str);
36-
}
37-
return str + "_" + name;
38-
}
39-
40-
static std::string getNamedPredictNet(const string& name) {
41-
return combine(PredictorConsts::default_instance().predict_net_type(), name);
42-
}
43-
44-
static std::string getNamedInitNet(const string& name) {
45-
return combine(
46-
PredictorConsts::default_instance().predict_init_net_type(), name);
47-
}
48-
49-
static std::string getNamedInputs(const string& name) {
50-
return combine(PredictorConsts::default_instance().inputs_blob_type(), name);
51-
}
52-
53-
static std::string getNamedOutputs(const string& name) {
54-
return combine(PredictorConsts::default_instance().outputs_blob_type(), name);
55-
}
56-
57-
static std::string getNamedParams(const string& name) {
58-
return combine(
59-
PredictorConsts::default_instance().parameters_blob_type(), name);
60-
}
61-
6233
} // namespace
6334

64-
PredictorConfig makePredictorConfig(
65-
const MetaNetDef& def,
66-
Workspace* parent,
67-
bool run_init,
68-
const std::string& net_name) {
69-
const auto& init_net = getNet(def, getNamedInitNet(net_name));
70-
const auto& run_net = getNet(def, getNamedPredictNet(net_name));
35+
PredictorConfig
36+
makePredictorConfig(const MetaNetDef& def, Workspace* parent, bool run_init) {
37+
const auto& init_net =
38+
getNet(def, PredictorConsts::default_instance().global_init_net_type());
39+
const auto& run_net =
40+
getNet(def, PredictorConsts::default_instance().predict_net_type());
7141
auto config = makePredictorConfig(init_net, run_net, parent, run_init);
72-
const auto& inputs = getBlobs(def, getNamedInputs(net_name));
42+
const auto& inputs =
43+
getBlobs(def, PredictorConsts::default_instance().inputs_blob_type());
7344
for (const auto& input : inputs) {
7445
config.input_names.emplace_back(input);
7546
}
7647

77-
const auto& outputs = getBlobs(def, getNamedOutputs(net_name));
48+
const auto& outputs =
49+
getBlobs(def, PredictorConsts::default_instance().outputs_blob_type());
7850
for (const auto& output : outputs) {
7951
config.output_names.emplace_back(output);
8052
}
81-
82-
const auto& params = getBlobs(def, getNamedParams(net_name));
83-
for (const auto& param : params) {
84-
config.parameter_names.emplace_back(param);
85-
}
86-
8753
return config;
8854
}
8955

caffe2/predictor/predictor_config.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ CAFFE2_API Workspace makeWorkspace(std::shared_ptr<PredictorParameters> paramete
4545
CAFFE2_API PredictorConfig makePredictorConfig(
4646
const MetaNetDef& net,
4747
Workspace* parent = nullptr,
48-
bool run_init = true,
49-
const std::string& net_name = "");
48+
bool run_init = true);
5049

5150
CAFFE2_API PredictorConfig makePredictorConfig(
5251
const NetDef& init_net,

caffe2/predictor/predictor_utils.cc

-51
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
#include "caffe2/predictor/predictor_utils.h"
2-
#include "caffe2/predictor/predictor_config.h"
32

43
#include "caffe2/core/blob.h"
54
#include "caffe2/core/logging.h"
65
#include "caffe2/proto/caffe2_pb.h"
76
#include "caffe2/proto/predictor_consts.pb.h"
87
#include "caffe2/utils/proto_utils.h"
98

10-
CAFFE2_DEFINE_bool(
11-
caffe2_predictor_claim_tensor_memory,
12-
true,
13-
"If false, then predictor will not claim tensor memory"
14-
"otherwise when tensor is shrinked to a size smaller than current size "
15-
"by FLAGS_caffe2_max_keep_on_shrink_memory, the memory will be claimed.");
16-
179
namespace caffe2 {
1810
namespace predictor_utils {
1911

@@ -87,47 +79,4 @@ std::unique_ptr<MetaNetDef> runGlobalInitialization(
8779
}
8880

8981
} // namespace predictor_utils
90-
91-
void removeExternalBlobs(
92-
const std::vector<std::string>& input_blobs,
93-
const std::vector<std::string>& output_blobs,
94-
Workspace* ws) {
95-
for (const auto& blob : input_blobs) {
96-
ws->RemoveBlob(blob);
97-
}
98-
for (const auto& blob : output_blobs) {
99-
ws->RemoveBlob(blob);
100-
}
101-
}
102-
103-
PredictorConfig makePredictorConfig(
104-
const string& db_type,
105-
const string& db_path) {
106-
// TODO: Remove this flags once Predictor accept PredictorConfig as
107-
// constructors. These comes are copied temporarly from the Predictor.
108-
if (FLAGS_caffe2_predictor_claim_tensor_memory) {
109-
if (FLAGS_caffe2_max_keep_on_shrink_memory == LLONG_MAX) {
110-
FLAGS_caffe2_max_keep_on_shrink_memory = 8 * 1024 * 1024;
111-
}
112-
}
113-
auto dbReader =
114-
make_unique<db::DBReader>(db::CreateDB(db_type, db_path, db::READ));
115-
auto ws = std::make_shared<Workspace>();
116-
auto net_def =
117-
predictor_utils::runGlobalInitialization(std::move(dbReader), ws.get());
118-
auto config = makePredictorConfig(*net_def, ws.get());
119-
config.ws = ws;
120-
const auto& init_net = predictor_utils::getNet(
121-
*net_def, PredictorConsts::default_instance().predict_init_net_type());
122-
CAFFE_ENFORCE(config.ws->RunNetOnce(init_net));
123-
config.ws->RemoveBlob(
124-
PredictorConsts::default_instance().predictor_dbreader());
125-
// Input and output blobs should never be allocated in the master workspace
126-
// since we'll end up with race-conditions due to these being shared among
127-
// predictor threads / TL workspaces. Safely handle against globalInitNet
128-
// creating them in the master.
129-
removeExternalBlobs(config.input_names, config.output_names, config.ws.get());
130-
return config;
131-
}
132-
13382
} // namespace caffe2

caffe2/predictor/predictor_utils.h

-10
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,4 @@ CAFFE2_API std::unique_ptr<MetaNetDef> runGlobalInitialization(
2424
Workspace* master);
2525

2626
} // namespace predictor_utils
27-
28-
PredictorConfig makePredictorConfig(
29-
const string& db_type,
30-
const string& db_path);
31-
32-
void removeExternalBlobs(
33-
const std::vector<std::string>& input_blobs,
34-
const std::vector<std::string>& output_blobs,
35-
Workspace* ws);
36-
3727
} // namespace caffe2

0 commit comments

Comments
 (0)