Skip to content

Commit 1b40b63

Browse files
[onnx] Add torch-mlir-import-onnx native port as an optional tool/library. (#2694)
As noted in the plan when this work started, we need to produce an ORT EP plugin for a downstream project, and this will necessitate a C-based ONNX importer (as opposed to the existing Python one). Because this comes with dependencies that we do not want to impart on various projects, this is optional in torch-mlir. It is also factored so that it can be used as standalone sources in downstreams that need it. Since it only depends on public C APIs on the MLIR side, this will make build coupling a lot better (since a C++ dep is not needed on the compiler and it is trivial to dynamically load). Our original plan was just to maintain this fork off to the side in our ORT plugin, but once work started, it seemed better to write it clean and contribute it upstream for anyone to use. We expect that for non-ORT use, the Python importer will have better ergonomics for most folks. I will follow-up with a test suite refactor so that we can drive the Python or C importer. This is a relatively mechanical port from Python to C, borrowing some scaffolding from the old JitIR importer. It does attempt to lay some groundwork for external data, which will need to be implemented on the Python side as well.
1 parent 2d796b7 commit 1b40b63

File tree

7 files changed

+1409
-0
lines changed

7 files changed

+1409
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ option(TORCH_MLIR_ENABLE_PYTORCH_EXTENSIONS "Enables PyTorch native extension fe
5151
cmake_dependent_option(TORCH_MLIR_ENABLE_JIT_IR_IMPORTER "Enables JIT IR Importer" ON TORCH_MLIR_ENABLE_PYTORCH_EXTENSIONS OFF)
5252
cmake_dependent_option(TORCH_MLIR_ENABLE_LTC "Enables LTC backend" OFF TORCH_MLIR_ENABLE_PYTORCH_EXTENSIONS OFF)
5353

54+
option(TORCH_MLIR_ENABLE_ONNX_C_IMPORTER "Enables the ONNX C importer" OFF)
55+
5456
#-------------------------------------------------------------------------------
5557
# Configure out-of-tree vs in-tree build
5658
#-------------------------------------------------------------------------------

projects/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
include(AddMLIRPython)
22

3+
if(TORCH_MLIR_ENABLE_ONNX_C_IMPORTER)
4+
add_subdirectory(onnx_c_importer)
5+
endif()
6+
37
################################################################################
48
# PyTorch
59
# Configure PyTorch if we have any features enabled which require it.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
message(STATUS "Enabling onnx_c_importer...")
2+
3+
include(FetchContent)
4+
5+
find_package(Protobuf)
6+
if(NOT Protobuf_FOUND)
7+
message(FATAL_ERROR
8+
"In order to build C ONNX support, the Protobuf package must be installed "
9+
"on the system. Without this ONNX will attempt to build it in the project "
10+
"and the dependent ABSEIL build system is incompatible. "
11+
"On Ubuntu, install with: "
12+
"apt install libprotobuf-dev protobuf-compiler\n\n"
13+
"(or this entire component can be disabled with "
14+
"-DTORCH_MLIR_ENABLE_ONNX_C_IMPORTER=OFF)")
15+
endif()
16+
17+
option(ONNX_DISABLE_EXCEPTIONS "For compatibility with LLVM build" ON)
18+
19+
FetchContent_Declare(
20+
onnx
21+
EXCLUDE_FROM_ALL
22+
GIT_REPOSITORY https://github.com/onnx/onnx.git
23+
GIT_TAG v1.15.0
24+
GIT_SHALLOW ON
25+
GIT_PROGRESS ON
26+
)
27+
FetchContent_MakeAvailable(onnx)
28+
29+
add_llvm_executable(
30+
torch-mlir-import-onnx
31+
PARTIAL_SOURCES_INTENDED
32+
33+
import-onnx-main.cpp
34+
OnnxImporter.h
35+
OnnxImporter.cpp
36+
)
37+
38+
target_link_libraries(
39+
torch-mlir-import-onnx
40+
LLVMSupport
41+
MLIRCAPIIR
42+
TorchMLIRCAPI
43+
onnx
44+
)

0 commit comments

Comments
 (0)