24 #ifndef TVM_RUNTIME_PACKED_FUNC_H_ 25 #define TVM_RUNTIME_PACKED_FUNC_H_ 31 #include <tvm/runtime/logging.h> 41 #include <type_traits> 46 #ifndef TVM_RUNTIME_HEADER_ONLY 47 #define TVM_RUNTIME_HEADER_ONLY 0 56 class TVMMovableArgValueWithContext_;
88 using FType = std::function<void(TVMArgs args, TVMRetValue* rv)>;
112 template <
typename... Args>
123 bool operator==(std::nullptr_t null)
const {
return body_ ==
nullptr; }
125 bool operator!=(std::nullptr_t null)
const {
return body_ !=
nullptr; }
135 template <
typename FType>
170 template <
typename R,
typename... Args>
228 template <
typename FLambda,
typename =
typename std::enable_if<std::is_convertible<
229 FLambda, std::function<R(Args...)>>::value>::type>
231 this->AssignTypedLambda(typed_lambda, name);
251 template <
typename FLambda,
typename =
typename std::enable_if<std::is_convertible<
252 FLambda, std::function<R(Args...)>>::value>::type>
254 this->AssignTypedLambda(typed_lambda);
272 template <
typename FLambda,
typename =
typename std::enable_if<
273 std::is_convertible<FLambda,
274 std::function<R(Args...)>>::value>::type>
276 this->AssignTypedLambda(typed_lambda);
293 TVM_ALWAYS_INLINE R
operator()(Args... args)
const;
304 bool operator==(std::nullptr_t null)
const {
return packed_ ==
nullptr; }
306 bool operator!=(std::nullptr_t null)
const {
return packed_ !=
nullptr; }
320 template <
typename FLambda>
321 inline void AssignTypedLambda(FLambda flambda, std::string name);
330 template <
typename FLambda>
331 inline void AssignTypedLambda(FLambda flambda);
347 : values(values), type_codes(type_codes), num_args(num_args) {}
349 inline int size()
const;
366 #define TVM_CHECK_TYPE_CODE(CODE, T) \ 367 ICHECK_EQ(CODE, T) << "expected " << ArgTypeCode2Str(T) << " but got " << ArgTypeCode2Str(CODE) 373 template <
typename T>
383 using ContainerType =
typename T::ContainerType;
384 if (ptr ==
nullptr) {
385 if (T::_type_is_nullable) {
403 using ContainerType =
typename T::ContainerType;
404 if (ptr ==
nullptr)
return T::_type_is_nullable;
408 using ContainerType =
typename T::ContainerType;
409 return ContainerType::_type_key;
414 template <
typename T>
417 if (ptr ==
nullptr) {
424 for (
size_t i = 0; i < n->size(); i++) {
428 return String(
"Array[index " + std::to_string(i) +
": " + check_subtype.
value() +
"]");
434 if (ptr ==
nullptr)
return true;
446 template <
typename K,
typename V>
449 if (ptr ==
nullptr)
return NullOpt;
452 for (
const auto& kv : *n) {
455 if (key_type.
defined() || value_type.defined()) {
456 std::string key_name =
458 std::string value_name = value_type.defined() ? std::string(value_type.value())
460 return String(
"Map[" + key_name +
", " + value_name +
"]");
466 if (ptr ==
nullptr)
return true;
469 for (
const auto& kv : *n) {
487 operator double()
const {
491 if (type_code_ == kDLInt) {
492 return static_cast<double>(value_.v_int64);
495 return value_.v_float64;
497 operator int64_t()
const {
499 return value_.v_int64;
501 operator uint64_t()
const {
503 return value_.v_int64;
505 operator int()
const {
509 return static_cast<int>(value_.v_int64);
511 operator bool()
const {
513 return value_.v_int64 != 0;
515 operator void*()
const {
519 return value_.v_handle;
521 operator DLTensor*()
const {
523 return static_cast<DLTensor*
>(value_.v_handle);
526 LOG(FATAL) <<
"Expected " 545 return value_.v_device;
553 template <
typename T>
555 return static_cast<T*
>(value_.v_handle);
558 template <
typename TObjectRef,
559 typename =
typename std::enable_if<std::is_base_of<ObjectRef, TObjectRef>::value>::type>
560 inline bool IsObjectRef()
const;
561 template <
typename TObjectRef>
562 inline TObjectRef AsObjectRef()
const;
594 using TVMPODValue_::operator double;
595 using TVMPODValue_::operator int64_t;
596 using TVMPODValue_::operator uint64_t;
597 using TVMPODValue_::operator int;
598 using TVMPODValue_::operator bool;
599 using TVMPODValue_::operator
void*;
600 using TVMPODValue_::operator DLTensor*;
601 using TVMPODValue_::operator
NDArray;
602 using TVMPODValue_::operator
Device;
603 using TVMPODValue_::operator
Module;
608 operator std::string()
const {
613 return std::string(arr->
data, arr->
size);
614 }
else if (type_code_ ==
kTVMStr) {
615 return std::string(value_.v_str);
617 ICHECK(IsObjectRef<tvm::runtime::String>())
620 return AsObjectRef<tvm::runtime::String>().
operator std::string();
626 return *ptr<PackedFunc>();
628 template <
typename FType>
634 template <typename T, typename = typename std::enable_if<std::is_class<T>::value>::type>
635 inline operator T()
const;
636 inline operator DLDataType()
const;
654 using TVMPODValue_::operator double;
655 using TVMPODValue_::operator int64_t;
656 using TVMPODValue_::operator uint64_t;
657 using TVMPODValue_::operator int;
658 using TVMPODValue_::operator bool;
659 using TVMPODValue_::operator
void*;
660 using TVMPODValue_::operator DLTensor*;
661 using TVMPODValue_::operator
NDArray;
662 using TVMPODValue_::operator
Device;
663 using TVMPODValue_::operator
Module;
665 operator std::string()
const {
return AsArgValue().operator std::string(); }
667 template <
typename FType>
671 operator DLDataType()
const {
return AsArgValue().operator DLDataType(); }
679 template <
typename T,
680 typename =
typename std::enable_if<std::is_base_of<ObjectRef, T>::value>::type>
681 inline operator T()
const;
706 const std::string* optional_name)
707 : value_(value, type_code), arg_index_(arg_index), optional_name_(optional_name) {}
709 template <
typename T>
713 }
catch (dmlc::Error& e) {
714 LOG(FATAL) <<
"In function " << (optional_name_ ==
nullptr ?
"<anonymous>" : *optional_name_)
715 <<
": error while converting argument " << arg_index_ <<
": " << e.what();
723 const std::string* optional_name_;
749 using TVMPODValue_::operator double;
750 using TVMPODValue_::operator int64_t;
751 using TVMPODValue_::operator uint64_t;
752 using TVMPODValue_::operator int;
753 using TVMPODValue_::operator bool;
754 using TVMPODValue_::operator
void*;
755 using TVMPODValue_::operator DLTensor*;
756 using TVMPODValue_::operator
Device;
757 using TVMPODValue_::operator
NDArray;
758 using TVMPODValue_::operator
Module;
764 operator std::string()
const {
768 return *ptr<std::string>();
771 return *ptr<std::string>();
773 operator DLDataType()
const {
778 return value_.v_type;
784 return *ptr<PackedFunc>();
786 template <
typename FType>
793 value_ = other.value_;
794 type_code_ = other.type_code_;
799 this->SwitchToPOD(kDLFloat);
800 value_.v_float64 = value;
805 value_.v_handle = value;
810 value_.v_handle = value;
814 this->SwitchToPOD(kDLInt);
815 value_.v_int64 = value;
819 this->SwitchToPOD(kDLInt);
820 value_.v_int64 = value;
825 value_.v_device = value;
835 this->SwitchToPOD(kDLInt);
836 value_.v_int64 = value;
840 this->SwitchToClass(
kTVMStr, value);
848 if (other.
data_ !=
nullptr) {
870 template <
typename FType>
872 return operator=(f.packed());
899 *ret_type_code = type_code_;
921 <<
"TVMRetValue.value can only be used for POD data";
925 template <
typename TObjectRef,
926 typename =
typename std::enable_if<std::is_base_of<ObjectRef, TObjectRef>::value>::type>
928 template <typename T, typename = typename std::enable_if<std::is_class<T>::value>::type>
929 inline operator T()
const;
932 template <
typename T>
933 void Assign(
const T& other) {
934 switch (other.type_code()) {
936 SwitchToClass<std::string>(
kTVMStr, other);
940 SwitchToClass<std::string>(
kTVMBytes, other);
948 *
this = other.operator Module();
952 *
this = other.operator NDArray();
958 GetObjectPtr<Object>(static_cast<Object*>(other.value_.v_handle)));
966 SwitchToPOD(other.type_code());
967 value_ = other.value_;
973 void SwitchToPOD(
int type_code) {
974 if (type_code_ != type_code) {
976 type_code_ = type_code;
979 template <
typename T>
980 void SwitchToClass(
int type_code, T v) {
981 if (type_code_ != type_code) {
983 type_code_ = type_code;
984 value_.v_handle =
new T(v);
986 *
static_cast<T*
>(value_.v_handle) = v;
990 if (other.data_ !=
nullptr) {
992 type_code_ = type_code;
994 value_.v_handle = other.data_;
995 other.data_ =
nullptr;
1002 switch (type_code_) {
1005 delete ptr<std::string>();
1008 delete ptr<PackedFunc>();
1015 static_cast<Object*
>(value_.v_handle)->DecRef();
1019 static_cast<Object*
>(value_.v_handle)->DecRef();
1036 template <
typename TObjectRef>
1071 #define TVM_DLL_EXPORT_PACKED_FUNC(ExportName, Function) \ 1073 TVM_DLL int ExportName(TVMValue* args, int* type_code, int num_args, TVMValue* out_value, \ 1074 int* out_type_code, void* resource_handle); \ 1075 int ExportName(TVMValue* args, int* type_code, int num_args, TVMValue* out_value, \ 1076 int* out_type_code, void* resource_handle) { \ 1078 ::tvm::runtime::TVMRetValue rv; \ 1079 Function(::tvm::runtime::TVMArgs(args, type_code, num_args), &rv); \ 1080 rv.MoveToCHost(out_value, out_type_code); \ 1082 } catch (const ::std::exception& _except_) { \ 1083 TVMAPISetLastError(_except_.what()); \ 1124 #define TVM_DLL_EXPORT_TYPED_FUNC(ExportName, Function) \ 1126 TVM_DLL int ExportName(TVMValue* args, int* type_code, int num_args, TVMValue* out_value, \ 1127 int* out_type_code, void* resource_handle) { \ 1129 auto f = Function; \ 1130 using FType = ::tvm::runtime::detail::function_signature<decltype(f)>::FType; \ 1131 ::tvm::runtime::TVMRetValue rv; \ 1132 ::tvm::runtime::detail::unpack_call_by_signature<FType>::run( \ 1133 f, ::tvm::runtime::TVMArgs(args, type_code, num_args), &rv); \ 1134 rv.MoveToCHost(out_value, out_type_code); \ 1136 } catch (const ::std::exception& _except_) { \ 1137 TVMAPISetLastError(_except_.what()); \ 1144 ICHECK_LT(i, num_args) <<
"not enough argument passed, " << num_args <<
" passed" 1145 <<
" but request arg[" << i <<
"].";
1157 switch (type_code) {
1173 return "ArrayHandle";
1175 return "DLDataType";
1179 return "FunctionHandle";
1181 return "ModuleHandle";
1183 return "NDArrayContainer";
1187 return "ObjectRValueRefArg";
1189 LOG(FATAL) <<
"unknown type_code=" <<
static_cast<int>(type_code);
1196 template <
bool stop, std::
size_t I,
typename F>
1197 struct for_each_dispatcher {
1198 template <
typename T,
typename... Args>
1199 static void run(
const F& f, T&& value, Args&&... args) {
1200 f(I, std::forward<T>(value));
1201 for_each_dispatcher<
sizeof...(Args) == 0, (I + 1), F>::run(f, std::forward<Args>(args)...);
1205 template <std::
size_t I,
typename F>
1206 struct for_each_dispatcher<true, I, F> {
1207 static void run(
const F& f) {}
1210 template <
typename F,
typename... Args>
1211 inline void for_each(
const F& f, Args&&... args) {
1212 for_each_dispatcher<
sizeof...(Args) == 0, 0, F>::run(f, std::forward<Args>(args)...);
1215 template <
typename T>
1216 struct func_signature_helper {
1220 template <
typename T,
typename R,
typename... Args>
1221 struct func_signature_helper<R (T::*)(Args...)> {
1222 using FType = R(Args...);
1223 static_assert(!std::is_reference<R>::value,
"TypedPackedFunc return reference");
1226 template <
typename T,
typename R,
typename... Args>
1227 struct func_signature_helper<R (T::*)(Args...) const> {
1228 using FType = R(Args...);
1229 static_assert(!std::is_reference<R>::value,
"TypedPackedFunc return reference");
1236 template <
typename T>
1237 struct function_signature {
1238 using FType =
typename func_signature_helper<decltype(&T::operator())>::FType;
1242 template <
typename R,
typename... Args>
1243 struct function_signature<R(Args...)> {
1244 using FType = R(Args...);
1245 static_assert(!std::is_reference<R>::value,
"TypedPackedFunc return reference");
1249 template <
typename R,
typename... Args>
1250 struct function_signature<R (*)(Args...)> {
1251 using FType = R(Args...);
1252 static_assert(!std::is_reference<R>::value,
"TypedPackedFunc return reference");
1261 template <typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type>
1263 values_[i].v_int64 =
static_cast<int64_t
>(value);
1264 type_codes_[i] = kDLInt;
1266 TVM_ALWAYS_INLINE
void operator()(
size_t i, uint64_t value)
const {
1267 values_[i].v_int64 =
static_cast<int64_t
>(value);
1269 type_codes_[i] = kDLInt;
1271 TVM_ALWAYS_INLINE
void operator()(
size_t i,
double value)
const {
1272 values_[i].v_float64 = value;
1273 type_codes_[i] = kDLFloat;
1275 TVM_ALWAYS_INLINE
void operator()(
size_t i, std::nullptr_t value)
const {
1276 values_[i].v_handle = value;
1280 values_[i] = value.
value_;
1284 values_[i].v_handle = value;
1287 TVM_ALWAYS_INLINE
void operator()(
size_t i, DLTensor* value)
const {
1288 values_[i].v_handle = value;
1292 values_[i].v_device = value;
1295 TVM_ALWAYS_INLINE
void operator()(
size_t i, DLDataType value)
const {
1296 values_[i].v_type = value;
1302 TVM_ALWAYS_INLINE
void operator()(
size_t i,
const char* value)
const {
1303 values_[i].v_str = value;
1307 TVM_ALWAYS_INLINE
void operator()(
size_t i,
const std::string& value)
const {
1308 values_[i].v_str = value.c_str();
1312 values_[i].v_handle =
const_cast<TVMByteArray*
>(&value);
1316 if (value !=
nullptr) {
1317 values_[i].v_handle =
const_cast<PackedFunc*
>(&value);
1320 values_[i].v_handle =
nullptr;
1324 template <
typename FType>
1330 values_[i].v_str = value.
ptr<std::string>()->c_str();
1334 values_[i] = value.
value_;
1339 template <
typename TObjectRef,
1340 typename =
typename std::enable_if<std::is_base_of<ObjectRef, TObjectRef>::value>::type>
1341 TVM_ALWAYS_INLINE
void operator()(
size_t i,
const TObjectRef& value)
const {
1342 this->SetObject(i, value);
1345 template <
typename TObjectRef,
1346 typename =
typename std::enable_if<std::is_base_of<
1347 ObjectRef,
typename std::remove_reference<TObjectRef>::type>::value>::type>
1348 TVM_ALWAYS_INLINE
void operator()(
size_t i, TObjectRef&& value)
const {
1349 this->SetObject(i, std::forward<TObjectRef>(value));
1353 template <
typename TObjectRef>
1354 inline void SetObject(
size_t i, TObjectRef&& value)
const;
1361 template <
typename... Args>
1363 const int kNumArgs =
sizeof...(Args);
1364 const int kArraySize = kNumArgs > 0 ? kNumArgs : 1;
1366 int type_codes[kArraySize];
1367 detail::for_each(
TVMArgsSetter(values, type_codes), std::forward<Args>(args)...);
1369 body_(
TVMArgs(values, type_codes, kNumArgs), &rv);
1374 template <
typename R,
int nleft,
int index,
typename F>
1375 struct unpack_call_dispatcher {
1376 template <
typename... Args>
1377 TVM_ALWAYS_INLINE
static void run(
const std::string* optional_name,
const F& f,
1379 Args&&... unpacked_args) {
1382 unpack_call_dispatcher<R, nleft - 1, index + 1, F>::run(
1383 optional_name, f, args_pack, rv, std::forward<Args>(unpacked_args)...,
1389 template <
typename R,
int index,
typename F>
1390 struct unpack_call_dispatcher<R, 0, index, F> {
1391 template <
typename... Args>
1392 TVM_ALWAYS_INLINE
static void run(
const std::string* optional_name,
const F& f,
1394 Args&&... unpacked_args) {
1395 using RetType = decltype(f(std::forward<Args>(unpacked_args)...));
1396 if (std::is_same<RetType, R>::value) {
1397 *rv = f(std::forward<Args>(unpacked_args)...);
1399 *rv = R(f(std::forward<Args>(unpacked_args)...));
1404 template <
int index,
typename F>
1405 struct unpack_call_dispatcher<void, 0, index, F> {
1406 template <
typename... Args>
1407 TVM_ALWAYS_INLINE
static void run(
const std::string* optional_name,
const F& f,
1409 Args&&... unpacked_args) {
1410 f(std::forward<Args>(unpacked_args)...);
1414 template <
typename R,
int nargs,
typename F>
1415 TVM_ALWAYS_INLINE
void unpack_call(
const std::string* optional_name,
const F& f,
1417 CHECK_EQ(nargs, args.
size()) <<
"Function " 1418 << (optional_name ==
nullptr ?
"<anonymous>" : *optional_name)
1419 <<
" expects " << nargs <<
" arguments but " << args.
size()
1420 <<
" were provided";
1421 unpack_call_dispatcher<R, nargs, 0, F>::run(optional_name, f, args, rv);
1424 template <
typename FType>
1425 struct unpack_call_by_signature {};
1427 template <
typename R,
typename... Args>
1428 struct unpack_call_by_signature<R(Args...)> {
1429 template <
typename F>
1430 TVM_ALWAYS_INLINE
static void run(
const F& f,
const TVMArgs& args,
TVMRetValue* rv) {
1431 unpack_call<R,
sizeof...(Args)>(
nullptr, f, args, rv);
1435 template <
typename R,
typename... Args>
1436 TVM_ALWAYS_INLINE R call_packed(
const PackedFunc& pf, Args&&... args) {
1437 return R(pf(std::forward<Args>(args)...));
1440 template <
typename R>
1441 struct typed_packed_call_dispatcher {
1442 template <
typename... Args>
1443 TVM_ALWAYS_INLINE
static R run(
const PackedFunc& pf, Args&&... args) {
1444 return pf(std::forward<Args>(args)...);
1449 struct typed_packed_call_dispatcher<void> {
1450 template <
typename... Args>
1451 TVM_ALWAYS_INLINE
static void run(
const PackedFunc& pf, Args&&... args) {
1452 pf(std::forward<Args>(args)...);
1457 template <
typename R,
typename... Args>
1460 template <
typename R,
typename... Args>
1464 template <
typename R,
typename... Args>
1468 template <
typename R,
typename... Args>
1472 template <
typename R,
typename... Args>
1473 template <
typename FType>
1474 inline void TypedPackedFunc<R(Args...)>::AssignTypedLambda(FType flambda, std::string name) {
1476 if (args.
size() !=
sizeof...(Args)) {
1477 LOG(FATAL) <<
"Function " << name <<
" expects " <<
sizeof...(Args) <<
" arguments, but " 1478 << args.
size() <<
" were provided.";
1480 detail::unpack_call<R,
sizeof...(Args)>(&name, flambda, args, rv);
1484 template <
typename R,
typename... Args>
1485 template <
typename FType>
1488 if (args.
size() !=
sizeof...(Args)) {
1489 LOG(FATAL) <<
"Function <anonymous> expects " <<
sizeof...(Args) <<
" arguments, but " 1490 << args.
size() <<
" were provided.";
1492 detail::unpack_call<R,
sizeof...(Args)>(
nullptr, flambda, args, rv);
1496 template <
typename R,
typename... Args>
1498 return detail::typed_packed_call_dispatcher<R>::run(packed_, std::forward<Args>(args)...);
1506 template <
typename T>
1507 inline void TVMArgsSetter::SetObject(
size_t i, T&& value)
const {
1508 using ContainerType =
typename std::remove_reference<T>::type::ContainerType;
1509 if (value.defined()) {
1510 Object* ptr = value.data_.data_;
1511 if (std::is_base_of<NDArray::ContainerType, ContainerType>::value ||
1512 (std::is_base_of<ContainerType, NDArray::ContainerType>::value &&
1516 }
else if (std::is_base_of<Module::ContainerType, ContainerType>::value ||
1517 (std::is_base_of<ContainerType, Module::ContainerType>::value &&
1519 values_[i].v_handle = ptr;
1521 }
else if (std::is_rvalue_reference<decltype(value)>::value) {
1522 values_[i].v_handle =
const_cast<Object**
>(&(value.data_.data_));
1525 values_[i].v_handle = value.data_.data_;
1533 template <
typename TObjectRef,
typename>
1535 using ContainerType =
typename TObjectRef::ContainerType;
1537 if (std::is_base_of<NDArray::ContainerType, ContainerType>::value) {
1542 if (std::is_base_of<Module::ContainerType, ContainerType>::value) {
1544 static_cast<Object*
>(value_.v_handle)->IsInstance<ContainerType>();
1550 return (std::is_base_of<ContainerType, NDArray::ContainerType>::value &&
1552 (std::is_base_of<ContainerType, Module::ContainerType>::value &&
1558 template <
typename TObjectRef>
1560 static_assert(std::is_base_of<ObjectRef, TObjectRef>::value,
1561 "Conversion only works for ObjectRef");
1562 using ContainerType =
typename TObjectRef::ContainerType;
1565 CHECK(TObjectRef::_type_is_nullable)
1566 <<
"Expect a not null value of " << ContainerType::_type_key;
1570 if (std::is_base_of<NDArray::ContainerType, ContainerType>::value) {
1575 CHECK(data->IsInstance<ContainerType>())
1576 <<
"Expected " << ContainerType::_type_key <<
" but got " << data->GetTypeKey();
1577 return TObjectRef(data);
1579 if (std::is_base_of<Module::ContainerType, ContainerType>::value) {
1583 CHECK(data->IsInstance<ContainerType>())
1584 <<
"Expected " << ContainerType::_type_key <<
" but got " << data->GetTypeKey();
1585 return TObjectRef(data);
1592 <<
", but got " << checked_type.
value();
1593 return TObjectRef(GetObjectPtr<Object>(ptr));
1598 <<
", but got " << checked_type.
value();
1599 return TObjectRef(GetObjectPtr<Object>(ptr));
1600 }
else if (std::is_base_of<ContainerType, NDArray::ContainerType>::value &&
1605 return TObjectRef(data);
1606 }
else if (std::is_base_of<ContainerType, Module::ContainerType>::value &&
1609 return TObjectRef(GetObjectPtr<Object>(static_cast<Object*>(value_.v_handle)));
1616 template <
typename TObjectRef,
typename>
1618 using ContainerType =
typename TObjectRef::ContainerType;
1619 const Object* ptr = other.get();
1620 if (ptr !=
nullptr) {
1621 if (std::is_base_of<NDArray::ContainerType, ContainerType>::value ||
1622 (std::is_base_of<ContainerType, NDArray::ContainerType>::value &&
1624 return operator=(
NDArray(std::move(other.data_)));
1626 if (std::is_base_of<Module::ContainerType, ContainerType>::value ||
1627 (std::is_base_of<ContainerType, Module::ContainerType>::value &&
1629 return operator=(
Module(std::move(other.data_)));
1638 template <
typename T,
typename>
1639 inline TVMArgValue::operator T()
const {
1643 template <
typename T,
typename>
1644 inline TVMMovableArgValue_::operator T()
const {
1646 auto** ref =
static_cast<Object**
>(value_.v_handle);
1655 template <
typename T,
typename>
1656 inline TVMRetValue::operator T()
const {
1661 return (*this)->GetFunction(name, query_imports);
1684 template <
typename T>
1700 inline TVMArgValue::operator DLDataType()
const {
1713 return value_.v_type;
1720 #endif // TVM_RUNTIME_PACKED_FUNC_H_ static TObjectRef From(const TVMArgValue &val)
Convert a TObjectRef from an argument value.
Definition: packed_func.h:1043
const char * ArgTypeCode2Str(int type_code)
Convert argument type code to string.
Definition: packed_func.h:1156
int num_args
Definition: packed_func.h:339
TVMArgValue()
default constructor
Definition: packed_func.h:586
static bool Check(const Object *ptr)
Definition: packed_func.h:433
TVMRetValue & operator=(std::string value)
Definition: packed_func.h:839
array node content in array
Definition: array.h:38
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:734
PackedFunc GetFunction(const std::string &name, bool query_imports=false)
Get packed function from current module by name.
Definition: packed_func.h:1660
static void FFIDecRef(TVMArrayHandle handle)
DecRef resource managed by an FFI array handle.
Definition: ndarray.h:396
TVMRetValue & operator=(TVMMovableArgValue_ &&other)
Definition: packed_func.h:882
TVM_ALWAYS_INLINE void operator()(size_t i, TObjectRef &&value) const
Definition: packed_func.h:1348
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. The managed resources are moved to the front-end. The front end should take charge in managing them.
Definition: packed_func.h:895
PrimExpr min(PrimExpr a, PrimExpr b, Span span=Span())
take minimum of two values
A custom smart pointer for Object.
Definition: object.h:356
TVMArgs(const TVMValue *values, const int *type_codes, int num_args)
constructor
Definition: packed_func.h:346
A PackedFunc wrapper to provide typed function signature. It is backed by a PackedFunc internally...
Definition: packed_func.h:171
TVM_ALWAYS_INLINE void operator()(size_t i, const char *value) const
Definition: packed_func.h:1302
bool operator!=(std::nullptr_t null) const
Definition: packed_func.h:125
TVMRetValue & operator=(bool value)
Definition: packed_func.h:834
int type_code_
the type code
Definition: packed_func.h:574
Internal base class to handle conversion to POD values.
Definition: packed_func.h:485
std::string DLDataType2String(DLDataType t)
convert a TVM type to string.
Definition: data_type.h:332
void * v_handle
Definition: c_runtime_api.h:147
const PackedFunc & packed() const
Definition: packed_func.h:302
Definition: c_runtime_api.h:124
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
static Optional< String > CheckAndGetMismatch(const Object *ptr)
Definition: packed_func.h:448
FType body() const
Definition: packed_func.h:1153
Definition: c_runtime_api.h:119
TVMMovableArgValue_(TVMValue value, int type_code)
Definition: packed_func.h:652
TVM_ALWAYS_INLINE void operator()(size_t i, DLTensor *value) const
Definition: packed_func.h:1287
~TVMRetValue()
destructor
Definition: packed_func.h:747
Definition: c_runtime_api.h:115
const TVMValue * values
Definition: packed_func.h:337
Definition: c_runtime_api.h:120
TVMArgValue operator[](int i) const
Get i-th argument.
Definition: packed_func.h:1143
size_t size
Definition: c_runtime_api.h:159
TVMMovableArgValueWithContext_(TVMValue value, int type_code, int arg_index, const std::string *optional_name)
move constructor from another return value.
Definition: packed_func.h:705
static std::string TypeName()
Definition: packed_func.h:407
static bool Check(const Object *ptr)
Check if an object matches the template type.
Definition: packed_func.h:402
Union type of values being passed through API and function calls.
Definition: c_runtime_api.h:144
PackedFunc(FType body)
constructing a packed function from a std::function.
Definition: packed_func.h:97
base class of all object containers.
Definition: object.h:165
Definition: c_runtime_api.h:118
const char * data
Definition: c_runtime_api.h:158
Object * TVMArrayHandleToObjectHandle(TVMArrayHandle handle)
Definition: ndarray.h:400
TVMValue value_
The value.
Definition: packed_func.h:572
TVMRetValue()
default constructor
Definition: packed_func.h:737
Managed NDArray. The array is backed by reference counted blocks.
Definition: ndarray.h:59
TVMRetValue & operator=(int value)
Definition: packed_func.h:818
static void FFIClearAfterMove(ObjectRef *ref)
Clear the object ref data field without DecRef after we successfully moved the field.
Definition: object.h:585
TVMRetValue & operator=(TVMRetValue &&other)
Definition: packed_func.h:791
Byte array type used to pass in byte array When kTVMBytes is used as data type.
Definition: c_runtime_api.h:157
TVMRetValue & operator=(TVMByteArray value)
Definition: packed_func.h:843
TypedPackedFunc(const FLambda &typed_lambda)
construct from a lambda function with the same signature.
Definition: packed_func.h:253
bool IsInstance() const
Definition: object.h:822
TVMRetValue & operator=(const TVMArgValue &other)
Definition: packed_func.h:878
PackedFunc()
default constructor
Definition: packed_func.h:90
TVMRetValue & operator=(double value)
Definition: packed_func.h:798
TVM_ALWAYS_INLINE void operator()(size_t i, const TVMByteArray &value) const
Definition: packed_func.h:1311
Runtime Array container types.
Definition: packed_func.h:38
Internal auxiliary struct for TypedPackedFunc to indicate a movable argument.
Definition: packed_func.h:650
static bool Check(const Object *ptr)
Definition: packed_func.h:465
TVM_ALWAYS_INLINE void operator()(size_t i, T value) const
Definition: packed_func.h:1262
static Optional< T > From(const TVMRetValue &val)
Definition: packed_func.h:1690
A device-independent managed NDArray abstraction.
DLDataType String2DLDataType(std::string s)
convert a string to TVM type.
Definition: data_type.h:339
static String From(const TVMRetValue &val)
Definition: packed_func.h:1675
static ObjectPtr< Object > FFIDataFromHandle(TVMArrayHandle handle)
Construct NDArray's Data field from array handle in FFI.
Definition: ndarray.h:383
Type traits for runtime type check during FFI conversion.
Definition: packed_func.h:374
bool IsObjectRef() const
Definition: packed_func.h:1534
static Optional< T > From(const TVMArgValue &val)
Definition: packed_func.h:1686
Runtime primitive data type.
Definition: data_type.h:41
bool defined() const
Definition: object.h:537
TVM_ALWAYS_INLINE void operator()(size_t i, Device value) const
Definition: packed_func.h:1291
Arguments into TVM functions.
Definition: packed_func.h:335
TVMRetValue(TVMRetValue &&other)
move constructor from another return value.
Definition: packed_func.h:742
TSelf & operator=(FLambda typed_lambda)
copy assignment operator from typed lambda
Definition: packed_func.h:275
T value() const
Definition: optional.h:92
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:270
Definition: c_runtime_api.h:123
TypedPackedFunc(const FLambda &typed_lambda, std::string name)
construct from a lambda function with the same signature.
Definition: packed_func.h:230
TVM_ALWAYS_INLINE void operator()(size_t i, uint64_t value) const
Definition: packed_func.h:1266
static std::string TypeName()
Definition: packed_func.h:444
TSelf & operator=(PackedFunc packed)
copy assignment operator from PackedFunc.
Definition: packed_func.h:284
std::function< void(TVMArgs args, TVMRetValue *rv)> FType
The internal std::function.
Definition: packed_func.h:88
TVMRetValue & operator=(Module m)
Definition: packed_func.h:858
Object container class that backs NDArray.
Definition: ndarray.h:261
TVMRetValue & operator=(PackedFunc f)
Definition: packed_func.h:862
TVM_ALWAYS_INLINE void operator()(size_t i, const TObjectRef &value) const
Definition: packed_func.h:1341
const int * type_codes
Definition: packed_func.h:338
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:567
TVMRetValue & operator=(DLDataType t)
Definition: packed_func.h:828
bool operator==(std::nullptr_t null) const
Definition: packed_func.h:123
TObjectRef AsObjectRef() const
Definition: packed_func.h:1559
PrimExpr max(PrimExpr a, PrimExpr b, Span span=Span())
take maximum of two values
TVM_ALWAYS_INLINE void operator()(size_t i, const std::string &value) const
Definition: packed_func.h:1307
Reference to string objects.
Definition: string.h:129
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:136
TVM_ALWAYS_INLINE void operator()(size_t i, DataType dtype) const
Definition: packed_func.h:1299
TVMPODValue_(TVMValue value, int type_code)
Definition: packed_func.h:569
TVM_ALWAYS_INLINE void operator()(size_t i, double value) const
Definition: packed_func.h:1271
TVM_ALWAYS_INLINE void operator()(size_t i, std::nullptr_t value) const
Definition: packed_func.h:1275
T * ptr() const
return handle as specific pointer type.
Definition: packed_func.h:554
DLDevice Device
Definition: ndarray.h:43
Definition: c_runtime_api.h:113
const Object * get() const
Definition: object.h:539
TVMRetValue & operator=(const TypedPackedFunc< FType > &f)
Definition: packed_func.h:871
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:909
TVMRetValue & operator=(DLDevice value)
Definition: packed_func.h:823
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:382
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:1696
Base class of all object reference.
Definition: object.h:504
Base container of module.
Definition: module.h:111
int size() const
Definition: packed_func.h:1149
std::string GetTypeKey() const
Definition: object.h:178
TVMRetValue & operator=(const DataType &other)
Definition: packed_func.h:833
Shared content of all specializations of hash map.
Definition: map.h:167
TVMRetValue & operator=(int64_t value)
Definition: packed_func.h:813
A managed object in the TVM runtime.
TVM_ALWAYS_INLINE void operator()(size_t i, const TVMArgValue &value) const
Definition: packed_func.h:1279
static TVMArrayHandle FFIGetHandle(const ObjectRef &nd)
Get FFI Array handle from ndarray.
Definition: ndarray.h:388
void operator()(size_t i, const TVMRetValue &value) const
Definition: packed_func.h:1328
TVM_ALWAYS_INLINE void operator()(size_t i, const PackedFunc &value) const
Definition: packed_func.h:1315
Runtime container of the functions generated by TVM, This is used to support dynamically link...
bool operator==(std::nullptr_t null) const
Definition: packed_func.h:304
Module container of TVM.
Definition: module.h:48
bool operator!=(std::nullptr_t null) const
Definition: packed_func.h:306
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics, which means map is mutable but copy will happen when array is referenced in more than two places.
Definition: map.h:1235
static std::string TypeIndex2Key(uint32_t tindex)
Get the type key of the corresponding index from runtime.
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
Runtime Map container types.
static String From(const TVMArgValue &val)
Definition: packed_func.h:1667
static std::string TypeName()
Definition: packed_func.h:475
Definition: c_runtime_api.h:114
int type_code() const
Definition: packed_func.h:547
A single argument value to PackedFunc. Containing both type_code and TVMValue.
Definition: packed_func.h:583
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:68
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
const TVMValue & value() const
Definition: packed_func.h:632
TVMRetValue operator()(Args &&... args) const
Call packed function by directly passing in unpacked format.
Definition: packed_func.h:1362
Definition: packed_func.h:1257
constexpr runtime::NullOptType NullOpt
Definition: optional.h:155
TVMArgsSetter(TVMValue *values, int *type_codes)
Definition: packed_func.h:1259
TypedPackedFunc(std::nullptr_t null)
constructor from null
Definition: packed_func.h:178
TVMRetValue(const TVMRetValue &other)
Definition: packed_func.h:762
void CallPacked(TVMArgs args, TVMRetValue *rv) const
Call the function in packed format.
Definition: packed_func.h:1151
PackedFunc(std::nullptr_t null)
constructor from null
Definition: packed_func.h:92
const TVMValue & value() const
Definition: packed_func.h:918
Definition: c_runtime_api.h:116
TVMArgValue(TVMValue value, int type_code)
constructor
Definition: packed_func.h:592
TVMRetValue & operator=(const TVMRetValue &other)
Definition: packed_func.h:874
#define TVM_CHECK_TYPE_CODE(CODE, T)
Definition: packed_func.h:366
TVM_ALWAYS_INLINE void operator()(size_t i, void *value) const
Definition: packed_func.h:1283
Definition: c_runtime_api.h:122
TVMRetValue & operator=(std::nullptr_t value)
Definition: packed_func.h:803
static Optional< String > CheckAndGetMismatch(const Object *ptr)
Definition: packed_func.h:416
runtime::DataType DataType
Definition: data_type.h:389
Definition: c_runtime_api.h:121
TVMRetValue & operator=(NDArray other)
Definition: packed_func.h:847
Type trait to specify special value conversion rules from TVMArgValue and TVMRetValue.
Definition: packed_func.h:1037
static TObjectRef From(const TVMRetValue &val)
Convert a TObjectRef from a return value.
Definition: packed_func.h:1049
Definition: c_runtime_api.h:117
TypedPackedFunc()
default constructor
Definition: packed_func.h:176
Internal auxiliary struct for TypedPackedFunc to indicate a movable argument with additional context ...
Definition: packed_func.h:695
TVMRetValue & operator=(void *value)
Definition: packed_func.h:808
TVM_ALWAYS_INLINE void operator()(size_t i, const TypedPackedFunc< FType > &value) const
Definition: packed_func.h:1325
TVM_ALWAYS_INLINE void operator()(size_t i, DLDataType value) const
Definition: packed_func.h:1295
TVMPODValue_()
Definition: packed_func.h:568