From 418c41d8fb4a9dd0f221e7c3e3b94d5433493ded Mon Sep 17 00:00:00 2001 From: Yan Chunwei Date: Mon, 4 Jun 2018 22:01:14 +0800 Subject: [PATCH] Feature/anakin embed (#11135) --- paddle/contrib/inference/CMakeLists.txt | 43 +++++++++- .../inference/demo/simple_on_word2vec.cc | 2 +- .../contrib/inference/paddle_inference_api.h | 27 +++--- .../paddle_inference_api_anakin_engine.cc | 82 +++++++++++++++++++ .../paddle_inference_api_anakin_engine.h | 51 ++++++++++++ ...ddle_inference_api_anakin_engine_tester.cc | 27 ++++++ .../inference/paddle_inference_api_impl.cc | 18 ++-- 7 files changed, 229 insertions(+), 21 deletions(-) create mode 100644 paddle/contrib/inference/paddle_inference_api_anakin_engine.cc create mode 100644 paddle/contrib/inference/paddle_inference_api_anakin_engine.h create mode 100644 paddle/contrib/inference/paddle_inference_api_anakin_engine_tester.cc diff --git a/paddle/contrib/inference/CMakeLists.txt b/paddle/contrib/inference/CMakeLists.txt index 8ca34465395..1e3bb7bf16f 100644 --- a/paddle/contrib/inference/CMakeLists.txt +++ b/paddle/contrib/inference/CMakeLists.txt @@ -17,6 +17,42 @@ if(APPLE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=pessimizing-move") endif(APPLE) +set(ANAKIN_INCLUDE "" CACHE STRING "root of Anakin header files") +set(ANAKIN_LIBRARY "" CACHE STRING "path of Anakin library") + + +set(inference_deps paddle_inference_api paddle_fluid_api) + +# if anakin is set enable anakin api implementation +if(ANAKIN_INCLUDE_DIR AND ANAKIN_LIBRARY) + set(ANAKIN_FOUND ON) +else() + set(ANAKIN_FOUND OFF) +endif() + +if (ANAKIN_FOUND) + # Anakin's code style doesn't follow google c style. + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=comment + -Wno-error=reorder + -Wno-error=format + -Wno-error=switch + -Wno-error=return-type + -Wno-error=non-virtual-dtor + -Wno-error=cpp") + + message(STATUS "Anakin for inference is enabled") + message(STATUS "Anakin is set INCLUDE:${ANAKIN_INCLUDE} LIBRARY:${ANAKIN_LIBRARY}") + include_directories("${ANAKIN_INCLUDE}") + # Anakin's source path is a mass, need to set sub-directories trivially. + include_directories("${ANAKIN_INCLUDE}/saber") + link_directories("${ANAKIN_LIBRARY}") + + nv_library(inference_anakin_api SRCS paddle_inference_api_anakin_engine.cc) + target_link_libraries(inference_anakin_api anakin) + list(APPEND inference_deps inference_anakin_api) +endif() + + function(inference_api_test TARGET_NAME) if (WITH_TESTING) set(options "") @@ -27,7 +63,7 @@ function(inference_api_test TARGET_NAME) set(PYTHON_TESTS_DIR ${PADDLE_BINARY_DIR}/python/paddle/fluid/tests) cc_test(${TARGET_NAME} SRCS ${TARGET_NAME}.cc - DEPS paddle_fluid paddle_inference_api + DEPS "${inference_deps}" ARGS --dirname=${PYTHON_TESTS_DIR}/book/) if(inference_test_ARGS) set_tests_properties(${TARGET_NAME} @@ -47,6 +83,11 @@ cc_test(test_paddle_inference_api inference_api_test(test_paddle_inference_api_impl ARGS test_word2vec test_image_classification) +if (ANAKIN_FOUND) + nv_test(inference_anakin_test SRCS paddle_inference_api_anakin_engine_tester.cc + DEPS ${inference_deps} protobuf) +endif() + if(WITH_TESTING) add_subdirectory(demo) endif() diff --git a/paddle/contrib/inference/demo/simple_on_word2vec.cc b/paddle/contrib/inference/demo/simple_on_word2vec.cc index 165d2e196b3..ee865f37900 100644 --- a/paddle/contrib/inference/demo/simple_on_word2vec.cc +++ b/paddle/contrib/inference/demo/simple_on_word2vec.cc @@ -54,7 +54,7 @@ void Main(bool use_gpu) { CHECK(predictor->Run(slots, &outputs)); //# 4. Get output. - ASSERT_EQ(outputs.size(), 1); + ASSERT_EQ(outputs.size(), 1UL); LOG(INFO) << "output buffer size: " << outputs.front().data.length; const size_t num_elements = outputs.front().data.length / sizeof(float); // The outputs' buffers are in CPU memory. diff --git a/paddle/contrib/inference/paddle_inference_api.h b/paddle/contrib/inference/paddle_inference_api.h index 5fe8399762b..b5cd0d603f1 100644 --- a/paddle/contrib/inference/paddle_inference_api.h +++ b/paddle/contrib/inference/paddle_inference_api.h @@ -1,16 +1,16 @@ /* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 +http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. */ +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ /* * This file contains the definition of a simple Inference API for Paddle. @@ -47,8 +47,8 @@ struct PaddleTensor { enum class PaddleEngineKind { kNative = 0, // Use the native Fluid facility. + kAnakin, // Use Anakin for inference. // TODO(Superjomn) support following engines latter. - // kAnakin, // Use Anakin for inference. // kTensorRT, // Use TensorRT for inference. // kAutoMixedAnakin, // Automatically mix Fluid with Anakin. // kAutoMixedTensorRT, // Automatically mix Fluid with TensorRT. @@ -95,6 +95,13 @@ struct NativeConfig : public PaddlePredictor::Config { std::string param_file; }; +// Configurations for Anakin engine. +struct AnakinConfig : public PaddlePredictor::Config { + int device; + std::string model_file; + int max_batch_size{-1}; +}; + // A factory to help create different predictors. // // FOR EXTENSION DEVELOPER: diff --git a/paddle/contrib/inference/paddle_inference_api_anakin_engine.cc b/paddle/contrib/inference/paddle_inference_api_anakin_engine.cc new file mode 100644 index 00000000000..865d7ac10db --- /dev/null +++ b/paddle/contrib/inference/paddle_inference_api_anakin_engine.cc @@ -0,0 +1,82 @@ +// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "paddle/contrib/inference/paddle_inference_api_anakin_engine.h" + +namespace paddle { + +PaddleInferenceAnakinPredictor::PaddleInferenceAnakinPredictor( + const AnakinConfig &config) { + CHECK(Init(config)); +} + +bool PaddleInferenceAnakinPredictor::Init(const AnakinConfig &config) { + // TODO(Superjomn) Tell anakin to support return code. + engine_.Build(config.model_file, config.max_batch_size); + return true; +} + +bool PaddleInferenceAnakinPredictor::Run( + const std::vector &inputs, + std::vector *output_data) { + for (const auto &input : inputs) { + if (input.dtype != PaddleDType::FLOAT32) { + LOG(ERROR) << "Only support float type inputs. " << input.name + << "'s type is not float"; + return false; + } + engine_.SetInputFromCPU( + input.name, static_cast(input.data.data), input.data.length); + } + + // TODO(Superjomn) Tell anakin to support return code. + engine_.Execute(); + + if (output_data->empty()) { + LOG(ERROR) << "At least one output should be set with tensors' names."; + return false; + } + for (auto &output : *output_data) { + auto *tensor = engine_.GetOutputInGPU(output.name); + output.shape = tensor->shape(); + // Copy data from GPU -> CPU + if (cudaMemcpy(output.data.data, + tensor->data(), + tensor->size(), + cudaMemcpyDeviceToHost) != 0) { + LOG(ERROR) << "copy data from GPU to CPU error"; + return false; + } + } + return true; +} + +// TODO(Superjomn) To implement latter. +std::unique_ptr PaddleInferenceAnakinPredictor::Clone() { + return nullptr; +} + +// A factory to help create difference predictor. +template <> +std::unique_ptr +CreatePaddlePredictor( + const AnakinConfig &config) { + std::unique_ptr x( + new PaddleInferenceAnakinPredictor(config)); + return x; +}; + +} // namespace paddle diff --git a/paddle/contrib/inference/paddle_inference_api_anakin_engine.h b/paddle/contrib/inference/paddle_inference_api_anakin_engine.h new file mode 100644 index 00000000000..fe9f562e9d1 --- /dev/null +++ b/paddle/contrib/inference/paddle_inference_api_anakin_engine.h @@ -0,0 +1,51 @@ +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +/* + * This file contains the implementation of inference API with Anakin engine + * embeded, this API can only support Anakin models. + */ + +#pragma once + +// NOTE This header file do not have namespace. +// TODO(Superjomn) Tell Anakin to provide better APIs. +#include +#include "paddle/contrib/inference/paddle_inference_api.h" + +namespace paddle { + +class PaddleInferenceAnakinPredictor : public PaddlePredictor { + public: + PaddleInferenceAnakinPredictor(const AnakinConfig& config); + + // NOTE Unlike the native engine, the buffers of anakin engine's output_data + // should be allocated first. + // TODO(Superjomn) should unify all the behaviors of output_data accross all + // the engines. + bool Run(const std::vector& inputs, + std::vector* output_data) override; + + std::unique_ptr Clone() override; + + private: + bool Init(const AnakinConfig& config); + + anakin::AnakinEngine + engine_; +}; + +} // namespace paddle diff --git a/paddle/contrib/inference/paddle_inference_api_anakin_engine_tester.cc b/paddle/contrib/inference/paddle_inference_api_anakin_engine_tester.cc new file mode 100644 index 00000000000..43324bc67cb --- /dev/null +++ b/paddle/contrib/inference/paddle_inference_api_anakin_engine_tester.cc @@ -0,0 +1,27 @@ +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#include "paddle/contrib/inference/paddle_inference_api.h" +#include + +namespace paddle { + +TEST(inference, anakin) { + AnakinConfig config; + + auto engine = + CreatePaddlePredictor(config); +} + +} // namespace paddle diff --git a/paddle/contrib/inference/paddle_inference_api_impl.cc b/paddle/contrib/inference/paddle_inference_api_impl.cc index e7a8fa68b7f..b52a43a463d 100644 --- a/paddle/contrib/inference/paddle_inference_api_impl.cc +++ b/paddle/contrib/inference/paddle_inference_api_impl.cc @@ -1,16 +1,16 @@ /* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 +http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. */ +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ #include #include -- GitLab