diff --git a/test/test_api/test_distributed.py b/test/test_api/test_distributed.py index d7b06ea700098bfe17295667fc3a60b2c4d67223..b0c699d4ab608541393c48d9d41d029e64ce5100 100644 --- a/test/test_api/test_distributed.py +++ b/test/test_api/test_distributed.py @@ -1,7 +1,21 @@ +# 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 os import torch -import torch_npu import torch.distributed as dist +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests os.environ["MASTER_ADDR"] = '127.0.0.1' diff --git a/test/test_api/test_nn_func/test_convolution_functions.py b/test/test_api/test_nn_func/test_convolution_functions.py index 40582cd3ede26b4429e9e4505d34949d20be688a..96b8b9cd33a1a5d95acde489c542a186f21c7bb1 100644 --- a/test/test_api/test_nn_func/test_convolution_functions.py +++ b/test/test_api/test_nn_func/test_convolution_functions.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. import torch -import torch_npu import torch.nn as nn import torch.nn.functional as F +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests diff --git a/test/test_api/test_nn_func/test_distance_functions.py b/test/test_api/test_nn_func/test_distance_functions.py index d1bdeae4b5994faa2a349a6cb8f0e82144f2c383..44f6f83d1bc406d0d176b90c7ab9893ac10e5796 100644 --- a/test/test_api/test_nn_func/test_distance_functions.py +++ b/test/test_api/test_nn_func/test_distance_functions.py @@ -14,8 +14,8 @@ import copy import torch -import torch_npu import torch.nn.functional as F +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests @@ -53,5 +53,4 @@ class TestDistanceFunctions(TestCase): self.assertRtolEqual(cpu_output.detach().numpy(), npu_output.detach().cpu().numpy()) if __name__ == "__main__": - torch.npu.set_device(0) run_tests() \ No newline at end of file diff --git a/test/test_api/test_nn_func/test_dropout_functions.py b/test/test_api/test_nn_func/test_dropout_functions.py index 4b18eed7239f7e590ac31ec73923d106190b6ca0..bdfc615e3c8d9fa39af5b76c6b37a2a0f4595e16 100644 --- a/test/test_api/test_nn_func/test_dropout_functions.py +++ b/test/test_api/test_nn_func/test_dropout_functions.py @@ -14,8 +14,8 @@ import copy import torch -import torch_npu import torch.nn.functional as F +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests @@ -54,5 +54,4 @@ class TestDropoutFunctions(TestCase): self.assertRtolEqual(cpu_output.numpy(), npu_output.cpu().numpy()) if __name__ == "__main__": - torch.npu.set_device(0) run_tests() \ No newline at end of file diff --git a/test/test_api/test_nn_func/test_linear_functions.py b/test/test_api/test_nn_func/test_linear_functions.py index 5a8ac0d7ec549c8bf039b86d681e7c63c4e1126f..237d198eaa707480ed0e0117e42eb4e913896bb3 100644 --- a/test/test_api/test_nn_func/test_linear_functions.py +++ b/test/test_api/test_nn_func/test_linear_functions.py @@ -14,8 +14,8 @@ import copy import torch -import torch_npu import torch.nn.functional as F +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests @@ -50,5 +50,4 @@ class TestLinearFunctions(TestCase): if __name__ == "__main__": - torch.npu.set_device(0) run_tests() \ No newline at end of file diff --git a/test/test_api/test_nn_func/test_loss_functions.py b/test/test_api/test_nn_func/test_loss_functions.py index 2671e420716934b56d4302131d6feea86453daf8..03046ced122960f1c4ff07828146482d9750a6e3 100644 --- a/test/test_api/test_nn_func/test_loss_functions.py +++ b/test/test_api/test_nn_func/test_loss_functions.py @@ -14,8 +14,8 @@ import copy import torch -import torch_npu import torch.nn.functional as F +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests @@ -69,20 +69,20 @@ class TestLossFunctions(TestCase): self.assertRtolEqual(cpu_output.detach().numpy(), npu_output.detach().cpu().numpy()) def test_ctc_loss(self): - log_probs = torch.randn(50, 16, 20).log_softmax(2).detach().requires_grad_() - targets = torch.randint(1, 20, (16, 30), dtype=torch.long) - input_lengths = torch.full((16,), 50, dtype=torch.long) - target_lengths = torch.randint(10,30,(16,), dtype=torch.long) - - npu_log_probs = copy.deepcopy(log_probs).npu() - npu_targets = copy.deepcopy(targets).npu().int() - npu_input_lengths = copy.deepcopy(input_lengths).npu().int() - npu_target_lengths = copy.deepcopy(target_lengths).npu().int() + log_probs = torch.randn(50, 16, 20).npu().log_softmax(2) + targets = torch.randint(1, 20, (16, 30), dtype=torch.int32).npu() + input_lengths = torch.full((16,), 50, dtype=torch.int32).npu() + target_lengths = torch.randint(10,30,(16,), dtype=torch.int32).npu() + + cpu_log_probs = copy.deepcopy(log_probs).cpu() + cpu_targets = copy.deepcopy(targets).cpu().long() + cpu_input_lengths = copy.deepcopy(input_lengths).cpu().long() + cpu_target_lengths = copy.deepcopy(target_lengths).cpu().long() - cpu_output = F.ctc_loss(log_probs, targets, input_lengths, target_lengths) - npu_output = F.ctc_loss(npu_log_probs, npu_targets, npu_input_lengths, npu_target_lengths) + npu_output = F.ctc_loss(log_probs, targets, input_lengths, target_lengths) + cpu_output = F.ctc_loss(cpu_log_probs, cpu_targets, cpu_input_lengths, cpu_target_lengths) - self.assertRtolEqual(cpu_output.detach().numpy(), npu_output.detach().cpu().numpy()) + self.assertRtolEqual(cpu_output.numpy(), npu_output.cpu().numpy()) def test_hinge_embedding_loss(self): input1 = torch.randn(5, 3) @@ -210,5 +210,4 @@ class TestLossFunctions(TestCase): if __name__ == "__main__": - torch.npu.set_device(0) run_tests() \ No newline at end of file diff --git a/test/test_api/test_nn_func/test_nonlinear_activation_functions.py b/test/test_api/test_nn_func/test_nonlinear_activation_functions.py index 811ad4531e8e6ba4d0c715a3a20cb5c8ff3d75f8..22870136de9b60666000351960a4d4852dbf00e2 100644 --- a/test/test_api/test_nn_func/test_nonlinear_activation_functions.py +++ b/test/test_api/test_nn_func/test_nonlinear_activation_functions.py @@ -15,8 +15,8 @@ import copy import torch -import torch_npu import torch.nn.functional as F +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests @@ -210,5 +210,4 @@ class TestNonLiACFunctions(TestCase): if __name__ == "__main__": - torch.npu.set_device(0) run_tests() \ No newline at end of file diff --git a/test/test_api/test_nn_func/test_normalization_functions.py b/test/test_api/test_nn_func/test_normalization_functions.py index 4f898634d7c624c4cd6d123818d7275ab08c83fc..3e5c9972933b3fb44e184653abc81da2ff35e854 100644 --- a/test/test_api/test_nn_func/test_normalization_functions.py +++ b/test/test_api/test_nn_func/test_normalization_functions.py @@ -13,9 +13,9 @@ # limitations under the License. import torch -import torch_npu import torch.nn as nn import torch.nn.functional as F +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests diff --git a/test/test_api/test_nn_func/test_pooling_functions.py b/test/test_api/test_nn_func/test_pooling_functions.py index c861f85c1eefd77147b341cce56c423f85aea5c5..dce7b7e606dcde9a8ef41715c5b925c22a3d6fad 100644 --- a/test/test_api/test_nn_func/test_pooling_functions.py +++ b/test/test_api/test_nn_func/test_pooling_functions.py @@ -11,10 +11,11 @@ # 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 torch.nn as nn import torch.nn.functional as F +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests diff --git a/test/test_api/test_nn_func/test_sparse_functions.py b/test/test_api/test_nn_func/test_sparse_functions.py index 79a5230a91cf7a4546fc439eeb7f752d4b0bcdfa..d05b986590684e5b9d4068dd04314b6b91f191d3 100644 --- a/test/test_api/test_nn_func/test_sparse_functions.py +++ b/test/test_api/test_nn_func/test_sparse_functions.py @@ -13,9 +13,9 @@ # limitations under the License. import torch -import torch_npu import torch.nn as nn import torch.nn.functional as F +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests diff --git a/test/test_api/test_nn_func/test_vision_functions.py b/test/test_api/test_nn_func/test_vision_functions.py index a12c8328a2eec128566514d1b8063495edbb52b9..7c82eb8ec3c8d565fc7511957a31901b52f8fde2 100644 --- a/test/test_api/test_nn_func/test_vision_functions.py +++ b/test/test_api/test_nn_func/test_vision_functions.py @@ -13,8 +13,8 @@ # limitations under the License. import torch -import torch_npu import torch.nn.functional as F +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests diff --git a/test/test_api/test_torch/test_locally_disabling_gradient_omputation.py b/test/test_api/test_torch/test_locally_disabling_gradient_omputation.py index 15899c3ca5b9781aa6f2efd48f5d286cee2f345f..8fd41e857161fc04daa93946f24d9d5da064aaa2 100644 --- a/test/test_api/test_torch/test_locally_disabling_gradient_omputation.py +++ b/test/test_api/test_torch/test_locally_disabling_gradient_omputation.py @@ -13,10 +13,9 @@ # limitations under the License. import torch -import torch_npu - from torch_npu.testing.testcase import TestCase, run_tests from torch.testing._internal.common_utils import freeze_rng_state +import torch_npu device = 'npu:0' torch.npu.set_device(device) diff --git a/test/test_api/test_torch/test_math_operations.py b/test/test_api/test_torch/test_math_operations.py index 873738a7e4e7c189ae2601fff7018cbc670813a5..c9569e28b4dcdaf28b61d4432ed78dabce0b46e5 100644 --- a/test/test_api/test_torch/test_math_operations.py +++ b/test/test_api/test_torch/test_math_operations.py @@ -13,12 +13,11 @@ # limitations under the License. import copy -import torch -import torch_npu import numpy as np - +import torch from torch_npu.testing.testcase import TestCase, run_tests from torch.testing._internal.common_utils import freeze_rng_state +import torch_npu device = 'npu:0' torch.npu.set_device(device) diff --git a/test/test_api/test_torch/test_serialization.py b/test/test_api/test_torch/test_serialization.py index 6d00c022e4b9f51555be0d249985d5d8ea37ce87..7b435a8e6ed656fbfaf894cf7919f28429606682 100644 --- a/test/test_api/test_torch/test_serialization.py +++ b/test/test_api/test_torch/test_serialization.py @@ -16,9 +16,9 @@ import os import tempfile import argparse import torch -import torch_npu import torch.nn as nn import torch.nn.functional as F +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests diff --git a/test/test_api/test_torch_nn/test_containers.py b/test/test_api/test_torch_nn/test_containers.py index 888a3d6b5af71203d3d69564917aea11c43de25a..a55a7350c61ad7d234a87fd371dcfc0efdd38be8 100644 --- a/test/test_api/test_torch_nn/test_containers.py +++ b/test/test_api/test_torch_nn/test_containers.py @@ -14,10 +14,10 @@ from collections import OrderedDict import torch -import torch_npu import torch.nn as nn import torch.nn.functional as F from torch.nn import Parameter +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests diff --git a/test/test_api/test_torch_nn/test_conv_layers.py b/test/test_api/test_torch_nn/test_conv_layers.py index 66b71eeb9637905b05ba1ae1b0080082f9c8e639..2640c552bee344b6a23f92e56733a486a2fe5ab8 100644 --- a/test/test_api/test_torch_nn/test_conv_layers.py +++ b/test/test_api/test_torch_nn/test_conv_layers.py @@ -13,8 +13,8 @@ # limitations under the License. import torch -import torch_npu import torch.nn as nn +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests diff --git a/test/test_api/test_torch_nn/test_loss_functions.py b/test/test_api/test_torch_nn/test_loss_functions.py index 3064d0e3e7fbad55d369e8cbfc9164a0cf18680a..42f6444a039476ca2786d434cdd009d752a529f9 100644 --- a/test/test_api/test_torch_nn/test_loss_functions.py +++ b/test/test_api/test_torch_nn/test_loss_functions.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. import torch -import torch_npu import torch.nn as nn import torch.nn.functional as F +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests @@ -55,27 +55,27 @@ class TestLossFunctions(TestCase): S_min = 10 # Minimum target length, for demonstration purposes # Initialize random batch of input vectors, for *size = (T,N,C) - input1 = torch.randn(T, N, C).log_softmax(2).detach() - ninput = input1.npu() + input1 = torch.randn(T, N, C).npu().log_softmax(2).detach() + cinput = input1.cpu() input1.requires_grad_(True) - ninput.requires_grad_(True) + cinput.requires_grad_(True) # Initialize random batch of targets (0 = blank, 1:C = classes) - target = torch.randint(low=1, high=C, size=(N, S), dtype=torch.long) + target = torch.randint(low=1, high=C, size=(N, S), dtype=torch.int32).npu() - input_lengths = torch.full(size=(N,), fill_value=T, dtype=torch.long) - target_lengths = torch.randint(low=S_min, high=S, size=(N,), dtype=torch.long) - ctc_loss = nn.CTCLoss() + input_lengths = torch.full(size=(N,), fill_value=T, dtype=torch.int32).npu() + target_lengths = torch.randint(low=S_min, high=S, size=(N,), dtype=torch.int32).npu() + ctc_loss = nn.CTCLoss().npu() loss = ctc_loss(input1, target, input_lengths, target_lengths) loss.backward(torch.ones_like(loss)) - ntarget = target.npu() - ninput_lengths = input_lengths.npu() - ntarget_lengths = target_lengths.npu() - nctc_loss = ctc_loss.npu() - nloss = nctc_loss(ninput, ntarget, ninput_lengths, ntarget_lengths) - nloss.backward(torch.ones_like(nloss)) - self.assertRtolEqual(loss.detach().numpy(), nloss.detach().cpu().numpy()) - self.assertRtolEqual(input1.grad.numpy(), ninput.grad.cpu().numpy()) + ctarget = target.cpu().long() + cinput_lengths = input_lengths.cpu().long() + ctarget_lengths = target_lengths.cpu().long() + cctc_loss = ctc_loss.cpu() + closs = cctc_loss(cinput, ctarget, cinput_lengths, ctarget_lengths) + closs.backward(torch.ones_like(closs)) + self.assertRtolEqual(loss.detach().cpu().numpy(), closs.detach().numpy()) + self.assertRtolEqual(input1.grad.cpu().numpy(), cinput.grad.numpy()) def test_NLLLoss(self): m = nn.LogSoftmax(dim=1) @@ -211,5 +211,4 @@ class TestLossFunctions(TestCase): if __name__ == "__main__": - torch.npu.set_device(0) run_tests() \ No newline at end of file diff --git a/test/test_api/test_torch_nn/test_nonlinear_activations.py b/test/test_api/test_torch_nn/test_nonlinear_activations.py index c5556115a2bdb806b88b6f93710021b3e9e33c3d..2b6be84d1082075264a1cef71ede56c4b1a26528 100644 --- a/test/test_api/test_torch_nn/test_nonlinear_activations.py +++ b/test/test_api/test_torch_nn/test_nonlinear_activations.py @@ -13,8 +13,8 @@ # limitations under the License. import torch -import torch_npu import torch.nn as nn +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests @@ -148,6 +148,5 @@ class TestOtherNonLinearActivations(TestCase): if __name__ == "__main__": - torch.npu.set_device(0) run_tests() diff --git a/test/test_api/test_torch_nn/test_normalization_layers.py b/test/test_api/test_torch_nn/test_normalization_layers.py index e2f1d73cb18bf5a3f3cce93ad049c2dee7852900..0b8651e2053a8165261ad7eef4ed5d9258722dea 100644 --- a/test/test_api/test_torch_nn/test_normalization_layers.py +++ b/test/test_api/test_torch_nn/test_normalization_layers.py @@ -13,8 +13,8 @@ # limitations under the License. import torch -import torch_npu import torch.nn as nn +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests @@ -89,6 +89,5 @@ class TestNormalizationLayers(TestCase): if __name__ == "__main__": - torch.npu.set_device(0) run_tests() diff --git a/test/test_api/test_torch_nn/test_padding_layers.py b/test/test_api/test_torch_nn/test_padding_layers.py index c4f1b61614a2457d352c51ecc86471b340b5693c..a3f0178cc717a0693e1a5544eb50263029673700 100644 --- a/test/test_api/test_torch_nn/test_padding_layers.py +++ b/test/test_api/test_torch_nn/test_padding_layers.py @@ -13,8 +13,8 @@ # limitations under the License. import torch -import torch_npu import torch.nn as nn +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests diff --git a/test/test_api/test_torch_nn/test_pooling_layers.py b/test/test_api/test_torch_nn/test_pooling_layers.py index 7226a6122c0119d063ffd55e9c773148e918cff6..0386150bc52850f94a7f4e6cb44aa4ce695ffd3c 100644 --- a/test/test_api/test_torch_nn/test_pooling_layers.py +++ b/test/test_api/test_torch_nn/test_pooling_layers.py @@ -13,8 +13,8 @@ # limitations under the License. import torch -import torch_npu import torch.nn as nn +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests @@ -117,6 +117,4 @@ class TestPoolingLayers(TestCase): if __name__ == "__main__": - torch.npu.set_device(0) - run_tests() - + run_tests() \ No newline at end of file diff --git a/test/test_api/test_torch_nn/test_recurrent_layers.py b/test/test_api/test_torch_nn/test_recurrent_layers.py index 54c6174dc8d66ed6eb0eb575534dbbcef7ec5b0d..be1ae2b1cb25b516923a0a3a0df68584ff05a46d 100644 --- a/test/test_api/test_torch_nn/test_recurrent_layers.py +++ b/test/test_api/test_torch_nn/test_recurrent_layers.py @@ -13,8 +13,8 @@ # limitations under the License. import torch -import torch_npu import torch.nn as nn +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests diff --git a/test/test_api/test_torch_nn/test_sparse_layers.py b/test/test_api/test_torch_nn/test_sparse_layers.py index bdae319bc185eaddaab4e3bc7b25dbf1b870b7c8..7aa98c261e9fc12f6e722c6b2a2cb01ce7578555 100644 --- a/test/test_api/test_torch_nn/test_sparse_layers.py +++ b/test/test_api/test_torch_nn/test_sparse_layers.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. import torch -import torch_npu import torch.nn as nn +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests @@ -27,5 +27,4 @@ class TestSparseLayers(TestCase): if __name__ == "__main__": - torch.npu.set_device(0) run_tests() \ No newline at end of file diff --git a/test/test_api/test_torch_nn/test_transformer_layers.py b/test/test_api/test_torch_nn/test_transformer_layers.py index cb6a67943a5bc3ecebeb4982a680bc8fccc8692a..7c4a20dc18a6346a8598c6168f23b5e42edbc407 100644 --- a/test/test_api/test_torch_nn/test_transformer_layers.py +++ b/test/test_api/test_torch_nn/test_transformer_layers.py @@ -13,8 +13,8 @@ # limitations under the License. import torch -import torch_npu import torch.nn as nn +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests diff --git a/test/test_api/test_torch_nn/test_utilities.py b/test/test_api/test_torch_nn/test_utilities.py index d634ee32dcda681dda0e6f9350d340936fea1f94..b6eaae8691e508c8764c5e784613f7b9d1a42698 100644 --- a/test/test_api/test_torch_nn/test_utilities.py +++ b/test/test_api/test_torch_nn/test_utilities.py @@ -15,9 +15,9 @@ from collections import OrderedDict import torch -import torch_npu import torch.nn as nn from torch.nn.utils import prune +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests diff --git a/test/test_api/test_torch_nn/test_vision_layers.py b/test/test_api/test_torch_nn/test_vision_layers.py index ddda4e4873be689ecf16877c289a3c2f089d00c9..adece30c9ff3ff16056479ca680ff6acd473e598 100644 --- a/test/test_api/test_torch_nn/test_vision_layers.py +++ b/test/test_api/test_torch_nn/test_vision_layers.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. import torch -import torch_npu import torch.nn as nn +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests diff --git a/test/test_api/test_torch_npu.py b/test/test_api/test_torch_npu.py index 0f0d1c95257deee834ed99ed24d15ca5b2ec6781..8c0559011d25686b1fad6a133caa018529b25db0 100644 --- a/test/test_api/test_torch_npu.py +++ b/test/test_api/test_torch_npu.py @@ -1,3 +1,17 @@ +# 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 contextlib import collections diff --git a/test/test_optimized_lib/test_activations.py b/test/test_optimized_lib/test_activations.py index 95057ca59ead861ce69ef89e551d66c9aaefe16a..ce8cb72e192ed5261874dc7136f4397c422de312 100644 --- a/test/test_optimized_lib/test_activations.py +++ b/test/test_optimized_lib/test_activations.py @@ -13,13 +13,13 @@ # See the License for the specific language governing permissions and # limitations under the License. +import numpy as np import torch -import torch_npu import torch.nn.functional as F -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 - from torch_npu.contrib.module import Mish, SiLU class TestActivations(TestCase): diff --git a/test/test_optimized_lib/test_index_op.py b/test/test_optimized_lib/test_index_op.py index 5716e4847c7a0d4ad11960f23aba81d5ada0b4f4..dbb11bcb2b03cdb142a6343d9e272687dee0b13f 100644 --- a/test/test_optimized_lib/test_index_op.py +++ b/test/test_optimized_lib/test_index_op.py @@ -13,9 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +import numpy as np import torch import torch_npu -import numpy as np from torch_npu.testing.testcase import TestCase, run_tests from torch_npu.testing.common_utils import create_common_tensor @@ -35,7 +35,7 @@ class TestIndexOp(TestCase): def test_npu_index_op(self): dtype_list = [np.float16, np.float32] - format_list = [-1] + format_list = [-1, 0, 2] shape_list = [ [2, 3, 7, 7], [1, 2, 3, 6, 6], diff --git a/torch_npu/contrib/function/anchor_generator.py b/torch_npu/contrib/function/anchor_generator.py index 9cb4cf85a6e4eb28bd8759e51c8e179327ac2848..2c1d8b180b05bd231d9eb5344c8923cd03bfaed8 100644 --- a/torch_npu/contrib/function/anchor_generator.py +++ b/torch_npu/contrib/function/anchor_generator.py @@ -18,6 +18,7 @@ import torch_npu def box_dtype_check(box): if box not in [torch.float, torch.half]: return box.float() + return box def npu_single_level_responsible_flags(featmap_size, gt_bboxes, diff --git a/torch_npu/contrib/function/bbox_coder.py b/torch_npu/contrib/function/bbox_coder.py index 6c8929595d843190ac9581dc4806d71cef3311fe..0c39da0a8507c0938b8239753b3e3b4cac601707 100644 --- a/torch_npu/contrib/function/bbox_coder.py +++ b/torch_npu/contrib/function/bbox_coder.py @@ -18,11 +18,13 @@ import torch_npu def box_dtype_check(box): if box not in [torch.float, torch.half]: return box.float() + return box def stride_dtype_check(stride): if stride not in [torch.int]: return stride.int() + return stride def npu_bbox_coder_encode_yolo(bboxes, gt_bboxes, stride): diff --git a/torch_npu/contrib/function/iou.py b/torch_npu/contrib/function/iou.py index 48ee1b650570a56858ad61134f5afa3b3f78e2af..d97fa3b6579b96194876c7537be0835cbc014858 100644 --- a/torch_npu/contrib/function/iou.py +++ b/torch_npu/contrib/function/iou.py @@ -18,6 +18,7 @@ import torch_npu def box_dtype_check(box): if box not in [torch.float, torch.half]: return box.float() + return box def npu_iou(boxes1, diff --git a/torch_npu/contrib/module/activations.py b/torch_npu/contrib/module/activations.py index 2776d2a4769bce0ed979ef902cc544e111277b60..8f27383d8cba6d21918274b9749b7359a19cc0bc 100644 --- a/torch_npu/contrib/module/activations.py +++ b/torch_npu/contrib/module/activations.py @@ -13,8 +13,8 @@ # limitations under the License. import torch -import torch_npu import torch.nn as nn +import torch_npu class Mish(nn.Module): def __init__(self): diff --git a/torch_npu/contrib/module/channel_shuffle.py b/torch_npu/contrib/module/channel_shuffle.py index 07bc37b1d6b83d77ecc8134ca536c30f2f5f196c..0a9c859e8ef46121977048d8daa95f1d1829a89c 100644 --- a/torch_npu/contrib/module/channel_shuffle.py +++ b/torch_npu/contrib/module/channel_shuffle.py @@ -14,8 +14,8 @@ import numpy as np import torch -import torch_npu import torch.nn as nn +import torch_npu class ChannelShuffle(nn.Module): diff --git a/torch_npu/contrib/module/crossentropy.py b/torch_npu/contrib/module/crossentropy.py index 5cf2932b3808fc25b6ac5b34b8836bee038d9d66..f7593d625468a3277bef5645827d249b4b4d3b00 100644 --- a/torch_npu/contrib/module/crossentropy.py +++ b/torch_npu/contrib/module/crossentropy.py @@ -13,8 +13,8 @@ # limitations under the License. import torch -import torch_npu import torch.nn as nn +import torch_npu class LabelSmoothingCrossEntropy(nn.Module): diff --git a/torch_npu/contrib/module/deform_conv.py b/torch_npu/contrib/module/deform_conv.py index 47fde7719d9f7d3d40ad306c630fca56da7c9404..c273bf33058f66b15618be7c1ae2542a0e197c28 100644 --- a/torch_npu/contrib/module/deform_conv.py +++ b/torch_npu/contrib/module/deform_conv.py @@ -14,10 +14,10 @@ import math import torch -import torch_npu import torch.nn as nn from torch.autograd import Function from torch.nn.modules.utils import _pair, _single +import torch_npu class ModulatedDeformConv2dFunction(Function): @@ -86,7 +86,61 @@ class ModulatedDeformConv2dFunction(Function): class ModulatedDeformConv(nn.Module): - + r"""Applies an NPU based Modulated Deformable 2D convolution operation. + + Paper link: + [Deformable ConvNets v2: More Deformable, Better Results](https://arxiv.org/abs/1811.11168) + + Reference implementation link: + https://github.com/open-mmlab/mmcv/blob/master/mmcv/ops/modulated_deform_conv.py + + The implementation of this ModulatedDeformConv is mainly based + on the implementation of mmcv for design and reconstruction. + + In ModulatedDeformConvFunction, the forward and backward are customized, + and the input tensor is reconstructed ito match the NPU based function. + + It is worth mentioning that DeformConv(DCNv1) is also implemented + by setting modulated==False. Due to the difference between input + and initialization, there is no additional implementation here. + + .. note:: + ModulatedDeformConv only implements operations under fp32 data types. + Notice, weight and bias in conv_offset must be initialized to 0. + + Args: + in_channels (int): Number of channels in the input image. + out_channels (int): Number of channels produced by the convolution. + kernel_size(int, tuple): Size of the convolving kernel. + stride(int, tuple): Stride of the convolution. Default: 1. + padding (int or tuple): Zero-padding added to both sides of the input. + Default: 0. + dilation (int or tuple): Spacing between kernel elements. Default: 1. + groups (int): Number of blocked connections from input. + channels to output channels. Default: 1. + deform_groups (int): Number of deformable group partitions. + bias (bool): If True, adds a learnable bias to the output. Default: False. + pack (bool): If True, conv_offset and mask will be included in this module. Default: True. + + Examples:: + >>> m = ModulatedDeformConv(32, 32, 1) + >>> input_tensor = torch.randn(2, 32, 5, 5) + >>> output = m(input_tensor) + + + >>> x = torch.randn(2, 32, 7, 7) + >>> model = ModulatedDeformConv(32, 32, 3, 2, 1) + + >>> torch.npu.set_device(0) + >>> x = x.npu() + >>> model = model.npu() + + >>> o = model(x) + >>> l = o.sum() + >>> l.backward() + >>> print(l) + """ + def __init__(self, in_channels, out_channels, @@ -99,64 +153,7 @@ class ModulatedDeformConv(nn.Module): bias=True, pack=True, ): - - r"""Applies an NPU based Modulated Deformable 2D convolution operation. - - Paper link: - [Deformable ConvNets v2: More Deformable, Better Results](https://arxiv.org/abs/1811.11168) - - Reference implementation link: - https://github.com/open-mmlab/mmcv/blob/master/mmcv/ops/modulated_deform_conv.py - - The implementation of this ModulatedDeformConv is mainly based - on the implementation of mmcv for design and reconstruction. - - In ModulatedDeformConvFunction, the forward and backward are customized, - and the input tensor is reconstructed ito match the NPU based function. - - It is worth mentioning that DeformConv(DCNv1) is also implemented - by setting modulated==False. Due to the difference between input - and initialization, there is no additional implementation here. - - .. note:: - ModulatedDeformConv only implements operations under fp32 data types. - Notice, weight and bias in conv_offset must be initialized to 0. - - Args: - in_channels (int): Number of channels in the input image. - out_channels (int): Number of channels produced by the convolution. - kernel_size(int, tuple): Size of the convolving kernel. - stride(int, tuple): Stride of the convolution. Default: 1. - padding (int or tuple): Zero-padding added to both sides of the input. - Default: 0. - dilation (int or tuple): Spacing between kernel elements. Default: 1. - groups (int): Number of blocked connections from input. - channels to output channels. Default: 1. - deform_groups (int): Number of deformable group partitions. - bias (bool): If True, adds a learnable bias to the output. Default: False. - pack (bool): If True, conv_offset and mask will be included in this module. Default: True. - - Examples:: - >>> m = ModulatedDeformConv(32, 32, 1) - >>> input_tensor = torch.randn(2, 32, 5, 5) - >>> output = m(input_tensor) - - - >>> x = torch.randn(2, 32, 7, 7) - >>> model = ModulatedDeformConv(32, 32, 3, 2, 1) - - >>> torch.npu.set_device(0) - >>> x = x.npu() - >>> model = model.npu() - - >>> o = model(x) - >>> l = o.sum() - >>> l.backward() - >>> print(l) - """ - super(ModulatedDeformConv, self).__init__() - self.in_channels = in_channels self.out_channels = out_channels self.kernel_size = _pair(kernel_size) @@ -167,14 +164,12 @@ class ModulatedDeformConv(nn.Module): self.deformable_groups = deformable_groups self.with_bias = bias self.pack = pack - self.weight = nn.Parameter( torch.Tensor(out_channels, in_channels // groups, *self.kernel_size)) if bias: self.bias = nn.Parameter(torch.Tensor(out_channels)) else: self.bias = torch.zeros(self.weight.shape[0]) - if self.pack: self.conv_offset = nn.Conv2d( self.in_channels, @@ -184,7 +179,6 @@ class ModulatedDeformConv(nn.Module): stride=_pair(self.stride), padding=_pair(self.padding), bias=True) - self.split_num = self.deformable_groups * 2 * self.kernel_size[0] * self.kernel_size[1] sort_index_for_npu = list(range(self.split_num)) sort_index_for_npu_fp = sort_index_for_npu[1::2] + sort_index_for_npu[::2] @@ -193,7 +187,6 @@ class ModulatedDeformConv(nn.Module): self.sort_index_for_npu_fp = torch.IntTensor(sort_index_for_npu_fp) self.sort_index_for_npu_bp = torch.IntTensor(sort_index_for_npu_bp) self.sort_index_for_npu_todevice = False - self.init_param() def init_param(self): diff --git a/torch_npu/contrib/module/ps_roi_pooling.py b/torch_npu/contrib/module/ps_roi_pooling.py index 05b708eab7034776b0d6e24e2c291090ecdda36f..5296ab2ac0933f50d995d8868deacae0d89f5333 100644 --- a/torch_npu/contrib/module/ps_roi_pooling.py +++ b/torch_npu/contrib/module/ps_roi_pooling.py @@ -13,8 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. import torch -import torch_npu from torch import nn +import torch_npu class PSROIPool(nn.Module): def __init__(self, pooled_height=7, pooled_width=7, spatial_scale=1 / 16.0, group_size=7, output_dim=22): diff --git a/torch_npu/contrib/module/roi_align.py b/torch_npu/contrib/module/roi_align.py index 8aeb656d161af9cbc7cd8aa1617067f3320abe4d..0d5c15839f0e38900a3cb8a54cd707a722f1d9ab 100644 --- a/torch_npu/contrib/module/roi_align.py +++ b/torch_npu/contrib/module/roi_align.py @@ -14,12 +14,11 @@ # See the License for the specific language governing permissions and # limitations under the License. import torch -import torch_npu from torch import nn - from torch.nn.modules.utils import _pair from torch.autograd import Function from torch.autograd.function import once_differentiable +import torch_npu class _ROIAlign(Function): diff --git a/torch_npu/utils/serialization.py b/torch_npu/utils/serialization.py index c9a2c5ce77bad65bf73a90dd27c921ec55e9b45e..cbfcdde23a3f0e7e65a97331d8f782ace38f7fe7 100644 --- a/torch_npu/utils/serialization.py +++ b/torch_npu/utils/serialization.py @@ -17,10 +17,10 @@ import pickle import argparse import torch -import torch_npu import torch.nn as nn import torch.serialization as se from torch._six import container_abcs, string_classes +import torch_npu DEFAULT_PROTOCOL = 2