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

Build light weight PyRuntime without llvm or onnx-mlir #3044

Merged
merged 15 commits into from
Jan 29, 2025

Conversation

chentong319
Copy link
Collaborator

@chentong319 chentong319 commented Jan 15, 2025

Motivation

Python driver is needed to run the compiled model. Currently, the driver is built with onnx-mlir and can only be run in the env where onnx-mlir is built, typically inside the onnx-mlir docker image. When the compilation can be done by calling the onnx-mlir docker image, we'd like to run the compiled .so with python driver in the local env, so that all the packages installed in the local env can be used, rather than installing them on top of docker.

In order to reach this goal, the PR tried to remove the unnecessary dependencies of pyruntime if the light-weight pyruntime is the target. Otherwise, there is no change in how onnx-mlir is built and used.
Details can be found in docs/build-pyruntime-lit.md.
I tried the build on a z16 machine: it takes less than 2 minutes.

Components in this PR

  1. CMakefile and source code changes to cut the dependencies of pyruntime. An option ONNX_MLIR_ENABLE_PYRUNTIME_LIT is used to control Cmake, and consequently a compile definition ENABLE_PYRUNTIME_LIT issued to control the source code.
  2. Wrap the built pyruntime driver into a python package
  3. This python package use python docker package to call the compiler. This is equivalent to docker/onnx-mlir.py in functionality but with "import docker" interface.

Test
Run successfully with utils/BuildPyRuntimeLit.sh.

Future works:

  1. Support of float16
  2. Not all llvm utilities are replaced. Only the essential ones have been implemented.
  3. Can third_party/onnx be removed?
  4. Try to integrate the precompiled lib for different os-arch into the package. Enable user to use pip install the package from pip server.
  5. Try to integrate the utils/build-pyruntime-lit.sh into python package and invoke the build when pip install is executed.
  6. Add the test of light weight PyRuntime into the build.

@chentong319 chentong319 marked this pull request as draft January 15, 2025 21:00
@chentong319 chentong319 marked this pull request as ready for review January 21, 2025 14:57
Copy link
Collaborator

@AlexandreEichenberger AlexandreEichenberger left a comment

Choose a reason for hiding this comment

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

Still very early review; let me get the overall picture.

How would the new code base change if we were to adapt it for PyTorch as well? Namely, what is common to any "docker compilation + pyruntime_lit" vs what is specialized for "ORT" and what would be specialized for PyTorch?

Do you mind giving me this high level info, and then I will be able to better understand the overall approach. Thanks

CMakeLists.txt Outdated
@@ -11,6 +11,7 @@ option(ONNX_MLIR_ENABLE_STABLEHLO "Enable StableHLO support." ON)
option(ONNX_MLIR_ENABLE_WERROR "Enable warnings as errors." OFF)
option(ONNX_MLIR_SUPPRESS_THIRD_PARTY_WARNINGS "Suppress warning in third_party code." ON)
option(ONNX_MLIR_ENABLE_JAVA "Set to ON for building the Java runtime, tools, and tests" ON)
option(ONNX_MLIR_ENABLE_PYRUNTIME_LIT "Set to ON for building Python driver of running the compiled model without llvm-project." OFF)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe the name should be explained: if off, then no pyruntime is build? Or its build anyway, but when on, then it's only the pyruntime? Or when off, pyruntime is build one way, but when off, its build another way?

Maybe the name could be a bit more explicit, depending on what the answer is from the question above.

minor question: _LIT is it for "_LIGHT"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When this option is off, the pyruntime is built with onnx-mlir and llvm-project, as it was previously.
When this option is on, only the pyruntime is built without llvm-project.

Yes, LIT for LIGHT.

