Intel® Extension for MLIR (IMEX) is a collection of MLIR dialects and passes from Intel for supporting MLIR lowering to Intel silicon (CPU, GPU, …). Goal of this project is to support development of MLIR enhancements for upstream contribution, and to provide a sandbox for validation independent of front end frameworks. Current project scope includes:
- Dialects and passes needed to lower and execute MLIR entry dialect (linalg, CFG, and etc) on Intel GPU.
- Wrapper libraries to inteface with level zero runtime and sycl runtime supporting Intel GPU.
- Other experimental dialects: PTensor, Dist
- CMake > 3.14.3 (3.18 if using Conda)
- Ninja
- doxygen (Optional for building docs)
- pre-commit
- clang-format
- lit (If set up as an out-of-tree build of LLVM)
conda create -n imex-dev -c intel -c defaults -c conda-forge pip">=21.2.4" pre-commit cmake clang-format lit doxygen
conda activate imex-devpre-commit install -f -c .pre-commit-config.yaml
IMEX supports three different ways of building depending on how LLVM is set up.
IMEX can be treated like a sub-project of LLVM and built as part of LLVM by using an LLVM config option called LLVM_EXTERNAL_PROJECTS.
git clone https://github.com/intel/mlir-extensions.git
git clone https://github.com/llvm/llvm-project.git
cd llvm-project
git checkout `cat ../mlir-extensions/build_tools/llvm_version.txt`
cmake -G Ninja -B build -S llvm \
-DLLVM_ENABLE_PROJECTS=mlir \
-DLLVM_BUILD_EXAMPLES=ON \
-DLLVM_TARGETS_TO_BUILD="X86" \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_EXTERNAL_PROJECTS="Imex" \
-DLLVM_EXTERNAL_IMEX_SOURCE_DIR=../mlir-extensions
cmake --build build --target check-imex
Note: -DLLVM_INSTALL_UTILS=ON is not needed for this build since all tests
will run using the FileCheck utility that is available in the build tree.
An external lit is not needed as well, since all tests will run using llvm-lit
in the build tree.
Note: Make sure to pass -DLLVM_INSTALL_UTILS=ON when building LLVM with
CMake so that it installs FileCheck to the chosen installation prefix.
The script build_tools/build_imex.py can build both LLVM/MLIR and IMEX for you. Use
build_imex.py -h to look at all the options provided by the script. It is
advisable to use an external lit when building IMEX.
If you want the script to build LLVM and then IMEX, do as follows:
external_lit=`which lit`
python build_tools/build_imex.py \
--working-dir $local-working-dir-to-build-llvm \
--external-lit ${external_lit}To reuse a previously built LLVM, use the following steps:
Make sure your LLVM install is built from the git commit sha as stated in 'build_tools/llvm_version.txt'.
external_lit=`which lit`
python build_tools/build_imex.py \
--working-dir $local-working-dir-to-build-llvm \
--llvm-install $llvm-target-dir \
--external-lit ${external_lit}Make sure your LLVM install is built from the git commit sha as stated in 'build_tools/llvm_version.txt'.
mkdir build
cd build
CC=gcc-9 CXX=g++-9 MLIR_DIR=<llvm-install-directory> cmake ..
make -j 12
For GPU support, pass the cmake variables to enable the required runtime libraries
CC=gcc-9 CXX=g++-9 MLIR_DIR=<llvm-install-directory> cmake .. -DSYCL_DIR=/PATH_TO/intel/oneapi/compiler/latest/linux/ -DLEVEL_ZERO_DIR=/PATH_TO/level-zero-install/ -DIMEX_ENABLE_L0_RUNTIME=1 -DIMEX_ENABLE_SYCL_RUNTIME=1
make -j 12This is similar to Option 2. Instead of building and installing LLVM. Just build LLVM and set "MLIR_DIR" to the sub-directory in the LLVM build tree that has generated file MLIRConfig.cmake. Rest of the step is the same as Option 2.
To build user documentation do
cd build
cmake --build . --target mlir-docIt will render docs to the 'doc' directory.
To build code documentation use '-DIMEX_INCLUDE_DOCS' when configuring with cmake and do
cd build
cmake --build . --target doc_doxygen# enter root directory of mlir-extension
cd mlir-extensions
python scripts/add_dialect.py <name-of-new-dialect>This will
- generate directories
IRandTransformsin the directories (include/mlir/Dialectandlib/dialect) - Extend/Create cmake infrastructure with defaults
- Create stub source files for IR and transforms
include/imex/Dialect/<name>/IR/<name>Ops.hinclude/imex/Dialect/<name>/IR/<name>Ops.tdlib/Dialect/IR/<name>Ops.cppinclude/imex/Dialect/<name>/Transforms/Passes.hinclude/imex/Dialect/<name>/Transforms/Passes.tdlib/Dialect/Transforms/PassDetail.h
Now, it's your turn to
- Add your dialect and its transforms/passes to appropriate places in
include/imex/InitIMEXDialects.hinclude/imex/InitIMEXPasses.hlib/Conversion/IMEXPassDetail.h
- Fill in what's marked with FIXME
- The documentation of the dialect should go into the
descriptionfields in<name>Ops.td. At build time the description will be extracted and a filedoc/<name>.mdwill be generated automatically. It will include descriptions of the dialect and operations in a standardized way.
# enter root directory of mlir-extension
cd mlir-extensions
python scripts/add_conversion.py $name-of-source-dialect $name-of-target-dialectThis will
- Let $conversion-name name be "$name-of-source-dialectTo$name-of-target-dialect"
- Add directories
include/mlir/Conversion/<conversion-name>andlib/Conversion/<conversion-name> - Extend/Create cmake infrastructure with defaults
- Add declarations to header
include/mlir/Conversion/<conversion-name>/<conversion-name>.h - Put cpp definition stubs to
lib/Conversion/<conversion-name>/<conversion-name>.cpp - Add conversion to
include/imex/Conversion/IMEXPasses.td and include/imex/Conversion/IMEXPasses.h - Add a pass def stub to
include/imex/Conversion/IMEXPasses.td and include/imex/Conversion/Passes.td
You will now have to
- Fill in the above files what's marked with FIXME
- The documentation of the pass should go into the
descriptionfield inPasses.td. At build time the description will be extracted and a filedoc/Conversions.mdwill be generated automatically. - Write your Pattern rewriters
git clone https://github.com/oneapi-src/level-zero.git
cd level-zero
mkdir build
cd build
cmake ../level-zero -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../level-zero-install
ninja installInstall DPC++ compiler : Instructions here
https://www.intel.com/content/www/us/en/developer/articles/tool/oneapi-standalone-components.html#dpcpp-cpp
Once DPC++ is installed source the compiler vars:
source /PATH_TO/intel/oneapi/compiler/latest/env/vars.sh
To run the FileCheck based tests, follow the following steps:
cd <to your IMEX build directory>
cmake --build . --target check-imexAdd '-v' to the above command-line to get verbose output.
IMEX provides an initial set of benchmarks for studying its performance. To build these benchmarks, users need
to manually add -DIMEX_ENABLE_BENCHMARK=ON option when building the IMEX. The benchmark testcases and the
script for running them will be generated under the build/benchmarks folder.
Currently, IMEX provides benchmarks for the following 4 categories of operations:
| Operation | CPU | GPU |
|---|---|---|
| elementwise (relu and silu) | Yes | Yes |
| reduction (softmax) | Yes | Yes |
| transpose (transpose) | Yes | Yes |
| fusion (kInputFusion and kLoopFusion) | No | Yes |
These test cases are mainly implemented using linalg dialect, and the spriv test cases for
relu are also provided. Each testcase is named following the pattern of opname_shape_dtype.mlir
For simplicity, the bench_imex script is provided to run the benchmark. It can take a mlir file or a folder as input.
for the later case, it will simply run all test cases inside the folder. In addition, it also has to choose a runtime
based on the option. It accepts one of the following three options:
-cfor cpu runtime-lfor level-zero runtime (for INTEL GPU)-sfor sycl runtime (for INTEL GPU)
# run a specific test case on CPU
./bench_imex -c relu/cpu/relu_1x160x160x120_f16.mlir
# run a set of test cases on GPU using sycl runtime
./bench_imex -s relu/gpu/NOTE: if you are using
-c, please use testcases undercpusubfolder; similarly, if you are using-sor-l, please use testcases undergpusubfolder. Otherwise, it may have unspecified errors or behaviors.
IMEX benchmark suite is implemented using CMAKE template, and initially provides limited set of shapes extraced from some production models, e.g., BERT, and AlexNet.
- ReLU: 1x160x160x120, 50x640x20x15, 512x640x20x15
- SiLU: 1x1024x40x30, 50x20x3072, 512x640x20x15
- Softmax: 1x2000, 16x2000, 64x2000, 256x2000, 1024x2000
- Transpose: 128x136, 1024x1024, 16x96x96, 96x7x96
- Reduce: 32x16x512x512
Users can extend it to evaluate more shapes by editing the, e.g, relu.shapes.in file, in each subfolder, and then
rebuild the imex. User can also add new data types, but it is currently only limited to basic data types including
fp32, fp16, int32 etc.
export IMEX_ENABLE_PROFILING=ON
run the testpython {your_path}/imex_runner.py xxx -o test.mlir
mlir-translate test.mlir -mlir-to-llvmir -o test.ll
llc test.ll -filetype=obj -o test.o
clang++ test.o {path}/libmlir_runner_utils.so {path}/libmlir_c_runner_utils.so {path}/libsycl-runtime.so -no-pie -o test
ze_tracer ./testThis code is made available under the Apache License 2.0 with LLVM Exceptions.
See the LICENSE.txt file for more details.