24 #ifndef TVM_RUNTIME_PACKED_FUNC_H_
25 #define TVM_RUNTIME_PACKED_FUNC_H_
33 #include <tvm/runtime/logging.h>
44 #include <type_traits>
49 #ifndef TVM_RUNTIME_HEADER_ONLY
50 #define TVM_RUNTIME_HEADER_ONLY 0
59 class TVMMovableArgValueWithContext_;
62 template <
typename FType>
64 template <
typename TSignature>
81 static constexpr
const char*
_type_key =
"runtime.PackedFunc";
88 template <
class TPackedFuncSubObj>
116 template <
class TCallable>
118 using TStorage =
typename std::remove_cv<typename std::remove_reference<TCallable>::type>::type;
150 template <
typename TCallable,
151 typename = std::enable_if_t<
153 !std::is_base_of<TCallable, PackedFunc>::value>>
156 data_ = make_object<ObjType>(std::forward<TCallable>(data));
172 template <
typename... Args>
194 template <
typename FType>
229 template <
typename R,
typename... Args>
287 template <
typename FLambda,
typename =
typename std::enable_if<std::is_convertible<
288 FLambda, std::function<R(Args...)>>::value>::type>
290 this->AssignTypedLambda(typed_lambda, name);
310 template <
typename FLambda,
typename =
typename std::enable_if<std::is_convertible<
311 FLambda, std::function<R(Args...)>>::value>::type>
313 this->AssignTypedLambda(typed_lambda);
331 template <
typename FLambda,
typename =
typename std::enable_if<
332 std::is_convertible<FLambda,
333 std::function<R(Args...)>>::value>::type>
335 this->AssignTypedLambda(typed_lambda);
352 TVM_ALWAYS_INLINE R operator()(Args... args)
const;
363 bool operator==(std::nullptr_t
null)
const {
return packed_ ==
nullptr; }
365 bool operator!=(std::nullptr_t
null)
const {
return packed_ !=
nullptr; }
379 template <
typename FLambda>
380 inline void AssignTypedLambda(FLambda flambda, std::string name);
389 template <
typename FLambda>
390 inline void AssignTypedLambda(FLambda flambda);
408 inline int size()
const;
421 template <
typename T>
422 inline T
At(
int i)
const;
432 inline std::ostream&
operator<<(std::ostream& os, DLDevice dev);
434 #define TVM_LOG_INCORRECT_TYPE_CODE(CODE, T) \
435 "expected " << ArgTypeCode2Str(T) << " but got " << ArgTypeCode2Str(CODE)
438 #define TVM_CHECK_TYPE_CODE(CODE, T) ICHECK_EQ(CODE, T) << TVM_LOG_INCORRECT_TYPE_CODE(CODE, T)
444 template <
typename T>
454 using ContainerType =
typename T::ContainerType;
455 if (ptr ==
nullptr) {
456 if (T::_type_is_nullable) {
474 using ContainerType =
typename T::ContainerType;
475 if (ptr ==
nullptr)
return T::_type_is_nullable;
479 using ContainerType =
typename T::ContainerType;
480 return ContainerType::_type_key;
485 template <
typename T>
488 if (ptr ==
nullptr) {
495 if constexpr (std::is_same_v<T, ObjectRef>) {
500 for (
size_t i = 0; i < n->
size(); i++) {
504 return String(
"Array[index " + std::to_string(i) +
": " + check_subtype.
value() +
"]");
510 if (ptr ==
nullptr)
return true;
512 if constexpr (std::is_same_v<T, ObjectRef>)
return true;
525 template <
typename K,
typename V>
528 if (ptr ==
nullptr)
return NullOpt;
531 if constexpr (std::is_same_v<K, ObjectRef> && std::is_same_v<V, ObjectRef>) {
536 for (
const auto& kv : *n) {
538 if constexpr (!std::is_same_v<K, ObjectRef>) {
542 if constexpr (!std::is_same_v<V, ObjectRef>) {
546 std::string key_name =
548 std::string value_name = value_type.
defined() ? std::string(value_type.
value())
550 return String(
"Map[" + key_name +
", " + value_name +
"]");
556 if (ptr ==
nullptr)
return true;
559 if constexpr (std::is_same_v<K, ObjectRef> && std::is_same_v<V, ObjectRef>) {
564 for (
const auto& kv : *n) {
565 if constexpr (!std::is_same_v<K, ObjectRef>) {
568 if constexpr (!std::is_same_v<V, ObjectRef>) {
580 template <
typename OnlyVariant>
586 static std::string
TypeName() {
return "Variant[" + VariantNames() +
"]"; }
590 template <
typename FirstVariant,
typename... RemainingVariants>
594 if (!try_first.defined()) {
604 static std::string
TypeName() {
return "Variant[" + VariantNames() +
"]"; }
617 operator void*()
const {
623 operator DLTensor*()
const {
628 LOG(FATAL) <<
"Expected "
662 template <
typename T>
737 template <
typename Derived>
743 template <
typename TObjectRef,
744 typename =
typename std::enable_if<std::is_base_of<ObjectRef, TObjectRef>::value>::type>
746 template <
typename TObjectRef>
749 operator double()
const {
763 operator int64_t()
const {
772 operator uint64_t()
const {
return operator int64_t(); }
773 operator int()
const {
774 int64_t value =
operator int64_t();
779 operator bool()
const {
807 using TVMPODValue_CRTP_::operator double;
808 using TVMPODValue_CRTP_::operator int64_t;
809 using TVMPODValue_CRTP_::operator uint64_t;
810 using TVMPODValue_CRTP_::operator int;
811 using TVMPODValue_CRTP_::operator bool;
812 using TVMPODValue_::operator
void*;
813 using TVMPODValue_::operator DLTensor*;
814 using TVMPODValue_::operator
NDArray;
815 using TVMPODValue_::operator
Device;
816 using TVMPODValue_::operator
Module;
822 operator std::string()
const {
827 return std::string(arr->
data, arr->
size);
831 return AsObjectRef<tvm::runtime::String>().operator std::string();
834 template <
typename FType>
840 template <typename T, typename = typename std::enable_if<std::is_class<T>::value>::type>
841 inline operator T()
const;
842 inline operator DLDataType()
const;
860 using TVMPODValue_CRTP_::operator double;
861 using TVMPODValue_CRTP_::operator int64_t;
862 using TVMPODValue_CRTP_::operator uint64_t;
863 using TVMPODValue_CRTP_::operator int;
864 using TVMPODValue_CRTP_::operator bool;
865 using TVMPODValue_::operator
void*;
866 using TVMPODValue_::operator DLTensor*;
867 using TVMPODValue_::operator
NDArray;
868 using TVMPODValue_::operator
Device;
869 using TVMPODValue_::operator
Module;
872 operator std::string()
const {
return AsArgValue().operator std::string(); }
873 template <
typename FType>
877 operator DLDataType()
const {
return AsArgValue().operator DLDataType(); }
885 template <
typename T,
886 typename =
typename std::enable_if<std::is_base_of<ObjectRef, T>::value>::type>
887 inline operator T()
const;
913 const std::string* optional_name,
FSig* f_sig)
914 : value_(value, type_code),
915 arg_index_(arg_index),
916 optional_name_(optional_name),
919 template <
typename T>
923 }
catch (dmlc::Error& e) {
924 LOG(FATAL) <<
"In function " << (optional_name_ ==
nullptr ?
"<anonymous>" : *optional_name_)
925 << (f_sig_ ==
nullptr ?
"" : (*f_sig_)()) <<
": error while converting argument "
926 << arg_index_ <<
": " << e.what();
934 const std::string* optional_name_;
961 using TVMPODValue_CRTP_::operator double;
962 using TVMPODValue_CRTP_::operator int64_t;
963 using TVMPODValue_CRTP_::operator uint64_t;
964 using TVMPODValue_CRTP_::operator int;
965 using TVMPODValue_CRTP_::operator bool;
966 using TVMPODValue_::operator
void*;
967 using TVMPODValue_::operator DLTensor*;
968 using TVMPODValue_::operator
Device;
969 using TVMPODValue_::operator
NDArray;
970 using TVMPODValue_::operator
Module;
977 operator std::string()
const {
981 return *ptr<std::string>();
984 return *ptr<std::string>();
986 operator DLDataType()
const {
994 template <
typename FType>
1007 this->SwitchToPOD(kDLFloat);
1022 this->SwitchToPOD(kDLInt);
1027 this->SwitchToPOD(kDLInt);
1056 if (other.
data_ !=
nullptr) {
1075 template <
typename FType>
1080 this->Assign(other);
1084 this->Assign(other);
1088 this->Assign(other);
1127 <<
"TVMRetValue.value can only be used for POD data";
1131 template <
typename TObjectRef,
1132 typename =
typename std::enable_if_t<std::is_base_of_v<ObjectRef, TObjectRef>>>
1134 template <
typename T,
typename =
typename std::enable_if_t<std::is_
class_v<T>>>
1135 inline operator T()
const;
1138 template <
typename T>
1139 void Assign(
const T& other) {
1140 switch (other.type_code()) {
1142 SwitchToClass<std::string>(
kTVMStr, other);
1146 SwitchToClass<std::string>(
kTVMBytes, other);
1150 *
this = other.operator PackedFunc();
1154 *
this = other.operator Module();
1158 *
this = other.operator NDArray();
1174 SwitchToPOD(other.type_code());
1187 template <
typename T>
1188 void SwitchToClass(
int type_code, T v) {
1197 void SwitchToObject(
int type_code, ObjectPtr<Object> other) {
1198 if (other.data_ !=
nullptr) {
1203 other.data_ =
nullptr;
1214 delete ptr<std::string>();
1245 template <
typename TObjectRef>
1280 #define TVM_DLL_EXPORT_PACKED_FUNC(ExportName, Function) \
1282 TVM_DLL int ExportName(TVMValue* args, int* type_code, int num_args, TVMValue* out_value, \
1283 int* out_type_code, void* resource_handle); \
1284 int ExportName(TVMValue* args, int* type_code, int num_args, TVMValue* out_value, \
1285 int* out_type_code, void* resource_handle) { \
1287 ::tvm::runtime::TVMRetValue rv; \
1288 Function(::tvm::runtime::TVMArgs(args, type_code, num_args), &rv); \
1289 rv.MoveToCHost(out_value, out_type_code); \
1291 } catch (const ::std::exception& _except_) { \
1292 TVMAPISetLastError(_except_.what()); \
1298 #define TVM_MODULE_VTABLE_BEGIN(TypeKey) \
1299 const char* type_key() const final { return TypeKey; } \
1300 PackedFunc GetFunction(const String& _name, const ObjectPtr<Object>& _self) override { \
1301 using SelfPtr = std::remove_cv_t<decltype(this)>;
1302 #define TVM_MODULE_VTABLE_END() \
1303 return PackedFunc(nullptr); \
1305 #define TVM_MODULE_VTABLE_END_WITH_DEFAULT(MemFunc) \
1307 auto f = (MemFunc); \
1308 return (this->*f)(_name); \
1311 #define TVM_MODULE_VTABLE_ENTRY(Name, MemFunc) \
1312 if (_name == Name) { \
1313 return PackedFunc([_self](TVMArgs args, TVMRetValue* rv) -> void { \
1314 using Helper = ::tvm::runtime::detail::ModuleVTableEntryHelper<decltype(MemFunc)>; \
1315 SelfPtr self = static_cast<SelfPtr>(_self.get()); \
1316 CHECK_EQ(args.size(), Helper::LenArgs) \
1317 << "Function `" << self->type_key() << "::" << Name << "` requires " << Helper::LenArgs \
1318 << " arguments, but got " << args.size(); \
1319 Helper::Call(rv, self, MemFunc, args, Helper::IndexSeq{}); \
1322 #define TVM_MODULE_VTABLE_ENTRY_PACKED(Name, MemFunc) \
1323 if (_name == Name) { \
1324 return PackedFunc([_self](TVMArgs args, TVMRetValue* rv) -> void { \
1325 (static_cast<SelfPtr>(_self.get())->*(MemFunc))(args, rv); \
1364 #define TVM_DLL_EXPORT_TYPED_FUNC(ExportName, Function) \
1366 TVM_DLL int ExportName(TVMValue* args, int* type_code, int num_args, TVMValue* out_value, \
1367 int* out_type_code, void* resource_handle) { \
1369 auto f = Function; \
1370 using FType = ::tvm::runtime::detail::function_signature<decltype(f)>::FType; \
1371 ::tvm::runtime::TVMRetValue rv; \
1372 ::tvm::runtime::detail::unpack_call_by_signature<FType>::run( \
1373 f, ::tvm::runtime::TVMArgs(args, type_code, num_args), &rv); \
1374 rv.MoveToCHost(out_value, out_type_code); \
1376 } catch (const ::std::exception& _except_) { \
1377 TVMAPISetLastError(_except_.what()); \
1384 ICHECK_LT(i,
num_args) <<
"not enough argument passed, " <<
num_args <<
" passed"
1385 <<
" but request arg[" << i <<
"].";
1391 template <
class TPackedFuncSubObj>
1394 (
static_cast<const TPackedFuncSubObj*
>(obj))->callable_(args, rv);
1398 (*f_call_packed_)(
this, args, rv);
1407 switch (type_code) {
1425 return "ArrayHandle";
1427 return "DLDataType";
1431 return "FunctionHandle";
1433 return "ModuleHandle";
1435 return "NDArrayContainer";
1439 return "ObjectRValueRefArg";
1441 LOG(FATAL) <<
"unknown type_code=" <<
static_cast<int>(type_code);
1459 case kDLCUDAManaged:
1460 return "cuda_managed";
1490 LOG(FATAL) <<
"unknown type = " << type;
1497 template <
bool stop, std::
size_t I,
typename F>
1498 struct for_each_dispatcher {
1499 template <
typename T,
typename... Args>
1500 static void run(
const F& f, T&& value, Args&&... args) {
1501 f(I, std::forward<T>(value));
1502 for_each_dispatcher<
sizeof...(Args) == 0, (I + 1), F>::run(f, std::forward<Args>(args)...);
1506 template <std::
size_t I,
typename F>
1507 struct for_each_dispatcher<true, I, F> {
1508 static void run(
const F& f) {}
1511 template <
typename F,
typename... Args>
1512 inline void for_each(
const F& f, Args&&... args) {
1513 for_each_dispatcher<
sizeof...(Args) == 0, 0, F>::run(f, std::forward<Args>(args)...);
1516 template <
typename T>
1517 struct ModuleVTableEntryHelper {};
1519 template <
typename T,
typename R,
typename... Args>
1520 struct ModuleVTableEntryHelper<R (T::*)(Args...) const> {
1521 using MemFnType = R (T::*)(Args...)
const;
1522 using IndexSeq = std::index_sequence_for<Args...>;
1523 static constexpr
const std::size_t LenArgs =
sizeof...(Args);
1525 template <std::size_t... Is>
1526 static TVM_ALWAYS_INLINE
void Call(TVMRetValue* rv, T*
self, MemFnType f, TVMArgs args,
1527 std::index_sequence<Is...>) {
1528 *rv = (
self->*f)(args[Is]...);
1532 template <
typename T,
typename R,
typename... Args>
1533 struct ModuleVTableEntryHelper<R (T::*)(Args...)> {
1534 using MemFnType = R (T::*)(Args...);
1535 using IndexSeq = std::index_sequence_for<Args...>;
1536 static constexpr
const std::size_t LenArgs =
sizeof...(Args);
1538 template <std::size_t... Is>
1539 static TVM_ALWAYS_INLINE
void Call(TVMRetValue* rv, T*
self, MemFnType f, TVMArgs args,
1540 std::index_sequence<Is...>) {
1541 *rv = (
self->*f)(args[Is]...);
1545 template <
typename T,
typename... Args>
1546 struct ModuleVTableEntryHelper<void (T::*)(Args...) const> {
1547 using MemFnType = void (T::*)(Args...)
const;
1548 using IndexSeq = std::index_sequence_for<Args...>;
1549 static constexpr
const std::size_t LenArgs =
sizeof...(Args);
1551 template <std::size_t... Is>
1552 static TVM_ALWAYS_INLINE
void Call(TVMRetValue* rv, T*
self, MemFnType f, TVMArgs args,
1553 std::index_sequence<Is...>) {
1554 (
self->*f)(args[Is]...);
1558 template <
typename T,
typename... Args>
1559 struct ModuleVTableEntryHelper<void (T::*)(Args...)> {
1560 using MemFnType = void (T::*)(Args...);
1561 using IndexSeq = std::index_sequence_for<Args...>;
1562 static constexpr
const std::size_t LenArgs =
sizeof...(Args);
1564 template <std::size_t... Is>
1565 static TVM_ALWAYS_INLINE
void Call(TVMRetValue* rv, T*
self, MemFnType f, TVMArgs args,
1566 std::index_sequence<Is...>) {
1567 (
self->*f)(args[Is]...);
1571 namespace parameter_pack {
1573 template <
typename... EnumArgs>
1574 struct EnumeratedParamPack {
1575 struct InvokeWithoutArg {
1576 template <
template <
size_t i,
typename TArgument>
class Functor,
typename ExtraParams>
1577 static void F(ExtraParams&& extra_params) {
1578 using TExpander =
int[];
1581 (Functor<EnumArgs::i, typename EnumArgs::T>::F(std::forward<ExtraParams>(extra_params)),
1586 struct InvokeWithArg {
1587 template <
template <
size_t i,
typename TArgument>
class Functor,
typename ExtraParams,
1589 static void F(ExtraParams&& extra_params, Params&&... params) {
1590 using TExpander =
int[];
1593 (Functor<EnumArgs::i, typename EnumArgs::T>::F(std::forward<ExtraParams>(extra_params),
1594 std::forward<Params>(params)),
1601 template <
typename... Args>
1602 struct EnumerateImpl {
1604 template <
size_t _i,
typename _T>
1606 static const constexpr
size_t i = _i;
1610 template <
typename...>
1613 template <std::size_t...
id>
1614 struct Zipper<std::integer_sequence<std::size_t, id...>> {
1615 using WithoutArg =
typename EnumeratedParamPack<Item<id, Args>...>::InvokeWithoutArg;
1616 using WithArg =
typename EnumeratedParamPack<Item<id, Args>...>::InvokeWithArg;
1620 using WithoutArg =
typename Zipper<std::index_sequence_for<Args...>>::WithoutArg;
1621 using WithArg =
typename Zipper<std::index_sequence_for<Args...>>::WithArg;
1624 template <
typename... Args>
1625 using EnumerateWithoutArg =
typename EnumerateImpl<Args...>::WithoutArg;
1627 template <
typename... Args>
1628 using EnumerateWithArg =
typename EnumerateImpl<Args...>::WithArg;
1630 template <
typename... Args>
1632 template <
template <
size_t i,
typename TArgument>
class Functor,
typename ExtraParams>
1633 static void InvokeWithoutArg(ExtraParams&& extra_params) {
1634 EnumerateWithoutArg<Args...>::template F<Functor, ExtraParams>(
1635 std::forward<ExtraParams>(extra_params));
1645 template <
typename T>
1646 struct func_signature_helper {
1650 template <
typename T,
typename R,
typename... Args>
1651 struct func_signature_helper<R (T::*)(Args...)> {
1652 using FType = R(Args...);
1653 using ParamType = parameter_pack::ParamPack<Args...>;
1655 static_assert(!std::is_reference<R>::value,
"TypedPackedFunc return reference");
1658 template <
typename T,
typename R,
typename... Args>
1659 struct func_signature_helper<R (T::*)(Args...) const> {
1660 using FType = R(Args...);
1661 using ParamType = parameter_pack::ParamPack<Args...>;
1663 static_assert(!std::is_reference<R>::value,
"TypedPackedFunc return reference");
1670 template <
typename T>
1671 struct function_signature {
1672 using FType =
typename func_signature_helper<decltype(&T::operator())>::FType;
1673 using ParamType =
typename func_signature_helper<decltype(&T::operator())>::ParamType;
1674 using RetType =
typename func_signature_helper<decltype(&T::operator())>::RetType;
1678 template <
typename R,
typename... Args>
1679 struct function_signature<R(Args...)> {
1680 using FType = R(Args...);
1681 using ParamType = parameter_pack::ParamPack<Args...>;
1683 static_assert(!std::is_reference<R>::value,
"TypedPackedFunc return reference");
1687 template <
typename R,
typename... Args>
1688 struct function_signature<R (*)(Args...)> {
1689 using FType = R(Args...);
1690 using ParamType = detail::parameter_pack::ParamPack<Args...>;
1692 static_assert(!std::is_reference<R>::value,
"TypedPackedFunc return reference");
1695 template <
typename TSignature>
1696 struct SignaturePrinter;
1698 namespace type2str {
1700 template <
typename T>
1701 struct TypeSimplifier;
1703 template <
typename T>
1705 template <typename = std::enable_if_t<std::is_base_of<ObjectRef, T>::value>>
1706 static std::string v() {
1707 return T::ContainerType::_type_key;
1711 struct Type2Str<int> {
1712 static std::string v() {
return "int"; }
1715 struct Type2Str<double> {
1716 static std::string v() {
return "double"; }
1719 struct Type2Str<int64_t> {
1720 static std::string v() {
return "int64_t"; }
1723 struct Type2Str<uint64_t> {
1724 static std::string v() {
return "uint64_t"; }
1727 struct Type2Str<bool> {
1728 static std::string v() {
return "bool"; }
1731 struct Type2Str<void> {
1732 static std::string v() {
return "void"; }
1735 struct Type2Str<std::basic_string<char>> {
1736 static std::string v() {
return "basic_string<char>"; }
1738 template <
typename K,
typename V>
1739 struct Type2Str<Map<K, V>> {
1740 static std::string v() {
1741 return "Map<" + TypeSimplifier<K>::v() +
", " + TypeSimplifier<V>::v() +
">";
1745 struct Type2Str<DLDevice> {
1746 static std::string v() {
return "DLDevice"; }
1749 struct Type2Str<DLTensor> {
1750 static std::string v() {
return "DLTensor"; }
1754 static std::string v() {
return "DataType"; }
1757 struct Type2Str<DLDataType> {
1758 static std::string v() {
return "DLDataType"; }
1761 struct Type2Str<TVMRetValue> {
1762 static std::string v() {
return "TVMRetValue"; }
1765 struct Type2Str<TVMArgValue> {
1766 static std::string v() {
return "TVMArgValue"; }
1770 static std::string v() {
return "TVMByteArray"; }
1772 template <
typename FType>
1773 struct Type2Str<TypedPackedFunc<FType>> {
1774 static std::string v() {
return SignaturePrinter<function_signature<FType>>::F(); }
1776 template <
typename T>
1777 struct Type2Str<Array<T>> {
1778 static std::string v() {
return "Array<" + TypeSimplifier<T>::v() +
">"; }
1785 template <
typename T>
1786 struct TypeSimplifier {
1787 static std::string v() {
1788 using U =
typename std::remove_cv<
1789 typename std::remove_reference<typename std::remove_pointer<T>::type>::type>::type;
1790 return (std::is_const<T>::value ?
"const " :
"") + Type2Str<U>::v() +
1791 (std::is_pointer<T>::value ?
"*" :
"") + (std::is_reference<T>::value ?
"&" :
"");
1801 template <
typename TSignature>
1802 struct SignaturePrinter {
1803 using ParamType =
typename TSignature::ParamType;
1804 using RetType =
typename TSignature::RetType;
1806 template <
size_t i,
typename TArgument>
1807 struct PrintParamType {
1808 static void F(std::ostream& os) {
1809 os << (i == 0 ?
"" :
", ") << i <<
": " << type2str::TypeSimplifier<TArgument>::v();
1813 static std::string F() {
1814 std::ostringstream oss;
1816 ParamType::template InvokeWithoutArg<PrintParamType>(oss);
1817 oss <<
") -> " << type2str::TypeSimplifier<RetType>::v();
1828 template <typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type>
1830 values_[i].
v_int64 =
static_cast<int64_t
>(value);
1831 type_codes_[i] = kDLInt;
1837 TVM_ALWAYS_INLINE
void operator()(
size_t i, uint64_t value)
const {
1838 values_[i].
v_int64 =
static_cast<int64_t
>(value);
1840 type_codes_[i] = kDLInt;
1842 TVM_ALWAYS_INLINE
void operator()(
size_t i,
double value)
const {
1844 type_codes_[i] = kDLFloat;
1846 TVM_ALWAYS_INLINE
void operator()(
size_t i, std::nullptr_t value)
const {
1851 values_[i] = value.
value_;
1858 TVM_ALWAYS_INLINE
void operator()(
size_t i, DLTensor* value)
const {
1866 TVM_ALWAYS_INLINE
void operator()(
size_t i, DLDataType value)
const {
1867 values_[i].
v_type = value;
1873 TVM_ALWAYS_INLINE
void operator()(
size_t i,
const char* value)
const {
1874 values_[i].
v_str = value;
1878 TVM_ALWAYS_INLINE
void operator()(
size_t i,
const std::string& value)
const {
1879 values_[i].
v_str = value.c_str();
1886 template <
typename FType>
1892 values_[i].
v_str = value.
ptr<std::string>()->c_str();
1896 values_[i] = value.
value_;
1901 template <
typename TObjectRef,
1902 typename =
typename std::enable_if<std::is_base_of<ObjectRef, TObjectRef>::value>::type>
1903 TVM_ALWAYS_INLINE
void operator()(
size_t i,
const TObjectRef& value)
const {
1904 this->SetObject(i, value);
1907 template <
typename TObjectRef,
1908 typename =
typename std::enable_if<std::is_base_of<
1909 ObjectRef,
typename std::remove_reference<TObjectRef>::type>::value>::type>
1910 TVM_ALWAYS_INLINE
void operator()(
size_t i, TObjectRef&& value)
const {
1911 this->SetObject(i, std::forward<TObjectRef>(value));
1915 template <
typename TObjectRef>
1916 inline void SetObject(
size_t i, TObjectRef&& value)
const;
1923 template <
typename... Args>
1925 const int kNumArgs =
sizeof...(Args);
1926 const int kArraySize = kNumArgs > 0 ? kNumArgs : 1;
1928 int type_codes[kArraySize];
1929 detail::for_each(
TVMArgsSetter(values, type_codes), std::forward<Args>(args)...);
1932 ->CallPacked(
TVMArgs(values, type_codes, kNumArgs), &rv);
1936 template <
size_t i,
typename T>
1939 (*setter)(i, std::forward<T>(value));
1943 template <
typename... Args>
1946 detail::parameter_pack::EnumerateWithArg<Args...>::template F<TVMArgsSetterApply>(
1947 &setter, std::forward<Args>(args)...);
1951 template <
typename R,
int nleft,
int index,
typename F>
1952 struct unpack_call_dispatcher {
1953 template <
typename... Args>
1954 TVM_ALWAYS_INLINE
static void run(
const std::string* optional_name,
FSig* f_sig,
const F& f,
1956 Args&&... unpacked_args) {
1959 unpack_call_dispatcher<R, nleft - 1, index + 1, F>::run(
1960 optional_name, f_sig, f, args_pack, rv, std::forward<Args>(unpacked_args)...,
1961 TVMMovableArgValueWithContext_(args_pack.
values[index], args_pack.
type_codes[index], index,
1962 optional_name, f_sig));
1966 template <
typename R,
int index,
typename F>
1967 struct unpack_call_dispatcher<R, 0, index, F> {
1968 template <
typename... Args>
1969 TVM_ALWAYS_INLINE
static void run(
const std::string* optional_name,
FSig* f_sig,
const F& f,
1970 const TVMArgs& args_pack, TVMRetValue* rv,
1971 Args&&... unpacked_args) {
1972 using RetType = decltype(f(std::forward<Args>(unpacked_args)...));
1973 if (std::is_same<RetType, R>::value) {
1974 *rv = f(std::forward<Args>(unpacked_args)...);
1976 *rv = R(f(std::forward<Args>(unpacked_args)...));
1981 template <
int index,
typename F>
1982 struct unpack_call_dispatcher<void, 0, index, F> {
1983 template <
typename... Args>
1984 TVM_ALWAYS_INLINE
static void run(
const std::string* optional_name,
FSig* f_sig,
const F& f,
1985 const TVMArgs& args_pack, TVMRetValue* rv,
1986 Args&&... unpacked_args) {
1987 f(std::forward<Args>(unpacked_args)...);
1991 template <
typename R,
int nargs,
typename F>
1992 TVM_ALWAYS_INLINE
void unpack_call(
const std::string* optional_name,
const F& f,
1993 const TVMArgs& args, TVMRetValue* rv) {
1994 FSig* f_sig = detail::SignaturePrinter<detail::function_signature<F>>::F;
1995 CHECK_EQ(nargs, args.size()) <<
"Function "
1996 << (optional_name ==
nullptr ?
"<anonymous>" : *optional_name)
1997 << (f_sig ==
nullptr ?
"" : (*f_sig)()) <<
" expects " << nargs
1998 <<
" arguments but " << args.size() <<
" were provided";
1999 unpack_call_dispatcher<R, nargs, 0, F>::run(optional_name, f_sig, f, args, rv);
2002 template <
typename FType>
2003 struct unpack_call_by_signature {};
2005 template <
typename R,
typename... Args>
2006 struct unpack_call_by_signature<R(Args...)> {
2007 template <
typename F>
2008 TVM_ALWAYS_INLINE
static void run(
const F& f,
const TVMArgs& args, TVMRetValue* rv) {
2009 unpack_call<R,
sizeof...(Args)>(
nullptr, f, args, rv);
2013 template <
typename R,
typename... Args>
2014 TVM_ALWAYS_INLINE R call_packed(
const PackedFunc& pf, Args&&... args) {
2015 return R(pf(std::forward<Args>(args)...));
2018 template <
typename R>
2019 struct typed_packed_call_dispatcher {
2020 template <
typename... Args>
2021 TVM_ALWAYS_INLINE
static R run(
const PackedFunc& pf, Args&&... args) {
2022 return pf(std::forward<Args>(args)...);
2027 struct typed_packed_call_dispatcher<void> {
2028 template <
typename... Args>
2029 TVM_ALWAYS_INLINE
static void run(
const PackedFunc& pf, Args&&... args) {
2030 pf(std::forward<Args>(args)...);
2035 template <
typename R,
typename... Args>
2038 template <
typename R,
typename... Args>
2042 template <
typename R,
typename... Args>
2046 template <
typename R,
typename... Args>
2050 template <
typename R,
typename... Args>
2051 template <
typename FType>
2052 inline void TypedPackedFunc<R(Args...)>::AssignTypedLambda(FType flambda, std::string name) {
2053 FSig* f_sig = detail::SignaturePrinter<detail::function_signature<FType>>::F;
2055 if (args.
size() !=
sizeof...(Args)) {
2056 LOG(FATAL) <<
"Function " << name << (f_sig ==
nullptr ?
"" : (*f_sig)()) <<
" expects "
2057 <<
sizeof...(Args) <<
" arguments, but " << args.
size() <<
" were provided.";
2059 detail::unpack_call<R,
sizeof...(Args)>(&name, flambda, args, rv);
2063 template <
typename R,
typename... Args>
2064 template <
typename FType>
2066 FSig* f_sig = detail::SignaturePrinter<detail::function_signature<FType>>::F;
2068 if (args.
size() !=
sizeof...(Args)) {
2069 LOG(FATAL) <<
"Function <anonymous> " << (*f_sig)() <<
" expects " <<
sizeof...(Args)
2070 <<
" arguments, but " << args.
size() <<
" were provided.";
2072 detail::unpack_call<R,
sizeof...(Args)>(
nullptr, flambda, args, rv);
2076 template <
typename R,
typename... Args>
2078 return detail::typed_packed_call_dispatcher<R>::run(packed_, std::forward<Args>(args)...);
2081 template <
typename T>
2085 return arg.operator T();
2086 }
catch (
const dmlc::Error& e) {
2087 LOG(FATAL) <<
"Argument " << i <<
" cannot be converted to type \""
2088 << tvm::runtime::detail::type2str::Type2Str<T>::v() <<
"\". Its type is \""
2099 template <
typename T>
2100 inline void TVMArgsSetter::SetObject(
size_t i, T&& value)
const {
2101 using ContainerType =
typename std::remove_reference<T>::type::ContainerType;
2102 if (!value.defined()) {
2108 Object* ptr = value.data_.data_;
2109 if constexpr (std::is_base_of_v<NDArray::ContainerType, ContainerType> ||
2110 std::is_base_of_v<ContainerType, NDArray::ContainerType>) {
2111 if (std::is_base_of_v<NDArray::ContainerType, ContainerType> ||
2119 if constexpr (std::is_base_of_v<Module::ContainerType, ContainerType> ||
2120 std::is_base_of_v<ContainerType, Module::ContainerType>) {
2121 if (std::is_base_of_v<Module::ContainerType, ContainerType> ||
2129 if constexpr (std::is_base_of_v<PackedFunc::ContainerType, ContainerType> ||
2130 std::is_base_of_v<ContainerType, PackedFunc::ContainerType>) {
2131 if (std::is_base_of_v<PackedFunc::ContainerType, ContainerType> ||
2141 if constexpr (std::is_base_of_v<Bool::ContainerType, ContainerType> ||
2142 std::is_base_of_v<ContainerType, Bool::ContainerType>) {
2143 if (std::is_base_of_v<Bool::ContainerType, ContainerType> ||
2170 if constexpr (std::is_base_of_v<Int::ContainerType, ContainerType> ||
2171 std::is_base_of_v<ContainerType, Int::ContainerType>) {
2172 if (std::is_base_of_v<Int::ContainerType, ContainerType> ||
2182 if constexpr (std::is_base_of_v<Float::ContainerType, ContainerType> ||
2183 std::is_base_of_v<ContainerType, Float::ContainerType>) {
2184 if (std::is_base_of_v<Float::ContainerType, ContainerType> ||
2194 if constexpr (std::is_rvalue_reference_v<decltype(value)>) {
2195 values_[i].
v_handle =
const_cast<Object**
>(&(value.data_.data_));
2198 values_[i].
v_handle = value.data_.data_;
2203 template <
typename Derived>
2204 template <
typename TObjectRef,
typename>
2206 using ContainerType =
typename TObjectRef::ContainerType;
2208 if (std::is_base_of<NDArray::ContainerType, ContainerType>::value) {
2213 if (std::is_base_of<Module::ContainerType, ContainerType>::value) {
2215 static_cast<Object*
>(value_.v_handle)->IsInstance<ContainerType>();
2217 if (std::is_base_of<PackedFunc::ContainerType, ContainerType>::value) {
2219 static_cast<Object*
>(value_.v_handle)->IsInstance<ContainerType>();
2225 return (std::is_base_of<ContainerType, NDArray::ContainerType>::value &&
2227 (std::is_base_of<ContainerType, Module::ContainerType>::value &&
2229 (std::is_base_of<ContainerType, PackedFunc::ContainerType>::value &&
2235 template <
typename Derived>
2236 template <
typename TObjectRef>
2238 static_assert(std::is_base_of<ObjectRef, TObjectRef>::value,
2239 "Conversion only works for ObjectRef");
2240 using ContainerType =
typename TObjectRef::ContainerType;
2243 CHECK(TObjectRef::_type_is_nullable)
2244 <<
"Expect a not null value of " << ContainerType::_type_key;
2250 if constexpr (std::is_base_of_v<NDArray::ContainerType, ContainerType>) {
2255 CHECK(data->IsInstance<ContainerType>())
2256 <<
"Expected " << ContainerType::_type_key <<
" but got " << data->GetTypeKey();
2257 return TObjectRef(data);
2260 if constexpr (std::is_base_of_v<Module::ContainerType, ContainerType>) {
2264 CHECK(data->IsInstance<ContainerType>())
2265 <<
"Expected " << ContainerType::_type_key <<
" but got " << data->GetTypeKey();
2266 return TObjectRef(data);
2269 if constexpr (std::is_base_of_v<PackedFunc::ContainerType, ContainerType>) {
2273 CHECK(data->IsInstance<ContainerType>())
2274 <<
"Expected " << ContainerType::_type_key <<
" but got " << data->GetTypeKey();
2275 return TObjectRef(data);
2283 <<
", but got " << checked_type.
value();
2284 return TObjectRef(GetObjectPtr<Object>(ptr));
2289 <<
", but got " << checked_type.
value();
2290 return TObjectRef(GetObjectPtr<Object>(ptr));
2293 if constexpr (std::is_base_of_v<ContainerType, NDArray::ContainerType>) {
2298 return TObjectRef(data);
2302 if constexpr (std::is_base_of_v<ContainerType, Module::ContainerType>) {
2305 return TObjectRef(GetObjectPtr<Object>(
static_cast<Object*
>(value_.v_handle)));
2309 if constexpr (std::is_base_of_v<ContainerType, PackedFunc::ContainerType>) {
2312 return TObjectRef(GetObjectPtr<Object>(
static_cast<Object*
>(value_.v_handle)));
2316 if constexpr (std::is_base_of_v<TObjectRef, Int>) {
2318 return Int(value_.v_int64);
2322 if constexpr (std::is_base_of_v<TObjectRef, Float>) {
2324 return Float(value_.v_float64);
2328 if constexpr (std::is_base_of_v<TObjectRef, Bool>) {
2330 return Bool(value_.v_int64);
2334 if constexpr (std::is_base_of_v<TObjectRef, String>) {
2345 return String(
static_cast<const Derived*
>(
this)->
operator std::string());
2353 template <
typename TObjectRef,
typename>
2355 using ContainerType =
typename TObjectRef::ContainerType;
2380 if constexpr (std::is_base_of_v<ContainerType, NDArray::ContainerType> ||
2381 std::is_base_of_v<NDArray::ContainerType, ContainerType>) {
2382 if (std::is_base_of_v<NDArray::ContainerType, ContainerType> ||
2388 if constexpr (std::is_base_of_v<ContainerType, Module::ContainerType> ||
2389 std::is_base_of_v<Module::ContainerType, ContainerType>) {
2390 if (std::is_base_of_v<Module::ContainerType, ContainerType> ||
2396 if constexpr (std::is_base_of_v<ContainerType, PackedFunc::ContainerType> ||
2397 std::is_base_of_v<PackedFunc::ContainerType, ContainerType>) {
2398 if (std::is_base_of_v<PackedFunc::ContainerType, ContainerType> ||
2404 if constexpr (std::is_base_of_v<Bool, TObjectRef> || std::is_base_of_v<TObjectRef, Bool>) {
2411 if constexpr (std::is_base_of_v<Int, TObjectRef> || std::is_base_of_v<TObjectRef, Int>) {
2418 if constexpr (std::is_base_of_v<Float, TObjectRef> || std::is_base_of_v<TObjectRef, Float>) {
2440 template <
typename T,
typename>
2441 inline TVMArgValue::operator T()
const {
2445 template <
typename T,
typename>
2446 inline TVMMovableArgValue_::operator T()
const {
2448 auto** ref =
static_cast<Object**
>(value_.v_handle);
2457 template <
typename T,
typename>
2458 inline TVMRetValue::operator T()
const {
2463 return (*this)->GetFunction(name, query_imports);
2469 template <
typename PODSub
class>
2471 if (val.template IsObjectRef<tvm::runtime::String>()) {
2472 return val.template AsObjectRef<tvm::runtime::String>();
2479 template <
typename T>
2484 if constexpr (std::is_same_v<T, ObjectRef>) {
2485 return untyped_array;
2525 if constexpr (std::is_same_v<T, ObjectRef>) {
2526 return untyped_array;
2531 item_val = std::move(item);
2537 template <
typename T,
typename U>
2542 if constexpr (std::is_same_v<T, ObjectRef> && std::is_same_v<U, ObjectRef>) {
2543 return Downcast<Map<T, U>>(untyped_map);
2549 return Downcast<Map<T, U>>(untyped_map);
2553 for (
const auto& kv : untyped_map) {
2555 if constexpr (std::is_same_v<T, ObjectRef>) {
2561 setter(0, kv.first);
2566 U new_value = [&]() {
2567 if constexpr (std::is_same_v<U, ObjectRef>) {
2573 setter(0, kv.second);
2578 output.
Set(new_key, new_value);
2585 if constexpr (std::is_same_v<T, ObjectRef> && std::is_same_v<U, ObjectRef>) {
2586 return Downcast<Map<T, U>>(untyped_map);
2592 return Downcast<Map<T, U>>(untyped_map);
2596 for (
const auto& kv : untyped_map) {
2598 if constexpr (std::is_same_v<T, ObjectRef>) {
2606 U new_value = [&]() {
2607 if constexpr (std::is_same_v<U, ObjectRef>) {
2615 output.
Set(new_key, new_value);
2621 template <
typename T>
2633 template <
typename... VariantTypes>
2640 template <
typename PODSub
class>
2642 if (
auto opt = TryAsObjectRef<VariantTypes...>(val)) {
2646 if (
auto opt = TryValueConverter<VariantTypes...>(val)) {
2650 LOG(FATAL) <<
"Expected one of "
2651 <<
static_cast<const std::stringstream&
>(
2652 (std::stringstream() << ... << VariantTypes::ContainerType::_type_key))
2657 template <
typename VarFirst,
typename... VarRest,
typename PODSubclass>
2659 if (val.template IsObjectRef<VarFirst>()) {
2660 return VType(val.template AsObjectRef<VarFirst>());
2661 }
else if constexpr (
sizeof...(VarRest)) {
2662 return TryAsObjectRef<VarRest...>(val);
2668 template <
typename VarFirst,
typename... VarRest,
typename PODSubclass>
2672 }
catch (
const Error&) {
2675 if constexpr (
sizeof...(VarRest)) {
2676 return TryValueConverter<VarRest...>(val);
2687 inline TVMArgValue::operator DLDataType()
const {
2700 return value_.v_type;
Runtime Array container types.
Runtime container types for primitives stored as ObjectRef.
@ kTVMPackedFuncHandle
Definition: c_runtime_api.h:185
@ kTVMNDArrayHandle
Definition: c_runtime_api.h:188
@ kTVMModuleHandle
Definition: c_runtime_api.h:184
@ kTVMBytes
Definition: c_runtime_api.h:187
@ kTVMDataType
Definition: c_runtime_api.h:180
@ kTVMArgBool
Definition: c_runtime_api.h:190
@ kTVMArgInt
Definition: c_runtime_api.h:176
@ kTVMDLTensorHandle
Definition: c_runtime_api.h:182
@ kDLDevice
Definition: c_runtime_api.h:181
@ kTVMOpaqueHandle
Definition: c_runtime_api.h:178
@ kTVMObjectHandle
Definition: c_runtime_api.h:183
@ kTVMObjectRValueRefArg
Definition: c_runtime_api.h:189
@ kTVMNullptr
Definition: c_runtime_api.h:179
@ kTVMArgFloat
Definition: c_runtime_api.h:177
@ kTVMStr
Definition: c_runtime_api.h:186
@ kDLMicroDev
Definition: c_runtime_api.h:125
@ kOpenGL
Definition: c_runtime_api.h:124
@ kDLSDAccel
Definition: c_runtime_api.h:123
@ kDLAOCL
Definition: c_runtime_api.h:122
DLTensor * TVMArrayHandle
the array handle
Definition: c_runtime_api.h:204
array node content in array
Definition: array.h:40
size_t size() const
Definition: array.h:43
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Array< U > Map(F fmap) const
Helper function to apply a map function onto the array.
Definition: array.h:651
Runtime primitive data type.
Definition: data_type.h:43
Shared content of all specializations of hash map.
Definition: map.h:174
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
void Set(const K &key, const V &value)
set the Map.
Definition: map.h:1374
Base container of module.
Definition: module.h:142
Module container of TVM.
Definition: module.h:79
PackedFunc GetFunction(const String &name, bool query_imports=false)
Get packed function from current module by name.
Definition: packed_func.h:2462
Object container class that backs NDArray.
Definition: ndarray.h:306
Managed NDArray. The array is backed by reference counted blocks.
Definition: ndarray.h:51
static TVMArrayHandle FFIGetHandle(const ObjectRef &nd)
Get FFI Array handle from ndarray.
Definition: ndarray.h:434
static ObjectPtr< Object > FFIDataFromHandle(TVMArrayHandle handle)
Construct NDArray's Data field from array handle in FFI.
Definition: ndarray.h:429
static void FFIDecRef(TVMArrayHandle handle)
DecRef resource managed by an FFI array handle.
Definition: ndarray.h:442
A custom smart pointer for Object.
Definition: object.h:362
Base class of all object reference.
Definition: object.h:519
bool defined() const
Definition: object.h:552
static void FFIClearAfterMove(ObjectRef *ref)
Clear the object ref data field without DecRef after we successfully moved the field.
Definition: object.h:623
friend class TVMArgsSetter
Definition: object.h:637
const Object * get() const
Definition: object.h:554
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:605
base class of all object containers.
Definition: object.h:171
std::string GetTypeKey() const
Definition: object.h:184
bool IsInstance() const
Definition: object.h:874
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
T value() const
Definition: optional.h:92
Object container class that backs PackedFunc.
Definition: packed_func.h:71
PackedFuncObj(FCallPacked *f_call_pack)
Constructing a packed function object from a function pointer.
Definition: packed_func.h:106
static constexpr const uint32_t _type_index
Definition: packed_func.h:80
TVM_DECLARE_FINAL_OBJECT_INFO(PackedFuncObj, Object)
FCallPacked * f_call_packed_
Internal callable function pointer used to call the packed function.
Definition: packed_func.h:112
static constexpr const char * _type_key
Definition: packed_func.h:81
PackedFuncObj()=delete
Delete the default constructor explicitly.
void(const PackedFuncObj *, TVMArgs, TVMRetValue *) FCallPacked
The internal callable function type.
Definition: packed_func.h:100
TVM_ALWAYS_INLINE void CallPacked(TVMArgs args, TVMRetValue *rv) const
Call the function in packed format.
Definition: packed_func.h:1397
Derived object class for constructing PackedFuncObj.
Definition: packed_func.h:117
PackedFuncSubObj(TCallable callable)
Derived object class for constructing PackedFuncObj.
Definition: packed_func.h:127
TStorage callable_
Type-erased filed for storing callable object.
Definition: packed_func.h:130
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:141
bool operator!=(std::nullptr_t null) const
Definition: packed_func.h:183
bool operator==(std::nullptr_t null) const
Definition: packed_func.h:181
TVMRetValue operator()(Args &&... args) const
Call packed function by directly passing in unpacked format.
Definition: packed_func.h:1924
TVM_ALWAYS_INLINE void CallPacked(TVMArgs args, TVMRetValue *rv) const
Call the function in packed format.
Definition: packed_func.h:1401
PackedFunc(TCallable data)
Constructing a packed function from a callable type whose signature is consistent with PackedFunc
Definition: packed_func.h:154
TVM_DEFINE_OBJECT_REF_METHODS(PackedFunc, ObjectRef, PackedFuncObj)
PackedFunc(std::nullptr_t null)
Constructor from null.
Definition: packed_func.h:144
Reference to string objects.
Definition: string.h:98
static bool CanConvertFrom(const TVMArgValue &val)
Check if a TVMArgValue can be converted to String, i.e. it can be std::string or String.
Definition: packed_func.h:2683
A single argument value to PackedFunc. Containing both type_code and TVMValue.
Definition: packed_func.h:796
bool IsObjectRef() const
Definition: packed_func.h:2205
const TVMValue & value() const
Definition: packed_func.h:838
TVMArgValue(TVMValue value, int type_code)
constructor
Definition: packed_func.h:805
TObjectRef AsObjectRef() const
Definition: packed_func.h:2237
TVMArgValue()
default constructor
Definition: packed_func.h:799
Definition: packed_func.h:1824
TVM_ALWAYS_INLINE void operator()(size_t i, DLTensor *value) const
Definition: packed_func.h:1858
TVM_ALWAYS_INLINE void operator()(size_t i, uint64_t value) const
Definition: packed_func.h:1837
TVM_ALWAYS_INLINE void operator()(size_t i, const TypedPackedFunc< FType > &value) const
Definition: packed_func.h:1887
TVM_ALWAYS_INLINE void operator()(size_t i, bool value) const
Definition: packed_func.h:1833
TVM_ALWAYS_INLINE void operator()(size_t i, const TObjectRef &value) const
Definition: packed_func.h:1903
void operator()(size_t i, const TVMRetValue &value) const
Definition: packed_func.h:1890
TVM_ALWAYS_INLINE void operator()(size_t i, TObjectRef &&value) const
Definition: packed_func.h:1910
TVM_ALWAYS_INLINE void operator()(size_t i, double value) const
Definition: packed_func.h:1842
TVM_ALWAYS_INLINE void operator()(size_t i, T value) const
Definition: packed_func.h:1829
TVMArgsSetter(TVMValue *values, int *type_codes)
Definition: packed_func.h:1826
TVM_ALWAYS_INLINE void operator()(size_t i, const char *value) const
Definition: packed_func.h:1873
TVM_ALWAYS_INLINE void operator()(size_t i, Device value) const
Definition: packed_func.h:1862
TVM_ALWAYS_INLINE void operator()(size_t i, void *value) const
Definition: packed_func.h:1854
TVM_ALWAYS_INLINE void operator()(size_t i, const std::string &value) const
Definition: packed_func.h:1878
TVM_ALWAYS_INLINE void operator()(size_t i, std::nullptr_t value) const
Definition: packed_func.h:1846
TVM_ALWAYS_INLINE void operator()(size_t i, const TVMByteArray &value) const
Definition: packed_func.h:1882
TVM_ALWAYS_INLINE void operator()(size_t i, DataType dtype) const
Definition: packed_func.h:1870
TVM_ALWAYS_INLINE void operator()(size_t i, DLDataType value) const
Definition: packed_func.h:1866
TVM_ALWAYS_INLINE void operator()(size_t i, const TVMArgValue &value) const
Definition: packed_func.h:1850
Arguments into TVM functions.
Definition: packed_func.h:394
T At(int i) const
Get the i-th argument and do proper type checking with detailed error messages.
Definition: packed_func.h:2082
const TVMValue * values
Definition: packed_func.h:396
TVMArgs(const TVMValue *values, const int *type_codes, int num_args)
constructor
Definition: packed_func.h:405
int size() const
Definition: packed_func.h:1389
TVMArgValue operator[](int i) const
Get i-th argument.
Definition: packed_func.h:1383
const int * type_codes
Definition: packed_func.h:397
int num_args
Definition: packed_func.h:398
Internal auxiliary struct for TypedPackedFunc to indicate a movable argument with additional context ...
Definition: packed_func.h:901
TVMMovableArgValueWithContext_(TVMValue value, int type_code, int arg_index, const std::string *optional_name, FSig *f_sig)
move constructor from another return value.
Definition: packed_func.h:912
Internal auxiliary struct for TypedPackedFunc to indicate a movable argument.
Definition: packed_func.h:856
TVMMovableArgValue_(TVMValue value, int type_code)
Definition: packed_func.h:858
A utility class that adds methods useful for each POD type.
Definition: packed_func.h:738
bool IsObjectRef() const
Definition: packed_func.h:2205
TObjectRef AsObjectRef() const
Definition: packed_func.h:2237
Internal base class to handle conversion to POD values.
Definition: packed_func.h:615
TVMPODValue_()
Definition: packed_func.h:704
std::optional< bool > TryAsBool() const
Definition: packed_func.h:667
TVMValue value_
The value.
Definition: packed_func.h:708
std::optional< int64_t > TryAsInt() const
Definition: packed_func.h:678
std::optional< double > TryAsFloat() const
Definition: packed_func.h:689
T * ptr() const
return handle as specific pointer type.
Definition: packed_func.h:663
int type_code_
the type code
Definition: packed_func.h:710
int type_code() const
Definition: packed_func.h:656
TVMPODValue_(TVMValue value, int type_code)
Definition: packed_func.h:705
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:946
static TVMRetValue MoveFromCHost(TVMValue value, int type_code)
Construct a new TVMRetValue by moving from return value stored via C API.
Definition: packed_func.h:1114
const TVMValue & value() const
Definition: packed_func.h:1124
~TVMRetValue()
destructor
Definition: packed_func.h:959
TVMRetValue & operator=(DLDataType t)
Definition: packed_func.h:1036
TVMRetValue & operator=(const TypedPackedFunc< FType > &f)
Definition: packed_func.h:1076
TVMRetValue & operator=(std::nullptr_t value)
Definition: packed_func.h:1011
TVMRetValue & operator=(TVMRetValue &&other)
Definition: packed_func.h:999
TVMRetValue & operator=(PackedFunc f)
Definition: packed_func.h:1071
TVMRetValue & operator=(bool value)
Definition: packed_func.h:1042
TVMRetValue & operator=(int value)
Definition: packed_func.h:1026
void MoveToCHost(TVMValue *ret_value, int *ret_type_code)
Move the value back to front-end via C API. This marks the current container as null....
Definition: packed_func.h:1100
TVMRetValue()
default constructor
Definition: packed_func.h:949
TVMRetValue & operator=(void *value)
Definition: packed_func.h:1016
TObjectRef AsObjectRef() const
Definition: packed_func.h:2237
TVMRetValue & operator=(std::string value)
Definition: packed_func.h:1047
TVMRetValue & operator=(const TVMArgValue &other)
Definition: packed_func.h:1083
TVMRetValue(const TVMRetValue &other)
Definition: packed_func.h:975
TVMRetValue & operator=(double value)
Definition: packed_func.h:1006
TVMRetValue(TVMRetValue &&other)
move constructor from another return value.
Definition: packed_func.h:954
TVMRetValue & operator=(const TVMRetValue &other)
Definition: packed_func.h:1079
TVMRetValue & operator=(int64_t value)
Definition: packed_func.h:1021
TVMRetValue & operator=(Module m)
Definition: packed_func.h:1067
TVMRetValue & operator=(const DataType &other)
Definition: packed_func.h:1041
TVMRetValue & operator=(DLDevice value)
Definition: packed_func.h:1031
TVMRetValue & operator=(TVMMovableArgValue_ &&other)
Definition: packed_func.h:1087
TVMRetValue & operator=(TVMByteArray value)
Definition: packed_func.h:1051
TVMRetValue & operator=(NDArray other)
Definition: packed_func.h:1055
A PackedFunc wrapper to provide typed function signature. It is backed by a PackedFunc internally.
Definition: packed_func.h:230
const PackedFunc & packed() const
Definition: packed_func.h:361
bool operator==(std::nullptr_t null) const
Definition: packed_func.h:363
TypedPackedFunc(const FLambda &typed_lambda)
construct from a lambda function with the same signature.
Definition: packed_func.h:312
TypedPackedFunc()
default constructor
Definition: packed_func.h:235
TypedPackedFunc(const FLambda &typed_lambda, std::string name)
construct from a lambda function with the same signature.
Definition: packed_func.h:289
TSelf & operator=(FLambda typed_lambda)
copy assignment operator from typed lambda
Definition: packed_func.h:334
TSelf & operator=(PackedFunc packed)
copy assignment operator from PackedFunc.
Definition: packed_func.h:343
bool operator!=(std::nullptr_t null) const
Definition: packed_func.h:365
TypedPackedFunc(std::nullptr_t null)
constructor from null
Definition: packed_func.h:237
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:63
Runtime Map container types.
Box< bool > Bool
Boxed version of C++ bool.
Definition: boxed_primitive.h:121
const char * ArgTypeCode2Str(int type_code)
Convert argument type code to string.
Definition: packed_func.h:1406
const char * DLDeviceType2Str(int type)
The name of DLDeviceType.
Definition: packed_func.h:1451
DLDataType String2DLDataType(std::string s)
convert a string to TVM type.
Definition: data_type.h:429
std::string() FSig
Using static function to output TypedPackedFunc signature.
Definition: packed_func.h:189
Box< int64_t > Int
Boxed version of C++ int64_t.
Definition: boxed_primitive.h:99
void TVM_ALWAYS_INLINE PackArgs(TVMValue *values, int *type_codes, Args &&... args)
Definition: packed_func.h:1944
Box< double > Float
Boxed version of C++ double.
Definition: boxed_primitive.h:107
std::string DLDataType2String(DLDataType t)
convert a TVM type to string.
Definition: data_type.h:422
std::ostream & operator<<(std::ostream &os, const ObjectRef &n)
Definition: repr_printer.h:97
Object * TVMArrayHandleToObjectHandle(TVMArrayHandle handle)
Definition: ndarray.h:446
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
PrimExpr max(PrimExpr a, PrimExpr b, Span span=Span())
take maximum of two values
runtime::DataType DataType
Definition: data_type.h:493
DLDevice Device
Definition: ndarray.h:43
PrimExpr min(PrimExpr a, PrimExpr b, Span span=Span())
take minimum of two values
constexpr runtime::NullOptType NullOpt
Definition: optional.h:169
A device-independent managed NDArray abstraction.
A managed object in the TVM runtime.
#define TVM_LOG_INCORRECT_TYPE_CODE(CODE, T)
Definition: packed_func.h:434
#define TVM_CHECK_TYPE_CODE(CODE, T)
Definition: packed_func.h:438
Runtime container of the functions generated by TVM, This is used to support dynamically link,...
Byte array type used to pass in byte array When kTVMBytes is used as data type.
Definition: c_runtime_api.h:223
size_t size
Definition: c_runtime_api.h:225
const char * data
Definition: c_runtime_api.h:224
static std::string TypeName()
Definition: packed_func.h:522
static bool Check(const Object *ptr)
Definition: packed_func.h:509
static Optional< String > CheckAndGetMismatch(const Object *ptr)
Definition: packed_func.h:487
static bool Check(const Object *ptr)
Definition: packed_func.h:555
static Optional< String > CheckAndGetMismatch(const Object *ptr)
Definition: packed_func.h:527
static std::string TypeName()
Definition: packed_func.h:574
static std::string VariantNames()
Definition: packed_func.h:605
static Optional< String > CheckAndGetMismatch(const Object *ptr)
Definition: packed_func.h:592
static std::string TypeName()
Definition: packed_func.h:604
static bool Check(const Object *ptr)
Definition: packed_func.h:600
static Optional< String > CheckAndGetMismatch(const Object *ptr)
Definition: packed_func.h:582
static bool Check(const Object *ptr)
Definition: packed_func.h:585
static std::string TypeName()
Definition: packed_func.h:586
static std::string VariantNames()
Definition: packed_func.h:587
Type traits for runtime type check during FFI conversion.
Definition: packed_func.h:445
static std::string TypeName()
Definition: packed_func.h:478
static bool Check(const Object *ptr)
Check if an object matches the template type.
Definition: packed_func.h:473
static Optional< String > CheckAndGetMismatch(const Object *ptr)
Check if an object matches the template type and return the mismatched type if it exists.
Definition: packed_func.h:453
static Array< T > From(const TVMArgValue &val)
Definition: packed_func.h:2481
static Array< T > From(const TVMRetValue &val)
Definition: packed_func.h:2522
static Map< T, U > From(const TVMArgValue &val)
Definition: packed_func.h:2539
static Map< T, U > From(const TVMRetValue &val)
Definition: packed_func.h:2582
static Optional< T > From(const TVMRetValue &val)
Definition: packed_func.h:2627
static Optional< T > From(const TVMArgValue &val)
Definition: packed_func.h:2623
static VType From(const PODSubclass &val)
Definition: packed_func.h:2641
static Optional< VType > TryValueConverter(const PODSubclass &val)
Definition: packed_func.h:2669
static Optional< VType > TryAsObjectRef(const PODSubclass &val)
Definition: packed_func.h:2658
static String From(const PODSubclass &val)
Definition: packed_func.h:2470
Type trait to specify special value conversion rules from TVMArgValue and TVMRetValue.
Definition: packed_func.h:1246
static TObjectRef From(const TVMRetValue &val)
Convert a TObjectRef from a return value.
Definition: packed_func.h:1258
static TObjectRef From(const TVMArgValue &val)
Convert a TObjectRef from an argument value.
Definition: packed_func.h:1252
Definition: packed_func.h:65
Definition: packed_func.h:1937
static TVM_ALWAYS_INLINE void F(TVMArgsSetter *setter, T &&value)
Definition: packed_func.h:1938
@ kRuntimePackedFunc
runtime::PackedFunc.
Definition: object.h:74
Union type of values being passed through API and function calls.
Definition: c_runtime_api.h:210
DLDevice v_device
Definition: c_runtime_api.h:216
void * v_handle
Definition: c_runtime_api.h:213
DLDataType v_type
Definition: c_runtime_api.h:215
int64_t v_int64
Definition: c_runtime_api.h:211
const char * v_str
Definition: c_runtime_api.h:214
double v_float64
Definition: c_runtime_api.h:212
Runtime Variant container types.