Skip to content

Commit

Permalink
clean after rebase. Last renaming stvm to tvm, separate TVM and Nupha…
Browse files Browse the repository at this point in the history
…r in cmake and build files
  • Loading branch information
Valery Chernov committed Feb 1, 2022
1 parent c1313e2 commit 6932c8d
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 99 deletions.
6 changes: 3 additions & 3 deletions cgmanifests/cgmanifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@
"commitHash": "638d7d2407de27f98f542f61a37a33c90a2e75a9",
"repositoryUrl": "https://github.com/microsoft/onnxruntime-tvm.git"
},
"comments": "needed for nuphar"
"comments": "needed for Nuphar EP"
}
},
{
"component": {
"type": "git",
"git": {
"commitHash": "7b3a22e465dd6aca4729504a19beb4bc23312755",
"commitHash": "36b48a5707321adba8a70e14da443566a9391e5a",
"repositoryUrl": "https://github.com/apache/tvm.git"
},
"comments": "needed for EP STVM"
"comments": "needed for TVM EP"
}
},
{
Expand Down
147 changes: 87 additions & 60 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ option(onnxruntime_BUILD_OBJC "Build Objective-C library" OFF)
option(onnxruntime_USE_PREINSTALLED_EIGEN "Use pre-installed EIGEN. Need to provide eigen_SOURCE_PATH if turn this on." OFF)
option(onnxruntime_BUILD_BENCHMARKS "Build ONNXRuntime micro-benchmarks" OFF)

option(onnxruntime_USE_NUPHAR_TVM "Build TVM for code generation." OFF)
option(onnxruntime_USE_NUPHAR_TVM "Build TVM for code generation for NUPHAR EP" OFF)
option(onnxruntime_USE_LLVM "Build TVM with LLVM" OFF)

option(onnxruntime_BUILD_FOR_NATIVE_MACHINE "Enable this option for turning on optimization specific to this machine" OFF)
Expand Down Expand Up @@ -806,6 +806,24 @@ if (onnxruntime_DISABLE_OPTIONAL_TYPE)
add_compile_definitions(DISABLE_OPTIONAL_TYPE)
endif()

if (onnxruntime_USE_CUDA AND onnxruntime_USE_TVM)
if ("${onnxruntime_CUDA_HOME}" STREQUAL "")
message(FATAL_ERROR "onnxruntime_CUDA_HOME required for onnxruntime_USE_CUDA")
endif()

set(USE_TVM_CUDA ${onnxruntime_CUDA_HOME} CACHE BOOL "Only defined for TVM EP")
# TODO(vvchernov): check cmake keys and remove if need
# set(USE_MKLDNN ON CACHE BOOL "Only defined for TVM")
# set(USE_CUDNN ON CACHE BOOL "Only defined for TVM")

if (WIN32)
list(APPEND onnxruntime_LINK_DIRS ${onnxruntime_CUDA_HOME}/x64/lib64)
else()
list(APPEND onnxruntime_LINK_DIRS ${onnxruntime_CUDA_HOME}/lib64)
endif()

set(onnxruntime_USE_CUDA OFF)
endif()
if (onnxruntime_USE_CUDA AND "${onnxruntime_CUDNN_HOME}" STREQUAL "")
message(FATAL_ERROR "onnxruntime_CUDNN_HOME required for onnxruntime_USE_CUDA")
endif()
Expand Down Expand Up @@ -1329,67 +1347,99 @@ if (onnxruntime_USE_DNNL)
include(dnnl)
endif()

# TVM
# NUPHAR EP
if (onnxruntime_USE_NUPHAR_TVM)
if (NOT TARGET tvm)
message(STATUS "Include TVM(*).")
include(tvm)
endif()
if (onnxruntime_USE_CUDA)
if (onnxruntime_USE_STVM)
set(USE_CUDA ${onnxruntime_CUDA_HOME} CACHE BOOL "Only defined for TVM")
set(USE_MKLDNN ON CACHE BOOL "Only defined for TVM")
set(USE_CUDNN ON CACHE BOOL "Only defined for TVM")
endif()
if (onnxruntime_USE_NUPHAR)
set(USE_CUDA ON CACHE BOOL "Only defined for TVM")
endif()
set(USE_CUDA ON CACHE BOOL "Only defined for TVM")
endif()
if (onnxruntime_USE_LLVM)
set(USE_LLVM ON CACHE BOOL "Only defined for TVM")
add_definitions(-DUSE_NUPHAR_TVM_WITH_LLVM)
endif()

if (onnxruntime_USE_STVM)
set(USE_OPENMP gnu CACHE STRING "Only defined for TVM")
set(USE_MICRO OFF CACHE BOOL "Only defined for TVM")
endif()
message(STATUS "TVM BEFORE USE_LLVM=${USE_LLVM} USE_OPENMP=${USE_OPENMP} USE_MICRO=${USE_MICRO} CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} USE_CUDA=${USE_CUDA} USE_GTEST=${USE_GTEST} CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
message(STATUS "TVM BEFORE USE_LLVM=${USE_LLVM} USE_OPENMP=${USE_OPENMP} CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} USE_CUDA=${USE_CUDA} USE_GTEST=${USE_GTEST} CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
message(STATUS "tvm_SOURCE_DIR=${tvm_SOURCE_DIR}")
message(STATUS "tvm_BINARY_DIR=${tvm_BINARY_DIR}")
add_subdirectory(${tvm_SOURCE_DIR} ${tvm_BINARY_DIR} EXCLUDE_FROM_ALL)
message(STATUS "TVM AFTER USE_LLVM=${USE_LLVM} USE_OPENMP=${USE_OPENMP} USE_MICRO=${USE_MICRO} CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} USE_CUDA=${USE_CUDA} USE_GTEST=${USE_GTEST} CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
message(STATUS "TVM AFTER USE_LLVM=${USE_LLVM} USE_OPENMP=${USE_OPENMP} CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} USE_CUDA=${USE_CUDA} USE_GTEST=${USE_GTEST} CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")

if (onnxruntime_USE_NUPHAR)
set_target_properties(tvm PROPERTIES FOLDER ${tvm_SOURCE_DIR})
set_target_properties(tvm_topi PROPERTIES FOLDER ${tvm_SOURCE_DIR})
set_target_properties(tvm_runtime PROPERTIES FOLDER ${tvm_SOURCE_DIR})
set_target_properties(tvm PROPERTIES FOLDER ${tvm_SOURCE_DIR})
set_target_properties(tvm_topi PROPERTIES FOLDER ${tvm_SOURCE_DIR})
set_target_properties(tvm_runtime PROPERTIES FOLDER ${tvm_SOURCE_DIR})

set(TVM_INCLUDES ${tvm_SOURCE_DIR}/include
${tvm_SOURCE_DIR}/3rdparty/dmlc-core/include
${tvm_SOURCE_DIR}/3rdparty/dlpack/include
$<TARGET_PROPERTY:tvm,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:tvm_topi,INTERFACE_INCLUDE_DIRECTORIES>)
endif()
set(TVM_INCLUDES ${tvm_SOURCE_DIR}/include
${tvm_SOURCE_DIR}/3rdparty/dmlc-core/include
${tvm_SOURCE_DIR}/3rdparty/dlpack/include
$<TARGET_PROPERTY:tvm,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:tvm_topi,INTERFACE_INCLUDE_DIRECTORIES>)

if (onnxruntime_USE_STVM)
set_target_properties(tvm PROPERTIES FOLDER ${tvm_SOURCE_DIR})
add_definitions(-DUSE_NUPHAR_TVM)

set(TVM_INCLUDES ${tvm_SOURCE_DIR}/include
${tvm_SOURCE_DIR}/3rdparty/dmlc-core/include
${tvm_SOURCE_DIR}/3rdparty/dlpack/include
$<TARGET_PROPERTY:tvm,INTERFACE_INCLUDE_DIRECTORIES>)
set(onnxruntime_tvm_libs onnxruntime_codegen_tvm)

# needs to link with stdc++fs in Linux
if (UNIX)
if (NOT APPLE)
set(FS_STDLIB stdc++fs)
endif()
endif()
list(APPEND onnxruntime_EXTERNAL_LIBRARIES tvm ${FS_STDLIB})
list(APPEND onnxruntime_EXTERNAL_DEPENDENCIES tvm)

add_definitions(-DUSE_NUPHAR_TVM)
if (WIN32 AND MSVC)
# wd4100: identifier' : unreferenced formal parameter
# wd4244: conversion from 'int' to 'char', possible loss of data
# wd4251: class X needs to have dll-interface to be used by clients of class Y
# wd4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
# wd4275: non dll-interface class X used as base for dll-interface class Y
# wd4389: signed/unsigned mismatch
# wd4456: declaration of X hides previous local declaration
set(DISABLED_WARNINGS_FOR_TVM "/wd4100" "/wd4244" "/wd4251" "/wd4267" "/wd4275" "/wd4389" "/wd4456")
else()
set(DISABLED_WARNINGS_FOR_TVM "-Wno-error=ignored-qualifiers" "-Wno-unused-parameter" "-Wno-ignored-qualifiers")
list(APPEND DISABLED_WARNINGS_FOR_TVM "-Wno-error=unused-parameter")
if (HAS_CATCH_VALUE)
#TODO: send a PR to TVM and fix it
list(APPEND DISABLED_WARNINGS_FOR_TVM "-Wno-error=catch-value")
endif()
endif()

include(onnxruntime_codegen.cmake)
endif(onnxruntime_USE_NUPHAR_TVM)

if (onnxruntime_USE_NUPHAR)
set(onnxruntime_tvm_libs onnxruntime_codegen_tvm)
# TVM EP
if (onnxruntime_USE_TVM)
if (NOT TARGET tvm)
message(STATUS "Include TVM(*).")
include(tvm)
endif()
if (onnxruntime_USE_STVM)
set(onnxruntime_tvm_libs onnxruntime_providers_stvm)

if (onnxruntime_USE_LLVM)
set(USE_LLVM ON CACHE BOOL "Only defined for TVM")
endif()

set(USE_OPENMP gnu CACHE STRING "Only defined for TVM")
# TODO(vvchernov): check messages and remove if need
message(STATUS "TVM BEFORE USE_LLVM=${USE_LLVM} USE_OPENMP=${USE_OPENMP} CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} USE_CUDA=${USE_TVM_CUDA} USE_GTEST=${USE_GTEST} CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
message(STATUS "tvm_SOURCE_DIR=${tvm_SOURCE_DIR}")
message(STATUS "tvm_BINARY_DIR=${tvm_BINARY_DIR}")
add_subdirectory(${tvm_SOURCE_DIR} ${tvm_BINARY_DIR} EXCLUDE_FROM_ALL)
message(STATUS "TVM AFTER USE_LLVM=${USE_LLVM} USE_OPENMP=${USE_OPENMP} CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} USE_CUDA=${USE_TVM_CUDA} USE_GTEST=${USE_GTEST} CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")

set_target_properties(tvm PROPERTIES FOLDER ${tvm_SOURCE_DIR})

set(TVM_INCLUDES ${tvm_SOURCE_DIR}/include
${tvm_SOURCE_DIR}/3rdparty/dmlc-core/include
${tvm_SOURCE_DIR}/3rdparty/dlpack/include
$<TARGET_PROPERTY:tvm,INTERFACE_INCLUDE_DIRECTORIES>)

#TODO(vvchernov): check
set(onnxruntime_tvm_libs onnxruntime_providers_tvm)

# needs to link with stdc++fs in Linux
if (UNIX)
if (NOT APPLE)
Expand Down Expand Up @@ -1704,29 +1754,6 @@ if (onnxruntime_USE_ROCM)
add_definitions(-DROCM_VERSION=${ROCM_VERSION_DEV_INT})
endif()

if (onnxruntime_USE_NUPHAR_TVM)
if (WIN32 AND MSVC)
# wd4100: identifier' : unreferenced formal parameter
# wd4244: conversion from 'int' to 'char', possible loss of data
# wd4251: class X needs to have dll-interface to be used by clients of class Y
# wd4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
# wd4275: non dll-interface class X used as base for dll-interface class Y
# wd4389: signed/unsigned mismatch
# wd4456: declaration of X hides previous local declaration
set(DISABLED_WARNINGS_FOR_TVM "/wd4100" "/wd4244" "/wd4251" "/wd4267" "/wd4275" "/wd4389" "/wd4456")
else()
set(DISABLED_WARNINGS_FOR_TVM "-Wno-error=ignored-qualifiers" "-Wno-unused-parameter" "-Wno-ignored-qualifiers")
list(APPEND DISABLED_WARNINGS_FOR_TVM "-Wno-error=unused-parameter")
if (HAS_CATCH_VALUE)
#TODO: send a PR to TVM and fix it
list(APPEND DISABLED_WARNINGS_FOR_TVM "-Wno-error=catch-value")
endif()
endif()
if (onnxruntime_USE_NUPHAR)
include(onnxruntime_codegen.cmake)
endif()
endif()

if (onnxruntime_ENABLE_MICROSOFT_INTERNAL)
add_definitions(-DMICROSOFT_INTERNAL)
endif()
Expand Down
11 changes: 4 additions & 7 deletions cmake/external/tvm.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
if (onnxruntime_USE_STVM)
message(STATUS "onnxruntime_USE_STVM: Fetch tvm for STVM.")
if (onnxruntime_USE_TVM)
message(STATUS "onnxruntime_USE_TVM: Fetch tvm for TVM EP")

FetchContent_Declare(
tvm
GIT_REPOSITORY https://github.com/apache/tvm.git
GIT_TAG v0.8.0
GIT_TAG 36b48a5707321adba8a70e14da443566a9391e5a
)

FetchContent_GetProperties(tvm)
Expand All @@ -13,14 +13,11 @@ if (onnxruntime_USE_STVM)
endif()

set(tvm_INCLUDE_DIRS ${tvm_SOURCE_DIR}/include)
set(onnxruntime_STVM_HOME ${tvm_SOURCE_DIR})
message(STATUS "Define onnxruntime_STVM_HOME.")
message(STATUS ${onnxruntime_STVM_HOME})

endif()

if (onnxruntime_USE_NUPHAR)
message(STATUS "onnxruntime_USE_NUPHAR: Fetch onnxruntime-tvm for NUPHAR.")
message(STATUS "onnxruntime_USE_NUPHAR: Fetch onnxruntime-tvm for NUPHAR EP")

FetchContent_Declare(
tvm
Expand Down
6 changes: 3 additions & 3 deletions cmake/onnxruntime_providers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1275,10 +1275,12 @@ if (onnxruntime_USE_ROCM)
endif()

if (onnxruntime_USE_TVM)
# TODO(vvchernov): repeat from CMakeLists, do we need it here?
if (NOT TARGET tvm)
message(STATUS "Include TVM.")
include(tvm)
endif()
# TODO(vvchernov): do we use this defenition in the code?
add_definitions(-DUSE_TVM=1)

file (GLOB_RECURSE onnxruntime_providers_tvm_cc_srcs CONFIGURE_DEPENDS
Expand All @@ -1293,9 +1295,7 @@ if (onnxruntime_USE_TVM)
endif()

target_include_directories(onnxruntime_providers_tvm PRIVATE
${onnxruntime_TVM_HOME}/include
${onnxruntime_TVM_HOME}/3rdparty/dlpack/include
${onnxruntime_TVM_HOME}/3rdparty/dmlc-core/include
${TVM_INCLUDES}
${PYTHON_INLCUDE_DIRS})
onnxruntime_add_include_to_target(onnxruntime_providers_tvm onnxruntime_common onnx tvm)

Expand Down
4 changes: 3 additions & 1 deletion cmake/onnxruntime_python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ if (onnxruntime_USE_ROCM)
)
endif()

if (onnxruntime_USE_NUPHAR_TVM)
if (onnxruntime_USE_NUPHAR)
add_custom_command(
TARGET onnxruntime_pybind11_state POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
Expand Down Expand Up @@ -676,11 +676,13 @@ if (onnxruntime_USE_TVM)
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:onnxruntime_providers_tvm>
$<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/capi/
# TODO(vvchernov): why?
COMMAND ${CMAKE_COMMAND} -E copy
${tvm_BINARY_DIR}/libtvm*
${tvm_SOURCE_DIR}/python/tvm
)

# TODO(vvchernov): repeat?
add_custom_command(
TARGET onnxruntime_pybind11_state POST_BUILD
WORKING_DIRECTORY ${tvm_SOURCE_DIR}/python
Expand Down
10 changes: 5 additions & 5 deletions cmake/onnxruntime_unittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,8 @@ if(onnxruntime_USE_COREML)
endif()
endif()

if(onnxruntime_USE_NUPHAR_TVM)
file(GLOB_RECURSE onnxruntime_test_nuphar_tvm_src CONFIGURE_DEPENDS
if(onnxruntime_USE_NUPHAR)
file(GLOB_RECURSE onnxruntime_test_nuphar_src CONFIGURE_DEPENDS
"${TEST_SRC_DIR}/nuphar_tvm/*.h"
"${TEST_SRC_DIR}/nuphar_tvm/*.cc"
)
Expand Down Expand Up @@ -644,8 +644,8 @@ if (onnxruntime_ENABLE_TRAINING)
list(APPEND all_tests ${onnxruntime_test_training_src})
endif()

if (onnxruntime_USE_NUPHAR_TVM)
list(APPEND all_tests ${onnxruntime_test_nuphar_tvm_src})
if (onnxruntime_USE_NUPHAR)
list(APPEND all_tests ${onnxruntime_test_nuphar_src})
endif()

if (onnxruntime_USE_TVM)
Expand Down Expand Up @@ -1276,7 +1276,7 @@ if (NOT onnxruntime_MINIMAL_BUILD AND NOT onnxruntime_EXTENDED_MINIMAL_BUILD
endif()

if (onnxruntime_USE_TVM)
# find_library(TVM_LIBS NAMES libtvm.so PATHS ${onnxruntime_TVM_HOME}/lib)
# find_library(TVM_LIBS NAMES libtvm.so PATHS ${tvm_SOURCE_DIR}/lib)
# link_directories(onnxruntime_test_all ${TVM_LIBS})
find_library(PYTHON_LIBS NAMES libpython PATHS /usr/local/lib)
#target_link_libraries(onnxruntime_test_all PRIVATE ${PYTHON_LIBRARIES} -lutil)
Expand Down
11 changes: 6 additions & 5 deletions docs/TVM_EP.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ Build ONNX Runtime:
./build.sh --config Release --enable_pybind --build_wheel --skip_tests --parallel --use_tvm --skip_onnx_tests
```

This command builds both TVM and onnxruntime-stvm. It creates two wheel, one for each project.
This command builds both TVM and onnxruntime-tvm. It creates two wheel, one for each project.
Build the python API for ONNX Runtime instead of using the standard package:
```bash
cd <path_to_onnx_runtime>
pip3 uninstall onnxruntime onnxruntime-tvm tvm -y
whl_path=$(find ./build/cpu_stvm/Release/dist -name "*.whl")
whl_path=$(find ./build/cpu_tvm/Release/dist -name "*.whl")
python3 -m pip install $whl_path
```
Alternatively, you can set PYTHONPATH to tell python where to find the ONNXRT library and the TVM library.
```bash
export PYTHONPATH=<path_to_onnx_runtime>/build/cpu_stvm/Release:${PYTHONPATH}
export PYTHONPATH=<path_to_onnx_runtime>/build/cpu_stvm/Release/_deps/tvm-src/python:${PYTHONPATH}
export PYTHONPATH=<path_to_onnx_runtime>/build/cpu_tvm/Release:${PYTHONPATH}
export PYTHONPATH=<path_to_onnx_runtime>/build/cpu_tvm/Release/_deps/tvm-src/python:${PYTHONPATH}
```

## Configuration options
Expand Down Expand Up @@ -123,7 +123,8 @@ The following pair of ONNX and protobuf versions have been found to be compatibl
- 3.17.3 and 1.8.0
- 3.19.1 and 1.10.1

When use onnxruntime-stvm after it was build from the source, the following error may happen:
# TODO(vvchernov): it looks like issue was resolved
When use onnxruntime-tvm after it was build from the source, the following error may happen:

```
terminate called after throwing an instance of 'tvm::runtime::InternalError'
Expand Down
2 changes: 2 additions & 0 deletions onnxruntime/core/providers/tvm/tvm_api.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#include "core/common/common.h"

#include "tvm_api.h"

#include <tvm/runtime/registry.h>
Expand Down
4 changes: 2 additions & 2 deletions onnxruntime/test/python/onnxruntime_test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

available_providers = [
provider for provider in onnxrt.get_available_providers()
if provider not in {'StvmExecutionProvider'}]
if provider not in {'TvmExecutionProvider'}]


class TestInferenceSession(unittest.TestCase):
Expand All @@ -33,7 +33,7 @@ def run_model(self, session_object, run_options):
np.testing.assert_allclose(output_expected, res[0], rtol=1e-05, atol=1e-08)

def testTvmImported(self):
if "StvmExecutionProvider" not in onnxrt.get_available_providers():
if "TvmExecutionProvider" not in onnxrt.get_available_providers():
return
import tvm
self.assertTrue(tvm is not None)
Expand Down
Loading

0 comments on commit 6932c8d

Please sign in to comment.