未验证 提交 a8fff38f 编写于 作者: Y Yuanle Liu 提交者: GitHub

fix gflags from environment not activated (#50864)

上级 75a2f9d5
......@@ -16,6 +16,7 @@
#include <string>
#include <tuple>
#include "paddle/fluid/inference/api/helper.h"
#include "paddle/fluid/inference/api/paddle_analysis_config.h"
#include "paddle/fluid/inference/api/paddle_pass_builder.h"
#include "paddle/fluid/inference/utils/table_printer.h"
......@@ -40,6 +41,12 @@ extern const std::vector<std::string> kTRTSubgraphPasses;
extern const std::vector<std::string> kDlnneSubgraphPasses;
extern const std::vector<std::string> kLiteSubgraphPasses;
AnalysisConfig::AnalysisConfig() {
// NOTE(liuyuanle): Why put the following code here?
// ref to https://github.com/PaddlePaddle/Paddle/pull/50864
inference::InitGflagsFromEnv();
}
PassStrategy *AnalysisConfig::pass_builder() const {
if (!pass_builder_.get()) {
if (use_gpu_) {
......
......@@ -1497,32 +1497,6 @@ CreatePaddlePredictor<AnalysisConfig, PaddleEngineKind::kAnalysis>(
if (std::getenv("FLAGS_initial_cpu_memory_in_mb") == nullptr) {
SetGflag("initial_cpu_memory_in_mb", "0");
}
// support set gflags from environment.
std::vector<std::string> gflags;
const phi::ExportedFlagInfoMap &env_map = phi::GetExportedFlagInfoMap();
std::ostringstream os;
for (auto &pair : env_map) {
os << pair.second.name << ",";
}
std::string tryfromenv_str = os.str();
if (!tryfromenv_str.empty()) {
tryfromenv_str.pop_back();
tryfromenv_str = "--tryfromenv=" + tryfromenv_str;
gflags.push_back(tryfromenv_str);
}
if (framework::InitGflags(gflags)) {
VLOG(3)
<< "The following gpu analysis configurations only take effect "
"for the first predictor: ";
for (const auto &gflag : gflags) {
VLOG(3) << gflag;
}
} else {
LOG(WARNING) << "The one-time configuration of analysis predictor "
"failed, which may be due to native predictor called "
"first and its configurations taken effect.";
}
});
if (config.thread_local_stream_enabled() &&
......
......@@ -16,6 +16,7 @@
#include "paddle/fluid/framework/custom_operator.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/platform/init.h"
#include "paddle/phi/api/ext/op_meta_info.h"
namespace paddle {
......@@ -59,5 +60,22 @@ void RegisterAllCustomOperator() {
}
}
void InitGflagsFromEnv() {
// support set gflags from environment.
std::vector<std::string> gflags;
const phi::ExportedFlagInfoMap &env_map = phi::GetExportedFlagInfoMap();
std::ostringstream os;
for (auto &pair : env_map) {
os << pair.second.name << ",";
}
std::string tryfromenv_str = os.str();
if (!tryfromenv_str.empty()) {
tryfromenv_str.pop_back();
tryfromenv_str = "--tryfromenv=" + tryfromenv_str;
gflags.push_back(tryfromenv_str);
}
framework::InitGflags(gflags);
}
} // namespace inference
} // namespace paddle
......@@ -432,6 +432,8 @@ static bool IsFileExists(const std::string &path) {
void RegisterAllCustomOperator();
void InitGflagsFromEnv();
static inline double ToMegaBytes(size_t bytes) {
return static_cast<double>(bytes) / (1 << 20);
}
......
......@@ -138,7 +138,7 @@ struct DistConfig {
/// and loading it into AnalysisPredictor.
///
struct PD_INFER_DECL AnalysisConfig {
AnalysisConfig() = default;
AnalysisConfig();
///
/// \brief Construct a new AnalysisConfig from another
/// AnalysisConfig.
......
......@@ -85,7 +85,6 @@ namespace framework {
std::once_flag gflags_init_flag;
std::once_flag glog_init_flag;
std::once_flag npu_init_flag;
std::once_flag memory_method_init_flag;
bool InitGflags(std::vector<std::string> args) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册