You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by tq...@apache.org on 2025/05/05 14:04:46 UTC

(tvm) branch refactor-s3 updated (832f838824 -> 251e89e811)

This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a change to branch refactor-s3
in repository https://gitbox.apache.org/repos/asf/tvm.git


 discard 832f838824 [WIN] Remove lock to avoid mutex dep at init time
 discard 1448ccd48a [WIN] Cleanup for win
 discard e15c9aaf0c Fix C codegen caller convention
 discard 61e8e2d8fa Bring custom hook to ffi layer
     new ab7a2c3e61 Bring custom hook to ffi layer
     new 3aaf85cbd7 Fix through relax
     new 84d40373bf [FFI] Simplify unpack traits
     new 6e85d026e6 [WIN] Cleanup for win
     new 5438ef692e Fix C codegen caller convention
     new 251e89e811 [WIN] Remove lock to avoid mutex dep at init time

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (832f838824)
            \
             N -- N -- N   refs/heads/refactor-s3 (251e89e811)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 6 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 ffi/include/tvm/ffi/container/variant.h            |  4 +-
 ffi/include/tvm/ffi/function.h                     | 27 ++++---
 ffi/include/tvm/ffi/function_details.h             | 92 +++-------------------
 ffi/include/tvm/ffi/reflection/reflection.h        |  9 ++-
 include/tvm/ir/env_func.h                          | 11 ++-
 include/tvm/runtime/packed_func.h                  | 19 +++--
 python/tvm/ffi/container.py                        |  3 +
 python/tvm/ffi/cython/dtype.pxi                    |  2 +-
 python/tvm/ffi/cython/object.pxi                   |  3 +-
 python/tvm/ffi/cython/string.pxi                   |  2 +
 python/tvm/ffi/dtype.py                            | 21 +++++
 python/tvm/meta_schedule/cost_model/mlp_model.py   |  5 +-
 python/tvm/relax/op/create.py                      |  2 +-
 python/tvm/runtime/ndarray.py                      |  4 +-
 src/node/structural_hash.cc                        | 30 +++++++
 src/tir/schedule/instruction_traits.h              |  9 ++-
 .../test_runtime_packed_func.py                    |  1 -
 .../contrib/test_hexagon/test_parallel_scalar.py   |  6 +-
 tests/python/ffi/test_dtype.py                     |  8 ++
 tests/python/ffi/test_string.py                    | 26 ++++++
 tests/python/relax/test_op_inspect.py              | 53 +++----------
 tests/python/relax/test_op_misc.py                 |  2 +-
 tests/python/relax/test_vm_build.py                |  8 +-
 tests/python/relax/test_vm_callback_function.py    | 11 +--
 tests/python/relax/test_vm_codegen_only.py         |  2 +-
 25 files changed, 180 insertions(+), 180 deletions(-)


(tvm) 04/06: [WIN] Cleanup for win

Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch refactor-s3
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit 6e85d026e60932aeb3e7d5b23a486c7b6d1a4886
Author: tqchen <tq...@gmail.com>
AuthorDate: Sun May 4 21:20:06 2025 -0400

    [WIN] Cleanup for win
---
 cmake/utils/FindLLVM.cmake   | 5 ++++-
 ffi/CMakeLists.txt           | 7 +++++++
 ffi/src/ffi/traceback_win.cc | 3 +--
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/cmake/utils/FindLLVM.cmake b/cmake/utils/FindLLVM.cmake
index 182a2c6693..2a243b06c8 100644
--- a/cmake/utils/FindLLVM.cmake
+++ b/cmake/utils/FindLLVM.cmake
@@ -199,7 +199,7 @@ macro(find_llvm use_llvm)
         message(STATUS "LLVM links against zlib")
         find_package(ZLIB REQUIRED)
         list(APPEND LLVM_LIBS "ZLIB::ZLIB")
-      elseif("${__flag}" STREQUAL "-lzstd" OR ("${__flag}" STREQUAL "zstd.dll.lib"))
+      elseif("${__flag}" STREQUAL "-lzstd")
         list(APPEND CMAKE_MODULE_PATH "${__llvm_cmakedir}")
         find_package(zstd REQUIRED)
         if (TARGET "zstd::libzstd_static")
@@ -212,6 +212,9 @@ macro(find_llvm use_llvm)
       elseif("${__flag}" STREQUAL "-lxml2")
         message(STATUS "LLVM links against xml2")
         list(APPEND LLVM_LIBS "-lxml2")
+      elseif("${__flag}" STREQUAL "zstd.dll.lib")
+        message(STATUS "LLVM linker flag under LLVM libdir: ${__llvm_libdir}/zstd.lib")
+        list(APPEND LLVM_LIBS "${__llvm_libdir}/zstd.lib")
       elseif((__flag MATCHES ".lib$") AND (EXISTS "${__llvm_libdir}/${__flag}"))
         # If the library file ends in .lib try to also search the llvm_libdir
         message(STATUS "LLVM linker flag under LLVM libdir: ${__llvm_libdir}/${__flag}")
diff --git a/ffi/CMakeLists.txt b/ffi/CMakeLists.txt
index 209913d60f..ab9a3fabc3 100644
--- a/ffi/CMakeLists.txt
+++ b/ffi/CMakeLists.txt
@@ -104,6 +104,13 @@ if (TARGET libbacktrace)
   target_link_libraries(tvm_ffi_shared PRIVATE libbacktrace)
   target_link_libraries(tvm_ffi_static PRIVATE libbacktrace)
 endif ()
+
+if (MSVC)
+  target_link_libraries(tvm_ffi_objs PRIVATE DbgHelp.lib)
+  target_link_libraries(tvm_ffi_shared PRIVATE DbgHelp.lib)
+  target_link_libraries(tvm_ffi_static PRIVATE DbgHelp.lib)
+endif ()
+
 target_link_libraries(tvm_ffi_objs PUBLIC tvm_ffi_header)
 target_link_libraries(tvm_ffi_shared PUBLIC tvm_ffi_header)
 target_link_libraries(tvm_ffi_static PUBLIC tvm_ffi_header)
diff --git a/ffi/src/ffi/traceback_win.cc b/ffi/src/ffi/traceback_win.cc
index 166b7eb242..4c89c4f239 100644
--- a/ffi/src/ffi/traceback_win.cc
+++ b/ffi/src/ffi/traceback_win.cc
@@ -23,9 +23,8 @@
  */
 #ifdef _MSC_VER
 
-#include <dbghelp.h>
 #include <windows.h>
-#pragma comment(lib, "DbgHelp.lib")
+#include <dbghelp.h>  // NOLINT(*)
 
 #include <tvm/ffi/c_api.h>
 #include <tvm/ffi/error.h>


(tvm) 03/06: [FFI] Simplify unpack traits

Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch refactor-s3
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit 84d40373bfb86793d0c3b25be887379943aff1bd
Author: tqchen <ti...@gmail.com>
AuthorDate: Sun May 4 20:49:32 2025 -0400

    [FFI] Simplify unpack traits
---
 ffi/include/tvm/ffi/container/variant.h     |  4 +-
 ffi/include/tvm/ffi/function.h              | 27 +++++----
 ffi/include/tvm/ffi/function_details.h      | 92 ++++-------------------------
 ffi/include/tvm/ffi/reflection/reflection.h |  9 +--
 include/tvm/ir/env_func.h                   | 11 +++-
 include/tvm/runtime/packed_func.h           | 19 +++---
 src/tir/schedule/instruction_traits.h       |  9 +--
 7 files changed, 60 insertions(+), 111 deletions(-)

