ONNX-Frontend is a project to build customized onnx graph --> onnx dialect --> stablehlo dialect pipeline.
git clone https://github.com/bytedance/byteir.git
cd byteir/frontends/onnx-frontend
export ONNX_FRONTEND_ROOT=$(pwd)
export ONNX_MLIR_ROOT=$ONNX_FRONTEND_ROOT/third_party/onnx-mlir
export ONNX_OFFICIAL_ROOT=$ONNX_MLIR_ROOT/third_party/onnx
git submodule update --init --recursive $ONNX_MLIR_ROOT
cd $ONNX_MLIR_ROOT && git apply $ONNX_FRONTEND_ROOT/third_party/patches/OnnxMlir*.patch
cd $ONNX_OFFICIAL_ROOT && git apply $ONNX_FRONTEND_ROOT/third_party/patches/OnnxOfficial*.patch
pip3 install lit>=14.0.0
First, build MLIR (llvm-project commit b2cdf3cc4c08729d0ff582d55e40793a20bbcdcc
) with cmake option -DLLVM_ENABLE_RTTI=ON
.
Then,
mkdir $ONNX_FRONTEND_ROOT/build
cd $ONNX_FRONTEND_ROOT/build
cmake "-H$ONNX_FRONTEND_ROOT" \
"-B$ONNX_FRONTEND_ROOT/build" \
-GNinja \
-DCMAKE_CXX_COMPILER=/usr/bin/c++ \
-DPython3_ROOT_DIR=/usr/bin/python3.7 \
-DPY_VERSION=3 \
-DMLIR_DIR="${YOUR_MLIR_DIR}/lib/cmake/mlir" \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_EXTERNAL_LIT=$(which lit)
cmake --build "$ONNX_FRONTEND_ROOT/build" --config Release --target onnx-frontend onnx-frontend-opt
After the above commands succeed, an onnx-frontend
executable should appear in $ONNX_FRONTEND_ROOT/build/onnx-frontend/src/
.
cmake --build "$ONNX_FRONTEND_ROOT/build" --target check-of-lit
onnx-frontend model.onnx -batch-size=1 -invokeOnnxVersionConverter -o model.stablehlo.mlir
-
Before you start
- Build onnx-mlir, see this.
- Learn the basic knowledge of MLIR, especially the Pattern Rewritten doc and the DDR doc.
-
Workflow
- Develop new patterns in third_party/onnx-mlir
- Save changes into a new .patch file under
third_party/patches
folder
-
Steps to add a onnx-2-stablehlo convertion pattern
- Take a look at the definition of the onnx op you want to convert, and onnx is more like a coarse-grained op collection.
- Take a look at the corresponding stablehlo op definition, and stablehlo is more like a fine-grained op collection.
- Implement the pattern under
onnx-mlir/src/Conversion/
directory, note that if there's no 1:1 mapping from onnx to stablehlo, converting the coarse-grained onnx op to fine-grained onnx ops is preferred before converting from onnx dialect to stablehlo dialect. In this way, the fine-grained onnx ops to stablehlo ops' conversion could be reused. - Populate the implemented pattern into convert-onnx-to-stablehlo pass
- Add lit test under
onnx-mlir/test/mlir/conversion/onnx_to_stablehlo
directory
-
Developing tips
- Reference how tensorflow convert coarse-grained ops to fine-grained ops.
- Reference how tensorflow lower tf dialect to mhlo dialect.