Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
baoachun committed Mar 16, 2022
1 parent d3fa155 commit 11862d4
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 23 deletions.
15 changes: 9 additions & 6 deletions paddle/fluid/inference/api/analysis_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ void AnalysisConfig::DisableGpu() {
Update();
}

void AnalysisConfig::EnableUseGpuFp16() {
void AnalysisConfig::Exp_EnableUseGpuFp16(
std::unordered_set<std::string> op_list) {
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
use_gpu_fp16_ = true;
gpu_fp16_disabled_op_types_.insert(op_list.begin(), op_list.end());
#else
LOG(ERROR) << "Please compile with gpu to EnableUseGpuFp16()";
LOG(ERROR) << "Please compile with gpu to Exp_EnableUseGpuFp16()";
use_gpu_fp16_ = false;
#endif

Expand Down Expand Up @@ -591,12 +593,13 @@ void AnalysisConfig::Update() {
if (use_gpu_fp16_) {
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
if (!enable_ir_optim_) {
LOG(ERROR)
<< "EnableUseGpuFp16() only works when IR optimization is enabled.";
LOG(ERROR) << "Exp_EnableUseGpuFp16() only works when IR optimization is "
"enabled.";
} else if (!use_gpu()) {
LOG(ERROR) << "EnableUseGpuFp16() only works when use_gpu is enabled.";
LOG(ERROR)
<< "Exp_EnableUseGpuFp16() only works when use_gpu is enabled.";
} else {
pass_builder()->EnableUseGpuFp16();
pass_builder()->Exp_EnableUseGpuFp16();
}
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/inference/api/analysis_predictor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ void AnalysisPredictor::PrepareArgument() {
argument_.SetDlnneMinSubgraphSize(config_.dlnne_min_subgraph_size_);
}

if (config_.use_gpu_fp16()) {
if (config_.gpu_fp16_enabled()) {
argument_.SetUseGPUFp16(true);
argument_.SetGpuFp16DisabledOpTypes(config_.gpu_fp16_disabled_op_types_);
}
Expand Down
26 changes: 26 additions & 0 deletions paddle/fluid/inference/api/analysis_predictor_tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,19 @@ TEST(AnalysisPredictor, enable_onnxruntime) {
ASSERT_TRUE(!config.use_onnxruntime());
}

TEST(AnalysisPredictor, exp_enable_use_gpu_fp16) {
AnalysisConfig config;
config.SwitchIrOptim();
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
config.EnableUseGpu(100, 0);
config.Exp_EnableUseGpuFp16();
ASSERT_TRUE(config.gpu_fp16_enabled());
#else
config.DisableGpu();
#endif
LOG(INFO) << config.Summary();
}

} // namespace paddle

namespace paddle_infer {
Expand Down Expand Up @@ -434,6 +447,19 @@ TEST(Predictor, EnableONNXRuntime) {
auto predictor = CreatePredictor(config);
}

TEST(Predictor, Exp_EnableUseGpuFp16) {
Config config;
config.SetModel(FLAGS_dirname);
config.SwitchIrOptim();
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
config.EnableUseGpu(100, 0);
config.Exp_EnableUseGpuFp16();
#else
config.DisableGpu();
#endif
auto predictor = CreatePredictor(config);
}

TEST(Tensor, CpuShareExternalData) {
Config config;
config.SetModel(FLAGS_dirname);
Expand Down
14 changes: 3 additions & 11 deletions paddle/fluid/inference/api/paddle_analysis_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,26 +254,18 @@ struct PD_INFER_DECL AnalysisConfig {
///
void DisableGpu();
///
/// \brief Turn on GPU fp16 precision.
///
///
void EnableUseGpuFp16();
///
/// \brief Specify the operator type list to keep original calculation
/// precision.
/// \brief Enable GPU fp16 precision computation, in experimental state.
///
/// \param op_list The operator type list.
///
void SetGpuFp16DisabledOp(std::unordered_set<std::string> op_list) {
gpu_fp16_disabled_op_types_.insert(op_list.begin(), op_list.end());
}
void Exp_EnableUseGpuFp16(std::unordered_set<std::string> op_list = {});
///
/// \brief A boolean state telling whether the GPU fp16 precision is turned
/// on.
///
/// \return bool Whether the GPU fp16 precision is turned on.
///
bool use_gpu_fp16() const { return use_gpu_fp16_; }
bool gpu_fp16_enabled() const { return use_gpu_fp16_; }

///
/// \brief Turn on XPU.
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/inference/api/paddle_pass_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void GpuPassStrategy::EnableCUDNN() {
use_cudnn_ = true;
}

void GpuPassStrategy::EnableUseGpuFp16() {
void GpuPassStrategy::Exp_EnableUseGpuFp16() {
passes_.assign({
"is_test_pass", //
"simplify_with_basic_ops_pass", //
Expand Down
4 changes: 2 additions & 2 deletions paddle/fluid/inference/api/paddle_pass_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class PD_INFER_DECL PassStrategy : public PaddlePassBuilder {
virtual void EnableCUDNN() {}

/// \brief Enable use gpu fp16 kernel.
virtual void EnableUseGpuFp16() {}
virtual void Exp_EnableUseGpuFp16() {}

/// \brief Enable the use of MKLDNN.
/// The MKLDNN control exists in both CPU and GPU mode, because there can
Expand Down Expand Up @@ -232,7 +232,7 @@ class PD_INFER_DECL GpuPassStrategy : public PassStrategy {
void EnableCUDNN() override;

/// \brief Enable the use of gpu fp16 kernel.
void EnableUseGpuFp16() override;
void Exp_EnableUseGpuFp16() override;

/// \brief Not supported in GPU mode yet.
void EnableMKLDNN() override;
Expand Down
5 changes: 3 additions & 2 deletions paddle/fluid/pybind/inference_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,9 @@ void BindAnalysisConfig(py::module *m) {
.def("params_file", &AnalysisConfig::params_file)
.def("enable_use_gpu", &AnalysisConfig::EnableUseGpu,
py::arg("memory_pool_init_size_mb"), py::arg("device_id") = 0)
.def("enable_use_gpu_fp16", &AnalysisConfig::EnableUseGpuFp16)
.def("set_gpu_fp16_disabled_op", &AnalysisConfig::SetGpuFp16DisabledOp)
.def("exp_enable_use_gpu_fp16", &AnalysisConfig::Exp_EnableUseGpuFp16,
py::arg("gpu_fp16_disabled_op_types") =
std::unordered_set<std::string>({}))
.def("enable_xpu", &AnalysisConfig::EnableXpu,
py::arg("l3_workspace_size") = 16 * 1024 * 1024,
py::arg("locked") = false, py::arg("autotune") = true,
Expand Down

1 comment on commit 11862d4

@paddle-bot-old
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulation! Your pull request passed all required CI. You could ask reviewer(s) to approve and merge. 🎉

Please sign in to comment.