Skip to content

Commit fd45c6d

Browse files
Superjomnjacquesqiao
authored andcommitted
feature/inference api demo impl (#10825)
add inference api demo impl
1 parent dd428a0 commit fd45c6d

File tree

8 files changed

+172
-27
lines changed

8 files changed

+172
-27
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ option(USE_NNPACK "Compile PaddlePaddle with NNPACK library" OFF)
5858
option(WITH_DISTRIBUTE "Compile with grpc distributed support" OFF)
5959
option(USE_EIGEN_FOR_BLAS "Use matrix multiplication in Eigen" OFF)
6060
option(WITH_ARM_FP16 "Use half precision support on armv8.2-a cpu" OFF)
61+
option(WITH_FAST_BUNDLE_TEST "Bundle tests that can be run in a single process together to reduce launch overhead" OFF)
62+
option(WITH_CONTRIB "Compile the third-party contributation" OFF)
6163

6264
# CMAKE_BUILD_TYPE
6365
if(NOT CMAKE_BUILD_TYPE)
@@ -230,3 +232,7 @@ if(WITH_DOC)
230232
find_python_module(recommonmark REQUIRED)
231233
add_subdirectory(doc)
232234
endif()
235+
236+
if (WITH_CONTRIB)
237+
add_subdirectory(paddle/contrib)
238+
endif()

paddle/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ GTAGS
1111
*.pb.cc
1212
*.pb.h
1313
*_pb2.py
14-
paddle_*
1514
output/
1615
google/
1716
Makefile

paddle/contrib/CMakeLists.txt

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+
add_subdirectory(inference)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
cc_library(paddle_inference_api
17+
SRCS paddle_inference_api.cc
18+
DEPS ${FLUID_CORE_MODULES} ${GLOB_OP_LIB})
19+
20+
cc_test(test_paddle_inference_api
21+
SRCS test_paddle_inference_api.cc
22+
DEPS paddle_inference_api)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
#include "paddle/contrib/inference/paddle_inference_api.h"

paddle/contrib/inference/paddle_inference_api.h

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,65 @@
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15+
/*
16+
* This file contains the definition of a simple Inference API for Paddle.
17+
*
18+
* ATTENTION: It requires some C++ features, for lower version C++ or C, we
19+
* might release another API.
20+
*/
21+
1522
#pragma once
1623

24+
#include <memory>
1725
#include <string>
1826
#include <vector>
1927

2028
namespace paddle {
2129

22-
class Predictor {
30+
struct PaddleTensor {
31+
std::string name; // variable name.
32+
std::vector<int> shape;
33+
std::vector<unsigned char> data; // bytes of data.
34+
size_t type{typeid(float).hash_code()}; // hash of type
35+
};
36+
37+
/*
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+
*/
42+
class PaddlePredictor {
2343
public:
24-
struct Attr;
25-
Predictor() = default;
44+
struct Config;
45+
PaddlePredictor() = default;
46+
PaddlePredictor(const PaddlePredictor&) = delete;
2647

27-
// Build the network before inference.
28-
bool Init(const Attr& attr);
48+
// One drived class should has such a constructor
49+
// PaddlePredictor(const XConfig& config);
50+
// The XConfig is a derived class of Config.
2951

3052
// Predict an record.
31-
// Arguments:
32-
// inputs: the name of the input variables.
33-
// outputs: the name of the output varaibles.
34-
// input_shapes: the shape of the input variables.
35-
// output_shapes: the shape of the output variables.
36-
// input_data: the data of the input variables.
37-
// output_data: the data of the output variables.
38-
bool Run(const std::vector<std::string>& inputs,
39-
const std::vector<std::string>& outputs,
40-
const std::vector<std::vector<int>>& input_shapes,
41-
const std::vector<std::vector<int>>& output_shapes,
42-
const std::vector<std::vector<float>>& input_data,
43-
std::vector<std::vector<float>>* output_data);
44-
45-
// Clone a predictor that share the model weights.
46-
Predictor* Clone();
53+
virtual bool Run(const std::vector<PaddleTensor>& inputs,
54+
std::vector<PaddleTensor>* output_data) = 0;
55+
56+
// Clone a predictor that share the model weights, the Cloned predictor should
57+
// be thread-safe.
58+
virtual std::unique_ptr<PaddlePredictor> Clone() = 0;
4759

4860
// Destroy the Predictor.
49-
~Predictor();
61+
virtual ~PaddlePredictor() {}
62+
63+
friend std::unique_ptr<PaddlePredictor> CreatePaddlePredictor(
64+
const PaddlePredictor::Config& config);
5065

51-
struct Attr {
66+
// The common configs for all the predictors.
67+
struct Config {
5268
enum class EngineKind;
5369

5470
std::string model_dir; // path to the model directory.
5571
bool enable_engine{false}; // Enable to execute (part of) the model on
56-
// third-party engines.
57-
EngineKind engine_kind{Attr::EngineKind::kNone};
72+
// third-party engines.
73+
EngineKind engine_kind{Config::EngineKind::kNone};
5874

5975
enum class EngineKind {
6076
kNone = -1, // Use the native Fluid facility.
@@ -66,4 +82,8 @@ class Predictor {
6682
};
6783
};
6884

85+
// A factory to help create difference predictor.
86+
template <typename ConfigT>
87+
std::unique_ptr<PaddlePredictor> CreatePaddlePredictor(const ConfigT& config);
88+
6989
} // namespace paddle
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
#include "paddle/contrib/inference/paddle_inference_api.h"
16+
17+
#include <glog/logging.h>
18+
#include <gtest/gtest.h>
19+
20+
namespace paddle {
21+
22+
/*
23+
* Do not use this, just a demo indicating how to customize a config for a
24+
* specific predictor.
25+
*/
26+
struct DemoConfig : public PaddlePredictor::Config {
27+
float other_config;
28+
};
29+
30+
/*
31+
* Do not use this, just a demo indicating how to customize a Predictor.
32+
*/
33+
class DemoPredictor : public PaddlePredictor {
34+
public:
35+
explicit DemoPredictor(const DemoConfig &config) {
36+
LOG(INFO) << "I get other_config " << config.other_config;
37+
}
38+
bool Run(const std::vector<PaddleTensor> &inputs,
39+
std::vector<PaddleTensor> *output_data) override {
40+
LOG(INFO) << "Run";
41+
return false;
42+
}
43+
44+
std::unique_ptr<PaddlePredictor> Clone() override { return nullptr; }
45+
46+
~DemoPredictor() override {}
47+
};
48+
49+
template <>
50+
std::unique_ptr<PaddlePredictor> CreatePaddlePredictor<DemoConfig>(
51+
const DemoConfig &config) {
52+
std::unique_ptr<PaddlePredictor> x(new DemoPredictor(config));
53+
return x;
54+
}
55+
56+
TEST(paddle_inference_api, demo) {
57+
DemoConfig config;
58+
config.other_config = 1.7;
59+
auto predictor = CreatePaddlePredictor(config);
60+
std::vector<PaddleTensor> outputs;
61+
predictor->Run({}, &outputs);
62+
}
63+
64+
} // namespace paddle

paddle/scripts/paddle_build.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ function cmake_gen() {
104104
-DCMAKE_MODULE_PATH=/opt/rocm/hip/cmake
105105
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
106106
-DWITH_FLUID_ONLY=${WITH_FLUID_ONLY:-OFF}
107+
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
108+
-DWITH_CONTRIB=ON
107109
========================================
108110
EOF
109111
# Disable UNITTEST_USE_VIRTUALENV in docker because
@@ -129,7 +131,8 @@ EOF
129131
-DWITH_FAST_BUNDLE_TEST=ON \
130132
-DCMAKE_MODULE_PATH=/opt/rocm/hip/cmake \
131133
-DWITH_FLUID_ONLY=${WITH_FLUID_ONLY:-OFF} \
132-
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
134+
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
135+
-DWITH_CONTRIB=ON
133136
}
134137

135138
function abort(){

0 commit comments

Comments
 (0)