Skip to content

Commit 809122c

Browse files
authored
Merge pull request #7097 from Xreki/core_inference_example
Add a simple C++ inference example for fluid
2 parents a8b3996 + c7bd777 commit 809122c

File tree

15 files changed

+398
-12
lines changed

15 files changed

+398
-12
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ set(PADDLE_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
2020
include(system)
2121

2222
project(paddle CXX C Go)
23-
message(STATUS "CXX compiler: " ${CMAKE_CXX_COMPILER} ", version: " ${CMAKE_CXX_COMPILER_VERSION})
24-
message(STATUS "C compiler: " ${CMAKE_C_COMPILER} ", version: " ${CMAKE_C_COMPILER_VERSION})
23+
message(STATUS "CXX compiler: ${CMAKE_CXX_COMPILER}, version: "
24+
"${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
25+
message(STATUS "C compiler: ${CMAKE_C_COMPILER}, version: "
26+
"${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION}")
2527

2628
find_package(Sphinx)
2729
if(NOT CMAKE_CROSSCOMPILING)

cmake/external/eigen.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ExternalProject_Add(
1919

2020
if (${CMAKE_VERSION} VERSION_LESS "3.3.0")
2121
set(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/eigen3_dummy.c)
22-
file(WRITE ${dummyfile} "const char * dummy_eigen3 = \"${dummyfile}\";")
22+
file(WRITE ${dummyfile} "const char *dummy_eigen3 = \"${dummyfile}\";")
2323
add_library(eigen3 STATIC ${dummyfile})
2424
else()
2525
add_library(eigen3 INTERFACE)

cmake/external/openblas.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ INCLUDE_DIRECTORIES(${CBLAS_INC_DIR})
109109
# FIXME(gangliao): generate cblas target to track all high performance
110110
# linear algebra libraries for cc_library(xxx SRCS xxx.c DEPS cblas)
111111
SET(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/cblas_dummy.c)
112-
FILE(WRITE ${dummyfile} "const char * dummy = \"${dummyfile}\";")
112+
FILE(WRITE ${dummyfile} "const char *dummy_cblas = \"${dummyfile}\";")
113113
ADD_LIBRARY(cblas STATIC ${dummyfile})
114114
TARGET_LINK_LIBRARIES(cblas ${CBLAS_LIBRARIES})
115115

cmake/generic.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function(merge_static_libs TARGET_NAME)
120120
DEPENDS ${libs})
121121

122122
# Generate dummy staic lib
123-
file(WRITE ${target_SRCS} "const char *dummy = \"${target_SRCS}\";")
123+
file(WRITE ${target_SRCS} "const char *dummy_${TARGET_NAME} = \"${target_SRCS}\";")
124124
add_library(${TARGET_NAME} STATIC ${target_SRCS})
125125
target_link_libraries(${TARGET_NAME} ${libs_deps})
126126

@@ -160,7 +160,7 @@ function(merge_static_libs TARGET_NAME)
160160
DEPENDS ${libs} ${target_OBJS})
161161

162162
# Generate dummy staic lib
163-
file(WRITE ${target_SRCS} "const char *dummy = \"${target_SRCS}\";")
163+
file(WRITE ${target_SRCS} "const char *dummy_${TARGET_NAME} = \"${target_SRCS}\";")
164164
add_library(${TARGET_NAME} STATIC ${target_SRCS})
165165
target_link_libraries(${TARGET_NAME} ${libs_deps})
166166

@@ -324,7 +324,7 @@ function(go_library TARGET_NAME)
324324
)
325325

326326
# Add dummy code to support `make target_name` under Terminal Command
327-
file(WRITE ${dummyfile} "const char * dummy = \"${dummyfile}\";")
327+
file(WRITE ${dummyfile} "const char *dummy_${TARGET_NAME} = \"${dummyfile}\";")
328328
if (go_library_SHARED OR go_library_shared)
329329
add_library(${TARGET_NAME} SHARED ${dummyfile})
330330
else()

paddle/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ else()
2424
add_subdirectory(framework)
2525
add_subdirectory(operators)
2626
add_subdirectory(pybind)
27+
add_subdirectory(inference)
2728
endif()
2829

2930
if(WITH_SWIG_PY)

paddle/framework/op_desc.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class CompileTimeInferShapeContext : public InferShapeContext {
6464
PADDLE_ENFORCE_EQ(in_var->GetType(), proto::VarDesc::LOD_TENSOR,
6565
"The %d-th output of Output(%s) must be LoDTensor.", j,
6666
out);
67-
out_var->SetLoDLevel(in_var->GetLodLevel());
67+
out_var->SetLoDLevel(in_var->GetLoDLevel());
6868
}
6969
bool IsRuntime() const override;
7070

paddle/framework/var_desc.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void VarDesc::SetLoDLevel(int32_t lod_level) {
5252
}
5353
}
5454

