Skip to content

Commit

Permalink
refactor: use std::vector and std::array instread of *ptr
Browse files Browse the repository at this point in the history
Signed-off-by: ktro2828 <[email protected]>
  • Loading branch information
ktro2828 committed May 4, 2024
1 parent fc9db66 commit 61960bf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
36 changes: 19 additions & 17 deletions perception/tensorrt_mtr/include/tensorrt_mtr/trajectory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define TENSORRT_MTR__TRAJECTORY_HPP_

#include <algorithm>
#include <array>
#include <cstddef>
#include <vector>

Expand All @@ -29,14 +30,14 @@ constexpr size_t PredictedStateDim = 7;
*/
struct PredictedState
{
explicit PredictedState(const float * state)
: x_(state[0]),
y_(state[1]),
dx_(state[2]),
dy_(state[3]),
yaw_(state[4]),
vx_(state[5]),
vy_(state[6])
explicit PredictedState(const std::array<float, PredictedStateDim> & state)
: x_(state.at(0)),
y_(state.at(1)),
dx_(state.at(2)),
dy_(state.at(3)),
yaw_(state.at(4)),
vx_(state.at(5)),
vy_(state.at(6))
{
}

Expand Down Expand Up @@ -80,13 +81,14 @@ struct PredictedState
*/
struct PredictedMode
{
PredictedMode(const float score, const float * waypoints, const size_t num_future)
PredictedMode(const float score, const std::vector<float> & waypoints, const size_t num_future)
: score_(score), num_future_(num_future)
{
for (size_t t = 0; t < num_future_; ++t) {
const auto start_ptr = waypoints + t * state_dim();
std::vector<float> state(start_ptr, start_ptr + state_dim());
waypoints_.emplace_back(state.data());
const auto start_itr = waypoints.cbegin() + t * state_dim();
std::array<float, PredictedStateDim> state;
std::copy_n(start_itr, PredictedStateDim, state.begin());
waypoints_.emplace_back(state);
}
}

Expand Down Expand Up @@ -124,15 +126,15 @@ struct PredictedTrajectory
* @param num_future The number of predicted timestamps.
*/
PredictedTrajectory(
const float * scores, const float * trajectories, const size_t num_mode,
const std::vector<float> & scores, const std::vector<float> & modes, const size_t num_mode,
const size_t num_future)
: num_mode_(num_mode), num_future_(num_future)
{
for (size_t m = 0; m < num_mode_; ++m) {
const auto score = *(scores + m);
const auto start_ptr = trajectories + m * num_future_ * state_dim();
std::vector<float> waypoints(start_ptr, start_ptr + num_future_ * state_dim());
modes_.emplace_back(score, waypoints.data(), num_future_);
const auto score = scores.at(m);
const auto wp_itr = modes.cbegin() + m * num_future_ * state_dim();
std::vector<float> waypoints(wp_itr, wp_itr + num_future_ * state_dim());
modes_.emplace_back(score, waypoints, num_future_);
}
// sort by score
sort_by_score();
Expand Down
4 changes: 2 additions & 2 deletions perception/tensorrt_mtr/include/tensorrt_mtr/trt_mtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ class TrtMTR
// outputs
cuda::unique_ptr<float[]> d_out_score_{nullptr};
cuda::unique_ptr<float[]> d_out_trajectory_{nullptr};
std::unique_ptr<float[]> h_out_score_{nullptr};
std::unique_ptr<float[]> h_out_trajectory_{nullptr};
std::vector<float> h_out_score_;
std::vector<float> h_out_trajectory_;
}; // class TrtMTR
} // namespace trt_mtr
#endif // TENSORRT_MTR__TRT_MTR_HPP_
23 changes: 13 additions & 10 deletions perception/tensorrt_mtr/src/trt_mtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ void TrtMTR::initCudaPtr(const AgentData & agent_data, const PolylineData & poly
d_out_trajectory_ =
cuda::make_unique<float[]>(num_target_ * num_mode_ * num_future_ * PredictedStateDim);

h_out_score_ = std::make_unique<float[]>(sizeof(float) * num_target_ * num_mode_);
h_out_trajectory_ = std::make_unique<float[]>(
sizeof(float) * num_target_ * num_mode_ * num_future_ * PredictedStateDim);

if (builder_->isDynamic()) {
// trajectory: (B, N, T, Da)
builder_->setBindingDimensions(
Expand Down Expand Up @@ -198,20 +194,27 @@ bool TrtMTR::postProcess(
num_target_, num_mode_, num_future_, agent_data.state_dim(), d_target_state_.get(),
PredictedStateDim, d_out_trajectory_.get(), stream_));

h_out_score_.clear();
h_out_trajectory_.clear();
h_out_score_.reserve(num_target_ * num_mode_);
h_out_trajectory_.reserve(num_target_ * num_mode_ * num_future_ * PredictedStateDim);

CHECK_CUDA_ERROR(cudaMemcpyAsync(
h_out_score_.get(), d_out_score_.get(), sizeof(float) * num_target_ * num_mode_,
h_out_score_.data(), d_out_score_.get(), sizeof(float) * num_target_ * num_mode_,
cudaMemcpyDeviceToHost, stream_));
CHECK_CUDA_ERROR(cudaMemcpyAsync(
h_out_trajectory_.get(), d_out_trajectory_.get(),
h_out_trajectory_.data(), d_out_trajectory_.get(),
sizeof(float) * num_target_ * num_mode_ * num_future_ * PredictedStateDim,
cudaMemcpyDeviceToHost, stream_));

trajectories.reserve(num_target_);
for (auto b = 0; b < num_target_; ++b) {
const auto score_ptr = h_out_score_.get() + b * num_mode_;
const auto trajectory_ptr =
h_out_trajectory_.get() + b * num_mode_ * num_future_ * PredictedStateDim;
trajectories.emplace_back(score_ptr, trajectory_ptr, num_mode_, num_future_);
const auto score_itr = h_out_score_.cbegin() + b * num_mode_;
std::vector<float> scores(score_itr, score_itr + num_mode_);
const auto mode_itr =
h_out_trajectory_.cbegin() + b * num_mode_ * num_future_ * PredictedStateDim;
std::vector<float> modes(mode_itr, mode_itr + num_mode_ * num_future_ * PredictedStateDim);
trajectories.emplace_back(scores, modes, num_mode_, num_future_);
}
return true;
}
Expand Down

0 comments on commit 61960bf

Please sign in to comment.