diff --git a/mindspore/ccsrc/kernel/cpu/cpu_kernel.h b/mindspore/ccsrc/kernel/cpu/cpu_kernel.h index edd133bf13..2538459336 100644 --- a/mindspore/ccsrc/kernel/cpu/cpu_kernel.h +++ b/mindspore/ccsrc/kernel/cpu/cpu_kernel.h @@ -47,6 +47,7 @@ const char TRANSPOSE_NO = 'N'; const char TRANSPOSE_YES = 'T'; const char AXIS[] = "axis"; const char BEGIN[] = "begin"; +const char END[] = "end"; const char SIZE[] = "size"; class CPUKernel : public kernel::KernelMod { diff --git a/mindspore/ccsrc/kernel/cpu/slice_cpu_kernel.cc b/mindspore/ccsrc/kernel/cpu/slice_cpu_kernel.cc index 9e27ddf3b1..b1565425e0 100644 --- a/mindspore/ccsrc/kernel/cpu/slice_cpu_kernel.cc +++ b/mindspore/ccsrc/kernel/cpu/slice_cpu_kernel.cc @@ -21,31 +21,53 @@ namespace mindspore { namespace kernel { void SliceCPUKernel::InitKernel(const CNodePtr &kernel_node) { CheckParam(kernel_node); - - begin_ = AnfAlgo::GetNodeAttr>(kernel_node, BEGIN); - size_ = AnfAlgo::GetNodeAttr>(kernel_node, SIZE); - input_shape_ = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0); - if (input_shape_.size() < 4) { - for (size_t i = 0; i < 4 - input_shape_.size(); ++i) { - input_shape_.insert(input_shape_.begin(), 1); - begin_.insert(begin_.begin(), 0); - size_.insert(size_.begin(), 1); - } - } - output_shape_ = AnfAlgo::GetOutputInferShape(kernel_node, 0); CPUKernelUtils::ExpandDimsTo4(&output_shape_); + begin_ = AnfAlgo::GetNodeAttr>(kernel_node, BEGIN); for (size_t i = 0; i < begin_.size(); i++) { if (begin_[i] < 0) { begin_[i] = begin_[i] + input_shape_[i]; } } - - for (size_t i = 0; i < size_.size(); i++) { - if (size_[i] < 0) { - size_[i] = (size_[i] + input_shape_[i]) > 0 ? (size_[i] + input_shape_[i]) : 0; + auto prim = AnfAlgo::GetCNodePrimitive(kernel_node); + MS_EXCEPTION_IF_NULL(prim); + auto strides = prim->GetAttr(STRIDES); + if (strides != nullptr) { + strides_ = AnfAlgo::GetNodeAttr>(kernel_node, STRIDES); + end_ = AnfAlgo::GetNodeAttr>(kernel_node, END); + if (strides_.size() != end_.size() || strides_.size() != input_shape_.size()) { + MS_LOG(EXCEPTION) << "stride|end|input size must be equal"; + } + for (size_t i = 0; i < strides_.size(); ++i) { + if (strides_[i] < 0) { + strides_[i] = (strides_[i] + input_shape_[i]) > 0 ? (strides_[i] + input_shape_[i]) : 0; + } + if (end_[i] < 0) { + end_[i] = (end_[i] + input_shape_[i]) > 0 ? (end_[i] + input_shape_[i]) : 0; + } + } + } else { + auto sizes = AnfAlgo::GetNodeAttr>(kernel_node, SIZE); + if (sizes.size() != input_shape_.size() || begin_.size() != input_shape_.size()) { + MS_LOG(EXCEPTION) << "begin|size|input size must be equal"; + } + for (size_t i = 0; i < sizes.size(); ++i) { + if (sizes[i] < 0) { + sizes[i] = (sizes[i] + input_shape_[i]) > 0 ? (sizes[i] + input_shape_[i]) : 0; + } + strides_.emplace_back(1); + end_.emplace_back(begin_[i] + sizes[i]); + } + } + auto input_len = input_shape_.size(); + if (input_len < 4) { + for (size_t i = 0; i < 4 - input_len; ++i) { + input_shape_.insert(input_shape_.begin(), 1); + begin_.insert(begin_.begin(), 0); + strides_.insert(strides_.begin(), 1); + end_.insert(end_.begin(), 1); } } } @@ -56,10 +78,10 @@ bool SliceCPUKernel::Launch(const std::vector &inputs, auto input_addr = reinterpret_cast(inputs[0]->addr); auto output_addr = reinterpret_cast(outputs[0]->addr); - for (int i = begin_[0]; i < begin_[0] + size_[0]; ++i) { - for (int j = begin_[1]; j < begin_[1] + size_[1]; ++j) { - for (int k = begin_[2]; k < begin_[2] + size_[2]; ++k) { - for (int m = begin_[3]; m < begin_[3] + size_[3]; ++m) { + for (int i = begin_[0]; i < end_[0]; i += strides_[0]) { + for (int j = begin_[1]; j < end_[1]; j += strides_[1]) { + for (int k = begin_[2]; k < end_[2]; k += strides_[2]) { + for (int m = begin_[3]; m < end_[3]; m += strides_[3]) { auto offset = CPUKernelUtils::CalcOffset(input_shape_, i, j, k, m); *output_addr++ = input_addr[offset]; } diff --git a/mindspore/ccsrc/kernel/cpu/slice_cpu_kernel.h b/mindspore/ccsrc/kernel/cpu/slice_cpu_kernel.h index d8a71a5335..788c4f39ad 100644 --- a/mindspore/ccsrc/kernel/cpu/slice_cpu_kernel.h +++ b/mindspore/ccsrc/kernel/cpu/slice_cpu_kernel.h @@ -35,13 +35,16 @@ class SliceCPUKernel : public CPUKernel { private: void CheckParam(const CNodePtr &kernel_node); std::vector begin_; - std::vector size_; + std::vector end_; + std::vector strides_; std::vector input_shape_; std::vector output_shape_; }; MS_REG_CPU_KERNEL(Slice, KernelAttr().AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32), SliceCPUKernel); +MS_REG_CPU_KERNEL(StridedSlice, KernelAttr().AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32), + SliceCPUKernel); } // namespace kernel } // namespace mindspore diff --git a/mindspore/ccsrc/kernel/cpu/slice_grad_cpu_kernel.cc b/mindspore/ccsrc/kernel/cpu/slice_grad_cpu_kernel.cc index 1176bef12c..2a61a0259a 100644 --- a/mindspore/ccsrc/kernel/cpu/slice_grad_cpu_kernel.cc +++ b/mindspore/ccsrc/kernel/cpu/slice_grad_cpu_kernel.cc @@ -21,33 +21,54 @@ namespace mindspore { namespace kernel { void SliceGradCPUKernel::InitKernel(const CNodePtr &kernel_node) { CheckParam(kernel_node); - - begin_ = AnfAlgo::GetNodeAttr>(kernel_node, BEGIN); - size_ = AnfAlgo::GetNodeAttr>(kernel_node, SIZE); - - input_dy_shape_ = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0); - if (input_dy_shape_.size() < 4) { - for (size_t i = 0; i < 4 - input_dy_shape_.size(); ++i) { - input_dy_shape_.insert(input_dy_shape_.begin(), 1); - begin_.insert(begin_.begin(), 0); - size_.insert(size_.begin(), 1); - } - } - - input_x_shape_ = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 1); output_dx_shape_ = AnfAlgo::GetOutputInferShape(kernel_node, 0); - CPUKernelUtils::ExpandDimsTo4(&input_x_shape_); - CPUKernelUtils::ExpandDimsTo4(&output_dx_shape_); + input_dy_shape_ = AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0); + begin_ = AnfAlgo::GetNodeAttr>(kernel_node, BEGIN); for (size_t i = 0; i < begin_.size(); i++) { if (begin_[i] < 0) { - begin_[i] = begin_[i] + input_x_shape_[i]; + begin_[i] = begin_[i] + output_dx_shape_[i]; } } - for (size_t i = 0; i < size_.size(); i++) { - if (size_[i] < 0) { - size_[i] = (size_[i] + input_x_shape_[i]) > 0 ? (size_[i] + input_x_shape_[i]) : 0; + auto prim = AnfAlgo::GetCNodePrimitive(kernel_node); + MS_EXCEPTION_IF_NULL(prim); + auto strides = prim->GetAttr(STRIDES); + if (strides != nullptr) { + strides_ = AnfAlgo::GetNodeAttr>(kernel_node, STRIDES); + end_ = AnfAlgo::GetNodeAttr>(kernel_node, END); + if (strides_.size() != end_.size() || strides_.size() != output_dx_shape_.size()) { + MS_LOG(EXCEPTION) << "stride|end|input size must be equal"; + } + for (size_t i = 0; i < strides_.size(); ++i) { + if (strides_[i] < 0) { + strides_[i] = (strides_[i] + output_dx_shape_[i]) > 0 ? (strides_[i] + output_dx_shape_[i]) : 0; + } + if (end_[i] < 0) { + end_[i] = (end_[i] + output_dx_shape_[i]) > 0 ? (end_[i] + output_dx_shape_[i]) : 0; + } + } + } else { + auto sizes = AnfAlgo::GetNodeAttr>(kernel_node, SIZE); + if (sizes.size() != output_dx_shape_.size() || begin_.size() != output_dx_shape_.size()) { + MS_LOG(EXCEPTION) << "begin|size|input size must be equal"; + } + for (size_t i = 0; i < sizes.size(); ++i) { + if (sizes[i] < 0) { + sizes[i] = (sizes[i] + output_dx_shape_[i]) > 0 ? (sizes[i] + output_dx_shape_[i]) : 0; + } + strides_.emplace_back(1); + end_.emplace_back(begin_[i] + sizes[i]); + } + } + CPUKernelUtils::ExpandDimsTo4(&output_dx_shape_); + auto input_len = input_dy_shape_.size(); + if (input_len < 4) { + for (size_t i = 0; i < 4 - input_len; ++i) { + input_dy_shape_.insert(input_dy_shape_.begin(), 1); + begin_.insert(begin_.begin(), 0); + strides_.insert(strides_.begin(), 1); + end_.insert(end_.begin(), 1); } } } @@ -65,10 +86,10 @@ bool SliceGradCPUKernel::Launch(const std::vector &inputs, return false; } - for (int i = begin_[0]; i < begin_[0] + size_[0]; ++i) { - for (int j = begin_[1]; j < begin_[1] + size_[1]; ++j) { - for (int k = begin_[2]; k < begin_[2] + size_[2]; ++k) { - for (int m = begin_[3]; m < begin_[3] + size_[3]; ++m) { + for (int i = begin_[0]; i < end_[0]; i += strides_[0]) { + for (int j = begin_[1]; j < end_[1]; j += strides_[1]) { + for (int k = begin_[2]; k < end_[2]; k += strides_[2]) { + for (int m = begin_[3]; m < end_[3]; m += strides_[3]) { auto offset = CPUKernelUtils::CalcOffset(output_dx_shape_, i, j, k, m); output_dx_addr[offset] = *input_dy_addr++; } diff --git a/mindspore/ccsrc/kernel/cpu/slice_grad_cpu_kernel.h b/mindspore/ccsrc/kernel/cpu/slice_grad_cpu_kernel.h index 5508b1b0ba..847208e4bb 100644 --- a/mindspore/ccsrc/kernel/cpu/slice_grad_cpu_kernel.h +++ b/mindspore/ccsrc/kernel/cpu/slice_grad_cpu_kernel.h @@ -35,9 +35,9 @@ class SliceGradCPUKernel : public CPUKernel { private: void CheckParam(const CNodePtr &kernel_node); std::vector begin_; - std::vector size_; + std::vector end_; + std::vector strides_; std::vector input_dy_shape_; - std::vector input_x_shape_; std::vector output_dx_shape_; }; @@ -45,6 +45,8 @@ MS_REG_CPU_KERNEL( SliceGrad, KernelAttr().AddInputAttr(kNumberTypeFloat32).AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32), SliceGradCPUKernel); +MS_REG_CPU_KERNEL(StridedSliceGrad, KernelAttr().AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32), + SliceGradCPUKernel); } // namespace kernel } // namespace mindspore diff --git a/tests/st/ops/cpu/test_stridedslice_grad_op.py b/tests/st/ops/cpu/test_stridedslice_grad_op.py new file mode 100644 index 0000000000..d255694691 --- /dev/null +++ b/tests/st/ops/cpu/test_stridedslice_grad_op.py @@ -0,0 +1,49 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +import numpy as np +import pytest + +import mindspore.context as context +import mindspore.nn as nn +from mindspore import Tensor +from mindspore.common.api import ms_function +from mindspore.ops import operations as P +from mindspore.ops.operations import _grad_ops as G + +context.set_context(mode=context.GRAPH_MODE, device_target='CPU') + + +class StridedSliceGrad(nn.Cell): + def __init__(self): + super(StridedSliceGrad, self).__init__() + self.ssg = G.StridedSliceGrad() + self.shape = P.Shape() + + @ms_function + def construct(self, dy, x): + return self.ssg(dy, self.shape(x), (2, 0, 0), (3, 2, 3), (1, 1, 1)) + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu_training +@pytest.mark.env_onecard +def test_slice(): + x = Tensor(np.array([[[1., 1., 1.], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 7, 8]]]).astype(np.float32)) + dy = Tensor(np.array([[[5., 1., 5.], [6., 1., 8.]]]).astype(np.float32)) + ssg = StridedSliceGrad() + output = ssg(dy, x) + expect = [[[0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]], [[5, 1, 5], [6, 1, 8]]] + assert (output.asnumpy() == expect).all() diff --git a/tests/st/ops/cpu/test_stridedslice_op.py b/tests/st/ops/cpu/test_stridedslice_op.py new file mode 100644 index 0000000000..bb404c5266 --- /dev/null +++ b/tests/st/ops/cpu/test_stridedslice_op.py @@ -0,0 +1,45 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +import numpy as np +import pytest + +import mindspore.context as context +import mindspore.nn as nn +from mindspore import Tensor +from mindspore.ops import operations as P + +context.set_context(mode=context.GRAPH_MODE, device_target='CPU') + + +class StridedSlice(nn.Cell): + def __init__(self): + super(StridedSlice, self).__init__() + self.stridedslice = P.StridedSlice() + + def construct(self, x): + return self.stridedslice(x, (2, 0, 0), (3, 2, 3), (1, 1, 1)) + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu_training +@pytest.mark.env_onecard +def test_slice(): + x = Tensor(np.array([[[1., 1., 1.], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 7, 8]]]).astype(np.float32)) + stridedslice = StridedSlice() + output = stridedslice(x) + expect = [[[5., 5., 5.], + [6., 7., 8.]]] + assert (output.asnumpy() == expect).all()