You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2021/10/11 08:41:53 UTC

[GitHub] [tvm] PhilippvK opened a new pull request #9243: Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension

PhilippvK opened a new pull request #9243:
URL: https://github.com/apache/tvm/pull/9243


   ## 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 `.c` files.
   
   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:
   
   1. Each `CSourceModuleNode` already has a `format` field 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.py`](https://github.com/apache/tvm/blob/main/python/tvm/micro/model_library_format.py) to 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.
   
   2. Introduce a new class `CppSourceModuleNode` which is essentially a copy of `CSourceModule` with the only difference being the the the `module_key` which 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 `.cc` or a `.cpp` file extension for every modules of that type.
   
   
   ### Open questions/thoughts:
   
   I realized that the module format `cc` is currently already used by the `CSourceCrtMetadataModuleNode` declared in [`source_module.cc`](https://github.com/apache/tvm/blob/main/src/target/source/source_module.cc#L331):
   
   ```
   auto n = make_object<CSourceCrtMetadataModuleNode>(func_names, "cc", target, metadata);
   ```
   
   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 `.cc` instead of `.c` as 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 the `CSourceCrtMetadataModuleNode` defined as `cc` or if should work around this issue instead by changing the module format of that file to `c`?
   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 `cc` format instead. During development I just patched the example [`codegen_c`](https://github.com/apache/tvm/blob/74aa856941ff31859d305b8ce564de23fb39ae2f/src/relay/backend/contrib/codegen_c/codegen.cc#L274) to 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


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] PhilippvK commented on pull request #9243: Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension

Posted by GitBox <gi...@apache.org>.
PhilippvK commented on pull request #9243:
URL: https://github.com/apache/tvm/pull/9243#issuecomment-939977914


   Ethosu and CMSIS-NN related builds seem to fail in CI, I will look into this and follow up with a fix for that.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] PhilippvK commented on pull request #9243: Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension

Posted by GitBox <gi...@apache.org>.
PhilippvK commented on pull request #9243:
URL: https://github.com/apache/tvm/pull/9243#issuecomment-979931456


   @areusch Sorry but I do not really understand what you mean. Could you tell me how this helps solving my mentioned "issues"?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] PhilippvK commented on pull request #9243: Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension

Posted by GitBox <gi...@apache.org>.
PhilippvK commented on pull request #9243:
URL: https://github.com/apache/tvm/pull/9243#issuecomment-961777427


   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:
   
   1. Currently the function `ModuleNode::GetFormat` is defined in `src/runtime/module.cc` but each inherited class need to implement the trivial method by itself. The reason for this decision is, that not every module is supposed to have a format. I first thought only `CSourceModuleNode` and `CSourceCrtMetadataModuleNode` needs to be updated but it seems like runtime modules such as `CMSISNNModuleNode` and `EthosUModuleNode` are directly based on 'runtime::ModuleNode' instead of 'CSourceModuleNode' leading to a need to implement a 'GetFormat' function for them as well. Do you have an advice on how to avoid this?
   
   2. Coming up with proper unit tests for this feature: I think there are two parts which shall be tested individually:
   
       - The handling of the `format` argument in the `runtime.CSourceModuleCreate` function invoced by severel BYOC kernels as well as the 'GetFormat()' function which should return the specified value.
       - Export of C++ kernels with the required file extension i.e. via the Model Library Format. This should also include tests ensuring that special cases (e.g. `nvcc` which has the `.cu` extension hardcoded) do not break.
   
   3. As mentioned above, i was wondering, why the `CSourceCrtMetadataModuleNode` is using the format `cc` instead of `c`, which would result in a change of the file extension for this source file to `tvmgen_default_lib0.cc` instead of `tvmgen_default_lib0.c` and therefore likely break some Makefile-Setups where kernel filenames are hardcoded.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] PhilippvK commented on pull request #9243: Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension

Posted by GitBox <gi...@apache.org>.
PhilippvK commented on pull request #9243:
URL: https://github.com/apache/tvm/pull/9243#issuecomment-961777427


   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:
   
   1. Currently the function `ModuleNode::GetFormat` is defined in `src/runtime/module.cc` but each inherited class need to implement the trivial method by itself. The reason for this decision is, that not every module is supposed to have a format. I first thought only `CSourceModuleNode` and `CSourceCrtMetadataModuleNode` needs to be updated but it seems like runtime modules such as `CMSISNNModuleNode` and `EthosUModuleNode` are directly based on 'runtime::ModuleNode' instead of 'CSourceModuleNode' leading to a need to implement a 'GetFormat' function for them as well. Do you have an advice on how to avoid this?
   
   2. Coming up with proper unit tests for this feature: I think there are two parts which shall be tested individually:
   
       - The handling of the `format` argument in the `runtime.CSourceModuleCreate` function invoced by severel BYOC kernels as well as the 'GetFormat()' function which should return the specified value.
       - Export of C++ kernels with the required file extension i.e. via the Model Library Format. This should also include tests ensuring that special cases (e.g. `nvcc` which has the `.cu` extension hardcoded) do not break.
   
   3. As mentioned above, i was wondering, why the `CSourceCrtMetadataModuleNode` is using the format `cc` instead of `c`, which would result in a change of the file extension for this source file to `tvmgen_default_lib0.cc` instead of `tvmgen_default_lib0.c` and therefore likely break some Makefile-Setups where kernel filenames are hardcoded.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] PhilippvK commented on pull request #9243: Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension

Posted by GitBox <gi...@apache.org>.
PhilippvK commented on pull request #9243:
URL: https://github.com/apache/tvm/pull/9243#issuecomment-961777427


   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:
   
   1. Currently the function `ModuleNode::GetFormat` is defined in `src/runtime/module.cc` but each inherited class need to implement the trivial method by itself. The reason for this decision is, that not every module is supposed to have a format. I first thought only `CSourceModuleNode` and `CSourceCrtMetadataModuleNode` needs to be updated but it seems like runtime modules such as `CMSISNNModuleNode` and `EthosUModuleNode` are directly based on 'runtime::ModuleNode' instead of 'CSourceModuleNode' leading to a need to implement a 'GetFormat' function for them as well. Do you have an advice on how to avoid this?
   
   2. Coming up with proper unit tests for this feature: I think there are two parts which shall be tested individually:
   
       - The handling of the `format` argument in the `runtime.CSourceModuleCreate` function invoced by severel BYOC kernels as well as the 'GetFormat()' function which should return the specified value.
       - Export of C++ kernels with the required file extension i.e. via the Model Library Format. This should also include tests ensuring that special cases (e.g. `nvcc` which has the `.cu` extension hardcoded) do not break.
   
   3. As mentioned above, i was wondering, why the `CSourceCrtMetadataModuleNode` is using the format `cc` instead of `c`, which would result in a change of the file extension for this source file to `tvmgen_default_lib0.cc` instead of `tvmgen_default_lib0.c` and therefore likely break some Makefile-Setups where kernel filenames are hardcoded.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] PhilippvK commented on pull request #9243: Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension

Posted by GitBox <gi...@apache.org>.
PhilippvK commented on pull request #9243:
URL: https://github.com/apache/tvm/pull/9243#issuecomment-943236871


   To fix the CI run i will very likely need to rebase on top of `main`. This will eventually screw up open discussions in this PR. Please let me know if should just merge `main` into this branch instead to prevent that. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] masahi commented on pull request #9243: Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension

Posted by GitBox <gi...@apache.org>.
masahi commented on pull request #9243:
URL: https://github.com/apache/tvm/pull/9243#issuecomment-1008442528


   @PhilippvK please resolve the conflict.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] huajsj commented on a change in pull request #9243: Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension

Posted by GitBox <gi...@apache.org>.
huajsj commented on a change in pull request #9243:
URL: https://github.com/apache/tvm/pull/9243#discussion_r727681973



##########
File path: python/tvm/runtime/module.py
##########
@@ -185,6 +185,11 @@ def type_key(self):
         """Get type key of the module."""
         return _ffi_api.ModuleGetTypeKey(self)
 
+    @property
+    def format(self):
+        """Get format of the module."""

Review comment:
       Get the format of the module.

##########
File path: python/tvm/runtime/module.py
##########
@@ -412,7 +418,9 @@ def export_library(self, file_name, fcompile=None, addons=None, workspace_dir=No
                 else:
                     assert module.type_key == "c"
                     object_format = "c"
-                    if "cc" in kwargs:
+                    if module.format in ["cc", "cpp"]:
+                        object_format = module.format
+                    elif "cc" in kwargs:
                         if kwargs["cc"] == "nvcc":
                             object_format = "cu"

Review comment:
       When "kwargs["cc"] == "nvcc" , for the format ["c", "cc", "cpp"] the object_format should either keep original format or use "cu" format, but currently logic distinguished ["c"] and ["cc", "cpp"] that seems not make sense. logic should like following.
   ```
   assert (module.format in ["c", "cc", "cpp"]) .f"<error message>"
   object_format = module.format
   if "cc" in kwargs:
       if kwargs["cc"] == "nvcc":
           object_format = "cu"
   ```

##########
File path: python/tvm/runtime/module.py
##########
@@ -402,7 +407,8 @@ def export_library(self, file_name, fcompile=None, addons=None, workspace_dir=No
         for index, module in enumerate(modules):
             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"]

Review comment:
       assert with error message?

##########
File path: include/tvm/runtime/module.h
##########
@@ -156,6 +156,11 @@ class TVM_DLL ModuleNode : public Object {
    * \return Possible source code when available.
    */
   virtual std::string GetSource(const std::string& format = "");
+  /*!
+   * \brief Get the format of module, when available.

Review comment:
       Get the format of the module




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] huajsj commented on a change in pull request #9243: Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension

Posted by GitBox <gi...@apache.org>.
huajsj commented on a change in pull request #9243:
URL: https://github.com/apache/tvm/pull/9243#discussion_r727681973



##########
File path: python/tvm/runtime/module.py
##########
@@ -185,6 +185,11 @@ def type_key(self):
         """Get type key of the module."""
         return _ffi_api.ModuleGetTypeKey(self)
 
+    @property
+    def format(self):
+        """Get format of the module."""

Review comment:
       Get the format of the module.

##########
File path: python/tvm/runtime/module.py
##########
@@ -412,7 +418,9 @@ def export_library(self, file_name, fcompile=None, addons=None, workspace_dir=No
                 else:
                     assert module.type_key == "c"
                     object_format = "c"
-                    if "cc" in kwargs:
+                    if module.format in ["cc", "cpp"]:
+                        object_format = module.format
+                    elif "cc" in kwargs:
                         if kwargs["cc"] == "nvcc":
                             object_format = "cu"

Review comment:
       When "kwargs["cc"] == "nvcc" , for the format ["c", "cc", "cpp"] the object_format should either keep original format or use "cu" format, but currently logic distinguished ["c"] and ["cc", "cpp"] that seems not make sense. logic should like following.
   ```
   assert (module.format in ["c", "cc", "cpp"]) .f"<error message>"
   object_format = module.format
   if "cc" in kwargs:
       if kwargs["cc"] == "nvcc":
           object_format = "cu"
   ```

##########
File path: python/tvm/runtime/module.py
##########
@@ -402,7 +407,8 @@ def export_library(self, file_name, fcompile=None, addons=None, workspace_dir=No
         for index, module in enumerate(modules):
             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"]

Review comment:
       assert with error message?

##########
File path: include/tvm/runtime/module.h
##########
@@ -156,6 +156,11 @@ class TVM_DLL ModuleNode : public Object {
    * \return Possible source code when available.
    */
   virtual std::string GetSource(const std::string& format = "");
+  /*!
+   * \brief Get the format of module, when available.

Review comment:
       Get the format of the module




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] areusch commented on pull request #9243: Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension

Posted by GitBox <gi...@apache.org>.
areusch commented on pull request #9243:
URL: https://github.com/apache/tvm/pull/9243#issuecomment-944384046


   @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.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] PhilippvK commented on pull request #9243: Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension

Posted by GitBox <gi...@apache.org>.
PhilippvK commented on pull request #9243:
URL: https://github.com/apache/tvm/pull/9243#issuecomment-948606866


   Sorry for the CI bugs. I am working on them.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] PhilippvK commented on pull request #9243: Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension

Posted by GitBox <gi...@apache.org>.
PhilippvK commented on pull request #9243:
URL: https://github.com/apache/tvm/pull/9243#issuecomment-943234803


   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 `nvcc` -> `cu` into the tests.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] masahi merged pull request #9243: Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension

Posted by GitBox <gi...@apache.org>.
masahi merged pull request #9243:
URL: https://github.com/apache/tvm/pull/9243


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] areusch commented on a change in pull request #9243: Add runtime.ModuleGetFormat method enabling export of BYOC generated sources which require a .cpp/.cc file extension

Posted by GitBox <gi...@apache.org>.
areusch commented on a change in pull request #9243:
URL: https://github.com/apache/tvm/pull/9243#discussion_r746228180



##########
File path: python/tvm/runtime/module.py
##########
@@ -185,6 +185,11 @@ def type_key(self):
         """Get type key of the module."""
         return _ffi_api.ModuleGetTypeKey(self)
 
+    @property
+    def format(self):
+        """Get the format of the module."""
+        return _ffi_api.ModuleGetFormat(self)
+
     def get_source(self, fmt=""):

Review comment:
       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"?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org