Skip to content

Commit 97b7502

Browse files
authored
inference API little fix (#11069)
1 parent f437c46 commit 97b7502

File tree

6 files changed

+75
-113
lines changed

6 files changed

+75
-113
lines changed

paddle/contrib/inference/CMakeLists.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function(inference_api_test TARGET_NAME TEST_SRC)
3636
string(REGEX REPLACE "^_$" "" arg "${arg}")
3737
cc_test(${TARGET_NAME}
3838
SRCS ${TEST_SRC}
39-
DEPS paddle_fluid_api paddle_inference_api paddle_inference_api_impl
39+
DEPS paddle_fluid_api paddle_inference_api
4040
ARGS --dirname=${PYTHON_TESTS_DIR}/book/)
4141
# TODO(panyx0178): Figure out how to add word2vec and image_classification
4242
# as deps.
@@ -47,13 +47,9 @@ endfunction(inference_api_test)
4747

4848

4949
cc_library(paddle_inference_api
50-
SRCS paddle_inference_api.cc
50+
SRCS paddle_inference_api.cc paddle_inference_api_impl.cc
5151
DEPS ${FLUID_CORE_MODULES} ${GLOB_OP_LIB})
5252

53-
cc_library(paddle_inference_api_impl
54-
SRCS paddle_inference_api_impl.cc
55-
DEPS paddle_inference_api paddle_fluid_api)
56-
5753
cc_test(test_paddle_inference_api
5854
SRCS test_paddle_inference_api.cc
5955
DEPS paddle_inference_api)

paddle/contrib/inference/paddle_inference_api.h

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ struct PaddleTensor {
4545
};
4646

4747
/*
48-
* A simple Inference API for Paddle. Currently this API might just be used by
49-
* non-sequence scenerios.
50-
* TODO(Superjomn) Prepare another API for NLP-related usages.
51-
*/
48+
* A simple Inference API for Paddle. Currently this API can be used by
49+
* non-sequence scenerios.
50+
* TODO(Superjomn) Support another API for NLP-related usages.
51+
*/
5252
class PaddlePredictor {
5353
public:
5454
struct Config;
@@ -66,34 +66,38 @@ class PaddlePredictor {
6666
// be thread-safe.
6767
virtual std::unique_ptr<PaddlePredictor> Clone() = 0;
6868

69-
virtual bool InitShared() { return false; }
7069
// Destroy the Predictor.
7170
virtual ~PaddlePredictor() {}
7271

73-
friend std::unique_ptr<PaddlePredictor> CreatePaddlePredictor(
74-
const PaddlePredictor::Config& config);
72+
enum class EngineKind {
73+
kNative = -1, // Use the native Fluid facility.
74+
// TODO(Superjomn) support latter.
75+
// kAnakin, // Use Anakin for inference.
76+
// kTensorRT, // Use TensorRT for inference.
77+
// kAutoMixedAnakin, // Automatically mix Fluid with Anakin.
78+
// kAutoMixedTensorRT, // Automatically mix Fluid with TensorRT.
79+
};
7580

7681
// The common configs for all the predictors.
7782
struct Config {
78-
enum class EngineKind;
79-
8083
std::string model_dir; // path to the model directory.
8184
bool enable_engine{false}; // Enable to execute (part of) the model on
82-
// third-party engines.
83-
EngineKind engine_kind{Config::EngineKind::kNone};
84-
85-
enum class EngineKind {
86-
kNone = -1, // Use the native Fluid facility.
87-
kAnakin, // Use Anakin for inference.
88-
kTensorRT, // Use TensorRT for inference.
89-
kAutoMixedAnakin, // Automatically mix Fluid with Anakin.
90-
kAutoMixedTensorRT, // Automatically mix Fluid with TensorRT.
91-
};
9285
};
9386
};
9487

88+
struct NativeConfig : public PaddlePredictor::Config {
89+
bool use_gpu{false};
90+
int device;
91+
float fraction_of_gpu_memory;
92+
std::string prog_file;
93+
std::string param_file;
94+
bool share_variables;
95+
};
96+
9597
// A factory to help create difference predictor.
96-
template <typename ConfigT>
98+
template <
99+
typename ConfigT,
100+
PaddlePredictor::EngineKind engine = PaddlePredictor::EngineKind::kNative>
97101
std::unique_ptr<PaddlePredictor> CreatePaddlePredictor(const ConfigT& config);
98102

99103
} // namespace paddle

paddle/contrib/inference/paddle_inference_api_impl.cc

Lines changed: 29 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ std::string num2str(T a) {
5454
}
5555
} // namespace
5656

