From 015033fc73d0c68419b2b69987d629c133572486 Mon Sep 17 00:00:00 2001 From: shenpengcheng Date: Tue, 25 Jan 2022 21:03:38 +0800 Subject: [PATCH 1/2] flip --- test/test_network_ops/test_flip.py | 49 +++++++++++++++++++ torch_npu/csrc/aten/npu_native_functions.yaml | 1 + torch_npu/csrc/aten/ops/FlipKernelNpu.cpp | 33 +++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 test/test_network_ops/test_flip.py create mode 100644 torch_npu/csrc/aten/ops/FlipKernelNpu.cpp diff --git a/test/test_network_ops/test_flip.py b/test/test_network_ops/test_flip.py new file mode 100644 index 00000000000..c77f0ee1b63 --- /dev/null +++ b/test/test_network_ops/test_flip.py @@ -0,0 +1,49 @@ +# Copyright (c) 2020, Huawei Technologies.All rights reserved. +# +# Licensed under the BSD 3-Clause License (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://opensource.org/licenses/BSD-3-Clause +# +# 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 torch +import torch_npu +import numpy as np +from torch_npu.testing.common_utils import TestCase, run_tests +from torch_npu.testing.common_device_type import instantiate_device_type_tests +from torch_npu.testing.util_test import create_common_tensor + +class TestFlip(TestCase): + def cpu_op_exec(self, input1, dims): + output = torch.flip(input1, dims) + output = output.numpy() + return output + + def npu_op_exec(self, input1, dims): + output = torch.flip(input1, dims) + output = output.to("cpu") + output = output.numpy() + return output + + def test_flip_shape_format(self, device): + shape_format = [ + [[np.float32, 0, [2,2,2]], [0]], + [[np.float32, 0, [2,2,2,4]], [-2]], + [[np.int32, 0, [2,2,2]], [0,1]], + [[np.int32, 0, [2,2,2,4]], [-1,1]], + ] + for item in shape_format: + cpu_input1, npu_input1 = create_common_tensor(item[0], 1, 100) + cpu_output = self.cpu_op_exec(cpu_input1, item[1]) + npu_output = self.npu_op_exec(npu_input1, item[1]) + self.assertRtolEqual(cpu_output, npu_output) + +instantiate_device_type_tests(TestFlip, globals(), except_for="cpu") +if __name__ == "__main__": + run_tests() \ No newline at end of file diff --git a/torch_npu/csrc/aten/npu_native_functions.yaml b/torch_npu/csrc/aten/npu_native_functions.yaml index 27b8a88a836..47f43e5313f 100644 --- a/torch_npu/csrc/aten/npu_native_functions.yaml +++ b/torch_npu/csrc/aten/npu_native_functions.yaml @@ -241,6 +241,7 @@ supported: - addcdiv_ - addcdiv.out - slogdet + - flip custom: - func: npu_transpose_to_contiguous(Tensor self) -> Tensor diff --git a/torch_npu/csrc/aten/ops/FlipKernelNpu.cpp b/torch_npu/csrc/aten/ops/FlipKernelNpu.cpp new file mode 100644 index 00000000000..7cb87fd7eb8 --- /dev/null +++ b/torch_npu/csrc/aten/ops/FlipKernelNpu.cpp @@ -0,0 +1,33 @@ +// Copyright (c) 2020, Huawei Technologies.All rights reserved. +// +// Licensed under the BSD 3-Clause License (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://opensource.org/licenses/BSD-3-Clause +// +// 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. + +#include "torch_npu/csrc/framework/utils/OpAdapter.h" +#include "torch_npu/csrc/aten/NPUNativeFunctions.h" + +namespace at_npu { +namespace native { + +at::Tensor NPUNativeFunctions::flip(const at::Tensor& self, IntArrayRef dims){ + at::Tensor result = OpPreparation::ApplyTensor(self); + at::SmallVector dimVec = array_to_small_vector(dims); + OpCommand cmd; + cmd.Name("ReverseV2") + .Input(self) + .Input(dimVec, at::kLong) + .Output(result) + .Run(); + return result; +} +} // namespace native +} // namespace at_npu \ No newline at end of file -- Gitee From eed1c0e1362100ad5f9e69dca39fe3e6b310b0ab Mon Sep 17 00:00:00 2001 From: shenpengcheng Date: Tue, 25 Jan 2022 21:23:15 +0800 Subject: [PATCH 2/2] flip --- torch_npu/csrc/aten/ops/FlipKernelNpu.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torch_npu/csrc/aten/ops/FlipKernelNpu.cpp b/torch_npu/csrc/aten/ops/FlipKernelNpu.cpp index 7cb87fd7eb8..a5a2b7824e6 100644 --- a/torch_npu/csrc/aten/ops/FlipKernelNpu.cpp +++ b/torch_npu/csrc/aten/ops/FlipKernelNpu.cpp @@ -18,7 +18,7 @@ namespace at_npu { namespace native { -at::Tensor NPUNativeFunctions::flip(const at::Tensor& self, IntArrayRef dims){ +at::Tensor NPUNativeFunctions::flip(const at::Tensor& self, at::IntArrayRef dims){ at::Tensor result = OpPreparation::ApplyTensor(self); at::SmallVector dimVec = array_to_small_vector(dims); OpCommand cmd; -- Gitee