Skip to content

Commit

Permalink
reflect feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
sunggg committed Mar 28, 2023
1 parent 4594ce4 commit 8dc164e
Show file tree
Hide file tree
Showing 34 changed files with 174 additions and 54 deletions.
38 changes: 23 additions & 15 deletions include/tvm/runtime/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,29 @@ namespace runtime {
/*!
* \brief Property of runtime module
* We classify the property of runtime module into the following categories.
* - kBinarySerializable: we can serialize the module to the stream of bytes. CUDA/OpenCL/JSON
* runtime are representative examples. A binary exportable module can be integrated into final
* runtime artifact by being serialized as data into the artifact, then deserialzied at runtime.
* This class of modules must implement SaveToBinary, and have a matching deserializer registered as
* 'runtime.module.loadbinary_<type_key>'.
* - kRunnable: we can run the module directly. LLVM/CUDA/JSON runtime, executors (e.g,
* virtual machine) runtimes are runnable. Non-runnable modules, such as CSourceModule, requires a
* few extra steps (e.g,. compilation, link) to make it runnable.
* - kDSOExportable: we can export the module as DSO. A DSO exportable module (e.g., a
* CSourceModuleNode of type_key 'c') can be incorporated into the final runtime artifact (ie shared
* library) by compilation and/or linking using the external compiler (llvm, nvcc, etc). DSO
* exportable modules must implement SaveToFile.
*/

enum ModulePropertyMask : int {
/*! \brief kBinarySerializable
* we can serialize the module to the stream of bytes. CUDA/OpenCL/JSON
* runtime are representative examples. A binary exportable module can be integrated into final
* runtime artifact by being serialized as data into the artifact, then deserialized at runtime.
* This class of modules must implement SaveToBinary, and have a matching deserializer registered
* as 'runtime.module.loadbinary_<type_key>'.
*/
kBinarySerializable = 0b001,
/*! \brief kRunnable
* we can run the module directly. LLVM/CUDA/JSON runtime, executors (e.g,
* virtual machine) runtimes are runnable. Non-runnable modules, such as CSourceModule, requires a
* few extra steps (e.g,. compilation, link) to make it runnable.
*/
kRunnable = 0b010,
/*! \brief kDSOExportable
* we can export the module as DSO. A DSO exportable module (e.g., a
* CSourceModuleNode of type_key 'c') can be incorporated into the final runtime artifact (ie
* shared library) by compilation and/or linking using the external compiler (llvm, nvcc, etc).
* DSO exportable modules must implement SaveToFile. In general, DSO exportable modules are not
* runnable unless there is a special support like JIT for `LLVMModule`.
*/
kDSOExportable = 0b100
};

Expand Down Expand Up @@ -220,10 +226,12 @@ class TVM_DLL ModuleNode : public Object {
* By default, none of the property is set. Derived class can override this function and set its
* own property.
*/
virtual int GetProperty() const { return 0b000; }
virtual int GetPropertyMask() const { return 0b000; }

/*! \brief Returns true if this module is 'DSO exportable'. */
bool IsDSOExportable() const { return GetProperty() & ModulePropertyMask::kDSOExportable; };
bool IsDSOExportable() const {
return (GetPropertyMask() & ModulePropertyMask::kDSOExportable) != 0;
}

/*!
* \brief Returns true if this module has a definition for a function of \p name. If
Expand Down
2 changes: 1 addition & 1 deletion include/tvm/runtime/vm/executable.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class TVM_DLL Executable : public ModuleNode {
PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self) final;

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return ModulePropertyMask::kBinarySerializable; };
int GetPropertyMask() const final { return ModulePropertyMask::kBinarySerializable; };

/*!
* \brief Write the Executable to the binary stream in serialized form.
Expand Down
2 changes: 1 addition & 1 deletion include/tvm/runtime/vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class TVM_DLL VirtualMachine : public runtime::ModuleNode {
virtual void LoadExecutable(const ObjectPtr<Executable>& exec);

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return ModulePropertyMask::kRunnable; }
int GetPropertyMask() const final { return ModulePropertyMask::kRunnable; }

protected:
/*! \brief Push a call frame on to the call stack. */
Expand Down
46 changes: 45 additions & 1 deletion python/tvm/runtime/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ def __str__(self):
)


class ModulePropertyMask(object):
"""Runtime Module Property Mask."""

BINARY_SERIALIZABLE = 0b001
RUNNABLE = 0b010
DSO_EXPORTABLE = 0b100


class Module(object):
"""Runtime Module."""

Expand Down Expand Up @@ -239,6 +247,40 @@ def imported_modules(self):
nmod = _ffi_api.ModuleImportsSize(self)
return [_ffi_api.ModuleGetImport(self, i) for i in range(nmod)]

def get_property_mask(self):
"""Get the runtime module property mask. The mapping is stated in ModulePropertyMask.
Returns
-------
mask : int
Bitmask of runtime module property
"""
return _ffi_api.ModuleGetPropertyMask(self)

@property
def is_binary_serializable(self):
"""Returns true if module is 'binary serializable', ie can be serialzed into binary
stream and loaded back to the runtime module.
Returns
-------
b : Bool
True if the module is binary serializable.
"""
return (self.get_property_mask() & ModulePropertyMask.BINARY_SERIALIZABLE) != 0

@property
def is_runnable(self):
"""Returns true if module is 'runnable'. ie can be executed without any extra
compilation/linking steps.
Returns
-------
b : Bool
True if the module is runnable.
"""
return (self.get_property_mask() & ModulePropertyMask.RUNNABLE) != 0

@property
def is_dso_exportable(self):
"""Returns true if module is 'DSO exportable', ie can be included in result of
Expand All @@ -249,7 +291,7 @@ def is_dso_exportable(self):
b : Bool
True if the module is DSO exportable.
"""
return _ffi_api.ModuleIsDSOExportable(self)
return (self.get_property_mask() & ModulePropertyMask.DSO_EXPORTABLE) != 0

def save(self, file_name, fmt=""):
"""Save the module to file.
Expand Down Expand Up @@ -383,6 +425,8 @@ def _collect_from_import_tree(self, filter_func):
stack.append(self)
while stack:
module = stack.pop()
assert module.is_dso_exportable or module.is_binary_serializable

if filter_func(module):
dso_modules.append(module)
for m in module.imported_modules:
Expand Down
4 changes: 2 additions & 2 deletions src/relay/backend/aot_executor_codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1357,9 +1357,9 @@ class AOTExecutorCodegenModule : public runtime::ModuleNode {
}

const char* type_key() const final { return "RelayGraphRuntimeCodegenModule"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return runtime::ModulePropertyMask::kRunnable; }
int GetPropertyMask() const final { return runtime::ModulePropertyMask::kRunnable; }

private:
void init(void* mod, const Array<Target>& targets) {
Expand Down
2 changes: 1 addition & 1 deletion src/relay/backend/build_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class RelayBuildModule : public runtime::ModuleNode {
const char* type_key() const final { return "RelayBuildModule"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return runtime::ModulePropertyMask::kRunnable; }
int GetPropertyMask() const final { return runtime::ModulePropertyMask::kRunnable; }

/*!
* \brief Build relay IRModule for graph executor
Expand Down
2 changes: 1 addition & 1 deletion src/relay/backend/contrib/ethosu/source_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class EthosUModuleNode : public ModuleNode {
}

/*! \brief Get the property of the runtime module .*/
int GetProperty() const { return ModulePropertyMask::kDSOExportable; }
int GetPropertyMask() const { return ModulePropertyMask::kDSOExportable; }

bool ImplementsFunction(const String& name, bool query_imports) final {
return std::find_if(compilation_artifacts_.begin(), compilation_artifacts_.end(),
Expand Down
2 changes: 1 addition & 1 deletion src/relay/backend/graph_executor_codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ class GraphExecutorCodegenModule : public runtime::ModuleNode {
const char* type_key() const final { return "RelayGraphExecutorCodegenModule"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return runtime::ModulePropertyMask::kRunnable; }
int GetPropertyMask() const final { return runtime::ModulePropertyMask::kRunnable; }

private:
std::shared_ptr<GraphExecutorCodegen> codegen_;
Expand Down
2 changes: 1 addition & 1 deletion src/relay/backend/vm/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class VMCompiler : public runtime::ModuleNode {
const char* type_key() const final { return "VMCompiler"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return ModulePropertyMask::kRunnable; }
int GetPropertyMask() const final { return ModulePropertyMask::kRunnable; }

/*!
* \brief Set the parameters
Expand Down
2 changes: 1 addition & 1 deletion src/relay/printer/model_library_format_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ModelLibraryFormatPrinter : public ::tvm::runtime::ModuleNode {
const char* type_key() const final { return "model_library_format_printer"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return runtime::ModulePropertyMask::kRunnable; }
int GetPropertyMask() const final { return runtime::ModulePropertyMask::kRunnable; }

std::string Print(const ObjectRef& node) {
std::ostringstream oss;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/aot_executor/aot_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class TVM_DLL AotExecutor : public ModuleNode {
const char* type_key() const final { return "AotExecutor"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return ModulePropertyMask::kRunnable; }
int GetPropertyMask() const final { return ModulePropertyMask::kRunnable; }

void Run();

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/aot_executor/aot_executor_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class TVM_DLL AotExecutorFactory : public runtime::ModuleNode {
const char* type_key() const final { return "AotExecutorFactory"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return ModulePropertyMask::kBinarySerializable; }
int GetPropertyMask() const final { return ModulePropertyMask::kBinarySerializable; }

/*!
* \brief Save the module to binary stream.
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/const_loader_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class ConstLoaderModuleNode : public ModuleNode {
const char* type_key() const final { return "const_loader"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return ModulePropertyMask::kBinarySerializable; };
int GetPropertyMask() const final { return ModulePropertyMask::kBinarySerializable; };

/*!
* \brief Get the list of constants that is required by the given module.
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/contrib/coreml/coreml_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ class CoreMLRuntime : public ModuleNode {
virtual PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self);

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return ModulePropertyMask::kBinarySerializable | ModulePropertyMask::kRunnable; };
int GetPropertyMask() const final {
return ModulePropertyMask::kBinarySerializable | ModulePropertyMask::kRunnable;
}

/*!
* \brief Serialize the content of the mlmodelc directory and save it to
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/contrib/json/json_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ class JSONRuntimeBase : public ModuleNode {
const char* type_key() const override { return "json"; } // May be overridden

/*! \brief Get the property of the runtime module .*/
int GetProperty() const { return ModulePropertyMask::kBinarySerializable | ModulePropertyMask::kRunnable; }
int GetPropertyMask() const {
return ModulePropertyMask::kBinarySerializable | ModulePropertyMask::kRunnable;
}

/*! \brief Initialize a specific json runtime. */
virtual void Init(const Array<NDArray>& consts) = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/contrib/libtorch/libtorch_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ class TorchModuleNode : public ModuleNode {

const char* type_key() const { return "torch"; }
/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return ModulePropertyMask::kBinarySerializable | ModulePropertyMask::kRunnable; };
int GetPropertyMask() const final {
return ModulePropertyMask::kBinarySerializable | ModulePropertyMask::kRunnable;
}

/*!
* \brief Get a packed function.
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/contrib/onnx/onnx_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ONNXSourceModuleNode : public runtime::ModuleNode {
const char* type_key() const { return "onnx"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return ModulePropertyMask::kRunnable; };
int GetPropertyMask() const final { return ModulePropertyMask::kRunnable; };

PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self) final {
if (name == "get_symbol") {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/contrib/tensorrt/tensorrt_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class TensorRTRuntime : public JSONRuntimeBase {
const char* type_key() const final { return "tensorrt"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final {
int GetPropertyMask() const final {
return ModulePropertyMask::kBinarySerializable | ModulePropertyMask::kRunnable;
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/contrib/tflite/tflite_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class TFLiteRuntime : public ModuleNode {
const char* type_key() const { return "TFLiteRuntime"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return ModulePropertyMask::kRunnable; };
int GetPropertyMask() const final { return ModulePropertyMask::kRunnable; };

/*!
* \brief Invoke the internal tflite interpreter and run the whole model in
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/contrib/vitis_ai/vitis_ai_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class VitisAIRuntime : public ModuleNode {
const char* type_key() const { return "VitisAIRuntime"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final {
int GetPropertyMask() const final {
return ModulePropertyMask::kBinarySerializable | ModulePropertyMask::kRunnable;
};

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/cuda/cuda_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CUDAModuleNode : public runtime::ModuleNode {
const char* type_key() const final { return "cuda"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final {
int GetPropertyMask() const final {
return ModulePropertyMask::kBinarySerializable | ModulePropertyMask::kRunnable;
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/graph_executor/graph_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class TVM_DLL GraphExecutor : public ModuleNode {
void Run();

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return ModulePropertyMask::kRunnable; }
int GetPropertyMask() const final { return ModulePropertyMask::kRunnable; }

/*!
* \brief Initialize the graph executor with graph and device.
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/graph_executor/graph_executor_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class TVM_DLL GraphExecutorFactory : public runtime::ModuleNode {
const char* type_key() const final { return "GraphExecutorFactory"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return ModulePropertyMask::kBinarySerializable; }
int GetPropertyMask() const final { return ModulePropertyMask::kBinarySerializable; }

/*!
* \brief Save the module to binary stream.
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/hexagon/hexagon_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class HexagonModuleNode : public runtime::ModuleNode {
std::string GetSource(const std::string& format) override;
const char* type_key() const final { return "hexagon"; }
/*! \brief Get the property of the runtime module .*/
int GetProperty() const {
int GetPropertyMask() const {
return ModulePropertyMask::kBinarySerializable | ModulePropertyMask::kRunnable;
};
}
void SaveToFile(const std::string& file_name, const std::string& format) override;
void SaveToBinary(dmlc::Stream* stream) override;

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/library_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class LibraryModuleNode final : public ModuleNode {
: lib_(lib), packed_func_wrapper_(wrapper) {}

const char* type_key() const final { return "library"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final {
int GetPropertyMask() const final {
return ModulePropertyMask::kBinarySerializable | ModulePropertyMask::kRunnable;
};

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class MetadataModuleNode : public ::tvm::runtime::ModuleNode {
const char* type_key() const final { return "metadata_module"; }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return ModulePropertyMask::kBinarySerializable; }
int GetPropertyMask() const final { return ModulePropertyMask::kBinarySerializable; }

static Module LoadFromBinary() {
return Module(make_object<MetadataModuleNode>(runtime::metadata::Metadata()));
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ TVM_REGISTER_GLOBAL("runtime.ModuleLoadFromFile").set_body_typed(Module::LoadFro
TVM_REGISTER_GLOBAL("runtime.ModuleSaveToFile")
.set_body_typed([](Module mod, String name, tvm::String fmt) { mod->SaveToFile(name, fmt); });

TVM_REGISTER_GLOBAL("runtime.ModuleIsDSOExportable").set_body_typed([](Module mod) {
return mod->IsDSOExportable();
TVM_REGISTER_GLOBAL("runtime.ModuleGetPropertyMask").set_body_typed([](Module mod) {
return mod->GetPropertyMask();
});

TVM_REGISTER_GLOBAL("runtime.ModuleImplementsFunction")
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/opencl/opencl_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ class OpenCLModuleNode : public ModuleNode {
const char* type_key() const final { return workspace_->type_key.c_str(); }

/*! \brief Get the property of the runtime module .*/
int GetProperty() const final {
int GetPropertyMask() const final {
return ModulePropertyMask::kBinarySerializable | ModulePropertyMask::kRunnable;
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/rpc/rpc_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class RPCModuleNode final : public ModuleNode {

const char* type_key() const final { return "rpc"; }
/*! \brief Get the property of the runtime module .*/
int GetProperty() const final { return ModulePropertyMask::kRunnable; }
int GetPropertyMask() const final { return ModulePropertyMask::kRunnable; }

PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self) final {
if (name == "CloseRPCConnection") {
Expand Down
Loading

0 comments on commit 8dc164e

Please sign in to comment.