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

Optimize CPU and Memory performance for Resize linear mode parser #3731

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
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
64 changes: 22 additions & 42 deletions src/onnx/parse_resize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,49 +33,28 @@ namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace onnx {

static std::vector<int>
calc_neighbor_points(const std::vector<std::vector<std::vector<std::size_t>>>& vvv_ind,
int i_dim,
std::vector<std::vector<std::size_t>> vec_dims,
const shape& in_s)
static void calc_neighbor_points(const std::vector<std::vector<std::vector<std::size_t>>>& vvv_ind,
coxuamd marked this conversation as resolved.
Show resolved Hide resolved
const std::size_t& n_dims,
const std::size_t& out_elements,
const shape& in_s,
std::vector<int>& vec_ind)
{
if(i_dim == vvv_ind.size())
for(std::size_t start = 0; start < (std::size_t{1} << n_dims); start++)
coxuamd marked this conversation as resolved.
Show resolved Hide resolved
{
std::vector<int> vec_ind(vec_dims.size());
std::transform(vec_dims.begin(), vec_dims.end(), vec_ind.begin(), [&](auto idx) {
return static_cast<int>(in_s.index(idx));
});
return vec_ind;
}

const auto& vv_lo = vvv_ind[i_dim][0];
std::vector<std::vector<std::size_t>> vec_dims1;
for(std::size_t start = 0; start < vec_dims.size(); start += vv_lo.size())
{
std::transform(vv_lo.begin(),
vv_lo.end(),
vec_dims.begin() + start,
std::back_inserter(vec_dims1),
[](auto i, auto dim) {
dim.push_back(i);
return dim;
});
}

const auto& vv_hi = vvv_ind[i_dim][1];
for(std::size_t start = 0; start < vec_dims.size(); start += vv_hi.size())
{
std::transform(vv_hi.begin(),
vv_hi.end(),
vec_dims.begin() + start,
std::back_inserter(vec_dims1),
[](auto i, auto dim) {
dim.push_back(i);
return dim;
});
std::vector<std::size_t> idx(n_dims);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any value in defining idx it of size n_dims, when it gets cleared in the for-loop below?

Copy link
Author

Choose a reason for hiding this comment

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

On defining, idx is empty.

  • In each out_elements loop, idx is cleared.
  • In each n_dims loop, using one bit to pick elements from vvv_ind() last dimension, and compose a new vector.
  • Use the new vector to get index in graph.
  • Restart next out_elements loop.

Copy link
Author

Choose a reason for hiding this comment

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

Add more comments above function to describe the algorithm.

for(std::size_t idx_e = 0; idx_e < out_elements; idx_e++)
coxuamd marked this conversation as resolved.
Show resolved Hide resolved
{
std::size_t bi = start;
idx.clear();
for(std::size_t dim = 0; dim < n_dims; dim++)
Copy link
Contributor

Choose a reason for hiding this comment

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

There are two for-loops in the prior code, and they seem to fill in a certain way -- which might be more complex than what is now done in this single for-loop. I am not clear on this code, but I would like to see some test cases being added with this PR, and that would help me see it better.

Copy link
Author

Choose a reason for hiding this comment

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

Add more comments above function to describe the algorithm.

{
idx.push_back(static_cast<bool>(bi & std::size_t{1}) ? vvv_ind[dim][1][idx_e]
coxuamd marked this conversation as resolved.
Show resolved Hide resolved
: vvv_ind[dim][0][idx_e]);
bi = bi >> std::size_t{1};
coxuamd marked this conversation as resolved.
Show resolved Hide resolved
}
vec_ind.push_back(in_s.index(idx));
}
}
vec_dims.clear();
return calc_neighbor_points(vvv_ind, i_dim + 1, std::move(vec_dims1), in_s);
}

static std::string get_coord_trans_mode(const onnx_parser::attribute_map& attr)
Expand Down Expand Up @@ -375,8 +354,9 @@ struct parse_resize : op_parser<parse_resize>
}
});

auto ind = calc_neighbor_points(
vvv_ind, 0, std::vector<std::vector<std::size_t>>(out_elements), in_s);
std::vector<int> ind;
coxuamd marked this conversation as resolved.
Show resolved Hide resolved
calc_neighbor_points(vvv_ind, n_dim, out_elements, in_s, ind);

auto ind_lens = out_lens;
ind_lens[0] *= (std::size_t{1} << n_dim);
shape ind_s{shape::int32_type, ind_lens};
Expand Down
Loading