From be70630687bdf6bba42f61fa63fe38edbaee555e Mon Sep 17 00:00:00 2001 From: superjomn Date: Sun, 5 May 2019 19:38:19 +0800 Subject: [PATCH] enable lite_test with light weight framework --- cmake/generic.cmake | 38 +++++++++++++++++++++++ paddle/fluid/lite/CMakeLists.txt | 4 ++- paddle/fluid/lite/api/CMakeLists.txt | 7 +++-- paddle/fluid/lite/api/cxx_api.cc | 2 +- paddle/fluid/lite/api/cxx_api.h | 4 +-- paddle/fluid/lite/api/cxx_api_test.cc | 4 +-- paddle/fluid/lite/api/light_api.h | 4 +-- paddle/fluid/lite/api/light_api_test.cc | 2 +- paddle/fluid/lite/core/CMakeLists.txt | 1 + paddle/fluid/lite/core/lite_gtest_main.cc | 21 +++++++++++++ paddle/fluid/lite/core/mir/CMakeLists.txt | 12 +++++-- paddle/fluid/lite/core/op_lite.h | 4 --- paddle/fluid/lite/kernels/CMakeLists.txt | 4 ++- 13 files changed, 87 insertions(+), 20 deletions(-) create mode 100644 paddle/fluid/lite/core/lite_gtest_main.cc diff --git a/cmake/generic.cmake b/cmake/generic.cmake index 19110812c24..43350333a5f 100644 --- a/cmake/generic.cmake +++ b/cmake/generic.cmake @@ -403,6 +403,44 @@ function(cc_test TARGET_NAME) endif() endfunction(cc_test) +# cc_test without default dependencies +function(raw_cc_test TARGET_NAME) + if(WITH_TESTING) + set(options SERIAL) + set(oneValueArgs "") + set(multiValueArgs SRCS DEPS ARGS) + cmake_parse_arguments(cc_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + add_executable(${TARGET_NAME} ${cc_test_SRCS}) + if(WIN32) + if("${cc_test_DEPS};" MATCHES "python;") + list(REMOVE_ITEM cc_test_DEPS python) + target_link_libraries(${TARGET_NAME} ${PYTHON_LIBRARIES}) + endif() + endif(WIN32) + get_property(os_dependency_modules GLOBAL PROPERTY OS_DEPENDENCY_MODULES) + target_link_libraries(${TARGET_NAME} ${cc_test_DEPS} ${os_dependency_modules} lite_gtest_main gtest gflags glog) + add_dependencies(${TARGET_NAME} ${cc_test_DEPS} lite_gtest_main gtest gflags glog) + common_link(${TARGET_NAME}) + add_test(NAME ${TARGET_NAME} + COMMAND ${TARGET_NAME} ${cc_test_ARGS} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + if (${cc_test_SERIAL}) + set_property(TEST ${TARGET_NAME} PROPERTY RUN_SERIAL 1) + endif() + # No unit test should exceed 10 minutes. + set_tests_properties(${TARGET_NAME} PROPERTIES TIMEOUT 600) + endif() +endfunction(raw_cc_test) + +function(lite_cc_test args) + if (LITE_WITH_LIGHT_WEIGHT_FRAMEWORK) + message(STATUS "building lite raw test: ${args}") + raw_cc_test(${args} ${ARGN}) + else() + cc_test(${args} ${ARGN}) + endif() +endfunction() + function(nv_library TARGET_NAME) if (WITH_GPU) set(options STATIC static SHARED shared) diff --git a/paddle/fluid/lite/CMakeLists.txt b/paddle/fluid/lite/CMakeLists.txt index 98357922eb8..75595cc17e1 100644 --- a/paddle/fluid/lite/CMakeLists.txt +++ b/paddle/fluid/lite/CMakeLists.txt @@ -1,7 +1,9 @@ add_subdirectory(core) add_subdirectory(x86) add_subdirectory(host) -add_subdirectory(cuda) +if(LITE_WITH_CUDA) + add_subdirectory(cuda) +endif() add_subdirectory(operators) add_subdirectory(kernels) add_subdirectory(model_parser) diff --git a/paddle/fluid/lite/api/CMakeLists.txt b/paddle/fluid/lite/api/CMakeLists.txt index 4fa6b6d34d6..abfdca48152 100644 --- a/paddle/fluid/lite/api/CMakeLists.txt +++ b/paddle/fluid/lite/api/CMakeLists.txt @@ -1,5 +1,6 @@ -set(cxx_api_lite_deps scope_lite host_kernels ops_lite optimizer_lite target_wrapper_host kernels_cuda optimizer_lite model_parser_lite) +set(cxx_api_lite_deps scope_lite host_kernels ops_lite optimizer_lite target_wrapper_host optimizer_lite model_parser_lite) if(LITE_WITH_CUDA) + set(cxx_api_lite_deps ${cxx_api_lite_deps} kernels_cuda) cc_library(cxx_api_lite_cuda SRCS cxx_api.cc DEPS ${cxx_api_lite_deps} target_wrapper_cuda) nv_test(test_cxx_api_lite_cuda SRCS cxx_api_test.cc DEPS cxx_api_lite_cuda) endif() @@ -15,5 +16,5 @@ endif() cc_library(light_api_lite SRCS light_api.cc DEPS ${light_api_deps}) -cc_test(test_cxx_api_lite SRCS cxx_api_test.cc DEPS cxx_api_lite model_parser_lite target_wrapper_host host_kernels) -cc_test(test_light_api SRCS light_api_test.cc DEPS light_api_lite) +lite_cc_test(test_cxx_api_lite SRCS cxx_api_test.cc DEPS cxx_api_lite model_parser_lite target_wrapper_host host_kernels) +lite_cc_test(test_light_api SRCS light_api_test.cc DEPS light_api_lite) diff --git a/paddle/fluid/lite/api/cxx_api.cc b/paddle/fluid/lite/api/cxx_api.cc index 48ce5f46520..04bcb388f62 100644 --- a/paddle/fluid/lite/api/cxx_api.cc +++ b/paddle/fluid/lite/api/cxx_api.cc @@ -18,7 +18,7 @@ namespace paddle { namespace lite { -void CxxPredictor::SaveModel(const std::string &dir) { +void LightPredictor::SaveModel(const std::string &dir) { MkDirRecursively(dir.c_str()); program_->PersistModel(dir, program_desc_); } diff --git a/paddle/fluid/lite/api/cxx_api.h b/paddle/fluid/lite/api/cxx_api.h index bc3b007d45e..81f5694fbae 100644 --- a/paddle/fluid/lite/api/cxx_api.h +++ b/paddle/fluid/lite/api/cxx_api.h @@ -25,9 +25,9 @@ namespace lite { struct Config {}; -class CxxPredictor { +class LightPredictor { public: - CxxPredictor() { scope_ = std::make_shared(); } + LightPredictor() { scope_ = std::make_shared(); } void Build(const std::string& model_path, const Place& prefer_place, const std::vector& valid_places) { diff --git a/paddle/fluid/lite/api/cxx_api_test.cc b/paddle/fluid/lite/api/cxx_api_test.cc index dd44ad937c7..cf78a3fe56d 100644 --- a/paddle/fluid/lite/api/cxx_api_test.cc +++ b/paddle/fluid/lite/api/cxx_api_test.cc @@ -22,7 +22,7 @@ namespace paddle { namespace lite { TEST(CXXApi, test) { - lite::CxxPredictor predictor; + lite::LightPredictor predictor; #ifndef LITE_WITH_CUDA std::vector valid_places({Place{TARGET(kHost), PRECISION(kFloat)}}); #else @@ -60,7 +60,7 @@ TEST(CXXApi, test) { } TEST(CXXApi, save_model) { - lite::CxxPredictor predictor; + lite::LightPredictor predictor; std::vector valid_places({Place{TARGET(kHost), PRECISION(kFloat)}}); predictor.Build("/home/chunwei/project/models/model2", Place{TARGET(kCUDA), PRECISION(kFloat)}, valid_places); diff --git a/paddle/fluid/lite/api/light_api.h b/paddle/fluid/lite/api/light_api.h index 78967896b1b..484af4d339b 100644 --- a/paddle/fluid/lite/api/light_api.h +++ b/paddle/fluid/lite/api/light_api.h @@ -28,9 +28,9 @@ namespace paddle { namespace lite { -class CxxPredictor { +class LightPredictor { public: - CxxPredictor() { scope_ = std::make_shared(); } + LightPredictor() { scope_ = std::make_shared(); } void Build(const std::string& model_dir) { framework::proto::ProgramDesc desc; diff --git a/paddle/fluid/lite/api/light_api_test.cc b/paddle/fluid/lite/api/light_api_test.cc index e0398824b68..e5f577dc911 100644 --- a/paddle/fluid/lite/api/light_api_test.cc +++ b/paddle/fluid/lite/api/light_api_test.cc @@ -23,7 +23,7 @@ const std::string model_dir = "api/optimized_model"; TEST(LightAPI, load) { - CxxPredictor predictor; + LightPredictor predictor; predictor.Build(model_dir); auto* input_tensor = predictor.GetInput(0); diff --git a/paddle/fluid/lite/core/CMakeLists.txt b/paddle/fluid/lite/core/CMakeLists.txt index fe84114739a..6eb988385b4 100644 --- a/paddle/fluid/lite/core/CMakeLists.txt +++ b/paddle/fluid/lite/core/CMakeLists.txt @@ -1,3 +1,4 @@ +cc_library(lite_gtest_main SRCS lite_gtest_main.cc) cc_library(memory_lite SRCS memory.cc) cc_library(target_wrapper_lite SRCS target_wrapper.cc) cc_library(tensor_lite SRCS tensor.cc DEPS memory_lite target_wrapper_lite) diff --git a/paddle/fluid/lite/core/lite_gtest_main.cc b/paddle/fluid/lite/core/lite_gtest_main.cc new file mode 100644 index 00000000000..9f9bd7ba467 --- /dev/null +++ b/paddle/fluid/lite/core/lite_gtest_main.cc @@ -0,0 +1,21 @@ +// 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. + +#include + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +} diff --git a/paddle/fluid/lite/core/mir/CMakeLists.txt b/paddle/fluid/lite/core/mir/CMakeLists.txt index 1a2a0d3697d..1ba5a3dae87 100644 --- a/paddle/fluid/lite/core/mir/CMakeLists.txt +++ b/paddle/fluid/lite/core/mir/CMakeLists.txt @@ -24,14 +24,20 @@ cc_test(test_ssa_graph SRCS ssa_graph_test.cc DEPS mir_pass_manager program_fake_utils ) -cc_test(test_variable_place_infrence_pass SRCS variable_place_inference_pass_test.cc DEPS +set(test_variable_place_infrence_pass_DEPS ops_lite host_kernels - kernels_cuda mir_passes mir_pass_manager optimizer_lite program_fake_utils target_wrapper_host - target_wrapper_cuda ) +if (LITE_WITH_CUDA) + set(test_variable_place_infrence_pass_DEPS + ${test_variable_place_infrence_pass_DEPS} target_wrapper_cuda + kernels_cuda + ) +endif() +cc_test(test_variable_place_infrence_pass SRCS variable_place_inference_pass_test.cc DEPS + ${test_variable_place_infrence_pass_DEPS}) diff --git a/paddle/fluid/lite/core/op_lite.h b/paddle/fluid/lite/core/op_lite.h index bfeb32d1a72..8b578c58282 100644 --- a/paddle/fluid/lite/core/op_lite.h +++ b/paddle/fluid/lite/core/op_lite.h @@ -15,7 +15,6 @@ #pragma once #include -#include #include #include #include @@ -28,9 +27,6 @@ namespace paddle { namespace lite { -using any_t = boost::variant; -using anys_t = std::map; - // For registry factory. struct Registry { void Touch() {} diff --git a/paddle/fluid/lite/kernels/CMakeLists.txt b/paddle/fluid/lite/kernels/CMakeLists.txt index ebbfb2139e5..5c2883d5379 100644 --- a/paddle/fluid/lite/kernels/CMakeLists.txt +++ b/paddle/fluid/lite/kernels/CMakeLists.txt @@ -1,4 +1,6 @@ set(lite_kernel_deps type_system kernel_lite op_registry_lite) add_subdirectory(host) add_subdirectory(arm) -add_subdirectory(cuda) +if(LITE_WITH_CUDA) + add_subdirectory(cuda) +endif() -- GitLab