From a5b2b08035d36c396f23906c8a19864d3549a11f Mon Sep 17 00:00:00 2001 From: zhoufeng Date: Wed, 30 Dec 2020 09:40:31 +0800 Subject: [PATCH] cpp st fwk and script Signed-off-by: zhoufeng --- .gitignore | 1 + CMakeLists.txt | 2 +- build.sh | 21 ++++- cmake/options.cmake | 1 + tests/CMakeLists.txt | 6 +- tests/cxx_st/common/common_test.cc | 40 -------- tests/cxx_st/runtest.sh | 43 --------- tests/{cxx_st => st/cpp}/CMakeLists.txt | 2 + tests/st/cpp/common/common_test.cc | 81 ++++++++++++++++ tests/{cxx_st => st/cpp}/common/common_test.h | 18 +--- tests/{cxx_st => st/cpp}/common/test_main.cc | 0 tests/{cxx_st => st/cpp}/dataset/test_de.cc | 2 + .../cpp}/model/test_tensor_add.cc | 5 +- tests/st/cpp/runtest.sh | 94 +++++++++++++++++++ 14 files changed, 207 insertions(+), 109 deletions(-) delete mode 100644 tests/cxx_st/common/common_test.cc delete mode 100755 tests/cxx_st/runtest.sh rename tests/{cxx_st => st/cpp}/CMakeLists.txt (72%) create mode 100644 tests/st/cpp/common/common_test.cc rename tests/{cxx_st => st/cpp}/common/common_test.h (80%) rename tests/{cxx_st => st/cpp}/common/test_main.cc (100%) rename tests/{cxx_st => st/cpp}/dataset/test_de.cc (99%) rename tests/{cxx_st => st/cpp}/model/test_tensor_add.cc (94%) create mode 100755 tests/st/cpp/runtest.sh diff --git a/.gitignore b/.gitignore index 9bbd42ed631..2258c328f2d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ build/ mindspore/lib output *.ir +st_tests # mindspore lite java mindspore/lite/java/java/.gradle diff --git a/CMakeLists.txt b/CMakeLists.txt index b724259f147..373123378f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,7 +77,7 @@ endif () set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden") add_subdirectory(mindspore/ccsrc) add_subdirectory(mindspore/core) -if (ENABLE_TESTCASES) +if (ENABLE_TESTCASES OR ENABLE_CPP_ST) add_subdirectory(tests) endif() diff --git a/build.sh b/build.sh index 93646221249..3ace0d3af65 100755 --- a/build.sh +++ b/build.sh @@ -22,7 +22,7 @@ export BUILD_PATH="${BASEPATH}/build/" usage() { echo "Usage:" - echo "bash build.sh [-d] [-r] [-v] [-c on|off] [-t on|off] [-g on|off] [-h] [-b ge] [-m infer|train] \\" + echo "bash build.sh [-d] [-r] [-v] [-c on|off] [-t ut|st] [-g on|off] [-h] [-b ge] [-m infer|train] \\" echo " [-a on|off] [-p on|off] [-i] [-L] [-R] [-D on|off] [-j[n]] [-e gpu|ascend|cpu|npu] \\" echo " [-P on|off] [-z [on|off]] [-M on|off] [-V 9.2|10.1|310|910] [-I arm64|arm32|x86_64] [-K] \\" echo " [-B on|off] [-E] [-l on|off] [-n full|lite|off] [-T on|off] \\" @@ -33,7 +33,7 @@ usage() echo " -r Release mode, default mode" echo " -v Display build command" echo " -c Enable code coverage, default off" - echo " -t Run testcases, default on" + echo " -t Run testcases, default off" echo " -g Use glog to output log, default on" echo " -h Print usage" echo " -b Select other backend, available: \\" @@ -86,6 +86,7 @@ checkopts() VERBOSE="" ENABLE_COVERAGE="off" RUN_TESTCASES="off" + RUN_CPP_ST_TESTS="off" ENABLE_BACKEND="" TRAIN_MODE="INFER" ENABLE_ASAN="off" @@ -152,8 +153,17 @@ checkopts() ENABLE_COVERAGE="$OPTARG" ;; t) - check_on_off $OPTARG t - RUN_TESTCASES="$OPTARG" + if [[ "X$OPTARG" == "Xon" || "X$OPTARG" == "Xut" ]]; then + RUN_TESTCASES="on" + elif [[ "X$OPTARG" == "Xoff" ]]; then + RUN_TESTCASES="off" + elif [[ "X$OPTARG" == "Xst" ]]; then + RUN_CPP_ST_TESTS="on" + else + echo "Invalid value ${OPTARG} for option -t" + usage + exit 1 + fi ;; g) check_on_off $OPTARG g @@ -406,6 +416,9 @@ build_mindspore() if [[ "X$RUN_TESTCASES" = "Xon" ]]; then CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_TESTCASES=ON" fi + if [[ "X$RUN_CPP_ST_TESTS" = "Xon" ]]; then + CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_CPP_ST=ON" + fi if [[ -n "$ENABLE_BACKEND" ]]; then CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_${ENABLE_BACKEND}=ON" fi diff --git a/cmake/options.cmake b/cmake/options.cmake index 8578c899f2b..92a1594fa19 100644 --- a/cmake/options.cmake +++ b/cmake/options.cmake @@ -5,6 +5,7 @@ option(ENABLE_GE "Enable graph engine as backend to execute" OFF) option(ENABLE_MINDDATA "Enable minddata compile" OFF) option(ENABLE_TRAIN "Enable ge train, default off(only infer)" OFF) option(ENABLE_TESTCASES "Run testcases switch, default off" OFF) +option(ENABLE_CPP_ST "Run cpp st testcases switch, default off" OFF) option(DEBUG_MODE "Debug mode, default off" OFF) option(ENABLE_ASAN "Enable Google Sanitizer to find memory bugs") option(ENABLE_LOAD_ANF_IR "Enable load ANF-IR as input of 'infer' stage of pipeline" OFF) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7fcabe615b6..a4639c6a578 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,8 +1,8 @@ #add flags set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-sign-compare") -if (ENABLE_ACL) - add_subdirectory(cxx_st) -else () +if (ENABLE_TESTCASES) add_subdirectory(ut) +elseif (ENABLE_CPP_ST) + add_subdirectory(st/cpp) endif () diff --git a/tests/cxx_st/common/common_test.cc b/tests/cxx_st/common/common_test.cc deleted file mode 100644 index c50f2f62009..00000000000 --- a/tests/cxx_st/common/common_test.cc +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * 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 "common/common_test.h" - -#ifdef __cplusplus -#if __cplusplus -extern "C" { -#endif -#endif - -namespace ST { - -void Common::SetUpTestCase() {} - -void Common::TearDownTestCase() {} - -void Common::SetUp() {} - -void Common::TearDown() {} - -} // namespace ST - -#ifdef __cplusplus -#if __cplusplus -} -#endif -#endif diff --git a/tests/cxx_st/runtest.sh b/tests/cxx_st/runtest.sh deleted file mode 100755 index 0db46c6216e..00000000000 --- a/tests/cxx_st/runtest.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/bash -# Copyright 2019 Huawei Technologies Co., Ltd -# -# 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. -# ============================================================================ - -set -e -BASEPATH=$(cd "$(dirname $0)"; pwd) -PROJECT_PATH=${BASEPATH}/../.. -if [ $BUILD_PATH ];then - echo "BUILD_PATH = $BUILD_PATH" -else - BUILD_PATH=${PROJECT_PATH}/build - echo "BUILD_PATH = $BUILD_PATH" -fi - -cd ${BUILD_PATH}/mindspore/tests/cxx_st - -export LD_LIBRARY_PATH=${BUILD_PATH}/mindspore/googletest/googlemock/gtest:${PROJECT_PATH}/mindspore:${PROJECT_PATH}/mindspore/lib:$LD_LIBRARY_PATH -export PYTHONPATH=${PROJECT_PATH}/tests/ut/cpp/python_input:$PYTHONPATH:${PROJECT_PATH} -export GLOG_v=2 -export GC_COLLECT_IN_CELL=1 - - -if [ $# -gt 0 ]; then - ./st_tests --gtest_filter=$1 -else - ./st_tests -fi -RET=$? -cd - - -exit ${RET} diff --git a/tests/cxx_st/CMakeLists.txt b/tests/st/cpp/CMakeLists.txt similarity index 72% rename from tests/cxx_st/CMakeLists.txt rename to tests/st/cpp/CMakeLists.txt index 32d8c749a00..a4abc71f61e 100644 --- a/tests/cxx_st/CMakeLists.txt +++ b/tests/st/cpp/CMakeLists.txt @@ -10,3 +10,5 @@ include_directories(${CUDA_INCLUDE_DIRS}) file(GLOB_RECURSE CXX_ST_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cc) add_executable(st_tests ${CXX_ST_SRC}) target_link_libraries(st_tests PRIVATE mindspore_shared_lib _c_dataengine mindspore::gtest) +set_target_properties(st_tests PROPERTIES SKIP_BUILD_RPATH TRUE RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) +file(COPY ${gtest_LIBPATH}/libgtest${CMAKE_SHARED_LIBRARY_SUFFIX} DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}) \ No newline at end of file diff --git a/tests/st/cpp/common/common_test.cc b/tests/st/cpp/common/common_test.cc new file mode 100644 index 00000000000..ad8de4d3226 --- /dev/null +++ b/tests/st/cpp/common/common_test.cc @@ -0,0 +1,81 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * 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 "common/common_test.h" +#include "include/api/context.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif + +namespace ST { +static std::string GetEnv(const std::string &envvar) { + const char *value = std::getenv(envvar.c_str()); + if (value == nullptr) { + return ""; + } + + return std::string(value); +} + +void Common::SetUpTestCase() {} + +void Common::TearDownTestCase() {} + +void Common::SetUp() {} + +void Common::TearDown() {} + +void Common::ReadFile(const char *file, size_t *size, char **buf) { + ASSERT_NE(nullptr, file); + ASSERT_NE(nullptr, size); + ASSERT_NE(nullptr, buf); + std::string path = std::string(file); + std::ifstream ifs(path); + ASSERT_EQ(true, ifs.good()); + ASSERT_EQ(true, ifs.is_open()); + + ifs.seekg(0, std::ios::end); + *size = ifs.tellg(); + *buf = new char[*size]; + + ifs.seekg(0, std::ios::beg); + ifs.read(*buf, *size); + ifs.close(); +} + +void Common::ContextAutoSet() { + auto device_target = GetEnv("DEVICE_TARGET"); + if (device_target.empty()) { + device_target = mindspore::api::kDeviceTypeAscend310; // default is 310 + } + + auto device_id_str = GetEnv("DEVICE_ID"); + if (device_id_str.empty()) { + device_id_str = "0"; // default is 0 + } + uint32_t device_id = std::strtoul(device_id_str.c_str(), nullptr, 10); + + mindspore::api::Context::Instance().SetDeviceTarget(device_target).SetDeviceID(device_id); +} +} // namespace ST + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif diff --git a/tests/cxx_st/common/common_test.h b/tests/st/cpp/common/common_test.h similarity index 80% rename from tests/cxx_st/common/common_test.h rename to tests/st/cpp/common/common_test.h index 0cc4fe2de76..9dc3417a047 100644 --- a/tests/cxx_st/common/common_test.h +++ b/tests/st/cpp/common/common_test.h @@ -54,23 +54,9 @@ class Common : public testing::Test { } } - void ReadFile(const char *file, size_t *size, char **buf) { - ASSERT_NE(nullptr, file); - ASSERT_NE(nullptr, size); - ASSERT_NE(nullptr, buf); - std::string path = std::string(file); - std::ifstream ifs(path); - ASSERT_EQ(true, ifs.good()); - ASSERT_EQ(true, ifs.is_open()); + void ReadFile(const char *file, size_t *size, char **buf); - ifs.seekg(0, std::ios::end); - *size = ifs.tellg(); - *buf = new char[*size]; - - ifs.seekg(0, std::ios::beg); - ifs.read(*buf, *size); - ifs.close(); - } + void ContextAutoSet(); }; } // namespace ST #endif // TESTS_CXX_ST_COMMON_COMMON_TEST_H_ diff --git a/tests/cxx_st/common/test_main.cc b/tests/st/cpp/common/test_main.cc similarity index 100% rename from tests/cxx_st/common/test_main.cc rename to tests/st/cpp/common/test_main.cc diff --git a/tests/cxx_st/dataset/test_de.cc b/tests/st/cpp/dataset/test_de.cc similarity index 99% rename from tests/cxx_st/dataset/test_de.cc rename to tests/st/cpp/dataset/test_de.cc index 44170b85dd5..57b4f0f75ba 100644 --- a/tests/cxx_st/dataset/test_de.cc +++ b/tests/st/cpp/dataset/test_de.cc @@ -57,6 +57,8 @@ TEST_F(TestDE, TestResNetPreprocess) { } TEST_F(TestDE, TestDvpp) { + ContextAutoSet(); + // Read images from target directory std::vector> images; MindDataEager::LoadImageFromDir("/home/workspace/mindspore_dataset/imagenet/imagenet_original/val/n01440764", diff --git a/tests/cxx_st/model/test_tensor_add.cc b/tests/st/cpp/model/test_tensor_add.cc similarity index 94% rename from tests/cxx_st/model/test_tensor_add.cc rename to tests/st/cpp/model/test_tensor_add.cc index c820af51884..fda2dba0e15 100644 --- a/tests/cxx_st/model/test_tensor_add.cc +++ b/tests/st/cpp/model/test_tensor_add.cc @@ -22,7 +22,7 @@ using namespace mindspore::api; -static const char tensor_add_file[] = "/home/workspace/mindspore_dataset/tensor_add/tensor_add.mindir"; +static const char tensor_add_file[] = "/home/workspace/mindspore_dataset/mindir/tensor_add/tensor_add.mindir"; static const std::vector input_data_1 = {1, 2, 3, 4}; static const std::vector input_data_2 = {2, 3, 4, 5}; @@ -32,7 +32,8 @@ class TestTensorAdd : public ST::Common { }; TEST_F(TestTensorAdd, InferMindIR) { - Context::Instance().SetDeviceTarget(kDeviceTypeAscend310).SetDeviceID(1); + ContextAutoSet(); + auto graph = Serialization::LoadModel(tensor_add_file, ModelType::kMindIR); Model tensor_add((GraphCell(graph))); Status ret = tensor_add.Build({}); diff --git a/tests/st/cpp/runtest.sh b/tests/st/cpp/runtest.sh new file mode 100755 index 00000000000..f6469424d09 --- /dev/null +++ b/tests/st/cpp/runtest.sh @@ -0,0 +1,94 @@ +#!/bin/bash +# Copyright 2019 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ + +set -e +BASEPATH=$(cd "$(dirname $0)"; pwd) +PROJECT_PATH=${BASEPATH}/../../.. + +# print usage message +usage() +{ + echo "Usage:" + echo "sh runtests.sh [-e ascend310|ascend910] [-n testcase_name] [-d n]" + echo "" + echo "Options:" + echo " -h Print usage" + echo " -e Device target, default is ascend310" + echo " -d Device ID, default is 0" + echo " -n Run single tesecase, default off" + echo "to be continued ..." +} + +checkopts() +{ + DEVICE_TARGET_OPT="ascend310" + DEVICE_ID_OPT=0 + TASECASE_NAME_OPT="" + + # Process the options + while getopts 'he:d:n:' opt + do + case "${opt}" in + h) + usage + exit 0 + ;; + e) + DEVICE_TARGET_OPT=$(echo ${OPTARG} | tr '[A-Z]' '[a-z]') + ;; + d) + DEVICE_ID_OPT=$OPTARG + ;; + n) + TASECASE_NAME_OPT=$OPTARG + ;; + *) + echo "Undefined option: ${opt}" + usage + exit 1 + esac + done +} +checkopts "$@" + +cd ${PROJECT_PATH}/tests/st/cpp + +MINDSPORE_PKG_PATH=`python -m pip show mindspore-ascend | grep Location | awk '{print $2"/mindspore"}' | xargs realpath` +if [[ "X${MINDSPORE_PKG_PATH}" == "X" ]]; then + MINDSPORE_PKG_PATH=${PROJECT_PATH}/build/package/mindspore: +fi + +export LD_LIBRARY_PATH=${MINDSPORE_PKG_PATH}:${MINDSPORE_PKG_PATH}/lib:${PROJECT_PATH}/tests/st/cpp:$LD_LIBRARY_PATH +export GLOG_v=2 +export GC_COLLECT_IN_CELL=1 +export DEVICE_ID=$DEVICE_ID_OPT +if [[ "X$DEVICE_TARGET_OPT" == "Xascend310" ]]; then + export DEVICE_TARGET=Ascend310 +elif [[ "X$DEVICE_TARGET_OPT" == "Xascend910" ]]; then + export DEVICE_TARGET=Ascend910 +else + export DEVICE_TARGET=$DEVICE_TARGET_OPT +fi + +if [[ "X$TASECASE_NAME_OPT" != "X" ]]; then + ./st_tests --gtest_filter=$TASECASE_NAME_OPT +else + ./st_tests +fi +RET=$? +cd - + +exit ${RET}