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

Packaging C++ functions in MONAI #4232

Closed
masadcv opened this issue May 5, 2022 · 15 comments
Closed

Packaging C++ functions in MONAI #4232

masadcv opened this issue May 5, 2022 · 15 comments

Comments

@masadcv
Copy link
Contributor

masadcv commented May 5, 2022

There are quite a few C++ functions currently in MONAI that are quite useful in a number of applications.
However, all C++ code requires compilation before they can be used (e.g. through JIT compilation or using BUILD_MONAI=1).

In order to use such C++ functions as dependency in other projects, these may need to be packaged as pre-compile binaries as we have already seen a number of issues with dependencies that required compilation (e.g. SImpleCRF dependency issues in MONAI Label see for example: Project-MONAI/MONAILabel#605 and Project-MONAI/MONAILabel#719).

There are of course discussions about moving some useful dependencies for MONAI Label scribbles into MONAI, so they can be maintained properly through community efforts (see for example discussion here: Project-MONAI/MONAILabel#731 and here: #1601)

It may be relevant to address these packaging issue for C++ functionality so that these can be used as dependency in other projects.

Relevant pointers:

cc: @tvercaut @charliebudd

@wyli wyli mentioned this issue May 5, 2022
7 tasks
@wyli
Copy link
Contributor

wyli commented May 5, 2022

Thanks for bringing this up. I think @charliebudd's solution https://github.com/charliebudd/torch-extension-builder is nice, perhaps we should experiment with it and leverage the project? cc @ericspod @rijobro @Nic-Ma.

But I think in the long term monai's release process couldn't rely on a personal repo with only one maintainer... Is there a more sustainable way of releasing the extension-builder?

(there's a related request #1462)

@tvercaut
Copy link
Member

tvercaut commented May 5, 2022

We can certainly transfer the torch-extension-builder repo to sit under the Project-MONAI organisation if there is appetite for MONAI to support it.

@charliebudd
Copy link
Collaborator

I'm happy for MONAI to fork the repo. I think we can all agree a good end goal would be to have this supported by PyTorch. Untill that time moving into MONAI would provide better visability. An alternate direction would be to form a new github org ("PyTorch extention community" for example) as this could help to bring in stake holders from other projects. Either way I have added an MIT license to the git repo. I hope to have more time to work on this in the near future.

@wyli
Copy link
Contributor

wyli commented May 6, 2022

Sure, we can discuss this at the dev meeting.

To test the feasibility of the precompiling I adapted the tool for monai. The pipeline is working fine now for linux + cuda based environments https://github.com/Project-MONAI/MONAI/actions/runs/2280287297 https://test.pypi.org/project/monai/0.8.1.post0.dev249/#files looks like the end-user's wheel file size will be less than 50Mb and it can support multiple versions of PyTorch/cuda libs.

@charliebudd
Copy link
Collaborator

charliebudd commented May 6, 2022

Glad its working 😊

From the looks of it the debug information is being left in. Adding the -g0 flag to the c++ compiler in setup.py will strip this. The wheels for my current project are around 1MB despite including binaries for multiple compilations.

@masadcv
Copy link
Contributor Author

masadcv commented May 6, 2022

@charliebudd would it be possible to also include Windows build in this? Or maybe this could be added as a future work?

@charliebudd
Copy link
Collaborator

I have spent some time looking into this, Unfortunetly my efforts hit a dead end when I found the github runner cannot run windows docker images. The work around is to install the packages inside the runner for each build. This will be a bit slower than downloading a pre-built docker image but I dont see another way. I'll try to spend some time on this next week.

@masadcv
Copy link
Contributor Author

masadcv commented May 6, 2022

Many thanks! Yes, it certainly will be quite useful to have these precompiled for Windows - since most users we have seen have issue compiling source on Windows (see for example: Project-MONAI/MONAILabel#363 (comment))

@tvercaut
Copy link
Member

tvercaut commented May 6, 2022

For the time being, I would suggest to track generic packaging issues with torch-extension-builder in the @charliebudd's repo. E.g. for windows support:
charliebudd/torch-extension-builder#13

@wyli
Copy link
Contributor

wyli commented May 9, 2022

From the looks of it the debug information is being left in. Adding the -g0 flag to the c++ compiler in setup.py will strip this. The wheels for my current project are around 1MB despite including binaries for multiple compilations.

thanks, this might be an issue for monai, I've enabled cuda10.2/11.3+pytorch1.9/1.10/1.11+multiple archs and the package size becomes 130Mb with the recommended compiler flags. with more than 100Mb, pypi is not supported by default https://test.pypi.org/help/#file-size-limit.

@masadcv
Copy link
Contributor Author

masadcv commented May 24, 2022

From the looks of it the debug information is being left in. Adding the -g0 flag to the c++ compiler in setup.py will strip this. The wheels for my current project are around 1MB despite including binaries for multiple compilations.

thanks, this might be an issue for monai, I've enabled cuda10.2/11.3+pytorch1.9/1.10/1.11+multiple archs and the package size becomes 130Mb with the recommended compiler flags. with more than 100Mb, pypi is not supported by default https://test.pypi.org/help/#file-size-limit.

is there any way to reduce this size to get to an acceptable range?

@charliebudd
Copy link
Collaborator

From the looks of it the debug information is being left in. Adding the -g0 flag to the c++ compiler in setup.py will strip this. The wheels for my current project are around 1MB despite including binaries for multiple compilations.

thanks, this might be an issue for monai, I've enabled cuda10.2/11.3+pytorch1.9/1.10/1.11+multiple archs and the package size becomes 130Mb with the recommended compiler flags. with more than 100Mb, pypi is not supported by default https://test.pypi.org/help/#file-size-limit.

is there any way to reduce this size to get to an acceptable range?

yes, as I said, the -g0 compiler flag will inform the compiler to strip the debug symbols. This is standard practice for release code and significantly decreases the build size.

@wyli
Copy link
Contributor

wyli commented May 25, 2022

I tried flags such as "cxx", ["-g0", "-Wl,-s", "-Os"], the wheel file size is still 126M, mainly within the package there are:

-rw-r--r--@  89M  _C.so.pt110cu102
-rw-r--r--@  78M  _C.so.pt110cu113
-rw-r--r--@  89M  _C.so.pt111cu102
-rw-r--r--@  78M  _C.so.pt111cu113
-rw-r--r--@  89M  _C.so.pt19cu102

@charliebudd
Copy link
Collaborator

These are very large file sizes, especially as each of these .so files is a single compilation of the extension. Checking with tool such as readelf/objdump would give clues as to why they are so large. Have we checked no normal compiled size of the MONAI extention?

@vikashg
Copy link

vikashg commented Jan 5, 2024

maybe out of scope for monai-core? @masadcv Please reopen if needed

@vikashg vikashg closed this as completed Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants