cpp st fwk and script

Signed-off-by: zhoufeng <zhoufeng54@huawei.com>
This commit is contained in:
zhoufeng 2020-12-30 09:40:31 +08:00
parent 28593e23c8
commit a5b2b08035
14 changed files with 207 additions and 109 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@ build/
mindspore/lib
output
*.ir
st_tests
# mindspore lite java
mindspore/lite/java/java/.gradle

View File

@ -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()

View File

@ -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

View File

@ -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)

View File

@ -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 ()

View File

@ -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

View File

@ -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}

View File

@ -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})

View File

@ -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

View File

@ -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_

View File

@ -57,6 +57,8 @@ TEST_F(TestDE, TestResNetPreprocess) {
}
TEST_F(TestDE, TestDvpp) {
ContextAutoSet();
// Read images from target directory
std::vector<std::shared_ptr<Tensor>> images;
MindDataEager::LoadImageFromDir("/home/workspace/mindspore_dataset/imagenet/imagenet_original/val/n01440764",

View File

@ -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<float> input_data_1 = {1, 2, 3, 4};
static const std::vector<float> 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({});

94
tests/st/cpp/runtest.sh Executable file
View File

@ -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}