Skip to content

Commit

Permalink
feat: add tensorrt_yolox package (#2370)
Browse files Browse the repository at this point in the history
* feat: clone tensorrt_yolox into autoware.universe

* build: clone cuda_utils and tensorrt_common into lib

* feat: support downloading pretrained model during build

* fix: make label being case insensitive

Now, all labels are treated as upper case

* fix: support COCO-style label

* docs: add README for tensorrt_yolox package

* fix: remove pre-commit error

* build: replace some boilerplate code by autoware_cmake

* refactor: divide tensorrt_common and cuda_utils into separated packages

* fix: add maintainer

* fix: correct typo

* use autoware lint common

Signed-off-by: Daisuke Nishimatsu <border_goldenmarket@yahoo.co.jp>

Signed-off-by: Daisuke Nishimatsu <border_goldenmarket@yahoo.co.jp>
Co-authored-by: Manato HIRABAYASHI <manato.hirabayashi@tier4.jp>
Co-authored-by: Daisuke Nishimatsu <border_goldenmarket@yahoo.co.jp>
  • Loading branch information
3 people authored Nov 30, 2022
1 parent a85643f commit 03de605
Show file tree
Hide file tree
Showing 17 changed files with 1,766 additions and 0 deletions.
22 changes: 22 additions & 0 deletions common/cuda_utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.5)
project(cuda_utils)

find_package(autoware_cmake REQUIRED)
autoware_package()

find_package(CUDA REQUIRED)

install(
DIRECTORY include/${PROJECT_NAME}/
DESTINATION include/${PROJECT_NAME}
)

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()

ament_export_include_directories(include)
ament_export_dependencies(CUDA)

ament_auto_package()
44 changes: 44 additions & 0 deletions common/cuda_utils/include/cuda_utils/cuda_check_error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2022 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.

// This code is licensed under CC0 1.0 Universal (Public Domain).
// You can use this without any limitation.
// https://creativecommons.org/publicdomain/zero/1.0/deed.en
// borrowed from https://proc-cpuinfo.fixstars.com/2019/02/cuda_smart_pointer/

#ifndef CUDA_UTILS__CUDA_CHECK_ERROR_HPP_
#define CUDA_UTILS__CUDA_CHECK_ERROR_HPP_

#include <cuda_runtime_api.h>

#include <sstream>
#include <stdexcept>

namespace cuda_utils
{
template <typename F, typename N>
void cuda_check_error(const ::cudaError_t e, F && f, N && n)
{
if (e != ::cudaSuccess) {
std::stringstream s;
s << ::cudaGetErrorName(e) << " (" << e << ")@" << f << "#L" << n << ": "
<< ::cudaGetErrorString(e);
throw std::runtime_error{s.str()};
}
}
} // namespace cuda_utils

#define CHECK_CUDA_ERROR(e) (cuda_utils::cuda_check_error(e, __FILE__, __LINE__))

#endif // CUDA_UTILS__CUDA_CHECK_ERROR_HPP_
81 changes: 81 additions & 0 deletions common/cuda_utils/include/cuda_utils/cuda_unique_ptr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2022 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.

// This code is licensed under CC0 1.0 Universal (Public Domain).
// You can use this without any limitation.
// https://creativecommons.org/publicdomain/zero/1.0/deed.en
// borrowed from https://proc-cpuinfo.fixstars.com/2019/02/cuda_smart_pointer/

#ifndef CUDA_UTILS__CUDA_UNIQUE_PTR_HPP_
#define CUDA_UTILS__CUDA_UNIQUE_PTR_HPP_

#include "cuda_utils/cuda_check_error.hpp"

#include <memory>
#include <type_traits>

namespace cuda_utils
{
struct CudaDeleter
{
void operator()(void * p) const { CHECK_CUDA_ERROR(::cudaFree(p)); }
};
template <typename T>
using CudaUniquePtr = std::unique_ptr<T, CudaDeleter>;

template <typename T>
typename std::enable_if_t<std::is_array<T>::value, CudaUniquePtr<T>> make_unique(
const std::size_t n)
{
using U = typename std::remove_extent_t<T>;
U * p;
CHECK_CUDA_ERROR(::cudaMalloc(reinterpret_cast<void **>(&p), sizeof(U) * n));
return CudaUniquePtr<T>{p};
}

template <typename T>
CudaUniquePtr<T> make_unique()
{
T * p;
CHECK_CUDA_ERROR(::cudaMalloc(reinterpret_cast<void **>(&p), sizeof(T)));
return CudaUniquePtr<T>{p};
}

struct CudaDeleterHost
{
void operator()(void * p) const { CHECK_CUDA_ERROR(::cudaFreeHost(p)); }
};
template <typename T>
using CudaUniquePtrHost = std::unique_ptr<T, CudaDeleterHost>;

template <typename T>
typename std::enable_if_t<std::is_array<T>::value, CudaUniquePtrHost<T>> make_unique_host(
const std::size_t n, unsigned int flag)
{
using U = typename std::remove_extent_t<T>;
U * p;
CHECK_CUDA_ERROR(::cudaHostAlloc(reinterpret_cast<void **>(&p), sizeof(U) * n, flag));
return CudaUniquePtrHost<T>{p};
}

template <typename T>
CudaUniquePtrHost<T> make_unique_host(unsigned int flag = cudaHostAllocDefault)
{
T * p;
CHECK_CUDA_ERROR(::cudaHostAlloc(reinterpret_cast<void **>(&p), sizeof(T), flag));
return CudaUniquePtrHost<T>{p};
}
} // namespace cuda_utils

