Skip to content

Commit 418c41d

Browse files
authored
Feature/anakin embed (#11135)
1 parent 07c80dd commit 418c41d

7 files changed

+229
-21
lines changed

paddle/contrib/inference/CMakeLists.txt

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,42 @@ if(APPLE)
1717
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=pessimizing-move")
1818
endif(APPLE)
1919

20+
set(ANAKIN_INCLUDE "" CACHE STRING "root of Anakin header files")
21+
set(ANAKIN_LIBRARY "" CACHE STRING "path of Anakin library")
22+
23+
24+
set(inference_deps paddle_inference_api paddle_fluid_api)
25+
26+
# if anakin is set enable anakin api implementation
27+
if(ANAKIN_INCLUDE_DIR AND ANAKIN_LIBRARY)
28+
set(ANAKIN_FOUND ON)
29+
else()
30+
set(ANAKIN_FOUND OFF)
31+
endif()
32+
33+
if (ANAKIN_FOUND)
34+
# Anakin's code style doesn't follow google c style.
35+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=comment
36+
-Wno-error=reorder
37+
-Wno-error=format
38+
-Wno-error=switch
39+
-Wno-error=return-type
40+
-Wno-error=non-virtual-dtor
41+
-Wno-error=cpp")
42+
43+
message(STATUS "Anakin for inference is enabled")
44+
message(STATUS "Anakin is set INCLUDE:${ANAKIN_INCLUDE} LIBRARY:${ANAKIN_LIBRARY}")
45+
include_directories("${ANAKIN_INCLUDE}")
46+
# Anakin's source path is a mass, need to set sub-directories trivially.
47+
include_directories("${ANAKIN_INCLUDE}/saber")
48+
link_directories("${ANAKIN_LIBRARY}")
49+
50+
nv_library(inference_anakin_api SRCS paddle_inference_api_anakin_engine.cc)
51+
target_link_libraries(inference_anakin_api anakin)
52+
list(APPEND inference_deps inference_anakin_api)
53+
endif()
54+
55+
2056
function(inference_api_test TARGET_NAME)
2157
if (WITH_TESTING)
2258
set(options "")
@@ -27,7 +63,7 @@ function(inference_api_test TARGET_NAME)
2763
set(PYTHON_TESTS_DIR ${PADDLE_BINARY_DIR}/python/paddle/fluid/tests)
2864
cc_test(${TARGET_NAME}
2965
SRCS ${TARGET_NAME}.cc
30-
DEPS paddle_fluid paddle_inference_api
66+
DEPS "${inference_deps}"
3167
ARGS --dirname=${PYTHON_TESTS_DIR}/book/)
3268
if(inference_test_ARGS)
3369
set_tests_properties(${TARGET_NAME}
@@ -47,6 +83,11 @@ cc_test(test_paddle_inference_api
4783
inference_api_test(test_paddle_inference_api_impl
4884
ARGS test_word2vec test_image_classification)
4985

86+
if (ANAKIN_FOUND)
87+
nv_test(inference_anakin_test SRCS paddle_inference_api_anakin_engine_tester.cc
88+
DEPS ${inference_deps} protobuf)
89+
endif()
90+
5091
if(WITH_TESTING)
5192
add_subdirectory(demo)
5293
endif()

paddle/contrib/inference/demo/simple_on_word2vec.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void Main(bool use_gpu) {
5454
CHECK(predictor->Run(slots, &outputs));
5555

5656
//# 4. Get output.
57-
ASSERT_EQ(outputs.size(), 1);
57+
ASSERT_EQ(outputs.size(), 1UL);
5858
LOG(INFO) << "output buffer size: " << outputs.front().data.length;
5959
const size_t num_elements = outputs.front().data.length / sizeof(float);
6060
// The outputs' buffers are in CPU memory.

paddle/contrib/inference/paddle_inference_api.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
22
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
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
66
7-
http://www.apache.org/licenses/LICENSE-2.0
7+
http://www.apache.org/licenses/LICENSE-2.0
88
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. */
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. */
1414

1515
/*
1616
* This file contains the definition of a simple Inference API for Paddle.
@@ -47,8 +47,8 @@ struct PaddleTensor {
4747

4848
enum class PaddleEngineKind {
4949
kNative = 0, // Use the native Fluid facility.
50+
kAnakin, // Use Anakin for inference.
5051
// TODO(Superjomn) support following engines latter.
51-
// kAnakin, // Use Anakin for inference.
5252
// kTensorRT, // Use TensorRT for inference.
5353
// kAutoMixedAnakin, // Automatically mix Fluid with Anakin.
5454
// kAutoMixedTensorRT, // Automatically mix Fluid with TensorRT.
@@ -95,6 +95,13 @@ struct NativeConfig : public PaddlePredictor::Config {
9595
std::string param_file;
9696
};
9797

98+
// Configurations for Anakin engine.
99+
struct AnakinConfig : public PaddlePredictor::Config {
100+
int device;
101+
std::string model_file;
102+
int max_batch_size{-1};
103+
};
104+
98105
// A factory to help create different predictors.
99106
//
100107
// FOR EXTENSION DEVELOPER:
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 <cuda.h>
16+
17+
#include "paddle/contrib/inference/paddle_inference_api_anakin_engine.h"
18+
19+
namespace paddle {
20+
21+
PaddleInferenceAnakinPredictor::PaddleInferenceAnakinPredictor(
22+
const AnakinConfig &config) {
23+
CHECK(Init(config));
24+
}
25+
26+
bool PaddleInferenceAnakinPredictor::Init(const AnakinConfig &config) {
27+
// TODO(Superjomn) Tell anakin to support return code.
28+
engine_.Build(config.model_file, config.max_batch_size);
29+
return true;
30+
}
31+
32+
bool PaddleInferenceAnakinPredictor::Run(
33+
const std::vector<PaddleTensor> &inputs,
34+
std::vector<PaddleTensor> *output_data) {
35+
for (const auto &input : inputs) {
36+
if (input.dtype != PaddleDType::FLOAT32) {
37+
LOG(ERROR) << "Only support float type inputs. " << input.name
38+
<< "'s type is not float";
39+
return false;
40+
}
41+
engine_.SetInputFromCPU(
42+
input.name, static_cast<float *>(input.data.data), input.data.length);
43+
}
44+
45+
// TODO(Superjomn) Tell anakin to support return code.
46+
engine_.Execute();
47+
48+
if (output_data->empty()) {
49+
LOG(ERROR) << "At least one output should be set with tensors' names.";
50+
return false;
51+
}
52+
for (auto &output : *output_data) {
53+
auto *tensor = engine_.GetOutputInGPU(output.name);
54+
output.shape = tensor->shape();
55+
// Copy data from GPU -> CPU
56+
if (cudaMemcpy(output.data.data,
57+
tensor->data(),
58+
tensor->size(),
59+
cudaMemcpyDeviceToHost) != 0) {
60+
LOG(ERROR) << "copy data from GPU to CPU error";
61+
return false;
62+
}
63+
}
64+
return true;
65+
}
66+
67+
// TODO(Superjomn) To implement latter.
68+
std::unique_ptr<PaddlePredictor> PaddleInferenceAnakinPredictor::Clone() {
69+
return nullptr;
70+
}
71+
72+
// A factory to help create difference predictor.
73+
template <>
74+
std::unique_ptr<PaddlePredictor>
75+
CreatePaddlePredictor<AnakinConfig, PaddleEngineKind::kAnakin>(
76+
const AnakinConfig &config) {
77+
std::unique_ptr<PaddlePredictor> x(
78+
new PaddleInferenceAnakinPredictor(config));
79+
return x;
80+
};
81+
82+
} // namespace paddle
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 the implementation of inference API with Anakin engine
17+
* embeded, this API can only support Anakin models.
18+
*/
19+
20+
#pragma once
21+
22+
// NOTE This header file do not have namespace.
23+
// TODO(Superjomn) Tell Anakin to provide better APIs.
24+
#include <test/framework/net/paddle_api.h>
25+
#include "paddle/contrib/inference/paddle_inference_api.h"
26+
27+
namespace paddle {
28+
29+
class PaddleInferenceAnakinPredictor : public PaddlePredictor {
30+
public:
31+
PaddleInferenceAnakinPredictor(const AnakinConfig& config);
32+
33+
// NOTE Unlike the native engine, the buffers of anakin engine's output_data
34+
// should be allocated first.
35+
// TODO(Superjomn) should unify all the behaviors of output_data accross all
36+
// the engines.
37+
bool Run(const std::vector<PaddleTensor>& inputs,
38+
std::vector<PaddleTensor>* output_data) override;
39+
40+
std::unique_ptr<PaddlePredictor> Clone() override;
41+
42+
private:
43+
bool Init(const AnakinConfig& config);
44+
45+
anakin::AnakinEngine<anakin::NV,
46+
anakin::saber::AK_FLOAT,
47+
anakin::Precision::FP32>
48+
engine_;
49+
};
50+
51+
} // namespace paddle
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
#include <gtest/gtest.h>
17+
18+
namespace paddle {
19+
20+
TEST(inference, anakin) {
21+
AnakinConfig config;
22+
23+
auto engine =
24+
CreatePaddlePredictor<AnakinConfig, PaddleEngineKind::kAnakin>(config);
25+
}
26+
27+
} // namespace paddle

paddle/contrib/inference/paddle_inference_api_impl.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
22
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
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
66
7-
http://www.apache.org/licenses/LICENSE-2.0
7+
http://www.apache.org/licenses/LICENSE-2.0
88
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. */
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. */
1414

1515
#include <sys/time.h>
1616
#include <algorithm>

0 commit comments

Comments
 (0)