Skip to content

Commit

Permalink
Optimize CPU and Memory performance for Resize linear mode parser
Browse files Browse the repository at this point in the history
Re-write calc_neighbor_points() by composing index from binary bits
instead of recursion.

With the optimized calc_neighbor_points(), CPU time required by 90% and
peak memory utilization is significantly reduced.

Perf. comparision on VM w/ 12-Core EPYC 9V64 + 128 GB mem:
  n_dim   out_elements   New t-CPU (us)   Old t-CPU (us)   t-CPU Ratio
 ------- -------------- ---------------- ---------------- -------------
      4        786,432          170,377        1,878,299        0.0907
      4      1,572,864          383,125        4,009,335        0.0956
      4      3,145,728          784,388        7,670,960        0.1023
      4      6,291,456        1,567,753       15,095,017        0.1039
      4     12,582,912        3,139,452       29,622,921        0.1060
      4     25,165,824        6,266,153       58,332,233        0.1074
      4     50,331,648       12,517,674      116,923,368        0.1071
      4    100,663,296       25,011,425         OOM Kill           N/A

Signed-off-by: Colin Xu <[email protected]>
  • Loading branch information
coxuamd committed Dec 22, 2024
1 parent 6acc1f9 commit 0e22cd0
Showing 1 changed file with 19 additions and 42 deletions.
61 changes: 19 additions & 42 deletions src/onnx/parse_resize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,49 +33,25 @@ namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace onnx {

static std::vector<int>
static void
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)
const int& n_dims,
const std::size_t& out_elements,
const shape& in_s,
std::vector<int>& vec_ind)
{
if(i_dim == vvv_ind.size())
{
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;
});
for (std::size_t start = 0; start < (1 << n_dims); start++) {

Check warning on line 43 in src/onnx/parse_resize.cpp

View workflow job for this annotation

GitHub Actions / tidy

use of a signed integer operand with a binary bitwise operator [hicpp-signed-bitwise,-warnings-as-errors]
std::size_t bi = start;
std::vector<std::size_t> idx(n_dims);
for (std::size_t idx_e = 0; idx_e < out_elements; idx_e++) {
idx.clear();
for (std::size_t dim = 0; dim < n_dims; dim++) {
idx.push_back(bi & 1 ? vvv_ind[dim][1][idx_e] : vvv_ind[dim][0][idx_e]);

Check warning on line 49 in src/onnx/parse_resize.cpp

View workflow job for this annotation

GitHub Actions / tidy

implicit conversion 'std::size_t' (aka 'unsigned long') -> 'bool' [readability-implicit-bool-conversion,-warnings-as-errors]

Check warning on line 49 in src/onnx/parse_resize.cpp

View workflow job for this annotation

GitHub Actions / tidy

use of a signed integer operand with a binary bitwise operator [hicpp-signed-bitwise,-warnings-as-errors]
}
vec_ind.push_back(in_s.index(idx));
bi = bi >> 1;

Check warning on line 52 in src/onnx/parse_resize.cpp

View workflow job for this annotation

GitHub Actions / tidy

use of a signed integer operand with a binary bitwise operator [hicpp-signed-bitwise,-warnings-as-errors]
}
}
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 +351,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;
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

0 comments on commit 0e22cd0

Please sign in to comment.