diff --git a/ffi/include/tvm/ffi/container/variant.h b/ffi/include/tvm/ffi/container/variant.h
index 1455a5b34a..c8b58ba49e 100644
--- a/ffi/include/tvm/ffi/container/variant.h
+++ b/ffi/include/tvm/ffi/container/variant.h
@@ -94,12 +94,12 @@ class Variant {
 
   template <typename T, typename = enable_if_variant_contains_t<T>>
   TVM_FFI_INLINE T get() const& {
-    return data_.cast<T>();
+    return data_.template cast<T>();
   }
 
   template <typename T, typename = enable_if_variant_contains_t<T>>
   TVM_FFI_INLINE T get() && {
-    return std::move(data_).cast<T>();
+    return std::move(data_).template cast<T>();
   }
 
   TVM_FFI_INLINE std::string GetTypeKey() const { return data_.GetTypeKey(); }
diff --git a/ffi/include/tvm/ffi/function.h b/ffi/include/tvm/ffi/function.h
index 913237ec04..e4b67e4a76 100644
--- a/ffi/include/tvm/ffi/function.h
+++ b/ffi/include/tvm/ffi/function.h
@@ -453,8 +453,8 @@ class Function : public ObjectRef {
   static Function FromUnpacked(TCallable callable) {
     using FuncInfo = details::FunctionInfo<TCallable>;
     auto call_packed = [callable](const AnyView* args, int32_t num_args, Any* rv) mutable -> void {
-      details::unpack_call<typename FuncInfo::RetType, FuncInfo::num_args>(nullptr, callable, args,
-                                                                           num_args, rv);
+      details::unpack_call<typename FuncInfo::RetType>(
+          std::make_index_sequence<FuncInfo::num_args>{}, nullptr, callable, args, num_args, rv);
     };
     return FromPackedInternal(call_packed);
   }
@@ -469,8 +469,8 @@ class Function : public ObjectRef {
     using FuncInfo = details::FunctionInfo<TCallable>;
     auto call_packed = [callable, name](const AnyView* args, int32_t num_args,
                                         Any* rv) mutable -> void {
-      details::unpack_call<typename FuncInfo::RetType, FuncInfo::num_args>(&name, callable, args,
-                                                                           num_args, rv);
+      details::unpack_call<typename FuncInfo::RetType>(
+          std::make_index_sequence<FuncInfo::num_args>{}, &name, callable, args, num_args, rv);
     };
     return FromPackedInternal(call_packed);
   }
@@ -674,7 +674,16 @@ class TypedFunction<R(Args...)> {
    * \returns The return value.
    */
   TVM_FFI_INLINE R operator()(Args... args) const {
-    return details::typed_packed_call_dispatcher<R>::run(packed_, std::forward<Args>(args)...);
+    if constexpr (std::is_same_v<R, void>) {
+      packed_(std::forward<Args>(args)...);
+    } else {
+      Any res = packed_(std::forward<Args>(args)...);
+      if constexpr (std::is_same_v<R, Any>) {
+        return res;
+      } else {
+        return std::move(res).cast<R>();
+      }
+    }
   }
   /*!
    * \brief convert to PackedFunc
@@ -850,8 +859,6 @@ class Function::Registry {
     return *this;
   }
 
-  operator details::EmptyStruct() const { return details::EmptyStruct(); }
-
  protected:
   /*!
    * \brief set the body of the function to be f
@@ -875,8 +882,8 @@ inline int32_t TypeKeyToIndex(const char* type_key) {
   return type_index;
 }
 
-#define TVM_FFI_REG_VAR_DEF \
-  static inline TVM_FFI_ATTRIBUTE_UNUSED ::tvm::ffi::details::EmptyStruct __mk_##TVMFFI
+#define TVM_FFI_FUNC_REG_VAR_DEF \
+  static inline TVM_FFI_ATTRIBUTE_UNUSED ::tvm::ffi::Function::Registry& __##TVMFFIFuncReg
 
 /*!
  * \brief Register a function globally.
@@ -888,7 +895,7 @@ inline int32_t TypeKeyToIndex(const char* type_key) {
  * \endcode
  */
 #define TVM_FFI_REGISTER_GLOBAL(OpName) \
-  TVM_FFI_STR_CONCAT(TVM_FFI_REG_VAR_DEF, __COUNTER__) = ::tvm::ffi::Function::Registry(OpName)
+  TVM_FFI_STR_CONCAT(TVM_FFI_FUNC_REG_VAR_DEF, __COUNTER__) = ::tvm::ffi::Function::Registry(OpName)
 }  // namespace ffi
 }  // namespace tvm
 #endif  // TVM_FFI_FUNCTION_H_
diff --git a/ffi/include/tvm/ffi/function_details.h b/ffi/include/tvm/ffi/function_details.h
index 34e166428e..d82d80b6eb 100644
--- a/ffi/include/tvm/ffi/function_details.h
+++ b/ffi/include/tvm/ffi/function_details.h
@@ -36,14 +36,6 @@ namespace tvm {
 namespace ffi {
 namespace details {
 
-/*!
- * \brief Empty struct, used to reduce unused variable in global static initialization.
- */
-struct EmptyStruct {
-  /*! \brief at least one byte to ensure address */
-  uint8_t pad;
-};
-
 template <typename ArgType>
 struct Arg2Str {
   template <size_t i>
@@ -167,46 +159,9 @@ class ArgValueWithContext {
   FGetFuncSignature f_sig_;
 };
 
-template <typename R, int nleft, int index, typename F>
-struct unpack_call_dispatcher {
-  template <typename... Args>
-  TVM_FFI_INLINE static void run(const std::string* optional_name, FGetFuncSignature f_sig,
-                                 const F& f, const AnyView* args, int32_t num_args, Any* rv,
-                                 Args&&... unpacked_args) {
-    // construct a movable argument value
-    // which allows potential move of argument to the input of F.
-    unpack_call_dispatcher<R, nleft - 1, index + 1, F>::run(
-        optional_name, f_sig, f, args, num_args, rv, std::forward<Args>(unpacked_args)...,
-        ArgValueWithContext(args, index, optional_name, f_sig));
-  }
-};
-
-template <typename R, int index, typename F>
-struct unpack_call_dispatcher<R, 0, index, F> {
-  template <typename... Args>
-  TVM_FFI_INLINE static void run(const std::string*, FGetFuncSignature, const F& f, const AnyView*,
-                                 int32_t, Any* rv, Args&&... unpacked_args) {
-    using RetType = decltype(f(std::forward<Args>(unpacked_args)...));
-    if constexpr (std::is_same_v<RetType, R>) {
-      *rv = f(std::forward<Args>(unpacked_args)...);
-    } else {
-      *rv = R(f(std::forward<Args>(unpacked_args)...));
-    }
-  }
-};
-
-template <int index, typename F>
-struct unpack_call_dispatcher<void, 0, index, F> {
-  template <typename... Args>
-  TVM_FFI_INLINE static void run(const std::string*, FGetFuncSignature, const F& f, const AnyView*,
-                                 int32_t, Any*, Args&&... unpacked_args) {
-    f(std::forward<Args>(unpacked_args)...);
-  }
-};
-
-template <typename R, int nargs, typename F>
-TVM_FFI_INLINE void unpack_call(const std::string* optional_name, const F& f, const AnyView* args,
-                                int32_t num_args, Any* rv) {
+template <typename R, std::size_t... Is, typename F>
+TVM_FFI_INLINE void unpack_call(std::index_sequence<Is...>, const std::string* optional_name,
+                                const F& f, const AnyView* args, int32_t num_args, Any* rv) {
   using FuncInfo = FunctionInfo<F>;
   FGetFuncSignature f_sig = FuncInfo::Sig;
 
@@ -214,47 +169,20 @@ TVM_FFI_INLINE void unpack_call(const std::string* optional_name, const F& f, co
 #ifndef _MSC_VER
   static_assert(FuncInfo::unpacked_supported, "The function signature do not support unpacked");
 #endif
-
+  constexpr size_t nargs = sizeof...(Is);
   if (nargs != num_args) {
     TVM_FFI_THROW(TypeError) << "Mismatched number of arguments when calling: `"
                              << (optional_name == nullptr ? "" : *optional_name)
                              << (f_sig == nullptr ? "" : (*f_sig)()) << "`. Expected " << nargs
                              << " but got " << num_args << " arguments";
   }
-  unpack_call_dispatcher<R, nargs, 0, F>::run(optional_name, f_sig, f, args, num_args, rv);
-}
-
-template <typename FType>
-struct unpack_call_by_signature {};
-
-template <typename R, typename... Args>
-struct unpack_call_by_signature<R(Args...)> {
-  template <typename F>
-  TVM_FFI_INLINE static void run(const F& f, const AnyView* args, int32_t num_args, Any* rv) {
-    unpack_call<R, sizeof...(Args)>(nullptr, f, args, num_args, rv);
+  // use index sequence to do recursive-less unpacking
+  if constexpr (std::is_same_v<R, void>) {
+    f(ArgValueWithContext(args, Is, optional_name, f_sig)...);
+  } else {
+    *rv = R(f(ArgValueWithContext(args, Is, optional_name, f_sig)...));
   }
-};
-
-template <typename R>
-struct typed_packed_call_dispatcher {
-  template <typename F, typename... Args>
-  TVM_FFI_INLINE static R run(const F& f, Args&&... args) {
-    Any res = f(std::forward<Args>(args)...);
-    if constexpr (std::is_same_v<R, Any>) {
-      return res;
-    } else {
-      return std::move(res).cast<R>();
-    }
-  }
-};
-
-template <>
-struct typed_packed_call_dispatcher<void> {
-  template <typename F, typename... Args>
-  TVM_FFI_INLINE static void run(const F& f, Args&&... args) {
-    f(std::forward<Args>(args)...);
-  }
-};
+}
 
 /*!
  * \brief Move the safe call raised error to the caller
diff --git a/ffi/include/tvm/ffi/reflection/reflection.h b/ffi/include/tvm/ffi/reflection/reflection.h
index b1088c1eda..8ce2d22ddd 100644
--- a/ffi/include/tvm/ffi/reflection/reflection.h
+++ b/ffi/include/tvm/ffi/reflection/reflection.h
@@ -66,8 +66,6 @@ class ReflectionDef {
     return *this;
   }
 
-  operator details::EmptyStruct() const { return details::EmptyStruct(); }
-
  private:
   template <typename Class, typename T>
   void RegisterField(const char* name, T Class::*field_ptr, bool readonly) {
@@ -138,11 +136,14 @@ class ReflectionFieldGetter {
   const TVMFFIFieldInfo* field_info_;
 };
 
+#define TVM_FFI_REFLECTION_REG_VAR_DEF \
+  static inline TVM_FFI_ATTRIBUTE_UNUSED ::tvm::ffi::details::ReflectionDef& __TVMFFIReflectionReg
+
 /*!
  * helper macro to define a reflection definition for an object
  */
-#define TVM_FFI_REFLECTION_DEF(TypeName)                 \
-  TVM_FFI_STR_CONCAT(TVM_FFI_REG_VAR_DEF, __COUNTER__) = \
+#define TVM_FFI_REFLECTION_DEF(TypeName)                            \
+  TVM_FFI_STR_CONCAT(TVM_FFI_REFLECTION_REG_VAR_DEF, __COUNTER__) = \
       ::tvm::ffi::details::ReflectionDef(TypeName::_GetOrAllocRuntimeTypeIndex())
 }  // namespace details
 }  // namespace ffi
diff --git a/include/tvm/ir/env_func.h b/include/tvm/ir/env_func.h
index c83711756b..c44e102cca 100644
--- a/include/tvm/ir/env_func.h
+++ b/include/tvm/ir/env_func.h
@@ -139,7 +139,16 @@ class TypedEnvFunc<R(Args...)> : public ObjectRef {
   R operator()(Args... args) const {
     const EnvFuncNode* n = operator->();
     ICHECK(n != nullptr);
-    return ffi::details::typed_packed_call_dispatcher<R>::run(n->func, std::forward<Args>(args)...);
+    if constexpr (std::is_same_v<R, void>) {
+      n->func(std::forward<Args>(args)...);
+    } else {
+      Any res = n->func(std::forward<Args>(args)...);
+      if constexpr (std::is_same_v<R, Any>) {
+        return res;
+      } else {
+        return std::move(res).cast<R>();
+      }
+    }
   }
   /*! \brief specify container node */
   using ContainerType = EnvFuncNode;
diff --git a/include/tvm/runtime/packed_func.h b/include/tvm/runtime/packed_func.h
index 228ce731e0..3609987d55 100644
--- a/include/tvm/runtime/packed_func.h
+++ b/include/tvm/runtime/packed_func.h
@@ -343,7 +343,8 @@ struct ModuleVTableEntryHelper<R (T::*)(Args...) const> {
   using MemFnType = R (T::*)(Args...) const;
   static TVM_ALWAYS_INLINE void Call(TVMRetValue* rv, T* self, MemFnType f, TVMArgs args) {
     auto wrapped = [self, f](Args... args) -> R { return (self->*f)(std::forward<Args>(args)...); };
-    ffi::details::unpack_call<R, sizeof...(Args)>(nullptr, wrapped, args.data(), args.size(), rv);
+    ffi::details::unpack_call<R>(std::make_index_sequence<sizeof...(Args)>{}, nullptr, wrapped,
+                                 args.data(), args.size(), rv);
   }
 };
 
@@ -352,7 +353,8 @@ struct ModuleVTableEntryHelper<R (T::*)(Args...)> {
   using MemFnType = R (T::*)(Args...);
   static TVM_ALWAYS_INLINE void Call(TVMRetValue* rv, T* self, MemFnType f, TVMArgs args) {
     auto wrapped = [self, f](Args... args) -> R { return (self->*f)(std::forward<Args>(args)...); };
-    ffi::details::unpack_call<R, sizeof...(Args)>(nullptr, wrapped, args.data(), args.size(), rv);
+    ffi::details::unpack_call<R>(std::make_index_sequence<sizeof...(Args)>{}, nullptr, wrapped,
+                                 args.data(), args.size(), rv);
   }
 };
 
@@ -361,8 +363,8 @@ struct ModuleVTableEntryHelper<void (T::*)(Args...) const> {
   using MemFnType = void (T::*)(Args...) const;
   static TVM_ALWAYS_INLINE void Call(TVMRetValue* rv, T* self, MemFnType f, TVMArgs args) {
     auto wrapped = [self, f](Args... args) -> void { (self->*f)(std::forward<Args>(args)...); };
-    ffi::details::unpack_call<void, sizeof...(Args)>(nullptr, wrapped, args.data(), args.size(),
-                                                     rv);
+    ffi::details::unpack_call<void>(std::make_index_sequence<sizeof...(Args)>{}, nullptr, wrapped,
+                                    args.data(), args.size(), rv);
   }
 };
 
@@ -371,8 +373,8 @@ struct ModuleVTableEntryHelper<void (T::*)(Args...)> {
   using MemFnType = void (T::*)(Args...);
   static TVM_ALWAYS_INLINE void Call(TVMRetValue* rv, T* self, MemFnType f, TVMArgs args) {
     auto wrapped = [self, f](Args... args) -> void { (self->*f)(std::forward<Args>(args)...); };
-    ffi::details::unpack_call<void, sizeof...(Args)>(nullptr, wrapped, args.data(), args.size(),
-                                                     rv);
+    ffi::details::unpack_call<void>(std::make_index_sequence<sizeof...(Args)>{}, nullptr, wrapped,
+                                    args.data(), args.size(), rv);
   }
 };
 }  // namespace details
@@ -446,8 +448,9 @@ struct ModuleVTableEntryHelper<void (T::*)(Args...)> {
     TVM_FFI_SAFE_CALL_BEGIN();                                                               \
     using FuncInfo = ::tvm::ffi::details::FunctionInfo<decltype(Function)>;                  \
     static std::string name = #ExportName;                                                   \
-    ::tvm::ffi::details::unpack_call<typename FuncInfo::RetType, FuncInfo::num_args>(        \
-        &name, Function, reinterpret_cast<const ::tvm::ffi::AnyView*>(args), num_args,       \
+    ::tvm::ffi::details::unpack_call<typename FuncInfo::RetType>(                            \
+        std::make_index_sequence<FuncInfo::num_args>{}, &name, Function,                     \
+        reinterpret_cast<const ::tvm::ffi::AnyView*>(args), num_args,                        \
         reinterpret_cast<::tvm::ffi::Any*>(result));                                         \
     TVM_FFI_SAFE_CALL_END();                                                                 \
   }                                                                                          \
diff --git a/src/tir/schedule/instruction_traits.h b/src/tir/schedule/instruction_traits.h
index d292bd3dc7..29165750e8 100644
--- a/src/tir/schedule/instruction_traits.h
+++ b/src/tir/schedule/instruction_traits.h
@@ -319,8 +319,9 @@ Array<Any> UnpackedInstTraits<TTraits>::ApplyToSchedule(const Schedule& sch,
   PackedFunc pf([](const TVMArgs& args, TVMRetValue* rv) -> void {
     constexpr size_t kNumArgs = details::NumArgs<method_type>;
     ICHECK_EQ(args.size(), kNumArgs);
-    ffi::details::unpack_call<return_type, kNumArgs>(nullptr, TTraits::UnpackedApplyToSchedule,
-                                                     args.data(), args.size(), rv);
+    ffi::details::unpack_call<return_type>(std::make_index_sequence<kNumArgs>{}, nullptr,
+                                           TTraits::UnpackedApplyToSchedule, args.data(),
+                                           args.size(), rv);
   });
   ffi::Any rv;
   pf.CallPacked(ffi::PackedArgs(packed_args, kNumArgs), &rv);
@@ -349,8 +350,8 @@ String UnpackedInstTraits<TTraits>::AsPython(const Array<Any>& inputs, const Arr
   PackedFunc pf([](const TVMArgs& args, TVMRetValue* rv) -> void {
     constexpr size_t kNumArgs = details::NumArgs<method_type>;
     ICHECK_EQ(args.size(), kNumArgs);
-    ffi::details::unpack_call<return_type, kNumArgs>(nullptr, TTraits::UnpackedAsPython,
-                                                     args.data(), args.size(), rv);
+    ffi::details::unpack_call<return_type>(std::make_index_sequence<kNumArgs>{}, nullptr,
+                                           TTraits::UnpackedAsPython, args.data(), args.size(), rv);
   });
   ffi::Any rv;
   pf.CallPacked(ffi::PackedArgs(packed_args, kNumArgs), &rv);


(tvm) 06/06: [WIN] Remove lock to avoid mutex dep at init time

Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch refactor-s3
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit 251e89e8113af590839003fbb2c7bde20de1f8ec
Author: tqchen <ti...@gmail.com>
AuthorDate: Mon May 5 09:35:15 2025 -0400

    [WIN] Remove lock to avoid mutex dep at init time
---
 src/node/attr_registry.h | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/src/node/attr_registry.h b/src/node/attr_registry.h
index 2b022d3e45..12913ccefd 100644
--- a/src/node/attr_registry.h
+++ b/src/node/attr_registry.h
@@ -28,7 +28,6 @@
 #include <tvm/runtime/packed_func.h>
 
 #include <memory>
-#include <mutex>
 #include <unordered_map>
 #include <utility>
 #include <vector>
@@ -95,7 +94,6 @@ class AttrRegistry {
    */
   void UpdateAttr(const String& attr_name, const KeyType& key, Any value, int plevel) {
     using runtime::TVMRetValue;
-    std::lock_guard<std::mutex> lock(mutex_);
     auto& op_map = attrs_[attr_name];
     if (op_map == nullptr) {
       op_map.reset(new AttrRegistryMapContainerMap<KeyType>());
@@ -122,7 +120,6 @@ class AttrRegistry {
    * \param key The key to the attribute table.
    */
   void ResetAttr(const String& attr_name, const KeyType& key) {
-    std::lock_guard<std::mutex> lock(mutex_);
     auto& op_map = attrs_[attr_name];
     if (op_map == nullptr) {
       return;
@@ -139,7 +136,6 @@ class AttrRegistry {
    * \return The result attribute map.
    */
   const AttrRegistryMapContainerMap<KeyType>& GetAttrMap(const String& attr_name) {
-    std::lock_guard<std::mutex> lock(mutex_);
     auto it = attrs_.find(attr_name);
     if (it == attrs_.end()) {
       LOG(FATAL) << "Attribute \'" << attr_name << "\' is not registered";
@@ -153,7 +149,6 @@ class AttrRegistry {
    * \return The check result.
    */
   bool HasAttrMap(const String& attr_name) {
-    std::lock_guard<std::mutex> lock(mutex_);
     return attrs_.count(attr_name);
   }
 
@@ -166,8 +161,6 @@ class AttrRegistry {
   }
 
  private:
-  // mutex to avoid registration from multiple threads.
-  std::mutex mutex_;
   // entries in the registry
   std::vector<std::unique_ptr<EntryType>> entries_;
   // map from name to entries.


(tvm) 01/06: Bring custom hook to ffi layer

Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch refactor-s3
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit ab7a2c3e618c7f5d046e47a319fc52d4d3be3727
Author: tqchen <ti...@gmail.com>
AuthorDate: Sun May 4 13:41:42 2025 -0400

    Bring custom hook to ffi layer
---
 python/tvm/_ffi/__init__.py                        |  2 +-
 python/tvm/_ffi/base.py                            |  9 ++--
 python/tvm/ffi/cython/dtype.pxi                    |  2 +-
 python/tvm/ffi/cython/object.pxi                   | 62 ++++++++++++++++++++++
 python/tvm/ffi/cython/string.pxi                   |  2 +
 python/tvm/ffi/dtype.py                            |  2 +-
 python/tvm/ir/container.py                         |  2 +
 python/tvm/meta_schedule/cost_model/mlp_model.py   |  4 +-
 python/tvm/rpc/client.py                           |  2 +-
 python/tvm/runtime/__init__.py                     |  2 +-
 python/tvm/runtime/_ffi_node_api.py                |  3 --
 python/tvm/runtime/container.py                    |  3 ++
 python/tvm/runtime/device.py                       |  5 +-
 python/tvm/runtime/module.py                       |  2 +-
 python/tvm/runtime/ndarray.py                      |  2 +-
 python/tvm/runtime/object.py                       | 62 +++++-----------------
 python/tvm/tir/expr.py                             |  4 +-
 src/node/structural_hash.cc                        | 30 +++++++++++
 src/support/ffi_testing.cc                         |  1 -
 tests/python/ffi/test_string.py                    |  9 ++++
 .../python/tvmscript/test_tvmscript_printer_doc.py |  2 -
 .../test_tvmscript_printer_structural_equal.py     |  5 +-
 22 files changed, 140 insertions(+), 77 deletions(-)

diff --git a/python/tvm/_ffi/__init__.py b/python/tvm/_ffi/__init__.py
index 4e8e59b3a8..559ca84635 100644
--- a/python/tvm/_ffi/__init__.py
+++ b/python/tvm/_ffi/__init__.py
@@ -28,4 +28,4 @@ from . import _pyversion
 from . import base
 from .registry import register_object, register_func
 from .registry import _init_api, get_global_func
-from tvm.ffi import register_error
+from ..ffi import register_error
diff --git a/python/tvm/_ffi/base.py b/python/tvm/_ffi/base.py
index 76293cf125..18ed40fb4c 100644
--- a/python/tvm/_ffi/base.py
+++ b/python/tvm/_ffi/base.py
@@ -18,13 +18,9 @@
 # pylint: disable=invalid-name, import-outside-toplevel
 """Base library for TVM FFI."""
 import ctypes
-import functools
 import os
-import re
 import sys
-import types
 
-from typing import Callable, Sequence, Optional
 
 import numpy as np
 
@@ -64,10 +60,11 @@ _LIB, _LIB_NAME = _load_lib()
 # Whether we are runtime only
 _RUNTIME_ONLY = "runtime" in _LIB_NAME
 
-import tvm.ffi.registry
 
 if _RUNTIME_ONLY:
-    tvm.ffi.registry._SKIP_UNKNOWN_OBJECTS = True
+    from ..ffi import registry as _tvm_ffi_registry
+
+    _tvm_ffi_registry._SKIP_UNKNOWN_OBJECTS = True
 
 # The FFI mode of TVM
 _FFI_MODE = os.environ.get("TVM_FFI", "auto")
diff --git a/python/tvm/ffi/cython/dtype.pxi b/python/tvm/ffi/cython/dtype.pxi
index ec045fce65..bbf9e60053 100644
--- a/python/tvm/ffi/cython/dtype.pxi
+++ b/python/tvm/ffi/cython/dtype.pxi
@@ -29,7 +29,7 @@ def _create_dtype_from_tuple(cls, code, bits, lanes):
     cdtype.bits = bits
     cdtype.lanes = lanes
     ret = cls.__new__(cls)
-    (<DLDataType>ret).cdtype = cdtype
+    (<DataType>ret).cdtype = cdtype
     return ret
 
 
diff --git a/python/tvm/ffi/cython/object.pxi b/python/tvm/ffi/cython/object.pxi
index 9abbb40195..1ac32c3bc6 100644
--- a/python/tvm/ffi/cython/object.pxi
+++ b/python/tvm/ffi/cython/object.pxi
@@ -32,6 +32,31 @@ def __object_repr__(obj):
     return type(obj).__name__ + "(" + obj.__ctypes_handle__().value + ")"
 
 
+def __object_save_json__(obj):
+    """Object repr function that can be overridden by assigning to it"""
+    raise NotImplementedError("JSON serialization depends on downstream init")
+
+
+def __object_load_json__(json_str):
+    """Object repr function that can be overridden by assigning to it"""
+    raise NotImplementedError("JSON serialization depends on downstream init")
+
+
+def __object_dir__(obj):
+    """Object dir function that can be overridden by assigning to it"""
+    return []
+
+
+def __object_getattr__(obj, name):
+    """Object getattr function that can be overridden by assigning to it"""
+    raise AttributeError()
+
+
+def _new_object(cls):
+    """Helper function for pickle"""
+    return cls.__new__(cls)
+
+
 class ObjectGeneric:
     """Base class for all classes that can be converted to object."""
 
@@ -71,9 +96,46 @@ cdef class Object:
         cdef uint64_t chandle = <uint64_t>self.chandle
         return chandle
 
+    def __reduce__(self):
+        cls = type(self)
+        return (_new_object, (cls,), self.__getstate__())
+
+    def __getstate__(self):
+        if not self.__chandle__() == 0:
+            # need to explicit convert to str in case String
+            # returned and triggered another infinite recursion in get state
+            return {"handle": str(__object_save_json__(self))}
+        return {"handle": None}
+
+    def __setstate__(self, state):
+        # pylint: disable=assigning-non-slot, assignment-from-no-return
+        handle = state["handle"]
+        if handle is not None:
+            self.__init_handle_by_constructor__(__object_load_json__, handle)
+        else:
+            self.chandle = NULL
+
+    def __getattr__(self, name):
+        try:
+            return __object_getattr__(self, name)
+        except AttributeError:
+            raise AttributeError(f"{type(self)} has no attribute {name}")
+
+    def __dir__(self):
+        return __object_dir__(self)
+
     def __repr__(self):
         return __object_repr__(self)
 
+    def __eq__(self, other):
+        return self.same_as(other)
+
+    def __ne__(self, other):
+        return not self.__eq__(other)
+
+    def __init_handle_by_load_json__(self, json_str):
+        raise NotImplementedError("JSON serialization depends on downstream init")
+
     def __init_handle_by_constructor__(self, fconstructor, *args):
         """Initialize the handle by calling constructor function.
 
diff --git a/python/tvm/ffi/cython/string.pxi b/python/tvm/ffi/cython/string.pxi
index 00ec92b7ec..733ea90301 100644
--- a/python/tvm/ffi/cython/string.pxi
+++ b/python/tvm/ffi/cython/string.pxi
@@ -65,6 +65,7 @@ class String(str, PyNativeObject):
         val.__tvm_ffi_object__ = obj
         return val
 
+
 _register_object_by_index(kTVMFFIStr, String)
 
 
@@ -89,6 +90,7 @@ class Bytes(bytes, PyNativeObject):
         val.__tvm_ffi_object__ = obj
         return val
 
+
 _register_object_by_index(kTVMFFIBytes, Bytes)
 
 # We special handle str/bytes constructor in cython to avoid extra cyclic deps
diff --git a/python/tvm/ffi/dtype.py b/python/tvm/ffi/dtype.py
index ca98726560..56b888316d 100644
--- a/python/tvm/ffi/dtype.py
+++ b/python/tvm/ffi/dtype.py
@@ -17,9 +17,9 @@
 """dtype class."""
 # pylint: disable=invalid-name
 from enum import IntEnum
+import numpy as np
 
 from . import core
-import numpy as np
 
 
 class DataTypeCode(IntEnum):
diff --git a/python/tvm/ir/container.py b/python/tvm/ir/container.py
index 6a013bce8c..4bc6fcae21 100644
--- a/python/tvm/ir/container.py
+++ b/python/tvm/ir/container.py
@@ -16,3 +16,5 @@
 # under the License.
 """Additional container data structures used across IR variants."""
 from tvm.ffi import Array, Map
+
+__all__ = ["Array", "Map"]
diff --git a/python/tvm/meta_schedule/cost_model/mlp_model.py b/python/tvm/meta_schedule/cost_model/mlp_model.py
index 8bd050b689..4ee5ba838d 100644
--- a/python/tvm/meta_schedule/cost_model/mlp_model.py
+++ b/python/tvm/meta_schedule/cost_model/mlp_model.py
@@ -541,7 +541,9 @@ class State:
                                 "_workload.json", "_candidates.json"
                             ),
                         )
-                    except tvm._ffi.base.TVMError:  # pylint: disable=protected-access
+                    except (
+                        tvm._ffi.base.TVMError
+                    ):  # pylint: disable=protected-access,broad-exception-caught
                         continue
                     candidates, results = [], []
                     tuning_records = database.get_all_tuning_records()
diff --git a/python/tvm/rpc/client.py b/python/tvm/rpc/client.py
index cf9706c348..f9e677e49e 100644
--- a/python/tvm/rpc/client.py
+++ b/python/tvm/rpc/client.py
@@ -14,7 +14,7 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-# pylint: disable=used-before-assignment
+# pylint: disable=used-before-assignment,broad-exception-caught
 """RPC client tools"""
 import os
 import socket
diff --git a/python/tvm/runtime/__init__.py b/python/tvm/runtime/__init__.py
index a630ce1101..774c8dd635 100644
--- a/python/tvm/runtime/__init__.py
+++ b/python/tvm/runtime/__init__.py
@@ -41,7 +41,7 @@ from .params import (
     load_param_dict_from_file,
 )
 
-from tvm.ffi import convert, dtype as DataType, DataTypeCode
 from . import disco
 
 from .support import _regex_match
+from ..ffi import convert, dtype as DataType, DataTypeCode
diff --git a/python/tvm/runtime/_ffi_node_api.py b/python/tvm/runtime/_ffi_node_api.py
index 9623a87351..395496d16b 100644
--- a/python/tvm/runtime/_ffi_node_api.py
+++ b/python/tvm/runtime/_ffi_node_api.py
@@ -48,6 +48,3 @@ def LoadJSON(json_str):
 # Exports functions registered via TVM_REGISTER_GLOBAL with the "node" prefix.
 # e.g. TVM_REGISTER_GLOBAL("node.AsRepr")
 tvm._ffi._init_api("node", __name__)
-
-# override the default repr function for tvm.ffi.core.Object
-tvm.ffi.core.__object_repr__ = AsRepr
diff --git a/python/tvm/runtime/container.py b/python/tvm/runtime/container.py
index 052701f0d3..3bf149d6b2 100644
--- a/python/tvm/runtime/container.py
+++ b/python/tvm/runtime/container.py
@@ -16,3 +16,6 @@
 # under the License.
 """Runtime container structures."""
 from tvm.ffi import String, Shape as ShapeTuple
+
+
+__all__ = ["ShapeTuple", "String"]
diff --git a/python/tvm/runtime/device.py b/python/tvm/runtime/device.py
index b83bb8cceb..d9d6abce50 100644
--- a/python/tvm/runtime/device.py
+++ b/python/tvm/runtime/device.py
@@ -16,13 +16,12 @@
 # under the License.
 """Common runtime ctypes."""
 # pylint: disable=invalid-name
+import json
+
 import tvm.ffi
-from tvm.ffi import dtype as DataType, DataTypeCode
 
 from . import _ffi_api
 
-import json
-
 
 RPC_SESS_MASK = 128
 
diff --git a/python/tvm/runtime/module.py b/python/tvm/runtime/module.py
index 657120f907..bb1fbb5fe3 100644
--- a/python/tvm/runtime/module.py
+++ b/python/tvm/runtime/module.py
@@ -102,7 +102,7 @@ class Module(tvm.ffi.Object):
     """Runtime Module."""
 
     def __new__(cls):
-        instance = super().__new__(cls)
+        instance = super(Module, cls).__new__(cls)  # pylint: disable=no-value-for-parameter
         instance.entry_name = "__tvm_main__"
         instance._entry = None
         return instance
diff --git a/python/tvm/runtime/ndarray.py b/python/tvm/runtime/ndarray.py
index f1a7d1c2f7..5581adbbc1 100644
--- a/python/tvm/runtime/ndarray.py
+++ b/python/tvm/runtime/ndarray.py
@@ -33,7 +33,7 @@ import tvm.ffi
 from . import _ffi_api
 
 
-from tvm.ffi import (
+from ..ffi import (
     device,
     cpu,
     cuda,
diff --git a/python/tvm/runtime/object.py b/python/tvm/runtime/object.py
index 2aa2c08632..688682d197 100644
--- a/python/tvm/runtime/object.py
+++ b/python/tvm/runtime/object.py
@@ -17,58 +17,22 @@
 # pylint: disable=invalid-name, unused-import
 """Runtime Object API"""
 
+from tvm.ffi.core import Object
 import tvm.ffi.core
+from . import _ffi_node_api
 
-from . import _ffi_api, _ffi_node_api
 
-
-def _new_object(cls):
-    """Helper function for pickle"""
-    return cls.__new__(cls)
-
-
-class Object(tvm.ffi.core.Object):
-    """Base class for all tvm's runtime objects."""
-
-    __slots__ = []
-
-    def __dir__(self):
-        class_names = dir(self.__class__)
-        fnames = _ffi_node_api.NodeListAttrNames(self)
-        size = fnames(-1)
-        return sorted([fnames(i) for i in range(size)] + class_names)
-
-    def __getattr__(self, name):
-        try:
-            return _ffi_node_api.NodeGetAttr(self, name)
-        except AttributeError:
-            raise AttributeError(f"{type(self)} has no attribute {name}") from None
-
-    def __hash__(self):
-        return _ffi_api.ObjectPtrHash(self)
-
-    def __eq__(self, other):
-        return self.same_as(other)
-
-    def __ne__(self, other):
-        return not self.__eq__(other)
-
-    def __reduce__(self):
-        cls = type(self)
-        return (_new_object, (cls,), self.__getstate__())
-
-    def __getstate__(self):
-        if not self.__chandle__() == 0:
-            # need to explicit convert to str in case String
-            # returned and triggered another infinite recursion in get state
-            return {"handle": str(_ffi_node_api.SaveJSON(self))}
-        return {"handle": None}
-
-    def __setstate__(self, state):
-        # pylint: disable=assigning-non-slot, assignment-from-no-return
-        handle = state["handle"]
-        if handle is not None:
-            self.__init_handle_by_constructor__(_ffi_node_api.LoadJSON, handle)
+def __object_dir__(obj):
+    class_names = dir(obj.__class__)
+    fnames = _ffi_node_api.NodeListAttrNames(obj)
+    size = fnames(-1)
+    return sorted([fnames(i) for i in range(size)] + class_names)
 
 
 tvm.ffi.core._set_class_object(Object)
+# override the default repr function for tvm.ffi.core.Object
+tvm.ffi.core.__object_repr__ = _ffi_node_api.AsRepr
+tvm.ffi.core.__object_save_json__ = _ffi_node_api.SaveJSON
+tvm.ffi.core.__object_load_json__ = _ffi_node_api.LoadJSON
+tvm.ffi.core.__object_getattr__ = _ffi_node_api.NodeGetAttr
+tvm.ffi.core.__object_dir__ = __object_dir__
diff --git a/python/tvm/tir/expr.py b/python/tvm/tir/expr.py
index 13e10ba3ac..b293343cae 100644
--- a/python/tvm/tir/expr.py
+++ b/python/tvm/tir/expr.py
@@ -69,7 +69,7 @@ def _dtype_is_float(value):
     )  # type: ignore
 
 
-class ExprOp(object):
+class ExprOp:
     """Operator overloading for Expr like expressions."""
 
     # TODO(tkonolige): use inspect to add source information to these objects
@@ -395,7 +395,7 @@ class SizeVar(Var):
 
 
 @tvm._ffi.register_object("tir.IterVar")
-class IterVar(Object, ExprOp, Scriptable):
+class IterVar(ExprOp, Object, Scriptable):
     """Represent iteration variable.
 
     IterVar represents axis iterations in the computation.
diff --git a/src/node/structural_hash.cc b/src/node/structural_hash.cc
index bba8a59647..94e6768203 100644
--- a/src/node/structural_hash.cc
+++ b/src/node/structural_hash.cc
@@ -328,6 +328,22 @@ struct StringObjTrait {
   }
 };
 
+struct BytesObjTrait {
+  static constexpr const std::nullptr_t VisitAttrs = nullptr;
+
+  static void SHashReduce(const ffi::BytesObj* key, SHashReducer hash_reduce) {
+    hash_reduce->SHashReduceHashedValue(ffi::details::StableHashBytes(key->data, key->size));
+  }
+
+  static bool SEqualReduce(const ffi::BytesObj* lhs, const ffi::BytesObj* rhs,
+                           SEqualReducer equal) {
+    if (lhs == rhs) return true;
+    if (lhs->size != rhs->size) return false;
+    if (lhs->data == rhs->data) return true;
+    return std::memcmp(lhs->data, rhs->data, lhs->size) == 0;
+  }
+};
+
 struct RefToObjectPtr : public ObjectRef {
   static ObjectPtr<Object> Get(const ObjectRef& ref) {
     return ffi::details::ObjectUnsafe::ObjectPtrFromObjectRef<Object>(ref);
@@ -350,6 +366,20 @@ TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
       p->stream << '"' << support::StrEscape(op->data, op->size) << '"';
     });
 
+TVM_REGISTER_REFLECTION_VTABLE(ffi::BytesObj, BytesObjTrait)
+    .set_creator([](const std::string& bytes) {
+      return RefToObjectPtr::Get(runtime::String(bytes));
+    })
+    .set_repr_bytes([](const Object* n) -> std::string {
+      return GetRef<ffi::Bytes>(static_cast<const ffi::BytesObj*>(n)).operator std::string();
+    });
+
+TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable)
+    .set_dispatch<ffi::BytesObj>([](const ObjectRef& node, ReprPrinter* p) {
+      auto* op = static_cast<const ffi::BytesObj*>(node.get());
+      p->stream << "b\"" << support::StrEscape(op->data, op->size) << '"';
+    });
+
 struct ModuleNodeTrait {
   static constexpr const std::nullptr_t VisitAttrs = nullptr;
   static constexpr const std::nullptr_t SHashReduce = nullptr;
diff --git a/src/support/ffi_testing.cc b/src/support/ffi_testing.cc
index cfd5c42f6e..9e727f06d4 100644
--- a/src/support/ffi_testing.cc
+++ b/src/support/ffi_testing.cc
@@ -71,7 +71,6 @@ TVM_REGISTER_GLOBAL("testing.test_wrap_callback_suppress_err")
       *ret = result;
     });
 
-
 TVM_REGISTER_GLOBAL("testing.test_check_eq_callback")
     .set_body_packed([](TVMArgs args, TVMRetValue* ret) {
       auto msg = args[0].cast<std::string>();
diff --git a/tests/python/ffi/test_string.py b/tests/python/ffi/test_string.py
index 67c82d52f7..98eab5bcb7 100644
--- a/tests/python/ffi/test_string.py
+++ b/tests/python/ffi/test_string.py
@@ -1,3 +1,4 @@
+import pickle
 from tvm import ffi as tvm_ffi
 
 
@@ -12,6 +13,10 @@ def test_string():
     assert isinstance(s3, tvm_ffi.String)
     assert isinstance(s3, str)
 
+    s4 = pickle.loads(pickle.dumps(s))
+    assert s4 == "hello"
+    assert isinstance(s4, tvm_ffi.String)
+
 
 def test_bytes():
     fecho = tvm_ffi.get_global_func("testing.echo")
@@ -27,3 +32,7 @@ def test_bytes():
     b4 = tvm_ffi.convert(bytearray(b"hello"))
     assert isinstance(b4, tvm_ffi.Bytes)
     assert isinstance(b4, bytes)
+
+    b5 = pickle.loads(pickle.dumps(b))
+    assert b5 == b"hello"
+    assert isinstance(b5, tvm_ffi.Bytes)
diff --git a/tests/python/tvmscript/test_tvmscript_printer_doc.py b/tests/python/tvmscript/test_tvmscript_printer_doc.py
index 6353627c58..e3d1280b32 100644
--- a/tests/python/tvmscript/test_tvmscript_printer_doc.py
+++ b/tests/python/tvmscript/test_tvmscript_printer_doc.py
@@ -307,7 +307,6 @@ def test_assign_doc(lhs, rhs, annotation):
 def test_invalid_assign_doc(lhs, rhs, annotation):
     with pytest.raises(ValueError) as e:
         AssignDoc(lhs, rhs, annotation)
-    assert "AssignDoc" in str(e.value)
 
 
 @pytest.mark.parametrize(
@@ -332,7 +331,6 @@ def test_if_doc(then_branch, else_branch):
     if not then_branch and not else_branch:
         with pytest.raises(ValueError) as e:
             IfDoc(predicate, then_branch, else_branch)
-        assert "IfDoc" in str(e.value)
         return
     else:
         doc = IfDoc(predicate, then_branch, else_branch)
diff --git a/tests/python/tvmscript/test_tvmscript_printer_structural_equal.py b/tests/python/tvmscript/test_tvmscript_printer_structural_equal.py
index 6f67733a28..58d9402e6f 100644
--- a/tests/python/tvmscript/test_tvmscript_printer_structural_equal.py
+++ b/tests/python/tvmscript/test_tvmscript_printer_structural_equal.py
@@ -24,12 +24,11 @@ from tvm.script import ir as I, tir as T
 
 
 def _error_message(exception):
-    splitter = "ValueError: StructuralEqual"
-    return splitter + str(exception).split(splitter)[1]
+    return str(exception)
 
 
 def _expected_result(func1, func2, objpath1, objpath2):
-    return f"""ValueError: StructuralEqual check failed, caused by lhs at {objpath1}:
+    return f"""StructuralEqual check failed, caused by lhs at {objpath1}:
 {func1.script(path_to_underline=[objpath1], syntax_sugar=False)}
 and rhs at {objpath2}:
 {func2.script(path_to_underline=[objpath2], syntax_sugar=False)}"""


(tvm) 02/06: Fix through relax

Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch refactor-s3
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit 3aaf85cbd7ee4bbb4a1d29885ba3d5f8615b9c4f
Author: tqchen <ti...@gmail.com>
AuthorDate: Sun May 4 19:41:53 2025 -0400

    Fix through relax
---
 python/tvm/ffi/container.py                        |  3 ++
 python/tvm/ffi/cython/dtype.pxi                    |  2 +-
 python/tvm/ffi/cython/object.pxi                   |  3 +-
 python/tvm/ffi/dtype.py                            | 21 +++++++++
 python/tvm/meta_schedule/cost_model/mlp_model.py   |  5 +-
 python/tvm/relax/op/create.py                      |  2 +-
 python/tvm/runtime/ndarray.py                      |  4 +-
 .../test_runtime_packed_func.py                    |  1 -
 .../contrib/test_hexagon/test_parallel_scalar.py   |  6 +--
 tests/python/ffi/test_dtype.py                     |  8 ++++
 tests/python/ffi/test_string.py                    | 17 +++++++
 tests/python/relax/test_op_inspect.py              | 53 +++++-----------------
 tests/python/relax/test_op_misc.py                 |  2 +-
 tests/python/relax/test_vm_build.py                |  8 ++--
 tests/python/relax/test_vm_callback_function.py    | 11 ++---
 tests/python/relax/test_vm_codegen_only.py         |  2 +-
 16 files changed, 79 insertions(+), 69 deletions(-)

diff --git a/python/tvm/ffi/container.py b/python/tvm/ffi/container.py
index ddddd2d7cc..6ababe2557 100644
--- a/python/tvm/ffi/container.py
+++ b/python/tvm/ffi/container.py
@@ -100,6 +100,9 @@ class KeysView(collections.abc.KeysView):
             if not functor(2):
                 break
 
+    def __contains__(self, k):
+        return self._backend_map.__contains__(k)
+
 
 class ValuesView(collections.abc.ValuesView):
     """Helper class to return values view"""
diff --git a/python/tvm/ffi/cython/dtype.pxi b/python/tvm/ffi/cython/dtype.pxi
index bbf9e60053..ef71ea4edd 100644
--- a/python/tvm/ffi/cython/dtype.pxi
+++ b/python/tvm/ffi/cython/dtype.pxi
@@ -28,7 +28,7 @@ def _create_dtype_from_tuple(cls, code, bits, lanes):
     cdtype.code = code
     cdtype.bits = bits
     cdtype.lanes = lanes
-    ret = cls.__new__(cls)
+    ret = cls.__new__(cls, str(cdtype))
     (<DataType>ret).cdtype = cdtype
     return ret
 
diff --git a/python/tvm/ffi/cython/object.pxi b/python/tvm/ffi/cython/object.pxi
index 1ac32c3bc6..c258f578b0 100644
--- a/python/tvm/ffi/cython/object.pxi
+++ b/python/tvm/ffi/cython/object.pxi
@@ -125,7 +125,8 @@ cdef class Object:
         return __object_dir__(self)
 
     def __repr__(self):
-        return __object_repr__(self)
+        # make sure repr is a raw string
+        return str(__object_repr__(self))
 
     def __eq__(self, other):
         return self.same_as(other)
diff --git a/python/tvm/ffi/dtype.py b/python/tvm/ffi/dtype.py
index 56b888316d..32986a4eb0 100644
--- a/python/tvm/ffi/dtype.py
+++ b/python/tvm/ffi/dtype.py
@@ -84,6 +84,27 @@ class dtype(str):
     def __repr__(self):
         return f"dtype('{self}')"
 
+    def with_lanes(self, lanes):
+        """
+        Create a new dtype with the given number of lanes.
+
+        Parameters
+        ----------
+        lanes : int
+            The number of lanes.
+
+        Returns
+        -------
+        dtype
+            The new dtype with the given number of lanes.
+        """
+        cdtype = core._create_dtype_from_tuple(
+            core.DataType, self.__tvm_ffi_dtype__.type_code, self.__tvm_ffi_dtype__.bits, lanes
+        )
+        val = str.__new__(dtype, str(cdtype))
+        val.__tvm_ffi_dtype__ = cdtype
+        return val
+
     @property
     def itemsize(self):
         return self.__tvm_ffi_dtype__.itemsize
diff --git a/python/tvm/meta_schedule/cost_model/mlp_model.py b/python/tvm/meta_schedule/cost_model/mlp_model.py
index 4ee5ba838d..9167d30e90 100644
--- a/python/tvm/meta_schedule/cost_model/mlp_model.py
+++ b/python/tvm/meta_schedule/cost_model/mlp_model.py
@@ -534,6 +534,7 @@ class State:
                         if json_file.endswith("_workload.json"):
                             workload_paths.append(json_file)
                 for workload_path in tqdm(workload_paths):
+                    # pylint: disable=protected-access,broad-exception-caught
                     try:
                         database = JSONDatabase(
                             path_workload=workload_path,
@@ -541,9 +542,7 @@ class State:
                                 "_workload.json", "_candidates.json"
                             ),
                         )
-                    except (
-                        tvm._ffi.base.TVMError
-                    ):  # pylint: disable=protected-access,broad-exception-caught
+                    except tvm._ffi.base.TVMError:
                         continue
                     candidates, results = [], []
                     tuning_records = database.get_all_tuning_records()
diff --git a/python/tvm/relax/op/create.py b/python/tvm/relax/op/create.py
index 029682ded8..c61d9521a4 100644
--- a/python/tvm/relax/op/create.py
+++ b/python/tvm/relax/op/create.py
@@ -17,7 +17,7 @@
 """Creation operators."""
 from typing import Optional, Tuple, Union
 
-from tvm import DataType
+from tvm import DataType, DataTypeCode
 from tvm.ir.expr import PrimExpr
 
 from . import _ffi_api
diff --git a/python/tvm/runtime/ndarray.py b/python/tvm/runtime/ndarray.py
index 5581adbbc1..e2ac40ef66 100644
--- a/python/tvm/runtime/ndarray.py
+++ b/python/tvm/runtime/ndarray.py
@@ -92,7 +92,7 @@ class NDArray(tvm.ffi.core.NDArray):
         ):
             raise ValueError("Array only support set from numpy array")
         if isinstance(value, NDArray):
-            if value.handle is not self.handle:
+            if not value.same_as(self):
                 value.copyto(self)
         elif isinstance(value, (np.ndarray, np.generic)):
             self.copyfrom(value)
@@ -128,7 +128,7 @@ class NDArray(tvm.ffi.core.NDArray):
         shape, dtype = self.shape, self.dtype
         if t.lanes > 1:
             shape = shape + (t.lanes,)
-            t.lanes = 1
+            t = t.with_lanes(1)
             dtype = str(t)
 
         if source_array.shape != shape:
diff --git a/tests/python/all-platform-minimal-test/test_runtime_packed_func.py b/tests/python/all-platform-minimal-test/test_runtime_packed_func.py
index f24e1edde7..f315b8f3c2 100644
--- a/tests/python/all-platform-minimal-test/test_runtime_packed_func.py
+++ b/tests/python/all-platform-minimal-test/test_runtime_packed_func.py
@@ -45,7 +45,6 @@ def test_get_callback_with_node():
     x = T.int32(10)
 
     def test(y):
-        assert y.handle != x.handle
         return y
 
     f2 = tvm.runtime.convert(test)
diff --git a/tests/python/contrib/test_hexagon/test_parallel_scalar.py b/tests/python/contrib/test_hexagon/test_parallel_scalar.py
index d7fd5a3b20..bd9c78d5da 100644
--- a/tests/python/contrib/test_hexagon/test_parallel_scalar.py
+++ b/tests/python/contrib/test_hexagon/test_parallel_scalar.py
@@ -117,11 +117,7 @@ def evaluate(hexagon_session, operations, expected, sch):
 class TestMatMulVec:
     """MatMul test class."""
 
-    (
-        operation_name,
-        operator_producer,
-        expected_output_producer,
-    ) = tvm.testing.parameters(
+    (operation_name, operator_producer, expected_output_producer,) = tvm.testing.parameters(
         ("add", get_add_operator, (lambda a, b: a + b)),
         ("mul", get_multiply_operator, (lambda a, b: a * b)),
         ("sub", get_sub_operator, (lambda a, b: a - b)),
diff --git a/tests/python/ffi/test_dtype.py b/tests/python/ffi/test_dtype.py
index fb6e14a17a..2758edf9d6 100644
--- a/tests/python/ffi/test_dtype.py
+++ b/tests/python/ffi/test_dtype.py
@@ -54,3 +54,11 @@ def test_dtype_pickle(dtype_str):
     assert dtype_pickled.type_code == dtype.type_code
     assert dtype_pickled.bits == dtype.bits
     assert dtype_pickled.lanes == dtype.lanes
+
+
+def test_dtype_with_lanes():
+    dtype = tvm_ffi.dtype("float32")
+    dtype_with_lanes = dtype.with_lanes(4)
+    assert dtype_with_lanes.type_code == dtype.type_code
+    assert dtype_with_lanes.bits == dtype.bits
+    assert dtype_with_lanes.lanes == 4
diff --git a/tests/python/ffi/test_string.py b/tests/python/ffi/test_string.py
index 98eab5bcb7..cac948b53d 100644
--- a/tests/python/ffi/test_string.py
+++ b/tests/python/ffi/test_string.py
@@ -1,3 +1,20 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
 import pickle
 from tvm import ffi as tvm_ffi
 
diff --git a/tests/python/relax/test_op_inspect.py b/tests/python/relax/test_op_inspect.py
index f8326db8dd..2ba9f9a709 100644
--- a/tests/python/relax/test_op_inspect.py
+++ b/tests/python/relax/test_op_inspect.py
@@ -158,10 +158,6 @@ def test_strides_of_compact_tensor(shape):
 
 
 def test_strides_of_non_compact_tensor():
-    backing_shape = [64, 64]
-    view_shape = [16, 16]
-    expected_strides = [backing_shape[0], 1]
-
     @I.ir_module
     class mod:
         @R.function
@@ -170,20 +166,12 @@ def test_strides_of_non_compact_tensor():
 
     built = tvm.compile(mod)
     vm = relax.VirtualMachine(built, tvm.cpu())
-
-    backing_ndarray = tvm.nd.empty(backing_shape, "int32")
-
-    # Manually overwrite the DLTensor fields to make a view into the
-    # tensor.
-    view = backing_ndarray.handle[0]
-    np_shape = np.array([16, 16], "int64")
-    view.shape = np_shape.ctypes.data_as(ctypes.POINTER(ctypes.c_long))
-    np_strides = np.array([64, 1], "int64")
-    view.strides = np_strides.ctypes.data_as(ctypes.POINTER(ctypes.c_long))
-    backing_ndarray.handle[0] = view
-
-    res = [vm["main"](backing_ndarray, i) for i, _ in enumerate(view_shape)]
-
+    view_shape = [4, 4]
+    expected_strides = [1, 4]
+    # use transpose to make strides non-compact
+    x = np.zeros([4, 4], "int32").T
+    y = tvm.ffi.from_dlpack(x, required_alignment=4, required_contiguous=False)
+    res = [vm["main"](y, i) for i, _ in enumerate(view_shape)]
     tvm.ir.assert_structural_equal(res, expected_strides)
 
 
@@ -200,19 +188,10 @@ def test_byte_offset(elem_offset):
 
     built = tvm.compile(mod)
     vm = relax.VirtualMachine(built, tvm.cpu())
-
-    backing_ndarray = tvm.nd.empty(backing_shape, "int32")
-
-    # Manually overwrite the DLTensor fields to make a view into the
-    # tensor.
-    view = backing_ndarray.handle[0]
-    np_shape = np.array(view_shape, "int64")
-    view.shape = np_shape.ctypes.data_as(ctypes.POINTER(ctypes.c_long))
-    view.byte_offset = byte_offset
-    backing_ndarray.handle[0] = view
-
-    res = vm["main"](backing_ndarray)
-
+    dtype = "int32"
+    backing_ndarray = tvm.nd.empty(backing_shape, dtype)
+    view = backing_ndarray._create_view(view_shape, dtype, relative_byte_offset=byte_offset)
+    res = vm["main"](view)
     assert res == byte_offset
 
 
@@ -234,16 +213,8 @@ def test_elem_offset(elem_offset, dtype):
     vm = relax.VirtualMachine(built, tvm.cpu())
 
     backing_ndarray = tvm.nd.empty(backing_shape, dtype)
-
-    # Manually overwrite the DLTensor fields to make a view into the
-    # tensor.
-    view = backing_ndarray.handle[0]
-    np_shape = np.array(view_shape, "int64")
-    view.shape = np_shape.ctypes.data_as(ctypes.POINTER(ctypes.c_long))
-    view.byte_offset = byte_offset
-    backing_ndarray.handle[0] = view
-
-    res = vm["main"](backing_ndarray)
+    view = backing_ndarray._create_view(view_shape, dtype, relative_byte_offset=byte_offset)
+    res = vm["main"](view)
 
     assert res == elem_offset
 
diff --git a/tests/python/relax/test_op_misc.py b/tests/python/relax/test_op_misc.py
index b53c10b369..366ea1b688 100644
--- a/tests/python/relax/test_op_misc.py
+++ b/tests/python/relax/test_op_misc.py
@@ -58,7 +58,7 @@ def test_call_tir_with_grad():
     )
     assert v2.attrs.te_grad_name == "identity_k_grad"
     assert isinstance(v2.attrs.te_grad_kwargs, tvm.ir.container.Map)
-    val = v2.attrs.te_grad_kwargs.items()[0]
+    val = list(v2.attrs.te_grad_kwargs.items())[0]
     assert val[0] == "k" and float(val[1]) == 1.0
 
 
diff --git a/tests/python/relax/test_vm_build.py b/tests/python/relax/test_vm_build.py
index 9acd1b8629..2078248880 100644
--- a/tests/python/relax/test_vm_build.py
+++ b/tests/python/relax/test_vm_build.py
@@ -541,7 +541,7 @@ def test_vm_relax_symbolic_shape_tuple(exec_mode):
 
     func = vm["main"]
 
-    assert func(ShapeTuple([2, 3])) == [4, 9]
+    assert func(ShapeTuple([2, 3])) == (4, 9)
 
     with pytest.raises(ValueError):
         func(ShapeTuple([2, 3, 4]))
@@ -595,7 +595,7 @@ def test_vm_relax_multiple_symbolic_prim_value(exec_mode):
 
     func = vm["main"]
 
-    assert func(2, ShapeTuple([4, 12]), 6) == [4, 7]
+    assert func(2, ShapeTuple([4, 12]), 6) == (4, 7)
 
     with pytest.raises(RuntimeError):
         func(2, ShapeTuple([4, 12]), 1)
@@ -873,8 +873,8 @@ def test_vm_to_device(exec_mode):
     res_2 = check_saved_func(vm, "foo2", x_inp)
 
     # check the copied tensor's device
-    assert str(res_1.device) == "cuda(0)"
-    assert str(res_2.device) == "cpu(0)"
+    assert res_1.device == tvm.cuda(0)
+    assert res_2.device == tvm.cpu(0)
 
     tvm.testing.assert_allclose(res_1.numpy(), x_inp.numpy())
     tvm.testing.assert_allclose(res_2.numpy(), x_inp.numpy())
diff --git a/tests/python/relax/test_vm_callback_function.py b/tests/python/relax/test_vm_callback_function.py
index 73336db559..1cee0b57d8 100644
--- a/tests/python/relax/test_vm_callback_function.py
+++ b/tests/python/relax/test_vm_callback_function.py
@@ -111,15 +111,10 @@ def test_catch_exception_with_full_stack_trace(exec_mode, target, dev):
         while stack.tb_next is not None:
             stack = stack.tb_next
         frame = stack.tb_frame
+        assert (
+            frame.f_code.co_filename.find("test_vm_callback_function.py") != -1
+        ), "Inner-most stack frame should be from Python callback"
 
-        assert frame.f_code is custom_callback.__code__, (
-            "Inner-most stack frame should be from Python callback, "
-            "even though that crosses an FFI boundary"
-        )
-        assert frame.f_locals.get("local_var") == 42, (
-            "Python __traceback__ should include local variables, "
-            "even though that crosses an FFI boundary"
-        )
     else:
         raise RuntimeError("Exception thrown in callback was not propagated to calling scope")
 
diff --git a/tests/python/relax/test_vm_codegen_only.py b/tests/python/relax/test_vm_codegen_only.py
index 5ae46b099e..dac0f867ce 100644
--- a/tests/python/relax/test_vm_codegen_only.py
+++ b/tests/python/relax/test_vm_codegen_only.py
@@ -78,7 +78,7 @@ def test_vm_to_device(exec_mode):
     res = check_saved_func(vm, "foo", inp)
     tvm.testing.assert_allclose(res.numpy(), inp.numpy(), rtol=1e-7, atol=1e-7)
     # check the resulting tensor is on cpu:0
-    assert str(res.device) == "cpu(0)"
+    assert res.device == tvm.cpu(0)
     assert res.device.device_type == 1
     assert res.device.device_id == 0
 


(tvm) 05/06: Fix C codegen caller convention

Posted by tq...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch refactor-s3
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit 5438ef692e8b184315ead62e5e6bdf1a1bd8f9ac
Author: tqchen <ti...@gmail.com>
AuthorDate: Mon May 5 09:24:27 2025 -0400

    Fix C codegen caller convention
---
 src/target/source/codegen_c_host.cc | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/target/source/codegen_c_host.cc b/src/target/source/codegen_c_host.cc
index 815904c946..ef86f09ca2 100644
--- a/src/target/source/codegen_c_host.cc
+++ b/src/target/source/codegen_c_host.cc
@@ -242,7 +242,11 @@ void CodeGenCHost::PrintCallPacked(const CallNode* op) {
   std::string result = name_supply_->FreshName("result");
   this->stream << "TVMFFIAny " << result << ";\n";
   this->PrintIndent();
-
+  // must make sure type_index is set to none
+  this->stream << result << ".type_index = kTVMFFINone;\n";
+  this->PrintIndent();
+  this->stream << result << ".v_int64 = 0;\n";
+  this->PrintIndent();
   if (op->op.same_as(builtin::tvm_call_packed_lowered())) {
     this->stream << "if (TVMFFIFunctionCall(" << packed_func_name << ", ";
   } else {