CMakeLists.txt Outdated
add_subdirectory(src)
add_subdirectory(docs)
add_subdirectory(test)
if (ONNX_MLIR_ENABLE_PYRUNTIME_LIT)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see this (and above): there are some dir that added on both path. Is it that the order of them is important?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Only the src is added on both path. I do not think the order of add_subdirectory matters. Just to keep the original add_subdirectory together.

)
endif()
if (ONNX_MLIR_ENABLE_PYRUNTIME_LIT)
function(llvm_update_compile_flags name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add a one liner comment on why this function is defined here.

@@ -0,0 +1,151 @@
import numpy as np
Copy link
Collaborator

Choose a reason for hiding this comment

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

That file is totally new, is that right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it is the file to use docker package.

@chentong319
Copy link
Collaborator Author

Still very early review; let me get the overall picture.

How would the new code base change if we were to adapt it for PyTorch as well? Namely, what is common to any "docker compilation + pyruntime_lit" vs what is specialized for "ORT" and what would be specialized for PyTorch?

Do you mind giving me this high level info, and then I will be able to better understand the overall approach. Thanks

This docker compilation + pyruntime_lit provides the basic functionality for compilation and run. The interface for ORT or PyTorch will be in the python package built on top of them. This PR contains only the package, onnxmlir, with ORT interface. In future, another package for PyTorch interface will be provided.

Copy link
Collaborator

@AlexandreEichenberger AlexandreEichenberger left a comment

Choose a reason for hiding this comment

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

LGTM as a first pass, will wait for additional feedback from the team as we develop this further. Thanks for taking this great first step @chentong319

Copy link
Collaborator

@tungld tungld left a comment

Choose a reason for hiding this comment

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

LGTM!

Hope that the environment variable ENABLE_PYRUNTIME_LIT will be removed completely in next PRs.

LIT in ONNX_MLIR_ENABLE_PYRUNTIME_LIT is confusing with LIT tests. It would be better to use other word, e.g. LIGHT or to remove it.

Copy link
Collaborator

@tungld tungld left a comment

Choose a reason for hiding this comment

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

LGTM!

@chentong319
Copy link
Collaborator Author

chentong319 commented Jan 29, 2025

LGTM!

Hope that the environment variable ENABLE_PYRUNTIME_LIT will be removed completely in next PRs.

LIT in ONNX_MLIR_ENABLE_PYRUNTIME_LIT is confusing with LIT tests. It would be better to use other word, e.g. LIGHT or to remove it.

The definition, ENABLE_PYRUNTIME_LIT, will be away finally. Current version is the transition from the previous one and the new one.
Alex has the same concern about _LIT. I will change it to LIGHT in this PR.

@chentong319 chentong319 merged commit 2e4a46a into onnx:main Jan 29, 2025
6 of 7 checks passed
@chentong319 chentong319 deleted the pyruntime-lit branch January 29, 2025 18:38
@jenkins-droid
Copy link
Collaborator

Jenkins Linux s390x Build #16181 [push] Build light weight PyRun... started at 13:38

@jenkins-droid
Copy link
Collaborator

Jenkins Linux amd64 Build #16180 [push] Build light weight PyRun... started at 12:39

@jenkins-droid
Copy link
Collaborator

Jenkins Linux ppc64le Build #15209 [push] Build light weight PyRun... started at 13:58

@jenkins-droid
Copy link
Collaborator

Jenkins Linux amd64 Build #16180 [push] Build light weight PyRun... passed after 1 hr 30 min

@jenkins-droid
Copy link
Collaborator

Jenkins Linux s390x Build #16181 [push] Build light weight PyRun... passed after 1 hr 47 min

@jenkins-droid
Copy link
Collaborator

Jenkins Linux ppc64le Build #15209 [push] Build light weight PyRun... passed after 2 hr 54 min

christopherlmunoz pushed a commit to christopherlmunoz/onnx-mlir that referenced this pull request Jan 30, 2025
* pass test

Signed-off-by: Chen Tong <[email protected]>

* package

Signed-off-by: Chen Tong <[email protected]>

* clean makefile

Signed-off-by: Chen Tong <[email protected]>

* document

Signed-off-by: Chen Tong <[email protected]>

* fix MLIR.cmake

Signed-off-by: Chen Tong <[email protected]>

* fix script

Signed-off-by: Chen Tong <[email protected]>

* fix

Signed-off-by: Chen Tong <[email protected]>

* add comments

Signed-off-by: Chen Tong <[email protected]>

* LIGHT

Signed-off-by: Chen Tong <[email protected]>

---------

Signed-off-by: Chen Tong <[email protected]>
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.

4 participants