#endif // CUDA_UTILS__CUDA_UNIQUE_PTR_HPP_
52 changes: 52 additions & 0 deletions common/cuda_utils/include/cuda_utils/stream_unique_ptr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2022 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.

// This code is licensed under CC0 1.0 Universal (Public Domain).
// You can use this without any limitation.
// https://creativecommons.org/publicdomain/zero/1.0/deed.en
// borrowed from https://proc-cpuinfo.fixstars.com/2019/02/cuda_smart_pointer/

#ifndef CUDA_UTILS__STREAM_UNIQUE_PTR_HPP_
#define CUDA_UTILS__STREAM_UNIQUE_PTR_HPP_

#include <cuda_runtime_api.h>

#include <memory>

namespace cuda_utils
{
struct StreamDeleter
{
void operator()(cudaStream_t * stream)
{
if (stream) {
cudaStreamDestroy(*stream);
delete stream;
}
}
};

using StreamUniquePtr = std::unique_ptr<cudaStream_t, StreamDeleter>;

inline StreamUniquePtr makeCudaStream(const uint32_t flags = cudaStreamDefault)
{
StreamUniquePtr stream(new cudaStream_t, StreamDeleter());
if (cudaStreamCreateWithFlags(stream.get(), flags) != cudaSuccess) {
stream.reset(nullptr);
}
return stream;
}
} // namespace cuda_utils

#endif // CUDA_UTILS__STREAM_UNIQUE_PTR_HPP_
23 changes: 23 additions & 0 deletions common/cuda_utils/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<package format="3">
<name>cuda_utils</name>
<version>0.0.1</version>
<description>cuda utility library</description>

<author email="daisuke.nishimatsu@tier4.jp">Daisuke Nishimatsu</author>
<maintainer email="daisuke.nishimatsu@tier4.jp">Daisuke Nishimatsu</maintainer>
<maintainer email="manato.hirabayashi@tier4.jp">Manato Hirabayashi</maintainer>

<license>Apache License 2.0</license>

<buildtool_depend>ament_cmake_auto</buildtool_depend>

<build_depend>autoware_cmake</build_depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>autoware_lint_common</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
54 changes: 54 additions & 0 deletions common/tensorrt_common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
cmake_minimum_required(VERSION 3.5)
project(tensorrt_common)

find_package(autoware_cmake REQUIRED)
autoware_package()

find_package(CUDA REQUIRED)
find_package(CUDNN REQUIRED)
find_package(TENSORRT REQUIRED)

if(NOT (${CUDA_FOUND} AND ${CUDNN_FOUND} AND ${TENSORRT_FOUND}))
message(WARNING "cuda, cudnn, tensorrt libraries are not found")
return()
endif()

cuda_add_library(${PROJECT_NAME} SHARED
src/tensorrt_common.cpp
)

ament_target_dependencies(${PROJECT_NAME}
rclcpp
)

target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
${TENSORRT_INCLUDE_DIRS}
)

target_link_libraries(${PROJECT_NAME}
${TENSORRT_LIBRARIES}
stdc++fs
)

target_compile_definitions(${PROJECT_NAME} PRIVATE
TENSORRT_VERSION_MAJOR=${TENSORRT_VERSION_MAJOR}
)

list(APPEND ${PROJECT_NAME}_LIBRARIES "${PROJECT_NAME}")

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()

ament_export_include_directories(include)
ament_export_dependencies(CUDA)
ament_export_dependencies(cudnn_cmake_module)
ament_export_dependencies(CUDNN)
ament_export_dependencies(tensorrt_cmake_module)
ament_export_dependencies(TENSORRT)

ament_auto_package()
Loading

0 comments on commit 03de605

Please sign in to comment.