Skip to content

Commit 8dc164e

Browse files
committed
reflect feedback
1 parent 4594ce4 commit 8dc164e

34 files changed

+174
-54
lines changed

include/tvm/runtime/module.h

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,29 @@ namespace runtime {
4444
/*!
4545
* \brief Property of runtime module
4646
* We classify the property of runtime module into the following categories.
47-
* - kBinarySerializable: we can serialize the module to the stream of bytes. CUDA/OpenCL/JSON
48-
* runtime are representative examples. A binary exportable module can be integrated into final
49-
* runtime artifact by being serialized as data into the artifact, then deserialzied at runtime.
50-
* This class of modules must implement SaveToBinary, and have a matching deserializer registered as
51-
* 'runtime.module.loadbinary_<type_key>'.
52-
* - kRunnable: we can run the module directly. LLVM/CUDA/JSON runtime, executors (e.g,
53-
* virtual machine) runtimes are runnable. Non-runnable modules, such as CSourceModule, requires a
54-
* few extra steps (e.g,. compilation, link) to make it runnable.
55-
* - kDSOExportable: we can export the module as DSO. A DSO exportable module (e.g., a
56-
* CSourceModuleNode of type_key 'c') can be incorporated into the final runtime artifact (ie shared
57-
* library) by compilation and/or linking using the external compiler (llvm, nvcc, etc). DSO
58-
* exportable modules must implement SaveToFile.
5947
*/
60-
6148
enum ModulePropertyMask : int {
49+
/*! \brief kBinarySerializable
50+
* we can serialize the module to the stream of bytes. CUDA/OpenCL/JSON
51+
* runtime are representative examples. A binary exportable module can be integrated into final
52+
* runtime artifact by being serialized as data into the artifact, then deserialized at runtime.
53+
* This class of modules must implement SaveToBinary, and have a matching deserializer registered
54+
* as 'runtime.module.loadbinary_<type_key>'.
55+
*/
6256
kBinarySerializable = 0b001,
57+
/*! \brief kRunnable
58+
* we can run the module directly. LLVM/CUDA/JSON runtime, executors (e.g,
59+
* virtual machine) runtimes are runnable. Non-runnable modules, such as CSourceModule, requires a
60+
* few extra steps (e.g,. compilation, link) to make it runnable.
61+
*/
6362
kRunnable = 0b010,
63+
/*! \brief kDSOExportable
64+
* we can export the module as DSO. A DSO exportable module (e.g., a
65+
* CSourceModuleNode of type_key 'c') can be incorporated into the final runtime artifact (ie
66+
* shared library) by compilation and/or linking using the external compiler (llvm, nvcc, etc).
67+
* DSO exportable modules must implement SaveToFile. In general, DSO exportable modules are not
68+
* runnable unless there is a special support like JIT for `LLVMModule`.
69+
*/
6470
kDSOExportable = 0b100
6571
};
6672

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

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

228236
/*!
229237
* \brief Returns true if this module has a definition for a function of \p name. If

include/tvm/runtime/vm/executable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class TVM_DLL Executable : public ModuleNode {
6767
PackedFunc GetFunction(const std::string& name, const ObjectPtr<Object>& sptr_to_self) final;
6868

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

7272
/*!
7373
* \brief Write the Executable to the binary stream in serialized form.

include/tvm/runtime/vm/vm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class TVM_DLL VirtualMachine : public runtime::ModuleNode {
179179
virtual void LoadExecutable(const ObjectPtr<Executable>& exec);
180180

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

184184
protected:
185185
/*! \brief Push a call frame on to the call stack. */

python/tvm/runtime/module.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ def __str__(self):
9696
)
9797

9898

99+
class ModulePropertyMask(object):
100+
"""Runtime Module Property Mask."""
101+
102+
BINARY_SERIALIZABLE = 0b001
103+
RUNNABLE = 0b010
104+
DSO_EXPORTABLE = 0b100
105+
106+
99107
class Module(object):
100108
"""Runtime Module."""
101109

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

