diff --git a/CMakeLists.txt b/CMakeLists.txt index 42609866cf..78cb36bd02 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,8 @@ cmake_minimum_required(VERSION 3.20.0) project(onnx-mlir) -option(ONNX_MLIR_BUILD_TESTS "Build ONNX-MLIR test executables. If OFF, just generate build targets." ON) +# Like in LLVM, default tests are off. Lit-tests are always on. +option(ONNX_MLIR_BUILD_TESTS "Build ONNX-MLIR test executables. If OFF, just generate build targets." OFF) option(ONNX_MLIR_CCACHE_BUILD "Set to ON for a ccache enabled build." OFF) option(ONNX_MLIR_ENABLE_STABLEHLO "Enable StableHLO support." ON) option(ONNX_MLIR_ENABLE_WERROR "Enable warnings as errors." OFF) diff --git a/docker/Dockerfile.onnx-mlir b/docker/Dockerfile.onnx-mlir index f028ed29b3..0877c5e679 100644 --- a/docker/Dockerfile.onnx-mlir +++ b/docker/Dockerfile.onnx-mlir @@ -43,6 +43,7 @@ RUN LLVM_PROJECT_ROOT=${WORK_DIR}/llvm-project \ && CC=clang CXX=clang++ \ cmake -DMLIR_DIR=${LLVM_PROJECT_ROOT}/build/lib/cmake/mlir \ -DCMAKE_BUILD_TYPE=Release \ + -DONNX_MLIR_BUILD_TESTS=ON \ -DLLVM_ENABLE_ASSERTIONS=ON \ -DCMAKE_INSTALL_MESSAGE=NEVER \ -DONNX_MLIR_ACCELERATORS=${ACCEL} .. \ diff --git a/docker/Dockerfile.onnx-mlir-dev b/docker/Dockerfile.onnx-mlir-dev index 574737c1a9..4d595de1fb 100644 --- a/docker/Dockerfile.onnx-mlir-dev +++ b/docker/Dockerfile.onnx-mlir-dev @@ -32,6 +32,7 @@ RUN LLVM_PROJECT_ROOT=${WORK_DIR}/llvm-project \ && CC=clang CXX=clang++ \ cmake -DMLIR_DIR=${LLVM_PROJECT_ROOT}/build/lib/cmake/mlir \ -DCMAKE_BUILD_TYPE=Debug \ + -DONNX_MLIR_BUILD_TESTS=ON \ -DONNX_MLIR_TEST_OPTLEVEL=0 \ -DONNX_MLIR_ACCELERATORS=${ACCEL} .. \ # Run the cppcheck scanner on s390x diff --git a/docs/BuildOnLinuxOSX.md b/docs/BuildOnLinuxOSX.md index d3917e6c9c..9824012751 100644 --- a/docs/BuildOnLinuxOSX.md +++ b/docs/BuildOnLinuxOSX.md @@ -57,6 +57,10 @@ This project uses lit ([LLVM's Integrated Tester](https://llvm.org/docs/CommandG To build ONNX-MLIR, use the following commands (maybe with additional `-DCMAKE_CXX_FLAGS` argument described [below](#enable-cpu-optimizations)): +To build a version with extended tests, keep the `-DONNX_MLIR_BUILD_TESTS=ON` line below. +Note that the extended tests requires the installation of `onnx` via a pip install of `third_party/onnx`. To build a version without a dependence on `third_party/onnx`, remove the `-DONNX_MLIR_BUILD_TESTS=ON` line or set to `OFF`. +The lightweight Literal tests are build regardless of this option. + [same-as-file]: <> ({"ref": "utils/install-onnx-mlir.sh", "skip-doc": 2}) ```bash git clone --recursive https://github.com/onnx/onnx-mlir.git @@ -68,6 +72,7 @@ if [[ -z "$pythonLocation" ]]; then cmake -G Ninja \ -DCMAKE_CXX_COMPILER=/usr/bin/c++ \ -DCMAKE_BUILD_TYPE=Release \ + -DONNX_MLIR_BUILD_TESTS=ON \ -DLLVM_ENABLE_ASSERTIONS=ON \ -DMLIR_DIR=${MLIR_DIR} \ .. @@ -75,6 +80,7 @@ else cmake -G Ninja \ -DCMAKE_CXX_COMPILER=/usr/bin/c++ \ -DCMAKE_BUILD_TYPE=Release \ + -DONNX_MLIR_BUILD_TESTS=ON \ -DLLVM_ENABLE_ASSERTIONS=ON \ -DPython3_ROOT_DIR=$pythonLocation \ -DMLIR_DIR=${MLIR_DIR} \ diff --git a/docs/BuildOnWindows.md b/docs/BuildOnWindows.md index 2d1628aa1a..c359fe5c94 100644 --- a/docs/BuildOnWindows.md +++ b/docs/BuildOnWindows.md @@ -108,6 +108,10 @@ call cmake --build . --config Release ``` After the above commands succeed, an `onnx-mlir` executable should appear in the `Debug/bin` or `Release/bin` directory. +To build a version with extended tests, add the `-DONNX_MLIR_BUILD_TESTS=ON` line above. +Note that the extended tests requires the installation of `onnx` via a pip install of `third_party/onnx`. To build a version without a dependence on `third_party/onnx`, do not add the `-DONNX_MLIR_BUILD_TESTS=ON` line or set to `OFF`. +The lightweight Literal tests are build regardless of this option. + ### Trouble shooting build issues Check this [page](TestingHighLevel.md) for helpful hints. diff --git a/docs/doc_check/CMakeLists.txt b/docs/doc_check/CMakeLists.txt index 5012d5c1cd..f1ca28d94a 100644 --- a/docs/doc_check/CMakeLists.txt +++ b/docs/doc_check/CMakeLists.txt @@ -1,5 +1,9 @@ # SPDX-License-Identifier: Apache-2.0 +if (NOT ONNX_MLIR_BUILD_TESTS) + set(EXCLUDE_FROM_ALL ON) +endif() + set(check_cmd ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/check.py) add_custom_target(check-doc diff --git a/test/accelerators/NNPA/CMakeLists.txt b/test/accelerators/NNPA/CMakeLists.txt index 1d8d768d16..314ef55c7e 100644 --- a/test/accelerators/NNPA/CMakeLists.txt +++ b/test/accelerators/NNPA/CMakeLists.txt @@ -1,5 +1,9 @@ # SPDX-License-Identifier: Apache-2.0 +if (NOT ONNX_MLIR_BUILD_TESTS) + set(EXCLUDE_FROM_ALL ON) +endif() + add_custom_target(check-onnx-backend-numerical-nnpa) add_subdirectory(backend) diff --git a/test/backend-cpp/CMakeLists.txt b/test/backend-cpp/CMakeLists.txt index fe9793abcd..ccea7a1d27 100644 --- a/test/backend-cpp/CMakeLists.txt +++ b/test/backend-cpp/CMakeLists.txt @@ -1,5 +1,9 @@ # SPDX-License-Identifier: Apache-2.0 +if (NOT ONNX_MLIR_BUILD_TESTS) + set(EXCLUDE_FROM_ALL ON) +endif() + add_custom_target(backend-cpp) set_target_properties(backend-cpp PROPERTIES FOLDER "Tests") diff --git a/test/backend/CMakeLists.txt b/test/backend/CMakeLists.txt index cfc65f5716..dae585b292 100644 --- a/test/backend/CMakeLists.txt +++ b/test/backend/CMakeLists.txt @@ -1,5 +1,9 @@ # SPDX-License-Identifier: Apache-2.0 +if (NOT ONNX_MLIR_BUILD_TESTS) + set(EXCLUDE_FROM_ALL ON) +endif() + file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/conftest.py INPUT ${CMAKE_CURRENT_SOURCE_DIR}/conftest.py diff --git a/test/backend/common.py b/test/backend/common.py index a9b077eae7..7e56f4ecb5 100644 --- a/test/backend/common.py +++ b/test/backend/common.py @@ -16,7 +16,15 @@ import sys import os import ctypes -import onnx + +try: + import onnx +except ImportError: + print( + "Failed to include the `onnx` package; please install to successfully use this script." + ) + exit(1) + import subprocess import variables from variables import * diff --git a/test/backend/inference_backend.py b/test/backend/inference_backend.py index b13f25f177..5bf36404cd 100644 --- a/test/backend/inference_backend.py +++ b/test/backend/inference_backend.py @@ -18,8 +18,16 @@ import base64 import numpy as np import re -import onnx -import onnx.parser + +try: + import onnx + import onnx.parser +except ImportError: + print( + "Failed to include the `onnx` package; please install to successfully use this script." + ) + exit(1) + import subprocess from onnx.backend.base import Device, DeviceType, Backend from onnx.backend.test import BackendTest diff --git a/test/backend/onnxmlir_node_tests.py b/test/backend/onnxmlir_node_tests.py index 9a63d34edc..864d3f767c 100644 --- a/test/backend/onnxmlir_node_tests.py +++ b/test/backend/onnxmlir_node_tests.py @@ -8,8 +8,16 @@ ################################################################################ import numpy as np -import onnx -import onnx.parser + +try: + import onnx + import onnx.parser +except ImportError: + print( + "Failed to include the `onnx` package; please install to successfully use this script." + ) + exit(1) + from collections import namedtuple OnnxMlirNodeTestCase = namedtuple( diff --git a/test/compilerlib/CMakeLists.txt b/test/compilerlib/CMakeLists.txt index b5d0da143b..58acdeff8d 100644 --- a/test/compilerlib/CMakeLists.txt +++ b/test/compilerlib/CMakeLists.txt @@ -1,5 +1,9 @@ # SPDX-License-Identifier: Apache-2.0 +if (NOT ONNX_MLIR_BUILD_TESTS) + set(EXCLUDE_FROM_ALL ON) +endif() + add_onnx_mlir_executable(CompilerLibTest CompilerLibTest.cpp diff --git a/test/mlir/CMakeLists.txt b/test/mlir/CMakeLists.txt index 0e2408fcda..ab681d987e 100644 --- a/test/mlir/CMakeLists.txt +++ b/test/mlir/CMakeLists.txt @@ -1,5 +1,7 @@ # SPDX-License-Identifier: Apache-2.0 +# Just like LLVM, ONNX_MLIR_BUILD_TESTS=false does not disable lit tests. + # Turn on lit test for specified accelerators even if the # accelerator code itself cannot be built. if (ONNX_MLIR_ACCELERATORS) diff --git a/test/modellib/CMakeLists.txt b/test/modellib/CMakeLists.txt index 338655b4cb..99270525f6 100644 --- a/test/modellib/CMakeLists.txt +++ b/test/modellib/CMakeLists.txt @@ -1,5 +1,9 @@ # SPDX-License-Identifier: Apache-2.0 +if (NOT ONNX_MLIR_BUILD_TESTS) + set(EXCLUDE_FROM_ALL ON) +endif() + add_onnx_mlir_library(ModelLib CategoryMapperModel.cpp ConvModel.cpp diff --git a/test/numerical/CMakeLists.txt b/test/numerical/CMakeLists.txt index 95cbd7d9f1..bb2b4c6269 100644 --- a/test/numerical/CMakeLists.txt +++ b/test/numerical/CMakeLists.txt @@ -1,5 +1,9 @@ # SPDX-License-Identifier: Apache-2.0 +if (NOT ONNX_MLIR_BUILD_TESTS) + set(EXCLUDE_FROM_ALL ON) +endif() + add_custom_target(numerical) set_target_properties(numerical PROPERTIES FOLDER "Tests") diff --git a/test/perf/CMakeLists.txt b/test/perf/CMakeLists.txt index 010a7882a1..ec5b13e8ac 100644 --- a/test/perf/CMakeLists.txt +++ b/test/perf/CMakeLists.txt @@ -1,5 +1,9 @@ # SPDX-License-Identifier: Apache-2.0 +if (NOT ONNX_MLIR_BUILD_TESTS) + set(EXCLUDE_FROM_ALL ON) +endif() + add_onnx_mlir_library(PerfLib PerfHelper.cpp EXCLUDE_FROM_OM_LIBS diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 849459b4c8..4e44bc6c8d 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -1,5 +1,9 @@ # SPDX-License-Identifier: Apache-2.0 +if (NOT ONNX_MLIR_BUILD_TESTS) + set(EXCLUDE_FROM_ALL ON) +endif() + add_custom_target(unittest) set_target_properties(unittest PROPERTIES FOLDER "Tests") diff --git a/utils/CheckONNXModel.py b/utils/CheckONNXModel.py index 7b90165de9..9bf2bb2e69 100755 --- a/utils/CheckONNXModel.py +++ b/utils/CheckONNXModel.py @@ -37,7 +37,14 @@ import os import sys import argparse -import onnx + +try: + import onnx +except ImportError: + print( + "Failed to include the `onnx` package; please install to successfully use this script." + ) + exit(1) import time import signal import subprocess diff --git a/utils/RunONNXModel.py b/utils/RunONNXModel.py index 56c2b603cb..e878f20e4e 100755 --- a/utils/RunONNXModel.py +++ b/utils/RunONNXModel.py @@ -14,7 +14,14 @@ import os import sys import argparse -import onnx + +try: + import onnx +except ImportError: + print( + "Failed to include the `onnx` package; please install to successfully use this script." + ) + exit(1) import time import signal import subprocess @@ -984,7 +991,14 @@ def run(self, outputname, input_feed, **kwargs): if args.verify.lower() == "onnxruntime": input_model_path = args.model # Reference backend by using onnxruntime. - import onnxruntime + + try: + import onnxruntime + except ImportError: + print( + "Failed to include the `onnxruntime` package; please install to successfully use this script." + ) + exit(1) input_feed = dict(zip(input_names, inputs)) print("Running inference using onnxruntime ...") diff --git a/utils/gen_onnx_mlir.py b/utils/gen_onnx_mlir.py index a32c931521..c74c442f4f 100755 --- a/utils/gen_onnx_mlir.py +++ b/utils/gen_onnx_mlir.py @@ -30,7 +30,14 @@ from typing import Any, Text, Sequence, Dict, List, Type, Set, Tuple import pprint -import onnx + +try: + import onnx +except ImportError: + print( + "Failed to include the `onnx` package; please install to successfully use this script." + ) + exit(1) parser = argparse.ArgumentParser() parser.add_argument( diff --git a/utils/install-onnx-mlir.sh b/utils/install-onnx-mlir.sh index 789d1e4d54..eb8cd562ad 100755 --- a/utils/install-onnx-mlir.sh +++ b/utils/install-onnx-mlir.sh @@ -5,6 +5,7 @@ if [[ -z "$pythonLocation" ]]; then cmake -G Ninja \ -DCMAKE_CXX_COMPILER=/usr/bin/c++ \ -DCMAKE_BUILD_TYPE=Release \ + -DONNX_MLIR_BUILD_TESTS=ON \ -DLLVM_ENABLE_ASSERTIONS=ON \ -DMLIR_DIR=${MLIR_DIR} \ .. @@ -12,6 +13,7 @@ else cmake -G Ninja \ -DCMAKE_CXX_COMPILER=/usr/bin/c++ \ -DCMAKE_BUILD_TYPE=Release \ + -DONNX_MLIR_BUILD_TESTS=ON \ -DLLVM_ENABLE_ASSERTIONS=ON \ -DPython3_ROOT_DIR=$pythonLocation \ -DMLIR_DIR=${MLIR_DIR} \ diff --git a/utils/onnx2json.py b/utils/onnx2json.py index 3f15a64859..1136a710da 100755 --- a/utils/onnx2json.py +++ b/utils/onnx2json.py @@ -10,7 +10,15 @@ ################################################################################ import argparse -import onnx + +try: + import onnx +except ImportError: + print( + "Failed to include the `onnx` package; please install to successfully use this script." + ) + exit(1) + from google.protobuf.json_format import MessageToJson parser = argparse.ArgumentParser() diff --git a/utils/onnx2text.py b/utils/onnx2text.py index 831eeb8e9a..6663a27b66 100755 --- a/utils/onnx2text.py +++ b/utils/onnx2text.py @@ -10,7 +10,15 @@ ################################################################################ import argparse -import onnx + +try: + import onnx +except ImportError: + print( + "Failed to include the `onnx` package; please install to successfully use this script." + ) + exit(1) + import onnx.printer parser = argparse.ArgumentParser() diff --git a/utils/onnxExternalizeData.py b/utils/onnxExternalizeData.py index 02b5bc3dc3..c301f1b0da 100755 --- a/utils/onnxExternalizeData.py +++ b/utils/onnxExternalizeData.py @@ -20,7 +20,15 @@ ################################################################################ import argparse -import onnx + +try: + import onnx +except ImportError: + print( + "Failed to include the `onnx` package; please install to successfully use this script." + ) + exit(1) + import os import sys diff --git a/utils/onnxmlirrun.py b/utils/onnxmlirrun.py index ae1332c68f..256c52d87d 100644 --- a/utils/onnxmlirrun.py +++ b/utils/onnxmlirrun.py @@ -3,7 +3,15 @@ import os import sys -import onnx + +try: + import onnx +except ImportError: + print( + "Failed to include the `onnx` package; please install to successfully use this script." + ) + exit(1) + import time import signal import subprocess diff --git a/utils/pre-onnx-mlir.py b/utils/pre-onnx-mlir.py index 66a5ea4a2c..557540cf08 100644 --- a/utils/pre-onnx-mlir.py +++ b/utils/pre-onnx-mlir.py @@ -12,7 +12,14 @@ python pre-onnx-mlir add.onnx -v --no_convert """ -import onnx +try: + import onnx +except ImportError: + print( + "Failed to include the `onnx` package; please install to successfully use this script." + ) + exit(1) + import argparse from onnx import version_converter, helper