tvm
Classes | Namespaces | Macros | Typedefs | Functions
packed_func.h File Reference

Type-erased function used across TVM API. More...

#include <tvm/runtime/c_runtime_api.h>
#include <tvm/runtime/container/array.h>
#include <tvm/runtime/container/map.h>
#include <tvm/runtime/container/variant.h>
#include <tvm/runtime/data_type.h>
#include <tvm/runtime/logging.h>
#include <tvm/runtime/module.h>
#include <tvm/runtime/ndarray.h>
#include <tvm/runtime/object.h>
#include <functional>
#include <limits>
#include <memory>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
Include dependency graph for packed_func.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  tvm::runtime::PackedFuncObj
 Object container class that backs PackedFunc. More...
 
struct  tvm::runtime::PackedFuncObj::Extractor< TPackedFuncSubObj >
 Internal struct for extracting the callable method from callable type. More...
 
class  tvm::runtime::PackedFuncSubObj< TCallable >
 Derived object class for constructing PackedFuncObj. More...
 
class  tvm::runtime::PackedFunc
 Packed function is a type-erased function. The arguments are passed by packed format. More...
 
class  tvm::runtime::TypedPackedFunc< R(Args...)>
 A PackedFunc wrapper to provide typed function signature. It is backed by a PackedFunc internally. More...
 
class  tvm::runtime::TVMArgs
 Arguments into TVM functions. More...
 
struct  tvm::runtime::ObjectTypeChecker< T >
 Type traits for runtime type check during FFI conversion. More...
 
struct  tvm::runtime::ObjectTypeChecker< Array< T > >
 
struct  tvm::runtime::ObjectTypeChecker< Map< K, V > >
 
class  tvm::runtime::TVMPODValue_
 Internal base class to handle conversion to POD values. More...
 
class  tvm::runtime::TVMArgValue
 A single argument value to PackedFunc. Containing both type_code and TVMValue. More...
 
class  tvm::runtime::TVMMovableArgValue_
 Internal auxiliary struct for TypedPackedFunc to indicate a movable argument. More...
 
class  tvm::runtime::TVMMovableArgValueWithContext_
 Internal auxiliary struct for TypedPackedFunc to indicate a movable argument with additional context information (function name and argument index) for better error reporting. More...
 
class  tvm::runtime::TVMRetValue
 Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlying container during destruction. More...
 
struct  tvm::runtime::PackedFuncValueConverter< TObjectRef >
 Type trait to specify special value conversion rules from TVMArgValue and TVMRetValue. More...
 
class  tvm::runtime::TVMArgsSetter
 
struct  tvm::runtime::TVMArgsSetterApply< i, T >
 
struct  tvm::runtime::PackedFuncValueConverter<::tvm::runtime::String >
 
struct  tvm::runtime::PackedFuncValueConverter< Optional< T > >
 
struct  tvm::runtime::PackedFuncValueConverter< Variant< VariantTypes... > >
 

Namespaces

 tvm
 runtime implementation for LibTorch/TorchScript.
 
 tvm::runtime
 

Macros

#define TVM_RUNTIME_HEADER_ONLY   0
 
#define TVM_CHECK_TYPE_CODE(CODE, T)    ICHECK_EQ(CODE, T) << "expected " << ArgTypeCode2Str(T) << " but got " << ArgTypeCode2Str(CODE)
 
#define TVM_DLL_EXPORT_PACKED_FUNC(ExportName, Function)
 Export a function with the PackedFunc signature as a PackedFunc that can be loaded by LibraryModule. More...
 
#define TVM_MODULE_VTABLE_BEGIN(TypeKey)
 
#define TVM_MODULE_VTABLE_END()
 
#define TVM_MODULE_VTABLE_END_WITH_DEFAULT(MemFunc)
 
#define TVM_MODULE_VTABLE_ENTRY(Name, MemFunc)
 
#define TVM_MODULE_VTABLE_ENTRY_PACKED(Name, MemFunc)
 
#define TVM_DLL_EXPORT_TYPED_FUNC(ExportName, Function)
 Export typed function as a PackedFunc that can be loaded by LibraryModule. More...
 

Typedefs

using tvm::runtime::FSig = std::string()
 Using static function to output TypedPackedFunc signature. More...
 

Functions

const char * tvm::runtime::ArgTypeCode2Str (int type_code)
 Convert argument type code to string. More...
 
std::ostream & tvm::runtime::operator<< (std::ostream &os, DLDevice dev)
 
const char * tvm::runtime::DLDeviceType2Str (int type)
 The name of DLDeviceType. More...
 
template<typename... Args>
void TVM_ALWAYS_INLINE tvm::runtime::PackArgs (TVMValue *values, int *type_codes, Args &&... args)
 

Detailed Description

Type-erased function used across TVM API.

Macro Definition Documentation

◆ TVM_CHECK_TYPE_CODE

#define TVM_CHECK_TYPE_CODE (   CODE,
 
)     ICHECK_EQ(CODE, T) << "expected " << ArgTypeCode2Str(T) << " but got " << ArgTypeCode2Str(CODE)

◆ TVM_DLL_EXPORT_PACKED_FUNC

#define TVM_DLL_EXPORT_PACKED_FUNC (   ExportName,
  Function 
)
Value:
extern "C" { \
TVM_DLL int ExportName(TVMValue* args, int* type_code, int num_args, TVMValue* out_value, \
int* out_type_code, void* resource_handle); \
int ExportName(TVMValue* args, int* type_code, int num_args, TVMValue* out_value, \
int* out_type_code, void* resource_handle) { \
try { \
Function(::tvm::runtime::TVMArgs(args, type_code, num_args), &rv); \
rv.MoveToCHost(out_value, out_type_code); \
return 0; \
} catch (const ::std::exception& _except_) { \
TVMAPISetLastError(_except_.what()); \
return -1; \
} \
} \
}
Arguments into TVM functions.
Definition: packed_func.h:392
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:807
Union type of values being passed through API and function calls.
Definition: c_runtime_api.h:208

Export a function with the PackedFunc signature as a PackedFunc that can be loaded by LibraryModule.

Parameters
ExportNameThe symbol name to be exported.
FunctionThe function with PackedFunc signature.
See also
PackedFunc
void AddOne_(TVMArgs args, TVMRetValue* rv) {
int value = args[0];
*rv = value + 1;
}
// Expose the function as "AddOne"
TVM_DLL_EXPORT_PACKED_FUNC(AddOne, AddOne_);
#define TVM_DLL_EXPORT_PACKED_FUNC(ExportName, Function)
Export a function with the PackedFunc signature as a PackedFunc that can be loaded by LibraryModule.
Definition: packed_func.h:1138
Definition: packed_func.h:38

◆ TVM_DLL_EXPORT_TYPED_FUNC

#define TVM_DLL_EXPORT_TYPED_FUNC (   ExportName,
  Function 
)
Value:
extern "C" { \
TVM_DLL int ExportName(TVMValue* args, int* type_code, int num_args, TVMValue* out_value, \
int* out_type_code, void* resource_handle) { \
try { \
auto f = Function; \
using FType = ::tvm::runtime::detail::function_signature<decltype(f)>::FType; \
::tvm::runtime::detail::unpack_call_by_signature<FType>::run( \
f, ::tvm::runtime::TVMArgs(args, type_code, num_args), &rv); \
rv.MoveToCHost(out_value, out_type_code); \
return 0; \
} catch (const ::std::exception& _except_) { \
TVMAPISetLastError(_except_.what()); \
return -1; \
} \
} \
}
tvm::relax::Function Function
Definition: transform.h:42

Export typed function as a PackedFunc that can be loaded by LibraryModule.

Parameters
ExportNameThe symbol name to be exported.
FunctionThe typed function.
Note
ExportName and Function must be different, see code examples below.
See also
TypedPackedFunc
int AddOne_(int x) {
return x + 1;
}
// Expose the function as "AddOne"
TVM_DLL_EXPORT_TYPED_FUNC(AddOne, AddOne_);
// Expose the function as "SubOne"
TVM_DLL_EXPORT_TYPED_FUNC(SubOne, [](int x) {
return x - 1;
});
// The following code will cause compilation error.
// Because the same Function and ExportName
// TVM_DLL_EXPORT_TYPED_FUNC(AddOne_, AddOne_);
// The following code is OK, assuming the macro
// is in a different namespace from xyz
// TVM_DLL_EXPORT_TYPED_FUNC(AddOne_, xyz::AddOne_);
#define TVM_DLL_EXPORT_TYPED_FUNC(ExportName, Function)
Export typed function as a PackedFunc that can be loaded by LibraryModule.
Definition: packed_func.h:1222

◆ TVM_MODULE_VTABLE_BEGIN

#define TVM_MODULE_VTABLE_BEGIN (   TypeKey)
Value:
const char* type_key() const final { return TypeKey; } \
PackedFunc GetFunction(const String& _name, const ObjectPtr<Object>& _self) override { \
using SelfPtr = std::remove_cv_t<decltype(this)>;

◆ TVM_MODULE_VTABLE_END

#define TVM_MODULE_VTABLE_END ( )
Value:
return PackedFunc(nullptr); \
}

◆ TVM_MODULE_VTABLE_END_WITH_DEFAULT

#define TVM_MODULE_VTABLE_END_WITH_DEFAULT (   MemFunc)
Value:
{ \
auto f = (MemFunc); \
return (this->*f)(_name); \
} \
}

◆ TVM_MODULE_VTABLE_ENTRY

#define TVM_MODULE_VTABLE_ENTRY (   Name,
  MemFunc 
)
Value:
if (_name == Name) { \
return PackedFunc([_self](TVMArgs args, TVMRetValue* rv) -> void { \
using Helper = ::tvm::runtime::detail::ModuleVTableEntryHelper<decltype(MemFunc)>; \
SelfPtr self = static_cast<SelfPtr>(_self.get()); \
CHECK_EQ(args.size(), Helper::LenArgs) \
<< "Function `" << self->type_key() << "::" << Name << "` requires " << Helper::LenArgs \
<< " arguments, but got " << args.size(); \
Helper::Call(rv, self, MemFunc, args, Helper::IndexSeq{}); \
}); \
}

◆ TVM_MODULE_VTABLE_ENTRY_PACKED

#define TVM_MODULE_VTABLE_ENTRY_PACKED (   Name,
  MemFunc 
)
Value:
if (_name == Name) { \
return PackedFunc([_self](TVMArgs args, TVMRetValue* rv) -> void { \
(static_cast<SelfPtr>(_self.get())->*(MemFunc))(args, rv); \
}); \
}

◆ TVM_RUNTIME_HEADER_ONLY

#define TVM_RUNTIME_HEADER_ONLY   0