From dc7274531b647ad80e808879c59a632bd49287b6 Mon Sep 17 00:00:00 2001 From: hxf12345677 Date: Sun, 24 Apr 2022 16:14:44 +0800 Subject: [PATCH] =?UTF-8?q?1.8.1=E6=96=B0=E5=A2=9E=E7=AE=97=E5=AD=90?= =?UTF-8?q?=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_network_ops/test_absolute.py | 62 +++++++ test/test_network_ops/test_acosh.py | 89 +++++++++ test/test_network_ops/test_add_relu.py | 181 +++++++++++++++++++ test/test_network_ops/test_arccosh.py | 89 +++++++++ test/test_network_ops/test_arcsin.py | 83 +++++++++ test/test_network_ops/test_arcsinh.py | 83 +++++++++ test/test_network_ops/test_arctan.py | 98 ++++++++++ test/test_network_ops/test_arctanh.py | 98 ++++++++++ test/test_network_ops/test_asinh.py | 83 +++++++++ test/test_network_ops/test_atanh.py | 98 ++++++++++ test/test_network_ops/test_clip.py | 156 ++++++++++++++++ test/test_network_ops/test_divide.py | 126 +++++++++++++ torch_npu/csrc/aten/ops/AcoshKernelNpu.cpp | 59 ++++++ torch_npu/csrc/aten/ops/AddReluKernelNpu.cpp | 64 +++++++ torch_npu/csrc/aten/ops/AsinhKernelNpu.cpp | 59 ++++++ torch_npu/csrc/aten/ops/AtanhKernelNpu.cpp | 56 ++++++ 16 files changed, 1484 insertions(+) create mode 100644 test/test_network_ops/test_absolute.py create mode 100644 test/test_network_ops/test_acosh.py create mode 100644 test/test_network_ops/test_add_relu.py create mode 100644 test/test_network_ops/test_arccosh.py create mode 100644 test/test_network_ops/test_arcsin.py create mode 100644 test/test_network_ops/test_arcsinh.py create mode 100644 test/test_network_ops/test_arctan.py create mode 100644 test/test_network_ops/test_arctanh.py create mode 100644 test/test_network_ops/test_asinh.py create mode 100644 test/test_network_ops/test_atanh.py create mode 100644 test/test_network_ops/test_clip.py create mode 100644 test/test_network_ops/test_divide.py create mode 100644 torch_npu/csrc/aten/ops/AcoshKernelNpu.cpp create mode 100644 torch_npu/csrc/aten/ops/AddReluKernelNpu.cpp create mode 100644 torch_npu/csrc/aten/ops/AsinhKernelNpu.cpp create mode 100644 torch_npu/csrc/aten/ops/AtanhKernelNpu.cpp diff --git a/test/test_network_ops/test_absolute.py b/test/test_network_ops/test_absolute.py new file mode 100644 index 00000000000..0d80e0c188e --- /dev/null +++ b/test/test_network_ops/test_absolute.py @@ -0,0 +1,62 @@ +# 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 numpy as np +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests +from torch_npu.testing.common_utils import create_common_tensor + + +class TestAbs(TestCase): + def cpu_op_exec(self, input1): + output = torch.absolute(input1) + output = output.numpy() + return output + + def npu_op_exec(self, input1): + output = torch.absolute(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def test_abs_shape_format_fp16(self): + format_list = [0, 3] + shape_list = [[5], [5, 10], [1, 3, 2], [52, 15, 15, 20]] + shape_format = [ + [np.float16, i, j] for i in format_list for j in shape_list + ] + for item in shape_format: + cpu_input, npu_input = create_common_tensor(item, -10, 10) + cpu_input = cpu_input.to(torch.float32) + cpu_output = self.cpu_op_exec(cpu_input) + npu_output = self.npu_op_exec(npu_input) + cpu_output = cpu_output.astype(np.float16) + self.assertRtolEqual(cpu_output, npu_output) + + def test_abs_shape_format_fp32(self): + format_list = [0, 3] + shape_list = [[5], [5, 10], [1, 3, 2], [52, 15, 15, 20]] + shape_format = [ + [np.float32, i, j] for i in format_list for j in shape_list + ] + for item in shape_format: + cpu_input, npu_input = create_common_tensor(item, -10, 10) + cpu_output = self.cpu_op_exec(cpu_input) + npu_output = self.npu_op_exec(npu_input) + self.assertRtolEqual(cpu_output, npu_output) + + +if __name__ == "__main__": + run_tests() diff --git a/test/test_network_ops/test_acosh.py b/test/test_network_ops/test_acosh.py new file mode 100644 index 00000000000..19deb52f40b --- /dev/null +++ b/test/test_network_ops/test_acosh.py @@ -0,0 +1,89 @@ +# 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 numpy as np +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests +from torch_npu.testing.common_utils import create_common_tensor + + +class TestAcosh(TestCase): + + def cpu_op_exec(self, input1): + output = torch.acosh(input1) + output = output.numpy() + return output + + def npu_op_exec(self, input1): + output = torch.acosh(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def npu_op_exec_out(self, input1, input2): + torch.acosh(input1, out=input2) + output = input2.to("cpu") + output = output.numpy() + return output + + def cpu_inp_op_exec(self, input1): + output = torch.acosh_(input1) + output = output.numpy() + return output + + def npu_inp_op_exec(self, input1): + output = torch.acosh_(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def test_acosh_common_shape_format(self): + shape_format1 = [ + [[np.float32, 0, (5, 3)]], + ] + for item in shape_format1: + cpu_input1, npu_input1 = create_common_tensor(item[0], -10, 10) + cpu_output = self.cpu_op_exec(cpu_input1) + npu_output = self.npu_op_exec(npu_input1) + mask = ~(np.isnan(cpu_output) | np.isinf(cpu_output)) + self.assertRtolEqual(cpu_output[mask], npu_output[mask], 0.001) + + def test_acosh_out_common_shape_format(self): + shape_format1 = [ + [[np.float32, 0, (4, 3)], [np.float32, 0, (4, 3)]], + ] + for item in shape_format1: + cpu_input1, npu_input1 = create_common_tensor(item[0], -10, 10) + cpu_input2, npu_input2 = create_common_tensor(item[1], -10, 10) + cpu_output = self.cpu_op_exec(cpu_input1) + npu_output = self.npu_op_exec_out(npu_input1, npu_input2) + mask = ~(np.isnan(cpu_output) | np.isinf(cpu_output)) + self.assertRtolEqual(cpu_output[mask], npu_output[mask], 0.001) + + def test_acosh_inp_common_shape_format(self): + shape_format1 = [ + [[np.float32, 0, (5, 3)]], + ] + for item in shape_format1: + cpu_input1, npu_input1 = create_common_tensor(item[0], -10, 10) + cpu_output = self.cpu_inp_op_exec(cpu_input1) + npu_output = self.npu_inp_op_exec(npu_input1) + mask = ~(np.isnan(cpu_output) | np.isinf(cpu_output)) + self.assertRtolEqual(cpu_output[mask], npu_output[mask], 0.001) + + +if __name__ == "__main__": + run_tests() \ No newline at end of file diff --git a/test/test_network_ops/test_add_relu.py b/test/test_network_ops/test_add_relu.py new file mode 100644 index 00000000000..1dfba2f2375 --- /dev/null +++ b/test/test_network_ops/test_add_relu.py @@ -0,0 +1,181 @@ +# 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 numpy as np +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests +from torch_npu.testing.common_utils import create_common_tensor + + +class TestAddRelu(TestCase): + def cpu_op_exec(self, input1, input2): + output = torch._VF._add_relu(input1, input2, alpha=1) + output = output.numpy() + return output + + def npu_op_exec_new(self, input1, input2): + output = torch._VF._add_relu(input1, input2, alpha=1) + output = output.to("cpu") + output = output.numpy() + return output + + def cpu_op_exec_alpha(self, input1, input2): + output = torch._VF._add_relu(input1, input2, alpha=3) + output = output.numpy() + return output + + def npu_op_exec_new_alpha(self, input1, input2): + output = torch._VF._add_relu(input1, input2, alpha=3) + output = output.to("cpu") + output = output.numpy() + return output + + def add_relu_result(self, shape_format): + for item in shape_format: + cpu_input1, npu_input1 = create_common_tensor(item, 0, 100) + cpu_input2, npu_input2 = create_common_tensor(item, 0, 100) + if cpu_input1.dtype == torch.float16: + cpu_input1 = cpu_input1.to(torch.float32) + cpu_input2 = cpu_input2.to(torch.float32) + + cpu_output = self.cpu_op_exec(cpu_input1, cpu_input2) + npu_output = self.npu_op_exec_new(npu_input1, npu_input2) + cpu_output = cpu_output.astype(npu_output.dtype) + + self.assertRtolEqual(cpu_output, npu_output) + + def add_relu_alpha_result(self, shape_format): + for item in shape_format: + cpu_input1, npu_input1 = create_common_tensor(item, 0, 100) + cpu_input2, npu_input2 = create_common_tensor(item, 0, 100) + if cpu_input1.dtype == torch.float16: + cpu_input1 = cpu_input1.to(torch.float32) + cpu_input2 = cpu_input2.to(torch.float32) + + cpu_output = self.cpu_op_exec_alpha(cpu_input1, cpu_input2) + npu_output = self.npu_op_exec_new_alpha(npu_input1, npu_input2) + cpu_output = cpu_output.astype(npu_output.dtype) + + self.assertRtolEqual(cpu_output, npu_output) + + def test_add_relu_shape_format_fp32_1d(self): + format_list = [0, 3] + shape_format = [ + [np.float32, i, [64]] for i in format_list + ] + self.add_relu_result(shape_format) + + def test_add_relu_shape_format_fp16_2d(self): + format_list = [0, 3, 29] + shape_format = [ + [np.float16, i, [5, 256]] for i in format_list + ] + self.add_relu_result(shape_format) + + def test_add_relu_shape_format_fp32_2d(self): + format_list = [0, 3, 29] + shape_format = [ + [np.float32, i, [5, 256]] for i in format_list + ] + self.add_relu_result(shape_format) + + def test_add_relu_shape_format_fp16_3d(self): + format_list = [0, 3, 29] + shape_format = [ + [np.float16, i, [32, 3, 3]] for i in format_list + ] + self.add_relu_result(shape_format) + + def test_add_relu_shape_format_fp32_3d(self): + format_list = [0, 3, 29] + shape_format = [ + [np.float32, i, [32, 3, 3]] for i in format_list + ] + self.add_relu_result(shape_format) + + def test_add_relu_shape_format_fp16_4d(self): + format_list = [0, 3, 29] + shape_format = [ + [np.float16, i, [64, 112, 7, 7]] for i in format_list + ] + self.add_relu_result(shape_format) + + def test_add_relu_shape_format_fp32_4d(self): + format_list = [0, 3, 29] + shape_format = [ + [np.float32, i, [64, 112, 7, 7]] for i in format_list + ] + self.add_relu_result(shape_format) + + def test_add_relu_shape_format_fp16_1d(self): + format_list = [0, 3] + shape_format = [ + [np.float16, i, [64]] for i in format_list + ] + self.add_relu_alpha_result(shape_format) + + def test_add_relu_alpha_shape_format_fp32_1d(self): + format_list = [0, 3] + shape_format = [ + [np.float32, i, [64]] for i in format_list + ] + self.add_relu_alpha_result(shape_format) + + def test_add_relu_alpha_shape_format_fp16_2d(self): + format_list = [0, 3, 29] + shape_format = [ + [np.float16, i, [5, 256]] for i in format_list + ] + self.add_relu_alpha_result(shape_format) + + def test_add_relu_alpha_shape_format_fp32_2d(self): + format_list = [0, 3, 29] + shape_format = [ + [np.float32, i, [5, 256]] for i in format_list + ] + self.add_relu_alpha_result(shape_format) + + def test_add_relu_alpha_shape_format_fp16_3d(self): + format_list = [0, 3, 29] + shape_format = [ + [np.float16, i, [32, 3, 3]] for i in format_list + ] + self.add_relu_alpha_result(shape_format) + + def test_add_relu_alpha_shape_format_fp32_3d(self): + format_list = [0, 3, 29] + shape_format = [ + [np.float32, i, [32, 3, 3]] for i in format_list + ] + self.add_relu_alpha_result(shape_format) + + def test_add_relu_alpha_shape_format_fp16_4d(self): + format_list = [0, 3, 29] + shape_format = [ + [np.float16, i, [64, 112, 7, 7]] for i in format_list + ] + self.add_relu_alpha_result(shape_format) + + def test_add_relu_alpha_shape_format_fp32_4d(self): + format_list = [0, 3, 29] + shape_format = [ + [np.float32, i, [64, 112, 7, 7]] for i in format_list + ] + self.add_relu_alpha_result(shape_format) + + +if __name__ == "__main__": + run_tests() diff --git a/test/test_network_ops/test_arccosh.py b/test/test_network_ops/test_arccosh.py new file mode 100644 index 00000000000..0dc22ba76c2 --- /dev/null +++ b/test/test_network_ops/test_arccosh.py @@ -0,0 +1,89 @@ +# 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 numpy as np +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests +from torch_npu.testing.common_utils import create_common_tensor + + +class TestArccosh(TestCase): + + def cpu_op_exec(self, input1): + output = torch.arccosh(input1) + output = output.numpy() + return output + + def npu_op_exec(self, input1): + output = torch.arccosh(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def npu_op_exec_out(self, input1, input2): + torch.arccosh(input1, out=input2) + output = input2.to("cpu") + output = output.numpy() + return output + + def cpu_inp_op_exec(self, input1): + output = torch.arccosh_(input1) + output = output.numpy() + return output + + def npu_inp_op_exec(self, input1): + output = torch.arccosh_(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def test_arccosh_common_shape_format(self): + shape_format = [ + [[np.float32, 0, (5, 3)]], + ] + for item in shape_format: + cpu_input1, npu_input1 = create_common_tensor(item[0], -10, 10) + cpu_output = self.cpu_op_exec(cpu_input1) + npu_output = self.npu_op_exec(npu_input1) + mask = ~(np.isnan(cpu_output) | np.isinf(cpu_output)) + self.assertRtolEqual(cpu_output[mask], npu_output[mask], 0.001) + + def test_arccosh_out_common_shape_format(self): + shape_format = [ + [[np.float32, 0, (4, 3)], [np.float32, 0, (4, 3)]], + ] + for item in shape_format: + cpu_input1, npu_input1 = create_common_tensor(item[0], -10, 10) + cpu_input2, npu_input2 = create_common_tensor(item[1], -10, 10) + cpu_output = self.cpu_op_exec(cpu_input1) + npu_output = self.npu_op_exec_out(npu_input1, npu_input2) + mask = ~(np.isnan(cpu_output) | np.isinf(cpu_output)) + self.assertRtolEqual(cpu_output[mask], npu_output[mask], 0.001) + + def test_arccosh_inp_common_shape_format(self): + shape_format = [ + [[np.float32, 0, (5, 3)]], + ] + for item in shape_format: + cpu_input1, npu_input1 = create_common_tensor(item[0], -10, 10) + cpu_output = self.cpu_inp_op_exec(cpu_input1) + npu_output = self.npu_inp_op_exec(npu_input1) + mask = ~(np.isnan(cpu_output) | np.isinf(cpu_output)) + self.assertRtolEqual(cpu_output[mask], npu_output[mask], 0.001) + + +if __name__ == "__main__": + run_tests() \ No newline at end of file diff --git a/test/test_network_ops/test_arcsin.py b/test/test_network_ops/test_arcsin.py new file mode 100644 index 00000000000..fa22c640ca8 --- /dev/null +++ b/test/test_network_ops/test_arcsin.py @@ -0,0 +1,83 @@ +# 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 numpy as np +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests +from torch_npu.testing.common_utils import create_common_tensor + +class TestArcsin(TestCase): + def cpu_op_exec(self, input1): + output = torch.arcsin(input1) + output = output.numpy() + return output + + def npu_op_exec(self, input1): + output = torch.arcsin(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def npu_op_exec_out(self, input1, input2): + torch.arcsin(input1, out=input2) + output = input2.to("cpu") + output = output.numpy() + return output + + def cpu_inp_op_exec(self, input1): + output = torch.arcsin_(input1) + output = output.numpy() + return output + + def npu_inp_op_exec(self, input1): + output = torch.arcsin_(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def test_arcsin_common_shape_format(self): + shape_format = [ + [[np.float32, 0, (5,3)]], + ] + for item in shape_format: + cpu_input1, npu_input1 = create_common_tensor(item[0], -1, 1) + cpu_output = self.cpu_op_exec(cpu_input1) + npu_output = self.npu_op_exec(npu_input1) + self.assertRtolEqual(cpu_output, npu_output) + + def test_arcsin_out_common_shape_format(self): + shape_format = [ + [[np.float32, 0, (4,3)], [np.float32, 0, (4,3)]], + ] + for item in shape_format: + cpu_input1, npu_input1 = create_common_tensor(item[0], -1, 1) + cpu_input2, npu_input2 = create_common_tensor(item[1], -1, 1) + cpu_output = self.cpu_op_exec(cpu_input1) + npu_output = self.npu_op_exec_out(npu_input1, npu_input2) + self.assertRtolEqual(cpu_output, npu_output) + + def test_arcsin_inp_common_shape_format(self): + shape_format = [ + [[np.float32, 0, (5,3)]], + ] + for item in shape_format: + cpu_input1, npu_input1 = create_common_tensor(item[0], -1, 1) + cpu_output = self.cpu_inp_op_exec(cpu_input1) + npu_output = self.npu_inp_op_exec(npu_input1) + self.assertRtolEqual(cpu_output, npu_output) + + +if __name__ == "__main__": + run_tests() diff --git a/test/test_network_ops/test_arcsinh.py b/test/test_network_ops/test_arcsinh.py new file mode 100644 index 00000000000..0a373bc3e45 --- /dev/null +++ b/test/test_network_ops/test_arcsinh.py @@ -0,0 +1,83 @@ +# 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 numpy as np +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests +from torch_npu.testing.common_utils import create_common_tensor + +class TestArcsinh(TestCase): + def cpu_op_exec(self, input1): + output = torch.arcsinh(input1) + output = output.numpy() + return output + + def npu_op_exec(self, input1): + output = torch.arcsinh(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def npu_op_exec_out(self, input1, input2): + torch.arcsinh(input1, out=input2) + output = input2.to("cpu") + output = output.numpy() + return output + + def cpu_inp_op_exec(self, input1): + output = torch.arcsinh_(input1) + output = output.numpy() + return output + + def npu_inp_op_exec(self, input1): + output = torch.arcsinh_(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def test_arcsinh_common_shape_format(self): + shape_format = [ + [[np.float32, 0, (5, 3)]], + ] + for item in shape_format: + cpu_input1, npu_input1 = create_common_tensor(item[0], -1, 1) + cpu_output = self.cpu_op_exec(cpu_input1) + npu_output = self.npu_op_exec(npu_input1) + self.assertRtolEqual(cpu_output, npu_output) + + def test_arcsinh_out_common_shape_format(self): + shape_format = [ + [[np.float32, 0, (4, 3)], [np.float32, 0, (4, 3)]], + ] + for item in shape_format: + cpu_input1, npu_input1 = create_common_tensor(item[0], -1, 1) + cpu_input2, npu_input2 = create_common_tensor(item[1], -1, 1) + cpu_output = self.cpu_op_exec(cpu_input1) + npu_output = self.npu_op_exec_out(npu_input1, npu_input2) + self.assertRtolEqual(cpu_output, npu_output) + + def test_arcsinh_inp_common_shape_format(self): + shape_format = [ + [[np.float32, 0, (5, 3)]], + ] + for item in shape_format: + cpu_input1, npu_input1 = create_common_tensor(item[0], -1, 1) + cpu_output = self.cpu_inp_op_exec(cpu_input1) + npu_output = self.npu_inp_op_exec(npu_input1) + self.assertRtolEqual(cpu_output, npu_output) + + +if __name__ == "__main__": + run_tests() diff --git a/test/test_network_ops/test_arctan.py b/test/test_network_ops/test_arctan.py new file mode 100644 index 00000000000..6ba87962cd3 --- /dev/null +++ b/test/test_network_ops/test_arctan.py @@ -0,0 +1,98 @@ +# Copyright (c) 2020 Huawei Technologies Co., Ltd +# Copyright (c) 2019, Facebook CORPORATION. +# 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 numpy as np +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests +from torch_npu.testing.common_utils import create_common_tensor + + +class TestArctan(TestCase): + def cpu_op_exec(self, input1): + output = torch.arctan(input1) + output = output.numpy() + return output + + def npu_op_exec(self, input1): + output = torch.arctan(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def npu_op_exec_out(self, input1, input2): + torch.arctan(input1, out=input2) + output = input2.to("cpu") + output = output.numpy() + return output + + def cpu_inp_op_exec(self, input1): + output = torch.arctan_(input1) + output = output.numpy() + return output + + def npu_inp_op_exec(self, input1): + output = torch.arctan_(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def test_arctan_shape_format(self): + shape_format1 = [ + [[np.float32, 0, 1]], + [[np.float32, 0, (64, 10)]], + [[np.float32, 3, (256, 2048, 7, 7)]], + [[np.float32, 4, (32, 1, 3, 3)]], + [[np.float32, 29, (10, 128)]] + ] + for item in shape_format1: + cpu_input1, npu_input1 = create_common_tensor(item[0], -1, 1) + cpu_output = self.cpu_op_exec(cpu_input1) + npu_output = self.npu_op_exec(npu_input1) + self.assertRtolEqual(cpu_output, npu_output) + + def test_arctan_out_shape_format(self): + shape_format1 = [ + [[np.float32, 0, 1]], + [[np.float32, 0, (64, 10)]], + [[np.float32, 3, (256, 2048, 7, 7)]], + [[np.float32, 4, (32, 1, 3, 3)]], + [[np.float32, 29, (10, 128)]] + ] + for item in shape_format1: + cpu_input1, npu_input1 = create_common_tensor(item[0], -1, 1) + cpu_input2, npu_input2 = create_common_tensor(item[0], -1, 1) + cpu_output1 = self.cpu_op_exec(cpu_input1) + npu_output1 = self.npu_op_exec_out(npu_input1, npu_input2) + self.assertRtolEqual(cpu_output1, npu_output1) + + def test_arctan_inp_shape_format(self): + shape_format1 = [ + [[np.float32, 0, 1]], + [[np.float32, 0, (64, 10)]], + [[np.float32, 3, (256, 2048, 7, 7)]], + [[np.float32, 4, (32, 1, 3, 3)]], + [[np.float32, 29, (10, 128)]] + ] + for item in shape_format1: + cpu_input1, npu_input1 = create_common_tensor(item[0], -1, 1) + cpu_output1 = self.cpu_inp_op_exec(cpu_input1) + npu_output1 = self.npu_inp_op_exec(npu_input1) + self.assertRtolEqual(cpu_output1, npu_output1) + + +if __name__ == "__main__": + run_tests() \ No newline at end of file diff --git a/test/test_network_ops/test_arctanh.py b/test/test_network_ops/test_arctanh.py new file mode 100644 index 00000000000..993432c8caf --- /dev/null +++ b/test/test_network_ops/test_arctanh.py @@ -0,0 +1,98 @@ +# Copyright (c) 2020 Huawei Technologies Co., Ltd +# Copyright (c) 2019, Facebook CORPORATION. +# 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 numpy as np +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests +from torch_npu.testing.common_utils import create_common_tensor + + +class TestArctanh(TestCase): + def cpu_op_exec(self, input1): + output = torch.arctanh(input1) + output = output.numpy() + return output + + def npu_op_exec(self, input1): + output = torch.arctanh(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def npu_op_exec_out(self, input1, input2): + torch.arctanh(input1, out=input2) + output = input2.to("cpu") + output = output.numpy() + return output + + def cpu_inp_op_exec(self, input1): + output = torch.arctanh_(input1) + output = output.numpy() + return output + + def npu_inp_op_exec(self, input1): + output = torch.arctanh_(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def test_arctan_shape_format(self): + shape_format = [ + [[np.float32, 0, 1]], + [[np.float32, 0, (64, 10)]], + [[np.float32, 3, (256, 2048, 7, 7)]], + [[np.float32, 4, (32, 1, 3, 3)]], + [[np.float32, 29, (10, 128)]] + ] + for item in shape_format: + cpu_input, npu_input = create_common_tensor(item[0], -1, 1) + cpu_output = self.cpu_op_exec(cpu_input) + npu_output = self.npu_op_exec(npu_input) + self.assertRtolEqual(cpu_output, npu_output) + + def test_arctan_out_shape_format(self): + shape_format = [ + [[np.float32, 0, 1]], + [[np.float32, 0, (64, 10)]], + [[np.float32, 3, (256, 2048, 7, 7)]], + [[np.float32, 4, (32, 1, 3, 3)]], + [[np.float32, 29, (10, 128)]] + ] + for item in shape_format: + cpu_input1, npu_input1 = create_common_tensor(item[0], -1, 1) + cpu_input2, npu_input2 = create_common_tensor(item[0], -1, 1) + cpu_output = self.cpu_op_exec(cpu_input1) + npu_output = self.npu_op_exec_out(npu_input1, npu_input2) + self.assertRtolEqual(cpu_output, npu_output) + + def test_arctan_inp_shape_format(self): + shape_format = [ + [[np.float32, 0, 1]], + [[np.float32, 0, (64, 10)]], + [[np.float32, 3, (256, 2048, 7, 7)]], + [[np.float32, 4, (32, 1, 3, 3)]], + [[np.float32, 29, (10, 128)]] + ] + for item in shape_format: + cpu_input, npu_input = create_common_tensor(item[0], -1, 1) + cpu_output = self.cpu_inp_op_exec(cpu_input) + npu_output = self.npu_inp_op_exec(npu_input) + self.assertRtolEqual(cpu_output, npu_output) + + +if __name__ == "__main__": + run_tests() \ No newline at end of file diff --git a/test/test_network_ops/test_asinh.py b/test/test_network_ops/test_asinh.py new file mode 100644 index 00000000000..b4a736553af --- /dev/null +++ b/test/test_network_ops/test_asinh.py @@ -0,0 +1,83 @@ +# 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 numpy as np +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests +from torch_npu.testing.common_utils import create_common_tensor + +class TestAsinh(TestCase): + def cpu_op_exec(self, input1): + output = torch.asinh(input1) + output = output.numpy() + return output + + def npu_op_exec(self, input1): + output = torch.asinh(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def npu_op_exec_out(self, input1, input2): + torch.asinh(input1, out=input2) + output = input2.to("cpu") + output = output.numpy() + return output + + def cpu_inp_op_exec(self, input1): + output = torch.asinh_(input1) + output = output.numpy() + return output + + def npu_inp_op_exec(self, input1): + output = torch.asinh_(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def test_asinh_common_shape_format(self): + shape_format1 = [ + [[np.float32, 0, (5, 3)]], + ] + for item in shape_format1: + cpu_input1, npu_input1 = create_common_tensor(item[0], -1, 1) + cpu_output = self.cpu_op_exec(cpu_input1) + npu_output = self.npu_op_exec(npu_input1) + self.assertRtolEqual(cpu_output, npu_output) + + def test_asinh_out_common_shape_format(self): + shape_format1 = [ + [[np.float32, 0, (4, 3)], [np.float32, 0, (4, 3)]], + ] + for item in shape_format1: + cpu_input1, npu_input1 = create_common_tensor(item[0], -1, 1) + cpu_input2, npu_input2 = create_common_tensor(item[1], -1, 1) + cpu_output = self.cpu_op_exec(cpu_input1) + npu_output = self.npu_op_exec_out(npu_input1, npu_input2) + self.assertRtolEqual(cpu_output, npu_output) + + def test_asinh_inp_common_shape_format(self): + shape_format1 = [ + [[np.float32, 0, (5, 3)]], + ] + for item in shape_format1: + cpu_input1, npu_input1 = create_common_tensor(item[0], -1, 1) + cpu_output = self.cpu_inp_op_exec(cpu_input1) + npu_output = self.npu_inp_op_exec(npu_input1) + self.assertRtolEqual(cpu_output, npu_output) + + +if __name__ == "__main__": + run_tests() diff --git a/test/test_network_ops/test_atanh.py b/test/test_network_ops/test_atanh.py new file mode 100644 index 00000000000..b4a9718494c --- /dev/null +++ b/test/test_network_ops/test_atanh.py @@ -0,0 +1,98 @@ +# Copyright (c) 2020 Huawei Technologies Co., Ltd +# Copyright (c) 2019, Facebook CORPORATION. +# 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 numpy as np +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests +from torch_npu.testing.common_utils import create_common_tensor + + +class TestAtanh(TestCase): + def cpu_op_exec(self, input1): + output = torch.atanh(input1) + output = output.numpy() + return output + + def npu_op_exec(self, input1): + output = torch.atanh(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def npu_op_exec_out(self, input1, input2): + torch.atanh(input1, out=input2) + output = input2.to("cpu") + output = output.numpy() + return output + + def cpu_inp_op_exec(self, input1): + output = torch.atanh_(input1) + output = output.numpy() + return output + + def npu_inp_op_exec(self, input1): + output = torch.atanh_(input1) + output = output.to("cpu") + output = output.numpy() + return output + + def test_atanh_shape_format(self): + shape_format1 = [ + [[np.float32, 0, 1]], + [[np.float32, 0, (64, 10)]], + [[np.float32, 3, (256, 2048, 7, 7)]], + [[np.float32, 4, (32, 1, 3, 3)]], + [[np.float32, 29, (10, 128)]] + ] + for item in shape_format1: + cpu_input1, npu_input1 = create_common_tensor(item[0], -1, 1) + cpu_output1 = self.cpu_op_exec(cpu_input1) + npu_output1 = self.npu_op_exec(npu_input1) + self.assertRtolEqual(cpu_output1, npu_output1) + + def test_atanh_out_shape_format(self): + shape_format = [ + [[np.float32, 0, 1]], + [[np.float32, 0, (64, 10)]], + [[np.float32, 3, (256, 2048, 7, 7)]], + [[np.float32, 4, (32, 1, 3, 3)]], + [[np.float32, 29, (10, 128)]] + ] + for item in shape_format: + cpu_input1, npu_input1 = create_common_tensor(item[0], -1, 1) + cpu_input, npu_input = create_common_tensor(item[0], -1, 1) + cpu_output = self.cpu_op_exec(cpu_input1) + npu_output = self.npu_op_exec_out(npu_input1, npu_input) + self.assertRtolEqual(cpu_output, npu_output) + + def test_atanh_inp_shape_format(self): + shape_format1 = [ + [[np.float32, 0, 1]], + [[np.float32, 0, (64, 10)]], + [[np.float32, 3, (256, 2048, 7, 7)]], + [[np.float32, 4, (32, 1, 3, 3)]], + [[np.float32, 29, (10, 128)]] + ] + for item in shape_format1: + cpu_input, npu_input = create_common_tensor(item[0], -1, 1) + cpu_output = self.cpu_inp_op_exec(cpu_input) + npu_output = self.npu_inp_op_exec(npu_input) + self.assertRtolEqual(cpu_output, npu_output) + + +if __name__ == "__main__": + run_tests() \ No newline at end of file diff --git a/test/test_network_ops/test_clip.py b/test/test_network_ops/test_clip.py new file mode 100644 index 00000000000..dcf43b2fc5e --- /dev/null +++ b/test/test_network_ops/test_clip.py @@ -0,0 +1,156 @@ +# Copyright (c) 2020 Huawei Technologies Co., Ltd +# Copyright (c) 2019, Facebook CORPORATION. +# 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 numpy as np +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests + + +class TestClip(TestCase): + def generate_data(self, data): + input1 = np.random.uniform(data[0], data[1], data[2]).astype(data[3]) + input1 = torch.from_numpy(input1) + + return input1 + + def npu_op_exec(self, input1, min_val, max_val): + input1 = input1.to("npu") + output = torch.clip(input1, min_val, max_val) + output = output.to("cpu") + output = output.numpy() + + return output + + def cpu_op_exec(self, input1, min_val, max_val): + output = torch.clip(input1, min_val, max_val) + output = output.numpy() + + return output + + def cpu_op_exec_float16(self, input1, min_val, max_val): + input1 = input1.to(torch.float32) + output = torch.clip(input1, min_val, max_val).to(torch.float16) + output = output.numpy() + + return output + + def npu_inp_op_exec(self, input1, min_val, max_val): + input1 = input1.to("npu") + output = torch.clip_(input1, min_val, max_val) + output = output.to("cpu") + output = output.numpy() + + return output + + def cpu_inp_op_exec(self, input1, min_val, max_val): + output = torch.clip_(input1, min_val, max_val) + output = output.numpy() + + return output + + def cpu_inp_op_exec_float16(self, input1, min_val, max_val): + input1 = input1.to(torch.float32) + output = torch.clip_(input1, min_val, max_val).to(torch.float16) + output = output.numpy() + + return output + + def npu_op_exec_out(self, input1, min_val, max_val, input2): + input1 = input1.to("npu") + output = input2.to("npu") + torch.clip(input1, min_val, max_val, out=output) + output = output.to("cpu") + output = output.numpy() + + return output + + def npu_inp_uncon_op_exec(self, input1, min_val, max_val): + input1 = input1.to("npu") + input1 = input1.as_strided([2, 2], [1, 2], 2) + output = torch.clip_(input1, min_val, max_val) + output = output.to("cpu") + output = output.numpy() + + return output + + def cpu_inp_uncon_op_exec(self, input1, min_val, max_val): + input1 = input1.as_strided([2, 2], [1, 2], 2) + output = torch.clip(input1, min_val, max_val) + output = output.numpy() + + return output + + def cpu_inp_uncon_op_exec_float16(self, input1, min_val, max_val): + input1 = input1.to(torch.float32).as_strided([2, 2], [1, 2], 2) + output = torch.clip(input1, min_val, max_val).to(torch.float16) + output = output.numpy() + + return output + + def test_clip_common(self): + shape_format = [ + [1, 100, (4, 3), np.float32], + [1, 100, (4, 3), np.int32], + ] + for item in shape_format: + input1 = self.generate_data(item) + + cpu_output = self.cpu_op_exec(input1, 40, 60) + npu_output = self.npu_op_exec(input1, 40, 60) + + cpu_inp_output = self.cpu_inp_op_exec(input1, 40, 60) + npu_inp_output = self.npu_inp_op_exec(input1, 40, 60) + + input2 = self.generate_data(item) + npu_out_output = self.npu_op_exec_out(input1, 40, 60, input2) + + cpu_inp_uncon_output = self.cpu_inp_uncon_op_exec(input1, 40, 60) + npu_inp_uncon_output = self.npu_inp_uncon_op_exec(input1, 40, 60) + + self.assertRtolEqual(cpu_output, npu_output) + self.assertRtolEqual(cpu_inp_output, npu_inp_output) + self.assertRtolEqual(cpu_output, npu_out_output) + self.assertRtolEqual(cpu_inp_uncon_output, npu_inp_uncon_output) + + def test_clip_float16(self): + shape_format = [ + [1, 100, (4, 3), np.float16], + ] + for item in shape_format: + input1 = self.generate_data(item) + + cpu_output = self.cpu_op_exec_float16(input1, 40, 60) + npu_output = self.npu_op_exec(input1, 40, 60) + + cpu_inp_output = self.cpu_inp_op_exec_float16(input1, 40, 60) + npu_inp_output = self.npu_inp_op_exec(input1, 40, 60) + + input2 = self.generate_data(item) + npu_out_output = self.npu_op_exec_out(input1, 40, 60, input2) + + cpu_inp_uncon_output = self.cpu_inp_uncon_op_exec_float16(input1, 40, 60) + npu_inp_uncon_output = self.npu_inp_uncon_op_exec(input1, 40, 60) + + self.assertRtolEqual(cpu_output, npu_output) + self.assertRtolEqual(cpu_inp_output, npu_inp_output) + self.assertRtolEqual(cpu_output, npu_out_output) + self.assertRtolEqual(cpu_inp_uncon_output, npu_inp_uncon_output) + + +if __name__ == "__main__": + run_tests() diff --git a/test/test_network_ops/test_divide.py b/test/test_network_ops/test_divide.py new file mode 100644 index 00000000000..58453b3ce35 --- /dev/null +++ b/test/test_network_ops/test_divide.py @@ -0,0 +1,126 @@ +# 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 unittest +import torch +import numpy as np +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests +from torch_npu.testing.common_utils import create_common_tensor, test_2args_broadcast, create_dtype_tensor +from torch_npu.testing.decorator import Dtypes, instantiate_tests + + +@instantiate_tests +class TestDivide(TestCase): + def get_outputs(self, cpu_args, npu_args, dtype): + # cpu not support fp16 div + cpu_args = [i.float() if dtype == torch.half else i for i in cpu_args] + cpu_output = torch.divide(cpu_args[0], cpu_args[1]).to(dtype).numpy() + npu_output = torch.divide(npu_args[0], npu_args[1]).to("cpu").numpy() + return cpu_output, npu_output + + def get_outputs_chk(self, cpu_args, npu_args, dtype): + # cpu not support fp16 div + cpu_out = torch.randn(6).to(dtype) + npu_out = torch.randn(6).to("npu").to(dtype) + cpu_args = [i.float() if dtype == torch.half else i for i in cpu_args] + torch.divide(cpu_args[0], cpu_args[1], out=cpu_out) + torch.divide(npu_args[0], npu_args[1], out=npu_out) + cpu_output = cpu_out.to(dtype).numpy() + npu_output = npu_out.to("cpu").numpy() + return cpu_output, npu_output + + def test_divide_broadcast(self): + for item in test_2args_broadcast(torch.divide): + self.assertRtolEqual(item[0], item[1]) + + # divide not support bool + @Dtypes(torch.float, torch.half, torch.int) + def test_divide_dtype(self, dtype): + cpu_input1, npu_input1 = create_dtype_tensor((2, 3, 4, 5), dtype) + cpu_input2, npu_input2 = create_dtype_tensor((2, 3, 4, 5), dtype, no_zero=True) + cpu_output, npu_output = self.get_outputs([cpu_input1, cpu_input2], [npu_input1, npu_input2], dtype) + + if dtype == torch.int: + cpu_output = np.floor_divide(cpu_input1.numpy(), cpu_input2.numpy()) + + self.assertRtolEqual(cpu_output, npu_output) + + def test_divide_shape_format_fp16(self): + format_list = [0, 3, 29] + shape_list = [1, (64, 10), (32, 3, 3), (256, 2048, 7, 7)] + shape_format1 = [ + [np.float16, i, j] for i in format_list for j in shape_list + ] + for item in shape_format1: + cpu_input1, npu_input1 = create_common_tensor(item, 1, 100) + cpu_input2, npu_input2 = create_common_tensor(item, 1, 100) + cpu_input1 = cpu_input1.to(torch.float32) + cpu_input2 = cpu_input2.to(torch.float32) + cpu_output, npu_output = self.get_outputs([cpu_input1, cpu_input2], [npu_input1, npu_input2], torch.half) + self.assertRtolEqual(cpu_output, npu_output) + + def test_divide_shape_format_fp32(self): + format_list = [0, 3, 29] + shape_list = [1, (64, 10), (32, 3, 3), (256, 2048, 7, 7), (2, 0, 2)] + shape_format1 = [ + [np.float32, i, j] for i in format_list for j in shape_list + ] + for item in shape_format1: + cpu_input1, npu_input1 = create_common_tensor(item, 1, 100) + cpu_input2, npu_input2 = create_common_tensor(item, 1, 100) + cpu_output, npu_output = self.get_outputs([cpu_input1, cpu_input2], [npu_input1, npu_input2], torch.float) + self.assertRtolEqual(cpu_output, npu_output) + + def test_divide_mix_dtype_1(self): + npu_input1, npu_input2 = create_common_tensor([np.int32, 0, (2, 3)], 1, 100) + npu_input3, npu_input4 = create_common_tensor([np.float32, 0, (2, 3)], 1, 100) + cpu_output, npu_output = self.get_outputs([npu_input1, npu_input3], [npu_input2, npu_input4], torch.float) + self.assertRtolEqual(cpu_output, npu_output) + + def test_divide_mix_dtype_2(self): + npu_input1, npu_input2 = create_common_tensor([np.float32, 0, (2, 3)], 1, 100) + npu_input3 = torch.tensor(3).int() + cpu_output, npu_output = self.get_outputs([npu_input1, npu_input3], [npu_input2, npu_input3], torch.float) + self.assertRtolEqual(cpu_output, npu_output) + + def test_divide_scalar_dtype(self): + cpu_input1, npu_input1 = create_common_tensor([np.int32, 0, (2, 3)], 1, 100) + cpu_output = cpu_input1 / 0.5 + npu_output = npu_input1 / 0.5 + self.assertRtolEqual(cpu_output, npu_output.cpu()) + + def test_divide_npuscalar_dtype(self): + cpu_input1, npu_input1 = create_common_tensor([np.int32, 0, (2, 3)], 1, 100) + cpu_output1 = cpu_input1 / torch.tensor(0.5) + npu_output1 = npu_input1 / torch.tensor(0.5).npu() + self.assertRtolEqual(cpu_output1, npu_output1.cpu()) + + def test_divide_shape_format_fp32_1(self): + format_list = [0, 3, 29] + shape_list = [1, (64, 10), (32, 3, 3), (256, 2048, 7, 7)] + shape_format1 = [ + [np.float32, i, j] for i in format_list for j in shape_list + ] + for item in shape_format1: + cpu_input1, npu_input1 = create_common_tensor(item, 1, 100) + cpu_input2, npu_input2 = create_common_tensor(item, 1, 100) + cpu_output, npu_output = self.get_outputs_chk([cpu_input1, cpu_input2], + [npu_input1, npu_input2], torch.float) + self.assertRtolEqual(cpu_output, npu_output) + + +if __name__ == "__main__": + run_tests() diff --git a/torch_npu/csrc/aten/ops/AcoshKernelNpu.cpp b/torch_npu/csrc/aten/ops/AcoshKernelNpu.cpp new file mode 100644 index 00000000000..83b8fd6187c --- /dev/null +++ b/torch_npu/csrc/aten/ops/AcoshKernelNpu.cpp @@ -0,0 +1,59 @@ +// 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& acosh_out_npu_nocheck(at::Tensor& result, const at::Tensor& self) { + OpCommand cmd; + cmd.Name("Acosh") + .Input(self) + .Output(result) + .Run(); + return result; +} + +at::Tensor& NPUNativeFunctions::acosh_out( + const at::Tensor& self, + at::Tensor& result) { + OpPreparation::CheckOut( + {self}, + result, + self); + if (!NpuUtils::check_match(&result)) { + at::Tensor contiguousResult = NpuUtils::format_contiguous(result); + acosh_out_npu_nocheck(contiguousResult, self); + NpuUtils::format_fresh_view(result, contiguousResult); + } else { + acosh_out_npu_nocheck(result, self); + } + return result; + +} + +at::Tensor NPUNativeFunctions::acosh(const at::Tensor& self) { + at::Tensor result = OpPreparation::ApplyTensor(self); + acosh_out_npu_nocheck(result, self); + return result; +} + +at::Tensor& NPUNativeFunctions::acosh_(at::Tensor& self) { + + return acosh_out(self, self); +} + +} // namespace native +} // namespace at_npu diff --git a/torch_npu/csrc/aten/ops/AddReluKernelNpu.cpp b/torch_npu/csrc/aten/ops/AddReluKernelNpu.cpp new file mode 100644 index 00000000000..213bc17b2c0 --- /dev/null +++ b/torch_npu/csrc/aten/ops/AddReluKernelNpu.cpp @@ -0,0 +1,64 @@ +// 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/framework/utils/CalcuOpUtil.h" +#include "torch_npu/csrc/aten/NPUNativeFunctions.h" + +namespace at_npu { +namespace native { + +at::Tensor &add_relu_out_nocheck( + const at::Tensor& self, + const at::Tensor& other, + at::Scalar alpha, + at::Tensor& result){ + at::Tensor addResult = at::add(self, other, alpha); + OpCommand cmd; + cmd.Name("Relu") + .Input(addResult) + .Output(result) + .Run(); + return result; +} + +at::Tensor& NPUNativeFunctions::_add_relu_out( + const at::Tensor& self, + const at::Tensor& other, + at::Scalar alpha, + at::Tensor& result) { + if (!NpuUtils::check_match(&result)) + { + at::Tensor contiguousResult = NpuUtils::format_contiguous(result); + add_relu_out_nocheck(self, other, alpha, contiguousResult); + NpuUtils::format_fresh_view(result, contiguousResult); + }else{ + add_relu_out_nocheck(self, other, alpha, result); + } + return result; +} + +at::Tensor NPUNativeFunctions::_add_relu(const at::Tensor& self, const at::Tensor& other, at::Scalar alpha) { + at::Tensor result = OpPreparation::ApplyTensor(self); + return _add_relu_out(self, other, alpha, result); +} + +at::Tensor& NPUNativeFunctions::_add_relu_(at::Tensor& self, const at::Tensor& other, at::Scalar alpha) { + c10::SmallVector inputs = {self, other}; + c10::SmallVector outputs = {self}; + CalcuOpUtil::check_memory_over_laps(inputs, outputs); + return _add_relu_out(self, other, alpha, self); +} +} // namespace native +} // namespace at_npu \ No newline at end of file diff --git a/torch_npu/csrc/aten/ops/AsinhKernelNpu.cpp b/torch_npu/csrc/aten/ops/AsinhKernelNpu.cpp new file mode 100644 index 00000000000..331bbd3646c --- /dev/null +++ b/torch_npu/csrc/aten/ops/AsinhKernelNpu.cpp @@ -0,0 +1,59 @@ +// 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& asinh_out_npu_nocheck(at::Tensor& result, const at::Tensor& self) { + OpCommand cmd; + cmd.Name("Asinh") + .Input(self) + .Output(result) + .Run(); + return result; +} + +at::Tensor& NPUNativeFunctions::asinh_out( + const at::Tensor& self, + at::Tensor& result) { + OpPreparation::CheckOut( + {self}, + result, + self); + if (!NpuUtils::check_match(&result)) { + at::Tensor contiguousResult = NpuUtils::format_contiguous(result); + asinh_out_npu_nocheck(contiguousResult, self); + NpuUtils::format_fresh_view(result, contiguousResult); + } else { + asinh_out_npu_nocheck(result, self); + } + return result; + +} + +at::Tensor NPUNativeFunctions::asinh(const at::Tensor& self) { + at::Tensor result = OpPreparation::ApplyTensor(self); + asinh_out_npu_nocheck(result, self); + return result; +} + +at::Tensor& NPUNativeFunctions::asinh_(at::Tensor& self) { + + return asinh_out(self, self); +} + +} // namespace native +} // namespace at_npu diff --git a/torch_npu/csrc/aten/ops/AtanhKernelNpu.cpp b/torch_npu/csrc/aten/ops/AtanhKernelNpu.cpp new file mode 100644 index 00000000000..ccd920a59e3 --- /dev/null +++ b/torch_npu/csrc/aten/ops/AtanhKernelNpu.cpp @@ -0,0 +1,56 @@ +// Copyright (c) 2020 Huawei Technologies Co., Ltd +// Copyright (c) 2019, Facebook CORPORATION. +// 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& atanh_out_npu_nocheck(const at::Tensor& self, at::Tensor& result) { + OpCommand cmd; + cmd.Name("Atanh") + .Input(self) + .Output(result) + .Run(); + return result; +} + +at::Tensor& NPUNativeFunctions::atanh_out(const at::Tensor& self, at::Tensor& result) { + OpPreparation::CheckOut( + {self}, + result, + self); + if (!NpuUtils::check_match(&result)) { + at::Tensor contiguousResult = NpuUtils::format_contiguous(result); + atanh_out_npu_nocheck(self, contiguousResult); + NpuUtils::format_fresh_view(result, contiguousResult); + } else { + atanh_out_npu_nocheck(self, result); + } + return result; +} + +at::Tensor NPUNativeFunctions::atanh(const at::Tensor& self) { + at::Tensor result = OpPreparation::ApplyTensor(self); + atanh_out_npu_nocheck(self, result); + return result; +} + +at::Tensor& NPUNativeFunctions::atanh_(at::Tensor& self) { + return atanh_out(self, self); +} + +}} // namespace at_npu::native \ No newline at end of file -- Gitee