|
| 1 | +# Copyright 2021-2022 Arm Limited and Contributors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +cmake_minimum_required(VERSION 3.14) |
| 16 | +project(tvm_utility) |
| 17 | + |
| 18 | +find_package(autoware_cmake REQUIRED) |
| 19 | +autoware_package() |
| 20 | + |
| 21 | +# Configure |
| 22 | + |
| 23 | +set(tvm_utility_NETWORKS_DIR ${neural_networks_provider_NETWORKS_DIR}) |
| 24 | +configure_file(tvm_utility-extras.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/tvm_utility-extras.cmake @ONLY) |
| 25 | + |
| 26 | +foreach(network_name ${neural_networks_provider_NAMES}) |
| 27 | + list(APPEND MODEL_INCLUDES_LIST |
| 28 | + "#define INCLUDE <${network_name}/NETWORKS_BACKEND/inference_engine_tvm_config.hpp>" |
| 29 | + "#include INCLUDE" |
| 30 | + "#undef INCLUDE" |
| 31 | + ) |
| 32 | +endforeach() |
| 33 | +list(JOIN MODEL_INCLUDES_LIST "\n" GENERATED_MODEL_INCLUDES) |
| 34 | +configure_file(model_zoo.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/include/tvm_utility/model_zoo.hpp) |
| 35 | + |
| 36 | +# Library |
| 37 | + |
| 38 | +set(TVM_UTILITY_NODE_LIB_HEADERS |
| 39 | + "include/${PROJECT_NAME}/pipeline.hpp" |
| 40 | + "${CMAKE_CURRENT_BINARY_DIR}/include/tvm_utility/model_zoo.hpp" |
| 41 | +) |
| 42 | + |
| 43 | +ament_auto_add_library(${PROJECT_NAME} SHARED ${TVM_UTILITY_NODE_LIB_HEADERS}) |
| 44 | +set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX) |
| 45 | + |
| 46 | +ament_export_include_directories(${tvm_utility_NETWORKS_DIR}) |
| 47 | +install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/ DESTINATION include) |
| 48 | + |
| 49 | +if(BUILD_TESTING) |
| 50 | + # Get target backend |
| 51 | + set(${PROJECT_NAME}_BACKEND llvm CACHE STRING "${PROJECT_NAME} neural network backend") |
| 52 | + |
| 53 | + # compile each folder inside test/ as a test case |
| 54 | + find_package(ament_cmake_gtest REQUIRED) |
| 55 | + find_package(OpenCV REQUIRED) |
| 56 | + find_package(tvm_vendor REQUIRED) |
| 57 | + set(tvm_runtime_DIR ${tvm_vendor_DIR}) |
| 58 | + find_package(tvm_runtime CONFIG REQUIRED) |
| 59 | + include(${CMAKE_CURRENT_BINARY_DIR}/tvm_utility-extras.cmake) |
| 60 | + |
| 61 | + set(TEST_ARTIFACTS "${CMAKE_CURRENT_LIST_DIR}/artifacts") |
| 62 | + file(GLOB TEST_CASES test/*) |
| 63 | + foreach(TEST_FOLDER ${TEST_CASES}) |
| 64 | + if(NOT IS_DIRECTORY ${TEST_FOLDER}) |
| 65 | + continue() |
| 66 | + endif() |
| 67 | + # the folder name becomes the test case name |
| 68 | + file(RELATIVE_PATH TEST_CASE_NAME ${CMAKE_CURRENT_LIST_DIR}/test ${TEST_FOLDER}) |
| 69 | + |
| 70 | + # Test if files exist. The result is set in ${TEST_CASE_NAME}_FOUND |
| 71 | + autoware_check_neural_network(${TEST_CASE_NAME} "${${PROJECT_NAME}_BACKEND}") |
| 72 | + |
| 73 | + if(${TEST_CASE_NAME}_FOUND) |
| 74 | + if(TEST_CASE_NAME STREQUAL "yolo_v2_tiny" AND |
| 75 | + NOT EXISTS ${TEST_ARTIFACTS}/yolo_v2_tiny/test_image_0.jpg) |
| 76 | + message(WARNING "Missing image artifact for yolo_v2_tiny, skipping test") |
| 77 | + continue() |
| 78 | + endif() |
| 79 | + # add all cpp files in the folder to the target |
| 80 | + file(GLOB TEST_CASE_SOURCES ${TEST_FOLDER}/*.cpp) |
| 81 | + ament_add_gtest(${TEST_CASE_NAME} ${TEST_CASE_SOURCES}) |
| 82 | + ament_target_dependencies(${TEST_CASE_NAME} |
| 83 | + "ament_index_cpp" |
| 84 | + "tvm_vendor" |
| 85 | + "sensor_msgs") |
| 86 | + |
| 87 | + target_link_libraries("${TEST_CASE_NAME}" |
| 88 | + "${OpenCV_LIBRARIES}" |
| 89 | + "${tvm_runtime_LIBRARIES}" |
| 90 | + ) |
| 91 | + |
| 92 | + target_include_directories("${TEST_CASE_NAME}" SYSTEM PUBLIC |
| 93 | + "${OpenCV_INCLUDE_DIRS}" |
| 94 | + "${tvm_vendor_INCLUDE_DIRS}" |
| 95 | + "include" |
| 96 | + "${CMAKE_CURRENT_BINARY_DIR}/include" |
| 97 | + "${tvm_utility_NETWORKS_DIR}" |
| 98 | + ) |
| 99 | + |
| 100 | + target_compile_definitions(${TEST_CASE_NAME} PRIVATE NETWORKS_BACKEND=${${PROJECT_NAME}_BACKEND}) |
| 101 | + |
| 102 | + # Install test-specific files |
| 103 | + if(IS_DIRECTORY ${TEST_ARTIFACTS}/${TEST_CASE_NAME}) |
| 104 | + install(DIRECTORY ${TEST_ARTIFACTS}/${TEST_CASE_NAME}/ |
| 105 | + DESTINATION ${CMAKE_BINARY_DIR}/${TEST_CASE_NAME}_artifacts |
| 106 | + ) |
| 107 | + endif() |
| 108 | + |
| 109 | + else() |
| 110 | + message(WARNING "No model is generated for ${TEST_FOLDER}, skipping test") |
| 111 | + endif() |
| 112 | + |
| 113 | + endforeach() |
| 114 | +endif() |
| 115 | + |
| 116 | +list(APPEND ${PROJECT_NAME}_CONFIG_EXTRAS "${CMAKE_CURRENT_BINARY_DIR}/tvm_utility-extras.cmake") |
| 117 | +ament_auto_package() |
0 commit comments