250+
def get_property_mask(self):
251+
"""Get the runtime module property mask. The mapping is stated in ModulePropertyMask.
252+
253+
Returns
254+
-------
255+
mask : int
256+
Bitmask of runtime module property
257+
"""
258+
return _ffi_api.ModuleGetPropertyMask(self)
259+
260+
@property
261+
def is_binary_serializable(self):
262+
"""Returns true if module is 'binary serializable', ie can be serialzed into binary
263+
stream and loaded back to the runtime module.
264+
265+
Returns
266+
-------
267+
b : Bool
268+
True if the module is binary serializable.
269+
"""
270+
return (self.get_property_mask() & ModulePropertyMask.BINARY_SERIALIZABLE) != 0
271+
272+
@property
273+
def is_runnable(self):
274+
"""Returns true if module is 'runnable'. ie can be executed without any extra
275+
compilation/linking steps.
276+
277+
Returns
278+
-------
279+
b : Bool
280+
True if the module is runnable.
281+
"""
282+
return (self.get_property_mask() & ModulePropertyMask.RUNNABLE) != 0
283+
242284
@property
243285
def is_dso_exportable(self):
244286
"""Returns true if module is 'DSO exportable', ie can be included in result of
@@ -249,7 +291,7 @@ def is_dso_exportable(self):
249291
b : Bool
250292
True if the module is DSO exportable.
251293
"""
252-
return _ffi_api.ModuleIsDSOExportable(self)
294+
return (self.get_property_mask() & ModulePropertyMask.DSO_EXPORTABLE) != 0
253295

254296
def save(self, file_name, fmt=""):
255297
"""Save the module to file.
@@ -383,6 +425,8 @@ def _collect_from_import_tree(self, filter_func):
383425
stack.append(self)
384426
while stack:
385427
module = stack.pop()
428+
assert module.is_dso_exportable or module.is_binary_serializable
429+
386430
if filter_func(module):
387431
dso_modules.append(module)
388432
for m in module.imported_modules:

src/relay/backend/aot_executor_codegen.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,9 +1357,9 @@ class AOTExecutorCodegenModule : public runtime::ModuleNode {
13571357
}
13581358

13591359
const char* type_key() const final { return "RelayGraphRuntimeCodegenModule"; }
1360-
1360+
13611361
/*! \brief Get the property of the runtime module .*/
1362-
int GetProperty() const final { return runtime::ModulePropertyMask::kRunnable; }
1362+
int GetPropertyMask() const final { return runtime::ModulePropertyMask::kRunnable; }
13631363

13641364
private:
13651365
void init(void* mod, const Array<Target>& targets) {

src/relay/backend/build_module.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class RelayBuildModule : public runtime::ModuleNode {
284284
const char* type_key() const final { return "RelayBuildModule"; }
285285

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

289289
/*!
290290
* \brief Build relay IRModule for graph executor

src/relay/backend/contrib/ethosu/source_module.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class EthosUModuleNode : public ModuleNode {
122122
}
123123

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

127127
bool ImplementsFunction(const String& name, bool query_imports) final {
128128
return std::find_if(compilation_artifacts_.begin(), compilation_artifacts_.end(),

src/relay/backend/graph_executor_codegen.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ class GraphExecutorCodegenModule : public runtime::ModuleNode {
688688
const char* type_key() const final { return "RelayGraphExecutorCodegenModule"; }
689689

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

693693
private:
694694
std::shared_ptr<GraphExecutorCodegen> codegen_;

src/relay/backend/vm/compiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class VMCompiler : public runtime::ModuleNode {
9494
const char* type_key() const final { return "VMCompiler"; }
9595

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

9999
/*!
100100
* \brief Set the parameters

src/relay/printer/model_library_format_printer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ModelLibraryFormatPrinter : public ::tvm::runtime::ModuleNode {
3838
const char* type_key() const final { return "model_library_format_printer"; }
3939

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

4343
std::string Print(const ObjectRef& node) {
4444
std::ostringstream oss;

0 commit comments

Comments
 (0)