tqchen commented on code in PR #331:
URL: https://github.com/apache/tvm-ffi/pull/331#discussion_r2610404284
##########
include/tvm/ffi/extra/module.h:
##########
@@ -262,6 +278,13 @@ class Module : public ObjectRef {
* Re-create import relationship by calling Import.
*/
TVM_FFI_EXTRA_CXX_API static Module LoadFromFile(const String& file_name);
+ /*!
+ * \brief Load a module from bytes.
+ * \param kind The kind of the module.
+ * \param bytes The bytes we want to load the module from.
+ * \return The loaded module.
+ */
+ TVM_FFI_EXTRA_CXX_API static Module LoadFromBytes(const std::string& kind,
const Bytes& bytes);
Review Comment:
this is a low level API that is incompelete(module needs assemble bytes, so
we deliberately not exposing them
##########
python/tvm_ffi/module.py:
##########
@@ -374,6 +374,77 @@ def write_to_file(self, file_name: str, fmt: str = "") ->
None:
"""
_ffi_api.ModuleWriteToFile(self, file_name, fmt)
+ def save_to_bytes(self) -> bytes:
Review Comment:
avoid exposing save_to_bytes and load_from_bytes as they are low-level API,
the standalone use may have issue as they do not preserve import trees
##########
include/tvm/ffi/extra/module.h:
##########
@@ -176,6 +176,22 @@ class TVM_FFI_EXTRA_CXX_API ModuleObj : public Object {
* \note Note the signature is not part of the public API.
*/
const Array<Any>& imports() const { return this->imports_; }
+ /*!
+ * \brief For unloadable libraries, if `keep_alive` is set to true, the
library will not be
+ * unloaded in its destructor; otherwise, it will be unloaded when the
library is destructed.
+ * \param keep_alive Whether to keep the library alive.
+ * \note This function is a no-op for libraries that do not support
unloading.
+ * \sa Close
+ */
+ virtual void SetKeepAlive(bool keep_alive) {}
Review Comment:
alternatively, we can expose a function specifically for DSO module
##########
src/ffi/extra/module.cc:
##########
@@ -107,6 +107,17 @@ bool ModuleObj::ImplementsFunction(const String& name,
bool query_imports) {
return false;
}
+Module Module::LoadFromBytes(const std::string& kind, const Bytes& bytes) {
+ std::string loader_key = "ffi.Module.load_from_bytes." + kind;
Review Comment:
avoid exposing this as it is low-level API auto-triggered by load library,
we need to assemble multiple binaries to completely loads
##########
include/tvm/ffi/extra/module.h:
##########
@@ -176,6 +176,22 @@ class TVM_FFI_EXTRA_CXX_API ModuleObj : public Object {
* \note Note the signature is not part of the public API.
*/
const Array<Any>& imports() const { return this->imports_; }
+ /*!
+ * \brief For unloadable libraries, if `keep_alive` is set to true, the
library will not be
+ * unloaded in its destructor; otherwise, it will be unloaded when the
library is destructed.
+ * \param keep_alive Whether to keep the library alive.
+ * \note This function is a no-op for libraries that do not support
unloading.
+ * \sa Close
+ */
+ virtual void SetKeepAlive(bool keep_alive) {}
Review Comment:
avoid changing the module interface, another alternative is to expose a new
module global functions like
```c++
static Module::KeepAliveAtGlobal(mod);
static Module::RemoveKeepAliveAtGlobal(mod);
```
and expose into the python. This way keeps the module interface clean, and
still gives same effect, as they are kept alive in libtvm_ffi that all apps
will link to
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]