From 841c2937785a176fdb003dc099faec9cd539cf91 Mon Sep 17 00:00:00 2001 From: xiaxia3 Date: Wed, 9 Mar 2022 19:36:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A1=A5=E5=85=85torch=20UT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_api/test_torch/test_generators.py | 42 ++++ ...t_locally_disabling_gradient_omputation.py | 68 +++++ test/test_api/test_torch/test_parallelism.py | 42 ++++ .../{ => test_torch}/test_serialization.py | 0 test/test_api/test_torch/test_utilities.py | 51 ++++ .../test_api/test_torch_nn/test_containers.py | 236 ++++++++++++++++++ .../test_torch_nn/test_dataparallel_layers.py | 54 ++++ .../test_torch_nn/test_distance_functions.py | 41 +++ .../test_torch_nn/test_dropout_layers.py | 53 ++++ .../test_torch_nn/test_linear_layers.py | 47 ++++ .../test_normalization_layers.py | 94 +++++++ .../test_torch_nn/test_padding_layers.py | 63 +++++ .../test_torch_nn/test_pooling_layers.py | 122 +++++++++ .../test_torch_nn/test_sparse_layers.py | 32 +++ 14 files changed, 945 insertions(+) create mode 100644 test/test_api/test_torch/test_generators.py create mode 100644 test/test_api/test_torch/test_locally_disabling_gradient_omputation.py create mode 100644 test/test_api/test_torch/test_parallelism.py rename test/test_api/{ => test_torch}/test_serialization.py (100%) create mode 100644 test/test_api/test_torch/test_utilities.py create mode 100644 test/test_api/test_torch_nn/test_containers.py create mode 100644 test/test_api/test_torch_nn/test_dataparallel_layers.py create mode 100644 test/test_api/test_torch_nn/test_distance_functions.py create mode 100644 test/test_api/test_torch_nn/test_dropout_layers.py create mode 100644 test/test_api/test_torch_nn/test_linear_layers.py create mode 100644 test/test_api/test_torch_nn/test_normalization_layers.py create mode 100644 test/test_api/test_torch_nn/test_padding_layers.py create mode 100644 test/test_api/test_torch_nn/test_pooling_layers.py create mode 100644 test/test_api/test_torch_nn/test_sparse_layers.py diff --git a/test/test_api/test_torch/test_generators.py b/test/test_api/test_torch/test_generators.py new file mode 100644 index 00000000000..38fb1675e53 --- /dev/null +++ b/test/test_api/test_torch/test_generators.py @@ -0,0 +1,42 @@ +# 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 + +from torch_npu.testing.testcase import TestCase, run_tests + +device = 'npu:0' +torch.npu.set_device(device) + +def get_npu_type(type_name): + if isinstance(type_name, type): + type_name = '{}.{}'.format(type_name.__module__, type_name.__name__) + module, name = type_name.rsplit('.', 1) + assert module == 'torch' + return getattr(torch.npu, name) + +class TestGenerators(TestCase): + def test_generator(self): + g_npu = torch.Generator(device=device) + print(g_npu.device) + self.assertExpectedInline(str(g_npu.device), '''npu:0''') + + def test_default_generator(self): + output = torch.default_generator + print(output) + + +if __name__ == "__main__": + run_tests() \ No newline at end of file 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 new file mode 100644 index 00000000000..15899c3ca5b --- /dev/null +++ b/test/test_api/test_torch/test_locally_disabling_gradient_omputation.py @@ -0,0 +1,68 @@ +# 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 + +from torch_npu.testing.testcase import TestCase, run_tests +from torch.testing._internal.common_utils import freeze_rng_state + +device = 'npu:0' +torch.npu.set_device(device) + +class TestLDGComputation(TestCase): + def test_no_grad(self): + x = torch.tensor([1], dtype=torch.float32, device=device, requires_grad=True) + with torch.no_grad(): + y = x * 2 + self.assertFalse(y.requires_grad) + + @torch.no_grad() + def doubler(x): + return x * 2 + z = doubler(x) + self.assertFalse(z.requires_grad) + + def test_enable_grad(self): + x = torch.tensor([1], dtype=torch.float32, device=device, requires_grad=True) + with torch.no_grad(): + with torch.enable_grad(): + y = x * 2 + self.assertTrue(y.requires_grad) + + @torch.enable_grad() + def doubler(x): + return x * 2 + with torch.no_grad(): + z = doubler(x) + self.assertTrue(z.requires_grad) + + def test_set_grad_enabled(self): + x = torch.tensor([1.], device=device, requires_grad=True) + with torch.set_grad_enabled(False): + y = x * 2 + self.assertFalse(y.requires_grad) + with torch.set_grad_enabled(True): + y = x * 2 + self.assertTrue(y.requires_grad) + with torch.set_grad_enabled(False): + torch.set_grad_enabled(True) + y = x * 2 + self.assertTrue(y.requires_grad) + + +if __name__ == "__main__": + run_tests() + + diff --git a/test/test_api/test_torch/test_parallelism.py b/test/test_api/test_torch/test_parallelism.py new file mode 100644 index 00000000000..eea1b5eb130 --- /dev/null +++ b/test/test_api/test_torch/test_parallelism.py @@ -0,0 +1,42 @@ +# 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 + +from torch_npu.testing.testcase import TestCase, run_tests +from torch.testing._internal.common_utils import freeze_rng_state + +device = 'npu:0' +torch.npu.set_device(device) + +class TestParallelism(TestCase): + def test_set_num_threads(self): + torch.set_num_threads(2) + + def test_get_num_threads(self): + output = torch.get_num_threads() + print(output) + + def test_set_num_interop_threads(self): + torch.set_num_interop_threads(2) + + def test_get_num_interop_threads(self): + output = torch.get_num_interop_threads() + print(output) + +if __name__ == "__main__": + run_tests() + + diff --git a/test/test_api/test_serialization.py b/test/test_api/test_torch/test_serialization.py similarity index 100% rename from test/test_api/test_serialization.py rename to test/test_api/test_torch/test_serialization.py diff --git a/test/test_api/test_torch/test_utilities.py b/test/test_api/test_torch/test_utilities.py new file mode 100644 index 00000000000..fddd2df87ce --- /dev/null +++ b/test/test_api/test_torch/test_utilities.py @@ -0,0 +1,51 @@ +# 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 + +from torch_npu.testing.testcase import TestCase, run_tests + +device = 'npu:0' +torch.npu.set_device(device) + +class TestUtilities(TestCase): + def test_compiled_with_cxx11_abi(self): + output = torch.compiled_with_cxx11_abi() + self.assertTrue(output) + + def test_result_type(self): + self.assertEqual(torch.result_type(torch.tensor(1, dtype=torch.int, device=device), 1), torch.int) + self.assertEqual(torch.result_type(1, torch.tensor(1, dtype=torch.int, device=device)), torch.int) + self.assertEqual(torch.result_type(1, 1.), torch.get_default_dtype()) + self.assertEqual(torch.result_type(torch.tensor(1, device=device), 1.), torch.get_default_dtype()) + self.assertEqual(torch.result_type(torch.tensor(1, dtype=torch.long, device=device), + torch.tensor([1, 1], dtype=torch.int, device=device)), + torch.int) + self.assertEqual(torch.result_type(torch.tensor([1., 1.], dtype=torch.float, device=device), 1.), torch.float) + self.assertEqual(torch.result_type(torch.tensor(1., dtype=torch.float, device=device), + torch.tensor(1, dtype=torch.double, device=device)), + torch.double) + + def test_can_cast(self): + self.assertTrue(torch.can_cast(torch.double, torch.float)) + self.assertFalse(torch.can_cast(torch.float, torch.int)) + + def test_promote_types(self): + self.assertEqual(torch.promote_types(torch.float, torch.int), torch.float) + self.assertEqual(torch.promote_types(torch.float, torch.double), torch.double) + self.assertEqual(torch.promote_types(torch.int, torch.uint8), torch.int) + +if __name__ == "__main__": + run_tests() diff --git a/test/test_api/test_torch_nn/test_containers.py b/test/test_api/test_torch_nn/test_containers.py new file mode 100644 index 00000000000..c2764de3b5b --- /dev/null +++ b/test/test_api/test_torch_nn/test_containers.py @@ -0,0 +1,236 @@ +# 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. + +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 + +from torch_npu.testing.testcase import TestCase, run_tests + +device = 'npu:0' +torch.npu.set_device(device) + +class TestNN(TestCase): + def test_parameter(self): + cpu_output = torch.nn.Parameter(torch.tensor([[1, 2, 3, 4, 5.0]])) + npu_output = torch.nn.Parameter(torch.tensor([[1, 2, 3, 4, 5.0]], device=device)) + self.assertEqual(cpu_output, npu_output) + + +class TestContainers(TestCase): + def test_module(self): + + class Model(nn.Module): + def __init__(self): + super(Model, self).__init__() + self.conv1 = nn.Conv2d(1, 2, 5) + + def forward(self, x): + x = F.relu(self.conv1(x)) + return x + + model = Model().npu() + x = torch.randn(1, 1, 32, 32).npu() + output = model(x) + assert output is not None + + def test_sequential(self): + torch.nn.Sequential(torch.nn.Linear(8, 8), torch.nn.Linear(8, 8)).to(device=device) + + def test_moduleList(self): + + class MyModule(nn.Module): + def __init__(self): + super(MyModule, self).__init__() + self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(10)]) + + def forward(self, x): + # ModuleList can act as an iterable, or be indexed using ints + for i, l in enumerate(self.linears): + x = self.linears[i // 2](x) + l(x) + return x + + model = MyModule().npu() + x = torch.randn(1, 1, 10, 10).npu() + output = model(x) + assert output is not None + + def test_ModuleDict(self): + class ModuleDict(nn.Module): + def __init__(self): + super(ModuleDict, self).__init__() + self.choices = nn.ModuleDict({ + 'conv': nn.Conv2d(10, 10, 3), + 'pool': nn.MaxPool2d(3) + }) + + self.activations = nn.ModuleDict({ + 'relu': nn.ReLU(), + 'prelu': nn.PReLU() + }) + + def forward(self, x, choice, act): + x = self.choices[choice](x) + x = self.activations[act](x) + return x + + net = ModuleDict().npu() + + fake_img = torch.randn((4, 10, 32, 32)).npu() + + output = net(fake_img, 'conv', 'relu') + + def make_param(self): + return Parameter(torch.randn(10, 10, device=device)) + + def check_list(self, parameters, param_list): + self.assertEqual(len(parameters), len(param_list)) + for p1, p2 in zip(parameters, param_list): + self.assertIs(p1, p2) + for p1, p2 in zip(parameters, param_list.parameters()): + self.assertIs(p1, p2) + for i, _ in enumerate(parameters): + self.assertIs(parameters[i], param_list[i]) + + def model(self): + l1 = nn.Linear(1, 2) + l2 = nn.Linear(2, 3) + l3 = nn.Linear(3, 2) + l4 = nn.Linear(2, 3) + subnet = nn.Sequential(l3, l4) + s = nn.Sequential( + OrderedDict([ + ("layer1", l1), + ("layer2", l2), + ("layer3", l3), + ("layer4", l4), + ("subnet_layer", subnet) + ]) + ) + return s + + def test_ParameterList(self): + parameters = [self.make_param(), self.make_param()] + param_list = nn.ParameterList(parameters) + self.check_list(parameters, param_list) + parameters += [self.make_param()] + param_list += [parameters[-1]] + self.check_list(parameters, param_list) + parameters.append(self.make_param()) + param_list.append(parameters[-1]) + self.check_list(parameters, param_list) + next_params = [self.make_param(), self.make_param()] + parameters.extend(next_params) + param_list.extend(next_params) + self.check_list(parameters, param_list) + parameters[2] = self.make_param() + param_list[2] = parameters[2] + self.check_list(parameters, param_list) + parameters[-1] = self.make_param() + param_list[-1] = parameters[-1] + self.check_list(parameters, param_list) + idx = torch.tensor(2, dtype=torch.int32) + parameters[2] = self.make_param() + param_list[idx] = parameters[2] + self.assertIs(param_list[idx], parameters[2]) + self.check_list(parameters, param_list) + self.assertEqual(param_list[1:], nn.ParameterList(parameters[1:])) + self.assertEqual(param_list[3:], nn.ParameterList(parameters[3:])) + self.assertEqual(param_list[:-1], nn.ParameterList(parameters[:-1])) + self.assertEqual(param_list[:-3], nn.ParameterList(parameters[:-3])) + self.assertEqual(param_list[::-1], nn.ParameterList(parameters[::-1])) + with self.assertRaises(TypeError): + param_list += self.make_param() + with self.assertRaises(TypeError): + param_list.extend(self.make_param()) + s = self.model() + parameters = list(s.parameters()) + param_list = nn.ParameterList() + param_list.extend(s.parameters()) + self.check_list(parameters, param_list) + + def check_dict(self, parameter_dict, parameters): + self.assertEqual(len(parameter_dict), len(parameters)) + for k1, m2 in zip(parameters, parameter_dict.parameters()): + self.assertIs(parameters[k1], m2) + for k1, k2 in zip(parameters, parameter_dict): + self.assertIs(parameters[k1], parameter_dict[k2]) + for k in parameter_dict: + self.assertIs(parameter_dict[k], parameters[k]) + for k in parameter_dict.keys(): + self.assertIs(parameter_dict[k], parameters[k]) + for k, v in parameter_dict.items(): + self.assertIs(v, parameters[k]) + for k1, m2 in zip(parameters, parameter_dict.values()): + self.assertIs(parameters[k1], m2) + for k in parameters.keys(): + self.assertTrue(k in parameter_dict) + + def test_ParameterDict(self): + parameters = OrderedDict([ + ('p1', Parameter(torch.randn(10, 10, device=device))), + ('p2', Parameter(torch.randn(10, 10, device=device))), + ('p3', Parameter(torch.randn(10, 10, device=device))), + ]) + parameter_dict = nn.ParameterDict(parameters) + self.check_dict(parameter_dict, parameters) + parameters['p4'] = Parameter(torch.randn(10, 10)) + parameter_dict['p4'] = parameters['p4'] + self.check_dict(parameter_dict, parameters) + next_parameters = [ + ('p5', Parameter(torch.randn(10, 10, device=device))), + ('p2', Parameter(torch.randn(10, 10, device=device))), + ] + parameters.update(next_parameters) + parameter_dict.update(next_parameters) + self.check_dict(parameter_dict, parameters) + next_parameters = OrderedDict([ + ('p6', Parameter(torch.randn(10, 10, device=device))), + ('p5', Parameter(torch.randn(10, 10, device=device))), + ]) + parameters.update(next_parameters) + parameter_dict.update(next_parameters) + self.check_dict(parameter_dict, parameters) + next_parameters = { + 'p8': Parameter(torch.randn(10, 10, device=device)), + 'p7': Parameter(torch.randn(10, 10, device=device)) + } + parameters.update(sorted(next_parameters.items())) + parameter_dict.update(next_parameters) + self.check_dict(parameter_dict, parameters) + del parameter_dict['p3'] + del parameters['p3'] + self.check_dict(parameter_dict, parameters) + with self.assertRaises(TypeError): + parameter_dict.update(1) + with self.assertRaises(TypeError): + parameter_dict.update([1]) + with self.assertRaises(ValueError): + parameter_dict.update(Parameter(torch.randn(10, 10, device=device))) + with self.assertRaises(TypeError): + parameter_dict[1] = Parameter(torch.randn(10, 10, device=device)) + p_pop = parameter_dict.pop('p4') + self.assertIs(p_pop, parameters['p4']) + parameters.pop('p4') + self.check_dict(parameter_dict, parameters) + parameter_dict.clear() + self.assertEqual(len(parameter_dict), 0) + parameters.clear() + +if __name__ == "__main__": + run_tests() + diff --git a/test/test_api/test_torch_nn/test_dataparallel_layers.py b/test/test_api/test_torch_nn/test_dataparallel_layers.py new file mode 100644 index 00000000000..49ba0db04d4 --- /dev/null +++ b/test/test_api/test_torch_nn/test_dataparallel_layers.py @@ -0,0 +1,54 @@ +# 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.nn.functional as F +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests + + +class TestDataParallelLayers(TestCase): + def test_parallel_DistributedDataParallel(self): + class Net(torch.nn.Module): + def __init__(self): + super(Net, self).__init__() + self.conv1 = torch.nn.Conv2d(3, 32, 3, 1, 1) + self.dense1 = torch.nn.Linear(32 * 3 * 3, 128) + self.dense2 = torch.nn.Linear(128, 10) + + def forward(self, x): + x = F.max_pool2d(F.relu(self.conv(x)), 2) + x = x.view(x.size(0), -1) + x = F.relu(self.dense1(x)) + x = self.dense2(x) + return x + + model = Net() + import os + os.environ["MASTER_ADDR"] = "127.0.0.1" + os.environ["MASTER_PORT"] = "29688" + + LOCAL_RANK = int(os.getenv('LOCAL_RANK', 0)) # https://pytorch.org/docs/stable/elastic/run.html + RANK = int(os.getenv('RANK', 0)) + WORLD_SIZE = int(os.getenv('WORLD_SIZE', 1)) + torch.distributed.init_process_group(backend="hccl", rank=RANK, world_size=WORLD_SIZE) + model = model.npu() + net = torch.nn.parallel.DistributedDataParallel(model, device_ids = [0], broadcast_buffers=False) + + assert net is not None + + +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_distance_functions.py b/test/test_api/test_torch_nn/test_distance_functions.py new file mode 100644 index 00000000000..9484bc31e4b --- /dev/null +++ b/test/test_api/test_torch_nn/test_distance_functions.py @@ -0,0 +1,41 @@ +# 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.nn as nn +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests + + +class TestDistanceFunctions(TestCase): + def test_CosineSimilarity(self): + input1 = torch.randn(100, 128).npu() + input2 = torch.randn(100, 128).npu() + cos = nn.CosineSimilarity(dim=1, eps=1e-6).npu() + output = cos(input1, input2) + print(output.shape) + assert output is not None + + def test_PairwiseDistance(self): + pdist = nn.PairwiseDistance(p=2).npu() + input1 = torch.randn(100, 128).npu() + input2 = torch.randn(100, 128).npu() + output = pdist(input1, input2) + print(output.shape) + assert output is not None + + +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_dropout_layers.py b/test/test_api/test_torch_nn/test_dropout_layers.py new file mode 100644 index 00000000000..f0938cbf683 --- /dev/null +++ b/test/test_api/test_torch_nn/test_dropout_layers.py @@ -0,0 +1,53 @@ +# 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.nn as nn +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests + + +class TestDropoutLayers(TestCase): + def test_Dropout(self): + m = nn.Dropout(p=0.2).npu() + input1 = torch.randn(20, 16).npu() + output = m(input1) + print(output.shape) + assert output is not None + + def test_Dropout2d(self): + m = nn.Dropout2d(p=0.2).npu() + input1 = torch.randn(20, 16, 32, 32).npu() + output = m(input1) + print(output.shape) + assert output is not None + + def test_Dropout3d(self): + m = nn.Dropout3d(p=0.2).npu() + input1 = torch.randn(20, 16, 4, 32, 32).npu() + output = m(input1) + print(output.shape) + assert output is not None + + def test_AlphaDropout(self): + m = nn.AlphaDropout(p=0.2).npu() + input1 = torch.randn(20, 16).npu() + output = m(input1) + print(output.shape) + assert output is not None + + +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_linear_layers.py b/test/test_api/test_torch_nn/test_linear_layers.py new file mode 100644 index 00000000000..1e5b650e51f --- /dev/null +++ b/test/test_api/test_torch_nn/test_linear_layers.py @@ -0,0 +1,47 @@ +# 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.nn as nn +import torch_npu + +from torch_npu.testing.testcase import TestCase, run_tests + + +class TestLinearLayers(TestCase): + def test_Identity(self): + m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False).npu() + input1 = torch.randn(128, 20).npu() + output = m(input1) + print(output.shape) + assert output is not None + + def test_Linear(self): + m = nn.Linear(20, 30).npu() + input1 = torch.randn(128, 20).npu() + output = m(input1) + print(output.shape) + assert output is not None + + def test_Bilinear(self): + m = nn.Bilinear(20, 30, 40).npu() + input1 = torch.randn(128, 20).npu() + input2 = torch.randn(128, 30).npu() + output = m(input1, input2) + print(output.shape) + assert output is not None + + +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_normalization_layers.py b/test/test_api/test_torch_nn/test_normalization_layers.py new file mode 100644 index 00000000000..a13c2ffca58 --- /dev/null +++ b/test/test_api/test_torch_nn/test_normalization_layers.py @@ -0,0 +1,94 @@ +# 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 torch.nn as nn + +from torch_npu.testing.testcase import TestCase, run_tests + + +class TestNormalizationLayers(TestCase): + def test_BatchNorm1d(self): + m = nn.BatchNorm1d(100).npu() + input1 = torch.randn(20, 100).npu() + output = m(input1) + assert output is not None + + def test_BatchNorm2d(self): + m = nn.BatchNorm2d(100, affine=False).npu() + input1 = torch.randn(20, 100, 35, 45).npu() + output = m(input1) + assert output is not None + + def test_BatchNorm3d(self): + m = nn.BatchNorm3d(100).npu() + input1 = torch.randn(20, 100, 35, 45, 10).npu() + output = m(input1) + assert output is not None + + def test_GroupNorm(self): + m = nn.GroupNorm(3, 6).npu() + input1 = torch.randn(20, 6, 10, 10).npu() + output = m(input1) + assert output is not None + + def test_convert_sync_batchnorm(self): + module = torch.nn.Sequential( + torch.nn.BatchNorm1d(100), + torch.nn.InstanceNorm1d(100) + ).npu() + sync_bn_module = torch.nn.SyncBatchNorm.convert_sync_batchnorm(module) + children = list(sync_bn_module.children()) + self.assertEqual(children[0].__class__, torch.nn.SyncBatchNorm) + self.assertEqual(children[1].__class__, torch.nn.InstanceNorm1d) + + def test_InstanceNorm1d(self): + m = nn.InstanceNorm1d(100).npu() + input1 = torch.randn(20, 100, 40).npu() + output = m(input1) + assert output is not None + + def test_InstanceNorm2d(self): + m = nn.InstanceNorm2d(100).npu() + input1 = torch.randn(20, 100, 35, 45).npu() + output = m(input1) + assert output is not None + + def test_InstanceNorm3d(self): + m = nn.InstanceNorm3d(100).npu() + input1 = torch.randn(20, 100, 35, 45, 10).npu() + output = m(input1) + assert output is not None + + def test_LayerNorm(self): + input1 = torch.randn(20, 5, 10, 10).npu() + m = nn.LayerNorm(input1.size()[1:]).npu() + output = m(input1) + assert output is not None + + def test_LocalResponseNorm(self): + lrn = nn.LocalResponseNorm(2).npu() + signal_2d = torch.randn(32, 5, 24, 24).npu() + signal_4d = torch.randn(16, 5, 7, 7, 7, 7).npu() + output_2d = lrn(signal_2d) + output_4d = lrn(signal_4d) + assert output_2d is not None + assert output_4d is not None + + +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 new file mode 100644 index 00000000000..e39b9a07695 --- /dev/null +++ b/test/test_api/test_torch_nn/test_padding_layers.py @@ -0,0 +1,63 @@ +# 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 torch.nn as nn + +from torch_npu.testing.testcase import TestCase, run_tests + + +class TestPaddingLayers(TestCase): + def test_ReflectionPad2d(self): + m = nn.ReflectionPad2d(2).npu() + input1 = torch.arange(9, dtype=torch.float).reshape(1, 1, 3, 3).npu() + output = m(input1) + assert output is not None + + def test_ReplicationPad2d(self): + m = nn.ReplicationPad2d(2).npu() + input1 = torch.arange(9, dtype=torch.float).reshape(1, 1, 3, 3).npu() + output = m(input1) + assert output is not None + + def test_ZeroPad2d(self): + m = nn.ZeroPad2d(2).npu() + input1 = torch.randn(1, 1, 3, 3).npu() + output = m(input1) + assert output is not None + + def test_ConstantPad1d(self): + m = nn.ConstantPad1d(2, 3.5).npu() + input1 = torch.randn(1, 2, 4).npu() + output = m(input1) + assert output is not None + + def test_ConstantPad2d(self): + m = nn.ConstantPad2d(2, 3.5).npu() + input1 = torch.randn(1, 2, 2).npu() + output = m(input1) + assert output is not None + + def test_ConstantPad3d(self): + m = nn.ConstantPad3d(3, 3.5).npu() + input1 = torch.randn(16, 3, 10, 20, 30).npu() + output = m(input1) + assert output is not None + + +if __name__ == "__main__": + torch.npu.set_device(0) + 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 new file mode 100644 index 00000000000..76152c3f7ad --- /dev/null +++ b/test/test_api/test_torch_nn/test_pooling_layers.py @@ -0,0 +1,122 @@ +# 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 torch.nn as nn + +from torch_npu.testing.testcase import TestCase, run_tests + +device = 'npu:0' + +class TestPoolingLayers(TestCase): + def test_MaxPool1d(self): + m = nn.MaxPool1d(3, stride=2) + input1 = torch.randn(20, 16, 50) + output = m.npu()(input1.npu()) + assert output is not None + + def test_MaxPool2d(self): + m = nn.MaxPool2d(3, stride=2) + input1 = torch.randn(20, 16, 50, 32) + output = m.npu()(input1.npu()) + assert output is not None + + def test_MaxPool3d(self): + m = nn.MaxPool3d(3, stride=2) + input1 = torch.randn(20, 16, 50,44, 31) + output = m.npu()(input1.npu()) + assert output is not None + + def test_MaxUnpool1d(self): + pool = nn.MaxPool1d(2, stride=2, return_indices=True) + unpool = nn.MaxUnpool1d(2, stride=2) + input1 = torch.tensor([[[1., 2, 3, 4, 5, 6, 7, 8]]]) + output, indices = pool(input1) + output = unpool.npu()(output.npu(), indices.npu()) + assert output is not None + + def test_MaxUnpool2d(self): + pool = nn.MaxPool2d(2, stride=2, return_indices=True) + unpool = nn.MaxUnpool2d(2, stride=2) + input1 = torch.tensor([[[[ 1., 2, 3, 4], + [ 5, 6, 7, 8], + [ 9, 10, 11, 12], + [13, 14, 15, 16]]]]) + output, indices = pool(input1) + output = unpool.npu()(output.npu(), indices.npu()) + assert output is not None + + def test_MaxUnpool3d(self): + pool = nn.MaxPool3d(3, stride=2, return_indices=True) + unpool = nn.MaxUnpool3d(3, stride=2) + output, indices = pool(torch.randn(20, 16, 51, 33, 15)) + output = unpool.npu()(output.npu(), indices.npu()) + assert output is not None + + def test_AvgPool1d(self): + m = nn.AvgPool1d(3, stride=2).npu() + output = m(torch.tensor([[[1.,2,3,4,5,6,7]]], device=device)) + assert output is not None + + def test_AvgPool2d(self): + m = nn.AvgPool2d(3, stride=2).npu() + output = m(torch.randn(20, 16, 50, 32, device=device)) + assert output is not None + + def test_AvgPool3d(self): + m = nn.AvgPool3d(3, stride=2).npu() + output = m(input=torch.randn(20, 16, 50,44, 31, device=device)) + assert output is not None + + def test_LPPool1d(self): + m = nn.LPPool1d(2, 3, stride=2).npu() + output = m(input=torch.randn(20, 16, 50, device=device)) + assert output is not None + + def test_LPPool2d(self): + m = nn.LPPool2d(2, 3, stride=2).npu() + output = m(input=torch.randn(20, 16, 50, 32, device=device)) + assert output is not None + + def test_AdaptiveMaxPool1d(self): + m = nn.AdaptiveMaxPool1d(4).npu() + output = m(input=torch.randn(32, 16, 16, device=device)) + assert output is not None + + def test_AdaptiveMaxPool2d(self): + m = nn.AdaptiveMaxPool2d((2, 3)).npu() + output = m(input=torch.randn(1, 3, 8, 9, device=device)) + assert output is not None + + def test_AdaptiveAvgPool1d(self): + m = nn.AdaptiveAvgPool1d(5).npu() + output = m(input=torch.randn(1, 64, 8, device=device)) + assert output is not None + + def test_AdaptiveAvgPool2d(self): + m = nn.AdaptiveAvgPool2d((5,7)).npu() + output = m(input=torch.randn(1, 64, 8, 9, device=device)) + assert output is not None + + def test_AdaptiveAvgPool3d(self): + m = nn.AdaptiveAvgPool3d((1,1,1)).npu() + output = m(input=torch.randn(1, 64, 8, 9, 10, device=device)) + assert output is not None + + +if __name__ == "__main__": + torch.npu.set_device(0) + 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 new file mode 100644 index 00000000000..b1ef25f9084 --- /dev/null +++ b/test/test_api/test_torch_nn/test_sparse_layers.py @@ -0,0 +1,32 @@ +# 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 torch.nn as nn + +from torch_npu.testing.testcase import TestCase, run_tests + + +class TestSparseLayers(TestCase): + def test_Embedding(self): + embedding = nn.Embedding(10, 3).npu() + input1 = torch.LongTensor([[1,2,4,5],[4,3,2,9]]).npu() + output = embedding(input1) + print(output.shape) + assert output is not None + + +if __name__ == "__main__": + torch.npu.set_device(0) + run_tests() \ No newline at end of file -- Gitee From 157a19fec816efeef5a0a7e3afb712e13e56e1b8 Mon Sep 17 00:00:00 2001 From: xiaxia3 Date: Thu, 10 Mar 2022 17:49:00 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=94=B9assert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test_api/test_torch_nn/test_containers.py | 4 +-- .../test_torch_nn/test_dataparallel_layers.py | 2 +- .../test_torch_nn/test_distance_functions.py | 6 ++-- .../test_torch_nn/test_dropout_layers.py | 15 +++------ .../test_torch_nn/test_linear_layers.py | 9 ++---- .../test_normalization_layers.py | 20 ++++++------ .../test_torch_nn/test_padding_layers.py | 12 +++---- .../test_torch_nn/test_pooling_layers.py | 32 +++++++++---------- .../test_torch_nn/test_sparse_layers.py | 3 +- 9 files changed, 46 insertions(+), 57 deletions(-) diff --git a/test/test_api/test_torch_nn/test_containers.py b/test/test_api/test_torch_nn/test_containers.py index c2764de3b5b..888a3d6b5af 100644 --- a/test/test_api/test_torch_nn/test_containers.py +++ b/test/test_api/test_torch_nn/test_containers.py @@ -46,7 +46,7 @@ class TestContainers(TestCase): model = Model().npu() x = torch.randn(1, 1, 32, 32).npu() output = model(x) - assert output is not None + self.assertEqual(output is not None, True) def test_sequential(self): torch.nn.Sequential(torch.nn.Linear(8, 8), torch.nn.Linear(8, 8)).to(device=device) @@ -67,7 +67,7 @@ class TestContainers(TestCase): model = MyModule().npu() x = torch.randn(1, 1, 10, 10).npu() output = model(x) - assert output is not None + self.assertEqual(output is not None, True) def test_ModuleDict(self): class ModuleDict(nn.Module): diff --git a/test/test_api/test_torch_nn/test_dataparallel_layers.py b/test/test_api/test_torch_nn/test_dataparallel_layers.py index 49ba0db04d4..89ba688b322 100644 --- a/test/test_api/test_torch_nn/test_dataparallel_layers.py +++ b/test/test_api/test_torch_nn/test_dataparallel_layers.py @@ -46,7 +46,7 @@ class TestDataParallelLayers(TestCase): model = model.npu() net = torch.nn.parallel.DistributedDataParallel(model, device_ids = [0], broadcast_buffers=False) - assert net is not None + aself.assertEqual(net is not None, True) if __name__ == "__main__": diff --git a/test/test_api/test_torch_nn/test_distance_functions.py b/test/test_api/test_torch_nn/test_distance_functions.py index 9484bc31e4b..042cb161822 100644 --- a/test/test_api/test_torch_nn/test_distance_functions.py +++ b/test/test_api/test_torch_nn/test_distance_functions.py @@ -24,16 +24,14 @@ class TestDistanceFunctions(TestCase): input2 = torch.randn(100, 128).npu() cos = nn.CosineSimilarity(dim=1, eps=1e-6).npu() output = cos(input1, input2) - print(output.shape) - assert output is not None + self.assertEqual(output is not None, True) def test_PairwiseDistance(self): pdist = nn.PairwiseDistance(p=2).npu() input1 = torch.randn(100, 128).npu() input2 = torch.randn(100, 128).npu() output = pdist(input1, input2) - print(output.shape) - assert output is not None + self.assertEqual(output is not None, True) if __name__ == "__main__": diff --git a/test/test_api/test_torch_nn/test_dropout_layers.py b/test/test_api/test_torch_nn/test_dropout_layers.py index f0938cbf683..29330a1022f 100644 --- a/test/test_api/test_torch_nn/test_dropout_layers.py +++ b/test/test_api/test_torch_nn/test_dropout_layers.py @@ -15,7 +15,7 @@ import torch import torch.nn as nn import torch_npu -from torch_npu.testing.testcase import TestCase, run_tests +from torch_npu.testing.testcase import c, run_tests class TestDropoutLayers(TestCase): @@ -23,30 +23,25 @@ class TestDropoutLayers(TestCase): m = nn.Dropout(p=0.2).npu() input1 = torch.randn(20, 16).npu() output = m(input1) - print(output.shape) - assert output is not None + self.assertEqual(output is not None, True) def test_Dropout2d(self): m = nn.Dropout2d(p=0.2).npu() input1 = torch.randn(20, 16, 32, 32).npu() output = m(input1) - print(output.shape) - assert output is not None + self.assertEqual(output is not None, True) def test_Dropout3d(self): m = nn.Dropout3d(p=0.2).npu() input1 = torch.randn(20, 16, 4, 32, 32).npu() output = m(input1) - print(output.shape) - assert output is not None + self.assertEqual(output is not None, True) def test_AlphaDropout(self): m = nn.AlphaDropout(p=0.2).npu() input1 = torch.randn(20, 16).npu() output = m(input1) - print(output.shape) - assert output is not None - + self.assertEqual(output is not None, True) if __name__ == "__main__": torch.npu.set_device(0) diff --git a/test/test_api/test_torch_nn/test_linear_layers.py b/test/test_api/test_torch_nn/test_linear_layers.py index 1e5b650e51f..f6eea9dee15 100644 --- a/test/test_api/test_torch_nn/test_linear_layers.py +++ b/test/test_api/test_torch_nn/test_linear_layers.py @@ -23,23 +23,20 @@ class TestLinearLayers(TestCase): m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False).npu() input1 = torch.randn(128, 20).npu() output = m(input1) - print(output.shape) - assert output is not None + self.assertEqual(output is not None, True) def test_Linear(self): m = nn.Linear(20, 30).npu() input1 = torch.randn(128, 20).npu() output = m(input1) - print(output.shape) - assert output is not None + self.assertEqual(output is not None, True) def test_Bilinear(self): m = nn.Bilinear(20, 30, 40).npu() input1 = torch.randn(128, 20).npu() input2 = torch.randn(128, 30).npu() output = m(input1, input2) - print(output.shape) - assert output is not None + self.assertEqual(output is not None, True) if __name__ == "__main__": 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 a13c2ffca58..e2f1d73cb18 100644 --- a/test/test_api/test_torch_nn/test_normalization_layers.py +++ b/test/test_api/test_torch_nn/test_normalization_layers.py @@ -24,25 +24,25 @@ class TestNormalizationLayers(TestCase): m = nn.BatchNorm1d(100).npu() input1 = torch.randn(20, 100).npu() output = m(input1) - assert output is not None + self.assertEqual(output is not None, True) def test_BatchNorm2d(self): m = nn.BatchNorm2d(100, affine=False).npu() input1 = torch.randn(20, 100, 35, 45).npu() output = m(input1) - assert output is not None + self.assertEqual(output is not None, True) def test_BatchNorm3d(self): m = nn.BatchNorm3d(100).npu() input1 = torch.randn(20, 100, 35, 45, 10).npu() output = m(input1) - assert output is not None + self.assertEqual(output is not None, True) def test_GroupNorm(self): m = nn.GroupNorm(3, 6).npu() input1 = torch.randn(20, 6, 10, 10).npu() output = m(input1) - assert output is not None + self.assertEqual(output is not None, True) def test_convert_sync_batchnorm(self): module = torch.nn.Sequential( @@ -58,25 +58,25 @@ class TestNormalizationLayers(TestCase): m = nn.InstanceNorm1d(100).npu() input1 = torch.randn(20, 100, 40).npu() output = m(input1) - assert output is not None + self.assertEqual(output is not None, True) def test_InstanceNorm2d(self): m = nn.InstanceNorm2d(100).npu() input1 = torch.randn(20, 100, 35, 45).npu() output = m(input1) - assert output is not None + self.assertEqual(output is not None, True) def test_InstanceNorm3d(self): m = nn.InstanceNorm3d(100).npu() input1 = torch.randn(20, 100, 35, 45, 10).npu() output = m(input1) - assert output is not None + self.assertEqual(output is not None, True) def test_LayerNorm(self): input1 = torch.randn(20, 5, 10, 10).npu() m = nn.LayerNorm(input1.size()[1:]).npu() output = m(input1) - assert output is not None + self.assertEqual(output is not None, True) def test_LocalResponseNorm(self): lrn = nn.LocalResponseNorm(2).npu() @@ -84,8 +84,8 @@ class TestNormalizationLayers(TestCase): signal_4d = torch.randn(16, 5, 7, 7, 7, 7).npu() output_2d = lrn(signal_2d) output_4d = lrn(signal_4d) - assert output_2d is not None - assert output_4d is not None + self.assertEqual(output_2d is not None, True) + self.assertEqual(output_4d is not None, True) if __name__ == "__main__": 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 e39b9a07695..c4f1b61614a 100644 --- a/test/test_api/test_torch_nn/test_padding_layers.py +++ b/test/test_api/test_torch_nn/test_padding_layers.py @@ -24,37 +24,37 @@ class TestPaddingLayers(TestCase): m = nn.ReflectionPad2d(2).npu() input1 = torch.arange(9, dtype=torch.float).reshape(1, 1, 3, 3).npu() output = m(input1) - assert output is not None + self.assertEqual(output is not None, True) def test_ReplicationPad2d(self): m = nn.ReplicationPad2d(2).npu() input1 = torch.arange(9, dtype=torch.float).reshape(1, 1, 3, 3).npu() output = m(input1) - assert output is not None + self.assertEqual(output is not None, True) def test_ZeroPad2d(self): m = nn.ZeroPad2d(2).npu() input1 = torch.randn(1, 1, 3, 3).npu() output = m(input1) - assert output is not None + self.assertEqual(output is not None, True) def test_ConstantPad1d(self): m = nn.ConstantPad1d(2, 3.5).npu() input1 = torch.randn(1, 2, 4).npu() output = m(input1) - assert output is not None + self.assertEqual(output is not None, True) def test_ConstantPad2d(self): m = nn.ConstantPad2d(2, 3.5).npu() input1 = torch.randn(1, 2, 2).npu() output = m(input1) - assert output is not None + self.assertEqual(output is not None, True) def test_ConstantPad3d(self): m = nn.ConstantPad3d(3, 3.5).npu() input1 = torch.randn(16, 3, 10, 20, 30).npu() output = m(input1) - assert output is not None + self.assertEqual(output is not None, True) if __name__ == "__main__": 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 76152c3f7ad..7226a6122c0 100644 --- a/test/test_api/test_torch_nn/test_pooling_layers.py +++ b/test/test_api/test_torch_nn/test_pooling_layers.py @@ -25,19 +25,19 @@ class TestPoolingLayers(TestCase): m = nn.MaxPool1d(3, stride=2) input1 = torch.randn(20, 16, 50) output = m.npu()(input1.npu()) - assert output is not None + self.assertEqual(output is not None, True) def test_MaxPool2d(self): m = nn.MaxPool2d(3, stride=2) input1 = torch.randn(20, 16, 50, 32) output = m.npu()(input1.npu()) - assert output is not None + self.assertEqual(output is not None, True) def test_MaxPool3d(self): m = nn.MaxPool3d(3, stride=2) input1 = torch.randn(20, 16, 50,44, 31) output = m.npu()(input1.npu()) - assert output is not None + self.assertEqual(output is not None, True) def test_MaxUnpool1d(self): pool = nn.MaxPool1d(2, stride=2, return_indices=True) @@ -45,7 +45,7 @@ class TestPoolingLayers(TestCase): input1 = torch.tensor([[[1., 2, 3, 4, 5, 6, 7, 8]]]) output, indices = pool(input1) output = unpool.npu()(output.npu(), indices.npu()) - assert output is not None + self.assertEqual(output is not None, True) def test_MaxUnpool2d(self): pool = nn.MaxPool2d(2, stride=2, return_indices=True) @@ -56,64 +56,64 @@ class TestPoolingLayers(TestCase): [13, 14, 15, 16]]]]) output, indices = pool(input1) output = unpool.npu()(output.npu(), indices.npu()) - assert output is not None + self.assertEqual(output is not None, True) def test_MaxUnpool3d(self): pool = nn.MaxPool3d(3, stride=2, return_indices=True) unpool = nn.MaxUnpool3d(3, stride=2) output, indices = pool(torch.randn(20, 16, 51, 33, 15)) output = unpool.npu()(output.npu(), indices.npu()) - assert output is not None + self.assertEqual(output is not None, True) def test_AvgPool1d(self): m = nn.AvgPool1d(3, stride=2).npu() output = m(torch.tensor([[[1.,2,3,4,5,6,7]]], device=device)) - assert output is not None + self.assertEqual(output is not None, True) def test_AvgPool2d(self): m = nn.AvgPool2d(3, stride=2).npu() output = m(torch.randn(20, 16, 50, 32, device=device)) - assert output is not None + self.assertEqual(output is not None, True) def test_AvgPool3d(self): m = nn.AvgPool3d(3, stride=2).npu() output = m(input=torch.randn(20, 16, 50,44, 31, device=device)) - assert output is not None + self.assertEqual(output is not None, True) def test_LPPool1d(self): m = nn.LPPool1d(2, 3, stride=2).npu() output = m(input=torch.randn(20, 16, 50, device=device)) - assert output is not None + self.assertEqual(output is not None, True) def test_LPPool2d(self): m = nn.LPPool2d(2, 3, stride=2).npu() output = m(input=torch.randn(20, 16, 50, 32, device=device)) - assert output is not None + self.assertEqual(output is not None, True) def test_AdaptiveMaxPool1d(self): m = nn.AdaptiveMaxPool1d(4).npu() output = m(input=torch.randn(32, 16, 16, device=device)) - assert output is not None + self.assertEqual(output is not None, True) def test_AdaptiveMaxPool2d(self): m = nn.AdaptiveMaxPool2d((2, 3)).npu() output = m(input=torch.randn(1, 3, 8, 9, device=device)) - assert output is not None + self.assertEqual(output is not None, True) def test_AdaptiveAvgPool1d(self): m = nn.AdaptiveAvgPool1d(5).npu() output = m(input=torch.randn(1, 64, 8, device=device)) - assert output is not None + self.assertEqual(output is not None, True) def test_AdaptiveAvgPool2d(self): m = nn.AdaptiveAvgPool2d((5,7)).npu() output = m(input=torch.randn(1, 64, 8, 9, device=device)) - assert output is not None + self.assertEqual(output is not None, True) def test_AdaptiveAvgPool3d(self): m = nn.AdaptiveAvgPool3d((1,1,1)).npu() output = m(input=torch.randn(1, 64, 8, 9, 10, device=device)) - assert output is not None + self.assertEqual(output is not None, True) if __name__ == "__main__": 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 b1ef25f9084..bdae319bc18 100644 --- a/test/test_api/test_torch_nn/test_sparse_layers.py +++ b/test/test_api/test_torch_nn/test_sparse_layers.py @@ -23,8 +23,7 @@ class TestSparseLayers(TestCase): embedding = nn.Embedding(10, 3).npu() input1 = torch.LongTensor([[1,2,4,5],[4,3,2,9]]).npu() output = embedding(input1) - print(output.shape) - assert output is not None + self.assertEqual(output is not None, True) if __name__ == "__main__": -- Gitee