Skip to content

Commit 74f7a0c

Browse files
Upstream the ONNX importer. (#2636)
This is part 1 of 2, which will also include upstreaming the FX importer. I started with ONNX because it forces some project layout updates and is more self contained/easier as a first step. Deviating somewhat from the RFCs on project layout, I made the following decisions: * Locating the `onnx_importer.py` into `torch_mlir.extras` as Maks already has opened up that namespace and it seemed to fit. Better to have fewer things at that level. * Setup the build so that the root project only contains MLIR Python and pure Python deps (like the importers), but this can be augmented with the `projects/` adding more depending on which features are enabled. * The default build continues to build everything whereas in `TORCH_MLIR_ENABLE_ONLY_MLIR_PYTHON_BINDINGS=1` mode, it builds a `torch-mlir-core` wheel with the pure contents only. `onnx_importer.py` and `importer_smoke_test.py` are almost verbatim copies from SHARK-Turbine. I made some minor local alterations to adapt to paths and generalize the way they interact with the outer project. I expect I can copy these back to Turbine verbatim from here. I also updated the license boilerplate (they have the same license but slightly different project norms for the headers) but retained the correct copyright. Other updates: * Added the ONNX importer unit test (which also can generate test data) in lit, conditioned on the availability of the Python `onnx` package. In a followup once I know everything is stable, I'll add another env var that the CI can set to always enable this so we know conclusively if tests pass. * Moved the ONNX conversion readme to `docs/`. * Renamed CMake option `TORCH_MLIR_ENABLE_ONLY_MLIR_PYTHON_BINDINGS` -> `TORCH_MLIR_ENABLE_PYTORCH_EXTENSIONS` and inverted the sense. Made the JitIR importer and LTC options `cmake_dependent_options` for robustness.
1 parent f67249d commit 74f7a0c

File tree

18 files changed

+1208
-149
lines changed

18 files changed

+1208
-149
lines changed

CMakeLists.txt

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ project(torch-mlir LANGUAGES CXX C)
2525
set(CMAKE_C_STANDARD 11)
2626
set(CMAKE_CXX_STANDARD 17)
2727

28+
include(CMakeDependentOption)
29+
2830
#-------------------------------------------------------------------------------
2931
# Project options
3032
#-------------------------------------------------------------------------------
@@ -43,24 +45,11 @@ endif()
4345

4446
option(TORCH_MLIR_OUT_OF_TREE_BUILD "Specifies an out of tree build" OFF)
4547

46-
# PT1 options.
47-
option(TORCH_MLIR_ENABLE_PROJECT_PT1 "Enables the PyTorch1 project under projects/pt1" OFF)
48-
# TODO: Rename/scope these. They use historic names for now to ease migration
49-
# burden.
50-
option(TORCH_MLIR_ENABLE_JIT_IR_IMPORTER "Enables JIT IR Importer" ON)
51-
option(TORCH_MLIR_ENABLE_LTC "Enables LTC backend" OFF)
52-
option(TORCH_MLIR_ENABLE_ONLY_MLIR_PYTHON_BINDINGS "Build Torch dialect MLIR Python bindings but neither JIT IR Importer nor LTC backend" OFF)
53-
if(TORCH_MLIR_ENABLE_ONLY_MLIR_PYTHON_BINDINGS)
54-
set(TORCH_MLIR_ENABLE_JIT_IR_IMPORTER OFF)
55-
set(TORCH_MLIR_ENABLE_LTC OFF)
56-
endif()
57-
# Force enable the PT1 project if either the JIT_IR_IMPORTER or LTC is enabled.
58-
if(NOT TORCH_MLIR_ENABLE_PROJECT_PT1)
59-
if(TORCH_MLIR_ENABLE_JIT_IR_IMPORTER OR TORCH_MLIR_ENABLE_LTC)
60-
message(STATUS "Enabling projects/pt1 because features requiring it are enabled")
61-
set(TORCH_MLIR_ENABLE_PROJECT_PT1 ON)
62-
endif()
63-
endif()
48+
# PyTorch native extension gate. If OFF, then no features which depend on
49+
# native extensions will be built.
50+
option(TORCH_MLIR_ENABLE_PYTORCH_EXTENSIONS "Enables PyTorch native extension features" ON)
51+
cmake_dependent_option(TORCH_MLIR_ENABLE_JIT_IR_IMPORTER "Enables JIT IR Importer" ON TORCH_MLIR_ENABLE_PYTORCH_EXTENSIONS OFF)
52+
cmake_dependent_option(TORCH_MLIR_ENABLE_LTC "Enables LTC backend" OFF TORCH_MLIR_ENABLE_PYTORCH_EXTENSIONS OFF)
6453

6554
#-------------------------------------------------------------------------------
6655
# Configure out-of-tree vs in-tree build
@@ -235,4 +224,16 @@ endif()
235224
# Sub-projects
236225
#-------------------------------------------------------------------------------
237226

227+
# Sub-projects can bundle additional PyTorch extensions by adding them to this
228+
# source target. It is typically empty unless if features are enabled.
229+
if(MLIR_ENABLE_BINDINGS_PYTHON)
230+
declare_mlir_python_sources(TorchMLIRPythonTorchExtensionsSources)
231+
endif()
232+
233+
# Build projects first as it may populate additional Python deps.
238234
add_subdirectory(projects)
235+
236+
# Finish with top-level Python bindings so it can handle additional deps.
237+
if(MLIR_ENABLE_BINDINGS_PYTHON)
238+
add_subdirectory(python)
239+
endif()

build_tools/python_deploy/build_linux_packages.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,14 @@ function setup_venv() {
351351
echo ":::: Using stable dependencies"
352352
python3 -m pip install --no-cache-dir torch torchvision --index-url https://download.pytorch.org/whl/cpu
353353
python3 -m pip install --no-cache-dir -r /main_checkout/torch-mlir/build-requirements.txt
354-
python3 -m pip install --no-cache-dir -r /main_checkout/torch-mlir/test-requirements.txt
355354
;;
356355
*)
357356
echo "Unrecognized torch version '$torch_version'"
358357
exit 1
359358
;;
360359
esac
361360

361+
python3 -m pip install --no-cache-dir -r /main_checkout/torch-mlir/test-requirements.txt
362362
}
363363

