Skip to content

Commit 857cd9f

Browse files
author
Pei Yang
authored
make config option DisableGlogInfo() able to mute all inference logs (#21544)
make config option DisableGlogInfo() able to mute all inference logs
1 parent 87a8caa commit 857cd9f

File tree

9 files changed

+31
-14
lines changed

9 files changed

+31
-14
lines changed

paddle/fluid/framework/naive_executor.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include <memory>
1516
#include <string>
17+
#include <utility>
1618
#include <vector>
1719

1820
#include "paddle/fluid/framework/feed_fetch_method.h"
@@ -100,9 +102,8 @@ void NaiveExecutor::CreateOps(const ProgramDesc &desc, int block_id,
100102
for (const auto &op_desc : desc.Block(block_id).AllOps()) {
101103
if (!with_feed_fetch_ops &&
102104
(op_desc->Type() == "feed" || op_desc->Type() == "fetch")) {
103-
string::PrettyLogEndl(string::Style::detail(), "--- skip [%s], %s -> %s",
104-
op_desc->Input("X")[0], op_desc->Type(),
105-
op_desc->Output("Out")[0]);
105+
LOG(INFO) << "--- skip [" << op_desc->Input("X")[0] << "], "
106+
<< op_desc->Type() << " -> " << op_desc->Output("Out")[0];
106107
continue;
107108
}
108109
ops_.emplace_back(OpRegistry::CreateOp(*op_desc));

paddle/fluid/inference/analysis/analyzer.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ void Analyzer::Run(Argument *argument) { RunAnalysis(argument); }
2929
void Analyzer::RunAnalysis(Argument *argument) {
3030
PADDLE_ENFORCE(argument->analysis_passes_valid(),
3131
"analsis_passes is not valid in the argument.");
32+
const bool disable_logs = argument->disable_logs();
3233
for (auto &pass : argument->analysis_passes()) {
33-
string::PrettyLogH1("--- Running analysis [%s]", pass);
34+
if (!disable_logs) {
35+
string::PrettyLogH1("--- Running analysis [%s]", pass);
36+
}
3437
if (!argument->enable_analysis_optim() && pass == "ir_analysis_pass")
3538
continue;
3639

paddle/fluid/inference/analysis/analyzer_tester.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ using namespace framework; // NOLINT
2929

3030
TEST(Analyzer, analysis_without_tensorrt) {
3131
Argument argument;
32+
argument.SetDisableLogs(false);
3233
argument.SetModelDir(FLAGS_inference_model_dir);
3334
argument.SetEnableAnalysisOptim(false);
3435
argument.SetUseGPU(false);
@@ -41,6 +42,7 @@ TEST(Analyzer, analysis_without_tensorrt) {
4142

4243
TEST(Analyzer, analysis_with_tensorrt) {
4344
Argument argument;
45+
argument.SetDisableLogs(false);
4446
argument.SetEnableAnalysisOptim(false);
4547
argument.SetTensorRtMaxBatchSize(3);
4648
argument.SetTensorRtWorkspaceSize(1 << 20);

paddle/fluid/inference/analysis/argument.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ struct Argument {
149149
DECL_ARGUMENT_FIELD(analysis_passes, AnalysisPasses,
150150
std::vector<std::string>);
151151

152+
// whether to mute all logs in inference.
153+
DECL_ARGUMENT_FIELD(disable_logs, DisableLogs, bool);
154+
152155
// Pass a set of op types to enable its mkldnn kernel
153156
DECL_ARGUMENT_FIELD(mkldnn_enabled_op_types, MKLDNNEnabledOpTypes,
154157
std::unordered_set<std::string>);

paddle/fluid/inference/analysis/ir_pass_manager.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ void IRPassManager::CreatePasses(Argument *argument,
147147
pass->Set("auto_config_layout",
148148
new bool(argument->anakin_auto_config_layout()));
149149
}
150+
disable_logs_ = argument->disable_logs();
151+
if (pass_name == "fc_fuse_pass") {
152+
pass->Set("use_gpu", new bool(argument->use_gpu()));
153+
}
150154

151155
pre_pass = pass_name;
152156

@@ -161,7 +165,7 @@ std::unique_ptr<Graph> IRPassManager::Apply(std::unique_ptr<Graph> graph) {
161165
PADDLE_ENFORCE(graph.get());
162166
// Apply all the passes
163167
for (const auto &pass : passes_) {
164-
if (pass->Type() != "graph_viz_pass") {
168+
if (pass->Type() != "graph_viz_pass" && !disable_logs_) {
165169
PrettyLogEndl(Style::H2(), "--- Running IR pass [%s]", pass->Type());
166170
}
167171
graph.reset(pass->Apply(graph.release()));

paddle/fluid/inference/analysis/ir_pass_manager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class IRPassManager final {
5656

5757
std::unique_ptr<Graph> graph_;
5858
std::vector<std::unique_ptr<framework::ir::Pass>> passes_;
59+
bool disable_logs_{false};
5960
};
6061

6162
} // namespace analysis

paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ void TensorRtSubgraphPass::CreateTensorRTOp(
108108
framework::BlockDesc block_desc(nullptr, &block_proto);
109109
block_desc.Proto()->set_parent_idx(-1);
110110
block_desc.Proto()->set_idx(0);
111-
string::PrettyLogDetail("--- detect a sub-graph with %d nodes",
112-
subgraph.size());
111+
LOG(INFO) << "--- detect a sub-graph with " << subgraph.size() << " nodes";
113112

114113
for (auto *node : subgraph) {
115114
auto *new_block_op = new_block->AppendOp();

paddle/fluid/inference/api/analysis_predictor.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ void AnalysisPredictor::PrepareArgument() {
451451
passes.clear();
452452
LOG(INFO) << "ir_optim is turned off, no IR pass will be executed";
453453
}
454+
argument_.SetDisableLogs(config_.glog_info_disabled());
454455
argument_.SetIrAnalysisPasses(passes);
455456
argument_.SetAnalysisPasses(config_.pass_builder()->AnalysisPasses());
456457
argument_.SetScopeNotOwned(scope_.get());
@@ -507,10 +508,10 @@ std::unique_ptr<PaddlePredictor> CreatePaddlePredictor<
507508
framework::InitGflags(flags);
508509
}
509510
}
511+
framework::InitGLOG("");
510512
if (config.glog_info_disabled()) {
511513
FLAGS_logtostderr = 1;
512-
FLAGS_minloglevel = google::WARNING;
513-
LOG(WARNING) << " - GLOG's LOG(INFO) is disabled.";
514+
FLAGS_minloglevel = 2; // GLOG_ERROR
514515
}
515516

516517
std::unique_ptr<PaddlePredictor> predictor(new AnalysisPredictor(config));

paddle/fluid/platform/init.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace framework {
4646
#endif
4747

4848
std::once_flag gflags_init_flag;
49+
std::once_flag glog_init_flag;
4950
std::once_flag p2p_init_flag;
5051

5152
void InitGflags(std::vector<std::string> argv) {
@@ -213,13 +214,15 @@ void SignalHandle(const char *data, int size) {
213214
#endif
214215

215216
void InitGLOG(const std::string &prog_name) {
216-
// glog will not hold the ARGV[0] inside.
217-
// Use strdup to alloc a new string.
218-
google::InitGoogleLogging(strdup(prog_name.c_str()));
217+
std::call_once(glog_init_flag, [&]() {
218+
// glog will not hold the ARGV[0] inside.
219+
// Use strdup to alloc a new string.
220+
google::InitGoogleLogging(strdup(prog_name.c_str()));
219221
#ifndef _WIN32
220-
google::InstallFailureSignalHandler();
221-
google::InstallFailureWriter(&SignalHandle);
222+
google::InstallFailureSignalHandler();
223+
google::InstallFailureWriter(&SignalHandle);
222224
#endif
225+
});
223226
}
224227

225228
} // namespace framework

0 commit comments

Comments
 (0)