Skip to content

Conversation

@kparzysz-quic
Copy link
Contributor

@kparzysz-quic kparzysz-quic commented Sep 4, 2023

When a module with imported modules is exported into a shared library, the imported modules are serialized and embedded inside of that library. This is done by generating a raw binary from the imported modules, which is then assigned to a symbol __tvm_dev_mblob in the final shared library.

The way it happens for targets that are not "llvm", is by creating a C source file, and defining a statically-initialized array __tvm_dev_mblob in it. The static initializer is the byte-by-byte hexadecimal representation of the serialized modules. While working with Hexagon, this has presented us with two issues:

  1. For most models, the embedded data is very large, taking significant amout of time to compile the auto-generated C source.
  2. There are some models for which the C source size has exceeded clang's limits on the size of the input file, making it impossible to export the corresponding module.

This PR allows users to provide a custom serialization routine to Module.export_library. We then apply it in Hexagon to build the ELF object file with __tvm_dev_mblob by using the objcopy tool from the Hexagon toolchain. This bypasses the C compilation altogether, avoiding both of the issues.

Using the same custom mechanism, similar method can be implemented for many other targets which have utilities to manipulate object files directly.

When a module with imported modules is exported into a shared library,
the imported modules are serialized and embedded inside of that library.
This is done by generating a raw binary from the imported modules, which
is then assigned to a symbol `__tvm_dev_mblob` in the final shared
library.

The way it happens for targets that are not "llvm", is by creating a C
source file, and defining a statically-initialized array `__tvm_dev_mblob`
in it. The static initializer is the byte-by-byte hexadecimal represen-
tation of the serialized modules. While working with Hexagon, this has
presented us with two issues:
1. For most models, the embedded data is very large, taking significant
amout of time to compile the auto-generated C source.
2. There are some models for which the C source size has exceeded clang's
limits on the size of the input file, making it impossible to export the
corresponding module.

This PR allows users to provide a custom serialization routine to
`Module.export_library`. We then apply it in Hexagon to build the ELF
object file with `__tvm_dev_mblob` by using the objcopy tool from the
Hexagon toolchain. This bypasses the C compilation altogether, avoiding
both of the issues.

Using the same custom mechanism, similar method can be implemented for
many other targets which have utilities to manipulate object files
directly.
It seems like older toolchains may work as well, but the oldest supported
SDK is 4.5.0.3, which contains toolchain version 8.5.08.
@tqchen tqchen merged commit 35164b3 into apache:main Sep 6, 2023
@kparzysz-quic kparzysz-quic deleted the custom-serialize branch September 6, 2023 13:56
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.

2 participants