Skip to content

Commit 16b2783

Browse files
authored
[REFACTOR][FFI] Cleanup PackedFunc related redirection (#17923)
1 parent bcb68b1 commit 16b2783

File tree

284 files changed

+2393
-2340
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

284 files changed

+2393
-2340
lines changed

apps/cpp_rpc/rpc_env.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ RPCEnv::RPCEnv(const std::string& wd) {
128128

129129
ffi::Function::SetGlobal(
130130
"tvm.rpc.server.workpath",
131-
ffi::Function::FromUnpacked([this](const std::string& path) { return this->GetPath(path); }));
131+
ffi::Function::FromTyped([this](const std::string& path) { return this->GetPath(path); }));
132132

133133
ffi::Function::SetGlobal("tvm.rpc.server.listdir",
134-
ffi::Function::FromUnpacked([this](const std::string& path) {
134+
ffi::Function::FromTyped([this](const std::string& path) {
135135
std::string dir = this->GetPath(path);
136136
std::ostringstream os;
137137
for (auto d : ListDir(dir)) {
@@ -141,15 +141,15 @@ RPCEnv::RPCEnv(const std::string& wd) {
141141
}));
142142

143143
ffi::Function::SetGlobal("tvm.rpc.server.load_module",
144-
ffi::Function::FromUnpacked([this](const std::string& path) {
144+
ffi::Function::FromTyped([this](const std::string& path) {
145145
std::string file_name = this->GetPath(path);
146146
file_name = BuildSharedLibrary(file_name);
147147
LOG(INFO) << "Load module from " << file_name << " ...";
148148
return Module::LoadFromFile(file_name, "");
149149
}));
150150

151151
ffi::Function::SetGlobal("tvm.rpc.server.download_linked_module",
152-
ffi::Function::FromUnpacked([this](const std::string& path) {
152+
ffi::Function::FromTyped([this](const std::string& path) {
153153
std::string file_name = this->GetPath(path);
154154
file_name = BuildSharedLibrary(file_name);
155155
std::string bin;

apps/hexagon_launcher/launcher_core.cc

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,27 +137,25 @@ Model::Model(tvm::runtime::Module executor, tvm::runtime::Module module, std::st
137137
run = get_module_func(model_executor, "run");
138138
}
139139

140-
const tvm::runtime::PackedFunc get_runtime_func(const std::string& name) {
140+
const tvm::ffi::Function get_runtime_func(const std::string& name) {
141141
if (auto pf = tvm::ffi::Function::GetGlobal(name)) {
142142
return *pf;
143143
}
144-
return tvm::runtime::PackedFunc();
144+
return tvm::ffi::Function();
145145
}
146146

147-
const tvm::runtime::PackedFunc get_module_func(tvm::runtime::Module module,
148-
const std::string& name) {
147+
const tvm::ffi::Function get_module_func(tvm::runtime::Module module, const std::string& name) {
149148
return module.GetFunction(name, false);
150149
}
151150

152151
void reset_device_api() {
153-
const tvm::runtime::PackedFunc api = get_runtime_func("device_api.hexagon");
152+
const tvm::ffi::Function api = get_runtime_func("device_api.hexagon");
154153
tvm::ffi::Function::SetGlobal("device_api.cpu", api, true);
155154
}
156155

157156
tvm::runtime::Module load_module(const std::string& file_name) {
158-
static const tvm::runtime::PackedFunc loader =
159-
get_runtime_func("runtime.module.loadfile_hexagon");
160-
tvm::runtime::TVMRetValue rv = loader(file_name);
157+
static const tvm::ffi::Function loader = get_runtime_func("runtime.module.loadfile_hexagon");
158+
tvm::ffi::Any rv = loader(file_name);
161159
if (rv.type_code() == kTVMModuleHandle) {
162160
ICHECK_EQ(rv.type_code(), kTVMModuleHandle)
163161
<< __func__ << ": loaded " << file_name << ", but did not get module handle";
@@ -180,26 +178,26 @@ tvm::runtime::Module create_graph_executor(const std::string& graph_json,
180178
tvm::runtime::Module graph_module, tvm::Device device) {
181179
std::string launcher_name = "tvm.graph_executor.create";
182180

183-
const tvm::runtime::PackedFunc create_executor = get_runtime_func(launcher_name);
181+
const tvm::ffi::Function create_executor = get_runtime_func(launcher_name);
184182
uint64_t device_type = device.device_type;
185183
uint64_t device_id = device.device_id;
186184

187185
if (graph_json.empty()) {
188186
LOG(ERROR) << __func__ << ": graph executor requires graph JSON";
189187
return tvm::runtime::Module();
190188
}
191-
tvm::runtime::TVMRetValue rv = create_executor(graph_json, graph_module, device_type, device_id);
189+
tvm::ffi::Any rv = create_executor(graph_json, graph_module, device_type, device_id);
192190
return rv.operator tvm::runtime::Module();
193191
}
194192

195193
tvm::runtime::Module create_aot_executor(tvm::runtime::Module factory_module, tvm::Device device) {
196-
tvm::runtime::PackedFunc list_modules = get_module_func(factory_module, "list_module_names");
194+
tvm::ffi::Function list_modules = get_module_func(factory_module, "list_module_names");
197195
tvm::Array<tvm::String> module_names = list_modules();
198196
if (module_names.size() != 1) {
199197
LOG(WARNING) << __func__ << ": expecting single module, got: " << module_names << ", using "
200198
<< module_names[0];
201199
}
202-
tvm::runtime::PackedFunc f = get_module_func(factory_module, module_names[0]);
200+
tvm::ffi::Function f = get_module_func(factory_module, module_names[0]);
203201
if (f.get() == nullptr) {
204202
LOG(ERROR) << __func__ << ": failed to obtain function " << module_names[0];
205203
return tvm::runtime::Module();

apps/hexagon_launcher/launcher_core.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ struct Model {
9090
static tvm::Device device() { return tvm::Device{static_cast<DLDeviceType>(kDLHexagon), 0}; }
9191
static tvm::Device external() { return tvm::Device{static_cast<DLDeviceType>(kDLCPU), 0}; }
9292

93-
tvm::runtime::PackedFunc run;
93+
tvm::ffi::Function run;
9494
};
9595

9696
struct ExecutionSession {
@@ -123,9 +123,8 @@ void reset_device_api();
123123

124124
tvm::runtime::Module load_module(const std::string& file_name);
125125

126-
const tvm::runtime::PackedFunc get_runtime_func(const std::string& name);
127-
const tvm::runtime::PackedFunc get_module_func(tvm::runtime::Module module,
128-
const std::string& name);
126+
const tvm::ffi::Function get_runtime_func(const std::string& name);
127+
const tvm::ffi::Function get_module_func(tvm::runtime::Module module, const std::string& name);
129128

130129
tvm::runtime::Module create_aot_executor(tvm::runtime::Module factory_module, tvm::Device device);
131130
tvm::runtime::Module create_graph_executor(const std::string& graph_json,

apps/hexagon_launcher/launcher_hexagon.cc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ static AEEResult error_too_small(const std::string& func_name, const std::string
4747
int __QAIC_HEADER(launcher_rpc_open)(const char* uri, remote_handle64* handle) {
4848
*handle = 0; // Just use any value.
4949
reset_device_api();
50-
static const tvm::runtime::PackedFunc acq_res =
50+
static const tvm::ffi::Function acq_res =
5151
get_runtime_func("device_api.hexagon.acquire_resources");
5252
acq_res();
5353
return AEE_SUCCESS;
5454
}
5555

5656
int __QAIC_HEADER(launcher_rpc_close)(remote_handle64 handle) {
5757
// Comment to stop clang-format from single-lining this function.
58-
static const tvm::runtime::PackedFunc rel_res =
58+
static const tvm::ffi::Function rel_res =
5959
get_runtime_func("device_api.hexagon.release_resources");
6060
rel_res();
6161
return AEE_SUCCESS;
@@ -104,8 +104,7 @@ AEEResult __QAIC_HEADER(launcher_rpc_get_num_inputs)(remote_handle64 handle, int
104104
return AEE_EBADSTATE;
105105
}
106106

107-
tvm::runtime::PackedFunc get_num_inputs =
108-
get_module_func(TheModel->model_executor, "get_num_inputs");
107+
tvm::ffi::Function get_num_inputs = get_module_func(TheModel->model_executor, "get_num_inputs");
109108
*num_inputs = get_num_inputs();
110109
return AEE_SUCCESS;
111110
}
@@ -140,7 +139,7 @@ AEEResult __QAIC_HEADER(launcher_rpc_set_input)(remote_handle64 handle, int inpu
140139

141140
auto input = tvm::runtime::NDArray::FromDLPack(&managed);
142141

143-
tvm::runtime::PackedFunc set_input = get_module_func(TheModel->model_executor, "set_input");
142+
tvm::ffi::Function set_input = get_module_func(TheModel->model_executor, "set_input");
144143
set_input(input_idx, input);
145144

146145
return AEE_SUCCESS;
@@ -152,8 +151,7 @@ AEEResult __QAIC_HEADER(launcher_rpc_get_num_outputs)(remote_handle64 handle, in
152151
return AEE_EBADSTATE;
153152
}
154153

155-
tvm::runtime::PackedFunc get_num_outputs =
156-
get_module_func(TheModel->model_executor, "get_num_outputs");
154+
tvm::ffi::Function get_num_outputs = get_module_func(TheModel->model_executor, "get_num_outputs");
157155
*num_outputs = get_num_outputs();
158156
return AEE_SUCCESS;
159157
}
@@ -173,7 +171,7 @@ AEEResult __QAIC_HEADER(launcher_rpc_get_output)(remote_handle64 handle, int out
173171
return AEE_EBADPARM;
174172
}
175173

176-
tvm::runtime::PackedFunc get_output = get_module_func(TheModel->model_executor, "get_output");
174+
tvm::ffi::Function get_output = get_module_func(TheModel->model_executor, "get_output");
177175
tvm::runtime::NDArray output = get_output(output_idx);
178176

179177
std::vector<int64_t> shape_vec{output->shape, output->shape + output->ndim};

apps/ios_rpc/tvmrpc/RPCServer.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
* 2: need to write
5252
* 0: shutdown
5353
*/
54-
using FEventHandler = PackedFunc;
54+
using FEventHandler = ffi::Function;
5555

5656
/*!
5757
* \brief Create a server event handler.
@@ -68,7 +68,7 @@ FEventHandler CreateServerEventHandler(NSOutputStream* outputStream, std::string
6868
<< "You are using tvm_runtime module built without RPC support. "
6969
<< "Please rebuild it with USE_RPC flag.";
7070

71-
PackedFunc writer_func([outputStream](TVMArgs args, TVMRetValue* rv) {
71+
ffi::Function writer_func([outputStream](ffi::PackedArgs args, ffi::Any* rv) {
7272
TVMByteArray* data = args[0].ptr<TVMByteArray>();
7373
int64_t nbytes = [outputStream write:reinterpret_cast<const uint8_t*>(data->data)
7474
maxLength:data->size];

apps/ios_rpc/tvmrpc/TVMRuntime.mm

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ void LogMessageImpl(const std::string& file, int lineno, int level, const std::s
5151

5252
} // namespace detail
5353

54-
TVM_REGISTER_GLOBAL("tvm.rpc.server.workpath").set_body_packed([](TVMArgs args, TVMRetValue* rv) {
55-
static const std::string base_ = NSTemporaryDirectory().UTF8String;
56-
const auto path = args[0].cast<std::string>();
57-
*rv = base_ + "/" + path;
58-
});
54+
TVM_REGISTER_GLOBAL("tvm.rpc.server.workpath")
55+
.set_body_packed([](ffi::PackedArgs args, ffi::Any* rv) {
56+
static const std::string base_ = NSTemporaryDirectory().UTF8String;
57+
const auto path = args[0].cast<std::string>();
58+
*rv = base_ + "/" + path;
59+
});
5960

6061
TVM_REGISTER_GLOBAL("tvm.rpc.server.load_module")
61-
.set_body_packed([](TVMArgs args, TVMRetValue* rv) {
62+
.set_body_packed([](ffi::PackedArgs args, ffi::Any* rv) {
6263
auto name = args[0].cast<std::string>();
6364
std::string fmt = GetFileFormat(name, "");
6465
NSString* base;
@@ -109,7 +110,7 @@ void Init(const std::string& name) {
109110

110111
// Add UnsignedDSOLoader plugin in global registry
111112
TVM_REGISTER_GLOBAL("runtime.module.loadfile_dylib_custom")
112-
.set_body_packed([](TVMArgs args, TVMRetValue* rv) {
113+
.set_body_packed([](ffi::PackedArgs args, ffi::Any* rv) {
113114
auto n = make_object<UnsignedDSOLoader>();
114115
n->Init(args[0]);
115116
*rv = CreateModuleFromLibrary(n);

docs/arch/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ tvm/runtime
198198
The runtime serves as the foundation of the TVM stack. It provides the mechanism to load and execute compiled artifacts.
199199
The runtime defines a stable standard set of C APIs to interface with frontend languages such as Python and Rust.
200200

201-
`runtime::Object` is one of the primary data structures in TVM runtime besides the `runtime::PackedFunc`.
201+
`runtime::Object` is one of the primary data structures in TVM runtime besides the `ffi::Function`.
202202
It is a reference-counted base class with a type index to support runtime type checking and downcasting.
203203
The object system allows the developer to introduce new data structures to the runtime, such as Array, Map, and new IR data structures.
204204

docs/arch/runtime.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ The following code block provides an example in C++
5454
5555
#include <tvm/runtime/packed_func.h>
5656
57-
void MyAdd(TVMArgs args, TVMRetValue* rv) {
57+
void MyAdd(ffi::PackedArgs args, ffi::Any* rv) {
5858
// automatically convert arguments to desired type.
5959
int a = args[0].cast<int>();
6060
int b = args[1].cast<int>();
@@ -71,8 +71,8 @@ The following code block provides an example in C++
7171
In the above codeblock, we defined a PackedFunc MyAdd. It takes two arguments
7272
: ``args`` represents input arguments and ``rv`` represents return value.
7373
The function is type-erased, which means that the function signature does not restrict which input type to pass in or type to return.
74-
Under the hood, when we call a PackedFunc, it packs the input arguments to TVMArgs on stack,
75-
and gets the result back via TVMRetValue.
74+
Under the hood, when we call a PackedFunc, it packs the input arguments to ffi::PackedArgs on stack,
75+
and gets the result back via ffi::Any.
7676

7777
Thanks to template tricks in C++, we can call a PackedFunc just like a normal function. Because of its type-erased nature, we can call a PackedFunc from dynamic languages like python, without additional glue code for each new type function created.
7878
The following example registers PackedFunc in C++ and calls from python.
@@ -91,7 +91,7 @@ The following example registers PackedFunc in C++ and calls from python.
9191
# prints 3
9292
print(myadd(1, 2))
9393
94-
Most of the magic of PackedFunc lies in ``TVMArgs`` and ``TVMRetValue`` structure.
94+
Most of the magic of PackedFunc lies in ``ffi::PackedArgs`` and ``ffi::Any`` structure.
9595
We restrict a list of possible types which can be passed.
9696
Here are the common ones:
9797

@@ -111,7 +111,7 @@ we can pass functions from python (as PackedFunc) to C++.
111111
.. code:: c
112112
113113
TVM_REGISTER_GLOBAL("callhello")
114-
.set_body_packed([](TVMArgs args, TVMRetValue* rv) {
114+
.set_body_packed([](ffi::PackedArgs args, ffi::Any* rv) {
115115
PackedFunc f = args[0];
116116
f("hello world");
117117
});

0 commit comments

Comments
 (0)