55-
int32_t VarDesc::GetLodLevel() const {
55+
int32_t VarDesc::GetLoDLevel() const {
5656
switch (desc_.type()) {
5757
case proto::VarDesc::LOD_TENSOR:
5858
return desc_.lod_tensor().lod_level();

paddle/framework/var_desc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class VarDesc {
7676

7777
void SetLoDLevel(int32_t lod_level);
7878

79-
int32_t GetLodLevel() const;
79+
int32_t GetLoDLevel() const;
8080

8181
proto::VarDesc::VarType GetType() const;
8282

paddle/inference/CMakeLists.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
set(FLUID_CORE_MODULES
2+
backward proto_desc paddle_memory executor prune init ${GLOB_OP_LIB})
3+
4+
cc_library(paddle_fluid_api
5+
SRCS inference.cc
6+
DEPS ${FLUID_CORE_MODULES})
7+
8+
# Merge all modules into a simgle static library
9+
cc_library(paddle_fluid DEPS paddle_fluid_api ${FLUID_CORE_MODULES})
10+
11+
# ptools
12+
# just for testing, we may need to change the storing format for inference_model
13+
# and move the dependent of pickle.
14+
# download from http://www.picklingtools.com/
15+
# build in the C++ sub-directory, using command
16+
# make -f Makefile.Linux libptools.so
17+
set(PTOOLS_LIB)
18+
set(PTOOLS_ROOT $ENV{PTOOLS_ROOT} CACHE PATH "Folder contains PicklingTools")
19+
find_path(PTOOLS_INC_DIR chooseser.h PATHS ${PTOOLS_ROOT}/C++)
20+
find_library(PTOOLS_SHARED_LIB NAMES ptools PATHS ${PTOOLS_ROOT}/C++)
21+
if(PTOOLS_INC_DIR AND PTOOLS_SHARED_LIB)
22+
add_definitions(-DPADDLE_USE_PTOOLS)
23+
set(PTOOLS_LIB ptools)
24+
message(STATUS "Found PicklingTools: ${PTOOLS_SHARED_LIB}")
25+
add_library(${PTOOLS_LIB} SHARED IMPORTED GLOBAL)
26+
set_property(TARGET ${PTOOLS_LIB} PROPERTY IMPORTED_LOCATION ${PTOOLS_SHARED_LIB})
27+
include_directories(${PTOOLS_ROOT}/C++)
28+
include_directories(${PTOOLS_ROOT}/C++/opencontainers_1_8_5/include)
29+
add_definitions(-DOC_NEW_STYLE_INCLUDES) # used in ptools
30+
endif()
31+
32+
add_executable(example example.cc)
33+
if(APPLE)
34+
set(OPTIONAL_LINK_FLAGS)
35+
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
36+
set(OPTIONAL_LINK_FLAGS "-undefined dynamic_lookup")
37+
endif()
38+
target_link_libraries(example
39+
-Wl,-force_load paddle_fluid
40+
${OPTIONAL_LINK_FLAGS}
41+
${PTOOLS_LIB})
42+
else()
43+
target_link_libraries(example
44+
-Wl,--start-group -Wl,--whole-archive paddle_fluid
45+
-Wl,--no-whole-archive -Wl,--end-group
46+
${PTOOLS_LIB})
47+
endif()

paddle/inference/example.cc

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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+
#include <time.h>
16+
#include <iostream>
17+
#include "gflags/gflags.h"
18+
#include "paddle/inference/inference.h"
19+
20+
DEFINE_string(dirname, "", "Directory of the inference model.");
21+
DEFINE_string(feed_var_names, "", "Names of feeding variables");
22+
DEFINE_string(fetch_var_names, "", "Names of fetching variables");
23+
24+
int main(int argc, char** argv) {
25+
google::ParseCommandLineFlags(&argc, &argv, true);
26+
if (FLAGS_dirname.empty() || FLAGS_feed_var_names.empty() ||
27+
FLAGS_fetch_var_names.empty()) {
28+
// Example:
29+
// ./example --dirname=recognize_digits_mlp.inference.model
30+
// --feed_var_names="x"
31+
// --fetch_var_names="fc_2.tmp_2"
32+
std::cout << "Usage: ./example --dirname=path/to/your/model "
33+
"--feed_var_names=x --fetch_var_names=y"
34+
<< std::endl;
35+
exit(1);
36+
}
37+
38+
std::cout << "FLAGS_dirname: " << FLAGS_dirname << std::endl;
39+
std::cout << "FLAGS_feed_var_names: " << FLAGS_feed_var_names << std::endl;
40+
std::cout << "FLAGS_fetch_var_names: " << FLAGS_fetch_var_names << std::endl;
41+
42+
std::string dirname = FLAGS_dirname;
43+
std::vector<std::string> feed_var_names = {FLAGS_feed_var_names};
44+
std::vector<std::string> fetch_var_names = {FLAGS_fetch_var_names};
45+
46+
paddle::InferenceEngine* engine = new paddle::InferenceEngine();
47+
engine->LoadInferenceModel(dirname, feed_var_names, fetch_var_names);
48+
49+
paddle::framework::LoDTensor input;
50+
srand(time(0));
51+
float* input_ptr =
52+
input.mutable_data<float>({1, 784}, paddle::platform::CPUPlace());
53+
for (int i = 0; i < 784; ++i) {
54+
input_ptr[i] = rand() / (static_cast<float>(RAND_MAX));
55+
}
56+
57+
std::vector<paddle::framework::LoDTensor> feeds;
58+
feeds.push_back(input);
59+
std::vector<paddle::framework::LoDTensor> fetchs;
60+
engine->Execute(feeds, fetchs);
61+
62+
for (size_t i = 0; i < fetchs.size(); ++i) {
63+
auto dims_i = fetchs[i].dims();
64+
std::cout << "dims_i:";
65+
for (int j = 0; j < dims_i.size(); ++j) {
66+
std::cout << " " << dims_i[j];
67+
}
68+
std::cout << std::endl;
69+
std::cout << "result:";
70+
float* output_ptr = fetchs[i].data<float>();
71+
for (int j = 0; j < paddle::framework::product(dims_i); ++j) {
72+
std::cout << " " << output_ptr[j];
73+
}
74+
std::cout << std::endl;
75+
}
76+
77+
delete engine;
78+
return 0;
79+
}

0 commit comments

Comments
 (0)