Created
December 19, 2018 17:14
-
-
Save ruslo/c7eb1f2afc2acfc3d8297dbe5da3c4b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*! | |
| * Copyright (c) 2017 by Contributors | |
| * \brief Example code on load and run TVM module.s | |
| * \file cpp_deploy_example.cc | |
| */ | |
| #include <cstdio> | |
| #include <dlpack/dlpack.h> | |
| #include <tvm/runtime/module.h> | |
| #include <tvm/runtime/registry.h> | |
| #include <tvm/runtime/packed_func.h> | |
| void Verify(tvm::runtime::Module mod, std::string fname) { | |
| // Get the function from the module. | |
| tvm::runtime::PackedFunc f = mod.GetFunction(fname); | |
| CHECK(f != nullptr); | |
| // Allocate the DLPack data structures. | |
| // | |
| // Note that we use TVM runtime API to allocate the DLTensor in this example. | |
| // TVM accept DLPack compatible DLTensors, so function can be invoked | |
| // as long as we pass correct pointer to DLTensor array. | |
| // | |
| // For more information please refer to dlpack. | |
| // One thing to notice is that DLPack contains alignment requirement for | |
| // the data pointer and TVM takes advantage of that. | |
| // If you plan to use your customized data container, please | |
| // make sure the DLTensor you pass in meet the alignment requirement. | |
| // | |
| DLTensor* x; | |
| DLTensor* y; | |
| int ndim = 1; | |
| int dtype_code = kDLFloat; | |
| int dtype_bits = 32; | |
| int dtype_lanes = 1; | |
| int device_type = kDLOpenCL; | |
| int device_id = 0; | |
| int64_t shape[1] = {10}; | |
| TVMArrayAlloc(shape, ndim, dtype_code, dtype_bits, dtype_lanes, | |
| device_type, device_id, &x); | |
| TVMArrayAlloc(shape, ndim, dtype_code, dtype_bits, dtype_lanes, | |
| device_type, device_id, &y); | |
| for (int i = 0; i < shape[0]; ++i) { | |
| static_cast<float*>(x->data)[i] = i; | |
| } | |
| // Invoke the function | |
| // PackedFunc is a function that can be invoked via positional argument. | |
| // The signature of the function is specified in tvm.build | |
| f(x, y); | |
| // Print out the output | |
| for (int i = 0; i < shape[0]; ++i) { | |
| CHECK_EQ(static_cast<float*>(y->data)[i], i + 1.0f); | |
| } | |
| LOG(INFO) << "Finish verification..."; | |
| } | |
| int main(void) { | |
| // For libraries that are directly packed as system lib and linked together with the app | |
| // We can directly use GetSystemLib to get the system wide library. | |
| LOG(INFO) << "Verify load function from system lib"; | |
| tvm::runtime::Module mod_syslib = (*tvm::runtime::Registry::Get("module._GetSystemLib"))(); | |
| Verify(mod_syslib, "addonesys"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment