Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/tvm/ffi/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion python/tvm_ffi/_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from enum import IntEnum
from typing import Any, ClassVar

from . import core
from . import _ffi_api, core


class DataTypeCode(IntEnum):
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion python/tvm_ffi/_ffi_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: ...
Expand Down Expand Up @@ -144,6 +145,7 @@ def _RegisterFFIInit(_0: int, /) -> None: ...
"ArraySize",
"Bytes",
"ContainerFindFirstNonCPUDevice",
"DataTypeTryParse",
"DeepCopy",
"Dict",
"DictClear",
Expand Down
12 changes: 7 additions & 5 deletions python/tvm_ffi/cython/object.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
57 changes: 40 additions & 17 deletions src/ffi/dtype.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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.
*/
Expand All @@ -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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add TVM_FFI_THROW_NO_BACKTRACE, keep private to file for now

TVM_FFI_THROW_NO_BACKTRACE(ValueError)
<< "expected opening brace after 'custom' type in" << str;
}
TVM_FFI_ICHECK(str.data() == tmp);
*scan += 1;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
};
Expand All @@ -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);
};
Expand All @@ -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;
};
Expand Down Expand Up @@ -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();
}
};
Expand All @@ -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();
}
};
Expand All @@ -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();
}
};
Expand Down Expand Up @@ -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

Expand Down
47 changes: 47 additions & 0 deletions tests/python/test_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.

import pickle
import time
from typing import Any

import numpy as np
Expand Down Expand Up @@ -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
17 changes: 17 additions & 0 deletions tests/python/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from __future__ import annotations

import sys
import time
from collections.abc import Sequence
from typing import Any

Expand Down Expand Up @@ -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
Loading