From ba99cb7bcb4114bd4231df1fbcaf181aa6d6d629 Mon Sep 17 00:00:00 2001 From: jiangjinsheng Date: Wed, 13 May 2020 15:43:05 +0800 Subject: [PATCH] support vm for space_to_depth --- mindspore/ops/_op_impl/tbe/__init__.py | 2 + mindspore/ops/_op_impl/tbe/depth_to_space.py | 46 ++++++++++++++++++++ mindspore/ops/_op_impl/tbe/space_to_depth.py | 46 ++++++++++++++++++++ mindspore/ops/operations/array_ops.py | 2 - tests/ut/python/ops/test_array_ops.py | 25 +++++++++++ 5 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 mindspore/ops/_op_impl/tbe/depth_to_space.py create mode 100644 mindspore/ops/_op_impl/tbe/space_to_depth.py diff --git a/mindspore/ops/_op_impl/tbe/__init__.py b/mindspore/ops/_op_impl/tbe/__init__.py index 3e9229c6aa8..eeeefdc784f 100644 --- a/mindspore/ops/_op_impl/tbe/__init__.py +++ b/mindspore/ops/_op_impl/tbe/__init__.py @@ -164,6 +164,8 @@ from .avg_pool_grad import _avg_pool_grad_tbe from .ones_like import _ones_like_tbe from .batch_to_space import _batch_to_space_tbe from .space_to_batch import _space_to_batch_tbe +from .depth_to_space import _depth_to_space_tbe +from .space_to_depth import _space_to_depth_tbe from .floor import _floor_tbe from .log1p import _log1p_tbe from .resize_bilinear import _resize_bilinear_tbe diff --git a/mindspore/ops/_op_impl/tbe/depth_to_space.py b/mindspore/ops/_op_impl/tbe/depth_to_space.py new file mode 100644 index 00000000000..b326fb00902 --- /dev/null +++ b/mindspore/ops/_op_impl/tbe/depth_to_space.py @@ -0,0 +1,46 @@ +# 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. +# ============================================================================ + +"""DepthToSpace op""" +from mindspore.ops.op_info_register import op_info_register, TBERegOp, DataType + +depth_to_space_op_info = TBERegOp("DepthToSpace") \ + .fusion_type("OPAQUE") \ + .async_flag(False) \ + .binfile_name("depth_to_space.so") \ + .compute_cost(10) \ + .kernel_name("depth_to_space") \ + .partial_flag(True) \ + .attr("block_size", "required", "int", "all") \ + .attr("data_format", "optional", "str", "all") \ + .input(0, "x", False, "required", "all") \ + .output(0, "y", False, "required", "all") \ + .dtype_format(DataType.F16_NHWC, DataType.F16_NHWC) \ + .dtype_format(DataType.F32_NHWC, DataType.F32_NHWC) \ + .dtype_format(DataType.I8_NHWC, DataType.I8_NHWC) \ + .dtype_format(DataType.I16_NHWC, DataType.I16_NHWC) \ + .dtype_format(DataType.I32_NHWC, DataType.I32_NHWC) \ + .dtype_format(DataType.I64_NHWC, DataType.I64_NHWC) \ + .dtype_format(DataType.U8_NHWC, DataType.U8_NHWC) \ + .dtype_format(DataType.U16_NHWC, DataType.U16_NHWC) \ + .dtype_format(DataType.U32_NHWC, DataType.U32_NHWC) \ + .dtype_format(DataType.U64_NHWC, DataType.U64_NHWC) \ + .get_op_info() + + +@op_info_register(depth_to_space_op_info) +def _depth_to_space_tbe(): + """DepthToSpace TBE register""" + return diff --git a/mindspore/ops/_op_impl/tbe/space_to_depth.py b/mindspore/ops/_op_impl/tbe/space_to_depth.py new file mode 100644 index 00000000000..5cf6aa2b38c --- /dev/null +++ b/mindspore/ops/_op_impl/tbe/space_to_depth.py @@ -0,0 +1,46 @@ +# 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. +# ============================================================================ + +"""SpaceToDepth op""" +from mindspore.ops.op_info_register import op_info_register, TBERegOp, DataType + +space_to_depth_op_info = TBERegOp("SpaceToDepth") \ + .fusion_type("OPAQUE") \ + .async_flag(False) \ + .binfile_name("space_to_depth.so") \ + .compute_cost(10) \ + .kernel_name("space_to_depth") \ + .partial_flag(True) \ + .attr("block_size", "required", "int", "all") \ + .attr("data_format", "optional", "str", "all") \ + .input(0, "x", False, "required", "all") \ + .output(0, "y", False, "required", "all") \ + .dtype_format(DataType.F16_NHWC, DataType.F16_NHWC) \ + .dtype_format(DataType.F32_NHWC, DataType.F32_NHWC) \ + .dtype_format(DataType.I8_NHWC, DataType.I8_NHWC) \ + .dtype_format(DataType.I16_NHWC, DataType.I16_NHWC) \ + .dtype_format(DataType.I32_NHWC, DataType.I32_NHWC) \ + .dtype_format(DataType.I64_NHWC, DataType.I64_NHWC) \ + .dtype_format(DataType.U8_NHWC, DataType.U8_NHWC) \ + .dtype_format(DataType.U16_NHWC, DataType.U16_NHWC) \ + .dtype_format(DataType.U32_NHWC, DataType.U32_NHWC) \ + .dtype_format(DataType.U64_NHWC, DataType.U64_NHWC) \ + .get_op_info() + + +@op_info_register(space_to_depth_op_info) +def _space_to_depth_tbe(): + """SpaceToDepth TBE register""" + return diff --git a/mindspore/ops/operations/array_ops.py b/mindspore/ops/operations/array_ops.py index c88735aa233..46f5e8bc73e 100644 --- a/mindspore/ops/operations/array_ops.py +++ b/mindspore/ops/operations/array_ops.py @@ -2127,7 +2127,6 @@ class SpaceToDepth(PrimitiveWithInfer): validator.check_value_type('block_size', block_size, [int], self.name) validator.check('block_size', block_size, '', 2, Rel.GE) self.block_size = block_size - self.add_prim_attr("data_format", "NCHW") def infer_shape(self, x_shape): validator.check('x dimension', len(x_shape), '', 4, Rel.EQ) @@ -2185,7 +2184,6 @@ class DepthToSpace(PrimitiveWithInfer): validator.check_value_type('block_size', block_size, [int], self.name) validator.check('block_size', block_size, '', 2, Rel.GE, self.name) self.block_size = block_size - self.add_prim_attr("data_format", "NCHW") def infer_shape(self, x_shape): validator.check('x dimension', len(x_shape), '', 4, Rel.EQ) diff --git a/tests/ut/python/ops/test_array_ops.py b/tests/ut/python/ops/test_array_ops.py index ebb3db3b269..2328bdfc834 100644 --- a/tests/ut/python/ops/test_array_ops.py +++ b/tests/ut/python/ops/test_array_ops.py @@ -243,6 +243,25 @@ class UnpackNet(Cell): def construct(self, x): return self.unpack(x) +class SpaceToDepthNet(Cell): + def __init__(self): + super(SpaceToDepthNet, self).__init__() + block_size = 2 + self.space_to_depth = P.SpaceToDepth(block_size) + + def construct(self, x): + return self.space_to_depth(x) + + +class DepthToSpaceNet(Cell): + def __init__(self): + super(DepthToSpaceNet, self).__init__() + block_size = 2 + self.depth_to_space = P.DepthToSpace(block_size) + + def construct(self, x): + return self.depth_to_space(x) + test_case_array_ops = [ ('CustNet1', { @@ -272,6 +291,12 @@ test_case_array_ops = [ ('UnpackNet', { 'block': UnpackNet(), 'desc_inputs': [Tensor(np.array([[1, 2], [3, 4]]).astype(np.float16))]}), + ('SpaceToDepthNet', { + 'block': SpaceToDepthNet(), + 'desc_inputs': [Tensor(np.random.rand(1,3,2,2).astype(np.float16))]}), + ('DepthToSpaceNet', { + 'block': DepthToSpaceNet(), + 'desc_inputs': [Tensor(np.random.rand(1,12,1,1).astype(np.float16))]}), ] test_case_lists = [test_case_array_ops]