Skip to content

Commit 1221412

Browse files
sidgoyal78abhinavarora
authored andcommitted
Fix cpplint for print_op (#10070)
* Fix print op cpplint errors * Remove commented code
1 parent 8113de9 commit 1221412

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

paddle/fluid/operators/print_op.cc

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ namespace operators {
2323

2424
#define CLOG std::cout
2525

26-
const std::string kForward = "FORWARD";
27-
const std::string kBackward = "BACKWARD";
28-
const std::string kBoth = "BOTH";
26+
const char kForward[] = "FORWARD";
27+
const char kBackward[] = "BACKWARD";
28+
const char kBoth[] = "BOTH";
2929

3030
struct Formater {
3131
std::string message;
3232
std::string name;
3333
std::vector<int> dims;
34-
std::type_index dtype{typeid(char)};
34+
std::type_index dtype{typeid(const char)};
3535
framework::LoD lod;
3636
int summarize;
3737
void* data{nullptr};
@@ -62,7 +62,7 @@ struct Formater {
6262
}
6363
}
6464
void PrintDtype() {
65-
if (dtype.hash_code() != typeid(char).hash_code()) {
65+
if (dtype.hash_code() != typeid(const char).hash_code()) {
6666
CLOG << "\tdtype: " << dtype.name() << std::endl;
6767
}
6868
}
@@ -83,15 +83,15 @@ struct Formater {
8383
void PrintData(size_t size) {
8484
PADDLE_ENFORCE_NOT_NULL(data);
8585
// print float
86-
if (dtype.hash_code() == typeid(float).hash_code()) {
86+
if (dtype.hash_code() == typeid(const float).hash_code()) {
8787
Display<float>(size);
88-
} else if (dtype.hash_code() == typeid(double).hash_code()) {
88+
} else if (dtype.hash_code() == typeid(const double).hash_code()) {
8989
Display<double>(size);
90-
} else if (dtype.hash_code() == typeid(int).hash_code()) {
90+
} else if (dtype.hash_code() == typeid(const int).hash_code()) {
9191
Display<int>(size);
92-
} else if (dtype.hash_code() == typeid(int64_t).hash_code()) {
92+
} else if (dtype.hash_code() == typeid(const int64_t).hash_code()) {
9393
Display<int64_t>(size);
94-
} else if (dtype.hash_code() == typeid(bool).hash_code()) {
94+
} else if (dtype.hash_code() == typeid(const bool).hash_code()) {
9595
Display<bool>(size);
9696
} else {
9797
CLOG << "\tdata: unprintable type: " << dtype.name() << std::endl;
@@ -100,7 +100,7 @@ struct Formater {
100100

101101
template <typename T>
102102
void Display(size_t size) {
103-
auto* d = (T*)data;
103+
auto* d = reinterpret_cast<T*>(data);
104104
CLOG << "\tdata: ";
105105
if (summarize != -1) {
106106
summarize = std::min(size, (size_t)summarize);
@@ -135,7 +135,7 @@ class TensorPrintOp : public framework::OperatorBase {
135135
void RunImpl(const framework::Scope& scope,
136136
const platform::Place& place) const override {
137137
const framework::Variable* in_var_ptr = nullptr;
138-
std::string phase = kForward;
138+
std::string phase(kForward);
139139
std::string printed_var_name = "";
140140

141141
auto& inputs = Inputs();
@@ -146,7 +146,7 @@ class TensorPrintOp : public framework::OperatorBase {
146146
!Inputs("In@GRAD").empty()) {
147147
in_var_ptr = scope.FindVar(Input("In@GRAD"));
148148
printed_var_name = Inputs("In@GRAD").front();
149-
phase = kBackward;
149+
phase = std::string(kBackward);
150150
} else {
151151
PADDLE_THROW("Unknown phase, should be forward or backward.");
152152
}
@@ -163,7 +163,7 @@ class TensorPrintOp : public framework::OperatorBase {
163163
out_tensor.set_lod(in_tensor.lod());
164164

165165
std::string print_phase = Attr<std::string>("print_phase");
166-
if (print_phase != phase && print_phase != kBoth) {
166+
if (print_phase != phase && print_phase != std::string(kBoth)) {
167167
return;
168168
}
169169

@@ -199,7 +199,7 @@ class TensorPrintOp : public framework::OperatorBase {
199199
formater.lod = printed_tensor.lod();
200200
}
201201
formater.summarize = Attr<int>("summarize");
202-
formater.data = (void*)printed_tensor.data<void>();
202+
formater.data = reinterpret_cast<void*>(printed_tensor.data<void>());
203203
formater(printed_tensor.numel());
204204
}
205205

@@ -223,8 +223,9 @@ class PrintOpProtoAndCheckMaker : public framework::OpProtoAndCheckerMaker {
223223
"print_phase",
224224
"(string, default 'BOTH') Which phase to display including 'FORWARD' "
225225
"'BACKWARD' and 'BOTH'.")
226-
.SetDefault(kBoth)
227-
.InEnum({kForward, kBackward, kBoth});
226+
.SetDefault(std::string(kBoth))
227+
.InEnum({std::string(kForward), std::string(kBackward),
228+
std::string(kBoth)});
228229
AddOutput("Out", "Output tensor with same data as input tensor.");
229230
AddComment(R"DOC(
230231
Creates a print op that will print when a tensor is accessed.

0 commit comments

Comments
 (0)