Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Runtime] Introduce runtime module property #14406

Merged
merged 8 commits into from
Mar 30, 2023

Conversation

sunggg
Copy link
Contributor

@sunggg sunggg commented Mar 26, 2023

Currently, we only classify whether the runtime module is DSO-exportable.

This PR further classifies each runtime module into the properties as follows:

  • kBinarySerializable: we can serialize the module to the stream of bytes. CUDA/OpenCL/JSON runtime are representative examples.
  • 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.
  • kBinaryExportable: when the module is kBinarySerializable and kRunnable, we consider this module as binary exportable. 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>.
  • 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.

Please note that kDSOExportable is a mutual exclusive property with kBinaryExportable.

To represent each category, the following status bits are defined.

namespace property {
/*! \brief Binary serializable runtime module */
constexpr const uint8_t kBinarySerializable = 0b001;
/*! \brief Runnable runtime module */
constexpr const uint8_t kRunnable = 0b010;
/*! \brief DSO exportable runtime module */
constexpr const uint8_t kDSOExportable = 0b100;
/*! \brief Binary exportable runtime module */
constexpr const uint8_t kBinaryExportable = kBinarySerializable | kRunnable;
};  // namespace property

Edit based on the review

This PR further classifies each runtime module into the properties as follows:

  • 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>.
  • 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. In general, DSO exportable modules are not runnable unless there is a special support like JIT for LLVMModule.

To represent each category, the following status bits are defined.

enum ModulePropertyMask : int {
  kBinarySerializable = 0b001,
  kRunnable = 0b010,
  kDSOExportable = 0b100
};

cc. @tqchen

@tvm-bot
Copy link
Collaborator

tvm-bot commented Mar 26, 2023

Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.

Generated by tvm-bot

@sunggg sunggg changed the title [Runtime] Introduce runtime module property [WIP][Runtime] Introduce runtime module property Mar 26, 2023
*/
namespace property {
/*! \brief Binary serializable runtime module */
constexpr const uint8_t kBinarySerializable = 0b001;
Copy link
Member

@tqchen tqchen Mar 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let us use int to keep things simple, make it an enum

enum class ModulePropertyMask : int

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used enum ModulePropertyMask : int instead for implicit casting.

@sunggg sunggg changed the title [WIP][Runtime] Introduce runtime module property [Runtime] Introduce runtime module property Mar 27, 2023
Copy link
Contributor

@echuraev echuraev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR. I have one question. Where is it planned to use these properties? In this PR I see only adding these properties but no use-cases. Probably a simple unit test will be helpful to understand what should be achieved after enabling these properties.

@tqchen
Copy link
Member

tqchen commented Mar 27, 2023

I think a good first step is to replace the current usage of the IsDSOExportable in the module.export_library using the flag, and report error when we encounter something that is neither DSOExportable or BinarySerializable

@sunggg
Copy link
Contributor Author

sunggg commented Mar 27, 2023

Thank you, @echuraev for the review. As @tqchen pointed out, this PR changes the way we achieve the same functionality but with a more refined manner. This PR was motivated by the discussion in my outstanding PR #14337 about roundtrip-abilty and with this change, we can offer more systematic error message when we cannot guarantee this.

I initially thought passing the existing tests would be sufficient since it does not introduce any new behavior or feature. But, if you think it is still good to have a unit test about it, I can definitely add one. :)

@junrushao
Copy link
Member

This is a really comprehensive summary and I believe it will generally be quite useful to anyone (including me) who wants to understand runtime.Module in depth. @sunggg @tqchen shall we put it somewhere in the official tutorial?

@echuraev
Copy link
Contributor

@sunggg Thank you for your reply! It is a nice summary and the reason why we did it starts to be much clearer for me. I'm ok with current tests but agree with @junrushao that creating some documentation might be very useful.

include/tvm/runtime/module.h Outdated Show resolved Hide resolved
include/tvm/runtime/module.h Outdated Show resolved Hide resolved
@sunggg
Copy link
Contributor Author

sunggg commented Mar 28, 2023

@junrushao @echuraev Thank you for the suggestion. Addition to the official tutorial also sounds great to me.

By reflecting @tqchen's suggestion, I also added some unit tests and assertion in export_library.
Hope this helps!

@sunggg sunggg force-pushed the runtime_module_property branch from 383be34 to 8dc164e Compare March 28, 2023 22:33
@mehrdadh
Copy link
Member

@tvm-bot rerun

Copy link
Contributor

@echuraev echuraev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@tqchen tqchen merged commit 7b34a6e into apache:main Mar 30, 2023
MasterJH5574 added a commit to MasterJH5574/tvm that referenced this pull request Apr 6, 2023
Following apache#14406, this PR adds the runtime module property mask for
Metal and Vulkan backend, which were left before.
MasterJH5574 added a commit to MasterJH5574/tvm that referenced this pull request Apr 7, 2023
Following apache#14406, this PR adds the runtime module property mask for
Metal and Vulkan backend, which were left before.
tqchen pushed a commit that referenced this pull request Apr 7, 2023
Following #14406, this PR adds the runtime module property mask for
Metal and Vulkan backend, which were left before.
jinhongyii pushed a commit to jinhongyii/tvm that referenced this pull request Apr 10, 2023
…kan (apache#14524) (apache#180)

Following apache#14406, this PR adds the runtime module property mask for
Metal and Vulkan backend, which were left before.
tqchen pushed a commit to tqchen/tvm that referenced this pull request Apr 11, 2023
…kan (apache#14524) (apache#180)

Following apache#14406, this PR adds the runtime module property mask for
Metal and Vulkan backend, which were left before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants