24 #ifndef TVM_RUNTIME_PACKED_FUNC_H_
25 #define TVM_RUNTIME_PACKED_FUNC_H_
32 #include <tvm/runtime/logging.h>
42 #include <type_traits>
47 #ifndef TVM_RUNTIME_HEADER_ONLY
48 #define TVM_RUNTIME_HEADER_ONLY 0
57 class TVMMovableArgValueWithContext_;
60 template <
typename FType>
62 template <
typename TSignature>
79 static constexpr
const char*
_type_key =
"runtime.PackedFunc";
86 template <
class TPackedFuncSubObj>
114 template <
class TCallable>
116 using TStorage =
typename std::remove_cv<typename std::remove_reference<TCallable>::type>::type;
148 template <
typename TCallable,
149 typename = std::enable_if_t<
151 !std::is_base_of<TCallable, PackedFunc>::value>>
154 data_ = make_object<ObjType>(std::forward<TCallable>(data));
170 template <
typename... Args>
192 template <
typename FType>
227 template <
typename R,
typename... Args>
285 template <
typename FLambda,
typename =
typename std::enable_if<std::is_convertible<
286 FLambda, std::function<R(Args...)>>::value>::type>
288 this->AssignTypedLambda(typed_lambda, name);
308 template <
typename FLambda,
typename =
typename std::enable_if<std::is_convertible<
309 FLambda, std::function<R(Args...)>>::value>::type>
311 this->AssignTypedLambda(typed_lambda);
329 template <
typename FLambda,
typename =
typename std::enable_if<
330 std::is_convertible<FLambda,
331 std::function<R(Args...)>>::value>::type>
333 this->AssignTypedLambda(typed_lambda);
350 TVM_ALWAYS_INLINE R operator()(Args... args)
const;
361 bool operator==(std::nullptr_t
null)
const {
return packed_ ==
nullptr; }
363 bool operator!=(std::nullptr_t
null)
const {
return packed_ !=
nullptr; }
377 template <
typename FLambda>
378 inline void AssignTypedLambda(FLambda flambda, std::string name);
387 template <
typename FLambda>
388 inline void AssignTypedLambda(FLambda flambda);
406 inline int size()
const;
419 template <
typename T>
420 inline T
At(
int i)
const;
430 inline std::ostream&
operator<<(std::ostream& os, DLDevice dev);
433 #define TVM_CHECK_TYPE_CODE(CODE, T) \
434 ICHECK_EQ(CODE, T) << "expected " << ArgTypeCode2Str(T) << " but got " << ArgTypeCode2Str(CODE)
440 template <
typename T>
450 using ContainerType =
typename T::ContainerType;
451 if (ptr ==
nullptr) {
452 if (T::_type_is_nullable) {
470 using ContainerType =
typename T::ContainerType;
471 if (ptr ==
nullptr)
return T::_type_is_nullable;
475 using ContainerType =
typename T::ContainerType;
476 return ContainerType::_type_key;
481 template <
typename T>
484 if (ptr ==
nullptr) {
491 for (
size_t i = 0; i < n->
size(); i++) {
495 return String(
"Array[index " + std::to_string(i) +
": " + check_subtype.
value() +
"]");
501 if (ptr ==
nullptr)
return true;
513 template <
typename K,
typename V>
516 if (ptr ==
nullptr)
return NullOpt;
519 for (
const auto& kv : *n) {
523 std::string key_name =
525 std::string value_name = value_type.
defined() ? std::string(value_type.
value())
527 return String(
"Map[" + key_name +
", " + value_name +
"]");
533 if (ptr ==
nullptr)
return true;
536 for (
const auto& kv : *n) {
554 operator double()
const {
564 operator int64_t()
const {
568 operator uint64_t()
const {
572 operator int()
const {
578 operator bool()
const {
582 operator void*()
const {
588 operator DLTensor*()
const {
593 LOG(FATAL) <<
"Expected "
627 template <
typename T>
632 template <
typename TObjectRef,
633 typename =
typename std::enable_if<std::is_base_of<ObjectRef, TObjectRef>::value>::type>
635 template <
typename TObjectRef>
668 using TVMPODValue_::operator double;
669 using TVMPODValue_::operator int64_t;
670 using TVMPODValue_::operator uint64_t;
671 using TVMPODValue_::operator int;
672 using TVMPODValue_::operator bool;
673 using TVMPODValue_::operator
void*;
674 using TVMPODValue_::operator DLTensor*;
675 using TVMPODValue_::operator
NDArray;
676 using TVMPODValue_::operator
Device;
677 using TVMPODValue_::operator
Module;
683 operator std::string()
const {
688 return std::string(arr->
data, arr->
size);
692 return AsObjectRef<tvm::runtime::String>().operator std::string();
695 template <
typename FType>
701 template <typename T, typename = typename std::enable_if<std::is_class<T>::value>::type>
702 inline operator T()
const;
703 inline operator DLDataType()
const;
721 using TVMPODValue_::operator double;
722 using TVMPODValue_::operator int64_t;
723 using TVMPODValue_::operator uint64_t;
724 using TVMPODValue_::operator int;
725 using TVMPODValue_::operator bool;
726 using TVMPODValue_::operator
void*;
727 using TVMPODValue_::operator DLTensor*;
728 using TVMPODValue_::operator
NDArray;
729 using TVMPODValue_::operator
Device;
730 using TVMPODValue_::operator
Module;
733 operator std::string()
const {
return AsArgValue().operator std::string(); }
734 template <
typename FType>
738 operator DLDataType()
const {
return AsArgValue().operator DLDataType(); }
746 template <
typename T,
747 typename =
typename std::enable_if<std::is_base_of<ObjectRef, T>::value>::type>
748 inline operator T()
const;
774 const std::string* optional_name,
FSig* f_sig)
775 : value_(value, type_code),
776 arg_index_(arg_index),
777 optional_name_(optional_name),
780 template <
typename T>
784 }
catch (dmlc::Error& e) {
785 LOG(FATAL) <<
"In function " << (optional_name_ ==
nullptr ?
"<anonymous>" : *optional_name_)
786 << (f_sig_ ==
nullptr ?
"" : (*f_sig_)()) <<
": error while converting argument "
787 << arg_index_ <<
": " << e.what();
795 const std::string* optional_name_;
822 using TVMPODValue_::operator double;
823 using TVMPODValue_::operator int64_t;
824 using TVMPODValue_::operator uint64_t;
825 using TVMPODValue_::operator int;
826 using TVMPODValue_::operator bool;
827 using TVMPODValue_::operator
void*;
828 using TVMPODValue_::operator DLTensor*;
829 using TVMPODValue_::operator
Device;
830 using TVMPODValue_::operator
NDArray;
831 using TVMPODValue_::operator
Module;
838 operator std::string()
const {
842 return *ptr<std::string>();
845 return *ptr<std::string>();
847 operator DLDataType()
const {
855 template <
typename FType>
868 this->SwitchToPOD(kDLFloat);
883 this->SwitchToPOD(kDLInt);
888 this->SwitchToPOD(kDLInt);
904 this->SwitchToPOD(kDLInt);
917 if (other.
data_ !=
nullptr) {
936 template <
typename FType>
987 <<
"TVMRetValue.value can only be used for POD data";
991 template <
typename TObjectRef,
992 typename =
typename std::enable_if<std::is_base_of<ObjectRef, TObjectRef>::value>::type>
994 template <typename T, typename = typename std::enable_if<std::is_class<T>::value>::type>
995 inline operator T()
const;
998 template <
typename T>
999 void Assign(
const T& other) {
1000 switch (other.type_code()) {
1002 SwitchToClass<std::string>(
kTVMStr, other);
1006 SwitchToClass<std::string>(
kTVMBytes, other);
1010 *
this = other.operator PackedFunc();
1014 *
this = other.operator Module();
1018 *
this = other.operator NDArray();
1024 GetObjectPtr<Object>(
static_cast<Object*
>(other.value_.v_handle)));
1032 SwitchToPOD(other.type_code());
1045 template <
typename T>
1046 void SwitchToClass(
int type_code, T v) {
1055 void SwitchToObject(
int type_code, ObjectPtr<Object> other) {
1056 if (other.data_ !=
nullptr) {
1061 other.data_ =
nullptr;
1072 delete ptr<std::string>();
1103 template <
typename TObjectRef>
1138 #define TVM_DLL_EXPORT_PACKED_FUNC(ExportName, Function) \
1140 TVM_DLL int ExportName(TVMValue* args, int* type_code, int num_args, TVMValue* out_value, \
1141 int* out_type_code, void* resource_handle); \
1142 int ExportName(TVMValue* args, int* type_code, int num_args, TVMValue* out_value, \
1143 int* out_type_code, void* resource_handle) { \
1145 ::tvm::runtime::TVMRetValue rv; \
1146 Function(::tvm::runtime::TVMArgs(args, type_code, num_args), &rv); \
1147 rv.MoveToCHost(out_value, out_type_code); \
1149 } catch (const ::std::exception& _except_) { \
1150 TVMAPISetLastError(_except_.what()); \
1156 #define TVM_MODULE_VTABLE_BEGIN(TypeKey) \
1157 const char* type_key() const final { return TypeKey; } \
1158 PackedFunc GetFunction(const String& _name, const ObjectPtr<Object>& _self) override { \
1159 using SelfPtr = std::remove_cv_t<decltype(this)>;
1160 #define TVM_MODULE_VTABLE_END() \
1161 return PackedFunc(nullptr); \
1163 #define TVM_MODULE_VTABLE_END_WITH_DEFAULT(MemFunc) \
1165 auto f = (MemFunc); \
1166 return (this->*f)(_name); \
1169 #define TVM_MODULE_VTABLE_ENTRY(Name, MemFunc) \
1170 if (_name == Name) { \
1171 return PackedFunc([_self](TVMArgs args, TVMRetValue* rv) -> void { \
1172 using Helper = ::tvm::runtime::detail::ModuleVTableEntryHelper<decltype(MemFunc)>; \
1173 SelfPtr self = static_cast<SelfPtr>(_self.get()); \
1174 CHECK_EQ(args.size(), Helper::LenArgs) \
1175 << "Function `" << self->type_key() << "::" << Name << "` requires " << Helper::LenArgs \
1176 << " arguments, but got " << args.size(); \
1177 Helper::Call(rv, self, MemFunc, args, Helper::IndexSeq{}); \
1180 #define TVM_MODULE_VTABLE_ENTRY_PACKED(Name, MemFunc) \
1181 if (_name == Name) { \
1182 return PackedFunc([_self](TVMArgs args, TVMRetValue* rv) -> void { \
1183 (static_cast<SelfPtr>(_self.get())->*(MemFunc))(args, rv); \
1222 #define TVM_DLL_EXPORT_TYPED_FUNC(ExportName, Function) \
1224 TVM_DLL int ExportName(TVMValue* args, int* type_code, int num_args, TVMValue* out_value, \
1225 int* out_type_code, void* resource_handle) { \
1227 auto f = Function; \
1228 using FType = ::tvm::runtime::detail::function_signature<decltype(f)>::FType; \
1229 ::tvm::runtime::TVMRetValue rv; \
1230 ::tvm::runtime::detail::unpack_call_by_signature<FType>::run( \
1231 f, ::tvm::runtime::TVMArgs(args, type_code, num_args), &rv); \
1232 rv.MoveToCHost(out_value, out_type_code); \
1234 } catch (const ::std::exception& _except_) { \
1235 TVMAPISetLastError(_except_.what()); \
1242 ICHECK_LT(i,
num_args) <<
"not enough argument passed, " <<
num_args <<
" passed"
1243 <<
" but request arg[" << i <<
"].";
1249 template <
class TPackedFuncSubObj>
1252 (
static_cast<const TPackedFuncSubObj*
>(obj))->callable_(args, rv);
1256 (*f_call_packed_)(
this, args, rv);
1265 switch (type_code) {
1281 return "ArrayHandle";
1283 return "DLDataType";
1287 return "FunctionHandle";
1289 return "ModuleHandle";
1291 return "NDArrayContainer";
1295 return "ObjectRValueRefArg";
1297 LOG(FATAL) <<
"unknown type_code=" <<
static_cast<int>(type_code);
1315 case kDLCUDAManaged:
1316 return "cuda_managed";
1346 LOG(FATAL) <<
"unknown type = " << type;
1353 template <
bool stop, std::
size_t I,
typename F>
1354 struct for_each_dispatcher {
1355 template <
typename T,
typename... Args>
1356 static void run(
const F& f, T&& value, Args&&... args) {
1357 f(I, std::forward<T>(value));
1358 for_each_dispatcher<
sizeof...(Args) == 0, (I + 1), F>::run(f, std::forward<Args>(args)...);
1362 template <std::
size_t I,
typename F>
1363 struct for_each_dispatcher<true, I, F> {
1364 static void run(
const F& f) {}
1367 template <
typename F,
typename... Args>
1368 inline void for_each(
const F& f, Args&&... args) {
1369 for_each_dispatcher<
sizeof...(Args) == 0, 0, F>::run(f, std::forward<Args>(args)...);
1372 template <
typename T>
1373 struct ModuleVTableEntryHelper {};
1375 template <
typename T,
typename R,
typename... Args>
1376 struct ModuleVTableEntryHelper<R (T::*)(Args...) const> {
1377 using MemFnType = R (T::*)(Args...)
const;
1378 using IndexSeq = std::index_sequence_for<Args...>;
1379 static constexpr
const std::size_t LenArgs =
sizeof...(Args);
1381 template <std::size_t... Is>
1382 static TVM_ALWAYS_INLINE
void Call(TVMRetValue* rv, T*
self, MemFnType f,
TVMArgs args,
1383 std::index_sequence<Is...>) {
1384 *rv = (
self->*f)(args[Is]...);
1388 template <
typename T,
typename R,
typename... Args>
1389 struct ModuleVTableEntryHelper<R (T::*)(Args...)> {
1390 using MemFnType = R (T::*)(Args...);
1391 using IndexSeq = std::index_sequence_for<Args...>;
1392 static constexpr
const std::size_t LenArgs =
sizeof...(Args);
1394 template <std::size_t... Is>
1395 static TVM_ALWAYS_INLINE
void Call(TVMRetValue* rv, T*
self, MemFnType f,
TVMArgs args,
1396 std::index_sequence<Is...>) {
1397 *rv = (
self->*f)(args[Is]...);
1401 template <
typename T,
typename... Args>
1402 struct ModuleVTableEntryHelper<void (T::*)(Args...) const> {
1403 using MemFnType = void (T::*)(Args...)
const;
1404 using IndexSeq = std::index_sequence_for<Args...>;
1405 static constexpr
const std::size_t LenArgs =
sizeof...(Args);
1407 template <std::size_t... Is>
1408 static TVM_ALWAYS_INLINE
void Call(TVMRetValue* rv, T*
self, MemFnType f,
TVMArgs args,
1409 std::index_sequence<Is...>) {
1410 (
self->*f)(args[Is]...);
1414 template <
typename T,
typename... Args>
1415 struct ModuleVTableEntryHelper<void (T::*)(Args...)> {
1416 using MemFnType = void (T::*)(Args...);
1417 using IndexSeq = std::index_sequence_for<Args...>;
1418 static constexpr
const std::size_t LenArgs =
sizeof...(Args);
1420 template <std::size_t... Is>
1421 static TVM_ALWAYS_INLINE
void Call(TVMRetValue* rv, T*
self, MemFnType f,
TVMArgs args,
1422 std::index_sequence<Is...>) {
1423 (
self->*f)(args[Is]...);
1427 namespace parameter_pack {
1429 template <
typename... EnumArgs>
1430 struct EnumeratedParamPack {
1431 struct InvokeWithoutArg {
1432 template <
template <
size_t i,
typename TArgument>
class Functor,
typename ExtraParams>
1433 static void F(ExtraParams&& extra_params) {
1434 using TExpander =
int[];
1437 (Functor<EnumArgs::i, typename EnumArgs::T>::F(std::forward<ExtraParams>(extra_params)),
1442 struct InvokeWithArg {
1443 template <
template <
size_t i,
typename TArgument>
class Functor,
typename ExtraParams,
1445 static void F(ExtraParams&& extra_params, Params&&... params) {
1446 using TExpander =
int[];
1449 (Functor<EnumArgs::i, typename EnumArgs::T>::F(std::forward<ExtraParams>(extra_params),
1450 std::forward<Params>(params)),
1457 template <
typename... Args>
1458 struct EnumerateImpl {
1460 template <
size_t _i,
typename _T>
1462 static const constexpr
size_t i = _i;
1466 template <
typename...>
1469 template <std::size_t...
id>
1470 struct Zipper<std::integer_sequence<std::size_t, id...>> {
1471 using WithoutArg =
typename EnumeratedParamPack<Item<id, Args>...>::InvokeWithoutArg;
1472 using WithArg =
typename EnumeratedParamPack<Item<id, Args>...>::InvokeWithArg;
1476 using WithoutArg =
typename Zipper<std::index_sequence_for<Args...>>::WithoutArg;
1477 using WithArg =
typename Zipper<std::index_sequence_for<Args...>>::WithArg;
1480 template <
typename... Args>
1481 using EnumerateWithoutArg =
typename EnumerateImpl<Args...>::WithoutArg;
1483 template <
typename... Args>
1484 using EnumerateWithArg =
typename EnumerateImpl<Args...>::WithArg;
1486 template <
typename... Args>
1488 template <
template <
size_t i,
typename TArgument>
class Functor,
typename ExtraParams>
1489 static void InvokeWithoutArg(ExtraParams&& extra_params) {
1490 EnumerateWithoutArg<Args...>::template F<Functor, ExtraParams>(
1491 std::forward<ExtraParams>(extra_params));
1501 template <
typename T>
1502 struct func_signature_helper {
1506 template <
typename T,
typename R,
typename... Args>
1507 struct func_signature_helper<R (T::*)(Args...)> {
1508 using FType = R(Args...);
1509 using ParamType = parameter_pack::ParamPack<Args...>;
1511 static_assert(!std::is_reference<R>::value,
"TypedPackedFunc return reference");
1514 template <
typename T,
typename R,
typename... Args>
1515 struct func_signature_helper<R (T::*)(Args...) const> {
1516 using FType = R(Args...);
1517 using ParamType = parameter_pack::ParamPack<Args...>;
1519 static_assert(!std::is_reference<R>::value,
"TypedPackedFunc return reference");
1526 template <
typename T>
1527 struct function_signature {
1528 using FType =
typename func_signature_helper<decltype(&T::operator())>::FType;
1529 using ParamType =
typename func_signature_helper<decltype(&T::operator())>::ParamType;
1530 using RetType =
typename func_signature_helper<decltype(&T::operator())>::RetType;
1534 template <
typename R,
typename... Args>
1535 struct function_signature<R(Args...)> {
1536 using FType = R(Args...);
1537 using ParamType = parameter_pack::ParamPack<Args...>;
1539 static_assert(!std::is_reference<R>::value,
"TypedPackedFunc return reference");
1543 template <
typename R,
typename... Args>
1544 struct function_signature<R (*)(Args...)> {
1545 using FType = R(Args...);
1546 using ParamType = detail::parameter_pack::ParamPack<Args...>;
1548 static_assert(!std::is_reference<R>::value,
"TypedPackedFunc return reference");
1551 template <
typename TSignature>
1552 struct SignaturePrinter;
1554 namespace type2str {
1556 template <
typename T>
1557 struct TypeSimplifier;
1559 template <
typename T>
1561 template <typename = std::enable_if_t<std::is_base_of<ObjectRef, T>::value>>
1562 static std::string v() {
1563 return T::ContainerType::_type_key;
1567 struct Type2Str<int> {
1568 static std::string v() {
return "int"; }
1571 struct Type2Str<double> {
1572 static std::string v() {
return "double"; }
1575 struct Type2Str<int64_t> {
1576 static std::string v() {
return "int64_t"; }
1579 struct Type2Str<uint64_t> {
1580 static std::string v() {
return "uint64_t"; }
1583 struct Type2Str<bool> {
1584 static std::string v() {
return "bool"; }
1587 struct Type2Str<void> {
1588 static std::string v() {
return "void"; }
1591 struct Type2Str<std::basic_string<char>> {
1592 static std::string v() {
return "basic_string<char>"; }
1594 template <
typename K,
typename V>
1595 struct Type2Str<Map<K, V>> {
1596 static std::string v() {
1597 return "Map<" + TypeSimplifier<K>::v() +
", " + TypeSimplifier<V>::v() +
">";
1601 struct Type2Str<DLDevice> {
1602 static std::string v() {
return "DLDevice"; }
1605 struct Type2Str<DLTensor> {
1606 static std::string v() {
return "DLTensor"; }
1610 static std::string v() {
return "DataType"; }
1613 struct Type2Str<DLDataType> {
1614 static std::string v() {
return "DLDataType"; }
1617 struct Type2Str<TVMRetValue> {
1618 static std::string v() {
return "TVMRetValue"; }
1621 struct Type2Str<TVMArgValue> {
1622 static std::string v() {
return "TVMArgValue"; }
1626 static std::string v() {
return "TVMByteArray"; }
1628 template <
typename FType>
1629 struct Type2Str<TypedPackedFunc<FType>> {
1630 static std::string v() {
return SignaturePrinter<function_signature<FType>>::F(); }
1632 template <
typename T>
1633 struct Type2Str<Array<T>> {
1634 static std::string v() {
return "Array<" + TypeSimplifier<T>::v() +
">"; }
1641 template <
typename T>
1642 struct TypeSimplifier {
1643 static std::string v() {
1644 using U =
typename std::remove_cv<
1645 typename std::remove_reference<typename std::remove_pointer<T>::type>::type>::type;
1646 return (std::is_const<T>::value ?
"const " :
"") + Type2Str<U>::v() +
1647 (std::is_pointer<T>::value ?
"*" :
"") + (std::is_reference<T>::value ?
"&" :
"");
1657 template <
typename TSignature>
1658 struct SignaturePrinter {
1659 using ParamType =
typename TSignature::ParamType;
1660 using RetType =
typename TSignature::RetType;
1662 template <
size_t i,
typename TArgument>
1663 struct PrintParamType {
1664 static void F(std::ostream& os) {
1665 os << (i == 0 ?
"" :
", ") << i <<
": " << type2str::TypeSimplifier<TArgument>::v();
1669 static std::string F() {
1670 std::ostringstream oss;
1672 ParamType::template InvokeWithoutArg<PrintParamType>(oss);
1673 oss <<
") -> " << type2str::TypeSimplifier<RetType>::v();
1684 template <typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type>
1686 values_[i].
v_int64 =
static_cast<int64_t
>(value);
1687 type_codes_[i] = kDLInt;
1689 TVM_ALWAYS_INLINE
void operator()(
size_t i, uint64_t value)
const {
1690 values_[i].
v_int64 =
static_cast<int64_t
>(value);
1692 type_codes_[i] = kDLInt;
1694 TVM_ALWAYS_INLINE
void operator()(
size_t i,
double value)
const {
1696 type_codes_[i] = kDLFloat;
1698 TVM_ALWAYS_INLINE
void operator()(
size_t i, std::nullptr_t value)
const {
1703 values_[i] = value.
value_;
1710 TVM_ALWAYS_INLINE
void operator()(
size_t i, DLTensor* value)
const {
1718 TVM_ALWAYS_INLINE
void operator()(
size_t i, DLDataType value)
const {
1719 values_[i].
v_type = value;
1725 TVM_ALWAYS_INLINE
void operator()(
size_t i,
const char* value)
const {
1726 values_[i].
v_str = value;
1730 TVM_ALWAYS_INLINE
void operator()(
size_t i,
const std::string& value)
const {
1731 values_[i].
v_str = value.c_str();
1738 template <
typename FType>
1744 values_[i].
v_str = value.
ptr<std::string>()->c_str();
1748 values_[i] = value.
value_;
1753 template <
typename TObjectRef,
1754 typename =
typename std::enable_if<std::is_base_of<ObjectRef, TObjectRef>::value>::type>
1755 TVM_ALWAYS_INLINE
void operator()(
size_t i,
const TObjectRef& value)
const {
1756 this->SetObject(i, value);
1759 template <
typename TObjectRef,
1760 typename =
typename std::enable_if<std::is_base_of<
1761 ObjectRef,
typename std::remove_reference<TObjectRef>::type>::value>::type>
1762 TVM_ALWAYS_INLINE
void operator()(
size_t i, TObjectRef&& value)
const {
1763 this->SetObject(i, std::forward<TObjectRef>(value));
1767 template <
typename TObjectRef>
1768 inline void SetObject(
size_t i, TObjectRef&& value)
const;
1775 template <
typename... Args>
1777 const int kNumArgs =
sizeof...(Args);
1778 const int kArraySize = kNumArgs > 0 ? kNumArgs : 1;
1780 int type_codes[kArraySize];
1781 detail::for_each(
TVMArgsSetter(values, type_codes), std::forward<Args>(args)...);
1784 ->CallPacked(
TVMArgs(values, type_codes, kNumArgs), &rv);
1788 template <
size_t i,
typename T>
1791 (*setter)(i, std::forward<T>(value));
1795 template <
typename... Args>
1798 detail::parameter_pack::EnumerateWithArg<Args...>::template F<TVMArgsSetterApply>(
1799 &setter, std::forward<Args>(args)...);
1803 template <
typename R,
int nleft,
int index,
typename F>
1804 struct unpack_call_dispatcher {
1805 template <
typename... Args>
1806 TVM_ALWAYS_INLINE
static void run(
const std::string* optional_name,
FSig* f_sig,
const F& f,
1808 Args&&... unpacked_args) {
1811 unpack_call_dispatcher<R, nleft - 1, index + 1, F>::run(
1812 optional_name, f_sig, f, args_pack, rv, std::forward<Args>(unpacked_args)...,
1813 TVMMovableArgValueWithContext_(args_pack.
values[index], args_pack.
type_codes[index], index,
1814 optional_name, f_sig));
1818 template <
typename R,
int index,
typename F>
1819 struct unpack_call_dispatcher<R, 0, index, F> {
1820 template <
typename... Args>
1821 TVM_ALWAYS_INLINE
static void run(
const std::string* optional_name,
FSig* f_sig,
const F& f,
1822 const TVMArgs& args_pack, TVMRetValue* rv,
1823 Args&&... unpacked_args) {
1824 using RetType = decltype(f(std::forward<Args>(unpacked_args)...));
1825 if (std::is_same<RetType, R>::value) {
1826 *rv = f(std::forward<Args>(unpacked_args)...);
1828 *rv = R(f(std::forward<Args>(unpacked_args)...));
1833 template <
int index,
typename F>
1834 struct unpack_call_dispatcher<void, 0, index, F> {
1835 template <
typename... Args>
1836 TVM_ALWAYS_INLINE
static void run(
const std::string* optional_name,
FSig* f_sig,
const F& f,
1837 const TVMArgs& args_pack, TVMRetValue* rv,
1838 Args&&... unpacked_args) {
1839 f(std::forward<Args>(unpacked_args)...);
1843 template <
typename R,
int nargs,
typename F>
1844 TVM_ALWAYS_INLINE
void unpack_call(
const std::string* optional_name,
const F& f,
1845 const TVMArgs& args, TVMRetValue* rv) {
1846 FSig* f_sig = detail::SignaturePrinter<detail::function_signature<F>>::F;
1847 CHECK_EQ(nargs, args.size()) <<
"Function "
1848 << (optional_name ==
nullptr ?
"<anonymous>" : *optional_name)
1849 << (f_sig ==
nullptr ?
"" : (*f_sig)()) <<
" expects " << nargs
1850 <<
" arguments but " << args.size() <<
" were provided";
1851 unpack_call_dispatcher<R, nargs, 0, F>::run(optional_name, f_sig, f, args, rv);
1854 template <
typename FType>
1855 struct unpack_call_by_signature {};
1857 template <
typename R,
typename... Args>
1858 struct unpack_call_by_signature<R(Args...)> {
1859 template <
typename F>
1860 TVM_ALWAYS_INLINE
static void run(
const F& f,
const TVMArgs& args, TVMRetValue* rv) {
1861 unpack_call<R,
sizeof...(Args)>(
nullptr, f, args, rv);
1865 template <
typename R,
typename... Args>
1866 TVM_ALWAYS_INLINE R call_packed(
const PackedFunc& pf, Args&&... args) {
1867 return R(pf(std::forward<Args>(args)...));
1870 template <
typename R>
1871 struct typed_packed_call_dispatcher {
1872 template <
typename... Args>
1873 TVM_ALWAYS_INLINE
static R run(
const PackedFunc& pf, Args&&... args) {
1874 return pf(std::forward<Args>(args)...);
1879 struct typed_packed_call_dispatcher<void> {
1880 template <
typename... Args>
1881 TVM_ALWAYS_INLINE
static void run(
const PackedFunc& pf, Args&&... args) {
1882 pf(std::forward<Args>(args)...);
1887 template <
typename R,
typename... Args>
1890 template <
typename R,
typename... Args>
1894 template <
typename R,
typename... Args>
1898 template <
typename R,
typename... Args>
1902 template <
typename R,
typename... Args>
1903 template <
typename FType>
1904 inline void TypedPackedFunc<R(Args...)>::AssignTypedLambda(FType flambda, std::string name) {
1905 FSig* f_sig = detail::SignaturePrinter<detail::function_signature<FType>>::F;
1907 if (args.
size() !=
sizeof...(Args)) {
1908 LOG(FATAL) <<
"Function " << name << (f_sig ==
nullptr ?
"" : (*f_sig)()) <<
" expects "
1909 <<
sizeof...(Args) <<
" arguments, but " << args.
size() <<
" were provided.";
1911 detail::unpack_call<R,
sizeof...(Args)>(&name, flambda, args, rv);
1915 template <
typename R,
typename... Args>
1916 template <
typename FType>
1918 FSig* f_sig = detail::SignaturePrinter<detail::function_signature<FType>>::F;
1920 if (args.
size() !=
sizeof...(Args)) {
1921 LOG(FATAL) <<
"Function <anonymous> " << (*f_sig)() <<
" expects " <<
sizeof...(Args)
1922 <<
" arguments, but " << args.
size() <<
" were provided.";
1924 detail::unpack_call<R,
sizeof...(Args)>(
nullptr, flambda, args, rv);
1928 template <
typename R,
typename... Args>
1930 return detail::typed_packed_call_dispatcher<R>::run(packed_, std::forward<Args>(args)...);
1933 template <
typename T>
1937 return arg.operator T();
1938 }
catch (
const dmlc::Error& e) {
1939 LOG(FATAL) <<
"Argument " << i <<
" cannot be converted to type \""
1940 << tvm::runtime::detail::type2str::Type2Str<T>::v() <<
"\". Its type is \""
1951 template <
typename T>
1952 inline void TVMArgsSetter::SetObject(
size_t i, T&& value)
const {
1953 using ContainerType =
typename std::remove_reference<T>::type::ContainerType;
1954 if (value.defined()) {
1955 Object* ptr = value.data_.data_;
1956 if (std::is_base_of<NDArray::ContainerType, ContainerType>::value ||
1957 (std::is_base_of<ContainerType, NDArray::ContainerType>::value &&
1961 }
else if (std::is_base_of<Module::ContainerType, ContainerType>::value ||
1962 (std::is_base_of<ContainerType, Module::ContainerType>::value &&
1966 }
else if (std::is_base_of<PackedFunc::ContainerType, ContainerType>::value ||
1967 (std::is_base_of<ContainerType, PackedFunc::ContainerType>::value &&
1971 }
else if (std::is_rvalue_reference<decltype(value)>::value) {
1972 values_[i].
v_handle =
const_cast<Object**
>(&(value.data_.data_));
1975 values_[i].
v_handle = value.data_.data_;
1984 template <
typename TObjectRef,
typename>
1986 using ContainerType =
typename TObjectRef::ContainerType;
1988 if (std::is_base_of<NDArray::ContainerType, ContainerType>::value) {
1993 if (std::is_base_of<Module::ContainerType, ContainerType>::value) {
1997 if (std::is_base_of<PackedFunc::ContainerType, ContainerType>::value) {
2005 return (std::is_base_of<ContainerType, NDArray::ContainerType>::value &&
2007 (std::is_base_of<ContainerType, Module::ContainerType>::value &&
2009 (std::is_base_of<ContainerType, PackedFunc::ContainerType>::value &&
2015 template <
typename TObjectRef>
2017 static_assert(std::is_base_of<ObjectRef, TObjectRef>::value,
2018 "Conversion only works for ObjectRef");
2019 using ContainerType =
typename TObjectRef::ContainerType;
2022 CHECK(TObjectRef::_type_is_nullable)
2023 <<
"Expect a not null value of " << ContainerType::_type_key;
2027 if (std::is_base_of<NDArray::ContainerType, ContainerType>::value) {
2032 CHECK(data->IsInstance<ContainerType>())
2033 <<
"Expected " << ContainerType::_type_key <<
" but got " << data->GetTypeKey();
2034 return TObjectRef(data);
2036 if (std::is_base_of<Module::ContainerType, ContainerType>::value) {
2040 CHECK(data->IsInstance<ContainerType>())
2041 <<
"Expected " << ContainerType::_type_key <<
" but got " << data->GetTypeKey();
2042 return TObjectRef(data);
2044 if (std::is_base_of<PackedFunc::ContainerType, ContainerType>::value) {
2048 CHECK(data->IsInstance<ContainerType>())
2049 <<
"Expected " << ContainerType::_type_key <<
" but got " << data->GetTypeKey();
2050 return TObjectRef(data);
2057 <<
", but got " << checked_type.
value();
2058 return TObjectRef(GetObjectPtr<Object>(
ptr));
2063 <<
", but got " << checked_type.
value();
2064 return TObjectRef(GetObjectPtr<Object>(
ptr));
2065 }
else if (std::is_base_of<ContainerType, NDArray::ContainerType>::value &&
2070 return TObjectRef(data);
2071 }
else if (std::is_base_of<ContainerType, Module::ContainerType>::value &&
2075 }
else if (std::is_base_of<ContainerType, PackedFunc::ContainerType>::value &&
2085 template <
typename TObjectRef,
typename>
2087 using ContainerType =
typename TObjectRef::ContainerType;
2089 if (
ptr !=
nullptr) {
2090 if (std::is_base_of<NDArray::ContainerType, ContainerType>::value ||
2091 (std::is_base_of<ContainerType, NDArray::ContainerType>::value &&
2095 if (std::is_base_of<Module::ContainerType, ContainerType>::value ||
2096 (std::is_base_of<ContainerType, Module::ContainerType>::value &&
2100 if (std::is_base_of<PackedFunc::ContainerType, ContainerType>::value ||
2101 (std::is_base_of<ContainerType, PackedFunc::ContainerType>::value &&
2113 template <
typename T,
typename>
2114 inline TVMArgValue::operator T()
const {
2118 template <
typename T,
typename>
2119 inline TVMMovableArgValue_::operator T()
const {
2121 auto** ref =
static_cast<Object**
>(value_.v_handle);
2130 template <
typename T,
typename>
2131 inline TVMRetValue::operator T()
const {
2136 return (*this)->GetFunction(name, query_imports);
2159 template <
typename T>
2171 template <
typename... VariantTypes>
2178 template <
typename PODSub
class>
2180 if (
auto opt = TryAsObjectRef<VariantTypes...>(val)) {
2184 if (
auto opt = TryValueConverter<PODSubclass, VariantTypes...>(val)) {
2188 LOG(FATAL) <<
"Expected one of "
2189 <<
static_cast<const std::stringstream&
>(
2190 (std::stringstream() << ... << VariantTypes::ContainerType::_type_key))
2195 template <
typename VarFirst,
typename... VarRest>
2199 }
else if constexpr (
sizeof...(VarRest)) {
2200 return TryAsObjectRef<VarRest...>(val);
2206 template <
typename PODSubclass,
typename VarFirst,
typename... VarRest>
2210 }
catch (
const InternalError&) {
2213 if constexpr (
sizeof...(VarRest)) {
2214 return TryValueConverter<PODSubclass, VarRest...>(val);
2225 inline TVMArgValue::operator DLDataType()
const {
2238 return value_.v_type;
Runtime Array container types.
@ kTVMPackedFuncHandle
Definition: c_runtime_api.h:184
@ kTVMNDArrayHandle
Definition: c_runtime_api.h:187
@ kTVMModuleHandle
Definition: c_runtime_api.h:183
@ kTVMBytes
Definition: c_runtime_api.h:186
@ kTVMDataType
Definition: c_runtime_api.h:179
@ kTVMDLTensorHandle
Definition: c_runtime_api.h:181
@ kDLDevice
Definition: c_runtime_api.h:180
@ kTVMOpaqueHandle
Definition: c_runtime_api.h:177
@ kTVMObjectHandle
Definition: c_runtime_api.h:182
@ kTVMObjectRValueRefArg
Definition: c_runtime_api.h:188
@ kTVMNullptr
Definition: c_runtime_api.h:178
@ kTVMStr
Definition: c_runtime_api.h:185
@ kDLMicroDev
Definition: c_runtime_api.h:124
@ kOpenGL
Definition: c_runtime_api.h:123
@ kDLSDAccel
Definition: c_runtime_api.h:122
@ kDLAOCL
Definition: c_runtime_api.h:121
DLTensor * TVMArrayHandle
the array handle
Definition: c_runtime_api.h:202
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
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
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:2135
Object container class that backs NDArray.
Definition: ndarray.h:288
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:416
static ObjectPtr< Object > FFIDataFromHandle(TVMArrayHandle handle)
Construct NDArray's Data field from array handle in FFI.
Definition: ndarray.h:411
static void FFIDecRef(TVMArrayHandle handle)
DecRef resource managed by an FFI array handle.
Definition: ndarray.h:424
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:870
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:69
PackedFuncObj(FCallPacked *f_call_pack)
Constructing a packed function object from a function pointer.
Definition: packed_func.h:104
static constexpr const uint32_t _type_index
Definition: packed_func.h:78
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:110
static constexpr const char * _type_key
Definition: packed_func.h:79
PackedFuncObj()=delete
Delete the default constructor explicitly.
void(const PackedFuncObj *, TVMArgs, TVMRetValue *) FCallPacked
The internal callable function type.
Definition: packed_func.h:98
TVM_ALWAYS_INLINE void CallPacked(TVMArgs args, TVMRetValue *rv) const
Call the function in packed format.
Definition: packed_func.h:1255
Derived object class for constructing PackedFuncObj.
Definition: packed_func.h:115
PackedFuncSubObj(TCallable callable)
Derived object class for constructing PackedFuncObj.
Definition: packed_func.h:125
TStorage callable_
Type-erased filed for storing callable object.
Definition: packed_func.h:128
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:139
bool operator!=(std::nullptr_t null) const
Definition: packed_func.h:181
bool operator==(std::nullptr_t null) const
Definition: packed_func.h:179
TVMRetValue operator()(Args &&... args) const
Call packed function by directly passing in unpacked format.
Definition: packed_func.h:1776
TVM_ALWAYS_INLINE void CallPacked(TVMArgs args, TVMRetValue *rv) const
Call the function in packed format.
Definition: packed_func.h:1259
PackedFunc(TCallable data)
Constructing a packed function from a callable type whose signature is consistent with PackedFunc
Definition: packed_func.h:152
TVM_DEFINE_OBJECT_REF_METHODS(PackedFunc, ObjectRef, PackedFuncObj)
PackedFunc(std::nullptr_t null)
Constructor from null.
Definition: packed_func.h:142
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:2221
A single argument value to PackedFunc. Containing both type_code and TVMValue.
Definition: packed_func.h:657
TObjectRef AsObjectRef() const
Definition: packed_func.h:2016
const TVMValue & value() const
Definition: packed_func.h:699
TVMArgValue(TVMValue value, int type_code)
constructor
Definition: packed_func.h:666
bool IsObjectRef() const
Definition: packed_func.h:1985
TVMArgValue()
default constructor
Definition: packed_func.h:660
Definition: packed_func.h:1680
TVM_ALWAYS_INLINE void operator()(size_t i, DLTensor *value) const
Definition: packed_func.h:1710
TVM_ALWAYS_INLINE void operator()(size_t i, uint64_t value) const
Definition: packed_func.h:1689
TVM_ALWAYS_INLINE void operator()(size_t i, const TypedPackedFunc< FType > &value) const
Definition: packed_func.h:1739
TVM_ALWAYS_INLINE void operator()(size_t i, const TObjectRef &value) const
Definition: packed_func.h:1755
void operator()(size_t i, const TVMRetValue &value) const
Definition: packed_func.h:1742
TVM_ALWAYS_INLINE void operator()(size_t i, TObjectRef &&value) const
Definition: packed_func.h:1762
TVM_ALWAYS_INLINE void operator()(size_t i, double value) const
Definition: packed_func.h:1694
TVM_ALWAYS_INLINE void operator()(size_t i, T value) const
Definition: packed_func.h:1685
TVMArgsSetter(TVMValue *values, int *type_codes)
Definition: packed_func.h:1682
TVM_ALWAYS_INLINE void operator()(size_t i, const char *value) const
Definition: packed_func.h:1725
TVM_ALWAYS_INLINE void operator()(size_t i, Device value) const
Definition: packed_func.h:1714
TVM_ALWAYS_INLINE void operator()(size_t i, void *value) const
Definition: packed_func.h:1706
TVM_ALWAYS_INLINE void operator()(size_t i, const std::string &value) const
Definition: packed_func.h:1730
TVM_ALWAYS_INLINE void operator()(size_t i, std::nullptr_t value) const
Definition: packed_func.h:1698
TVM_ALWAYS_INLINE void operator()(size_t i, const TVMByteArray &value) const
Definition: packed_func.h:1734
TVM_ALWAYS_INLINE void operator()(size_t i, DataType dtype) const
Definition: packed_func.h:1722
TVM_ALWAYS_INLINE void operator()(size_t i, DLDataType value) const
Definition: packed_func.h:1718
TVM_ALWAYS_INLINE void operator()(size_t i, const TVMArgValue &value) const
Definition: packed_func.h:1702
Arguments into TVM functions.
Definition: packed_func.h:392
const TVMValue * values
Definition: packed_func.h:394
TVMArgs(const TVMValue *values, const int *type_codes, int num_args)
constructor
Definition: packed_func.h:403
TVMArgValue operator[](int i) const
Get i-th argument.
Definition: packed_func.h:1241
T At(int i) const
Get the i-th argument and do proper type checking with detailed error messages.
Definition: packed_func.h:1934
const int * type_codes
Definition: packed_func.h:395
int size() const
Definition: packed_func.h:1247
int num_args
Definition: packed_func.h:396
Internal auxiliary struct for TypedPackedFunc to indicate a movable argument with additional context ...
Definition: packed_func.h:762
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:773
Internal auxiliary struct for TypedPackedFunc to indicate a movable argument.
Definition: packed_func.h:717
TVMMovableArgValue_(TVMValue value, int type_code)
Definition: packed_func.h:719
Internal base class to handle conversion to POD values.
Definition: packed_func.h:552
TObjectRef AsObjectRef() const
Definition: packed_func.h:2016
TVMPODValue_()
Definition: packed_func.h:642
bool IsObjectRef() const
Definition: packed_func.h:1985
TVMValue value_
The value.
Definition: packed_func.h:646
T * ptr() const
return handle as specific pointer type.
Definition: packed_func.h:628
int type_code_
the type code
Definition: packed_func.h:648
int type_code() const
Definition: packed_func.h:621
TVMPODValue_(TVMValue value, int type_code)
Definition: packed_func.h:643
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:807
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:975
const TVMValue & value() const
Definition: packed_func.h:984
~TVMRetValue()
destructor
Definition: packed_func.h:820
TObjectRef AsObjectRef() const
Definition: packed_func.h:2016
TVMRetValue & operator=(DLDataType t)
Definition: packed_func.h:897
TVMRetValue & operator=(const TypedPackedFunc< FType > &f)
Definition: packed_func.h:937
TVMRetValue & operator=(std::nullptr_t value)
Definition: packed_func.h:872
TVMRetValue & operator=(TVMRetValue &&other)
Definition: packed_func.h:860
TVMRetValue & operator=(PackedFunc f)
Definition: packed_func.h:932
TVMRetValue & operator=(bool value)
Definition: packed_func.h:903
TVMRetValue & operator=(int value)
Definition: packed_func.h:887
bool IsObjectRef() const
Definition: packed_func.h:1985
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:961
TVMRetValue()
default constructor
Definition: packed_func.h:810
TVMRetValue & operator=(void *value)
Definition: packed_func.h:877
TVMRetValue & operator=(std::string value)
Definition: packed_func.h:908
TVMRetValue & operator=(const TVMArgValue &other)
Definition: packed_func.h:944
TVMRetValue(const TVMRetValue &other)
Definition: packed_func.h:836
TVMRetValue & operator=(double value)
Definition: packed_func.h:867
TVMRetValue(TVMRetValue &&other)
move constructor from another return value.
Definition: packed_func.h:815
TVMRetValue & operator=(const TVMRetValue &other)
Definition: packed_func.h:940
TVMRetValue & operator=(int64_t value)
Definition: packed_func.h:882
TVMRetValue & operator=(Module m)
Definition: packed_func.h:928
TVMRetValue & operator=(const DataType &other)
Definition: packed_func.h:902
TVMRetValue & operator=(DLDevice value)
Definition: packed_func.h:892
TVMRetValue & operator=(TVMMovableArgValue_ &&other)
Definition: packed_func.h:948
TVMRetValue & operator=(TVMByteArray value)
Definition: packed_func.h:912
TVMRetValue & operator=(NDArray other)
Definition: packed_func.h:916
A PackedFunc wrapper to provide typed function signature. It is backed by a PackedFunc internally.
Definition: packed_func.h:228
const PackedFunc & packed() const
Definition: packed_func.h:359
bool operator==(std::nullptr_t null) const
Definition: packed_func.h:361
TypedPackedFunc(const FLambda &typed_lambda)
construct from a lambda function with the same signature.
Definition: packed_func.h:310
TypedPackedFunc()
default constructor
Definition: packed_func.h:233
TypedPackedFunc(const FLambda &typed_lambda, std::string name)
construct from a lambda function with the same signature.
Definition: packed_func.h:287
TSelf & operator=(FLambda typed_lambda)
copy assignment operator from typed lambda
Definition: packed_func.h:332
TSelf & operator=(PackedFunc packed)
copy assignment operator from PackedFunc.
Definition: packed_func.h:341
bool operator!=(std::nullptr_t null) const
Definition: packed_func.h:363
TypedPackedFunc(std::nullptr_t null)
constructor from null
Definition: packed_func.h:235
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:61
Runtime Map container types.
const char * ArgTypeCode2Str(int type_code)
Convert argument type code to string.
Definition: packed_func.h:1264
const char * DLDeviceType2Str(int type)
The name of DLDeviceType.
Definition: packed_func.h:1307
DLDataType String2DLDataType(std::string s)
convert a string to TVM type.
Definition: data_type.h:427
std::string() FSig
Using static function to output TypedPackedFunc signature.
Definition: packed_func.h:187
void TVM_ALWAYS_INLINE PackArgs(TVMValue *values, int *type_codes, Args &&... args)
Definition: packed_func.h:1796
std::string DLDataType2String(DLDataType t)
convert a TVM type to string.
Definition: data_type.h:420
std::ostream & operator<<(std::ostream &os, const ObjectRef &n)
Definition: repr_printer.h:97
Object * TVMArrayHandleToObjectHandle(TVMArrayHandle handle)
Definition: ndarray.h:428
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:491
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_CHECK_TYPE_CODE(CODE, T)
Definition: packed_func.h:433
Runtime container of the functions generated by TVM, This is used to support dynamically link,...
Definition: packed_func.h:38
Byte array type used to pass in byte array When kTVMBytes is used as data type.
Definition: c_runtime_api.h:221
size_t size
Definition: c_runtime_api.h:223
const char * data
Definition: c_runtime_api.h:222
static std::string TypeName()
Definition: packed_func.h:511
static bool Check(const Object *ptr)
Definition: packed_func.h:500
static Optional< String > CheckAndGetMismatch(const Object *ptr)
Definition: packed_func.h:483
static bool Check(const Object *ptr)
Definition: packed_func.h:532
static Optional< String > CheckAndGetMismatch(const Object *ptr)
Definition: packed_func.h:515
static std::string TypeName()
Definition: packed_func.h:542
Type traits for runtime type check during FFI conversion.
Definition: packed_func.h:441
static std::string TypeName()
Definition: packed_func.h:474
static bool Check(const Object *ptr)
Check if an object matches the template type.
Definition: packed_func.h:469
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:449
static Optional< T > From(const TVMRetValue &val)
Definition: packed_func.h:2165
static Optional< T > From(const TVMArgValue &val)
Definition: packed_func.h:2161
static VType From(const PODSubclass &val)
Definition: packed_func.h:2179
static Optional< VType > TryValueConverter(const PODSubclass &val)
Definition: packed_func.h:2207
static Optional< VType > TryAsObjectRef(const TVMPODValue_ &val)
Definition: packed_func.h:2196
static String From(const TVMArgValue &val)
Definition: packed_func.h:2142
static String From(const TVMRetValue &val)
Definition: packed_func.h:2150
Type trait to specify special value conversion rules from TVMArgValue and TVMRetValue.
Definition: packed_func.h:1104
static TObjectRef From(const TVMRetValue &val)
Convert a TObjectRef from a return value.
Definition: packed_func.h:1116
static TObjectRef From(const TVMArgValue &val)
Convert a TObjectRef from an argument value.
Definition: packed_func.h:1110
Definition: packed_func.h:63
Definition: packed_func.h:1789
static TVM_ALWAYS_INLINE void F(TVMArgsSetter *setter, T &&value)
Definition: packed_func.h:1790
@ kRuntimePackedFunc
runtime::PackedFunc.
Definition: object.h:74
Union type of values being passed through API and function calls.
Definition: c_runtime_api.h:208
DLDevice v_device
Definition: c_runtime_api.h:214
void * v_handle
Definition: c_runtime_api.h:211
DLDataType v_type
Definition: c_runtime_api.h:213
int64_t v_int64
Definition: c_runtime_api.h:209
const char * v_str
Definition: c_runtime_api.h:212
double v_float64
Definition: c_runtime_api.h:210
Runtime Variant container types.