From 7aecb0990f40d48b8f55648d214b0cace2635b1d Mon Sep 17 00:00:00 2001 From: Zihao Ye Date: Fri, 13 Nov 2026 18:03:41 -0800 Subject: [PATCH] [PERF] Avoid backtraces for type-key and dtype probes --- include/tvm/ffi/c_api.h | 2 +- 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 ++++++++++ 7 files changed, 133 insertions(+), 25 deletions(-) diff --git a/include/tvm/ffi/c_api.h b/include/tvm/ffi/c_api.h index 0deb498ac..4d8a2f72a 100644 --- a/include/tvm/ffi/c_api.h +++ b/include/tvm/ffi/c_api.h @@ -70,7 +70,7 @@ /*! \brief TVM FFI minor version. */ #define TVM_FFI_VERSION_MINOR 1 /*! \brief TVM FFI patch version. */ -#define TVM_FFI_VERSION_PATCH 16 +#define TVM_FFI_VERSION_PATCH 17 // NOLINTEND(modernize-macro-to-enum) #ifdef __cplusplus diff --git a/python/tvm_ffi/_dtype.py b/python/tvm_ffi/_dtype.py index e75afaa83..c972a152c 100644 --- a/python/tvm_ffi/_dtype.py +++ b/python/tvm_ffi/_dtype.py @@ -23,7 +23,7 @@ from enum import IntEnum from typing import Any, ClassVar -from . import core +from . import _ffi_api, core class DataTypeCode(IntEnum): @@ -114,6 +114,23 @@ def __new__(cls, content: Any) -> dtype: 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 5f56e8c0f..0ec632a15 100644 --- a/python/tvm_ffi/_ffi_api.py +++ b/python/tvm_ffi/_ffi_api.py @@ -25,7 +25,7 @@ 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 @@ def ArrayGetItem(_0: Sequence[Any], _1: int, /) -> Any: ... 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 @@ def _RegisterFFIInit(_0: int, /) -> None: ... "ArraySize", "Bytes", "ContainerFindFirstNonCPUDevice", + "DataTypeTryParse", "DeepCopy", "Dict", "DictClear", diff --git a/python/tvm_ffi/cython/object.pxi b/python/tvm_ffi/cython/object.pxi index 3219158ff..ccdfb0533 100644 --- a/python/tvm_ffi/cython/object.pxi +++ b/python/tvm_ffi/cython/object.pxi @@ -417,12 +417,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 b86d561a3..5fda92595 100644 --- a/src/ffi/dtype.cc +++ b/src/ffi/dtype.cc @@ -17,6 +17,8 @@ * under the License. */ #include +#include +#include #include #include @@ -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(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(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(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(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 { + 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 163f9be52..0c2c27402 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 + + +@pytest.mark.parametrize( + "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, + ) + + +@pytest.mark.parametrize( + "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 23df2d5c2..3d0e199e7 100644 --- a/tests/python/test_object.py +++ b/tests/python/test_object.py @@ -17,6 +17,7 @@ from __future__ import annotations import sys +import time from collections.abc import Sequence from typing import Any @@ -483,3 +484,19 @@ def test_issubclass_sibling_containers(self) -> None: 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