-
Notifications
You must be signed in to change notification settings - Fork 892
test(autoware_tensorrt_plugins): add reference kernel tests #12561
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
Open
mojomex
wants to merge
7
commits into
autowarefoundation:main
Choose a base branch
from
mojomex:test/tensorrt-reference-kernels
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b13d749
test(autoware_tensorrt_plugins): add reference kernel tests
mojomex 757c360
style(pre-commit): autofix
pre-commit-ci-lite[bot] 2fa44b3
refactor(autoware_tensorrt_plugins): split tests into logical units
mojomex 676007a
chore: add include guard
mojomex fd53532
style(pre-commit): autofix
pre-commit-ci-lite[bot] a46dee5
chore: remove unused includes
mojomex 5002ac5
test: hopefully made tests GPU-less CI friendly
mojomex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
perception/autoware_tensorrt_plugins/test/argsort_kernel_test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Copyright 2026 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 "autoware/argsort_ops/argsort.hpp" | ||
| #include "test_utils.hpp" | ||
|
|
||
| #include <autoware/cuda_utils/cuda_gtest_utils.hpp> | ||
|
|
||
| #include <cuda_runtime_api.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <algorithm> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <vector> | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| using autoware::tensorrt_plugins::test::copy_to_device; | ||
| using autoware::tensorrt_plugins::test::copy_to_host; | ||
| using autoware::tensorrt_plugins::test::CudaStreamGuard; | ||
| using autoware::tensorrt_plugins::test::DeviceBuffer; | ||
|
|
||
| std::size_t get_argsort_total_workspace_size(const std::size_t num_elements) | ||
| { | ||
| const auto temp_size = get_argsort_workspace_size(num_elements); | ||
| const auto scratch_offset = | ||
| ((temp_size + alignof(std::int64_t) - 1U) / alignof(std::int64_t)) * alignof(std::int64_t); | ||
| return scratch_offset + sizeof(std::int64_t) * 2U * num_elements; | ||
| } | ||
|
|
||
| std::vector<std::int64_t> make_argsort_reference(const std::vector<std::int64_t> & input) | ||
| { | ||
| std::vector<std::int64_t> indices(input.size()); | ||
| for (std::size_t index = 0; index < input.size(); ++index) { | ||
| indices[index] = static_cast<std::int64_t>(index); | ||
| } | ||
|
|
||
| std::stable_sort( | ||
| indices.begin(), indices.end(), | ||
| [&input](const std::int64_t lhs, const std::int64_t rhs) { return input[lhs] < input[rhs]; }); | ||
|
|
||
| return indices; | ||
| } | ||
|
|
||
| TEST(ReferenceKernelsTest, ArgsortMatchesCpuReference) | ||
| { | ||
| SKIP_TEST_IF_CUDA_UNAVAILABLE(); | ||
|
|
||
| const std::vector<std::int64_t> input{7, 3, 7, 5, 3, 3, 9, 5, 11, 7}; | ||
| const auto reference = make_argsort_reference(input); | ||
|
|
||
| CudaStreamGuard stream; | ||
| DeviceBuffer<std::int64_t> input_d(input.size()); | ||
| DeviceBuffer<std::int64_t> output_d(input.size()); | ||
| DeviceBuffer<std::uint8_t> workspace_d(get_argsort_total_workspace_size(input.size())); | ||
|
|
||
| copy_to_device(input_d.get(), input); | ||
|
|
||
| ASSERT_EQ( | ||
| argsort( | ||
| input_d.get(), output_d.get(), workspace_d.get(), input.size(), | ||
| get_argsort_workspace_size(input.size()), stream.get()), | ||
| cudaSuccess); | ||
| ASSERT_EQ(cudaStreamSynchronize(stream.get()), cudaSuccess); | ||
|
|
||
| EXPECT_EQ(copy_to_host(output_d.get(), input.size()), reference); | ||
| } | ||
|
|
||
| } // namespace | ||
109 changes: 109 additions & 0 deletions
109
perception/autoware_tensorrt_plugins/test/test_utils.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright 2026 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cuda_runtime_api.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <cstddef> | ||
| #include <stdexcept> | ||
| #include <vector> | ||
|
|
||
| namespace autoware::tensorrt_plugins::test | ||
| { | ||
|
|
||
| class CudaStreamGuard | ||
| { | ||
| public: | ||
| CudaStreamGuard() | ||
| { | ||
| const cudaError_t status = cudaStreamCreate(&stream_); | ||
| if (status != cudaSuccess) { | ||
| throw std::runtime_error(cudaGetErrorString(status)); | ||
| } | ||
| } | ||
|
|
||
| ~CudaStreamGuard() | ||
| { | ||
| if (stream_ != nullptr) { | ||
| cudaStreamDestroy(stream_); | ||
| } | ||
| } | ||
|
|
||
| CudaStreamGuard(const CudaStreamGuard &) = delete; | ||
| CudaStreamGuard & operator=(const CudaStreamGuard &) = delete; | ||
| CudaStreamGuard(CudaStreamGuard &&) = delete; | ||
| CudaStreamGuard & operator=(CudaStreamGuard &&) = delete; | ||
|
|
||
| [[nodiscard]] cudaStream_t get() const { return stream_; } | ||
|
|
||
| private: | ||
| cudaStream_t stream_{nullptr}; | ||
| }; | ||
|
|
||
| template <typename T> | ||
| class DeviceBuffer | ||
| { | ||
| public: | ||
| explicit DeviceBuffer(std::size_t element_count) : element_count_(element_count) | ||
| { | ||
| const cudaError_t status = cudaMalloc(&data_, sizeof(T) * element_count_); | ||
| if (status != cudaSuccess) { | ||
| throw std::runtime_error(cudaGetErrorString(status)); | ||
| } | ||
| } | ||
|
|
||
| ~DeviceBuffer() | ||
| { | ||
| if (data_ != nullptr) { | ||
| cudaFree(data_); | ||
| } | ||
| } | ||
|
|
||
| DeviceBuffer(const DeviceBuffer &) = delete; | ||
| DeviceBuffer & operator=(const DeviceBuffer &) = delete; | ||
| DeviceBuffer(DeviceBuffer &&) = delete; | ||
| DeviceBuffer & operator=(DeviceBuffer &&) = delete; | ||
|
|
||
| [[nodiscard]] T * get() const { return static_cast<T *>(data_); } | ||
|
|
||
| private: | ||
| void * data_{nullptr}; | ||
| std::size_t element_count_{0U}; | ||
| }; | ||
|
|
||
| template <typename T> | ||
| void copy_to_device(T * device_ptr, const std::vector<T> & host_values) | ||
| { | ||
| const cudaError_t status = cudaMemcpy( | ||
| device_ptr, host_values.data(), sizeof(T) * host_values.size(), cudaMemcpyHostToDevice); | ||
| if (status != cudaSuccess) { | ||
| throw std::runtime_error(cudaGetErrorString(status)); | ||
| } | ||
| } | ||
|
|
||
| template <typename T> | ||
| std::vector<T> copy_to_host(const T * device_ptr, const std::size_t element_count) | ||
| { | ||
| std::vector<T> host_values(element_count); | ||
| const cudaError_t status = | ||
| cudaMemcpy(host_values.data(), device_ptr, sizeof(T) * element_count, cudaMemcpyDeviceToHost); | ||
| if (status != cudaSuccess) { | ||
| throw std::runtime_error(cudaGetErrorString(status)); | ||
| } | ||
| return host_values; | ||
| } | ||
|
|
||
| } // namespace autoware::tensorrt_plugins::test |
99 changes: 99 additions & 0 deletions
99
perception/autoware_tensorrt_plugins/test/unique_kernel_test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // Copyright 2026 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 "autoware/unique_ops/unique.hpp" | ||
| #include "test_utils.hpp" | ||
|
|
||
| #include <autoware/cuda_utils/cuda_gtest_utils.hpp> | ||
|
|
||
| #include <cuda_runtime_api.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <algorithm> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <map> | ||
| #include <vector> | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| using autoware::tensorrt_plugins::test::copy_to_device; | ||
| using autoware::tensorrt_plugins::test::copy_to_host; | ||
| using autoware::tensorrt_plugins::test::CudaStreamGuard; | ||
| using autoware::tensorrt_plugins::test::DeviceBuffer; | ||
|
|
||
| struct UniqueReference | ||
| { | ||
| std::vector<std::int64_t> unique_values; | ||
| std::vector<std::int64_t> inverse_indices; | ||
| std::vector<std::int64_t> counts; | ||
| }; | ||
|
|
||
| UniqueReference make_unique_reference(const std::vector<std::int64_t> & input) | ||
| { | ||
| UniqueReference reference; | ||
| reference.unique_values = input; | ||
| std::sort(reference.unique_values.begin(), reference.unique_values.end()); | ||
| reference.unique_values.erase( | ||
| std::unique(reference.unique_values.begin(), reference.unique_values.end()), | ||
| reference.unique_values.end()); | ||
|
|
||
| std::map<std::int64_t, std::int64_t> value_to_index; | ||
| for (std::size_t index = 0; index < reference.unique_values.size(); ++index) { | ||
| value_to_index.emplace(reference.unique_values[index], static_cast<std::int64_t>(index)); | ||
| } | ||
| reference.counts.resize(reference.unique_values.size(), 0); | ||
|
|
||
| reference.inverse_indices.reserve(input.size()); | ||
| for (const auto value : input) { | ||
| const auto index = value_to_index.at(value); | ||
| reference.inverse_indices.push_back(index); | ||
| reference.counts[static_cast<std::size_t>(index)] += 1; | ||
| } | ||
|
|
||
| return reference; | ||
| } | ||
|
|
||
| TEST(ReferenceKernelsTest, UniqueMatchesCpuReference) | ||
| { | ||
| SKIP_TEST_IF_CUDA_UNAVAILABLE(); | ||
|
|
||
| const std::vector<std::int64_t> input{7, 3, 7, 5, 3, 3, 9, 5, 11, 7}; | ||
| const UniqueReference reference = make_unique_reference(input); | ||
|
|
||
| CudaStreamGuard stream; | ||
| DeviceBuffer<std::int64_t> input_d(input.size()); | ||
| DeviceBuffer<std::int64_t> unique_d(input.size()); | ||
| DeviceBuffer<std::int64_t> inverse_d(input.size()); | ||
| DeviceBuffer<std::int64_t> counts_d(input.size()); | ||
| DeviceBuffer<std::uint8_t> workspace_d(get_unique_workspace_size(input.size())); | ||
|
|
||
| copy_to_device(input_d.get(), input); | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as the the case of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above reply. |
||
| const auto num_unique = unique( | ||
| input_d.get(), unique_d.get(), inverse_d.get(), counts_d.get(), workspace_d.get(), input.size(), | ||
| get_unique_workspace_size(input.size()), stream.get()); | ||
| ASSERT_EQ(cudaStreamSynchronize(stream.get()), cudaSuccess); | ||
|
|
||
| const auto unique_values = copy_to_host(unique_d.get(), static_cast<std::size_t>(num_unique)); | ||
| const auto inverse_indices = copy_to_host(inverse_d.get(), input.size()); | ||
| const auto counts = copy_to_host(counts_d.get(), static_cast<std::size_t>(num_unique)); | ||
|
|
||
| EXPECT_EQ(unique_values, reference.unique_values); | ||
| EXPECT_EQ(inverse_indices, reference.inverse_indices); | ||
| EXPECT_EQ(counts, reference.counts); | ||
| } | ||
|
|
||
| } // namespace | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the memcpy synchronous behavior:
Since
inputis pageable host memory, it would be better to insert explicit synchronization before operating oninput_d.Alternatively, we can skip this kind of explicit synchronization if
copy_to_devicetakes a CUDA stream as an argument and performscudaMemcpyAsyncinside.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.