From a8c7ac9bfd5614128184ff9bc57a56f41761ac3f Mon Sep 17 00:00:00 2001 From: zhujingxuan Date: Fri, 24 Sep 2021 15:44:13 +0800 Subject: [PATCH] solve Signed and Unsigned Integers issue --- .../kernel_compiler/cpu/rank_cpu_kernel.cc | 20 +++++++++--------- .../kernel_compiler/cpu/rank_cpu_kernel.h | 2 +- .../kernel_compiler/cpu/rolling_cpu_kernel.cc | 21 ++++++++++--------- .../kernel_compiler/cpu/rolling_cpu_kernel.h | 2 +- .../kernel_compiler/cpu/shift_cpu_kernel.cc | 16 +++++++------- .../kernel_compiler/cpu/shift_cpu_kernel.h | 11 +++++----- 6 files changed, 37 insertions(+), 35 deletions(-) diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/rank_cpu_kernel.cc b/mindspore/ccsrc/backend/kernel_compiler/cpu/rank_cpu_kernel.cc index 63e719fcac5..64a8d4f0730 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/rank_cpu_kernel.cc +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/rank_cpu_kernel.cc @@ -81,17 +81,17 @@ template void RankCpuKernel::SetFunc() { switch (method_) { case Method::Max: { - func_ = [](int i, int duplicate_count, int culmutive_rank, const AxisIterator &axisIterator, + func_ = [](size_t i, int duplicate_count, int culmutive_rank, const AxisIterator &axisIterator, const size_t *const sort_idx, float *const output_addr) { - for (int j = i - duplicate_count + 1; j < i + 1; ++j) { + for (size_t j = i - duplicate_count + 1; j < i + 1; ++j) { output_addr[axisIterator.GetPos(sort_idx[j])] = i + 1; } }; } break; case Method::Min: { - func_ = [](int i, int duplicate_count, int culmutive_rank, const AxisIterator &axisIterator, + func_ = [](size_t i, int duplicate_count, int culmutive_rank, const AxisIterator &axisIterator, const size_t *const sort_idx, float *const output_addr) { - for (int j = i - duplicate_count + 1; j < i + 1; ++j) { + for (size_t j = i - duplicate_count + 1; j < i + 1; ++j) { output_addr[axisIterator.GetPos(sort_idx[j])] = i - duplicate_count + 2; } }; @@ -102,26 +102,26 @@ void RankCpuKernel::SetFunc() { // = duplicate_count * (2 * i - duplicate_count + 1) / 2 // rank_sum = sum + duplicate_count = duplicate_count * (2 * i - duplicate_count + 3) / 2 // avg = rank_sum / duplicate_count = (2 * i - duplicate_count + 3) / 2 - func_ = [](int i, int duplicate_count, int culmutive_rank, const AxisIterator &axisIterator, + func_ = [](size_t i, int duplicate_count, int culmutive_rank, const AxisIterator &axisIterator, const size_t *const sort_idx, float *const output_addr) { float avg = (2 * i - duplicate_count + 3) / 2.0; - for (int j = i - duplicate_count + 1; j < i + 1; ++j) { + for (size_t j = i - duplicate_count + 1; j < i + 1; ++j) { output_addr[axisIterator.GetPos(sort_idx[j])] = avg; } }; } break; case Method::First: { - func_ = [](int i, int duplicate_count, int culmutive_rank, const AxisIterator &axisIterator, + func_ = [](size_t i, int duplicate_count, int culmutive_rank, const AxisIterator &axisIterator, const size_t *const sort_idx, float *const output_addr) { - for (int j = i - duplicate_count + 1; j < i + 1; ++j) { + for (size_t j = i - duplicate_count + 1; j < i + 1; ++j) { output_addr[axisIterator.GetPos(sort_idx[j])] = j + 1; } }; } break; case Method::Dense: { - func_ = [](int i, int duplicate_count, int culmutive_rank, const AxisIterator &axisIterator, + func_ = [](size_t i, int duplicate_count, int culmutive_rank, const AxisIterator &axisIterator, const size_t *const sort_idx, float *const output_addr) { - for (int j = i - duplicate_count + 1; j < i + 1; ++j) { + for (size_t j = i - duplicate_count + 1; j < i + 1; ++j) { output_addr[axisIterator.GetPos(sort_idx[j])] = culmutive_rank; } }; diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/rank_cpu_kernel.h b/mindspore/ccsrc/backend/kernel_compiler/cpu/rank_cpu_kernel.h index de841f2216d..3ea0897b3fd 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/rank_cpu_kernel.h +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/rank_cpu_kernel.h @@ -65,7 +65,7 @@ class RankCpuKernel : public CPUKernel { // parameters size_t axis_{0}; rank::Method method_{rank::MethodNotDefined}; - std::function func_; + std::function func_; rank::NaOption option_{rank::OptionNotDefined}; bool ascending_{true}; bool pct_{false}; diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/rolling_cpu_kernel.cc b/mindspore/ccsrc/backend/kernel_compiler/cpu/rolling_cpu_kernel.cc index 66788e0fea2..562aceeed51 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/rolling_cpu_kernel.cc +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/rolling_cpu_kernel.cc @@ -38,10 +38,11 @@ void RollingCpuKernel::InitKernel(const CNodePtr &kernel_node) { MS_LOG(EXCEPTION) << "[" << method << "] not supported"; } method_ = kValidMethods.at(method); - window_ = AnfAlgo::GetNodeAttr(kernel_node, WINDOW); - if (window_ <= 0) { - MS_LOG(EXCEPTION) << "window size should not less than 0, but got " << window_; + auto window = AnfAlgo::GetNodeAttr(kernel_node, WINDOW); + if (window <= 0) { + MS_LOG(EXCEPTION) << "window size should not less than 0, but got " << window; } + window_ = LongToInt(window); min_periods_ = AnfAlgo::GetNodeAttr(kernel_node, MIN_PERIODS); if (min_periods_ <= 0) { MS_LOG(EXCEPTION) << "min_periods should not less than 0, but got " << min_periods_; @@ -86,12 +87,12 @@ void RollingCpuKernel::RollingBoundsCalculate() { } else if (closed_ == "neither") { end_offset -= 1; } - int axis_size = axisIterator_.AxisSize(); - for (size_t i = 0; i < axisIterator_.AxisSize(); ++i) { + int axis_size = SizeToInt(axisIterator_.AxisSize()); + for (int i = 0; i < axis_size; ++i) { int end = offset + i + end_offset; int start = offset + i - window_ + start_offset; - ends_[i] = std::max(0, std::min(end, axis_size)); - starts_[i] = std::max(0, std::min(start, axis_size)); + ends_[i] = IntToSize(std::max(0, std::min(end, axis_size))); + starts_[i] = IntToSize(std::max(0, std::min(start, axis_size))); } } @@ -135,13 +136,13 @@ void RollingCpuKernel::MethodSwitch() { for (size_t x = start; x < end; ++x) { sum += input_addr[ids[x]]; } - return sum * 1.0 / (end - start); + return sum / SizeToFloat(end - start); }; break; case Method::Var: reduceMethod_ = [](const T *input_addr, const size_t *ids, size_t start, size_t end) { // float for division - float n = end - start; + float n = SizeToFloat(end - start); T sum1 = 0; for (size_t x = start; x < end; ++x) { sum1 += input_addr[ids[x]]; @@ -159,7 +160,7 @@ void RollingCpuKernel::MethodSwitch() { case Method::Std: reduceMethod_ = [](const T *input_addr, const size_t *ids, size_t start, size_t end) { // float for division - float n = end - start; + float n = SizeToFloat(end - start); T sum1 = 0; for (size_t x = start; x < end; ++x) { sum1 += input_addr[ids[x]]; diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/rolling_cpu_kernel.h b/mindspore/ccsrc/backend/kernel_compiler/cpu/rolling_cpu_kernel.h index 1d58775764f..ca9a15fd390 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/rolling_cpu_kernel.h +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/rolling_cpu_kernel.h @@ -50,7 +50,7 @@ class RollingCpuKernel : public CPUKernel { void RollingBoundsCalculate(); void MethodSwitch(); - int64_t window_{0}; + int32_t window_{0}; int64_t min_periods_{0}; bool center_{false}; std::string closed_{}; diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/shift_cpu_kernel.cc b/mindspore/ccsrc/backend/kernel_compiler/cpu/shift_cpu_kernel.cc index 7a5a8dd0748..7692e95c853 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/shift_cpu_kernel.cc +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/shift_cpu_kernel.cc @@ -51,14 +51,14 @@ void ShiftCpuKernel::InitKernel(const CNodePtr &kernel_node) { copy_src_begin_ = 0; copy_dst_begin_ = periods_; - copy_size_ = input_shape[axis] - periods_; + copy_size_ = SizeToLong(input_shape[axis]) - periods_; } else if (periods_ < 0) { - fill_begin_ = input_shape[axis] + periods_; + fill_begin_ = SizeToLong(input_shape[axis]) + periods_; fill_size_ = -periods_; copy_src_begin_ = -periods_; copy_dst_begin_ = 0; - copy_size_ = input_shape[axis] + periods_; + copy_size_ = SizeToLong(input_shape[axis]) + periods_; } } @@ -84,12 +84,12 @@ bool ShiftCpuKernel::Launch(const std::vector &inputs, const std: return true; } - const size_t outer_size = axisIterator_.OuterSize(); - const size_t axis_size = axisIterator_.AxisSize(); - const size_t inner_size = axisIterator_.InnerSize(); + const int64_t outer_size = SizeToLong(axisIterator_.OuterSize()); + const int64_t axis_size = SizeToLong(axisIterator_.AxisSize()); + const int64_t inner_size = SizeToLong(axisIterator_.InnerSize()); // periods is larger than size, all value of the tensor would be fill_value - if (std::abs(periods_) >= static_cast(axis_size)) { + if (std::abs(periods_) >= axis_size) { (void)std::fill_n(output, outer_size * axis_size * inner_size, fill_value); return true; } @@ -111,7 +111,7 @@ bool ShiftCpuKernel::Launch(const std::vector &inputs, const std: // normal procedure std::vector tasks; tasks.reserve(outer_size); - for (size_t i = 0; i < outer_size; ++i) { + for (int i = 0; i < outer_size; ++i) { (void)tasks.emplace_back([this, i, fill_value, axis_size, inner_size, input, output, outputs] { size_t offset = i * axis_size * inner_size; size_t input_offset = offset + copy_src_begin_ * inner_size; diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/shift_cpu_kernel.h b/mindspore/ccsrc/backend/kernel_compiler/cpu/shift_cpu_kernel.h index 5d11213ee17..aeec0c1715d 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/shift_cpu_kernel.h +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/shift_cpu_kernel.h @@ -42,12 +42,13 @@ class ShiftCpuKernel : public CPUKernel { // slice info AxisIterator axisIterator_{}; - size_t copy_src_begin_{0}; - size_t copy_dst_begin_{0}; - size_t copy_size_{0}; + // set type to int64_t to avoid computation signed and unsigned values + int64_t copy_src_begin_{0}; + int64_t copy_dst_begin_{0}; + int64_t copy_size_{0}; - size_t fill_begin_{0}; - size_t fill_size_{0}; + int64_t fill_begin_{0}; + int64_t fill_size_{0}; }; MS_REG_CPU_KERNEL_T(