-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension #9243
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
Conversation
…y fix failing tests I realized that the module format `cc` is currently already used by the `CSourceCrtMetadataModuleNode` declared in `src/target/source/source_module.cc`. This needs to be discussed first to decide if either the module_key should be changed or the test cases expecting the systemlib kernel (e.g. `default_lib0.c`) to have a `.c` extension.
…extensions AOT: Add c++ support to aot_test.mk AOT: Add c++ support to corstone300.mk
|
Ethosu and CMSIS-NN related builds seem to fail in CI, I will look into this and follow up with a fix for that. |
huajsj
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not see any UT logic, may need a test to explain the use case.
python/tvm/runtime/module.py
Outdated
| if fcompile is not None and hasattr(fcompile, "object_format"): | ||
| if module.type_key == "c": | ||
| object_format = "c" | ||
| assert module.format in ["c", "cc", "cpp"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert with error message?
|
Thank you for your review @huajsj. I will try to apply those patches soon. Regarding unit tests, to did not want to introduce a new but otherwise useless BYOC codegen just for testing purposes. I will try to come up with something as simple as possible to test to changes to the SourceModule export. Eventuelly I will also figure out how include the special case |
|
To fix the CI run i will very likely need to rebase on top of |
|
@PhilippvK yeah feel free to merge main. The github workflow is really confusing to me--basically what I do (mostly) is rebase and force-push until my PRs receive a comment; the minute they receive a comment, I merge instead. Then GitHub isn't allowed to garbage collect the old pushes. |
|
Sorry for the CI bugs. I am working on them. |
|
Sorry for not following up on this. While the CI bugs have been fixed in the meantime, there are still 3 points open for discussion:
|
areusch
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PhilippvK sorry I missed this! one question on the approach here.
| """Get the format of the module.""" | ||
| return _ffi_api.ModuleGetFormat(self) | ||
|
|
||
| def get_source(self, fmt=""): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think Module can sometimes have multiple formats (e.g. CUDA modules do this I believe). what about leveraging this and modifying export_module to try with fmt="c" here and if not also try fmt="cc"?
|
@areusch Sorry but I do not really understand what you mean. Could you tell me how this helps solving my mentioned "issues"? |
|
@PhilippvK please resolve the conflict. |
…sources which require a .cpp/.cc file extension (apache#9243) * Allow export of C++ kernels using correct file extension * [WIP] Set module_key=c for CSourceCrtMetadataModuleNode to temporarily fix failing tests I realized that the module format `cc` is currently already used by the `CSourceCrtMetadataModuleNode` declared in `src/target/source/source_module.cc`. This needs to be discussed first to decide if either the module_key should be changed or the test cases expecting the systemlib kernel (e.g. `default_lib0.c`) to have a `.c` extension. * Update Makefiles used by tests/python/relay/aot/ to support C++ file extensions AOT: Add c++ support to aot_test.mk AOT: Add c++ support to corstone300.mk * Add missing definition of GetFormat to cmsisnn and ethosn codegens (WIP) * Resolve PR comments * lint python/tvm/runtime/module.py * fix EthosUModuleNode for CI * Fix: detect empty module.format * Add error message to assertion * Lint python/tvm/runtime/module.py
…sources which require a .cpp/.cc file extension (apache#9243) * Allow export of C++ kernels using correct file extension * [WIP] Set module_key=c for CSourceCrtMetadataModuleNode to temporarily fix failing tests I realized that the module format `cc` is currently already used by the `CSourceCrtMetadataModuleNode` declared in `src/target/source/source_module.cc`. This needs to be discussed first to decide if either the module_key should be changed or the test cases expecting the systemlib kernel (e.g. `default_lib0.c`) to have a `.c` extension. * Update Makefiles used by tests/python/relay/aot/ to support C++ file extensions AOT: Add c++ support to aot_test.mk AOT: Add c++ support to corstone300.mk * Add missing definition of GetFormat to cmsisnn and ethosn codegens (WIP) * Resolve PR comments * lint python/tvm/runtime/module.py * fix EthosUModuleNode for CI * Fix: detect empty module.format * Add error message to assertion * Lint python/tvm/runtime/module.py
…sources which require a .cpp/.cc file extension (apache#9243) * Allow export of C++ kernels using correct file extension * [WIP] Set module_key=c for CSourceCrtMetadataModuleNode to temporarily fix failing tests I realized that the module format `cc` is currently already used by the `CSourceCrtMetadataModuleNode` declared in `src/target/source/source_module.cc`. This needs to be discussed first to decide if either the module_key should be changed or the test cases expecting the systemlib kernel (e.g. `default_lib0.c`) to have a `.c` extension. * Update Makefiles used by tests/python/relay/aot/ to support C++ file extensions AOT: Add c++ support to aot_test.mk AOT: Add c++ support to corstone300.mk * Add missing definition of GetFormat to cmsisnn and ethosn codegens (WIP) * Resolve PR comments * lint python/tvm/runtime/module.py * fix EthosUModuleNode for CI * Fix: detect empty module.format * Add error message to assertion * Lint python/tvm/runtime/module.py
Motivation
I ran into the issue that I need to use C++ features in a BYOC generated kernel which is tricky to achieve because those are typically exported as
.cfiles.I am aware of the possibility to force
gcc/g++to threat all input files as C++ but this solves the issue only partially and is not really intuitive. Renaming the file names e.g. changing the file extension manually after the export via MLF is if also a workaround which does not work out for me.Solution(s):
I came up with two rather simple solutions to solutions to solve the explained limitation in the ability to choose the correct file extension for kernel files written by a custom codegen and implemented the first one in this PR because it should add minimal complexity to the TVM codebase which might be desirable:
Each
CSourceModuleNodealready has aformatfield which can be set in the constructor. Currently it can not be accessed by other classes but this can be easily solved by adding a getter and the respective python hooks for that property. This information can then be accessed by i.e.model_library_format.pyto determine the file extension for the exported kernel. It would also be possible to use this interface for other types of modules in the future.Introduce a new class
CppSourceModuleNodewhich is essentially a copy ofCSourceModulewith the only difference being the the themodule_keywhich may then be used to choose a file extension in the aforementioned python functions. This sounds like much redundant code to me and also has less flexibility compared to option one as we would have to stick with either a.ccor a.cppfile extension for every modules of that type.Open questions/thoughts:
I realized that the module format
ccis currently already used by theCSourceCrtMetadataModuleNodedeclared insource_module.cc:This does not really make sense to me and especially makes some AoT and Model Library Format related pytests fail because the metadata module now being exported as
.ccinstead of.cas explaind the the previous paragraph. Let me know if I should add the fix these Makefiles to this PR and keep the module format for theCSourceCrtMetadataModuleNodedefined asccor if should work around this issue instead by changing the module format of that file toc?I did not yet come up with new test cases which exercise the introduced changes to the MLF export as this would very likely result in a need to add a new dummy BYOC module which uses the
ccformat instead. During development I just patched the examplecodegen_cto export C++ kernels instead which worked fine,I decided against an RFC for this discussion as only a couple of lines are added to the TVM codebase to realize this feature. Let me know if I should make this a (Pre-)RFC instead. Otherwise I would be happy for any feedback and answers to the remaining questions introduced in the last section.
CC @areusch @Mousius @giuseros @manupa-arm