From 95be8498b79944e32671855a19792ab02db48b09 Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Sun, 24 Mar 2019 05:59:17 +0000 Subject: [PATCH 01/18] Remove SGX in preparation for adding SGX --- CMakeLists.txt | 6 +- cmake/modules/SGX.cmake | 70 --- docker/Dockerfile.ci_cpu | 5 - docker/install/install_tvm_cpu.sh | 2 - docker/install/ubuntu_install_sgx.sh | 41 -- include/tvm/runtime/device_api.h | 2 - include/tvm/runtime/packed_func.h | 424 +++++++------------ src/contrib/random/random.cc | 4 - src/contrib/random/sgx_random_engine.cc | 309 -------------- src/runtime/c_runtime_api.cc | 15 - src/runtime/cpu_device_api.cc | 2 +- src/runtime/file_util.cc | 1 - src/runtime/graph/graph_runtime.cc | 4 - src/runtime/module.cc | 6 - src/runtime/module_util.cc | 6 - src/runtime/sgx/common.h | 44 -- src/runtime/sgx/trusted/ecall_registry.h | 110 ----- src/runtime/sgx/trusted/runtime.cc | 94 ---- src/runtime/sgx/trusted/runtime.h | 62 --- src/runtime/sgx/trusted/threading_backend.cc | 97 ----- src/runtime/sgx/tvm.edl | 26 -- src/runtime/sgx/untrusted/sgx_module.cc | 255 ----------- src/runtime/thread_pool.cc | 12 +- 23 files changed, 168 insertions(+), 1429 deletions(-) delete mode 100644 cmake/modules/SGX.cmake delete mode 100755 docker/install/ubuntu_install_sgx.sh delete mode 100644 src/contrib/random/sgx_random_engine.cc delete mode 100644 src/runtime/sgx/common.h delete mode 100644 src/runtime/sgx/trusted/ecall_registry.h delete mode 100644 src/runtime/sgx/trusted/runtime.cc delete mode 100644 src/runtime/sgx/trusted/runtime.h delete mode 100644 src/runtime/sgx/trusted/threading_backend.cc delete mode 100644 src/runtime/sgx/tvm.edl delete mode 100644 src/runtime/sgx/untrusted/sgx_module.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index abf198de1c53..1971753a9008 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,8 +33,6 @@ tvm_option(USE_LLVM "Build with LLVM, can be set to specific llvm-config path" O tvm_option(USE_STACKVM_RUNTIME "Include stackvm into the runtime" OFF) 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_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_MSVC_MT "Build with MT" OFF) tvm_option(USE_MICRO "Build with Micro" OFF) @@ -236,7 +234,6 @@ include(cmake/modules/OpenGL.cmake) include(cmake/modules/Vulkan.cmake) include(cmake/modules/Metal.cmake) include(cmake/modules/ROCM.cmake) -include(cmake/modules/SGX.cmake) include(cmake/modules/LLVM.cmake) include(cmake/modules/Micro.cmake) include(cmake/modules/ANTLR.cmake) @@ -263,6 +260,7 @@ endif() add_library(tvm SHARED ${COMPILER_SRCS} ${RUNTIME_SRCS}) add_library(tvm_topi SHARED ${TOPI_SRCS}) add_library(tvm_runtime SHARED ${RUNTIME_SRCS}) +<<<<<<< HEAD if(USE_RELAY_DEBUG) message(STATUS "Building Relay in debug mode...") @@ -276,6 +274,8 @@ if(NOT USE_SGX STREQUAL "OFF") add_dependencies(tvm_runtime sgx_edl tvm_t) install(TARGETS tvm_t ARCHIVE DESTINATION lib${LIB_SUFFIX}) endif() +======= +>>>>>>> f531c1a7... Remove SGX in preparation for adding SGX add_library(nnvm_compiler SHARED ${NNVM_COMPILER_SRCS}) if(USE_THREADS) diff --git a/cmake/modules/SGX.cmake b/cmake/modules/SGX.cmake deleted file mode 100644 index ca40f8cd039b..000000000000 --- a/cmake/modules/SGX.cmake +++ /dev/null @@ -1,70 +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. - -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_u_h} - COMMAND sed -i "4i '#include '" ${_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() diff --git a/docker/Dockerfile.ci_cpu b/docker/Dockerfile.ci_cpu index cb73dc867da0..cd726d25d34a 100644 --- a/docker/Dockerfile.ci_cpu +++ b/docker/Dockerfile.ci_cpu @@ -33,11 +33,6 @@ RUN bash /install/ubuntu_install_python_package.sh COPY install/ubuntu_install_llvm.sh /install/ubuntu_install_llvm.sh RUN bash /install/ubuntu_install_llvm.sh -# SGX deps (build early; changes infrequently) -COPY install/ubuntu_install_sgx.sh /install/ubuntu_install_sgx.sh -RUN bash /install/ubuntu_install_sgx.sh -ENV LD_LIBRARY_PATH /opt/sgxsdk/lib64:${LD_LIBRARY_PATH} - # Rust env (build early; takes a while) COPY install/ubuntu_install_rust.sh /install/ubuntu_install_rust.sh RUN bash /install/ubuntu_install_rust.sh diff --git a/docker/install/install_tvm_cpu.sh b/docker/install/install_tvm_cpu.sh index c67206aef762..66522e9c7b96 100755 --- a/docker/install/install_tvm_cpu.sh +++ b/docker/install/install_tvm_cpu.sh @@ -28,8 +28,6 @@ echo set\(USE_RPC ON\) >> config.cmake echo set\(USE_SORT ON\) >> config.cmake echo set\(USE_GRAPH_RUNTIME ON\) >> 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 cd build cmake .. diff --git a/docker/install/ubuntu_install_sgx.sh b/docker/install/ubuntu_install_sgx.sh deleted file mode 100755 index 71494062aa2b..000000000000 --- a/docker/install/ubuntu_install_sgx.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/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 - diff --git a/include/tvm/runtime/device_api.h b/include/tvm/runtime/device_api.h index 68029c13cb93..cf1651cfa2f7 100644 --- a/include/tvm/runtime/device_api.h +++ b/include/tvm/runtime/device_api.h @@ -220,7 +220,6 @@ inline const char* DeviceName(int type) { } } -#ifndef _LIBCPP_SGX_NO_IOSTREAMS inline std::ostream& operator<<(std::ostream& os, DLContext ctx) { // NOLINT(*) int device_type = static_cast(ctx.device_type); if (device_type > kRPCSessMask) { @@ -230,7 +229,6 @@ inline std::ostream& operator<<(std::ostream& os, DLContext ctx) { // NOLINT(*) os << runtime::DeviceName(device_type) << "(" << ctx.device_id << ")"; return os; } -#endif } // namespace runtime } // namespace tvm #endif // TVM_RUNTIME_DEVICE_API_H_ diff --git a/include/tvm/runtime/packed_func.h b/include/tvm/runtime/packed_func.h index 1ebddb805d0c..e47bc1393be2 100644 --- a/include/tvm/runtime/packed_func.h +++ b/include/tvm/runtime/packed_func.h @@ -24,23 +24,21 @@ #ifndef TVM_RUNTIME_PACKED_FUNC_H_ #define TVM_RUNTIME_PACKED_FUNC_H_ -#ifndef _LIBCPP_SGX_NO_IOSTREAMS -#include -#endif #include #include -#include -#include -#include #include #include -#include +#include +#include +#include #include +#include +#include #include "c_runtime_api.h" #include "module.h" #include "ndarray.h" -#include "object.h" #include "node_base.h" +#include "object.h" // Whether use TVM runtime in header only mode. #ifndef TVM_RUNTIME_HEADER_ONLY @@ -111,7 +109,7 @@ class PackedFunc { * } * \endcode */ - using FType = std::function; + using FType = std::function; /*! \brief default constructor */ PackedFunc() {} /*! \brief constructor from null */ @@ -135,8 +133,8 @@ class PackedFunc { * } * \endcode */ - template - inline TVMRetValue operator()(Args&& ...args) const; + template + inline TVMRetValue operator()(Args&&... args) const; /*! * \brief Call the function in packed format. * \param args The arguments @@ -146,13 +144,9 @@ class PackedFunc { /*! \return the internal body function */ inline FType body() const; /*! \return Whether the packed function is nullptr */ - bool operator==(std::nullptr_t null) const { - return body_ == nullptr; - } + bool operator==(std::nullptr_t null) const { return body_ == nullptr; } /*! \return Whether the packed function is not nullptr */ - bool operator!=(std::nullptr_t null) const { - return body_ != nullptr; - } + bool operator!=(std::nullptr_t null) const { return body_ != nullptr; } private: /*! \brief internal container of packed function */ @@ -162,7 +156,7 @@ class PackedFunc { /*! * \brief Please refer to \ref TypedPackedFuncAnchor "TypedPackedFunc" */ -template +template class TypedPackedFunc; /*! @@ -197,7 +191,7 @@ class TypedPackedFunc; * \tparam R The return value of the function. * \tparam Args The argument signature of the function. */ -template +template class TypedPackedFunc { public: /*! \brief short hand for this function type */ @@ -249,11 +243,9 @@ class TypedPackedFunc { * \param typed_lambda typed lambda function. * \tparam FLambda the type of the lambda function. */ - template - >::value>::type> + template >::value>::type> TypedPackedFunc(const FLambda& typed_lambda) { // NOLINT(*) this->AssignTypedLambda(typed_lambda); } @@ -273,11 +265,9 @@ class TypedPackedFunc { * \tparam FLambda the type of the lambda function. * \returns reference to self. */ - template - >::value>::type> + template >::value>::type> TSelf& operator=(FLambda typed_lambda) { // NOLINT(*) this->AssignTypedLambda(typed_lambda); return *this; @@ -296,28 +286,20 @@ class TypedPackedFunc { * \param args The arguments * \returns The return value. */ - inline R operator()(Args ...args) const; + inline R operator()(Args... args) const; /*! * \brief convert to PackedFunc * \return the internal PackedFunc */ - operator PackedFunc() const { - return packed(); - } + operator PackedFunc() const { return packed(); } /*! * \return reference the internal PackedFunc */ - const PackedFunc& packed() const { - return packed_; - } + const PackedFunc& packed() const { return packed_; } /*! \return Whether the packed function is nullptr */ - bool operator==(std::nullptr_t null) const { - return packed_ == nullptr; - } + bool operator==(std::nullptr_t null) const { return packed_ == nullptr; } /*! \return Whether the packed function is not nullptr */ - bool operator!=(std::nullptr_t null) const { - return packed_ != nullptr; - } + bool operator!=(std::nullptr_t null) const { return packed_ != nullptr; } private: friend class TVMRetValue; @@ -330,7 +312,7 @@ class TypedPackedFunc { * \tparam FLambda The lambda function type. * \note We capture the lambda when possible for maximum efficiency. */ - template + template inline void AssignTypedLambda(FLambda flambda); }; @@ -346,12 +328,8 @@ class TVMArgs { * \param type_codes The argument type codes * \param num_args number of arguments. */ - TVMArgs(const TVMValue* values, - const int* type_codes, - int num_args) - : values(values), - type_codes(type_codes), - num_args(num_args) { } + TVMArgs(const TVMValue* values, const int* type_codes, int num_args) + : values(values), type_codes(type_codes), num_args(num_args) {} /*! \return size of the arguments */ inline int size() const; /*! @@ -384,9 +362,8 @@ inline TVMType String2TVMType(std::string s); inline std::string TVMType2String(TVMType t); // macro to check type code. -#define TVM_CHECK_TYPE_CODE(CODE, T) \ - CHECK_EQ(CODE, T) << " expected " \ - << TypeCode2Str(T) << " but get " << TypeCode2Str(CODE) \ +#define TVM_CHECK_TYPE_CODE(CODE, T) \ + CHECK_EQ(CODE, T) << " expected " << TypeCode2Str(T) << " but get " << TypeCode2Str(CODE) /*! * \brief Type traits to mark if a class is tvm extension type. @@ -399,7 +376,7 @@ inline std::string TVMType2String(TVMType t); * * \tparam T the typename */ -template +template struct extension_type_info { static const int code = 0; }; @@ -458,8 +435,7 @@ class TVMPODValue_ { } operator int() const { TVM_CHECK_TYPE_CODE(type_code_, kDLInt); - CHECK_LE(value_.v_int64, - std::numeric_limits::max()); + CHECK_LE(value_.v_int64, std::numeric_limits::max()); return static_cast(value_.v_int64); } operator bool() const { @@ -473,14 +449,12 @@ class TVMPODValue_ { return value_.v_handle; } operator DLTensor*() const { - if (type_code_ == kArrayHandle || - type_code_ == kNDArrayContainer) { + if (type_code_ == kArrayHandle || type_code_ == kNDArrayContainer) { return static_cast(value_.v_handle); } else { if (type_code_ == kNull) return nullptr; LOG(FATAL) << "Expected " - << "DLTensor* or NDArray but get " - << TypeCode2Str(type_code_); + << "DLTensor* or NDArray but get " << TypeCode2Str(type_code_); return nullptr; } } @@ -498,29 +472,26 @@ class TVMPODValue_ { TVM_CHECK_TYPE_CODE(type_code_, kTVMContext); return value_.v_ctx; } - template::value>::type> + template ::value>::type> TNDArray AsNDArray() const { if (type_code_ == kNull) return TNDArray(nullptr); - auto *container = static_cast(value_.v_handle); + auto* container = static_cast(value_.v_handle); CHECK_EQ(container->array_type_code_, array_type_info::code); return TNDArray(container); } - template + template const TExtension& AsExtension() const { CHECK_LT(type_code_, kExtEnd); return static_cast(value_.v_handle)[0]; } - int type_code() const { - return type_code_; - } + int type_code() const { return type_code_; } /*! * \brief return handle as specific pointer type. * \tparam T the data type. * \return The pointer type. */ - template + template T* ptr() const { return static_cast(value_.v_handle); } @@ -529,8 +500,7 @@ class TVMPODValue_ { friend class TVMArgsSetter; friend class TVMRetValue; TVMPODValue_() : type_code_(kNull) {} - TVMPODValue_(TVMValue value, int type_code) - : value_(value), type_code_(type_code) {} + TVMPODValue_(TVMValue value, int type_code) : value_(value), type_code_(type_code) {} /*! \brief The value */ TVMValue value_; @@ -553,9 +523,7 @@ class TVMArgValue : public TVMPODValue_ { * \param value of the function * \param type_code The type code. */ - TVMArgValue(TVMValue value, int type_code) - : TVMPODValue_(value, type_code) { - } + TVMArgValue(TVMValue value, int type_code) : TVMPODValue_(value, type_code) {} // reuse converter from parent using TVMPODValue_::operator double; using TVMPODValue_::operator int64_t; @@ -587,7 +555,9 @@ class TVMArgValue : public TVMPODValue_ { // None type if (type_code_ == kNull) { TVMType t; - t.code = kHandle; t.bits = 0; t.lanes = 0; + t.code = kHandle; + t.bits = 0; + t.lanes = 0; return t; } TVM_CHECK_TYPE_CODE(type_code_, kTVMType); @@ -598,7 +568,7 @@ class TVMArgValue : public TVMPODValue_ { TVM_CHECK_TYPE_CODE(type_code_, kFuncHandle); return *ptr(); } - template + template operator TypedPackedFunc() const { return TypedPackedFunc(operator PackedFunc()); } @@ -606,19 +576,14 @@ class TVMArgValue : public TVMPODValue_ { TVM_CHECK_TYPE_CODE(type_code_, kModuleHandle); return *ptr(); } - const TVMValue& value() const { - return value_; - } + const TVMValue& value() const { return value_; } // Deferred extension handler. - template + template inline TNodeRef AsNodeRef() const; - template::value>::type> + template ::value>::type> inline operator T() const; - template::value>::type> + template ::value>::type> inline bool IsNodeType() const; inline operator tvm::DataType() const; inline operator tvm::Expr() const; @@ -643,15 +608,12 @@ class TVMRetValue : public TVMPODValue_ { * \brief move constructor from anoter return value. * \param other The other return value. */ - TVMRetValue(TVMRetValue&& other) - : TVMPODValue_(other.value_, other.type_code_) { + TVMRetValue(TVMRetValue&& other) : TVMPODValue_(other.value_, other.type_code_) { other.value_.v_handle = nullptr; other.type_code_ = kNull; } /*! \brief destructor */ - ~TVMRetValue() { - this->Clear(); - } + ~TVMRetValue() { this->Clear(); } // reuse converter from parent using TVMPODValue_::operator double; using TVMPODValue_::operator int64_t; @@ -663,9 +625,7 @@ class TVMRetValue : public TVMPODValue_ { using TVMPODValue_::operator TVMContext; using TVMPODValue_::operator NDArray; using TVMPODValue_::operator Object; - TVMRetValue(const TVMRetValue& other) : TVMPODValue_() { - this->Assign(other); - } + TVMRetValue(const TVMRetValue& other) : TVMPODValue_() { this->Assign(other); } // conversion operators operator std::string() const { if (type_code_ == kTVMType) { @@ -688,7 +648,7 @@ class TVMRetValue : public TVMPODValue_ { TVM_CHECK_TYPE_CODE(type_code_, kFuncHandle); return *ptr(); } - template + template operator TypedPackedFunc() const { return TypedPackedFunc(operator PackedFunc()); } @@ -770,7 +730,7 @@ class TVMRetValue : public TVMPODValue_ { this->SwitchToClass(kFuncHandle, f); return *this; } - template + template TVMRetValue& operator=(const TypedPackedFunc& f) { return operator=(f.packed()); } @@ -786,12 +746,9 @@ class TVMRetValue : public TVMPODValue_ { this->Assign(other); return *this; } - template::code != 0>::type> + template ::code != 0>::type> TVMRetValue& operator=(const T& other) { - this->SwitchToClass( - extension_type_info::code, other); + this->SwitchToClass(extension_type_info::code, other); return *this; } /*! @@ -803,8 +760,7 @@ class TVMRetValue : public TVMPODValue_ { * \param ret_value The return value. * \param ret_type_code The return type code. */ - void MoveToCHost(TVMValue* ret_value, - int* ret_type_code) { + void MoveToCHost(TVMValue* ret_value, int* ret_type_code) { // cannot move str; need specially handle. CHECK(type_code_ != kStr && type_code_ != kBytes); *ret_value = value_; @@ -813,18 +769,15 @@ class TVMRetValue : public TVMPODValue_ { } /*! \return The value field, if the data is POD */ const TVMValue& value() const { - CHECK(type_code_ != kNodeHandle && - type_code_ != kFuncHandle && - type_code_ != kModuleHandle && - type_code_ != kStr) << "TVMRetValue.value can only be used for POD data"; + CHECK(type_code_ != kNodeHandle && type_code_ != kFuncHandle && type_code_ != kModuleHandle && + type_code_ != kStr) + << "TVMRetValue.value can only be used for POD data"; return value_; } // NodeRef related extenstions: in tvm/packed_func_ext.h - template::value>::type> + template ::value>::type> inline operator T() const; - template + template inline TNodeRef AsNodeRef() const; inline TVMRetValue& operator=(const NodeRef& other); inline TVMRetValue& operator=(const NodePtr& other); @@ -833,7 +786,7 @@ class TVMRetValue : public TVMPODValue_ { inline TVMRetValue& operator=(const tvm::DataType& other); private: - template + template void Assign(const T& other) { switch (other.type_code()) { case kStr: { @@ -857,8 +810,7 @@ class TVMRetValue : public TVMPODValue_ { break; } case kNodeHandle: { - SwitchToClass >( - kNodeHandle, *other.template ptr >()); + SwitchToClass >(kNodeHandle, *other.template ptr >()); break; } case kObjectCell: { @@ -876,8 +828,7 @@ class TVMRetValue : public TVMPODValue_ { this->Clear(); type_code_ = other.type_code(); value_.v_handle = - (*(ExtTypeVTable::Get(other.type_code())->clone))( - other.value().v_handle); + (*(ExtTypeVTable::Get(other.type_code())->clone))(other.value().v_handle); #endif } break; @@ -891,7 +842,7 @@ class TVMRetValue : public TVMPODValue_ { type_code_ = type_code; } } - template + template void SwitchToClass(int type_code, T v) { if (type_code_ != type_code) { this->Clear(); @@ -904,10 +855,18 @@ class TVMRetValue : public TVMPODValue_ { void Clear() { if (type_code_ == kNull) return; switch (type_code_) { - case kStr: delete ptr(); break; - case kFuncHandle: delete ptr(); break; - case kModuleHandle: delete ptr(); break; - case kNodeHandle: delete ptr >(); break; + case kStr: + delete ptr(); + break; + case kFuncHandle: + delete ptr(); + break; + case kModuleHandle: + delete ptr(); + break; + case kNodeHandle: + delete ptr >(); + break; case kNDArrayContainer: { static_cast(value_.v_handle)->DecRef(); break; @@ -919,7 +878,7 @@ class TVMRetValue : public TVMPODValue_ { } if (type_code_ > kExtBegin) { #if TVM_RUNTIME_HEADER_ONLY - LOG(FATAL) << "Header only mode do not support ext type"; + LOG(FATAL) << "Header only mode do not support ext type"; #else (*(ExtTypeVTable::Get(type_code_)->destroy))(value_.v_handle); #endif @@ -951,10 +910,10 @@ inline const char* TypeCode2Str(int type_code) { } } -#ifndef _LIBCPP_SGX_NO_IOSTREAMS inline std::ostream& operator<<(std::ostream& os, TVMType t) { // NOLINT(*) if (t.bits == 1 && t.lanes == 1 && t.code == kDLUInt) { - os << "bool"; return os; + os << "bool"; + return os; } if (t.code < kCustomBegin) { os << TypeCode2Str(t.code); @@ -969,47 +928,34 @@ inline std::ostream& operator<<(std::ostream& os, TVMType t) { // NOLINT(*) return os; } -#endif - inline std::string TVMType2String(TVMType t) { if (t.bits == 0) return ""; -#ifndef _LIBCPP_SGX_NO_IOSTREAMS std::ostringstream os; os << t; return os.str(); -#else - if (t.bits == 1 && t.lanes == 1 && t.code == kDLUInt) { - return "bool"; - } - if (t.code < kCustomBegin) { - repr += TypeCode2Str(t.code); - } else { - repr += "custom[" + GetCustomTypeName(t.code) + "]"; - } - if (t.code == kHandle) return repr; - repr += std::to_string(static_cast(t.bits)); - if (t.lanes != 1) { - repr += "x" + std::to_string(static_cast(t.lanes)); - } - return repr; -#endif } inline TVMType String2TVMType(std::string s) { TVMType t; // handle None type if (s.length() == 0) { - t.bits = 0; t.lanes = 0; t.code = kHandle; + t.bits = 0; + t.lanes = 0; + t.code = kHandle; return t; } - t.bits = 32; t.lanes = 1; + t.bits = 32; + t.lanes = 1; const char* scan; if (s.substr(0, 3) == "int") { - t.code = kDLInt; scan = s.c_str() + 3; + t.code = kDLInt; + scan = s.c_str() + 3; } else if (s.substr(0, 4) == "uint") { - t.code = kDLUInt; scan = s.c_str() + 4; + t.code = kDLUInt; + scan = s.c_str() + 4; } else if (s.substr(0, 5) == "float") { - t.code = kDLFloat; scan = s.c_str() + 5; + t.code = kDLFloat; + scan = s.c_str() + 5; } else if (s.substr(0, 6) == "handle") { t.code = kHandle; t.bits = 64; // handle uses 64 bit by default. @@ -1037,69 +983,53 @@ inline TVMType String2TVMType(std::string s) { } inline TVMArgValue TVMArgs::operator[](int i) const { - CHECK_LT(i, num_args) - << "not enough argument passed, " - << num_args << " passed" - << " but request arg[" << i << "]."; + CHECK_LT(i, num_args) << "not enough argument passed, " << num_args << " passed" + << " but request arg[" << i << "]."; return TVMArgValue(values[i], type_codes[i]); } -inline int TVMArgs::size() const { - return num_args; -} - -inline void PackedFunc::CallPacked(TVMArgs args, TVMRetValue* rv) const { - body_(args, rv); -} - -inline PackedFunc::FType PackedFunc::body() const { - return body_; -} +inline int TVMArgs::size() const { return num_args; } +inline void PackedFunc::CallPacked(TVMArgs args, TVMRetValue* rv) const { body_(args, rv); } +inline PackedFunc::FType PackedFunc::body() const { return body_; } // internal namespace namespace detail { -template +template struct for_each_dispatcher { - template + template static void run(const F& f, T&& value, Args&&... args) { // NOLINT(*) f(I, std::forward(value)); - for_each_dispatcher - ::run(f, std::forward(args)...); + for_each_dispatcher::run(f, std::forward(args)...); } }; -template -struct for_each_dispatcher { +template +struct for_each_dispatcher { static void run(const F& f) {} // NOLINT(*) }; -template +template inline void for_each(const F& f, Args&&... args) { // NOLINT(*) - for_each_dispatcher - ::run(f, std::forward(args)...); + for_each_dispatcher::run(f, std::forward(args)...); } } // namespace detail /* \brief argument settter to PackedFunc */ class TVMArgsSetter { public: - TVMArgsSetter(TVMValue* values, int* type_codes) - : values_(values), type_codes_(type_codes) {} + TVMArgsSetter(TVMValue* values, int* type_codes) : values_(values), type_codes_(type_codes) {} // setters for POD types - template::value>::type> + template ::value>::type> void operator()(size_t i, T value) const { values_[i].v_int64 = static_cast(value); type_codes_[i] = kDLInt; } void operator()(size_t i, uint64_t value) const { values_[i].v_int64 = static_cast(value); - CHECK_LE(value, - static_cast(std::numeric_limits::max())); + CHECK_LE(value, static_cast(std::numeric_limits::max())); type_codes_[i] = kDLInt; } void operator()(size_t i, double value) const { @@ -1149,7 +1079,7 @@ class TVMArgsSetter { values_[i].v_handle = const_cast(&value); type_codes_[i] = kFuncHandle; } - template + template void operator()(size_t i, const TypedPackedFunc& value) const { // NOLINT(*) operator()(i, value.packed()); } @@ -1172,9 +1102,7 @@ class TVMArgsSetter { } } // extension - template::code != 0>::type> + template ::code != 0>::type> inline void operator()(size_t i, const T& value) const; // NodeRef related extenstions: in tvm/packed_func_ext.h inline void operator()(size_t i, const NodeRef& other) const; // NOLINT(*) @@ -1187,112 +1115,98 @@ class TVMArgsSetter { int* type_codes_; }; -template -inline TVMRetValue PackedFunc::operator()(Args&& ...args) const { +template +inline TVMRetValue PackedFunc::operator()(Args&&... args) const { 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)...); + detail::for_each(TVMArgsSetter(values, type_codes), std::forward(args)...); TVMRetValue rv; body_(TVMArgs(values, type_codes, kNumArgs), &rv); return rv; } namespace detail { -template +template struct unpack_call_dispatcher { - template - static void run(const F& f, - const TVMArgs& args_pack, - TVMRetValue* rv, - Args&&... unpacked_args) { - unpack_call_dispatcher - ::run(f, args_pack, rv, - std::forward(unpacked_args)..., - args_pack[index]); + template + static void run(const F& f, const TVMArgs& args_pack, TVMRetValue* rv, Args&&... unpacked_args) { + unpack_call_dispatcher::run( + f, args_pack, rv, std::forward(unpacked_args)..., args_pack[index]); } }; -template +template struct unpack_call_dispatcher { - template - static void run(const F& f, - const TVMArgs& args_pack, - TVMRetValue* rv, - Args&&... unpacked_args) { + template + static void run(const F& f, const TVMArgs& args_pack, TVMRetValue* rv, Args&&... unpacked_args) { *rv = R(f(std::forward(unpacked_args)...)); } }; -template +template struct unpack_call_dispatcher { - template - static void run(const F& f, - const TVMArgs& args_pack, - TVMRetValue* rv, - Args&&... unpacked_args) { + template + static void run(const F& f, const TVMArgs& args_pack, TVMRetValue* rv, Args&&... unpacked_args) { f(std::forward(unpacked_args)...); } }; -template +template inline void unpack_call(const F& f, const TVMArgs& args, TVMRetValue* rv) { unpack_call_dispatcher::run(f, args, rv); } -template -inline R call_packed(const PackedFunc& pf, Args&& ...args) { +template +inline R call_packed(const PackedFunc& pf, Args&&... args) { return R(pf(std::forward(args)...)); } -template +template struct typed_packed_call_dispatcher { - template - static inline R run(const PackedFunc& pf, Args&& ...args) { + template + static inline R run(const PackedFunc& pf, Args&&... args) { return pf(std::forward(args)...); } }; -template<> +template <> struct typed_packed_call_dispatcher { - template - static inline void run(const PackedFunc& pf, Args&& ...args) { + template + static inline void run(const PackedFunc& pf, Args&&... args) { pf(std::forward(args)...); } }; } // namespace detail -template -TypedPackedFunc::TypedPackedFunc(PackedFunc packed) - : packed_(packed) {} +template +TypedPackedFunc::TypedPackedFunc(PackedFunc packed) : packed_(packed) {} -template +template TypedPackedFunc::TypedPackedFunc(const TVMRetValue& value) : packed_(value.operator PackedFunc()) {} -template +template TypedPackedFunc::TypedPackedFunc(const TVMArgValue& value) : packed_(value.operator PackedFunc()) {} -template -template +template +template inline void TypedPackedFunc::AssignTypedLambda(FType flambda) { packed_ = PackedFunc([flambda](const TVMArgs& args, TVMRetValue* rv) { - detail::unpack_call(flambda, args, rv); - }); + detail::unpack_call(flambda, args, rv); + }); } -template +template inline R TypedPackedFunc::operator()(Args... args) const { - return detail::typed_packed_call_dispatcher - ::run(packed_, std::forward(args)...); + return detail::typed_packed_call_dispatcher::run(packed_, std::forward(args)...); } // extension and node type handling namespace detail { -template +template struct TVMValueCast { static T Apply(const TSrc* self) { static_assert(!is_ext && !is_nd, "The default case accepts only non-extensions"); @@ -1300,64 +1214,48 @@ struct TVMValueCast { } }; -template +template struct TVMValueCast { - static T Apply(const TSrc* self) { - return self->template AsExtension(); - } + static T Apply(const TSrc* self) { return self->template AsExtension(); } }; -template +template struct TVMValueCast { - static T Apply(const TSrc* self) { - return self->template AsNDArray(); - } + static T Apply(const TSrc* self) { return self->template AsNDArray(); } }; } // namespace detail -template +template inline TVMArgValue::operator T() const { - return detail:: - TVMValueCast::code != 0), - (array_type_info::code > 0)> - ::Apply(this); + return detail::TVMValueCast::code != 0), + (array_type_info::code > 0)>::Apply(this); } -template +template inline TVMRetValue::operator T() const { - return detail:: - TVMValueCast::code != 0), - (array_type_info::code > 0)> - ::Apply(this); + return detail::TVMValueCast::code != 0), + (array_type_info::code > 0)>::Apply(this); } -template +template inline void TVMArgsSetter::operator()(size_t i, const T& value) const { - static_assert(extension_type_info::code != 0, - "Need to have extesion code"); + static_assert(extension_type_info::code != 0, "Need to have extesion code"); type_codes_[i] = extension_type_info::code; values_[i].v_handle = const_cast(&value); } // extension type handling -template +template struct ExtTypeInfo { - static void destroy(void* handle) { - delete static_cast(handle); - } - static void* clone(void* handle) { - return new T(*static_cast(handle)); - } + static void destroy(void* handle) { delete static_cast(handle); } + static void* clone(void* handle) { return new T(*static_cast(handle)); } }; -template +template inline ExtTypeVTable* ExtTypeVTable::Register_() { const int code = extension_type_info::code; - static_assert(code != 0, - "require extension_type_info traits to be declared with non-zero code"); + static_assert(code != 0, "require extension_type_info traits to be declared with non-zero code"); ExtTypeVTable vt; vt.clone = ExtTypeInfo::clone; vt.destroy = ExtTypeInfo::destroy; diff --git a/src/contrib/random/random.cc b/src/contrib/random/random.cc index f465902d437d..90a2d811d393 100644 --- a/src/contrib/random/random.cc +++ b/src/contrib/random/random.cc @@ -26,11 +26,7 @@ #include #include #include -#ifndef _LIBCPP_SGX_CONFIG #include "mt_random_engine.cc" -#else -#include "sgx_random_engine.cc" -#endif #define DLPACK_INTEGER_TYPE_SWITCH(type, DType, ...) \ if (type.code == kDLInt && type.bits == 32) { \ diff --git a/src/contrib/random/sgx_random_engine.cc b/src/contrib/random/sgx_random_engine.cc deleted file mode 100644 index 98e2b616bb69..000000000000 --- a/src/contrib/random/sgx_random_engine.cc +++ /dev/null @@ -1,309 +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. - */ - -/*! - * Copyright (c) 2018 by Contributors - * \file random/sgx_random_engine.h - * \brief SGX trusted random engine - */ -#include -#include -#include -#include -#include "../../runtime/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(&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(std::numeric_limits::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(data->data), size, [&] () { - float max_int = static_cast(std::numeric_limits::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(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 diff --git a/src/runtime/c_runtime_api.cc b/src/runtime/c_runtime_api.cc index 20793b4618b3..6436346f607e 100644 --- a/src/runtime/c_runtime_api.cc +++ b/src/runtime/c_runtime_api.cc @@ -29,12 +29,7 @@ #include #include #include -#ifdef _LIBCPP_SGX_CONFIG -#include "sgx/trusted/runtime.h" -#endif -#ifndef _LIBCPP_SGX_NO_IOSTREAMS #include -#endif #include #include #include @@ -174,7 +169,6 @@ void DeviceAPI::SyncStreamFromTo(TVMContext ctx, LOG(FATAL) << "Device does not support stream api."; } -#ifndef _LIBCPP_SGX_NO_IOSTREAMS //-------------------------------------------------------- // Error handling mechanism // ------------------------------------------------------- @@ -331,11 +325,6 @@ std::string NormalizeError(std::string err_msg) { return os.str(); } -#else -std::string NormalizeError(std::string err_msg) { - return err_msg; -} -#endif } // namespace runtime } // namespace tvm @@ -359,11 +348,7 @@ int TVMAPIHandleException(const std::runtime_error &e) { } void TVMAPISetLastError(const char* msg) { -#ifndef _LIBCPP_SGX_CONFIG TVMAPIRuntimeStore::Get()->last_error = msg; -#else - sgx::OCallPackedFunc("__sgx_set_last_error__", msg); -#endif } int TVMModLoadFromFile(const char* file_name, diff --git a/src/runtime/cpu_device_api.cc b/src/runtime/cpu_device_api.cc index 89cde0b7f151..2b815537c681 100644 --- a/src/runtime/cpu_device_api.cc +++ b/src/runtime/cpu_device_api.cc @@ -51,7 +51,7 @@ class CPUDeviceAPI final : public DeviceAPI { #if _MSC_VER ptr = _aligned_malloc(nbytes, alignment); 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); if (ptr == nullptr) throw std::bad_alloc(); #else diff --git a/src/runtime/file_util.cc b/src/runtime/file_util.cc index 189bd0cbc3aa..5950b8011857 100644 --- a/src/runtime/file_util.cc +++ b/src/runtime/file_util.cc @@ -74,7 +74,6 @@ std::string GetFileFormat(const std::string& file_name, const std::string& format) { std::string fmt = format; if (fmt.length() == 0) { - if (file_name.find(".signed.so") != std::string::npos) return "sgx"; size_t pos = file_name.find_last_of("."); if (pos != std::string::npos) { return file_name.substr(pos + 1, file_name.length() - pos - 1); diff --git a/src/runtime/graph/graph_runtime.cc b/src/runtime/graph/graph_runtime.cc index 38016ab87cbb..d06f036523b0 100644 --- a/src/runtime/graph/graph_runtime.cc +++ b/src/runtime/graph/graph_runtime.cc @@ -68,11 +68,7 @@ void GraphRuntime::Run() { void GraphRuntime::Init(const std::string& graph_json, tvm::runtime::Module module, const std::vector& ctxs) { -#ifndef _LIBCPP_SGX_NO_IOSTREAMS std::istringstream is(graph_json); -#else - std::string is = graph_json; -#endif dmlc::JSONReader reader(&is); this->Load(&reader); module_ = module; diff --git a/src/runtime/module.cc b/src/runtime/module.cc index c0acb315a04f..e5f216daf959 100644 --- a/src/runtime/module.cc +++ b/src/runtime/module.cc @@ -27,9 +27,7 @@ #include #include #include -#ifndef _LIBCPP_SGX_CONFIG #include "file_util.h" -#endif namespace tvm { namespace runtime { @@ -65,7 +63,6 @@ void Module::Import(Module other) { Module Module::LoadFromFile(const std::string& file_name, const std::string& format) { -#ifndef _LIBCPP_SGX_CONFIG std::string fmt = GetFileFormat(file_name, format); CHECK(fmt.length() != 0) << "Cannot deduce format of file " << file_name; @@ -79,9 +76,6 @@ Module Module::LoadFromFile(const std::string& file_name, << load_f_name << ") is not presented."; Module m = (*f)(file_name, format); return m; -#else - LOG(FATAL) << "SGX does not support LoadFromFile"; -#endif } void ModuleNode::SaveToFile(const std::string& file_name, diff --git a/src/runtime/module_util.cc b/src/runtime/module_util.cc index 456d28278abc..b6b496a8ad56 100644 --- a/src/runtime/module_util.cc +++ b/src/runtime/module_util.cc @@ -22,9 +22,7 @@ * \file module_util.cc * \brief Utilities for module. */ -#ifndef _LIBCPP_SGX_CONFIG #include -#endif #include #include #include @@ -35,7 +33,6 @@ namespace tvm { namespace runtime { void ImportModuleBlob(const char* mblob, std::vector* mlist) { -#ifndef _LIBCPP_SGX_CONFIG CHECK(mblob != nullptr); uint64_t nbytes = 0; for (size_t i = 0; i < sizeof(nbytes); ++i) { @@ -58,9 +55,6 @@ void ImportModuleBlob(const char* mblob, std::vector* mlist) { Module m = (*f)(static_cast(stream)); mlist->push_back(m); } -#else - LOG(FATAL) << "SGX does not support ImportModuleBlob"; -#endif } PackedFunc WrapPackedFunc(BackendPackedCFunc faddr, diff --git a/src/runtime/sgx/common.h b/src/runtime/sgx/common.h deleted file mode 100644 index 43e295f2b388..000000000000 --- a/src/runtime/sgx/common.h +++ /dev/null @@ -1,44 +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. - */ - -/*! - * Copyright (c) 2018 by Contributors - * \file common.h - * \brief TVM SGX common API. - */ -#ifndef TVM_RUNTIME_SGX_COMMON_H_ -#define TVM_RUNTIME_SGX_COMMON_H_ - -#include - -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_ diff --git a/src/runtime/sgx/trusted/ecall_registry.h b/src/runtime/sgx/trusted/ecall_registry.h deleted file mode 100644 index b4f3d604a872..000000000000 --- a/src/runtime/sgx/trusted/ecall_registry.h +++ /dev/null @@ -1,110 +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. - */ - -/*! - * Copyright (c) 2018 by Contributors - * \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 -#include -#include -#include -#include - -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 ListNames() { - std::vector names; - names.resize(exports_.size()); - std::transform(exports_.begin(), exports_.end(), names.begin(), - [](ECallRegistry r) { return r.name_; }); - return names; - } - - static std::vector exports_; -}; - -std::vector 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_ diff --git a/src/runtime/sgx/trusted/runtime.cc b/src/runtime/sgx/trusted/runtime.cc deleted file mode 100644 index f4d1e5765539..000000000000 --- a/src/runtime/sgx/trusted/runtime.cc +++ /dev/null @@ -1,94 +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. - */ - -/*! - * Copyright (c) 2018 by Contributors - * \file runtime_t.cc - */ -#include -#include -#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 == kNull) return; - - TVMValue ret_value; - if (ret_type_code == kBytes || ret_type_code == kStr) { - // 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(ret_buf) + sizeof(TVMByteArray); - memcpy(data_buf, bytes.data(), bytes.size()); - - TVMByteArray* arr = static_cast(ret_buf); - arr->data = data_buf; - arr->size = bytes.size(); - - ret_value = TVMValue{.v_handle = arr}; - ret_type_code = kBytes; - } 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("module._GetSystemLib"))(); - mod.GetFunction("default_function").CallPacked(args, rv); -}); - -} // namespace sgx -} // namespace runtime -} // namespace tvm diff --git a/src/runtime/sgx/trusted/runtime.h b/src/runtime/sgx/trusted/runtime.h deleted file mode 100644 index 1ba9eb7351b6..000000000000 --- a/src/runtime/sgx/trusted/runtime.h +++ /dev/null @@ -1,62 +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. - */ - -/*! - * Copyright (c) 2018 by Contributors - * \file trusted/runtime.h - * \brief TVM SGX trusted API. - */ -#ifndef TVM_RUNTIME_SGX_TRUSTED_RUNTIME_H_ -#define TVM_RUNTIME_SGX_TRUSTED_RUNTIME_H_ - -#include -#include -#include -#include "../common.h" - -namespace tvm { -namespace runtime { -namespace sgx { - -template -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)...); - 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_ diff --git a/src/runtime/sgx/trusted/threading_backend.cc b/src/runtime/sgx/trusted/threading_backend.cc deleted file mode 100644 index f48759439d75..000000000000 --- a/src/runtime/sgx/trusted/threading_backend.cc +++ /dev/null @@ -1,97 +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. - */ - -/*! - * Copyright (c) 2018 by Contributors - * \file sgx/threading_backend.cc - * \brief SGX threading backend - */ -#include -#include -#include -#include -#include -#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 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(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 worker_callback_; - std::atomic next_task_id_; -}; - -ThreadGroup::ThreadGroup(int num_workers, - std::function 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(tg)->RunTask(); - }); - -} // namespace threading -} // namespace runtime -} // namespace tvm diff --git a/src/runtime/sgx/tvm.edl b/src/runtime/sgx/tvm.edl deleted file mode 100644 index 8466d78af72f..000000000000 --- a/src/runtime/sgx/tvm.edl +++ /dev/null @@ -1,26 +0,0 @@ -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); - }; -}; - diff --git a/src/runtime/sgx/untrusted/sgx_module.cc b/src/runtime/sgx/untrusted/sgx_module.cc deleted file mode 100644 index b6aa48c0dcc9..000000000000 --- a/src/runtime/sgx/untrusted/sgx_module.cc +++ /dev/null @@ -1,255 +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. - */ - -/*! - * Copyright (c) 2018 by Contributors - * \file sgx_module.cc - * \brief SGX enclave module. - */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#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& 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 runner = [this](int _worker_id) { - this->GetFunction("__tvm_run_worker__", - std::shared_ptr(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 exports_; - std::unique_ptr 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(args[i]); break; - case kDLUInt: msg << static_cast(args[i]); break; - case kDLFloat: msg << static_cast(args[i]); break; - case kStr: - case kBytes: { - 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("module.loadfile_sgx") -.set_body([](TVMArgs args, TVMRetValue* rv) { - std::shared_ptr node = std::make_shared(); - node->Init(args[0]); - *rv = runtime::Module(node); -}); - -} // namespace runtime -} // namespace tvm diff --git a/src/runtime/thread_pool.cc b/src/runtime/thread_pool.cc index 2e101364db2a..c5556eb17e60 100644 --- a/src/runtime/thread_pool.cc +++ b/src/runtime/thread_pool.cc @@ -103,16 +103,14 @@ class ParallelLauncher { tvm::runtime::threading::Yield(); } if (!has_error_.load()) return 0; - // the following is intended to use string due to - // security issue raised in SGX backend - std::string err(""); + std::ostringstream os; for (size_t i = 0; i < par_errors_.size(); ++i) { 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(); } } - TVMAPISetLastError(err.c_str()); + TVMAPISetLastError(os.str().c_str()); return -1; } // Signal that one job has finished. @@ -367,11 +365,7 @@ class ThreadPool { // number of workers used (can be restricted with affinity pref) int num_workers_used_; // if excluding worker 0 and using master to run task 0 -#ifndef _LIBCPP_SGX_CONFIG bool exclude_worker0_{true}; -#else - bool exclude_worker0_{false}; -#endif std::vector > queues_; std::unique_ptr threads_; }; From 43ac98d55ec0272b2ddcf0c63e2af751403e9797 Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Sun, 24 Mar 2019 06:25:53 +0000 Subject: [PATCH 02/18] Remove SGX from Rust --- rust/runtime/Cargo.toml | 26 ++++----- rust/runtime/src/allocator.rs | 3 - rust/runtime/src/errors.rs | 6 -- rust/runtime/src/lib.rs | 10 ---- rust/runtime/src/sgx.rs | 101 ---------------------------------- rust/runtime/src/threading.rs | 81 +-------------------------- 6 files changed, 12 insertions(+), 215 deletions(-) delete mode 100644 rust/runtime/src/sgx.rs diff --git a/rust/runtime/Cargo.toml b/rust/runtime/Cargo.toml index 3c81a93c9bbf..14b974c305bf 100644 --- a/rust/runtime/Cargo.toml +++ b/rust/runtime/Cargo.toml @@ -27,25 +27,19 @@ categories = ["api-bindings", "science"] authors = ["TVM Contributors"] edition = "2018" -[features] -default = ["nom/std"] -sgx = ["nom/alloc"] - [dependencies] -bounded-spsc-queue = "0.4.0" -failure = "0.1.5" -itertools = "0.7.8" -lazy_static = "1.1.0" -ndarray="0.12.1" -nom = {version = "4.0.0", default-features = false } -serde = "1.0.59" -serde_derive = "1.0.79" -serde_json = "1.0.17" +bounded-spsc-queue = "0.4" +failure = "0.1" +itertools = "0.7" +lazy_static = "1.1" +ndarray="0.12" +nom = "4.0" +num_cpus = "1.8" +serde = "1.0" +serde_derive = "1.0" +serde_json = "1.0" tvm-common = { version = "0.1", path = "../common" } 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] libloading = "0.5" diff --git a/rust/runtime/src/allocator.rs b/rust/runtime/src/allocator.rs index 97634e61e05c..e26c7f1b8c47 100644 --- a/rust/runtime/src/allocator.rs +++ b/rust/runtime/src/allocator.rs @@ -17,9 +17,6 @@ * under the License. */ -#[cfg(target_env = "sgx")] -use alloc::alloc::{self, Layout, LayoutErr}; -#[cfg(not(target_env = "sgx"))] use std::alloc::{self, Layout, LayoutErr}; const DEFAULT_ALIGN_BYTES: usize = 4; diff --git a/rust/runtime/src/errors.rs b/rust/runtime/src/errors.rs index d8ee5930b213..a7d0f5b49066 100644 --- a/rust/runtime/src/errors.rs +++ b/rust/runtime/src/errors.rs @@ -30,9 +30,3 @@ pub enum GraphFormatError { #[fail(display = "Invalid DLType: {}", 0)] InvalidDLType(String), } - -#[derive(Debug, Fail)] -#[fail(display = "SGX error: 0x{:x}", code)] -pub struct SgxError { - pub code: u32, -} diff --git a/rust/runtime/src/lib.rs b/rust/runtime/src/lib.rs index 010fbf7d6a29..3944fd5d84c3 100644 --- a/rust/runtime/src/lib.rs +++ b/rust/runtime/src/lib.rs @@ -36,11 +36,7 @@ vec_remove_item )] -#[cfg(target_env = "sgx")] -extern crate alloc; extern crate bounded_spsc_queue; -#[cfg(target_env = "sgx")] -extern crate core; #[macro_use] extern crate failure; #[macro_use] @@ -50,7 +46,6 @@ extern crate lazy_static; extern crate ndarray; #[macro_use] extern crate nom; -#[cfg(not(target_env = "sgx"))] extern crate num_cpus; extern crate serde; #[macro_use] @@ -63,9 +58,6 @@ mod array; pub mod errors; mod graph; mod module; -#[cfg(target_env = "sgx")] -#[macro_use] -pub mod sgx; mod threading; mod workspace; @@ -88,8 +80,6 @@ lazy_static! { #[no_mangle] pub extern "C" fn TVMAPISetLastError(cmsg: *const i8) { *LAST_ERROR.write().unwrap() = Some(unsafe { std::ffi::CStr::from_ptr(cmsg) }); - #[cfg(target_env = "sgx")] - ocall_packed!("__sgx_set_last_error__", cmsg); } #[no_mangle] diff --git a/rust/runtime/src/sgx.rs b/rust/runtime/src/sgx.rs deleted file mode 100644 index 3c1768518b96..000000000000 --- a/rust/runtime/src/sgx.rs +++ /dev/null @@ -1,101 +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>( - fn_name: S, - args: &[TVMArgValue], -) -> Result { - 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::>() - .as_ptr(), - args.iter() - .map(|ref arg| arg.type_code as i32) - .collect::>() - .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() - } -} diff --git a/rust/runtime/src/threading.rs b/rust/runtime/src/threading.rs index eb2f418473ed..5838188a91e5 100644 --- a/rust/runtime/src/threading.rs +++ b/rust/runtime/src/threading.rs @@ -18,30 +18,18 @@ */ use std::{ + env, os::raw::{c_int, c_void}, sync::{ atomic::{AtomicUsize, Ordering}, Arc, Barrier, }, -}; - -#[cfg(not(target_env = "sgx"))] -use num_cpus; -#[cfg(not(target_env = "sgx"))] -use std::{ - env, thread::{self, JoinHandle}, }; -#[cfg(target_env = "sgx")] -use std::{collections::VecDeque, ptr, sync::Mutex}; - use bounded_spsc_queue::{self, Producer}; use tvm_common::ffi::TVMParallelGroupEnv; -#[cfg(target_env = "sgx")] -use super::{TVMArgValue, TVMRetValue}; - pub(crate) type FTVMParallelLambda = extern "C" fn(task_id: usize, penv: *const TVMParallelGroupEnv, cdata: *const c_void) -> i32; @@ -82,7 +70,6 @@ impl Job { /// Waits for all tasks in this `Job` to be completed. fn wait(&self) { while self.pending.load(Ordering::Acquire) > 0 { - #[cfg(not(target_env = "sgx"))] thread::yield_now(); } } @@ -111,13 +98,11 @@ impl FnOnce<()> for Task { #[derive(Default)] struct Threads { #[allow(unused)] - #[cfg(not(target_env = "sgx"))] handles: Vec>, queues: Vec>, } impl<'a> Threads { - #[cfg(not(target_env = "sgx"))] fn launch) + 'static + Copy>( num_threads: usize, cb: F, @@ -134,23 +119,6 @@ impl<'a> Threads { queues: queues, } } - - #[cfg(target_env = "sgx")] - fn launch) + '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 } - } } struct ThreadPool { @@ -211,13 +179,7 @@ impl Consumer { unsafe impl Send for Consumer {} unsafe impl Sync for Consumer {} -#[cfg(target_env = "sgx")] -lazy_static! { - /// Holds tasks for untrusted threads which re-enter the enclave to execute. - static ref SGX_QUEUES: Mutex>> = Mutex::new(VecDeque::new()); -} - -#[cfg(all(not(target_arch = "wasm32"), not(target_env = "sgx")))] +#[cfg(not(target_arch = "wasm32"))] fn max_concurrency() -> usize { if let Ok(threads_str) = env::var("TVM_NUM_THREADS").or(env::var("OMP_NUM_THREADS")) { if let Ok(threads) = usize::from_str_radix(&threads_str, 10) { @@ -227,29 +189,11 @@ fn max_concurrency() -> usize { num_cpus::get_physical() } -#[cfg(target_env = "sgx")] -fn max_concurrency() -> usize { - usize::from_str_radix(env!("TVM_NUM_THREADS"), 10).unwrap_or(1) -} - #[cfg(target_arch = "wasm32")] fn max_concurrency() -> usize { 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] pub extern "C" fn TVMBackendParallelLaunch( cb: FTVMParallelLambda, @@ -275,27 +219,6 @@ pub extern "C" fn TVMBackendParallelLaunch( return 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 { - ::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 https://github.com/dmlc/tvm/issues/988 for information on why this function is used. #[no_mangle] pub extern "C" fn TVMBackendParallelBarrier(_task_id: usize, penv: *const TVMParallelGroupEnv) { From 4959522d32f3bf8337c3b9b72beba0ef0200519c Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Sun, 24 Mar 2019 07:58:54 +0000 Subject: [PATCH 03/18] Use Fortanix SGX --- apps/sgx/.cargo/config | 2 + apps/sgx/.gitignore | 1 - apps/sgx/.rustfmt.toml | 59 ++++++++++ apps/sgx/Cargo.toml | 11 ++ apps/sgx/Makefile | 106 ------------------ apps/sgx/README.md | 78 ------------- apps/sgx/build.rs | 24 ++++ apps/sgx/enclave/.rustfmt.toml | 1 - apps/sgx/enclave/enclave.lds | 9 -- apps/sgx/enclave/enclave_config.xml.in | 11 -- apps/sgx/enclave/sgx-deps.diff | 13 --- .../sgx/enclave/x86_64-unknown-linux-sgx.json | 31 ----- apps/sgx/{enclave => }/src/build_model.py | 15 +-- apps/sgx/src/main.rs | 31 +++++ rust/runtime/src/allocator.rs | 10 ++ rust/runtime/src/array.rs | 24 ++++ rust/runtime/src/graph.rs | 12 +- 17 files changed, 175 insertions(+), 263 deletions(-) create mode 100644 apps/sgx/.cargo/config delete mode 100644 apps/sgx/.gitignore create mode 100644 apps/sgx/.rustfmt.toml create mode 100644 apps/sgx/Cargo.toml delete mode 100644 apps/sgx/Makefile delete mode 100644 apps/sgx/README.md create mode 100644 apps/sgx/build.rs delete mode 120000 apps/sgx/enclave/.rustfmt.toml delete mode 100644 apps/sgx/enclave/enclave.lds delete mode 100644 apps/sgx/enclave/enclave_config.xml.in delete mode 100644 apps/sgx/enclave/sgx-deps.diff delete mode 100644 apps/sgx/enclave/x86_64-unknown-linux-sgx.json rename apps/sgx/{enclave => }/src/build_model.py (87%) mode change 100644 => 100755 create mode 100644 apps/sgx/src/main.rs diff --git a/apps/sgx/.cargo/config b/apps/sgx/.cargo/config new file mode 100644 index 000000000000..6a8d6b2fc9f0 --- /dev/null +++ b/apps/sgx/.cargo/config @@ -0,0 +1,2 @@ +[build] +target = "x86_64-fortanix-unknown-sgx" diff --git a/apps/sgx/.gitignore b/apps/sgx/.gitignore deleted file mode 100644 index c3af857904eb..000000000000 --- a/apps/sgx/.gitignore +++ /dev/null @@ -1 +0,0 @@ -lib/ diff --git a/apps/sgx/.rustfmt.toml b/apps/sgx/.rustfmt.toml new file mode 100644 index 000000000000..3db835c7fd93 --- /dev/null +++ b/apps/sgx/.rustfmt.toml @@ -0,0 +1,59 @@ +max_width = 100 +hard_tabs = false +tab_spaces = 4 +newline_style = "Auto" +use_small_heuristics = "Default" +indent_style = "Block" +wrap_comments = false +comment_width = 80 +normalize_comments = false +format_strings = false +format_macro_matchers = false +format_macro_bodies = true +empty_item_single_line = true +struct_lit_single_line = true +fn_single_line = false +where_single_line = false +imports_indent = "Block" +imports_layout = "Mixed" +merge_imports = true +reorder_imports = true +reorder_modules = true +reorder_impl_items = false +type_punctuation_density = "Wide" +space_before_colon = false +space_after_colon = true +spaces_around_ranges = false +binop_separator = "Front" +remove_nested_parens = true +combine_control_expr = true +struct_field_align_threshold = 0 +match_arm_blocks = true +force_multiline_blocks = false +fn_args_density = "Tall" +brace_style = "SameLineWhere" +control_brace_style = "AlwaysSameLine" +trailing_semicolon = true +trailing_comma = "Vertical" +match_block_trailing_comma = false +blank_lines_upper_bound = 1 +blank_lines_lower_bound = 0 +edition = "2018" +merge_derives = true +use_try_shorthand = true +use_field_init_shorthand = false +force_explicit_abi = true +condense_wildcard_suffixes = false +color = "Auto" +required_version = "1.0.3" +unstable_features = false +disable_all_formatting = false +skip_children = false +hide_parse_errors = false +error_on_line_overflow = true +error_on_unformatted = true +report_todo = "Never" +report_fixme = "Never" +ignore = [] +emit_mode = "Files" +make_backup = false diff --git a/apps/sgx/Cargo.toml b/apps/sgx/Cargo.toml new file mode 100644 index 000000000000..e3bac108d9ad --- /dev/null +++ b/apps/sgx/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "sgx-demo" +version = "0.1.0" +authors = ["Nick Hynes "] +edition = "2018" + +[dependencies] +tvm-runtime = { path = "../../rust/runtime" } + +[build-dependencies] +ar = "0.6" diff --git a/apps/sgx/Makefile b/apps/sgx/Makefile deleted file mode 100644 index c867d65e9daa..000000000000 --- a/apps/sgx/Makefile +++ /dev/null @@ -1,106 +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. - -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 diff --git a/apps/sgx/README.md b/apps/sgx/README.md deleted file mode 100644 index 10dbcd94c586..000000000000 --- a/apps/sgx/README.md +++ /dev/null @@ -1,78 +0,0 @@ - - - - - - - - - - - - - - - - - -# TVM in Intel SGX Example - -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. - -## Prerequisites - -1. The TVM premade Docker image - -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/dmlc/tvm.git -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 -e nnvm/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/) diff --git a/apps/sgx/build.rs b/apps/sgx/build.rs new file mode 100644 index 000000000000..6c2903d0059f --- /dev/null +++ b/apps/sgx/build.rs @@ -0,0 +1,24 @@ +fn main() { + let out_dir = std::env::var("OUT_DIR").unwrap(); + + let output = + std::process::Command::new(concat!(env!("CARGO_MANIFEST_DIR"), "/src/build_model.py")) + .arg(&out_dir) + .output() + .expect("Failed to execute command"); + assert!( + std::path::Path::new(&format!("{}/model.o", out_dir)).exists(), + "Could not build tvm lib: {}", + String::from_utf8(output.stderr).unwrap().trim() + ); + + std::process::Command::new("llvm-ar-8") + .arg("rcs") + .arg(&format!("{}/libmodel.a", out_dir)) + .arg(&format!("{}/model.o", out_dir)) + .output() + .expect("Failed to execute command"); + + println!("cargo:rustc-link-lib=static=model"); + println!("cargo:rustc-link-search=native={}", out_dir); +} diff --git a/apps/sgx/enclave/.rustfmt.toml b/apps/sgx/enclave/.rustfmt.toml deleted file mode 120000 index ec1baa2f89be..000000000000 --- a/apps/sgx/enclave/.rustfmt.toml +++ /dev/null @@ -1 +0,0 @@ -../../../rust/.rustfmt.toml \ No newline at end of file diff --git a/apps/sgx/enclave/enclave.lds b/apps/sgx/enclave/enclave.lds deleted file mode 100644 index e3d9d0ee0d90..000000000000 --- a/apps/sgx/enclave/enclave.lds +++ /dev/null @@ -1,9 +0,0 @@ -enclave.so -{ - global: - g_global_data_sim; - g_global_data; - enclave_entry; - local: - *; -}; diff --git a/apps/sgx/enclave/enclave_config.xml.in b/apps/sgx/enclave/enclave_config.xml.in deleted file mode 100644 index 630c84c2cc31..000000000000 --- a/apps/sgx/enclave/enclave_config.xml.in +++ /dev/null @@ -1,11 +0,0 @@ - - 0 - 0 - 0xf0000 - 0xf000000 - NUM_THREADS - 0 - 0 - 0 - 0xFFFFFFFF - diff --git a/apps/sgx/enclave/sgx-deps.diff b/apps/sgx/enclave/sgx-deps.diff deleted file mode 100644 index 1c67e7957f38..000000000000 --- a/apps/sgx/enclave/sgx-deps.diff +++ /dev/null @@ -1,13 +0,0 @@ -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" diff --git a/apps/sgx/enclave/x86_64-unknown-linux-sgx.json b/apps/sgx/enclave/x86_64-unknown-linux-sgx.json deleted file mode 100644 index 6cbb524f4439..000000000000 --- a/apps/sgx/enclave/x86_64-unknown-linux-sgx.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "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" -} diff --git a/apps/sgx/enclave/src/build_model.py b/apps/sgx/src/build_model.py old mode 100644 new mode 100755 similarity index 87% rename from apps/sgx/enclave/src/build_model.py rename to apps/sgx/src/build_model.py index 5a6b10cfcd38..5542abdf2174 --- a/apps/sgx/enclave/src/build_model.py +++ b/apps/sgx/src/build_model.py @@ -1,3 +1,4 @@ +<<<<<<< HEAD:apps/sgx/enclave/src/build_model.py # 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 @@ -14,11 +15,15 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +======= +#!/usr/bin/python3 + +>>>>>>> cec2cf25... Use Fortanix SGX:apps/sgx/src/build_model.py """Creates a simple TVM modules.""" -import argparse import os from os import path as osp +import sys import nnvm.compiler import nnvm.testing @@ -26,10 +31,6 @@ def main(): - parser = argparse.ArgumentParser() - parser.add_argument('-o', '--out-dir', default='.') - opts = parser.parse_args() - # from tutorials/nnvm_quick_start.py dshape = (1, 3, 224, 224) net, params = nnvm.testing.resnet.get_workload( @@ -39,11 +40,11 @@ def main(): graph, lib, params = nnvm.compiler.build( net, 'llvm --system-lib', shape={'data': dshape}, params=params) - build_dir = osp.abspath(opts.out_dir) + build_dir = osp.abspath(sys.argv[1]) if not osp.isdir(build_dir): 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: f_graph_json.write(graph.json()) with open(osp.join(build_dir, 'params.bin'), 'wb') as f_params: diff --git a/apps/sgx/src/main.rs b/apps/sgx/src/main.rs new file mode 100644 index 000000000000..d937e31a1345 --- /dev/null +++ b/apps/sgx/src/main.rs @@ -0,0 +1,31 @@ +extern crate tvm_runtime; + +use std::{ + convert::TryFrom as _, + io::{Read as _, Write as _}, +}; + +fn main() { + let syslib = tvm_runtime::SystemLibModule::default(); + + let graph_json = include_str!(concat!(env!("OUT_DIR"), "/graph.json")); + let params_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/params.bin")); + 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); + + // this is actually unsafe but is necessary until tensors can be constructed from bytes + let mut input_bytes = exec.get_input("data").unwrap().data().view(); + + let listener = std::net::TcpListener::bind("127.0.0.1:4242").unwrap(); + for stream in listener.incoming() { + let mut stream = stream.unwrap(); + stream.read_exact(input_bytes.as_mut_slice()).unwrap(); + exec.run(); + stream + .write_all(exec.get_output(0).unwrap().data().as_slice()) + .unwrap(); + } +} diff --git a/rust/runtime/src/allocator.rs b/rust/runtime/src/allocator.rs index e26c7f1b8c47..00d94bfe4b1e 100644 --- a/rust/runtime/src/allocator.rs +++ b/rust/runtime/src/allocator.rs @@ -55,6 +55,16 @@ impl Allocation { pub fn align(&self) -> usize { 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 { diff --git a/rust/runtime/src/array.rs b/rust/runtime/src/array.rs index 3de7a8590fde..0fb588b7db8c 100644 --- a/rust/runtime/src/array.rs +++ b/rust/runtime/src/array.rs @@ -101,6 +101,22 @@ impl<'a> Storage<'a> { } 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> { @@ -154,6 +170,14 @@ impl<'a> Tensor<'a> { 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`. /// /// # Panics diff --git a/rust/runtime/src/graph.rs b/rust/runtime/src/graph.rs index cacd7a38a97f..61e603fa7645 100644 --- a/rust/runtime/src/graph.rs +++ b/rust/runtime/src/graph.rs @@ -162,15 +162,15 @@ impl<'a> TryFrom<&'a str> for Graph { /// /// println!("{:#?}", Array::try_from(output).unwrap()); /// ``` -pub struct GraphExecutor<'m, 't> { +pub struct GraphExecutor<'m> { graph: Graph, op_execs: Vec>, tensors: Vec>, } -unsafe impl<'m, 't> Send for GraphExecutor<'m, 't> {} +unsafe impl<'m> Send for GraphExecutor<'m> {} -impl<'m, 't> GraphExecutor<'m, 't> { +impl<'m> GraphExecutor<'m> { pub fn new(graph: Graph, lib: &'m M) -> Result { let tensors = Self::setup_storages(&graph)?; Ok(GraphExecutor { @@ -188,7 +188,7 @@ impl<'m, 't> GraphExecutor<'m, 't> { } /// Allocates `Storages` for each `storage_id` and returns `Tensor`s to hold each output. - fn setup_storages<'a>(graph: &'a Graph) -> Result>, Error> { + fn setup_storages<'a>(graph: &'a Graph) -> Result>, Error> { let storage_ids = graph.get_attr::<(String, Vec)>("storage_id")?.1; let shapes = graph.get_attr::<(String, Vec>)>("shape")?.1; let dtypes = graph @@ -319,8 +319,8 @@ impl<'m, 't> GraphExecutor<'m, 't> { } } - /// Returns the graph input with name `name`, if it exists. - pub fn get_input>(&mut self, name: S) -> Option<&Tensor> { + /// Returns a reference to the graph input with name `name`, if it exists. + pub fn get_input>(&self, name: S) -> Option<&Tensor> { self.get_input_index(name.as_ref()) .and_then(move |idx| Some(&self.tensors[idx])) } From 20074e9193d0e27bc11edc4cbcca0c80ff26f3a9 Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Mon, 25 Mar 2019 02:09:46 -0500 Subject: [PATCH 04/18] Update demo --- apps/sgx/Cargo.toml | 3 --- apps/sgx/build.rs | 14 +++++++++++--- apps/sgx/src/build_model.py | 22 ++++++++++------------ apps/sgx/src/main.rs | 10 ++++++---- rust/runtime/src/module/syslib.rs | 7 +++++++ rust/runtime/src/threading.rs | 4 ++-- 6 files changed, 36 insertions(+), 24 deletions(-) diff --git a/apps/sgx/Cargo.toml b/apps/sgx/Cargo.toml index e3bac108d9ad..2a299a4f2269 100644 --- a/apps/sgx/Cargo.toml +++ b/apps/sgx/Cargo.toml @@ -6,6 +6,3 @@ edition = "2018" [dependencies] tvm-runtime = { path = "../../rust/runtime" } - -[build-dependencies] -ar = "0.6" diff --git a/apps/sgx/build.rs b/apps/sgx/build.rs index 6c2903d0059f..8836f2983579 100644 --- a/apps/sgx/build.rs +++ b/apps/sgx/build.rs @@ -7,17 +7,25 @@ fn main() { .output() .expect("Failed to execute command"); assert!( - std::path::Path::new(&format!("{}/model.o", out_dir)).exists(), + ["model.o", "graph.json", "params.bin"].iter().all(|f| { + std::path::Path::new(&format!("{}/{}", out_dir, f)).exists() + }), "Could not build tvm lib: {}", String::from_utf8(output.stderr).unwrap().trim() ); - std::process::Command::new("llvm-ar-8") + std::process::Command::new("objcopy") + .arg("--globalize-symbol=__tvm_module_startup") + .arg(&format!("{}/model.o", out_dir)) + .output() + .expect("Could not gloablize startup function."); + + std::process::Command::new("llvm-ar") .arg("rcs") .arg(&format!("{}/libmodel.a", out_dir)) .arg(&format!("{}/model.o", out_dir)) .output() - .expect("Failed to execute command"); + .expect("Failed to package model archive."); println!("cargo:rustc-link-lib=static=model"); println!("cargo:rustc-link-search=native={}", out_dir); diff --git a/apps/sgx/src/build_model.py b/apps/sgx/src/build_model.py index 5542abdf2174..793bd76de26b 100755 --- a/apps/sgx/src/build_model.py +++ b/apps/sgx/src/build_model.py @@ -25,20 +25,18 @@ from os import path as osp import sys -import nnvm.compiler -import nnvm.testing import tvm +from tvm import relay +import tvm.relay.testing def main(): - # from tutorials/nnvm_quick_start.py - dshape = (1, 3, 224, 224) - net, params = nnvm.testing.resnet.get_workload( - layers=18, batch_size=dshape[0], image_shape=dshape[1:]) + dshape = (1, 28, 28) + net, params = relay.testing.mlp.get_workload(batch_size=dshape[0], dtype='float32') - with nnvm.compiler.build_config(opt_level=3): - graph, lib, params = nnvm.compiler.build( - net, 'llvm --system-lib', shape={'data': dshape}, params=params) + with relay.build_config(opt_level=3): + graph, lib, params = relay.build_module.build( + net, target='llvm --system-lib', params=params) build_dir = osp.abspath(sys.argv[1]) if not osp.isdir(build_dir): @@ -46,9 +44,9 @@ def main(): lib.save(osp.join(build_dir, 'model.o')) with open(osp.join(build_dir, 'graph.json'), 'w') as f_graph_json: - f_graph_json.write(graph.json()) - with open(osp.join(build_dir, 'params.bin'), 'wb') as f_params: - f_params.write(nnvm.compiler.save_param_dict(params)) + f_graph_json.write(graph) + with open(osp.join(build_dir, 'params.bin'), 'wb') as f_params: + f_params.write(relay.save_param_dict(params)) if __name__ == '__main__': diff --git a/apps/sgx/src/main.rs b/apps/sgx/src/main.rs index d937e31a1345..83aa99a94600 100644 --- a/apps/sgx/src/main.rs +++ b/apps/sgx/src/main.rs @@ -22,10 +22,12 @@ fn main() { let listener = std::net::TcpListener::bind("127.0.0.1:4242").unwrap(); for stream in listener.incoming() { let mut stream = stream.unwrap(); - stream.read_exact(input_bytes.as_mut_slice()).unwrap(); + if let Err(_) = stream.read_exact(input_bytes.as_mut_slice()) { + continue; + } exec.run(); - stream - .write_all(exec.get_output(0).unwrap().data().as_slice()) - .unwrap(); + if let Err(_) = stream.write_all(exec.get_output(0).unwrap().data().as_slice()) { + continue; + } } } diff --git a/rust/runtime/src/module/syslib.rs b/rust/runtime/src/module/syslib.rs index 227b8c727e8f..c727cf0dedc4 100644 --- a/rust/runtime/src/module/syslib.rs +++ b/rust/runtime/src/module/syslib.rs @@ -27,6 +27,11 @@ use super::Module; pub struct SystemLibModule; +#[cfg(target_env = "sgx")] +extern "C" { + fn __tvm_module_startup(); +} + lazy_static! { static ref SYSTEM_LIB_FUNCTIONS: Mutex> = Mutex::new(HashMap::new()); @@ -44,6 +49,8 @@ impl Module for SystemLibModule { impl Default for SystemLibModule { fn default() -> Self { + #[cfg(target_env = "sgx")] + unsafe { __tvm_module_startup(); } SystemLibModule {} } } diff --git a/rust/runtime/src/threading.rs b/rust/runtime/src/threading.rs index 5838188a91e5..5e4afe0732b0 100644 --- a/rust/runtime/src/threading.rs +++ b/rust/runtime/src/threading.rs @@ -186,7 +186,7 @@ fn max_concurrency() -> usize { return threads; } } - num_cpus::get_physical() + num_cpus::get() } #[cfg(target_arch = "wasm32")] @@ -200,7 +200,7 @@ pub extern "C" fn TVMBackendParallelLaunch( cdata: *const c_void, num_task: usize, ) -> c_int { - if max_concurrency() == 0 { + if max_concurrency() < 2 { let penv = TVMParallelGroupEnv { sync_handle: 0 as *mut c_void, num_task: 1, From 77cbf4b75902f105eb0783241ce7815eac4c2738 Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Wed, 15 May 2019 19:12:17 -0700 Subject: [PATCH 05/18] Rebase onto master --- apps/sgx/src/build_model.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/apps/sgx/src/build_model.py b/apps/sgx/src/build_model.py index 793bd76de26b..b26b05a24dd2 100755 --- a/apps/sgx/src/build_model.py +++ b/apps/sgx/src/build_model.py @@ -1,4 +1,5 @@ -<<<<<<< HEAD:apps/sgx/enclave/src/build_model.py +#!/usr/bin/python3 + # 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 @@ -15,10 +16,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -======= -#!/usr/bin/python3 ->>>>>>> cec2cf25... Use Fortanix SGX:apps/sgx/src/build_model.py """Creates a simple TVM modules.""" import os From 9b163a17072272617a484674ca905f2d850da901 Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Mon, 29 Jul 2019 01:24:44 +0000 Subject: [PATCH 06/18] Update Cargo.tomls --- rust/common/Cargo.toml | 6 +++--- rust/frontend/Cargo.toml | 8 ++++---- rust/frontend/examples/resnet/Cargo.toml | 6 +++--- rust/frontend/tests/basics/Cargo.toml | 2 +- rust/frontend/tests/callback/Cargo.toml | 2 +- rust/macros/Cargo.toml | 2 +- rust/runtime/src/graph.rs | 12 ++++++------ rust/runtime/tests/test_nnvm/Cargo.toml | 8 ++++---- rust/runtime/tests/test_tvm_basic/Cargo.toml | 4 ++-- 9 files changed, 25 insertions(+), 25 deletions(-) diff --git a/rust/common/Cargo.toml b/rust/common/Cargo.toml index 8a19ad0449f9..054499d53523 100644 --- a/rust/common/Cargo.toml +++ b/rust/common/Cargo.toml @@ -26,8 +26,8 @@ edition = "2018" bindings = [] [dependencies] -failure = "0.1.5" -ndarray = "0.12.1" +failure = "0.1" +ndarray = "0.12" [build-dependencies] -bindgen = "0.37.4" +bindgen = "0.37" diff --git a/rust/frontend/Cargo.toml b/rust/frontend/Cargo.toml index fa05c5695ebd..2f87611693b3 100644 --- a/rust/frontend/Cargo.toml +++ b/rust/frontend/Cargo.toml @@ -33,11 +33,11 @@ name = "tvm_frontend" crate-type = ["dylib"] [dependencies] -failure = "0.1.5" -lazy_static = "1.1.0" -ndarray = "0.12.1" +failure = "0.1" +lazy_static = "1.1" +ndarray = "0.12" num-traits = "0.2" -tvm-common = { version = "0.1.0", path = "../common/", features = ["bindings"] } +tvm-common = { version = "0.1", path = "../common/", features = ["bindings"] } [features] blas = ["ndarray/blas"] diff --git a/rust/frontend/examples/resnet/Cargo.toml b/rust/frontend/examples/resnet/Cargo.toml index 0b547ba1458d..dbf59f338a95 100644 --- a/rust/frontend/examples/resnet/Cargo.toml +++ b/rust/frontend/examples/resnet/Cargo.toml @@ -23,7 +23,7 @@ license = "Apache-2.0" build = "build.rs" [dependencies] -ndarray = "0.12.1" +ndarray = "0.12" tvm-frontend = { path = "../../" } -image = "0.20.1" -csv = "1" +image = "0.20" +csv = "1.1" diff --git a/rust/frontend/tests/basics/Cargo.toml b/rust/frontend/tests/basics/Cargo.toml index 11fbda2bb15e..d4db184e931a 100644 --- a/rust/frontend/tests/basics/Cargo.toml +++ b/rust/frontend/tests/basics/Cargo.toml @@ -23,7 +23,7 @@ license = "Apache-2.0" build = "build.rs" [dependencies] -ndarray = "0.12.1" +ndarray = "0.12" tvm-frontend = { path = "../../" } [features] diff --git a/rust/frontend/tests/callback/Cargo.toml b/rust/frontend/tests/callback/Cargo.toml index e6df1d2c0ad3..a452572573fd 100644 --- a/rust/frontend/tests/callback/Cargo.toml +++ b/rust/frontend/tests/callback/Cargo.toml @@ -21,5 +21,5 @@ version = "0.0.0" authors = ["TVM Contributors"] [dependencies] -ndarray = "0.12.1" +ndarray = "0.12" tvm-frontend = { path = "../../" } diff --git a/rust/macros/Cargo.toml b/rust/macros/Cargo.toml index 15773b625be9..d1ddf2567da4 100644 --- a/rust/macros/Cargo.toml +++ b/rust/macros/Cargo.toml @@ -30,7 +30,7 @@ edition = "2018" proc-macro = true [dependencies] -goblin = "0.0.22" +goblin = "0.0.24" proc-macro2 = "0.4" proc-quote = "0.2" syn = "0.15" diff --git a/rust/runtime/src/graph.rs b/rust/runtime/src/graph.rs index 61e603fa7645..cacd7a38a97f 100644 --- a/rust/runtime/src/graph.rs +++ b/rust/runtime/src/graph.rs @@ -162,15 +162,15 @@ impl<'a> TryFrom<&'a str> for Graph { /// /// println!("{:#?}", Array::try_from(output).unwrap()); /// ``` -pub struct GraphExecutor<'m> { +pub struct GraphExecutor<'m, 't> { graph: Graph, op_execs: Vec>, tensors: Vec>, } -unsafe impl<'m> Send for GraphExecutor<'m> {} +unsafe impl<'m, 't> Send for GraphExecutor<'m, 't> {} -impl<'m> GraphExecutor<'m> { +impl<'m, 't> GraphExecutor<'m, 't> { pub fn new(graph: Graph, lib: &'m M) -> Result { let tensors = Self::setup_storages(&graph)?; Ok(GraphExecutor { @@ -188,7 +188,7 @@ impl<'m> GraphExecutor<'m> { } /// Allocates `Storages` for each `storage_id` and returns `Tensor`s to hold each output. - fn setup_storages<'a>(graph: &'a Graph) -> Result>, Error> { + fn setup_storages<'a>(graph: &'a Graph) -> Result>, Error> { let storage_ids = graph.get_attr::<(String, Vec)>("storage_id")?.1; let shapes = graph.get_attr::<(String, Vec>)>("shape")?.1; let dtypes = graph @@ -319,8 +319,8 @@ impl<'m> GraphExecutor<'m> { } } - /// Returns a reference to the graph input with name `name`, if it exists. - pub fn get_input>(&self, name: S) -> Option<&Tensor> { + /// Returns the graph input with name `name`, if it exists. + pub fn get_input>(&mut self, name: S) -> Option<&Tensor> { self.get_input_index(name.as_ref()) .and_then(move |idx| Some(&self.tensors[idx])) } diff --git a/rust/runtime/tests/test_nnvm/Cargo.toml b/rust/runtime/tests/test_nnvm/Cargo.toml index 93fdef4f61c3..446308d36361 100644 --- a/rust/runtime/tests/test_nnvm/Cargo.toml +++ b/rust/runtime/tests/test_nnvm/Cargo.toml @@ -22,10 +22,10 @@ license = "Apache-2.0" authors = ["TVM Contributors"] [dependencies] -ndarray="0.12.1" -serde = "1.0.59" -serde_json = "1.0.17" +ndarray="0.12" +serde = "1.0" +serde_json = "1.0" tvm-runtime = { path = "../../" } [build-dependencies] -ar = "0.6.0" +ar = "0.6" diff --git a/rust/runtime/tests/test_tvm_basic/Cargo.toml b/rust/runtime/tests/test_tvm_basic/Cargo.toml index e821487b12b2..d11531450298 100644 --- a/rust/runtime/tests/test_tvm_basic/Cargo.toml +++ b/rust/runtime/tests/test_tvm_basic/Cargo.toml @@ -22,8 +22,8 @@ license = "Apache-2.0" authors = ["TVM Contributors"] [dependencies] -ndarray="0.12.1" +ndarray="0.12" tvm-runtime = { path = "../../" } [build-dependencies] -ar = "0.6.0" +ar = "0.6" From 03378e65735ab6b988fc35f12e20916ffb7e9e95 Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Mon, 29 Jul 2019 02:15:10 +0000 Subject: [PATCH 07/18] Update SGX --- CMakeLists.txt | 9 +-- apps/sgx/.rustfmt.toml | 60 +------------- apps/sgx/README.md | 6 ++ apps/sgx/build.rs | 28 +++++-- apps/sgx/enclave/Cargo.toml | 32 -------- apps/sgx/enclave/Makefile | 59 -------------- apps/sgx/enclave/Xargo.toml | 30 ------- apps/sgx/enclave/build.rs | 28 ------- apps/sgx/enclave/src/lib.rs | 155 ------------------------------------ apps/sgx/run_example.sh | 26 ------ apps/sgx/run_model.py | 38 --------- apps/sgx/src/main.rs | 7 +- rust/.rustfmt.toml | 15 +++- rust/runtime/src/graph.rs | 2 +- 14 files changed, 43 insertions(+), 452 deletions(-) mode change 100644 => 120000 apps/sgx/.rustfmt.toml create mode 100644 apps/sgx/README.md delete mode 100644 apps/sgx/enclave/Cargo.toml delete mode 100644 apps/sgx/enclave/Makefile delete mode 100644 apps/sgx/enclave/Xargo.toml delete mode 100644 apps/sgx/enclave/build.rs delete mode 100644 apps/sgx/enclave/src/lib.rs delete mode 100755 apps/sgx/run_example.sh delete mode 100644 apps/sgx/run_model.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 1971753a9008..b352802db34a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ tvm_option(USE_LLVM "Build with LLVM, can be set to specific llvm-config path" O tvm_option(USE_STACKVM_RUNTIME "Include stackvm into the runtime" OFF) 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_RELAY_DEBUG "Building Relay in debug mode..." OFF) tvm_option(USE_RTTI "Build with RTTI" ON) tvm_option(USE_MSVC_MT "Build with MT" OFF) tvm_option(USE_MICRO "Build with Micro" OFF) @@ -260,7 +261,6 @@ endif() add_library(tvm SHARED ${COMPILER_SRCS} ${RUNTIME_SRCS}) add_library(tvm_topi SHARED ${TOPI_SRCS}) add_library(tvm_runtime SHARED ${RUNTIME_SRCS}) -<<<<<<< HEAD if(USE_RELAY_DEBUG) message(STATUS "Building Relay in debug mode...") @@ -269,13 +269,6 @@ else() set_target_properties(tvm PROPERTIES COMPILE_DEFINITIONS "NDEBUG") 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() -======= ->>>>>>> f531c1a7... Remove SGX in preparation for adding SGX add_library(nnvm_compiler SHARED ${NNVM_COMPILER_SRCS}) if(USE_THREADS) diff --git a/apps/sgx/.rustfmt.toml b/apps/sgx/.rustfmt.toml deleted file mode 100644 index 3db835c7fd93..000000000000 --- a/apps/sgx/.rustfmt.toml +++ /dev/null @@ -1,59 +0,0 @@ -max_width = 100 -hard_tabs = false -tab_spaces = 4 -newline_style = "Auto" -use_small_heuristics = "Default" -indent_style = "Block" -wrap_comments = false -comment_width = 80 -normalize_comments = false -format_strings = false -format_macro_matchers = false -format_macro_bodies = true -empty_item_single_line = true -struct_lit_single_line = true -fn_single_line = false -where_single_line = false -imports_indent = "Block" -imports_layout = "Mixed" -merge_imports = true -reorder_imports = true -reorder_modules = true -reorder_impl_items = false -type_punctuation_density = "Wide" -space_before_colon = false -space_after_colon = true -spaces_around_ranges = false -binop_separator = "Front" -remove_nested_parens = true -combine_control_expr = true -struct_field_align_threshold = 0 -match_arm_blocks = true -force_multiline_blocks = false -fn_args_density = "Tall" -brace_style = "SameLineWhere" -control_brace_style = "AlwaysSameLine" -trailing_semicolon = true -trailing_comma = "Vertical" -match_block_trailing_comma = false -blank_lines_upper_bound = 1 -blank_lines_lower_bound = 0 -edition = "2018" -merge_derives = true -use_try_shorthand = true -use_field_init_shorthand = false -force_explicit_abi = true -condense_wildcard_suffixes = false -color = "Auto" -required_version = "1.0.3" -unstable_features = false -disable_all_formatting = false -skip_children = false -hide_parse_errors = false -error_on_line_overflow = true -error_on_unformatted = true -report_todo = "Never" -report_fixme = "Never" -ignore = [] -emit_mode = "Files" -make_backup = false diff --git a/apps/sgx/.rustfmt.toml b/apps/sgx/.rustfmt.toml new file mode 120000 index 000000000000..27139e42a3f2 --- /dev/null +++ b/apps/sgx/.rustfmt.toml @@ -0,0 +1 @@ +../../rust/.rustfmt.toml \ No newline at end of file diff --git a/apps/sgx/README.md b/apps/sgx/README.md new file mode 100644 index 000000000000..f0f21f9c270c --- /dev/null +++ b/apps/sgx/README.md @@ -0,0 +1,6 @@ +## Setup + +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` +3. `cargo run` to start the enclave TCP server +4. Send a 28x28 "image" to the enclave model server using `head -c $((28*28*4)) /dev/urandom | nc 127.0.0.1 4242` diff --git a/apps/sgx/build.rs b/apps/sgx/build.rs index 8836f2983579..60691d481aa9 100644 --- a/apps/sgx/build.rs +++ b/apps/sgx/build.rs @@ -1,26 +1,38 @@ fn main() { let out_dir = std::env::var("OUT_DIR").unwrap(); - let output = + let build_output = std::process::Command::new(concat!(env!("CARGO_MANIFEST_DIR"), "/src/build_model.py")) .arg(&out_dir) .output() - .expect("Failed to execute command"); + .expect("Failed to build model"); assert!( - ["model.o", "graph.json", "params.bin"].iter().all(|f| { - std::path::Path::new(&format!("{}/{}", out_dir, f)).exists() - }), + ["model.o", "graph.json", "params.bin"] + .iter() + .all(|f| { std::path::Path::new(&format!("{}/{}", out_dir, f)).exists() }), "Could not build tvm lib: {}", - String::from_utf8(output.stderr).unwrap().trim() + String::from_utf8(build_output.stderr).unwrap().trim() ); - std::process::Command::new("objcopy") + let sysroot_output = std::process::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); + let target = sysroot.splitn(2, "-").nth(1).expect(&sysroot); + llvm_tools_path.push("lib/rustlib"); + llvm_tools_path.push(target); + llvm_tools_path.push("bin"); + + std::process::Command::new(llvm_tools_path.join("llvm-objcopy")) .arg("--globalize-symbol=__tvm_module_startup") .arg(&format!("{}/model.o", out_dir)) .output() .expect("Could not gloablize startup function."); - std::process::Command::new("llvm-ar") + std::process::Command::new(llvm_tools_path.join("llvm-ar")) .arg("rcs") .arg(&format!("{}/libmodel.a", out_dir)) .arg(&format!("{}/model.o", out_dir)) diff --git a/apps/sgx/enclave/Cargo.toml b/apps/sgx/enclave/Cargo.toml deleted file mode 100644 index 70b7ec462675..000000000000 --- a/apps/sgx/enclave/Cargo.toml +++ /dev/null @@ -1,32 +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. - -[package] -name = "model-enclave" -version = "0.1.0" -authors = ["Nick Hynes "] - -[lib] -crate-type = ["staticlib"] - -[dependencies] -lazy_static = "1.1.0" -tvm = { path = "../../../rust", default-features = false, features = ["sgx"] } - -[profile.release] -lto = true -opt-level = 3 diff --git a/apps/sgx/enclave/Makefile b/apps/sgx/enclave/Makefile deleted file mode 100644 index 8d6571b3cd69..000000000000 --- a/apps/sgx/enclave/Makefile +++ /dev/null @@ -1,59 +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. - -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 diff --git a/apps/sgx/enclave/Xargo.toml b/apps/sgx/enclave/Xargo.toml deleted file mode 100644 index 4b6b60aaa3c3..000000000000 --- a/apps/sgx/enclave/Xargo.toml +++ /dev/null @@ -1,30 +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. - -[dependencies] -alloc = {} -panic_unwind = {} -panic_abort = {} - -[dependencies.std] -path = "/opt/rust-sgx-sdk/xargo/sgx_tstd" -features = ["backtrace", "stdio", "untrusted_time"] -stage = 2 - -[dependencies.xargo_sgx_rand] -path = "/opt/rust-sgx-sdk/xargo/sgx_rand" -stage = 3 diff --git a/apps/sgx/enclave/build.rs b/apps/sgx/enclave/build.rs deleted file mode 100644 index 49596816819d..000000000000 --- a/apps/sgx/enclave/build.rs +++ /dev/null @@ -1,28 +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::env; - -fn main() { - println!( - "cargo:rustc-link-search=native={}", - env::var("BUILD_DIR").unwrap() - ); - println!("cargo:rustc-link-lib=static=model"); -} diff --git a/apps/sgx/enclave/src/lib.rs b/apps/sgx/enclave/src/lib.rs deleted file mode 100644 index bb0fc795d90d..000000000000 --- a/apps/sgx/enclave/src/lib.rs +++ /dev/null @@ -1,155 +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. - */ -#![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> = { - 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]) -> TVMRetValue + Send + Sync>; - - lazy_static! { - static ref ECALL_FUNCS: Vec = { - 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::>>() - }; - let (rv, tc) = ECALL_FUNCS[func_id as usize](&args).into_tvm_value(); - unsafe { - *ret_val = rv; - *ret_type_code = tc; - } - } -} diff --git a/apps/sgx/run_example.sh b/apps/sgx/run_example.sh deleted file mode 100755 index c09807fb8acc..000000000000 --- a/apps/sgx/run_example.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/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. - -sgx_sdk=${SGX_SDK:=/opt/sgxsdk} - -export LD_LIBRARY_PATH="$sgx_sdk/lib64":${LD_LIBRARY_PATH} -export CC=clang-6.0 -export AR=llvm-ar-6.0 -export TVM_CACHE_DIR=/tmp - -make && printf "\n" && python3 run_model.py diff --git a/apps/sgx/run_model.py b/apps/sgx/run_model.py deleted file mode 100644 index 9f21c2685d45..000000000000 --- a/apps/sgx/run_model.py +++ /dev/null @@ -1,38 +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. -import os.path as osp -import numpy as np -import tvm - -CWD = osp.abspath(osp.dirname(__file__)) - - -def main(): - ctx = tvm.context('cpu', 0) - model = tvm.module.load(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() diff --git a/apps/sgx/src/main.rs b/apps/sgx/src/main.rs index 83aa99a94600..98eea2272f7d 100644 --- a/apps/sgx/src/main.rs +++ b/apps/sgx/src/main.rs @@ -16,13 +16,12 @@ fn main() { let mut exec = tvm_runtime::GraphExecutor::new(graph, &syslib).unwrap(); exec.load_params(params); - // this is actually unsafe but is necessary until tensors can be constructed from bytes - let mut input_bytes = exec.get_input("data").unwrap().data().view(); - 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(input_bytes.as_mut_slice()) { + if let Err(_) = + stream.read_exact(exec.get_input("data").unwrap().data().view().as_mut_slice()) + { continue; } exec.run(); diff --git a/rust/.rustfmt.toml b/rust/.rustfmt.toml index 597bec8d0484..f54b2f6fdadb 100644 --- a/rust/.rustfmt.toml +++ b/rust/.rustfmt.toml @@ -22,8 +22,11 @@ newline_style = "Auto" use_small_heuristics = "Default" indent_style = "Block" wrap_comments = false +format_code_in_doc_comments = false comment_width = 80 normalize_comments = false +normalize_doc_attributes = false +license_template_path = "" format_strings = false format_macro_matchers = false format_macro_bodies = true @@ -44,10 +47,12 @@ spaces_around_ranges = false binop_separator = "Front" remove_nested_parens = true combine_control_expr = true +overflow_delimited_expr = false struct_field_align_threshold = 0 +enum_discrim_align_threshold = 0 match_arm_blocks = true force_multiline_blocks = false -fn_args_density = "Tall" +fn_args_layout = "Tall" brace_style = "SameLineWhere" control_brace_style = "AlwaysSameLine" trailing_semicolon = true @@ -56,8 +61,10 @@ match_block_trailing_comma = false blank_lines_upper_bound = 1 blank_lines_lower_bound = 0 edition = "2018" +version = "One" +inline_attribute_width = 0 merge_derives = true -use_try_shorthand = true +use_try_shorthand = false use_field_init_shorthand = false force_explicit_abi = true condense_wildcard_suffixes = false @@ -66,8 +73,8 @@ unstable_features = false disable_all_formatting = false skip_children = false hide_parse_errors = false -error_on_line_overflow = true -error_on_unformatted = true +error_on_line_overflow = false +error_on_unformatted = false report_todo = "Never" report_fixme = "Never" ignore = [] diff --git a/rust/runtime/src/graph.rs b/rust/runtime/src/graph.rs index cacd7a38a97f..a35c16b0beb8 100644 --- a/rust/runtime/src/graph.rs +++ b/rust/runtime/src/graph.rs @@ -181,7 +181,7 @@ impl<'m, 't> GraphExecutor<'m, 't> { } /// Runs the computation graph. - pub fn run(&self) { + pub fn run(&mut self) { self.op_execs.iter().for_each(|op_exec| { op_exec(); }); From 5a89c1bdddd3ed3b6cf5de854053841f96a215f6 Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Mon, 29 Jul 2019 02:21:01 +0000 Subject: [PATCH 08/18] Fix lint --- apps/sgx/Cargo.toml | 17 +++++++++++++++++ apps/sgx/README.md | 17 +++++++++++++++++ apps/sgx/build.rs | 19 +++++++++++++++++++ apps/sgx/src/main.rs | 19 +++++++++++++++++++ tests/lint/check_file_type.py | 4 ++-- 5 files changed, 74 insertions(+), 2 deletions(-) diff --git a/apps/sgx/Cargo.toml b/apps/sgx/Cargo.toml index 2a299a4f2269..93fae202abee 100644 --- a/apps/sgx/Cargo.toml +++ b/apps/sgx/Cargo.toml @@ -1,3 +1,20 @@ +# 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. + [package] name = "sgx-demo" version = "0.1.0" diff --git a/apps/sgx/README.md b/apps/sgx/README.md index f0f21f9c270c..370b21566438 100644 --- a/apps/sgx/README.md +++ b/apps/sgx/README.md @@ -1,3 +1,20 @@ + + + + + + + + + + + + + + + + + ## Setup 1. [Install the Fortanix Enclave Development Platform](https://edp.fortanix.com/docs/installation/guide/) diff --git a/apps/sgx/build.rs b/apps/sgx/build.rs index 60691d481aa9..dcd60b042fb6 100644 --- a/apps/sgx/build.rs +++ b/apps/sgx/build.rs @@ -1,3 +1,22 @@ +/* + * 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. + */ + fn main() { let out_dir = std::env::var("OUT_DIR").unwrap(); diff --git a/apps/sgx/src/main.rs b/apps/sgx/src/main.rs index 98eea2272f7d..16dd9a9c8b75 100644 --- a/apps/sgx/src/main.rs +++ b/apps/sgx/src/main.rs @@ -1,3 +1,22 @@ +/* + * 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 tvm_runtime; use std::{ diff --git a/tests/lint/check_file_type.py b/tests/lint/check_file_type.py index e5f2dc7e2aa6..7fded58b9946 100644 --- a/tests/lint/check_file_type.py +++ b/tests/lint/check_file_type.py @@ -96,8 +96,8 @@ "LICENSE", "NOTICE", "Jenkinsfile", - # sgx file - "apps/sgx/enclave/sgx-deps.diff", + # sgx config + "apps/sgx/.cargo/config", # html for demo purposes "nnvm/tutorials/web/resnet.html", "tests/webgl/test_static_webgl_library.html", From 4e61851d2826511a2dea8748f6b8312846b52168 Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Mon, 29 Jul 2019 03:11:05 +0000 Subject: [PATCH 09/18] Fix rustfmt --- rust/.rustfmt.toml | 1 - rust/runtime/src/module/syslib.rs | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/rust/.rustfmt.toml b/rust/.rustfmt.toml index f54b2f6fdadb..bd56ec6b4988 100644 --- a/rust/.rustfmt.toml +++ b/rust/.rustfmt.toml @@ -26,7 +26,6 @@ format_code_in_doc_comments = false comment_width = 80 normalize_comments = false normalize_doc_attributes = false -license_template_path = "" format_strings = false format_macro_matchers = false format_macro_bodies = true diff --git a/rust/runtime/src/module/syslib.rs b/rust/runtime/src/module/syslib.rs index c727cf0dedc4..6ee1a2a7d4b9 100644 --- a/rust/runtime/src/module/syslib.rs +++ b/rust/runtime/src/module/syslib.rs @@ -50,7 +50,9 @@ impl Module for SystemLibModule { impl Default for SystemLibModule { fn default() -> Self { #[cfg(target_env = "sgx")] - unsafe { __tvm_module_startup(); } + unsafe { + __tvm_module_startup(); + } SystemLibModule {} } } From e28700b0bbd72cb1a0914b941a652c4463ee0e36 Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Mon, 29 Jul 2019 04:53:49 +0000 Subject: [PATCH 10/18] Put back feature flags --- rust/common/src/lib.rs | 2 +- rust/frontend/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/common/src/lib.rs b/rust/common/src/lib.rs index b1362aeb29ba..a2418270002b 100644 --- a/rust/common/src/lib.rs +++ b/rust/common/src/lib.rs @@ -20,7 +20,7 @@ //! This crate contains the refactored basic components required //! for `runtime` and `frontend` TVM crates. -#![feature(box_syntax, trait_alias)] +#![feature(box_syntax, trait_alias, type_alias_enum_variants)] #[macro_use] extern crate failure; diff --git a/rust/frontend/src/lib.rs b/rust/frontend/src/lib.rs index 9bf982ee29c8..adb258dbd3d9 100644 --- a/rust/frontend/src/lib.rs +++ b/rust/frontend/src/lib.rs @@ -30,7 +30,7 @@ //! //! Checkout the `examples` repository for more details. -#![feature(box_syntax)] +#![feature(box_syntax, type_alias_enum_variants)] #[macro_use] extern crate failure; From 08bf7e5214cd4c7aec996a42e393302dbf431bb9 Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Sun, 22 Sep 2019 23:29:10 +0000 Subject: [PATCH 11/18] Update macros crate --- rust/macros/Cargo.toml | 8 ++++---- rust/macros/src/lib.rs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rust/macros/Cargo.toml b/rust/macros/Cargo.toml index d1ddf2567da4..fa5e9ca755f9 100644 --- a/rust/macros/Cargo.toml +++ b/rust/macros/Cargo.toml @@ -17,7 +17,7 @@ [package] name = "tvm-macros" -version = "0.1.0" +version = "0.1.1" license = "Apache-2.0" description = "Proc macros used by the TVM crates." repository = "https://github.com/dmlc/tvm" @@ -31,6 +31,6 @@ proc-macro = true [dependencies] goblin = "0.0.24" -proc-macro2 = "0.4" -proc-quote = "0.2" -syn = "0.15" +proc-macro2 = "1.0" +quote = "1.0" +syn = "1.0" diff --git a/rust/macros/src/lib.rs b/rust/macros/src/lib.rs index f6b1afd2c32b..c9af89141823 100644 --- a/rust/macros/src/lib.rs +++ b/rust/macros/src/lib.rs @@ -23,7 +23,7 @@ extern crate proc_macro; use std::{fs::File, io::Read}; -use proc_quote::quote; +use quote::quote; #[proc_macro] pub fn import_module(input: proc_macro::TokenStream) -> proc_macro::TokenStream { @@ -57,7 +57,7 @@ pub fn import_module(input: proc_macro::TokenStream) -> proc_macro::TokenStream goblin::Object::Mach(goblin::mach::Mach::Binary(obj)) => { obj.symbols() .filter_map(|s| match s { - Ok((name, nlist)) + Ok((name, ref nlist)) if nlist.is_global() && nlist.n_sect != 0 && !name.ends_with("tvm_module_ctx") => From 134c1a99cee7500000fef1d3cf203382e232c7b7 Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Mon, 23 Sep 2019 01:06:20 +0000 Subject: [PATCH 12/18] Update crates --- rust/common/Cargo.toml | 2 +- rust/common/build.rs | 4 +- rust/common/src/array.rs | 2 +- rust/common/src/lib.rs | 2 +- rust/common/src/packed_func.rs | 11 +- rust/common/src/value.rs | 8 +- rust/frontend/src/function.rs | 37 +++--- rust/frontend/src/lib.rs | 2 +- rust/frontend/src/module.rs | 6 +- rust/frontend/src/ndarray.rs | 37 +++--- rust/frontend/src/value.rs | 2 +- rust/runtime/Cargo.toml | 8 +- rust/runtime/src/allocator.rs | 9 +- rust/runtime/src/array.rs | 30 +++-- rust/runtime/src/graph.rs | 153 ++++++++++++----------- rust/runtime/src/lib.rs | 4 +- rust/runtime/src/module/dso.rs | 20 +-- rust/runtime/src/module/mod.rs | 2 +- rust/runtime/src/module/syslib.rs | 5 +- rust/runtime/src/threading.rs | 26 ++-- rust/runtime/src/workspace.rs | 16 ++- rust/runtime/tests/test_graph_serde.rs | 31 ++++- rust/runtime/tests/test_nnvm/src/main.rs | 8 +- 23 files changed, 230 insertions(+), 195 deletions(-) diff --git a/rust/common/Cargo.toml b/rust/common/Cargo.toml index 054499d53523..ddfc9098418d 100644 --- a/rust/common/Cargo.toml +++ b/rust/common/Cargo.toml @@ -30,4 +30,4 @@ failure = "0.1" ndarray = "0.12" [build-dependencies] -bindgen = "0.37" +bindgen = "0.51" diff --git a/rust/common/build.rs b/rust/common/build.rs index 919e0adc46c8..1b46169971b2 100644 --- a/rust/common/build.rs +++ b/rust/common/build.rs @@ -23,10 +23,10 @@ use std::path::PathBuf; fn main() { 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() .unwrap(); - tvm_home + crate_dir .parent() .unwrap() .parent() diff --git a/rust/common/src/array.rs b/rust/common/src/array.rs index 5cc8cc2ef051..4ccfb4bf42d9 100644 --- a/rust/common/src/array.rs +++ b/rust/common/src/array.rs @@ -97,7 +97,7 @@ pub struct TVMContext { impl<'a> From<&'a TVMContext> for DLContext { fn from(ctx: &'a TVMContext) -> Self { Self { - device_type: ctx.device_type as i32, + device_type: ctx.device_type as u32, device_id: ctx.device_id as i32, } } diff --git a/rust/common/src/lib.rs b/rust/common/src/lib.rs index a2418270002b..b1362aeb29ba 100644 --- a/rust/common/src/lib.rs +++ b/rust/common/src/lib.rs @@ -20,7 +20,7 @@ //! This crate contains the refactored basic components required //! for `runtime` and `frontend` TVM crates. -#![feature(box_syntax, trait_alias, type_alias_enum_variants)] +#![feature(box_syntax, trait_alias)] #[macro_use] extern crate failure; diff --git a/rust/common/src/packed_func.rs b/rust/common/src/packed_func.rs index 0f3c7755da98..62e3bce80162 100644 --- a/rust/common/src/packed_func.rs +++ b/rust/common/src/packed_func.rs @@ -83,7 +83,7 @@ macro_rules! TVMPODValue { use $name::*; #[allow(non_upper_case_globals)] unsafe { - match type_code as i32 { + match type_code { DLDataTypeCode_kDLInt => Int($value.v_int64), DLDataTypeCode_kDLUInt => UInt($value.v_int64), DLDataTypeCode_kDLFloat => Float($value.v_float64), @@ -153,7 +153,7 @@ TVMPODValue! { }, match &self { 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_kBytes) } Str(val) => { (TVMValue { v_handle: val.as_ptr() as *mut c_void }, TVMTypeCode_kStr) } } @@ -166,11 +166,14 @@ TVMPODValue! { /// # Example /// /// ``` + /// use std::convert::{TryFrom, TryInto}; + /// use tvm_common::TVMRetValue; + /// /// 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 t: TVMRetValue = s.into(); + /// let t: TVMRetValue = s.to_string().into(); /// assert_eq!(String::try_from(t).unwrap(), s); /// ``` TVMRetValue { diff --git a/rust/common/src/value.rs b/rust/common/src/value.rs index 6d17db207865..fa0563ebd754 100644 --- a/rust/common/src/value.rs +++ b/rust/common/src/value.rs @@ -42,7 +42,7 @@ impl FromStr for TVMType { return Ok(TVMType::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 lanes = type_lanes .next() @@ -163,7 +163,7 @@ impl_tvm_context!( /// /// ``` /// 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.data(), &[104u8, 101, 108, 108, 111]); /// ``` @@ -182,6 +182,10 @@ impl TVMByteArray { pub fn to_vec(&self) -> Vec { self.data().to_vec() } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } } // Needs AsRef for Vec diff --git a/rust/frontend/src/function.rs b/rust/frontend/src/function.rs index 948711276304..131b365f9484 100644 --- a/rust/frontend/src/function.rs +++ b/rust/frontend/src/function.rs @@ -28,7 +28,7 @@ use std::{ collections::BTreeMap, ffi::{CStr, CString}, - mem, + mem::{self, MaybeUninit}, os::raw::{c_char, c_int, c_void}, ptr, slice, str, sync::Mutex, @@ -36,25 +36,20 @@ use std::{ use failure::Error; -use crate::{ - errors, - ffi::{self, TVMValue}, - Module, TVMArgValue, TVMRetValue, -}; +use crate::{errors, ffi, Module, TVMArgValue, TVMRetValue}; lazy_static! { static ref GLOBAL_FUNCTIONS: Mutex>> = { let mut out_size = 0 as c_int; - let name = ptr::null_mut() as *mut c_char; - let mut out_array = name as *mut _; + let mut names_ptr = ptr::null_mut() as *mut *const c_char; check_call!(ffi::TVMFuncListGlobalNames( &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( names_list - .into_iter() + .iter() .map(|&p| (unsafe { CStr::from_ptr(p).to_str().unwrap() }, None)) .collect(), ) @@ -80,7 +75,7 @@ unsafe impl Sync for Function {} impl Function { pub(crate) fn new(handle: ffi::TVMFunctionHandle) -> Self { Function { - handle: handle, + handle, is_global: false, is_cloned: false, } @@ -98,15 +93,13 @@ impl Function { &mut handle as *mut _ )); maybe_func.replace(Function { - handle: handle, + handle, is_global: true, is_cloned: false, }); } unsafe { - std::mem::transmute::, Option<&'static Function>>( - maybe_func.as_ref(), - ) + mem::transmute::, Option<&'static Function>>(maybe_func.as_ref()) } }) } @@ -214,7 +207,7 @@ impl<'a, 'm> Builder<'a, 'm> { let (mut values, mut type_codes): (Vec, Vec) = self.arg_buf.iter().map(|arg| arg.to_tvm_value()).unzip(); - let mut ret_val = unsafe { std::mem::uninitialized::() }; + let mut ret_val = unsafe { MaybeUninit::uninit().assume_init() }; let mut ret_type_code = 0i32; check_call!(ffi::TVMFuncCall( self.func.ok_or(errors::FunctionNotFoundError)?.handle, @@ -257,8 +250,8 @@ unsafe extern "C" fn tvm_callback( let args_list = slice::from_raw_parts_mut(args, len); let type_codes_list = slice::from_raw_parts_mut(type_codes, len); let mut local_args: Vec = Vec::new(); - let mut value = mem::uninitialized::(); - let mut tcode = mem::uninitialized::(); + let mut value = MaybeUninit::uninit().assume_init(); + let mut tcode = MaybeUninit::uninit().assume_init(); let rust_fn = mem::transmute::<*mut c_void, fn(&[TVMArgValue]) -> Result>(fhandle); for i in 0..len { @@ -270,7 +263,7 @@ unsafe extern "C" fn tvm_callback( { 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()) { @@ -293,9 +286,9 @@ unsafe extern "C" fn tvm_callback( } 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>(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) -> Function { diff --git a/rust/frontend/src/lib.rs b/rust/frontend/src/lib.rs index adb258dbd3d9..9bf982ee29c8 100644 --- a/rust/frontend/src/lib.rs +++ b/rust/frontend/src/lib.rs @@ -30,7 +30,7 @@ //! //! Checkout the `examples` repository for more details. -#![feature(box_syntax, type_alias_enum_variants)] +#![feature(box_syntax)] #[macro_use] extern crate failure; diff --git a/rust/frontend/src/module.rs b/rust/frontend/src/module.rs index 4fbe0b5cc8ab..fae8988ab9f6 100644 --- a/rust/frontend/src/module.rs +++ b/rust/frontend/src/module.rs @@ -32,7 +32,7 @@ use tvm_common::ffi; 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. /// The entry function can be applied to an imported module through [`entry_func`]. @@ -72,7 +72,7 @@ impl Module { ensure!( !fhandle.is_null(), errors::NullHandleError { - name: format!("{}", name.into_string()?) + name: name.into_string()?.to_string() } ); Ok(Function::new(fhandle)) @@ -88,7 +88,7 @@ impl Module { let ext = CString::new( path.as_ref() .extension() - .unwrap_or(std::ffi::OsStr::new("")) + .unwrap_or_else(|| std::ffi::OsStr::new("")) .to_str() .ok_or_else(|| { format_err!("Bad module load path: `{}`.", path.as_ref().display()) diff --git a/rust/frontend/src/ndarray.rs b/rust/frontend/src/ndarray.rs index 885fcfac8e3d..5122a835af1e 100644 --- a/rust/frontend/src/ndarray.rs +++ b/rust/frontend/src/ndarray.rs @@ -63,7 +63,7 @@ pub struct NDArray { impl NDArray { pub(crate) fn new(handle: ffi::TVMArrayHandle) -> Self { NDArray { - handle: handle, + handle, is_view: true, } } @@ -89,8 +89,7 @@ impl NDArray { /// Returns the total number of entries of the NDArray. pub fn size(&self) -> Option { - self.shape() - .map(|v| v.into_iter().fold(1, |acc, &mut e| acc * e)) + self.shape().map(|v| v.iter().product()) } /// Returns the context which the NDArray was defined. @@ -100,7 +99,7 @@ impl NDArray { /// Returns the type of the entries of the NDArray. pub fn dtype(&self) -> TVMType { - unsafe { (*self.handle).dtype.into() } + unsafe { (*self.handle).dtype } } /// Returns the number of dimensions of the NDArray. @@ -211,8 +210,8 @@ impl NDArray { bail!( "{}", errors::TypeMismatchError { - expected: format!("{}", self.dtype().to_string()), - actual: format!("{}", target.dtype().to_string()), + expected: self.dtype().to_string(), + actual: target.dtype().to_string(), } ); } @@ -228,7 +227,7 @@ impl NDArray { pub fn copy_to_ctx(&self, target: &TVMContext) -> Result { let tmp = NDArray::empty( self.shape().ok_or(errors::MissingShapeError)?, - target.clone(), + *target, self.dtype(), ); let copy = self.copy_to_ndarray(tmp)?; @@ -241,8 +240,8 @@ impl NDArray { ctx: TVMContext, dtype: TVMType, ) -> Result { - let mut shape = rnd.shape().to_vec(); - let mut nd = NDArray::empty(&mut shape, ctx, dtype); + let shape = rnd.shape().to_vec(); + let mut nd = NDArray::empty(&shape, ctx, dtype); let mut buf = Array::from_iter(rnd.into_iter().map(|&v| v as T)); nd.copy_from_buffer( buf.as_slice_mut() @@ -257,9 +256,9 @@ impl NDArray { check_call!(ffi::TVMArrayAlloc( shape.as_ptr() as *const i64, shape.len() as c_int, - dtype.code as c_int, - dtype.bits as c_int, - dtype.lanes as c_int, + i32::from(dtype.code) as c_int, + i32::from(dtype.bits) as c_int, + i32::from(dtype.lanes) as c_int, ctx.device_type.0 as c_int, ctx.device_id as c_int, &mut handle as *mut _, @@ -364,9 +363,9 @@ mod tests { assert_eq!(ndarray.ndim(), 1); assert!(ndarray.is_contiguous().is_ok()); assert_eq!(ndarray.byte_offset(), 0); - let mut shape = vec![4]; + let shape = vec![4]; let e = NDArray::empty( - &mut shape, + &shape, TVMContext::cpu(0), TVMType::from_str("int32").unwrap(), ); @@ -378,16 +377,12 @@ mod tests { #[test] #[should_panic(expected = "called `Result::unwrap()` on an `Err`")] fn copy_wrong_dtype() { - let mut shape = vec![4]; + let shape = vec![4]; let mut data = vec![1f32, 2., 3., 4.]; let ctx = TVMContext::cpu(0); - let mut nd_float = NDArray::empty( - &mut shape, - ctx.clone(), - TVMType::from_str("float32").unwrap(), - ); + let mut nd_float = NDArray::empty(&shape, ctx, TVMType::from_str("float32").unwrap()); 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(); } diff --git a/rust/frontend/src/value.rs b/rust/frontend/src/value.rs index b829d9183703..1e031e4f809c 100644 --- a/rust/frontend/src/value.rs +++ b/rust/frontend/src/value.rs @@ -93,7 +93,7 @@ mod tests { let tvm: TVMByteArray = TVMRetValue::from(v).try_into().unwrap(); assert_eq!( tvm.data(), - w.iter().map(|e| *e).collect::>().as_slice() + w.iter().copied().collect::>().as_slice() ); } diff --git a/rust/runtime/Cargo.toml b/rust/runtime/Cargo.toml index 14b974c305bf..3fc50b5d192d 100644 --- a/rust/runtime/Cargo.toml +++ b/rust/runtime/Cargo.toml @@ -30,11 +30,11 @@ edition = "2018" [dependencies] bounded-spsc-queue = "0.4" failure = "0.1" -itertools = "0.7" -lazy_static = "1.1" +itertools = "0.8" +lazy_static = "1.4" ndarray="0.12" -nom = "4.0" -num_cpus = "1.8" +nom = "5.0" +num_cpus = "1.10" serde = "1.0" serde_derive = "1.0" serde_json = "1.0" diff --git a/rust/runtime/src/allocator.rs b/rust/runtime/src/allocator.rs index 00d94bfe4b1e..81499af5f8b8 100644 --- a/rust/runtime/src/allocator.rs +++ b/rust/runtime/src/allocator.rs @@ -32,14 +32,11 @@ impl Allocation { pub fn new(size: usize, align: Option) -> Result { let alignment = align.unwrap_or(DEFAULT_ALIGN_BYTES); 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() { alloc::handle_alloc_error(layout); } - Ok(Self { - ptr: ptr, - layout: layout, - }) + Ok(Self { ptr, layout }) } pub fn as_mut_ptr(&self) -> *mut u8 { @@ -70,7 +67,7 @@ impl Allocation { impl Drop for Allocation { fn drop(&mut self) { unsafe { - alloc::dealloc(self.ptr, self.layout.clone()); + alloc::dealloc(self.ptr, self.layout); } } } diff --git a/rust/runtime/src/array.rs b/rust/runtime/src/array.rs index 0fb588b7db8c..2b6c7c217e28 100644 --- a/rust/runtime/src/array.rs +++ b/rust/runtime/src/array.rs @@ -139,14 +139,18 @@ impl<'d, 's, T> From<&'d [T]> for Storage<'s> { /// /// ``` /// 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 = ndarray::Array::from_vec(vec![1f32, 2., 3., 4.]); /// 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 { Ok(TVMRetValue::default()) }; /// call_packed!(tvm_fn, &mut a_dl); /// /// // 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 = a.try_into().unwrap(); /// ``` #[derive(PartialEq)] pub struct Tensor<'a> { @@ -244,9 +248,9 @@ impl<'a> Tensor<'a> { pub fn to_owned(&self) -> Tensor<'static> { let t = Tensor { data: self.data.to_owned(), - ctx: self.ctx.clone(), - dtype: self.dtype.clone(), - size: self.size.clone(), + ctx: self.ctx, + dtype: self.dtype, + size: self.size, shape: self.shape.clone(), strides: None, byte_offset: 0, @@ -270,7 +274,7 @@ impl<'a> Tensor<'a> { }, size: arr.len(), 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, } } @@ -300,9 +304,9 @@ impl<'a> Tensor<'a> { /// Conversions to `ndarray::Array` from `Tensor`, if the types match. macro_rules! impl_ndarray_try_from_tensor { ($type:ty, $dtype:expr) => { - impl<'a, 't> TryFrom<&'a Tensor<'t>> for ndarray::ArrayD<$type> { + impl<'t> TryFrom> for ndarray::ArrayD<$type> { type Error = Error; - fn try_from(tensor: &'a Tensor) -> Result, Error> { + fn try_from(tensor: Tensor) -> Result, Error> { ensure!( tensor.dtype == $dtype, "Cannot convert Tensor with dtype {:?} to ndarray", @@ -366,10 +370,10 @@ impl<'a> From for Tensor<'a> { Self { data: storage, ctx: TVMContext::default(), - dtype: dtype, - size: size, - shape: shape, - strides: if dlt.strides == ptr::null_mut() { + dtype, + size, + shape, + strides: if dlt.strides.is_null() { None } else { Some(slice::from_raw_parts_mut(dlt.strides as *mut usize, size).to_vec()) diff --git a/rust/runtime/src/graph.rs b/rust/runtime/src/graph.rs index a35c16b0beb8..5e87ee06806c 100644 --- a/rust/runtime/src/graph.rs +++ b/rust/runtime/src/graph.rs @@ -20,7 +20,10 @@ use std::{cmp, collections::HashMap, convert::TryFrom, iter::FromIterator, mem, str}; 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_json; use tvm_common::{ @@ -32,15 +35,15 @@ use tvm_common::{ use crate::{errors::GraphFormatError, Module, Storage, Tensor}; // @see `kTVMNDArrayMagic` in `ndarray.h` -const _NDARRAY_MAGIC: u64 = 0xDD5E40F096B4A13F; +const _NDARRAY_MAGIC: u64 = 0xDD5E_40F0_96B4_A13F; // @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. /// /// # Examples /// -/// ``` +/// ```norun /// let graph_json = fs::read_to_string("graph.json").unwrap(); /// let graph = Graph::try_from(&graph_json).unwrap(); /// ``` @@ -141,7 +144,7 @@ impl<'a> TryFrom<&'a str> for Graph { /// /// # Examples /// -/// ``` +/// ```norun /// use ndarray::Array; /// /// let syslib = SystemLibModule::default(); // a provider of TVM functions @@ -175,8 +178,8 @@ impl<'m, 't> GraphExecutor<'m, 't> { let tensors = Self::setup_storages(&graph)?; Ok(GraphExecutor { op_execs: Self::setup_op_execs(&graph, lib, &tensors)?, - tensors: tensors, - graph: graph, + tensors, + graph, }) } @@ -196,7 +199,7 @@ impl<'m, 't> GraphExecutor<'m, 't> { .1 .iter() .map(|dltype| { - if let Ok((_, dtype)) = tvm_str_to_type(CompleteStr(dltype)) { + if let Ok((_, dtype)) = tvm_str_to_type(dltype) { Ok(dtype) } else { Err(GraphFormatError::InvalidDLType(dltype.to_string())) @@ -207,7 +210,7 @@ impl<'m, 't> GraphExecutor<'m, 't> { 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]; 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::() as usize; storage_num_bytes[storage_id] = cmp::max(nbytes, storage_num_bytes[storage_id]); } @@ -223,9 +226,9 @@ impl<'m, 't> GraphExecutor<'m, 't> { Tensor { data: mem::replace(&mut storages[storage_id], storage), ctx: TVMContext::default(), - dtype: dtype, + dtype, size: shape.iter().product::() as usize, - shape: shape, + shape, strides: None, byte_offset: 0, } @@ -239,7 +242,7 @@ impl<'m, 't> GraphExecutor<'m, 't> { fn setup_op_execs( graph: &Graph, lib: &'m M, - tensors: &Vec>, + tensors: &[Tensor<'t>], ) -> Result>, Error> { ensure!(graph.node_row_ptr.is_some(), "Missing node_row_ptr."); let node_row_ptr = graph.node_row_ptr.as_ref().unwrap(); @@ -258,15 +261,14 @@ impl<'m, 't> GraphExecutor<'m, 't> { continue; } - let func = lib.get_function(&attrs.func_name).ok_or(format_err!( - "Library is missing function {}", - attrs.func_name - ))?; + let func = lib + .get_function(&attrs.func_name) + .ok_or_else(|| format_err!("Library is missing function {}", attrs.func_name))?; let arg_indices = node .inputs .iter() .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 .map(|idx| { @@ -297,6 +299,7 @@ impl<'m, 't> GraphExecutor<'m, 't> { }) } + #[allow(clippy::if_same_then_else)] pub fn set_input>(&mut self, name: S, value: Tensor) { if let Some(idx) = self.get_input_index(name.as_ref()) { // TODO: consider `new_with_params` to avoid ever allocating @@ -322,7 +325,7 @@ impl<'m, 't> GraphExecutor<'m, 't> { /// Returns the graph input with name `name`, if it exists. pub fn get_input>(&mut self, name: S) -> Option<&Tensor> { 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. @@ -353,70 +356,75 @@ impl<'m, 't> GraphExecutor<'m, 't> { } // Converts a string to TVM DLDataTypeCode. @see `String2TVMType` in packed_func.h -named!( - tvm_str_to_type, +named! { + tvm_str_to_type<&str, DataType>, do_parse!( type_name: alpha1 >> - bits: digit1 >> - lanes: opt!(tuple!(tag!("x"), digit1)) >> - (DataType { - code: match type_name { - CompleteStr("int") => DLDataTypeCode_kDLInt, - CompleteStr("uint") => DLDataTypeCode_kDLUInt, - CompleteStr("float") => DLDataTypeCode_kDLFloat, - _ => DLDataTypeCode_kDLFloat, - } as usize, - bits: bits.parse::().unwrap() as usize, - lanes: match lanes { - Some(lanes) => lanes.1.parse::().unwrap() as usize, - None => 1, - }, - }) + bits: digit1 >> + lanes: opt!(complete!(tuple!(tag!("x"), digit1))) >> + ( + DataType { + code: match type_name { + "int" => DLDataTypeCode_kDLInt, + "uint" => DLDataTypeCode_kDLUInt, + "float" => DLDataTypeCode_kDLFloat, + _ => DLDataTypeCode_kDLFloat, + } as usize, + bits: bits.parse::().unwrap() as usize, + lanes: lanes + .map(|(_, lanes)| lanes.parse::().unwrap() as usize) + .unwrap_or(1) + } + ) ) -); +} // Converts a bytes to String. -named!( +named! { name, - map_res!(length_bytes!(le_u64), |b: &[u8]| String::from_utf8( - b.to_vec() - )) -); + map_res!(length_data!(le_u64), |b: &[u8]| String::from_utf8(b.to_vec())) +} // Parses a TVMContext -named!( +named! { tvm_ctx<&[u8], TVMContext>, do_parse!( device_type: le_u32 >> - device_id: le_i32 >> - (TVMContext { device_type: device_type as usize, device_id: device_id as usize }) + device_id: le_i32 >> + ( + TVMContext { + device_type: device_type as usize, + device_id: device_id as usize, + } + ) ) -); +} // Parses a DataType -named!( +named! { data_type<&[u8], DataType>, do_parse!( - code: le_u8 >> - bits: le_u8 >> + code: le_u8 >> + bits: le_u8 >> lanes: le_u16 >> (DataType { code: code as usize, bits: bits as usize, lanes: lanes as usize }) ) -); +} // Parses a Tensor from a TVM array file. -named!( +named! { tensor, do_parse!( - take!(8) - >> bits!(tag_bits!(u64, 64, 0)) - >> ctx: tvm_ctx - >> ndim: le_u32 - >> dtype: data_type - >> shape: count!(map!(le_i64, |sz| sz as i64), ndim as usize) - >> length: le_i64 - >> data: take!(length) - >> (Tensor { + take!(8) >> + le_u64 >> + ctx: tvm_ctx >> + ndim: le_u32 >> + dtype: data_type >> + shape: count!(map!(le_i64, |sz| sz as i64), ndim as usize) >> + length: le_i64 >> + data: take!(length) >> + ( + Tensor { data: Storage::from(data), ctx: ctx, dtype: dtype, @@ -424,26 +432,29 @@ named!( shape: shape, strides: None, byte_offset: 0, - }) + } + ) ) -); +} // Parses a graph params dict from a params binary file. -named!( +named! { parse_param_dict>, do_parse!( - take!(8) - >> bits!(tag_bits!(u64, 64, 0)) - >> names: length_count!(le_u64, name) - >> tensors: length_count!(le_u64, tensor) - >> (HashMap::from_iter(names.into_iter().zip(tensors.into_iter()))) + take!(8) >> + le_u64 >> + names: length_count!(le_u64, name) >> + tensors: length_count!(le_u64, tensor) >> + ( + HashMap::from_iter(names.into_iter().zip(tensors.into_iter())) + ) ) -); +} /// Loads a param dict saved using `nnvm.compiler.save_param_dict`. pub fn load_param_dict(bytes: &[u8]) -> Result, GraphFormatError> { if let Ok((remaining_bytes, param_dict)) = parse_param_dict(bytes) { - if remaining_bytes.len() == 0 { + if remaining_bytes.is_empty() { Ok(param_dict) } else { Err(GraphFormatError::Params) @@ -460,7 +471,7 @@ mod tests { #[test] fn test_str_to_type() { assert_eq!( - tvm_str_to_type(CompleteStr("float24")).unwrap().1, + tvm_str_to_type("float24").unwrap().1, DataType { code: DLDataTypeCode_kDLFloat as usize, bits: 24, @@ -468,7 +479,7 @@ mod tests { } ); assert_eq!( - tvm_str_to_type(CompleteStr("uint111x44")).unwrap().1, + tvm_str_to_type("uint111x44").unwrap().1, DataType { code: DLDataTypeCode_kDLUInt as usize, bits: 111, diff --git a/rust/runtime/src/lib.rs b/rust/runtime/src/lib.rs index 3944fd5d84c3..c614c5bad1bf 100644 --- a/rust/runtime/src/lib.rs +++ b/rust/runtime/src/lib.rs @@ -78,8 +78,8 @@ lazy_static! { } #[no_mangle] -pub extern "C" fn TVMAPISetLastError(cmsg: *const i8) { - *LAST_ERROR.write().unwrap() = Some(unsafe { std::ffi::CStr::from_ptr(cmsg) }); +pub unsafe extern "C" fn TVMAPISetLastError(cmsg: *const i8) { + *LAST_ERROR.write().unwrap() = Some(std::ffi::CStr::from_ptr(cmsg)); } #[no_mangle] diff --git a/rust/runtime/src/module/dso.rs b/rust/runtime/src/module/dso.rs index 3442fad13bf9..8c0e4f4eb0ab 100644 --- a/rust/runtime/src/module/dso.rs +++ b/rust/runtime/src/module/dso.rs @@ -35,8 +35,8 @@ use crate::{ use super::Module; -const TVM_MAIN: &'static [u8] = b"__tvm_main__"; -const TVM_MODULE_CTX: &'static [u8] = b"__tvm_module_ctx"; +const TVM_MAIN: &[u8] = b"__tvm_main__"; +const TVM_MODULE_CTX: &[u8] = b"__tvm_module_ctx"; /// A module backed by a Dynamic Shared Object (dylib). pub struct DsoModule<'a> { @@ -64,22 +64,26 @@ impl<'a> DsoModule<'a> { init_context_func!( lib, - (TVMAPISetLastError, extern "C" fn(*const i8)), + (TVMAPISetLastError, unsafe extern "C" fn(*const i8)), ( 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, - 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, - 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, - 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> { &*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() } } diff --git a/rust/runtime/src/module/mod.rs b/rust/runtime/src/module/mod.rs index 2c7c107f6b30..cdb4910213f9 100644 --- a/rust/runtime/src/module/mod.rs +++ b/rust/runtime/src/module/mod.rs @@ -38,7 +38,7 @@ pub trait Module { fn wrap_backend_packed_func(func_name: String, func: BackendPackedCFunc) -> Box { box move |args: &[TVMArgValue]| { let (values, type_codes): (Vec, Vec) = args - .into_iter() + .iter() .map(|arg| { let (val, code) = arg.to_tvm_value(); (val, code as i32) diff --git a/rust/runtime/src/module/syslib.rs b/rust/runtime/src/module/syslib.rs index 6ee1a2a7d4b9..96e08ab7e197 100644 --- a/rust/runtime/src/module/syslib.rs +++ b/rust/runtime/src/module/syslib.rs @@ -42,8 +42,7 @@ impl Module for SystemLibModule { SYSTEM_LIB_FUNCTIONS .lock() .unwrap() - .get(name.as_ref()) - .map(|f| *f) + .get(name.as_ref()).copied() } } @@ -67,5 +66,5 @@ pub extern "C" fn TVMBackendRegisterSystemLibSymbol( name.to_string(), &*Box::leak(super::wrap_backend_packed_func(name.to_string(), func)), ); - return 0; + 0 } diff --git a/rust/runtime/src/threading.rs b/rust/runtime/src/threading.rs index 5e4afe0732b0..770256ed81b6 100644 --- a/rust/runtime/src/threading.rs +++ b/rust/runtime/src/threading.rs @@ -114,10 +114,7 @@ impl<'a> Threads { (handle, p) }) .unzip(); - Threads { - handles: handles, - queues: queues, - } + Threads { handles, queues } } } @@ -133,7 +130,7 @@ impl ThreadPool { fn new() -> Self { let num_workers = max_concurrency(); ThreadPool { - num_workers: num_workers, + num_workers, threads: Threads::launch(num_workers, ThreadPool::run_worker), } } @@ -181,7 +178,7 @@ unsafe impl Sync for Consumer {} #[cfg(not(target_arch = "wasm32"))] 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) { return threads; } @@ -202,27 +199,30 @@ pub extern "C" fn TVMBackendParallelLaunch( ) -> c_int { if max_concurrency() < 2 { let penv = TVMParallelGroupEnv { - sync_handle: 0 as *mut c_void, + sync_handle: std::ptr::null_mut(), num_task: 1, }; cb(0, &penv as *const _, cdata); } else { THREAD_POOL.with(|pool| { pool.launch(Job { - cb: cb, - cdata: cdata, + cb, + cdata, req_num_tasks: num_task, pending: Arc::new(AtomicUsize::new(0)), }); }); } - return 0; + 0 } // @see https://github.com/dmlc/tvm/issues/988 for information on why this function is used. #[no_mangle] -pub extern "C" fn TVMBackendParallelBarrier(_task_id: usize, penv: *const TVMParallelGroupEnv) { - let barrier: &Arc = unsafe { &*((*penv).sync_handle as *const Arc) }; +pub unsafe extern "C" fn TVMBackendParallelBarrier( + _task_id: usize, + penv: *const TVMParallelGroupEnv, +) { + let barrier: &Arc = &*((*penv).sync_handle as *const Arc); barrier.wait(); } @@ -246,7 +246,7 @@ mod tests { penv: *const TVMParallelGroupEnv, cdata: *const c_void, ) -> i32 { - if cdata == ptr::null() { + if cdata.is_null() { return 0; } unsafe { diff --git a/rust/runtime/src/workspace.rs b/rust/runtime/src/workspace.rs index 888daa052993..88120d4a4ec7 100644 --- a/rust/runtime/src/workspace.rs +++ b/rust/runtime/src/workspace.rs @@ -51,7 +51,7 @@ impl WorkspacePool { } 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); } let idx = self @@ -64,10 +64,7 @@ impl WorkspacePool { } cur_ws_idx.or(Some(idx)).and_then(|cur_idx| { let cur_size = self.workspaces[cur_idx].size(); - Some(match ws_size <= cur_size { - true => idx, - false => cur_idx, - }) + Some(if ws_size <= cur_size { idx } else { cur_idx }) }) }); match idx { @@ -90,9 +87,10 @@ impl WorkspacePool { break; } } - Ok(self - .free - .push(ws_idx.ok_or(format_err!("Tried to free nonexistent workspace."))?)) + if let Some(ws_idx) = ws_idx { + self.free.push(ws_idx); + } + Ok(()) } } @@ -133,5 +131,5 @@ pub extern "C" fn TVMBackendFreeWorkspace( Err(_) => -1, }) as c_int }); - return 0; + 0 } diff --git a/rust/runtime/tests/test_graph_serde.rs b/rust/runtime/tests/test_graph_serde.rs index 846f8a30bc7e..803a535e3990 100644 --- a/rust/runtime/tests/test_graph_serde.rs +++ b/rust/runtime/tests/test_graph_serde.rs @@ -26,11 +26,38 @@ use std::{convert::TryFrom, fs, io::Read}; 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] 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(); - fs::File::open(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/graph.params")) - .expect("Could not find TVM graph. Did you run `tests/build_model.py`?") + fs::File::open(PARAMS_FIXTURE_PATH) + .unwrap() .read_to_end(&mut params_bytes) .unwrap(); let _params = tvm_runtime::load_param_dict(¶ms_bytes); diff --git a/rust/runtime/tests/test_nnvm/src/main.rs b/rust/runtime/tests/test_nnvm/src/main.rs index 9543b35332a3..2ee95b929ce0 100644 --- a/rust/runtime/tests/test_nnvm/src/main.rs +++ b/rust/runtime/tests/test_nnvm/src/main.rs @@ -33,11 +33,11 @@ const IN_DIM: usize = 8; macro_rules! check_sum { ($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); }; ($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); }; ($a:ident, $b:ident) => { @@ -73,11 +73,11 @@ fn main() { .collect::>(), ) .unwrap(); - let w = Array::try_from(params.get("dense0_weight").unwrap()) + let w = Array::try_from(params.get("dense0_weight").unwrap().to_owned()) .unwrap() .into_shape((IN_DIM * 2, IN_DIM)) .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 left = dense.slice(s![.., 0..IN_DIM]); let right = dense.slice(s![.., IN_DIM..]); From b5ec2f10aed4c7e18a1fc73341f27f5a38660f5d Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Mon, 23 Sep 2019 01:34:22 +0000 Subject: [PATCH 13/18] Remove dependency on backtrace-rs --- rust/common/Cargo.toml | 2 +- rust/common/src/value.rs | 30 ++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/rust/common/Cargo.toml b/rust/common/Cargo.toml index ddfc9098418d..60f5a6b336d4 100644 --- a/rust/common/Cargo.toml +++ b/rust/common/Cargo.toml @@ -26,7 +26,7 @@ edition = "2018" bindings = [] [dependencies] -failure = "0.1" +failure = { version = "0.1", default-features = false, features = ["derive"] } ndarray = "0.12" [build-dependencies] diff --git a/rust/common/src/value.rs b/rust/common/src/value.rs index fa0563ebd754..976affbaee39 100644 --- a/rust/common/src/value.rs +++ b/rust/common/src/value.rs @@ -19,8 +19,6 @@ use std::{os::raw::c_char, str::FromStr}; -use failure::Error; - use crate::ffi::*; impl TVMType { @@ -33,10 +31,18 @@ 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}` /// such as "int32", "float32" or with lane "float32x1". impl FromStr for TVMType { - type Err = Error; + type Err = ParseTvmTypeError; fn from_str(type_str: &str) -> Result { if type_str == "bool" { return Ok(TVMType::new(1, 1, 1)); @@ -47,11 +53,15 @@ impl FromStr for TVMType { let lanes = type_lanes .next() .map(|l| ::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) { Some(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), }; @@ -61,7 +71,7 @@ impl FromStr for TVMType { "uint" => 1, "float" => 2, "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)) @@ -116,16 +126,20 @@ impl_pod_tvm_value!(v_float64, f64, f32, f64); impl_pod_tvm_value!(v_type, TVMType); impl_pod_tvm_value!(v_ctx, TVMContext); +#[derive(Debug, Fail)] +#[fail(display = "unsupported device: {}", _0)] +pub struct UnsupportedDeviceError(String); + macro_rules! impl_tvm_context { ( $( $dev_type:ident : [ $( $dev_name:ident ),+ ] ),+ ) => { /// Creates a TVMContext from a string (e.g., "cpu", "gpu", "ext_dev") impl FromStr for TVMContext { - type Err = Error; + type Err = UnsupportedDeviceError; fn from_str(type_str: &str) -> Result { Ok(Self { device_type: match type_str { $( $( 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, }) From 521bfc42bd77f95769246e633f4526162ba098ac Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Mon, 23 Sep 2019 01:49:10 +0000 Subject: [PATCH 14/18] Fix SGX build script --- apps/sgx/README.md | 2 +- apps/sgx/build.rs | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/apps/sgx/README.md b/apps/sgx/README.md index 370b21566438..a83db91ce7e8 100644 --- a/apps/sgx/README.md +++ b/apps/sgx/README.md @@ -18,6 +18,6 @@ ## Setup 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` +2. `rustup component add llvm-tools-preview` to get `llvm-ar` and `llvm-objcopy` 3. `cargo run` to start the enclave TCP server 4. Send a 28x28 "image" to the enclave model server using `head -c $((28*28*4)) /dev/urandom | nc 127.0.0.1 4242` diff --git a/apps/sgx/build.rs b/apps/sgx/build.rs index dcd60b042fb6..3f7181ad9c1c 100644 --- a/apps/sgx/build.rs +++ b/apps/sgx/build.rs @@ -40,10 +40,7 @@ fn main() { let sysroot = String::from_utf8(sysroot_output.stdout).unwrap(); let sysroot = sysroot.trim(); let mut llvm_tools_path = std::path::PathBuf::from(&sysroot); - let target = sysroot.splitn(2, "-").nth(1).expect(&sysroot); - llvm_tools_path.push("lib/rustlib"); - llvm_tools_path.push(target); - llvm_tools_path.push("bin"); + llvm_tools_path.push("lib/rustlib/x86_64-unknown-linux-gnu/bin"); std::process::Command::new(llvm_tools_path.join("llvm-objcopy")) .arg("--globalize-symbol=__tvm_module_startup") From dc37e96800a005a6314062b6696903b45943ac6c Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Mon, 23 Sep 2019 03:54:43 +0000 Subject: [PATCH 15/18] Update sgx demo --- apps/sgx/.cargo/config | 3 + apps/sgx/Cargo.lock | 750 +++++++++++++++++++++++++++++++++++++++ apps/sgx/Cargo.toml | 3 + apps/sgx/README.md | 5 +- apps/sgx/build.rs | 42 ++- apps/sgx/read_results.py | 28 ++ 6 files changed, 820 insertions(+), 11 deletions(-) create mode 100644 apps/sgx/Cargo.lock create mode 100644 apps/sgx/read_results.py diff --git a/apps/sgx/.cargo/config b/apps/sgx/.cargo/config index 6a8d6b2fc9f0..b9dcef4a6092 100644 --- a/apps/sgx/.cargo/config +++ b/apps/sgx/.cargo/config @@ -1,2 +1,5 @@ [build] target = "x86_64-fortanix-unknown-sgx" + +[target.x86_64-fortanix-unknown-sgx] +runner = "ftxsgx-runner-cargo" diff --git a/apps/sgx/Cargo.lock b/apps/sgx/Cargo.lock new file mode 100644 index 000000000000..93755f95aba5 --- /dev/null +++ b/apps/sgx/Cargo.lock @@ -0,0 +1,750 @@ +# 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)" = "" +"checksum backtrace-sys 0.1.31 (git+https://github.com/nhynes/backtrace-rs?branch=fix-sgx)" = "" +"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" diff --git a/apps/sgx/Cargo.toml b/apps/sgx/Cargo.toml index 93fae202abee..00705399304c 100644 --- a/apps/sgx/Cargo.toml +++ b/apps/sgx/Cargo.toml @@ -23,3 +23,6 @@ edition = "2018" [dependencies] tvm-runtime = { path = "../../rust/runtime" } + +[patch.crates-io] +"backtrace" = { git = "https://github.com/nhynes/backtrace-rs", branch = "fix-sgx" } diff --git a/apps/sgx/README.md b/apps/sgx/README.md index a83db91ce7e8..6be52583ab52 100644 --- a/apps/sgx/README.md +++ b/apps/sgx/README.md @@ -19,5 +19,6 @@ 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` -3. `cargo run` to start the enclave TCP server -4. Send a 28x28 "image" to the enclave model server using `head -c $((28*28*4)) /dev/urandom | nc 127.0.0.1 4242` +3. `pip install numpy decorator psutil` +4. `cargo run` to start the enclave TCP server +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` diff --git a/apps/sgx/build.rs b/apps/sgx/build.rs index 3f7181ad9c1c..702dd0486020 100644 --- a/apps/sgx/build.rs +++ b/apps/sgx/build.rs @@ -17,23 +17,41 @@ * 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 = - std::process::Command::new(concat!(env!("CARGO_MANIFEST_DIR"), "/src/build_model.py")) - .arg(&out_dir) - .output() - .expect("Failed to build model"); + 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: {}", + "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 = std::process::Command::new("rustc") + let sysroot_output = Command::new("rustc") .args(&["--print", "sysroot"]) .output() .expect("Failed to get sysroot"); @@ -42,18 +60,24 @@ fn main() { 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("Could not gloablize startup function."); + .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."); + .expect("failed to package model archive"); println!("cargo:rustc-link-lib=static=model"); println!("cargo:rustc-link-search=native={}", out_dir); diff --git a/apps/sgx/read_results.py b/apps/sgx/read_results.py new file mode 100644 index 000000000000..2e41036c7522 --- /dev/null +++ b/apps/sgx/read_results.py @@ -0,0 +1,28 @@ +# 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 struct +import sys + +import numpy as np + +def float_bytes(l): + for i in range(0, len(l), 4): + yield l[i:i + 4] + +floats = [struct.unpack('f', f)[0] for f in float_bytes(sys.stdin.buffer.read())] +print(np.array(floats)) From becf73596457785f52b2e58328633356a408455f Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Mon, 23 Sep 2019 03:58:38 +0000 Subject: [PATCH 16/18] Allow cargo.lock --- tests/lint/check_file_type.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/lint/check_file_type.py b/tests/lint/check_file_type.py index 7fded58b9946..3f4dd4578977 100644 --- a/tests/lint/check_file_type.py +++ b/tests/lint/check_file_type.py @@ -88,6 +88,7 @@ ".gitmodules", "CODEOWNERS", ".scalafmt.conf", + "Cargo.lock" } # List of specific files allowed in relpath to From a3c7f9c8522bfdeb3f291e02867068613fee579a Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Mon, 23 Sep 2019 05:00:07 +0000 Subject: [PATCH 17/18] Fix frontend compilation --- rust/frontend/Cargo.toml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/rust/frontend/Cargo.toml b/rust/frontend/Cargo.toml index 2f87611693b3..1a9a4030f4e8 100644 --- a/rust/frontend/Cargo.toml +++ b/rust/frontend/Cargo.toml @@ -28,10 +28,6 @@ categories = ["api-bindings", "science"] authors = ["TVM Contributors"] edition = "2018" -[lib] -name = "tvm_frontend" -crate-type = ["dylib"] - [dependencies] failure = "0.1" lazy_static = "1.1" From 827fbcd60dbd56395626ec1749c83a1714a81810 Mon Sep 17 00:00:00 2001 From: Nick Hynes Date: Sat, 12 Oct 2019 18:07:44 +0000 Subject: [PATCH 18/18] Re-enable Rust CI --- Jenkinsfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Jenkinsfile b/Jenkinsfile index 6134023f9c21..4707cb9a6999 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -200,6 +200,7 @@ stage('Build') { sh "${docker_run} ${ci_cpu} ./tests/scripts/task_python_unittest.sh" sh "${docker_run} ${ci_cpu} ./tests/scripts/task_python_integration.sh" sh "${docker_run} ${ci_cpu} ./tests/scripts/task_python_vta.sh" + sh "${docker_run} ${ci_cpu} ./tests/scripts/task_rust.sh" } } }