57-
bool PaddlePredictorImpl::Init() {
57+
bool NativePaddlePredictor::Init() {
5858
VLOG(3) << "Predictor::init()";
5959

6060
// TODO(panyx0718): Should CPU vs GPU device be decided by id?
@@ -96,8 +96,8 @@ bool PaddlePredictorImpl::Init() {
9696
return true;
9797
}
9898

99-
bool PaddlePredictorImpl::Run(const std::vector<PaddleTensor> &inputs,
100-
std::vector<PaddleTensor> *output_data) {
99+
bool NativePaddlePredictor::Run(const std::vector<PaddleTensor> &inputs,
100+
std::vector<PaddleTensor> *output_data) {
101101
VLOG(3) << "Predictor::predict";
102102
Timer timer;
103103
timer.tic();
@@ -133,59 +133,20 @@ bool PaddlePredictorImpl::Run(const std::vector<PaddleTensor> &inputs,
133133
return true;
134134
}
135135

136-
std::unique_ptr<PaddlePredictor> PaddlePredictorImpl::Clone() {
136+
std::unique_ptr<PaddlePredictor> NativePaddlePredictor::Clone() {
137137
VLOG(3) << "Predictor::clone";
138-
std::unique_ptr<PaddlePredictor> cls(new PaddlePredictorImpl(config_));
139-
if (!cls->InitShared()) {
140-
LOG(ERROR) << "fail to call InitShared";
138+
std::unique_ptr<PaddlePredictor> cls(new NativePaddlePredictor(config_));
139+
140+
if (!dynamic_cast<NativePaddlePredictor *>(cls.get())->Init()) {
141+
LOG(ERROR) << "fail to call Init";
141142
return nullptr;
142143
}
143144
// fix manylinux compile error.
144145
return std::move(cls);
145146
}
146147

147-
// TODO(panyx0718): Consider merge with Init()?
148-
bool PaddlePredictorImpl::InitShared() {
149-
VLOG(3) << "Predictor::init_shared";
150-
// 1. Define place, executor, scope
151-
if (this->config_.device >= 0) {
152-
place_ = platform::CUDAPlace();
153-
} else {
154-
place_ = platform::CPUPlace();
155-
}
156-
this->executor_.reset(new framework::Executor(this->place_));
157-
this->scope_.reset(new framework::Scope());
158-
// Initialize the inference program
159-
if (!this->config_.model_dir.empty()) {
160-
// Parameters are saved in separate files sited in
161-
// the specified `dirname`.
162-
this->inference_program_ = inference::Load(
163-
this->executor_.get(), this->scope_.get(), this->config_.model_dir);
164-
} else if (!this->config_.prog_file.empty() &&
165-
!this->config_.param_file.empty()) {
166-
// All parameters are saved in a single file.
167-
// The file names should be consistent with that used
168-
// in Python API `fluid.io.save_inference_model`.
169-
this->inference_program_ = inference::Load(this->executor_.get(),
170-
this->scope_.get(),
171-
this->config_.prog_file,
172-
this->config_.param_file);
173-
}
174-
this->ctx_ = this->executor_->Prepare(*this->inference_program_, 0);
175-
// 3. create variables
176-
// TODO(panyx0718): why test share_variables.
177-
if (config_.share_variables) {
178-
this->executor_->CreateVariables(
179-
*this->inference_program_, this->scope_.get(), 0);
180-
}
181-
// 4. Get the feed_target_names and fetch_target_names
182-
this->feed_target_names_ = this->inference_program_->GetFeedTargetNames();
183-
this->fetch_target_names_ = this->inference_program_->GetFetchTargetNames();
184-
return true;
185-
}
186-
187-
bool PaddlePredictorImpl::SetFeed(const std::vector<PaddleTensor> &inputs,
188-
std::vector<framework::LoDTensor> *feeds) {
148+
bool NativePaddlePredictor::SetFeed(const std::vector<PaddleTensor> &inputs,
149+
std::vector<framework::LoDTensor> *feeds) {
189150
VLOG(3) << "Predictor::set_feed";
190151
if (inputs.size() != feed_target_names_.size()) {
191152
LOG(ERROR) << "wrong feed input size.";
@@ -213,7 +174,7 @@ bool PaddlePredictorImpl::SetFeed(const std::vector<PaddleTensor> &inputs,
213174
return true;
214175
}
215176

216-
bool PaddlePredictorImpl::GetFetch(
177+
bool NativePaddlePredictor::GetFetch(
217178
const std::vector<framework::LoDTensor> &fetchs,
218179
std::vector<PaddleTensor> *outputs) {
219180
VLOG(3) << "Predictor::get_fetch";
@@ -280,23 +241,26 @@ bool PaddlePredictorImpl::GetFetch(
280241
}
281242

282243
template <>
283-
std::unique_ptr<PaddlePredictor> CreatePaddlePredictor(
284-
const ConfigImpl &config) {
285-
VLOG(3) << "create PaddlePredictorImpl";
286-
// 1. GPU memeroy
287-
std::vector<std::string> flags;
288-
if (config.fraction_of_gpu_memory >= 0.0f ||
289-
config.fraction_of_gpu_memory <= 0.95f) {
290-
flags.push_back("dummpy");
291-
std::string flag = "--fraction_of_gpu_memory_to_use=" +
292-
num2str<float>(config.fraction_of_gpu_memory);
293-
flags.push_back(flag);
294-
VLOG(3) << "set flag: " << flag;
295-
framework::InitGflags(flags);
244+
std::unique_ptr<PaddlePredictor>
245+
CreatePaddlePredictor<NativeConfig, PaddlePredictor::EngineKind::kNative>(
246+
const NativeConfig &config) {
247+
VLOG(3) << "create NativePaddlePredictor";
248+
if (config.use_gpu) {
249+
// 1. GPU memeroy
250+
std::vector<std::string> flags;
251+
if (config.fraction_of_gpu_memory >= 0.0f ||
252+
config.fraction_of_gpu_memory <= 0.95f) {
253+
flags.push_back("dummpy");
254+
std::string flag = "--fraction_of_gpu_memory_to_use=" +
255+
num2str<float>(config.fraction_of_gpu_memory);
256+
flags.push_back(flag);
257+
VLOG(3) << "set flag: " << flag;
258+
framework::InitGflags(flags);
259+
}
296260
}
297261

298-
std::unique_ptr<PaddlePredictor> predictor(new PaddlePredictorImpl(config));
299-
if (!dynamic_cast<PaddlePredictorImpl *>(predictor.get())->Init()) {
262+
std::unique_ptr<PaddlePredictor> predictor(new NativePaddlePredictor(config));
263+
if (!dynamic_cast<NativePaddlePredictor *>(predictor.get())->Init()) {
300264
return nullptr;
301265
}
302266
return std::move(predictor);

paddle/contrib/inference/paddle_inference_api_impl.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,10 @@
2929

3030
namespace paddle {
3131

32-
struct ConfigImpl : public PaddlePredictor::Config {
33-
int device;
34-
float fraction_of_gpu_memory;
35-
std::string prog_file;
36-
std::string param_file;
37-
bool share_variables;
38-
};
39-
40-
class PaddlePredictorImpl : public PaddlePredictor {
32+
class NativePaddlePredictor : public PaddlePredictor {
4133
public:
42-
explicit PaddlePredictorImpl(const ConfigImpl &config) : config_(config) {}
34+
explicit NativePaddlePredictor(const NativeConfig &config)
35+
: config_(config) {}
4336

4437
bool Init();
4538

@@ -48,16 +41,15 @@ class PaddlePredictorImpl : public PaddlePredictor {
4841

4942
std::unique_ptr<PaddlePredictor> Clone() override;
5043

51-
~PaddlePredictorImpl() override{};
44+
~NativePaddlePredictor() override{};
5245

5346
private:
54-
bool InitShared() override;
5547
bool SetFeed(const std::vector<PaddleTensor> &input_datas,
5648
std::vector<framework::LoDTensor> *feeds);
5749
bool GetFetch(const std::vector<framework::LoDTensor> &fetchs,
5850
std::vector<PaddleTensor> *output_data);
5951

60-
ConfigImpl config_;
52+
NativeConfig config_;
6153
platform::Place place_;
6254
std::unique_ptr<framework::Executor> executor_;
6355
std::unique_ptr<framework::Scope> scope_;

paddle/contrib/inference/test_paddle_inference_api_impl.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,20 @@ PaddleTensor LodTensorToPaddleTensor(framework::LoDTensor* t) {
4040
return pt;
4141
}
4242

43-
ConfigImpl GetConfig() {
44-
ConfigImpl config;
43+
NativeConfig GetConfig() {
44+
NativeConfig config;
4545
config.model_dir = FLAGS_dirname + "word2vec.inference.model";
4646
LOG(INFO) << "dirname " << config.model_dir;
4747
config.fraction_of_gpu_memory = 0.15;
48+
config.use_gpu = true;
4849
config.device = 0;
4950
config.share_variables = true;
5051
return config;
5152
}
5253

5354
TEST(paddle_inference_api_impl, word2vec) {
54-
ConfigImpl config = GetConfig();
55-
std::unique_ptr<PaddlePredictor> predictor = CreatePaddlePredictor(config);
55+
NativeConfig config = GetConfig();
56+
auto predictor = CreatePaddlePredictor<NativeConfig>(config);
5657

5758
framework::LoDTensor first_word, second_word, third_word, fourth_word;
5859
framework::LoD lod{{0, 1}};
@@ -104,7 +105,7 @@ TEST(paddle_inference_api_impl, image_classification) {
104105
int batch_size = 2;
105106
bool use_mkldnn = false;
106107
bool repeat = false;
107-
ConfigImpl config = GetConfig();
108+
NativeConfig config = GetConfig();
108109
config.model_dir =
109110
FLAGS_dirname + "image_classification_resnet.inference.model";
110111

@@ -133,7 +134,7 @@ TEST(paddle_inference_api_impl, image_classification) {
133134
is_combined,
134135
use_mkldnn);
135136

136-
std::unique_ptr<PaddlePredictor> predictor = CreatePaddlePredictor(config);
137+
auto predictor = CreatePaddlePredictor(config);
137138
std::vector<PaddleTensor> paddle_tensor_feeds;
138139
paddle_tensor_feeds.push_back(LodTensorToPaddleTensor(&input));
139140

paddle/fluid/inference/CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ cc_library(paddle_fluid_api
55
SRCS io.cc
66
DEPS ${FLUID_CORE_MODULES} ${GLOB_OP_LIB})
77

8-
# Create static library
98
get_property(fluid_modules GLOBAL PROPERTY FLUID_MODULES)
10-
cc_library(paddle_fluid DEPS ${fluid_modules})
119

10+
if(WITH_CONTRIB)
11+
set(fluid_modules "${fluid_modules}" paddle_inference_api)
12+
endif()
13+
14+
# Create static library
15+
cc_library(paddle_fluid DEPS ${fluid_modules} paddle_fluid_api)
1216
# Create shared library
1317
cc_library(paddle_fluid_shared SHARED
1418
SRCS io.cc
15-
DEPS ${fluid_modules})
19+
DEPS ${fluid_modules} paddle_fluid_api)
20+
1621
set_target_properties(paddle_fluid_shared PROPERTIES OUTPUT_NAME paddle_fluid)
1722
if(NOT APPLE)
1823
# TODO(liuyiqun): Temporarily disable the link flag because it is not support on Mac.

0 commit comments

Comments
 (0)