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

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


The following commit(s) were added to refs/heads/main by this push:
     new e1ba35fe [PERF] Avoid backtraces for type-key and dtype probes (#975)
e1ba35fe is described below

commit e1ba35fe0c6bcb58548484890f709553d1a3b652
Author: Yaxing Cai <[email protected]>
AuthorDate: Sun Nov 15 22:47:21 2026 +0800

    [PERF] Avoid backtraces for type-key and dtype probes (#975)
    
    Type-key and dtype probes can capture expensive error backtraces for
    expected misses.
    
    Query type keys without raising, and add `tvm_ffi.dtype.try_parse(text)
    -> dtype | None`. It returns the parsed dtype directly on success and
    None for an invalid dtype string, avoiding a second parse after a
    successful probe. The registered `ffi.DataTypeTryParse` function returns
    `Optional<DLDataType>`; there is no boolean can-parse API.
    
    Dtype parsing reuses ErrorBuilder through a file-local
    TVM_FFI_THROW_NO_BACKTRACE macro. Invalid dtype construction still
    raises ValueError with the existing message, without capturing a parser
    backtrace. Other errors propagate. No new C API or Cython wrapper is
    required.
    
    Validation: editable C++/Cython build succeeded; dtype and object tests:
    161 passed, 1 skipped. Type checking, Ruff, C++ formatting, and
    whitespace checks passed.
    
    Co-authored-by: Zihao Ye <[email protected]>
---
 python/tvm_ffi/_dtype.py         | 19 +++++++++++++-
 python/tvm_ffi/_ffi_api.py       |  4 ++-
 python/tvm_ffi/cython/object.pxi | 12 +++++----
 src/ffi/dtype.cc                 | 57 ++++++++++++++++++++++++++++------------
 tests/python/test_dtype.py       | 47 +++++++++++++++++++++++++++++++++
 tests/python/test_object.py      | 17 ++++++++++++
 6 files changed, 132 insertions(+), 24 deletions(-)

diff --git a/python/tvm_ffi/_dtype.py b/python/tvm_ffi/_dtype.py
index e75afaa8..c972a152 100644
--- a/python/tvm_ffi/_dtype.py
+++ b/python/tvm_ffi/_dtype.py
@@ -23,7 +23,7 @@ import builtins
 from enum import IntEnum
 from typing import Any, ClassVar
 
-from . import core
+from . import _ffi_api, core
 
 
 class DataTypeCode(IntEnum):
@@ -114,6 +114,23 @@ class dtype(str):
         val._tvm_ffi_dtype = core.DataType(content)
         return val
 
+    @staticmethod
+    def try_parse(text: str) -> dtype | None:
+        """Parse a dtype string, returning None if it is not a valid dtype.
+
+        Expected parse failures do not capture a backtrace. Other errors,
+        such as an unregistered custom type, propagate to the caller.
+
+        Examples
+        --------
+        >>> tvm_ffi.dtype.try_parse("float32")
+        'float32'
+        >>> tvm_ffi.dtype.try_parse("not_a_dtype") is None
+        True
+
+        """
+        return _ffi_api.DataTypeTryParse(text)
+
     @staticmethod
     def from_dlpack_data_type(dltype_data_type: tuple[int, int, int]) -> dtype:
         """Create a dtype from a DLPack data type tuple.
diff --git a/python/tvm_ffi/_ffi_api.py b/python/tvm_ffi/_ffi_api.py
index 5f56e8c0..0ec632a1 100644
--- a/python/tvm_ffi/_ffi_api.py
+++ b/python/tvm_ffi/_ffi_api.py
@@ -25,7 +25,7 @@ from typing import TYPE_CHECKING
 if TYPE_CHECKING:
     from collections.abc import Mapping, MutableMapping, MutableSequence, 
Sequence
     from ctypes import c_void_p
-    from tvm_ffi import Device, Module, Object, StructuralKey as 
_StructuralKey, StructuralMutator as _StructuralMutator, StructuralVisitor as 
_StructuralVisitor, VisitInterrupt as _VisitInterrupt
+    from tvm_ffi import dtype, Device, Module, Object, StructuralKey as 
_StructuralKey, StructuralMutator as _StructuralMutator, StructuralVisitor as 
_StructuralVisitor, VisitInterrupt as _VisitInterrupt
     from tvm_ffi.access_path import AccessPath
     from typing import Any, Callable
 # isort: on
@@ -42,6 +42,7 @@ if TYPE_CHECKING:
     def ArraySize(_0: Sequence[Any], /) -> int: ...
     def Bytes(_0: bytes, /) -> bytes: ...
     def ContainerFindFirstNonCPUDevice(_0: Any, /) -> Device: ...
+    def DataTypeTryParse(_0: str, /) -> dtype | None: ...
     def DeepCopy(_0: Any, /) -> Any: ...
     def Dict(*args: Any) -> Any: ...
     def DictClear(_0: MutableMapping[Any, Any], /) -> None: ...
@@ -144,6 +145,7 @@ __all__ = [
     "ArraySize",
     "Bytes",
     "ContainerFindFirstNonCPUDevice",
+    "DataTypeTryParse",
     "DeepCopy",
     "Dict",
     "DictClear",
diff --git a/python/tvm_ffi/cython/object.pxi b/python/tvm_ffi/cython/object.pxi
index 6ea548d1..5846ea5d 100644
--- a/python/tvm_ffi/cython/object.pxi
+++ b/python/tvm_ffi/cython/object.pxi
@@ -454,12 +454,14 @@ class PyNativeObject:
 
 
 def _object_type_key_to_index(str type_key):
-    """get the type index of object class"""
+    """get the type index of object class, or None when no type of that key is 
registered"""
     cdef int32_t tidx
-    type_key_arg = ByteArrayArg(c_str(type_key))
-    if TVMFFITypeKeyToIndex(type_key_arg.cptr(), &tidx) == 0:
-        return tidx
-    return None
+    cdef ByteArrayArg type_key_arg = ByteArrayArg(c_str(type_key))
+    # Query only (parent_type_index=-2): a miss returns -2. 
TVMFFITypeKeyToIndex raises on a
+    # miss instead, and the error captures a backtrace: in the first error of 
a process,
+    # libbacktrace reads the symbols of every loaded library, most of a second 
with torch loaded.
+    tidx = TVMFFITypeGetOrAllocIndex(type_key_arg.cptr(), -1, 0, 0, 0, -2)
+    return None if tidx == -2 else tidx
 
 
 cdef inline str _type_index_to_key(int32_t tindex):
diff --git a/src/ffi/dtype.cc b/src/ffi/dtype.cc
index b86d561a..5fda9259 100644
--- a/src/ffi/dtype.cc
+++ b/src/ffi/dtype.cc
@@ -17,6 +17,8 @@
  * under the License.
  */
 #include <tvm/ffi/dtype.h>
+#include <tvm/ffi/optional.h>
+#include <tvm/ffi/reflection/registry.h>
 #include <tvm/ffi/string.h>
 
 #include <string>
@@ -25,6 +27,10 @@
 namespace tvm {
 namespace ffi {
 namespace detail {
+// Keep dtype parse failures cheap for callers probing candidate strings.
+#define TVM_FFI_THROW_NO_BACKTRACE(ErrorKind) \
+  ::tvm::ffi::detail::ErrorBuilder(#ErrorKind, std::string(), false).stream()
+
 /*!
  * \brief Get the custom type name for a given type code.
  */
@@ -46,7 +52,8 @@ inline int ParseCustomDataTypeCode(const std::string_view& 
str, const char** sca
   *scan = str.data() + 6;
   TVM_FFI_ICHECK(str.data() == tmp);
   if (**scan != '[') {
-    TVM_FFI_THROW(ValueError) << "expected opening brace after 'custom' type 
in" << str;
+    TVM_FFI_THROW_NO_BACKTRACE(ValueError)
+        << "expected opening brace after 'custom' type in" << str;
   }
   TVM_FFI_ICHECK(str.data() == tmp);
   *scan += 1;
@@ -59,7 +66,8 @@ inline int ParseCustomDataTypeCode(const std::string_view& 
str, const char** sca
   }
   TVM_FFI_ICHECK(str.data() == tmp);
   if (*(*scan + custom_name_len) != ']') {
-    TVM_FFI_THROW(ValueError) << "expected closing brace after 'custom' type 
in" << str;
+    TVM_FFI_THROW_NO_BACKTRACE(ValueError)
+        << "expected closing brace after 'custom' type in" << str;
   }
   TVM_FFI_ICHECK(str.data() == tmp);
   *scan += custom_name_len + 1;
@@ -250,9 +258,9 @@ inline DLDataType StringViewToDLDataType_(std::string_view 
str) {
       (*ptr)++;
     }
     if (value > UINT32_MAX) {
-      TVM_FFI_THROW(ValueError) << "Integer value in dtype string '"
-                                << std::string_view(start_ptr, *ptr - 
start_ptr)
-                                << "' is out of range for uint32_t";
+      TVM_FFI_THROW_NO_BACKTRACE(ValueError)
+          << "Integer value in dtype string '" << std::string_view(start_ptr, 
*ptr - start_ptr)
+          << "' is out of range for uint32_t";
     }
     return static_cast<uint32_t>(value);
   };
@@ -275,12 +283,13 @@ inline DLDataType 
StringViewToDLDataType_(std::string_view str) {
     const char* digits_start = *ptr;
     uint32_t lanes_val = parse_digits(ptr, end);
     if (*ptr == digits_start || lanes_val == 0) {
-      TVM_FFI_THROW(ValueError) << "Invalid lanes specification in dtype '" << 
dtype_str
-                                << "'. Lanes must be a positive integer.";
+      TVM_FFI_THROW_NO_BACKTRACE(ValueError) << "Invalid lanes specification 
in dtype '"
+                                             << dtype_str << "'. Lanes must be 
a positive integer.";
     }
     if (lanes_val > UINT16_MAX) {
-      TVM_FFI_THROW(ValueError) << "Lanes value " << lanes_val
-                                << " is out of range for uint16_t in dtype '" 
<< dtype_str << "'";
+      TVM_FFI_THROW_NO_BACKTRACE(ValueError)
+          << "Lanes value " << lanes_val << " is out of range for uint16_t in 
dtype '" << dtype_str
+          << "'";
     }
     return static_cast<uint16_t>(multiplier * lanes_val);
   };
@@ -297,7 +306,7 @@ inline DLDataType StringViewToDLDataType_(std::string_view 
str) {
     dtype.lanes = parse_lanes(&endpt, str_end, str);
     scan = endpt;
     if (scan != str_end) {
-      TVM_FFI_THROW(ValueError) << "unknown dtype `" << str << '`';
+      TVM_FFI_THROW_NO_BACKTRACE(ValueError) << "unknown dtype `" << str << 
'`';
     }
     return dtype;
   };
@@ -332,7 +341,7 @@ inline DLDataType StringViewToDLDataType_(std::string_view 
str) {
     } else if (int end = match_float_variant(offset, "e5m2"); end >= 0) {
       return parse_float(str, end, kDLFloat8_e5m2, 8);
     } else {
-      TVM_FFI_THROW(ValueError) << "unknown float8 type `" << str << '`';
+      TVM_FFI_THROW_NO_BACKTRACE(ValueError) << "unknown float8 type `" << str 
<< '`';
       TVM_FFI_UNREACHABLE();
     }
   };
@@ -343,7 +352,7 @@ inline DLDataType StringViewToDLDataType_(std::string_view 
str) {
     } else if (int end = match_float_variant(offset, "e3m2fn"); end >= 0) {
       return parse_float(str, end, kDLFloat6_e3m2fn, 6);
     } else {
-      TVM_FFI_THROW(ValueError) << "unknown float6 type `" << str << '`';
+      TVM_FFI_THROW_NO_BACKTRACE(ValueError) << "unknown float6 type `" << str 
<< '`';
       TVM_FFI_UNREACHABLE();
     }
   };
@@ -353,7 +362,7 @@ inline DLDataType StringViewToDLDataType_(std::string_view 
str) {
         end >= 0) {
       return parse_float(str, end, kDLFloat4_e2m1fn, 4, 
/*allow_underscore_lanes=*/true);
     } else {
-      TVM_FFI_THROW(ValueError) << "unknown float4 type `" << str << '`';
+      TVM_FFI_THROW_NO_BACKTRACE(ValueError) << "unknown float4 type `" << str 
<< '`';
       TVM_FFI_UNREACHABLE();
     }
   };
@@ -419,25 +428,39 @@ inline DLDataType 
StringViewToDLDataType_(std::string_view str) {
     dtype.code = static_cast<uint8_t>(detail::ParseCustomDataTypeCode(str, 
&scan));
   } else {
     scan = str.data();
-    TVM_FFI_THROW(ValueError) << "unknown dtype `" << str << '`';
+    TVM_FFI_THROW_NO_BACKTRACE(ValueError) << "unknown dtype `" << str << '`';
   }
   // Parse bits manually to handle non-null-terminated string_view
   const char* xdelim = scan;
   uint32_t bits_val = parse_digits(&xdelim, str_end);
   if (bits_val > UINT8_MAX) {
-    TVM_FFI_THROW(ValueError) << "Bits value " << bits_val
-                              << " is out of range for uint8_t in dtype '" << 
str << "'";
+    TVM_FFI_THROW_NO_BACKTRACE(ValueError)
+        << "Bits value " << bits_val << " is out of range for uint8_t in dtype 
'" << str << "'";
   }
   uint8_t bits = static_cast<uint8_t>(bits_val);
   if (bits != 0) dtype.bits = bits;
   const char* endpt = xdelim;
   dtype.lanes = parse_lanes(&endpt, str_end, str, /*allow_scalable=*/true);
   if (endpt != str_end) {
-    TVM_FFI_THROW(ValueError) << "unknown dtype `" << str << '`';
+    TVM_FFI_THROW_NO_BACKTRACE(ValueError) << "unknown dtype `" << str << '`';
   }
   return dtype;
 }
 
+TVM_FFI_STATIC_INIT_BLOCK() {
+  reflection::GlobalDef().def(
+      "ffi.DataTypeTryParse", [](const String& str) -> Optional<DLDataType> {
+        try {
+          return StringViewToDLDataType_(std::string_view(str.data(), 
str.size()));
+        } catch (const Error& error) {
+          if (error.kind() == "ValueError") return std::nullopt;
+          throw;
+        }
+      });
+}
+
+#undef TVM_FFI_THROW_NO_BACKTRACE
+
 }  // namespace ffi
 }  // namespace tvm
 
diff --git a/tests/python/test_dtype.py b/tests/python/test_dtype.py
index 163f9be5..0c2c2740 100644
--- a/tests/python/test_dtype.py
+++ b/tests/python/test_dtype.py
@@ -16,6 +16,7 @@
 # under the License.
 
 import pickle
+import time
 from typing import Any
 
 import numpy as np
@@ -318,3 +319,49 @@ def test_dtype_kind_properties(
     assert dtype.is_integer is is_integer
     assert dtype.is_float is is_float
     assert dtype.is_handle is is_handle
+
+
[email protected](
+    "dtype_str", ["int32", "f32x2", "bf16", "float8_e4m3fn", "fp4_e2m1fn", 
"bool", "void"]
+)
+def test_data_type_try_parse_a_dtype(dtype_str: str) -> None:
+    result = tvm_ffi.dtype.try_parse(dtype_str)
+    assert isinstance(result, tvm_ffi.dtype)
+    expected = tvm_ffi.dtype(dtype_str)
+    assert (result.type_code, result.bits, result.lanes) == (
+        expected.type_code,
+        expected.bits,
+        expected.lanes,
+    )
+
+
[email protected](
+    "dtype_str", ["mbarrier", "fill_from", "int32x", "float8_e9m9", "custom[", 
"i32x0"]
+)
+def test_data_type_try_parse_no_dtype_without_an_error(dtype_str: str) -> None:
+    assert tvm_ffi.dtype.try_parse(dtype_str) is None
+    # Constructing the dtype still raises the error the parse reports.
+    with pytest.raises(ValueError):
+        tvm_ffi.dtype(dtype_str)
+
+
+def test_data_type_try_parse_a_miss_cheaply() -> None:
+    # Expected misses should not capture a backtrace.
+    try_parse = tvm_ffi.dtype.try_parse
+    start = time.perf_counter()
+    for _ in range(1000):
+        assert try_parse("mbarrier") is None
+    assert time.perf_counter() - start < 1.0
+
+
+def test_unknown_dtype_message() -> None:
+    with pytest.raises(ValueError, match="unknown dtype `mbarrier`"):
+        tvm_ffi.dtype("mbarrier")
+
+
+def test_data_type_try_parse_ffi() -> None:
+    try_parse = tvm_ffi.get_global_func("ffi.DataTypeTryParse")
+    result = try_parse("float32")
+    assert isinstance(result, tvm_ffi.dtype)
+    assert result == "float32"
+    assert try_parse("not_a_dtype") is None
diff --git a/tests/python/test_object.py b/tests/python/test_object.py
index d38a71c5..9e08f774 100644
--- a/tests/python/test_object.py
+++ b/tests/python/test_object.py
@@ -18,6 +18,7 @@ from __future__ import annotations
 
 import subprocess
 import sys
+import time
 from collections.abc import Sequence
 from typing import Any
 
@@ -524,3 +525,19 @@ class TestIsinstanceIssubclass:
         assert not issubclass(tvm_ffi.Dict, tvm_ffi.Map)
         assert not issubclass(tvm_ffi.Array, tvm_ffi.List)
         assert not issubclass(tvm_ffi.Map, tvm_ffi.Dict)
+
+
+def test_object_type_key_to_index_registered_and_missing() -> None:
+    tindex = tvm_ffi.core._object_type_key_to_index("testing.TestObjectBase")
+    type_info = 
tvm_ffi.core._type_cls_to_type_info(tvm_ffi.testing.TestObjectBase)
+    assert type_info is not None
+    assert tindex == type_info.type_index
+    assert tvm_ffi.core._object_type_key_to_index("testing.NoSuchTypeKey") is 
None
+
+
+def test_object_type_key_to_index_miss_is_cheap() -> None:
+    # A miss raises no error, whose backtrace would cost milliseconds per call.
+    start = time.perf_counter()
+    for _ in range(1000):
+        assert tvm_ffi.core._object_type_key_to_index("testing.NoSuchTypeKey") 
is None
+    assert time.perf_counter() - start < 1.0

Reply via email to