Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tensorrt_common): add gtest #5174

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions common/tensorrt_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,45 @@ if(NOT (CUDAToolkit_FOUND AND CUDNN_FOUND AND TENSORRT_FOUND))
return()
endif()

# Download onnx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We decided not to include download scripts in CMake.
Maybe we could skip TestGetInputDims for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without downloading the model this package will not be able to be tested.
so do I go ahead and write test case for this package?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, this package is not really used in autoware.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about that? The header files seems to be included by the other packages. (example)

I will discuss this in the next Software WG if we could consider adding sample models in the repository or not for tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, this package is not really used in autoware.

@mitsudome-r
sorry, I didn't make it clear, there are 3 packages in the perception folder related to tensorrt: tensorrt_classifier , tensorrt_yolo, tensorrt_yolox, where both tensorrt_classifier and tensorrt_yolox contain the tensorrt_common, but tensorrt_yolo does not contain it. for our real car tests we only used the tensorrt_yolo.

set(DATA_PATH "${CMAKE_CURRENT_SOURCE_DIR}/data")
if(NOT EXISTS "${DATA_PATH}")
execute_process(COMMAND mkdir -p ${DATA_PATH})
endif()
function(download FILE_NAME FILE_HASH)
message(STATUS "Checking and downloading ${FILE_NAME}")
set(FILE_PATH ${DATA_PATH}/${FILE_NAME})
set(STATUS_CODE 0)
message(STATUS "start ${FILE_NAME}")
if(EXISTS ${FILE_PATH})
message(STATUS "found ${FILE_NAME}")
file(MD5 ${FILE_PATH} EXISTING_FILE_HASH)
if(${FILE_HASH} STREQUAL ${EXISTING_FILE_HASH})
message(STATUS "same ${FILE_NAME}")
message(STATUS "File already exists.")
else()
message(STATUS "diff ${FILE_NAME}")
message(STATUS "File hash changes. Downloading now ...")
file(DOWNLOAD https://awf.ml.dev.web.auto/perception/models/${FILE_NAME} ${FILE_PATH} STATUS DOWNLOAD_STATUS TIMEOUT 3600)
list(GET DOWNLOAD_STATUS 0 STATUS_CODE)
list(GET DOWNLOAD_STATUS 1 ERROR_MESSAGE)
endif()
else()
message(STATUS "not found ${FILE_NAME}")
message(STATUS "File doesn't exists. Downloading now ...")
file(DOWNLOAD https://awf.ml.dev.web.auto/perception/models/${FILE_NAME} ${FILE_PATH} STATUS DOWNLOAD_STATUS TIMEOUT 3600)
list(GET DOWNLOAD_STATUS 0 STATUS_CODE)
list(GET DOWNLOAD_STATUS 1 ERROR_MESSAGE)
endif()
if(${STATUS_CODE} EQUAL 0)
message(STATUS "Download completed successfully!")
else()
message(FATAL_ERROR "Error occurred during download: ${ERROR_MESSAGE}")
endif()
endfunction()

download(yolov5s.onnx 646b295db6089597e79163446d6eedca)

add_library(${PROJECT_NAME} SHARED
src/tensorrt_common.cpp
src/simple_profiler.cpp
Expand Down Expand Up @@ -61,6 +100,11 @@ if(BUILD_TESTING)
set(ament_cmake_uncrustify_FOUND TRUE)

ament_lint_auto_find_test_dependencies()

file(GLOB_RECURSE test_files test/*.cpp)
ament_add_ros_isolated_gtest(test_tensorrt_common ${test_files})
target_link_libraries(test_tensorrt_common ${PROJECT_NAME})

endif()

install(TARGETS ${PROJECT_NAME} EXPORT export_${PROJECT_NAME})
Expand Down
1 change: 1 addition & 0 deletions common/tensorrt_common/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<depend>rclcpp</depend>

<test_depend>ament_cmake_ros</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

Expand Down
43 changes: 43 additions & 0 deletions common/tensorrt_common/test/test_tensorrt_common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2023 TIER IV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "tensorrt_common/tensorrt_common.hpp"

#include <gtest/gtest.h>

// test get_input_dims function
TEST(TrtCommonTest, TestGetInputDims)
{
std::string onnx_file_path =
"src/universe/autoware.universe/common/tensorrt_common/data/yolov5s.onnx";
nvinfer1::Dims input_dims = tensorrt_common::get_input_dims(onnx_file_path);
ASSERT_GT(input_dims.nbDims, 0);
}

// test is_valid_precision_string function
TEST(TrtCommonTest, TestIsValidPrecisionString)
{
std::string valid_precision = "fp16";
std::string invalid_precision = "invalid_precision";
ASSERT_TRUE(tensorrt_common::is_valid_precision_string(valid_precision));
ASSERT_FALSE(tensorrt_common::is_valid_precision_string(invalid_precision));
}

// In the future, more test cases will be written to test the functionality of TrtCommon class

int main(int argc, char * argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Loading