Unverified Commit 41e1d5f9 by Jared Roesch Committed by GitHub

Revive the Rust + SGX refactor (#4976)

* Add Nick's changes's squashed

* Fix frontend compilation

* Re-enable Rust CI

* Add changes with conflicted badly

* Restructure import_module! macro in order to avoid unstable features

* Kill old unstable feature enablement

* Refactor common to use new APIs

* Move the code to stable

* Fix warning

Co-authored-by: Nick Hynes <nhynes@oasislabs.com>
parent 93dff448
...@@ -36,7 +36,6 @@ tvm_option(USE_GRAPH_RUNTIME "Build with tiny graph runtime" ON) ...@@ -36,7 +36,6 @@ tvm_option(USE_GRAPH_RUNTIME "Build with tiny graph runtime" ON)
tvm_option(USE_GRAPH_RUNTIME_DEBUG "Build with tiny graph runtime debug mode" OFF) tvm_option(USE_GRAPH_RUNTIME_DEBUG "Build with tiny graph runtime debug mode" OFF)
tvm_option(USE_OPENMP "Build with OpenMP thread pool implementation" OFF) tvm_option(USE_OPENMP "Build with OpenMP thread pool implementation" OFF)
tvm_option(USE_RELAY_DEBUG "Building Relay in debug mode..." OFF) tvm_option(USE_RELAY_DEBUG "Building Relay in debug mode..." OFF)
tvm_option(USE_SGX "Build with SGX" OFF)
tvm_option(USE_RTTI "Build with RTTI" ON) tvm_option(USE_RTTI "Build with RTTI" ON)
tvm_option(USE_MSVC_MT "Build with MT" OFF) tvm_option(USE_MSVC_MT "Build with MT" OFF)
tvm_option(USE_MICRO "Build with Micro" OFF) tvm_option(USE_MICRO "Build with Micro" OFF)
...@@ -243,7 +242,6 @@ include(cmake/modules/OpenMP.cmake) ...@@ -243,7 +242,6 @@ include(cmake/modules/OpenMP.cmake)
include(cmake/modules/Vulkan.cmake) include(cmake/modules/Vulkan.cmake)
include(cmake/modules/Metal.cmake) include(cmake/modules/Metal.cmake)
include(cmake/modules/ROCM.cmake) include(cmake/modules/ROCM.cmake)
include(cmake/modules/SGX.cmake)
include(cmake/modules/LLVM.cmake) include(cmake/modules/LLVM.cmake)
include(cmake/modules/Micro.cmake) include(cmake/modules/Micro.cmake)
include(cmake/modules/ANTLR.cmake) include(cmake/modules/ANTLR.cmake)
...@@ -283,12 +281,6 @@ else() ...@@ -283,12 +281,6 @@ else()
set_target_properties(tvm PROPERTIES COMPILE_DEFINITIONS "NDEBUG") set_target_properties(tvm PROPERTIES COMPILE_DEFINITIONS "NDEBUG")
endif(USE_RELAY_DEBUG) endif(USE_RELAY_DEBUG)
if(NOT USE_SGX STREQUAL "OFF")
add_dependencies(tvm sgx_edl)
add_dependencies(tvm_runtime sgx_edl tvm_t)
install(TARGETS tvm_t ARCHIVE DESTINATION lib${LIB_SUFFIX})
endif()
if(USE_THREADS) if(USE_THREADS)
message(STATUS "Build with thread support...") message(STATUS "Build with thread support...")
set(CMAKE_THREAD_PREFER_PTHREAD TRUE) set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
......
...@@ -220,6 +220,7 @@ stage('Build') { ...@@ -220,6 +220,7 @@ stage('Build') {
sh "${docker_run} ${ci_cpu} ./tests/scripts/task_python_vta_fsim.sh" sh "${docker_run} ${ci_cpu} ./tests/scripts/task_python_vta_fsim.sh"
sh "${docker_run} ${ci_cpu} ./tests/scripts/task_python_vta_tsim.sh" sh "${docker_run} ${ci_cpu} ./tests/scripts/task_python_vta_tsim.sh"
sh "${docker_run} ${ci_cpu} ./tests/scripts/task_golang.sh" sh "${docker_run} ${ci_cpu} ./tests/scripts/task_golang.sh"
sh "${docker_run} ${ci_cpu} ./tests/scripts/task_rust.sh"
} }
} }
} }
......
[build]
target = "x86_64-fortanix-unknown-sgx"
[target.x86_64-fortanix-unknown-sgx]
runner = "ftxsgx-runner-cargo"
../../rust/.rustfmt.toml
\ No newline at end of file
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "aho-corasick"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "arrayvec"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "atty"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "autocfg"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "backtrace"
version = "0.3.37"
source = "git+https://github.com/nhynes/backtrace-rs?branch=fix-sgx#5f89f9d891b843ca6f4e8ac4365c18a076092945"
dependencies = [
"backtrace-sys 0.1.31 (git+https://github.com/nhynes/backtrace-rs?branch=fix-sgx)",
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "backtrace-sys"
version = "0.1.31"
source = "git+https://github.com/nhynes/backtrace-rs?branch=fix-sgx#5f89f9d891b843ca6f4e8ac4365c18a076092945"
dependencies = [
"cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bindgen"
version = "0.51.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cexpr 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
"clang-sys 0.28.1 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
"fxhash 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"which 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bounded-spsc-queue"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "byteorder"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "cc"
version = "1.0.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "cexpr"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cfg-if"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "clang-sys"
version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
"libloading 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "clap"
version = "2.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "either"
version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "env_logger"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
"humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "failure"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"backtrace 0.3.37 (git+https://github.com/nhynes/backtrace-rs?branch=fix-sgx)",
"failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "failure_derive"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)",
"synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "fxhash"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "glob"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "goblin"
version = "0.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"plain 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"scroll 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "humantime"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "itertools"
version = "0.7.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "itertools"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "itoa"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lexical-core"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"arrayvec 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"static_assertions 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "libc"
version = "0.2.62"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libloading"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "log"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "matrixmultiply"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rawpointer 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "memchr"
version = "2.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "ndarray"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)",
"matrixmultiply 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
"num-complex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "nodrop"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "nom"
version = "4.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "nom"
version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lexical-core 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-complex"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-traits"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num_cpus"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "peeking_take_while"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "plain"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "proc-macro2"
version = "0.4.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "proc-macro2"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quick-error"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "quote"
version = "0.6.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quote"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rawpointer"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "regex"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.6.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rustc-demangle"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rustc_version"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ryu"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "scroll"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"scroll_derive 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "scroll_derive"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "semver"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "semver-parser"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde"
version = "1.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde_derive"
version = "1.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde_json"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "sgx-demo"
version = "0.1.0"
dependencies = [
"tvm-runtime 0.1.0",
]
[[package]]
name = "shlex"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "static_assertions"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "syn"
version = "0.15.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "synstructure"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "termcolor"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"wincolor 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tvm-common"
version = "0.1.0"
dependencies = [
"bindgen 0.51.0 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"ndarray 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tvm-macros"
version = "0.1.1"
dependencies = [
"goblin 0.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tvm-runtime"
version = "0.1.0"
dependencies = [
"bounded-spsc-queue 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libloading 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"ndarray 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)",
"nom 5.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)",
"tvm-common 0.1.0",
"tvm-macros 0.1.1",
]
[[package]]
name = "unicode-width"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-xid"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-xid"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "vec_map"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "which"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-util"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "wincolor"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d"
"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
"checksum arrayvec 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b8d73f9beda665eaa98ab9e4f7442bd4e7de6652587de55b2525e52e29c1b0ba"
"checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90"
"checksum autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "b671c8fb71b457dd4ae18c4ba1e59aa81793daacc361d82fcd410cef0d491875"
"checksum backtrace 0.3.37 (git+https://github.com/nhynes/backtrace-rs?branch=fix-sgx)" = "<none>"
"checksum backtrace-sys 0.1.31 (git+https://github.com/nhynes/backtrace-rs?branch=fix-sgx)" = "<none>"
"checksum bindgen 0.51.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18270cdd7065ec045a6bb4bdcd5144d14a78b3aedb3bc5111e688773ac8b9ad0"
"checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd"
"checksum bounded-spsc-queue 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e4c9c650d1935bc66dec0e80beb9353b6c86fb9bcd0a4c0c7f5a70d9770e5194"
"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5"
"checksum cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)" = "4fc9a35e1f4290eb9e5fc54ba6cf40671ed2a2514c3eeb2b2a908dda2ea5a1be"
"checksum cexpr 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a7fa24eb00d5ffab90eaeaf1092ac85c04c64aaf358ea6f84505b8116d24c6af"
"checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33"
"checksum clang-sys 0.28.1 (registry+https://github.com/rust-lang/crates.io-index)" = "81de550971c976f176130da4b2978d3b524eaa0fd9ac31f3ceb5ae1231fb4853"
"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
"checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
"checksum env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3"
"checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2"
"checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1"
"checksum fxhash 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c"
"checksum glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
"checksum goblin 0.0.24 (registry+https://github.com/rust-lang/crates.io-index)" = "e3fa261d919c1ae9d1e4533c4a2f99e10938603c4208d56c05bec7a872b661b0"
"checksum humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
"checksum itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0d47946d458e94a1b7bcabbf6521ea7c037062c81f534615abcad76e84d4970d"
"checksum itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b8467d9c1cebe26feb08c640139247fac215782d35371ade9a2136ed6085358"
"checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
"checksum lexical-core 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2304bccb228c4b020f3a4835d247df0a02a7c4686098d4167762cfbbe4c5cb14"
"checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba"
"checksum libloading 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f2b111a074963af1d37a139918ac6d49ad1d0d5e47f72fd55388619691a7d753"
"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
"checksum matrixmultiply 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "dcad67dcec2d58ff56f6292582377e6921afdf3bfbd533e26fb8900ae575e002"
"checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e"
"checksum ndarray 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7cf380a8af901ad627594013a3bbac903ae0a6f94e176e47e46b5bbc1877b928"
"checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945"
"checksum nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6"
"checksum nom 5.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c618b63422da4401283884e6668d39f819a106ef51f5f59b81add00075da35ca"
"checksum num-complex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "fcb0cf31fb3ff77e6d2a6ebd6800df7fdcd106f2ad89113c9130bcd07f93dffc"
"checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32"
"checksum num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bcef43580c035376c0705c42792c294b66974abbfd2789b511784023f71f3273"
"checksum peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
"checksum plain 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6"
"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
"checksum proc-macro2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "afdc77cc74ec70ed262262942ebb7dac3d479e9e5cfa2da1841c0806f6cdabcc"
"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
"checksum quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1"
"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
"checksum rawpointer 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ebac11a9d2e11f2af219b8b8d833b76b1ea0e054aa0e8d8e9e4cbde353bdf019"
"checksum regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dc220bd33bdce8f093101afe22a037b8eb0e5af33592e6a9caafff0d4cb81cbd"
"checksum regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "11a7e20d1cce64ef2fed88b66d347f88bd9babb82845b2b858f3edbf59a4f716"
"checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783"
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
"checksum ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c92464b447c0ee8c4fb3824ecc8383b81717b9f1e74ba2e72540aef7b9f82997"
"checksum scroll 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2f84d114ef17fd144153d608fba7c446b0145d038985e7a8cc5d08bb0ce20383"
"checksum scroll_derive 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8f1aa96c45e7f5a91cb7fabe7b279f02fea7126239fc40b732316e8b6a2d0fcb"
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
"checksum serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)" = "9796c9b7ba2ffe7a9ce53c2287dfc48080f4b2b362fcc245a259b3a7201119dd"
"checksum serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)" = "4b133a43a1ecd55d4086bd5b4dc6c1751c68b1bfbeba7a5040442022c7e7c02e"
"checksum serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)" = "051c49229f282f7c6f3813f8286cc1e3323e8051823fce42c7ea80fe13521704"
"checksum shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2"
"checksum static_assertions 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7f3eb36b47e512f8f1c9e3d10c2c1965bc992bd9cdb024fa581e2194501c83d3"
"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
"checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5"
"checksum syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "66850e97125af79138385e9b88339cbcd037e3f28ceab8c5ad98e64f0f1f80bf"
"checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f"
"checksum termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "96d6098003bde162e4277c70665bd87c326f5a0c3f3fbfb285787fa482d54e6e"
"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7007dbd421b92cc6e28410fe7362e2e0a2503394908f417b68ec8d1c364c4e20"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
"checksum which 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b57acb10231b9493c8472b20cb57317d0679a49e0bdbee44b3b803a6473af164"
"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
"checksum wincolor 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "96f5016b18804d24db43cebf3c77269e7569b8954a8464501c216cc5e070eaa9"
...@@ -16,17 +16,13 @@ ...@@ -16,17 +16,13 @@
# under the License. # under the License.
[package] [package]
name = "model-enclave" name = "sgx-demo"
version = "0.1.0" version = "0.1.0"
authors = ["Nick Hynes <nhynes@berkeley.edu>"] authors = ["Nick Hynes <nhynes@nhynes.com>"]
edition = "2018"
[lib]
crate-type = ["staticlib"]
[dependencies] [dependencies]
lazy_static = "1.1.0" tvm-runtime = { path = "../../rust/runtime" }
tvm = { path = "../../../rust", default-features = false, features = ["sgx"] }
[profile.release] [patch.crates-io]
lto = true "backtrace" = { git = "https://github.com/nhynes/backtrace-rs", branch = "fix-sgx" }
opt-level = 3
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
SGX_SDK ?= /opt/sgxsdk
RUST_SGX_SDK ?= /opt/rust-sgx-sdk
SGX_MODE ?= SIM
DEBUG ?= true
NUM_THREADS ?= 4
TVM_DIR ?= $(shell git rev-parse --show-toplevel)
export
sgx_edger8r := $(SGX_SDK)/bin/x64/sgx_edger8r
sgx_enclave_signer := $(SGX_SDK)/bin/x64/sgx_sign
ifneq ($(SGX_MODE), HW)
sgx_sim := _sim
endif
urts_library_name := sgx_urts$(sgx_sim)
trts_library_name := sgx_trts$(sgx_sim)
tservice_library_name := sgx_tservice$(sgx_sim)
uservice_library_name := sgx_uae_service$(sgx_sim)
pkg_cflags := -std=c++11 -fPIC \
-I$(SGX_SDK)/include \
-I$(TVM_DIR)/include \
-I$(TVM_DIR)/dlpack/include \
-I$(TVM_DIR)/dmlc-core/include
pkg_ldflags := -L$(TVM_DIR)/build -ltvm_runtime
ifneq ($(DEBUG), false)
debug := debug
enclave_cflags += -Og -g
pkg_cflags += -Og -g
else
debug := release
enclave_cflags += -O2
pkg_cflags += -O2
endif
build_dir := build
enclave_cflags := \
-I$(SGX_SDK)/include \
-I$(SGX_SDK)/include/tlibc \
-I$(SGX_SDK)/include/stdport \
-I$(SGX_SDK)/include/epid \
-I$(TVM_DIR)/include \
-I$(TVM_DIR)/dlpack/include \
-I$(TVM_DIR)/dmlc-core/include
enclave_ldflags :=\
-L$(build_dir) -L$(TVM_DIR)/build \
-Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_SDK)/lib64\
-Wl,--whole-archive -l$(trts_library_name) -Wl,--no-whole-archive\
-Wl,--start-group\
-lsgx_tstdc -lsgx_tstdcxx -lsgx_tcxx -lsgx_tcrypto -lsgx_tkey_exchange -l$(tservice_library_name)\
-lenclave -ltvm_t\
-Wl,--end-group\
-Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined\
-Wl,-pie,-eenclave_entry -Wl,--export-dynamic\
-Wl,--defsym,__ImageBase=0 -Wl,--gc-sections\
-Wl,--version-script=enclave/enclave.lds
.PHONY: enclave clean
enclave: $(build_dir)/enclave.signed.so
$(build_dir)/enclave.signed.so: $(build_dir)/enclave.so build/enclave_config.xml enclave/enclave.pem
$(sgx_enclave_signer) sign -key enclave/enclave.pem -enclave $< -out $@ -config build/enclave_config.xml
enclave/enclave.pem:
curl -sSo $@ 'https://gist.githubusercontent.com/nhynes/8a2d80068a92e672f8b0b7d710ceb404/raw/2d5ae5fbe83198ede49465fdc6535065e093543b/tvm_sgx_demo.pem'
build/enclave_config.xml: enclave/enclave_config.xml.in
cpp $^ -P -o $@ -DNUM_THREADS=$$(( $(NUM_THREADS) + 1 ))
$(build_dir)/enclave.so: $(build_dir)/libenclave.a $(TVM_DIR)/build/libtvm_t.a
$(CXX) $< -o $@ $(enclave_ldflags) $(enclave_cflags) -ltvm_t
$(build_dir)/libenclave.a: enclave/target/x86_64-unknown-linux-sgx/$(debug)/libmodel_enclave.a
@mkdir -p $(@D)
@cp $< $@
enclave/target/x86_64-unknown-linux-sgx/$(debug)/libmodel_enclave.a: enclave/**/*
$(MAKE) -C enclave
clean:
$(MAKE) -s -C enclave clean
rm -rf build
...@@ -15,64 +15,10 @@ ...@@ -15,64 +15,10 @@
<!--- specific language governing permissions and limitations --> <!--- specific language governing permissions and limitations -->
<!--- under the License. --> <!--- under the License. -->
# TVM in Intel SGX Example ## Setup
This application demonstrates the use of a simple TVM model in the [Intel SGX](https://software.intel.com/en-us/blogs/2013/09/26/protecting-application-secrets-with-intel-sgx) trusted computing environment. 1. [Install the Fortanix Enclave Development Platform](https://edp.fortanix.com/docs/installation/guide/)
2. `rustup component add llvm-tools-preview` to get `llvm-ar` and `llvm-objcopy`
## Prerequisites 3. `pip install numpy decorator psutil`
4. `cargo run` to start the enclave TCP server
1. The TVM premade Docker image 5. Send a 28x28 "image" to the enclave model server using `head -c $((28*28*4)) /dev/urandom | nc 127.0.0.1 4242 | python read_results.py`
or
1. A GNU/Linux environment
2. TVM compiled with LLVM and SGX; and the `tvm` Python module
3. The [Linux SGX SDK](https://github.com/intel/linux-sgx) [link to pre-built libraries](https://01.org/intel-software-guard-extensions/downloads)
4. [Rust](https://rustup.sh)
5. The [rust-sgx-sdk](https://github.com/baidu/rust-sgx-sdk)
6. [xargo](https://github.com/japaric/xargo)
Check out the `/tvm/install/ubuntu_install_sgx.sh` for the commands to get these dependencies.
## Running the example
If using Docker, start by running
```
git clone --recursive https://github.com/apache/incubator-tvm.git tvm
docker run --rm -it -v $(pwd)/tvm:/mnt tvmai/ci-cpu /bin/bash
```
then, in the container
```
cd /mnt
mkdir build && cd build
cmake .. -DUSE_LLVM=ON -DUSE_SGX=/opt/sgxsdk -DRUST_SGX_SDK=/opt/rust-sgx-sdk
make -j4
cd ..
pip install -e python -e topi/python
cd apps/sgx
```
Once TVM is build and installed, just
`./run_example.sh`
If everything goes well, you should see a lot of build messages and below them
the text `It works!`.
## High-level overview
First of all, it helps to think of an SGX enclave as a library that can be called
to perform trusted computation.
In this library, one can use other libraries like TVM.
Building this example performs the following steps:
1. Creates a simple TVM module that computes `x + 1` and save it as a system library.
2. Builds a TVM runtime that links the module and allows running it using the TVM Python runtime.
3. Packages the bundle into an SGX enclave
4. Runs the enclave using the usual TVM Python `module` API
For more information on building, please refer to the `Makefile`.
For more information on the TVM module, please refer to `../howto_deploy`.
For more in formation on SGX enclaves, please refer to the [SGX Enclave Demo](https://github.com/intel/linux-sgx/tree/master/SampleCode/SampleEnclave/)
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
use std::process::Command;
macro_rules! mf_dir {
($p:literal) => {
concat!(env!("CARGO_MANIFEST_DIR"), $p)
};
}
fn main() {
let out_dir = std::env::var("OUT_DIR").unwrap();
let build_output = Command::new(mf_dir!("/src/build_model.py"))
.arg(&out_dir)
.env(
"PYTHONPATH",
concat!(
mf_dir!("/../../python"),
":",
mf_dir!("/../../nnvm/python"),
":",
mf_dir!("/../../topi/python")
),
)
.output()
.expect("Failed to build model");
assert!(
["model.o", "graph.json", "params.bin"]
.iter()
.all(|f| { std::path::Path::new(&format!("{}/{}", out_dir, f)).exists() }),
"Could not build tvm lib: STDOUT:\n\n{}\n\nSTDERR\n\n{}",
String::from_utf8(build_output.stdout).unwrap().trim(),
String::from_utf8(build_output.stderr).unwrap().trim()
);
let sysroot_output = Command::new("rustc")
.args(&["--print", "sysroot"])
.output()
.expect("Failed to get sysroot");
let sysroot = String::from_utf8(sysroot_output.stdout).unwrap();
let sysroot = sysroot.trim();
let mut llvm_tools_path = std::path::PathBuf::from(&sysroot);
llvm_tools_path.push("lib/rustlib/x86_64-unknown-linux-gnu/bin");
Command::new("rustup")
.args(&["component", "add", "llvm-tools-preview"])
.output()
.expect("failed to install llvm tools");
std::process::Command::new(llvm_tools_path.join("llvm-objcopy"))
.arg("--globalize-symbol=__tvm_module_startup")
.arg("--remove-section=.ctors")
.arg(&format!("{}/model.o", out_dir))
.output()
.expect("gould not gloablize startup function");
std::process::Command::new(llvm_tools_path.join("llvm-ar"))
.arg("rcs")
.arg(&format!("{}/libmodel.a", out_dir))
.arg(&format!("{}/model.o", out_dir))
.output()
.expect("failed to package model archive");
println!("cargo:rustc-link-lib=static=model");
println!("cargo:rustc-link-search=native={}", out_dir);
}
../../../rust/.rustfmt.toml
\ No newline at end of file
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
MODEL ?= resnet
NUM_THREADS ?= 4
BATCH_SIZE ?= 64
TRAINING ?= true
DEBUG ?= false
build_dir := ../build
ifeq ($(DEBUG), false)
debug := release
xargo_args := --release
else
debug := debug
endif
target=target/x86_64-unknown-linux-sgx/$(debug)/libmodel-enclave.a
$(target): $(build_dir)/libmodel.a **/* $(TVM_DIR)/rust/patched.txt
RUST_TARGET_PATH=$(shell pwd) \
RUST_TARGET_DIR=$(shell pwd)/target \
RUSTFLAGS="-Z force-unstable-if-unmarked" \
TVM_NUM_THREADS=$(NUM_THREADS) \
BUILD_DIR=../build \
xargo build --target x86_64-unknown-linux-sgx $(xargo_args) -q
$(TVM_DIR)/rust/patched.txt: $(shell pwd)/sgx-deps.diff
echo $(TVM_DIR)
cd $(TVM_DIR) && git apply $<
touch $@
$(build_dir)/libmodel.a: $(build_dir)/model.o
$(AR) cr $@ $^
$(build_dir)/model.o: $(build_dir)/model.bc
$(CC) -c $< -o $@ -fPIC -O3
objcopy --globalize-symbol __tvm_module_startup $@
$(build_dir)/model.bc: src/build_model.py
python3 $< -o $(build_dir)
clean:
xargo clean
enclave.so
{
global:
g_global_data_sim;
g_global_data;
enclave_entry;
local:
*;
};
<EnclaveConfiguration>
<ProdID>0</ProdID>
<ISVSVN>0</ISVSVN>
<StackMaxSize>0xf0000</StackMaxSize>
<HeapMaxSize>0xf000000</HeapMaxSize>
<TCSNum>NUM_THREADS</TCSNum>
<TCSPolicy>0</TCSPolicy> <!-- must be "bound" to use thread_local -->
<DisableDebug>0</DisableDebug>
<MiscSelect>0</MiscSelect>
<MiscMask>0xFFFFFFFF</MiscMask>
</EnclaveConfiguration>
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 0819e0c7..e56f4ef2 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -14,7 +14,7 @@ default = ["nom/std"]
sgx = ["nom/alloc"]
[dependencies]
-bounded-spsc-queue = "0.4.0"
+bounded-spsc-queue = { git = "https://github.com/nhynes/bounded-spsc-queue", branch = "sgx" }
error-chain = { version = "0.12.0", default-features = false }
itertools = "0.7.8"
lazy_static = "1.1.0"
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
#![feature(try_from)]
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate tvm;
use std::{
convert::{TryFrom, TryInto},
sync::Mutex,
};
use tvm::{
ffi::runtime::DLTensor,
runtime::{
load_param_dict, sgx, Graph, GraphExecutor, SystemLibModule, TVMArgValue, TVMRetValue, Tensor,
},
};
lazy_static! {
static ref SYSLIB: SystemLibModule = { SystemLibModule::default() };
static ref MODEL: Mutex<GraphExecutor<'static, 'static>> = {
let graph_json = include_str!(concat!("../", env!("BUILD_DIR"), "/graph.json"));
let params_bytes = include_bytes!(concat!("../", env!("BUILD_DIR"), "/params.bin"));
let params = load_param_dict(params_bytes).unwrap();
let graph = Graph::try_from(graph_json).unwrap();
let mut exec = GraphExecutor::new(graph, &*SYSLIB).unwrap();
exec.load_params(params);
Mutex::new(exec)
};
}
fn ecall_init(_args: &[TVMArgValue]) -> TVMRetValue {
lazy_static::initialize(&MODEL);
TVMRetValue::from(0)
}
fn ecall_main(args: &[TVMArgValue<'static>]) -> TVMRetValue {
let mut model = MODEL.lock().unwrap();
let inp = args[0].try_into().unwrap();
let mut out: Tensor = args[1].try_into().unwrap();
model.set_input("data", inp);
model.run();
sgx::shutdown();
out.copy(model.get_output(0).unwrap());
TVMRetValue::from(1)
}
pub mod ecalls {
//! todo: generate this using proc_macros
use super::*;
use std::{
ffi::CString,
mem,
os::raw::{c_char, c_int, c_void},
slice,
};
use tvm::{
ffi::runtime::{TVMRetValueHandle, TVMValue},
runtime::{
sgx::{ocall_packed_func, run_worker, SgxStatus},
DataType, PackedFunc,
},
};
macro_rules! tvm_ocall {
($func: expr) => {
match $func {
0 => Ok(()),
err => Err(err),
}
};
}
const ECALLS: &'static [&'static str] = &["__tvm_run_worker__", "__tvm_main__", "init"];
pub type EcallPackedFunc = Box<Fn(&[TVMArgValue<'static>]) -> TVMRetValue + Send + Sync>;
lazy_static! {
static ref ECALL_FUNCS: Vec<EcallPackedFunc> = {
vec![
Box::new(run_worker),
Box::new(ecall_main),
Box::new(ecall_init),
]
};
}
extern "C" {
fn __tvm_module_startup() -> ();
fn tvm_ocall_register_export(name: *const c_char, func_id: c_int) -> SgxStatus;
}
#[no_mangle]
pub extern "C" fn tvm_ecall_init(_ret: TVMRetValueHandle) {
unsafe {
__tvm_module_startup();
ECALLS.into_iter().enumerate().for_each(|(i, ecall)| {
tvm_ocall!(tvm_ocall_register_export(
CString::new(*ecall).unwrap().as_ptr(),
i as i32
))
.expect(&format!("Error registering `{}`", ecall));
});
}
}
#[no_mangle]
pub extern "C" fn tvm_ecall_packed_func(
func_id: c_int,
arg_values: *const TVMValue,
type_codes: *const c_int,
num_args: c_int,
ret_val: *mut TVMValue,
ret_type_code: *mut i64,
) {
let args = unsafe {
let values = slice::from_raw_parts(arg_values, num_args as usize);
let type_codes = slice::from_raw_parts(type_codes, num_args as usize);
values
.into_iter()
.zip(type_codes.into_iter())
.map(|(v, t)| TVMArgValue::new(*v, *t as i64))
.collect::<Vec<TVMArgValue<'static>>>()
};
let (rv, tc) = ECALL_FUNCS[func_id as usize](&args).into_tvm_value();
unsafe {
*ret_val = rv;
*ret_type_code = tc;
}
}
}
{
"arch": "x86_64",
"cpu": "x86-64",
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
"dynamic-linking": true,
"env": "sgx",
"exe-allocation-crate": "alloc_system",
"executables": true,
"has-elf-tls": true,
"has-rpath": true,
"linker-flavor": "gcc",
"linker-is-gnu": true,
"llvm-target": "x86_64-unknown-linux-gnu",
"max-atomic-width": 64,
"os": "linux",
"position-independent-executables": true,
"pre-link-args": {
"gcc": [
"-Wl,--as-needed",
"-Wl,-z,noexecstack",
"-m64"
]
},
"relro-level": "full",
"stack-probes": true,
"target-c-int-width": "32",
"target-endian": "little",
"target-family": "unix",
"target-pointer-width": "64",
"vendor": "unknown"
}
...@@ -15,16 +15,14 @@ ...@@ -15,16 +15,14 @@
# specific language governing permissions and limitations # specific language governing permissions and limitations
# under the License. # under the License.
[dependencies] import struct
alloc = {} import sys
panic_unwind = {}
panic_abort = {}
[dependencies.std] import numpy as np
path = "/opt/rust-sgx-sdk/xargo/sgx_tstd"
features = ["backtrace", "stdio", "untrusted_time"]
stage = 2
[dependencies.xargo_sgx_rand] def float_bytes(l):
path = "/opt/rust-sgx-sdk/xargo/sgx_rand" for i in range(0, len(l), 4):
stage = 3 yield l[i:i + 4]
floats = [struct.unpack('f', f)[0] for f in float_bytes(sys.stdin.buffer.read())]
print(np.array(floats))
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
import os.path as osp
import numpy as np
import tvm
from tvm import te
CWD = osp.abspath(osp.dirname(__file__))
def main():
ctx = tvm.context('cpu', 0)
model = tvm.runtime.load_module(osp.join(CWD, 'build', 'enclave.signed.so'))
inp = tvm.nd.array(np.ones((1, 3, 224, 224), dtype='float32'), ctx)
out = tvm.nd.array(np.empty((1, 1000), dtype='float32'), ctx)
model(inp, out)
if abs(out.asnumpy().sum() - 1) < 0.001:
print('It works!')
else:
print('It doesn\'t work!')
exit(1)
if __name__ == '__main__':
main()
#!/usr/bin/python3
# Licensed to the Apache Software Foundation (ASF) under one # Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file # or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information # distributed with this work for additional information
...@@ -14,11 +16,12 @@ ...@@ -14,11 +16,12 @@
# KIND, either express or implied. See the License for the # KIND, either express or implied. See the License for the
# specific language governing permissions and limitations # specific language governing permissions and limitations
# under the License. # under the License.
"""Creates a simple TVM modules.""" """Creates a simple TVM modules."""
import argparse
import os import os
from os import path as osp from os import path as osp
import sys
from tvm import relay from tvm import relay
from tvm.relay import testing from tvm.relay import testing
...@@ -27,9 +30,8 @@ from tvm import te ...@@ -27,9 +30,8 @@ from tvm import te
def main(): def main():
parser = argparse.ArgumentParser() dshape = (1, 28, 28)
parser.add_argument('-o', '--out-dir', default='.') net, params = relay.testing.mlp.get_workload(batch_size=dshape[0], dtype='float32')
opts = parser.parse_args()
dshape = (1, 3, 224, 224) dshape = (1, 3, 224, 224)
net, params = relay.testing.resnet.get_workload( net, params = relay.testing.resnet.get_workload(
...@@ -39,11 +41,11 @@ def main(): ...@@ -39,11 +41,11 @@ def main():
graph, lib, params = relay.build( graph, lib, params = relay.build(
net, 'llvm --system-lib', params=params) net, 'llvm --system-lib', params=params)
build_dir = osp.abspath(opts.out_dir) build_dir = osp.abspath(sys.argv[1])
if not osp.isdir(build_dir): if not osp.isdir(build_dir):
os.makedirs(build_dir, exist_ok=True) os.makedirs(build_dir, exist_ok=True)
lib.save(osp.join(build_dir, 'model.bc')) lib.save(osp.join(build_dir, 'model.o'))
with open(osp.join(build_dir, 'graph.json'), 'w') as f_graph_json: with open(osp.join(build_dir, 'graph.json'), 'w') as f_graph_json:
f_graph_json.write(graph) f_graph_json.write(graph)
with open(osp.join(build_dir, 'params.bin'), 'wb') as f_params: with open(osp.join(build_dir, 'params.bin'), 'wb') as f_params:
......
...@@ -17,12 +17,35 @@ ...@@ -17,12 +17,35 @@
* under the License. * under the License.
*/ */
use std::env; extern crate tvm_runtime;
use std::{
convert::TryFrom as _,
io::{Read as _, Write as _},
};
fn main() { fn main() {
println!( let syslib = tvm_runtime::SystemLibModule::default();
"cargo:rustc-link-search=native={}",
env::var("BUILD_DIR").unwrap() let graph_json = include_str!(concat!(env!("OUT_DIR"), "/graph.json"));
); let params_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/params.bin"));
println!("cargo:rustc-link-lib=static=model"); let params = tvm_runtime::load_param_dict(params_bytes).unwrap();
let graph = tvm_runtime::Graph::try_from(graph_json).unwrap();
let mut exec = tvm_runtime::GraphExecutor::new(graph, &syslib).unwrap();
exec.load_params(params);
let listener = std::net::TcpListener::bind("127.0.0.1:4242").unwrap();
for stream in listener.incoming() {
let mut stream = stream.unwrap();
if let Err(_) =
stream.read_exact(exec.get_input("data").unwrap().data().view().as_mut_slice())
{
continue;
}
exec.run();
if let Err(_) = stream.write_all(exec.get_output(0).unwrap().data().as_slice()) {
continue;
}
}
} }
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
if(NOT USE_SGX STREQUAL "OFF")
set(_sgx_src ${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/sgx)
set(_tvm_u_h ${_sgx_src}/untrusted/tvm_u.h)
set(_tvm_t_h ${_sgx_src}/trusted/tvm_t.h)
set(_tvm_t_c ${_sgx_src}/trusted/tvm_t.c)
set(_tvm_edl ${_sgx_src}/tvm.edl)
set(_sgx_ustdc ${RUST_SGX_SDK}/sgx_ustdc)
set(_urts_lib "sgx_urts")
if(NOT SGX_MODE STREQUAL "HW")
message(STATUS "Build with SGX support (SIM)")
set(_urts_lib "${_urts_lib}_sim")
else()
message(STATUS "Build with SGX support (HW)")
endif()
# build edge routines
add_custom_command(
OUTPUT ${_tvm_u_h}
COMMAND ${USE_SGX}/bin/x64/sgx_edger8r --untrusted
--untrusted --untrusted-dir ${_sgx_src}/untrusted
--trusted --trusted-dir ${_sgx_src}/trusted
--search-path ${USE_SGX}/include --search-path ${RUST_SGX_SDK}/edl
${_tvm_edl}
COMMAND sed -i "4i '#include <tvm/runtime/c_runtime_api.h>'" ${_tvm_u_h}
COMMAND sed -i "4i '#include <tvm/runtime/c_runtime_api.h>'" ${_tvm_t_h}
DEPENDS ${_tvm_edl}
)
add_custom_command(
OUTPUT ${_sgx_ustdc}/libsgx_ustdc.a
COMMAND make
WORKING_DIRECTORY ${_sgx_ustdc}
)
add_custom_target(sgx_edl DEPENDS ${_tvm_u_h} ${_sgx_ustdc}/libsgx_ustdc.a)
# build trusted library
set_source_files_properties(${_tvm_t_c} PROPERTIES GENERATED TRUE)
add_library(tvm_t STATIC ${_tvm_t_c})
add_dependencies(tvm_t sgx_edl)
target_include_directories(tvm_t PUBLIC ${USE_SGX}/include ${USE_SGX}/include/tlibc)
# add untrusted runtime files
include_directories(${USE_SGX}/include)
file(GLOB RUNTIME_SGX_SRCS ${_sgx_src}/untrusted/*.c*)
list(APPEND TVM_RUNTIME_LINKER_LIBS
-lpthread
-L${USE_SGX}/lib64 -l${_urts_lib}
-L${RUST_SGX_SDK}/sgx_ustdc -lsgx_ustdc)
list(APPEND RUNTIME_SRCS ${RUNTIME_SGX_SRCS})
include_directories(${RUST_SGX_SDK}/edl ${RUST_SGX_SDK}/common)
endif()
...@@ -31,8 +31,6 @@ echo set\(USE_RPC ON\) >> config.cmake ...@@ -31,8 +31,6 @@ echo set\(USE_RPC ON\) >> config.cmake
echo set\(USE_SORT ON\) >> config.cmake echo set\(USE_SORT ON\) >> config.cmake
echo set\(USE_GRAPH_RUNTIME ON\) >> config.cmake echo set\(USE_GRAPH_RUNTIME ON\) >> config.cmake
echo set\(USE_BLAS openblas\) >> config.cmake echo set\(USE_BLAS openblas\) >> config.cmake
echo set\(USE_SGX /opt/sgxsdk\) >> config.cmake
echo set\(RUST_SGX_SDK /opt/rust-sgx-sdk\) >> config.cmake
mkdir -p build mkdir -p build
cd build cd build
cmake .. cmake ..
......
#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
set -e
set -u
set -o pipefail
apt-get update && apt-get install -y --no-install-recommends \
build-essential git cmake \
wget python pkg-config software-properties-common \
autoconf automake libtool ocaml \
protobuf-compiler libprotobuf-dev \
libssl-dev libcurl4-openssl-dev curl
git clone --branch=sgx_2.2 --depth=1 https://github.com/intel/linux-sgx.git
cd linux-sgx
curl -s -S -L 'https://gist.githubusercontent.com/nhynes/c770b0e91610f8c020a8d1a803a1e7cb/raw/8f5372d9cb88929b3cc49a384943bb363bc06827/intel-sgx.patch' | git apply
./download_prebuilt.sh
make -j4 sdk && make -j4 sdk_install_pkg
./linux/installer/bin/sgx_linux_x64_sdk*.bin --prefix /opt
cd -
git clone --branch=v1.0.5 --depth=1 https://github.com/baidu/rust-sgx-sdk.git /opt/rust-sgx-sdk
cd /opt/rust-sgx-sdk
curl -s -S -L 'https://gist.githubusercontent.com/nhynes/37164039c5d3f33aa4f123e4ba720036/raw/b0de575fe937231799930764e76c664b92975163/rust-sgx-sdk.diff' | git apply
cd -
...@@ -221,7 +221,6 @@ inline const char* DeviceName(int type) { ...@@ -221,7 +221,6 @@ inline const char* DeviceName(int type) {
} }
} }
#ifndef _LIBCPP_SGX_NO_IOSTREAMS
inline std::ostream& operator<<(std::ostream& os, DLContext ctx) { // NOLINT(*) inline std::ostream& operator<<(std::ostream& os, DLContext ctx) { // NOLINT(*)
int device_type = static_cast<int>(ctx.device_type); int device_type = static_cast<int>(ctx.device_type);
if (device_type > kRPCSessMask) { if (device_type > kRPCSessMask) {
...@@ -231,8 +230,6 @@ inline std::ostream& operator<<(std::ostream& os, DLContext ctx) { // NOLINT(*) ...@@ -231,8 +230,6 @@ inline std::ostream& operator<<(std::ostream& os, DLContext ctx) { // NOLINT(*)
os << runtime::DeviceName(device_type) << "(" << ctx.device_id << ")"; os << runtime::DeviceName(device_type) << "(" << ctx.device_id << ")";
return os; return os;
} }
#endif
} // namespace runtime } // namespace runtime
} // namespace tvm } // namespace tvm
#endif // TVM_RUNTIME_DEVICE_API_H_ #endif // TVM_RUNTIME_DEVICE_API_H_
...@@ -24,9 +24,6 @@ ...@@ -24,9 +24,6 @@
#ifndef TVM_RUNTIME_PACKED_FUNC_H_ #ifndef TVM_RUNTIME_PACKED_FUNC_H_
#define TVM_RUNTIME_PACKED_FUNC_H_ #define TVM_RUNTIME_PACKED_FUNC_H_
#ifndef _LIBCPP_SGX_NO_IOSTREAMS
#include <sstream>
#endif
#include <dmlc/logging.h> #include <dmlc/logging.h>
#include <tvm/runtime/c_runtime_api.h> #include <tvm/runtime/c_runtime_api.h>
#include <tvm/runtime/module.h> #include <tvm/runtime/module.h>
...@@ -1019,7 +1016,6 @@ inline const char* TypeCode2Str(int type_code) { ...@@ -1019,7 +1016,6 @@ inline const char* TypeCode2Str(int type_code) {
} }
} }
#ifndef _LIBCPP_SGX_NO_IOSTREAMS
inline std::ostream& operator<<(std::ostream& os, DLDataType t) { // NOLINT(*) inline std::ostream& operator<<(std::ostream& os, DLDataType t) { // NOLINT(*)
if (t.bits == 1 && t.lanes == 1 && t.code == kDLUInt) { if (t.bits == 1 && t.lanes == 1 && t.code == kDLUInt) {
os << "bool"; return os; os << "bool"; return os;
...@@ -1041,30 +1037,11 @@ inline std::ostream& operator<<(std::ostream& os, const DataType& dtype) { // NO ...@@ -1041,30 +1037,11 @@ inline std::ostream& operator<<(std::ostream& os, const DataType& dtype) { // NO
return os << dtype.operator DLDataType(); return os << dtype.operator DLDataType();
} }
#endif
inline std::string DLDataType2String(DLDataType t) { inline std::string DLDataType2String(DLDataType t) {
if (t.bits == 0) return ""; if (t.bits == 0) return "";
#ifndef _LIBCPP_SGX_NO_IOSTREAMS
std::ostringstream os; std::ostringstream os;
os << t; os << t;
return os.str(); return os.str();
#else
if (t.bits == 1 && t.lanes == 1 && t.code == kDLUInt) {
return "bool";
}
if (t.code < kTVMCustomBegin) {
repr += TypeCode2Str(t.code);
} else {
repr += "custom[" + GetCustomTypeName(t.code) + "]";
}
if (t.code == kTVMOpaqueHandle) return repr;
repr += std::to_string(static_cast<int>(t.bits));
if (t.lanes != 1) {
repr += "x" + std::to_string(static_cast<int>(t.lanes));
}
return repr;
#endif
} }
inline DLDataType String2DLDataType(std::string s) { inline DLDataType String2DLDataType(std::string s) {
......
...@@ -22,8 +22,10 @@ newline_style = "Auto" ...@@ -22,8 +22,10 @@ newline_style = "Auto"
use_small_heuristics = "Default" use_small_heuristics = "Default"
indent_style = "Block" indent_style = "Block"
wrap_comments = false wrap_comments = false
format_code_in_doc_comments = false
comment_width = 80 comment_width = 80
normalize_comments = false normalize_comments = false
normalize_doc_attributes = false
format_strings = false format_strings = false
format_macro_matchers = false format_macro_matchers = false
format_macro_bodies = true format_macro_bodies = true
...@@ -44,10 +46,12 @@ spaces_around_ranges = false ...@@ -44,10 +46,12 @@ spaces_around_ranges = false
binop_separator = "Front" binop_separator = "Front"
remove_nested_parens = true remove_nested_parens = true
combine_control_expr = true combine_control_expr = true
overflow_delimited_expr = false
struct_field_align_threshold = 0 struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = true match_arm_blocks = true
force_multiline_blocks = false force_multiline_blocks = false
fn_args_density = "Tall" fn_args_layout = "Tall"
brace_style = "SameLineWhere" brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine" control_brace_style = "AlwaysSameLine"
trailing_semicolon = true trailing_semicolon = true
...@@ -56,8 +60,10 @@ match_block_trailing_comma = false ...@@ -56,8 +60,10 @@ match_block_trailing_comma = false
blank_lines_upper_bound = 1 blank_lines_upper_bound = 1
blank_lines_lower_bound = 0 blank_lines_lower_bound = 0
edition = "2018" edition = "2018"
version = "One"
inline_attribute_width = 0
merge_derives = true merge_derives = true
use_try_shorthand = true use_try_shorthand = false
use_field_init_shorthand = false use_field_init_shorthand = false
force_explicit_abi = true force_explicit_abi = true
condense_wildcard_suffixes = false condense_wildcard_suffixes = false
...@@ -66,8 +72,8 @@ unstable_features = false ...@@ -66,8 +72,8 @@ unstable_features = false
disable_all_formatting = false disable_all_formatting = false
skip_children = false skip_children = false
hide_parse_errors = false hide_parse_errors = false
error_on_line_overflow = true error_on_line_overflow = false
error_on_unformatted = true error_on_unformatted = false
report_todo = "Never" report_todo = "Never"
report_fixme = "Never" report_fixme = "Never"
ignore = [] ignore = []
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
members = [ members = [
"common", "common",
"macros", "macros",
"macros_raw",
"runtime", "runtime",
"runtime/tests/test_tvm_basic", "runtime/tests/test_tvm_basic",
"runtime/tests/test_tvm_dso", "runtime/tests/test_tvm_dso",
......
...@@ -26,8 +26,8 @@ edition = "2018" ...@@ -26,8 +26,8 @@ edition = "2018"
bindings = [] bindings = []
[dependencies] [dependencies]
failure = "0.1.5" failure = { version = "0.1", default-features = false, features = ["derive"] }
ndarray = "0.12.1" ndarray = "0.12"
[build-dependencies] [build-dependencies]
bindgen = "0.37.4" bindgen = "0.51"
...@@ -23,10 +23,10 @@ use std::path::PathBuf; ...@@ -23,10 +23,10 @@ use std::path::PathBuf;
fn main() { fn main() {
let tvm_home = option_env!("TVM_HOME").map(str::to_string).unwrap_or({ let tvm_home = option_env!("TVM_HOME").map(str::to_string).unwrap_or({
let tvm_home = PathBuf::from(env!("CARGO_MANIFEST_DIR")) let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.canonicalize() .canonicalize()
.unwrap(); .unwrap();
tvm_home crate_dir
.parent() .parent()
.unwrap() .unwrap()
.parent() .parent()
...@@ -46,6 +46,7 @@ fn main() { ...@@ -46,6 +46,7 @@ fn main() {
.header(format!("{}/include/tvm/runtime/c_runtime_api.h", tvm_home)) .header(format!("{}/include/tvm/runtime/c_runtime_api.h", tvm_home))
.header(format!("{}/include/tvm/runtime/c_backend_api.h", tvm_home)) .header(format!("{}/include/tvm/runtime/c_backend_api.h", tvm_home))
.clang_arg(format!("-I{}/3rdparty/dlpack/include/", tvm_home)) .clang_arg(format!("-I{}/3rdparty/dlpack/include/", tvm_home))
.clang_arg(format!("-I{}/include/", tvm_home))
.blacklist_type("max_align_t") .blacklist_type("max_align_t")
.layout_tests(false) .layout_tests(false)
.derive_partialeq(true) .derive_partialeq(true)
......
...@@ -20,8 +20,6 @@ ...@@ -20,8 +20,6 @@
//! This crate contains the refactored basic components required //! This crate contains the refactored basic components required
//! for `runtime` and `frontend` TVM crates. //! for `runtime` and `frontend` TVM crates.
#![feature(box_syntax, trait_alias)]
#[macro_use] #[macro_use]
extern crate failure; extern crate failure;
...@@ -44,5 +42,5 @@ pub mod packed_func; ...@@ -44,5 +42,5 @@ pub mod packed_func;
pub mod value; pub mod value;
pub use errors::*; pub use errors::*;
pub use ffi::{TVMByteArray, TVMContext, TVMType}; pub use ffi::{TVMByteArray, TVMContext, DLDataType as TVMType};
pub use packed_func::{TVMArgValue, TVMRetValue}; pub use packed_func::{TVMArgValue, TVMRetValue};
...@@ -26,8 +26,10 @@ use std::{ ...@@ -26,8 +26,10 @@ use std::{
pub use crate::ffi::TVMValue; pub use crate::ffi::TVMValue;
use crate::{errors::ValueDowncastError, ffi::*}; use crate::{errors::ValueDowncastError, ffi::*};
pub trait PackedFunc = pub trait PackedFunc : Fn(&[TVMArgValue]) -> Result<TVMRetValue, crate::errors::FuncCallError> + Send + Sync {}
Fn(&[TVMArgValue]) -> Result<TVMRetValue, crate::errors::FuncCallError> + Send + Sync;
impl<T> PackedFunc for T
where T : Fn(&[TVMArgValue]) -> Result<TVMRetValue, crate::errors::FuncCallError> + Send + Sync {}
/// Calls a packed function and returns a `TVMRetValue`. /// Calls a packed function and returns a `TVMRetValue`.
/// ///
...@@ -66,7 +68,7 @@ macro_rules! TVMPODValue { ...@@ -66,7 +68,7 @@ macro_rules! TVMPODValue {
UInt(i64), UInt(i64),
Float(f64), Float(f64),
Null, Null,
Type(TVMType), DataType(DLDataType),
String(CString), String(CString),
Context(TVMContext), Context(TVMContext),
Handle(*mut c_void), Handle(*mut c_void),
...@@ -87,15 +89,15 @@ macro_rules! TVMPODValue { ...@@ -87,15 +89,15 @@ macro_rules! TVMPODValue {
DLDataTypeCode_kDLInt => Int($value.v_int64), DLDataTypeCode_kDLInt => Int($value.v_int64),
DLDataTypeCode_kDLUInt => UInt($value.v_int64), DLDataTypeCode_kDLUInt => UInt($value.v_int64),
DLDataTypeCode_kDLFloat => Float($value.v_float64), DLDataTypeCode_kDLFloat => Float($value.v_float64),
TVMTypeCode_kNull => Null, TVMTypeCode_kTVMNullptr => Null,
TVMTypeCode_kTVMType => Type($value.v_type), TVMTypeCode_kTVMDataType => DataType($value.v_type),
TVMTypeCode_kTVMContext => Context($value.v_ctx), TVMTypeCode_kTVMContext => Context($value.v_ctx),
TVMTypeCode_kHandle => Handle($value.v_handle), TVMTypeCode_kTVMOpaqueHandle => Handle($value.v_handle),
TVMTypeCode_kArrayHandle => ArrayHandle($value.v_handle as TVMArrayHandle), TVMTypeCode_kTVMDLTensorHandle => ArrayHandle($value.v_handle as TVMArrayHandle),
TVMTypeCode_kObjectHandle => ObjectHandle($value.v_handle), TVMTypeCode_kTVMObjectHandle => ObjectHandle($value.v_handle),
TVMTypeCode_kModuleHandle => ModuleHandle($value.v_handle), TVMTypeCode_kTVMModuleHandle => ModuleHandle($value.v_handle),
TVMTypeCode_kFuncHandle => FuncHandle($value.v_handle), TVMTypeCode_kTVMPackedFuncHandle => FuncHandle($value.v_handle),
TVMTypeCode_kNDArrayContainer => NDArrayContainer($value.v_handle), TVMTypeCode_kTVMNDArrayHandle => NDArrayContainer($value.v_handle),
$( $tvm_type => { $from_tvm_type } ),+ $( $tvm_type => { $from_tvm_type } ),+
_ => unimplemented!("{}", type_code), _ => unimplemented!("{}", type_code),
} }
...@@ -108,31 +110,31 @@ macro_rules! TVMPODValue { ...@@ -108,31 +110,31 @@ macro_rules! TVMPODValue {
Int(val) => (TVMValue { v_int64: *val }, DLDataTypeCode_kDLInt), Int(val) => (TVMValue { v_int64: *val }, DLDataTypeCode_kDLInt),
UInt(val) => (TVMValue { v_int64: *val as i64 }, DLDataTypeCode_kDLUInt), UInt(val) => (TVMValue { v_int64: *val as i64 }, DLDataTypeCode_kDLUInt),
Float(val) => (TVMValue { v_float64: *val }, DLDataTypeCode_kDLFloat), Float(val) => (TVMValue { v_float64: *val }, DLDataTypeCode_kDLFloat),
Null => (TVMValue{ v_int64: 0 },TVMTypeCode_kNull), Null => (TVMValue{ v_int64: 0 },TVMTypeCode_kTVMNullptr),
Type(val) => (TVMValue { v_type: *val }, TVMTypeCode_kTVMType), DataType(val) => (TVMValue { v_type: *val }, TVMTypeCode_kTVMDataType),
Context(val) => (TVMValue { v_ctx: val.clone() }, TVMTypeCode_kTVMContext), Context(val) => (TVMValue { v_ctx: val.clone() }, TVMTypeCode_kTVMContext),
String(val) => { String(val) => {
( (
TVMValue { v_handle: val.as_ptr() as *mut c_void }, TVMValue { v_handle: val.as_ptr() as *mut c_void },
TVMTypeCode_kStr, TVMTypeCode_kTVMStr,
) )
} }
Handle(val) => (TVMValue { v_handle: *val }, TVMTypeCode_kHandle), Handle(val) => (TVMValue { v_handle: *val }, TVMTypeCode_kTVMOpaqueHandle),
ArrayHandle(val) => { ArrayHandle(val) => {
( (
TVMValue { v_handle: *val as *const _ as *mut c_void }, TVMValue { v_handle: *val as *const _ as *mut c_void },
TVMTypeCode_kArrayHandle, TVMTypeCode_kTVMNDArrayHandle,
) )
}, },
ObjectHandle(val) => (TVMValue { v_handle: *val }, TVMTypeCode_kObjectHandle), ObjectHandle(val) => (TVMValue { v_handle: *val }, TVMTypeCode_kTVMObjectHandle),
ModuleHandle(val) => ModuleHandle(val) =>
(TVMValue { v_handle: *val }, TVMTypeCode_kModuleHandle), (TVMValue { v_handle: *val }, TVMTypeCode_kTVMModuleHandle),
FuncHandle(val) => ( FuncHandle(val) => (
TVMValue { v_handle: *val }, TVMValue { v_handle: *val },
TVMTypeCode_kFuncHandle TVMTypeCode_kTVMPackedFuncHandle
), ),
NDArrayContainer(val) => NDArrayContainer(val) =>
(TVMValue { v_handle: *val }, TVMTypeCode_kNDArrayContainer), (TVMValue { v_handle: *val }, TVMTypeCode_kTVMNDArrayHandle),
$( $self_type($val) => { $from_self_type } ),+ $( $self_type($val) => { $from_self_type } ),+
} }
} }
...@@ -148,14 +150,14 @@ TVMPODValue! { ...@@ -148,14 +150,14 @@ TVMPODValue! {
Str(&'a CStr), Str(&'a CStr),
}, },
match value { match value {
TVMTypeCode_kBytes => { Bytes(&*(value.v_handle as *const TVMByteArray)) } TVMTypeCode_kTVMBytes => { Bytes(&*(value.v_handle as *const TVMByteArray)) }
TVMTypeCode_kStr => { Str(CStr::from_ptr(value.v_handle as *const i8)) } TVMTypeCode_kTVMStr => { Str(CStr::from_ptr(value.v_handle as *const i8)) }
}, },
match &self { match &self {
Bytes(val) => { Bytes(val) => {
(TVMValue { v_handle: val.clone() as *const _ as *mut c_void }, TVMTypeCode_kBytes) (TVMValue { v_handle: val as *const _ as *mut c_void }, TVMTypeCode_kTVMBytes)
} }
Str(val) => { (TVMValue { v_handle: val.as_ptr() as *mut c_void }, TVMTypeCode_kStr) } Str(val) => { (TVMValue { v_handle: val.as_ptr() as *mut c_void }, TVMTypeCode_kTVMStr) }
} }
} }
...@@ -166,11 +168,14 @@ TVMPODValue! { ...@@ -166,11 +168,14 @@ TVMPODValue! {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// use std::convert::{TryFrom, TryInto};
/// use tvm_common::TVMRetValue;
///
/// let a = 42u32; /// let a = 42u32;
/// let b: i64 = TVMRetValue::from(a).try_into().unwrap(); /// let b: u32 = tvm_common::TVMRetValue::from(a).try_into().unwrap();
/// ///
/// let s = "hello, world!"; /// let s = "hello, world!";
/// let t: TVMRetValue = s.into(); /// let t: TVMRetValue = s.to_string().into();
/// assert_eq!(String::try_from(t).unwrap(), s); /// assert_eq!(String::try_from(t).unwrap(), s);
/// ``` /// ```
TVMRetValue { TVMRetValue {
...@@ -178,14 +183,14 @@ TVMPODValue! { ...@@ -178,14 +183,14 @@ TVMPODValue! {
Str(&'static CStr), Str(&'static CStr),
}, },
match value { match value {
TVMTypeCode_kBytes => { Bytes(*(value.v_handle as *const TVMByteArray)) } TVMTypeCode_kTVMBytes => { Bytes(*(value.v_handle as *const TVMByteArray)) }
TVMTypeCode_kStr => { Str(CStr::from_ptr(value.v_handle as *mut i8)) } TVMTypeCode_kTVMStr => { Str(CStr::from_ptr(value.v_handle as *mut i8)) }
}, },
match &self { match &self {
Bytes(val) => Bytes(val) =>
{ (TVMValue { v_handle: val as *const _ as *mut c_void }, TVMTypeCode_kBytes ) } { (TVMValue { v_handle: val as *const _ as *mut c_void }, TVMTypeCode_kTVMBytes ) }
Str(val) => Str(val) =>
{ (TVMValue { v_str: val.as_ptr() }, TVMTypeCode_kStr ) } { (TVMValue { v_str: val.as_ptr() }, TVMTypeCode_kTVMStr ) }
} }
} }
...@@ -251,7 +256,7 @@ macro_rules! impl_pod_value { ...@@ -251,7 +256,7 @@ macro_rules! impl_pod_value {
impl_pod_value!(Int, i64, [i8, i16, i32, i64, isize]); impl_pod_value!(Int, i64, [i8, i16, i32, i64, isize]);
impl_pod_value!(UInt, i64, [u8, u16, u32, u64, usize]); impl_pod_value!(UInt, i64, [u8, u16, u32, u64, usize]);
impl_pod_value!(Float, f64, [f32, f64]); impl_pod_value!(Float, f64, [f32, f64]);
impl_pod_value!(Type, TVMType, [TVMType]); impl_pod_value!(DataType, DLDataType, [DLDataType]);
impl_pod_value!(Context, TVMContext, [TVMContext]); impl_pod_value!(Context, TVMContext, [TVMContext]);
impl<'a> From<&'a str> for TVMArgValue<'a> { impl<'a> From<&'a str> for TVMArgValue<'a> {
......
...@@ -19,11 +19,9 @@ ...@@ -19,11 +19,9 @@
use std::{os::raw::c_char, str::FromStr}; use std::{os::raw::c_char, str::FromStr};
use failure::Error;
use crate::ffi::*; use crate::ffi::*;
impl TVMType { impl DLDataType {
fn new(type_code: u8, bits: u8, lanes: u16) -> Self { fn new(type_code: u8, bits: u8, lanes: u16) -> Self {
Self { Self {
code: type_code, code: type_code,
...@@ -33,25 +31,37 @@ impl TVMType { ...@@ -33,25 +31,37 @@ impl TVMType {
} }
} }
#[derive(Debug, Fail)]
pub enum ParseTvmTypeError {
#[fail(display = "invalid number: {}", _0)]
InvalidNumber(std::num::ParseIntError),
#[fail(display = "unknown type: {}", _0)]
UnknownType(String),
}
/// Implements TVMType conversion from `&str` of general format `{dtype}{bits}x{lanes}` /// Implements TVMType conversion from `&str` of general format `{dtype}{bits}x{lanes}`
/// such as "int32", "float32" or with lane "float32x1". /// such as "int32", "float32" or with lane "float32x1".
impl FromStr for TVMType { impl FromStr for DLDataType {
type Err = Error; type Err = ParseTvmTypeError;
fn from_str(type_str: &str) -> Result<Self, Self::Err> { fn from_str(type_str: &str) -> Result<Self, Self::Err> {
if type_str == "bool" { if type_str == "bool" {
return Ok(TVMType::new(1, 1, 1)); return Ok(DLDataType::new(1, 1, 1));
} }
let mut type_lanes = type_str.split("x"); let mut type_lanes = type_str.split('x');
let typ = type_lanes.next().expect("Missing dtype"); let typ = type_lanes.next().expect("Missing dtype");
let lanes = type_lanes let lanes = type_lanes
.next() .next()
.map(|l| <u16>::from_str_radix(l, 10)) .map(|l| <u16>::from_str_radix(l, 10))
.unwrap_or(Ok(1))?; .unwrap_or(Ok(1))
.map_err(ParseTvmTypeError::InvalidNumber)?;
let (type_name, bits) = match typ.find(char::is_numeric) { let (type_name, bits) = match typ.find(char::is_numeric) {
Some(idx) => { Some(idx) => {
let (name, bits_str) = typ.split_at(idx); let (name, bits_str) = typ.split_at(idx);
(name, u8::from_str_radix(bits_str, 10)?) (
name,
u8::from_str_radix(bits_str, 10).map_err(ParseTvmTypeError::InvalidNumber)?,
)
} }
None => (typ, 32), None => (typ, 32),
}; };
...@@ -61,14 +71,14 @@ impl FromStr for TVMType { ...@@ -61,14 +71,14 @@ impl FromStr for TVMType {
"uint" => 1, "uint" => 1,
"float" => 2, "float" => 2,
"handle" => 3, "handle" => 3,
_ => return Err(format_err!("Unknown type {}", type_name)), _ => return Err(ParseTvmTypeError::UnknownType(type_name.to_string())),
}; };
Ok(TVMType::new(type_code, bits, lanes)) Ok(DLDataType::new(type_code, bits, lanes))
} }
} }
impl std::fmt::Display for TVMType { impl std::fmt::Display for DLDataType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if self.bits == 1 && self.lanes == 1 { if self.bits == 1 && self.lanes == 1 {
return write!(f, "bool"); return write!(f, "bool");
...@@ -113,19 +123,23 @@ macro_rules! impl_pod_tvm_value { ...@@ -113,19 +123,23 @@ macro_rules! impl_pod_tvm_value {
impl_pod_tvm_value!(v_int64, i64, i8, u8, i16, u16, i32, u32, i64, u64, isize, usize); impl_pod_tvm_value!(v_int64, i64, i8, u8, i16, u16, i32, u32, i64, u64, isize, usize);
impl_pod_tvm_value!(v_float64, f64, f32, f64); impl_pod_tvm_value!(v_float64, f64, f32, f64);
impl_pod_tvm_value!(v_type, TVMType); impl_pod_tvm_value!(v_type, DLDataType);
impl_pod_tvm_value!(v_ctx, TVMContext); impl_pod_tvm_value!(v_ctx, TVMContext);
#[derive(Debug, Fail)]
#[fail(display = "unsupported device: {}", _0)]
pub struct UnsupportedDeviceError(String);
macro_rules! impl_tvm_context { macro_rules! impl_tvm_context {
( $( $dev_type:ident : [ $( $dev_name:ident ),+ ] ),+ ) => { ( $( $dev_type:ident : [ $( $dev_name:ident ),+ ] ),+ ) => {
/// Creates a TVMContext from a string (e.g., "cpu", "gpu", "ext_dev") /// Creates a TVMContext from a string (e.g., "cpu", "gpu", "ext_dev")
impl FromStr for TVMContext { impl FromStr for TVMContext {
type Err = Error; type Err = UnsupportedDeviceError;
fn from_str(type_str: &str) -> Result<Self, Self::Err> { fn from_str(type_str: &str) -> Result<Self, Self::Err> {
Ok(Self { Ok(Self {
device_type: match type_str { device_type: match type_str {
$( $( stringify!($dev_name) )|+ => $dev_type ),+, $( $( stringify!($dev_name) )|+ => $dev_type ),+,
_ => return Err(format_err!("device {} not supported", type_str).into()), _ => return Err(UnsupportedDeviceError(type_str.to_string())),
}, },
device_id: 0, device_id: 0,
}) })
...@@ -163,7 +177,7 @@ impl_tvm_context!( ...@@ -163,7 +177,7 @@ impl_tvm_context!(
/// ///
/// ``` /// ```
/// let v = b"hello"; /// let v = b"hello";
/// let barr = TVMByteArray::from(&v); /// let barr = tvm_common::TVMByteArray::from(&v);
/// assert_eq!(barr.len(), v.len()); /// assert_eq!(barr.len(), v.len());
/// assert_eq!(barr.data(), &[104u8, 101, 108, 108, 111]); /// assert_eq!(barr.data(), &[104u8, 101, 108, 108, 111]);
/// ``` /// ```
...@@ -182,6 +196,10 @@ impl TVMByteArray { ...@@ -182,6 +196,10 @@ impl TVMByteArray {
pub fn to_vec(&self) -> Vec<u8> { pub fn to_vec(&self) -> Vec<u8> {
self.data().to_vec() self.data().to_vec()
} }
pub fn is_empty(&self) -> bool {
self.len() == 0
}
} }
// Needs AsRef for Vec // Needs AsRef for Vec
......
...@@ -28,16 +28,12 @@ categories = ["api-bindings", "science"] ...@@ -28,16 +28,12 @@ categories = ["api-bindings", "science"]
authors = ["TVM Contributors"] authors = ["TVM Contributors"]
edition = "2018" edition = "2018"
[lib]
name = "tvm_frontend"
crate-type = ["dylib"]
[dependencies] [dependencies]
failure = "0.1.5" failure = "0.1"
lazy_static = "1.1.0" lazy_static = "1.1"
ndarray = "0.12.1" ndarray = "0.12"
num-traits = "0.2" num-traits = "0.2"
tvm-common = { version = "0.1.0", path = "../common/", features = ["bindings"] } tvm-common = { version = "0.1", path = "../common/", features = ["bindings"] }
[features] [features]
blas = ["ndarray/blas"] blas = ["ndarray/blas"]
...@@ -23,7 +23,7 @@ license = "Apache-2.0" ...@@ -23,7 +23,7 @@ license = "Apache-2.0"
build = "build.rs" build = "build.rs"
[dependencies] [dependencies]
ndarray = "0.12.1" ndarray = "0.12"
tvm-frontend = { path = "../../" } tvm-frontend = { path = "../../" }
image = "0.20.1" image = "0.20"
csv = "1" csv = "1.1"
...@@ -65,7 +65,7 @@ fn main() { ...@@ -65,7 +65,7 @@ fn main() {
let input = NDArray::from_rust_ndarray( let input = NDArray::from_rust_ndarray(
&arr, &arr,
TVMContext::cpu(0), TVMContext::cpu(0),
TVMType::from_str("float32").unwrap(), DLDataType::from_str("float32").unwrap(),
) )
.unwrap(); .unwrap();
println!( println!(
...@@ -117,7 +117,7 @@ fn main() { ...@@ -117,7 +117,7 @@ fn main() {
let output = NDArray::empty( let output = NDArray::empty(
output_shape, output_shape,
TVMContext::cpu(0), TVMContext::cpu(0),
TVMType::from_str("float32").unwrap(), DLDataType::from_str("float32").unwrap(),
); );
// get the `get_output` function from runtime module // get the `get_output` function from runtime module
let ref get_output_fn = graph_runtime_module let ref get_output_fn = graph_runtime_module
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
use std::{ use std::{
collections::BTreeMap, collections::BTreeMap,
ffi::{CStr, CString}, ffi::{CStr, CString},
mem, mem::{self, MaybeUninit},
os::raw::{c_char, c_int, c_void}, os::raw::{c_char, c_int, c_void},
ptr, slice, str, ptr, slice, str,
sync::Mutex, sync::Mutex,
...@@ -36,25 +36,20 @@ use std::{ ...@@ -36,25 +36,20 @@ use std::{
use failure::Error; use failure::Error;
use crate::{ use crate::{errors, ffi, Module, TVMArgValue, TVMRetValue};
errors,
ffi::{self, TVMValue},
Module, TVMArgValue, TVMRetValue,
};
lazy_static! { lazy_static! {
static ref GLOBAL_FUNCTIONS: Mutex<BTreeMap<&'static str, Option<Function>>> = { static ref GLOBAL_FUNCTIONS: Mutex<BTreeMap<&'static str, Option<Function>>> = {
let mut out_size = 0 as c_int; let mut out_size = 0 as c_int;
let name = ptr::null_mut() as *mut c_char; let mut names_ptr = ptr::null_mut() as *mut *const c_char;
let mut out_array = name as *mut _;
check_call!(ffi::TVMFuncListGlobalNames( check_call!(ffi::TVMFuncListGlobalNames(
&mut out_size as *mut _, &mut out_size as *mut _,
&mut out_array &mut names_ptr as *mut _,
)); ));
let names_list = unsafe { slice::from_raw_parts(out_array, out_size as usize) }; let names_list = unsafe { slice::from_raw_parts(names_ptr, out_size as usize) };
Mutex::new( Mutex::new(
names_list names_list
.into_iter() .iter()
.map(|&p| (unsafe { CStr::from_ptr(p).to_str().unwrap() }, None)) .map(|&p| (unsafe { CStr::from_ptr(p).to_str().unwrap() }, None))
.collect(), .collect(),
) )
...@@ -80,7 +75,7 @@ unsafe impl Sync for Function {} ...@@ -80,7 +75,7 @@ unsafe impl Sync for Function {}
impl Function { impl Function {
pub(crate) fn new(handle: ffi::TVMFunctionHandle) -> Self { pub(crate) fn new(handle: ffi::TVMFunctionHandle) -> Self {
Function { Function {
handle: handle, handle,
is_global: false, is_global: false,
is_cloned: false, is_cloned: false,
} }
...@@ -98,15 +93,13 @@ impl Function { ...@@ -98,15 +93,13 @@ impl Function {
&mut handle as *mut _ &mut handle as *mut _
)); ));
maybe_func.replace(Function { maybe_func.replace(Function {
handle: handle, handle,
is_global: true, is_global: true,
is_cloned: false, is_cloned: false,
}); });
} }
unsafe { unsafe {
std::mem::transmute::<Option<&Function>, Option<&'static Function>>( mem::transmute::<Option<&Function>, Option<&'static Function>>(maybe_func.as_ref())
maybe_func.as_ref(),
)
} }
}) })
} }
...@@ -214,7 +207,7 @@ impl<'a, 'm> Builder<'a, 'm> { ...@@ -214,7 +207,7 @@ impl<'a, 'm> Builder<'a, 'm> {
let (mut values, mut type_codes): (Vec<ffi::TVMValue>, Vec<ffi::TVMTypeCode>) = let (mut values, mut type_codes): (Vec<ffi::TVMValue>, Vec<ffi::TVMTypeCode>) =
self.arg_buf.iter().map(|arg| arg.to_tvm_value()).unzip(); self.arg_buf.iter().map(|arg| arg.to_tvm_value()).unzip();
let mut ret_val = unsafe { std::mem::uninitialized::<TVMValue>() }; let mut ret_val = unsafe { MaybeUninit::uninit().assume_init() };
let mut ret_type_code = 0i32; let mut ret_type_code = 0i32;
check_call!(ffi::TVMFuncCall( check_call!(ffi::TVMFuncCall(
self.func.ok_or(errors::FunctionNotFoundError)?.handle, self.func.ok_or(errors::FunctionNotFoundError)?.handle,
...@@ -257,20 +250,20 @@ unsafe extern "C" fn tvm_callback( ...@@ -257,20 +250,20 @@ unsafe extern "C" fn tvm_callback(
let args_list = slice::from_raw_parts_mut(args, len); let args_list = slice::from_raw_parts_mut(args, len);
let type_codes_list = slice::from_raw_parts_mut(type_codes, len); let type_codes_list = slice::from_raw_parts_mut(type_codes, len);
let mut local_args: Vec<TVMArgValue> = Vec::new(); let mut local_args: Vec<TVMArgValue> = Vec::new();
let mut value = mem::uninitialized::<ffi::TVMValue>(); let mut value = MaybeUninit::uninit().assume_init();
let mut tcode = mem::uninitialized::<c_int>(); let mut tcode = MaybeUninit::uninit().assume_init();
let rust_fn = let rust_fn =
mem::transmute::<*mut c_void, fn(&[TVMArgValue]) -> Result<TVMRetValue, Error>>(fhandle); mem::transmute::<*mut c_void, fn(&[TVMArgValue]) -> Result<TVMRetValue, Error>>(fhandle);
for i in 0..len { for i in 0..len {
value = args_list[i]; value = args_list[i];
tcode = type_codes_list[i]; tcode = type_codes_list[i];
if tcode == ffi::TVMTypeCode_kObjectHandle as c_int if tcode == ffi::TVMTypeCode_kTVMObjectHandle as c_int
|| tcode == ffi::TVMTypeCode_kFuncHandle as c_int || tcode == ffi::TVMTypeCode_kTVMPackedFuncHandle as c_int
|| tcode == ffi::TVMTypeCode_kModuleHandle as c_int || tcode == ffi::TVMTypeCode_kTVMModuleHandle as c_int
{ {
check_call!(ffi::TVMCbArgToReturn(&mut value as *mut _, tcode)); check_call!(ffi::TVMCbArgToReturn(&mut value as *mut _, tcode));
} }
local_args.push(TVMArgValue::from_tvm_value(value.into(), tcode as u32)); local_args.push(TVMArgValue::from_tvm_value(value, tcode as u32));
} }
let rv = match rust_fn(local_args.as_slice()) { let rv = match rust_fn(local_args.as_slice()) {
...@@ -293,9 +286,9 @@ unsafe extern "C" fn tvm_callback( ...@@ -293,9 +286,9 @@ unsafe extern "C" fn tvm_callback(
} }
unsafe extern "C" fn tvm_callback_finalizer(fhandle: *mut c_void) { unsafe extern "C" fn tvm_callback_finalizer(fhandle: *mut c_void) {
let rust_fn = let _rust_fn =
mem::transmute::<*mut c_void, fn(&[TVMArgValue]) -> Result<TVMRetValue, Error>>(fhandle); mem::transmute::<*mut c_void, fn(&[TVMArgValue]) -> Result<TVMRetValue, Error>>(fhandle);
mem::drop(rust_fn); // XXX: give converted functions lifetimes so they're not called after use
} }
fn convert_to_tvm_func(f: fn(&[TVMArgValue]) -> Result<TVMRetValue, Error>) -> Function { fn convert_to_tvm_func(f: fn(&[TVMArgValue]) -> Result<TVMRetValue, Error>) -> Function {
......
...@@ -30,8 +30,6 @@ ...@@ -30,8 +30,6 @@
//! //!
//! Checkout the `examples` repository for more details. //! Checkout the `examples` repository for more details.
#![feature(box_syntax)]
#[macro_use] #[macro_use]
extern crate failure; extern crate failure;
#[macro_use] #[macro_use]
...@@ -55,7 +53,7 @@ pub use crate::{ ...@@ -55,7 +53,7 @@ pub use crate::{
ndarray::NDArray, ndarray::NDArray,
tvm_common::{ tvm_common::{
errors as common_errors, errors as common_errors,
ffi::{self, TVMByteArray, TVMType}, ffi::{self, TVMByteArray, DLDataType},
packed_func::{TVMArgValue, TVMRetValue}, packed_func::{TVMArgValue, TVMRetValue},
}, },
}; };
......
...@@ -32,7 +32,7 @@ use tvm_common::ffi; ...@@ -32,7 +32,7 @@ use tvm_common::ffi;
use crate::{errors, function::Function}; use crate::{errors, function::Function};
const ENTRY_FUNC: &'static str = "__tvm_main__"; const ENTRY_FUNC: &str = "__tvm_main__";
/// Wrapper around TVM module handle which contains an entry function. /// Wrapper around TVM module handle which contains an entry function.
/// The entry function can be applied to an imported module through [`entry_func`]. /// The entry function can be applied to an imported module through [`entry_func`].
...@@ -72,7 +72,7 @@ impl Module { ...@@ -72,7 +72,7 @@ impl Module {
ensure!( ensure!(
!fhandle.is_null(), !fhandle.is_null(),
errors::NullHandleError { errors::NullHandleError {
name: format!("{}", name.into_string()?) name: name.into_string()?.to_string()
} }
); );
Ok(Function::new(fhandle)) Ok(Function::new(fhandle))
...@@ -88,7 +88,7 @@ impl Module { ...@@ -88,7 +88,7 @@ impl Module {
let ext = CString::new( let ext = CString::new(
path.as_ref() path.as_ref()
.extension() .extension()
.unwrap_or(std::ffi::OsStr::new("")) .unwrap_or_else(|| std::ffi::OsStr::new(""))
.to_str() .to_str()
.ok_or_else(|| { .ok_or_else(|| {
format_err!("Bad module load path: `{}`.", path.as_ref().display()) format_err!("Bad module load path: `{}`.", path.as_ref().display())
......
...@@ -63,7 +63,7 @@ pub struct NDArray { ...@@ -63,7 +63,7 @@ pub struct NDArray {
impl NDArray { impl NDArray {
pub(crate) fn new(handle: ffi::TVMArrayHandle) -> Self { pub(crate) fn new(handle: ffi::TVMArrayHandle) -> Self {
NDArray { NDArray {
handle: handle, handle,
is_view: true, is_view: true,
} }
} }
...@@ -89,8 +89,7 @@ impl NDArray { ...@@ -89,8 +89,7 @@ impl NDArray {
/// Returns the total number of entries of the NDArray. /// Returns the total number of entries of the NDArray.
pub fn size(&self) -> Option<usize> { pub fn size(&self) -> Option<usize> {
self.shape() self.shape().map(|v| v.iter().product())
.map(|v| v.into_iter().fold(1, |acc, &mut e| acc * e))
} }
/// Returns the context which the NDArray was defined. /// Returns the context which the NDArray was defined.
...@@ -100,7 +99,7 @@ impl NDArray { ...@@ -100,7 +99,7 @@ impl NDArray {
/// Returns the type of the entries of the NDArray. /// Returns the type of the entries of the NDArray.
pub fn dtype(&self) -> TVMType { pub fn dtype(&self) -> TVMType {
unsafe { (*self.handle).dtype.into() } unsafe { (*self.handle).dtype }
} }
/// Returns the number of dimensions of the NDArray. /// Returns the number of dimensions of the NDArray.
...@@ -211,8 +210,8 @@ impl NDArray { ...@@ -211,8 +210,8 @@ impl NDArray {
bail!( bail!(
"{}", "{}",
errors::TypeMismatchError { errors::TypeMismatchError {
expected: format!("{}", self.dtype().to_string()), expected: self.dtype().to_string(),
actual: format!("{}", target.dtype().to_string()), actual: target.dtype().to_string(),
} }
); );
} }
...@@ -228,7 +227,7 @@ impl NDArray { ...@@ -228,7 +227,7 @@ impl NDArray {
pub fn copy_to_ctx(&self, target: &TVMContext) -> Result<NDArray, Error> { pub fn copy_to_ctx(&self, target: &TVMContext) -> Result<NDArray, Error> {
let tmp = NDArray::empty( let tmp = NDArray::empty(
self.shape().ok_or(errors::MissingShapeError)?, self.shape().ok_or(errors::MissingShapeError)?,
target.clone(), *target,
self.dtype(), self.dtype(),
); );
let copy = self.copy_to_ndarray(tmp)?; let copy = self.copy_to_ndarray(tmp)?;
...@@ -241,8 +240,8 @@ impl NDArray { ...@@ -241,8 +240,8 @@ impl NDArray {
ctx: TVMContext, ctx: TVMContext,
dtype: TVMType, dtype: TVMType,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
let mut shape = rnd.shape().to_vec(); let shape = rnd.shape().to_vec();
let mut nd = NDArray::empty(&mut shape, ctx, dtype); let mut nd = NDArray::empty(&shape, ctx, dtype);
let mut buf = Array::from_iter(rnd.into_iter().map(|&v| v as T)); let mut buf = Array::from_iter(rnd.into_iter().map(|&v| v as T));
nd.copy_from_buffer( nd.copy_from_buffer(
buf.as_slice_mut() buf.as_slice_mut()
...@@ -257,9 +256,9 @@ impl NDArray { ...@@ -257,9 +256,9 @@ impl NDArray {
check_call!(ffi::TVMArrayAlloc( check_call!(ffi::TVMArrayAlloc(
shape.as_ptr() as *const i64, shape.as_ptr() as *const i64,
shape.len() as c_int, shape.len() as c_int,
dtype.code as c_int, i32::from(dtype.code) as c_int,
dtype.bits as c_int, i32::from(dtype.bits) as c_int,
dtype.lanes as c_int, i32::from(dtype.lanes) as c_int,
ctx.device_type.0 as c_int, ctx.device_type.0 as c_int,
ctx.device_id as c_int, ctx.device_id as c_int,
&mut handle as *mut _, &mut handle as *mut _,
...@@ -364,9 +363,9 @@ mod tests { ...@@ -364,9 +363,9 @@ mod tests {
assert_eq!(ndarray.ndim(), 1); assert_eq!(ndarray.ndim(), 1);
assert!(ndarray.is_contiguous().is_ok()); assert!(ndarray.is_contiguous().is_ok());
assert_eq!(ndarray.byte_offset(), 0); assert_eq!(ndarray.byte_offset(), 0);
let mut shape = vec![4]; let shape = vec![4];
let e = NDArray::empty( let e = NDArray::empty(
&mut shape, &shape,
TVMContext::cpu(0), TVMContext::cpu(0),
TVMType::from_str("int32").unwrap(), TVMType::from_str("int32").unwrap(),
); );
...@@ -378,16 +377,12 @@ mod tests { ...@@ -378,16 +377,12 @@ mod tests {
#[test] #[test]
#[should_panic(expected = "called `Result::unwrap()` on an `Err`")] #[should_panic(expected = "called `Result::unwrap()` on an `Err`")]
fn copy_wrong_dtype() { fn copy_wrong_dtype() {
let mut shape = vec![4]; let shape = vec![4];
let mut data = vec![1f32, 2., 3., 4.]; let mut data = vec![1f32, 2., 3., 4.];
let ctx = TVMContext::cpu(0); let ctx = TVMContext::cpu(0);
let mut nd_float = NDArray::empty( let mut nd_float = NDArray::empty(&shape, ctx, TVMType::from_str("float32").unwrap());
&mut shape,
ctx.clone(),
TVMType::from_str("float32").unwrap(),
);
nd_float.copy_from_buffer(&mut data); nd_float.copy_from_buffer(&mut data);
let empty_int = NDArray::empty(&mut shape, ctx, TVMType::from_str("int32").unwrap()); let empty_int = NDArray::empty(&shape, ctx, TVMType::from_str("int32").unwrap());
nd_float.copy_to_ndarray(empty_int).unwrap(); nd_float.copy_to_ndarray(empty_int).unwrap();
} }
......
...@@ -93,7 +93,7 @@ mod tests { ...@@ -93,7 +93,7 @@ mod tests {
let tvm: TVMByteArray = TVMRetValue::from(v).try_into().unwrap(); let tvm: TVMByteArray = TVMRetValue::from(v).try_into().unwrap();
assert_eq!( assert_eq!(
tvm.data(), tvm.data(),
w.iter().map(|e| *e).collect::<Vec<u8>>().as_slice() w.iter().copied().collect::<Vec<u8>>().as_slice()
); );
} }
......
...@@ -23,7 +23,7 @@ license = "Apache-2.0" ...@@ -23,7 +23,7 @@ license = "Apache-2.0"
build = "build.rs" build = "build.rs"
[dependencies] [dependencies]
ndarray = "0.12.1" ndarray = "0.12"
tvm-frontend = { path = "../../" } tvm-frontend = { path = "../../" }
[features] [features]
......
...@@ -33,7 +33,7 @@ fn main() { ...@@ -33,7 +33,7 @@ fn main() {
} else { } else {
(TVMContext::gpu(0), "gpu") (TVMContext::gpu(0), "gpu")
}; };
let dtype = TVMType::from_str("float32").unwrap(); let dtype = DLDataType::from_str("float32").unwrap();
let mut arr = NDArray::empty(shape, ctx, dtype); let mut arr = NDArray::empty(shape, ctx, dtype);
arr.copy_from_buffer(data.as_mut_slice()); arr.copy_from_buffer(data.as_mut_slice());
let mut ret = NDArray::empty(shape, ctx, dtype); let mut ret = NDArray::empty(shape, ctx, dtype);
......
...@@ -21,5 +21,5 @@ version = "0.0.0" ...@@ -21,5 +21,5 @@ version = "0.0.0"
authors = ["TVM Contributors"] authors = ["TVM Contributors"]
[dependencies] [dependencies]
ndarray = "0.12.1" ndarray = "0.12"
tvm-frontend = { path = "../../" } tvm-frontend = { path = "../../" }
...@@ -39,7 +39,7 @@ fn main() { ...@@ -39,7 +39,7 @@ fn main() {
for arg in args.iter() { for arg in args.iter() {
let e = NDArray::empty( let e = NDArray::empty(
shape, TVMContext::cpu(0), shape, TVMContext::cpu(0),
TVMType::from_str("float32").unwrap() DLDataType::from_str("float32").unwrap()
); );
let arg: NDArray = arg.try_into()?; let arg: NDArray = arg.try_into()?;
let arr = arg.copy_to_ndarray(e)?; let arr = arg.copy_to_ndarray(e)?;
...@@ -55,7 +55,7 @@ fn main() { ...@@ -55,7 +55,7 @@ fn main() {
let mut arr = NDArray::empty( let mut arr = NDArray::empty(
shape, shape,
TVMContext::cpu(0), TVMContext::cpu(0),
TVMType::from_str("float32").unwrap(), DLDataType::from_str("float32").unwrap(),
); );
arr.copy_from_buffer(data.as_mut_slice()); arr.copy_from_buffer(data.as_mut_slice());
......
...@@ -17,9 +17,6 @@ ...@@ -17,9 +17,6 @@
* under the License. * under the License.
*/ */
#![feature(panic_info_message)]
#![allow(unused_imports)]
use std::panic; use std::panic;
#[macro_use] #[macro_use]
...@@ -44,9 +41,9 @@ fn main() { ...@@ -44,9 +41,9 @@ fn main() {
println!("expected error message is:"); println!("expected error message is:");
panic::set_hook(Box::new(|panic_info| { panic::set_hook(Box::new(|panic_info| {
if let Some(msg) = panic_info.message() { // if let Some(msg) = panic_info.message() {
println!("{:?}", msg); // println!("{:?}", msg);
} // }
if let Some(location) = panic_info.location() { if let Some(location) = panic_info.location() {
println!( println!(
"panic occurred in file '{}' at line {}", "panic occurred in file '{}' at line {}",
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
[package] [package]
name = "tvm-macros" name = "tvm-macros"
version = "0.1.0" version = "0.1.1"
license = "Apache-2.0" license = "Apache-2.0"
description = "Proc macros used by the TVM crates." description = "Proc macros used by the TVM crates."
repository = "https://github.com/apache/incubator-tvm" repository = "https://github.com/apache/incubator-tvm"
...@@ -26,11 +26,6 @@ keywords = ["tvm"] ...@@ -26,11 +26,6 @@ keywords = ["tvm"]
authors = ["TVM Contributors"] authors = ["TVM Contributors"]
edition = "2018" edition = "2018"
[lib]
proc-macro = true
[dependencies] [dependencies]
goblin = "0.0.22" tvm-macros-raw = { path = "../macros_raw" }
proc-macro2 = "0.4"
proc-quote = "0.2"
syn = "0.15"
...@@ -17,106 +17,12 @@ ...@@ -17,106 +17,12 @@
* under the License. * under the License.
*/ */
#![feature(proc_macro_span)] #[macro_use]
extern crate tvm_macros_raw;
extern crate proc_macro; #[macro_export]
macro_rules! import_module {
use std::{fs::File, io::Read}; ($module_path:literal) => {
$crate::import_module_raw!(file!(), $module_path);
use proc_quote::quote;
#[proc_macro]
pub fn import_module(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let obj_file_path = syn::parse_macro_input!(input as syn::LitStr);
let mut path = obj_file_path.span().unwrap().source_file().path();
path.pop(); // remove the filename
path.push(obj_file_path.value());
let mut fd = File::open(&path)
.unwrap_or_else(|_| panic!("Unable to find TVM object file at `{}`", path.display()));
let mut buffer = Vec::new();
fd.read_to_end(&mut buffer).unwrap();
let fn_names = match goblin::Object::parse(&buffer).unwrap() {
goblin::Object::Elf(elf) => elf
.syms
.iter()
.filter_map(|s| {
if s.st_type() == 0 || goblin::elf::sym::type_to_str(s.st_type()) == "FILE" {
return None;
}
match elf.strtab.get(s.st_name) {
Some(Ok(name)) if name != "" => {
Some(syn::Ident::new(name, proc_macro2::Span::call_site()))
}
_ => None,
}
})
.collect::<Vec<_>>(),
goblin::Object::Mach(goblin::mach::Mach::Binary(obj)) => {
obj.symbols()
.filter_map(|s| match s {
Ok((name, nlist))
if nlist.is_global()
&& nlist.n_sect != 0
&& !name.ends_with("tvm_module_ctx") =>
{
Some(syn::Ident::new(
if name.starts_with('_') {
// Mach objects prepend a _ to globals.
&name[1..]
} else {
&name
},
proc_macro2::Span::call_site(),
))
}
_ => None,
})
.collect::<Vec<_>>()
}
_ => panic!("Unsupported object format."),
}; };
let extern_fns = quote! {
mod ext {
extern "C" {
#(
pub(super) fn #fn_names(
args: *const tvm_runtime::ffi::TVMValue,
type_codes: *const std::os::raw::c_int,
num_args: std::os::raw::c_int
) -> std::os::raw::c_int;
)*
}
}
};
let fns = quote! {
use tvm_runtime::{ffi::TVMValue, TVMArgValue, TVMRetValue, FuncCallError};
#extern_fns
#(
pub fn #fn_names(args: &[TVMArgValue]) -> Result<TVMRetValue, FuncCallError> {
let (values, type_codes): (Vec<TVMValue>, Vec<i32>) = args
.into_iter()
.map(|arg| {
let (val, code) = arg.to_tvm_value();
(val, code as i32)
})
.unzip();
let exit_code = unsafe {
ext::#fn_names(values.as_ptr(), type_codes.as_ptr(), values.len() as i32)
};
if exit_code == 0 {
Ok(TVMRetValue::default())
} else {
Err(FuncCallError::get_with_context(stringify!(#fn_names).to_string()))
}
}
)*
};
proc_macro::TokenStream::from(fns)
} }
#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one # Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file # or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information # distributed with this work for additional information
...@@ -16,11 +15,22 @@ ...@@ -16,11 +15,22 @@
# specific language governing permissions and limitations # specific language governing permissions and limitations
# under the License. # under the License.
sgx_sdk=${SGX_SDK:=/opt/sgxsdk} [package]
name = "tvm-macros-raw"
version = "0.1.1"
license = "Apache-2.0"
description = "Proc macros used by the TVM crates."
repository = "https://github.com/apache/incubator-tvm"
readme = "README.md"
keywords = ["tvm"]
authors = ["TVM Contributors"]
edition = "2018"
export LD_LIBRARY_PATH="$sgx_sdk/lib64":${LD_LIBRARY_PATH} [lib]
export CC=clang-6.0 proc-macro = true
export AR=llvm-ar-6.0
export TVM_CACHE_DIR=/tmp
make && printf "\n" && python3 run_model.py [dependencies]
goblin = "0.0.24"
proc-macro2 = "^1.0"
quote = "1.0"
syn = "1.0"
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
extern crate proc_macro;
use std::{fs::File, io::Read};
use syn::parse::{Parse, ParseStream, Result};
use syn::{Token, LitStr};
use quote::quote;
use std::path::PathBuf;
struct ImportModule {
importing_file: LitStr,
module_path: LitStr,
}
impl Parse for ImportModule {
fn parse(input: ParseStream) -> Result<Self> {
let importing_file: LitStr = input.parse()?;
input.parse::<Token![,]>()?;
let module_path: LitStr = input.parse()?;
Ok(ImportModule {
importing_file,
module_path,
})
}
}
#[proc_macro]
pub fn import_module_raw(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let import_module_args = syn::parse_macro_input!(input as ImportModule);
let mut path = PathBuf::new();
path = path.join(import_module_args.importing_file.value());
path.pop(); // remove the filename
path.push(import_module_args.module_path.value());
let mut fd = File::open(&path)
.unwrap_or_else(|_| panic!("Unable to find TVM object file at `{}`", path.display()));
let mut buffer = Vec::new();
fd.read_to_end(&mut buffer).unwrap();
let fn_names = match goblin::Object::parse(&buffer).unwrap() {
goblin::Object::Elf(elf) => elf
.syms
.iter()
.filter_map(|s| {
if s.st_type() == 0 || goblin::elf::sym::type_to_str(s.st_type()) == "FILE" {
return None;
}
match elf.strtab.get(s.st_name) {
Some(Ok(name)) if name != "" => {
Some(syn::Ident::new(name, proc_macro2::Span::call_site()))
}
_ => None,
}
})
.collect::<Vec<_>>(),
goblin::Object::Mach(goblin::mach::Mach::Binary(obj)) => {
obj.symbols()
.filter_map(|s| match s {
Ok((name, ref nlist))
if nlist.is_global()
&& nlist.n_sect != 0
&& !name.ends_with("tvm_module_ctx") =>
{
Some(syn::Ident::new(
if name.starts_with('_') {
// Mach objects prepend a _ to globals.
&name[1..]
} else {
&name
},
proc_macro2::Span::call_site(),
))
}
_ => None,
})
.collect::<Vec<_>>()
}
_ => panic!("Unsupported object format."),
};
let extern_fns = quote! {
mod ext {
extern "C" {
#(
pub(super) fn #fn_names(
args: *const tvm_runtime::ffi::TVMValue,
type_codes: *const std::os::raw::c_int,
num_args: std::os::raw::c_int
) -> std::os::raw::c_int;
)*
}
}
};
let fns = quote! {
use tvm_runtime::{ffi::TVMValue, TVMArgValue, TVMRetValue, FuncCallError};
#extern_fns
#(
pub fn #fn_names(args: &[TVMArgValue]) -> Result<TVMRetValue, FuncCallError> {
let (values, type_codes): (Vec<TVMValue>, Vec<i32>) = args
.into_iter()
.map(|arg| {
let (val, code) = arg.to_tvm_value();
(val, code as i32)
})
.unzip();
let exit_code = unsafe {
ext::#fn_names(values.as_ptr(), type_codes.as_ptr(), values.len() as i32)
};
if exit_code == 0 {
Ok(TVMRetValue::default())
} else {
Err(FuncCallError::get_with_context(stringify!(#fn_names).to_string()))
}
}
)*
};
proc_macro::TokenStream::from(fns)
}
...@@ -27,25 +27,19 @@ categories = ["api-bindings", "science"] ...@@ -27,25 +27,19 @@ categories = ["api-bindings", "science"]
authors = ["TVM Contributors"] authors = ["TVM Contributors"]
edition = "2018" edition = "2018"
[features]
default = ["nom/std"]
sgx = ["nom/alloc"]
[dependencies] [dependencies]
bounded-spsc-queue = "0.4.0" crossbeam = "0.7.3"
failure = "0.1.5" failure = "0.1"
itertools = "0.7.8" itertools = "0.8"
lazy_static = "1.1.0" lazy_static = "1.4"
ndarray="0.12.1" ndarray="0.12"
nom = {version = "4.0.0", default-features = false } nom = "5.0"
serde = "1.0.59" num_cpus = "1.10"
serde_derive = "1.0.79" serde = "1.0"
serde_json = "1.0.17" serde_derive = "1.0"
serde_json = "1.0"
tvm-common = { version = "0.1", path = "../common" } tvm-common = { version = "0.1", path = "../common" }
tvm-macros = { version = "0.1", path = "../macros" } tvm-macros = { version = "0.1", path = "../macros" }
[target.'cfg(not(target_env = "sgx"))'.dependencies]
num_cpus = "1.8.0"
[target.'cfg(not(any(target_arch = "wasm32", target_env = "sgx")))'.dependencies] [target.'cfg(not(any(target_arch = "wasm32", target_env = "sgx")))'.dependencies]
libloading = "0.5" libloading = "0.5"
...@@ -17,9 +17,6 @@ ...@@ -17,9 +17,6 @@
* under the License. * under the License.
*/ */
#[cfg(target_env = "sgx")]
use alloc::alloc::{self, Layout, LayoutErr};
#[cfg(not(target_env = "sgx"))]
use std::alloc::{self, Layout, LayoutErr}; use std::alloc::{self, Layout, LayoutErr};
const DEFAULT_ALIGN_BYTES: usize = 4; const DEFAULT_ALIGN_BYTES: usize = 4;
...@@ -35,14 +32,11 @@ impl Allocation { ...@@ -35,14 +32,11 @@ impl Allocation {
pub fn new(size: usize, align: Option<usize>) -> Result<Self, LayoutErr> { pub fn new(size: usize, align: Option<usize>) -> Result<Self, LayoutErr> {
let alignment = align.unwrap_or(DEFAULT_ALIGN_BYTES); let alignment = align.unwrap_or(DEFAULT_ALIGN_BYTES);
let layout = Layout::from_size_align(size, alignment)?; let layout = Layout::from_size_align(size, alignment)?;
let ptr = unsafe { alloc::alloc(layout.clone()) }; let ptr = unsafe { alloc::alloc(layout) };
if ptr.is_null() { if ptr.is_null() {
alloc::handle_alloc_error(layout); alloc::handle_alloc_error(layout);
} }
Ok(Self { Ok(Self { ptr, layout })
ptr: ptr,
layout: layout,
})
} }
pub fn as_mut_ptr(&self) -> *mut u8 { pub fn as_mut_ptr(&self) -> *mut u8 {
...@@ -58,12 +52,22 @@ impl Allocation { ...@@ -58,12 +52,22 @@ impl Allocation {
pub fn align(&self) -> usize { pub fn align(&self) -> usize {
self.layout.align() self.layout.align()
} }
/// Returns a view of the Allocation.
pub fn as_slice(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.as_mut_ptr(), self.size()) }
}
/// Returns a mutable view of the Allocation.
pub fn as_mut_slice(&mut self) -> &mut [u8] {
unsafe { std::slice::from_raw_parts_mut(self.as_mut_ptr(), self.size()) }
}
} }
impl Drop for Allocation { impl Drop for Allocation {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
alloc::dealloc(self.ptr, self.layout.clone()); alloc::dealloc(self.ptr, self.layout);
} }
} }
} }
...@@ -101,6 +101,22 @@ impl<'a> Storage<'a> { ...@@ -101,6 +101,22 @@ impl<'a> Storage<'a> {
} }
s s
} }
/// Returns a view of the stored data.
pub fn as_slice(&self) -> &[u8] {
match self {
Storage::Owned(alloc) => alloc.as_slice(),
Storage::View(slice, _) => &*slice,
}
}
/// Returns a mutable view of the stored data.
pub fn as_mut_slice(&mut self) -> &mut [u8] {
match self {
Storage::Owned(alloc) => alloc.as_mut_slice(),
Storage::View(slice, _) => slice,
}
}
} }
impl<'d, 's, T> From<&'d [T]> for Storage<'s> { impl<'d, 's, T> From<&'d [T]> for Storage<'s> {
...@@ -123,14 +139,18 @@ impl<'d, 's, T> From<&'d [T]> for Storage<'s> { ...@@ -123,14 +139,18 @@ impl<'d, 's, T> From<&'d [T]> for Storage<'s> {
/// ///
/// ``` /// ```
/// extern crate ndarray; /// extern crate ndarray;
/// use std::convert::TryInto;
/// use tvm_runtime::{call_packed, DLTensor, TVMArgValue, TVMRetValue, Tensor};
/// ///
/// let mut a_nd: ndarray::Array = ndarray::Array::from_vec(vec![1f32, 2., 3., 4.]); /// let mut a_nd: ndarray::Array1<f32> = ndarray::Array::from_vec(vec![1f32, 2., 3., 4.]);
/// let mut a: Tensor = a_nd.into(); /// let mut a: Tensor = a_nd.into();
/// let mut a_dl: DLTensor = (&mut t).into(); /// let mut a_dl: DLTensor = (&mut a).into();
///
/// let tvm_fn = |args: &[TVMArgValue]| -> Result<TVMRetValue, ()> { Ok(TVMRetValue::default()) };
/// call_packed!(tvm_fn, &mut a_dl); /// call_packed!(tvm_fn, &mut a_dl);
/// ///
/// // Array -> Tensor is mostly useful when post-processing TVM graph outputs. /// // Array -> Tensor is mostly useful when post-processing TVM graph outputs.
/// let mut a_nd = ndarray::Array::try_from(&a).unwrap(); /// let mut a_nd: ndarray::ArrayD<f32> = a.try_into().unwrap();
/// ``` /// ```
#[derive(PartialEq)] #[derive(PartialEq)]
pub struct Tensor<'a> { pub struct Tensor<'a> {
...@@ -154,6 +174,14 @@ impl<'a> Tensor<'a> { ...@@ -154,6 +174,14 @@ impl<'a> Tensor<'a> {
self.shape.clone() self.shape.clone()
} }
pub fn data(&self) -> &Storage {
&self.data
}
pub fn data_mut(&mut self) -> &'a mut Storage {
&mut self.data
}
/// Returns the data of this `Tensor` as a `Vec`. /// Returns the data of this `Tensor` as a `Vec`.
/// ///
/// # Panics /// # Panics
...@@ -220,9 +248,9 @@ impl<'a> Tensor<'a> { ...@@ -220,9 +248,9 @@ impl<'a> Tensor<'a> {
pub fn to_owned(&self) -> Tensor<'static> { pub fn to_owned(&self) -> Tensor<'static> {
let t = Tensor { let t = Tensor {
data: self.data.to_owned(), data: self.data.to_owned(),
ctx: self.ctx.clone(), ctx: self.ctx,
dtype: self.dtype.clone(), dtype: self.dtype,
size: self.size.clone(), size: self.size,
shape: self.shape.clone(), shape: self.shape.clone(),
strides: None, strides: None,
byte_offset: 0, byte_offset: 0,
...@@ -246,7 +274,7 @@ impl<'a> Tensor<'a> { ...@@ -246,7 +274,7 @@ impl<'a> Tensor<'a> {
}, },
size: arr.len(), size: arr.len(),
shape: arr.shape().iter().map(|&v| v as i64).collect(), shape: arr.shape().iter().map(|&v| v as i64).collect(),
strides: Some(arr.strides().into_iter().map(|&v| v as usize).collect()), strides: Some(arr.strides().iter().map(|&v| v as usize).collect()),
byte_offset: 0, byte_offset: 0,
} }
} }
...@@ -276,9 +304,9 @@ impl<'a> Tensor<'a> { ...@@ -276,9 +304,9 @@ impl<'a> Tensor<'a> {
/// Conversions to `ndarray::Array` from `Tensor`, if the types match. /// Conversions to `ndarray::Array` from `Tensor`, if the types match.
macro_rules! impl_ndarray_try_from_tensor { macro_rules! impl_ndarray_try_from_tensor {
($type:ty, $dtype:expr) => { ($type:ty, $dtype:expr) => {
impl<'a, 't> TryFrom<&'a Tensor<'t>> for ndarray::ArrayD<$type> { impl<'t> TryFrom<Tensor<'t>> for ndarray::ArrayD<$type> {
type Error = Error; type Error = Error;
fn try_from(tensor: &'a Tensor) -> Result<ndarray::ArrayD<$type>, Error> { fn try_from(tensor: Tensor) -> Result<ndarray::ArrayD<$type>, Error> {
ensure!( ensure!(
tensor.dtype == $dtype, tensor.dtype == $dtype,
"Cannot convert Tensor with dtype {:?} to ndarray", "Cannot convert Tensor with dtype {:?} to ndarray",
...@@ -342,10 +370,10 @@ impl<'a> From<DLTensor> for Tensor<'a> { ...@@ -342,10 +370,10 @@ impl<'a> From<DLTensor> for Tensor<'a> {
Self { Self {
data: storage, data: storage,
ctx: TVMContext::default(), ctx: TVMContext::default(),
dtype: dtype, dtype,
size: size, size,
shape: shape, shape,
strides: if dlt.strides == ptr::null_mut() { strides: if dlt.strides.is_null() {
None None
} else { } else {
Some(slice::from_raw_parts_mut(dlt.strides as *mut usize, size).to_vec()) Some(slice::from_raw_parts_mut(dlt.strides as *mut usize, size).to_vec())
......
...@@ -30,9 +30,3 @@ pub enum GraphFormatError { ...@@ -30,9 +30,3 @@ pub enum GraphFormatError {
#[fail(display = "Invalid DLType: {}", 0)] #[fail(display = "Invalid DLType: {}", 0)]
InvalidDLType(String), InvalidDLType(String),
} }
#[derive(Debug, Fail)]
#[fail(display = "SGX error: 0x{:x}", code)]
pub struct SgxError {
pub code: u32,
}
...@@ -20,7 +20,10 @@ ...@@ -20,7 +20,10 @@
use std::{cmp, collections::HashMap, convert::TryFrom, iter::FromIterator, mem, str}; use std::{cmp, collections::HashMap, convert::TryFrom, iter::FromIterator, mem, str};
use failure::Error; use failure::Error;
use nom::{alpha1, digit1, le_i32, le_i64, le_u16, le_u32, le_u64, le_u8, types::CompleteStr}; use nom::{
character::complete::{alpha1, digit1},
number::complete::{le_i32, le_i64, le_u16, le_u32, le_u64, le_u8},
};
use serde; use serde;
use serde_json; use serde_json;
use tvm_common::{ use tvm_common::{
...@@ -32,15 +35,15 @@ use tvm_common::{ ...@@ -32,15 +35,15 @@ use tvm_common::{
use crate::{errors::GraphFormatError, Module, Storage, Tensor}; use crate::{errors::GraphFormatError, Module, Storage, Tensor};
// @see `kTVMNDArrayMagic` in `ndarray.h` // @see `kTVMNDArrayMagic` in `ndarray.h`
const _NDARRAY_MAGIC: u64 = 0xDD5E40F096B4A13F; const _NDARRAY_MAGIC: u64 = 0xDD5E_40F0_96B4_A13F;
// @see `kTVMNDArrayListMagic` in `graph_runtime.h` // @see `kTVMNDArrayListMagic` in `graph_runtime.h`
const _NDARRAY_LIST_MAGIC: u64 = 0xF7E58D4F05049CB7; const _NDARRAY_LIST_MAGIC: u64 = 0xF7E5_8D4F_0504_9CB7;
/// A TVM computation graph. /// A TVM computation graph.
/// ///
/// # Examples /// # Examples
/// ///
/// ``` /// ```norun
/// let graph_json = fs::read_to_string("graph.json").unwrap(); /// let graph_json = fs::read_to_string("graph.json").unwrap();
/// let graph = Graph::try_from(&graph_json).unwrap(); /// let graph = Graph::try_from(&graph_json).unwrap();
/// ``` /// ```
...@@ -141,7 +144,7 @@ impl<'a> TryFrom<&'a str> for Graph { ...@@ -141,7 +144,7 @@ impl<'a> TryFrom<&'a str> for Graph {
/// ///
/// # Examples /// # Examples
/// ///
/// ``` /// ```norun
/// use ndarray::Array; /// use ndarray::Array;
/// ///
/// let syslib = SystemLibModule::default(); // a provider of TVM functions /// let syslib = SystemLibModule::default(); // a provider of TVM functions
...@@ -175,13 +178,13 @@ impl<'m, 't> GraphExecutor<'m, 't> { ...@@ -175,13 +178,13 @@ impl<'m, 't> GraphExecutor<'m, 't> {
let tensors = Self::setup_storages(&graph)?; let tensors = Self::setup_storages(&graph)?;
Ok(GraphExecutor { Ok(GraphExecutor {
op_execs: Self::setup_op_execs(&graph, lib, &tensors)?, op_execs: Self::setup_op_execs(&graph, lib, &tensors)?,
tensors: tensors, tensors,
graph: graph, graph,
}) })
} }
/// Runs the computation graph. /// Runs the computation graph.
pub fn run(&self) { pub fn run(&mut self) {
self.op_execs.iter().for_each(|op_exec| { self.op_execs.iter().for_each(|op_exec| {
op_exec(); op_exec();
}); });
...@@ -196,7 +199,7 @@ impl<'m, 't> GraphExecutor<'m, 't> { ...@@ -196,7 +199,7 @@ impl<'m, 't> GraphExecutor<'m, 't> {
.1 .1
.iter() .iter()
.map(|dltype| { .map(|dltype| {
if let Ok((_, dtype)) = tvm_str_to_type(CompleteStr(dltype)) { if let Ok((_, dtype)) = tvm_str_to_type(dltype) {
Ok(dtype) Ok(dtype)
} else { } else {
Err(GraphFormatError::InvalidDLType(dltype.to_string())) Err(GraphFormatError::InvalidDLType(dltype.to_string()))
...@@ -207,7 +210,7 @@ impl<'m, 't> GraphExecutor<'m, 't> { ...@@ -207,7 +210,7 @@ impl<'m, 't> GraphExecutor<'m, 't> {
let align = dtypes.iter().map(|dtype| dtype.bits() as usize).max(); let align = dtypes.iter().map(|dtype| dtype.bits() as usize).max();
let mut storage_num_bytes = vec![0usize; *storage_ids.iter().max().unwrap_or(&1) + 1]; let mut storage_num_bytes = vec![0usize; *storage_ids.iter().max().unwrap_or(&1) + 1];
for (i, &storage_id) in storage_ids.iter().enumerate() { for (i, &storage_id) in storage_ids.iter().enumerate() {
let dtype_size = dtypes[i].bits() * dtypes[i].lanes() >> 3; let dtype_size = (dtypes[i].bits() * dtypes[i].lanes()) >> 3;
let nbytes = dtype_size * shapes[i].iter().product::<i64>() as usize; let nbytes = dtype_size * shapes[i].iter().product::<i64>() as usize;
storage_num_bytes[storage_id] = cmp::max(nbytes, storage_num_bytes[storage_id]); storage_num_bytes[storage_id] = cmp::max(nbytes, storage_num_bytes[storage_id]);
} }
...@@ -223,9 +226,9 @@ impl<'m, 't> GraphExecutor<'m, 't> { ...@@ -223,9 +226,9 @@ impl<'m, 't> GraphExecutor<'m, 't> {
Tensor { Tensor {
data: mem::replace(&mut storages[storage_id], storage), data: mem::replace(&mut storages[storage_id], storage),
ctx: TVMContext::default(), ctx: TVMContext::default(),
dtype: dtype, dtype,
size: shape.iter().product::<i64>() as usize, size: shape.iter().product::<i64>() as usize,
shape: shape, shape,
strides: None, strides: None,
byte_offset: 0, byte_offset: 0,
} }
...@@ -239,7 +242,7 @@ impl<'m, 't> GraphExecutor<'m, 't> { ...@@ -239,7 +242,7 @@ impl<'m, 't> GraphExecutor<'m, 't> {
fn setup_op_execs<M: 'm + Module>( fn setup_op_execs<M: 'm + Module>(
graph: &Graph, graph: &Graph,
lib: &'m M, lib: &'m M,
tensors: &Vec<Tensor<'t>>, tensors: &[Tensor<'t>],
) -> Result<Vec<Box<dyn Fn() + 'm>>, Error> { ) -> Result<Vec<Box<dyn Fn() + 'm>>, Error> {
ensure!(graph.node_row_ptr.is_some(), "Missing node_row_ptr."); ensure!(graph.node_row_ptr.is_some(), "Missing node_row_ptr.");
let node_row_ptr = graph.node_row_ptr.as_ref().unwrap(); let node_row_ptr = graph.node_row_ptr.as_ref().unwrap();
...@@ -258,15 +261,14 @@ impl<'m, 't> GraphExecutor<'m, 't> { ...@@ -258,15 +261,14 @@ impl<'m, 't> GraphExecutor<'m, 't> {
continue; continue;
} }
let func = lib.get_function(&attrs.func_name).ok_or(format_err!( let func = lib
"Library is missing function {}", .get_function(&attrs.func_name)
attrs.func_name .ok_or_else(|| format_err!("Library is missing function {}", attrs.func_name))?;
))?;
let arg_indices = node let arg_indices = node
.inputs .inputs
.iter() .iter()
.map(|entry| graph.entry_index(entry)) .map(|entry| graph.entry_index(entry))
.chain((0..attrs.num_outputs).map(|oi| Ok(node_row_ptr[i].clone() + oi))); .chain((0..attrs.num_outputs).map(|oi| Ok(node_row_ptr[i] + oi)));
let dl_tensors = arg_indices let dl_tensors = arg_indices
.map(|idx| { .map(|idx| {
...@@ -279,13 +281,13 @@ impl<'m, 't> GraphExecutor<'m, 't> { ...@@ -279,13 +281,13 @@ impl<'m, 't> GraphExecutor<'m, 't> {
}) })
.collect::<Result<Vec<DLTensor>, Error>>() .collect::<Result<Vec<DLTensor>, Error>>()
.unwrap(); .unwrap();
let op: Box<dyn Fn()> = box move || { let op: Box<dyn Fn()> = Box::new(move || {
let args = dl_tensors let args = dl_tensors
.iter() .iter()
.map(|t| t.into()) .map(|t| t.into())
.collect::<Vec<TVMArgValue>>(); .collect::<Vec<TVMArgValue>>();
func(&args).unwrap(); func(&args).unwrap();
}; });
op_execs.push(op); op_execs.push(op);
} }
Ok(op_execs) Ok(op_execs)
...@@ -297,6 +299,7 @@ impl<'m, 't> GraphExecutor<'m, 't> { ...@@ -297,6 +299,7 @@ impl<'m, 't> GraphExecutor<'m, 't> {
}) })
} }
#[allow(clippy::if_same_then_else)]
pub fn set_input<S: AsRef<str>>(&mut self, name: S, value: Tensor) { pub fn set_input<S: AsRef<str>>(&mut self, name: S, value: Tensor) {
if let Some(idx) = self.get_input_index(name.as_ref()) { if let Some(idx) = self.get_input_index(name.as_ref()) {
// TODO: consider `new_with_params` to avoid ever allocating // TODO: consider `new_with_params` to avoid ever allocating
...@@ -322,7 +325,7 @@ impl<'m, 't> GraphExecutor<'m, 't> { ...@@ -322,7 +325,7 @@ impl<'m, 't> GraphExecutor<'m, 't> {
/// Returns the graph input with name `name`, if it exists. /// Returns the graph input with name `name`, if it exists.
pub fn get_input<S: AsRef<str>>(&mut self, name: S) -> Option<&Tensor> { pub fn get_input<S: AsRef<str>>(&mut self, name: S) -> Option<&Tensor> {
self.get_input_index(name.as_ref()) self.get_input_index(name.as_ref())
.and_then(move |idx| Some(&self.tensors[idx])) .map(move |idx| &self.tensors[idx])
} }
/// Returns the graph output with index `index`, if it exists. /// Returns the graph output with index `index`, if it exists.
...@@ -353,48 +356,52 @@ impl<'m, 't> GraphExecutor<'m, 't> { ...@@ -353,48 +356,52 @@ impl<'m, 't> GraphExecutor<'m, 't> {
} }
// Converts a string to TVM DLDataTypeCode. @see `String2DLDataType` in packed_func.h // Converts a string to TVM DLDataTypeCode. @see `String2DLDataType` in packed_func.h
named!( named! {
tvm_str_to_type<CompleteStr, DataType>, tvm_str_to_type<&str, DataType>,
do_parse!( do_parse!(
type_name: alpha1 >> type_name: alpha1 >>
bits: digit1 >> bits: digit1 >>
lanes: opt!(tuple!(tag!("x"), digit1)) >> lanes: opt!(complete!(tuple!(tag!("x"), digit1))) >>
(DataType { (
DataType {
code: match type_name { code: match type_name {
CompleteStr("int") => DLDataTypeCode_kDLInt, "int" => DLDataTypeCode_kDLInt,
CompleteStr("uint") => DLDataTypeCode_kDLUInt, "uint" => DLDataTypeCode_kDLUInt,
CompleteStr("float") => DLDataTypeCode_kDLFloat, "float" => DLDataTypeCode_kDLFloat,
_ => DLDataTypeCode_kDLFloat, _ => DLDataTypeCode_kDLFloat,
} as usize, } as usize,
bits: bits.parse::<u8>().unwrap() as usize, bits: bits.parse::<u8>().unwrap() as usize,
lanes: match lanes { lanes: lanes
Some(lanes) => lanes.1.parse::<u16>().unwrap() as usize, .map(|(_, lanes)| lanes.parse::<u16>().unwrap() as usize)
None => 1, .unwrap_or(1)
}, }
}) )
) )
); }
// Converts a bytes to String. // Converts a bytes to String.
named!( named! {
name<String>, name<String>,
map_res!(length_bytes!(le_u64), |b: &[u8]| String::from_utf8( map_res!(length_data!(le_u64), |b: &[u8]| String::from_utf8(b.to_vec()))
b.to_vec() }
))
);
// Parses a TVMContext // Parses a TVMContext
named!( named! {
tvm_ctx<&[u8], TVMContext>, tvm_ctx<&[u8], TVMContext>,
do_parse!( do_parse!(
device_type: le_u32 >> device_type: le_u32 >>
device_id: le_i32 >> device_id: le_i32 >>
(TVMContext { device_type: device_type as usize, device_id: device_id as usize }) (
TVMContext {
device_type: device_type as usize,
device_id: device_id as usize,
}
)
) )
); }
// Parses a DataType // Parses a DataType
named!( named! {
data_type<&[u8], DataType>, data_type<&[u8], DataType>,
do_parse!( do_parse!(
code: le_u8 >> code: le_u8 >>
...@@ -402,21 +409,22 @@ named!( ...@@ -402,21 +409,22 @@ named!(
lanes: le_u16 >> lanes: le_u16 >>
(DataType { code: code as usize, bits: bits as usize, lanes: lanes as usize }) (DataType { code: code as usize, bits: bits as usize, lanes: lanes as usize })
) )
); }
// Parses a Tensor from a TVM array file. // Parses a Tensor from a TVM array file.
named!( named! {
tensor<Tensor>, tensor<Tensor>,
do_parse!( do_parse!(
take!(8) take!(8) >>
>> bits!(tag_bits!(u64, 64, 0)) le_u64 >>
>> ctx: tvm_ctx ctx: tvm_ctx >>
>> ndim: le_u32 ndim: le_u32 >>
>> dtype: data_type dtype: data_type >>
>> shape: count!(map!(le_i64, |sz| sz as i64), ndim as usize) shape: count!(map!(le_i64, |sz| sz as i64), ndim as usize) >>
>> length: le_i64 length: le_i64 >>
>> data: take!(length) data: take!(length) >>
>> (Tensor { (
Tensor {
data: Storage::from(data), data: Storage::from(data),
ctx: ctx, ctx: ctx,
dtype: dtype, dtype: dtype,
...@@ -424,26 +432,29 @@ named!( ...@@ -424,26 +432,29 @@ named!(
shape: shape, shape: shape,
strides: None, strides: None,
byte_offset: 0, byte_offset: 0,
}) }
)
) )
); }
// Parses a graph params dict from a params binary file. // Parses a graph params dict from a params binary file.
named!( named! {
parse_param_dict<HashMap<String, Tensor>>, parse_param_dict<HashMap<String, Tensor>>,
do_parse!( do_parse!(
take!(8) take!(8) >>
>> bits!(tag_bits!(u64, 64, 0)) le_u64 >>
>> names: length_count!(le_u64, name) names: length_count!(le_u64, name) >>
>> tensors: length_count!(le_u64, tensor) tensors: length_count!(le_u64, tensor) >>
>> (HashMap::from_iter(names.into_iter().zip(tensors.into_iter()))) (
HashMap::from_iter(names.into_iter().zip(tensors.into_iter()))
)
) )
); }
/// Loads a param dict saved using `relay.save_param_dict`. /// Loads a param dict saved using `relay.save_param_dict`.
pub fn load_param_dict(bytes: &[u8]) -> Result<HashMap<String, Tensor>, GraphFormatError> { pub fn load_param_dict(bytes: &[u8]) -> Result<HashMap<String, Tensor>, GraphFormatError> {
if let Ok((remaining_bytes, param_dict)) = parse_param_dict(bytes) { if let Ok((remaining_bytes, param_dict)) = parse_param_dict(bytes) {
if remaining_bytes.len() == 0 { if remaining_bytes.is_empty() {
Ok(param_dict) Ok(param_dict)
} else { } else {
Err(GraphFormatError::Params) Err(GraphFormatError::Params)
...@@ -460,7 +471,7 @@ mod tests { ...@@ -460,7 +471,7 @@ mod tests {
#[test] #[test]
fn test_str_to_type() { fn test_str_to_type() {
assert_eq!( assert_eq!(
tvm_str_to_type(CompleteStr("float24")).unwrap().1, tvm_str_to_type("float24").unwrap().1,
DataType { DataType {
code: DLDataTypeCode_kDLFloat as usize, code: DLDataTypeCode_kDLFloat as usize,
bits: 24, bits: 24,
...@@ -468,7 +479,7 @@ mod tests { ...@@ -468,7 +479,7 @@ mod tests {
} }
); );
assert_eq!( assert_eq!(
tvm_str_to_type(CompleteStr("uint111x44")).unwrap().1, tvm_str_to_type("uint111x44").unwrap().1,
DataType { DataType {
code: DLDataTypeCode_kDLUInt as usize, code: DLDataTypeCode_kDLUInt as usize,
bits: 111, bits: 111,
......
...@@ -28,19 +28,6 @@ ...@@ -28,19 +28,6 @@
//! The main entrypoints to this crate are `GraphExecutor` //! The main entrypoints to this crate are `GraphExecutor`
//! For examples of use, please refer to the multi-file tests in the `tests` directory. //! For examples of use, please refer to the multi-file tests in the `tests` directory.
#![feature(
allocator_api,
box_syntax,
fn_traits,
unboxed_closures,
vec_remove_item
)]
#[cfg(target_env = "sgx")]
extern crate alloc;
extern crate bounded_spsc_queue;
#[cfg(target_env = "sgx")]
extern crate core;
#[macro_use] #[macro_use]
extern crate failure; extern crate failure;
#[macro_use] #[macro_use]
...@@ -50,7 +37,6 @@ extern crate lazy_static; ...@@ -50,7 +37,6 @@ extern crate lazy_static;
extern crate ndarray; extern crate ndarray;
#[macro_use] #[macro_use]
extern crate nom; extern crate nom;
#[cfg(not(target_env = "sgx"))]
extern crate num_cpus; extern crate num_cpus;
extern crate serde; extern crate serde;
#[macro_use] #[macro_use]
...@@ -63,9 +49,6 @@ mod array; ...@@ -63,9 +49,6 @@ mod array;
pub mod errors; pub mod errors;
mod graph; mod graph;
mod module; mod module;
#[cfg(target_env = "sgx")]
#[macro_use]
pub mod sgx;
mod threading; mod threading;
mod workspace; mod workspace;
...@@ -86,10 +69,8 @@ lazy_static! { ...@@ -86,10 +69,8 @@ lazy_static! {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn TVMAPISetLastError(cmsg: *const i8) { pub unsafe extern "C" fn TVMAPISetLastError(cmsg: *const i8) {
*LAST_ERROR.write().unwrap() = Some(unsafe { std::ffi::CStr::from_ptr(cmsg) }); *LAST_ERROR.write().unwrap() = Some(std::ffi::CStr::from_ptr(cmsg));
#[cfg(target_env = "sgx")]
ocall_packed!("__sgx_set_last_error__", cmsg);
} }
#[no_mangle] #[no_mangle]
......
...@@ -35,8 +35,8 @@ use crate::{ ...@@ -35,8 +35,8 @@ use crate::{
use super::Module; use super::Module;
const TVM_MAIN: &'static [u8] = b"__tvm_main__"; const TVM_MAIN: &[u8] = b"__tvm_main__";
const TVM_MODULE_CTX: &'static [u8] = b"__tvm_module_ctx"; const TVM_MODULE_CTX: &[u8] = b"__tvm_module_ctx";
/// A module backed by a Dynamic Shared Object (dylib). /// A module backed by a Dynamic Shared Object (dylib).
pub struct DsoModule<'a> { pub struct DsoModule<'a> {
...@@ -64,22 +64,26 @@ impl<'a> DsoModule<'a> { ...@@ -64,22 +64,26 @@ impl<'a> DsoModule<'a> {
init_context_func!( init_context_func!(
lib, lib,
(TVMAPISetLastError, extern "C" fn(*const i8)), (TVMAPISetLastError, unsafe extern "C" fn(*const i8)),
( (
TVMBackendAllocWorkspace, TVMBackendAllocWorkspace,
extern "C" fn(c_int, c_int, u64, c_int, c_int) -> *mut c_void unsafe extern "C" fn(c_int, c_int, u64, c_int, c_int) -> *mut c_void
), ),
( (
TVMBackendFreeWorkspace, TVMBackendFreeWorkspace,
extern "C" fn(c_int, c_int, *mut c_void) -> c_int unsafe extern "C" fn(c_int, c_int, *mut c_void) -> c_int
), ),
( (
TVMBackendParallelLaunch, TVMBackendParallelLaunch,
extern "C" fn(crate::threading::FTVMParallelLambda, *const c_void, usize) -> c_int unsafe extern "C" fn(
crate::threading::FTVMParallelLambda,
*const c_void,
usize,
) -> c_int
), ),
( (
TVMBackendParallelBarrier, TVMBackendParallelBarrier,
extern "C" fn(usize, *const tvm_common::ffi::TVMParallelGroupEnv) unsafe extern "C" fn(usize, *const tvm_common::ffi::TVMParallelGroupEnv)
), ),
); );
...@@ -129,7 +133,7 @@ impl<'a> Module for DsoModule<'a> { ...@@ -129,7 +133,7 @@ impl<'a> Module for DsoModule<'a> {
&*Box::leak(super::wrap_backend_packed_func(name.to_string(), *func)), &*Box::leak(super::wrap_backend_packed_func(name.to_string(), *func)),
); );
self.packed_funcs.borrow().get(name).map(|f| *f) self.packed_funcs.borrow().get(name).copied()
} }
} }
......
...@@ -36,9 +36,9 @@ pub trait Module { ...@@ -36,9 +36,9 @@ pub trait Module {
// @see `WrapPackedFunc` in `llvm_module.cc`. // @see `WrapPackedFunc` in `llvm_module.cc`.
fn wrap_backend_packed_func(func_name: String, func: BackendPackedCFunc) -> Box<dyn PackedFunc> { fn wrap_backend_packed_func(func_name: String, func: BackendPackedCFunc) -> Box<dyn PackedFunc> {
box move |args: &[TVMArgValue]| { Box::new(move |args: &[TVMArgValue]| {
let (values, type_codes): (Vec<TVMValue>, Vec<i32>) = args let (values, type_codes): (Vec<TVMValue>, Vec<i32>) = args
.into_iter() .iter()
.map(|arg| { .map(|arg| {
let (val, code) = arg.to_tvm_value(); let (val, code) = arg.to_tvm_value();
(val, code as i32) (val, code as i32)
...@@ -52,5 +52,5 @@ fn wrap_backend_packed_func(func_name: String, func: BackendPackedCFunc) -> Box< ...@@ -52,5 +52,5 @@ fn wrap_backend_packed_func(func_name: String, func: BackendPackedCFunc) -> Box<
func_name.clone(), func_name.clone(),
)) ))
} }
} })
} }
...@@ -27,6 +27,11 @@ use super::Module; ...@@ -27,6 +27,11 @@ use super::Module;
pub struct SystemLibModule; pub struct SystemLibModule;
#[cfg(target_env = "sgx")]
extern "C" {
fn __tvm_module_startup();
}
lazy_static! { lazy_static! {
static ref SYSTEM_LIB_FUNCTIONS: Mutex<HashMap<String, &'static (dyn PackedFunc)>> = static ref SYSTEM_LIB_FUNCTIONS: Mutex<HashMap<String, &'static (dyn PackedFunc)>> =
Mutex::new(HashMap::new()); Mutex::new(HashMap::new());
...@@ -37,13 +42,16 @@ impl Module for SystemLibModule { ...@@ -37,13 +42,16 @@ impl Module for SystemLibModule {
SYSTEM_LIB_FUNCTIONS SYSTEM_LIB_FUNCTIONS
.lock() .lock()
.unwrap() .unwrap()
.get(name.as_ref()) .get(name.as_ref()).copied()
.map(|f| *f)
} }
} }
impl Default for SystemLibModule { impl Default for SystemLibModule {
fn default() -> Self { fn default() -> Self {
#[cfg(target_env = "sgx")]
unsafe {
__tvm_module_startup();
}
SystemLibModule {} SystemLibModule {}
} }
} }
...@@ -58,5 +66,5 @@ pub extern "C" fn TVMBackendRegisterSystemLibSymbol( ...@@ -58,5 +66,5 @@ pub extern "C" fn TVMBackendRegisterSystemLibSymbol(
name.to_string(), name.to_string(),
&*Box::leak(super::wrap_backend_packed_func(name.to_string(), func)), &*Box::leak(super::wrap_backend_packed_func(name.to_string(), func)),
); );
return 0; 0
} }
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
use std::{
ffi::CString,
os::raw::{c_char, c_int},
};
pub use crate::threading::tvm_run_worker as run_worker;
use crate::{threading::sgx_join_threads, SystemLibModule, TVMArgValue, TVMRetValue};
use errors::SgxError;
use ffi::TVMValue;
#[macro_export]
macro_rules! tvm_ocall {
($func: expr) => {
match $func {
0 => Ok(()),
code => Err(SgxError { code }),
}
};
}
pub type SgxStatus = u32;
#[cfg(target_env = "sgx")]
extern "C" {
fn tvm_ocall_packed_func(
name: *const c_char,
arg_values: *const TVMValue,
type_codes: *const c_int,
num_args: c_int,
ret_val: *mut TVMValue,
ret_type_code: *mut c_int,
) -> SgxStatus;
}
pub fn ocall_packed_func<S: AsRef<str>>(
fn_name: S,
args: &[TVMArgValue],
) -> Result<TVMRetValue, SgxError> {
let mut ret_val = TVMValue { v_int64: 0 };
let ret_type_code = 0i64;
unsafe {
tvm_ocall!(tvm_ocall_packed_func(
CString::new(fn_name.as_ref()).unwrap().as_ptr(),
args.iter()
.map(|ref arg| arg.value)
.collect::<Vec<TVMValue>>()
.as_ptr(),
args.iter()
.map(|ref arg| arg.type_code as i32)
.collect::<Vec<i32>>()
.as_ptr() as *const i32,
args.len() as i32,
&mut ret_val as *mut TVMValue,
&mut (ret_type_code as i32) as *mut c_int,
))?;
}
Ok(TVMRetValue::from_tvm_value(ret_val, ret_type_code as i64))
}
#[macro_export]
macro_rules! ocall_packed {
($fn_name:expr, $($args:expr),+) => {
$crate::sgx::ocall_packed_func($fn_name, &[$($args.into(),)+])
.expect(concat!("Error calling `", $fn_name, "`"))
};
($fn_name:expr) => {
$crate::sgx::ocall_packed_func($fn_name, &Vec::new())
.expect(concat!("Error calling `", $fn_name, "`"))
}
}
pub fn shutdown() {
if env!("TVM_NUM_THREADS") != "0" {
sgx_join_threads()
}
}
impl Drop for SystemLibModule {
fn drop(&mut self) {
shutdown()
}
}
...@@ -18,30 +18,18 @@ ...@@ -18,30 +18,18 @@
*/ */
use std::{ use std::{
env,
os::raw::{c_int, c_void}, os::raw::{c_int, c_void},
sync::{ sync::{
atomic::{AtomicUsize, Ordering}, atomic::{AtomicUsize, Ordering},
Arc, Barrier, Arc, Barrier,
}, },
};
#[cfg(not(target_env = "sgx"))]
use num_cpus;
#[cfg(not(target_env = "sgx"))]
use std::{
env,
thread::{self, JoinHandle}, thread::{self, JoinHandle},
}; };
#[cfg(target_env = "sgx")] use crossbeam::channel::{Sender, Receiver, bounded};
use std::{collections::VecDeque, ptr, sync::Mutex};
use bounded_spsc_queue::{self, Producer};
use tvm_common::ffi::TVMParallelGroupEnv; use tvm_common::ffi::TVMParallelGroupEnv;
#[cfg(target_env = "sgx")]
use super::{TVMArgValue, TVMRetValue};
pub(crate) type FTVMParallelLambda = pub(crate) type FTVMParallelLambda =
extern "C" fn(task_id: usize, penv: *const TVMParallelGroupEnv, cdata: *const c_void) -> i32; extern "C" fn(task_id: usize, penv: *const TVMParallelGroupEnv, cdata: *const c_void) -> i32;
...@@ -82,7 +70,6 @@ impl Job { ...@@ -82,7 +70,6 @@ impl Job {
/// Waits for all tasks in this `Job` to be completed. /// Waits for all tasks in this `Job` to be completed.
fn wait(&self) { fn wait(&self) {
while self.pending.load(Ordering::Acquire) > 0 { while self.pending.load(Ordering::Acquire) > 0 {
#[cfg(not(target_env = "sgx"))]
thread::yield_now(); thread::yield_now();
} }
} }
...@@ -99,9 +86,8 @@ struct Task { ...@@ -99,9 +86,8 @@ struct Task {
unsafe impl Send for Task {} unsafe impl Send for Task {}
unsafe impl Sync for Task {} unsafe impl Sync for Task {}
impl FnOnce<()> for Task { impl Task {
type Output = i32; fn run(self) -> i32 {
extern "rust-call" fn call_once(self, _args: ()) -> Self::Output {
let status = (self.flambda)(self.id, &self.penv as *const _, self.cdata); let status = (self.flambda)(self.id, &self.penv as *const _, self.cdata);
self.pending.fetch_sub(1, Ordering::AcqRel); self.pending.fetch_sub(1, Ordering::AcqRel);
status status
...@@ -111,45 +97,23 @@ impl FnOnce<()> for Task { ...@@ -111,45 +97,23 @@ impl FnOnce<()> for Task {
#[derive(Default)] #[derive(Default)]
struct Threads { struct Threads {
#[allow(unused)] #[allow(unused)]
#[cfg(not(target_env = "sgx"))]
handles: Vec<JoinHandle<()>>, handles: Vec<JoinHandle<()>>,
queues: Vec<Producer<Task>>, queues: Vec<Sender<Task>>,
} }
impl<'a> Threads { impl<'a> Threads {
#[cfg(not(target_env = "sgx"))] fn launch<F: Sync + Send + FnOnce(Receiver<Task>) + 'static + Copy>(
fn launch<F: Sync + Send + FnOnce(Consumer<Task>) + 'static + Copy>(
num_threads: usize, num_threads: usize,
cb: F, cb: F,
) -> Self { ) -> Self {
let (handles, queues) = (0..num_threads) let (handles, queues) = (0..num_threads)
.map(|_| { .map(|_| {
let (p, c) = bounded_spsc_queue::make(2); let (p, c) = bounded(2);
let handle = thread::spawn(move || cb(c.into())); let handle = thread::spawn(move || cb(c.into()));
(handle, p) (handle, p)
}) })
.unzip(); .unzip();
Threads { Threads { handles, queues }
handles: handles,
queues: queues,
}
}
#[cfg(target_env = "sgx")]
fn launch<F: Sync + Send + FnOnce(Consumer<Task>) + 'static + Copy>(
num_threads: usize,
_cb: F,
) -> Self {
let mut consumer_queues = SGX_QUEUES.lock().unwrap();
let queues = (0..num_threads)
.map(|_| {
let (p, c) = bounded_spsc_queue::make(2);
consumer_queues.push_back(c.into());
p
})
.collect();
ocall_packed!("__sgx_thread_group_launch__", num_threads as u64);
Threads { queues: queues }
} }
} }
...@@ -165,7 +129,7 @@ impl ThreadPool { ...@@ -165,7 +129,7 @@ impl ThreadPool {
fn new() -> Self { fn new() -> Self {
let num_workers = max_concurrency(); let num_workers = max_concurrency();
ThreadPool { ThreadPool {
num_workers: num_workers, num_workers,
threads: Threads::launch(num_workers, ThreadPool::run_worker), threads: Threads::launch(num_workers, ThreadPool::run_worker),
} }
} }
...@@ -174,17 +138,18 @@ impl ThreadPool { ...@@ -174,17 +138,18 @@ impl ThreadPool {
let mut tasks = job.tasks(self.num_workers + 1); let mut tasks = job.tasks(self.num_workers + 1);
for (i, task) in tasks.split_off(1).into_iter().enumerate() { for (i, task) in tasks.split_off(1).into_iter().enumerate() {
self.threads.queues[i].push(task); self.threads.queues[i].send(task)
.expect("should send");
} }
tasks.pop().unwrap()(); tasks.pop().unwrap().run();
job.wait(); job.wait();
} }
fn run_worker(queue: Consumer<Task>) { fn run_worker(queue: Receiver<Task>) {
loop { loop {
let task = queue.pop(); let task = queue.recv().expect("should recv");
let result = task(); let result = task.run();
if result == <i32>::min_value() { if result == <i32>::min_value() {
break; break;
} else if result != 0 { } else if result != 0 {
...@@ -194,42 +159,14 @@ impl ThreadPool { ...@@ -194,42 +159,14 @@ impl ThreadPool {
} }
} }
// Send + Sync wrapper for bounded_spsc_queue::Consumer #[cfg(not(target_arch = "wasm32"))]
struct Consumer<T> {
consumer: bounded_spsc_queue::Consumer<T>,
}
impl<T> From<bounded_spsc_queue::Consumer<T>> for Consumer<T> {
fn from(c: bounded_spsc_queue::Consumer<T>) -> Self {
Consumer { consumer: c }
}
}
impl<T> Consumer<T> {
fn pop(&self) -> T {
self.consumer.pop()
}
}
unsafe impl<T> Send for Consumer<T> {}
unsafe impl<T> Sync for Consumer<T> {}
#[cfg(target_env = "sgx")]
lazy_static! {
/// Holds tasks for untrusted threads which re-enter the enclave to execute.
static ref SGX_QUEUES: Mutex<VecDeque<Consumer<Task>>> = Mutex::new(VecDeque::new());
}
#[cfg(all(not(target_arch = "wasm32"), not(target_env = "sgx")))]
fn max_concurrency() -> usize { fn max_concurrency() -> usize {
if let Ok(threads_str) = env::var("TVM_NUM_THREADS").or(env::var("OMP_NUM_THREADS")) { if let Ok(threads_str) = env::var("TVM_NUM_THREADS").or_else(|_| env::var("OMP_NUM_THREADS")) {
if let Ok(threads) = usize::from_str_radix(&threads_str, 10) { if let Ok(threads) = usize::from_str_radix(&threads_str, 10) {
return threads; return threads;
} }
} }
num_cpus::get_physical() num_cpus::get()
}
#[cfg(target_env = "sgx")]
fn max_concurrency() -> usize {
usize::from_str_radix(env!("TVM_NUM_THREADS"), 10).unwrap_or(1)
} }
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
...@@ -237,69 +174,38 @@ fn max_concurrency() -> usize { ...@@ -237,69 +174,38 @@ fn max_concurrency() -> usize {
0 // wasm doesn't support threads yet 0 // wasm doesn't support threads yet
} }
#[cfg(target_env = "sgx")]
pub fn tvm_run_worker(_args: &[TVMArgValue]) -> TVMRetValue {
let q = {
let mut qs = SGX_QUEUES.lock().unwrap();
qs.pop_front()
// `qs: MutexGuard` needs to be dropped here since `run_worker` won't return
};
if let Some(q) = q {
ThreadPool::run_worker(q);
}
TVMRetValue::default()
}
#[no_mangle] #[no_mangle]
pub extern "C" fn TVMBackendParallelLaunch( pub extern "C" fn TVMBackendParallelLaunch(
cb: FTVMParallelLambda, cb: FTVMParallelLambda,
cdata: *const c_void, cdata: *const c_void,
num_task: usize, num_task: usize,
) -> c_int { ) -> c_int {
if max_concurrency() == 0 { if max_concurrency() < 2 {
let penv = TVMParallelGroupEnv { let penv = TVMParallelGroupEnv {
sync_handle: 0 as *mut c_void, sync_handle: std::ptr::null_mut(),
num_task: 1, num_task: 1,
}; };
cb(0, &penv as *const _, cdata); cb(0, &penv as *const _, cdata);
} else { } else {
THREAD_POOL.with(|pool| { THREAD_POOL.with(|pool| {
pool.launch(Job { pool.launch(Job {
cb: cb, cb,
cdata: cdata, cdata,
req_num_tasks: num_task, req_num_tasks: num_task,
pending: Arc::new(AtomicUsize::new(0)), pending: Arc::new(AtomicUsize::new(0)),
}); });
}); });
} }
return 0; 0
}
#[cfg(target_env = "sgx")]
pub(crate) fn sgx_join_threads() {
extern "C" fn poison_pill(
_task_id: usize,
_penv: *const TVMParallelGroupEnv,
_cdata: *const c_void,
) -> i32 {
<i32>::min_value()
}
THREAD_POOL.with(|pool| {
pool.launch(Job {
cb: poison_pill,
cdata: ptr::null(),
req_num_tasks: 0,
pending: Arc::new(AtomicUsize::new(0)),
});
});
ocall_packed!("__sgx_thread_group_join__", 0);
} }
// @see issue 988 for information on why this function is used. // @see issue 988 for information on why this function is used.
#[no_mangle] #[no_mangle]
pub extern "C" fn TVMBackendParallelBarrier(_task_id: usize, penv: *const TVMParallelGroupEnv) { pub unsafe extern "C" fn TVMBackendParallelBarrier(
let barrier: &Arc<Barrier> = unsafe { &*((*penv).sync_handle as *const Arc<Barrier>) }; _task_id: usize,
penv: *const TVMParallelGroupEnv,
) {
let barrier: &Arc<Barrier> = &*((*penv).sync_handle as *const Arc<Barrier>);
barrier.wait(); barrier.wait();
} }
...@@ -323,7 +229,7 @@ mod tests { ...@@ -323,7 +229,7 @@ mod tests {
penv: *const TVMParallelGroupEnv, penv: *const TVMParallelGroupEnv,
cdata: *const c_void, cdata: *const c_void,
) -> i32 { ) -> i32 {
if cdata == ptr::null() { if cdata.is_null() {
return 0; return 0;
} }
unsafe { unsafe {
......
...@@ -29,6 +29,11 @@ use crate::allocator::Allocation; ...@@ -29,6 +29,11 @@ use crate::allocator::Allocation;
const WS_ALIGN: usize = 64; // taken from `kTempAllocaAlignment` in `device_api.h` const WS_ALIGN: usize = 64; // taken from `kTempAllocaAlignment` in `device_api.h`
pub fn remove_item<T: PartialEq>(vec: &mut Vec<T>, item: &T) -> Option<T> {
let pos = vec.iter().position(|x| *x == *item)?;
Some(vec.remove(pos))
}
struct WorkspacePool { struct WorkspacePool {
workspaces: Vec<Allocation>, workspaces: Vec<Allocation>,
free: Vec<usize>, free: Vec<usize>,
...@@ -51,7 +56,7 @@ impl WorkspacePool { ...@@ -51,7 +56,7 @@ impl WorkspacePool {
} }
fn alloc(&mut self, size: usize) -> Result<*mut u8, Error> { fn alloc(&mut self, size: usize) -> Result<*mut u8, Error> {
if self.free.len() == 0 { if self.free.is_empty() {
return self.alloc_new(size); return self.alloc_new(size);
} }
let idx = self let idx = self
...@@ -64,15 +69,12 @@ impl WorkspacePool { ...@@ -64,15 +69,12 @@ impl WorkspacePool {
} }
cur_ws_idx.or(Some(idx)).and_then(|cur_idx| { cur_ws_idx.or(Some(idx)).and_then(|cur_idx| {
let cur_size = self.workspaces[cur_idx].size(); let cur_size = self.workspaces[cur_idx].size();
Some(match ws_size <= cur_size { Some(if ws_size <= cur_size { idx } else { cur_idx })
true => idx,
false => cur_idx,
})
}) })
}); });
match idx { match idx {
Some(idx) => { Some(idx) => {
self.free.remove_item(&idx).unwrap(); remove_item(&mut self.free, &idx).unwrap();
self.in_use.push(idx); self.in_use.push(idx);
Ok(self.workspaces[idx].as_mut_ptr()) Ok(self.workspaces[idx].as_mut_ptr())
} }
...@@ -90,9 +92,10 @@ impl WorkspacePool { ...@@ -90,9 +92,10 @@ impl WorkspacePool {
break; break;
} }
} }
Ok(self if let Some(ws_idx) = ws_idx {
.free self.free.push(ws_idx);
.push(ws_idx.ok_or(format_err!("Tried to free nonexistent workspace."))?)) }
Ok(())
} }
} }
...@@ -133,5 +136,5 @@ pub extern "C" fn TVMBackendFreeWorkspace( ...@@ -133,5 +136,5 @@ pub extern "C" fn TVMBackendFreeWorkspace(
Err(_) => -1, Err(_) => -1,
}) as c_int }) as c_int
}); });
return 0; 0
} }
...@@ -26,11 +26,38 @@ use std::{convert::TryFrom, fs, io::Read}; ...@@ -26,11 +26,38 @@ use std::{convert::TryFrom, fs, io::Read};
use tvm_runtime::Graph; use tvm_runtime::Graph;
macro_rules! mf_dir {
($p:literal) => {
concat!(env!("CARGO_MANIFEST_DIR"), $p)
};
}
static PARAMS_FIXTURE_PATH: &str = mf_dir!("/tests/graph.params");
#[test] #[test]
fn test_load_graph() { fn test_load_graph() {
let output = std::process::Command::new(mf_dir!("/tests/build_model.py"))
.env(
"PYTHONPATH",
concat!(
mf_dir!("/../../python"),
":",
mf_dir!("/../../nnvm/python"),
":",
mf_dir!("/../../topi/python")
),
)
.output()
.expect("Failed to build test model");
assert!(
std::path::Path::new(PARAMS_FIXTURE_PATH).exists(),
"Could not build test graph fixture: STDOUT:\n\n{}\nSTDERR: {}\n\n",
String::from_utf8(output.stdout).unwrap(),
String::from_utf8(output.stderr).unwrap()
);
let mut params_bytes = Vec::new(); let mut params_bytes = Vec::new();
fs::File::open(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/graph.params")) fs::File::open(PARAMS_FIXTURE_PATH)
.expect("Could not find TVM graph. Did you run `tests/build_model.py`?") .unwrap()
.read_to_end(&mut params_bytes) .read_to_end(&mut params_bytes)
.unwrap(); .unwrap();
let _params = tvm_runtime::load_param_dict(&params_bytes); let _params = tvm_runtime::load_param_dict(&params_bytes);
......
...@@ -22,10 +22,10 @@ license = "Apache-2.0" ...@@ -22,10 +22,10 @@ license = "Apache-2.0"
authors = ["TVM Contributors"] authors = ["TVM Contributors"]
[dependencies] [dependencies]
ndarray="0.12.1" ndarray="0.12"
serde = "1.0.59" serde = "1.0"
serde_json = "1.0.17" serde_json = "1.0"
tvm-runtime = { path = "../../" } tvm-runtime = { path = "../../" }
[build-dependencies] [build-dependencies]
ar = "0.6.0" ar = "0.6"
...@@ -33,11 +33,11 @@ const IN_DIM: usize = 8; ...@@ -33,11 +33,11 @@ const IN_DIM: usize = 8;
macro_rules! check_sum { macro_rules! check_sum {
($e:expr, $a:ident, $b:ident) => { ($e:expr, $a:ident, $b:ident) => {
let a = Array::try_from($e.get_input(stringify!($a)).unwrap()).unwrap(); let a = Array::try_from($e.get_input(stringify!($a)).unwrap().to_owned()).unwrap();
check_sum!(a, $b); check_sum!(a, $b);
}; };
($e:expr, $a:expr, $b:ident) => { ($e:expr, $a:expr, $b:ident) => {
let a = Array::try_from($e.get_output($a).unwrap()).unwrap(); let a = Array::try_from($e.get_output($a).unwrap().to_owned()).unwrap();
check_sum!(a, $b); check_sum!(a, $b);
}; };
($a:ident, $b:ident) => { ($a:ident, $b:ident) => {
...@@ -73,11 +73,11 @@ fn main() { ...@@ -73,11 +73,11 @@ fn main() {
.collect::<Vec<f32>>(), .collect::<Vec<f32>>(),
) )
.unwrap(); .unwrap();
let w = Array::try_from(params.get("dense0_weight").unwrap()) let w = Array::try_from(params.get("dense0_weight").unwrap().to_owned())
.unwrap() .unwrap()
.into_shape((IN_DIM * 2, IN_DIM)) .into_shape((IN_DIM * 2, IN_DIM))
.unwrap(); .unwrap();
let b = Array::try_from(params.get("dense0_bias").unwrap()).unwrap(); let b = Array::try_from(params.get("dense0_bias").unwrap().to_owned()).unwrap();
let dense = x.dot(&w.t()) + &b; let dense = x.dot(&w.t()) + &b;
let left = dense.slice(s![.., 0..IN_DIM]); let left = dense.slice(s![.., 0..IN_DIM]);
let right = dense.slice(s![.., IN_DIM..]); let right = dense.slice(s![.., IN_DIM..]);
......
...@@ -22,8 +22,8 @@ license = "Apache-2.0" ...@@ -22,8 +22,8 @@ license = "Apache-2.0"
authors = ["TVM Contributors"] authors = ["TVM Contributors"]
[dependencies] [dependencies]
ndarray="0.12.1" ndarray="0.12"
tvm-runtime = { path = "../../" } tvm-runtime = { path = "../../" }
[build-dependencies] [build-dependencies]
ar = "0.6.0" ar = "0.6"
...@@ -28,12 +28,7 @@ ...@@ -28,12 +28,7 @@
#include <tvm/runtime/module.h> #include <tvm/runtime/module.h>
#include <tvm/runtime/registry.h> #include <tvm/runtime/registry.h>
#include <tvm/runtime/device_api.h> #include <tvm/runtime/device_api.h>
#ifdef _LIBCPP_SGX_CONFIG
#include "sgx/trusted/runtime.h"
#endif
#ifndef _LIBCPP_SGX_NO_IOSTREAMS
#include <sstream> #include <sstream>
#endif
#include <array> #include <array>
#include <algorithm> #include <algorithm>
#include <string> #include <string>
...@@ -174,7 +169,6 @@ void DeviceAPI::SyncStreamFromTo(TVMContext ctx, ...@@ -174,7 +169,6 @@ void DeviceAPI::SyncStreamFromTo(TVMContext ctx,
LOG(FATAL) << "Device does not support stream api."; LOG(FATAL) << "Device does not support stream api.";
} }
#ifndef _LIBCPP_SGX_NO_IOSTREAMS
//-------------------------------------------------------- //--------------------------------------------------------
// Error handling mechanism // Error handling mechanism
// ------------------------------------------------------- // -------------------------------------------------------
...@@ -338,11 +332,6 @@ std::string NormalizeError(std::string err_msg) { ...@@ -338,11 +332,6 @@ std::string NormalizeError(std::string err_msg) {
return os.str(); return os.str();
} }
#else
std::string NormalizeError(std::string err_msg) {
return err_msg;
}
#endif
} // namespace runtime } // namespace runtime
} // namespace tvm } // namespace tvm
...@@ -366,11 +355,7 @@ int TVMAPIHandleException(const std::runtime_error &e) { ...@@ -366,11 +355,7 @@ int TVMAPIHandleException(const std::runtime_error &e) {
} }
void TVMAPISetLastError(const char* msg) { void TVMAPISetLastError(const char* msg) {
#ifndef _LIBCPP_SGX_CONFIG
TVMAPIRuntimeStore::Get()->last_error = msg; TVMAPIRuntimeStore::Get()->last_error = msg;
#else
sgx::OCallPackedFunc("__sgx_set_last_error__", msg);
#endif
} }
int TVMModLoadFromFile(const char* file_name, int TVMModLoadFromFile(const char* file_name,
......
...@@ -25,11 +25,7 @@ ...@@ -25,11 +25,7 @@
#include <dmlc/logging.h> #include <dmlc/logging.h>
#include <dmlc/thread_local.h> #include <dmlc/thread_local.h>
#include <algorithm> #include <algorithm>
#ifndef _LIBCPP_SGX_CONFIG
#include "mt_random_engine.cc" #include "mt_random_engine.cc"
#else
#include "sgx_random_engine.cc"
#endif
#define DLPACK_INTEGER_TYPE_SWITCH(type, DType, ...) \ #define DLPACK_INTEGER_TYPE_SWITCH(type, DType, ...) \
if (type.code == kDLInt && type.bits == 32) { \ if (type.code == kDLInt && type.bits == 32) { \
......
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*!
* \file random/sgx_random_engine.h
* \brief SGX trusted random engine
*/
#include <dmlc/logging.h>
#include <sgx_trts.h>
#include <algorithm>
#include <cmath>
#include "../../sgx/common.h"
namespace tvm {
namespace contrib {
/*!
* \brief An interface for generating [tensors of] random numbers.
*/
class RandomEngine {
public:
/*!
* \brief Creates a RandomEngine, suggesting the use of a provided seed.
*/
explicit RandomEngine(unsigned seed) {
LOG(WARNING) << "SGX RandomEngine does not support seeding.";
}
/*!
* \brief Seeds the underlying RNG, if possible.
*/
inline void Seed(unsigned seed) {
LOG(WARNING) << "SGX RandomEngine does not support seeding.";
}
/*!
* \return the seed associated with the underlying RNG.
*/
inline unsigned GetSeed() const {
LOG(WARNING) << "SGX RandomEngine does not support seeding.";
return 0;
}
/*!
* \return a random integer sampled from the RNG.
*/
inline unsigned GetRandInt() {
int rand_int;
TVM_SGX_CHECKED_CALL(
sgx_read_rand(reinterpret_cast<unsigned char*>(&rand_int), sizeof(int)));
return rand_int;
}
/*!
* \return a random integer sampled from Unif(low, high).
*/
inline float GetUniform(float low, float high) {
float max_int = static_cast<float>(std::numeric_limits<unsigned>::max());
float unif01 = GetRandInt() / max_int;
return low + unif01 * (high - low);
}
/*!
* \return a random value sampled from Normal(loc, scale**2).
*/
inline float GetNormal(float loc, float scale) {
float sign = GetUniform(-1, 1);
float sample = GetStandardNormalOneside();
return loc + (sign > 0 ? scale : -scale) * sample;
}
/*!
* \brief Fills a tensor with values drawn from Unif(low, high)
*/
void SampleUniform(DLTensor* data, float low, float high) {
CHECK_GT(high, low) << "high must be bigger than low";
CHECK(data->strides == nullptr);
DLDataType dtype = data->dtype;
int64_t size = 1;
for (int i = 0; i < data->ndim; ++i) {
size *= data->shape[i];
}
CHECK(dtype.code == kDLFloat && dtype.bits == 32 && dtype.lanes == 1);
std::generate_n(static_cast<float*>(data->data), size, [&] () {
float max_int = static_cast<float>(std::numeric_limits<unsigned>::max());
float unif01 = GetRandInt() / max_int;
return low + unif01 * (high - low);
});
}
/*!
* \brief Fills a tensor with values drawn from Normal(loc, scale)
*/
void SampleNormal(DLTensor* data, float loc, float scale) {
CHECK_GT(scale, 0) << "scale must be positive";
CHECK(data->strides == nullptr);
DLDataType dtype = data->dtype;
int64_t size = 1;
for (int i = 0; i < data->ndim; ++i) {
size *= data->shape[i];
}
CHECK(dtype.code == kDLFloat && dtype.bits == 32 && dtype.lanes == 1);
std::generate_n(static_cast<float*>(data->data), size, [&] () {
return GetNormal(loc, scale);
});
}
private:
/*!
* \return a random value sampled from Normal(0, 1) such that the
* sampled value is greater than tail
*/
inline float GetStandardNormalTail(float tail) {
while (true) {
float u1 = GetUniform(0, 1);
float u2 = GetUniform(0, 1);
float x = - log(u1) / tail;
float y = - log(u2);
if (2 * y < x * x) {
return x + tail;
}
}
}
/*!
* \return a random positive value sampled from Normal(0, 1).
*/
inline float GetStandardNormalOneside() {
while (true) {
unsigned i = GetRandInt() & 255;
float x = GetUniform(0, ZIG_NORM_X[i]);
if (x < ZIG_NORM_X[i+1]) {
return x;
}
if (i == 0) {
return GetStandardNormalTail(ZIG_NORM_X[1]);
}
float y = GetUniform(ZIG_NORM_F[i], ZIG_NORM_F[i+1]);
if (y < exp(-0.5 * x * x)) {
return x;
}
}
}
/*!
* Tables for normal distribution which is sampled using the ziggurat algorithm.
*/
static constexpr float ZIG_NORM_X[257] =
{3.910757959537090045, 3.654152885361008796, 3.449278298560964462, 3.320244733839166074,
3.224575052047029100, 3.147889289517149969, 3.083526132001233044, 3.027837791768635434,
2.978603279880844834, 2.934366867207854224, 2.894121053612348060, 2.857138730872132548,
2.822877396825325125, 2.790921174000785765, 2.760944005278822555, 2.732685359042827056,
2.705933656121858100, 2.680514643284522158, 2.656283037575502437, 2.633116393630324570,
2.610910518487548515, 2.589575986706995181, 2.569035452680536569, 2.549221550323460761,
2.530075232158516929, 2.511544441625342294, 2.493583041269680667, 2.476149939669143318,
2.459208374333311298, 2.442725318198956774, 2.426670984935725972, 2.411018413899685520,
2.395743119780480601, 2.380822795170626005, 2.366237056715818632, 2.351967227377659952,
2.337996148795031370, 2.324308018869623016, 2.310888250599850036, 2.297723348901329565,
2.284800802722946056, 2.272108990226823888, 2.259637095172217780, 2.247375032945807760,
2.235313384928327984, 2.223443340090905718, 2.211756642882544366, 2.200245546609647995,
2.188902771624720689, 2.177721467738641614, 2.166695180352645966, 2.155817819875063268,
2.145083634046203613, 2.134487182844320152, 2.124023315687815661, 2.113687150684933957,
2.103474055713146829, 2.093379631137050279, 2.083399693996551783, 2.073530263516978778,
2.063767547809956415, 2.054107931648864849, 2.044547965215732788, 2.035084353727808715,
2.025713947862032960, 2.016433734904371722, 2.007240830558684852, 1.998132471356564244,
1.989106007615571325, 1.980158896898598364, 1.971288697931769640, 1.962493064942461896,
1.953769742382734043, 1.945116560006753925, 1.936531428273758904, 1.928012334050718257,
1.919557336591228847, 1.911164563769282232, 1.902832208548446369, 1.894558525668710081,
1.886341828534776388, 1.878180486290977669, 1.870072921069236838, 1.862017605397632281,
1.854013059758148119, 1.846057850283119750, 1.838150586580728607, 1.830289919680666566,
1.822474540091783224, 1.814703175964167636, 1.806974591348693426, 1.799287584547580199,
1.791640986550010028, 1.784033659547276329, 1.776464495522344977, 1.768932414909077933,
1.761436365316706665, 1.753975320315455111, 1.746548278279492994, 1.739154261283669012,
1.731792314050707216, 1.724461502945775715, 1.717160915015540690, 1.709889657069006086,
1.702646854797613907, 1.695431651932238548, 1.688243209434858727, 1.681080704722823338,
1.673943330923760353, 1.666830296159286684, 1.659740822855789499, 1.652674147080648526,
1.645629517902360339, 1.638606196773111146, 1.631603456932422036, 1.624620582830568427,
1.617656869570534228, 1.610711622367333673, 1.603784156023583041, 1.596873794420261339,
1.589979870021648534, 1.583101723393471438, 1.576238702733332886, 1.569390163412534456,
1.562555467528439657, 1.555733983466554893, 1.548925085471535512, 1.542128153226347553,
1.535342571438843118, 1.528567729435024614, 1.521803020758293101, 1.515047842773992404,
1.508301596278571965, 1.501563685112706548, 1.494833515777718391, 1.488110497054654369,
1.481394039625375747, 1.474683555695025516, 1.467978458615230908, 1.461278162507407830,
1.454582081885523293, 1.447889631277669675, 1.441200224845798017, 1.434513276002946425,
1.427828197027290358, 1.421144398672323117, 1.414461289772464658, 1.407778276843371534,
1.401094763676202559, 1.394410150925071257, 1.387723835686884621, 1.381035211072741964,
1.374343665770030531, 1.367648583594317957, 1.360949343030101844, 1.354245316759430606,
1.347535871177359290, 1.340820365893152122, 1.334098153216083604, 1.327368577624624679,
1.320630975217730096, 1.313884673146868964, 1.307128989027353860, 1.300363230327433728,
1.293586693733517645, 1.286798664489786415, 1.279998415710333237, 1.273185207661843732,
1.266358287014688333, 1.259516886060144225, 1.252660221891297887, 1.245787495544997903,
1.238897891102027415, 1.231990574742445110, 1.225064693752808020, 1.218119375481726552,
1.211153726239911244, 1.204166830140560140, 1.197157747875585931, 1.190125515422801650,
1.183069142678760732, 1.175987612011489825, 1.168879876726833800, 1.161744859441574240,
1.154581450355851802, 1.147388505416733873, 1.140164844363995789, 1.132909248648336975,
1.125620459211294389, 1.118297174115062909, 1.110938046009249502, 1.103541679420268151,
1.096106627847603487, 1.088631390649514197, 1.081114409698889389, 1.073554065787871714,
1.065948674757506653, 1.058296483326006454, 1.050595664586207123, 1.042844313139370538,
1.035040439828605274, 1.027181966030751292, 1.019266717460529215, 1.011292417434978441,
1.003256679539591412, 0.995156999629943084, 0.986990747093846266, 0.978755155288937750,
0.970447311058864615, 0.962064143217605250, 0.953602409875572654, 0.945058684462571130,
0.936429340280896860, 0.927710533396234771, 0.918898183643734989, 0.909987953490768997,
0.900975224455174528, 0.891855070726792376, 0.882622229578910122, 0.873271068082494550,
0.863795545546826915, 0.854189171001560554, 0.844444954902423661, 0.834555354079518752,
0.824512208745288633, 0.814306670128064347, 0.803929116982664893, 0.793369058833152785,
0.782615023299588763, 0.771654424216739354, 0.760473406422083165, 0.749056662009581653,
0.737387211425838629, 0.725446140901303549, 0.713212285182022732, 0.700661841097584448,
0.687767892786257717, 0.674499822827436479, 0.660822574234205984, 0.646695714884388928,
0.632072236375024632, 0.616896989996235545, 0.601104617743940417, 0.584616766093722262,
0.567338257040473026, 0.549151702313026790, 0.529909720646495108, 0.509423329585933393,
0.487443966121754335, 0.463634336771763245, 0.437518402186662658, 0.408389134588000746,
0.375121332850465727, 0.335737519180459465, 0.286174591747260509, 0.215241895913273806,
0.000000000000000000};
static constexpr float ZIG_NORM_F[257] =
{0.000477467764586655, 0.001260285930498598, 0.002609072746106363, 0.004037972593371872,
0.005522403299264754, 0.007050875471392110, 0.008616582769422917, 0.010214971439731100,
0.011842757857943104, 0.013497450601780807, 0.015177088307982072, 0.016880083152595839,
0.018605121275783350, 0.020351096230109354, 0.022117062707379922, 0.023902203305873237,
0.025705804008632656, 0.027527235669693315, 0.029365939758230111, 0.031221417192023690,
0.033093219458688698, 0.034980941461833073, 0.036884215688691151, 0.038802707404656918,
0.040736110656078753, 0.042684144916619378, 0.044646552251446536, 0.046623094902089664,
0.048613553216035145, 0.050617723861121788, 0.052635418276973649, 0.054666461325077916,
0.056710690106399467, 0.058767952921137984, 0.060838108349751806, 0.062921024437977854,
0.065016577971470438, 0.067124653828023989, 0.069245144397250269, 0.071377949059141965,
0.073522973714240991, 0.075680130359194964, 0.077849336702372207, 0.080030515814947509,
0.082223595813495684, 0.084428509570654661, 0.086645194450867782, 0.088873592068594229,
0.091113648066700734, 0.093365311913026619, 0.095628536713353335, 0.097903279039215627,
0.100189498769172020, 0.102487158942306270, 0.104796225622867056, 0.107116667775072880,
0.109448457147210021, 0.111791568164245583, 0.114145977828255210, 0.116511665626037014,
0.118888613443345698, 0.121276805485235437, 0.123676228202051403, 0.126086870220650349,
0.128508722280473636, 0.130941777174128166, 0.133386029692162844, 0.135841476571757352,
0.138308116449064322, 0.140785949814968309, 0.143274978974047118, 0.145775208006537926,
0.148286642733128721, 0.150809290682410169, 0.153343161060837674, 0.155888264725064563,
0.158444614156520225, 0.161012223438117663, 0.163591108232982951, 0.166181285765110071,
0.168782774801850333, 0.171395595638155623, 0.174019770082499359, 0.176655321444406654,
0.179302274523530397, 0.181960655600216487, 0.184630492427504539, 0.187311814224516926,
0.190004651671193070, 0.192709036904328807, 0.195425003514885592, 0.198152586546538112,
0.200891822495431333, 0.203642749311121501, 0.206405406398679298, 0.209179834621935651,
0.211966076307852941, 0.214764175252008499, 0.217574176725178370, 0.220396127481011589,
0.223230075764789593, 0.226076071323264877, 0.228934165415577484, 0.231804410825248525,
0.234686861873252689, 0.237581574432173676, 0.240488605941449107, 0.243408015423711988,
0.246339863502238771, 0.249284212419516704, 0.252241126056943765, 0.255210669955677150,
0.258192911338648023, 0.261187919133763713, 0.264195763998317568, 0.267216518344631837,
0.270250256366959984, 0.273297054069675804, 0.276356989296781264, 0.279430141762765316,
0.282516593084849388, 0.285616426816658109, 0.288729728483353931, 0.291856585618280984,
0.294997087801162572, 0.298151326697901342, 0.301319396102034120, 0.304501391977896274,
0.307697412505553769, 0.310907558127563710, 0.314131931597630143, 0.317370638031222396,
0.320623784958230129, 0.323891482377732021, 0.327173842814958593, 0.330470981380537099,
0.333783015832108509, 0.337110066638412809, 0.340452257045945450, 0.343809713148291340,
0.347182563958251478, 0.350570941482881204, 0.353974980801569250, 0.357394820147290515,
0.360830600991175754, 0.364282468130549597, 0.367750569780596226, 0.371235057669821344,
0.374736087139491414, 0.378253817247238111, 0.381788410875031348, 0.385340034841733958,
0.388908860020464597, 0.392495061461010764, 0.396098818517547080, 0.399720314981931668,
0.403359739222868885, 0.407017284331247953, 0.410693148271983222, 0.414387534042706784,
0.418100649839684591, 0.421832709231353298, 0.425583931339900579, 0.429354541031341519,
0.433144769114574058, 0.436954852549929273, 0.440785034667769915, 0.444635565397727750,
0.448506701509214067, 0.452398706863882505, 0.456311852680773566, 0.460246417814923481,
0.464202689050278838, 0.468180961407822172, 0.472181538469883255, 0.476204732721683788,
0.480250865911249714, 0.484320269428911598, 0.488413284707712059, 0.492530263646148658,
0.496671569054796314, 0.500837575128482149, 0.505028667945828791, 0.509245245998136142,
0.513487720749743026, 0.517756517232200619, 0.522052074674794864, 0.526374847174186700,
0.530725304406193921, 0.535103932383019565, 0.539511234259544614, 0.543947731192649941,
0.548413963257921133, 0.552910490428519918, 0.557437893621486324, 0.561996775817277916,
0.566587763258951771, 0.571211506738074970, 0.575868682975210544, 0.580559996103683473,
0.585286179266300333, 0.590047996335791969, 0.594846243770991268, 0.599681752622167719,
0.604555390700549533, 0.609468064928895381, 0.614420723892076803, 0.619414360609039205,
0.624450015550274240, 0.629528779928128279, 0.634651799290960050, 0.639820277456438991,
0.645035480824251883, 0.650298743114294586, 0.655611470583224665, 0.660975147780241357,
0.666391343912380640, 0.671861719900766374, 0.677388036222513090, 0.682972161648791376,
0.688616083008527058, 0.694321916130032579, 0.700091918140490099, 0.705928501336797409,
0.711834248882358467, 0.717811932634901395, 0.723864533472881599, 0.729995264565802437,
0.736207598131266683, 0.742505296344636245, 0.748892447223726720, 0.755373506511754500,
0.761953346841546475, 0.768637315803334831, 0.775431304986138326, 0.782341832659861902,
0.789376143571198563, 0.796542330428254619, 0.803849483176389490, 0.811307874318219935,
0.818929191609414797, 0.826726833952094231, 0.834716292992930375, 0.842915653118441077,
0.851346258465123684, 0.860033621203008636, 0.869008688043793165, 0.878309655816146839,
0.887984660763399880, 0.898095921906304051, 0.908726440060562912, 0.919991505048360247,
0.932060075968990209, 0.945198953453078028, 0.959879091812415930, 0.977101701282731328,
1.000000000000000000};
};
constexpr float RandomEngine::ZIG_NORM_X[];
constexpr float RandomEngine::ZIG_NORM_F[];
} // namespace contrib
} // namespace tvm
...@@ -50,7 +50,7 @@ class CPUDeviceAPI final : public DeviceAPI { ...@@ -50,7 +50,7 @@ class CPUDeviceAPI final : public DeviceAPI {
#if _MSC_VER #if _MSC_VER
ptr = _aligned_malloc(nbytes, alignment); ptr = _aligned_malloc(nbytes, alignment);
if (ptr == nullptr) throw std::bad_alloc(); if (ptr == nullptr) throw std::bad_alloc();
#elif defined(_LIBCPP_SGX_CONFIG) || (defined(__ANDROID__) && __ANDROID_API__ < 17) #elif defined(__ANDROID__) && __ANDROID_API__ < 17
ptr = memalign(alignment, nbytes); ptr = memalign(alignment, nbytes);
if (ptr == nullptr) throw std::bad_alloc(); if (ptr == nullptr) throw std::bad_alloc();
#else #else
......
...@@ -73,7 +73,6 @@ std::string GetFileFormat(const std::string& file_name, ...@@ -73,7 +73,6 @@ std::string GetFileFormat(const std::string& file_name,
const std::string& format) { const std::string& format) {
std::string fmt = format; std::string fmt = format;
if (fmt.length() == 0) { if (fmt.length() == 0) {
if (file_name.find(".signed.so") != std::string::npos) return "sgx";
size_t pos = file_name.find_last_of("."); size_t pos = file_name.find_last_of(".");
if (pos != std::string::npos) { if (pos != std::string::npos) {
return file_name.substr(pos + 1, file_name.length() - pos - 1); return file_name.substr(pos + 1, file_name.length() - pos - 1);
......
...@@ -67,11 +67,7 @@ void GraphRuntime::Run() { ...@@ -67,11 +67,7 @@ void GraphRuntime::Run() {
void GraphRuntime::Init(const std::string& graph_json, void GraphRuntime::Init(const std::string& graph_json,
tvm::runtime::Module module, tvm::runtime::Module module,
const std::vector<TVMContext>& ctxs) { const std::vector<TVMContext>& ctxs) {
#ifndef _LIBCPP_SGX_NO_IOSTREAMS
std::istringstream is(graph_json); std::istringstream is(graph_json);
#else
std::string is = graph_json;
#endif
dmlc::JSONReader reader(&is); dmlc::JSONReader reader(&is);
this->Load(&reader); this->Load(&reader);
module_ = module; module_ = module;
......
...@@ -21,9 +21,7 @@ ...@@ -21,9 +21,7 @@
* \file module_util.cc * \file module_util.cc
* \brief Utilities for module. * \brief Utilities for module.
*/ */
#ifndef _LIBCPP_SGX_CONFIG
#include <dmlc/memory_io.h> #include <dmlc/memory_io.h>
#endif
#include <tvm/runtime/module.h> #include <tvm/runtime/module.h>
#include <tvm/runtime/registry.h> #include <tvm/runtime/registry.h>
#include <string> #include <string>
...@@ -121,7 +119,6 @@ void InitContextFunctions(std::function<void*(const char*)> fgetsymbol) { ...@@ -121,7 +119,6 @@ void InitContextFunctions(std::function<void*(const char*)> fgetsymbol) {
* \return Root Module. * \return Root Module.
*/ */
runtime::Module ProcessModuleBlob(const char* mblob, ObjectPtr<Library> lib) { runtime::Module ProcessModuleBlob(const char* mblob, ObjectPtr<Library> lib) {
#ifndef _LIBCPP_SGX_CONFIG
CHECK(mblob != nullptr); CHECK(mblob != nullptr);
uint64_t nbytes = 0; uint64_t nbytes = 0;
for (size_t i = 0; i < sizeof(nbytes); ++i) { for (size_t i = 0; i < sizeof(nbytes); ++i) {
...@@ -180,10 +177,6 @@ runtime::Module ProcessModuleBlob(const char* mblob, ObjectPtr<Library> lib) { ...@@ -180,10 +177,6 @@ runtime::Module ProcessModuleBlob(const char* mblob, ObjectPtr<Library> lib) {
// invariance: root module is always at location 0. // invariance: root module is always at location 0.
// The module order is collected via DFS // The module order is collected via DFS
return modules[0]; return modules[0];
#else
LOG(FATAL) << "SGX does not support ImportModuleBlob";
return Module();
#endif
} }
Module CreateModuleFromLibrary(ObjectPtr<Library> lib) { Module CreateModuleFromLibrary(ObjectPtr<Library> lib) {
......
...@@ -26,9 +26,7 @@ ...@@ -26,9 +26,7 @@
#include <tvm/runtime/packed_func.h> #include <tvm/runtime/packed_func.h>
#include <unordered_set> #include <unordered_set>
#include <cstring> #include <cstring>
#ifndef _LIBCPP_SGX_CONFIG
#include "file_util.h" #include "file_util.h"
#endif
namespace tvm { namespace tvm {
namespace runtime { namespace runtime {
...@@ -77,7 +75,6 @@ PackedFunc ModuleNode::GetFunction(const std::string& name, bool query_imports) ...@@ -77,7 +75,6 @@ PackedFunc ModuleNode::GetFunction(const std::string& name, bool query_imports)
Module Module::LoadFromFile(const std::string& file_name, Module Module::LoadFromFile(const std::string& file_name,
const std::string& format) { const std::string& format) {
#ifndef _LIBCPP_SGX_CONFIG
std::string fmt = GetFileFormat(file_name, format); std::string fmt = GetFileFormat(file_name, format);
CHECK(fmt.length() != 0) CHECK(fmt.length() != 0)
<< "Cannot deduce format of file " << file_name; << "Cannot deduce format of file " << file_name;
...@@ -91,9 +88,6 @@ Module Module::LoadFromFile(const std::string& file_name, ...@@ -91,9 +88,6 @@ Module Module::LoadFromFile(const std::string& file_name,
<< load_f_name << ") is not presented."; << load_f_name << ") is not presented.";
Module m = (*f)(file_name, format); Module m = (*f)(file_name, format);
return m; return m;
#else
LOG(FATAL) << "SGX does not support LoadFromFile";
#endif
} }
void ModuleNode::SaveToFile(const std::string& file_name, void ModuleNode::SaveToFile(const std::string& file_name,
......
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*!
* \file common.h
* \brief TVM SGX common API.
*/
#ifndef TVM_RUNTIME_SGX_COMMON_H_
#define TVM_RUNTIME_SGX_COMMON_H_
#include <sgx_error.h>
namespace tvm {
namespace runtime {
namespace sgx {
#define TVM_SGX_CHECKED_CALL(Function) \
sgx_status_t TVM_STR_CONCAT(__sgx_status_, __LINE__) = SGX_ERROR_UNEXPECTED; \
TVM_STR_CONCAT(__sgx_status_, __LINE__) = Function; \
CHECK_EQ(TVM_STR_CONCAT(__sgx_status_, __LINE__), SGX_SUCCESS) \
<< "SGX Error: " << TVM_STR_CONCAT(__sgx_status_, __LINE__);
} // namespace sgx
} // namespace runtime
} // namespace tvm
#endif // TVM_RUNTIME_SGX_COMMON_H_
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*!
* \file ecall_registry.h
* \brief The global registry of packed functions available via ecall_packed_func.
*/
#ifndef TVM_RUNTIME_SGX_TRUSTED_ECALL_REGISTRY_H_
#define TVM_RUNTIME_SGX_TRUSTED_ECALL_REGISTRY_H_
#include <dmlc/logging.h>
#include <tvm/runtime/registry.h>
#include <string>
#include <algorithm>
#include <vector>
namespace tvm {
namespace runtime {
namespace sgx {
class ECallRegistry: public Registry {
public:
explicit ECallRegistry(std::string name) {
name_ = name;
}
Registry& set_body(PackedFunc f) {
func_ = f;
return *this;
}
Registry& set_body(PackedFunc::FType f) { // NOLINT(*)
return set_body(PackedFunc(f));
}
static Registry& Register(const std::string& name, bool override = false) {
for (auto& r : exports_) {
if (r.name_ == name) {
CHECK(override) << "ecall " << name << " is already registered";
return r;
}
}
TVM_SGX_CHECKED_CALL(
tvm_ocall_register_export(name.c_str(), exports_.size()));
exports_.emplace_back(name);
return exports_.back();
}
static bool Remove(const std::string& name) {
LOG(FATAL) << "Removing enclave exports is not supported.";
}
static const PackedFunc* Get(const std::string& name) {
for (const auto& r : exports_) {
if (r.name_ == name) return &r.func_;
}
return nullptr;
}
static const PackedFunc* Get(unsigned func_id) {
return func_id >= exports_.size() ? nullptr : &exports_[func_id].func_;
}
static std::vector<std::string> ListNames() {
std::vector<std::string> names;
names.resize(exports_.size());
std::transform(exports_.begin(), exports_.end(), names.begin(),
[](ECallRegistry r) { return r.name_; });
return names;
}
static std::vector<ECallRegistry> exports_;
};
std::vector<ECallRegistry> ECallRegistry::exports_;
/*!
* \brief Register a function callable via ecall_packed_func
* \code
* TVM_REGISTER_ENCLAVE_FUNC("DoThing")
* .set_body([](TVMArgs args, TVMRetValue* rv) {
* });
* \endcode
*/
#define TVM_REGISTER_ENCLAVE_FUNC(OpName) \
TVM_STR_CONCAT(TVM_FUNC_REG_VAR_DEF, __COUNTER__) = \
::tvm::runtime::sgx::ECallRegistry::Register(OpName, true)
} // namespace sgx
} // namespace runtime
} // namespace tvm
#endif // TVM_RUNTIME_SGX_TRUSTED_ECALL_REGISTRY_H_
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*!
* \file runtime_t.cc
*/
#include <tvm/runtime/c_runtime_api.h>
#include <tvm/runtime/packed_func.h>
#include "../../c_runtime_api.cc"
#include "../../cpu_device_api.cc"
#include "../../module.cc"
#include "../../module_util.cc"
#include "../../registry.cc"
#include "../../system_lib_module.cc"
#include "../../thread_pool.cc"
#include "../../workspace_pool.cc"
#include "ecall_registry.h"
#include "runtime.h"
#include "threading_backend.cc"
namespace tvm {
namespace runtime {
namespace sgx {
extern "C" {
void tvm_ecall_init(TVMRetValueHandle ret) {}
void tvm_ecall_packed_func(int func_id,
const TVMValue* arg_values,
const int* type_codes,
int num_args,
TVMRetValueHandle ret) {
const PackedFunc* f = ECallRegistry::Get(func_id);
CHECK(f != nullptr) << "ecall function not found.";
TVMRetValue rv;
f->CallPacked(TVMArgs(arg_values, type_codes, num_args), &rv);
int ret_type_code = rv.type_code();
if (ret_type_code == kTVMNullptr) return;
TVMValue ret_value;
if (ret_type_code == kTVMBytes || ret_type_code == kTVMStr) {
// allocate a buffer in untrusted, copy the values in
std::string bytes = rv;
void* ret_buf;
TVM_SGX_CHECKED_CALL(tvm_ocall_reserve_space(
&ret_buf, bytes.size() + sizeof(TVMByteArray), sizeof(uint64_t)));
char* data_buf = static_cast<char*>(ret_buf) + sizeof(TVMByteArray);
memcpy(data_buf, bytes.data(), bytes.size());
TVMByteArray* arr = static_cast<TVMByteArray*>(ret_buf);
arr->data = data_buf;
arr->size = bytes.size();
ret_value = TVMValue{.v_handle = arr};
ret_type_code = kTVMBytes;
} else {
rv.MoveToCHost(&ret_value, &ret_type_code);
}
TVM_SGX_CHECKED_CALL(tvm_ocall_set_return(ret, &ret_value, &ret_type_code, 1));
}
} // extern "C"
TVM_REGISTER_ENCLAVE_FUNC("__tvm_main__")
.set_body([](TVMArgs args, TVMRetValue* rv) {
Module mod = (*Registry::Get("runtime.SystemLib"))();
mod.GetFunction("default_function").CallPacked(args, rv);
});
} // namespace sgx
} // namespace runtime
} // namespace tvm
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*!
* \file trusted/runtime.h
* \brief TVM SGX trusted API.
*/
#ifndef TVM_RUNTIME_SGX_TRUSTED_RUNTIME_H_
#define TVM_RUNTIME_SGX_TRUSTED_RUNTIME_H_
#include <tvm/runtime/packed_func.h>
#include <string>
#include <utility>
#include "../common.h"
namespace tvm {
namespace runtime {
namespace sgx {
template<typename... Args>
inline TVMRetValue OCallPackedFunc(std::string name, Args&& ...args) {
const int kNumArgs = sizeof...(Args);
const int kArraySize = kNumArgs > 0 ? kNumArgs : 1;
TVMValue values[kArraySize];
int type_codes[kArraySize];
detail::for_each(TVMArgsSetter(values, type_codes),
std::forward<Args>(args)...);
TVMValue ret_val;
int ret_type_code;
TVM_SGX_CHECKED_CALL(tvm_ocall_packed_func(name.c_str(),
values,
type_codes,
kNumArgs,
&ret_val,
&ret_type_code));
TVMRetValue* rv = new TVMRetValue();
*rv = TVMArgValue(ret_val, ret_type_code);
return *rv;
}
} // namespace sgx
} // namespace runtime
} // namespace tvm
#endif // TVM_RUNTIME_SGX_TRUSTED_RUNTIME_H_
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*!
* \file sgx/threading_backend.cc
* \brief SGX threading backend
*/
#include <tvm/runtime/threading_backend.h>
#include <dmlc/logging.h>
#include <sgx_edger8r.h>
#include <sgx_trts.h>
#include <atomic>
#include "runtime.h"
#ifndef TVM_SGX_MAX_CONCURRENCY
#define TVM_SGX_MAX_CONCURRENCY 1
#endif
namespace tvm {
namespace runtime {
namespace threading {
class ThreadGroup::Impl {
public:
Impl(int num_workers, std::function<void(int)> worker_callback,
bool exclude_worker0)
: num_workers_(num_workers),
worker_callback_(worker_callback),
next_task_id_(exclude_worker0) {
CHECK(num_workers <= TVM_SGX_MAX_CONCURRENCY)
<< "Tried spawning more threads than allowed by TVM_SGX_MAX_CONCURRENCY.";
sgx::OCallPackedFunc("__sgx_thread_group_launch__",
num_workers_, reinterpret_cast<void*>(this));
}
~Impl() {
sgx::OCallPackedFunc("__sgx_thread_group_join__");
}
void RunTask() {
int task_id = next_task_id_++;
CHECK(task_id < num_workers_)
<< "More workers entered enclave than allowed by TVM_SGX_MAX_CONCURRENCY";
worker_callback_(task_id);
}
private:
int num_workers_;
std::function<void(int)> worker_callback_;
std::atomic<int> next_task_id_;
};
ThreadGroup::ThreadGroup(int num_workers,
std::function<void(int)> worker_callback,
bool exclude_worker0)
: impl_(new ThreadGroup::Impl(num_workers, worker_callback, exclude_worker0)) {}
void ThreadGroup::Join() {}
int ThreadGroup::Configure(AffinityMode mode, int nthreads, bool exclude_worker0) {
int max_conc = MaxConcurrency();
if (!nthreads || ntheads > max_conc) {
return max_conc;
}
return nthreads;
}
ThreadGroup::~ThreadGroup() { delete impl_; }
void Yield() {}
int MaxConcurrency() { return TVM_SGX_MAX_CONCURRENCY; }
TVM_REGISTER_ENCLAVE_FUNC("__tvm_run_worker__")
.set_body([](TVMArgs args, TVMRetValue* rv) {
void* tg = args[0];
if (!sgx_is_within_enclave(tg, sizeof(ThreadGroup::Impl))) return;
reinterpret_cast<ThreadGroup::Impl*>(tg)->RunTask();
});
} // namespace threading
} // namespace runtime
} // namespace tvm
enclave {
from "sgx_tstdc.edl" import *;
from "sgx_stdio.edl" import *;
from "sgx_backtrace.edl" import *;
trusted {
public void tvm_ecall_init([isptr, user_check] TVMRetValueHandle ret);
public void tvm_ecall_packed_func(int func_id,
[in, count=num_args] const TVMValue* arg_values,
[in, count=num_args] const int* type_codes,
int num_args,
[out] TVMValue* ret_val,
[out] int* ret_type_code);
};
untrusted {
void tvm_ocall_packed_func([in, string] const char* name,
[in, count=num_args] const TVMValue* arg_values,
[in, count=num_args] const int* type_codes,
int num_args,
[out] TVMValue* ret_val,
[out] int* ret_type_code);
void tvm_ocall_register_export([in, string] const char* name, int func_id);
};
};
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/*!
* \file sgx_module.cc
* \brief SGX enclave module.
*/
#include <dmlc/logging.h>
#include <sgx_urts.h>
#include <tvm/runtime/c_runtime_api.h>
#include <tvm/runtime/device_api.h>
#include <tvm/runtime/registry.h>
#include <tvm/runtime/threading_backend.h>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <unordered_map>
#include "../common.h"
#include "../../file_util.h"
#include "./tvm_u.h"
namespace tvm {
namespace runtime {
class SGXModuleNode;
namespace sgx {
class EnclaveContext {
public:
explicit EnclaveContext(SGXModuleNode* mod) {
CHECK(Context()->mod_ == nullptr)
<< "Tried overriding existing enclave context.";
CHECK(mod != nullptr) << "Tried setting null enclave context.";
Context()->mod_ = mod;
}
~EnclaveContext() {
Context()->mod_ = nullptr;
}
static SGXModuleNode* GetModule() {
SGXModuleNode* ctx = Context()->mod_;
CHECK(ctx != nullptr) << "No current enclave context";
return ctx;
}
private:
EnclaveContext() {}
SGXModuleNode* mod_;
static EnclaveContext* Context() {
static thread_local EnclaveContext inst;
return &inst;
}
};
} // namespace sgx
class SGXModuleNode : public ModuleNode {
public:
~SGXModuleNode() {
if (eid_) {
sgx::EnclaveContext ctx(this);
sgx_destroy_enclave(eid_);
}
}
void Init(const std::string& enclave_file) {
std::string token_file = GetCacheDir() + "/" +
GetFileBasename(enclave_file) + ".token";
sgx_launch_token_t token = {0};
int token_updated = 0;
try {
std::ifstream ifs(token_file, std::fstream::in | std::fstream::binary);
ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
ifs >> token;
} catch (std::ifstream::failure e) {
memset(&token, 0x0, sizeof(sgx_launch_token_t));
}
TVM_SGX_CHECKED_CALL(sgx_create_enclave(
enclave_file.c_str(), SGX_DEBUG_FLAG, &token, &token_updated, &eid_, NULL));
sgx::EnclaveContext ctx(this);
TVMRetValue rv;
TVM_SGX_CHECKED_CALL(tvm_ecall_init(eid_, &rv));
if (!token_updated) return;
try {
std::ofstream ofs(token_file, std::fstream::trunc | std::fstream::binary);
ofs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
ofs << token;
} catch (std::ifstream::failure e) {
LOG(INFO) << "Could not save SGX launch token to " << token_file;
}
}
const char* type_key() const final {
return "sgx";
}
PackedFunc GetFunction(
const std::string& name,
const std::shared_ptr<ModuleNode>& sptr_to_self) final {
auto exported = exports_.find(name);
if (exported == exports_.end()) return PackedFunc();
int func_id = exported->second;
return PackedFunc([this, func_id](TVMArgs args, TVMRetValue* rv) {
sgx::EnclaveContext ctx(this);
TVMValue ret_value;
int ret_type_code;
TVM_SGX_CHECKED_CALL(tvm_ecall_packed_func(eid_, func_id,
args.values, args.type_codes, args.num_args, &ret_value, &ret_type_code));
*rv = TVMArgValue(ret_value, ret_type_code);
});
}
void RunWorkers(int num_tasks) {
std::function<void(int)> runner = [this](int _worker_id) {
this->GetFunction("__tvm_run_worker__",
std::shared_ptr<SGXModuleNode>(nullptr))();
};
thread_group_.reset(new tvm::runtime::threading::ThreadGroup(
num_tasks, runner, false /* include_main_thread */));
}
void JoinThreads() {
thread_group_->Join();
}
void RegisterExport(std::string name, int func_id) {
exports_[name] = func_id;
}
private:
// ID of the loaded enclave
sgx_enclave_id_t eid_;
// Names and IDs of functions exported by the enclave module
std::unordered_map<std::string, int> exports_;
std::unique_ptr<tvm::runtime::threading::ThreadGroup> thread_group_;
};
namespace sgx {
TVM_REGISTER_GLOBAL("__sgx_thread_group_launch__")
.set_body([](TVMArgs args, TVMRetValue* rv) {
EnclaveContext::GetModule()->RunWorkers(args[0]);
});
TVM_REGISTER_GLOBAL("__sgx_thread_group_join__")
.set_body([](TVMArgs args, TVMRetValue* rv) {
EnclaveContext::GetModule()->JoinThreads();
});
TVM_REGISTER_GLOBAL("__sgx_set_last_error__")
.set_body([](TVMArgs args, TVMRetValue* rv) {
std::string err = args[0];
TVMAPISetLastError(err.c_str());
});
TVM_REGISTER_GLOBAL("__sgx_println__")
.set_body([](TVMArgs args, TVMRetValue* rv) {
std::ostringstream msg;
for (int i = 0; i < args.num_args; ++i) {
switch (args.type_codes[i]) {
case kDLInt: msg << static_cast<int64_t>(args[i]); break;
case kDLUInt: msg << static_cast<uint64_t>(args[i]); break;
case kDLFloat: msg << static_cast<double>(args[i]); break;
case kTVMStr:
case kTVMBytes: {
std::string val = args[i];
msg << val;
}
break;
}
msg << " ";
}
LOG(INFO) << msg.str();
});
extern "C" {
void tvm_ocall_register_export(const char* name, int func_id) {
EnclaveContext::GetModule()->RegisterExport(name, func_id);
}
void tvm_ocall_packed_func(const char* name,
const TVMValue* arg_values,
const int* type_codes,
int num_args,
TVMValue* ret_val,
int* ret_type_code) {
const PackedFunc* f = Registry::Get(name);
CHECK(f != nullptr) << "ocall to nonexistent function \"" << name << "\"";
TVMRetValue rv;
f->CallPacked(TVMArgs(arg_values, type_codes, num_args), &rv);
rv.MoveToCHost(ret_val, ret_type_code);
}
// Allocates space for return values. The returned pointer is only valid between
// successive calls to `tvm_ocall_reserve_space`.
TVM_REGISTER_GLOBAL("__sgx_reserve_space__")
.set_body([](TVMArgs args, TVMRetValue* rv) {
size_t num_bytes = args[0];
size_t alignment = args[1];
static TVMContext ctx = { kDLCPU, 0 };
static thread_local void* buf = nullptr;
static thread_local size_t buf_size = 0;
static thread_local size_t buf_align = 0;
if (buf_size >= num_bytes && buf_align >= alignment) *rv = nullptr;
DeviceAPI::Get(ctx)->FreeDataSpace(ctx, buf);
buf = DeviceAPI::Get(ctx)->AllocDataSpace(ctx, num_bytes, alignment, {});
buf_size = num_bytes;
buf_align = alignment;
*rv = buf;
});
} // extern "C"
} // namespace sgx
TVM_REGISTER_GLOBAL("runtime.module.loadfile_sgx")
.set_body([](TVMArgs args, TVMRetValue* rv) {
std::shared_ptr<SGXModuleNode> node = std::make_shared<SGXModuleNode>();
node->Init(args[0]);
*rv = runtime::Module(node);
});
} // namespace runtime
} // namespace tvm
...@@ -105,16 +105,14 @@ class ParallelLauncher { ...@@ -105,16 +105,14 @@ class ParallelLauncher {
tvm::runtime::threading::Yield(); tvm::runtime::threading::Yield();
} }
if (!has_error_.load()) return 0; if (!has_error_.load()) return 0;
// the following is intended to use string due to std::ostringstream os;
// security issue raised in SGX backend
std::string err("");
for (size_t i = 0; i < par_errors_.size(); ++i) { for (size_t i = 0; i < par_errors_.size(); ++i) {
if (par_errors_[i].length() != 0) { if (par_errors_[i].length() != 0) {
err += "Task " + std::to_string(i) + " error: " + par_errors_[i] + '\n'; os << "Task " << i << " error: " << par_errors_[i] << '\n';
par_errors_[i].clear(); par_errors_[i].clear();
} }
} }
TVMAPISetLastError(err.c_str()); TVMAPISetLastError(os.str().c_str());
return -1; return -1;
} }
// Signal that one job has finished. // Signal that one job has finished.
...@@ -373,11 +371,7 @@ class ThreadPool { ...@@ -373,11 +371,7 @@ class ThreadPool {
// number of workers used (can be restricted with affinity pref) // number of workers used (can be restricted with affinity pref)
int num_workers_used_; int num_workers_used_;
// if or not to exclude worker 0 and use master to run task 0 // if or not to exclude worker 0 and use master to run task 0
#ifndef _LIBCPP_SGX_CONFIG
bool exclude_worker0_{true}; bool exclude_worker0_{true};
#else
bool exclude_worker0_{false};
#endif
std::vector<std::unique_ptr<SpscTaskQueue> > queues_; std::vector<std::unique_ptr<SpscTaskQueue> > queues_;
std::unique_ptr<tvm::runtime::threading::ThreadGroup> threads_; std::unique_ptr<tvm::runtime::threading::ThreadGroup> threads_;
}; };
......
...@@ -89,6 +89,7 @@ ALLOW_FILE_NAME = { ...@@ -89,6 +89,7 @@ ALLOW_FILE_NAME = {
".gitmodules", ".gitmodules",
"CODEOWNERS", "CODEOWNERS",
".scalafmt.conf", ".scalafmt.conf",
"Cargo.lock"
} }
# List of specific files allowed in relpath to <proj_root> # List of specific files allowed in relpath to <proj_root>
...@@ -99,8 +100,8 @@ ALLOW_SPECIFIC_FILE = { ...@@ -99,8 +100,8 @@ ALLOW_SPECIFIC_FILE = {
"KEYS", "KEYS",
"DISCLAIMER", "DISCLAIMER",
"Jenkinsfile", "Jenkinsfile",
# sgx file # sgx config
"apps/sgx/enclave/sgx-deps.diff", "apps/sgx/.cargo/config",
# html for demo purposes # html for demo purposes
"tests/webgl/test_static_webgl_library.html", "tests/webgl/test_static_webgl_library.html",
"web/example_rpc.html", "web/example_rpc.html",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment