forked from huawei/mindspore2022
!31524 clean code
Merge pull request !31524 from wanyiming/clean_code_0318
This commit is contained in:
commit
b0c6fa9944
|
|
@ -177,31 +177,30 @@ void MirrorPadCpuKernelMod::LaunchKernel(const std::vector<AddressPtr> &inputs,
|
|||
int64_t matchval_y_index = padded_y;
|
||||
int64_t matchval_channel_index = padded_channel;
|
||||
int64_t matchval_batch_index = padded_batch;
|
||||
int64_t equiv_block_num = 0;
|
||||
|
||||
// update matching index in original tensor across all 4 dims
|
||||
if ((padded_x < ap1_x) || (padded_x > ap2_x)) {
|
||||
int64_t x_dist = (padded_x < ap1_x) ? (ap1_x - padded_x) : (padded_x - ap2_x);
|
||||
matchval_x_index = (padded_x < ap1_x) ? (ap1_x + x_dist - mode) : (ap2_x - x_dist + mode);
|
||||
matchval_x_index = (padded_x < ap1_x) ? ((ap1_x + x_dist) - mode) : ((ap2_x - x_dist) + mode);
|
||||
}
|
||||
if ((padded_y < ap1_y) || (padded_y > ap2_y)) {
|
||||
int64_t y_dist = (padded_y < ap1_y) ? (ap1_y - padded_y) : (padded_y - ap2_y);
|
||||
matchval_y_index = (padded_y < ap1_y) ? (ap1_y + y_dist - mode) : (ap2_y - y_dist + mode);
|
||||
matchval_y_index = (padded_y < ap1_y) ? ((ap1_y + y_dist) - mode) : ((ap2_y - y_dist) + mode);
|
||||
}
|
||||
if ((padded_channel < ap1_channel) || (padded_channel > ap2_channel)) {
|
||||
int64_t channel_dist =
|
||||
(padded_channel < ap1_channel) ? (ap1_channel - padded_channel) : (padded_channel - ap2_channel);
|
||||
matchval_channel_index =
|
||||
(padded_channel < ap1_channel) ? (ap1_channel + channel_dist - mode) : (ap2_channel - channel_dist + mode);
|
||||
(padded_channel < ap1_channel) ? ((ap1_channel + channel_dist) - mode) : ((ap2_channel - channel_dist) + mode);
|
||||
}
|
||||
if ((padded_batch < ap1_batch) || (padded_batch > ap2_batch)) {
|
||||
int64_t batch_dist = (padded_batch < ap1_batch) ? (ap1_batch - padded_batch) : (padded_batch - ap2_batch);
|
||||
matchval_batch_index =
|
||||
(padded_batch < ap1_batch) ? (ap1_batch + batch_dist - mode) : (ap2_batch - batch_dist + mode);
|
||||
(padded_batch < ap1_batch) ? ((ap1_batch + batch_dist) - mode) : ((ap2_batch - batch_dist) + mode);
|
||||
}
|
||||
|
||||
// calculate equivalent block in input
|
||||
equiv_block_num =
|
||||
int64_t equiv_block_num =
|
||||
((matchval_batch_index - paddings[BATCH]) * old_channel) + (matchval_channel_index - paddings[CHANNEL]);
|
||||
|
||||
// copy data from equiv block and adjusted x and y values in unpadded tensor
|
||||
|
|
|
|||
|
|
@ -95,7 +95,7 @@ void MirrorPadGradCpuKernelMod::InitKernel(const CNodePtr &kernel_node) {
|
|||
output_shape_.push_back(SizeToLong(x));
|
||||
}
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
for (size_t i = 0; i < 2; i++) {
|
||||
workspace_size_ *= output_shape[i];
|
||||
workspace_size_ *= input_shape[i + 2];
|
||||
}
|
||||
|
|
@ -195,10 +195,10 @@ void MirrorPadGradCpuKernelMod::MirrorPadGrad_Width_Height(const size_t size, co
|
|||
// copy position's own value into output
|
||||
dx[pos] = interim_dy[(dx_block_num * dy_height + grad_y) * dy_width + grad_x];
|
||||
|
||||
int64_t x_dist_1 = (ap1_x - grad_x - mode);
|
||||
int64_t y_dist_1 = (ap1_y - grad_y - mode);
|
||||
int64_t x_dist_2 = (ap2_x - grad_x + mode);
|
||||
int64_t y_dist_2 = (ap2_y - grad_y + mode);
|
||||
int64_t x_dist_1 = ((ap1_x - grad_x) - mode);
|
||||
int64_t y_dist_1 = ((ap1_y - grad_y) - mode);
|
||||
int64_t x_dist_2 = ((ap2_x - grad_x) + mode);
|
||||
int64_t y_dist_2 = ((ap2_y - grad_y) + mode);
|
||||
int64_t axis_dist[] = {x_dist_1, x_dist_2, y_dist_1, y_dist_2};
|
||||
int64_t anch_point[] = {ap1_x, ap2_x, ap1_y, ap2_y};
|
||||
bool x_axis_check[] = {true, true, false, false}; // true - update X , false - update Y
|
||||
|
|
@ -272,8 +272,7 @@ void MirrorPadGradCpuKernelMod::MirrorPadGradBatchChannel(const size_t size, T1
|
|||
const int64_t equiv_dy_batch = interim_batch + paddings[BATCH];
|
||||
int64_t target_batch = 0;
|
||||
int64_t target_channel = 0;
|
||||
int64_t equiv_block_num = 0;
|
||||
equiv_block_num = ((equiv_dy_batch * dy_channels) + equiv_dy_channel);
|
||||
int64_t equiv_block_num = ((equiv_dy_batch * dy_channels) + equiv_dy_channel);
|
||||
// generate values to sweep over all possible mirrored points
|
||||
int64_t batch_offsets[] = {2 * (ap1_batch - equiv_dy_batch) - mode, 0, 2 * (ap2_batch - equiv_dy_batch) + mode};
|
||||
int64_t channel_offsets[] = {2 * (ap1_channel - equiv_dy_channel) - mode, 0,
|
||||
|
|
|
|||
|
|
@ -28,8 +28,7 @@ constexpr size_t kPadElemSize = 2;
|
|||
void PadCpuKernelMod::InitKernel(const CNodePtr &kernel_node) {
|
||||
MS_EXCEPTION_IF_NULL(kernel_node);
|
||||
kernel_name_ = common::AnfAlgo::GetCNodeName(kernel_node);
|
||||
std::vector<std::vector<int64_t>> paddings_ =
|
||||
common::AnfAlgo::GetNodeAttr<std::vector<std::vector<int64_t>>>(kernel_node, "paddings");
|
||||
paddings_ = common::AnfAlgo::GetNodeAttr<std::vector<std::vector<int64_t>>>(kernel_node, "paddings");
|
||||
dtype_ = AnfAlgo::GetInputDeviceDataType(kernel_node, 0);
|
||||
input_shape_ = common::AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0);
|
||||
std::vector<size_t> output_shape = AnfAlgo::GetOutputDeviceShape(kernel_node, 0);
|
||||
|
|
@ -53,8 +52,8 @@ void PadCpuKernelMod::InitKernel(const CNodePtr &kernel_node) {
|
|||
|
||||
for (size_t i = 0; i < input_rank_; i++) {
|
||||
input_size_ *= input_shape_[i];
|
||||
output_size_ *=
|
||||
(input_shape_[i] + flattened_paddings_[kPadElemSize * i] + flattened_paddings_[(kPadElemSize * i) + 1]);
|
||||
output_size_ *= (input_shape_[i] + IntToSize(flattened_paddings_[kPadElemSize * i]) +
|
||||
IntToSize(flattened_paddings_[(kPadElemSize * i) + 1]));
|
||||
}
|
||||
|
||||
if (input_rank_ < 1) {
|
||||
|
|
@ -70,7 +69,8 @@ void PadCpuKernelMod::InitKernel(const CNodePtr &kernel_node) {
|
|||
strides_.resize(input_rank_);
|
||||
strides_[input_rank_ - 1] = 1;
|
||||
for (int32_t i = input_rank_ - 2; i >= 0; i--) {
|
||||
strides_[i] = output_shape[i + 1] * strides_[i + 1];
|
||||
size_t ind = IntToSize(i);
|
||||
strides_[ind] = output_shape[ind + 1] * strides_[ind + 1];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -109,7 +109,8 @@ bool PadCpuKernelMod::LaunchKernel(const std::vector<AddressPtr> &inputs, const
|
|||
for (size_t i = input_rank_; i >= 1; i--) {
|
||||
size_t unravel_dimension = input_shape_[i - 1];
|
||||
size_t unraveled_index = linear_index % unravel_dimension;
|
||||
padded_linear_index += ((unraveled_index + flattened_paddings_[kPadElemSize * (i - 1)]) * strides_[i - 1]);
|
||||
padded_linear_index +=
|
||||
((unraveled_index + IntToSize(flattened_paddings_[kPadElemSize * (i - 1)])) * strides_[i - 1]);
|
||||
linear_index -= unraveled_index;
|
||||
linear_index /= unravel_dimension;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,8 +44,8 @@ void ResizeBilinearCpuKernelMod::InitKernel(const CNodePtr &kernel_node) {
|
|||
}
|
||||
size_t in_height = shape_[2];
|
||||
size_t in_width = shape_[3];
|
||||
size_t out_height = size_[0];
|
||||
size_t out_width = size_[1];
|
||||
size_t out_height = LongToSize(size_[0]);
|
||||
size_t out_width = LongToSize(size_[1]);
|
||||
height_scale = Scaling(in_height, out_height, align_corners_);
|
||||
width_scale = Scaling(in_width, out_width, align_corners_);
|
||||
}
|
||||
|
|
@ -105,8 +105,8 @@ bool ResizeBilinearCpuKernelMod::LaunchKernel(const std::vector<AddressPtr> &inp
|
|||
size_t channel = shape_[1];
|
||||
size_t in_height = shape_[2];
|
||||
size_t in_width = shape_[3];
|
||||
size_t out_height = size_[0];
|
||||
size_t out_width = size_[1];
|
||||
size_t out_height = LongToSize(size_[0]);
|
||||
size_t out_width = LongToSize(size_[1]);
|
||||
size_t out_hw_size = out_height * out_width;
|
||||
size_t in_hw_size = in_height * in_width;
|
||||
size_t bhwc_size = in_hw_size * channel * batch_size;
|
||||
|
|
|
|||
|
|
@ -48,8 +48,8 @@ void ResizeNearestNeighborCpuKernelMod::InitKernel(const CNodePtr &kernel_node)
|
|||
channel_ = input_shape[1];
|
||||
in_height_ = input_shape[2];
|
||||
in_width_ = input_shape[3];
|
||||
out_height_ = output_size[0];
|
||||
out_width_ = output_size[1];
|
||||
out_height_ = LongToSize(output_size[0]);
|
||||
out_width_ = LongToSize(output_size[1]);
|
||||
height_scale_ = Scaling(in_height_, out_height_, align_corners_);
|
||||
width_scale_ = Scaling(in_width_, out_width_, align_corners_);
|
||||
output_size_ = batch_size_ * channel_ * out_height_ * out_width_;
|
||||
|
|
|
|||
Loading…
Reference in New Issue