Skip to content

Commit

Permalink
Remove more protobuf APIs. (pytorch#2348)
Browse files Browse the repository at this point in the history
* Wrap ShutdownProtobufLibrary

* Remove text_format.h header and only put the function in proto_utils.h

* ParseFromString returns bool
  • Loading branch information
Yangqing authored Mar 21, 2018
1 parent b1684e9 commit 611a89c
Show file tree
Hide file tree
Showing 24 changed files with 60 additions and 43 deletions.
2 changes: 1 addition & 1 deletion binaries/predictor_verifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ int main(int argc, char** argv) {
caffe2::GlobalInit(&argc, &argv);
caffe2::run();
// This is to allow us to use memory leak checks.
google::protobuf::ShutdownProtobufLibrary();
caffe2::ShutdownProtobufLibrary();
return 0;
}
2 changes: 1 addition & 1 deletion binaries/run_plan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ int main(int argc, char** argv) {
workspace->RunPlan(plan_def);

// This is to allow us to use memory leak checks.
google::protobuf::ShutdownProtobufLibrary();
caffe2::ShutdownProtobufLibrary();
return 0;
}
2 changes: 1 addition & 1 deletion binaries/run_plan_mpi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int main(int argc, char** argv) {
workspace->RunPlan(plan_def);

// This is to allow us to use memory leak checks.
google::protobuf::ShutdownProtobufLibrary();
caffe2::ShutdownProtobufLibrary();
MPI_Finalize();
return 0;
}
1 change: 0 additions & 1 deletion caffe2/contrib/aten/aten_op_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <caffe2/core/context.h>
#include <caffe2/core/operator.h>
#include <caffe2/utils/math.h>
#include <google/protobuf/text_format.h>
#include <iostream>

// a map from descriptor strings (see [DESCRIPTORS])
Expand Down
1 change: 0 additions & 1 deletion caffe2/core/graph_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

#include <google/protobuf/text_format.h>
#include <gtest/gtest.h>
#include "caffe2/core/graph.h"
#include "caffe2/core/net.h"
Expand Down
3 changes: 1 addition & 2 deletions caffe2/core/memonger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <unordered_set>

#include "caffe2/utils/proto_utils.h"
#include "google/protobuf/text_format.h"

namespace caffe2 {
namespace memonger {
Expand Down Expand Up @@ -305,7 +304,7 @@ class ComputeBlobRecyclingForDag {
} else {
NetDef step_net;
CAFFE_ENFORCE(
google::protobuf::TextFormat::ParseFromString(
TextFormat::ParseFromString(
arg->s(), &step_net),
"Could not parse step net:",
name);
Expand Down
3 changes: 1 addition & 2 deletions caffe2/core/net_gpu_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

#include <google/protobuf/text_format.h>
#include <gtest/gtest.h>
#include "caffe2/core/common_gpu.h"
#include "caffe2/core/net.h"
Expand Down Expand Up @@ -93,7 +92,7 @@ void checkChainingAndRun(
Workspace ws;
ws.CreateBlob("in");
NetDef net_def;
CAFFE_ENFORCE(google::protobuf::TextFormat::ParseFromString(spec, &net_def));
CAFFE_ENFORCE(TextFormat::ParseFromString(spec, &net_def));
{
net_def.set_num_workers(4);
auto old = FLAGS_caffe2_disable_chaining;
Expand Down
7 changes: 3 additions & 4 deletions caffe2/core/net_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

#include <google/protobuf/text_format.h>
#include <gtest/gtest.h>
#include "caffe2/core/net.h"
#include "caffe2/core/net_dag.h"
Expand Down Expand Up @@ -161,7 +160,7 @@ void checkChainingAndRun(
Workspace ws;
ws.CreateBlob("in");
NetDef net_def;
CAFFE_ENFORCE(google::protobuf::TextFormat::ParseFromString(spec, &net_def));
CAFFE_ENFORCE(TextFormat::ParseFromString(spec, &net_def));
{
net_def.set_num_workers(4);
auto old = FLAGS_caffe2_disable_chaining;
Expand All @@ -181,7 +180,7 @@ void checkNumChainsAndRun(const char* spec, const int expected_num_chains) {
Workspace ws;

NetDef net_def;
CAFFE_ENFORCE(google::protobuf::TextFormat::ParseFromString(spec, &net_def));
CAFFE_ENFORCE(TextFormat::ParseFromString(spec, &net_def));
net_def.set_num_workers(4);

// Create all external inputs
Expand Down Expand Up @@ -580,7 +579,7 @@ TEST(NetTest, FailingOperator) {
ws.CreateBlob("in");

NetDef net_def;
CAFFE_ENFORCE(google::protobuf::TextFormat::ParseFromString(spec, &net_def));
CAFFE_ENFORCE(TextFormat::ParseFromString(spec, &net_def));

{
net_def.set_num_workers(4);
Expand Down
1 change: 0 additions & 1 deletion caffe2/core/observer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

#include <google/protobuf/text_format.h>
#include <gtest/gtest.h>
#include "caffe2/core/common.h"
#include "caffe2/core/net.h"
Expand Down
3 changes: 1 addition & 2 deletions caffe2/core/parallel_net_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include "caffe2/core/net.h"
#include "caffe2/core/operator.h"
#include "google/protobuf/text_format.h"
#include <gtest/gtest.h>

namespace caffe2 {
Expand Down Expand Up @@ -104,7 +103,7 @@ namespace {
int RunNetAndGetDuration(const string& net_def_str, const string& type) {
NetDef net_def;
CAFFE_ENFORCE(
google::protobuf::TextFormat::ParseFromString(net_def_str, &net_def));
TextFormat::ParseFromString(net_def_str, &net_def));
net_def.set_type(type);
Workspace ws;
unique_ptr<NetBase> net(CreateNet(net_def, &ws));
Expand Down
5 changes: 2 additions & 3 deletions caffe2/core/predictor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

#include <google/protobuf/text_format.h>
#include "caffe2/core/context.h"
#include "caffe2/core/operator.h"
#include "caffe2/core/predictor.h"
Expand Down Expand Up @@ -158,7 +157,7 @@ std::unique_ptr<Blob> randomTensor(
NetDef parseNetDef(const std::string& value) {
NetDef def;
CAFFE_ENFORCE(
google::protobuf::TextFormat::ParseFromString(value, &def),
TextFormat::ParseFromString(value, &def),
"Failed to parse NetDef with value: ",
value);
return def;
Expand All @@ -167,7 +166,7 @@ NetDef parseNetDef(const std::string& value) {
MetaNetDef parseMetaNetDef(const std::string& value) {
MetaNetDef def;
CAFFE_ENFORCE(
google::protobuf::TextFormat::ParseFromString(value, &def),
TextFormat::ParseFromString(value, &def),
"Failed to parse NetDef with value: ",
value);
return def;
Expand Down
1 change: 0 additions & 1 deletion caffe2/core/transform_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

#include <google/protobuf/text_format.h>
#include <gtest/gtest.h>
#include "caffe2/core/net.h"
#include "caffe2/core/operator.h"
Expand Down
11 changes: 5 additions & 6 deletions caffe2/mpi/mpi_gpu_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "caffe2/core/net.h"
#include "caffe2/core/operator.h"
#include "caffe2/mpi/mpi_common.h"
#include "google/protobuf/text_format.h"
#include <gtest/gtest.h>

CAFFE2_DEFINE_string(
Expand Down Expand Up @@ -62,7 +61,7 @@ const char kBcastNet[] = R"NET(

TEST(MPITest, TestMPIBroadcast) {
NetDef net_def;
CHECK(google::protobuf::TextFormat::ParseFromString(
CHECK(TextFormat::ParseFromString(
string(kBcastNet), &net_def));
// Let's set the network's constant fill value to be the mpi rank.
auto* arg = net_def.mutable_op(1)->mutable_arg(1);
Expand Down Expand Up @@ -124,7 +123,7 @@ const char kReduceNet[] = R"NET(

TEST(MPITest, TestMPIReduce) {
NetDef net_def;
CHECK(google::protobuf::TextFormat::ParseFromString(
CHECK(TextFormat::ParseFromString(
string(kReduceNet), &net_def));
// Let's set the network's constant fill value to be the mpi rank.
auto* arg = net_def.mutable_op(1)->mutable_arg(1);
Expand Down Expand Up @@ -190,7 +189,7 @@ const char kMPIAllgatherNet[] = R"NET(

TEST(MPITest, TestMPIAllgather) {
NetDef net_def;
CHECK(google::protobuf::TextFormat::ParseFromString(
CHECK(TextFormat::ParseFromString(
string(kMPIAllgatherNet), &net_def));
// Let's set the network's constant fill value to be the mpi rank.
auto* arg = net_def.mutable_op(1)->mutable_arg(1);
Expand Down Expand Up @@ -253,7 +252,7 @@ const char kMPIAllreduceNet[] = R"NET(

TEST(MPITest, TestMPIAllreduce) {
NetDef net_def;
CHECK(google::protobuf::TextFormat::ParseFromString(
CHECK(TextFormat::ParseFromString(
string(kMPIAllreduceNet), &net_def));
// Let's set the network's constant fill value to be the mpi rank.
auto* arg = net_def.mutable_op(1)->mutable_arg(1);
Expand Down Expand Up @@ -315,7 +314,7 @@ const char kInPlaceMPIAllreduceNet[] = R"NET(

TEST(MPITest, TestInPlaceMPIAllreduce) {
NetDef net_def;
CHECK(google::protobuf::TextFormat::ParseFromString(
CHECK(TextFormat::ParseFromString(
string(kInPlaceMPIAllreduceNet), &net_def));
// Let's set the network's constant fill value to be the mpi rank.
auto* arg = net_def.mutable_op(1)->mutable_arg(1);
Expand Down
11 changes: 5 additions & 6 deletions caffe2/mpi/mpi_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "caffe2/core/net.h"
#include "caffe2/core/operator.h"
#include "caffe2/mpi/mpi_common.h"
#include "google/protobuf/text_format.h"
#include <gtest/gtest.h>

CAFFE2_DEFINE_string(
Expand Down Expand Up @@ -58,7 +57,7 @@ const char kBcastNet[] = R"NET(

TEST(MPITest, TestMPIBroadcast) {
NetDef net_def;
CHECK(google::protobuf::TextFormat::ParseFromString(
CHECK(TextFormat::ParseFromString(
string(kBcastNet), &net_def));
// Let's set the network's constant fill value to be the mpi rank.
auto* arg = net_def.mutable_op(1)->mutable_arg(1);
Expand Down Expand Up @@ -116,7 +115,7 @@ const char kReduceNet[] = R"NET(

TEST(MPITest, TestMPIReduce) {
NetDef net_def;
CHECK(google::protobuf::TextFormat::ParseFromString(
CHECK(TextFormat::ParseFromString(
string(kReduceNet), &net_def));
// Let's set the network's constant fill value to be the mpi rank.
auto* arg = net_def.mutable_op(1)->mutable_arg(1);
Expand Down Expand Up @@ -178,7 +177,7 @@ const char kMPIAllgatherNet[] = R"NET(

TEST(MPITest, TestMPIAllgather) {
NetDef net_def;
CHECK(google::protobuf::TextFormat::ParseFromString(
CHECK(TextFormat::ParseFromString(
string(kMPIAllgatherNet), &net_def));
// Let's set the network's constant fill value to be the mpi rank.
auto* arg = net_def.mutable_op(1)->mutable_arg(1);
Expand Down Expand Up @@ -236,7 +235,7 @@ const char kMPIAllreduceNet[] = R"NET(

TEST(MPITest, TestMPIAllreduce) {
NetDef net_def;
CHECK(google::protobuf::TextFormat::ParseFromString(
CHECK(TextFormat::ParseFromString(
string(kMPIAllreduceNet), &net_def));
// Let's set the network's constant fill value to be the mpi rank.
auto* arg = net_def.mutable_op(1)->mutable_arg(1);
Expand Down Expand Up @@ -293,7 +292,7 @@ const char kInPlaceMPIAllreduceNet[] = R"NET(

TEST(MPITest, TestInPlaceMPIAllreduce) {
NetDef net_def;
CHECK(google::protobuf::TextFormat::ParseFromString(
CHECK(TextFormat::ParseFromString(
string(kInPlaceMPIAllreduceNet), &net_def));
// Let's set the network's constant fill value to be the mpi rank.
auto* arg = net_def.mutable_op(1)->mutable_arg(1);
Expand Down
1 change: 0 additions & 1 deletion caffe2/observers/time_observer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "caffe2/core/operator.h"
#include "time_observer.h"

#include <google/protobuf/text_format.h>
#include <gtest/gtest.h>
#include <chrono>
#include <thread>
Expand Down
1 change: 0 additions & 1 deletion caffe2/onnx/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "device.h"
#include "onnx/onnx_pb.h"

#include <google/protobuf/text_format.h>
#include <functional>
#include <string>
#include <unordered_map>
Expand Down
1 change: 0 additions & 1 deletion caffe2/operators/recurrent_network_blob_fetcher_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "caffe2/core/operator.h"
#include "caffe2/core/tensor.h"
#include "caffe2/operators/recurrent_network_op.h"
#include "google/protobuf/text_format.h"

#include <string>

Expand Down
3 changes: 1 addition & 2 deletions caffe2/operators/recurrent_network_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "caffe2/utils/proto_utils.h"

#ifndef CAFFE2_RNN_NO_TEXT_FORMAT
#include <google/protobuf/text_format.h>
#endif

CAFFE2_DEFINE_bool(
Expand Down Expand Up @@ -265,7 +264,7 @@ NetDef extractNetDef(const OperatorDef& op, const std::string& argName) {
const auto netString =
ArgumentHelper::GetSingleArgument<OperatorDef, string>(op, argName, "");
CAFFE_ENFORCE(
google::protobuf::TextFormat::ParseFromString(netString, &result),
TextFormat::ParseFromString(netString, &result),
"Invalid NetDef");
return result;
#else
Expand Down
1 change: 0 additions & 1 deletion caffe2/transforms/common_subexpression_elimination_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

#include <google/protobuf/text_format.h>
#include <gtest/gtest.h>
#include "caffe2/core/net.h"
#include "caffe2/core/operator.h"
Expand Down
1 change: 0 additions & 1 deletion caffe2/transforms/conv_to_nnpack_transform_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

#include <google/protobuf/text_format.h>
#include <gtest/gtest.h>
#include "caffe2/core/net.h"
#include "caffe2/core/operator.h"
Expand Down
1 change: 0 additions & 1 deletion caffe2/transforms/pattern_net_transform_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

#include <google/protobuf/text_format.h>
#include <gtest/gtest.h>
#include "caffe2/core/net.h"
#include "caffe2/core/operator.h"
Expand Down
12 changes: 11 additions & 1 deletion caffe2/utils/proto_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@

#include "caffe2/core/logging.h"

using ::google::protobuf::Message;
using ::google::protobuf::MessageLite;

namespace caffe2 {

void ShutdownProtobufLibrary() {
::google::protobuf::ShutdownProtobufLibrary();
}

std::string DeviceTypeName(const int32_t& d) {
switch (d) {
case CPU:
Expand Down Expand Up @@ -145,6 +148,13 @@ using ::google::protobuf::io::ZeroCopyInputStream;
using ::google::protobuf::io::CodedInputStream;
using ::google::protobuf::io::ZeroCopyOutputStream;
using ::google::protobuf::io::CodedOutputStream;
using ::google::protobuf::Message;

namespace TextFormat {
bool ParseFromString(const string& spec, Message* proto) {
return ::google::protobuf::TextFormat::ParseFromString(spec, proto);
}
} // namespace TextFormat

bool ReadProtoFromTextFile(const char* filename, Message* proto) {
int fd = open(filename, O_RDONLY);
Expand Down
Loading

0 comments on commit 611a89c

Please sign in to comment.