onnx-mlir/docs
gongsu832 81c774ba5b
Cleanup rtmemref api (#238)
* Detect llvm-project commit change in utils/clone-mlir.sh and rebuild llvm-project
for zLinux Jenkins build bot

* Cleanup RtMemRef API

-  use forward declaration to hide private data fields
-  RtMemRef.h: external user header, C/C++
- _RtMemRef.h: internal user header, C++ only
-  RtMemRef.hpp and RtMemRef.cpp: implementation header and file
-  add external APIs
       OrderedRtMemRefDict *ormrd_create(RtMemRef **rmrs, int n)
       RtMemRef **ormrd_getRmrs(OrderedRtMemRefDict *ormrd)
       int ormrd_getNumOfRmrs(OrderedRtMemRefDict *ormrd)
   for creating and querying OrderedRtMemRefDict with RtMemRef arrays
-  data buffer installed by rmr_setData() will be managed by user
-  unique_ptr<RtMemRef> must use custom deleter <RtMemRef,decltype(&rmr_destroy)>

* See if I have write access.

* Remove test CMake code.

* Use new API.

* Format code.

* Format code & rename variables for readability.

* Remove used API spec.

* Rename OrderedRtMemRefDict -> RtMemRefList, _dataMalloc -> _owningData.

* OrderedRtMemRefDict -> RtMemRefList

* Update KrnlToLLVM.cpp

* Trigger Jenkins

* Restart Jenkins

* OrderedRtMemRefDict -> RtRmrRefList

* More OrderedRtMemRefDict -> RtMemRefList.

* Format jni wrapper.

* Rename API functions to maintain stylistic consistency.

* Bug fix.

* Bug fix.

* Format code.

* Fix RtMemRefUtils.

* Format code.

* Using llvm function naming scheme.

* Rename runtime api file name to project name (onnx-mlir) as per convention.

* Include the new runtime header file.

* Reflect api header file name change in build script.

* Bug fix.

* Remove C++ code.

* Revert "Remove C++ code."

This reverts commit b217dfabae99e42db30721600cb5507866d4dc98.

* Clarify memory management responsibility.

* Add constructor to specify name & data ownership.

* Include stdbool.

* Remove dictionary semantics from RtMemRefList

* Bug fix.

* Format code.

* Format code.

* Use macro to define database of metadata.

* Prevent formatter from acting on metadata decl.

* Nit.

* Restore backend unit tests.

* Use spaces instead of tabs for better formatting.

* Use explicit template instantiation.

* Update RtMemRef struct doc.

* Make runtime compilable both in c and c++ mode.
Build two versions of the runtime library, one c version as the user-facing c runtime, and one c++ version as the one used inside this project.

* Bug fix, avoid stack allocation for output rmr list.

* Change _dyn_entry_point_main_graph -> run_main_graph for better memorability.

* Write a complete introductory tutorial on c99 Runtime and a test for it.

* Add onnx installation as dependency.

* Use target_include_directory to avoid installation.

* Format code.

* Fix cmake target_include_directories.

* Address compiler warning.

* First pass of RtMemRef->OMTensor.

* Second pass of RtMemRef -> OMTensor.

* nit, omtList -> omTensorList.

* omt -> omTensor for clarity.

* Rename OnnxMlirInternal.h -> OnnxMlirRuntime.hpp because there's no internal/external API, only C/C++ API.

* Format code.

* Restructure Runtime source code and move header -> /include and test -> /test/unit.

* Bugfix.

* Format code.

* Add unit test for OMTensor ctor.

* Update JNI CMake include directory.

* Bugfix.

* No need to re-declare ONNX types.

* Disable runtime doc test on non-x86 platforms.

* Rename OMTensor fields to be more sensible.

* Fix data type mismatch.

* size_t -> int64_t, prefer fixed width integers.

* Use consistent header guard style.

* Further tweak OMTensor API.

* Bugfix.

* Bugfix.

* Format code.

* Add doxygen config file.

* Tweak OMTensor API.

* Tweak API doc, hide OMTensorList implementation.

* Format code.

* Add new documentation item for Runtime API.

* Hide internal use only API declarations, move their comments to their implementations.

* Clarify ownership semantics in relevant API documentations.

* Fix PyRuntime.

* Remove alignment concerns from public API and include explaination of alignment issue in struct OMTensor definition.

* Print out unsupported numpy dtype.

* Use preferred way of type comparison in pybind11.

* Debug s390x issue.

* Remove debug code.

* Clarify semantics of strides/shape setter/getter, use \brief to include short description of API function.

* Improve documentation.

* Single out unpolished C++ API declarations.

* Clarify OMTensorList API.

* Bugfix.

* Bugfix.

* Assert after malloc.

* Handle malloc failures.

* Nit.

* Tweak legal notices.

* Format code.

* Remove doxygen generated files.

* Tweak legal notice format.

* Upgrade Cython

Numpy installation depends on Cython.

Co-authored-by: Tian Jin <tjingrant@gmail.com>
2020-10-10 10:32:09 -04:00
..
Dialects Support LLVM as of 7dcd0042 (#309) 2020-09-22 23:42:50 +07:00
_data Cleanup rtmemref api (#238) 2020-10-10 10:32:09 -04:00
_layouts Fix documentation path issue. (#80) 2020-04-13 22:09:41 +08:00
doc_check Make onnx-mlir work with latest mlir. (#93) 2020-04-27 17:03:56 +08:00
docker-example change tag to x86 (#273) 2020-08-26 14:14:59 -04:00
.gitignore Make onnx-mlir work with latest mlir. (#93) 2020-04-27 17:03:56 +08:00
CMakeLists.txt Setup Documentation Page using Github Pages (#76) 2020-04-09 23:37:04 +08:00
DebuggingNumericalError.md Support model debugging (#138) 2020-05-21 13:02:48 +08:00
Documentation.md Setup Documentation Page using Github Pages (#76) 2020-04-09 23:37:04 +08:00
ErrorHandling.md Believe all errors now results in meaningful actions (#153) 2020-06-01 13:55:19 -04:00
HowToAddAnOperation.md Make onnx-mlir work with latest mlir. (#93) 2020-04-27 17:03:56 +08:00
ImportONNXDefs.md Constprop (#162) 2020-06-08 15:45:32 -04:00
README.md Support LLVM as of 7dcd0042 (#309) 2020-09-22 23:42:50 +07:00
Testing.md Rapid check test (#141) 2020-06-08 10:18:55 +08:00
_config.yml Setup Documentation Page using Github Pages (#76) 2020-04-09 23:37:04 +08:00

README.md

ONNX MLIR

The Open Neural Network Exchange implementation in MLIR.

CircleCI

Prerequisites

gcc >= 6.4
libprotoc >= 3.11.0
cmake >= 3.15.4

Installation on UNIX

MLIR

Firstly, install MLIR (as a part of LLVM-Project):

git clone https://github.com/llvm/llvm-project.git
# Check out a specific branch that is known to work with ONNX MLIR.
cd llvm-project && git checkout 91671e13efbc5dbd17b832d7973401350d0a6ee6 && cd ..
mkdir llvm-project/build
cd llvm-project/build
cmake -G Ninja ../llvm \
   -DLLVM_ENABLE_PROJECTS=mlir \
   -DLLVM_BUILD_EXAMPLES=ON \
   -DLLVM_TARGETS_TO_BUILD="host" \
   -DCMAKE_BUILD_TYPE=Release \
   -DLLVM_ENABLE_ASSERTIONS=ON \
   -DLLVM_ENABLE_RTTI=ON

cmake --build . --target -- ${MAKEFLAGS}
cmake --build . --target check-mlir

ONNX-MLIR (this project)

Two environment variables need to be set:

  • LLVM_PROJ_SRC should point to the llvm-project src directory (e.g., llvm-project/).
  • LLVM_PROJ_BUILD should point to the llvm-project build directory (e.g., llvm-project/build).

To build ONNX-MLIR, use the following command:

git clone --recursive https://github.com/onnx/onnx-mlir.git

# Export environment variables pointing to LLVM-Projects.
export LLVM_PROJ_SRC=$(pwd)/llvm-project/
export LLVM_PROJ_BUILD=$(pwd)/llvm-project/build

mkdir onnx-mlir/build && cd onnx-mlir/build
cmake ..
cmake --build .

# Run FileCheck tests:
export LIT_OPTS=-v
cmake --build . --target check-onnx-lit

After the above commands succeed, an onnx-mlir executable should appear in the bin directory.

Installation on Windows

Building onnx-mlir on Windows requires building some additional prerequisites that are not available by default.

Note that the instructions in this file assume you are using Visual Studio 2019 Community Edition. It is recommended that you have the Desktop development with C++ and Linux development with C++ workloads installed. This ensures you have all toolchains and libraries needed to compile this project and its dependencies on Windows.

Run all the commands from a shell started from "Developer Command Prompt for VS 2019".

Protobuf

Build protobuf as a static library.

set root_dir=%cd%
git clone --recurse-submodules https://github.com/protocolbuffers/protobuf.git
cd protobuf
cd cmake
cmake -G "Visual Studio 16 2019" -A x64 -T host=x64 -DCMAKE_BUILD_TYPE=Release -Dprotobuf_MSVC_STATIC_RUNTIME=OFF -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_EXAMPLES=OFF -Dprotobuf_WITH_ZLIB=OFF -DCMAKE_INSTALL_PREFIX="%root_dir%\protobuf\install"
call msbuild protobuf.sln /m /p:Configuration=Release
call msbuild INSTALL.vcxproj /p:Configuration=Release

Before running CMake for onnx-mlir, ensure that the bin directory to this protobuf is before any others in your PATH:

set PATH=%root_dir%\protobuf\install\bin;%PATH%

PDCurses

Build a local version of the curses library, used by various commandline tools in onnx-mlir. These instructions assume you use Public Domain Curses.

Run this from a Visual Studio developer command prompt since you will need access to the appropriate version of Visual Studio's nmake tool.

set root_dir=%cd%
git clone https://github.com/wmcbrine/PDCurses.git
set PDCURSES_SRCDIR=%root_dir%/PDCurses
cd PDCurses
call nmake -f wincon/Makefile.vc

MLIR

Install MLIR (as a part of LLVM-Project):

git clone https://github.com/llvm/llvm-project.git
# Check out a specific branch that is known to work with ONNX MLIR.
cd llvm-project && git checkout 91671e13efbc5dbd17b832d7973401350d0a6ee6 && cd ..
set root_dir=%cd%
md llvm-project\build
cd llvm-project\build
call cmake -G "Visual Studio 16 2019" -A x64 -T host=x64 ..\llvm ^
   -DCMAKE_INSTALL_PREFIX="%root_dir%\llvm-project\build\install" ^
   -DLLVM_ENABLE_PROJECTS=mlir ^
   -DLLVM_BUILD_EXAMPLES=ON ^
   -DLLVM_TARGETS_TO_BUILD="host" ^
   -DCMAKE_BUILD_TYPE=Release ^
   -DLLVM_ENABLE_ASSERTIONS=ON ^
   -DLLVM_ENABLE_RTTI=ON ^
   -DLLVM_ENABLE_ZLIB=OFF

call cmake --build . --config Release --target -- /m
call cmake --build . --config Release --target install
call cmake --build . --config Release --target check-mlir

ONNX-MLIR (this project)

The following environment variables need to be set before building onnx-mlir:

  • CURSES_LIB_PATH: Path to curses library (e.g. c:/repos/PDCurses)
  • LLVM_PROJ_BUILD: Path to the build directory for LLVM (e.g. c:/repos/llvm-project/build)
  • LLVM_PROJ_SRC: Path to the source directory for LLVM (e.g. c:/repos/llvm-project)

This project uses lit (LLVM's Integrated Tester) for unit tests. When running CMake, we will also specify the path to the lit tool from LLVM using the LLVM_EXTERNAL_LIT define.

To build ONNX MLIR, use the following command:

git clone --recursive https://github.com/onnx/onnx-mlir.git

REM Export environment variables pointing to LLVM-Projects.
set root_dir=%cd%
set CURSES_LIB_PATH=%root_dir%/PDCurses
set LLVM_PROJ_BUILD=%root_dir%/llvm-project/build
set LLVM_PROJ_SRC=%root_dir%/llvm-project

md onnx-mlir\build
cd onnx-mlir\build
call cmake -G "Visual Studio 16 2019" -A x64 -T host=x64 -DLLVM_EXTERNAL_LIT="%root_dir%\llvm-project\build\Release\bin\llvm-lit.py" -DCMAKE_BUILD_TYPE=Release ..
call cmake --build . --config Release --target onnx-mlir -- /m

REM Run FileCheck tests
set LIT_OPTS=-v
call cmake --build . --config Release --target check-onnx-lit

After the above commands succeed, an onnx-mlir executable should appear in the bin directory.

Using ONNX-MLIR

The usage of onnx-mlir is as such:

OVERVIEW: ONNX MLIR modular optimizer driver

USAGE: onnx-mlir [options] <input file>

OPTIONS:

Generic Options:

  --help        - Display available options (--help-hidden for more)
  --help-list   - Display list of available options (--help-list-hidden for more)
  --version     - Display the version of this program

ONNX MLIR Options:
These are frontend options.

  Choose target to emit:
      --EmitONNXIR - Ingest ONNX and emit corresponding ONNX dialect.
      --EmitMLIR   - Lower model to MLIR built-in transformation dialect.
      --EmitLLVMIR - Lower model to LLVM IR (LLVM dialect).
      --EmitLLVMBC - Lower model to LLVM IR and emit (to file) LLVM bitcode for model.

Example

For example, to lower an ONNX model (e.g., add.onnx) to ONNX dialect, use the following command:

./onnx-mlir --EmitONNXIR add.onnx

The output should look like:

module {
  func @main_graph(%arg0: tensor<10x10x10xf32>, %arg1: tensor<10x10x10xf32>) -> tensor<10x10x10xf32> {
    %0 = "onnx.Add"(%arg0, %arg1) : (tensor<10x10x10xf32>, tensor<10x10x10xf32>) -> tensor<10x10x10xf32>
    return %0 : tensor<10x10x10xf32>
  }
}

Troubleshooting

If the latest LLVM project fails to work due to the latest changes to the MLIR subproject please consider using a slightly older version of LLVM. One such version, which we use, can be found here.