From e7eea6825ef724716cc9941641be3f801bd6e233 Mon Sep 17 00:00:00 2001 From: sangoly Date: Tue, 17 Sep 2019 14:07:58 +0800 Subject: [PATCH] [Cxx API] add build-in version info (#2047) * [Cxx API] add build-in version info * update: add version.h.in template --- lite/api/cxx_api_impl.cc | 6 +++++ lite/api/light_api_impl.cc | 6 +++++ lite/api/paddle_api.h | 2 ++ lite/api/paddle_api_test.cc | 4 +++ lite/core/CMakeLists.txt | 27 ++++++++++++++++++++ lite/core/version.h.in | 49 +++++++++++++++++++++++++++++++++++++ 6 files changed, 94 insertions(+) create mode 100644 lite/core/version.h.in diff --git a/lite/api/cxx_api_impl.cc b/lite/api/cxx_api_impl.cc index b8c92a8f96..459c8f34f1 100644 --- a/lite/api/cxx_api_impl.cc +++ b/lite/api/cxx_api_impl.cc @@ -13,7 +13,9 @@ // limitations under the License. #include "lite/api/cxx_api.h" +#include #include "lite/api/paddle_api.h" +#include "lite/core/version.h" namespace paddle { namespace lite { @@ -31,6 +33,8 @@ class CxxPaddleApiImpl : public lite_api::PaddlePredictor { void Run() override; + std::string GetVersion() const override; + std::unique_ptr GetTensor( const std::string &name) const override; @@ -63,6 +67,8 @@ std::unique_ptr CxxPaddleApiImpl::GetOutput( void CxxPaddleApiImpl::Run() { raw_predictor_.Run(); } +std::string CxxPaddleApiImpl::GetVersion() const { return version(); } + std::unique_ptr CxxPaddleApiImpl::GetTensor( const std::string &name) const { auto *x = raw_predictor_.GetTensor(name); diff --git a/lite/api/light_api_impl.cc b/lite/api/light_api_impl.cc index 6075f1a36f..69ec5cc496 100644 --- a/lite/api/light_api_impl.cc +++ b/lite/api/light_api_impl.cc @@ -13,7 +13,9 @@ // limitations under the License. #include "lite/api/light_api.h" +#include #include "lite/api/paddle_api.h" +#include "lite/core/version.h" #include "lite/model_parser/model_parser.h" namespace paddle { @@ -29,6 +31,8 @@ class LightPredictorImpl : public PaddlePredictor { void Run() override; + std::string GetVersion() const override; + std::unique_ptr GetTensor( const std::string& name) const override; @@ -61,6 +65,8 @@ std::unique_ptr LightPredictorImpl::GetOutput(int i) const { void LightPredictorImpl::Run() { raw_predictor_->Run(); } +std::string LightPredictorImpl::GetVersion() const { return lite::version(); } + std::unique_ptr LightPredictorImpl::GetTensor( const std::string& name) const { return std::unique_ptr( diff --git a/lite/api/paddle_api.h b/lite/api/paddle_api.h index b1a8b21935..df51ce84b2 100644 --- a/lite/api/paddle_api.h +++ b/lite/api/paddle_api.h @@ -72,6 +72,8 @@ class LITE_API PaddlePredictor { virtual void Run() = 0; + virtual std::string GetVersion() const = 0; + /// Get a readonly tensor, return null if no one called `name` exists. virtual std::unique_ptr GetTensor( const std::string& name) const = 0; diff --git a/lite/api/paddle_api_test.cc b/lite/api/paddle_api_test.cc index 02502ff9c8..ac5388e5dd 100644 --- a/lite/api/paddle_api_test.cc +++ b/lite/api/paddle_api_test.cc @@ -36,6 +36,8 @@ TEST(CxxApi, run) { auto predictor = lite_api::CreatePaddlePredictor(config); + LOG(INFO) << "Version: " << predictor->GetVersion(); + auto input_tensor = predictor->GetInput(0); input_tensor->Resize(std::vector({100, 100})); auto* data = input_tensor->mutable_data(); @@ -66,6 +68,8 @@ TEST(LightApi, run) { auto predictor = lite_api::CreatePaddlePredictor(config); + LOG(INFO) << "Version: " << predictor->GetVersion(); + auto input_tensor = predictor->GetInput(0); input_tensor->Resize(std::vector({100, 100})); auto* data = input_tensor->mutable_data(); diff --git a/lite/core/CMakeLists.txt b/lite/core/CMakeLists.txt index fa401827e0..0983b86d3c 100644 --- a/lite/core/CMakeLists.txt +++ b/lite/core/CMakeLists.txt @@ -38,6 +38,33 @@ else() lite_cc_library(context SRCS context.cc DEPS tensor any device_info eigen3 CL_DEPS cl_context gflags) endif() +#-------------------------------------------- GET CODE META INFO ------------------------------------------ +execute_process( + COMMAND git describe --tags --exact-match + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE PADDLE_LITE_TAG + OUTPUT_STRIP_TRAILING_WHITESPACE +) + +execute_process( + COMMAND git rev-parse --abbrev-ref HEAD + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE PADDLE_LITE_BRANCH + OUTPUT_STRIP_TRAILING_WHITESPACE +) + +execute_process( + COMMAND git log -1 --format=%h + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE PADDLE_LITE_COMMIT + OUTPUT_STRIP_TRAILING_WHITESPACE +) + +message(STATUS "tag: ${PADDLE_LITE_TAG}") +message(STATUS "branch: ${PADDLE_LITE_BRANCH}") +message(STATUS "commit: ${PADDLE_LITE_COMMIT}") + +configure_file(version.h.in version.h) #----------------------------------------------- NOT CHANGE ----------------------------------------------- # A trick to generate the paddle_use_kernels.h add_custom_command( diff --git a/lite/core/version.h.in b/lite/core/version.h.in new file mode 100644 index 0000000000..13f6a3b8e3 --- /dev/null +++ b/lite/core/version.h.in @@ -0,0 +1,49 @@ +// Copyright (c) 2019 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. + +#pragma once + +#include +#include "lite/utils/replace_stl/stream.h" + +namespace paddle { +namespace lite { + +static std::string paddlelite_commit() { + return "@PADDLE_LITE_COMMIT@"; +} + +static std::string paddlelite_branch() { + return "@PADDLE_LITE_BRANCH@"; +} + +static std::string paddlelite_tag() { + return "@PADDLE_LITE_TAG@"; +} + +static std::string version() { + STL::stringstream ss; + + std::string tag = paddlelite_tag(); + if (tag.empty()) { + ss << paddlelite_branch() << "(" << paddlelite_commit() << ")"; + } else { + ss << tag; + } + + return ss.str(); +} + +} // namespace lite +} // namespace paddle -- GitLab