diff --git a/test/test_network_ops/test_scatterv1.py b/test/test_network_ops/test_scatterv1.py new file mode 100644 index 0000000000000000000000000000000000000000..22d1546fbfb91b5f676ac92f64c96013449e28fd --- /dev/null +++ b/test/test_network_ops/test_scatterv1.py @@ -0,0 +1,40 @@ +# 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_device_type import instantiate_device_type_tests +from torch_npu.testing.util_test import create_common_tensor +from torch_npu.testing.common_utils import TestCase, run_tests + + +class TestScatterV1(TestCase): + def npu_op_exec(self, input1, indices, updates, dim): + output = torch_npu.npu_scatter(input1, indices, updates, dim) + output = output.to("cpu") + output = output.numpy() + return output + + def test_scatterv1(self, device): + input1 = torch.tensor([[1.6279, 0.1226], [0.9041, 1.0980]]).npu() + indices = torch.tensor([0, 1]).npu().to(torch.int32) + updates = torch.tensor([-1.1993, -1.5247]).npu() + dim = 0 + exoutput = torch.tensor([[-1.1993, 0.1226], [0.9041, -1.5247]]) + output = self.npu_op_exec(input1, indices, updates, dim) + self.assertRtolEqual(exoutput.numpy(), output) + +instantiate_device_type_tests(TestScatterV1, globals(), except_for="cpu") +if __name__ == "__main__": + run_tests() diff --git a/torch_npu/csrc/aten/npu_native_functions.yaml b/torch_npu/csrc/aten/npu_native_functions.yaml index 412fb9da7ce2fdf366e37a24e4b21404c4e90c55..6b6eded8dcba70e9d4b7331b1976d51eb5e18fae 100644 --- a/torch_npu/csrc/aten/npu_native_functions.yaml +++ b/torch_npu/csrc/aten/npu_native_functions.yaml @@ -283,6 +283,9 @@ custom: - func: npu_slice.out(Tensor self, int[] offsets, int[] size, *, Tensor(a!) out) -> Tensor(a!) - func: npu_indexing(Tensor self, int[] begin, int[] end, int[] strides, int begin_mask=0, int end_mask=0, int ellipsis_mask=0, int new_axis_mask=0, int shrink_axis_mask=0) -> Tensor - func: npu_indexing.out(Tensor self, int[] begin, int[] end, int[] strides, int begin_mask=0, int end_mask=0, int ellipsis_mask=0, int new_axis_mask=0, int shrink_axis_mask=0, *, Tensor(a!) out) -> Tensor(a!) + - func: npu_scatter(Tensor self, Tensor indices, Tensor updates, int dim) -> Tensor + variants: function, method + custom_autograd: - func: npu_convolution(Tensor input, Tensor weight, Tensor? bias, int[] stride, int[] padding, int[] dilation, int groups) -> Tensor - func: npu_convolution_transpose(Tensor input, Tensor weight, Tensor? bias, int[] padding, int[] output_padding, int[] stride, int[] dilation, int groups) -> Tensor diff --git a/torch_npu/csrc/aten/ops/ScatterV1KernelNpu.cpp b/torch_npu/csrc/aten/ops/ScatterV1KernelNpu.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9341316f6b8f628a5174917a43a9518ca3bfa499 --- /dev/null +++ b/torch_npu/csrc/aten/ops/ScatterV1KernelNpu.cpp @@ -0,0 +1,50 @@ +// 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/framework/utils/CalcuOpUtil.h" +#include "torch_npu/csrc/aten/NPUNativeFunctions.h" + +namespace at_npu { +namespace native { + +at::Tensor& scatter_out_npu_nocheck( + at::Tensor& output, + const at::Tensor& self, + const at::Tensor& indices, + const at::Tensor& updates, + int64_t dim) { + OpCommand cmd; + cmd.Name("ArgMaxGrad") + .Input(self) + .Input(indices) + .Input(updates) + .Output(output) + .Attr("dimension", dim) + .Run(); + + return output; +} + +at::Tensor NPUNativeFunctions::npu_scatter(const at::Tensor& self, const at::Tensor& indices, const at::Tensor& updates, int64_t dim) { + at::Tensor outputs = OpPreparation::ApplyTensor(self); + scatter_out_npu_nocheck(outputs, self, indices, updates, dim); + + return outputs; +} + +} +}