Skip to content

Commit b5b6661

Browse files
authored
feature/simple inference demo (#11105)
1 parent 663f4e6 commit b5b6661

File tree

5 files changed

+116
-24
lines changed

5 files changed

+116
-24
lines changed

paddle/contrib/inference/CMakeLists.txt

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,35 @@ if(APPLE)
1818
endif(APPLE)
1919

2020
function(inference_api_test TARGET_NAME)
21-
set(options "")
22-
set(oneValueArgs "")
23-
set(multiValueArgs ARGS)
24-
cmake_parse_arguments(inference_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
21+
if (WITH_TESTING)
22+
set(options "")
23+
set(oneValueArgs "")
24+
set(multiValueArgs ARGS)
25+
cmake_parse_arguments(inference_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
2526

26-
set(PYTHON_TESTS_DIR ${PADDLE_BINARY_DIR}/python/paddle/fluid/tests)
27-
cc_test(test_paddle_inference_${TARGET_NAME}
28-
SRCS test_paddle_inference_${TARGET_NAME}.cc
29-
DEPS paddle_fluid_api paddle_inference_api
30-
ARGS --dirname=${PYTHON_TESTS_DIR}/book/)
31-
if(inference_test_ARGS)
32-
set_tests_properties(test_paddle_inference_${TARGET_NAME}
33-
PROPERTIES DEPENDS "${inference_test_ARGS}")
34-
endif()
27+
set(PYTHON_TESTS_DIR ${PADDLE_BINARY_DIR}/python/paddle/fluid/tests)
28+
cc_test(${TARGET_NAME}
29+
SRCS ${TARGET_NAME}.cc
30+
DEPS paddle_fluid paddle_inference_api
31+
ARGS --dirname=${PYTHON_TESTS_DIR}/book/)
32+
if(inference_test_ARGS)
33+
set_tests_properties(${TARGET_NAME}
34+
PROPERTIES DEPENDS "${inference_test_ARGS}")
35+
endif()
36+
endif(WITH_TESTING)
3537
endfunction(inference_api_test)
3638

37-
3839
cc_library(paddle_inference_api
3940
SRCS paddle_inference_api.cc paddle_inference_api_impl.cc
4041
DEPS ${FLUID_CORE_MODULES} ${GLOB_OP_LIB})
4142

42-
if(WITH_TESTING)
43-
cc_test(test_paddle_inference_api
44-
SRCS test_paddle_inference_api.cc
45-
DEPS paddle_inference_api)
43+
cc_test(test_paddle_inference_api
44+
SRCS test_paddle_inference_api.cc
45+
DEPS paddle_inference_api)
4646

47-
inference_api_test(api_impl
48-
ARGS test_word2vec test_image_classification)
47+
inference_api_test(test_paddle_inference_api_impl
48+
ARGS test_word2vec test_image_classification)
49+
50+
if(WITH_TESTING)
51+
add_subdirectory(demo)
4952
endif()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
inference_api_test(simple_on_word2vec ARGS test_word2vec)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
/*
16+
* This file contains a simple demo for how to take a model for inference.
17+
*/
18+
19+
#include <glog/logging.h>
20+
#include <gtest/gtest.h>
21+
#include <memory>
22+
#include "paddle/contrib/inference/paddle_inference_api.h"
23+
24+
namespace paddle {
25+
namespace demo {
26+
27+
DEFINE_string(dirname, "", "Directory of the inference model.");
28+
29+
void Main(bool use_gpu) {
30+
//# 1. Create PaddlePredictor with a config.
31+
NativeConfig config;
32+
config.model_dir = FLAGS_dirname + "word2vec.inference.model";
33+
config.use_gpu = use_gpu;
34+
config.fraction_of_gpu_memory = 0.15;
35+
config.device = 0;
36+
auto predictor =
37+
CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config);
38+
39+
for (int batch_id = 0; batch_id < 3; batch_id++) {
40+
//# 2. Prepare input.
41+
int64_t data[4] = {1, 2, 3, 4};
42+
43+
PaddleBuf buf{.data = data, .length = sizeof(data)};
44+
PaddleTensor tensor{.name = "",
45+
.shape = std::vector<int>({4, 1}),
46+
.data = buf,
47+
.dtype = PaddleDType::INT64};
48+
49+
// For simplicity, we set all the slots with the same data.
50+
std::vector<PaddleTensor> slots(4, tensor);
51+
52+
//# 3. Run
53+
std::vector<PaddleTensor> outputs;
54+
CHECK(predictor->Run(slots, &outputs));
55+
56+
//# 4. Get output.
57+
ASSERT_EQ(outputs.size(), 1);
58+
LOG(INFO) << "output buffer size: " << outputs.front().data.length;
59+
const size_t num_elements = outputs.front().data.length / sizeof(float);
60+
// The outputs' buffers are in CPU memory.
61+
for (size_t i = 0; i < std::min(5UL, num_elements); i++) {
62+
LOG(INFO) << static_cast<float*>(outputs.front().data.data)[i];
63+
}
64+
}
65+
}
66+
67+
TEST(demo, word2vec_cpu) { Main(false /*use_gpu*/); }
68+
TEST(demo, word2vec_gpu) { Main(true /*use_gpu*/); }
69+
70+
} // namespace demo
71+
} // namespace paddle

paddle/contrib/inference/paddle_inference_api_impl.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,11 @@ CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(
248248
VLOG(3) << "create NativePaddlePredictor";
249249
if (config.use_gpu) {
250250
// 1. GPU memeroy
251-
PADDLE_ENFORCE(
252-
config.fraction_of_gpu_memory > 0.f,
251+
PADDLE_ENFORCE_GT(
252+
config.fraction_of_gpu_memory,
253+
0.f,
253254
"fraction_of_gpu_memory in the config should be set to range (0., 1.]");
255+
PADDLE_ENFORCE_GE(config.device, 0, "Invalid device id %d", config.device);
254256
std::vector<std::string> flags;
255257
if (config.fraction_of_gpu_memory >= 0.0f ||
256258
config.fraction_of_gpu_memory <= 0.95f) {

paddle/contrib/inference/test_paddle_inference_api_impl.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ TEST(paddle_inference_api_impl, word2vec) {
7474
ASSERT_EQ(outputs.size(), 1UL);
7575
size_t len = outputs[0].data.length;
7676
float* data = static_cast<float*>(outputs[0].data.data);
77-
for (int j = 0; j < len / sizeof(float); ++j) {
77+
for (size_t j = 0; j < len / sizeof(float); ++j) {
7878
ASSERT_LT(data[j], 1.0);
7979
ASSERT_GT(data[j], -1.0);
8080
}
@@ -92,7 +92,7 @@ TEST(paddle_inference_api_impl, word2vec) {
9292
TestInference<platform::CPUPlace>(config.model_dir, cpu_feeds, cpu_fetchs1);
9393

9494
float* lod_data = output1.data<float>();
95-
for (size_t i = 0; i < output1.numel(); ++i) {
95+
for (int i = 0; i < output1.numel(); ++i) {
9696
EXPECT_LT(lod_data[i] - data[i], 1e-3);
9797
EXPECT_GT(lod_data[i] - data[i], -1e-3);
9898
}

0 commit comments

Comments
 (0)