Skip to content

Representation and Reference Lowering of ONNX Models in MLIR Compiler Infrastructure

License

Notifications You must be signed in to change notification settings

manbearian/onnx-mlir

 
 

Repository files navigation

ONNX-MLIR

The Open Neural Network Exchange implementation in MLIR (http://onnx.ai/onnx-mlir/).

System Build Status
s390x-Linux Build Status
ppc64le-Linux Build Status
amd64-Linux Build Status
amd64-Windows Build Status
amd64-macOS Build Status

Setting up ONNX-MLIR using Prebuilt Containers

The prefered approach to using and developing ONNX-MLIR is to used Docker Images and Containers, as getting the proper code dependences may be tricky on some systems. Our instructions on using ONNX-MLIR with dockers are here.

Setting up ONNX-MLIR directly

Prerequisites

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

Help to update the prerequisites is found here.

At any point in time, ONNX-MLIR depends on a specific commit of the LLVM project that has been shown to work with the project. Periodically the maintainers need to move to a more recent LLVM level. Among other things, this requires to update the commit string in (utils/clone-mlir.sh). When updating ONNX-MLIR, it is good practice to check that the commit string of the MLIR/LLVM is the same as the one listed in that file.

Build on Linux or OSX

Directions to install MLIR and ONNX-MLIR are provided here.

Build on Windows

Directions to install Protobuf, MLIR, and ONNX-MLIR are provided here.

Testing build and summary of custom envrionment variables

After installation, an onnx-mlir executable should appear in the build/Debug/bin or build/Release/bin directory.

There are several cmake targets that are used to verify the validity of the onnx-mlir compiler, which are listed here.

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:
      --EmitONNXBasic - Ingest ONNX and emit the basic ONNX operations without inferred shapes.
      --EmitONNXIR   - Ingest ONNX and emit corresponding ONNX dialect.
      --EmitMLIR     - Lower input to MLIR built-in transformation dialect.
      --EmitLLVMIR   - Lower input to LLVM IR (LLVM MLIR dialect).
      --EmitLib      - Lower input to LLVM IR, emit LLVM bitcode,
                       compile and link it to a shared library (default).
      --EmitJNI      - Lower input to LLVM IR -> LLVM bitcode -> JNI shared library ->
                       jar.

  Optimization levels:
      --O0           - Optimization level 0 (default).
      --O1           - Optimization level 1.
      --O2           - Optimization level 2.
      --O3           - Optimization level 3.

The full list of options is given by the --help option. Note that just as most compilers, the default optimization level is -O0. We recommend using -O3 for most applications.

Simple Example

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

./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>
  }
}

An example based on the add operation is found here, which build an ONNX model using a python script, and then provide a main program to load the model's value, compute, and print the models output.

End to End Example

An end to end example is provided here, which train, compile, and execute a simple MNIST example using both the C++ or Python interface.

Slack channel

We have a slack channel established under the Linux Foundation AI and Data Workspace, named #onnx-mlir-discussion. This channel can be used for asking quick questions related to this project. A direct link is here.

Contributing

Want to contribute, consult this page for specific help on our project here or the docs sub-directory.

About

Representation and Reference Lowering of ONNX Models in MLIR Compiler Infrastructure

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 55.8%
  • MLIR 33.5%
  • Python 7.0%
  • C 1.4%
  • CMake 1.2%
  • Java 0.9%
  • Other 0.2%