From 0a172a9798e230a6dccc2447b7360037096ddc47 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Wed, 16 Dec 2020 09:04:27 -0800 Subject: [PATCH 01/67] [RUNTIME] Add libbacktrace for backtraces with line numbers - Changed build settings to give absolute paths in debug symbols - Add new TVM error types for internal errors - Move CHECK and LOG(FATAL) to tvm/runtime/logging.h --- 3rdparty/dmlc-core | 2 +- CMakeLists.txt | 43 +- cmake/config.cmake | 5 + cmake/modules/Libbacktrace.cmake | 38 ++ cmake/modules/libbacktrace_macos.patch | 13 + include/tvm/auto_scheduler/loop_state.h | 0 include/tvm/auto_scheduler/transform_step.h | 0 include/tvm/ir/attrs.h | 4 +- include/tvm/ir/error.h | 26 +- include/tvm/ir/type_relation.h | 2 +- include/tvm/relay/analysis.h | 2 +- include/tvm/runtime/container.h | 1 + include/tvm/runtime/data_type.h | 2 +- include/tvm/runtime/logging.h | 372 ++++++++++++++++++ include/tvm/runtime/object.h | 2 +- include/tvm/runtime/packed_func.h | 6 +- include/tvm/runtime/vm/bytecode.h | 2 +- include/tvm/support/logging.h | 158 -------- include/tvm/support/with.h | 2 +- licenses/LICENSE.libbacktrace.txt | 29 ++ python/tvm/_ffi/base.py | 4 +- src/auto_scheduler/compute_dag.cc | 2 +- src/auto_scheduler/feature.cc | 2 +- .../search_policy/sketch_policy_rules.cc | 4 +- src/auto_scheduler/transform_step.cc | 2 +- src/ir/error.cc | 3 +- src/parser/parser.cc | 8 +- src/parser/span_check.h | 2 +- src/relay/analysis/annotated_region_set.cc | 5 +- src/relay/analysis/kind_check.cc | 2 +- src/relay/analysis/type_solver.cc | 4 +- src/relay/analysis/well_formed.cc | 2 +- src/relay/backend/vm/compiler.cc | 2 +- src/relay/backend/vm/compiler.h | 2 +- src/relay/backend/vm/inline_primitives.cc | 2 +- src/relay/backend/vm/lambda_lift.cc | 2 +- src/relay/backend/vm/removed_unused_funcs.cc | 2 +- src/relay/op/nn/convolution.h | 2 +- src/relay/op/tensor/transform.cc | 10 +- src/relay/op/tensor/transform.h | 4 +- src/relay/op/type_relations.cc | 2 +- src/relay/qnn/op/concatenate.cc | 14 +- src/relay/transforms/inline.cc | 2 +- src/relay/transforms/partial_eval.cc | 4 +- src/relay/transforms/simplify_expr.cc | 2 +- src/relay/transforms/to_a_normal_form.cc | 2 +- .../transforms/to_basic_block_normal_form.cc | 2 +- src/relay/transforms/type_infer.cc | 2 +- src/runtime/c_runtime_api.cc | 7 +- src/runtime/contrib/cblas/cblas.cc | 2 +- src/runtime/contrib/cblas/mkl.cc | 2 +- src/runtime/contrib/cblas/mkldnn.cc | 2 +- src/runtime/contrib/cublas/cublas.cc | 2 +- src/runtime/contrib/cublas/cublas_utils.h | 2 +- src/runtime/contrib/cudnn/cudnn_utils.h | 2 +- src/runtime/contrib/miopen/miopen_utils.h | 2 +- src/runtime/contrib/mps/mps_utils.h | 2 +- src/runtime/contrib/nnpack/convolution.cc | 2 +- src/runtime/contrib/nnpack/fully_connected.cc | 2 +- src/runtime/contrib/nnpack/nnpack_utils.h | 2 +- .../contrib/random/mt_random_engine.cc | 2 +- src/runtime/contrib/random/random.cc | 2 +- src/runtime/contrib/rocblas/rocblas.cc | 2 +- .../contrib/tensorrt/tensorrt_logger.h | 2 +- src/runtime/cpu_device_api.cc | 2 +- src/runtime/file_utils.cc | 2 +- src/runtime/graph/graph_runtime.cc | 2 +- src/runtime/hexagon/hexagon_device_api.cc | 2 +- src/runtime/hexagon/hexagon_module.cc | 2 +- src/runtime/hexagon/hexagon_module.h | 2 +- src/runtime/hexagon/sim/hexagon_device_sim.cc | 4 +- .../hexagon/target/hexagon_dsprpcapi.cc | 2 +- .../hexagon/target/hexagon_dsprpcapi.h | 2 +- src/runtime/hexagon/target/hexagon_stubapi.cc | 2 +- src/runtime/hexagon/target/hexagon_stubapi.h | 2 +- src/runtime/logging.cc | 124 ++++++ src/runtime/metal/metal_common.h | 2 +- src/runtime/micro/micro_session.cc | 2 +- src/runtime/minrpc/minrpc_server.h | 2 +- src/runtime/ndarray.cc | 2 +- src/runtime/object.cc | 2 +- src/runtime/opencl/opencl_common.h | 2 +- src/runtime/registry.cc | 2 +- src/runtime/rocm/rocm_device_api.cc | 2 +- src/runtime/rpc/rpc_device_api.cc | 4 +- src/runtime/rpc/rpc_endpoint.cc | 10 +- src/runtime/rpc/rpc_module.cc | 4 +- src/runtime/rpc/rpc_session.cc | 8 +- src/runtime/runtime_base.h | 6 +- src/runtime/thread_pool.cc | 2 +- src/runtime/threading_backend.cc | 2 +- src/runtime/vm/bytecode.cc | 2 +- src/runtime/vm/vm.cc | 2 +- src/runtime/vulkan/vulkan_common.h | 2 +- src/runtime/vulkan/vulkan_shader.h | 2 +- src/support/base64.h | 2 +- src/support/parallel_for.cc | 2 +- src/support/pipe.h | 2 +- src/support/socket.h | 2 +- src/target/llvm/llvm_common.cc | 2 +- src/target/target.cc | 81 ++-- tests/cpp/parallel_for_test.cc | 2 +- 102 files changed, 806 insertions(+), 337 deletions(-) create mode 100644 cmake/modules/Libbacktrace.cmake create mode 100644 cmake/modules/libbacktrace_macos.patch mode change 100755 => 100644 include/tvm/auto_scheduler/loop_state.h mode change 100755 => 100644 include/tvm/auto_scheduler/transform_step.h create mode 100644 include/tvm/runtime/logging.h delete mode 100644 include/tvm/support/logging.h create mode 100644 licenses/LICENSE.libbacktrace.txt mode change 100755 => 100644 src/auto_scheduler/transform_step.cc create mode 100644 src/runtime/logging.cc diff --git a/3rdparty/dmlc-core b/3rdparty/dmlc-core index 6c401e242c59..21cc7de0dc9f 160000 --- a/3rdparty/dmlc-core +++ b/3rdparty/dmlc-core @@ -1 +1 @@ -Subproject commit 6c401e242c59a1f4c913918246591bb13fd714e7 +Subproject commit 21cc7de0dc9fd6acb796e1be6181fa8e6b6c8f41 diff --git a/CMakeLists.txt b/CMakeLists.txt index 451b6a7ee2c2..936f3ee77703 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,11 @@ tvm_option(USE_TF_TVMDSOOP "Build with TensorFlow TVMDSOOp" OFF) tvm_option(USE_FALLBACK_STL_MAP "Use TVM's POD compatible Map" OFF) tvm_option(USE_ETHOSN "Build with Arm Ethos-N" OFF) tvm_option(INDEX_DEFAULT_I64 "Defaults the index datatype to int64" ON) +set(_LIBBACKTRACE_DEFAULT OFF) +if(CMAKE_SYSTEM_NAME MATCHES "Darwin" OR CMAKE_SYSTEM_NAME MATCHES "Linux") + set(_LIBBACKTRACE_DEFAULT ON) +endif() +tvm_option(USE_LIBBACKTRACE "Build libbacktrace to supply linenumbers on stack traces" ${_LIBBACKTRACE_DEFAULT}) # 3rdparty libraries tvm_option(DLPACK_PATH "Path to DLPACK" "3rdparty/dlpack/include") @@ -88,6 +93,7 @@ include_directories(${CMAKE_INCLUDE_PATH}) include_directories("include") include_directories(SYSTEM ${DLPACK_PATH}) include_directories(SYSTEM ${DMLC_PATH}) +add_definitions(-DDMLC_USE_LOGGING_LIBRARY=) include_directories(SYSTEM ${RANG_PATH}) include_directories(SYSTEM ${COMPILER_RT_PATH}) include_directories(SYSTEM ${PICOJSON_PATH}) @@ -388,6 +394,17 @@ set_property(TARGET tvm APPEND PROPERTY LINK_OPTIONS "${TVM_VISIBILITY_FLAG}") add_library(tvm_runtime SHARED $) set_property(TARGET tvm_runtime APPEND PROPERTY LINK_OPTIONS "${TVM_VISIBILITY_FLAG}") +if(USE_LIBBACKTRACE) + message(STATUS "Building with libbacktrace...") + include(cmake/modules/Libbacktrace.cmake) + target_link_libraries(tvm PRIVATE libbacktrace) + target_link_libraries(tvm_objs PRIVATE libbacktrace) + target_link_libraries(tvm_runtime PRIVATE libbacktrace) + target_link_libraries(tvm_runtime_objs PRIVATE libbacktrace) +else() + add_definitions(-DTVM_BACKTRACE_DISABLED) +endif() + if(USE_MICRO) # NOTE: cmake doesn't track dependencies at the file level across subdirectories. For the # Unix Makefiles generator, need to add these explicit target-level dependency) @@ -402,9 +419,9 @@ endif() if(USE_RELAY_DEBUG) message(STATUS "Building Relay in debug mode...") target_compile_definitions(tvm_objs PRIVATE "USE_RELAY_DEBUG") - target_compile_definitions(tvm_objs PRIVATE "DMLC_LOG_DEBUG") + target_compile_definitions(tvm_objs PRIVATE "TVM_LOG_DEBUG") target_compile_definitions(tvm_runtime_objs PRIVATE "USE_RELAY_DEBUG") - target_compile_definitions(tvm_runtime_objs PRIVATE "DMLC_LOG_DEBUG") + target_compile_definitions(tvm_runtime_objs PRIVATE "TVM_LOG_DEBUG") else() target_compile_definitions(tvm_objs PRIVATE "NDEBUG") target_compile_definitions(tvm_runtime_objs PRIVATE "NDEBUG") @@ -543,3 +560,25 @@ if(MSVC) target_compile_definitions(tvm_objs PRIVATE -DTVM_EXPORTS) target_compile_definitions(tvm_runtime_objs PRIVATE -DTVM_EXPORTS) endif() + +set(TVM_IS_DEBUG_BUILD OFF) +if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo" OR CMAKE_CXX_FLAGS MATCHES "-g") + set(TVM_IS_DEBUG_BUILD ON) +endif() + +# Change relative paths in backtrace to absolute ones +if(TVM_IS_DEBUG_BUILD) + target_compile_options(tvm PRIVATE "-ffile-prefix-map=..=${CMAKE_CURRENT_SOURCE_DIR}") +endif() + +# Run dsymutil to generate debugging symbols for backtraces +if(APPLE AND TVM_IS_DEBUG_BUILD) + find_program(DSYMUTIL dsymutil) + mark_as_advanced(DSYMUTIL) + add_custom_command(TARGET tvm + POST_BUILD + COMMAND ${DSYMUTIL} ARGS $ + COMMENT "Running dsymutil" + VERBATIM + ) +endif() diff --git a/cmake/config.cmake b/cmake/config.cmake index 65859566a664..ed45f20e6b0d 100644 --- a/cmake/config.cmake +++ b/cmake/config.cmake @@ -272,3 +272,8 @@ set(USE_TARGET_ONNX OFF) # Whether enable BNNS runtime set(USE_BNNS OFF) + +# Whether to use libbacktrace +# Libbacktrace provides line and column information on stack traces from errors. It is only +# supported on linux and macOS. +# set(USE_LIBBACKTRACE OFF) diff --git a/cmake/modules/Libbacktrace.cmake b/cmake/modules/Libbacktrace.cmake new file mode 100644 index 000000000000..d9c5958b1265 --- /dev/null +++ b/cmake/modules/Libbacktrace.cmake @@ -0,0 +1,38 @@ +include(ExternalProject) + +# Git tag for libbacktrace +set(TAG dedbe13fda00253fe5d4f2fb812c909729ed5937) + +ExternalProject_Add(project_libbacktrace + PREFIX libbacktrace + GIT_REPOSITORY https://github.com/ianlancetaylor/libbacktrace.git + GIT_TAG ${TAG} + CONFIGURE_COMMAND "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/src/project_libbacktrace/configure" + "--prefix=${CMAKE_CURRENT_BINARY_DIR}/libbacktrace" + INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace" + BUILD_COMMAND make + INSTALL_COMMAND make install + BUILD_BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/lib/libbacktrace.a" + "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include/backtrace.h" + # disable the builtin update because it rebuilds on every build + UPDATE_DISCONNECTED ON + UPDATE_COMMAND "" + # libbacktrace has a bug on macOS with shared libraries. + PATCH_COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/libbacktrace_macos.patch + ) + +# Only rebuild libbacktrace if this file changes +ExternalProject_Add_Step(project_libbacktrace update-new + DEPENDERS configure + DEPENDS "${CMAKE_CURRENT_LIST_DIR}/Libbacktrace.cmake" + COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/src/project_libbacktrace; git checkout -f ${TAG} + ) + +add_library(libbacktrace STATIC IMPORTED) +add_dependencies(libbacktrace project_libbacktrace) +set_property(TARGET libbacktrace + PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/lib/libbacktrace.a) +# create include directory so cmake doesn't complain +file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include) +target_include_directories(libbacktrace + INTERFACE $) diff --git a/cmake/modules/libbacktrace_macos.patch b/cmake/modules/libbacktrace_macos.patch new file mode 100644 index 000000000000..3d6cbf672091 --- /dev/null +++ b/cmake/modules/libbacktrace_macos.patch @@ -0,0 +1,13 @@ +diff --git a/macho.c b/macho.c +index 66e101e..d00aea9 100644 +--- a/macho.c ++++ b/macho.c +@@ -1268,7 +1268,7 @@ backtrace_initialize (struct backtrace_state *state, const char *filename, + mff = macho_nodebug; + if (!macho_add (state, name, d, 0, NULL, base_address, 0, + error_callback, data, &mff, &mfs)) +- return 0; ++ continue; + + if (mff != macho_nodebug) + macho_fileline_fn = mff; diff --git a/include/tvm/auto_scheduler/loop_state.h b/include/tvm/auto_scheduler/loop_state.h old mode 100755 new mode 100644 diff --git a/include/tvm/auto_scheduler/transform_step.h b/include/tvm/auto_scheduler/transform_step.h old mode 100755 new mode 100644 diff --git a/include/tvm/ir/attrs.h b/include/tvm/ir/attrs.h index f05ab04c3305..da7bc12619bd 100644 --- a/include/tvm/ir/attrs.h +++ b/include/tvm/ir/attrs.h @@ -92,12 +92,12 @@ inline DataType NullValue() { } /*! \brief Error thrown during attribute checking. */ -struct AttrError : public dmlc::Error { +struct AttrError : public Error { /*! * \brief constructor * \param msg error message */ - explicit AttrError(std::string msg) : dmlc::Error("AttributeError:" + msg) {} + explicit AttrError(std::string msg) : Error("AttributeError:" + msg) {} }; /*! diff --git a/include/tvm/ir/error.h b/include/tvm/ir/error.h index ac7b96a3bd59..6ff61781ac44 100644 --- a/include/tvm/ir/error.h +++ b/include/tvm/ir/error.h @@ -36,11 +36,11 @@ namespace tvm { /*! * \brief A wrapper around std::stringstream to build error. * - * Can be consumed by Error to construct an error. + * Can be consumed by CompileError to construct an error. * * \code * - * void ReportError(const Error& err); + * void ReportError(const CompileError& err); * * void Test(int number) { * // Use error reporter to construct an error. @@ -59,13 +59,13 @@ struct ErrorBuilder { private: std::stringstream stream_; - friend class Error; + friend class CompileError; }; /*! * \brief Custom Error class to be thrown during compilation. */ -class Error : public dmlc::Error { +class CompileError : public Error { public: /*! \brief Location of the error */ Span span; @@ -73,20 +73,20 @@ class Error : public dmlc::Error { * \brief construct error from message. * \param msg The message */ - explicit Error(const std::string& msg) : dmlc::Error(msg), span(nullptr) {} + explicit CompileError(const std::string& msg) : Error(msg), span(nullptr) {} /*! * \brief construct error from error builder. * \param err The error builder */ - Error(const ErrorBuilder& err) : dmlc::Error(err.stream_.str()), span(nullptr) {} // NOLINT(*) + CompileError(const ErrorBuilder& err) : Error(err.stream_.str()), span(nullptr) {} // NOLINT(*) /*! * \brief copy constructor. * \param other The other ereor. */ - Error(const Error& other) : dmlc::Error(other.what()), span(other.span) {} // NOLINT(*) + CompileError(const CompileError& other) : Error(other.what()), span(other.span) {} // NOLINT(*) /*! * \brief default constructor. */ - Error() : dmlc::Error(""), span(nullptr) {} + CompileError() : Error(""), span(nullptr) {} }; /*! @@ -115,13 +115,13 @@ class ErrorReporter { ErrorReporter() : errors_(), node_to_error_() {} /*! - * \brief Report a tvm::Error. + * \brief Report a CompileError. * * This API is useful for reporting spanned errors. * * \param err The error to report. */ - void Report(const Error& err) { + void Report(const CompileError& err) { if (!err.span.defined()) { throw err; } @@ -143,7 +143,7 @@ class ErrorReporter { */ void ReportAt(const GlobalVar& global, const ObjectRef& node, std::stringstream& err) { std::string err_msg = err.str(); - this->ReportAt(global, node, Error(err_msg)); + this->ReportAt(global, node, CompileError(err_msg)); } /*! @@ -158,7 +158,7 @@ class ErrorReporter { * \param node The expression or type to report the error at. * \param err The error to report. */ - void ReportAt(const GlobalVar& global, const ObjectRef& node, const Error& err); + void ReportAt(const GlobalVar& global, const ObjectRef& node, const CompileError& err); /*! * \brief Render all reported errors and exit the program. @@ -176,7 +176,7 @@ class ErrorReporter { inline bool AnyErrors() { return errors_.size() != 0; } private: - std::vector errors_; + std::vector errors_; std::unordered_map, ObjectPtrHash, ObjectPtrEqual> node_to_error_; std::unordered_map node_to_gv_; }; diff --git a/include/tvm/ir/type_relation.h b/include/tvm/ir/type_relation.h index 462588006c9b..dd6861750a10 100644 --- a/include/tvm/ir/type_relation.h +++ b/include/tvm/ir/type_relation.h @@ -29,7 +29,7 @@ #include #include #include -#include +#include namespace tvm { diff --git a/include/tvm/relay/analysis.h b/include/tvm/relay/analysis.h index 5dd837038731..f88b04994099 100644 --- a/include/tvm/relay/analysis.h +++ b/include/tvm/relay/analysis.h @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include diff --git a/include/tvm/runtime/container.h b/include/tvm/runtime/container.h index 336fef21ab88..362582f4dab9 100644 --- a/include/tvm/runtime/container.h +++ b/include/tvm/runtime/container.h @@ -30,6 +30,7 @@ #include #include +#include #include #include diff --git a/include/tvm/runtime/data_type.h b/include/tvm/runtime/data_type.h index 7d914ce6bff9..b4fdcbff58b4 100644 --- a/include/tvm/runtime/data_type.h +++ b/include/tvm/runtime/data_type.h @@ -25,7 +25,7 @@ #define TVM_RUNTIME_DATA_TYPE_H_ #include -#include +#include #include #include diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h new file mode 100644 index 000000000000..01cf54090da4 --- /dev/null +++ b/include/tvm/runtime/logging.h @@ -0,0 +1,372 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * \file tvm/runtime/logging.h + * \brief logging utilities + * + * We define our own CHECK and LOG macros to replace those from dmlc-core. + * These macros are then injected into dmlc-core via the + * DMLC_USE_LOGGING_LIBRARY define. dmlc-core will #include this file wherever + * it needs logging. + */ +#ifndef TVM_RUNTIME_LOGGING_H_ +#define TVM_RUNTIME_LOGGING_H_ + +#include +#include +#include +#include +#include + +#include "tvm/runtime/c_runtime_api.h" + +// a technique that enables overriding macro names on the number of parameters. This is used +// to define other macros below +#define GET_MACRO(_1, _2, _3, _4, _5, NAME, ...) NAME + +/*! + * \brief COND_X calls COND_X_N where N is the number of parameters passed to COND_X + * X can be any of CHECK_GE, CHECK_EQ, CHECK, or LOG COND_X (but not COND_X_N) + * are supposed to be used outside this file. + * The first parameter of COND_X (and therefore, COND_X_N), which we call 'quit_on_assert', + * is a boolean. The rest of the parameters of COND_X is the same as the parameters of X. + * quit_on_assert determines the overall behavior of COND_X. If it's true COND_X + * quits the program on assertion failure. If it's false, then it moves on and somehow reports + * the assertion failure back to the macro caller in an appropriate manner (e.g, 'return false' + * in a function, or 'continue' or 'break' in a loop) + * The default behavior when quit_on_assertion is false, is to 'return false'. If this is not + * desirable, the macro caller can pass one more last parameter to COND_X to tell COND_X what + * to do when when quit_on_assertion is false and the assertion fails. + * + * Rationale: These macros were designed to implement functions that have two behaviors + * in a concise way. Those behaviors are quitting on assertion failures, or trying to + * move on from assertion failures. Note that these macros hide lots of control flow in them, + * and therefore, makes the logic of the whole code slightly harder to understand. However, + * in pieces of code that use these macros frequently, it will significantly shorten the + * amount of code needed to be read, and we won't need to clutter the main logic of the + * function by repetitive control flow structure. The first problem + * mentioned will be improved over time as the developer gets used to the macro. + * + * Here is an example of how to use it + * \code + * bool f(..., bool quit_on_assertion) { + * int a = 0, b = 0; + * ... + * a = ... + * b = ... + * // if quit_on_assertion is true, if a==b, continue, otherwise quit. + * // if quit_on_assertion is false, if a==b, continue, otherwise 'return false' (default + * behaviour) COND_CHECK_EQ(quit_on_assertion, a, b) << "some error message when quiting" + * ... + * for (int i = 0; i < N; i++) { + * a = ... + * b = ... + * // if quit_on_assertion is true, if a==b, continue, otherwise quit. + * // if quit_on_assertion is false, if a==b, continue, otherwise 'break' (non-default + * // behaviour, therefore, has to be explicitly specified) + * COND_CHECK_EQ(quit_on_assertion, a, b, break) << "some error message when quiting" + * } + * } + * \endcode + */ +#define COND_CHECK_GE(...) \ + GET_MACRO(__VA_ARGS__, COND_CHECK_GE_5, COND_CHECK_GE_4, COND_CHECK_GE_3)(__VA_ARGS__) +#define COND_CHECK_EQ(...) \ + GET_MACRO(__VA_ARGS__, COND_CHECK_EQ_5, COND_CHECK_EQ_4, COND_CHECK_EQ_3)(__VA_ARGS__) +#define COND_CHECK(...) \ + GET_MACRO(__VA_ARGS__, COND_CHECK_5, COND_CHECK_4, COND_CHECK_3, COND_CHECK_2)(__VA_ARGS__) +#define COND_LOG(...) \ + GET_MACRO(__VA_ARGS__, COND_LOG_5, COND_LOG_4, COND_LOG_3, COND_LOG_2)(__VA_ARGS__) + +// Not supposed to be used by users directly. +#define COND_CHECK_OP(quit_on_assert, x, y, what, op) \ + if (!quit_on_assert) { \ + if (!((x)op(y))) what; \ + } else /* NOLINT(*) */ \ + CHECK_##op(x, y) + +#define COND_CHECK_EQ_4(quit_on_assert, x, y, what) COND_CHECK_OP(quit_on_assert, x, y, what, ==) +#define COND_CHECK_GE_4(quit_on_assert, x, y, what) COND_CHECK_OP(quit_on_assert, x, y, what, >=) + +#define COND_CHECK_3(quit_on_assert, x, what) \ + if (!quit_on_assert) { \ + if (!(x)) what; \ + } else /* NOLINT(*) */ \ + CHECK(x) + +#define COND_LOG_3(quit_on_assert, x, what) \ + if (!quit_on_assert) { \ + what; \ + } else /* NOLINT(*) */ \ + LOG(x) + +#define COND_CHECK_EQ_3(quit_on_assert, x, y) COND_CHECK_EQ_4(quit_on_assert, x, y, return false) +#define COND_CHECK_GE_3(quit_on_assert, x, y) COND_CHECK_GE_4(quit_on_assert, x, y, return false) +#define COND_CHECK_2(quit_on_assert, x) COND_CHECK_3(quit_on_assert, x, return false) +#define COND_LOG_2(quit_on_assert, x) COND_LOG_3(quit_on_assert, x, return false) + +#define TVM_THROW_EXCEPTION noexcept(false) + +namespace tvm { + +#ifndef TVM_BACKTRACE_DISABLED +/* \brief Generate a backtrace when called. */ +std::string Backtrace(); +#else +// TODO(bkimball,tkonolige) This inline function is to work around a linking error I am having when +// using MSVC If the function definition is in logging.cc then the linker can't find it no matter +// what kind of attributes (dllexport) I decorate it with. This is temporary and will be addressed +// when we get backtrace working on Windows. +inline std::string Backtrace() { return ""; } +#endif + +/*! \brief Base error type for TVM. Wraps a string message. */ +class Error : public std::runtime_error { + public: + explicit Error(const std::string& s) : std::runtime_error(s) {} +}; + +/*! \brief Error type for errors from CHECK, ICHECK, and LOG(FATAL). This error + * contains a backtrace of where it occured. + */ +class InternalError : public Error { + public: + /*! \brief Construct an error. Not recommended to use directly. Instead use LOG(FATAL). + * + * \param file The file where the error occurred. + * \param lineno The line number where the error occurred. + * \param message The error message to display. + * \param time The time at which the error occurred. + * \param backtrace Backtrace from when the error occurred. + */ + InternalError(const std::string& file, int lineno, const std::string& message, + const std::time_t& time = std::time(nullptr), std::string backtrace = Backtrace()) + : Error(""), + file_(file), + lineno_(lineno), + message_(message), + time_(time), + backtrace_(backtrace) { + std::ostringstream s; + s << "[" << std::put_time(std::localtime(&time), "%H:%M:%S") << "] " << file << ":" << lineno + << ": " << std::endl + << backtrace << std::endl + << message << std::endl; + full_message_ = s.str(); + } + /*! \brief The file in which the error occurred. */ + const std::string& file() const { return file_; } + /*! \brief The message associated with this error. */ + const std::string& message() const { return message_; } + /*! \brief Formatted error message including file, linenumber, backtrace, and message. */ + const std::string& full_message() const { return full_message_; } + /*! \brief The backtrace from where this error occurred. */ + const std::string& backtrace() const { return backtrace_; } + /*! \brief The time at which this error occurred. */ + const std::time_t& time() const { return time_; } + /*! \brief The line number at which this error occurred. */ + int lineno() const { return lineno_; } + virtual const char* what() const noexcept { return full_message_.c_str(); } + + private: + std::string file_; + int lineno_; + std::string message_; + std::time_t time_; + std::string backtrace_; + std::string full_message_; // holds the full error string +}; + +/*! \brief Class to accumulate an error message and throw it. Do not use + * directly, instead use LOG(FATAL). + */ +class LogFatal { + public: + LogFatal(const std::string& file, int lineno) : file_(file), lineno_(lineno) {} + ~LogFatal() TVM_THROW_EXCEPTION { throw InternalError(file_, lineno_, stream_.str()); } + std::ostringstream& stream() { return stream_; } + + private: + std::ostringstream stream_; + std::string file_; + int lineno_; +}; + +/*! \brief Class to accumulate an log message. Do not use directly, instead use + * LOG(INFO), LOG(WARNING), LOG(ERROR). + */ +class LogMessage { + public: + LogMessage(const std::string& file, int lineno) { + std::time_t t = std::time(nullptr); + stream_ << "[" << std::put_time(std::localtime(&t), "%H:%M:%S") << "] " << file << ":" << lineno + << ": "; + } + ~LogMessage() { std::cerr << stream_.str() << std::endl; } + std::ostringstream& stream() { return stream_; } + + private: + std::ostringstream stream_; +}; + +// Below is from dmlc-core +// This class is used to explicitly ignore values in the conditional +// logging macros. This avoids compiler warnings like "value computed +// is not used" and "statement has no effect". +class LogMessageVoidify { + public: + LogMessageVoidify() {} + // This has to be an operator with a precedence lower than << but + // higher than "?:". See its usage. + void operator&(std::ostream&) {} +}; + +// Also from dmlc-core +inline bool DebugLoggingEnabled() { + static int state = 0; + if (state == 0) { + if (auto var = std::getenv("TVM_LOG_DEBUG")) { + if (std::string(var) == "1") { + state = 1; + } else { + state = -1; + } + } else { + // by default hide debug logging. + state = -1; + } + } + return state == 1; +} + +#define LOG(level) LOG_##level +#define LOG_FATAL ::tvm::LogFatal(__FILE__, __LINE__).stream() +#define LOG_INFO ::tvm::LogMessage(__FILE__, __LINE__).stream() +#define LOG_ERROR (::tvm::LogMessage(__FILE__, __LINE__).stream() << "error: ") +#define LOG_WARNING (::tvm::LogMessage(__FILE__, __LINE__).stream() << "warning: ") + +#define TVM_CHECK_BINARY_OP(name, op, x, y) \ + if (!((x)op(y))) \ + ::tvm::LogFatal(__FILE__, __LINE__).stream() << "Check failed: " << #x " " #op " " #y << ": " + +#define CHECK(x) \ + if (!(x)) ::tvm::LogFatal(__FILE__, __LINE__).stream() << "Check failed: " #x << " == false: " + +#define CHECK_LT(x, y) TVM_CHECK_BINARY_OP(_LT, <, x, y) +#define CHECK_GT(x, y) TVM_CHECK_BINARY_OP(_GT, >, x, y) +#define CHECK_LE(x, y) TVM_CHECK_BINARY_OP(_LE, <=, x, y) +#define CHECK_GE(x, y) TVM_CHECK_BINARY_OP(_GE, >=, x, y) +#define CHECK_EQ(x, y) TVM_CHECK_BINARY_OP(_EQ, ==, x, y) +#define CHECK_NE(x, y) TVM_CHECK_BINARY_OP(_NE, !=, x, y) +#define CHECK_NOTNULL(x) \ + ((x) == nullptr ? ::tvm::LogFatal(__FILE__, __LINE__).stream() << "Check not null: " #x << ' ', \ + (x) : (x)) // NOLINT(*) + +#define LOG_IF(severity, condition) \ + !(condition) ? (void)0 : ::tvm::LogMessageVoidify() & LOG(severity) + +#if TVM_LOG_DEBUG + +#define LOG_DFATAL LOG_FATAL +#define DFATAL FATAL +#define DLOG(severity) LOG_IF(severity, ::tvm::DebugLoggingEnabled()) +#define DLOG_IF(severity, condition) LOG_IF(severity, ::tvm::DebugLoggingEnabled() && (condition)) + +#else + +#define LOG_DFATAL LOG_ERROR +#define DFATAL ERROR +#define DLOG(severity) true ? (void)0 : ::tvm::LogMessageVoidify() & LOG(severity) +#define DLOG_IF(severity, condition) \ + (true || !(condition)) ? (void)0 : ::tvm::LogMessageVoidify() & LOG(severity) + +#endif + +#if TVM_LOG_DEBUG +#define DCHECK(x) \ + while (false) CHECK(x) +#define DCHECK_LT(x, y) \ + while (false) CHECK((x) < (y)) +#define DCHECK_GT(x, y) \ + while (false) CHECK((x) > (y)) +#define DCHECK_LE(x, y) \ + while (false) CHECK((x) <= (y)) +#define DCHECK_GE(x, y) \ + while (false) CHECK((x) >= (y)) +#define DCHECK_EQ(x, y) \ + while (false) CHECK((x) == (y)) +#define DCHECK_NE(x, y) \ + while (false) CHECK((x) != (y)) +#else +#define DCHECK(x) CHECK(x) +#define DCHECK_LT(x, y) CHECK((x) < (y)) +#define DCHECK_GT(x, y) CHECK((x) > (y)) +#define DCHECK_LE(x, y) CHECK((x) <= (y)) +#define DCHECK_GE(x, y) CHECK((x) >= (y)) +#define DCHECK_EQ(x, y) CHECK((x) == (y)) +#define DCHECK_NE(x, y) CHECK((x) != (y)) +#endif + +constexpr const char* kTVM_INTERNAL_ERROR_MESSAGE = + "---------------------------------------------------------------\n" + "An internal invariant was violated during the execution of TVM.\n" + "Please read TVM's error reporting guidelines.\n" + "More details can be found here: https://discuss.tvm.ai/t/error-reporting/7793.\n" + "---------------------------------------------------------------\n"; + +#define TVM_ICHECK_INDENT " " + +#define ICHECK_BINARY_OP(name, op, x, y) \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wsign-compare\"") if (!((x)op(y))) \ + _Pragma("GCC diagnostic pop")::tvm::LogFatal(__FILE__, __LINE__) \ + .stream() \ + << ::tvm::kTVM_INTERNAL_ERROR_MESSAGE << std::endl \ + << TVM_ICHECK_INDENT << "Check failed: " << #x " " #op " " #y << ": " + +#define ICHECK(x) \ + if (!(x)) \ + ::tvm::LogFatal(__FILE__, __LINE__).stream() \ + << ::tvm::kTVM_INTERNAL_ERROR_MESSAGE << TVM_ICHECK_INDENT << "Check failed: " #x \ + << " == false: " + +#define ICHECK_LT(x, y) ICHECK_BINARY_OP(_LT, <, x, y) +#define ICHECK_GT(x, y) ICHECK_BINARY_OP(_GT, >, x, y) +#define ICHECK_LE(x, y) ICHECK_BINARY_OP(_LE, <=, x, y) +#define ICHECK_GE(x, y) ICHECK_BINARY_OP(_GE, >=, x, y) +#define ICHECK_EQ(x, y) ICHECK_BINARY_OP(_EQ, ==, x, y) +#define ICHECK_NE(x, y) ICHECK_BINARY_OP(_NE, !=, x, y) +#define ICHECK_NOTNULL(x) \ + ((x) == nullptr ? ::tvm::LogFatal(__FILE__, __LINE__).stream() \ + << ::tvm::kTVM_INTERNAL_ERROR_MESSAGE << TVM_ICHECK_INDENT \ + << "Check not null: " #x << ' ', \ + (x) : (x)) // NOLINT(*) + +/*! \brief The diagnostic level, controls the printing of the message. */ +enum class DiagnosticLevel : int { + kBug = 10, + kError = 20, + kWarning = 30, + kNote = 40, + kHelp = 50, +}; + +} // namespace tvm +#endif // TVM_RUNTIME_LOGGING_H_ diff --git a/include/tvm/runtime/object.h b/include/tvm/runtime/object.h index 47788394126e..048fc1d5af54 100644 --- a/include/tvm/runtime/object.h +++ b/include/tvm/runtime/object.h @@ -24,7 +24,7 @@ #define TVM_RUNTIME_OBJECT_H_ #include -#include +#include #include #include diff --git a/include/tvm/runtime/packed_func.h b/include/tvm/runtime/packed_func.h index 751a435c734a..7113863a6fb3 100644 --- a/include/tvm/runtime/packed_func.h +++ b/include/tvm/runtime/packed_func.h @@ -24,10 +24,10 @@ #ifndef TVM_RUNTIME_PACKED_FUNC_H_ #define TVM_RUNTIME_PACKED_FUNC_H_ -#include #include #include #include +#include #include #include #include @@ -1086,7 +1086,7 @@ struct PackedFuncValueConverter { Function(::tvm::runtime::TVMArgs(args, type_code, num_args), &rv); \ rv.MoveToCHost(out_value, out_type_code); \ return 0; \ - } catch (const ::std::runtime_error& _except_) { \ + } catch (const ::std::exception& _except_) { \ TVMAPISetLastError(_except_.what()); \ return -1; \ } \ @@ -1140,7 +1140,7 @@ struct PackedFuncValueConverter { f, ::tvm::runtime::TVMArgs(args, type_code, num_args), &rv); \ rv.MoveToCHost(out_value, out_type_code); \ return 0; \ - } catch (const ::std::runtime_error& _except_) { \ + } catch (const ::std::exception& _except_) { \ TVMAPISetLastError(_except_.what()); \ return -1; \ } \ diff --git a/include/tvm/runtime/vm/bytecode.h b/include/tvm/runtime/vm/bytecode.h index e858c4458054..72a557fa93b1 100644 --- a/include/tvm/runtime/vm/bytecode.h +++ b/include/tvm/runtime/vm/bytecode.h @@ -25,7 +25,7 @@ #define TVM_RUNTIME_VM_BYTECODE_H_ #include -#include +#include #include #include diff --git a/include/tvm/support/logging.h b/include/tvm/support/logging.h deleted file mode 100644 index ced1902a1bd1..000000000000 --- a/include/tvm/support/logging.h +++ /dev/null @@ -1,158 +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. - */ - -/*! - * \file tvm/support/logging.h - * \brief logging utilities on top of dmlc-core - */ -#ifndef TVM_SUPPORT_LOGGING_H_ -#define TVM_SUPPORT_LOGGING_H_ - -#include - -// a technique that enables overriding macro names on the number of parameters. This is used -// to define other macros below -#define GET_MACRO(_1, _2, _3, _4, _5, NAME, ...) NAME - -/*! - * \brief COND_X calls COND_X_N where N is the number of parameters passed to COND_X - * X can be any of CHECK_GE, CHECK_EQ, CHECK, or LOG (defined dmlc-core/include/dmlc/logging.h.) - * COND_X (but not COND_X_N) are supposed to be used outside this file. - * The first parameter of COND_X (and therefore, COND_X_N), which we call 'quit_on_assert', - * is a boolean. The rest of the parameters of COND_X is the same as the parameters of X. - * quit_on_assert determines the overall behaviour of COND_X. If it's true COND_X - * quits the program on assertion failure. If it's false, then it moves on and somehow reports - * the assertion failure back to the macro caller in an appropriate manner (e.g, 'return false' - * in a function, or 'continue' or 'break' in a loop) - * The default behavior when quit_on_assertion is false, is to 'return false'. If this is not - * desirable, the macro caller can pass one more last parameter to COND_X to tell COND_X what - * to do when when quit_on_assertion is false and the assertion fails. - * - * Rationale: These macros were designed to implement functions that have two behaviours - * in a concise way. Those behaviours are quitting on assertion failures, or trying to - * move on from assertion failures. Note that these macros hide lots of control flow in them, - * and therefore, makes the logic of the whole code slightly harder to understand. However, - * in pieces of code that use these macros frequently, it will significantly shorten the - * amount of code needed to be read, and we won't need to clutter the main logic of the - * function by repetitive control flow structure. The first problem - * mentioned will be improved over time as the developer gets used to the macro. - * - * Here is an example of how to use it - * \code - * bool f(..., bool quit_on_assertion) { - * int a = 0, b = 0; - * ... - * a = ... - * b = ... - * // if quit_on_assertion is true, if a==b, continue, otherwise quit. - * // if quit_on_assertion is false, if a==b, continue, otherwise 'return false' (default - * behaviour) COND_CHECK_EQ(quit_on_assertion, a, b) << "some error message when quiting" - * ... - * for (int i = 0; i < N; i++) { - * a = ... - * b = ... - * // if quit_on_assertion is true, if a==b, continue, otherwise quit. - * // if quit_on_assertion is false, if a==b, continue, otherwise 'break' (non-default - * // behaviour, therefore, has to be explicitly specified) - * COND_CHECK_EQ(quit_on_assertion, a, b, break) << "some error message when quiting" - * } - * } - * \endcode - */ -#define COND_CHECK_GE(...) \ - GET_MACRO(__VA_ARGS__, COND_CHECK_GE_5, COND_CHECK_GE_4, COND_CHECK_GE_3)(__VA_ARGS__) -#define COND_CHECK_EQ(...) \ - GET_MACRO(__VA_ARGS__, COND_CHECK_EQ_5, COND_CHECK_EQ_4, COND_CHECK_EQ_3)(__VA_ARGS__) -#define COND_CHECK(...) \ - GET_MACRO(__VA_ARGS__, COND_CHECK_5, COND_CHECK_4, COND_CHECK_3, COND_CHECK_2)(__VA_ARGS__) -#define COND_LOG(...) \ - GET_MACRO(__VA_ARGS__, COND_LOG_5, COND_LOG_4, COND_LOG_3, COND_LOG_2)(__VA_ARGS__) - -// Not supposed to be used by users directly. -#define COND_CHECK_OP(quit_on_assert, x, y, what, op) \ - if (!quit_on_assert) { \ - if (!((x)op(y))) what; \ - } else /* NOLINT(*) */ \ - CHECK_##op(x, y) - -#define COND_CHECK_EQ_4(quit_on_assert, x, y, what) COND_CHECK_OP(quit_on_assert, x, y, what, ==) -#define COND_CHECK_GE_4(quit_on_assert, x, y, what) COND_CHECK_OP(quit_on_assert, x, y, what, >=) - -#define COND_CHECK_3(quit_on_assert, x, what) \ - if (!quit_on_assert) { \ - if (!(x)) what; \ - } else /* NOLINT(*) */ \ - CHECK(x) - -#define COND_LOG_3(quit_on_assert, x, what) \ - if (!quit_on_assert) { \ - what; \ - } else /* NOLINT(*) */ \ - LOG(x) - -#define COND_CHECK_EQ_3(quit_on_assert, x, y) COND_CHECK_EQ_4(quit_on_assert, x, y, return false) -#define COND_CHECK_GE_3(quit_on_assert, x, y) COND_CHECK_GE_4(quit_on_assert, x, y, return false) -#define COND_CHECK_2(quit_on_assert, x) COND_CHECK_3(quit_on_assert, x, return false) -#define COND_LOG_2(quit_on_assert, x) COND_LOG_3(quit_on_assert, x, return false) - -namespace tvm { - -constexpr const char* kTVM_INTERNAL_ERROR_MESSAGE = - "\n---------------------------------------------------------------\n" - "An internal invariant was violated during the execution of TVM.\n" - "Please read TVM's error reporting guidelines.\n" - "More details can be found here: https://discuss.tvm.ai/t/error-reporting/7793.\n" - "---------------------------------------------------------------\n"; - -#define ICHECK_INDENT " " - -#define ICHECK_BINARY_OP(name, op, x, y) \ - if (dmlc::LogCheckError _check_err = dmlc::LogCheck##name(x, y)) \ - dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \ - << tvm::kTVM_INTERNAL_ERROR_MESSAGE << std::endl \ - << ICHECK_INDENT << "Check failed: " << #x " " #op " " #y << *(_check_err.str) << ": " - -#define ICHECK(x) \ - if (!(x)) \ - dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \ - << tvm::kTVM_INTERNAL_ERROR_MESSAGE << ICHECK_INDENT << "Check failed: " #x << " == false: " - -#define ICHECK_LT(x, y) ICHECK_BINARY_OP(_LT, <, x, y) -#define ICHECK_GT(x, y) ICHECK_BINARY_OP(_GT, >, x, y) -#define ICHECK_LE(x, y) ICHECK_BINARY_OP(_LE, <=, x, y) -#define ICHECK_GE(x, y) ICHECK_BINARY_OP(_GE, >=, x, y) -#define ICHECK_EQ(x, y) ICHECK_BINARY_OP(_EQ, ==, x, y) -#define ICHECK_NE(x, y) ICHECK_BINARY_OP(_NE, !=, x, y) -#define ICHECK_NOTNULL(x) \ - ((x) == nullptr ? dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \ - << tvm::kTVM_INTERNAL_ERROR_MESSAGE << ICHECK_INDENT \ - << "Check not null: " #x << ' ', \ - (x) : (x)) // NOLINT(*) - -/*! \brief The diagnostic level, controls the printing of the message. */ -enum class DiagnosticLevel : int { - kBug = 10, - kError = 20, - kWarning = 30, - kNote = 40, - kHelp = 50, -}; - -} // namespace tvm -#endif // TVM_SUPPORT_LOGGING_H_ diff --git a/include/tvm/support/with.h b/include/tvm/support/with.h index 90c82c4f3a06..d4547a304e8f 100644 --- a/include/tvm/support/with.h +++ b/include/tvm/support/with.h @@ -25,7 +25,7 @@ #ifndef TVM_SUPPORT_WITH_H_ #define TVM_SUPPORT_WITH_H_ -#include +#include #include diff --git a/licenses/LICENSE.libbacktrace.txt b/licenses/LICENSE.libbacktrace.txt new file mode 100644 index 000000000000..097d2774e5df --- /dev/null +++ b/licenses/LICENSE.libbacktrace.txt @@ -0,0 +1,29 @@ +# Copyright (C) 2012-2016 Free Software Foundation, Inc. + +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: + +# (1) Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. + +# (2) Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. + +# (3) The name of the author may not be used to +# endorse or promote products derived from this software without +# specific prior written permission. + +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, +# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. diff --git a/python/tvm/_ffi/base.py b/python/tvm/_ffi/base.py index 397090618ade..0496195fd73f 100644 --- a/python/tvm/_ffi/base.py +++ b/python/tvm/_ffi/base.py @@ -253,7 +253,9 @@ def c2pyerror(err_msg): message = [] for line in arr: if trace_mode: - if line.startswith(" "): + if line.startswith(" "): + stack_trace[-1] += "\n" + line + elif line.startswith(" "): stack_trace.append(line) else: trace_mode = False diff --git a/src/auto_scheduler/compute_dag.cc b/src/auto_scheduler/compute_dag.cc index 4e7fb05660a4..abbcba234848 100644 --- a/src/auto_scheduler/compute_dag.cc +++ b/src/auto_scheduler/compute_dag.cc @@ -1367,7 +1367,7 @@ Array ComputeDAG::InferBound(const Array& states) const { support::parallel_for(0, states.size(), [this, &states, &out_states](int i) { try { out_states.Set(i, (states[i].defined()) ? this->InferBound(states[i]) : states[i]); - } catch (dmlc::Error& e) { + } catch (Error& e) { LOG(WARNING) << "InferBound fails on the state:\n" << states[i] << "\n" << "with: " << e.what() << std::endl; diff --git a/src/auto_scheduler/feature.cc b/src/auto_scheduler/feature.cc index d93218c0208c..b3c62f01c7c8 100755 --- a/src/auto_scheduler/feature.cc +++ b/src/auto_scheduler/feature.cc @@ -1328,7 +1328,7 @@ void GetPerStoreFeaturesWorkerFunc(const SearchTask& task, const State& state, i const auto& prim_func = (*it).second.as(); GetPerStoreFeature(prim_func->body, task->hardware_params->cache_line_bytes, max_n_bufs, feature); - } catch (dmlc::Error& e) { + } catch (Error& e) { (*error_ct)++; } } diff --git a/src/auto_scheduler/search_policy/sketch_policy_rules.cc b/src/auto_scheduler/search_policy/sketch_policy_rules.cc index 110be6bd6f68..8eaf80321456 100644 --- a/src/auto_scheduler/search_policy/sketch_policy_rules.cc +++ b/src/auto_scheduler/search_policy/sketch_policy_rules.cc @@ -1106,7 +1106,7 @@ PopulationGenerationRule::ResultKind MutateComputeLocation::Apply(SketchPolicyNo } try { StepApplyToState(tmp_s->transform_steps.back(), &tmp_s, policy->search_task->compute_dag); - } catch (dmlc::Error& e) { + } catch (Error& e) { return ResultKind::kInvalid; } } @@ -1228,7 +1228,7 @@ PopulationGenerationRule::ResultKind MutateParallel::Apply(SketchPolicyNode* pol tmp_s.CopyOnWrite()->transform_steps.push_back(step); try { StepApplyToState(tmp_s->transform_steps.back(), &tmp_s, policy->search_task->compute_dag); - } catch (dmlc::Error& e) { + } catch (Error& e) { return ResultKind::kInvalid; } } diff --git a/src/auto_scheduler/transform_step.cc b/src/auto_scheduler/transform_step.cc old mode 100755 new mode 100644 index 5ba3eee07098..b67d5cdd7bd9 --- a/src/auto_scheduler/transform_step.cc +++ b/src/auto_scheduler/transform_step.cc @@ -26,8 +26,8 @@ #include #include #include +#include #include -#include #include #include diff --git a/src/ir/error.cc b/src/ir/error.cc index 5d3978dda4ff..0089f55a4da8 100644 --- a/src/ir/error.cc +++ b/src/ir/error.cc @@ -132,7 +132,8 @@ void ErrorReporter::RenderErrors(const IRModule& module, bool use_color) { LOG(FATAL) << annotated_prog.str() << std::endl; } -void ErrorReporter::ReportAt(const GlobalVar& global, const ObjectRef& node, const Error& err) { +void ErrorReporter::ReportAt(const GlobalVar& global, const ObjectRef& node, + const CompileError& err) { size_t index_to_insert = this->errors_.size(); this->errors_.push_back(err); auto it = this->node_to_error_.find(node); diff --git a/src/parser/parser.cc b/src/parser/parser.cc index 3061735eff7c..c7d8e025848a 100644 --- a/src/parser/parser.cc +++ b/src/parser/parser.cc @@ -28,9 +28,9 @@ #include #include #include +#include #include #include -#include #include @@ -172,8 +172,8 @@ class ScopeStack { void PopStack() { this->scope_stack.pop_back(); } }; -struct DuplicateKeyError : public dmlc::Error { - explicit DuplicateKeyError(const std::string& msg) : dmlc::Error(msg) {} +struct DuplicateKeyError : public Error { + explicit DuplicateKeyError(const std::string& msg) : Error(msg) {} }; /*! \brief A table of interning strings as global function and type names. */ @@ -1492,7 +1492,7 @@ class Parser { DLOG(INFO) << "op_name=" << op_name << " span=" << span; try { return Op::Get(op_name); - } catch (const dmlc::Error& e) { + } catch (const Error& e) { // we can relax this, but probably need to relax checks or return non-null here. this->diag_ctx.EmitFatal(Diagnostic::Error(span) << "operator `" << op_name diff --git a/src/parser/span_check.h b/src/parser/span_check.h index 9a887474fe67..ab71d30a54f5 100644 --- a/src/parser/span_check.h +++ b/src/parser/span_check.h @@ -30,8 +30,8 @@ #include #include #include +#include #include -#include #include #include diff --git a/src/relay/analysis/annotated_region_set.cc b/src/relay/analysis/annotated_region_set.cc index 04a18c4b7351..85a9c51a2fa8 100644 --- a/src/relay/analysis/annotated_region_set.cc +++ b/src/relay/analysis/annotated_region_set.cc @@ -157,8 +157,9 @@ class AnnotatedRegionSet::Creator : protected MixedModeVisitor { // Check if the argument already belongs to a region auto region = region_set_->GetRegion(call->args[0]); if (!region.defined()) { - throw Error(ErrorBuilder() << "Cannot find the corresponding region for end annotation:\n" - << AsText(GetRef(call), false)); + throw CompileError(ErrorBuilder() + << "Cannot find the corresponding region for end annotation:\n" + << AsText(GetRef(call), false)); } else { // If the argument is belonged to a region, it must have the same target. // Otherwise we should see a region_begin op. diff --git a/src/relay/analysis/kind_check.cc b/src/relay/analysis/kind_check.cc index c7c5a0a9f083..65b8516cb16c 100644 --- a/src/relay/analysis/kind_check.cc +++ b/src/relay/analysis/kind_check.cc @@ -139,7 +139,7 @@ struct KindChecker : TypeFunctor { << "Expected " << data->type_vars.size() << "arguments for " << tc << "; got " << op->args.size()); } - } catch (const dmlc::Error& err) { + } catch (const Error& err) { // TODO(@jroesch): can probably relax to just emit EmitFatal(Diagnostic::Error(op->span) << "the type variable : `" << var->name_hint << "` is undefined"); diff --git a/src/relay/analysis/type_solver.cc b/src/relay/analysis/type_solver.cc index cc1ada677c65..22e2e9a71040 100644 --- a/src/relay/analysis/type_solver.cc +++ b/src/relay/analysis/type_solver.cc @@ -617,10 +617,10 @@ bool TypeSolver::Solve() { } rnode->resolved = resolved; - } catch (const Error& err) { + } catch (const CompileError& err) { this->diag_ctx_.Emit(Diagnostic::Error(rnode->span) << err.what()); rnode->resolved = false; - } catch (const dmlc::Error& e) { + } catch (const Error& e) { ICHECK(false) << e.what(); } diff --git a/src/relay/analysis/well_formed.cc b/src/relay/analysis/well_formed.cc index 856c5dc7aac1..acc1a9adc9f4 100644 --- a/src/relay/analysis/well_formed.cc +++ b/src/relay/analysis/well_formed.cc @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include diff --git a/src/relay/backend/vm/compiler.cc b/src/relay/backend/vm/compiler.cc index 251a55f10b72..9d3ffc558aae 100644 --- a/src/relay/backend/vm/compiler.cc +++ b/src/relay/backend/vm/compiler.cc @@ -33,8 +33,8 @@ #include #include #include +#include #include -#include #include #include diff --git a/src/relay/backend/vm/compiler.h b/src/relay/backend/vm/compiler.h index 615a8181b387..9c813a4f561c 100644 --- a/src/relay/backend/vm/compiler.h +++ b/src/relay/backend/vm/compiler.h @@ -29,8 +29,8 @@ #include #include #include +#include #include -#include #include #include diff --git a/src/relay/backend/vm/inline_primitives.cc b/src/relay/backend/vm/inline_primitives.cc index eb848eb7a828..05fb2a120620 100644 --- a/src/relay/backend/vm/inline_primitives.cc +++ b/src/relay/backend/vm/inline_primitives.cc @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/relay/backend/vm/lambda_lift.cc b/src/relay/backend/vm/lambda_lift.cc index cc530a10188e..c768a2c300ec 100644 --- a/src/relay/backend/vm/lambda_lift.cc +++ b/src/relay/backend/vm/lambda_lift.cc @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/relay/backend/vm/removed_unused_funcs.cc b/src/relay/backend/vm/removed_unused_funcs.cc index cdf898fca756..5e9b1b7978f9 100644 --- a/src/relay/backend/vm/removed_unused_funcs.cc +++ b/src/relay/backend/vm/removed_unused_funcs.cc @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/relay/op/nn/convolution.h b/src/relay/op/nn/convolution.h index 2a49a2e251f8..379fa3fa71d3 100644 --- a/src/relay/op/nn/convolution.h +++ b/src/relay/op/nn/convolution.h @@ -25,7 +25,7 @@ #define TVM_RELAY_OP_NN_CONVOLUTION_H_ #include -#include +#include #include #include diff --git a/src/relay/op/tensor/transform.cc b/src/relay/op/tensor/transform.cc index e3929bf8b77e..557e679088a3 100644 --- a/src/relay/op/tensor/transform.cc +++ b/src/relay/op/tensor/transform.cc @@ -312,7 +312,7 @@ bool StackRel(const Array& types, int num_inputs, const Attrs& attrs, if (first->shape[j].as() || e->shape[j].as() || reporter->AssertEQ(first->shape[j], e->shape[j])) continue; - throw Error( + throw CompileError( "relay.stack requires all tensors have the same shape " "on non-stacking axes"); } @@ -1691,8 +1691,8 @@ bool MeshgridRel(const Array& types, int num_inputs, const Attrs& raw_attr const MeshgridAttrs* attrs = raw_attrs.as(); const auto* tensor_tuple = types[0].as(); if (tensor_tuple == nullptr) { - throw Error( - ErrorBuilder() << "meshgrid requires a tuple of tensors as the first argument, found " + throw CompileError(ErrorBuilder() + << "meshgrid requires a tuple of tensors as the first argument, found " << PrettyPrint(types[0])); } else if (types[0].as() != nullptr) { return false; @@ -1714,14 +1714,14 @@ bool MeshgridRel(const Array& types, int num_inputs, const Attrs& raw_attr int e_ndim = static_cast(e->shape.size()); const DataType& e_dtype = e->dtype; if (e_dtype != dtype) { - throw Error("relay.meshgrid requires all tensors have the same dtype"); + throw CompileError("relay.meshgrid requires all tensors have the same dtype"); } if (e_ndim == 0) { grid_shape.emplace_back(1); } else if (e_ndim == 1) { grid_shape.emplace_back(e->shape[0]); } else { - throw Error("relay.meshgrid requires all tensors be either scalars or 1-D vectors."); + throw CompileError("relay.meshgrid requires all tensors be either scalars or 1-D vectors."); } } diff --git a/src/relay/op/tensor/transform.h b/src/relay/op/tensor/transform.h index dbf8537e0dad..3c670bcaaa51 100644 --- a/src/relay/op/tensor/transform.h +++ b/src/relay/op/tensor/transform.h @@ -78,8 +78,8 @@ bool ConcatenateRel(const Array& types, int num_inputs, const Attrs& attrs // Sanity check: axis int axis = param->axis; if (!(-ndim <= axis && axis < ndim)) { - throw Error(ErrorBuilder() << "concatenate only accepts `axis` in [-ndim, ndim)" - << ", but got axis = " << axis << ", and ndim = " << ndim); + throw CompileError(ErrorBuilder() << "concatenate only accepts `axis` in [-ndim, ndim)" + << ", but got axis = " << axis << ", and ndim = " << ndim); } axis = axis < 0 ? ndim + axis : axis; diff --git a/src/relay/op/type_relations.cc b/src/relay/op/type_relations.cc index 7b30aea2eb57..6e30ad9624c4 100644 --- a/src/relay/op/type_relations.cc +++ b/src/relay/op/type_relations.cc @@ -85,7 +85,7 @@ TensorType ConcreteBroadcast(const TensorType& t1, const TensorType& t2, DataTyp } else if (EqualCheck(s1, s2)) { oshape.push_back(s1); } else { - throw Error(ErrorBuilder() << "Incompatible broadcast type " << t1 << " and " << t2); + throw CompileError(ErrorBuilder() << "Incompatible broadcast type " << t1 << " and " << t2); } } diff --git a/src/relay/qnn/op/concatenate.cc b/src/relay/qnn/op/concatenate.cc index 59a519d66436..eb0f83836a54 100644 --- a/src/relay/qnn/op/concatenate.cc +++ b/src/relay/qnn/op/concatenate.cc @@ -51,9 +51,10 @@ bool QnnConcatenateRel(const Array& types, int num_inputs, const Attrs& at if (types[1].as()) { return false; } else { - throw Error(ErrorBuilder() - << "qnn concatenate requires a tuple of scales as the second argument, found " - << PrettyPrint(types[1])); + throw CompileError( + ErrorBuilder() + << "qnn concatenate requires a tuple of scales as the second argument, found " + << PrettyPrint(types[1])); } } for (const auto& input_scale : input_scales_tuple->fields) { @@ -68,9 +69,10 @@ bool QnnConcatenateRel(const Array& types, int num_inputs, const Attrs& at if (types[2].as()) { return false; } else { - throw Error(ErrorBuilder() - << "qnn concatenate requires a tuple of zero_points as the third argument, found " - << PrettyPrint(types[2])); + throw CompileError( + ErrorBuilder() + << "qnn concatenate requires a tuple of zero_points as the third argument, found " + << PrettyPrint(types[2])); } } for (const auto& input_zero_point : input_zero_points_tuple->fields) { diff --git a/src/relay/transforms/inline.cc b/src/relay/transforms/inline.cc index dae34674de77..6e6505b28dc6 100644 --- a/src/relay/transforms/inline.cc +++ b/src/relay/transforms/inline.cc @@ -36,7 +36,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/relay/transforms/partial_eval.cc b/src/relay/transforms/partial_eval.cc index fa080a7ff22c..3a87aa8ed498 100644 --- a/src/relay/transforms/partial_eval.cc +++ b/src/relay/transforms/partial_eval.cc @@ -861,8 +861,8 @@ class PartialEvaluator : public ExprFunctor return VisitFunc(GetRef(op), ll); } - struct ReflectError : dmlc::Error { - ReflectError() : dmlc::Error("static value not found") {} + struct ReflectError : Error { + ReflectError() : Error("static value not found") {} }; Expr Reflect(const PStatic& st) { diff --git a/src/relay/transforms/simplify_expr.cc b/src/relay/transforms/simplify_expr.cc index 3c8876ceccb5..b4f4cc16e9df 100644 --- a/src/relay/transforms/simplify_expr.cc +++ b/src/relay/transforms/simplify_expr.cc @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include "../op/tensor/transform.h" #include "pattern_utils.h" diff --git a/src/relay/transforms/to_a_normal_form.cc b/src/relay/transforms/to_a_normal_form.cc index 05844477cc5b..91e8d90c1232 100644 --- a/src/relay/transforms/to_a_normal_form.cc +++ b/src/relay/transforms/to_a_normal_form.cc @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include "../../support/arena.h" #include "../analysis/dependency_graph.h" diff --git a/src/relay/transforms/to_basic_block_normal_form.cc b/src/relay/transforms/to_basic_block_normal_form.cc index 1aab367cf22a..79157bba1918 100644 --- a/src/relay/transforms/to_basic_block_normal_form.cc +++ b/src/relay/transforms/to_basic_block_normal_form.cc @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include "../../support/arena.h" #include "../analysis/dependency_graph.h" diff --git a/src/relay/transforms/type_infer.cc b/src/relay/transforms/type_infer.cc index b4ccd1659865..4c6013792426 100644 --- a/src/relay/transforms/type_infer.cc +++ b/src/relay/transforms/type_infer.cc @@ -166,7 +166,7 @@ class TypeInferencer : private ExprFunctor, bool assign_rhs = true) { try { return solver_.Unify(t1, t2, span, assign_lhs, assign_rhs); - } catch (const dmlc::Error& e) { + } catch (const Error& e) { this->EmitFatal(Diagnostic::Error(span) << "Error unifying `" << t1 << "` and `" << t2 << "`: " << e.what()); return Type(); diff --git a/src/runtime/c_runtime_api.cc b/src/runtime/c_runtime_api.cc index 7fd27cba6136..ea18208dcfab 100644 --- a/src/runtime/c_runtime_api.cc +++ b/src/runtime/c_runtime_api.cc @@ -384,7 +384,7 @@ typedef dmlc::ThreadLocalStore TVMAPIRuntimeStore; const char* TVMGetLastError() { return TVMAPIRuntimeStore::Get()->last_error.c_str(); } -int TVMAPIHandleException(const std::runtime_error& e) { +int TVMAPIHandleException(const std::exception& e) { TVMAPISetLastError(NormalizeError(e.what()).c_str()); return -1; } @@ -518,7 +518,8 @@ int TVMFuncCreateFromCFunc(TVMPackedCFunc func, void* resource_handle, TVMPacked int ret = func(const_cast(args.values), const_cast(args.type_codes), args.num_args, rv, resource_handle); if (ret != 0) { - throw dmlc::Error(TVMGetLastError() + ::dmlc::StackTrace()); + throw tvm::Error(TVMGetLastError() + + tvm::Backtrace()); // TODO(tkonolige): this may double backtrace } }); } else { @@ -529,7 +530,7 @@ int TVMFuncCreateFromCFunc(TVMPackedCFunc func, void* resource_handle, TVMPacked int ret = func(const_cast(args.values), const_cast(args.type_codes), args.num_args, rv, rpack.get()); if (ret != 0) { - throw dmlc::Error(TVMGetLastError() + ::dmlc::StackTrace()); + throw tvm::Error(TVMGetLastError() + tvm::Backtrace()); } }); } diff --git a/src/runtime/contrib/cblas/cblas.cc b/src/runtime/contrib/cblas/cblas.cc index 16496e06aae3..fbac6222488d 100644 --- a/src/runtime/contrib/cblas/cblas.cc +++ b/src/runtime/contrib/cblas/cblas.cc @@ -21,8 +21,8 @@ * \file Use external cblas library call. */ #include +#include #include -#include extern "C" { #include diff --git a/src/runtime/contrib/cblas/mkl.cc b/src/runtime/contrib/cblas/mkl.cc index 273aa45367dd..4323878db276 100644 --- a/src/runtime/contrib/cblas/mkl.cc +++ b/src/runtime/contrib/cblas/mkl.cc @@ -21,8 +21,8 @@ * \file Use external mkl library call. */ #include +#include #include -#include extern "C" { #include diff --git a/src/runtime/contrib/cblas/mkldnn.cc b/src/runtime/contrib/cblas/mkldnn.cc index 1c3fa023dcc7..31abd317c6a4 100644 --- a/src/runtime/contrib/cblas/mkldnn.cc +++ b/src/runtime/contrib/cblas/mkldnn.cc @@ -21,8 +21,8 @@ * \file Use external cblas library call. */ #include +#include #include -#include extern "C" { #include diff --git a/src/runtime/contrib/cublas/cublas.cc b/src/runtime/contrib/cublas/cublas.cc index b12992f57159..9af1602cf3c0 100644 --- a/src/runtime/contrib/cublas/cublas.cc +++ b/src/runtime/contrib/cublas/cublas.cc @@ -21,8 +21,8 @@ * \file Use external cblas library call. */ #include +#include #include -#include #include "../cblas/gemm_common.h" #include "cublas_utils.h" diff --git a/src/runtime/contrib/cublas/cublas_utils.h b/src/runtime/contrib/cublas/cublas_utils.h index 32c3b03ddbb0..3edb8300be88 100644 --- a/src/runtime/contrib/cublas/cublas_utils.h +++ b/src/runtime/contrib/cublas/cublas_utils.h @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #if CUDART_VERSION >= 10010 diff --git a/src/runtime/contrib/cudnn/cudnn_utils.h b/src/runtime/contrib/cudnn/cudnn_utils.h index 528298b75187..9b8e9fb33f98 100644 --- a/src/runtime/contrib/cudnn/cudnn_utils.h +++ b/src/runtime/contrib/cudnn/cudnn_utils.h @@ -26,7 +26,7 @@ #include #include -#include +#include #include "../../cuda/cuda_common.h" diff --git a/src/runtime/contrib/miopen/miopen_utils.h b/src/runtime/contrib/miopen/miopen_utils.h index 9982f0914f6b..e5a769a974f0 100644 --- a/src/runtime/contrib/miopen/miopen_utils.h +++ b/src/runtime/contrib/miopen/miopen_utils.h @@ -26,7 +26,7 @@ #include #include -#include +#include #include diff --git a/src/runtime/contrib/mps/mps_utils.h b/src/runtime/contrib/mps/mps_utils.h index d1c49732318a..c2b7e3c7aa99 100644 --- a/src/runtime/contrib/mps/mps_utils.h +++ b/src/runtime/contrib/mps/mps_utils.h @@ -28,8 +28,8 @@ #include #include #include +#include #include -#include #include diff --git a/src/runtime/contrib/nnpack/convolution.cc b/src/runtime/contrib/nnpack/convolution.cc index b3ea6c891d43..0d6359495902 100644 --- a/src/runtime/contrib/nnpack/convolution.cc +++ b/src/runtime/contrib/nnpack/convolution.cc @@ -23,8 +23,8 @@ #include #include #include +#include #include -#include #include "nnpack_utils.h" diff --git a/src/runtime/contrib/nnpack/fully_connected.cc b/src/runtime/contrib/nnpack/fully_connected.cc index 8b72eb38e08c..28570026ada3 100644 --- a/src/runtime/contrib/nnpack/fully_connected.cc +++ b/src/runtime/contrib/nnpack/fully_connected.cc @@ -22,8 +22,8 @@ */ #include #include +#include #include -#include #include "nnpack_utils.h" diff --git a/src/runtime/contrib/nnpack/nnpack_utils.h b/src/runtime/contrib/nnpack/nnpack_utils.h index 231309baaa8e..4396ea0bcde6 100644 --- a/src/runtime/contrib/nnpack/nnpack_utils.h +++ b/src/runtime/contrib/nnpack/nnpack_utils.h @@ -25,8 +25,8 @@ #include #include #include +#include #include -#include namespace tvm { namespace contrib { diff --git a/src/runtime/contrib/random/mt_random_engine.cc b/src/runtime/contrib/random/mt_random_engine.cc index 49bc056dcafb..699f6bbcf376 100644 --- a/src/runtime/contrib/random/mt_random_engine.cc +++ b/src/runtime/contrib/random/mt_random_engine.cc @@ -22,8 +22,8 @@ * \brief mt19937 random engine */ #include +#include #include -#include #include #include diff --git a/src/runtime/contrib/random/random.cc b/src/runtime/contrib/random/random.cc index edcd20883369..2d111bc322ab 100644 --- a/src/runtime/contrib/random/random.cc +++ b/src/runtime/contrib/random/random.cc @@ -22,8 +22,8 @@ */ #include #include +#include #include -#include #include diff --git a/src/runtime/contrib/rocblas/rocblas.cc b/src/runtime/contrib/rocblas/rocblas.cc index dca1ebc6ed83..d977b1a211b0 100644 --- a/src/runtime/contrib/rocblas/rocblas.cc +++ b/src/runtime/contrib/rocblas/rocblas.cc @@ -23,8 +23,8 @@ #include "rocblas.h" #include +#include #include -#include namespace tvm { namespace contrib { diff --git a/src/runtime/contrib/tensorrt/tensorrt_logger.h b/src/runtime/contrib/tensorrt/tensorrt_logger.h index 087cb010189c..eb0164210dbb 100644 --- a/src/runtime/contrib/tensorrt/tensorrt_logger.h +++ b/src/runtime/contrib/tensorrt/tensorrt_logger.h @@ -25,7 +25,7 @@ #ifndef TVM_RUNTIME_CONTRIB_TENSORRT_TENSORRT_LOGGER_H_ #define TVM_RUNTIME_CONTRIB_TENSORRT_TENSORRT_LOGGER_H_ -#include +#include #include "NvInfer.h" #include "tensorrt_utils.h" diff --git a/src/runtime/cpu_device_api.cc b/src/runtime/cpu_device_api.cc index b745be33b456..133bb01d7d13 100644 --- a/src/runtime/cpu_device_api.cc +++ b/src/runtime/cpu_device_api.cc @@ -22,8 +22,8 @@ */ #include #include +#include #include -#include #include #include diff --git a/src/runtime/file_utils.cc b/src/runtime/file_utils.cc index 92c398b559d2..32dd1d8020c9 100644 --- a/src/runtime/file_utils.cc +++ b/src/runtime/file_utils.cc @@ -24,9 +24,9 @@ #include #include +#include #include #include -#include #include #include diff --git a/src/runtime/graph/graph_runtime.cc b/src/runtime/graph/graph_runtime.cc index 6c51e711aef1..7e98acb6fb3e 100644 --- a/src/runtime/graph/graph_runtime.cc +++ b/src/runtime/graph/graph_runtime.cc @@ -491,7 +491,7 @@ PackedFunc GraphRuntime::GetFunction(const std::string& name, } else if (name == "share_params") { return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { const auto& module = args[0].operator Module(); - ICHECK_EQ(module.operator->()->type_key(), "GraphRuntime"); + ICHECK_EQ(module.operator->()->type_key(), std::string("GraphRuntime")); const auto& param_blob = args[1].operator std::string(); dmlc::MemoryStringStream strm(const_cast(¶m_blob)); this->ShareParams(dynamic_cast(*module.operator->()), &strm); diff --git a/src/runtime/hexagon/hexagon_device_api.cc b/src/runtime/hexagon/hexagon_device_api.cc index 70cebf5afa44..a01c9def5d5d 100644 --- a/src/runtime/hexagon/hexagon_device_api.cc +++ b/src/runtime/hexagon/hexagon_device_api.cc @@ -18,8 +18,8 @@ */ #include +#include #include -#include #include #include diff --git a/src/runtime/hexagon/hexagon_module.cc b/src/runtime/hexagon/hexagon_module.cc index 994e24b99084..f6a57ff55355 100644 --- a/src/runtime/hexagon/hexagon_module.cc +++ b/src/runtime/hexagon/hexagon_module.cc @@ -22,8 +22,8 @@ #ifdef __ANDROID__ #include #endif +#include #include -#include #include #include diff --git a/src/runtime/hexagon/hexagon_module.h b/src/runtime/hexagon/hexagon_module.h index e558997b7a4c..02ed7d2541c2 100644 --- a/src/runtime/hexagon/hexagon_module.h +++ b/src/runtime/hexagon/hexagon_module.h @@ -20,8 +20,8 @@ #ifndef TVM_RUNTIME_HEXAGON_HEXAGON_MODULE_H_ #define TVM_RUNTIME_HEXAGON_HEXAGON_MODULE_H_ +#include #include -#include #include #include diff --git a/src/runtime/hexagon/sim/hexagon_device_sim.cc b/src/runtime/hexagon/sim/hexagon_device_sim.cc index 6cc7dcf3209f..5d18cdab5d00 100644 --- a/src/runtime/hexagon/sim/hexagon_device_sim.cc +++ b/src/runtime/hexagon/sim/hexagon_device_sim.cc @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include #include @@ -110,7 +110,7 @@ struct non_const_str { ICHECK_EQ(pointers_.size(), 1); return pointers_[0]; } - operator char* *() { return pointers_.data(); } + operator char**() { return pointers_.data(); } private: std::vector pointers_; diff --git a/src/runtime/hexagon/target/hexagon_dsprpcapi.cc b/src/runtime/hexagon/target/hexagon_dsprpcapi.cc index d494db82e2c7..a089684c4188 100644 --- a/src/runtime/hexagon/target/hexagon_dsprpcapi.cc +++ b/src/runtime/hexagon/target/hexagon_dsprpcapi.cc @@ -22,7 +22,7 @@ #include #include -#include +#include #include "hexagon_target_log.h" diff --git a/src/runtime/hexagon/target/hexagon_dsprpcapi.h b/src/runtime/hexagon/target/hexagon_dsprpcapi.h index c0e40805ecbf..e4711e3da584 100644 --- a/src/runtime/hexagon/target/hexagon_dsprpcapi.h +++ b/src/runtime/hexagon/target/hexagon_dsprpcapi.h @@ -22,7 +22,7 @@ #ifdef __ANDROID__ #include -#include +#include #include "remote.h" #include "remote64.h" diff --git a/src/runtime/hexagon/target/hexagon_stubapi.cc b/src/runtime/hexagon/target/hexagon_stubapi.cc index 5428ae7c1cff..1fb7d942e968 100644 --- a/src/runtime/hexagon/target/hexagon_stubapi.cc +++ b/src/runtime/hexagon/target/hexagon_stubapi.cc @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include "hexagon_target_log.h" diff --git a/src/runtime/hexagon/target/hexagon_stubapi.h b/src/runtime/hexagon/target/hexagon_stubapi.h index cc5b7b7413ca..fba22b10247c 100644 --- a/src/runtime/hexagon/target/hexagon_stubapi.h +++ b/src/runtime/hexagon/target/hexagon_stubapi.h @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include diff --git a/src/runtime/logging.cc b/src/runtime/logging.cc new file mode 100644 index 000000000000..317c3b2b316a --- /dev/null +++ b/src/runtime/logging.cc @@ -0,0 +1,124 @@ +/* + * 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. + */ + +#ifndef TVM_BACKTRACE_DISABLED + +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace tvm { +namespace { + +struct BacktraceInfo { + std::vector lines; + size_t max_size; + std::string error_message; +}; + +static backtrace_state* _backtrace_state = nullptr; + +std::string DemangleName(const char* name) { + int status = 0; + size_t length = std::string::npos; + std::unique_ptr demangled_name = { + abi::__cxa_demangle(name, 0, &length, &status), &std::free}; + if (demangled_name && status == 0 && length > 0) { + return demangled_name.get(); + } else { + return name; + } +} + +void BacktraceErrorCallback(void* data, const char* msg, int errnum) { + auto stack_trace = reinterpret_cast(data); + stack_trace->error_message = msg; +} + +void BacktraceSyminfoCallback(void* data, uintptr_t pc, const char* symname, uintptr_t symval, + uintptr_t symsize) { + auto str = reinterpret_cast(data); + + if (symname != nullptr) { + *str = DemangleName(symname); + } else { + std::ostringstream s; + s << "0x" << std::setfill('0') << std::setw(sizeof(uintptr_t) * 2) << std::hex << pc; + *str = s.str(); + } +} +int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int lineno, + const char* symbol) { + auto stack_trace = reinterpret_cast(data); + std::stringstream s; + + std::string symbol_str = ""; + if (symbol != nullptr) { + symbol_str = DemangleName(symbol); + } else { + // see if syminfo gives anything + backtrace_syminfo(_backtrace_state, pc, BacktraceSyminfoCallback, BacktraceErrorCallback, + &symbol_str); + } + s << symbol_str; + + if (filename != nullptr) { + s << std::endl << " at " << filename; + if (lineno != 0) { + s << ":" << lineno; + } + } + // Skip tvm::backtrace and tvm::LogFatal::~LogFatal at the beginning of the trace as they don't + // add anything useful to the backtrace. + if (!(stack_trace->lines.size() == 0 && + (symbol_str.find("tvm::Backtrace", 0) == 0 || symbol_str.find("tvm::LogFatal", 0) == 0))) { + stack_trace->lines.push_back(s.str()); + } + // TVMFuncCall denotes the API boundary so we stop there. Exceptions should be caught there. + if (symbol_str == "TVMFuncCall" || stack_trace->lines.size() >= stack_trace->max_size) { + return 1; + } + return 0; +} +} // namespace + +std::string Backtrace() { + BacktraceInfo bt; + bt.max_size = 100; + if (_backtrace_state == nullptr) { + _backtrace_state = backtrace_create_state(NULL, 1, BacktraceErrorCallback, &bt); + } + backtrace_full(_backtrace_state, 0, BacktraceFullCallback, BacktraceErrorCallback, &bt); + + std::ostringstream s; + s << "Stack trace:\n"; + for (size_t i = 0; i < bt.lines.size(); i++) { + s << " " << i << ": " << bt.lines[i] << "\n"; + } + + return s.str(); +} +} // namespace tvm +#endif diff --git a/src/runtime/metal/metal_common.h b/src/runtime/metal/metal_common.h index bd07dbfde9d0..b5d06192396b 100644 --- a/src/runtime/metal/metal_common.h +++ b/src/runtime/metal/metal_common.h @@ -32,8 +32,8 @@ #import #include #include +#include #include -#include #include #include diff --git a/src/runtime/micro/micro_session.cc b/src/runtime/micro/micro_session.cc index 6c0d0c4c40fe..cd916d46971d 100644 --- a/src/runtime/micro/micro_session.cc +++ b/src/runtime/micro/micro_session.cc @@ -25,8 +25,8 @@ #include #include +#include #include -#include #include #include diff --git a/src/runtime/minrpc/minrpc_server.h b/src/runtime/minrpc/minrpc_server.h index d5c61eccfd6d..3b9772f2fb60 100644 --- a/src/runtime/minrpc/minrpc_server.h +++ b/src/runtime/minrpc/minrpc_server.h @@ -46,7 +46,7 @@ #endif #if TVM_MINRPC_ENABLE_LOGGING -#include +#include #endif namespace tvm { diff --git a/src/runtime/ndarray.cc b/src/runtime/ndarray.cc index d3ddbf8c0229..d46f0868a2ea 100644 --- a/src/runtime/ndarray.cc +++ b/src/runtime/ndarray.cc @@ -23,9 +23,9 @@ */ #include #include +#include #include #include -#include #include "runtime_base.h" diff --git a/src/runtime/object.cc b/src/runtime/object.cc index ad68c70698ea..c9a9669671e6 100644 --- a/src/runtime/object.cc +++ b/src/runtime/object.cc @@ -20,9 +20,9 @@ * \file src/runtime/object.cc * \brief Object type management system. */ +#include #include #include -#include #include #include diff --git a/src/runtime/opencl/opencl_common.h b/src/runtime/opencl/opencl_common.h index 2e7f05f91020..3fca368c758b 100644 --- a/src/runtime/opencl/opencl_common.h +++ b/src/runtime/opencl/opencl_common.h @@ -26,8 +26,8 @@ #include #include +#include #include -#include /* There are many OpenCL platforms that do not yet support OpenCL 2.0, * hence we use 1.2 APIs, some of which are now deprecated. In order diff --git a/src/runtime/registry.cc b/src/runtime/registry.cc index a65235090bfd..bb5a794a030b 100644 --- a/src/runtime/registry.cc +++ b/src/runtime/registry.cc @@ -22,8 +22,8 @@ * \brief The global registry of packed function. */ #include +#include #include -#include #include #include diff --git a/src/runtime/rocm/rocm_device_api.cc b/src/runtime/rocm/rocm_device_api.cc index 5f24ce0eec48..5a68c316bcf4 100644 --- a/src/runtime/rocm/rocm_device_api.cc +++ b/src/runtime/rocm/rocm_device_api.cc @@ -26,8 +26,8 @@ #include #include #include +#include #include -#include #include "rocm_common.h" diff --git a/src/runtime/rpc/rpc_device_api.cc b/src/runtime/rpc/rpc_device_api.cc index 06737f99a4de..cdeeb368f5a2 100644 --- a/src/runtime/rpc/rpc_device_api.cc +++ b/src/runtime/rpc/rpc_device_api.cc @@ -21,8 +21,8 @@ * \file rpc_device_api.cc */ #include +#include #include -#include #include @@ -72,7 +72,7 @@ class RPCDeviceAPI final : public DeviceAPI { auto remote_ctx = RemoveRPCSessionMask(ctx); try { GetSess(ctx)->GetDeviceAPI(remote_ctx)->FreeDataSpace(remote_ctx, space->data); - } catch (const dmlc::Error& e) { + } catch (const Error& e) { // fault tolerance to remote close. } delete space; diff --git a/src/runtime/rpc/rpc_endpoint.cc b/src/runtime/rpc/rpc_endpoint.cc index 8716355fd68f..5e2bba88921e 100644 --- a/src/runtime/rpc/rpc_endpoint.cc +++ b/src/runtime/rpc/rpc_endpoint.cc @@ -526,7 +526,7 @@ class RPCEndpoint::EventHandler : public dmlc::Stream { try { fconstructor->CallPacked(constructor_args, &con_ret); - } catch (const dmlc::Error& e) { + } catch (const Error& e) { LOG(FATAL) << "Server[" << name_ << "]:" << " Error caught from session constructor " << constructor_name << ":\n" << e.what(); @@ -540,7 +540,7 @@ class RPCEndpoint::EventHandler : public dmlc::Stream { ICHECK_EQ(tkey, "rpc") << "Constructor " << constructor_name << " to return an RPCModule"; serving_session_ = RPCModuleGetSession(mod); this->ReturnVoid(); - } catch (const std::runtime_error& e) { + } catch (const std::exception& e) { this->ReturnException(e.what()); } @@ -562,7 +562,7 @@ class RPCEndpoint::EventHandler : public dmlc::Stream { } this->SwitchToState(kRecvPacketNumBytes); }); - } catch (const std::runtime_error& e) { + } catch (const std::exception& e) { this->ReturnException(e.what()); this->SwitchToState(kRecvPacketNumBytes); } @@ -581,7 +581,7 @@ class RPCEndpoint::EventHandler : public dmlc::Stream { setter(0, rv); this->ReturnPackedSeq(TVMArgs(&ret_value, &ret_tcode, 1)); - } catch (const std::runtime_error& e) { + } catch (const std::exception& e) { this->ReturnException(e.what()); } this->SwitchToState(kRecvPacketNumBytes); @@ -719,7 +719,7 @@ void RPCEndpoint::Shutdown() { writer_.bytes_available()); if (n == 0) break; } - } catch (const dmlc::Error& e) { + } catch (const Error& e) { } channel_.reset(nullptr); } diff --git a/src/runtime/rpc/rpc_module.cc b/src/runtime/rpc/rpc_module.cc index 34691415c1a4..46e1be794520 100644 --- a/src/runtime/rpc/rpc_module.cc +++ b/src/runtime/rpc/rpc_module.cc @@ -130,7 +130,7 @@ class RPCWrappedFunc : public Object { ~RPCWrappedFunc() { try { sess_->FreeHandle(handle_, kTVMPackedFuncHandle); - } catch (const dmlc::Error& e) { + } catch (const Error& e) { // fault tolerance to remote close } } @@ -165,7 +165,7 @@ class RPCModuleNode final : public ModuleNode { if (module_handle_ != nullptr) { try { sess_->FreeHandle(module_handle_, kTVMModuleHandle); - } catch (const dmlc::Error& e) { + } catch (const Error& e) { // fault tolerance to remote close } module_handle_ = nullptr; diff --git a/src/runtime/rpc/rpc_session.cc b/src/runtime/rpc/rpc_session.cc index 0ac5b8dc74ef..2b75018099d5 100644 --- a/src/runtime/rpc/rpc_session.cc +++ b/src/runtime/rpc/rpc_session.cc @@ -46,7 +46,7 @@ void RPCSession::AsyncCallFunc(PackedFuncHandle func, const TVMValue* arg_values try { this->CallFunc(func, arg_values, arg_type_codes, num_args, [&callback](TVMArgs args) { callback(RPCCode::kReturn, args); }); - } catch (const std::runtime_error& e) { + } catch (const std::exception& e) { this->SendException(callback, e.what()); } } @@ -60,7 +60,7 @@ void RPCSession::AsyncCopyToRemote(void* local_from_bytes, DLTensor* remote_to, try { this->CopyToRemote(local_from_bytes, remote_to, nbytes); callback(RPCCode::kReturn, TVMArgs(&value, &tcode, 1)); - } catch (const std::runtime_error& e) { + } catch (const std::exception& e) { this->SendException(callback, e.what()); } } @@ -74,7 +74,7 @@ void RPCSession::AsyncCopyFromRemote(DLTensor* remote_from, void* local_to_bytes try { this->CopyFromRemote(remote_from, local_to_bytes, nbytes); callback(RPCCode::kReturn, TVMArgs(&value, &tcode, 1)); - } catch (const std::runtime_error& e) { + } catch (const std::exception& e) { this->SendException(callback, e.what()); } } @@ -88,7 +88,7 @@ void RPCSession::AsyncStreamWait(TVMContext ctx, TVMStreamHandle stream, try { this->GetDeviceAPI(ctx)->StreamSync(ctx, stream); callback(RPCCode::kReturn, TVMArgs(&value, &tcode, 1)); - } catch (const std::runtime_error& e) { + } catch (const std::exception& e) { this->SendException(callback, e.what()); } } diff --git a/src/runtime/runtime_base.h b/src/runtime/runtime_base.h index 21601df1ad39..7abb32935a2b 100644 --- a/src/runtime/runtime_base.h +++ b/src/runtime/runtime_base.h @@ -34,7 +34,7 @@ and finishes with API_END() or API_END_HANDLE_ERROR */ #define API_END() \ } \ - catch (std::runtime_error & _except_) { \ + catch (std::exception & _except_) { \ return TVMAPIHandleException(_except_); \ } \ return 0; // NOLINT(*) @@ -45,7 +45,7 @@ */ #define API_END_HANDLE_ERROR(Finalize) \ } \ - catch (std::runtime_error & _except_) { \ + catch (std::exception & _except_) { \ Finalize; \ return TVMAPIHandleException(_except_); \ } \ @@ -56,6 +56,6 @@ * \param e the exception * \return the return value of API after exception is handled */ -int TVMAPIHandleException(const std::runtime_error& e); +int TVMAPIHandleException(const std::exception& e); #endif // TVM_RUNTIME_RUNTIME_BASE_H_ diff --git a/src/runtime/thread_pool.cc b/src/runtime/thread_pool.cc index 5f5a811c2d30..cab04ec0db4a 100644 --- a/src/runtime/thread_pool.cc +++ b/src/runtime/thread_pool.cc @@ -24,10 +24,10 @@ #include #include #include +#include #include #include #include -#include #if TVM_THREADPOOL_USE_OPENMP #include #endif diff --git a/src/runtime/threading_backend.cc b/src/runtime/threading_backend.cc index 2527f4799086..7f9cfaa8730c 100644 --- a/src/runtime/threading_backend.cc +++ b/src/runtime/threading_backend.cc @@ -21,8 +21,8 @@ * \file threading_backend.cc * \brief Native threading backend */ +#include #include -#include #include #include diff --git a/src/runtime/vm/bytecode.cc b/src/runtime/vm/bytecode.cc index f82d708468f7..09b928fa1e39 100644 --- a/src/runtime/vm/bytecode.cc +++ b/src/runtime/vm/bytecode.cc @@ -22,8 +22,8 @@ * \brief The bytecode for Relay virtual machine. */ +#include #include -#include #include diff --git a/src/runtime/vm/vm.cc b/src/runtime/vm/vm.cc index 6d121aa67733..4683398b01d4 100644 --- a/src/runtime/vm/vm.cc +++ b/src/runtime/vm/vm.cc @@ -24,10 +24,10 @@ #include #include +#include #include #include #include -#include #include #include diff --git a/src/runtime/vulkan/vulkan_common.h b/src/runtime/vulkan/vulkan_common.h index 9cd1f257f091..3083ba6f9ce4 100644 --- a/src/runtime/vulkan/vulkan_common.h +++ b/src/runtime/vulkan/vulkan_common.h @@ -22,8 +22,8 @@ #include #include +#include #include -#include #include #include diff --git a/src/runtime/vulkan/vulkan_shader.h b/src/runtime/vulkan/vulkan_shader.h index c9fbb13e938d..513e3bccc36e 100644 --- a/src/runtime/vulkan/vulkan_shader.h +++ b/src/runtime/vulkan/vulkan_shader.h @@ -22,8 +22,8 @@ #include #include +#include #include -#include #include diff --git a/src/support/base64.h b/src/support/base64.h index 901922db8edc..3aac9920a075 100644 --- a/src/support/base64.h +++ b/src/support/base64.h @@ -26,7 +26,7 @@ #ifndef TVM_SUPPORT_BASE64_H_ #define TVM_SUPPORT_BASE64_H_ -#include +#include #include #include diff --git a/src/support/parallel_for.cc b/src/support/parallel_for.cc index f4756c29adeb..4ced0df6ddf3 100644 --- a/src/support/parallel_for.cc +++ b/src/support/parallel_for.cc @@ -21,7 +21,7 @@ * \file parallel_for.cc * \brief An implementation to run loop in parallel. */ -#include +#include #include #include diff --git a/src/support/pipe.h b/src/support/pipe.h index 3c1356ba174c..a2803638e1f3 100644 --- a/src/support/pipe.h +++ b/src/support/pipe.h @@ -25,7 +25,7 @@ #define TVM_SUPPORT_PIPE_H_ #include -#include +#include #ifdef _WIN32 #include diff --git a/src/support/socket.h b/src/support/socket.h index 16fba6b58e3d..11060ae8aae1 100644 --- a/src/support/socket.h +++ b/src/support/socket.h @@ -49,7 +49,7 @@ using ssize_t = int; #include #include #endif -#include +#include #include #include diff --git a/src/target/llvm/llvm_common.cc b/src/target/llvm/llvm_common.cc index 35bfc8dc2e5b..61dd7024ff05 100644 --- a/src/target/llvm/llvm_common.cc +++ b/src/target/llvm/llvm_common.cc @@ -24,7 +24,7 @@ #include "llvm_common.h" -#include +#include #include #include diff --git a/src/target/target.cc b/src/target/target.cc index b5ca4c38bbb9..55ef5f1a4e24 100644 --- a/src/target/target.cc +++ b/src/target/target.cc @@ -79,7 +79,7 @@ static const TObj* ObjTypeCheck(const ObjectRef& obj, const std::string& expecte std::ostringstream os; os << ": Expects type \"" << expected_type << "\", but gets \"" << obj->GetTypeKey() << "\" for object: " << obj; - throw dmlc::Error(os.str()); + throw Error(os.str()); } return ptr; } @@ -87,7 +87,7 @@ static const TObj* ObjTypeCheck(const ObjectRef& obj, const std::string& expecte static TargetKind GetTargetKind(const String& name) { Optional kind = TargetKind::Get(name); if (!kind.defined()) { - throw dmlc::Error(": Target kind \"" + name + "\" is not defined"); + throw Error(": Target kind \"" + name + "\" is not defined"); } return kind.value(); } @@ -98,10 +98,10 @@ static std::string RemovePrefixDashes(const std::string& s) { for (; n_dashes < len && s[n_dashes] == '-'; ++n_dashes) { } if (n_dashes == 0) { - throw dmlc::Error(": Attribute keys should start with '-', not an attribute key: " + s); + throw Error(": Attribute keys should start with '-', not an attribute key: " + s); } if (n_dashes >= len) { - throw dmlc::Error(": Not an attribute key: " + s); + throw Error(": Not an attribute key: " + s); } return s.substr(n_dashes); } @@ -133,7 +133,7 @@ static int ParseKVPair(const std::string& s, const std::string& s_next, std::str result_k = s.substr(0, pos); result_v = s.substr(pos + 1); if (result_k.empty() || result_v.empty()) { - throw dmlc::Error(": Empty attribute key or value in \"" + s + "\""); + throw Error(": Empty attribute key or value in \"" + s + "\""); } return 1; } else if (!s_next.empty() && s_next[0] != '-') { @@ -163,7 +163,7 @@ const TargetKindNode::ValueTypeInfo& TargetInternal::FindTypeInfo(const TargetKi } os << kv.first; } - throw dmlc::Error(os.str()); + throw Error(os.str()); } return it->second; } @@ -177,14 +177,14 @@ ObjectRef TargetInternal::ParseType(const std::string& str, // Parsing integer int v; if (!(is >> v)) { - throw dmlc::Error(": Cannot parse into type \"Integer\" from string: " + str); + throw Error(": Cannot parse into type \"Integer\" from string: " + str); } return Integer(v); } else if (info.type_index == String::ContainerType::_GetOrAllocRuntimeTypeIndex()) { // Parsing string std::string v; if (!(is >> v)) { - throw dmlc::Error(": Cannot parse into type \"String\" from string: " + str); + throw Error(": Cannot parse into type \"String\" from string: " + str); } return String(v); } else if (info.type_index == Target::ContainerType::_GetOrAllocRuntimeTypeIndex()) { @@ -197,14 +197,14 @@ ObjectRef TargetInternal::ParseType(const std::string& str, try { ObjectRef parsed = TargetInternal::ParseType(substr, *info.key); result.push_back(parsed); - } catch (const dmlc::Error& e) { + } catch (const Error& e) { std::string index = "[" + std::to_string(result.size()) + "]"; - throw dmlc::Error(index + e.what()); + throw Error(index + e.what()); } } return Array(result); } - throw dmlc::Error(": Unsupported type \"" + info.type_key + "\" for parsing from string: " + str); + throw Error(": Unsupported type \"" + info.type_key + "\" for parsing from string: " + str); } ObjectRef TargetInternal::ParseType(const ObjectRef& obj, @@ -224,15 +224,14 @@ ObjectRef TargetInternal::ParseType(const ObjectRef& obj, } else if (const auto* ptr = obj.as()) { for (const auto& kv : *ptr) { if (!kv.first->IsInstance()) { - throw dmlc::Error(": Target object requires key of dict to be str, but get: " + - kv.first->GetTypeKey()); + throw Error(": Target object requires key of dict to be str, but get: " + + kv.first->GetTypeKey()); } } Map config = GetRef>(ptr); return Target(TargetInternal::FromConfig({config.begin(), config.end()})); } - throw dmlc::Error(": Expect type 'dict' or 'str' to construct Target, but get: " + - obj->GetTypeKey()); + throw Error(": Expect type 'dict' or 'str' to construct Target, but get: " + obj->GetTypeKey()); } else if (info.type_index == ArrayNode::_GetOrAllocRuntimeTypeIndex()) { // Parsing array const auto* array = ObjTypeCheck(obj, "Array"); @@ -240,9 +239,9 @@ ObjectRef TargetInternal::ParseType(const ObjectRef& obj, for (const ObjectRef& e : *array) { try { result.push_back(TargetInternal::ParseType(e, *info.key)); - } catch (const dmlc::Error& e) { + } catch (const Error& e) { std::string index = '[' + std::to_string(result.size()) + ']'; - throw dmlc::Error(index + e.what()); + throw Error(index + e.what()); } } return Array(result); @@ -254,17 +253,17 @@ ObjectRef TargetInternal::ParseType(const ObjectRef& obj, ObjectRef key, val; try { key = TargetInternal::ParseType(kv.first, *info.key); - } catch (const dmlc::Error& e) { + } catch (const Error& e) { std::ostringstream os; os << "'s key \"" << key << "\"" << e.what(); - throw dmlc::Error(os.str()); + throw Error(os.str()); } try { val = TargetInternal::ParseType(kv.second, *info.val); - } catch (const dmlc::Error& e) { + } catch (const Error& e) { std::ostringstream os; os << "[\"" << key << "\"]" << e.what(); - throw dmlc::Error(os.str()); + throw Error(os.str()); } result[key] = val; } @@ -275,7 +274,7 @@ ObjectRef TargetInternal::ParseType(const ObjectRef& obj, os << ": Parsing type \"" << info.type_key << "\" is not supported for the given object of type \"" << obj->GetTypeKey() << "\". The object is: " << obj; - throw dmlc::Error(os.str()); + throw Error(os.str()); } return obj; } @@ -355,7 +354,7 @@ Target::Target(const String& tag_or_config_or_target_str) { ObjectPtr target; try { target = TargetInternal::FromString(tag_or_config_or_target_str); - } catch (const dmlc::Error& e) { + } catch (const Error& e) { LOG(FATAL) << "ValueError" << e.what() << ". Target creation from string failed: " << tag_or_config_or_target_str; } @@ -366,7 +365,7 @@ Target::Target(const Map& config) { ObjectPtr target; try { target = TargetInternal::FromConfig({config.begin(), config.end()}); - } catch (const dmlc::Error& e) { + } catch (const Error& e) { LOG(FATAL) << "ValueError" << e.what() << ". Target creation from config dict failed: " << config; } @@ -496,7 +495,7 @@ ObjectPtr TargetInternal::FromConfigString(const String& config_str) { "if the python module is properly loaded"; Optional> config = (*loader)(config_str); if (!config.defined()) { - throw dmlc::Error(": Cannot load config dict with python JSON loader"); + throw Error(": Cannot load config dict with python JSON loader"); } return TargetInternal::FromConfig({config.value().begin(), config.value().end()}); } @@ -514,7 +513,7 @@ ObjectPtr TargetInternal::FromRawString(const String& target_str) { } } if (name.empty()) { - throw dmlc::Error(": Cannot parse empty target string"); + throw Error(": Cannot parse empty target string"); } // Create the target config std::unordered_map config = {{"kind", String(name)}}; @@ -525,17 +524,17 @@ ObjectPtr TargetInternal::FromRawString(const String& target_str) { // Parse key-value pair std::string s_next = (iter + 1 < options.size()) ? options[iter + 1] : ""; iter += ParseKVPair(RemovePrefixDashes(options[iter]), s_next, &key, &value); - } catch (const dmlc::Error& e) { - throw dmlc::Error(": Error when parsing target" + std::string(e.what())); + } catch (const Error& e) { + throw Error(": Error when parsing target" + std::string(e.what())); } try { // check if `key` has been used if (config.count(key)) { - throw dmlc::Error(": The key \"" + key + "\" appears more than once"); + throw Error(": The key \"" + key + "\" appears more than once"); } config[key] = TargetInternal::ParseType(value, TargetInternal::FindTypeInfo(kind, key)); - } catch (const dmlc::Error& e) { - throw dmlc::Error(": Error when parsing target[\"" + key + "\"]" + e.what()); + } catch (const Error& e) { + throw Error(": Error when parsing target[\"" + key + "\"]" + e.what()); } } return TargetInternal::FromConfig(config); @@ -554,11 +553,11 @@ ObjectPtr TargetInternal::FromConfig(std::unordered_mapkind = GetTargetKind(GetRef(kind)); config.erase(kKind); } else { - throw dmlc::Error(": Expect type of field \"kind\" is String, but get type: " + - config[kKind]->GetTypeKey()); + throw Error(": Expect type of field \"kind\" is String, but get type: " + + config[kKind]->GetTypeKey()); } } else { - throw dmlc::Error(": Field \"kind\" is not found"); + throw Error(": Field \"kind\" is not found"); } // parse "tag" if (config.count(kTag)) { @@ -566,8 +565,8 @@ ObjectPtr TargetInternal::FromConfig(std::unordered_maptag = GetRef(tag); config.erase(kTag); } else { - throw dmlc::Error(": Expect type of field \"tag\" is String, but get type: " + - config[kTag]->GetTypeKey()); + throw Error(": Expect type of field \"tag\" is String, but get type: " + + config[kTag]->GetTypeKey()); } } else { target->tag = ""; @@ -582,15 +581,15 @@ ObjectPtr TargetInternal::FromConfig(std::unordered_map()) { keys.push_back(GetRef(key)); } else { - throw dmlc::Error( + throw Error( ": Expect 'keys' to be an array of strings, but it " "contains an element of type: " + e->GetTypeKey()); } } } else { - throw dmlc::Error(": Expect type of field \"keys\" is Array, but get type: " + - config[kKeys]->GetTypeKey()); + throw Error(": Expect type of field \"keys\" is Array, but get type: " + + config[kKeys]->GetTypeKey()); } } // add device name @@ -615,8 +614,8 @@ ObjectPtr TargetInternal::FromConfig(std::unordered_mapkind, key); attrs[key] = TargetInternal::ParseType(value, info); - } catch (const dmlc::Error& e) { - throw dmlc::Error(": Error when parsing target[\"" + key + "\"]" + e.what()); + } catch (const Error& e) { + throw Error(": Error when parsing target[\"" + key + "\"]" + e.what()); } } // parse host diff --git a/tests/cpp/parallel_for_test.cc b/tests/cpp/parallel_for_test.cc index bf5fe94b83ff..a4549344bd11 100644 --- a/tests/cpp/parallel_for_test.cc +++ b/tests/cpp/parallel_for_test.cc @@ -19,7 +19,7 @@ #include #include -#include +#include #include #include From 27b9d39f7f4c9b86039b48e203b50aaca6a4be68 Mon Sep 17 00:00:00 2001 From: Robert Kimball Date: Fri, 22 Jan 2021 13:29:08 -0800 Subject: [PATCH 02/67] Fix _Pragma issue on windows build --- CMakeLists.txt | 2 ++ include/tvm/runtime/logging.h | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 936f3ee77703..cd157c4f0d70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -143,6 +143,8 @@ if(MSVC) add_compile_options(/wd4146) # 'inline': used more than once add_compile_options(/wd4141) + # unknown pragma + add_compile_options(/wd4068) else(MSVC) set(WARNING_FLAG -Wall) if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index 01cf54090da4..560d243f126f 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -277,7 +277,7 @@ inline bool DebugLoggingEnabled() { #define CHECK_NE(x, y) TVM_CHECK_BINARY_OP(_NE, !=, x, y) #define CHECK_NOTNULL(x) \ ((x) == nullptr ? ::tvm::LogFatal(__FILE__, __LINE__).stream() << "Check not null: " #x << ' ', \ - (x) : (x)) // NOLINT(*) + (x) : (x)) // NOLINT(*) #define LOG_IF(severity, condition) \ !(condition) ? (void)0 : ::tvm::LogMessageVoidify() & LOG(severity) @@ -333,6 +333,13 @@ constexpr const char* kTVM_INTERNAL_ERROR_MESSAGE = #define TVM_ICHECK_INDENT " " +#ifdef _WIN32 +#define ICHECK_BINARY_OP(name, op, x, y) \ + if (!((x)op(y))) \ + ::tvm::LogFatal(__FILE__, __LINE__).stream() \ + << ::tvm::kTVM_INTERNAL_ERROR_MESSAGE << std::endl \ + << TVM_ICHECK_INDENT << "Check failed: " << #x " " #op " " #y << ": " +#else #define ICHECK_BINARY_OP(name, op, x, y) \ _Pragma("GCC diagnostic push") \ _Pragma("GCC diagnostic ignored \"-Wsign-compare\"") if (!((x)op(y))) \ @@ -340,6 +347,7 @@ constexpr const char* kTVM_INTERNAL_ERROR_MESSAGE = .stream() \ << ::tvm::kTVM_INTERNAL_ERROR_MESSAGE << std::endl \ << TVM_ICHECK_INDENT << "Check failed: " << #x " " #op " " #y << ": " +#endif #define ICHECK(x) \ if (!(x)) \ From 3db736f1a1b36d54ef0b44961d55a96822b184d7 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Fri, 22 Jan 2021 11:48:06 -0800 Subject: [PATCH 03/67] allow libbacktrace patch --- tests/lint/check_file_type.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/lint/check_file_type.py b/tests/lint/check_file_type.py index ab51b6c79c83..2e4a1fcc774c 100644 --- a/tests/lint/check_file_type.py +++ b/tests/lint/check_file_type.py @@ -131,6 +131,8 @@ # microTVM Virtual Machines "apps/microtvm/reference-vm/zephyr/Vagrantfile", "apps/microtvm/reference-vm/zephyr/base-box/Vagrantfile.packer-template", + # patch file for libbacktrace + "cmake/modules/libbacktrace_macos.patch" } From e546407155db46e7045a859c3e1e6b6c0e0b4637 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Fri, 22 Jan 2021 14:17:02 -0800 Subject: [PATCH 04/67] add apache headers --- cmake/modules/Libbacktrace.cmake | 16 ++++++++++++++++ cmake/modules/libbacktrace_macos.patch | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/cmake/modules/Libbacktrace.cmake b/cmake/modules/Libbacktrace.cmake index d9c5958b1265..032f0ef892c3 100644 --- a/cmake/modules/Libbacktrace.cmake +++ b/cmake/modules/Libbacktrace.cmake @@ -1,3 +1,19 @@ +# 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. include(ExternalProject) # Git tag for libbacktrace diff --git a/cmake/modules/libbacktrace_macos.patch b/cmake/modules/libbacktrace_macos.patch index 3d6cbf672091..eee309c00a9b 100644 --- a/cmake/modules/libbacktrace_macos.patch +++ b/cmake/modules/libbacktrace_macos.patch @@ -1,3 +1,19 @@ +# 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. diff --git a/macho.c b/macho.c index 66e101e..d00aea9 100644 --- a/macho.c From 9e39bb553718a1d9c6c50a3978c8630291f10619 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Fri, 22 Jan 2021 14:23:36 -0800 Subject: [PATCH 05/67] lint fix --- tests/lint/check_file_type.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lint/check_file_type.py b/tests/lint/check_file_type.py index 2e4a1fcc774c..f5c0de0a50b0 100644 --- a/tests/lint/check_file_type.py +++ b/tests/lint/check_file_type.py @@ -132,7 +132,7 @@ "apps/microtvm/reference-vm/zephyr/Vagrantfile", "apps/microtvm/reference-vm/zephyr/base-box/Vagrantfile.packer-template", # patch file for libbacktrace - "cmake/modules/libbacktrace_macos.patch" + "cmake/modules/libbacktrace_macos.patch", } From 926b9b77785f29fcf2a3fc1c47e600718be48712 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Fri, 22 Jan 2021 15:46:14 -0800 Subject: [PATCH 06/67] handle early cmake versions --- CMakeLists.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cd157c4f0d70..ee90b8d3e23a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -400,9 +400,10 @@ if(USE_LIBBACKTRACE) message(STATUS "Building with libbacktrace...") include(cmake/modules/Libbacktrace.cmake) target_link_libraries(tvm PRIVATE libbacktrace) - target_link_libraries(tvm_objs PRIVATE libbacktrace) target_link_libraries(tvm_runtime PRIVATE libbacktrace) - target_link_libraries(tvm_runtime_objs PRIVATE libbacktrace) + # pre 3.11 versions of cmake cannot propagate include directories from imported targets so we set them manually + target_include_directories(tvm_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") + target_include_directories(tvm_runtime_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") else() add_definitions(-DTVM_BACKTRACE_DISABLED) endif() From c69df46c2c1629601b7c7c4047ab1f4165670609 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 25 Jan 2021 09:14:41 -0800 Subject: [PATCH 07/67] Move logging into runtime namespace. Add detail namespace for logging impl. Documentation. --- include/tvm/ir/diagnostic.h | 9 +++ include/tvm/runtime/logging.h | 128 ++++++++++++++++++---------------- src/runtime/c_runtime_api.cc | 4 +- src/runtime/logging.cc | 2 + 4 files changed, 81 insertions(+), 62 deletions(-) diff --git a/include/tvm/ir/diagnostic.h b/include/tvm/ir/diagnostic.h index 2053a295a3b8..41130a5be0aa 100644 --- a/include/tvm/ir/diagnostic.h +++ b/include/tvm/ir/diagnostic.h @@ -37,6 +37,15 @@ namespace tvm { using tvm::parser::SourceMap; using tvm::runtime::TypedPackedFunc; +/*! \brief The diagnostic level, controls the printing of the message. */ +enum class DiagnosticLevel : int { + kBug = 10, + kError = 20, + kWarning = 30, + kNote = 40, + kHelp = 50, +}; + class DiagnosticBuilder; /*! \brief A compiler diagnostic. */ diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index 560d243f126f..27fd531b50a0 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -125,9 +125,12 @@ #define TVM_THROW_EXCEPTION noexcept(false) namespace tvm { +namespace runtime { #ifndef TVM_BACKTRACE_DISABLED -/* \brief Generate a backtrace when called. */ +/* \brief Generate a backtrace when called. + * \return A multiline string of the backtrace. There will be either one or two lines per frame. + */ std::string Backtrace(); #else // TODO(bkimball,tkonolige) This inline function is to work around a linking error I am having when @@ -140,6 +143,9 @@ inline std::string Backtrace() { return ""; } /*! \brief Base error type for TVM. Wraps a string message. */ class Error : public std::runtime_error { public: + /*! \brief Construct an error. + * \param s The message to be displayed with the error. + */ explicit Error(const std::string& s) : std::runtime_error(s) {} }; @@ -156,8 +162,8 @@ class InternalError : public Error { * \param time The time at which the error occurred. * \param backtrace Backtrace from when the error occurred. */ - InternalError(const std::string& file, int lineno, const std::string& message, - const std::time_t& time = std::time(nullptr), std::string backtrace = Backtrace()) + InternalError(std::string file, int lineno, std::string message, + std::time_t time = std::time(nullptr), std::string backtrace = Backtrace()) : Error(""), file_(file), lineno_(lineno), @@ -171,17 +177,17 @@ class InternalError : public Error { << message << std::endl; full_message_ = s.str(); } - /*! \brief The file in which the error occurred. */ + /*! \return The file in which the error occurred. */ const std::string& file() const { return file_; } - /*! \brief The message associated with this error. */ + /*! \return The message associated with this error. */ const std::string& message() const { return message_; } - /*! \brief Formatted error message including file, linenumber, backtrace, and message. */ + /*! \return Formatted error message including file, linenumber, backtrace, and message. */ const std::string& full_message() const { return full_message_; } - /*! \brief The backtrace from where this error occurred. */ + /*! \return The backtrace from where this error occurred. */ const std::string& backtrace() const { return backtrace_; } - /*! \brief The time at which this error occurred. */ + /*! \return The time at which this error occurred. */ const std::time_t& time() const { return time_; } - /*! \brief The line number at which this error occurred. */ + /*! \return The line number at which this error occurred. */ int lineno() const { return lineno_; } virtual const char* what() const noexcept { return full_message_.c_str(); } @@ -194,6 +200,7 @@ class InternalError : public Error { std::string full_message_; // holds the full error string }; +namespace detail { /*! \brief Class to accumulate an error message and throw it. Do not use * directly, instead use LOG(FATAL). */ @@ -256,18 +263,29 @@ inline bool DebugLoggingEnabled() { return state == 1; } +constexpr const char* kTVM_INTERNAL_ERROR_MESSAGE = + "---------------------------------------------------------------\n" + "An internal invariant was violated during the execution of TVM.\n" + "Please read TVM's error reporting guidelines.\n" + "More details can be found here: https://discuss.tvm.ai/t/error-reporting/7793.\n" + "---------------------------------------------------------------\n"; +} // namespace detail + #define LOG(level) LOG_##level -#define LOG_FATAL ::tvm::LogFatal(__FILE__, __LINE__).stream() -#define LOG_INFO ::tvm::LogMessage(__FILE__, __LINE__).stream() -#define LOG_ERROR (::tvm::LogMessage(__FILE__, __LINE__).stream() << "error: ") -#define LOG_WARNING (::tvm::LogMessage(__FILE__, __LINE__).stream() << "warning: ") +#define LOG_FATAL ::tvm::runtime::detail::LogFatal(__FILE__, __LINE__).stream() +#define LOG_INFO ::tvm::runtime::detail::LogMessage(__FILE__, __LINE__).stream() +#define LOG_ERROR (::tvm::runtime::detail::LogMessage(__FILE__, __LINE__).stream() << "error: ") +#define LOG_WARNING (::tvm::runtime::detail::LogMessage(__FILE__, __LINE__).stream() << "warning: ") -#define TVM_CHECK_BINARY_OP(name, op, x, y) \ - if (!((x)op(y))) \ - ::tvm::LogFatal(__FILE__, __LINE__).stream() << "Check failed: " << #x " " #op " " #y << ": " +#define TVM_CHECK_BINARY_OP(name, op, x, y) \ + if (!((x)op(y))) \ + ::tvm::runtime::detail::LogFatal(__FILE__, __LINE__).stream() \ + << "Check failed: " << #x " " #op " " #y << ": " -#define CHECK(x) \ - if (!(x)) ::tvm::LogFatal(__FILE__, __LINE__).stream() << "Check failed: " #x << " == false: " +#define CHECK(x) \ + if (!(x)) \ + ::tvm::runtime::detail::LogFatal(__FILE__, __LINE__).stream() \ + << "Check failed: " #x << " == false: " #define CHECK_LT(x, y) TVM_CHECK_BINARY_OP(_LT, <, x, y) #define CHECK_GT(x, y) TVM_CHECK_BINARY_OP(_GT, >, x, y) @@ -275,27 +293,29 @@ inline bool DebugLoggingEnabled() { #define CHECK_GE(x, y) TVM_CHECK_BINARY_OP(_GE, >=, x, y) #define CHECK_EQ(x, y) TVM_CHECK_BINARY_OP(_EQ, ==, x, y) #define CHECK_NE(x, y) TVM_CHECK_BINARY_OP(_NE, !=, x, y) -#define CHECK_NOTNULL(x) \ - ((x) == nullptr ? ::tvm::LogFatal(__FILE__, __LINE__).stream() << "Check not null: " #x << ' ', \ +#define CHECK_NOTNULL(x) \ + ((x) == nullptr ? ::tvm::runtime::detail::LogFatal(__FILE__, __LINE__).stream() \ + << "Check not null: " #x << ' ', \ (x) : (x)) // NOLINT(*) #define LOG_IF(severity, condition) \ - !(condition) ? (void)0 : ::tvm::LogMessageVoidify() & LOG(severity) + !(condition) ? (void)0 : ::tvm::runtime::detail::LogMessageVoidify() & LOG(severity) #if TVM_LOG_DEBUG #define LOG_DFATAL LOG_FATAL #define DFATAL FATAL -#define DLOG(severity) LOG_IF(severity, ::tvm::DebugLoggingEnabled()) -#define DLOG_IF(severity, condition) LOG_IF(severity, ::tvm::DebugLoggingEnabled() && (condition)) +#define DLOG(severity) LOG_IF(severity, ::tvm::runtime::detail::DebugLoggingEnabled()) +#define DLOG_IF(severity, condition) \ + LOG_IF(severity, ::tvm::runtime::detail::DebugLoggingEnabled() && (condition)) #else #define LOG_DFATAL LOG_ERROR #define DFATAL ERROR -#define DLOG(severity) true ? (void)0 : ::tvm::LogMessageVoidify() & LOG(severity) +#define DLOG(severity) true ? (void)0 : ::tvm::runtime::detail::LogMessageVoidify() & LOG(severity) #define DLOG_IF(severity, condition) \ - (true || !(condition)) ? (void)0 : ::tvm::LogMessageVoidify() & LOG(severity) + (true || !(condition)) ? (void)0 : ::tvm::runtime::detail::LogMessageVoidify() & LOG(severity) #endif @@ -324,36 +344,29 @@ inline bool DebugLoggingEnabled() { #define DCHECK_NE(x, y) CHECK((x) != (y)) #endif -constexpr const char* kTVM_INTERNAL_ERROR_MESSAGE = - "---------------------------------------------------------------\n" - "An internal invariant was violated during the execution of TVM.\n" - "Please read TVM's error reporting guidelines.\n" - "More details can be found here: https://discuss.tvm.ai/t/error-reporting/7793.\n" - "---------------------------------------------------------------\n"; - #define TVM_ICHECK_INDENT " " #ifdef _WIN32 -#define ICHECK_BINARY_OP(name, op, x, y) \ - if (!((x)op(y))) \ - ::tvm::LogFatal(__FILE__, __LINE__).stream() \ - << ::tvm::kTVM_INTERNAL_ERROR_MESSAGE << std::endl \ +#define ICHECK_BINARY_OP(name, op, x, y) \ + if (!((x)op(y))) \ + ::tvm::runtime::detail::LogFatal(__FILE__, __LINE__).stream() \ + << ::tvm::runtime::detail::kTVM_INTERNAL_ERROR_MESSAGE << std::endl \ << TVM_ICHECK_INDENT << "Check failed: " << #x " " #op " " #y << ": " #else -#define ICHECK_BINARY_OP(name, op, x, y) \ - _Pragma("GCC diagnostic push") \ - _Pragma("GCC diagnostic ignored \"-Wsign-compare\"") if (!((x)op(y))) \ - _Pragma("GCC diagnostic pop")::tvm::LogFatal(__FILE__, __LINE__) \ - .stream() \ - << ::tvm::kTVM_INTERNAL_ERROR_MESSAGE << std::endl \ +#define ICHECK_BINARY_OP(name, op, x, y) \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wsign-compare\"") if (!((x)op(y))) \ + _Pragma("GCC diagnostic pop")::tvm::runtime::detail::LogFatal(__FILE__, __LINE__) \ + .stream() \ + << ::tvm::runtime::detail::kTVM_INTERNAL_ERROR_MESSAGE << std::endl \ << TVM_ICHECK_INDENT << "Check failed: " << #x " " #op " " #y << ": " #endif -#define ICHECK(x) \ - if (!(x)) \ - ::tvm::LogFatal(__FILE__, __LINE__).stream() \ - << ::tvm::kTVM_INTERNAL_ERROR_MESSAGE << TVM_ICHECK_INDENT << "Check failed: " #x \ - << " == false: " +#define ICHECK(x) \ + if (!(x)) \ + ::tvm::runtime::detail::LogFatal(__FILE__, __LINE__).stream() \ + << ::tvm::runtime::detail::kTVM_INTERNAL_ERROR_MESSAGE << TVM_ICHECK_INDENT \ + << "Check failed: " #x << " == false: " #define ICHECK_LT(x, y) ICHECK_BINARY_OP(_LT, <, x, y) #define ICHECK_GT(x, y) ICHECK_BINARY_OP(_GT, >, x, y) @@ -361,20 +374,15 @@ constexpr const char* kTVM_INTERNAL_ERROR_MESSAGE = #define ICHECK_GE(x, y) ICHECK_BINARY_OP(_GE, >=, x, y) #define ICHECK_EQ(x, y) ICHECK_BINARY_OP(_EQ, ==, x, y) #define ICHECK_NE(x, y) ICHECK_BINARY_OP(_NE, !=, x, y) -#define ICHECK_NOTNULL(x) \ - ((x) == nullptr ? ::tvm::LogFatal(__FILE__, __LINE__).stream() \ - << ::tvm::kTVM_INTERNAL_ERROR_MESSAGE << TVM_ICHECK_INDENT \ - << "Check not null: " #x << ' ', \ +#define ICHECK_NOTNULL(x) \ + ((x) == nullptr ? ::tvm::runtime::detail::LogFatal(__FILE__, __LINE__).stream() \ + << ::tvm::runtime::detail::kTVM_INTERNAL_ERROR_MESSAGE \ + << TVM_ICHECK_INDENT << "Check not null: " #x << ' ', \ (x) : (x)) // NOLINT(*) -/*! \brief The diagnostic level, controls the printing of the message. */ -enum class DiagnosticLevel : int { - kBug = 10, - kError = 20, - kWarning = 30, - kNote = 40, - kHelp = 50, -}; - +} // namespace runtime +// Re-export error types +using runtime::Error; +using runtime::InternalError; } // namespace tvm #endif // TVM_RUNTIME_LOGGING_H_ diff --git a/src/runtime/c_runtime_api.cc b/src/runtime/c_runtime_api.cc index ea18208dcfab..0e47f63c449b 100644 --- a/src/runtime/c_runtime_api.cc +++ b/src/runtime/c_runtime_api.cc @@ -519,7 +519,7 @@ int TVMFuncCreateFromCFunc(TVMPackedCFunc func, void* resource_handle, TVMPacked args.num_args, rv, resource_handle); if (ret != 0) { throw tvm::Error(TVMGetLastError() + - tvm::Backtrace()); // TODO(tkonolige): this may double backtrace + tvm::runtime::Backtrace()); // TODO(tkonolige): this may double backtrace } }); } else { @@ -530,7 +530,7 @@ int TVMFuncCreateFromCFunc(TVMPackedCFunc func, void* resource_handle, TVMPacked int ret = func(const_cast(args.values), const_cast(args.type_codes), args.num_args, rv, rpack.get()); if (ret != 0) { - throw tvm::Error(TVMGetLastError() + tvm::Backtrace()); + throw tvm::Error(TVMGetLastError() + tvm::runtime::Backtrace()); } }); } diff --git a/src/runtime/logging.cc b/src/runtime/logging.cc index 317c3b2b316a..836c0280f77c 100644 --- a/src/runtime/logging.cc +++ b/src/runtime/logging.cc @@ -30,6 +30,7 @@ #include namespace tvm { +namespace runtime { namespace { struct BacktraceInfo { @@ -120,5 +121,6 @@ std::string Backtrace() { return s.str(); } +} // namespace runtime } // namespace tvm #endif From ef74ccdc869c19938433dc14f29ca2edf1f7069e Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 25 Jan 2021 09:42:03 -0800 Subject: [PATCH 08/67] push CHECK_OP comparisons into functions to handle early versions of GCCs pragmas. --- include/tvm/runtime/logging.h | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index 27fd531b50a0..fc3d37405cb6 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -29,6 +29,8 @@ #ifndef TVM_RUNTIME_LOGGING_H_ #define TVM_RUNTIME_LOGGING_H_ +#include + #include #include #include @@ -269,6 +271,25 @@ constexpr const char* kTVM_INTERNAL_ERROR_MESSAGE = "Please read TVM's error reporting guidelines.\n" "More details can be found here: https://discuss.tvm.ai/t/error-reporting/7793.\n" "---------------------------------------------------------------\n"; + +// Inline _Pragma in macros does not work reliably on old version of MVSC and +// GCC. We wrap all comparisons in a function so that we can use #pragma to +// silence bad comparison warnings. +#define TVM_CHECK_FUNC(name, op) \ + template \ + DMLC_ALWAYS_INLINE bool LogCheck##name(const A& a, const B& b) { \ + return a op b; \ + } + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wsign-compare" +TVM_CHECK_FUNC(_LT, <) +TVM_CHECK_FUNC(_GT, >) +TVM_CHECK_FUNC(_LE, <=) +TVM_CHECK_FUNC(_GE, >=) +TVM_CHECK_FUNC(_EQ, ==) +TVM_CHECK_FUNC(_NE, !=) +#pragma GCC diagnostic pop } // namespace detail #define LOG(level) LOG_##level @@ -278,7 +299,7 @@ constexpr const char* kTVM_INTERNAL_ERROR_MESSAGE = #define LOG_WARNING (::tvm::runtime::detail::LogMessage(__FILE__, __LINE__).stream() << "warning: ") #define TVM_CHECK_BINARY_OP(name, op, x, y) \ - if (!((x)op(y))) \ + if (::tvm::runtime::detail::LogCheck##name(x, y)) \ ::tvm::runtime::detail::LogFatal(__FILE__, __LINE__).stream() \ << "Check failed: " << #x " " #op " " #y << ": " @@ -346,21 +367,11 @@ constexpr const char* kTVM_INTERNAL_ERROR_MESSAGE = #define TVM_ICHECK_INDENT " " -#ifdef _WIN32 #define ICHECK_BINARY_OP(name, op, x, y) \ - if (!((x)op(y))) \ + if (::tvm::runtime::detail::LogCheck##name(x, y)) \ ::tvm::runtime::detail::LogFatal(__FILE__, __LINE__).stream() \ << ::tvm::runtime::detail::kTVM_INTERNAL_ERROR_MESSAGE << std::endl \ << TVM_ICHECK_INDENT << "Check failed: " << #x " " #op " " #y << ": " -#else -#define ICHECK_BINARY_OP(name, op, x, y) \ - _Pragma("GCC diagnostic push") \ - _Pragma("GCC diagnostic ignored \"-Wsign-compare\"") if (!((x)op(y))) \ - _Pragma("GCC diagnostic pop")::tvm::runtime::detail::LogFatal(__FILE__, __LINE__) \ - .stream() \ - << ::tvm::runtime::detail::kTVM_INTERNAL_ERROR_MESSAGE << std::endl \ - << TVM_ICHECK_INDENT << "Check failed: " << #x " " #op " " #y << ": " -#endif #define ICHECK(x) \ if (!(x)) \ From 6f8505eefd8d21921ce331d28badca6d81bb447b Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 25 Jan 2021 10:24:15 -0800 Subject: [PATCH 09/67] make object files depend on libbacktrace --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index ee90b8d3e23a..ce5b4cb3e1ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -403,7 +403,9 @@ if(USE_LIBBACKTRACE) target_link_libraries(tvm_runtime PRIVATE libbacktrace) # pre 3.11 versions of cmake cannot propagate include directories from imported targets so we set them manually target_include_directories(tvm_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") + add_dependencies(tvm_objs libbacktrace) target_include_directories(tvm_runtime_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") + add_dependencies(tvm_runtime_objs libbacktrace) else() add_definitions(-DTVM_BACKTRACE_DISABLED) endif() From a79c3646d4c9332dbebae649dc39be11f99d238f Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 25 Jan 2021 13:13:08 -0800 Subject: [PATCH 10/67] Make tvm::Error a subclass of dmlc::Error --- include/tvm/runtime/logging.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index fc3d37405cb6..0cbe466df0d8 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -143,12 +143,12 @@ inline std::string Backtrace() { return ""; } #endif /*! \brief Base error type for TVM. Wraps a string message. */ -class Error : public std::runtime_error { +class Error : public ::dmlc::Error { // for backwards compatibility public: /*! \brief Construct an error. * \param s The message to be displayed with the error. */ - explicit Error(const std::string& s) : std::runtime_error(s) {} + explicit Error(const std::string& s) : ::dmlc::Error(s) {} }; /*! \brief Error type for errors from CHECK, ICHECK, and LOG(FATAL). This error From 939a3d9eee2c977a31aaa5bfa02d109b20e78223 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 25 Jan 2021 13:13:26 -0800 Subject: [PATCH 11/67] allow custom logging for non-standard backends --- .../app/src/main/jni/tvm_runtime.h | 57 ++++++++++++++----- .../app/src/main/jni/tvm_runtime.h | 54 +++++++++++++----- apps/ios_rpc/tvmrpc/TVMRuntime.h | 2 +- apps/ios_rpc/tvmrpc/TVMRuntime.mm | 34 ++++++++++- apps/ios_rpc/tvmrpc/ViewController.mm | 4 +- include/tvm/runtime/logging.h | 11 ++++ src/relay/op/tensor/transform.cc | 2 +- src/runtime/crt/graph_runtime/load_json.c | 2 +- tests/cpp/ir_functor_test.cc | 2 +- tutorials/auto_scheduler/tune_network_cuda.py | 2 +- tutorials/auto_scheduler/tune_network_mali.py | 2 +- tutorials/auto_scheduler/tune_network_x86.py | 2 +- web/emcc/tvmjs_support.cc | 6 +- web/emcc/wasm_runtime.cc | 6 +- web/emcc/webgpu_runtime.cc | 48 ++++++++++++++-- 15 files changed, 177 insertions(+), 57 deletions(-) diff --git a/apps/android_camera/app/src/main/jni/tvm_runtime.h b/apps/android_camera/app/src/main/jni/tvm_runtime.h index 5f3db04274a1..0ab993a37a26 100644 --- a/apps/android_camera/app/src/main/jni/tvm_runtime.h +++ b/apps/android_camera/app/src/main/jni/tvm_runtime.h @@ -25,17 +25,11 @@ #include -/* Enable custom logging - this will cause TVM to pass every log message - * through CustomLogMessage instead of LogMessage. By enabling this, we must - * implement dmlc::CustomLogMessage::Log. We use this to pass TVM log - * messages to Android logcat. +/* Enable custom logging - this will cause TVM to use a custom implementation + * of tvm::runtime::detail::LogMessage. We use this to pass TVM log messages to + * Android logcat. */ -#define DMLC_LOG_CUSTOMIZE 1 - -/* Ensure that fatal errors are passed to the logger before throwing - * in LogMessageFatal - */ -#define DMLC_LOG_BEFORE_THROW 1 +#define TVM_LOG_CUSTOMIZE 1 #include "../src/runtime/c_runtime_api.cc" #include "../src/runtime/cpu_device_api.cc" @@ -72,8 +66,41 @@ #include -void dmlc::CustomLogMessage::Log(const std::string& msg) { - // This is called for every message logged by TVM. - // We pass the message to logcat. - __android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", msg.c_str()); -} \ No newline at end of file + +namespace tvm { +namespace runtime{ +namespace detail{ +// Override logging mechanism +class LogFatal { + public: + LogFatal(const std::string& file, int lineno) : file_(file), lineno_(lineno) {} + ~LogFatal() TVM_THROW_EXCEPTION { + __android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", stream_.str().c_str()); + throw InternalError(file_, lineno_, stream_.str()); } + std::ostringstream& stream() { return stream_; } + + private: + std::ostringstream stream_; + std::string file_; + int lineno_; +}; + +class LogMessage { + public: + LogMessage(const std::string& file, int lineno) { + std::time_t t = std::time(nullptr); + stream_ << "[" << std::put_time(std::localtime(&t), "%H:%M:%S") << "] " << file << ":" << lineno + << ": "; + } + ~LogMessage() { + + __android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", stream_.str().c_str()); + } + std::ostringstream& stream() { return stream_; } + + private: + std::ostringstream stream_; +}; +} +} +} // namespace dmlc diff --git a/apps/android_rpc/app/src/main/jni/tvm_runtime.h b/apps/android_rpc/app/src/main/jni/tvm_runtime.h index fb5993066448..fbc1aaf46445 100644 --- a/apps/android_rpc/app/src/main/jni/tvm_runtime.h +++ b/apps/android_rpc/app/src/main/jni/tvm_runtime.h @@ -25,17 +25,11 @@ #include -/* Enable custom logging - this will cause TVM to pass every log message - * through CustomLogMessage instead of LogMessage. By enabling this, we must - * implement dmlc::CustomLogMessage::Log. We use this to pass TVM log - * messages to Android logcat. +/* Enable custom logging - this will cause TVM to use a custom implementation + * of tvm::runtime::detail::LogMessage. We use this to pass TVM log messages to + * Android logcat. */ -#define DMLC_LOG_CUSTOMIZE 1 - -/* Ensure that fatal errors are passed to the logger before throwing - * in LogMessageFatal - */ -#define DMLC_LOG_BEFORE_THROW 1 +#define TVM_LOG_CUSTOMIZE 1 #include "../src/runtime/c_runtime_api.cc" #include "../src/runtime/cpu_device_api.cc" @@ -81,8 +75,40 @@ #include -void dmlc::CustomLogMessage::Log(const std::string& msg) { - // This is called for every message logged by TVM. - // We pass the message to logcat. - __android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", msg.c_str()); +namespace tvm { +namespace runtime{ +namespace detail{ +// Override logging mechanism +class LogFatal { + public: + LogFatal(const std::string& file, int lineno) : file_(file), lineno_(lineno) {} + ~LogFatal() TVM_THROW_EXCEPTION { + __android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", stream_.str().c_str()); + throw InternalError(file_, lineno_, stream_.str()); } + std::ostringstream& stream() { return stream_; } + + private: + std::ostringstream stream_; + std::string file_; + int lineno_; +}; + +class LogMessage { + public: + LogMessage(const std::string& file, int lineno) { + std::time_t t = std::time(nullptr); + stream_ << "[" << std::put_time(std::localtime(&t), "%H:%M:%S") << "] " << file << ":" << lineno + << ": "; + } + ~LogMessage() { + + __android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", stream_.str().c_str()); + } + std::ostringstream& stream() { return stream_; } + + private: + std::ostringstream stream_; +}; +} } +} // namespace dmlc diff --git a/apps/ios_rpc/tvmrpc/TVMRuntime.h b/apps/ios_rpc/tvmrpc/TVMRuntime.h index f6a6dc64c53a..0d172fc3eaa1 100644 --- a/apps/ios_rpc/tvmrpc/TVMRuntime.h +++ b/apps/ios_rpc/tvmrpc/TVMRuntime.h @@ -22,7 +22,7 @@ */ #import // Customize logging mechanism, redirect to NSLOG -#define DMLC_LOG_CUSTOMIZE 1 +#define TVM_LOG_CUSTOMIZE 1 #define TVM_METAL_RUNTIME 1 #include diff --git a/apps/ios_rpc/tvmrpc/TVMRuntime.mm b/apps/ios_rpc/tvmrpc/TVMRuntime.mm index fbe4850e1b57..0d0b49b3cbdf 100644 --- a/apps/ios_rpc/tvmrpc/TVMRuntime.mm +++ b/apps/ios_rpc/tvmrpc/TVMRuntime.mm @@ -53,9 +53,37 @@ // CoreML #include "../../../src/runtime/contrib/coreml/coreml_runtime.mm" -namespace dmlc { +namespace tvm { +namespace runtime{ +namespace detail{ // Override logging mechanism -void CustomLogMessage::Log(const std::string& msg) { NSLog(@"%s", msg.c_str()); } +class LogFatal { + public: + LogFatal(const std::string& file, int lineno) : file_(file), lineno_(lineno) {} + ~LogFatal() TVM_THROW_EXCEPTION { throw InternalError(file_, lineno_, stream_.str()); } + std::ostringstream& stream() { return stream_; } + + private: + std::ostringstream stream_; + std::string file_; + int lineno_; +}; + +class LogMessage { + public: + LogMessage(const std::string& file, int lineno) { + std::time_t t = std::time(nullptr); + stream_ << "[" << std::put_time(std::localtime(&t), "%H:%M:%S") << "] " << file << ":" << lineno + << ": "; + } + ~LogMessage() { NSLog(@"%s", stream_.str().c_str(); } + std::ostringstream& stream() { return stream_; } + + private: + std::ostringstream stream_; +}; +} +} } // namespace dmlc namespace tvm { @@ -69,7 +97,7 @@ size_t Send(const void* data, size_t size) final { ssize_t nbytes = [stream_ write:reinterpret_cast(data) maxLength:size]; if (nbytes < 0) { NSLog(@"%@", [stream_ streamError].localizedDescription); - throw dmlc::Error("Stream error"); + throw tvm::Error("Stream error"); } return nbytes; } diff --git a/apps/ios_rpc/tvmrpc/ViewController.mm b/apps/ios_rpc/tvmrpc/ViewController.mm index 910c650aedc1..879ed2334a84 100644 --- a/apps/ios_rpc/tvmrpc/ViewController.mm +++ b/apps/ios_rpc/tvmrpc/ViewController.mm @@ -100,7 +100,7 @@ - (void)onReadAvailable { if (flag == 2) { [self onShutdownReceived]; } - } catch (const dmlc::Error& e) { + } catch (const tvm::Error& e) { [self close]; } } @@ -123,7 +123,7 @@ - (void)onWriteAvailable { if (flag == 2) { [self onShutdownReceived]; } - } catch (const dmlc::Error& e) { + } catch (const tvm::Error& e) { [self close]; } } diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index 0cbe466df0d8..9be8142a4f94 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -203,8 +203,14 @@ class InternalError : public Error { }; namespace detail { +#ifndef TVM_LOG_CUSTOMIZE + /*! \brief Class to accumulate an error message and throw it. Do not use * directly, instead use LOG(FATAL). + * + * If TVM_LOG_CUSTOMIZE is defined, then the user must provide their own + * implementation of LogFatal. This implementation will then be used in place + * of the default one. */ class LogFatal { public: @@ -220,6 +226,10 @@ class LogFatal { /*! \brief Class to accumulate an log message. Do not use directly, instead use * LOG(INFO), LOG(WARNING), LOG(ERROR). + * + * If TVM_LOG_CUSTOMIZE is defined, then the user must provide their own + * implementation of LogMessage. This implementation will then be used in place + * of the default one. */ class LogMessage { public: @@ -234,6 +244,7 @@ class LogMessage { private: std::ostringstream stream_; }; +#endif // Below is from dmlc-core // This class is used to explicitly ignore values in the conditional diff --git a/src/relay/op/tensor/transform.cc b/src/relay/op/tensor/transform.cc index 557e679088a3..b65068bd0506 100644 --- a/src/relay/op/tensor/transform.cc +++ b/src/relay/op/tensor/transform.cc @@ -483,7 +483,7 @@ Array> TransposeInferCorrectLayout(const Attrs& attrs, } try { return Array>({{Layout(in_layout_str)}, {Layout(out_layout_str)}}); - } catch (const dmlc::Error& e) { + } catch (const tvm::Error& e) { // If the layout string is invalid for any reason, give up. return Array>({{Layout::Undef()}, {Layout::Undef()}}); } diff --git a/src/runtime/crt/graph_runtime/load_json.c b/src/runtime/crt/graph_runtime/load_json.c index 6de49a3f9789..3d1fb601a355 100644 --- a/src/runtime/crt/graph_runtime/load_json.c +++ b/src/runtime/crt/graph_runtime/load_json.c @@ -173,7 +173,7 @@ char JSONReader_PeekNextNonSpace(JSONReader* reader) { * \param out_str the output string. NULL to merely consume input and discard it. * \param out_str_size Number of bytes available to write starting from out_str. Includes * terminating \0. - * \throw dmlc::Error when next token is not string + * \throw tvm::Error when next token is not string */ int JSONReader_ReadString(JSONReader* reader, char* out_str, size_t out_str_size) { int status = 0; diff --git a/tests/cpp/ir_functor_test.cc b/tests/cpp/ir_functor_test.cc index 1f7d18f747ea..9e8595d6809c 100644 --- a/tests/cpp/ir_functor_test.cc +++ b/tests/cpp/ir_functor_test.cc @@ -125,7 +125,7 @@ TEST(IRF, ExprTransform) { try { f(z - 1, 2); LOG(FATAL) << "should fail"; - } catch (dmlc::Error&) { + } catch (Error&) { } } diff --git a/tutorials/auto_scheduler/tune_network_cuda.py b/tutorials/auto_scheduler/tune_network_cuda.py index 5ed3ceef5ba0..bc88457f94f9 100644 --- a/tutorials/auto_scheduler/tune_network_cuda.py +++ b/tutorials/auto_scheduler/tune_network_cuda.py @@ -252,7 +252,7 @@ def run_tuning(): # The last line also prints the total number of measurement trials, # total time spent on auto-tuning and the id of the next task to tune. # -# There will also be some "dmlc::Error"s and CUDA errors, because the +# There will also be some "tvm::Error"s and CUDA errors, because the # auto-scheduler will try some invalid schedules. # You can safely ignore them if the tuning can continue, because these # errors are isolated from the main process. diff --git a/tutorials/auto_scheduler/tune_network_mali.py b/tutorials/auto_scheduler/tune_network_mali.py index ca1067b27c80..6a650228199e 100644 --- a/tutorials/auto_scheduler/tune_network_mali.py +++ b/tutorials/auto_scheduler/tune_network_mali.py @@ -329,7 +329,7 @@ def tune_and_evaluate(): # The last line also prints the total number of measurement trials, # total time spent on auto-tuning and the id of the next task to tune. # -# There will also be some "dmlc::Error"s errors, because the +# There will also be some "tvn::Error"s errors, because the # auto-scheduler will try some invalid schedules. # You can safely ignore them if the tuning can continue, because these # errors are isolated from the main process. diff --git a/tutorials/auto_scheduler/tune_network_x86.py b/tutorials/auto_scheduler/tune_network_x86.py index 8526abbbe6ca..1cfcd7206be1 100644 --- a/tutorials/auto_scheduler/tune_network_x86.py +++ b/tutorials/auto_scheduler/tune_network_x86.py @@ -251,7 +251,7 @@ def run_tuning(): # The last line also prints the total number of measurement trials, # total time spent on auto-tuning and the id of the next task to tune. # -# There will also be some "dmlc::Error"s errors, because the +# There will also be some "tvn::Error"s errors, because the # auto-scheduler will try some invalid schedules. # You can safely ignore them if the tuning can continue, because these # errors are isolated from the main process. diff --git a/web/emcc/tvmjs_support.cc b/web/emcc/tvmjs_support.cc index b72caad1e3df..e60d84979d83 100644 --- a/web/emcc/tvmjs_support.cc +++ b/web/emcc/tvmjs_support.cc @@ -25,11 +25,7 @@ */ // configurations for the dmlc log. -#define DMLC_LOG_CUSTOMIZE 0 -#define DMLC_LOG_STACK_TRACE 0 -#define DMLC_LOG_DEBUG 0 -#define DMLC_LOG_NODATE 1 -#define DMLC_LOG_FATAL_THROW 0 +#define TVM_LOG_DEBUG 0 #include #include diff --git a/web/emcc/wasm_runtime.cc b/web/emcc/wasm_runtime.cc index 214c1883f874..fffdb76e6b4d 100644 --- a/web/emcc/wasm_runtime.cc +++ b/web/emcc/wasm_runtime.cc @@ -23,11 +23,7 @@ */ // configurations for the dmlc log. -#define DMLC_LOG_CUSTOMIZE 0 -#define DMLC_LOG_STACK_TRACE 0 -#define DMLC_LOG_DEBUG 0 -#define DMLC_LOG_NODATE 1 -#define DMLC_LOG_FATAL_THROW 0 +#define TVM_LOG_DEBUG 0 #include #include diff --git a/web/emcc/webgpu_runtime.cc b/web/emcc/webgpu_runtime.cc index 62b87af01774..7507fc4e056c 100644 --- a/web/emcc/webgpu_runtime.cc +++ b/web/emcc/webgpu_runtime.cc @@ -22,12 +22,48 @@ * \brief WebGPU runtime based on the TVM JS. */ -// configurations for the dmlc log. -#define DMLC_LOG_CUSTOMIZE 0 -#define DMLC_LOG_STACK_TRACE 0 -#define DMLC_LOG_DEBUG 0 -#define DMLC_LOG_NODATE 1 -#define DMLC_LOG_FATAL_THROW 0 +// configurations for tvm logging. +#define TVM_LOG_DEBUG 0 + +namespace tvm { +namespace runtime{ +namespace detail{ +// Override logging mechanism +class LogMessage { + public: + LogMessage(const std::string& file, int lineno) { + stream_ << file << ":" << lineno + << ": "; + } + ~LogMessage() { + std::cerr << stream_.str() << std::endl; + } + std::ostringstream& stream() { return stream_; } + + private: + std::ostringstream stream_; +}; + +class LogFatal { + public: + LogFatal(const std::string& file, int lineno) : file_(file), lineno_(lineno) {} + ~LogFatal() TVM_THROW_EXCEPTION { + { + LogMessage(file, lineno) << stream_.str(); + } + abort(); + } + std::ostringstream& stream() { return stream_; } + + private: + std::ostringstream stream_; + std::string file_; + int lineno_; +}; + +} +} +} // namespace dmlc #include #include From 6cd5457f16901aee5118611f445557c582e55526 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 25 Jan 2021 13:47:28 -0800 Subject: [PATCH 12/67] forgot a not... whoops --- include/tvm/runtime/logging.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index 9be8142a4f94..d2ac90bde4f2 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -310,7 +310,7 @@ TVM_CHECK_FUNC(_NE, !=) #define LOG_WARNING (::tvm::runtime::detail::LogMessage(__FILE__, __LINE__).stream() << "warning: ") #define TVM_CHECK_BINARY_OP(name, op, x, y) \ - if (::tvm::runtime::detail::LogCheck##name(x, y)) \ + if (!::tvm::runtime::detail::LogCheck##name(x, y)) \ ::tvm::runtime::detail::LogFatal(__FILE__, __LINE__).stream() \ << "Check failed: " << #x " " #op " " #y << ": " @@ -379,7 +379,7 @@ TVM_CHECK_FUNC(_NE, !=) #define TVM_ICHECK_INDENT " " #define ICHECK_BINARY_OP(name, op, x, y) \ - if (::tvm::runtime::detail::LogCheck##name(x, y)) \ + if (!::tvm::runtime::detail::LogCheck##name(x, y)) \ ::tvm::runtime::detail::LogFatal(__FILE__, __LINE__).stream() \ << ::tvm::runtime::detail::kTVM_INTERNAL_ERROR_MESSAGE << std::endl \ << TVM_ICHECK_INDENT << "Check failed: " << #x " " #op " " #y << ": " From 0b54636b0ff3e00f8e334fa837a4a612c043d01f Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 25 Jan 2021 14:28:03 -0800 Subject: [PATCH 13/67] allow user to override logging with TVM_LOG_CUSTOMIZE --- apps/ios_rpc/tvmrpc/TVMRuntime.mm | 30 ++++++-------------------- include/tvm/runtime/logging.h | 36 ++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/apps/ios_rpc/tvmrpc/TVMRuntime.mm b/apps/ios_rpc/tvmrpc/TVMRuntime.mm index 0d0b49b3cbdf..409d8415bdb1 100644 --- a/apps/ios_rpc/tvmrpc/TVMRuntime.mm +++ b/apps/ios_rpc/tvmrpc/TVMRuntime.mm @@ -57,31 +57,13 @@ namespace runtime{ namespace detail{ // Override logging mechanism -class LogFatal { - public: - LogFatal(const std::string& file, int lineno) : file_(file), lineno_(lineno) {} - ~LogFatal() TVM_THROW_EXCEPTION { throw InternalError(file_, lineno_, stream_.str()); } - std::ostringstream& stream() { return stream_; } - - private: - std::ostringstream stream_; - std::string file_; - int lineno_; -}; - -class LogMessage { - public: - LogMessage(const std::string& file, int lineno) { - std::time_t t = std::time(nullptr); - stream_ << "[" << std::put_time(std::localtime(&t), "%H:%M:%S") << "] " << file << ":" << lineno - << ": "; - } - ~LogMessage() { NSLog(@"%s", stream_.str().c_str(); } - std::ostringstream& stream() { return stream_; } +static LogFatalImpl(const std::string& file, int lineno, const std::string& message) { + throw tvm::runtime::InternalError(file, lineno, message); +} - private: - std::ostringstream stream_; -}; +static LogMessageImpl(const std::string& file, int lineno, const std::string& message) { + NSLog(@"%s", stream_.str().c_str(); + } } } } // namespace dmlc diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index d2ac90bde4f2..93caf997ac5a 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -207,10 +207,6 @@ namespace detail { /*! \brief Class to accumulate an error message and throw it. Do not use * directly, instead use LOG(FATAL). - * - * If TVM_LOG_CUSTOMIZE is defined, then the user must provide their own - * implementation of LogFatal. This implementation will then be used in place - * of the default one. */ class LogFatal { public: @@ -226,10 +222,6 @@ class LogFatal { /*! \brief Class to accumulate an log message. Do not use directly, instead use * LOG(INFO), LOG(WARNING), LOG(ERROR). - * - * If TVM_LOG_CUSTOMIZE is defined, then the user must provide their own - * implementation of LogMessage. This implementation will then be used in place - * of the default one. */ class LogMessage { public: @@ -244,6 +236,34 @@ class LogMessage { private: std::ostringstream stream_; }; +#else +// Custom implementations of LogFatal and LogMessage that allow the user to +// override handling of the message. The user must implement LogFatalImpl and LogMessageImpl +static void LogFatalImpl(const std::string& file, int lineno, const std::string& message); +class LogFatal { + public: + LogFatal(const std::string& file, int lineno) : file_(file), lineno_(lineno) {} + ~LogFatal() TVM_THROW_EXCEPTION { LogFatalImpl(file, lineno, stream_.str()); } + std::ostringstream& stream() { return stream_; } + + private: + std::ostringstream stream_; + std::string file_; + int lineno_; +}; + +static void LogMessageImpl(const std::string& file, int lineno, const std::string& message); +class LogMessage { + public: + LogMessage(const std::string& file, int lineno) { + LogMessageImpl(file, lineno, stream_.str()); + } + ~LogMessage() { std::cerr << stream_.str() << std::endl; } + std::ostringstream& stream() { return stream_; } + + private: + std::ostringstream stream_; +}; #endif // Below is from dmlc-core From 8d6b00c311cc70769bb0d1dd656764a63f1ffb3a Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 25 Jan 2021 14:59:47 -0800 Subject: [PATCH 14/67] ios_rpc builds --- apps/ios_rpc/tvmrpc.xcodeproj/project.pbxproj | 6 ++++++ apps/ios_rpc/tvmrpc/TVMRuntime.mm | 6 +++--- include/tvm/runtime/logging.h | 15 +++++++++------ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/apps/ios_rpc/tvmrpc.xcodeproj/project.pbxproj b/apps/ios_rpc/tvmrpc.xcodeproj/project.pbxproj index b33c892cf002..b723fa69aa3f 100644 --- a/apps/ios_rpc/tvmrpc.xcodeproj/project.pbxproj +++ b/apps/ios_rpc/tvmrpc.xcodeproj/project.pbxproj @@ -349,6 +349,8 @@ GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", "$(inherited)", + "DMLC_USE_LOGGING_LIBRARY=", + "TVM_BACKTRACE_DISABLED=1", ); GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; @@ -393,6 +395,10 @@ ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_NO_COMMON_BLOCKS = YES; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DMLC_USE_LOGGING_LIBRARY=", + "TVM_BACKTRACE_DISABLE=1", + ); GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; diff --git a/apps/ios_rpc/tvmrpc/TVMRuntime.mm b/apps/ios_rpc/tvmrpc/TVMRuntime.mm index 409d8415bdb1..67b1ee7d44b1 100644 --- a/apps/ios_rpc/tvmrpc/TVMRuntime.mm +++ b/apps/ios_rpc/tvmrpc/TVMRuntime.mm @@ -57,12 +57,12 @@ namespace runtime{ namespace detail{ // Override logging mechanism -static LogFatalImpl(const std::string& file, int lineno, const std::string& message) { +void LogFatalImpl(const std::string& file, int lineno, const std::string& message) { throw tvm::runtime::InternalError(file, lineno, message); } -static LogMessageImpl(const std::string& file, int lineno, const std::string& message) { - NSLog(@"%s", stream_.str().c_str(); +void LogMessageImpl(const std::string& file, int lineno, const std::string& message) { + NSLog(@"%s:%d: %s", file.c_str(), lineno, message.c_str()); } } } diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index 93caf997ac5a..e0e1dbc7fd29 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -239,11 +239,11 @@ class LogMessage { #else // Custom implementations of LogFatal and LogMessage that allow the user to // override handling of the message. The user must implement LogFatalImpl and LogMessageImpl -static void LogFatalImpl(const std::string& file, int lineno, const std::string& message); +void LogFatalImpl(const std::string& file, int lineno, const std::string& message); class LogFatal { public: LogFatal(const std::string& file, int lineno) : file_(file), lineno_(lineno) {} - ~LogFatal() TVM_THROW_EXCEPTION { LogFatalImpl(file, lineno, stream_.str()); } + ~LogFatal() TVM_THROW_EXCEPTION { LogFatalImpl(file_, lineno_, stream_.str()); } std::ostringstream& stream() { return stream_; } private: @@ -252,16 +252,19 @@ class LogFatal { int lineno_; }; -static void LogMessageImpl(const std::string& file, int lineno, const std::string& message); +void LogMessageImpl(const std::string& file, int lineno, const std::string& message); class LogMessage { public: - LogMessage(const std::string& file, int lineno) { - LogMessageImpl(file, lineno, stream_.str()); + LogMessage(const std::string& file, int lineno):file_(file), lineno_(lineno) { + } + ~LogMessage() { + LogMessageImpl(file_, lineno_, stream_.str()); } - ~LogMessage() { std::cerr << stream_.str() << std::endl; } std::ostringstream& stream() { return stream_; } private: + std::string file_; + int lineno_; std::ostringstream stream_; }; #endif From 3896ed5f6f529ce307108e0e51f286e106b0642a Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 25 Jan 2021 15:09:24 -0800 Subject: [PATCH 15/67] formatting --- apps/ios_rpc/tvmrpc/TVMRuntime.mm | 8 ++++---- include/tvm/runtime/logging.h | 13 +++++-------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/apps/ios_rpc/tvmrpc/TVMRuntime.mm b/apps/ios_rpc/tvmrpc/TVMRuntime.mm index 67b1ee7d44b1..87cb6f9b4c69 100644 --- a/apps/ios_rpc/tvmrpc/TVMRuntime.mm +++ b/apps/ios_rpc/tvmrpc/TVMRuntime.mm @@ -54,16 +54,16 @@ #include "../../../src/runtime/contrib/coreml/coreml_runtime.mm" namespace tvm { -namespace runtime{ -namespace detail{ +namespace runtime { +namespace detail { // Override logging mechanism void LogFatalImpl(const std::string& file, int lineno, const std::string& message) { throw tvm::runtime::InternalError(file, lineno, message); } void LogMessageImpl(const std::string& file, int lineno, const std::string& message) { - NSLog(@"%s:%d: %s", file.c_str(), lineno, message.c_str()); - } + NSLog(@"%s:%d: %s", file.c_str(), lineno, message.c_str()); +} } } } // namespace dmlc diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index e0e1dbc7fd29..cc61bbc884ae 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -143,7 +143,7 @@ inline std::string Backtrace() { return ""; } #endif /*! \brief Base error type for TVM. Wraps a string message. */ -class Error : public ::dmlc::Error { // for backwards compatibility +class Error : public ::dmlc::Error { // for backwards compatibility public: /*! \brief Construct an error. * \param s The message to be displayed with the error. @@ -255,11 +255,8 @@ class LogFatal { void LogMessageImpl(const std::string& file, int lineno, const std::string& message); class LogMessage { public: - LogMessage(const std::string& file, int lineno):file_(file), lineno_(lineno) { - } - ~LogMessage() { - LogMessageImpl(file_, lineno_, stream_.str()); - } + LogMessage(const std::string& file, int lineno) : file_(file), lineno_(lineno) {} + ~LogMessage() { LogMessageImpl(file_, lineno_, stream_.str()); } std::ostringstream& stream() { return stream_; } private: @@ -333,7 +330,7 @@ TVM_CHECK_FUNC(_NE, !=) #define LOG_WARNING (::tvm::runtime::detail::LogMessage(__FILE__, __LINE__).stream() << "warning: ") #define TVM_CHECK_BINARY_OP(name, op, x, y) \ - if (!::tvm::runtime::detail::LogCheck##name(x, y)) \ + if (!::tvm::runtime::detail::LogCheck##name(x, y)) \ ::tvm::runtime::detail::LogFatal(__FILE__, __LINE__).stream() \ << "Check failed: " << #x " " #op " " #y << ": " @@ -402,7 +399,7 @@ TVM_CHECK_FUNC(_NE, !=) #define TVM_ICHECK_INDENT " " #define ICHECK_BINARY_OP(name, op, x, y) \ - if (!::tvm::runtime::detail::LogCheck##name(x, y)) \ + if (!::tvm::runtime::detail::LogCheck##name(x, y)) \ ::tvm::runtime::detail::LogFatal(__FILE__, __LINE__).stream() \ << ::tvm::runtime::detail::kTVM_INTERNAL_ERROR_MESSAGE << std::endl \ << TVM_ICHECK_INDENT << "Check failed: " << #x " " #op " " #y << ": " From 05c2984e068fa93b6f118b15bf969f370cfe2c0f Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 25 Jan 2021 15:55:26 -0800 Subject: [PATCH 16/67] fix android build --- .../app/src/main/jni/Application.mk | 2 +- .../app/src/main/jni/tvm_runtime.h | 47 +++++-------------- .../app/src/main/jni/Application.mk | 2 +- .../app/src/main/jni/Application.mk | 2 +- .../app/src/main/jni/tvm_runtime.h | 46 +++++------------- apps/ios_rpc/tvmrpc.xcodeproj/project.pbxproj | 2 +- 6 files changed, 30 insertions(+), 71 deletions(-) diff --git a/apps/android_camera/app/src/main/jni/Application.mk b/apps/android_camera/app/src/main/jni/Application.mk index 63a79458ef94..cf39c049f043 100644 --- a/apps/android_camera/app/src/main/jni/Application.mk +++ b/apps/android_camera/app/src/main/jni/Application.mk @@ -31,7 +31,7 @@ include $(config) APP_ABI ?= all APP_STL := c++_shared -APP_CPPFLAGS += -DDMLC_LOG_STACK_TRACE=0 -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti +APP_CPPFLAGS += -DDMLC_USE_LOGGING_LIBRARY=\ -DTVM_BACKTRACE_DISABLED=1 -DTVM_LOG_CUTOMIZE=1 -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti ifeq ($(USE_OPENCL), 1) APP_CPPFLAGS += -DTVM_OPENCL_RUNTIME=1 endif diff --git a/apps/android_camera/app/src/main/jni/tvm_runtime.h b/apps/android_camera/app/src/main/jni/tvm_runtime.h index 0ab993a37a26..1324364be4aa 100644 --- a/apps/android_camera/app/src/main/jni/tvm_runtime.h +++ b/apps/android_camera/app/src/main/jni/tvm_runtime.h @@ -66,41 +66,20 @@ #include - namespace tvm { -namespace runtime{ -namespace detail{ +namespace runtime { +namespace detail { // Override logging mechanism -class LogFatal { - public: - LogFatal(const std::string& file, int lineno) : file_(file), lineno_(lineno) {} - ~LogFatal() TVM_THROW_EXCEPTION { - __android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", stream_.str().c_str()); - throw InternalError(file_, lineno_, stream_.str()); } - std::ostringstream& stream() { return stream_; } - - private: - std::ostringstream stream_; - std::string file_; - int lineno_; -}; - -class LogMessage { - public: - LogMessage(const std::string& file, int lineno) { - std::time_t t = std::time(nullptr); - stream_ << "[" << std::put_time(std::localtime(&t), "%H:%M:%S") << "] " << file << ":" << lineno - << ": "; - } - ~LogMessage() { - - __android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", stream_.str().c_str()); - } - std::ostringstream& stream() { return stream_; } - - private: - std::ostringstream stream_; -}; +void LogFatalImpl(const std::string& file, int lineno, const std::string& message) { + std::string m = file + ":" + std::to_string(lineno) + ": " + message; + __android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", m.c_str()); + throw InternalError(file, lineno, message); } +void LogMessageImpl(const std::string& file, int lineno, const std::string& message) { + std::string m = file + ":" + std::to_string(lineno) + ": " + message; + __android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", m.c_str()); } -} // namespace dmlc + +} // namespace detail +} // namespace runtime +} // namespace tvm diff --git a/apps/android_deploy/app/src/main/jni/Application.mk b/apps/android_deploy/app/src/main/jni/Application.mk index a50a40bf5cd1..ca21f5482f5b 100644 --- a/apps/android_deploy/app/src/main/jni/Application.mk +++ b/apps/android_deploy/app/src/main/jni/Application.mk @@ -27,7 +27,7 @@ include $(config) APP_STL := c++_static -APP_CPPFLAGS += -DDMLC_LOG_STACK_TRACE=0 -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti +APP_CPPFLAGS += -DDMLC_USE_LOGGING_LIBRARY=\ -DTVM_BACKTRACE_DISABLED=1 -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti ifeq ($(USE_OPENCL), 1) APP_CPPFLAGS += -DTVM_OPENCL_RUNTIME=1 endif diff --git a/apps/android_rpc/app/src/main/jni/Application.mk b/apps/android_rpc/app/src/main/jni/Application.mk index 5f885f1c6f14..be22434e760d 100644 --- a/apps/android_rpc/app/src/main/jni/Application.mk +++ b/apps/android_rpc/app/src/main/jni/Application.mk @@ -31,7 +31,7 @@ include $(config) APP_ABI ?= armeabi-v7a arm64-v8a x86 x86_64 mips APP_STL := c++_shared -APP_CPPFLAGS += -DDMLC_LOG_STACK_TRACE=0 -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti +APP_CPPFLAGS += -DDMLC_USE_LOGGING_LIBRARY=\ -DTVM_BACKTRACE_DISABLED=1 -DTVM_LOG_CUTOMIZE=1 -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti ifeq ($(USE_OPENCL), 1) APP_CPPFLAGS += -DTVM_OPENCL_RUNTIME=1 endif diff --git a/apps/android_rpc/app/src/main/jni/tvm_runtime.h b/apps/android_rpc/app/src/main/jni/tvm_runtime.h index fbc1aaf46445..af5af779982f 100644 --- a/apps/android_rpc/app/src/main/jni/tvm_runtime.h +++ b/apps/android_rpc/app/src/main/jni/tvm_runtime.h @@ -76,39 +76,19 @@ #include namespace tvm { -namespace runtime{ -namespace detail{ +namespace runtime { +namespace detail { // Override logging mechanism -class LogFatal { - public: - LogFatal(const std::string& file, int lineno) : file_(file), lineno_(lineno) {} - ~LogFatal() TVM_THROW_EXCEPTION { - __android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", stream_.str().c_str()); - throw InternalError(file_, lineno_, stream_.str()); } - std::ostringstream& stream() { return stream_; } - - private: - std::ostringstream stream_; - std::string file_; - int lineno_; -}; - -class LogMessage { - public: - LogMessage(const std::string& file, int lineno) { - std::time_t t = std::time(nullptr); - stream_ << "[" << std::put_time(std::localtime(&t), "%H:%M:%S") << "] " << file << ":" << lineno - << ": "; - } - ~LogMessage() { - - __android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", stream_.str().c_str()); - } - std::ostringstream& stream() { return stream_; } - - private: - std::ostringstream stream_; -}; +void LogFatalImpl(const std::string& file, int lineno, const std::string& message) { + std::string m = file + ":" + std::to_string(lineno) + ": " + message; + __android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", m.c_str()); + throw InternalError(file, lineno, message); } +void LogMessageImpl(const std::string& file, int lineno, const std::string& message) { + std::string m = file + ":" + std::to_string(lineno) + ": " + message; + __android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", m.c_str()); } -} // namespace dmlc + +} // namespace detail +} // namespace runtime +} // namespace tvm diff --git a/apps/ios_rpc/tvmrpc.xcodeproj/project.pbxproj b/apps/ios_rpc/tvmrpc.xcodeproj/project.pbxproj index b723fa69aa3f..28079e710a38 100644 --- a/apps/ios_rpc/tvmrpc.xcodeproj/project.pbxproj +++ b/apps/ios_rpc/tvmrpc.xcodeproj/project.pbxproj @@ -397,7 +397,7 @@ GCC_NO_COMMON_BLOCKS = YES; GCC_PREPROCESSOR_DEFINITIONS = ( "DMLC_USE_LOGGING_LIBRARY=", - "TVM_BACKTRACE_DISABLE=1", + "TVM_BACKTRACE_DISABLED=1", ); GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; From c80610ca6c3368b367c0c6e134c886fdc9c92149 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 25 Jan 2021 16:19:45 -0800 Subject: [PATCH 17/67] fix emcc --- web/emcc/tvmjs_support.cc | 2 ++ web/emcc/wasm_runtime.cc | 4 ++- web/emcc/webgpu_runtime.cc | 57 ++++++++++++-------------------------- 3 files changed, 22 insertions(+), 41 deletions(-) diff --git a/web/emcc/tvmjs_support.cc b/web/emcc/tvmjs_support.cc index e60d84979d83..12f930f491a5 100644 --- a/web/emcc/tvmjs_support.cc +++ b/web/emcc/tvmjs_support.cc @@ -26,6 +26,8 @@ // configurations for the dmlc log. #define TVM_LOG_DEBUG 0 +#define DMLC_USE_LOGGING_LIBRARY +#define TVM_BACKTRACE_DISABLED 1 #include #include diff --git a/web/emcc/wasm_runtime.cc b/web/emcc/wasm_runtime.cc index fffdb76e6b4d..0b14ef6476d2 100644 --- a/web/emcc/wasm_runtime.cc +++ b/web/emcc/wasm_runtime.cc @@ -24,9 +24,11 @@ // configurations for the dmlc log. #define TVM_LOG_DEBUG 0 +#define DMLC_USE_LOGGING_LIBRARY +#define TVM_BACKTRACE_DISABLED 1 -#include #include +#include #include "src/runtime/c_runtime_api.cc" #include "src/runtime/cpu_device_api.cc" diff --git a/web/emcc/webgpu_runtime.cc b/web/emcc/webgpu_runtime.cc index 7507fc4e056c..01e42ef3faa8 100644 --- a/web/emcc/webgpu_runtime.cc +++ b/web/emcc/webgpu_runtime.cc @@ -24,46 +24,8 @@ // configurations for tvm logging. #define TVM_LOG_DEBUG 0 - -namespace tvm { -namespace runtime{ -namespace detail{ -// Override logging mechanism -class LogMessage { - public: - LogMessage(const std::string& file, int lineno) { - stream_ << file << ":" << lineno - << ": "; - } - ~LogMessage() { - std::cerr << stream_.str() << std::endl; - } - std::ostringstream& stream() { return stream_; } - - private: - std::ostringstream stream_; -}; - -class LogFatal { - public: - LogFatal(const std::string& file, int lineno) : file_(file), lineno_(lineno) {} - ~LogFatal() TVM_THROW_EXCEPTION { - { - LogMessage(file, lineno) << stream_.str(); - } - abort(); - } - std::ostringstream& stream() { return stream_; } - - private: - std::ostringstream stream_; - std::string file_; - int lineno_; -}; - -} -} -} // namespace dmlc +#define DMLC_USE_LOGGING_LIBRARY +#define TVM_BACKTRACE_DISABLED 1 #include #include @@ -71,12 +33,27 @@ class LogFatal { #include #include +#include +#include + #include "../../src/runtime/meta_data.h" #include "../../src/runtime/vulkan/vulkan_shader.h" #include "../../src/runtime/workspace_pool.h" namespace tvm { namespace runtime { +namespace detail { +// Override logging mechanism +void LogFatalImpl(const std::string& file, int lineno, const std::string& message) { + std::cerr << file << ":" << lineno << ": " << message << std::endl; + abort(); +} + +void LogMessageImpl(const std::string& file, int lineno, const std::string& message) { + std::cerr << file << ":" << lineno << ": " << message << std::endl; +} + +} // namespace detail /*! \brief Thread local workspace */ class WebGPUThreadEntry { From 4abe9027293b4c44bb731675cc72e523deb07f3d Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Tue, 26 Jan 2021 11:10:02 -0800 Subject: [PATCH 18/67] fix cmake --- CMakeLists.txt | 8 +++++--- cmake/modules/Libbacktrace.cmake | 2 -- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ce5b4cb3e1ee..65b657be28c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -401,11 +401,13 @@ if(USE_LIBBACKTRACE) include(cmake/modules/Libbacktrace.cmake) target_link_libraries(tvm PRIVATE libbacktrace) target_link_libraries(tvm_runtime PRIVATE libbacktrace) - # pre 3.11 versions of cmake cannot propagate include directories from imported targets so we set them manually - target_include_directories(tvm_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") add_dependencies(tvm_objs libbacktrace) - target_include_directories(tvm_runtime_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") add_dependencies(tvm_runtime_objs libbacktrace) + # pre 3.12 versions of cmake cannot propagate include directories from imported targets so we set them manually + target_include_directories(tvm PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") + target_include_directories(tvm_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") + target_include_directories(tvm_runtime PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") + target_include_directories(tvm_runtime_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") else() add_definitions(-DTVM_BACKTRACE_DISABLED) endif() diff --git a/cmake/modules/Libbacktrace.cmake b/cmake/modules/Libbacktrace.cmake index 032f0ef892c3..8ef02b30b6d5 100644 --- a/cmake/modules/Libbacktrace.cmake +++ b/cmake/modules/Libbacktrace.cmake @@ -50,5 +50,3 @@ set_property(TARGET libbacktrace PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/lib/libbacktrace.a) # create include directory so cmake doesn't complain file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include) -target_include_directories(libbacktrace - INTERFACE $) From 5aa2e85b5e489f3cfd83934bb216787658d33558 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Tue, 26 Jan 2021 11:38:13 -0800 Subject: [PATCH 19/67] fix vitis_ai --- src/runtime/contrib/vitis_ai/vitis_ai_runtime.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/runtime/contrib/vitis_ai/vitis_ai_runtime.cc b/src/runtime/contrib/vitis_ai/vitis_ai_runtime.cc index 37dc767d31af..0e5e2ce4c4fa 100755 --- a/src/runtime/contrib/vitis_ai/vitis_ai_runtime.cc +++ b/src/runtime/contrib/vitis_ai/vitis_ai_runtime.cc @@ -25,6 +25,7 @@ #include +#include #include #include #include From d4dd7d892e4d8851f6ece567dfdf12e0cd197d1a Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Wed, 27 Jan 2021 10:55:50 -0800 Subject: [PATCH 20/67] fix error message format. fix clang warnings --- include/tvm/runtime/logging.h | 11 ++++++++--- src/runtime/logging.cc | 6 ++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index cc61bbc884ae..2661a2a3e98a 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -173,10 +173,15 @@ class InternalError : public Error { time_(time), backtrace_(backtrace) { std::ostringstream s; + // XXX: Do not change this format, otherwise all error handling in python will break (because it + // parses the message to reconstruct the error type). + // TODO(tkonolige): Convert errors to Objects, so we can avoid the mess of formatting/parsing + // error messages correctly. s << "[" << std::put_time(std::localtime(&time), "%H:%M:%S") << "] " << file << ":" << lineno - << ": " << std::endl - << backtrace << std::endl - << message << std::endl; + << ": " << message << std::endl; + if (backtrace.size() > 0) { + s << backtrace << std::endl; + } full_message_ = s.str(); } /*! \return The file in which the error occurred. */ diff --git a/src/runtime/logging.cc b/src/runtime/logging.cc index 836c0280f77c..638c4488dbe4 100644 --- a/src/runtime/logging.cc +++ b/src/runtime/logging.cc @@ -45,7 +45,7 @@ std::string DemangleName(const char* name) { int status = 0; size_t length = std::string::npos; std::unique_ptr demangled_name = { - abi::__cxa_demangle(name, 0, &length, &status), &std::free}; + abi::__cxa_demangle(name, nullptr, &length, &status), &std::free}; if (demangled_name && status == 0 && length > 0) { return demangled_name.get(); } else { @@ -109,7 +109,9 @@ std::string Backtrace() { BacktraceInfo bt; bt.max_size = 100; if (_backtrace_state == nullptr) { - _backtrace_state = backtrace_create_state(NULL, 1, BacktraceErrorCallback, &bt); + // TVM is loaded as a shared library into python, so we cannot supply an + // executable path to libbacktrace. + _backtrace_state = backtrace_create_state(nullptr, 1, BacktraceErrorCallback, &bt); } backtrace_full(_backtrace_state, 0, BacktraceFullCallback, BacktraceErrorCallback, &bt); From 2105e3f8d26999b3a63476eb80a5159158081450 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Thu, 28 Jan 2021 09:44:07 -0800 Subject: [PATCH 21/67] fixes for older cmake --- cmake/modules/Libbacktrace.cmake | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmake/modules/Libbacktrace.cmake b/cmake/modules/Libbacktrace.cmake index 8ef02b30b6d5..964ec80e8cf4 100644 --- a/cmake/modules/Libbacktrace.cmake +++ b/cmake/modules/Libbacktrace.cmake @@ -24,14 +24,13 @@ ExternalProject_Add(project_libbacktrace GIT_REPOSITORY https://github.com/ianlancetaylor/libbacktrace.git GIT_TAG ${TAG} CONFIGURE_COMMAND "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/src/project_libbacktrace/configure" - "--prefix=${CMAKE_CURRENT_BINARY_DIR}/libbacktrace" + "--prefix=${CMAKE_CURRENT_BINARY_DIR}/libbacktrace" --with-pic INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace" BUILD_COMMAND make INSTALL_COMMAND make install BUILD_BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/lib/libbacktrace.a" "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include/backtrace.h" # disable the builtin update because it rebuilds on every build - UPDATE_DISCONNECTED ON UPDATE_COMMAND "" # libbacktrace has a bug on macOS with shared libraries. PATCH_COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/libbacktrace_macos.patch From 0a83a2f6a8f715b9714a1b51d8d8c621876bff0d Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Fri, 29 Jan 2021 09:46:55 -0800 Subject: [PATCH 22/67] apply patch after update --- cmake/modules/Libbacktrace.cmake | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmake/modules/Libbacktrace.cmake b/cmake/modules/Libbacktrace.cmake index 964ec80e8cf4..0dbaa50cf65f 100644 --- a/cmake/modules/Libbacktrace.cmake +++ b/cmake/modules/Libbacktrace.cmake @@ -22,7 +22,7 @@ set(TAG dedbe13fda00253fe5d4f2fb812c909729ed5937) ExternalProject_Add(project_libbacktrace PREFIX libbacktrace GIT_REPOSITORY https://github.com/ianlancetaylor/libbacktrace.git - GIT_TAG ${TAG} + GIT_TAG "${TAG}" CONFIGURE_COMMAND "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/src/project_libbacktrace/configure" "--prefix=${CMAKE_CURRENT_BINARY_DIR}/libbacktrace" --with-pic INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace" @@ -40,7 +40,8 @@ ExternalProject_Add(project_libbacktrace ExternalProject_Add_Step(project_libbacktrace update-new DEPENDERS configure DEPENDS "${CMAKE_CURRENT_LIST_DIR}/Libbacktrace.cmake" - COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/src/project_libbacktrace; git checkout -f ${TAG} + COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/src/project_libbacktrace; git checkout -f ${TAG}; + git apply ${CMAKE_CURRENT_LIST_DIR}/libbacktrace_macos.patch ) add_library(libbacktrace STATIC IMPORTED) From 7d9e3b5a62997dc3b30d034446a6ac42159f46ae Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Fri, 29 Jan 2021 11:15:16 -0800 Subject: [PATCH 23/67] missing logging header --- src/relay/transforms/memory_alloc.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/relay/transforms/memory_alloc.cc b/src/relay/transforms/memory_alloc.cc index b8c87909a025..f75b7ba1fc75 100644 --- a/src/relay/transforms/memory_alloc.cc +++ b/src/relay/transforms/memory_alloc.cc @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include From 34eed0b3b882528b645c064ca82cada95c868560 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 1 Feb 2021 09:44:52 -0800 Subject: [PATCH 24/67] set defines for cython --- python/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/setup.py b/python/setup.py index e02369e97777..b47e5b14f6a7 100644 --- a/python/setup.py +++ b/python/setup.py @@ -94,7 +94,7 @@ def config_cython(): subdir = "_cy2" ret = [] path = "tvm/_ffi/_cython" - extra_compile_args = ["-std=c++14"] + extra_compile_args = ["-std=c++14", "-DDMLC_USE_LOGGING_LIBRARY="] if os.name == "nt": library_dirs = ["tvm", "../build/Release", "../build"] libraries = ["tvm"] From ce3942ae19b4c9df7059276b23ffb7c3a516fd2b Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 1 Feb 2021 09:45:03 -0800 Subject: [PATCH 25/67] specify debug build for testing --- tests/scripts/task_build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/scripts/task_build.sh b/tests/scripts/task_build.sh index d8e35ebd4de3..b8c2c780553f 100755 --- a/tests/scripts/task_build.sh +++ b/tests/scripts/task_build.sh @@ -16,4 +16,4 @@ # specific language governing permissions and limitations # under the License. export VTA_HW_PATH=`pwd`/3rdparty/vta-hw -cd $1 && cmake .. && make $2 && cd .. +cd $1 && cmake .. -DCMAKE_BUILD_TYPE=Debug && make $2 && cd .. From c51ebfa225aa65f1683ebe596425775540d9467c Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 1 Feb 2021 11:15:27 -0800 Subject: [PATCH 26/67] disable builtin patch step --- cmake/modules/Libbacktrace.cmake | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cmake/modules/Libbacktrace.cmake b/cmake/modules/Libbacktrace.cmake index 0dbaa50cf65f..855050618118 100644 --- a/cmake/modules/Libbacktrace.cmake +++ b/cmake/modules/Libbacktrace.cmake @@ -32,14 +32,13 @@ ExternalProject_Add(project_libbacktrace "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include/backtrace.h" # disable the builtin update because it rebuilds on every build UPDATE_COMMAND "" - # libbacktrace has a bug on macOS with shared libraries. - PATCH_COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/libbacktrace_macos.patch ) # Only rebuild libbacktrace if this file changes +# libbacktrace has a bug on macOS with shared libraries, so we patch it here ExternalProject_Add_Step(project_libbacktrace update-new DEPENDERS configure - DEPENDS "${CMAKE_CURRENT_LIST_DIR}/Libbacktrace.cmake" + DEPENDS "${CMAKE_CURRENT_LIST_DIR}/Libbacktrace.cmake" "${CMAKE_CURRENT_LIST_DIR}/libbacktrace_macos.patch" COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/src/project_libbacktrace; git checkout -f ${TAG}; git apply ${CMAKE_CURRENT_LIST_DIR}/libbacktrace_macos.patch ) From 0323a73b86a059a81552d6cf9077837cb1d73f4c Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 1 Feb 2021 11:33:26 -0800 Subject: [PATCH 27/67] more robust patching --- cmake/modules/Libbacktrace.cmake | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cmake/modules/Libbacktrace.cmake b/cmake/modules/Libbacktrace.cmake index 855050618118..33280753d661 100644 --- a/cmake/modules/Libbacktrace.cmake +++ b/cmake/modules/Libbacktrace.cmake @@ -36,11 +36,14 @@ ExternalProject_Add(project_libbacktrace # Only rebuild libbacktrace if this file changes # libbacktrace has a bug on macOS with shared libraries, so we patch it here -ExternalProject_Add_Step(project_libbacktrace update-new +ExternalProject_Add_Step(project_libbacktrace checkout DEPENDERS configure + DEPENDEES download DEPENDS "${CMAKE_CURRENT_LIST_DIR}/Libbacktrace.cmake" "${CMAKE_CURRENT_LIST_DIR}/libbacktrace_macos.patch" - COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/src/project_libbacktrace; git checkout -f ${TAG}; - git apply ${CMAKE_CURRENT_LIST_DIR}/libbacktrace_macos.patch + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/src/project_libbacktrace + COMMAND git checkout -f ${TAG}; + COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/libbacktrace_macos.patch + COMMENT "update and patch" ) add_library(libbacktrace STATIC IMPORTED) From 36640eaaacf9538e07391e73cc674f28320e31a3 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Tue, 2 Feb 2021 15:44:10 -0800 Subject: [PATCH 28/67] more dmlc logging defines --- apps/dso_plugin_module/Makefile | 3 ++- apps/extension/Makefile | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/dso_plugin_module/Makefile b/apps/dso_plugin_module/Makefile index c2ce3306870a..438d9db223a8 100644 --- a/apps/dso_plugin_module/Makefile +++ b/apps/dso_plugin_module/Makefile @@ -19,7 +19,8 @@ TVM_ROOT=$(shell cd ../..; pwd) PKG_CFLAGS = -std=c++14 -O2 -fPIC\ -I${TVM_ROOT}/include\ -I${TVM_ROOT}/3rdparty/dmlc-core/include\ - -I${TVM_ROOT}/3rdparty/dlpack/include + -I${TVM_ROOT}/3rdparty/dlpack/include\ + -DDMLC_USE_LOGGING_LIBRARY=\ PKG_LDFLAGS =-L${TVM_ROOT}/build UNAME_S := $(shell uname -s) diff --git a/apps/extension/Makefile b/apps/extension/Makefile index 91d914aba63b..6eba941f7c98 100644 --- a/apps/extension/Makefile +++ b/apps/extension/Makefile @@ -20,7 +20,8 @@ TVM_ROOT=$(shell cd ../..; pwd) PKG_CFLAGS = -std=c++14 -O2 -fPIC\ -I${TVM_ROOT}/include\ -I${TVM_ROOT}/3rdparty/dmlc-core/include\ - -I${TVM_ROOT}/3rdparty/dlpack/include + -I${TVM_ROOT}/3rdparty/dlpack/include\ + -DDMLC_USE_LOGGING_LIBRARY=\ PKG_LDFLAGS =-L${TVM_ROOT}/build From 56584b98b3c1e5076e29134e7a1587dcf8ffe6b6 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 8 Feb 2021 11:42:44 -0800 Subject: [PATCH 29/67] address comments --- CMakeLists.txt | 22 ++++++++++++++----- cmake/modules/Libbacktrace.cmake | 5 ++++- include/tvm/auto_scheduler/loop_state.h | 0 include/tvm/auto_scheduler/transform_step.h | 0 include/tvm/runtime/logging.h | 2 +- src/runtime/logging.cc | 17 +++++++++----- tests/scripts/task_build.sh | 2 +- tutorials/auto_scheduler/tune_network_mali.py | 2 +- tutorials/auto_scheduler/tune_network_x86.py | 2 +- 9 files changed, 36 insertions(+), 16 deletions(-) mode change 100644 => 100755 include/tvm/auto_scheduler/loop_state.h mode change 100644 => 100755 include/tvm/auto_scheduler/transform_step.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 65b657be28c4..af67f5683398 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -93,7 +93,6 @@ include_directories(${CMAKE_INCLUDE_PATH}) include_directories("include") include_directories(SYSTEM ${DLPACK_PATH}) include_directories(SYSTEM ${DMLC_PATH}) -add_definitions(-DDMLC_USE_LOGGING_LIBRARY=) include_directories(SYSTEM ${RANG_PATH}) include_directories(SYSTEM ${COMPILER_RT_PATH}) include_directories(SYSTEM ${PICOJSON_PATH}) @@ -396,20 +395,24 @@ set_property(TARGET tvm APPEND PROPERTY LINK_OPTIONS "${TVM_VISIBILITY_FLAG}") add_library(tvm_runtime SHARED $) set_property(TARGET tvm_runtime APPEND PROPERTY LINK_OPTIONS "${TVM_VISIBILITY_FLAG}") +target_compile_definitions(tvm_objs PUBLIC DMLC_USE_LOGGING_LIBRARY=) +target_compile_definitions(tvm_runtime_objs PUBLIC DMLC_USE_LOGGING_LIBRARY=) +target_compile_definitions(tvm PUBLIC DMLC_USE_LOGGING_LIBRARY=) +target_compile_definitions(tvm_runtime PUBLIC DMLC_USE_LOGGING_LIBRARY=) if(USE_LIBBACKTRACE) message(STATUS "Building with libbacktrace...") include(cmake/modules/Libbacktrace.cmake) target_link_libraries(tvm PRIVATE libbacktrace) target_link_libraries(tvm_runtime PRIVATE libbacktrace) - add_dependencies(tvm_objs libbacktrace) - add_dependencies(tvm_runtime_objs libbacktrace) + target_link_libraries(tvm_runtime_objs libbacktrace) # pre 3.12 versions of cmake cannot propagate include directories from imported targets so we set them manually target_include_directories(tvm PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") target_include_directories(tvm_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") target_include_directories(tvm_runtime PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") target_include_directories(tvm_runtime_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") else() - add_definitions(-DTVM_BACKTRACE_DISABLED) + target_compile_definitions(tvm_objs PRIVATE TVM_BACKTRACE_DISABLED) + target_compile_definitions(tvm_runtime_objs PRIVATE TVM_BACKTRACE_DISABLED) endif() if(USE_MICRO) @@ -499,6 +502,7 @@ if (HIDE_PRIVATE_SYMBOLS AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin") # once minimum CMake version is bumped up to 3.13 or above. target_link_libraries(tvm PRIVATE ${HIDE_SYMBOLS_LINKER_FLAGS}) target_link_libraries(tvm_runtime PRIVATE ${HIDE_SYMBOLS_LINKER_FLAGS}) + target_compile_definitions(tvm_allvisible PUBLIC DMLC_USE_LOGGING_LIBRARY=) endif() # Tests @@ -575,7 +579,15 @@ endif() # Change relative paths in backtrace to absolute ones if(TVM_IS_DEBUG_BUILD) - target_compile_options(tvm PRIVATE "-ffile-prefix-map=..=${CMAKE_CURRENT_SOURCE_DIR}") + set(FILE_PREFIX_MAP_FLAG "-ffile-prefix-map=..=${CMAKE_CURRENT_SOURCE_DIR}") + target_compile_options(tvm PRIVATE "${FILE_PREFIX_MAP_FLAG}") + CHECK_CXX_COMPILER_FLAG("${FILE_PREFIX_MAP_FLAG}" FILE_PREFIX_MAP_SUPPORTED) + if(FILE_PREFIX_MAP_SUPPORTED) + target_compile_options(tvm PRIVATE "${FILE_PREFIX_MAP_FLAG}") + target_compile_options(tvm_objs PRIVATE "${FILE_PREFIX_MAP_FLAG}") + target_compile_options(tvm_runtime PRIVATE "${FILE_PREFIX_MAP_FLAG}") + target_compile_options(tvm_runtime_objs PRIVATE "${FILE_PREFIX_MAP_FLAG}") + endif() endif() # Run dsymutil to generate debugging symbols for backtraces diff --git a/cmake/modules/Libbacktrace.cmake b/cmake/modules/Libbacktrace.cmake index 33280753d661..a01be59bab7c 100644 --- a/cmake/modules/Libbacktrace.cmake +++ b/cmake/modules/Libbacktrace.cmake @@ -35,7 +35,10 @@ ExternalProject_Add(project_libbacktrace ) # Only rebuild libbacktrace if this file changes -# libbacktrace has a bug on macOS with shared libraries, so we patch it here +# libbacktrace has a bug on macOS with shared libraries: when looking for symbols in shared +# libraries, if any library is missing symbols, then the process stops and no symbols are used. We +# include a patch that changes libbacktrace to instead perform a best-effort lookup of symbols. This +# is a one-line change where a return is changed to a continue within the symbol lookup loop. ExternalProject_Add_Step(project_libbacktrace checkout DEPENDERS configure DEPENDEES download diff --git a/include/tvm/auto_scheduler/loop_state.h b/include/tvm/auto_scheduler/loop_state.h old mode 100644 new mode 100755 diff --git a/include/tvm/auto_scheduler/transform_step.h b/include/tvm/auto_scheduler/transform_step.h old mode 100644 new mode 100755 diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index 2661a2a3e98a..aa03fc36db4c 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -161,7 +161,7 @@ class InternalError : public Error { * \param file The file where the error occurred. * \param lineno The line number where the error occurred. * \param message The error message to display. - * \param time The time at which the error occurred. + * \param time The time at which the error occurred. This should be in local time. * \param backtrace Backtrace from when the error occurred. */ InternalError(std::string file, int lineno, std::string message, diff --git a/src/runtime/logging.cc b/src/runtime/logging.cc index 638c4488dbe4..41ba6ad22b3c 100644 --- a/src/runtime/logging.cc +++ b/src/runtime/logging.cc @@ -39,7 +39,10 @@ struct BacktraceInfo { std::string error_message; }; -static backtrace_state* _backtrace_state = nullptr; +backtrace_state** GetBacktraceState() { + thread_local backtrace_state* state = nullptr; + return &state; +} std::string DemangleName(const char* name) { int status = 0; @@ -80,7 +83,8 @@ int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int li symbol_str = DemangleName(symbol); } else { // see if syminfo gives anything - backtrace_syminfo(_backtrace_state, pc, BacktraceSyminfoCallback, BacktraceErrorCallback, + backtrace_state** bt_state = GetBacktraceState(); + backtrace_syminfo(*bt_state, pc, BacktraceSyminfoCallback, BacktraceErrorCallback, &symbol_str); } s << symbol_str; @@ -94,7 +98,7 @@ int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int li // Skip tvm::backtrace and tvm::LogFatal::~LogFatal at the beginning of the trace as they don't // add anything useful to the backtrace. if (!(stack_trace->lines.size() == 0 && - (symbol_str.find("tvm::Backtrace", 0) == 0 || symbol_str.find("tvm::LogFatal", 0) == 0))) { + (symbol_str.find("tvm::runtime::Backtrace", 0) == 0 || symbol_str.find("tvm::runtime::detail::LogFatal", 0) == 0))) { stack_trace->lines.push_back(s.str()); } // TVMFuncCall denotes the API boundary so we stop there. Exceptions should be caught there. @@ -108,12 +112,13 @@ int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int li std::string Backtrace() { BacktraceInfo bt; bt.max_size = 100; - if (_backtrace_state == nullptr) { + backtrace_state** bt_state = GetBacktraceState(); + if (*bt_state == nullptr) { // TVM is loaded as a shared library into python, so we cannot supply an // executable path to libbacktrace. - _backtrace_state = backtrace_create_state(nullptr, 1, BacktraceErrorCallback, &bt); + *bt_state = backtrace_create_state(nullptr, 1, BacktraceErrorCallback, &bt); } - backtrace_full(_backtrace_state, 0, BacktraceFullCallback, BacktraceErrorCallback, &bt); + backtrace_full(*bt_state, 0, BacktraceFullCallback, BacktraceErrorCallback, &bt); std::ostringstream s; s << "Stack trace:\n"; diff --git a/tests/scripts/task_build.sh b/tests/scripts/task_build.sh index b8c2c780553f..845b7153ae20 100755 --- a/tests/scripts/task_build.sh +++ b/tests/scripts/task_build.sh @@ -16,4 +16,4 @@ # specific language governing permissions and limitations # under the License. export VTA_HW_PATH=`pwd`/3rdparty/vta-hw -cd $1 && cmake .. -DCMAKE_BUILD_TYPE=Debug && make $2 && cd .. +cd $1 && cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo && make $2 && cd .. diff --git a/tutorials/auto_scheduler/tune_network_mali.py b/tutorials/auto_scheduler/tune_network_mali.py index 6a650228199e..2bce968771e3 100644 --- a/tutorials/auto_scheduler/tune_network_mali.py +++ b/tutorials/auto_scheduler/tune_network_mali.py @@ -329,7 +329,7 @@ def tune_and_evaluate(): # The last line also prints the total number of measurement trials, # total time spent on auto-tuning and the id of the next task to tune. # -# There will also be some "tvn::Error"s errors, because the +# There will also be some "tvm::Error"s errors, because the # auto-scheduler will try some invalid schedules. # You can safely ignore them if the tuning can continue, because these # errors are isolated from the main process. diff --git a/tutorials/auto_scheduler/tune_network_x86.py b/tutorials/auto_scheduler/tune_network_x86.py index 1cfcd7206be1..2b47c64729e0 100644 --- a/tutorials/auto_scheduler/tune_network_x86.py +++ b/tutorials/auto_scheduler/tune_network_x86.py @@ -251,7 +251,7 @@ def run_tuning(): # The last line also prints the total number of measurement trials, # total time spent on auto-tuning and the id of the next task to tune. # -# There will also be some "tvn::Error"s errors, because the +# There will also be some "tvm::Error"s errors, because the # auto-scheduler will try some invalid schedules. # You can safely ignore them if the tuning can continue, because these # errors are isolated from the main process. From e7fb96598ede4d7722f0153819afa68b68cadbf6 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Tue, 9 Feb 2021 13:49:19 -0800 Subject: [PATCH 30/67] move backtrace definition to source file --- include/tvm/runtime/logging.h | 8 -------- src/runtime/contrib/cublas/cublas_utils.h | 2 +- src/runtime/logging.cc | 14 ++++++++++---- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index aa03fc36db4c..5954f5a04de1 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -129,18 +129,10 @@ namespace tvm { namespace runtime { -#ifndef TVM_BACKTRACE_DISABLED /* \brief Generate a backtrace when called. * \return A multiline string of the backtrace. There will be either one or two lines per frame. */ std::string Backtrace(); -#else -// TODO(bkimball,tkonolige) This inline function is to work around a linking error I am having when -// using MSVC If the function definition is in logging.cc then the linker can't find it no matter -// what kind of attributes (dllexport) I decorate it with. This is temporary and will be addressed -// when we get backtrace working on Windows. -inline std::string Backtrace() { return ""; } -#endif /*! \brief Base error type for TVM. Wraps a string message. */ class Error : public ::dmlc::Error { // for backwards compatibility diff --git a/src/runtime/contrib/cublas/cublas_utils.h b/src/runtime/contrib/cublas/cublas_utils.h index 3edb8300be88..d47837987a92 100644 --- a/src/runtime/contrib/cublas/cublas_utils.h +++ b/src/runtime/contrib/cublas/cublas_utils.h @@ -68,7 +68,7 @@ inline const char* GetCublasErrorString(int error) { int error = static_cast(fn); \ ICHECK_EQ(error, CUBLAS_STATUS_SUCCESS) << "CUBLAS: " << GetCublasErrorString(error); \ } while (0) // ; intentionally left off. -#endif // CHECK_CUBLAS_ERROR +#endif // CHECK_CUBLAS_ERROR struct CuBlasThreadEntry { CuBlasThreadEntry(); diff --git a/src/runtime/logging.cc b/src/runtime/logging.cc index 41ba6ad22b3c..4f0099654650 100644 --- a/src/runtime/logging.cc +++ b/src/runtime/logging.cc @@ -17,7 +17,13 @@ * under the License. */ -#ifndef TVM_BACKTRACE_DISABLED +#ifdef TVM_BACKTRACE_DISABLED +// TODO(bkimball,tkonolige) This inline function is to work around a linking error I am having when +// using MSVC If the function definition is in logging.cc then the linker can't find it no matter +// what kind of attributes (dllexport) I decorate it with. This is temporary and will be addressed +// when we get backtrace working on Windows. +std::string tvm::runtime::Backtrace() { return ""; } +#else #include #include @@ -84,8 +90,7 @@ int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int li } else { // see if syminfo gives anything backtrace_state** bt_state = GetBacktraceState(); - backtrace_syminfo(*bt_state, pc, BacktraceSyminfoCallback, BacktraceErrorCallback, - &symbol_str); + backtrace_syminfo(*bt_state, pc, BacktraceSyminfoCallback, BacktraceErrorCallback, &symbol_str); } s << symbol_str; @@ -98,7 +103,8 @@ int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int li // Skip tvm::backtrace and tvm::LogFatal::~LogFatal at the beginning of the trace as they don't // add anything useful to the backtrace. if (!(stack_trace->lines.size() == 0 && - (symbol_str.find("tvm::runtime::Backtrace", 0) == 0 || symbol_str.find("tvm::runtime::detail::LogFatal", 0) == 0))) { + (symbol_str.find("tvm::runtime::Backtrace", 0) == 0 || + symbol_str.find("tvm::runtime::detail::LogFatal", 0) == 0))) { stack_trace->lines.push_back(s.str()); } // TVMFuncCall denotes the API boundary so we stop there. Exceptions should be caught there. From cc9f6c18ffa688505094e1e592e7973873e963a3 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Tue, 9 Feb 2021 15:30:11 -0800 Subject: [PATCH 31/67] namespace windows backtrace correctly? --- src/runtime/logging.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/runtime/logging.cc b/src/runtime/logging.cc index 4f0099654650..c6f251403820 100644 --- a/src/runtime/logging.cc +++ b/src/runtime/logging.cc @@ -18,11 +18,17 @@ */ #ifdef TVM_BACKTRACE_DISABLED +#include + // TODO(bkimball,tkonolige) This inline function is to work around a linking error I am having when // using MSVC If the function definition is in logging.cc then the linker can't find it no matter // what kind of attributes (dllexport) I decorate it with. This is temporary and will be addressed // when we get backtrace working on Windows. -std::string tvm::runtime::Backtrace() { return ""; } +namespace tvm { +namespace runtime { +std::string Backtrace() { return ""; } +} // namespace runtime +} // namespace tvm #else #include From cd04cb92b022fff8cd96c9b54c1871e8f0944343 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Tue, 9 Feb 2021 15:48:41 -0800 Subject: [PATCH 32/67] fix warnings on windows --- include/tvm/runtime/logging.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index 5954f5a04de1..cfdf373753b1 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -124,7 +124,11 @@ #define COND_CHECK_2(quit_on_assert, x) COND_CHECK_3(quit_on_assert, x, return false) #define COND_LOG_2(quit_on_assert, x) COND_LOG_3(quit_on_assert, x, return false) +#ifdef _MSC_VER +#define TVM_THROW_EXCEPTION noexcept(false) __declspec(noreturn) +#else #define TVM_THROW_EXCEPTION noexcept(false) +#endif namespace tvm { namespace runtime { From b53137ff11fcc882a37b472973c9a15196dabf23 Mon Sep 17 00:00:00 2001 From: Robert Kimball Date: Tue, 9 Feb 2021 17:02:31 -0800 Subject: [PATCH 33/67] Fix building on windows --- include/tvm/runtime/logging.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index cfdf373753b1..952a5ffec637 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -212,7 +212,14 @@ namespace detail { class LogFatal { public: LogFatal(const std::string& file, int lineno) : file_(file), lineno_(lineno) {} - ~LogFatal() TVM_THROW_EXCEPTION { throw InternalError(file_, lineno_, stream_.str()); } +#ifdef _MSC_VER +#pragma disagnostic push +#pragma warning(disable : 4722) +#endif + ~LogFatal() noexcept(false) { throw InternalError(file_, lineno_, stream_.str()); } +#ifdef _MSC_VER +#pragma disagnostic pop +#endif std::ostringstream& stream() { return stream_; } private: From d63e6bbfa7d292ce73c0f53b5d1e1920452d2c75 Mon Sep 17 00:00:00 2001 From: Robert Kimball Date: Thu, 11 Feb 2021 09:41:06 -0800 Subject: [PATCH 34/67] Fix unresolved symbol for function Backtrace on Windows. Apply style. --- src/runtime/contrib/cublas/cublas_utils.h | 2 +- src/runtime/hexagon/sim/hexagon_device_sim.cc | 2 +- src/runtime/logging.cc | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/runtime/contrib/cublas/cublas_utils.h b/src/runtime/contrib/cublas/cublas_utils.h index d47837987a92..3edb8300be88 100644 --- a/src/runtime/contrib/cublas/cublas_utils.h +++ b/src/runtime/contrib/cublas/cublas_utils.h @@ -68,7 +68,7 @@ inline const char* GetCublasErrorString(int error) { int error = static_cast(fn); \ ICHECK_EQ(error, CUBLAS_STATUS_SUCCESS) << "CUBLAS: " << GetCublasErrorString(error); \ } while (0) // ; intentionally left off. -#endif // CHECK_CUBLAS_ERROR +#endif // CHECK_CUBLAS_ERROR struct CuBlasThreadEntry { CuBlasThreadEntry(); diff --git a/src/runtime/hexagon/sim/hexagon_device_sim.cc b/src/runtime/hexagon/sim/hexagon_device_sim.cc index 5d18cdab5d00..1d3f0fd1006f 100644 --- a/src/runtime/hexagon/sim/hexagon_device_sim.cc +++ b/src/runtime/hexagon/sim/hexagon_device_sim.cc @@ -110,7 +110,7 @@ struct non_const_str { ICHECK_EQ(pointers_.size(), 1); return pointers_[0]; } - operator char**() { return pointers_.data(); } + operator char* *() { return pointers_.data(); } private: std::vector pointers_; diff --git a/src/runtime/logging.cc b/src/runtime/logging.cc index c6f251403820..890061c6b4f7 100644 --- a/src/runtime/logging.cc +++ b/src/runtime/logging.cc @@ -26,7 +26,7 @@ // when we get backtrace working on Windows. namespace tvm { namespace runtime { -std::string Backtrace() { return ""; } +__declspec(dllexport) std::string Backtrace() { return ""; } } // namespace runtime } // namespace tvm #else From 59fd4c37e5f32717b87d765821cbcc026ea3462a Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Thu, 11 Feb 2021 10:23:52 -0800 Subject: [PATCH 35/67] pass dmlc flag to standalone crt --- cmake/modules/StandaloneCrt.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/cmake/modules/StandaloneCrt.cmake b/cmake/modules/StandaloneCrt.cmake index dc1b3b2665f2..cd7b332684bb 100644 --- a/cmake/modules/StandaloneCrt.cmake +++ b/cmake/modules/StandaloneCrt.cmake @@ -105,6 +105,7 @@ if(USE_MICRO) set(make_common_args "CRT_CONFIG=template/host/crt_config.h" "BUILD_DIR=${host_build_dir_abspath}" + "DMLC_USE_LOGGING_LIBRARY=" "EXTRA_CFLAGS=-fPIC" "EXTRA_CXXFLAGS=-fPIC" "EXTRA_LDFLAGS=-fPIC" From b408ed4abe4de614981f08ff6060d539db5fabf1 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Thu, 11 Feb 2021 10:38:41 -0800 Subject: [PATCH 36/67] forgot private --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index af67f5683398..accdf4a5a9d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -404,7 +404,7 @@ if(USE_LIBBACKTRACE) include(cmake/modules/Libbacktrace.cmake) target_link_libraries(tvm PRIVATE libbacktrace) target_link_libraries(tvm_runtime PRIVATE libbacktrace) - target_link_libraries(tvm_runtime_objs libbacktrace) + target_link_libraries(tvm_runtime_objs PRIVATE libbacktrace) # pre 3.12 versions of cmake cannot propagate include directories from imported targets so we set them manually target_include_directories(tvm PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") target_include_directories(tvm_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") From 5255c3d14fb3565a805a840a49de40adad2d44f8 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Thu, 11 Feb 2021 11:23:16 -0800 Subject: [PATCH 37/67] switch to TVM_DLL --- cmake/modules/StandaloneCrt.cmake | 1 + include/tvm/runtime/logging.h | 2 +- src/runtime/logging.cc | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cmake/modules/StandaloneCrt.cmake b/cmake/modules/StandaloneCrt.cmake index cd7b332684bb..3239447bb3fb 100644 --- a/cmake/modules/StandaloneCrt.cmake +++ b/cmake/modules/StandaloneCrt.cmake @@ -104,6 +104,7 @@ if(USE_MICRO) set(make_common_args "CRT_CONFIG=template/host/crt_config.h" + "DMLC_USE_LOGGING_LIBRARY=" "BUILD_DIR=${host_build_dir_abspath}" "DMLC_USE_LOGGING_LIBRARY=" "EXTRA_CFLAGS=-fPIC" diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index 952a5ffec637..222bcdbbded9 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -136,7 +136,7 @@ namespace runtime { /* \brief Generate a backtrace when called. * \return A multiline string of the backtrace. There will be either one or two lines per frame. */ -std::string Backtrace(); +TVM_DLL std::string Backtrace(); /*! \brief Base error type for TVM. Wraps a string message. */ class Error : public ::dmlc::Error { // for backwards compatibility diff --git a/src/runtime/logging.cc b/src/runtime/logging.cc index 890061c6b4f7..c6f251403820 100644 --- a/src/runtime/logging.cc +++ b/src/runtime/logging.cc @@ -26,7 +26,7 @@ // when we get backtrace working on Windows. namespace tvm { namespace runtime { -__declspec(dllexport) std::string Backtrace() { return ""; } +std::string Backtrace() { return ""; } } // namespace runtime } // namespace tvm #else From a5589a4252999ab9325df66d4a901bb2964d9a35 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Thu, 11 Feb 2021 13:27:11 -0800 Subject: [PATCH 38/67] more dll stuff --- src/runtime/logging.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/logging.cc b/src/runtime/logging.cc index c6f251403820..bf6e41a154bf 100644 --- a/src/runtime/logging.cc +++ b/src/runtime/logging.cc @@ -26,7 +26,7 @@ // when we get backtrace working on Windows. namespace tvm { namespace runtime { -std::string Backtrace() { return ""; } +TVM_DLL std::string Backtrace() { return ""; } } // namespace runtime } // namespace tvm #else From 76b0165c0d248f6262a1d9330614c3c191112ba5 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Thu, 11 Feb 2021 14:16:19 -0800 Subject: [PATCH 39/67] revert to windows-only approach --- include/tvm/runtime/logging.h | 2 +- src/runtime/logging.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index 222bcdbbded9..952a5ffec637 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -136,7 +136,7 @@ namespace runtime { /* \brief Generate a backtrace when called. * \return A multiline string of the backtrace. There will be either one or two lines per frame. */ -TVM_DLL std::string Backtrace(); +std::string Backtrace(); /*! \brief Base error type for TVM. Wraps a string message. */ class Error : public ::dmlc::Error { // for backwards compatibility diff --git a/src/runtime/logging.cc b/src/runtime/logging.cc index bf6e41a154bf..890061c6b4f7 100644 --- a/src/runtime/logging.cc +++ b/src/runtime/logging.cc @@ -26,7 +26,7 @@ // when we get backtrace working on Windows. namespace tvm { namespace runtime { -TVM_DLL std::string Backtrace() { return ""; } +__declspec(dllexport) std::string Backtrace() { return ""; } } // namespace runtime } // namespace tvm #else From 6155a1124c359545327e3808101595418a3e3f56 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Thu, 11 Feb 2021 16:07:02 -0800 Subject: [PATCH 40/67] fix vta_fsim defines --- cmake/modules/VTA.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/cmake/modules/VTA.cmake b/cmake/modules/VTA.cmake index 115216680fff..21b024775004 100644 --- a/cmake/modules/VTA.cmake +++ b/cmake/modules/VTA.cmake @@ -60,6 +60,7 @@ elseif(PYTHON) # Target lib: vta_fsim add_library(vta_fsim SHARED ${FSIM_RUNTIME_SRCS}) target_include_directories(vta_fsim SYSTEM PUBLIC ${VTA_HW_PATH}/include) + target_compile_definitions(vta_fsim PUBLIC DMLC_USE_LOGGING_LIBRARY=) foreach(__def ${VTA_DEFINITIONS}) string(SUBSTRING ${__def} 3 -1 __strip_def) target_compile_definitions(vta_fsim PUBLIC ${__strip_def}) From ef2c9f863f935538239b99fb8e207a699cf12464 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Thu, 11 Feb 2021 16:23:37 -0800 Subject: [PATCH 41/67] adding more -D... --- apps/bundle_deploy/Makefile | 6 ++++-- golang/Makefile | 2 +- python/tvm/micro/build.py | 2 +- src/runtime/crt/Makefile | 4 ++-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/apps/bundle_deploy/Makefile b/apps/bundle_deploy/Makefile index 38d9d3456d55..8e23a92afa93 100644 --- a/apps/bundle_deploy/Makefile +++ b/apps/bundle_deploy/Makefile @@ -32,12 +32,14 @@ PKG_CXXFLAGS = ${PKG_COMPILE_OPTS} -std=c++14 \ -I${TVM_ROOT}/include \ -I${DMLC_CORE}/include \ -I${TVM_ROOT}/3rdparty/dlpack/include \ - -Icrt_config + -Icrt_config \ + -DDMLC_USE_LOGGING_LIBRARY=\ PKG_CFLAGS = ${PKG_COMPILE_OPTS} \ -I${TVM_ROOT}/include \ -I${DMLC_CORE}/include \ -I${TVM_ROOT}/3rdparty/dlpack/include \ - -Icrt_config + -Icrt_config \ + -DDMLC_USE_LOGGING_LIBRARY=\ PKG_LDFLAGS = -pthread -lm diff --git a/golang/Makefile b/golang/Makefile index 6fd77996e119..137e2a488e29 100644 --- a/golang/Makefile +++ b/golang/Makefile @@ -25,7 +25,7 @@ NATIVE_SRC = tvm_runtime_pack.cc GOPATH=$(CURDIR)/gopath GOPATHDIR=${GOPATH}/src/${TARGET}/ CGO_CPPFLAGS="-I. -I${TVM_BASE}/ -I${TVM_BASE}/3rdparty/dmlc-core/include -I${TVM_BASE}/include -I${TVM_BASE}/3rdparty/dlpack/include/" -CGO_CXXFLAGS="-std=c++14" +CGO_CXXFLAGS="-std=c++14 -DDMLC_USE_LOGGING_LIBRARY=\" CGO_CFLAGS="-I${TVM_BASE}" CGO_LDFLAGS="-ldl -lm" diff --git a/python/tvm/micro/build.py b/python/tvm/micro/build.py index 3837d423f8bd..d95f14f0349e 100644 --- a/python/tvm/micro/build.py +++ b/python/tvm/micro/build.py @@ -118,7 +118,7 @@ def get_runtime_libs() -> str: RUNTIME_SRC_REGEX = re.compile(r"^.*\.cc?$", re.IGNORECASE) -_COMMON_CFLAGS = ["-Wall", "-Werror"] +_COMMON_CFLAGS = ["-Wall", "-Werror", "-DDMLC_USE_LOGGING_LIBRARY="] def _build_default_compiler_options(standalone_crt_dir: typing.Optional[str] = None) -> str: diff --git a/src/runtime/crt/Makefile b/src/runtime/crt/Makefile index 0f3e3096e319..d707d0c63b81 100644 --- a/src/runtime/crt/Makefile +++ b/src/runtime/crt/Makefile @@ -45,8 +45,8 @@ QUIET ?= @ CRT_PREFIX = $(wildcard src/crt) INCLUDES ?= -isystem include -iquote $(dir ${CRT_CONFIG}) -CFLAGS += ${INCLUDES} -Werror -g $(EXTRA_CFLAGS) -CXXFLAGS += ${INCLUDES} -std=c++11 -Werror -g $(EXTRA_CXXFLAGS) +CFLAGS += ${INCLUDES} -Werror -g $(EXTRA_CFLAGS) -DDMLC_USE_LOGGING_LIBRARY=\ +CXXFLAGS += ${INCLUDES} -std=c++11 -Werror -g $(EXTRA_CXXFLAGS) -DDMLC_USE_LOGGING_LIBRARY=\ LDFLAGS += -Werror -g $(EXTRA_LDFLAGS) ${BUILD_DIR}/%.o: src/%.c $(CRT_CONFIG) From 3a63b2c793a9d780122c57fd943630cfa7cbe3fe Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 15 Feb 2021 11:04:55 -0800 Subject: [PATCH 42/67] vta flags --- cmake/modules/VTA.cmake | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmake/modules/VTA.cmake b/cmake/modules/VTA.cmake index 21b024775004..58b58d231d83 100644 --- a/cmake/modules/VTA.cmake +++ b/cmake/modules/VTA.cmake @@ -82,6 +82,7 @@ elseif(PYTHON) # Target lib: vta_tsim add_library(vta_tsim SHARED ${TSIM_RUNTIME_SRCS}) target_include_directories(vta_tsim SYSTEM PUBLIC ${VTA_HW_PATH}/include) + target_compile_definitions(vta_tsim PUBLIC DMLC_USE_LOGGING_LIBRARY=) foreach(__def ${VTA_DEFINITIONS}) string(SUBSTRING ${__def} 3 -1 __strip_def) target_compile_definitions(vta_tsim PUBLIC ${__strip_def}) @@ -108,6 +109,7 @@ elseif(PYTHON) add_library(vta SHARED ${FPGA_RUNTIME_SRCS}) target_include_directories(vta PUBLIC vta/runtime) target_include_directories(vta PUBLIC ${VTA_HW_PATH}/include) + target_compile_definitions(vta PUBLIC DMLC_USE_LOGGING_LIBRARY=) foreach(__def ${VTA_DEFINITIONS}) string(SUBSTRING ${__def} 3 -1 __strip_def) target_compile_definitions(vta PUBLIC ${__strip_def}) From b52f8f2296c451583b5c894ff9b50624a6ad4eab Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 15 Feb 2021 11:05:06 -0800 Subject: [PATCH 43/67] clean up added includes --- CMakeLists.txt | 5 ----- 1 file changed, 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index accdf4a5a9d2..982036f30666 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -404,14 +404,9 @@ if(USE_LIBBACKTRACE) include(cmake/modules/Libbacktrace.cmake) target_link_libraries(tvm PRIVATE libbacktrace) target_link_libraries(tvm_runtime PRIVATE libbacktrace) - target_link_libraries(tvm_runtime_objs PRIVATE libbacktrace) # pre 3.12 versions of cmake cannot propagate include directories from imported targets so we set them manually - target_include_directories(tvm PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") - target_include_directories(tvm_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") - target_include_directories(tvm_runtime PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") target_include_directories(tvm_runtime_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") else() - target_compile_definitions(tvm_objs PRIVATE TVM_BACKTRACE_DISABLED) target_compile_definitions(tvm_runtime_objs PRIVATE TVM_BACKTRACE_DISABLED) endif() From a5f9f495fe03697347e7c69e88b13d2de452655f Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 15 Feb 2021 11:08:08 -0800 Subject: [PATCH 44/67] bad rebase --- cmake/modules/StandaloneCrt.cmake | 1 - 1 file changed, 1 deletion(-) diff --git a/cmake/modules/StandaloneCrt.cmake b/cmake/modules/StandaloneCrt.cmake index 3239447bb3fb..cd7b332684bb 100644 --- a/cmake/modules/StandaloneCrt.cmake +++ b/cmake/modules/StandaloneCrt.cmake @@ -104,7 +104,6 @@ if(USE_MICRO) set(make_common_args "CRT_CONFIG=template/host/crt_config.h" - "DMLC_USE_LOGGING_LIBRARY=" "BUILD_DIR=${host_build_dir_abspath}" "DMLC_USE_LOGGING_LIBRARY=" "EXTRA_CFLAGS=-fPIC" From 614a4e38e50add49124d39596ca383e42ff280bc Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 15 Feb 2021 12:51:02 -0800 Subject: [PATCH 45/67] apparently include directories is needed on all targets --- CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 982036f30666..2ffd1b17c9ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -405,8 +405,12 @@ if(USE_LIBBACKTRACE) target_link_libraries(tvm PRIVATE libbacktrace) target_link_libraries(tvm_runtime PRIVATE libbacktrace) # pre 3.12 versions of cmake cannot propagate include directories from imported targets so we set them manually + target_include_directories(tvm PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") + target_include_directories(tvm_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") + target_include_directories(tvm_runtime PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") target_include_directories(tvm_runtime_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") else() + target_compile_definitions(tvm_objs PRIVATE TVM_BACKTRACE_DISABLED) target_compile_definitions(tvm_runtime_objs PRIVATE TVM_BACKTRACE_DISABLED) endif() From 8b2e0987dc6782960bff5b63330421d0ea1a2697 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Tue, 16 Feb 2021 09:19:28 -0800 Subject: [PATCH 46/67] fix flag for standalone crt --- cmake/modules/StandaloneCrt.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/modules/StandaloneCrt.cmake b/cmake/modules/StandaloneCrt.cmake index cd7b332684bb..46ccfdbac6a8 100644 --- a/cmake/modules/StandaloneCrt.cmake +++ b/cmake/modules/StandaloneCrt.cmake @@ -105,7 +105,7 @@ if(USE_MICRO) set(make_common_args "CRT_CONFIG=template/host/crt_config.h" "BUILD_DIR=${host_build_dir_abspath}" - "DMLC_USE_LOGGING_LIBRARY=" + "DMLC_USE_LOGGING_LIBRARY=\" "EXTRA_CFLAGS=-fPIC" "EXTRA_CXXFLAGS=-fPIC" "EXTRA_LDFLAGS=-fPIC" From 935d941f1dbf37f2e29e7e6c8ac4f6f9726093bd Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Tue, 16 Feb 2021 09:26:27 -0800 Subject: [PATCH 47/67] remove unnessesary flags? --- cmake/modules/StandaloneCrt.cmake | 1 - 1 file changed, 1 deletion(-) diff --git a/cmake/modules/StandaloneCrt.cmake b/cmake/modules/StandaloneCrt.cmake index 46ccfdbac6a8..dc1b3b2665f2 100644 --- a/cmake/modules/StandaloneCrt.cmake +++ b/cmake/modules/StandaloneCrt.cmake @@ -105,7 +105,6 @@ if(USE_MICRO) set(make_common_args "CRT_CONFIG=template/host/crt_config.h" "BUILD_DIR=${host_build_dir_abspath}" - "DMLC_USE_LOGGING_LIBRARY=\" "EXTRA_CFLAGS=-fPIC" "EXTRA_CXXFLAGS=-fPIC" "EXTRA_LDFLAGS=-fPIC" From ad0cf2379848c9b16f6ef555f14c00aafb173209 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Tue, 16 Feb 2021 09:34:51 -0800 Subject: [PATCH 48/67] correctly depend on libbacktrace --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ffd1b17c9ac..9caa7618a0ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -404,6 +404,7 @@ if(USE_LIBBACKTRACE) include(cmake/modules/Libbacktrace.cmake) target_link_libraries(tvm PRIVATE libbacktrace) target_link_libraries(tvm_runtime PRIVATE libbacktrace) + add_dependencies(tvm_runtime_objs libbacktrace) # pre 3.12 versions of cmake cannot propagate include directories from imported targets so we set them manually target_include_directories(tvm PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") target_include_directories(tvm_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include") From 54ecb7afca07689059dcc48ca4d71da1e19ded7b Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Fri, 19 Feb 2021 08:57:17 -0800 Subject: [PATCH 49/67] fix overcommit in test --- tests/python/relay/test_ir_text_printer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/relay/test_ir_text_printer.py b/tests/python/relay/test_ir_text_printer.py index 72a243dbbb67..66bf8789adfc 100644 --- a/tests/python/relay/test_ir_text_printer.py +++ b/tests/python/relay/test_ir_text_printer.py @@ -182,7 +182,7 @@ def test_squeezenet(): def test_vgg(): - net, _ = tvm.relay.testing.vgg.get_workload(batch_size=1) + net, _ = tvm.relay.testing.vgg.get_workload(batch_size=1, num_layers=2) astext(net) From 54e69d26d52e6d7d02bf6b4190d48d2e4b6690cd Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Fri, 19 Feb 2021 10:57:06 -0800 Subject: [PATCH 50/67] revert layer change --- tests/python/relay/test_ir_text_printer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/relay/test_ir_text_printer.py b/tests/python/relay/test_ir_text_printer.py index 66bf8789adfc..72a243dbbb67 100644 --- a/tests/python/relay/test_ir_text_printer.py +++ b/tests/python/relay/test_ir_text_printer.py @@ -182,7 +182,7 @@ def test_squeezenet(): def test_vgg(): - net, _ = tvm.relay.testing.vgg.get_workload(batch_size=1, num_layers=2) + net, _ = tvm.relay.testing.vgg.get_workload(batch_size=1) astext(net) From 3e6edf85523eba8e2dd4a3e1ef5dfee2395839e3 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Fri, 19 Feb 2021 13:20:43 -0800 Subject: [PATCH 51/67] use float16 for vgg test --- tests/python/relay/test_ir_text_printer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/relay/test_ir_text_printer.py b/tests/python/relay/test_ir_text_printer.py index 72a243dbbb67..3cb395c34330 100644 --- a/tests/python/relay/test_ir_text_printer.py +++ b/tests/python/relay/test_ir_text_printer.py @@ -182,7 +182,7 @@ def test_squeezenet(): def test_vgg(): - net, _ = tvm.relay.testing.vgg.get_workload(batch_size=1) + net, _ = tvm.relay.testing.vgg.get_workload(batch_size=1, dtype="float16") astext(net) From 9634240f3babcdeabd6e7caab25dcd4d345e44f9 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Fri, 19 Feb 2021 15:38:25 -0800 Subject: [PATCH 52/67] delete vgg ir printing test. it takes too much memory --- tests/python/relay/test_ir_text_printer.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/python/relay/test_ir_text_printer.py b/tests/python/relay/test_ir_text_printer.py index 3cb395c34330..b2ae28649e6a 100644 --- a/tests/python/relay/test_ir_text_printer.py +++ b/tests/python/relay/test_ir_text_printer.py @@ -181,11 +181,6 @@ def test_squeezenet(): astext(net) -def test_vgg(): - net, _ = tvm.relay.testing.vgg.get_workload(batch_size=1, dtype="float16") - astext(net) - - def test_densenet(): net, _ = tvm.relay.testing.densenet.get_workload(batch_size=1) astext(net) From 8ab5d59a5e57f973aaca737a256a75af6a57ea81 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 22 Feb 2021 10:03:28 -0800 Subject: [PATCH 53/67] only add flags for CXX --- CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9caa7618a0ad..20a80213a373 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -583,10 +583,10 @@ if(TVM_IS_DEBUG_BUILD) target_compile_options(tvm PRIVATE "${FILE_PREFIX_MAP_FLAG}") CHECK_CXX_COMPILER_FLAG("${FILE_PREFIX_MAP_FLAG}" FILE_PREFIX_MAP_SUPPORTED) if(FILE_PREFIX_MAP_SUPPORTED) - target_compile_options(tvm PRIVATE "${FILE_PREFIX_MAP_FLAG}") - target_compile_options(tvm_objs PRIVATE "${FILE_PREFIX_MAP_FLAG}") - target_compile_options(tvm_runtime PRIVATE "${FILE_PREFIX_MAP_FLAG}") - target_compile_options(tvm_runtime_objs PRIVATE "${FILE_PREFIX_MAP_FLAG}") + target_compile_options(tvm PRIVATE $<$:${FILE_PREFIX_MAP_FLAG}>) + target_compile_options(tvm_objs PRIVATE $<$:${FILE_PREFIX_MAP_FLAG}>) + target_compile_options(tvm_runtime PRIVATE $<$:${FILE_PREFIX_MAP_FLAG}>) + target_compile_options(tvm_runtime_objs PRIVATE $<$:${FILE_PREFIX_MAP_FLAG}>) endif() endif() From a734de35843398e446bfdd7b5e6300dcffb9deff Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Tue, 23 Feb 2021 12:07:09 -0800 Subject: [PATCH 54/67] bump stack size on CI --- Jenkinsfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index 506dcab4e306..a627cc552f9e 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -321,6 +321,7 @@ stage('Integration Test') { init_git() unpack_lib('gpu', tvm_multilib) timeout(time: max_time, unit: 'MINUTES') { + sh "ulimit -S -s 16384" sh "${docker_run} ${ci_gpu} ./tests/scripts/task_ci_setup.sh" sh "${docker_run} ${ci_gpu} ./tests/scripts/task_python_frontend.sh" junit "build/pytest-results/*.xml" @@ -347,6 +348,7 @@ stage('Integration Test') { init_git() unpack_lib('gpu', tvm_multilib) timeout(time: max_time, unit: 'MINUTES') { + sh "ulimit -S -s 16384" sh "${docker_run} ${ci_gpu} ./tests/scripts/task_ci_setup.sh" sh "${docker_run} ${ci_gpu} ./tests/scripts/task_python_docs.sh" } From 4c9f84996dea95353ecc2a54042f07df6ffafd3c Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Tue, 23 Feb 2021 12:17:17 -0800 Subject: [PATCH 55/67] remove bad todo --- src/runtime/c_runtime_api.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/runtime/c_runtime_api.cc b/src/runtime/c_runtime_api.cc index 0e47f63c449b..150d7f215da5 100644 --- a/src/runtime/c_runtime_api.cc +++ b/src/runtime/c_runtime_api.cc @@ -518,8 +518,7 @@ int TVMFuncCreateFromCFunc(TVMPackedCFunc func, void* resource_handle, TVMPacked int ret = func(const_cast(args.values), const_cast(args.type_codes), args.num_args, rv, resource_handle); if (ret != 0) { - throw tvm::Error(TVMGetLastError() + - tvm::runtime::Backtrace()); // TODO(tkonolige): this may double backtrace + throw tvm::Error(TVMGetLastError() + tvm::runtime::Backtrace()); } }); } else { From 17991376447218a9ea6ec85ab150d3768bce6c42 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Tue, 23 Feb 2021 17:39:38 -0800 Subject: [PATCH 56/67] switch relay test to tvm so it stops running out of memory --- tests/python/relay/test_ir_parser.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/python/relay/test_ir_parser.py b/tests/python/relay/test_ir_parser.py index 62e52abefeb4..bfcd13c1dff1 100644 --- a/tests/python/relay/test_ir_parser.py +++ b/tests/python/relay/test_ir_parser.py @@ -827,8 +827,8 @@ def test_import_grad(): mod.import_from_std("gradient.rly") -def test_resnet(): - mod, _ = relay.testing.resnet.get_workload() +def test_mlp(): + mod, _ = relay.testing.mlp.get_workload() text = mod.astext() parsed_mod = tvm.parser.parse(text) tvm.ir.assert_structural_equal(mod, parsed_mod) @@ -850,8 +850,8 @@ def inline_params(mod, params): return mod -def test_resnet_inlined_params(): - mod, params = relay.testing.resnet.get_workload() +def test_mlp_inlined_params(): + mod, params = relay.testing.mlp.get_workload() mod = inline_params(mod, params) mod = relay.transform.InferType()(mod) text = mod.astext() From c5b9b121eb50ee370ac8cec85c7addc981825fc4 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Wed, 24 Feb 2021 09:55:10 -0800 Subject: [PATCH 57/67] fix workload --- 3rdparty/dmlc-core | 2 +- tests/python/relay/test_ir_parser.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/3rdparty/dmlc-core b/3rdparty/dmlc-core index 21cc7de0dc9f..6c401e242c59 160000 --- a/3rdparty/dmlc-core +++ b/3rdparty/dmlc-core @@ -1 +1 @@ -Subproject commit 21cc7de0dc9fd6acb796e1be6181fa8e6b6c8f41 +Subproject commit 6c401e242c59a1f4c913918246591bb13fd714e7 diff --git a/tests/python/relay/test_ir_parser.py b/tests/python/relay/test_ir_parser.py index bfcd13c1dff1..8b6b39e3df15 100644 --- a/tests/python/relay/test_ir_parser.py +++ b/tests/python/relay/test_ir_parser.py @@ -828,7 +828,7 @@ def test_import_grad(): def test_mlp(): - mod, _ = relay.testing.mlp.get_workload() + mod, _ = relay.testing.mlp.get_workload(1) text = mod.astext() parsed_mod = tvm.parser.parse(text) tvm.ir.assert_structural_equal(mod, parsed_mod) @@ -851,7 +851,7 @@ def inline_params(mod, params): def test_mlp_inlined_params(): - mod, params = relay.testing.mlp.get_workload() + mod, params = relay.testing.mlp.get_workload(1) mod = inline_params(mod, params) mod = relay.transform.InferType()(mod) text = mod.astext() From 7c68da19aef4e641b0c0296f855474b04544a216 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Wed, 24 Feb 2021 10:23:02 -0800 Subject: [PATCH 58/67] Accidentally droped dmlc dep --- 3rdparty/dmlc-core | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rdparty/dmlc-core b/3rdparty/dmlc-core index 6c401e242c59..21cc7de0dc9f 160000 --- a/3rdparty/dmlc-core +++ b/3rdparty/dmlc-core @@ -1 +1 @@ -Subproject commit 6c401e242c59a1f4c913918246591bb13fd714e7 +Subproject commit 21cc7de0dc9fd6acb796e1be6181fa8e6b6c8f41 From ccbe2123ba01017ba9b0a5e83fc25654cbc4168c Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Thu, 25 Feb 2021 09:23:07 -0800 Subject: [PATCH 59/67] different ulimit change --- Jenkinsfile | 1 - tests/scripts/task_ci_setup.sh | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index a627cc552f9e..36bab4c88b31 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -321,7 +321,6 @@ stage('Integration Test') { init_git() unpack_lib('gpu', tvm_multilib) timeout(time: max_time, unit: 'MINUTES') { - sh "ulimit -S -s 16384" sh "${docker_run} ${ci_gpu} ./tests/scripts/task_ci_setup.sh" sh "${docker_run} ${ci_gpu} ./tests/scripts/task_python_frontend.sh" junit "build/pytest-results/*.xml" diff --git a/tests/scripts/task_ci_setup.sh b/tests/scripts/task_ci_setup.sh index 17838c58a83c..d26f0df3e30c 100755 --- a/tests/scripts/task_ci_setup.sh +++ b/tests/scripts/task_ci_setup.sh @@ -20,6 +20,9 @@ set -e set -u set -o pipefail +# bump ulimit because frontend tests are deeply recursive +ulimit -S -s 16384 + # Script to setup additional python env. # # Use the following command to install the From 481461be5f6ec5952b18eb2d5c5054bbf4d15567 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Thu, 25 Feb 2021 17:27:07 -0800 Subject: [PATCH 60/67] finally fix stack overflow --- Jenkinsfile | 1 - tests/scripts/task_ci_setup.sh | 3 --- 2 files changed, 4 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 36bab4c88b31..506dcab4e306 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -347,7 +347,6 @@ stage('Integration Test') { init_git() unpack_lib('gpu', tvm_multilib) timeout(time: max_time, unit: 'MINUTES') { - sh "ulimit -S -s 16384" sh "${docker_run} ${ci_gpu} ./tests/scripts/task_ci_setup.sh" sh "${docker_run} ${ci_gpu} ./tests/scripts/task_python_docs.sh" } diff --git a/tests/scripts/task_ci_setup.sh b/tests/scripts/task_ci_setup.sh index d26f0df3e30c..17838c58a83c 100755 --- a/tests/scripts/task_ci_setup.sh +++ b/tests/scripts/task_ci_setup.sh @@ -20,9 +20,6 @@ set -e set -u set -o pipefail -# bump ulimit because frontend tests are deeply recursive -ulimit -S -s 16384 - # Script to setup additional python env. # # Use the following command to install the From f73b7e779a58325a9fbdaeb1f39f1769fd009dd4 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Fri, 5 Mar 2021 14:57:50 -0800 Subject: [PATCH 61/67] one global backtrace object --- src/runtime/logging.cc | 50 +++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/runtime/logging.cc b/src/runtime/logging.cc index 890061c6b4f7..9417ba00e830 100644 --- a/src/runtime/logging.cc +++ b/src/runtime/logging.cc @@ -51,16 +51,21 @@ struct BacktraceInfo { std::string error_message; }; -backtrace_state** GetBacktraceState() { - thread_local backtrace_state* state = nullptr; - return &state; +void BacktraceCreateErrorCallback(void* data, const char* msg, int errnum) { + std::cerr << "Could not initialize backtrace state: " << msg << std::endl; } -std::string DemangleName(const char* name) { +backtrace_state* BacktraceCreate() { + return backtrace_create_state(nullptr, 1, BacktraceCreateErrorCallback, nullptr); +} + +static backtrace_state* _bt_state = BacktraceCreate(); + +std::string DemangleName(std::string name) { int status = 0; - size_t length = std::string::npos; + size_t length = name.size(); std::unique_ptr demangled_name = { - abi::__cxa_demangle(name, nullptr, &length, &status), &std::free}; + abi::__cxa_demangle(name.c_str(), nullptr, &length, &status), &std::free}; if (demangled_name && status == 0 && length > 0) { return demangled_name.get(); } else { @@ -69,8 +74,7 @@ std::string DemangleName(const char* name) { } void BacktraceErrorCallback(void* data, const char* msg, int errnum) { - auto stack_trace = reinterpret_cast(data); - stack_trace->error_message = msg; + // do nothing } void BacktraceSyminfoCallback(void* data, uintptr_t pc, const char* symname, uintptr_t symval, @@ -78,7 +82,8 @@ void BacktraceSyminfoCallback(void* data, uintptr_t pc, const char* symname, uin auto str = reinterpret_cast(data); if (symname != nullptr) { - *str = DemangleName(symname); + std::string tmp(symname, symsize); + *str = DemangleName(tmp.c_str()); } else { std::ostringstream s; s << "0x" << std::setfill('0') << std::setw(sizeof(uintptr_t) * 2) << std::hex << pc; @@ -90,15 +95,14 @@ int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int li auto stack_trace = reinterpret_cast(data); std::stringstream s; - std::string symbol_str = ""; + std::unique_ptr symbol_str = std::make_unique(""); if (symbol != nullptr) { - symbol_str = DemangleName(symbol); + *symbol_str = DemangleName(symbol); } else { // see if syminfo gives anything - backtrace_state** bt_state = GetBacktraceState(); - backtrace_syminfo(*bt_state, pc, BacktraceSyminfoCallback, BacktraceErrorCallback, &symbol_str); + backtrace_syminfo(_bt_state, pc, BacktraceSyminfoCallback, BacktraceErrorCallback, symbol_str.get()); } - s << symbol_str; + s << *symbol_str; if (filename != nullptr) { s << std::endl << " at " << filename; @@ -109,12 +113,12 @@ int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int li // Skip tvm::backtrace and tvm::LogFatal::~LogFatal at the beginning of the trace as they don't // add anything useful to the backtrace. if (!(stack_trace->lines.size() == 0 && - (symbol_str.find("tvm::runtime::Backtrace", 0) == 0 || - symbol_str.find("tvm::runtime::detail::LogFatal", 0) == 0))) { + (symbol_str->find("tvm::runtime::Backtrace", 0) == 0 || + symbol_str->find("tvm::runtime::detail::LogFatal", 0) == 0))) { stack_trace->lines.push_back(s.str()); } // TVMFuncCall denotes the API boundary so we stop there. Exceptions should be caught there. - if (symbol_str == "TVMFuncCall" || stack_trace->lines.size() >= stack_trace->max_size) { + if (*symbol_str == "TVMFuncCall" || stack_trace->lines.size() >= stack_trace->max_size) { return 1; } return 0; @@ -124,13 +128,13 @@ int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int li std::string Backtrace() { BacktraceInfo bt; bt.max_size = 100; - backtrace_state** bt_state = GetBacktraceState(); - if (*bt_state == nullptr) { - // TVM is loaded as a shared library into python, so we cannot supply an - // executable path to libbacktrace. - *bt_state = backtrace_create_state(nullptr, 1, BacktraceErrorCallback, &bt); + if(_bt_state == nullptr) { + return ""; } - backtrace_full(*bt_state, 0, BacktraceFullCallback, BacktraceErrorCallback, &bt); + // libbacktrace eats memory if run on multiple threads at the same time, so we guard against it + static std::mutex m; + std::lock_guard lock(m); + backtrace_full(_bt_state, 0, BacktraceFullCallback, BacktraceErrorCallback, &bt); std::ostringstream s; s << "Stack trace:\n"; From 2bc5dd18a92f6b66db93eaa3db7e4bf54ceda3de Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 8 Mar 2021 10:37:10 -0800 Subject: [PATCH 62/67] rename --- src/relay/transforms/fold_explicit_padding.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/relay/transforms/fold_explicit_padding.cc b/src/relay/transforms/fold_explicit_padding.cc index bab8b814df05..d959e5b75e40 100644 --- a/src/relay/transforms/fold_explicit_padding.cc +++ b/src/relay/transforms/fold_explicit_padding.cc @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include "../op/tensor/transform.h" #include "pattern_utils.h" From 3ef265f54df7cc192c641d39440c0dc2dc3b3e2a Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Mon, 8 Mar 2021 14:27:22 -0800 Subject: [PATCH 63/67] formatting --- src/runtime/logging.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/runtime/logging.cc b/src/runtime/logging.cc index 9417ba00e830..fdf0e7c1beee 100644 --- a/src/runtime/logging.cc +++ b/src/runtime/logging.cc @@ -56,7 +56,7 @@ void BacktraceCreateErrorCallback(void* data, const char* msg, int errnum) { } backtrace_state* BacktraceCreate() { - return backtrace_create_state(nullptr, 1, BacktraceCreateErrorCallback, nullptr); + return backtrace_create_state(nullptr, 1, BacktraceCreateErrorCallback, nullptr); } static backtrace_state* _bt_state = BacktraceCreate(); @@ -100,7 +100,8 @@ int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int li *symbol_str = DemangleName(symbol); } else { // see if syminfo gives anything - backtrace_syminfo(_bt_state, pc, BacktraceSyminfoCallback, BacktraceErrorCallback, symbol_str.get()); + backtrace_syminfo(_bt_state, pc, BacktraceSyminfoCallback, BacktraceErrorCallback, + symbol_str.get()); } s << *symbol_str; @@ -128,7 +129,7 @@ int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int li std::string Backtrace() { BacktraceInfo bt; bt.max_size = 100; - if(_bt_state == nullptr) { + if (_bt_state == nullptr) { return ""; } // libbacktrace eats memory if run on multiple threads at the same time, so we guard against it From 70e8d61c9d7b5fb8f4faba0745d245ea9b4878ac Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Tue, 9 Mar 2021 09:15:17 -0800 Subject: [PATCH 64/67] formatting --- src/runtime/rocm/rocm_device_api.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/rocm/rocm_device_api.cc b/src/runtime/rocm/rocm_device_api.cc index 5a68c316bcf4..5d03374a4571 100644 --- a/src/runtime/rocm/rocm_device_api.cc +++ b/src/runtime/rocm/rocm_device_api.cc @@ -25,8 +25,8 @@ #include #include #include -#include #include +#include #include #include "rocm_common.h" From 1cd330657f645ab6af0235bc1f2f84a50a6fa519 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Tue, 16 Mar 2021 10:15:40 -0700 Subject: [PATCH 65/67] move defines into header for jni --- CMakeLists.txt | 6 +++--- apps/android_camera/app/src/main/jni/Application.mk | 4 ++-- apps/android_camera/app/src/main/jni/tvm_runtime.h | 2 ++ apps/android_deploy/app/src/main/jni/Application.mk | 2 +- apps/android_deploy/app/src/main/jni/tvm_runtime.h | 3 +++ apps/android_rpc/app/src/main/jni/Application.mk | 2 +- apps/android_rpc/app/src/main/jni/tvm_runtime.h | 2 ++ src/runtime/logging.cc | 1 + 8 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 20a80213a373..dab99f501665 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -595,9 +595,9 @@ if(APPLE AND TVM_IS_DEBUG_BUILD) find_program(DSYMUTIL dsymutil) mark_as_advanced(DSYMUTIL) add_custom_command(TARGET tvm - POST_BUILD - COMMAND ${DSYMUTIL} ARGS $ + POST_BUILD + COMMAND ${DSYMUTIL} ARGS $ COMMENT "Running dsymutil" - VERBATIM + VERBATIM ) endif() diff --git a/apps/android_camera/app/src/main/jni/Application.mk b/apps/android_camera/app/src/main/jni/Application.mk index cf39c049f043..5c8774889685 100644 --- a/apps/android_camera/app/src/main/jni/Application.mk +++ b/apps/android_camera/app/src/main/jni/Application.mk @@ -31,7 +31,7 @@ include $(config) APP_ABI ?= all APP_STL := c++_shared -APP_CPPFLAGS += -DDMLC_USE_LOGGING_LIBRARY=\ -DTVM_BACKTRACE_DISABLED=1 -DTVM_LOG_CUTOMIZE=1 -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti +APP_CPPFLAGS += -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti ifeq ($(USE_OPENCL), 1) APP_CPPFLAGS += -DTVM_OPENCL_RUNTIME=1 endif @@ -43,4 +43,4 @@ endif ifeq ($(USE_SORT), 1) APP_CPPFLAGS += -DUSE_SORT=1 -endif \ No newline at end of file +endif diff --git a/apps/android_camera/app/src/main/jni/tvm_runtime.h b/apps/android_camera/app/src/main/jni/tvm_runtime.h index 1324364be4aa..47a3a3de6bba 100644 --- a/apps/android_camera/app/src/main/jni/tvm_runtime.h +++ b/apps/android_camera/app/src/main/jni/tvm_runtime.h @@ -25,6 +25,8 @@ #include +#define DMLC_USE_LOGGING_LIBRARY +#define TVM_BACKTRACE_DISABLED 1 /* Enable custom logging - this will cause TVM to use a custom implementation * of tvm::runtime::detail::LogMessage. We use this to pass TVM log messages to * Android logcat. diff --git a/apps/android_deploy/app/src/main/jni/Application.mk b/apps/android_deploy/app/src/main/jni/Application.mk index ca21f5482f5b..42c4f232a553 100644 --- a/apps/android_deploy/app/src/main/jni/Application.mk +++ b/apps/android_deploy/app/src/main/jni/Application.mk @@ -27,7 +27,7 @@ include $(config) APP_STL := c++_static -APP_CPPFLAGS += -DDMLC_USE_LOGGING_LIBRARY=\ -DTVM_BACKTRACE_DISABLED=1 -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti +APP_CPPFLAGS += -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti ifeq ($(USE_OPENCL), 1) APP_CPPFLAGS += -DTVM_OPENCL_RUNTIME=1 endif diff --git a/apps/android_deploy/app/src/main/jni/tvm_runtime.h b/apps/android_deploy/app/src/main/jni/tvm_runtime.h index 362d278c38c4..4412e9c62e9d 100644 --- a/apps/android_deploy/app/src/main/jni/tvm_runtime.h +++ b/apps/android_deploy/app/src/main/jni/tvm_runtime.h @@ -25,6 +25,9 @@ #include +#define DMLC_USE_LOGGING_LIBRARY +#define TVM_BACKTRACE_DISABLED 1 + #include "../src/runtime/c_runtime_api.cc" #include "../src/runtime/cpu_device_api.cc" #include "../src/runtime/dso_library.cc" diff --git a/apps/android_rpc/app/src/main/jni/Application.mk b/apps/android_rpc/app/src/main/jni/Application.mk index be22434e760d..088eeed750b8 100644 --- a/apps/android_rpc/app/src/main/jni/Application.mk +++ b/apps/android_rpc/app/src/main/jni/Application.mk @@ -31,7 +31,7 @@ include $(config) APP_ABI ?= armeabi-v7a arm64-v8a x86 x86_64 mips APP_STL := c++_shared -APP_CPPFLAGS += -DDMLC_USE_LOGGING_LIBRARY=\ -DTVM_BACKTRACE_DISABLED=1 -DTVM_LOG_CUTOMIZE=1 -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti +APP_CPPFLAGS += -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti ifeq ($(USE_OPENCL), 1) APP_CPPFLAGS += -DTVM_OPENCL_RUNTIME=1 endif diff --git a/apps/android_rpc/app/src/main/jni/tvm_runtime.h b/apps/android_rpc/app/src/main/jni/tvm_runtime.h index af5af779982f..40e6279fb386 100644 --- a/apps/android_rpc/app/src/main/jni/tvm_runtime.h +++ b/apps/android_rpc/app/src/main/jni/tvm_runtime.h @@ -25,6 +25,8 @@ #include +#define DMLC_USE_LOGGING_LIBRARY +#define TVM_BACKTRACE_DISABLED 1 /* Enable custom logging - this will cause TVM to use a custom implementation * of tvm::runtime::detail::LogMessage. We use this to pass TVM log messages to * Android logcat. diff --git a/src/runtime/logging.cc b/src/runtime/logging.cc index fdf0e7c1beee..8a44ec04532c 100644 --- a/src/runtime/logging.cc +++ b/src/runtime/logging.cc @@ -90,6 +90,7 @@ void BacktraceSyminfoCallback(void* data, uintptr_t pc, const char* symname, uin *str = s.str(); } } + int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int lineno, const char* symbol) { auto stack_trace = reinterpret_cast(data); From 0999ce1111fbcaf755378761c7423221c2f1950f Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Tue, 16 Mar 2021 11:25:55 -0700 Subject: [PATCH 66/67] switch to submodule --- .gitmodules | 3 +++ 3rdparty/libbacktrace | 1 + cmake/modules/Libbacktrace.cmake | 26 +++++++---------------- cmake/modules/libbacktrace_macos.patch | 29 -------------------------- 4 files changed, 11 insertions(+), 48 deletions(-) create mode 160000 3rdparty/libbacktrace delete mode 100644 cmake/modules/libbacktrace_macos.patch diff --git a/.gitmodules b/.gitmodules index a1367c97b2f5..6ef740e33153 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "3rdparty/vta-hw"] path = 3rdparty/vta-hw url = https://github.com/apache/incubator-tvm-vta +[submodule "3rdparty/libbacktrace"] + path = 3rdparty/libbacktrace + url = https://github.com/tlc-pack/libbacktrace.git diff --git a/3rdparty/libbacktrace b/3rdparty/libbacktrace new file mode 160000 index 000000000000..08f7c7e69f8e --- /dev/null +++ b/3rdparty/libbacktrace @@ -0,0 +1 @@ +Subproject commit 08f7c7e69f8ea61a0c4151359bc8023be8e9217b diff --git a/cmake/modules/Libbacktrace.cmake b/cmake/modules/Libbacktrace.cmake index a01be59bab7c..742855358809 100644 --- a/cmake/modules/Libbacktrace.cmake +++ b/cmake/modules/Libbacktrace.cmake @@ -16,38 +16,26 @@ # under the License. include(ExternalProject) -# Git tag for libbacktrace -set(TAG dedbe13fda00253fe5d4f2fb812c909729ed5937) - ExternalProject_Add(project_libbacktrace PREFIX libbacktrace - GIT_REPOSITORY https://github.com/ianlancetaylor/libbacktrace.git - GIT_TAG "${TAG}" - CONFIGURE_COMMAND "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/src/project_libbacktrace/configure" + SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../../3rdparty/libbacktrace + BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace + CONFIGURE_COMMAND "${CMAKE_CURRENT_LIST_DIR}/../../3rdparty/libbacktrace/configure" "--prefix=${CMAKE_CURRENT_BINARY_DIR}/libbacktrace" --with-pic INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace" BUILD_COMMAND make INSTALL_COMMAND make install BUILD_BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/lib/libbacktrace.a" "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include/backtrace.h" - # disable the builtin update because it rebuilds on every build - UPDATE_COMMAND "" ) -# Only rebuild libbacktrace if this file changes -# libbacktrace has a bug on macOS with shared libraries: when looking for symbols in shared -# libraries, if any library is missing symbols, then the process stops and no symbols are used. We -# include a patch that changes libbacktrace to instead perform a best-effort lookup of symbols. This -# is a one-line change where a return is changed to a continue within the symbol lookup loop. +# Custom step to rebuild libbacktrace if any of the source files change +file(GLOB LIBBACKTRACE_SRCS "${CMAKE_CURRENT_LIST_DIR}/../../3rdparty/libbacktrace/*.c") ExternalProject_Add_Step(project_libbacktrace checkout DEPENDERS configure DEPENDEES download - DEPENDS "${CMAKE_CURRENT_LIST_DIR}/Libbacktrace.cmake" "${CMAKE_CURRENT_LIST_DIR}/libbacktrace_macos.patch" - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/src/project_libbacktrace - COMMAND git checkout -f ${TAG}; - COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/libbacktrace_macos.patch - COMMENT "update and patch" - ) + DEPENDS ${LIBBACKTRACE_SRCS} +) add_library(libbacktrace STATIC IMPORTED) add_dependencies(libbacktrace project_libbacktrace) diff --git a/cmake/modules/libbacktrace_macos.patch b/cmake/modules/libbacktrace_macos.patch deleted file mode 100644 index eee309c00a9b..000000000000 --- a/cmake/modules/libbacktrace_macos.patch +++ /dev/null @@ -1,29 +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. -diff --git a/macho.c b/macho.c -index 66e101e..d00aea9 100644 ---- a/macho.c -+++ b/macho.c -@@ -1268,7 +1268,7 @@ backtrace_initialize (struct backtrace_state *state, const char *filename, - mff = macho_nodebug; - if (!macho_add (state, name, d, 0, NULL, base_address, 0, - error_callback, data, &mff, &mfs)) -- return 0; -+ continue; - - if (mff != macho_nodebug) - macho_fileline_fn = mff; From 4711201b1c6dc9dc153f50ec18cceea2601f7b4f Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Wed, 17 Mar 2021 10:24:01 -0700 Subject: [PATCH 67/67] Trigger CI