From 0e0f4bc88895896b1b4e2ececc501d8f96a170be Mon Sep 17 00:00:00 2001 From: wangyicheng Date: Wed, 23 Mar 2022 18:02:09 +0800 Subject: [PATCH 1/3] test/test_c10d.py change import order --- test/test_c10d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_c10d.py b/test/test_c10d.py index 428280e9430..1046bab6ad1 100644 --- a/test/test_c10d.py +++ b/test/test_c10d.py @@ -17,10 +17,10 @@ from enum import IntEnum, unique import os import unittest import torch -import torch_npu import torch.distributed as c10d import torch.distributed as dist import torch.multiprocessing as mp +import torch_npu from torch_npu.testing.testcase import TestCase, run_tests -- Gitee From 850fd469bfcf26d94308a21e278eb959b9153226 Mon Sep 17 00:00:00 2001 From: wangyicheng Date: Thu, 24 Mar 2022 14:44:25 +0800 Subject: [PATCH 2/3] fix redundant codo problem --- test/test_npu.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/test_npu.py b/test/test_npu.py index b8f63f24abd..a3bf1d87ee6 100644 --- a/test/test_npu.py +++ b/test/test_npu.py @@ -91,9 +91,7 @@ class TestNpu(TestCase): return torch.npu.FloatTensor(*size) def assert_change(comp=1, empty_cache=False, reset_peak=False): - # comp > 0: increased - # comp = 0: equal - # comp < 0: decreased + # comp > 0: increased, comp = 0: equal, comp < 0: decreased new_m = torch_npu.npu.memory_allocated(device) new_max_m = torch_npu.npu.max_memory_allocated(device) if comp > 0: -- Gitee From 954d586b6ff4ecd1f7b5951a4a3977458d42961c Mon Sep 17 00:00:00 2001 From: wangyicheng Date: Thu, 24 Mar 2022 15:20:12 +0800 Subject: [PATCH 3/3] fix line over 50 --- test/test_npu.py | 96 ++++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/test/test_npu.py b/test/test_npu.py index a3bf1d87ee6..d353e3c1d25 100644 --- a/test/test_npu.py +++ b/test/test_npu.py @@ -91,7 +91,6 @@ class TestNpu(TestCase): return torch.npu.FloatTensor(*size) def assert_change(comp=1, empty_cache=False, reset_peak=False): - # comp > 0: increased, comp = 0: equal, comp < 0: decreased new_m = torch_npu.npu.memory_allocated(device) new_max_m = torch_npu.npu.max_memory_allocated(device) if comp > 0: @@ -139,65 +138,68 @@ class TestNpu(TestCase): assert_change(0) yield - tensors1 = [alloc(1), alloc(10, 20), alloc(200, 300, 2000)] - m1 = torch_npu.npu.memory_allocated(device) - assert_change(1) - yield - - tensors2 = [] - - for i in range(1, int(N / 2) + 1): - # small ones - tensors2.append(alloc(i, i * 4)) + def change_with_tensor(): + tensors1 = [alloc(1), alloc(10, 20), alloc(200, 300, 2000)] + m1 = torch_npu.npu.memory_allocated(device) assert_change(1) yield - for i in range(5, int(N / 2) + 5): - # large ones - tensors2.append(alloc(i, i * 7, i * 9, i * 11)) - assert_change(1, reset_peak=(i % 2 == 0)) - yield + tensors2 = [] - tensors2.append(alloc(0, 0, 0)) - assert_change(0) - yield + for i in range(1, int(N / 2) + 1): + # small ones + tensors2.append(alloc(i, i * 4)) + assert_change(1) + yield - permute = [] - for i in torch.randperm(len(tensors2)): - permute.append(tensors2[i]) + for i in range(5, int(N / 2) + 5): + # large ones + tensors2.append(alloc(i, i * 7, i * 9, i * 11)) + assert_change(1, reset_peak=(i % 2 == 0)) + yield + + tensors2.append(alloc(0, 0, 0)) assert_change(0) yield - del tensors2 - assert_change(0) - yield - tensors2 = permute - assert_change(0) - yield - del permute - assert_change(0, reset_peak=True) - yield + permute = [] + for i in torch.randperm(len(tensors2)): + permute.append(tensors2[i]) + assert_change(0) + yield - for i in range(int(N / 2)): - x = tensors2[i].numel() - del tensors2[i] - assert_change(-x) # in case that tensors2[i] is empty + del tensors2 + assert_change(0) yield - - for i in range(2, int(2 * N / 3) + 2): - tensors2.append(alloc(i, i * 3, i * 8)) - assert_change(1) + tensors2 = permute + assert_change(0) + yield + del permute + assert_change(0, reset_peak=True) yield - del tensors2 - assert_change(-1, reset_peak=True) - assert_change(0) - self.assertEqual(torch_npu.npu.memory_allocated(device), m1) - yield True + for i in range(int(N / 2)): + x = tensors2[i].numel() + del tensors2[i] + assert_change(-x) # in case that tensors2[i] is empty + yield + + for i in range(2, int(2 * N / 3) + 2): + tensors2.append(alloc(i, i * 3, i * 8)) + assert_change(1) + yield + + del tensors2 + assert_change(-1, reset_peak=True) + assert_change(0) + self.assertEqual(torch_npu.npu.memory_allocated(device), m1) + yield True + + del tensors1 + assert_change(-1, reset_peak=True) + self.assertEqual(torch_npu.npu.memory_allocated(device), m0) - del tensors1 - assert_change(-1, reset_peak=True) - self.assertEqual(torch_npu.npu.memory_allocated(device), m0) + change_with_tensor() # test empty_cache and reset_peak assert_change(0, empty_cache=True) -- Gitee