From 9eab4991aef788e4107085026f05a83ee97c170e Mon Sep 17 00:00:00 2001 From: huanruizhi Date: Mon, 31 May 2021 10:42:02 +0800 Subject: [PATCH] tfadapter ut --- CMakeLists.txt | 27 +- build.sh | 59 +- cmake/secure_c.cmake | 2 +- cmake/tests/gtest.cmake | 64 ++ cmake/tests/intf_pub_linux.cmake | 34 + cmake/tests/securec.cmake | 68 ++ configure.py | 9 +- tf_adapter/kernels/geop_npu.cc | 4 - tf_adapter/kernels/npu_sys_ctl_ops.cc | 4 - .../optimizers/gradient_fusion_optimizer.cc | 208 ---- .../optimizers/om_partition_subgraphs_pass.cc | 8 - tf_adapter/tests/CMakeLists.txt | 42 + tf_adapter/tests/depends/alog/CMakeLists.txt | 27 + .../tests/depends/alog/src/alog_stub.cc | 31 + tf_adapter/tests/depends/aoe/CMakeLists.txt | 27 + .../tests/depends/datatransfer/CMakeLists.txt | 27 + .../datatransfer/src/datatransfer_stub.cc | 33 + .../tests/depends/ge_runner/CMakeLists.txt | 31 + .../depends/ge_runner/src/ge_runner_stub.cc | 330 ++++++ .../depends/indextransform/CMakeLists.txt | 27 + .../indextransform/src/indextransform_stub.cc | 22 + tf_adapter/tests/depends/mmpa/CMakeLists.txt | 30 + .../tests/depends/mmpa/src/mmpa_stub.cc | 81 ++ .../0001-add-securec-cmake-script.patch | 105 ++ .../tests/patch/securec/securec.patch001 | 22 + tf_adapter/tests/ut/CMakeLists.txt | 66 ++ tf_adapter/tests/ut/kernels/pbtxt/geop.pbtxt | 521 +++++++++ .../geop_dynamic_input_lazy_recompile.pbtxt | 1019 +++++++++++++++++ .../ut/kernels/testcase/geop_npu_test.cc | 146 +++ tf_adapter/tests/ut/main.cc | 7 + .../pbtxt/add_input_pass_localhost_test.pbtxt | 162 +++ ...add_input_pass_no_need_optimize_test.pbtxt | 168 +++ .../pbtxt/add_input_pass_worker_test.pbtxt | 162 +++ .../ut/optimizers/pbtxt/ctrl_if_test.pbtxt | 273 +++++ .../optimizers/pbtxt/dp_test_build_geop.pbtxt | 319 ++++++ .../pbtxt/dp_test_no_dataset_in_device.pbtxt | 368 ++++++ .../pbtxt/get_attr_job_chief_test.pbtxt | 156 +++ .../optimizers/pbtxt/om_test_accumulate.pbtxt | 374 ++++++ .../optimizers/pbtxt/om_test_build_geop.pbtxt | 330 ++++++ .../pbtxt/om_test_copy_variables.pbtxt | 622 ++++++++++ .../pbtxt/om_test_getnext_build_geop.pbtxt | 122 ++ .../om_test_getnext_lazy_recompile.pbtxt | 238 ++++ .../pbtxt/om_test_in_out_pair.pbtxt | 168 +++ .../om_test_in_out_pair_flag_false.pbtxt | 168 +++ .../pbtxt/om_test_in_out_pair_flag_true.pbtxt | 174 +++ .../pbtxt/om_test_iterator_build_geop.pbtxt | 232 ++++ .../pbtxt/om_test_merge_clusters.pbtxt | 296 +++++ .../pbtxt/om_test_mix_compile.pbtxt | 207 ++++ .../optimizers/pbtxt/om_test_unaryops.pbtxt | 158 +++ .../pbtxt/weight_update_shard_test_func.pbtxt | 178 +++ .../testcase/add_input_pass_test.cc | 139 +++ .../testcase/dp_tf_ge_conversion_pass_test.cc | 102 ++ .../testcase/get_attr_optimize_pass_test.cc | 208 ++++ .../gradient_fusion_optimizer_test.cc | 20 + .../mark_and_ctrl_conversion_pass_test.cc | 116 ++ .../testcase/mark_start_node_pass_test.cc | 122 ++ .../om_partition_subgraphs_pass_test.cc | 189 +++ .../weight_update_sharding_pass_test.cc | 111 ++ .../tests/ut/util/testcase/ge_plugin_test.cc | 114 ++ .../tests/ut/util/testcase/npu_attrs_test.cc | 49 + 60 files changed, 8882 insertions(+), 244 deletions(-) create mode 100644 cmake/tests/gtest.cmake create mode 100644 cmake/tests/intf_pub_linux.cmake create mode 100644 cmake/tests/securec.cmake create mode 100644 tf_adapter/tests/CMakeLists.txt create mode 100644 tf_adapter/tests/depends/alog/CMakeLists.txt create mode 100644 tf_adapter/tests/depends/alog/src/alog_stub.cc create mode 100644 tf_adapter/tests/depends/aoe/CMakeLists.txt create mode 100644 tf_adapter/tests/depends/datatransfer/CMakeLists.txt create mode 100644 tf_adapter/tests/depends/datatransfer/src/datatransfer_stub.cc create mode 100644 tf_adapter/tests/depends/ge_runner/CMakeLists.txt create mode 100644 tf_adapter/tests/depends/ge_runner/src/ge_runner_stub.cc create mode 100644 tf_adapter/tests/depends/indextransform/CMakeLists.txt create mode 100644 tf_adapter/tests/depends/indextransform/src/indextransform_stub.cc create mode 100644 tf_adapter/tests/depends/mmpa/CMakeLists.txt create mode 100644 tf_adapter/tests/depends/mmpa/src/mmpa_stub.cc create mode 100644 tf_adapter/tests/patch/securec/0001-add-securec-cmake-script.patch create mode 100644 tf_adapter/tests/patch/securec/securec.patch001 create mode 100644 tf_adapter/tests/ut/CMakeLists.txt create mode 100644 tf_adapter/tests/ut/kernels/pbtxt/geop.pbtxt create mode 100644 tf_adapter/tests/ut/kernels/pbtxt/geop_dynamic_input_lazy_recompile.pbtxt create mode 100644 tf_adapter/tests/ut/kernels/testcase/geop_npu_test.cc create mode 100644 tf_adapter/tests/ut/main.cc create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/add_input_pass_localhost_test.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/add_input_pass_no_need_optimize_test.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/add_input_pass_worker_test.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/ctrl_if_test.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/dp_test_build_geop.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/dp_test_no_dataset_in_device.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/get_attr_job_chief_test.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/om_test_accumulate.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/om_test_build_geop.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/om_test_copy_variables.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/om_test_getnext_build_geop.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/om_test_getnext_lazy_recompile.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/om_test_in_out_pair.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/om_test_in_out_pair_flag_false.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/om_test_in_out_pair_flag_true.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/om_test_iterator_build_geop.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/om_test_merge_clusters.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/om_test_mix_compile.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/om_test_unaryops.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/pbtxt/weight_update_shard_test_func.pbtxt create mode 100644 tf_adapter/tests/ut/optimizers/testcase/add_input_pass_test.cc create mode 100644 tf_adapter/tests/ut/optimizers/testcase/dp_tf_ge_conversion_pass_test.cc create mode 100644 tf_adapter/tests/ut/optimizers/testcase/get_attr_optimize_pass_test.cc create mode 100644 tf_adapter/tests/ut/optimizers/testcase/gradient_fusion_optimizer_test.cc create mode 100644 tf_adapter/tests/ut/optimizers/testcase/mark_and_ctrl_conversion_pass_test.cc create mode 100644 tf_adapter/tests/ut/optimizers/testcase/mark_start_node_pass_test.cc create mode 100644 tf_adapter/tests/ut/optimizers/testcase/om_partition_subgraphs_pass_test.cc create mode 100644 tf_adapter/tests/ut/optimizers/testcase/weight_update_sharding_pass_test.cc create mode 100644 tf_adapter/tests/ut/util/testcase/ge_plugin_test.cc create mode 100644 tf_adapter/tests/ut/util/testcase/npu_attrs_test.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 31dad277c..688420148 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,17 +1,23 @@ option(ENABLE_OPEN_SRC "Enable graphengine compile in opensource." FALSE) + +set(TFADAPTER_DIR ${CMAKE_CURRENT_LIST_DIR}) + if (ENABLE_OPEN_SRC) cmake_minimum_required(VERSION 3.14) project(TFAdapter) set(CMAKE_CXX_STANDARD 11) - set(CMAKE_C_FLAGS "-O2 -DNDEBUG -Wno-deprecated-declarations -Wall -fPIC -fstack-protector-all -Wl,-z,relro,-z,now,-z,noexecstack -s -pipe ${CMAKE_C_FLAGS}") - set(CMAKE_CXX_FLAGS "-std=c++11 -O2 -DNDEBUG -Wno-deprecated-declarations -Wall -fPIC -fstack-protector-all -Wl,-z,relro,-z,now,-z,noexecstack -s -pipe ${CMAKE_CXX_FLAGS}") + set(CMAKE_C_FLAGS "-O0 -DNDEBUG -Wno-deprecated-declarations -Wall -fPIC -fstack-protector-all -Wl,-z,relro,-z,now,-z,noexecstack -pipe ${CMAKE_C_FLAGS}") + set(CMAKE_CXX_FLAGS "-std=c++11 -O0 -DNDEBUG -Wno-deprecated-declarations -Wall -fPIC -fstack-protector-all -Wl,-z,relro,-z,now,-z,noexecstack -pipe ${CMAKE_CXX_FLAGS}") + add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0) + # build external prjects if(DEFINED ENV{D_PKG_SERVER}) set(TF_PKG_SERVER $ENV{D_PKG_SERVER}) message("Download packages from PKG server") endif() + include(${CMAKE_CURRENT_LIST_DIR}/cmake/nlohmann_json.cmake) include(${CMAKE_CURRENT_LIST_DIR}/cmake/secure_c.cmake) include(${CMAKE_CURRENT_LIST_DIR}/cmake/tensorflow.cmake) @@ -35,6 +41,11 @@ if (ENABLE_OPEN_SRC) file(STRINGS "${CMAKE_CURRENT_LIST_DIR}/tools/LINK_FLAGS" LINK_FLAGS) file(STRINGS "${CMAKE_CURRENT_LIST_DIR}/tools/PYTHON_BIN_PATH" PYTHON_BIN_PATH) file(STRINGS "${CMAKE_CURRENT_LIST_DIR}/tools/SWIG_BIN_PATH" SWIG_BIN_PATH) + file(STRINGS "${CMAKE_CURRENT_LIST_DIR}/tools/UT_LINK_FLAGS" UT_LINK_FLAGS) + + if(ENABLE_TFADAPTER_UT) + add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/tf_adapter/tests) + endif() file(COPY ${CMAKE_CURRENT_LIST_DIR}/tf_adapter/python DESTINATION ${CMAKE_BINARY_DIR}/dist) file(COPY ${CMAKE_CURRENT_LIST_DIR}/convert_tf2npu DESTINATION ${CMAKE_BINARY_DIR}/dist/python/npu_bridge) @@ -42,7 +53,13 @@ if (ENABLE_OPEN_SRC) execute_process(COMMAND ${SWIG_BIN_PATH} -python -c++ -threads ${CMAKE_BINARY_DIR}/dist/swig/ge_plugin.i) file(COPY ${CMAKE_BINARY_DIR}/dist/swig/tf_adapter.py DESTINATION ${CMAKE_BINARY_DIR}/dist/python/npu_bridge) - file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_LIST_DIR}/tf_adapter/*.cc) + file(GLOB_RECURSE SOURCES + ${CMAKE_CURRENT_LIST_DIR}/tf_adapter/common/*.cc + ${CMAKE_CURRENT_LIST_DIR}/tf_adapter/kernels/*.cc + ${CMAKE_CURRENT_LIST_DIR}/tf_adapter/ops/*.cc + ${CMAKE_CURRENT_LIST_DIR}/tf_adapter/optimizers/*.cc + ${CMAKE_CURRENT_LIST_DIR}/tf_adapter/util/*.cc + ) add_library(_tf_adapter SHARED ${SOURCES} ${CMAKE_BINARY_DIR}/dist/swig/ge_plugin_wrap.cxx) foreach (COMPILE_FLAG ${COMPILE_FLAGS}) @@ -68,6 +85,7 @@ if (ENABLE_OPEN_SRC) VERBATIM ) else() + get_filename_component(TOP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.." ABSOLUTE) set(BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) set(CMAKE_VERBOSE_MAKEFILE ON) @@ -118,15 +136,12 @@ else() ${TOP_DIR}/inc/common/ ${TOP_DIR}/inc/soft_dp/ ${TOP_DIR}/soft_dp/ - ${TOP_DIR}/ace/execfwk/soft_dp/ ${TOP_DIR}/graphengine/inc/ ${TOP_DIR}/graphengine/inc/external/ ${TOP_DIR}/metadef/inc/ ${TOP_DIR}/metadef/inc/external/ ${TOP_DIR}/libc_sec/include/ - ${TOP_DIR}/abl/libc_sec/include/ ${TOP_DIR}/third_party/json/include/ - ${TOP_DIR}/open_source/json/include/ ${TOP_DIR}/third_party/tensorflow/tensorflow-1.15.0/ ${TOP_DIR}/third_party/tensorflow/compile_deps/tf-1.15.0/include/ ${HI_PYTHON_INC}/ diff --git a/build.sh b/build.sh index 72e4536a9..d912d366c 100644 --- a/build.sh +++ b/build.sh @@ -24,13 +24,14 @@ CMAKE_PATH="${BUILD_PATH}/tfadapter" # print usage message usage() { echo "Usage:" - echo " bash build.sh [-h] [-j[n]] [-v] [-g]" + echo " bash build.sh [-h] [-j[n]] [-v] [-g] [-u]" echo "" echo "Options:" echo " -h Print usage" echo " -j[n] Set the number of threads used to build CANN, default is 8" echo " -v Verbose" echo " -g GCC compiler prefix, used to specify the compiler toolchain" + echo " -u Tf_adapter utest" echo "to be continued ..." } @@ -43,8 +44,9 @@ checkopts() { VERBOSE="" THREAD_NUM=8 GCC_PREFIX="" + ENABLE_TFADAPTER_UT="off" # Process the options - while getopts 'hj:vg:' opt + while getopts 'hj:vug:' opt do case "${opt}" in h) usage @@ -52,6 +54,7 @@ checkopts() { j) THREAD_NUM=$OPTARG ;; v) VERBOSE="VERBOSE=1" ;; g) GCC_PREFIX=$OPTARG ;; + u) ENABLE_TFADAPTER_UT="on" ;; *) logging "Undefined option: ${opt}" usage exit 1 ;; @@ -68,26 +71,39 @@ mk_dir() { # create build path build_tfadapter() { + export OPEN_UT=1 + export D_PKG_SERVER=/home/hrz/tfadapter_ut/server_pkg/ logging "Create build directory and build tfadapter" cd "${BASE_PATH}" && ./configure - CMAKE_ARGS="-DENABLE_OPEN_SRC=True -DBUILD_PATH=$BUILD_PATH" + CMAKE_ARGS="-DENABLE_OPEN_SRC=True -DBUILD_PATH=$BUILD_PATH -DCMAKE_INSTALL_PREFIX=${RELEASE_PATH}" if [[ "$GCC_PREFIX" != "" ]]; then CMAKE_ARGS="$CMAKE_ARGS -DGCC_PREFIX=$GCC_PREFIX" fi + if [[ "X$ENABLE_TFADAPTER_UT" = "Xon" ]]; then + CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_TFADAPTER_UT=ON" + fi logging "CMake Args: ${CMAKE_ARGS}" mk_dir "${CMAKE_PATH}" cd "${CMAKE_PATH}" && cmake ${CMAKE_ARGS} ../.. - make ${VERBOSE} -j${THREAD_NUM} - logging "tfadapter build success!" - - chmod +x "${BASE_PATH}/tf_adapter_2.x/CI_Build" - sh "${BASE_PATH}/tf_adapter_2.x/CI_Build" + if [ 0 -ne $? ] + then + echo "execute command: cmake ${CMAKE_ARGS} .. failed." + return 1 + fi + if [[ "X$ENABLE_TFADAPTER_UT" = "Xon" ]]; then + make tfadapter_utest ${VERBOSE} -j${THREAD_NUM} + logging "Build tfadapter utest success!" + else + make ${VERBOSE} -j${THREAD_NUM} + logging "tfadapter build success!" + chmod +x "${BASE_PATH}/tf_adapter_2.x/CI_Build" + sh "${BASE_PATH}/tf_adapter_2.x/CI_Build" + fi } release_tfadapter() { logging "Create output directory" - mk_dir "${RELEASE_PATH}" RELEASE_TARGET="tfadapter.tar" cd ${CMAKE_PATH}/dist/python/dist && mkdir -p tfplugin/bin && cp -r "${BASE_PATH}/script" tfplugin/ && mv npu_bridge-*.whl tfplugin/bin && mv "${BASE_PATH}/tf_adapter_2.x/build/dist/python/dist/npu_device-0.1-py3-none-any.whl" tfplugin/bin && tar cfz "${RELEASE_TARGET}" * && mv "${RELEASE_TARGET}" "${RELEASE_PATH}" } @@ -97,9 +113,30 @@ main() { # tfadapter build start logging "---------------- tfadapter build start ----------------" ${GCC_PREFIX}g++ -v + mk_dir "${RELEASE_PATH}" build_tfadapter - release_tfadapter + if [[ "X$ENABLE_TFADAPTER_UT" = "Xoff" ]]; then + release_tfadapter + fi + if [[ "X$ENABLE_TFADAPTER_UT" = "Xon" ]]; then + cd ${BASE_PATH} + export ASCEND_OPP_PATH=/home/hrz/tfadapter_ut/support_json + export PRINT_MODEL=1 + RUN_TEST_CASE=${CMAKE_PATH}/tf_adapter/tests/ut/tfadapter_utest && ${RUN_TEST_CASE} + if [[ "$?" -ne 0 ]]; then + echo "!!! UT FAILED, PLEASE CHECK YOUR CHANGES !!!" + echo -e "\033[31m${RUN_TEST_CASE}\033[0m" + exit 1; + fi + logging "Generating coverage statistics, please wait..." + rm -rf ${BASE_PATH}/coverage + mkdir ${BASE_PATH}/coverage + lcov -c -d ${CMAKE_PATH}/tf_adapter/tests/ut/ -o coverage/tmp.info + lcov -r coverage/tmp.info '*/tests/*' '*/nlohmann_json-src/*' '*/tensorflow-src/*' \ + '*/inc/*' '*/output/*' '*/usr/*' '*/Eigen/*' '*/absl/*' '*/google/*' '*/tensorflow/core/*' \ + -o coverage/coverage.info + fi logging "---------------- tfadapter build finished ----------------" } -main "$@" \ No newline at end of file +main "$@" diff --git a/cmake/secure_c.cmake b/cmake/secure_c.cmake index 4e5ff552f..66baca228 100755 --- a/cmake/secure_c.cmake +++ b/cmake/secure_c.cmake @@ -18,4 +18,4 @@ FetchContent_GetProperties(secure_c) if (NOT secure_c_POPULATED) FetchContent_Populate(secure_c) include_directories(${secure_c_SOURCE_DIR}/include) -endif () +endif () \ No newline at end of file diff --git a/cmake/tests/gtest.cmake b/cmake/tests/gtest.cmake new file mode 100644 index 000000000..9763e4bf3 --- /dev/null +++ b/cmake/tests/gtest.cmake @@ -0,0 +1,64 @@ +if (HAVE_GTEST) + return() +endif() + +include(ExternalProject) + +if ((${CMAKE_INSTALL_PREFIX} STREQUAL /usr/local) OR + (${CMAKE_INSTALL_PREFIX} STREQUAL "C:/Program Files (x86)/ascend")) + set(CMAKE_INSTALL_PREFIX ${TFADAPTER_DIR}/output CACHE STRING "path for install()" FORCE) + message(STATUS "No install prefix selected, default to ${CMAKE_INSTALL_PREFIX}.") +endif() + +if (TF_PKG_SERVER) + set(REQ_URL "${TF_PKG_SERVER}/libs/gtest/release-1.8.1.tar.gz") + set(MD5 "") +elseif (ENABLE_GITEE) + set(REQ_URL "https://gitee.com/mirrors/googletest/repository/archive/release-1.8.1.tar.gz") + set(MD5 "") +else() + set(REQ_URL "https://github.com/google/googletest/archive/release-1.8.1.tar.gz") + set(MD5 "") +endif () + +set (gtest_CXXFLAGS "-D_GLIBCXX_USE_CXX11_ABI=0 -D_FORTIFY_SOURCE=2 -O2 -fstack-protector-all -Wl,-z,relro,-z,now,-z,noexecstack") +set (gtest_CFLAGS "-D_GLIBCXX_USE_CXX11_ABI=0 -D_FORTIFY_SOURCE=2 -O2 -fstack-protector-all -Wl,-z,relro,-z,now,-z,noexecstack") +ExternalProject_Add(gtest_build + URL ${REQ_URL} + TLS_VERIFY OFF + CONFIGURE_COMMAND ${CMAKE_COMMAND} -DCMAKE_CXX_FLAGS=${gtest_CXXFLAGS} -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}/gtest + -DBUILD_TESTING=OFF -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_MACOSX_RPATH=TRUE -Dgtest_disable_pthreads=ON + BUILD_COMMAND $(MAKE) + INSTALL_COMMAND $(MAKE) install + EXCLUDE_FROM_ALL TRUE +) + +set(GTEST_PKG_DIR ${CMAKE_INSTALL_PREFIX}/gtest) + +file(MAKE_DIRECTORY ${GTEST_PKG_DIR}/include) + +add_library(gtest SHARED IMPORTED) + +set_target_properties(gtest PROPERTIES + IMPORTED_LOCATION ${GTEST_PKG_DIR}/lib/libgtest.so +) + +add_library(gtest_main SHARED IMPORTED) + +set_target_properties(gtest_main PROPERTIES + IMPORTED_LOCATION ${GTEST_PKG_DIR}/lib/libgtest_main.so +) + +target_include_directories(gtest INTERFACE ${GTEST_PKG_DIR}/include) +target_include_directories(gtest_main INTERFACE ${GTEST_PKG_DIR}/include) + +set(INSTALL_BASE_DIR "") +set(INSTALL_LIBRARY_DIR lib) + +install(FILES ${GTEST_PKG_DIR}/lib/libgtest.so ${GTEST_PKG_DIR}/lib/libgtest_main.so OPTIONAL + DESTINATION ${INSTALL_LIBRARY_DIR}) + +add_dependencies(gtest gtest_build) + +#set(HAVE_GFLAGS TRUE CACHE BOOL "gflags build add") +set(HAVE_GTEST TRUE) diff --git a/cmake/tests/intf_pub_linux.cmake b/cmake/tests/intf_pub_linux.cmake new file mode 100644 index 000000000..894329860 --- /dev/null +++ b/cmake/tests/intf_pub_linux.cmake @@ -0,0 +1,34 @@ +if (HAVE_PUB) + return() +endif() + +add_library(intf_pub INTERFACE) + +target_compile_options(intf_pub INTERFACE + -Wall + -fPIC + $,-fstack-protector-all,-fstack-protector-strong> + $<$:-std=c++11> +) +target_compile_definitions(intf_pub INTERFACE + _GLIBCXX_USE_CXX11_ABI=0 + $<$:CFG_BUILD_NDEBUG> + $<$:CFG_BUILD_DEBUG> + WIN64=1 + LINUX=0 + LOG_CPP +) +target_link_options(intf_pub INTERFACE + -Wl,-z,relro + -Wl,-z,now + -Wl,-z,noexecstack + $<$:-Wl,--build-id=none> +) +target_link_directories(intf_pub INTERFACE +) +target_link_libraries(intf_pub INTERFACE + -lpthread +) + +#set(HAVE_PUB TRUE CACHE BOOL "pub add") +set(HAVE_PUB TRUE) \ No newline at end of file diff --git a/cmake/tests/securec.cmake b/cmake/tests/securec.cmake new file mode 100644 index 000000000..55af981b9 --- /dev/null +++ b/cmake/tests/securec.cmake @@ -0,0 +1,68 @@ +if (HAVE_C_SEC) + return() +endif() + +include(ExternalProject) + +if ((${CMAKE_INSTALL_PREFIX} STREQUAL /usr/local) OR + (${CMAKE_INSTALL_PREFIX} STREQUAL "C:/Program Files (x86)/ascend")) + set(CMAKE_INSTALL_PREFIX ${TFADAPTER_DIR}/output CACHE STRING "path for install()" FORCE) + message(STATUS "No install prefix selected, default to ${CMAKE_INSTALL_PREFIX}.") +endif() + +if (TF_PKG_SERVER) + set(REQ_URL "${TF_PKG_SERVER}/libs/securec/v1.1.10.tar.gz") + set(MD5 "") +else() + set(REQ_URL "https://gitee.com/openeuler/libboundscheck/repository/archive/v1.1.10.tar.gz") + set(MD5 "") +endif () + +ExternalProject_Add(c_sec_build + URL ${REQ_URL} + PATCH_COMMAND patch -p1 < ${TFADAPTER_DIR}/tf_adapter/tests/patch/securec/0001-add-securec-cmake-script.patch + CONFIGURE_COMMAND ${CMAKE_COMMAND} + -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_LINKER=${CMAKE_LINKER} + -DCMAKE_AR=${CMAKE_AR} + -DCMAKE_RANLIB=${CMAKE_RANLIB} + -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}/c_sec + BUILD_COMMAND $(MAKE) + INSTALL_COMMAND $(MAKE) install + EXCLUDE_FROM_ALL TRUE +) + +set(C_SEC_PKG_DIR ${CMAKE_INSTALL_PREFIX}/c_sec) + +add_library(c_sec SHARED IMPORTED) + +file(MAKE_DIRECTORY ${C_SEC_PKG_DIR}/include) + +set_target_properties(c_sec PROPERTIES + IMPORTED_LOCATION ${C_SEC_PKG_DIR}/lib/libc_sec.so +) + +target_include_directories(c_sec INTERFACE ${C_SEC_PKG_DIR}/include) + +add_dependencies(c_sec c_sec_build) + +set(INSTALL_BASE_DIR "") +set(INSTALL_LIBRARY_DIR lib) + +install(FILES ${C_SEC_PKG_DIR}/lib/libc_sec.so OPTIONAL + DESTINATION ${INSTALL_LIBRARY_DIR}) + +add_library(c_sec_static_lib STATIC IMPORTED) +set_target_properties(c_sec_static_lib PROPERTIES + IMPORTED_LOCATION ${C_SEC_PKG_DIR}/lib/libc_sec.a +) + +add_library(c_sec_static INTERFACE) +target_include_directories(c_sec_static INTERFACE ${C_SEC_PKG_DIR}/include) +target_link_libraries(c_sec_static INTERFACE c_sec_static_lib) + +add_dependencies(c_sec_static c_sec_build) + +#set(HAVE_C_SEC TRUE CACHE BOOL "c_sec build add") +set(HAVE_C_SEC TRUE) \ No newline at end of file diff --git a/configure.py b/configure.py index ffaa3e784..328d94292 100755 --- a/configure.py +++ b/configure.py @@ -29,6 +29,7 @@ _COMPAT_TENSORFLOW_VERSION = "1.15.0" _COMPAT_PYTHON_VERSION = "Python 3.7" _COMPAT_SWIG_VERSION = "SWIG Version " _ASCEND_INSTALL_PATH_ENV = "ASCEND_CUSTOM_PATH" +_OPEN_UT = "OPEN_UT" @@ -115,6 +116,9 @@ def setup_python(): with open(real_config_path('LINK_FLAGS'), 'w') as f: f.write(os.path.join(compile_args[1], 'libtensorflow_framework.so.1\n')) f.write(os.path.join(compile_args[1], 'python', '_pywrap_tensorflow_internal.so\n')) + with open(real_config_path('UT_LINK_FLAGS'), 'w') as f: + f.write(os.path.join(compile_args[1], 'libtensorflow_framework.so.1\n')) + f.write(os.path.join(compile_args[1], 'python', '_pywrap_tensorflow_internal.so\n')) break @@ -139,7 +143,7 @@ def setup_ascend(env_path): f.write(os.path.join(ascend_path, "fwkacllib", "lib64", "libfmk_parser.so\n")) f.write(os.path.join(ascend_path, "fwkacllib", "lib64", "libdatatransfer.so\n")) f.write(os.path.join(ascend_path, "fwkacllib", "lib64", "libindextransform.so\n")) - f.write(os.path.join(ascend_path, "atc", "lib64", "libalog.so\n")) + f.write(os.path.join(ascend_path, "fwkacllib", "lib64", "libalog.so\n")) def setup_swig(): """Get swig install path.""" default_swig_path = which('swig') @@ -182,7 +186,8 @@ def setup_swig(): def main(): env_snapshot = dict(os.environ) setup_python() - setup_ascend(env_snapshot.get(_ASCEND_INSTALL_PATH_ENV)) + if not env_snapshot.get(_OPEN_UT): + setup_ascend(env_snapshot.get(_ASCEND_INSTALL_PATH_ENV)) setup_swig() diff --git a/tf_adapter/kernels/geop_npu.cc b/tf_adapter/kernels/geop_npu.cc index c912a1c54..817bfb2ef 100644 --- a/tf_adapter/kernels/geop_npu.cc +++ b/tf_adapter/kernels/geop_npu.cc @@ -68,10 +68,6 @@ limitations under the License. #include "framework/omg/parser/parser_inner_ctx.h" #include "ge/ge_api.h" #include "ge/ge_api_types.h" -#include "tdt/tdt_host_interface.h" -#include "tdt/tsd_client.h" - -using namespace tdt; namespace tensorflow { Status FunctionalizeControlFlow(Graph *graph, FunctionLibraryDefinition *library); diff --git a/tf_adapter/kernels/npu_sys_ctl_ops.cc b/tf_adapter/kernels/npu_sys_ctl_ops.cc index 82530a6d6..b31210cdf 100644 --- a/tf_adapter/kernels/npu_sys_ctl_ops.cc +++ b/tf_adapter/kernels/npu_sys_ctl_ops.cc @@ -39,8 +39,6 @@ limitations under the License. #include "ge/ge_api.h" #include "ge/ge_api_types.h" #include "hccl/hcom.h" -#include "tdt/tdt_host_interface.h" -#include "tdt/tsd_client.h" #include "tensorflow/core/framework/op_kernel.h" #include "tensorflow/core/platform/mutex.h" #include "tf_adapter/common/adp_logger.h" @@ -49,8 +47,6 @@ limitations under the License. #include "tf_adapter/util/ge_plugin.h" #include "tf_adapter/util/npu_attrs.h" -using namespace tdt; - namespace tensorflow { inline string ToString(ge::Status status) { return ::ge::StatusFactory::Instance()->GetErrDesc(status); } diff --git a/tf_adapter/optimizers/gradient_fusion_optimizer.cc b/tf_adapter/optimizers/gradient_fusion_optimizer.cc index 3dea189a0..659431857 100644 --- a/tf_adapter/optimizers/gradient_fusion_optimizer.cc +++ b/tf_adapter/optimizers/gradient_fusion_optimizer.cc @@ -35,213 +35,6 @@ limitations under the License. namespace tensorflow { namespace grappler { -const string OP_TYPE_BROADCAST = "HcomBroadcast"; - -const string DATA_TYPE_ATTR = "T"; -const string SHAPE_ATTR = "shape"; -const string GROUP_ATTR = "group"; -const string ROOT_RANK_ATTR = "root_rank"; -const string REDUCE_TYPE_ATTR = "reduction"; -const string FUSION_ATTR = "fusion"; -const string FUSION_ID_ATTR = "fusion_id"; - -Status SizeofDataType(DataType dType, size_t &size) { - static const std::map dataTypeSizeMap = { - {DT_INVALID, 0}, {DT_FLOAT, 4}, {DT_DOUBLE, 8}, {DT_INT32, 4}, {DT_UINT8, 1}, - {DT_INT16, 2}, {DT_INT8, 1}, {DT_COMPLEX64, 8}, {DT_INT64, 8}, {DT_BOOL, 1}, - {DT_QINT8, 8}, {DT_QUINT8, 8}, {DT_QINT32, 4}, {DT_BFLOAT16, 2}, {DT_QINT16, 2}, - {DT_QUINT16, 2}, {DT_UINT16, 2}, {DT_COMPLEX128, 16}, {DT_HALF, 2}, {DT_UINT32, 4}, - {DT_UINT64, 8}, {DT_FLOAT_REF, 4}, {DT_DOUBLE_REF, 8}, {DT_INT32_REF, 4}, {DT_UINT8_REF, 1}, - {DT_INT16_REF, 2}, {DT_INT8_REF, 1}, {DT_COMPLEX64_REF, 8}, {DT_INT64_REF, 8}, {DT_BOOL_REF, 1}, - {DT_QINT8_REF, 1}, {DT_QUINT8_REF, 1}, {DT_QINT32_REF, 4}, {DT_BFLOAT16_REF, 2}, {DT_QINT16_REF, 2}, - {DT_QUINT16_REF, 2}, {DT_UINT16_REF, 2}, {DT_COMPLEX128_REF, 16}, {DT_HALF_REF, 2}, {DT_UINT32_REF, 4}, - {DT_UINT64_REF, 8}, - }; - auto iter = dataTypeSizeMap.find(dType); - if (iter != dataTypeSizeMap.end()) { - size = iter->second; - return Status::OK(); - } - return errors::InvalidArgument("data type not support"); -} - -bool GradFusionOptimizer::IsHcomOp(const NodeDef &nodeDef) { - if (nodeDef.op() == OP_TYPE_BROADCAST) { - return true; - } else { - return false; - } -} - -string GetNodeScopeName(NodeDef &nodeDef) { - string::size_type pos = nodeDef.name().find_last_of('/'); - if (pos == string::npos) { return string(""); } - return nodeDef.name().substr(0, pos + 1); -} - -Status GradFusionOptimizer::GetInputTensorSize(NodeDef &nodeDef, int64_t &inputTensorSize) { - int64_t inputTensorSizeRet = 0; - int64_t inputTensorSizeRetTmp = 1; - string err; - TensorShapeProto shape; - auto attrMap = nodeDef.attr(); - int inputsNameSize = nodeDef.input_size(); - if (inputsNameSize > 0) { - for (int i = 0; i < inputsNameSize; i++) { - const string &inputNodeName = nodeDef.input(i); - if (inputNodeName.empty()) { - ADP_LOG(INFO) << "Cannot get input node name, curr node : " << nodeDef.name() << " index: " << i; - continue; - } - NodeDef inputNode = nameToNode_[inputNodeName]; - shape = (*inputNode.mutable_attr())[SHAPE_ATTR].shape(); - if (!shape.unknown_rank()) { - inputTensorSizeRetTmp = 1; - for (const auto &dim : shape.dim()) { - if (dim.size() == -1) { - err = string("Unknown size"); - break; - } - inputTensorSizeRetTmp = MultiplyWithoutOverflow(inputTensorSizeRetTmp, dim.size()); - if (inputTensorSizeRetTmp < 0) { return errors::InvalidArgument("input tensor size is overflow"); } - } - } else { - err = string("Unknown rank"); - } - if (!err.empty()) { break; } - if (INT64_MAX - inputTensorSizeRetTmp < inputTensorSizeRet) { - return errors::InvalidArgument("input tensor size is overflow"); - } - inputTensorSizeRet += inputTensorSizeRetTmp; - } - } else { - err = string("Does not have the attr of \'inputs_name\'"); - } - if (!err.empty()) { - return errors::InvalidArgument("GetInputTensorSize failed. Node name: ", nodeDef.name(), ", error msg: ", err); - } - - DataType dType = (*nodeDef.mutable_attr())[DATA_TYPE_ATTR].list().type(0); - size_t size = 0; - Status ret = SizeofDataType(dType, size); - REQUIRES_STATUS_OK(ret); - inputTensorSize = MultiplyWithoutOverflow(inputTensorSizeRet, size); - if (inputTensorSize < 0) { return errors::InvalidArgument("input tensor size is overflow"); } - return Status::OK(); -} - -Status GradFusionOptimizer::SetHcomBroadcastAttr(NodeDef *fusionNode, NodeDef &originNode) { - std::set &fusionBroadcastName = fusionOpPool_[fusionNode->op()]; - DataType dType = (*originNode.mutable_attr())[DATA_TYPE_ATTR].list().type(0); - if (fusionBroadcastName.count(fusionNode->name()) == 0) { - int64 rootRank = (*originNode.mutable_attr())[ROOT_RANK_ATTR].i(); - (*fusionNode->mutable_attr())[ROOT_RANK_ATTR].set_i(rootRank); - string group = (*originNode.mutable_attr())[GROUP_ATTR].s(); - (*fusionNode->mutable_attr())[GROUP_ATTR].set_s(group); - (*fusionNode->mutable_attr())[FUSION_ATTR].set_i(0); - (*fusionNode->mutable_attr())[FUSION_ID_ATTR].set_i(-1); - fusionBroadcastName.insert(fusionNode->name()); - } - // All input data types are the same. - (*fusionNode->mutable_attr())[DATA_TYPE_ATTR].mutable_list()->add_type(dType); - return Status::OK(); -} - -Status GradFusionOptimizer::SetFusionNodeAttr(NodeDef *fusionNode, NodeDef &originNode) { - string opType = fusionNode->op(); - if (opType == OP_TYPE_BROADCAST) { return SetHcomBroadcastAttr(fusionNode, originNode); } - return Status::OK(); -} - -Status GradFusionOptimizer::FusionOp(std::vector fusionHcomOps, GraphDef *graphDef) { - std::set outputsSet; - string opType = fusionHcomOps[0].op(); - if (fusionOpInfo_.count(opType) == 0) { fusionOpInfo_[opType].push_back(std::make_pair(0, "")); } - int &fusionOpIndex = fusionOpInfo_[opType].back().first; - string &fusionOpName = fusionOpInfo_[opType].back().second; - string scope = GetNodeScopeName(fusionHcomOps[0]); - string fusionNodeName = scope + "FusionNode_" + opType + "_" + std::to_string(fusionOpIndex); - - // add fusion node - NodeDef *fusionNode = graphDef->add_node(); - REQUIRES_NOT_NULL(fusionNode); - fusionNode->set_name(fusionNodeName); - fusionNode->set_device(fusionHcomOps[0].device()); - fusionNode->set_op(opType); - - ADP_LOG(INFO) << "INFO: GradFusionOptimizer::FusionOp New FusionNodeName: " << fusionNodeName; - int fusionOutputIdx = 0; - std::set fusionInputs; - std::set fusionCtrlInputs; - for (auto &nodeDef : fusionHcomOps) { - for (const string &input : nodeDef.input()) { - if (fusionInputs.count(input) == 1) { continue; } - // Control dependencies must come after regular dependencies - if (!input.empty() && str_util::StartsWith(input, "^")) { - fusionCtrlInputs.insert(input); - continue; - } - fusionNode->add_input(input); - TF_RETURN_IF_ERROR(SetFusionNodeAttr(fusionNode, nodeDef)); - fusionInputs.insert(input); - } - fusionInputs.clear(); - - // outputs - outputsSet = nodeMap_->GetOutputs(nodeDef.name()); - for (auto outNodeDef : outputsSet) { - int idx = 0; - for (const auto &input : outNodeDef->input()) { - if (input.find(nodeDef.name()) == std::string::npos) { - idx++; - continue; - } - if (input.find('^') == std::string::npos) { - outNodeDef->set_input(idx, fusionNodeName + ":" + std::to_string(fusionOutputIdx)); - fusionOutputIdx++; - } else { - outNodeDef->set_input(idx, "^" + fusionNodeName); - } - } - } - - // delete origin nodes - ::google::protobuf::RepeatedPtrField<::tensorflow::NodeDef> *nodeList = graphDef->mutable_node(); - for (::google::protobuf::RepeatedPtrField<::tensorflow::NodeDef>::const_iterator const_it = nodeList->begin(); - const_it != nodeList->end(); ++const_it) { - if (const_it->name() == nodeDef.name()) { - nodeList->erase(const_it); - break; - } - } - } - if (!fusionCtrlInputs.empty()) { - for (const string &ctrlInput : fusionCtrlInputs) { fusionNode->add_input(ctrlInput); } - } - fusionCtrlInputs.clear(); - - // add control edges - if (fusionOpIndex >= 1) { fusionNode->add_input("^" + fusionOpName); } - - // update fusion op info. - fusionOpIndex++; - fusionOpName = fusionNodeName; - - return Status::OK(); -} - -int64 GradFusionOptimizer::GetFusionTensorSize() { - const char *env = getenv("FUSION_TENSOR_SIZE"); - // default (50KBytes) - const int64 fusionTensorSizeDefault = 524288000; - if (env == nullptr || strlen(env) >= ADAPTER_ENV_MAX_LENTH) { return fusionTensorSizeDefault; } - string envSize(env); - std::istringstream ss(envSize); - int64 fusionTensorSize; - if (!(ss >> fusionTensorSize)) { fusionTensorSize = fusionTensorSizeDefault; } - return fusionTensorSize; -} - Status GradFusionOptimizer::Optimize(Cluster *cluster, const GrapplerItem &item, GraphDef *optimizedGraph) { ADP_LOG(INFO) << "INFO: GradFusionOptimizer::Optimize begin"; REQUIRES_NOT_NULL(optimizedGraph); @@ -249,7 +42,6 @@ Status GradFusionOptimizer::Optimize(Cluster *cluster, const GrapplerItem &item, ADP_LOG(INFO) << "INFO: GradFusionOptimizer::Optimize end"; return Status::OK(); } - REGISTER_GRAPH_OPTIMIZER(GradFusionOptimizer); } // end namespace grappler } // end namespace tensorflow diff --git a/tf_adapter/optimizers/om_partition_subgraphs_pass.cc b/tf_adapter/optimizers/om_partition_subgraphs_pass.cc index bfc9a70bd..a488291ab 100644 --- a/tf_adapter/optimizers/om_partition_subgraphs_pass.cc +++ b/tf_adapter/optimizers/om_partition_subgraphs_pass.cc @@ -235,14 +235,6 @@ bool IsWhiteListSupport(const string &op_name, bool mix_compile_mode, const stri return ans; } -bool IsOptimizerOp(Node *node) { - static const std::unordered_set Optimizer_Names = { - "GradientDescent", "Momentum", "Adam", "Adadelta", "Adagrad", "AdagradDA", "ProximalAdagrad", - "ProximalGradientDescent", "RMSProp", "Ftrl", - }; - return (Optimizer_Names.count(node->name()) > 0); -} - Status SetIteratorShardName(Node *node) { if (node->type_string() != "Iterator" && node->type_string() != "IteratorV2") { return errors::InvalidArgument("Node op type is not Iterator."); diff --git a/tf_adapter/tests/CMakeLists.txt b/tf_adapter/tests/CMakeLists.txt new file mode 100644 index 000000000..d0e71de82 --- /dev/null +++ b/tf_adapter/tests/CMakeLists.txt @@ -0,0 +1,42 @@ +# Copyright 2019-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. +# ============================================================================ + +project(tests CXX C) + +include(${TFADAPTER_DIR}/cmake/tests/gtest.cmake) +include(${TFADAPTER_DIR}/cmake/tests/securec.cmake) +include(${TFADAPTER_DIR}/cmake/tests/intf_pub_linux.cmake) + +execute_process(COMMAND ${PYTHON_BIN_PATH} -c "import tensorflow as tf; print(tf.sysconfig.get_lib(), end='')" + OUTPUT_VARIABLE TF_INSTALL_LIB_PATH) + +execute_process(COMMAND ${PYTHON_BIN_PATH} -c "from distutils import sysconfig; import os.path as op; \ + v=sysconfig.get_config_vars(); print(op.join(v['LIBDIR'], v['LDLIBRARY']))" + OUTPUT_VARIABLE PYTHON_LIB_PATH) +message("python lib path:${PYTHON_LIB_PATH}") + +add_subdirectory(depends/mmpa) +add_subdirectory(depends/indextransform) +add_subdirectory(depends/alog) +add_subdirectory(depends/datatransfer) +add_subdirectory(depends/ge_runner) + +if (ENABLE_TFADAPTER_UT) + add_subdirectory(ut) +endif() + +if (ENABLE_TFADAPTER_ST) + add_subdirectory(st) +endif() \ No newline at end of file diff --git a/tf_adapter/tests/depends/alog/CMakeLists.txt b/tf_adapter/tests/depends/alog/CMakeLists.txt new file mode 100644 index 000000000..52946ba94 --- /dev/null +++ b/tf_adapter/tests/depends/alog/CMakeLists.txt @@ -0,0 +1,27 @@ +# Copyright 2019-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. +# ============================================================================ + +project(alog_stub) + +file(GLOB_RECURSE SRC_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + "src/*.cc" +) + +include_directories(${TFADAPTER_DIR}/inc/) + +add_library(alog_stub SHARED ${SRC_FILES}) +target_link_libraries(alog_stub PRIVATE + $ +) \ No newline at end of file diff --git a/tf_adapter/tests/depends/alog/src/alog_stub.cc b/tf_adapter/tests/depends/alog/src/alog_stub.cc new file mode 100644 index 000000000..30fd354e9 --- /dev/null +++ b/tf_adapter/tests/depends/alog/src/alog_stub.cc @@ -0,0 +1,31 @@ +/** + * Copyright 2019-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 "toolchain/slog.h" +#include "toolchain/plog.h" + +DLL_EXPORT int CheckLogLevelForC(int moduleId, int logLevel) { return 1; } + +void DlogInnerForC(int moduleId, int level, const char *fmt, ...) { return; } + +#define DlogForC(moduleId, level, fmt, ...) \ + do { \ + if(CheckLogLevelForC(moduleId, level) == 1) { \ + DlogInnerForC(moduleId, level, "[%s:%d]" fmt, __FILE__, __LINE__, ##__VA_ARGS__); \ + } \ + } while (0) + +DLL_EXPORT int DlogReportFinalize() { return 0; } \ No newline at end of file diff --git a/tf_adapter/tests/depends/aoe/CMakeLists.txt b/tf_adapter/tests/depends/aoe/CMakeLists.txt new file mode 100644 index 000000000..52946ba94 --- /dev/null +++ b/tf_adapter/tests/depends/aoe/CMakeLists.txt @@ -0,0 +1,27 @@ +# Copyright 2019-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. +# ============================================================================ + +project(alog_stub) + +file(GLOB_RECURSE SRC_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + "src/*.cc" +) + +include_directories(${TFADAPTER_DIR}/inc/) + +add_library(alog_stub SHARED ${SRC_FILES}) +target_link_libraries(alog_stub PRIVATE + $ +) \ No newline at end of file diff --git a/tf_adapter/tests/depends/datatransfer/CMakeLists.txt b/tf_adapter/tests/depends/datatransfer/CMakeLists.txt new file mode 100644 index 000000000..e114dce5f --- /dev/null +++ b/tf_adapter/tests/depends/datatransfer/CMakeLists.txt @@ -0,0 +1,27 @@ +# Copyright 2019-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. +# ============================================================================ + +project(datatransfer_stub) + +file(GLOB_RECURSE SRC_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + "src/*.cc" +) + +include_directories(${TFADAPTER_DIR}/inc/) + +add_library(datatransfer_stub SHARED ${SRC_FILES}) +target_link_libraries(datatransfer_stub PRIVATE + $ +) \ No newline at end of file diff --git a/tf_adapter/tests/depends/datatransfer/src/datatransfer_stub.cc b/tf_adapter/tests/depends/datatransfer/src/datatransfer_stub.cc new file mode 100644 index 000000000..2f43ab3e5 --- /dev/null +++ b/tf_adapter/tests/depends/datatransfer/src/datatransfer_stub.cc @@ -0,0 +1,33 @@ +/** + * Copyright 2019-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 "tdt/tdt_host_interface.h" + +namespace tdt { +int32_t TdtInFeedInit(uint32_t deviceId) { return 0; } + +int32_t TdtOutFeedInit(uint32_t deviceId) { return 0; } + +int32_t TdtInFeedDestroy(uint32_t deviceId) { return 0; } + +int32_t TdtOutFeedDestroy() { return 0; } + +int32_t TdtHostPreparePopData() { return 0; } + +int32_t TdtHostPopData(const std::string &channelName, std::vector &item) { return 0; } + +int32_t TdtHostStop(const std::string &channelName) { return 0; } +} \ No newline at end of file diff --git a/tf_adapter/tests/depends/ge_runner/CMakeLists.txt b/tf_adapter/tests/depends/ge_runner/CMakeLists.txt new file mode 100644 index 000000000..f985fae5c --- /dev/null +++ b/tf_adapter/tests/depends/ge_runner/CMakeLists.txt @@ -0,0 +1,31 @@ +# Copyright 2019-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. +# ============================================================================ + +project(ge_runner_stub) + +file(GLOB_RECURSE SRC_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + "src/*.cc" +) + +add_library(ge_runner_stub SHARED ${SRC_FILES}) + +target_include_directories(ge_runner_stub PRIVATE + ${TFADAPTER_DIR}/inc/ + ${TF_INSTALL_LIB_PATH}/include/ +) + +target_link_libraries(ge_runner_stub PRIVATE + $ +) \ No newline at end of file diff --git a/tf_adapter/tests/depends/ge_runner/src/ge_runner_stub.cc b/tf_adapter/tests/depends/ge_runner/src/ge_runner_stub.cc new file mode 100644 index 000000000..4a3d09b6b --- /dev/null +++ b/tf_adapter/tests/depends/ge_runner/src/ge_runner_stub.cc @@ -0,0 +1,330 @@ +/** + * Copyright 2019-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 "framework/common/ge_inner_error_codes.h" +#include "framework/common/types.h" +#include "framework/memory/memory_api.h" +#include "framework/omg/omg_inner_types.h" +#include "framework/omg/parser/model_parser.h" +#include "framework/omg/parser/parser_api.h" +#include "framework/omg/parser/parser_factory.h" +#include "framework/omg/parser/parser_inner_ctx.h" +#include "ge/ge_api.h" +#include "ge/ge_api_types.h" +#include "graph/tensor.h" +#include "graph/utils/graph_utils.h" +#include "tensorflow/core/framework/types.pb.h" + +namespace ge { +namespace { +const std::map data_type_map = { + {tensorflow::DataType::DT_FLOAT, ge::DataType::DT_FLOAT}, + {tensorflow::DataType::DT_HALF, ge::DataType::DT_FLOAT16}, + {tensorflow::DataType::DT_INT8, ge::DataType::DT_INT8}, + {tensorflow::DataType::DT_INT16, ge::DataType::DT_INT16}, + {tensorflow::DataType::DT_UINT16, ge::DataType::DT_UINT16}, + {tensorflow::DataType::DT_UINT8, ge::DataType::DT_UINT8}, + {tensorflow::DataType::DT_INT32, ge::DataType::DT_INT32}, + {tensorflow::DataType::DT_INT64, ge::DataType::DT_INT64}, + {tensorflow::DataType::DT_UINT32, ge::DataType::DT_UINT32}, + {tensorflow::DataType::DT_UINT64, ge::DataType::DT_UINT64}, + {tensorflow::DataType::DT_BOOL, ge::DataType::DT_BOOL}, + {tensorflow::DataType::DT_DOUBLE, ge::DataType::DT_DOUBLE}, + {tensorflow::DataType::DT_COMPLEX64, ge::DataType::DT_COMPLEX64}, + {tensorflow::DataType::DT_QINT8, ge::DataType::DT_INT8}, + {tensorflow::DataType::DT_QUINT8, ge::DataType::DT_UINT8}, + {tensorflow::DataType::DT_QINT32, ge::DataType::DT_INT32}, + {tensorflow::DataType::DT_QINT16, ge::DataType::DT_INT16}, + {tensorflow::DataType::DT_QUINT16, ge::DataType::DT_UINT16}, + {tensorflow::DataType::DT_COMPLEX128, ge::DataType::DT_COMPLEX128}, + {tensorflow::DataType::DT_RESOURCE, ge::DataType::DT_RESOURCE}, + {tensorflow::DataType::DT_BFLOAT16, ge::DataType::DT_FLOAT16}, + {tensorflow::DataType::DT_STRING, ge::DataType::DT_STRING}, + {tensorflow::DataType::DT_FLOAT_REF, ge::DataType::DT_FLOAT}, + {tensorflow::DataType::DT_DOUBLE_REF, ge::DataType::DT_DOUBLE}, + {tensorflow::DataType::DT_INT32_REF, ge::DataType::DT_INT32}, + {tensorflow::DataType::DT_INT8_REF, ge::DataType::DT_INT8}, + {tensorflow::DataType::DT_UINT8_REF, ge::DataType::DT_UINT8}, + {tensorflow::DataType::DT_INT16_REF, ge::DataType::DT_INT16}, + {tensorflow::DataType::DT_UINT16_REF, ge::DataType::DT_UINT16}, + {tensorflow::DataType::DT_COMPLEX64_REF, ge::DataType::DT_COMPLEX64}, + {tensorflow::DataType::DT_QINT8_REF, ge::DataType::DT_INT8}, + {tensorflow::DataType::DT_QUINT8_REF, ge::DataType::DT_UINT8}, + {tensorflow::DataType::DT_QINT32_REF, ge::DataType::DT_INT32}, + {tensorflow::DataType::DT_QINT16_REF, ge::DataType::DT_INT16}, + {tensorflow::DataType::DT_QUINT16_REF, ge::DataType::DT_UINT16}, + {tensorflow::DataType::DT_COMPLEX128_REF, ge::DataType::DT_COMPLEX128}, + {tensorflow::DataType::DT_RESOURCE_REF, ge::DataType::DT_RESOURCE}, + {tensorflow::DataType::DT_BFLOAT16_REF, ge::DataType::DT_FLOAT16}, + {tensorflow::DataType::DT_UINT32_REF, ge::DataType::DT_UINT32}, + {tensorflow::DataType::DT_UINT64_REF, ge::DataType::DT_UINT64}, + {tensorflow::DataType::DT_INT64_REF, ge::DataType::DT_INT64}, + {tensorflow::DataType::DT_BOOL_REF, ge::DataType::DT_BOOL}, + {tensorflow::DataType::DT_HALF_REF, ge::DataType::DT_FLOAT16}, + {tensorflow::DataType::DT_STRING_REF, ge::DataType::DT_STRING}, + {tensorflow::DataType::DT_VARIANT, ge::DataType::DT_VARIANT}, +}; +} // end +class TensorFlowModelParser : public domi::ModelParser { + public: + TensorFlowModelParser() {} + virtual ~TensorFlowModelParser() {} + + Status Parse(const char *file, ge::Graph &graph) override; + + Status ParseFromMemory(const char *data, uint32_t size, ge::ComputeGraphPtr &graph) override; + + Status ParseFromMemory(const char *data, uint32_t size, ge::Graph &graph) override { + return domi::SUCCESS; + } + + Status ToJson(const char *model_file, const char *json_file) override; + + Status ParseProto(const google::protobuf::Message *proto, ge::ComputeGraphPtr &graph) override; + + Status ParseProtoWithSubgraph(const google::protobuf::Message *root_proto, + domi::GetGraphCallback callback, + ge::ComputeGraphPtr &graph) override; + + ge::DataType ConvertToGeDataType(const uint32_t type) override; + + Status ParseAllGraph(const google::protobuf::Message *root_proto, ge::ComputeGraphPtr &root_graph) override ; +}; +ge::DataType TensorFlowModelParser::ConvertToGeDataType(const uint32_t type) { + auto search = data_type_map.find(type); + if (search != data_type_map.end()) { + return search->second; + } else { + return ge::DataType::DT_UNDEFINED; + } +} + +Status TensorFlowModelParser::Parse(const char *file, ge::Graph &graph) { return ge::SUCCESS; } + +Status TensorFlowModelParser::ParseFromMemory(const char *data, uint32_t size, ge::ComputeGraphPtr &graph) { return ge::SUCCESS; } + +Status TensorFlowModelParser::ToJson(const char *model_file, const char *json_file) { return ge::SUCCESS; } + +Status TensorFlowModelParser::ParseProto(const google::protobuf::Message *proto, ge::ComputeGraphPtr &graph) { return ge::SUCCESS; } + +Status TensorFlowModelParser::ParseProtoWithSubgraph(const google::protobuf::Message *root_proto, + domi::GetGraphCallback callback, + ge::ComputeGraphPtr &graph) { return ge::SUCCESS; } + +Status TensorFlowModelParser::ParseAllGraph(const google::protobuf::Message *root_proto, ge::ComputeGraphPtr &root_graph) { return ge::SUCCESS; } + +static std::map graphs_map; +static std::atomic is_ge_init(false); +static std::atomic is_parser_init(false); + +GE_FUNC_VISIBILITY Status InitRdmaPool(size_t size, rtMemType_t mem_type) { + if (size == 0) { + return ge::FAILED; + } + return ge::SUCCESS; +} + +GE_FUNC_VISIBILITY Status RdmaRemoteRegister(const std::vector &var_info, + rtMemType_t mem_type) { + if (var_info.empty()) { + return ge::FAILED; + } + return ge::SUCCESS; +} + +GE_FUNC_VISIBILITY Status MallocSharedMemory(const TensorInfo &tensor_info, uint64_t &dev_addr, uint64_t &memory_size) { + if (tensor_info.var_name.empty()) { + return ge::FAILED; + } + return ge::SUCCESS; +} + +GE_FUNC_VISIBILITY Status GetVarBaseAddrAndSize(const std::string &var_name, uint64_t &base_addr, uint64_t &var_size) { + if (var_name.empty()) { + return ge::FAILED; + } + return ge::SUCCESS; +} + +GE_FUNC_VISIBILITY Status GEInitialize(const std::map &options) { + if (options.empty()) { + return ge::FAILED; + } + is_ge_init = true; + return ge::SUCCESS; +} + +GE_FUNC_VISIBILITY Status GEFinalize() { + if (!is_ge_init) { + return ge::FAILED; + } + return ge::SUCCESS; +} + +Status ParserInitialize(const std::map &options) { + if (options.empty()) { + return ge::FAILED; + } + is_parser_init = true; + return ge::SUCCESS; +} + +Status ParserFinalize() { + if (!is_parser_init) { + return ge::FAILED; + } + return ge::SUCCESS; +} + +GE_FUNC_VISIBILITY std::string GEGetErrorMsg() { return "ERROR";} + +Session::Session(const std::map &options) {} + +Session::~Session() { + graphs_map.clear(); +} + +Status Session::RemoveGraph(uint32_t graphId) { + auto ret = graphs_map.find(graphId); + if (ret != graphs_map.end()) { + graphs_map.erase(ret); + return ge::SUCCESS; + } + return ge::FAILED; +} + +bool Session::IsGraphNeedRebuild(uint32_t graphId) { + auto ret = graphs_map.find(graphId); + if (ret != graphs_map.end()) { + return false; + } + return true; +} + +Status Session::AddGraph(uint32_t graphId, const Graph &graph, const std::map &options) { + auto ret = graphs_map.find(graphId); + if (ret != graphs_map.end()) { + return ge::FAILED; + } + graphs_map[graphId] = graph; + return ge::SUCCESS; +} + +Status Session::BuildGraph(uint32_t graphId, const std::vector &inputs) { + auto ret = graphs_map.find(graphId); + if (ret == graphs_map.end()) { + return ge::FAILED; + } + return ge::SUCCESS; +} + +Status Session::RunGraphAsync(uint32_t graphId, const std::vector &inputs, RunAsyncCallback callback) { + ge::Status ret; + std::vector outputs; + outputs.push_back(ge::Tensor()); + auto res = graphs_map.find(graphId); + if (res == graphs_map.end()) { + ret = ge::FAILED; + } else { + ret = ge::SUCCESS; + } + callback(ret, outputs); + return ret; +} + +ComputeGraph::ComputeGraph(const std::string &name) + : name_(name), nodes_(), input_nodes_(), sub_graph_(), is_valid_flag_(false), need_iteration_(false) { +} + +ComputeGraph::~ComputeGraph() {} + +ProtoAttrMapHelper ComputeGraph::MutableAttrMap() { return attrs_; } + +ConstProtoAttrMapHelper ComputeGraph::GetAttrMap() const { + return ConstProtoAttrMapHelper(attrs_.GetProtoOwner(), attrs_.GetProtoMsg()); +} + +size_t ComputeGraph::GetAllNodesSize() const { + if (name_ == "total_0") { return 0; } + return 1; +} + +Graph::Graph(char const* name) {} + +Graph GraphUtils::CreateGraphFromComputeGraph(const ComputeGraphPtr compute_graph) { return Graph("ge"); } + +void Graph::SetNeedIteration(bool need_iteration) {} + +Tensor::Tensor() {} + +graphStatus Tensor::SetTensorDesc(const TensorDesc &tensorDesc) { return GRAPH_SUCCESS; } + +TensorDesc Tensor::GetTensorDesc() const { return TensorDesc(); } + +graphStatus Tensor::SetData(const uint8_t *data, size_t size) { return GRAPH_SUCCESS; } + +graphStatus Tensor::SetData(uint8_t *data, size_t size, const Tensor::DeleteFunc &deleter_func) { return GRAPH_SUCCESS; } + +size_t Tensor::GetSize() const { return 4; } + +Shape::Shape(const std::vector &dims) {} + +Shape::Shape() {} + +std::vector Shape::GetDims() const { + std::vector dims; + return dims; +} + +TensorDesc::TensorDesc() {} + +TensorDesc::TensorDesc(Shape shape, Format format, DataType dt) {} + +void TensorDesc::SetDataType(DataType dt) {} + +Shape TensorDesc::GetShape() const { return Shape(); } + +static Placement ge_placement = ge::kPlacementHost; + +Placement TensorDesc::GetPlacement() const { return ge_placement; } + +void TensorDesc::SetPlacement(ge::Placement placement) { ge_placement = placement; } + +std::unique_ptr Tensor::ResetData() { return nullptr; } + +ParserContext &GetParserContext() { + static ParserContext context; + return context; +} +} // end ge + +namespace domi { +ge::OmgContext &GetContext() { + static ge::OmgContext context; + return context; +} + +ModelParserFactory *ModelParserFactory::Instance() { + static ModelParserFactory instance; + return &instance; +} + +std::shared_ptr ModelParserFactory::CreateModelParser(const domi::FrameworkType type) { + return std::make_shared(); +} + +ModelParserFactory::~ModelParserFactory() {} +} // end domi \ No newline at end of file diff --git a/tf_adapter/tests/depends/indextransform/CMakeLists.txt b/tf_adapter/tests/depends/indextransform/CMakeLists.txt new file mode 100644 index 000000000..5f10848e4 --- /dev/null +++ b/tf_adapter/tests/depends/indextransform/CMakeLists.txt @@ -0,0 +1,27 @@ +# Copyright 2019-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. +# ============================================================================ + +project(indextransform_stub) + +file(GLOB_RECURSE SRC_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + "src/*.cc" +) + +include_directories(${TFADAPTER_DIR}/inc/) + +add_library(indextransform_stub SHARED ${SRC_FILES}) +target_link_libraries(indextransform_stub PRIVATE + $ +) \ No newline at end of file diff --git a/tf_adapter/tests/depends/indextransform/src/indextransform_stub.cc b/tf_adapter/tests/depends/indextransform/src/indextransform_stub.cc new file mode 100644 index 000000000..162f6b684 --- /dev/null +++ b/tf_adapter/tests/depends/indextransform/src/indextransform_stub.cc @@ -0,0 +1,22 @@ +/** + * Copyright 2019-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 "tdt/index_transform.h" + +int32_t IndexTransform(const uint32_t phy_id, uint32_t &logic_id) { + logic_id = phy_id; + return 0; +} \ No newline at end of file diff --git a/tf_adapter/tests/depends/mmpa/CMakeLists.txt b/tf_adapter/tests/depends/mmpa/CMakeLists.txt new file mode 100644 index 000000000..f5d1a3a53 --- /dev/null +++ b/tf_adapter/tests/depends/mmpa/CMakeLists.txt @@ -0,0 +1,30 @@ +# Copyright 2019-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. +# ============================================================================ + +project(mmpa_stub) + +file(GLOB_RECURSE SRC_FILES RELATIVE ${CMAKE_CURRENT_LIST_DIR} + "src/mmpa_stub.cc" +) + +include_directories(${TFADAPTER_DIR}/inc) + +add_library(mmpa_stub SHARED ${SRC_FILES}) +target_link_libraries(mmpa_stub PRIVATE + $ + -Wl,--no-as-needed + c_sec + -Wl,--as-needed +) \ No newline at end of file diff --git a/tf_adapter/tests/depends/mmpa/src/mmpa_stub.cc b/tf_adapter/tests/depends/mmpa/src/mmpa_stub.cc new file mode 100644 index 000000000..cac769f23 --- /dev/null +++ b/tf_adapter/tests/depends/mmpa/src/mmpa_stub.cc @@ -0,0 +1,81 @@ +/** + * Copyright 2019-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 "mmpa/mmpa_api.h" + +INT32 mmAccess2(const CHAR *path_name, INT32 mode) { + if (path_name == NULL) { return EN_INVALID_PARAM; } + + INT32 ret = access(path_name, mode); + if (ret != EN_OK) { return EN_ERROR; } + return EN_OK; +} + +INT32 mmGetPid() { + return (INT32)getpid(); +} + +INT32 mmMkdir(const CHAR *path_name, mmMode_t mode) { + if (path_name == NULL) { return EN_INVALID_PARAM; } + + INT32 ret = mkdir(path_name, mode); + if (ret != EN_OK) { return EN_ERROR; } + return EN_OK; +} + +INT32 mmIsDir(const CHAR *file_name) { + if (file_name == NULL) { return EN_INVALID_PARAM; } + struct stat file_stat; + (void)memset_s(&file_stat, sizeof(file_stat), 0, sizeof(file_stat)); + INT32 ret = lstat(file_name, &file_stat); + if (ret < MMPA_ZERO) { return EN_ERROR; } + + if (!S_ISDIR(file_stat.st_mode)) { return EN_ERROR; } + return EN_OK; +} + +INT32 mmRealPath(const CHAR *path, CHAR *real_path, INT32 real_path_len) { + INT32 ret = EN_OK; + if ((real_path == NULL) || (path == NULL) || (real_path_len < MMPA_MAX_PATH)) { + return EN_INVALID_PARAM; + } + + CHAR *ptr = realpath(path, real_path); + if (ptr == NULL) { ret = EN_ERROR; } + return ret; +} + +VOID *mmDlopen(const CHAR *file_name, INT32 mode) { + if ((file_name == NULL) || (mode < MMPA_ZERO)) { return NULL; } + return dlopen(file_name, mode); +} + +CHAR *mmDlerror(void) { + return dlerror(); +} + +VOID *mmDlsym(VOID *handle, const CHAR *func_name) { + if ((handle == NULL) || (func_name == NULL)) { return NULL; } + return dlsym(handle, func_name); +} + +INT32 mmDlclose(VOID *handle) { + if (handle == NULL) { return EN_INVALID_PARAM; } + + INT32 ret = dlclose(handle); + if (ret != EN_OK) { return EN_ERROR; } + return EN_OK; +} \ No newline at end of file diff --git a/tf_adapter/tests/patch/securec/0001-add-securec-cmake-script.patch b/tf_adapter/tests/patch/securec/0001-add-securec-cmake-script.patch new file mode 100644 index 000000000..0fcf50c47 --- /dev/null +++ b/tf_adapter/tests/patch/securec/0001-add-securec-cmake-script.patch @@ -0,0 +1,105 @@ +From 455c9812d70646fe725896d597d6c953bf5a09ac Mon Sep 17 00:00:00 2001 +From: taoxiangdong +Date: Wed, 14 Oct 2020 22:14:01 +0800 +Subject: [PATCH] add securec cmake script + +--- + CMakeLists.txt | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ + 1 file changed, 86 insertions(+) + create mode 100755 CMakeLists.txt + +diff --git a/CMakeLists.txt b/CMakeLists.txt +new file mode 100755 +index 0000000..9b91fb2 +--- /dev/null ++++ b/CMakeLists.txt +@@ -0,0 +1,86 @@ ++cmake_minimum_required(VERSION 3.14) ++project(Securec) ++file(GLOB SRC_LIST RELATIVE ${CMAKE_CURRENT_LIST_DIR} ++ "src/vsprintf_s.c" ++ "src/wmemmove_s.c" ++ "src/strncat_s.c" ++ "src/vsnprintf_s.c" ++ "src/fwscanf_s.c" ++ "src/scanf_s.c" ++ "src/strcat_s.c" ++ "src/sscanf_s.c" ++ "src/secureprintoutput_w.c" ++ "src/wmemcpy_s.c" ++ "src/wcsncat_s.c" ++ "src/secureprintoutput_a.c" ++ "src/secureinput_w.c" ++ "src/memcpy_s.c" ++ "src/fscanf_s.c" ++ "src/vswscanf_s.c" ++ "src/secureinput_a.c" ++ "src/sprintf_s.c" ++ "src/memmove_s.c" ++ "src/swscanf_s.c" ++ "src/snprintf_s.c" ++ "src/vscanf_s.c" ++ "src/vswprintf_s.c" ++ "src/wcscpy_s.c" ++ "src/vfwscanf_s.c" ++ "src/memset_s.c" ++ "src/wscanf_s.c" ++ "src/vwscanf_s.c" ++ "src/strtok_s.c" ++ "src/wcsncpy_s.c" ++ "src/vfscanf_s.c" ++ "src/vsscanf_s.c" ++ "src/wcstok_s.c" ++ "src/securecutil.c" ++ "src/gets_s.c" ++ "src/swprintf_s.c" ++ "src/strcpy_s.c" ++ "src/wcscat_s.c" ++ "src/strncpy_s.c" ++ ) ++ ++include_directories(./include) ++include_directories(./src) ++add_library(shared_c_sec SHARED ${SRC_LIST}) ++ ++target_compile_options(shared_c_sec PRIVATE ++ -I/usr/local/include ++ -Werror ++ -Wall ++ -O1 ++) ++target_compile_definitions(shared_c_sec PRIVATE ++ NDEBUG ++ SECUREC_SUPPORT_STRTOLD=1 ++ ) ++ ++add_library(static_c_sec STATIC ${SRC_LIST}) ++ ++target_compile_options(static_c_sec PRIVATE ++ -I/usr/local/include ++ -Werror ++ -Wall ++ -O1 ++) ++ ++target_compile_definitions(static_c_sec PRIVATE ++ NDEBUG ++ SECUREC_SUPPORT_STRTOLD=1 ++ ) ++ ++set_target_properties(static_c_sec ++ PROPERTIES ++ OUTPUT_NAME c_sec ++) ++set_target_properties(shared_c_sec ++ PROPERTIES ++ OUTPUT_NAME c_sec ++) ++install(TARGETS shared_c_sec static_c_sec OPTIONAL ++ DESTINATION lib) ++install(FILES "./include/securec.h" ++ "./include/securectype.h" ++ DESTINATION include) +-- +2.17.1 + diff --git a/tf_adapter/tests/patch/securec/securec.patch001 b/tf_adapter/tests/patch/securec/securec.patch001 new file mode 100644 index 000000000..01c2d769e --- /dev/null +++ b/tf_adapter/tests/patch/securec/securec.patch001 @@ -0,0 +1,22 @@ +diff -Npur -x .git libboundscheck/CMakeLists.txt securec/CMakeLists.txt +--- libboundscheck/CMakeLists.txt 1970-01-01 08:00:00.000000000 +0800 ++++ securec/CMakeLists.txt 2020-09-19 16:53:48.689460700 +0800 +@@ -0,0 +1,18 @@ ++cmake_minimum_required(VERSION 3.14) ++project(Securec) ++set(CMAKE_C_FLAGS_DEBUG "$ENV{CFLAGS} -fPIC -O0 -Wall -Wno-deprecated-declarations -g2 -ggdb -fno-inline-functions -fno-omit-frame-pointer -D_LIBCPP_INLINE_VISIBILITY='' -D'_LIBCPP_EXTERN_TEMPLATE(...)='") ++set(CMAKE_C_FLAGS_RELEASE "$ENV{CFLAGS} -fPIC -Wall -D_FORTIFY_SOURCE=2 -O2 -Wno-deprecated-declarations -fstack-protector-all -Wl,-z,relro,-z,now") ++set(CMAKE_EXPORT_COMPILE_COMMANDS ON) ++ ++#add flags ++set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -I/usr/local/include -Werror") ++ ++include_directories(./include) ++aux_source_directory(./src SECUREC_SRCS) ++add_library(c_sec SHARED ${SECUREC_SRCS}) ++ ++install(TARGETS c_sec ++ DESTINATION lib) ++install(FILES "./include/securec.h" ++ "./include/securectype.h" ++ DESTINATION include) diff --git a/tf_adapter/tests/ut/CMakeLists.txt b/tf_adapter/tests/ut/CMakeLists.txt new file mode 100644 index 000000000..b5f2c4db6 --- /dev/null +++ b/tf_adapter/tests/ut/CMakeLists.txt @@ -0,0 +1,66 @@ +# Copyright 2019-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. +# ============================================================================ + +project(ut_optimizers) + +set(CMAKE_CXX_STANDARD 11) + +file(GLOB_RECURSE UT_SOURCES + ${TFADAPTER_DIR}/tf_adapter/common/*.cc + ${TFADAPTER_DIR}/tf_adapter/ops/*.cc + ${TFADAPTER_DIR}/tf_adapter/optimizers/*.cc + ${TFADAPTER_DIR}/tf_adapter/util/*.cc + #${TFADAPTER_DIR}/tf_adapter/kernels/*.cc + "optimizers/testcase/*.cc" + "util/testcase/*.cc" + "kernels/testcase/*.cc" +) + +add_executable(tfadapter_utest + "main.cc" + ${UT_SOURCES} + ${TFADAPTER_DIR}/tf_adapter/kernels/geop_npu.cc + ${TFADAPTER_DIR}/tf_adapter/kernels/infeed_outfeed_ops.cc + ${TFADAPTER_DIR}/tf_adapter/kernels/npu_sys_ctl_ops.cc + ) + +target_include_directories(tfadapter_utest PRIVATE + ${TF_INSTALL_LIB_PATH}/include/ +) + +target_compile_options(tfadapter_utest PRIVATE + -g --coverage -fprofile-arcs -ftest-coverage + -Werror=format +) + +add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0) + +target_compile_definitions(tfadapter_utest PRIVATE + _FORTIFY_SOURCE=2 + LOG_CPP +) + +foreach (UT_LINK_FLAG ${UT_LINK_FLAGS}) + target_link_libraries(tfadapter_utest PUBLIC "${UT_LINK_FLAG}") +endforeach (UT_LINK_FLAG) + +string(STRIP ${PYTHON_LIB_PATH} PYTHON_LIB_PATH) +message("hrz python lib path------------${PYTHON_LIB_PATH}") +target_link_libraries(tfadapter_utest PUBLIC + $ + gtest gtest_main c_sec mmpa_stub indextransform_stub alog_stub datatransfer_stub ge_runner_stub + ${PYTHON_LIB_PATH} -lrt -ldl -lgcov +) + diff --git a/tf_adapter/tests/ut/kernels/pbtxt/geop.pbtxt b/tf_adapter/tests/ut/kernels/pbtxt/geop.pbtxt new file mode 100644 index 000000000..58575959f --- /dev/null +++ b/tf_adapter/tests/ut/kernels/pbtxt/geop.pbtxt @@ -0,0 +1,521 @@ +node { + name: "retval_Add_0_0" + op: "_Retval" + input: "GeOp1_0" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +node { + name: "GeOp1_0" + op: "GeOp" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "Tin" + value { + list { + } + } + } + attr { + key: "Tout" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_auto_tune_mode" + value { + s: "" + } + } + attr { + key: "_buffer_optimize" + value { + s: "l2_optimize" + } + } + attr { + key: "_compress_weight_conf" + value { + s: "" + } + } + attr { + key: "_debug_dir" + value { + s: "" + } + } + attr { + key: "_distribute_config" + value { + s: "" + } + } + attr { + key: "_do_npu_optimizer" + value { + s: "1" + } + } + attr { + key: "_dump_debug_mode" + value { + s: "all" + } + } + attr { + key: "_dump_mode" + value { + s: "output" + } + } + attr { + key: "_dump_path" + value { + s: "./" + } + } + attr { + key: "_dump_step" + value { + s: "1" + } + } + attr { + key: "_dynamic_dims" + value { + s: "" + } + } + attr { + key: "_dynamic_graph_execute_mode" + value { + s: "dynamic_execute" + } + } + attr { + key: "_dynamic_input" + value { + s: "0" + } + } + attr { + key: "_dynamic_node_type" + value { + s: "" + } + } + attr { + key: "_enable_compress_weight" + value { + s: "0" + } + } + attr { + key: "_enable_data_pre_proc" + value { + s: "0" + } + } + attr { + key: "_enable_dump" + value { + s: "1" + } + } + attr { + key: "_enable_dump_debug" + value { + s: "1" + } + } + attr { + key: "_enable_exception_dump" + value { + s: "" + } + } + attr { + key: "_enable_scope_fusion_passes" + value { + s: "" + } + } + attr { + key: "_enable_small_channel" + value { + s: "0" + } + } + attr { + key: "_fusion_switch_file" + value { + s: "" + } + } + attr { + key: "_graph_run_mode" + value { + s: "1" + } + } + attr { + key: "_hcom_multi_mode" + value { + s: "" + } + } + attr { + key: "_hcom_parallel" + value { + s: "0" + } + } + attr { + key: "_in_out_pair" + value { + s: "" + } + } + attr { + key: "_in_out_pair_flag" + value { + s: "1" + } + } + attr { + key: "_input_shape" + value { + s: "" + } + } + attr { + key: "_is_tailing_optimization" + value { + s: "0" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_local_device_list" + value { + s: "" + } + } + attr { + key: "_local_rank_id" + value { + s: "-1" + } + } + attr { + key: "_lower_functional_ops" + value { + s: "0" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_mstune_mode" + value { + s: "" + } + } + attr { + key: "_op_compiler_cache_dir" + value { + s: "" + } + } + attr { + key: "_op_compiler_cache_mode" + value { + s: "" + } + } + attr { + key: "_op_debug_level" + value { + s: "0" + } + } + attr { + key: "_op_select_implmode" + value { + s: "" + } + } + attr { + key: "_op_tune_mode" + value { + s: "" + } + } + attr { + key: "_optypelist_for_implmode" + value { + s: "" + } + } + attr { + key: "_precision_mode" + value { + s: "" + } + } + attr { + key: "_profiling_mode" + value { + s: "0" + } + } + attr { + key: "_profiling_options" + value { + s: "" + } + } + attr { + key: "_session_device_id" + value { + s: "" + } + } + attr { + key: "_stream_max_parallel_num" + value { + s: "" + } + } + attr { + key: "_task_index" + value { + s: "0" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_variable_format_optimize" + value { + s: "1" + } + } + attr { + key: "_work_path" + value { + s: "" + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } + attr { + key: "function" + value { + func { + name: "GeOp1_0" + } + } + } +} +library { + function { + signature { + name: "GeOp1_0" + output_arg { + name: "Add_0_retval" + type: DT_FLOAT + } + } + node_def { + name: "Const_1" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_task_index" + value { + s: "0" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 2 + } + } + tensor_content: "\000\000 A\000\000 A" + } + } + } + } + node_def { + name: "Variable" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@Variable/read" + } + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } + } + node_def { + name: "Variable/read" + op: "Identity" + input: "Variable:ref:0" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } + } + node_def { + name: "Add" + op: "Add" + input: "Const_1:output:0" + input: "Variable/read:output:0" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + } + ret { + key: "Add_0_retval" + value: "Add:z:0" + } + } +} +versions { + producer: 134 +} diff --git a/tf_adapter/tests/ut/kernels/pbtxt/geop_dynamic_input_lazy_recompile.pbtxt b/tf_adapter/tests/ut/kernels/pbtxt/geop_dynamic_input_lazy_recompile.pbtxt new file mode 100644 index 000000000..3fba450df --- /dev/null +++ b/tf_adapter/tests/ut/kernels/pbtxt/geop_dynamic_input_lazy_recompile.pbtxt @@ -0,0 +1,1019 @@ +node { + name: "arg_arg_Placeholder_0_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT32 + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +node { + name: "retval_Mul_0_0" + op: "_Retval" + input: "GeOp11_1" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT32 + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +node { + name: "GeOp11_1" + op: "GeOp" + input: "GeOp11_0" + input: "GeOp11_0:1" + input: "arg_arg_Placeholder_0_0" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "Tin" + value { + list { + type: DT_INT64 + type: DT_INT64 + type: DT_INT32 + } + } + } + attr { + key: "Tout" + value { + list { + type: DT_INT32 + } + } + } + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_auto_tune_mode" + value { + s: "" + } + } + attr { + key: "_buffer_optimize" + value { + s: "l2_optimize" + } + } + attr { + key: "_compress_weight_conf" + value { + s: "" + } + } + attr { + key: "_debug_dir" + value { + s: "" + } + } + attr { + key: "_distribute_config" + value { + s: "" + } + } + attr { + key: "_do_npu_optimizer" + value { + s: "1" + } + } + attr { + key: "_dump_debug_mode" + value { + s: "all" + } + } + attr { + key: "_dump_mode" + value { + s: "output" + } + } + attr { + key: "_dump_path" + value { + s: "" + } + } + attr { + key: "_dump_step" + value { + s: "" + } + } + attr { + key: "_dynamic_dims" + value { + s: "" + } + } + attr { + key: "_dynamic_graph_execute_mode" + value { + s: "lazy_recompile" + } + } + attr { + key: "_dynamic_input" + value { + s: "1" + } + } + attr { + key: "_dynamic_node_type" + value { + s: "" + } + } + attr { + key: "_enable_compress_weight" + value { + s: "0" + } + } + attr { + key: "_enable_data_pre_proc" + value { + s: "1" + } + } + attr { + key: "_enable_dump" + value { + s: "0" + } + } + attr { + key: "_enable_dump_debug" + value { + s: "0" + } + } + attr { + key: "_enable_exception_dump" + value { + s: "" + } + } + attr { + key: "_enable_scope_fusion_passes" + value { + s: "" + } + } + attr { + key: "_enable_small_channel" + value { + s: "0" + } + } + attr { + key: "_fusion_switch_file" + value { + s: "" + } + } + attr { + key: "_graph_run_mode" + value { + s: "1" + } + } + attr { + key: "_hcom_multi_mode" + value { + s: "" + } + } + attr { + key: "_hcom_parallel" + value { + s: "0" + } + } + attr { + key: "_in_out_pair" + value { + s: "" + } + } + attr { + key: "_in_out_pair_flag" + value { + s: "1" + } + } + attr { + key: "_input_shape" + value { + s: "" + } + } + attr { + key: "_is_dynamic_getnext" + value { + s: "1" + } + } + attr { + key: "_is_tailing_optimization" + value { + s: "0" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_local_device_list" + value { + s: "" + } + } + attr { + key: "_local_rank_id" + value { + s: "-1" + } + } + attr { + key: "_lower_functional_ops" + value { + s: "0" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_mstune_mode" + value { + s: "" + } + } + attr { + key: "_op_compiler_cache_dir" + value { + s: "" + } + } + attr { + key: "_op_compiler_cache_mode" + value { + s: "" + } + } + attr { + key: "_op_debug_level" + value { + s: "0" + } + } + attr { + key: "_op_select_implmode" + value { + s: "" + } + } + attr { + key: "_op_tune_mode" + value { + s: "" + } + } + attr { + key: "_optypelist_for_implmode" + value { + s: "" + } + } + attr { + key: "_placeholder_index" + value { + s: "2" + } + } + attr { + key: "_precision_mode" + value { + s: "" + } + } + attr { + key: "_profiling_mode" + value { + s: "0" + } + } + attr { + key: "_profiling_options" + value { + s: "" + } + } + attr { + key: "_session_device_id" + value { + s: "" + } + } + attr { + key: "_stream_max_parallel_num" + value { + s: "" + } + } + attr { + key: "_task_index" + value { + s: "0" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_variable_format_optimize" + value { + s: "1" + } + } + attr { + key: "_work_path" + value { + s: "" + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } + attr { + key: "function" + value { + func { + name: "GeOp11_1" + } + } + } +} +node { + name: "GeOp11_0" + op: "GeOp" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "Tin" + value { + list { + } + } + } + attr { + key: "Tout" + value { + list { + type: DT_INT64 + type: DT_INT64 + } + } + } + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_auto_tune_mode" + value { + s: "" + } + } + attr { + key: "_buffer_optimize" + value { + s: "l2_optimize" + } + } + attr { + key: "_compress_weight_conf" + value { + s: "" + } + } + attr { + key: "_debug_dir" + value { + s: "" + } + } + attr { + key: "_distribute_config" + value { + s: "" + } + } + attr { + key: "_do_npu_optimizer" + value { + s: "1" + } + } + attr { + key: "_dump_debug_mode" + value { + s: "all" + } + } + attr { + key: "_dump_mode" + value { + s: "output" + } + } + attr { + key: "_dump_path" + value { + s: "" + } + } + attr { + key: "_dump_step" + value { + s: "" + } + } + attr { + key: "_dynamic_dims" + value { + s: "" + } + } + attr { + key: "_dynamic_graph_execute_mode" + value { + s: "lazy_recompile" + } + } + attr { + key: "_dynamic_input" + value { + s: "1" + } + } + attr { + key: "_dynamic_node_type" + value { + s: "" + } + } + attr { + key: "_enable_compress_weight" + value { + s: "0" + } + } + attr { + key: "_enable_data_pre_proc" + value { + s: "1" + } + } + attr { + key: "_enable_dump" + value { + s: "0" + } + } + attr { + key: "_enable_dump_debug" + value { + s: "0" + } + } + attr { + key: "_enable_exception_dump" + value { + s: "" + } + } + attr { + key: "_enable_scope_fusion_passes" + value { + s: "" + } + } + attr { + key: "_enable_small_channel" + value { + s: "0" + } + } + attr { + key: "_fusion_switch_file" + value { + s: "" + } + } + attr { + key: "_graph_run_mode" + value { + s: "1" + } + } + attr { + key: "_hcom_multi_mode" + value { + s: "" + } + } + attr { + key: "_hcom_parallel" + value { + s: "0" + } + } + attr { + key: "_in_out_pair" + value { + s: "" + } + } + attr { + key: "_in_out_pair_flag" + value { + s: "1" + } + } + attr { + key: "_input_shape" + value { + s: "" + } + } + attr { + key: "_is_dynamic_getnext" + value { + s: "1" + } + } + attr { + key: "_is_tailing_optimization" + value { + s: "0" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_local_device_list" + value { + s: "" + } + } + attr { + key: "_local_rank_id" + value { + s: "-1" + } + } + attr { + key: "_lower_functional_ops" + value { + s: "0" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_mstune_mode" + value { + s: "" + } + } + attr { + key: "_op_compiler_cache_dir" + value { + s: "" + } + } + attr { + key: "_op_compiler_cache_mode" + value { + s: "" + } + } + attr { + key: "_op_debug_level" + value { + s: "0" + } + } + attr { + key: "_op_select_implmode" + value { + s: "" + } + } + attr { + key: "_op_tune_mode" + value { + s: "" + } + } + attr { + key: "_optypelist_for_implmode" + value { + s: "" + } + } + attr { + key: "_precision_mode" + value { + s: "" + } + } + attr { + key: "_profiling_mode" + value { + s: "0" + } + } + attr { + key: "_profiling_options" + value { + s: "" + } + } + attr { + key: "_session_device_id" + value { + s: "" + } + } + attr { + key: "_stream_max_parallel_num" + value { + s: "" + } + } + attr { + key: "_task_index" + value { + s: "0" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_variable_format_optimize" + value { + s: "1" + } + } + attr { + key: "_work_path" + value { + s: "" + } + } + attr { + key: "data_format" + value { + s: "NHWC" + } + } + attr { + key: "function" + value { + func { + name: "GeOp11_0" + } + } + } +} +library { + function { + signature { + name: "GeOp11_1" + input_arg { + name: "IteratorGetNext_0_arg" + type: DT_INT64 + } + input_arg { + name: "IteratorGetNext_1_arg" + type: DT_INT64 + } + input_arg { + name: "arg_arg_Placeholder_0_0_0_arg" + type: DT_INT32 + } + output_arg { + name: "Mul_0_retval" + type: DT_INT32 + } + } + node_def { + name: "Cast" + op: "Cast" + input: "IteratorGetNext_0_arg" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "DstT" + value { + type: DT_INT32 + } + } + attr { + key: "SrcT" + value { + type: DT_INT64 + } + } + attr { + key: "Truncate" + value { + b: false + } + } + } + node_def { + name: "Cast_1" + op: "Cast" + input: "IteratorGetNext_1_arg" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "DstT" + value { + type: DT_INT32 + } + } + attr { + key: "SrcT" + value { + type: DT_INT64 + } + } + attr { + key: "Truncate" + value { + b: false + } + } + } + node_def { + name: "Add" + op: "Add" + input: "Cast:y:0" + input: "Cast_1:y:0" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT32 + } + } + } + node_def { + name: "Mul" + op: "Mul" + input: "Add:z:0" + input: "arg_arg_Placeholder_0_0_0_arg" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT32 + } + } + attr { + key: "_graph_dynamic_graph_execute_mode" + value { + s: "lazy_recompile" + } + } + attr { + key: "_graph_dynamic_input" + value { + b: true + } + } + attr { + key: "_graph_dynamic_inputs_shape_range" + value { + s: "" + } + } + } + ret { + key: "Mul_0_retval" + value: "Mul:z:0" + } + } + function { + signature { + name: "GeOp11_0" + output_arg { + name: "IteratorGetNext_0_retval" + type: DT_INT64 + } + output_arg { + name: "IteratorGetNext_1_retval" + type: DT_INT64 + } + } + node_def { + name: "IteratorV2" + op: "IteratorV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_enable_data_pre_proc" + value { + s: "1" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "output_shapes" + value { + list { + shape { + dim { + size: -1 + } + dim { + size: -1 + } + } + shape { + dim { + size: -1 + } + dim { + size: -1 + } + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_INT64 + type: DT_INT64 + } + } + } + attr { + key: "shared_name" + value { + s: "IteratorV2" + } + } + } + node_def { + name: "IteratorGetNext" + op: "IteratorGetNext" + input: "IteratorV2:handle:0" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "output_shapes" + value { + list { + shape { + dim { + size: -1 + } + dim { + size: -1 + } + } + shape { + dim { + size: -1 + } + dim { + size: -1 + } + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_INT64 + type: DT_INT64 + } + } + } + } + ret { + key: "IteratorGetNext_0_retval" + value: "IteratorGetNext:components:0" + } + ret { + key: "IteratorGetNext_1_retval" + value: "IteratorGetNext:components:1" + } + } +} +versions { + producer: 134 + min_consumer: 12 +} diff --git a/tf_adapter/tests/ut/kernels/testcase/geop_npu_test.cc b/tf_adapter/tests/ut/kernels/testcase/geop_npu_test.cc new file mode 100644 index 000000000..e93a2733e --- /dev/null +++ b/tf_adapter/tests/ut/kernels/testcase/geop_npu_test.cc @@ -0,0 +1,146 @@ +#include "tf_adapter/kernels/geop_npu.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/graph/graph_constructor.h" +#include "tensorflow/core/public/version.h" +#include +#include "gtest/gtest.h" + +namespace tensorflow { +namespace { +using geDataUniquePtr = std::unique_ptr>; +class NpuGetNextOutputInfo { +public: + NpuGetNextOutputInfo(ge::Placement placement, std::vector dims, + size_t output_size, geDataUniquePtr data) + : placement_(placement), dims_(dims), output_size_(output_size), data_(std::move(data)) {} + ~NpuGetNextOutputInfo() { LOG(INFO) << "[GEOP] Release NpuGetNextOutputInfo."; } + ge::Placement placement_; + std::vector dims_; + size_t output_size_; + geDataUniquePtr data_; +}; + +class NpuHostGetNextAllocator : public tensorflow::Allocator { + public: + static tensorflow::Allocator *Create(std::unique_ptr output) { + return new (std::nothrow) NpuHostGetNextAllocator(std::move(output)); + } + private: + explicit NpuHostGetNextAllocator(std::unique_ptr output) : output_(std::move(output)) { + LOG(INFO) << "[GEOP] getnext data addr:" << reinterpret_cast(output_->data_.get()); + } + ~NpuHostGetNextAllocator() override { + LOG(INFO) << "[GEOP] Release getnext data addr:" << reinterpret_cast(output_->data_.get()); + } + std::string Name() override { return "NpuHostGetNextAllocator"; } + void *AllocateRaw(size_t alignment, size_t num_bytes) override { return output_.get(); } + void DeallocateRaw(void *ptr) override { delete this; } + std::unique_ptr output_; +}; + +class GeOpTest : public testing::Test { + protected: + virtual void SetUp() {} + virtual void TearDown() {} +}; +class DummyDevice : public DeviceBase { + public: + DummyDevice(Env* env, bool save) : DeviceBase(env), save_(save) {} + bool RequiresRecordingAccessedTensors() const override { return save_; } + Allocator* GetAllocator(AllocatorAttributes /*attr*/) override { return cpu_allocator(); } + private: + bool save_; +}; + +TEST_F(GeOpTest, GeOpFuncTest) { + Env* env = Env::Default(); + GraphDef graph_def; + std::string graph_def_path = "tf_adapter/tests/ut/kernels/pbtxt/geop.pbtxt"; + ReadTextProto(env, graph_def_path, &graph_def); + for (int i = 0; i < graph_def.node_size(); i++) { + NodeDef* node_def = graph_def.mutable_node(i); + if (node_def->name().find("GeOp") != std::string::npos) { + OpKernelContext::Params params; + params.record_tensor_accesses = false; + auto device = absl::make_unique(env, params.record_tensor_accesses); + params.device = device.get(); + Status status; + std::unique_ptr op(CreateOpKernel(DEVICE_CPU, params.device, cpu_allocator(), + *node_def, TF_GRAPH_DEF_VERSION, &status)); + EXPECT_TRUE(status.ok()); + AsyncOpKernel* async_op = op->AsAsync(); + params.op_kernel = async_op; + params.session_handle = "session_0"; + gtl::InlinedVector inputs; + params.inputs = &inputs; + + //function library + FunctionDefLibrary func_def_lib = graph_def.library(); + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), func_def_lib)); + OptimizerOptions opts; + std::unique_ptr proc_flr( + new ProcessFunctionLibraryRuntime(nullptr, Env::Default(), TF_GRAPH_DEF_VERSION, + lib_def.get(), opts, nullptr, nullptr)); + FunctionLibraryRuntime* flr = proc_flr->GetFLR(ProcessFunctionLibraryRuntime::kDefaultFLRDevice); + params.function_library = flr; + auto ctx = absl::make_unique(¶ms); + AsyncOpKernel::DoneCallback done = []() { LOG(INFO) << "DONE DoneCallback"; }; + async_op->ComputeAsync(ctx.get(), done); + EXPECT_EQ(ctx->status().ok(), true); + } + } +} +TEST_F(GeOpTest, GeOpDynamicInputTest) { + Env* env = Env::Default(); + GraphDef graph_def; + std::string graph_def_path = "tf_adapter/tests/ut/kernels/pbtxt/geop_dynamic_input_lazy_recompile.pbtxt"; + ReadTextProto(env, graph_def_path, &graph_def); + for (int i = 0; i < graph_def.node_size(); i++) { + NodeDef* node_def = graph_def.mutable_node(i); + if (node_def->name() == "GeOp11_1") { + OpKernelContext::Params params; + params.record_tensor_accesses = false; + auto device = absl::make_unique(env, params.record_tensor_accesses); + params.device = device.get(); + Status status; + std::unique_ptr op(CreateOpKernel(DEVICE_CPU, params.device, cpu_allocator(), + *node_def, TF_GRAPH_DEF_VERSION, &status)); + EXPECT_TRUE(status.ok()); + AsyncOpKernel* async_op = op->AsAsync(); + params.op_kernel = async_op; + params.session_handle = "session_0"; + std::vector ge_output1_dims{2,2}; + auto getnext_output1_info = std::unique_ptr(new NpuGetNextOutputInfo( + ge::kPlacementDevice, ge_output1_dims, 8, nullptr)); + Allocator *allocator1 = NpuHostGetNextAllocator::Create(std::move(getnext_output1_info)); + Tensor a(allocator1, DT_INT64, TensorShape({2, 2})); + std::vector ge_output2_dims{2,2}; + auto getnext_output2_info = std::unique_ptr(new NpuGetNextOutputInfo( + ge::kPlacementDevice, ge_output2_dims, 8, nullptr)); + Allocator *allocator2 = NpuHostGetNextAllocator::Create(std::move(getnext_output2_info)); + Tensor b(allocator2, DT_INT64, TensorShape({2, 2})); + Tensor c(DT_INT32, TensorShape({1,})); + gtl::InlinedVector inputs{TensorValue(&a), TensorValue(&b), + TensorValue(&c)}; + params.inputs = &inputs; + + //function library + FunctionDefLibrary func_def_lib = graph_def.library(); + std::unique_ptr lib_def( + new FunctionLibraryDefinition(OpRegistry::Global(), func_def_lib)); + OptimizerOptions opts; + std::unique_ptr proc_flr( + new ProcessFunctionLibraryRuntime(nullptr, Env::Default(), TF_GRAPH_DEF_VERSION, + lib_def.get(), opts, nullptr, nullptr)); + FunctionLibraryRuntime* flr = proc_flr->GetFLR(ProcessFunctionLibraryRuntime::kDefaultFLRDevice); + params.function_library = flr; + auto ctx = absl::make_unique(¶ms); + AsyncOpKernel::DoneCallback done = []() { LOG(INFO) << "DONE DoneCallback"; }; + async_op->ComputeAsync(ctx.get(), done); + EXPECT_EQ(ctx->status().ok(), true); + } + } +} +} +} //end tensorflow \ No newline at end of file diff --git a/tf_adapter/tests/ut/main.cc b/tf_adapter/tests/ut/main.cc new file mode 100644 index 000000000..fa1b904c0 --- /dev/null +++ b/tf_adapter/tests/ut/main.cc @@ -0,0 +1,7 @@ +#include + +int main(int argc, char **argv) { + testing::InitGoogleTest(&argc, argv); + int ret = RUN_ALL_TESTS(); + return ret; +} \ No newline at end of file diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/add_input_pass_localhost_test.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/add_input_pass_localhost_test.pbtxt new file mode 100644 index 000000000..63e9caedf --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/add_input_pass_localhost_test.pbtxt @@ -0,0 +1,162 @@ +node { + name: "Const_1" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_task_index" + value { + s: "0" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 2 + } + } + tensor_content: "\000\000 A\000\000 A" + } + } + } +} +node { + name: "Variable" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@Variable/read" + } + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "Variable/read" + op: "Identity" + input: "Variable" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } +} +node { + name: "Add" + op: "Add" + input: "Const_1" + input: "Variable/read" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "retval_Add_0_0" + op: "_Retval" + input: "Add" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +library { +} +versions { + producer: 134 +} \ No newline at end of file diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/add_input_pass_no_need_optimize_test.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/add_input_pass_no_need_optimize_test.pbtxt new file mode 100644 index 000000000..6fa474270 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/add_input_pass_no_need_optimize_test.pbtxt @@ -0,0 +1,168 @@ +node { + name: "Const_1" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_job" + value { + s: "worker" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_task_index" + value { + s: "0" + } + } + attr { + key: "_NoNeedOptimize" + value { + b: true + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 2 + } + } + tensor_content: "\000\000 A\000\000 A" + } + } + } +} +node { + name: "Variable" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@Variable/read" + } + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "Variable/read" + op: "Identity" + input: "Variable" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } +} +node { + name: "Add" + op: "Add" + input: "Const_1" + input: "Variable/read" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "retval_Add_0_0" + op: "_Retval" + input: "Add" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +library { +} +versions { + producer: 134 +} \ No newline at end of file diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/add_input_pass_worker_test.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/add_input_pass_worker_test.pbtxt new file mode 100644 index 000000000..5da432972 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/add_input_pass_worker_test.pbtxt @@ -0,0 +1,162 @@ +node { + name: "Const_1" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_job" + value { + s: "worker" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_task_index" + value { + s: "0" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 2 + } + } + tensor_content: "\000\000 A\000\000 A" + } + } + } +} +node { + name: "Variable" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@Variable/read" + } + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "Variable/read" + op: "Identity" + input: "Variable" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } +} +node { + name: "Add" + op: "Add" + input: "Const_1" + input: "Variable/read" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "retval_Add_0_0" + op: "_Retval" + input: "Add" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +library { +} +versions { + producer: 134 +} \ No newline at end of file diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/ctrl_if_test.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/ctrl_if_test.pbtxt new file mode 100644 index 000000000..e0674004c --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/ctrl_if_test.pbtxt @@ -0,0 +1,273 @@ +node { + name: "Placeholder" + op: "Placeholder" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + unknown_rank: true + } + } + } +} +node { + name: "Greater/y" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0 + } + } + } +} +node { + name: "Greater" + op: "Greater" + input: "Placeholder" + input: "Greater/y" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "IF_branch_1" + op: "If" + input: "Greater" + input: "Placeholder" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "Tcond" + value { + type: DT_BOOL + } + } + attr { + key: "Tin" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "Tout" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "else_branch" + value { + func { + name: "finall_branch1_2VgofXZ96o4" + } + } + } + attr { + key: "output_shapes" + value { + list { + } + } + } + attr { + key: "then_branch" + value { + func { + name: "finall_branch1_2VgofXZ96o4" + } + } + } +} +library { + function { + signature { + name: "finall_branch2_0zkt64DYFRw" + input_arg { + name: "x_0" + type: DT_FLOAT + } + output_arg { + name: "x" + type: DT_FLOAT + } + } + ret { + key: "x" + value: "x_0" + } + attr { + key: "_disable_call_shape_inference" + value { + b: true + } + } + } + function { + signature { + name: "finall_branch1_2VgofXZ96o4" + input_arg { + name: "x" + type: DT_FLOAT + } + output_arg { + name: "if_branch_2" + type: DT_FLOAT + } + is_stateful: true + } + node_def { + name: "Greater/y" + op: "Const" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0 + } + } + } + } + node_def { + name: "Greater" + op: "Greater" + input: "x" + input: "Greater/y:output:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + } + node_def { + name: "IF_branch_2" + op: "If" + input: "Greater:z:0" + input: "x" + attr { + key: "Tcond" + value { + type: DT_BOOL + } + } + attr { + key: "Tin" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "Tout" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "else_branch" + value { + func { + name: "finall_branch2_0zkt64DYFRw" + } + } + } + attr { + key: "output_shapes" + value { + list { + } + } + } + attr { + key: "then_branch" + value { + func { + name: "finall_branch2_0zkt64DYFRw" + } + } + } + } + ret { + key: "if_branch_2" + value: "IF_branch_2:output:0" + } + attr { + key: "_disable_call_shape_inference" + value { + b: true + } + } + } +} +versions { + producer: 134 + min_consumer: 12 +} diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/dp_test_build_geop.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/dp_test_build_geop.pbtxt new file mode 100644 index 000000000..87542c1bf --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/dp_test_build_geop.pbtxt @@ -0,0 +1,319 @@ +node { + name: "normalize_element/component_0" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_DOUBLE + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_DOUBLE + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\000\000\000\000\000\000\360?\000\000\000\000\000\000\000@\000\000\000\000\000\000\010@\000\000\000\000\000\000\020@" + } + } + } +} +node { + name: "batch_size" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_INT64 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT64 + tensor_shape { + } + int64_val: 2 + } + } + } +} +node { + name: "drop_remainder" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + bool_val: false + } + } + } +} +node { + name: "optimizations" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 3 + } + } + string_val: "map_and_batch_fusion" + string_val: "noop_elimination" + string_val: "shuffle_and_repeat_fusion" + } + } + } +} +node { + name: "IteratorV2" + op: "IteratorV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_enable_data_pre_proc" + value { + s: "1" + } + } + attr { + key: "_class" + value { + list { + s: "loc:@MakeIterator" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "output_shapes" + value { + list { + shape { + dim { + size: -1 + } + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_DOUBLE + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "TensorSliceDataset" + op: "TensorSliceDataset" + input: "normalize_element/component_0" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "Toutput_types" + value { + list { + type: DT_DOUBLE + } + } + } + attr { + key: "output_shapes" + value { + list { + shape { + } + } + } + } +} +node { + name: "BatchDatasetV2" + op: "BatchDatasetV2" + input: "TensorSliceDataset" + input: "batch_size" + input: "drop_remainder" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "output_shapes" + value { + list { + shape { + dim { + size: -1 + } + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_DOUBLE + } + } + } + attr { + key: "parallel_copy" + value { + b: false + } + } +} +node { + name: "OptimizeDataset" + op: "OptimizeDataset" + input: "BatchDatasetV2" + input: "optimizations" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "optimization_configs" + value { + list { + s: "map_vectorization:use_choose_fastest:false" + } + } + } + attr { + key: "output_shapes" + value { + list { + shape { + dim { + size: -1 + } + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_DOUBLE + } + } + } +} +node { + name: "ModelDataset" + op: "ModelDataset" + input: "OptimizeDataset" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "algorithm" + value { + i: 0 + } + } + attr { + key: "cpu_budget" + value { + i: 0 + } + } + attr { + key: "output_shapes" + value { + list { + shape { + dim { + size: -1 + } + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_DOUBLE + } + } + } +} +node { + name: "MakeIterator" + op: "MakeIterator" + input: "ModelDataset" + input: "IteratorV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" +} +library { +} +versions { + producer: 134 +} diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/dp_test_no_dataset_in_device.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/dp_test_no_dataset_in_device.pbtxt new file mode 100644 index 000000000..e89efaed5 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/dp_test_no_dataset_in_device.pbtxt @@ -0,0 +1,368 @@ +node { + name: "normalize_element/component_0" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_DOUBLE + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_DOUBLE + tensor_shape { + dim { + size: 4 + } + } + tensor_content: "\000\000\000\000\000\000\360?\000\000\000\000\000\000\000@\000\000\000\000\000\000\010@\000\000\000\000\000\000\020@" + } + } + } +} +node { + name: "batch_size" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_INT64 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT64 + tensor_shape { + } + int64_val: 2 + } + } + } +} +node { + name: "drop_remainder" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_BOOL + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_BOOL + tensor_shape { + } + bool_val: false + } + } + } +} +node { + name: "count" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_INT64 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT64 + tensor_shape { + } + int64_val: -1 + } + } + } +} +node { + name: "optimizations" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 3 + } + } + string_val: "map_and_batch_fusion" + string_val: "noop_elimination" + string_val: "shuffle_and_repeat_fusion" + } + } + } +} +node { + name: "IteratorV2" + op: "IteratorV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_enable_data_pre_proc" + value { + s: "1" + } + } + attr { + key: "_class" + value { + list { + s: "loc:@MakeIterator" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "output_shapes" + value { + list { + shape { + dim { + size: -1 + } + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_DOUBLE + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "TensorSliceDataset" + op: "TensorSliceDataset" + input: "normalize_element/component_0" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "Toutput_types" + value { + list { + type: DT_DOUBLE + } + } + } + attr { + key: "output_shapes" + value { + list { + shape { + } + } + } + } +} +node { + name: "BatchDatasetV2" + op: "BatchDatasetV2" + input: "TensorSliceDataset" + input: "batch_size" + input: "drop_remainder" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "output_shapes" + value { + list { + shape { + dim { + size: -1 + } + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_DOUBLE + } + } + } + attr { + key: "parallel_copy" + value { + b: false + } + } +} +node { + name: "RepeatDataset" + op: "RepeatDataset" + input: "BatchDatasetV2" + input: "count" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "output_shapes" + value { + list { + shape { + dim { + size: -1 + } + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_DOUBLE + } + } + } +} +node { + name: "OptimizeDataset" + op: "OptimizeDataset" + input: "RepeatDataset" + input: "optimizations" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "optimization_configs" + value { + list { + s: "map_vectorization:use_choose_fastest:false" + } + } + } + attr { + key: "output_shapes" + value { + list { + shape { + dim { + size: -1 + } + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_DOUBLE + } + } + } +} +node { + name: "ModelDataset" + op: "ModelDataset" + input: "OptimizeDataset" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "algorithm" + value { + i: 0 + } + } + attr { + key: "cpu_budget" + value { + i: 0 + } + } + attr { + key: "output_shapes" + value { + list { + shape { + dim { + size: -1 + } + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_DOUBLE + } + } + } +} +node { + name: "MakeIterator" + op: "MakeIterator" + input: "ModelDataset" + input: "IteratorV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" +} +library { +} +versions { + producer: 134 +} diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/get_attr_job_chief_test.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/get_attr_job_chief_test.pbtxt new file mode 100644 index 000000000..15e4de379 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/get_attr_job_chief_test.pbtxt @@ -0,0 +1,156 @@ +node { + name: "Const_1" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_job" + value { + s: "worker" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_task_index" + value { + s: "0" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 2 + } + } + tensor_content: "\000\000 A\000\000 A" + } + } + } +} +node { + name: "Variable" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@Variable/read" + } + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "Variable/read" + op: "Identity" + input: "Variable" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } +} +node { + name: "Add" + op: "Add" + input: "Const_1" + input: "Variable/read" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "retval_Add_0_0" + op: "_Retval" + input: "Add" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +library { +} +versions { + producer: 134 +} \ No newline at end of file diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/om_test_accumulate.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_accumulate.pbtxt new file mode 100644 index 000000000..348002fa2 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_accumulate.pbtxt @@ -0,0 +1,374 @@ +node { + name: "random_uniform/shape" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 2 + } + } + tensor_content: "\002\000\000\000\002\000\000\000" + } + } + } +} +node { + name: "random_uniform/min" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + } + int_val: 2 + } + } + } +} +node { + name: "random_uniform/max" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + } + int_val: 6 + } + } + } +} +node { + name: "random_uniform_1/min" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + } + int_val: 3 + } + } + } +} +node { + name: "random_uniform_1/max" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + } + int_val: 5 + } + } + } +} +node { + name: "AccumulateNV2/Internal/_0" + op: "TemporaryVariable" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "shape" + value { + shape { + dim { + } + } + } + } + attr { + key: "var_name" + value { + s: "AccumulateNV2/Internal/Accumulator" + } + } + experimental_debug_info { + original_node_names: "AccumulateNV2" + } +} +node { + name: "random_uniform" + op: "RandomUniformInt" + input: "random_uniform/shape" + input: "random_uniform/min" + input: "random_uniform/max" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT32 + } + } + attr { + key: "Tout" + value { + type: DT_INT32 + } + } + attr { + key: "seed" + value { + i: 0 + } + } + attr { + key: "seed2" + value { + i: 0 + } + } +} +node { + name: "random_uniform_1" + op: "RandomUniformInt" + input: "random_uniform/shape" + input: "random_uniform_1/min" + input: "random_uniform_1/max" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT32 + } + } + attr { + key: "Tout" + value { + type: DT_INT32 + } + } + attr { + key: "seed" + value { + i: 0 + } + } + attr { + key: "seed2" + value { + i: 0 + } + } +} +node { + name: "AccumulateNV2/Internal/_3" + op: "Const" + input: "^random_uniform" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 2 + } + dim { + size: 2 + } + } + int_val: 0 + } + } + } + experimental_debug_info { + original_node_names: "AccumulateNV2" + } +} +node { + name: "AccumulateNV2/Internal/_4" + op: "Assign" + input: "AccumulateNV2/Internal/_0" + input: "AccumulateNV2/Internal/_3" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT32 + } + } + attr { + key: "_grappler_relax_allocator_constraints" + value { + b: true + } + } + attr { + key: "use_locking" + value { + b: true + } + } + attr { + key: "validate_shape" + value { + b: false + } + } + experimental_debug_info { + original_node_names: "AccumulateNV2" + } +} +node { + name: "AccumulateNV2/Internal/_6" + op: "AssignAdd" + input: "AccumulateNV2/Internal/_4" + input: "random_uniform_1" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT32 + } + } + attr { + key: "use_locking" + value { + b: true + } + } + experimental_debug_info { + original_node_names: "AccumulateNV2" + } +} +node { + name: "AccumulateNV2/Internal/_5" + op: "AssignAdd" + input: "AccumulateNV2/Internal/_4" + input: "random_uniform" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT32 + } + } + attr { + key: "use_locking" + value { + b: true + } + } + experimental_debug_info { + original_node_names: "AccumulateNV2" + } +} +node { + name: "AccumulateNV2" + op: "DestroyTemporaryVariable" + input: "AccumulateNV2/Internal/_4" + input: "^AccumulateNV2/Internal/_5" + input: "^AccumulateNV2/Internal/_6" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT32 + } + } + attr { + key: "var_name" + value { + s: "AccumulateNV2/Internal/Accumulator" + } + } + experimental_debug_info { + original_node_names: "AccumulateNV2" + } +} +library { +} +versions { + producer: 134 +} diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/om_test_build_geop.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_build_geop.pbtxt new file mode 100644 index 000000000..feab4309e --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_build_geop.pbtxt @@ -0,0 +1,330 @@ +node { + name: "Const_1" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_task_index" + value { + s: "0" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "_dump_debug_mode" + value { + s: "all" + } + } + attr { + key: "_dump_mode" + value { + s: "output" + } + } + attr { + key: "_dump_path" + value { + s: "./" + } + } + attr { + key: "_dump_step" + value { + s: "1" + } + } + attr { + key: "_enable_dump" + value { + s: "1" + } + } + attr { + key: "_enable_dump_debug" + value { + s: "1" + } + } + attr { + key: "_enable_exception_dump" + value { + s: "" + } + } + attr { + key: "_lower_functional_ops" + value { + s: "0" + } + } + attr { + key: "_local_rank_id" + value { + s: "-1" + } + } + attr { + key: "_local_device_list" + value { + s: "" + } + } + attr { + key: "_variable_format_optimize" + value { + s: "1" + } + } + attr { + key: "_graph_memory_max_size" + value { + s: "1" + } + } + attr { + key: "_variable_memory_max_size" + value { + s: "1" + } + } + attr { + key: "_stream_max_parallel_num" + value { + s: "1" + } + } + attr { + key: "_is_tailing_optimization" + value { + s: "0" + } + } + attr { + key: "_enable_scope_fusion_passes" + value { + s: "0" + } + } + attr { + key: "_enable_exception_dump" + value { + s: "0" + } + } + attr { + key: "_op_select_implmode" + value { + s: "" + } + } + attr { + key: "_dynamic_node_type" + value { + s: "0" + } + } + attr { + key: "_enable_small_channel" + value { + s: "0" + } + } + attr { + key: "_fusion_switch_file" + value { + s: "" + } + } + attr { + key: "_enable_compress_weight" + value { + s: "0" + } + } + attr { + key: "_compress_weight_conf" + value { + s: "" + } + } + attr { + key: "_op_compiler_cache_mode" + value { + s: "" + } + } + attr { + key: "_op_compiler_cache_dir" + value { + s: "" + } + } + attr { + key: "_debug_dir" + value { + s: "" + } + } + attr { + key: "_hcom_multi_mode" + value { + s: "" + } + } + attr { + key: "_session_device_id" + value { + s: "" + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 2 + } + } + tensor_content: "\000\000 A\000\000 A" + } + } + } +} +node { + name: "Variable" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@Variable/read" + } + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "Variable/read" + op: "Identity" + input: "Variable" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } +} +node { + name: "Add" + op: "Add" + input: "Const_1" + input: "Variable/read" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "retval_Add_0_0" + op: "_Retval" + input: "Add" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +library { +} +versions { + producer: 134 +} \ No newline at end of file diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/om_test_copy_variables.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_copy_variables.pbtxt new file mode 100644 index 000000000..53bd743b5 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_copy_variables.pbtxt @@ -0,0 +1,622 @@ +node { + name: "Placeholder" + op: "Placeholder" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 10 + } + dim { + size: 10 + } + } + } + } +} +node { + name: "Variable" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "1" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_class" + value { + list { + s: "loc:@Momentum/update_Variable/ApplyMomentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 10 + } + dim { + size: 10 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "Variable_1" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@Momentum/update_Variable_1/ApplyMomentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 10 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "Variable/Momentum" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@Momentum/update_Variable/ApplyMomentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 10 + } + dim { + size: 10 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "Momentum/learning_rate" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.01 + } + } + } +} +node { + name: "gradients/Softmax_grad/Sum/reduction_indices" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + } + int_val: -1 + } + } + } +} +node { + name: "Momentum/momentum" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + float_val: 0.9 + } + } + } +} +node { + name: "Variable_1/Momentum" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@Momentum/update_Variable_1/ApplyMomentum" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 10 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "gradients/add_grad/Sum/reduction_indices" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 1 + } + } + int_val: 0 + } + } + } +} +node { + name: "gradients/sub_grad/tuple/control_dependency" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 10 + } + dim { + size: 10 + } + } + float_val: 0.01 + } + } + } +} +node { + name: "Variable/read" + op: "Identity" + input: "Variable" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@Momentum/update_Variable/ApplyMomentum" + } + } + } +} +node { + name: "Variable_1/read" + op: "Identity" + input: "Variable_1" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_class" + value { + list { + s: "loc:@Momentum/update_Variable_1/ApplyMomentum" + } + } + } +} +node { + name: "MatMul" + op: "MatMul" + input: "Placeholder" + input: "Variable/read" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "transpose_a" + value { + b: false + } + } + attr { + key: "transpose_b" + value { + b: false + } + } +} +node { + name: "add" + op: "AddV2" + input: "MatMul" + input: "Variable_1/read" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_without_npu_compile" + value { + b: true + } + } +} +node { + name: "Softmax" + op: "Softmax" + input: "add" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "gradients/Softmax_grad/mul" + op: "Mul" + input: "Softmax" + input: "gradients/sub_grad/tuple/control_dependency" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "gradients/Softmax_grad/Sum" + op: "Sum" + input: "gradients/Softmax_grad/mul" + input: "gradients/Softmax_grad/Sum/reduction_indices" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: true + } + } +} +node { + name: "gradients/Softmax_grad/sub" + op: "Sub" + input: "gradients/sub_grad/tuple/control_dependency" + input: "gradients/Softmax_grad/Sum" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "gradients/Softmax_grad/mul_1" + op: "Mul" + input: "Softmax" + input: "gradients/Softmax_grad/sub" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "gradients/add_grad/Sum" + op: "Sum" + input: "gradients/Softmax_grad/mul_1" + input: "gradients/add_grad/Sum/reduction_indices" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "keep_dims" + value { + b: false + } + } +} +node { + name: "Momentum/update_Variable_1/ApplyMomentum" + op: "ApplyMomentum" + input: "Variable_1" + input: "Variable_1/Momentum" + input: "Momentum/learning_rate" + input: "gradients/add_grad/Sum" + input: "Momentum/momentum" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: false + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "gradients/MatMul_grad/MatMul_1" + op: "MatMul" + input: "Placeholder" + input: "gradients/Softmax_grad/mul_1" + input: "^gradients/add_grad/Sum" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "transpose_a" + value { + b: true + } + } + attr { + key: "transpose_b" + value { + b: false + } + } +} +node { + name: "Momentum/update_Variable/ApplyMomentum" + op: "ApplyMomentum" + input: "Variable" + input: "Variable/Momentum" + input: "Momentum/learning_rate" + input: "gradients/MatMul_grad/MatMul_1" + input: "Momentum/momentum" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "use_locking" + value { + b: false + } + } + attr { + key: "use_nesterov" + value { + b: false + } + } +} +node { + name: "Momentum" + op: "NoOp" + input: "^Momentum/update_Variable/ApplyMomentum" + input: "^Momentum/update_Variable_1/ApplyMomentum" + device: "/job:localhost/replica:0/task:0/device:CPU:0" +} +library { +} +versions { + producer: 134 +} diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/om_test_getnext_build_geop.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_getnext_build_geop.pbtxt new file mode 100644 index 000000000..f9dc46cb5 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_getnext_build_geop.pbtxt @@ -0,0 +1,122 @@ +node { + name: "IteratorV2" + op: "IteratorV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_enable_data_pre_proc" + value { + s: "1" + } + } + attr { + key: "_graph_dynamic_graph_execute_mode" + value { + s: "dynamic_execute" + } + } + attr { + key: "_graph_dynamic_input" + value { + b: true + } + } + attr { + key: "_graph_dynamic_inputs_shape_range" + value { + s: "data:[2,3],[1~2,3~4];getnext:[2~3,4~5]" + } + } + attr { + key: "_is_train_graph" + value { + b: false + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_DOUBLE + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "IteratorGetNext" + op: "IteratorGetNext" + input: "IteratorV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_DOUBLE + } + } + } +} +library { +} +versions { + producer: 134 +} diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/om_test_getnext_lazy_recompile.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_getnext_lazy_recompile.pbtxt new file mode 100644 index 000000000..dfeb744a4 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_getnext_lazy_recompile.pbtxt @@ -0,0 +1,238 @@ +node { + name: "arg_arg_Placeholder_0_0" + op: "_Arg" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT32 + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +node { + name: "IteratorV2" + op: "IteratorV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_enable_data_pre_proc" + value { + s: "1" + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "output_shapes" + value { + list { + shape { + dim { + size: -1 + } + dim { + size: -1 + } + } + shape { + dim { + size: -1 + } + dim { + size: -1 + } + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_INT64 + type: DT_INT64 + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "IteratorGetNext" + op: "IteratorGetNext" + input: "IteratorV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "output_shapes" + value { + list { + shape { + dim { + size: -1 + } + dim { + size: -1 + } + } + shape { + dim { + size: -1 + } + dim { + size: -1 + } + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_INT64 + type: DT_INT64 + } + } + } +} +node { + name: "Cast" + op: "Cast" + input: "IteratorGetNext" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "DstT" + value { + type: DT_INT32 + } + } + attr { + key: "SrcT" + value { + type: DT_INT64 + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "Cast_1" + op: "Cast" + input: "IteratorGetNext:1" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "DstT" + value { + type: DT_INT32 + } + } + attr { + key: "SrcT" + value { + type: DT_INT64 + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "Add" + op: "Add" + input: "Cast" + input: "Cast_1" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT32 + } + } +} +node { + name: "Mul" + op: "Mul" + input: "Add" + input: "arg_arg_Placeholder_0_0" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_graph_dynamic_graph_execute_mode" + value { + s: "lazy_recompile" + } + } + attr { + key: "_graph_dynamic_input" + value { + b: true + } + } + attr { + key: "_graph_dynamic_inputs_shape_range" + value { + s: "" + } + } + attr { + key: "T" + value { + type: DT_INT32 + } + } +} +library { +} +versions { + producer: 134 + min_consumer: 12 +} diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/om_test_in_out_pair.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_in_out_pair.pbtxt new file mode 100644 index 000000000..c61f48e84 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_in_out_pair.pbtxt @@ -0,0 +1,168 @@ +node { + name: "Const_1" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "1" + } + } + attr { + key: "_task_index" + value { + s: "0" + } + } + attr { + key: "_in_out_pair" + value { + s: "[['Variable'],['Add']]" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 2 + } + } + tensor_content: "\000\000 A\000\000 A" + } + } + } +} +node { + name: "Variable" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@Variable/read" + } + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "Variable/read" + op: "Identity" + input: "Variable" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } +} +node { + name: "Add" + op: "Add" + input: "Const_1" + input: "Variable/read" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "retval_Add_0_0" + op: "_Retval" + input: "Add" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +library { +} +versions { + producer: 134 +} \ No newline at end of file diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/om_test_in_out_pair_flag_false.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_in_out_pair_flag_false.pbtxt new file mode 100644 index 000000000..967f25862 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_in_out_pair_flag_false.pbtxt @@ -0,0 +1,168 @@ +node { + name: "Const_1" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "1" + } + } + attr { + key: "_task_index" + value { + s: "0" + } + } + attr { + key: "_in_out_pair" + value { + s: "[[['Variable', 'Const_1'],['Add']]]" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 2 + } + } + tensor_content: "\000\000 A\000\000 A" + } + } + } +} +node { + name: "Variable" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@Variable/read" + } + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "Variable/read" + op: "Identity" + input: "Variable" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } +} +node { + name: "Add" + op: "Add" + input: "Const_1" + input: "Variable/read" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "retval_Add_0_0" + op: "_Retval" + input: "Add" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +library { +} +versions { + producer: 134 +} \ No newline at end of file diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/om_test_in_out_pair_flag_true.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_in_out_pair_flag_true.pbtxt new file mode 100644 index 000000000..86d42c44e --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_in_out_pair_flag_true.pbtxt @@ -0,0 +1,174 @@ +node { + name: "Const_1" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "1" + } + } + attr { + key: "_task_index" + value { + s: "0" + } + } + attr { + key: "_in_out_pair" + value { + s: "[[['Variable'],['Add']]]" + } + } + attr { + key: "_in_out_pair_flag" + value { + s: "0" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + dim { + size: 2 + } + } + tensor_content: "\000\000 A\000\000 A" + } + } + } +} +node { + name: "Variable" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@Variable/read" + } + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 2 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "Variable/read" + op: "Identity" + input: "Variable" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } +} +node { + name: "Add" + op: "Add" + input: "Const_1" + input: "Variable/read" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "retval_Add_0_0" + op: "_Retval" + input: "Add" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "index" + value { + i: 0 + } + } +} +library { +} +versions { + producer: 134 +} \ No newline at end of file diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/om_test_iterator_build_geop.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_iterator_build_geop.pbtxt new file mode 100644 index 000000000..7198bc97e --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_iterator_build_geop.pbtxt @@ -0,0 +1,232 @@ +node { + name: "normalize_element/component_0" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_DOUBLE + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_DOUBLE + tensor_shape { + dim { + size: 5 + } + } + tensor_content: "\000\000\000\000\000\000\360?\000\000\000\000\000\000\000@\000\000\000\000\000\000\010@\000\000\000\000\000\000\020@\000\000\000\000\000\000\024@" + } + } + } +} +node { + name: "optimizations" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 3 + } + } + string_val: "map_and_batch_fusion" + string_val: "noop_elimination" + string_val: "shuffle_and_repeat_fusion" + } + } + } +} +node { + name: "IteratorV2" + op: "IteratorV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_enable_data_pre_proc" + value { + s: "1" + } + } + attr { + key: "_class" + value { + list { + s: "loc:@MakeIterator" + } + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_DOUBLE + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "TensorSliceDataset" + op: "TensorSliceDataset" + input: "normalize_element/component_0" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "Toutput_types" + value { + list { + type: DT_DOUBLE + } + } + } + attr { + key: "output_shapes" + value { + list { + shape { + } + } + } + } +} +node { + name: "OptimizeDataset" + op: "OptimizeDataset" + input: "TensorSliceDataset" + input: "optimizations" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "optimization_configs" + value { + list { + s: "map_vectorization:use_choose_fastest:false" + } + } + } + attr { + key: "output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_DOUBLE + } + } + } +} +node { + name: "ModelDataset" + op: "ModelDataset" + input: "OptimizeDataset" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "algorithm" + value { + i: 0 + } + } + attr { + key: "cpu_budget" + value { + i: 0 + } + } + attr { + key: "output_shapes" + value { + list { + shape { + } + } + } + } + attr { + key: "output_types" + value { + list { + type: DT_DOUBLE + } + } + } +} +node { + name: "MakeIterator" + op: "MakeIterator" + input: "ModelDataset" + input: "IteratorV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" +} +library { +} +versions { + producer: 134 +} diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/om_test_merge_clusters.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_merge_clusters.pbtxt new file mode 100644 index 000000000..79cffa490 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_merge_clusters.pbtxt @@ -0,0 +1,296 @@ +node { + name: "save/SaveV2/shape_and_slices" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 3 + } + } + string_val: "" + string_val: "" + string_val: "" + } + } + } +} +node { + name: "v1" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "_enable_data_pre_proc" + value { + s: "0" + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 3 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "save/filename/input" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + } + string_val: "model" + } + } + } +} +node { + name: "v2" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 5 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "save/SaveV2/tensor_names" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_STRING + tensor_shape { + dim { + size: 3 + } + } + string_val: "v1" + string_val: "v2" + string_val: "v3" + } + } + } +} +node { + name: "v3" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 5 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "save/filename" + op: "PlaceholderWithDefault" + input: "save/filename/input" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "shape" + value { + shape { + } + } + } +} +node { + name: "save/Const" + op: "PlaceholderWithDefault" + input: "save/filename" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@save/control_dependency" + } + } + } + attr { + key: "dtype" + value { + type: DT_STRING + } + } + attr { + key: "shape" + value { + shape { + } + } + } +} +node { + name: "save/SaveV2" + op: "SaveV2" + input: "save/Const" + input: "save/SaveV2/tensor_names" + input: "save/SaveV2/shape_and_slices" + input: "v1" + input: "v2" + input: "v3" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtypes" + value { + list { + type: DT_FLOAT + type: DT_FLOAT + type: DT_FLOAT + } + } + } +} +node { + name: "save/control_dependency" + op: "Identity" + input: "save/Const" + input: "^save/SaveV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_STRING + } + } +} +library { +} +versions { + producer: 134 +} diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/om_test_mix_compile.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_mix_compile.pbtxt new file mode 100644 index 000000000..36d716b20 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_mix_compile.pbtxt @@ -0,0 +1,207 @@ +node { + name: "random_uniform/shape" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "1" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 1 + } + } + int_val: 5 + } + } + } +} +node { + name: "random_uniform/min" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_INT64 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT64 + tensor_shape { + } + int64_val: 0 + } + } + } +} +node { + name: "random_uniform/max" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_INT64 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT64 + tensor_shape { + } + int64_val: 5 + } + } + } +} +node { + name: "ArgMax/dimension" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + } + int_val: 0 + } + } + } +} +node { + name: "random_uniform" + op: "RandomUniformInt" + input: "random_uniform/shape" + input: "random_uniform/min" + input: "random_uniform/max" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT32 + } + } + attr { + key: "Tout" + value { + type: DT_INT64 + } + } + attr { + key: "seed" + value { + i: 0 + } + } + attr { + key: "seed2" + value { + i: 0 + } + } +} +node { + name: "Unique" + op: "Unique" + input: "random_uniform" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT64 + } + } + attr { + key: "_without_npu_compile" + value { + b: true + } + } + attr { + key: "out_idx" + value { + type: DT_INT32 + } + } +} +node { + name: "ArgMax" + op: "ArgMax" + input: "Unique" + input: "ArgMax/dimension" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT64 + } + } + attr { + key: "Tidx" + value { + type: DT_INT32 + } + } + attr { + key: "output_type" + value { + type: DT_INT64 + } + } +} +library { +} +versions { + producer: 134 +} diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/om_test_unaryops.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_unaryops.pbtxt new file mode 100644 index 000000000..67354ff9d --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/om_test_unaryops.pbtxt @@ -0,0 +1,158 @@ +node { + name: "random_uniform/shape" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_NpuOptimizer" + value { + s: "NpuOptimizer" + } + } + attr { + key: "_job" + value { + s: "localhost" + } + } + attr { + key: "_mix_compile_mode" + value { + s: "0" + } + } + attr { + key: "_use_off_line" + value { + s: "1" + } + } + attr { + key: "_iterations_per_loop" + value { + s: "1" + } + } + attr { + key: "dtype" + value { + type: DT_INT32 + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_INT32 + tensor_shape { + dim { + size: 1 + } + } + int_val: 5 + } + } + } +} +node { + name: "random_uniform/sub" + op: "Const" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "value" + value { + tensor { + dtype: DT_FLOAT + tensor_shape { + } + tensor_content: "\000\000\240@" + } + } + } +} +node { + name: "random_uniform/RandomUniform" + op: "RandomUniform" + input: "random_uniform/shape" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT32 + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "seed" + value { + i: 0 + } + } + attr { + key: "seed2" + value { + i: 0 + } + } +} +node { + name: "random_uniform/mul" + op: "Mul" + input: "random_uniform/RandomUniform" + input: "random_uniform/sub" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +node { + name: "Floor/unary_ops_composition" + op: "_UnaryOpsComposition" + input: "random_uniform/mul" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } + attr { + key: "op_names" + value { + list { + s: "Abs" + s: "Floor" + } + } + } +} +node { + name: "Abs_1" + op: "Abs" + input: "Floor/unary_ops_composition" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +library { +} +versions { + producer: 134 +} diff --git a/tf_adapter/tests/ut/optimizers/pbtxt/weight_update_shard_test_func.pbtxt b/tf_adapter/tests/ut/optimizers/pbtxt/weight_update_shard_test_func.pbtxt new file mode 100644 index 000000000..94acef6d7 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/pbtxt/weight_update_shard_test_func.pbtxt @@ -0,0 +1,178 @@ +node { + name: "apply_grads/DistributedAdamWeightDecayOptimizer_Broadcast_Weight_Update_Sharding/HcomBroadcast" + op: "HcomBroadcast" + input: "bert/embeddings/word_embeddings/read" + input: "^apply_grads/Assign" + input: "^apply_grads/apply_one_adam/AdamApplyOneWithDecayAssign" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + list { + type: DT_FLOAT + } + } + } + attr { + key: "FUSION" + value { + i: 0 + } + } + attr { + key: "fusion_id" + value { + i: -1 + } + } + attr { + key: "group" + value { + s: "hccl_world_group" + } + } + attr { + key: "root_rank" + value { + i: 0 + } + } +} +node { + name: "bert/embeddings/word_embeddings/read" + op: "Identity" + input: "bert/embeddings/word_embeddings" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type:DT_FLOAT + } + } + attr { + key: "_value_format" + value { + s: "4D" + } + } +} +node { + name: "bert/embeddings/word_embeddings" + op: "VariableV2" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "_class" + value { + list { + s: "loc:@bert/embeddings/word_embeddings/read" + } + } + } + attr { + key: "_value_format" + value { + s: "4D" + } + } + attr { + key: "container" + value { + s: "" + } + } + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 81216 + } + dim { + size: 8 + } + } + } + } + attr { + key: "shared_name" + value { + s: "" + } + } +} +node { + name: "cls/predictions/ArithmeticOptimizer/RecorderCastLikeAndValuePreserving_float_Cast_1" + op: "Cast" + input: "bert/embeddings/word_embeddings" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "DstT" + value { + type: DT_HALF + } + } + attr { + key: "SrcT" + value { + type: DT_FLOAT + } + } + attr { + key: "Truncate" + value { + b: false + } + } +} +node { + name: "apply_grads/Assign" + op: "Assign" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_INT64 + } + } + attr { + key: "_grappler_relax_allocator_constraints" + value { + b: true + } + } + attr { + key: "_var_format" + value { + s: "4D" + } + } + attr { + key: "use_locking" + value { + b: false + } + } + attr { + key: "validate_shape" + value { + b: true + } + } +} +node { + name: "apply_grads/apply_one_adam/AdamApplyOneWithDecayAssign" + op: "AdamApplyOneWithDecayAssign" + device: "/job:localhost/replica:0/task:0/device:CPU:0" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} \ No newline at end of file diff --git a/tf_adapter/tests/ut/optimizers/testcase/add_input_pass_test.cc b/tf_adapter/tests/ut/optimizers/testcase/add_input_pass_test.cc new file mode 100644 index 000000000..7bc459c3e --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/testcase/add_input_pass_test.cc @@ -0,0 +1,139 @@ +#include "gtest/gtest.h" +#include "mmpa/mmpa_api.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/graph/graph_constructor.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/public/session_options.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" +#include + +namespace tensorflow { +class AddInputPass : public GraphOptimizationPass { + public: + AddInputPass() = default; + ~AddInputPass() override = default; + Status Run(const GraphOptimizationPassOptions &options) override; +}; + +namespace { +class AddInputPassTest : public testing::Test { + public: + AddInputPassTest() : graph_(absl::make_unique(OpRegistry::Global())) {} + static void InitGraph(const string &graph_def_path, Graph *graph) { + GraphDef graph_def; + ReadTextProto(Env::Default(), graph_def_path, &graph_def); + GraphConstructorOptions opts; + TF_CHECK_OK(ConvertGraphDefToGraph(opts, graph_def, graph)); + } + + void InitGraph(const string &graph_def_path) { + char trusted_path[MMPA_MAX_PATH] = { "\0" }; + if (mmRealPath(graph_def_path.c_str(), trusted_path, MMPA_MAX_PATH) != EN_OK) { + LOG(ERROR) << "Get real path failed."; + return; + } + LOG(INFO) << "input graph def path: " << trusted_path; + InitGraph(trusted_path, graph_.get()); + original_ = CanonicalGraphString(graph_.get()); + } + + static bool IncludeNode(const Node *n) { return n->IsOp(); } + + static string EdgeId(const Node* n, int index) { + if (index == 0) { + return n->type_string(); + } else if (index == Graph::kControlSlot) { + return strings::StrCat(n->type_string(), ":control"); + } else { + return strings::StrCat(n->type_string(), ":", index); + } + } + + string CanonicalGraphString(Graph* g) { + for (Node* n : g->nodes()) { + if (IncludeNode(n)) { + if (n->assigned_device_name().empty()) { + n->set_assigned_device_name("/job:ps/replica:0/task:0/device:CPU:0"); + break; + } + } + } + + std::vector edges; + for (const Edge* e : g->edges()) { + if (IncludeNode(e->src()) && IncludeNode(e->dst())) { + edges.push_back(strings::StrCat(EdgeId(e->src(), e->src_output()), "->", + EdgeId(e->dst(), e->dst_input()))); + } + } + // Canonicalize + return strings::StrCat(absl::StrJoin(edges, ";")); + } + + string DoRunAddInputPassTest(SessionOptions &session_options) { + string before = CanonicalGraphString(graph_.get()); + LOG(INFO) << "Before om conversion pass: " << before; + + FunctionDefLibrary flib; + GraphOptimizationPassOptions options; + options.session_options = &session_options; + Graph graph(OpRegistry::Global()); + std::unique_ptr flib_def( + new FunctionLibraryDefinition(graph.op_registry(), flib)); + options.flib_def = flib_def.get(); + std::unordered_map> partition_graphs; + partition_graphs.emplace("cpu", std::move(graph_)); + options.partition_graphs = &partition_graphs; + AddInputPass().Run(options); + + string result = ""; + LOG(INFO) << "After om conversion pass: " << result; + return result; + } + + const string &OriginalGraph() const { return original_; } + + std::unique_ptr graph_; + string original_; + protected: + virtual void SetUp() {} + virtual void TearDown() {} +}; + +TEST_F(AddInputPassTest, FuncTest) { + string org_graph_def_path = "tf_adapter/tests/ut//optimizers/pbtxt/add_input_pass_worker_test.pbtxt"; + InitGraph(org_graph_def_path); + SessionOptions session_options; + session_options.config.mutable_graph_options() + ->mutable_optimizer_options() + ->set_do_function_inlining(true); + auto *custom_config = session_options.config.mutable_graph_options()->mutable_rewrite_options()->add_custom_optimizers(); + custom_config->set_name("NpuOptimizer"); + EXPECT_EQ(DoRunAddInputPassTest(session_options), ""); +} +TEST_F(AddInputPassTest, SkipFuncTest) { + string org_graph_def_path = "tf_adapter/tests/ut//optimizers/pbtxt/add_input_pass_no_need_optimize_test.pbtxt"; + InitGraph(org_graph_def_path); + SessionOptions session_options; + session_options.config.mutable_graph_options() + ->mutable_optimizer_options() + ->set_do_function_inlining(true); + auto *custom_config = session_options.config.mutable_graph_options()->mutable_rewrite_options()->add_custom_optimizers(); + custom_config->set_name("NpuOptimizer"); + EXPECT_EQ(DoRunAddInputPassTest(session_options), ""); +} +TEST_F(AddInputPassTest, SkipFunc1Test) { + string org_graph_def_path = "tf_adapter/tests/ut//optimizers/pbtxt/add_input_pass_localhost_test.pbtxt"; + InitGraph(org_graph_def_path); + SessionOptions session_options; + session_options.config.mutable_graph_options() + ->mutable_optimizer_options() + ->set_do_function_inlining(true); + auto *custom_config = session_options.config.mutable_graph_options()->mutable_rewrite_options()->add_custom_optimizers(); + custom_config->set_name("NpuOptimizer"); + EXPECT_EQ(DoRunAddInputPassTest(session_options), ""); +} +} // end namespace +} // end tensorflow diff --git a/tf_adapter/tests/ut/optimizers/testcase/dp_tf_ge_conversion_pass_test.cc b/tf_adapter/tests/ut/optimizers/testcase/dp_tf_ge_conversion_pass_test.cc new file mode 100644 index 000000000..1d0bdb23f --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/testcase/dp_tf_ge_conversion_pass_test.cc @@ -0,0 +1,102 @@ +#include "tf_adapter/optimizers/dp_tf_ge_conversion_pass.h" +#include "gtest/gtest.h" +#include "mmpa/mmpa_api.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/graph/graph_constructor.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/logging.h" + +namespace tensorflow { +namespace { +class DpOptimizationPassTest : public testing::Test { + public: + DpOptimizationPassTest() : graph_(absl::make_unique(OpRegistry::Global())) {} + static void InitGraph(const string &graph_def_path, Graph *graph) { + GraphDef graph_def; + ReadTextProto(Env::Default(), graph_def_path, &graph_def); + GraphConstructorOptions opts; + TF_CHECK_OK(ConvertGraphDefToGraph(opts, graph_def, graph)); + } + + void InitGraph(const string &graph_def_path) { + char trusted_path[MMPA_MAX_PATH] = { "\0" }; + if (mmRealPath(graph_def_path.c_str(), trusted_path, MMPA_MAX_PATH) != EN_OK) { + LOG(ERROR) << "Get real path failed."; + return; + } + LOG(INFO) << "input graph def path: " << trusted_path; + InitGraph(trusted_path, graph_.get()); + original_ = CanonicalGraphString(graph_.get()); + } + + static bool IncludeNode(const Node *n) { return n->IsOp(); } + + static string EdgeId(const Node* n, int index) { + if (index == 0) { + return n->type_string(); + } else if (index == Graph::kControlSlot) { + return strings::StrCat(n->type_string(), ":control"); + } else { + return strings::StrCat(n->type_string(), ":", index); + } + } + + string CanonicalGraphString(Graph* g) { + std::vector edges; + for (const Edge* e : g->edges()) { + if (IncludeNode(e->src()) && IncludeNode(e->dst())) { + edges.push_back(strings::StrCat(EdgeId(e->src(), e->src_output()), "->", + EdgeId(e->dst(), e->dst_input()))); + } + } + // Canonicalize + return strings::StrCat(absl::StrJoin(edges, ";")); + } + + string DoRunDpOptimizationPassTest() { + string before = CanonicalGraphString(graph_.get()); + LOG(INFO) << "Before om conversion pass: " << before; + + FunctionDefLibrary flib; + std::unique_ptr *ug = &graph_; + GraphOptimizationPassOptions options; + options.graph = ug; + std::unique_ptr flib_def( + new FunctionLibraryDefinition((*options.graph)->op_registry(), flib)); + options.flib_def = flib_def.get(); + DpTfToGEConversionPass().Run(options); + + string result = CanonicalGraphString(options.graph->get()); + LOG(INFO) << "After om conversion pass: " << result; + return result; + } + + const string &OriginalGraph() const { return original_; } + + std::unique_ptr graph_; + string original_; + protected: + virtual void SetUp() {} + virtual void TearDown() {} +}; + +TEST_F(DpOptimizationPassTest, BuildGeOpTest) { + string org_graph_def_path = "tf_adapter/tests/ut//optimizers/pbtxt/dp_test_build_geop.pbtxt"; + InitGraph(org_graph_def_path); + std::string target_graph = "Const->TensorSliceDataset;IteratorV2->MakeIterator:1;"\ + "TensorSliceDataset->HostQueueDataset:1;HostQueueDataset->DPGroupDataset;GEOPDataset->HostQueueDataset;"\ + "DPGroupDataset->MakeIterator"; + EXPECT_EQ(DoRunDpOptimizationPassTest(), target_graph); +} +TEST_F(DpOptimizationPassTest, DatasetNotInDeviceTest) { + string org_graph_def_path = "tf_adapter/tests/ut//optimizers/pbtxt/dp_test_no_dataset_in_device.pbtxt"; + InitGraph(org_graph_def_path); + std::string target_graph = "Const->TensorSliceDataset;TensorSliceDataset->BatchDatasetV2;Const->BatchDatasetV2:1;"\ + "Const->BatchDatasetV2:2;BatchDatasetV2->RepeatDataset;Const->RepeatDataset:1;RepeatDataset->OptimizeDataset;"\ + "Const->OptimizeDataset:1;OptimizeDataset->ModelDataset;IteratorV2->MakeIterator:1;ModelDataset->HostQueueDataset:1;"\ + "HostQueueDataset->DPGroupDataset;GEOPDataset->HostQueueDataset;DPGroupDataset->MakeIterator"; + EXPECT_EQ(DoRunDpOptimizationPassTest(), target_graph); +} +} // end namespace +} // end tensorflow diff --git a/tf_adapter/tests/ut/optimizers/testcase/get_attr_optimize_pass_test.cc b/tf_adapter/tests/ut/optimizers/testcase/get_attr_optimize_pass_test.cc new file mode 100644 index 000000000..876a417fc --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/testcase/get_attr_optimize_pass_test.cc @@ -0,0 +1,208 @@ +#include "gtest/gtest.h" +#include "mmpa/mmpa_api.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/graph/graph_constructor.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/public/session_options.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" + +namespace tensorflow { +class GetAttrOptimizePass : public GraphOptimizationPass { + public: + GetAttrOptimizePass() = default; + ~GetAttrOptimizePass() override = default; + Status Run(const GraphOptimizationPassOptions &options) override; +}; + +namespace { +class GetAttrOptimizationPassTest : public testing::Test { + public: + GetAttrOptimizationPassTest() : graph_(absl::make_unique(OpRegistry::Global())) {} + static void InitGraph(const string &graph_def_path, Graph *graph) { + GraphDef graph_def; + ReadTextProto(Env::Default(), graph_def_path, &graph_def); + GraphConstructorOptions opts; + TF_CHECK_OK(ConvertGraphDefToGraph(opts, graph_def, graph)); + } + + void InitGraph(const string &graph_def_path) { + char trusted_path[MMPA_MAX_PATH] = { "\0" }; + if (mmRealPath(graph_def_path.c_str(), trusted_path, MMPA_MAX_PATH) != EN_OK) { + LOG(ERROR) << "Get real path failed."; + return; + } + LOG(INFO) << "input graph def path: " << trusted_path; + InitGraph(trusted_path, graph_.get()); + original_ = CanonicalGraphString(graph_.get()); + } + + static bool IncludeNode(const Node *n) { return n->IsOp(); } + + static string EdgeId(const Node* n, int index) { + if (index == 0) { + return n->type_string(); + } else if (index == Graph::kControlSlot) { + return strings::StrCat(n->type_string(), ":control"); + } else { + return strings::StrCat(n->type_string(), ":", index); + } + } + + string CanonicalGraphString(Graph* g) { + for (Node* n : g->nodes()) { + if (IncludeNode(n)) { + if (n->assigned_device_name().empty()) { + n->set_assigned_device_name("/job:localhost/replica:0/task:0/device:CPU:0"); + break; + } + } + } + + std::vector edges; + for (const Edge* e : g->edges()) { + if (IncludeNode(e->src()) && IncludeNode(e->dst())) { + edges.push_back(strings::StrCat(EdgeId(e->src(), e->src_output()), "->", + EdgeId(e->dst(), e->dst_input()))); + } + } + // Canonicalize + return strings::StrCat(absl::StrJoin(edges, ";")); + } + + string DoRunGetAttrOptimizationPassTest(bool is_set_attrs = true) { + string before = CanonicalGraphString(graph_.get()); + LOG(INFO) << "Before om conversion pass: " << before; + + FunctionDefLibrary flib; + std::unique_ptr *ug = &graph_; + GraphOptimizationPassOptions options; + SessionOptions session_options; + session_options.config.mutable_graph_options() + ->mutable_optimizer_options() + ->set_do_function_inlining(true); + auto *custom_config = session_options.config.mutable_graph_options()->mutable_rewrite_options()->add_custom_optimizers(); + custom_config->set_name("NpuOptimizer"); + AttrValue job = AttrValue(); + job.set_s("chief"); + (*custom_config->mutable_parameter_map())["job"] = job; + if (is_set_attrs) { + AttrValue enable_data_pre_proc = AttrValue(); + enable_data_pre_proc.set_b(true); + (*custom_config->mutable_parameter_map())["enable_data_pre_proc"] = enable_data_pre_proc; + AttrValue dynamic_input = AttrValue(); + dynamic_input.set_b(true); + (*custom_config->mutable_parameter_map())["dynamic_input"] = dynamic_input; + AttrValue dynamic_graph_execute_mode = AttrValue(); + dynamic_graph_execute_mode.set_s("lazy_recompile"); + (*custom_config->mutable_parameter_map())["dynamic_graph_execute_mode"] = dynamic_graph_execute_mode; + AttrValue local_rank_id = AttrValue(); + local_rank_id.set_i(0); + (*custom_config->mutable_parameter_map())["local_rank_id"] = local_rank_id; + AttrValue local_device_list = AttrValue(); + local_device_list.set_s("0,1"); + (*custom_config->mutable_parameter_map())["local_device_list"] = local_device_list; + AttrValue enable_dump = AttrValue(); + enable_dump.set_b(true); + (*custom_config->mutable_parameter_map())["enable_dump"] = enable_dump; + AttrValue dump_path = AttrValue(); + dump_path.set_s("./"); + (*custom_config->mutable_parameter_map())["dump_path"] = dump_path; + AttrValue dump_step = AttrValue(); + dump_step.set_s("1"); + (*custom_config->mutable_parameter_map())["dump_step"] = dump_step; + AttrValue dump_mode = AttrValue(); + dump_mode.set_s("all"); + (*custom_config->mutable_parameter_map())["dump_mode"] = dump_mode; + AttrValue enable_dump_debug = AttrValue(); + enable_dump_debug.set_b(true); + (*custom_config->mutable_parameter_map())["enable_dump_debug"] = enable_dump_debug; + AttrValue dump_debug_mode = AttrValue(); + dump_debug_mode.set_s("all"); + (*custom_config->mutable_parameter_map())["dump_debug_mode"] = dump_debug_mode; + AttrValue profiling_mode = AttrValue(); + profiling_mode.set_b(true); + (*custom_config->mutable_parameter_map())["profiling_mode"] = profiling_mode; + AttrValue profiling_options = AttrValue(); + profiling_options.set_s("1"); + (*custom_config->mutable_parameter_map())["profiling_options"] = profiling_options; + AttrValue graph_run_mode = AttrValue(); + graph_run_mode.set_i(1); + (*custom_config->mutable_parameter_map())["graph_run_mode"] = graph_run_mode; + AttrValue mstune_mode = AttrValue(); + mstune_mode.set_s("2"); + (*custom_config->mutable_parameter_map())["mstune_mode"] = mstune_mode; + AttrValue op_tune_mode = AttrValue(); + op_tune_mode.set_s("GA"); + (*custom_config->mutable_parameter_map())["op_tune_mode"] = op_tune_mode; + AttrValue work_path = AttrValue(); + work_path.set_s("./"); + (*custom_config->mutable_parameter_map())["work_path"] = work_path; + AttrValue input_shape = AttrValue(); + input_shape.set_s("data:1,1,40,-1;lable:1,-1;mask:-1,-1"); + (*custom_config->mutable_parameter_map())["input_shape"] = input_shape; + AttrValue dynamic_dims = AttrValue(); + dynamic_dims.set_s("20,20,1,1;40,40,2,2;80,60,4,4"); + (*custom_config->mutable_parameter_map())["dynamic_dims"] = dynamic_dims; + AttrValue dynamic_node_type = AttrValue(); + dynamic_node_type.set_s("1"); + (*custom_config->mutable_parameter_map())["dynamic_node_type"] = dynamic_node_type; + AttrValue buffer_optimize = AttrValue(); + buffer_optimize.set_s("l2_optimize"); + (*custom_config->mutable_parameter_map())["buffer_optimize"] = buffer_optimize; + AttrValue op_select_implmode = AttrValue(); + op_select_implmode.set_s("high_performance"); + (*custom_config->mutable_parameter_map())["op_select_implmode"] = op_select_implmode; + AttrValue optypelist_for_implmode = AttrValue(); + optypelist_for_implmode.set_s("Add"); + (*custom_config->mutable_parameter_map())["optypelist_for_implmode"] = optypelist_for_implmode; + AttrValue op_compiler_cache_mode = AttrValue(); + op_compiler_cache_mode.set_s("Add"); + (*custom_config->mutable_parameter_map())["op_compiler_cache_mode"] = op_compiler_cache_mode; + AttrValue op_compiler_cache_dir = AttrValue(); + op_compiler_cache_dir.set_s("./"); + (*custom_config->mutable_parameter_map())["op_compiler_cache_dir"] = op_compiler_cache_dir; + AttrValue debug_dir = AttrValue(); + debug_dir.set_s("./"); + (*custom_config->mutable_parameter_map())["debug_dir"] = debug_dir; + AttrValue session_device_id = AttrValue(); + session_device_id.set_i(1); + (*custom_config->mutable_parameter_map())["session_device_id"] = session_device_id; + } + + options.session_options = &session_options; + options.graph = ug; + std::unique_ptr flib_def( + new FunctionLibraryDefinition((*options.graph)->op_registry(), flib)); + options.flib_def = flib_def.get(); + GetAttrOptimizePass().Run(options); + + string result = CanonicalGraphString(options.graph->get()); + LOG(INFO) << "After om conversion pass: " << result; + return result; + } + + const string &OriginalGraph() const { return original_; } + + std::unique_ptr graph_; + string original_; + protected: + virtual void SetUp() {} + virtual void TearDown() {} +}; + +TEST_F(GetAttrOptimizationPassTest, SetAttrTest) { + string org_graph_def_path = "tf_adapter/tests/ut/optimizers/pbtxt/get_attr_job_chief_test.pbtxt"; + InitGraph(org_graph_def_path); + std::string target_graph = "VariableV2->Identity;Const->Add;Identity->Add:1;Add->_Retval"; + EXPECT_EQ(DoRunGetAttrOptimizationPassTest(), target_graph); +} +TEST_F(GetAttrOptimizationPassTest, NotSetAttrTest) { + string org_graph_def_path = "tf_adapter/tests/ut/optimizers/pbtxt/get_attr_job_chief_test.pbtxt"; + InitGraph(org_graph_def_path); + std::string target_graph = "VariableV2->Identity;Const->Add;Identity->Add:1;Add->_Retval"; + EXPECT_EQ(DoRunGetAttrOptimizationPassTest(false), target_graph); +} +} // end namespace +} // end tensorflow diff --git a/tf_adapter/tests/ut/optimizers/testcase/gradient_fusion_optimizer_test.cc b/tf_adapter/tests/ut/optimizers/testcase/gradient_fusion_optimizer_test.cc new file mode 100644 index 000000000..dc276ef43 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/testcase/gradient_fusion_optimizer_test.cc @@ -0,0 +1,20 @@ +#include "gtest/gtest.h" +#include "tf_adapter/optimizers/gradient_fusion_optimizer.h" +#include "tensorflow/core/grappler/grappler_item.h" + +namespace tensorflow { +namespace grappler { +class GradientFusionOptimizerTest : public testing::Test { +protected: + void SetUp() {} + void TearDown() {} +}; +TEST_F(GradientFusionOptimizerTest, RunOptimizer) { + GrapplerItem item; + item.graph = GraphDef(); + GraphDef output; + const Status status = GradFusionOptimizer().Optimize(nullptr, item, &output); + EXPECT_EQ(status, Status::OK()); +} +} // end grappler +} // end tensorflow \ No newline at end of file diff --git a/tf_adapter/tests/ut/optimizers/testcase/mark_and_ctrl_conversion_pass_test.cc b/tf_adapter/tests/ut/optimizers/testcase/mark_and_ctrl_conversion_pass_test.cc new file mode 100644 index 000000000..3cb5c56de --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/testcase/mark_and_ctrl_conversion_pass_test.cc @@ -0,0 +1,116 @@ +#include "tf_adapter/optimizers/control_flow_conversion_pass.h" +#include "gtest/gtest.h" +#include "mmpa/mmpa_api.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/graph/graph_constructor.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/public/session_options.h" + +namespace tensorflow { +class MarkNoNeedOptimizePass : public GraphOptimizationPass { + public: + MarkNoNeedOptimizePass() = default; + ~MarkNoNeedOptimizePass() override = default; + Status Run(const GraphOptimizationPassOptions &options) override; + + private: + Status ProcessGraph(std::unique_ptr *graph, FunctionLibraryDefinition *func_lib, + const OptimizationPassRegistry::Grouping pass_group_value); +}; + +namespace { +class MarkAndCtrlOptimizationPassTest : public testing::Test { + public: + MarkAndCtrlOptimizationPassTest() : graph_(absl::make_unique(OpRegistry::Global())) {} + static void InitGraph(const string &graph_def_path, Graph *graph) { + GraphDef graph_def; + ReadTextProto(Env::Default(), graph_def_path, &graph_def); + GraphConstructorOptions opts; + TF_CHECK_OK(ConvertGraphDefToGraph(opts, graph_def, graph)); + } + + void InitGraph(const string &graph_def_path) { + char trusted_path[MMPA_MAX_PATH] = { "\0" }; + if (mmRealPath(graph_def_path.c_str(), trusted_path, MMPA_MAX_PATH) != EN_OK) { + LOG(ERROR) << "Get real path failed."; + return; + } + LOG(INFO) << "input graph def path: " << trusted_path; + InitGraph(trusted_path, graph_.get()); + original_ = CanonicalGraphString(graph_.get()); + } + + static bool IncludeNode(const Node *n) { return n->IsOp(); } + + static string EdgeId(const Node* n, int index) { + if (index == 0) { + return n->type_string(); + } else if (index == Graph::kControlSlot) { + return strings::StrCat(n->type_string(), ":control"); + } else { + return strings::StrCat(n->type_string(), ":", index); + } + } + + string CanonicalGraphString(Graph* g) { + std::vector edges; + for (const Edge* e : g->edges()) { + if (IncludeNode(e->src()) && IncludeNode(e->dst())) { + edges.push_back(strings::StrCat(EdgeId(e->src(), e->src_output()), "->", + EdgeId(e->dst(), e->dst_input()))); + } + } + // Canonicalize + return strings::StrCat(absl::StrJoin(edges, ";")); + } + + string DoRunMarkAndCtrlOptimizationPassTest() { + string before = CanonicalGraphString(graph_.get()); + LOG(INFO) << "Before om conversion pass: " << before; + + std::unique_ptr *ug = &graph_; + GraphOptimizationPassOptions options; + SessionOptions session_options; + session_options.config.mutable_graph_options() + ->mutable_optimizer_options() + ->set_do_function_inlining(true); + auto *custom_config = session_options.config.mutable_graph_options()->mutable_rewrite_options()->add_custom_optimizers(); + custom_config->set_name("NpuOptimizer"); + AttrValue job = AttrValue(); + job.set_s("localhost"); + (*custom_config->mutable_parameter_map())["job"] = job; + options.session_options = &session_options; + options.graph = ug; + GraphDef graph_def; + options.graph->get()->ToGraphDef(&graph_def); + FunctionDefLibrary func_def_lib = graph_def.library(); + std::unique_ptr flib_def( + new FunctionLibraryDefinition((*options.graph)->op_registry(), func_def_lib)); + options.flib_def = flib_def.get(); + MarkNoNeedOptimizePass().Run(options); + ControlFlowConversionPass().Run(options); + + string result = CanonicalGraphString(options.graph->get()); + LOG(INFO) << "After om conversion pass: " << result; + return result; + } + + const string &OriginalGraph() const { return original_; } + + std::unique_ptr graph_; + string original_; + protected: + virtual void SetUp() {} + virtual void TearDown() {} +}; + +TEST_F(MarkAndCtrlOptimizationPassTest, FuncTest) { + string org_graph_def_path = "tf_adapter/tests/ut//optimizers/pbtxt/ctrl_if_test.pbtxt"; + InitGraph(org_graph_def_path); + std::string target_graph = "Placeholder->Greater;Const->Greater:1;Greater->If;Placeholder->If:1"; + EXPECT_EQ(DoRunMarkAndCtrlOptimizationPassTest(), target_graph); +} +} // end namespace +} // end tensorflow diff --git a/tf_adapter/tests/ut/optimizers/testcase/mark_start_node_pass_test.cc b/tf_adapter/tests/ut/optimizers/testcase/mark_start_node_pass_test.cc new file mode 100644 index 000000000..0e9a65ef0 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/testcase/mark_start_node_pass_test.cc @@ -0,0 +1,122 @@ +#include "gtest/gtest.h" +#include "mmpa/mmpa_api.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/graph/graph_constructor.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/public/session_options.h" +#include "tensorflow/core/common_runtime/optimization_registry.h" + +namespace tensorflow { +class MarkStartNodePass : public GraphOptimizationPass { + public: + MarkStartNodePass() = default; + ~MarkStartNodePass() override = default; + Status Run(const GraphOptimizationPassOptions &options) override; + Status TraverseNode(Node *start_node); +}; + +namespace { +class MarkStartNodePassTest : public testing::Test { + public: + MarkStartNodePassTest() : graph_(absl::make_unique(OpRegistry::Global())) {} + static void InitGraph(const string &graph_def_path, Graph *graph) { + GraphDef graph_def; + ReadTextProto(Env::Default(), graph_def_path, &graph_def); + GraphConstructorOptions opts; + TF_CHECK_OK(ConvertGraphDefToGraph(opts, graph_def, graph)); + } + + void InitGraph(const string &graph_def_path) { + char trusted_path[MMPA_MAX_PATH] = { "\0" }; + if (mmRealPath(graph_def_path.c_str(), trusted_path, MMPA_MAX_PATH) != EN_OK) { + LOG(ERROR) << "Get real path failed."; + return; + } + LOG(INFO) << "input graph def path: " << trusted_path; + InitGraph(trusted_path, graph_.get()); + original_ = CanonicalGraphString(graph_.get()); + } + + static bool IncludeNode(const Node *n) { return n->IsOp(); } + + static string EdgeId(const Node* n, int index) { + if (index == 0) { + return n->type_string(); + } else if (index == Graph::kControlSlot) { + return strings::StrCat(n->type_string(), ":control"); + } else { + return strings::StrCat(n->type_string(), ":", index); + } + } + + string CanonicalGraphString(Graph* g) { + for (Node* n : g->nodes()) { + if (IncludeNode(n)) { + if (n->type_string() == "Add" && n->assigned_device_name().empty()) { + n->set_assigned_device_name("/job:ps/replica:0/task:0/device:CPU:0"); + break; + } + if (n->assigned_device_name().empty()) { + n->set_assigned_device_name("/job:localhost/replica:0/task:0/device:CPU:0"); + } + } + } + + std::vector edges; + for (const Edge* e : g->edges()) { + if (IncludeNode(e->src()) && IncludeNode(e->dst())) { + edges.push_back(strings::StrCat(EdgeId(e->src(), e->src_output()), "->", + EdgeId(e->dst(), e->dst_input()))); + } + } + // Canonicalize + return strings::StrCat(absl::StrJoin(edges, ";")); + } + + string DoRunMarkStartNodePassTest() { + string before = CanonicalGraphString(graph_.get()); + LOG(INFO) << "Before om conversion pass: " << before; + + FunctionDefLibrary flib; + std::unique_ptr *ug = &graph_; + GraphOptimizationPassOptions options; + SessionOptions session_options; + session_options.config.mutable_graph_options() + ->mutable_optimizer_options() + ->set_do_function_inlining(true); + auto *custom_config = session_options.config.mutable_graph_options()->mutable_rewrite_options()->add_custom_optimizers(); + custom_config->set_name("NpuOptimizer"); + AttrValue job = AttrValue(); + job.set_s("chief"); + (*custom_config->mutable_parameter_map())["job"] = job; + options.session_options = &session_options; + options.graph = ug; + std::unique_ptr flib_def( + new FunctionLibraryDefinition((*options.graph)->op_registry(), flib)); + options.flib_def = flib_def.get(); + MarkStartNodePass().Run(options); + + string result = CanonicalGraphString(options.graph->get()); + LOG(INFO) << "After om conversion pass: " << result; + return result; + } + + const string &OriginalGraph() const { return original_; } + + std::unique_ptr graph_; + string original_; + protected: + virtual void SetUp() {} + virtual void TearDown() {} +}; + +TEST_F(MarkStartNodePassTest, FuncTest) { + string org_graph_def_path = "tf_adapter/tests/ut//optimizers/pbtxt/om_test_build_geop.pbtxt"; + InitGraph(org_graph_def_path); + std::string target_graph = "VariableV2->Identity;Const->Add;Identity->Add:1;Add->_Retval"; + EXPECT_EQ(DoRunMarkStartNodePassTest(), target_graph); +} +} // end namespace +} // end tensorflow diff --git a/tf_adapter/tests/ut/optimizers/testcase/om_partition_subgraphs_pass_test.cc b/tf_adapter/tests/ut/optimizers/testcase/om_partition_subgraphs_pass_test.cc new file mode 100644 index 000000000..137c3f15c --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/testcase/om_partition_subgraphs_pass_test.cc @@ -0,0 +1,189 @@ +#include "tf_adapter/optimizers/om_partition_subgraphs_pass.h" +#include "tf_adapter/util/infershape_util.h" +#include "tf_adapter/util/npu_attrs.h" +#include "gtest/gtest.h" +#include "mmpa/mmpa_api.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/graph/graph_constructor.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/logging.h" + +namespace tensorflow { +namespace { +class OmOptimizationPassTest : public testing::Test { + public: + OmOptimizationPassTest() : graph_(absl::make_unique(OpRegistry::Global())) {} + static void InitGraph(const string &graph_def_path, Graph *graph) { + GraphDef graph_def; + ReadTextProto(Env::Default(), graph_def_path, &graph_def); + GraphConstructorOptions opts; + TF_CHECK_OK(ConvertGraphDefToGraph(opts, graph_def, graph)); + } + + void InitGraph(const string &graph_def_path) { + char trusted_path[MMPA_MAX_PATH] = { "\0" }; + if (mmRealPath(graph_def_path.c_str(), trusted_path, MMPA_MAX_PATH) != EN_OK) { + LOG(ERROR) << "Get real path failed."; + return; + } + LOG(INFO) << "input graph def path: " << trusted_path; + InitGraph(trusted_path, graph_.get()); + original_ = CanonicalGraphString(graph_.get()); + } + + static bool IncludeNode(const Node *n) { return n->IsOp(); } + + static string EdgeId(const Node* n, int index) { + if (index == 0) { + return n->name(); + } else if (index == Graph::kControlSlot) { + return strings::StrCat(n->name(), ":control"); + } else { + return strings::StrCat(n->name(), ":", index); + } + } + + string CanonicalGraphString(Graph* g) { + std::vector edges; + for (const Edge* e : g->edges()) { + if (IncludeNode(e->src()) && IncludeNode(e->dst())) { + edges.push_back(strings::StrCat(EdgeId(e->src(), e->src_output()), "->", + EdgeId(e->dst(), e->dst_input()))); + } + } + return strings::StrCat(absl::StrJoin(edges, ";")); + } + + void ConvertFuncDefToGraph(const FunctionDef &func_def, Graph *g) { + Graph graph(OpRegistry::Global()); + FunctionDefLibrary flib; + FunctionLibraryDefinition flib_def(graph.op_registry(), flib); + (void)InferShapeUtil::GetSubGraphFromFunctionDef(flib_def, func_def, g); + } + + string DoRunOmOptimizationPassTest(bool is_convert_func_def = false) { + string before = CanonicalGraphString(graph_.get()); + LOG(INFO) << "Before om conversion pass: " << before; + + FunctionDefLibrary flib; + std::unique_ptr *ug = &graph_; + GraphOptimizationPassOptions options; + options.graph = ug; + std::unique_ptr flib_def( + new FunctionLibraryDefinition((*options.graph)->op_registry(), flib)); + options.flib_def = flib_def.get(); + OMPartitionSubgraphsPass().Run(options); + string result; + if (is_convert_func_def) { + GraphDef graph_def; + options.graph->get()->ToGraphDef(&graph_def); + FunctionDefLibrary func_def_lib = graph_def.library(); + for (auto func_def : func_def_lib.function()) { + Graph ge_graph(OpRegistry::Global()); + ConvertFuncDefToGraph(func_def, &ge_graph); + result += CanonicalGraphString(&ge_graph) + "|"; + } + + } else { + result = CanonicalGraphString(options.graph->get()); + } + + LOG(INFO) << "After om conversion pass: " << result; + return result; + } + + const string &OriginalGraph() const { return original_; } + + std::unique_ptr graph_; + string original_; + protected: + virtual void SetUp() {} + virtual void TearDown() {} +}; + +TEST_F(OmOptimizationPassTest, BuildGeOpTest) { + string org_graph_def_path = "tf_adapter/tests/ut/optimizers/pbtxt/om_test_build_geop.pbtxt"; + InitGraph(org_graph_def_path); + std::string target_graph = "GeOp1_0->retval_Add_0_0"; + EXPECT_EQ(DoRunOmOptimizationPassTest(), target_graph); +} +TEST_F(OmOptimizationPassTest, AccumulateTest) { + string org_graph_def_path = "tf_adapter/tests/ut/optimizers/pbtxt/om_test_accumulate.pbtxt"; + InitGraph(org_graph_def_path); + std::string result = DoRunOmOptimizationPassTest(true); + EXPECT_EQ(result.find("AccumulateNV2") != result.npos, true); +} +TEST_F(OmOptimizationPassTest, MixCompileCopyVarsTest) { + string org_graph_def_path = "tf_adapter/tests/ut/optimizers/pbtxt/om_test_copy_variables.pbtxt"; + InitGraph(org_graph_def_path); + std::string result = DoRunOmOptimizationPassTest(true); + std::vector result_graphs; + Split(result, result_graphs, "|"); + bool has_variables = false; + for (auto result_graph : result_graphs) { + if (result_graph.find("Variable_1->") != result_graph.npos) { + has_variables = true; + } else { + has_variables = false; + } + } + EXPECT_EQ(has_variables, true); +} +TEST_F(OmOptimizationPassTest, BuildGetNextGeOpTest) { + string org_graph_def_path = "tf_adapter/tests/ut/optimizers/pbtxt/om_test_getnext_build_geop.pbtxt"; + InitGraph(org_graph_def_path); + std::string target_graph = DoRunOmOptimizationPassTest(true); + EXPECT_EQ(target_graph.find("IteratorGetNext") != target_graph.npos, true); +} +TEST_F(OmOptimizationPassTest, DpGraphTest) { + string org_graph_def_path = "tf_adapter/tests/ut/optimizers/pbtxt/om_test_iterator_build_geop.pbtxt"; + InitGraph(org_graph_def_path); + std::string target_graph = "normalize_element/component_0->TensorSliceDataset;TensorSliceDataset->OptimizeDataset;optimizations->OptimizeDataset:1;OptimizeDataset->ModelDataset;ModelDataset->MakeIterator;IteratorV2->MakeIterator:1"; + EXPECT_EQ(DoRunOmOptimizationPassTest(), target_graph); +} +TEST_F(OmOptimizationPassTest, MergeClustersTest) { + string org_graph_def_path = "tf_adapter/tests/ut/optimizers/pbtxt/om_test_merge_clusters.pbtxt"; + InitGraph(org_graph_def_path); + std::string target_graph = DoRunOmOptimizationPassTest(true); + bool ret = false; + if (target_graph.find("v1->") != target_graph.npos && target_graph.find("v2->") != target_graph.npos + && target_graph.find("v3->") != target_graph.npos) { ret = true; } + EXPECT_EQ(ret, true); +} +TEST_F(OmOptimizationPassTest, MixCompileTest) { + string org_graph_def_path = "tf_adapter/tests/ut/optimizers/pbtxt/om_test_mix_compile.pbtxt"; + InitGraph(org_graph_def_path); + std::string target_graph = "GeOp7_0->Unique;Unique->GeOp7_1"; + EXPECT_EQ(DoRunOmOptimizationPassTest(), target_graph); +} +TEST_F(OmOptimizationPassTest, UnaryOpsTest) { + string org_graph_def_path = "tf_adapter/tests/ut/optimizers/pbtxt/om_test_unaryops.pbtxt"; + InitGraph(org_graph_def_path); + std::string target_graph = DoRunOmOptimizationPassTest(true); + bool ret = false; + if (target_graph.find("_Floor") != target_graph.npos && target_graph.find("_Abs") != target_graph.npos) { + ret = true; + } + EXPECT_EQ(ret, true); +} +TEST_F(OmOptimizationPassTest, InOutPairFlagFalseTest) { + string org_graph_def_path = "tf_adapter/tests/ut/optimizers/pbtxt/om_test_in_out_pair_flag_false.pbtxt"; + InitGraph(org_graph_def_path); + EXPECT_EQ(DoRunOmOptimizationPassTest(), "GeOp9_0->retval_Add_0_0"); +} +TEST_F(OmOptimizationPassTest, InOutPairFlagTrueTest) { + string org_graph_def_path = "tf_adapter/tests/ut/optimizers/pbtxt/om_test_in_out_pair_flag_true.pbtxt"; + InitGraph(org_graph_def_path); + EXPECT_EQ(DoRunOmOptimizationPassTest(), "Variable->Variable/read;GeOp10_0->Add;Variable/read->Add:1;Add->retval_Add_0_0"); +} +TEST_F(OmOptimizationPassTest, DynamicGetNextInputTest) { + string org_graph_def_path = "tf_adapter/tests/ut/optimizers/pbtxt/om_test_getnext_lazy_recompile.pbtxt"; + InitGraph(org_graph_def_path); + std::string target_graph = DoRunOmOptimizationPassTest(true); + std::vector result_graphs; + Split(target_graph, result_graphs, "|"); + EXPECT_EQ(result_graphs[1], "IteratorV2->IteratorGetNext;IteratorGetNext->IteratorGetNext_0_retval_RetVal;IteratorGetNext:1->IteratorGetNext_1_retval_RetVal"); +} +} // end namespace +} // end tensorflow \ No newline at end of file diff --git a/tf_adapter/tests/ut/optimizers/testcase/weight_update_sharding_pass_test.cc b/tf_adapter/tests/ut/optimizers/testcase/weight_update_sharding_pass_test.cc new file mode 100644 index 000000000..616913b67 --- /dev/null +++ b/tf_adapter/tests/ut/optimizers/testcase/weight_update_sharding_pass_test.cc @@ -0,0 +1,111 @@ +#include "tf_adapter/optimizers/weight_update_sharding_pass.h" +#include "gtest/gtest.h" +#include "mmpa/mmpa_api.h" +#include "tensorflow/core/graph/graph.h" +#include "tensorflow/core/graph/graph_constructor.h" +#include "tensorflow/core/lib/strings/str_util.h" +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/public/session_options.h" + +namespace tensorflow { +namespace { +class WeightShardOptimizationPassTest : public testing::Test { + public: + WeightShardOptimizationPassTest() : graph_(absl::make_unique(OpRegistry::Global())) {} + static void InitGraph(const string &graph_def_path, Graph *graph) { + GraphDef graph_def; + ReadTextProto(Env::Default(), graph_def_path, &graph_def); + GraphConstructorOptions opts; + TF_CHECK_OK(ConvertGraphDefToGraph(opts, graph_def, graph)); + } + + void InitGraph(const string &graph_def_path) { + char trusted_path[MMPA_MAX_PATH] = { "\0" }; + if (mmRealPath(graph_def_path.c_str(), trusted_path, MMPA_MAX_PATH) != EN_OK) { + LOG(ERROR) << "Get real path failed."; + return; + } + LOG(INFO) << "input graph def path: " << trusted_path; + InitGraph(trusted_path, graph_.get()); + original_ = CanonicalGraphString(graph_.get()); + } + + static bool IncludeNode(const Node *n) { return n->IsOp(); } + + static string EdgeId(const Node* n, int index) { + if (index == 0) { + return n->type_string(); + } else if (index == Graph::kControlSlot) { + return strings::StrCat(n->type_string(), ":control"); + } else { + return strings::StrCat(n->type_string(), ":", index); + } + } + + string CanonicalGraphString(Graph* g) { + for (Node* n : g->nodes()) { + if (IncludeNode(n)) { + if (n->assigned_device_name().empty()) { + n->set_assigned_device_name("/job:localhost/replica:0/task:0/device:CPU:0"); + break; + } + } + } + + std::vector edges; + for (const Edge* e : g->edges()) { + if (IncludeNode(e->src()) && IncludeNode(e->dst())) { + edges.push_back(strings::StrCat(EdgeId(e->src(), e->src_output()), "->", + EdgeId(e->dst(), e->dst_input()))); + } + } + // Canonicalize + return strings::StrCat(absl::StrJoin(edges, ";")); + } + + string DoRunWeightShardOptimizationPassTest() { + string before = CanonicalGraphString(graph_.get()); + LOG(INFO) << "Before om conversion pass: " << before; + + FunctionDefLibrary flib; + std::unique_ptr *ug = &graph_; + GraphOptimizationPassOptions options; + SessionOptions session_options; + session_options.config.mutable_graph_options() + ->mutable_optimizer_options() + ->set_do_function_inlining(true); + auto *custom_config = session_options.config.mutable_graph_options()->mutable_rewrite_options()->add_custom_optimizers(); + custom_config->set_name("NpuOptimizer"); + AttrValue job = AttrValue(); + job.set_s("localhost"); + (*custom_config->mutable_parameter_map())["job"] = job; + options.session_options = &session_options; + options.graph = ug; + std::unique_ptr flib_def( + new FunctionLibraryDefinition((*options.graph)->op_registry(), flib)); + options.flib_def = flib_def.get(); + WeightUpdateShardingPass().Run(options); + + string result = CanonicalGraphString(options.graph->get()); + LOG(INFO) << "After om conversion pass: " << result; + return result; + } + + const string &OriginalGraph() const { return original_; } + + std::unique_ptr graph_; + string original_; + protected: + virtual void SetUp() {} + virtual void TearDown() {} +}; + +TEST_F(WeightShardOptimizationPassTest, FuncTest) { + string org_graph_def_path = "tf_adapter/tests/ut//optimizers/pbtxt/weight_update_shard_test_func.pbtxt"; + InitGraph(org_graph_def_path); + std::string target_graph = "VariableV2->Identity;VariableV2->Cast;VariableV2->HcomBroadcast;HcomBroadcast:control->Identity:control;HcomBroadcast:control->Cast:control"; + EXPECT_EQ(DoRunWeightShardOptimizationPassTest(), target_graph); +} +} // end namespace +} // end tensorflow diff --git a/tf_adapter/tests/ut/util/testcase/ge_plugin_test.cc b/tf_adapter/tests/ut/util/testcase/ge_plugin_test.cc new file mode 100644 index 000000000..0d0b7c0a0 --- /dev/null +++ b/tf_adapter/tests/ut/util/testcase/ge_plugin_test.cc @@ -0,0 +1,114 @@ +#include "tf_adapter/util/npu_plugin.h" +#include "tf_adapter/util/npu_attrs.h" +#include "gtest/gtest.h" +#include + +namespace tensorflow { +namespace { +class GePluginTest : public testing::Test { + protected: + virtual void SetUp() {} + virtual void TearDown() {} +}; + +TEST_F(GePluginTest, PluginInitTest) { + std::map init_options; + setenv("JOB_ID", "1000", true); + setenv("RANK_SIZE", "1", true); + setenv("RANK_ID", "0", true); + setenv("POD_NAME", "0", true); + setenv("RANK_TABLE_FILE", "rank_table", true); + setenv("FUSION_TENSOR_SIZE", "524288000", true); + std::string tf_config = "{'task':{'type':'a'}, 'cluster':{'chief':['1']}}"; + setenv("TF_CONFIG", tf_config.c_str(), true); + init_options["ge.exec.profilingMode"] = "1"; + init_options["ge.exec.profilingOptions"] = "trace"; + init_options["ge.exec.precision_mode"] = "allow_fp32_to_fp16"; + init_options["ge.autoTuneMode"] = "GA"; + init_options["ge.opDebugLevel"] = "1"; + init_options["ge.jobType"] = "2"; + PluginInit(init_options); +} + +TEST_F(GePluginTest, PluginFinalizeTest) { + PluginFinalize(); +} +TEST_F(GePluginTest, InitRdmaPoolOKTest) { + int32_t ret = InitRdmaPool(1); + EXPECT_EQ(ret, 0); +} +TEST_F(GePluginTest, InitRdmaPoolFaliedTest) { + int32_t ret = InitRdmaPool(0); + EXPECT_EQ(ret, -1); +} +TEST_F(GePluginTest, RegistRdmaRemoteAddrFailedTest) { + std::vector var_info; + int32_t ret = RegistRdmaRemoteAddr(var_info); + EXPECT_EQ(ret, -1); +} +TEST_F(GePluginTest, RegistRdmaRemoteAddrOKTest) { + std::vector var_info; + ge::HostVarInfo host_var_info; + host_var_info.base_addr = 0; + var_info.push_back(host_var_info); + int32_t ret = RegistRdmaRemoteAddr(var_info); + EXPECT_EQ(ret, 0); +} +TEST_F(GePluginTest, GetVarAddrAndSizeOKTest) { + uint64_t base_addr = 0; + uint64_t var_size = 0; + int32_t ret = GetVarAddrAndSize("var", base_addr, var_size); + EXPECT_EQ(ret, 0); +} +TEST_F(GePluginTest, GetVarAddrAndSizeFailedTest) { + uint64_t base_addr = 0; + uint64_t var_size = 0; + int32_t ret = GetVarAddrAndSize("", base_addr, var_size); + EXPECT_EQ(ret, -1); +} +TEST_F(GePluginTest, MallocSharedMemFailedTest) { + ge::TensorInfo tensor_info; + uint64_t dev_addr = 0; + uint64_t memory_size = 0; + int32_t ret = MallocSharedMem(tensor_info, dev_addr, memory_size); + EXPECT_EQ(ret, -1); +} +TEST_F(GePluginTest, MallocSharedMemOKTest) { + ge::TensorInfo tensor_info; + tensor_info.var_name = "ge"; + uint64_t dev_addr = 0; + uint64_t memory_size = 0; + int32_t ret = MallocSharedMem(tensor_info, dev_addr, memory_size); + EXPECT_EQ(ret, 0); +} +TEST_F(GePluginTest, NpuCloseTest) { + NpuAttrs::SetUseTdtStatus(0, true); + NpuClose(); +} +TEST_F(GePluginTest, RdmaInitAndRegisterFail1Test) { + std::vector var_info; + ge::HostVarInfo host_var_info; + host_var_info.base_addr = 0; + var_info.push_back(host_var_info); + size_t size = 0; + int32_t ret = RdmaInitAndRegister(var_info, size); + EXPECT_EQ(ret, -1); +} +TEST_F(GePluginTest, RdmaInitAndRegisterFail2Test) { + std::vector var_info; + size_t size = 1; + int32_t ret = RdmaInitAndRegister(var_info, size); + EXPECT_EQ(ret, -1); +} +TEST_F(GePluginTest, RdmaInitAndRegisterOKTest) { + std::vector var_info; + ge::HostVarInfo host_var_info; + host_var_info.base_addr = 0; + var_info.push_back(host_var_info); + size_t size = 1; + int32_t ret = RdmaInitAndRegister(var_info, size); + EXPECT_EQ(ret, 0); +} + +} +} // end tensorflow \ No newline at end of file diff --git a/tf_adapter/tests/ut/util/testcase/npu_attrs_test.cc b/tf_adapter/tests/ut/util/testcase/npu_attrs_test.cc new file mode 100644 index 000000000..52ea74f38 --- /dev/null +++ b/tf_adapter/tests/ut/util/testcase/npu_attrs_test.cc @@ -0,0 +1,49 @@ +#include "tf_adapter/util/npu_attrs.h" +#include "gtest/gtest.h" +#include + +namespace tensorflow { +namespace { +class NpuAttrTest : public testing::Test { + protected: + virtual void SetUp() {} + virtual void TearDown() {} +}; +TEST_F(NpuAttrTest, GetEnvDeviceIdDefaultTest) { + uint32_t device_id = 0; + (void)GetEnvDeviceID(device_id); + EXPECT_EQ(device_id, 0); +} +TEST_F(NpuAttrTest, GetEnvAscendDeviceIdEmptyTest) { + uint32_t device_id = 0; + setenv("DEVICE_ID", "1", true); + (void)GetEnvDeviceID(device_id); + EXPECT_EQ(device_id, 1); +} +TEST_F(NpuAttrTest, GetEnvDeviceIdFailTest) { + uint32_t device_id = 0; + setenv("DEVICE_ID", "-1", true); + Status s = GetEnvDeviceID(device_id); + EXPECT_EQ(s.ok(), false); +} +TEST_F(NpuAttrTest, GetEnvDeviceIdEmptyTest) { + uint32_t device_id = 0; + setenv("ASCEND_DEVICE_ID", "1", true); + (void)GetEnvDeviceID(device_id); + EXPECT_EQ(device_id, 1); +} +TEST_F(NpuAttrTest, GetEnvAscendDeviceIdFailTest) { + uint32_t device_id = 0; + setenv("ASCEND_DEVICE_ID", "-aa", true); + Status s = GetEnvDeviceID(device_id); + EXPECT_EQ(s.ok(), false); +} +TEST_F(NpuAttrTest, SplitTest) { + std::string s = "a,b,c"; + std::vector res; + Split(s, res, ","); + EXPECT_EQ(res[2], "c"); +} + +} +} // end tensorflow \ No newline at end of file -- Gitee