!24064 solve Signed and Unsigned Integers issue

Merge pull request !24064 from zhujingxuan/master
This commit is contained in:
i-robot 2021-09-26 06:26:12 +00:00 committed by Gitee
commit 0acbd7ffa9
6 changed files with 37 additions and 35 deletions

View File

@ -81,17 +81,17 @@ template <typename T>
void RankCpuKernel<T>::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<T>::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;
}
};

View File

@ -65,7 +65,7 @@ class RankCpuKernel : public CPUKernel {
// parameters
size_t axis_{0};
rank::Method method_{rank::MethodNotDefined};
std::function<void(int, int, int, const AxisIterator &, const size_t *const, float *const)> func_;
std::function<void(size_t, int, int, const AxisIterator &, const size_t *const, float *const)> func_;
rank::NaOption option_{rank::OptionNotDefined};
bool ascending_{true};
bool pct_{false};

View File

@ -38,10 +38,11 @@ void RollingCpuKernel<T, S>::InitKernel(const CNodePtr &kernel_node) {
MS_LOG(EXCEPTION) << "[" << method << "] not supported";
}
method_ = kValidMethods.at(method);
window_ = AnfAlgo::GetNodeAttr<int64_t>(kernel_node, WINDOW);
if (window_ <= 0) {
MS_LOG(EXCEPTION) << "window size should not less than 0, but got " << window_;
auto window = AnfAlgo::GetNodeAttr<int64_t>(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<int64_t>(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<T, S>::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<T, S>::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<T, S>::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]];

View File

@ -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_{};

View File

@ -51,14 +51,14 @@ void ShiftCpuKernel<T>::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<T>::Launch(const std::vector<AddressPtr> &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<int>(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<T>::Launch(const std::vector<AddressPtr> &inputs, const std:
// normal procedure
std::vector<common::Task> 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;

View File

@ -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(