Skip to content

Commit 7323be1

Browse files
authored
Merge pull request #10956 from panyx0718/infer2
paddle inference interface implementation
2 parents 376c948 + 2f0df56 commit 7323be1

File tree

6 files changed

+523
-10
lines changed

6 files changed

+523
-10
lines changed

paddle/contrib/inference/CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,45 @@
1313
# limitations under the License.
1414
#
1515

16+
function(inference_api_test TARGET_NAME TEST_SRC DEP_TEST)
17+
set(options "")
18+
set(oneValueArgs "")
19+
set(multiValueArgs ARGS)
20+
cmake_parse_arguments(inference_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
21+
22+
set(PYTHON_TESTS_DIR ${PADDLE_BINARY_DIR}/python/paddle/fluid/tests)
23+
set(arg_list "")
24+
if(inference_test_ARGS)
25+
foreach(arg ${inference_test_ARGS})
26+
list(APPEND arg_list "_${arg}")
27+
endforeach()
28+
else()
29+
list(APPEND arg_list "_")
30+
endif()
31+
foreach(arg ${arg_list})
32+
string(REGEX REPLACE "^_$" "" arg "${arg}")
33+
cc_test(${TARGET_NAME}
34+
SRCS ${TEST_SRC}
35+
DEPS paddle_fluid_api paddle_inference_api paddle_inference_api_impl
36+
ARGS --dirname=${PYTHON_TESTS_DIR}/book/)
37+
# set_tests_properties(${TARGET_NAME}
38+
# PROPERTIES DEPENDS ${DEP_TEST})
39+
endforeach()
40+
endfunction(inference_api_test)
41+
42+
1643
cc_library(paddle_inference_api
1744
SRCS paddle_inference_api.cc
1845
DEPS ${FLUID_CORE_MODULES} ${GLOB_OP_LIB})
1946

47+
cc_library(paddle_inference_api_impl
48+
SRCS paddle_inference_api_impl.cc
49+
DEPS paddle_inference_api paddle_fluid_api)
50+
2051
cc_test(test_paddle_inference_api
2152
SRCS test_paddle_inference_api.cc
2253
DEPS paddle_inference_api)
54+
55+
inference_api_test(test_paddle_inference_api_impl
56+
test_paddle_inference_api_impl.cc
57+
test_word2vec)

paddle/contrib/inference/paddle_inference_api.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,38 @@
2727

2828
namespace paddle {
2929

30+
enum PaddleDType {
31+
FLOAT32,
32+
INT64,
33+
};
34+
35+
struct PaddleBuf {
36+
void* data; // pointer to the data memory.
37+
size_t length; // number of memory bytes.
38+
};
39+
3040
struct PaddleTensor {
3141
std::string name; // variable name.
3242
std::vector<int> shape;
33-
std::vector<unsigned char> data; // bytes of data.
34-
size_t type{typeid(float).hash_code()}; // hash of type
43+
PaddleBuf data; // blob of data.
44+
PaddleDType dtype;
3545
};
3646

3747
/*
38-
* A simple Inference API for Paddle. Currently this API might just be used by
39-
* non-sequence scenerios.
40-
* TODO(Superjomn) Prepare another API for NLP-related usages.
41-
*/
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+
*/
4252
class PaddlePredictor {
4353
public:
4454
struct Config;
4555
PaddlePredictor() = default;
4656
PaddlePredictor(const PaddlePredictor&) = delete;
4757

48-
// One drived class should has such a constructor
49-
// PaddlePredictor(const XConfig& config);
50-
// The XConfig is a derived class of Config.
51-
5258
// Predict an record.
59+
// The caller should be responsible for allocating and releasing the memory of
60+
// `inputs`. `inputs` should be alive until Run returns. caller should be
61+
// responsible for releasing the memory of `output_data`.
5362
virtual bool Run(const std::vector<PaddleTensor>& inputs,
5463
std::vector<PaddleTensor>* output_data) = 0;
5564

0 commit comments

Comments
 (0)