364364
function build_out_of_tree() {

include/torch-mlir/Conversion/TorchOnnxToTorch/README.md renamed to docs/importers/onnx_importer.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
We enable the direct representation of many ONNX features directly in
44
the `torch` dialect as `torch.operator` custom ops with names like
55
`onnx.{OperatorName}`. The majority of ONNX operators are represented
6-
with a systematic transformation. See
7-
[onnx_importer.py](https://github.com/nod-ai/SHARK-Turbine/blob/main/python/shark_turbine/importers/onnx_importer.py)
8-
for the reference importer which complies with the rules below
9-
(this is planned to be upstreamed to torch-mlir proper in the near
10-
future).
6+
with a systematic transformation. `torch_mlir.extras.onnx_importer`
7+
for the reference importer which complies with the rules below.
118

129
## Adding new ONNX operators
1310

@@ -26,10 +23,11 @@ are relatively straight-forward to map, following this general procedure:
2623
* Open the corresponding implementation file `DefaultDomainXtoY.cpp`
2724
corresponding with the alphabetic sort of the op and add a conversion.
2825
* Generate successful test cases:
29-
* Either run the Turbine importer to produce MLIR output for all
30-
ops/models in the ONNX test suite or use a dump that someone has
31-
generated:
32-
* [2023-Nov-21](https://drive.google.com/file/d/1P6QaRXGnCeApjdjNmykLxWa-yqMmIO-d/view?usp=sharing)
26+
* All `onnx_importer.py` tests are dumped to the test temp dir (success
27+
or failure). This is typically located under
28+
`tools/torch-mlir/test/python/onnx_importer/Output`. The `.mlir` files
29+
under there should provide good variants to drive lit test coverage of
30+
conversion.
3331
* There are often many variants of tests for checking conformance of
3432
different historic ONNX encodings, but these are often not load bearing
3533
at the MLIR level.

projects/CMakeLists.txt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
11
include(AddMLIRPython)
22

3+
################################################################################
4+
# PyTorch
35
# Configure PyTorch if we have any features enabled which require it.
6+
################################################################################
47
if(TORCH_MLIR_ENABLE_JIT_IR_IMPORTER OR TORCH_MLIR_ENABLE_LTC)
8+
9+
if (NOT TORCH_MLIR_USE_INSTALLED_PYTORCH)
10+
# Source builds
11+
message(STATUS "Building libtorch from source (features depend on it and NOT TORCH_MLIR_USE_INSTALLED_PYTORCH)")
12+
set(ENV{TORCH_MLIR_SRC_PYTORCH_REPO} ${TORCH_MLIR_SRC_PYTORCH_REPO})
13+
set(ENV{TORCH_MLIR_SRC_PYTORCH_BRANCH} ${TORCH_MLIR_SRC_PYTORCH_BRANCH})
14+
set(ENV{TM_PYTORCH_INSTALL_WITHOUT_REBUILD} ${TM_PYTORCH_INSTALL_WITHOUT_REBUILD})
15+
set(ENV{MACOSX_DEPLOYMENT_TARGET} ${MACOSX_DEPLOYMENT_TARGET})
16+
set(ENV{CMAKE_OSX_ARCHITECTURES} ${CMAKE_OSX_ARCHITECTURES})
17+
set(ENV{CMAKE_C_COMPILER_LAUNCHER} ${CMAKE_C_COMPILER_LAUNCHER})
18+
set(ENV{CMAKE_CXX_COMPILER_LAUNCHER} ${CMAKE_CXX_COMPILER_LAUNCHER})
19+
execute_process(
20+
COMMAND ${TORCH_MLIR_SOURCE_DIR}/build_tools/build_libtorch.sh
21+
RESULT_VARIABLE _result
22+
)
23+
if(_result)
24+
message(FATAL_ERROR "Failed to run `build_libtorch.sh`")
25+
endif()
26+
set(TORCH_INSTALL_PREFIX "libtorch")
27+
endif()
28+
529
message(STATUS "Enabling PyTorch C++ dep (features depend on it)")
630
include(TorchMLIRPyTorch)
731

@@ -48,6 +72,6 @@ if(TORCH_MLIR_ENABLE_LTC)
4872
endif()
4973

5074
# Include overall PT1 project.
51-
if(TORCH_MLIR_ENABLE_PROJECT_PT1)
75+
if(TORCH_MLIR_ENABLE_PYTORCH_EXTENSIONS)
5276
add_subdirectory(pt1)
5377
endif()

projects/pt1/python/CMakeLists.txt

Lines changed: 19 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -7,79 +7,22 @@ set(CMAKE_PLATFORM_NO_VERSIONED_SONAME ON)
77
# argument.
88
set(TORCH_MLIR_PYTHON_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/torch_mlir")
99

10-
1110
# We vendor our own MLIR instance in the `torch_mlir` namespace.
1211
add_compile_definitions("MLIR_PYTHON_PACKAGE_PREFIX=torch_mlir.")
1312

14-
################################################################################
15-
# PyTorch
16-
################################################################################
17-
18-
if (NOT TORCH_MLIR_USE_INSTALLED_PYTORCH)
19-
# Source builds
20-
set(ENV{TORCH_MLIR_SRC_PYTORCH_REPO} ${TORCH_MLIR_SRC_PYTORCH_REPO})
21-
set(ENV{TORCH_MLIR_SRC_PYTORCH_BRANCH} ${TORCH_MLIR_SRC_PYTORCH_BRANCH})
22-
set(ENV{TM_PYTORCH_INSTALL_WITHOUT_REBUILD} ${TM_PYTORCH_INSTALL_WITHOUT_REBUILD})
23-
set(ENV{MACOSX_DEPLOYMENT_TARGET} ${MACOSX_DEPLOYMENT_TARGET})
24-
set(ENV{CMAKE_OSX_ARCHITECTURES} ${CMAKE_OSX_ARCHITECTURES})
25-
set(ENV{CMAKE_C_COMPILER_LAUNCHER} ${CMAKE_C_COMPILER_LAUNCHER})
26-
set(ENV{CMAKE_CXX_COMPILER_LAUNCHER} ${CMAKE_CXX_COMPILER_LAUNCHER})
27-
execute_process(
28-
COMMAND ${TORCH_MLIR_SOURCE_DIR}/build_tools/build_libtorch.sh
29-
RESULT_VARIABLE _result
30-
)
31-
if(_result)
32-
message(FATAL_ERROR "Failed to run `build_libtorch.sh`")
33-
endif()
34-
set(TORCH_INSTALL_PREFIX "libtorch")
35-
endif()
36-
37-
################################################################################
38-
# Sources
39-
################################################################################
40-
41-
declare_mlir_python_sources(TorchMLIRPythonSources)
42-
declare_mlir_python_sources(TorchMLIRPythonExtensions)
43-
44-
if (NOT TORCH_MLIR_ENABLE_ONLY_MLIR_PYTHON_BINDINGS)
45-
declare_mlir_python_sources(TorchMLIRPythonSources.TopLevel
46-
ROOT_DIR "${TORCH_MLIR_PYTHON_ROOT_DIR}"
47-
ADD_TO_PARENT TorchMLIRPythonSources
48-
SOURCES
49-
__init__.py
50-
_dynamo_fx_importer.py
51-
compiler_utils.py
52-
dynamo.py
53-
_version.py
54-
)
55-
endif()
56-
57-
declare_mlir_python_sources(TorchMLIRPythonSources.Dialects
58-
ROOT_DIR "${TORCH_MLIR_PYTHON_ROOT_DIR}"
59-
ADD_TO_PARENT TorchMLIRPythonSources
60-
)
13+
# ################################################################################
14+
# # Sources
15+
# ################################################################################
6116

62-
declare_mlir_dialect_python_bindings(
63-
ADD_TO_PARENT TorchMLIRPythonSources.Dialects
17+
declare_mlir_python_sources(TorchMLIRPythonSources.TopLevel
6418
ROOT_DIR "${TORCH_MLIR_PYTHON_ROOT_DIR}"
65-
TD_FILE dialects/TorchBinding.td
66-
SOURCES dialects/torch/__init__.py
67-
DIALECT_NAME torch
68-
)
69-
70-
################################################################################
71-
# Extensions
72-
################################################################################
73-
74-
declare_mlir_python_extension(TorchMLIRPythonExtensions.Main
75-
MODULE_NAME _torchMlir
76-
ADD_TO_PARENT TorchMLIRPythonExtensions
19+
ADD_TO_PARENT TorchMLIRPythonTorchExtensionsSources
7720
SOURCES
78-
TorchMLIRModule.cpp
79-
EMBED_CAPI_LINK_LIBS
80-
TorchMLIRCAPI
81-
PRIVATE_LINK_LIBS
82-
LLVMSupport
21+
__init__.py
22+
_dynamo_fx_importer.py
23+
compiler_utils.py
24+
dynamo.py
25+
_version.py
8326
)
8427

8528
################################################################################
@@ -110,56 +53,23 @@ endif()
11053

11154
# add_subdirectory(torch_mlir/_torch_mlir_custom_op_example)
11255

113-
################################################################################
114-
# Generate packages and shared library
115-
# Downstreams typically will not use these, but they are useful for local
116-
# testing.
117-
################################################################################
118-
119-
set(_source_components
120-
# TODO: Core is now implicitly building/registering all dialects, increasing
121-
# build burden by ~5x. Make it stop.
122-
# TODO: Reduce dependencies. We need ExecutionEngine and a bunch of passes
123-
# for the reference backend, but logically they can be separate. But seemingly
124-
# the only way to handle that is to create a separate mlir python package
125-
# tree, which seems excessive.
126-
MLIRPythonSources
127-
MLIRPythonExtension.Core
128-
MLIRPythonExtension.RegisterEverything
129-
TorchMLIRPythonSources
130-
TorchMLIRPythonExtensions
131-
)
132-
133-
add_mlir_python_common_capi_library(TorchMLIRAggregateCAPI
134-
INSTALL_COMPONENT TorchMLIRPythonModules
135-
INSTALL_DESTINATION python_packages/torch_mlir/torch_mlir/_mlir_libs
136-
OUTPUT_DIRECTORY "${TORCH_MLIR_PYTHON_PACKAGES_DIR}/torch_mlir/torch_mlir/_mlir_libs"
137-
RELATIVE_INSTALL_ROOT "../../../.."
138-
DECLARED_SOURCES ${_source_components}
139-
)
140-
141-
add_mlir_python_modules(TorchMLIRPythonModules
142-
ROOT_PREFIX "${TORCH_MLIR_PYTHON_PACKAGES_DIR}/torch_mlir/torch_mlir"
143-
INSTALL_PREFIX "python_packages/torch_mlir/torch_mlir"
144-
DECLARED_SOURCES ${_source_components}
145-
COMMON_CAPI_LINK_LIBS
146-
TorchMLIRAggregateCAPI
147-
)
148-
14956
# TODO: Find a cleaner way to do this.
15057
# Can we build the JIT IR importer with `declare_mlir_python_extension`?
15158
# Then it would "just work".
15259
if(TORCH_MLIR_ENABLE_JIT_IR_IMPORTER)
153-
add_dependencies(TorchMLIRPythonModules TorchMLIRJITIRImporter)
154-
add_dependencies(TorchMLIRPythonModules TorchMLIRJITIRImporterPybind)
155-
# Build the E2E Tests (which depend on the JIT IR importer now).
156-
add_dependencies(TorchMLIRPythonModules TorchMLIRE2ETestPythonModules)
60+
add_dependencies(TorchMLIRPythonTorchExtensionsSources
61+
TorchMLIRJITIRImporter
62+
TorchMLIRJITIRImporterPybind
63+
TorchMLIRE2ETestPythonModules
64+
)
15765
endif()
15866

15967
if(TORCH_MLIR_ENABLE_LTC)
16068
# Add Torch-MLIR LTC backend as dependency
161-
add_dependencies(TorchMLIRPythonModules torch_mlir_ltc_backend)
162-
add_dependencies(TorchMLIRPythonModules reference_lazy_backend)
69+
add_dependencies(TorchMLIRPythonTorchExtensionsSources
70+
torch_mlir_ltc_backend
71+
reference_lazy_backend
72+
)
16373
endif()
16474

16575
add_subdirectory(test)

projects/pt1/python/torch_mlir/jit_ir_importer/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
## Declare the sources of the Python module.
66

7-
declare_mlir_python_sources(TorchMLIRPythonSources.JitIRImporter
7+
declare_mlir_python_sources(TorchMLIRPythonTorchExtensionsSources.JitIRImporter
88
ROOT_DIR "${TORCH_MLIR_PYTHON_ROOT_DIR}"
9-
ADD_TO_PARENT TorchMLIRPythonSources
9+
ADD_TO_PARENT TorchMLIRPythonTorchExtensionsSources
1010
SOURCES_GLOB
1111
jit_ir_importer/*.py
1212
)

0 commit comments

Comments
 (0)