已合并
del 32 padding size with cann version check #33482
关龙锋创建于 4月10日
del 32 padding size with cann version check #33482
已合并
关龙锋创建于 4月10日
2 个文件变更+49-12
@@ -1,29 +1,50 @@
1import os1import os
2+import math
2import shutil3import shutil
3import unittest4import unittest
4import torch5import torch
5import torch_npu6import torch_npu
6 7 
8+import torch_npu.npu.utils as utils
9+ 
7from torch_npu.testing.testcase import TestCase, run_tests10from torch_npu.testing.testcase import TestCase, run_tests
8from torch_npu.testing.common_utils import SupportedDevices11from torch_npu.testing.common_utils import SupportedDevices
9 12 
10 13 
11class TestAllocator(TestCase):14class TestAllocator(TestCase):
12- @SupportedDevices(['Ascend910B'])
13 def test_huge_memory_alloc_20M(self):15 def test_huge_memory_alloc_20M(self):
14 prev = torch_npu.npu.memory_allocated()16 prev = torch_npu.npu.memory_allocated()
15 a = torch.rand(1024 * 1024 * 40, dtype=torch.float32).npu()17 a = torch.rand(1024 * 1024 * 40, dtype=torch.float32).npu()
16- torch.npu.synchronize()
17 # 实际申请1G内存18 # 实际申请1G内存
18- self.assertEqual(torch_npu.npu.memory_allocated(), prev + ((4 * 40 * 1024 * 1024 + 32) // 512 + 1) * 512)19+ version = utils.get_cann_version(module="CANN")
20+ if (utils.get_soc_version() >= 260 and version >= "9.1.0"):
21+ self.assertEqual(torch_npu.npu.memory_allocated(), prev + math.ceil((4 * 40 * 1024 * 1024) / 512) * 512)
22+ else:
23+ self.assertEqual(torch_npu.npu.memory_allocated(),
24+ prev + math.ceil((4 * 40 * 1024 * 1024 + 32) / 512) * 512)
19 25 
20- @SupportedDevices(['Ascend910B'])
21 def test_huge_memory_alloc_512B(self):26 def test_huge_memory_alloc_512B(self):
27+ os.environ["PYTORCH_NPU_ALLOC_CONF"] = "expandable_segments:False"
22 prev = torch_npu.npu.memory_allocated()28 prev = torch_npu.npu.memory_allocated()
23- a = torch.rand(8 * 8 * 16, dtype=torch.float32).npu() # 512B29+ a = torch.rand(8 * 8 * 16, dtype=torch.float32).npu() # 512B
24- torch.npu.synchronize()
25 # 实际申请1M内存30 # 实际申请1M内存
26- self.assertEqual(torch_npu.npu.memory_allocated(), prev + ((8 * 8 * 16 * 4 + 32) // 512 + 1) * 512)31+ version = utils.get_cann_version(module="CANN")
32+ if (utils.get_soc_version() >= 260 and version >= "9.1.0"):
33+ self.assertEqual(torch_npu.npu.memory_allocated(), prev + math.ceil((8 * 8 * 16 * 4) / 512) * 512)
34+ else:
35+ self.assertEqual(torch_npu.npu.memory_allocated(), prev + math.ceil((8 * 8 * 16 * 4 + 32) / 512) * 512)
36+ 
37+ def test_huge_memory_alloc_512B_by_vm(self):
38+ os.environ["PYTORCH_NPU_ALLOC_CONF"] = "expandable_segments:True"
39+ prev = torch_npu.npu.memory_allocated()
40+ a = torch.rand(8 * 8 * 16, dtype=torch.float32).npu() # 512B
41+ # 实际申请1M内存
42+ version = utils.get_cann_version(module="CANN")
43+ if (utils.get_soc_version() >= 260 and version >= "9.1.0"):
44+ self.assertEqual(torch_npu.npu.memory_allocated(), prev + math.ceil((8 * 8 * 16 * 4) / 512) * 512)
45+ else:
46+ self.assertEqual(torch_npu.npu.memory_allocated(), prev + math.ceil((8 * 8 * 16 * 4 + 32) / 512) * 512)
47+ del os.environ["PYTORCH_NPU_ALLOC_CONF"]
27 48 
28if __name__ == '__main__':49if __name__ == '__main__':
29 run_tests()50 run_tests()
@@ -104,6 +104,7 @@ constexpr size_t kRoundUpPowerOfTwoEnd = 1ULL << 36; // 64 GB
104constexpr size_t kRoundUpPowerOfTwoIntervals = 16;104constexpr size_t kRoundUpPowerOfTwoIntervals = 16;
105const std::string kMinCannVersion = "8.1.RC1"; // minimum cann version which supports 1g mem 8.1.RC1105const std::string kMinCannVersion = "8.1.RC1"; // minimum cann version which supports 1g mem 8.1.RC1
106const std::string kMinDriverVersion = "25.0.RC1"; // minimum driver version which supports 1g mem 25.0.RC1106const std::string kMinDriverVersion = "25.0.RC1"; // minimum driver version which supports 1g mem 25.0.RC1
107+const std::string kMinDel32PaddingSizeCannVersion = "9.1.0";// minimum cann version which supports del 32 paddingsize
107const std::string kCannModule = "CANN"; // cann module name108const std::string kCannModule = "CANN"; // cann module name
108constexpr int kPrecision = 4; // precision of the memory usage information109constexpr int kPrecision = 4; // precision of the memory usage information
109constexpr size_t kLazyQuerySize = 512; // lazy query event size110constexpr size_t kLazyQuerySize = 512; // lazy query event size
@@ -186,6 +187,23 @@ bool IsMallocPage1GMem(bool is_small_pool)
186 return !is_small_pool && is_support_page_size_1g;187 return !is_small_pool && is_support_page_size_1g;
187}188}
188 189 
190+size_t AddPadSize()
191+{
192+ static size_t add_size = -1;
193+ if (add_size == -1) {
194+ // 新增cann版本兼容
195+ if (GetSocVersion() >= SocVersion::Ascend950 &&
196+ IsGteCANNVersion(kMinDel32PaddingSizeCannVersion, kCannModule)) {
197+ add_size = 0;
198+ } else {
199+ TORCH_NPU_WARN_ONCE(
200+ "The current CANN and Soc versions require processing for 32 padding size, with memory allocation.");
201+ add_size = 32;
202+ }
203+ }
204+ return add_size;
205+}
206+ 
189struct Block;207struct Block;
190struct PrivatePool;208struct PrivatePool;
191using Comparison = bool (*)(const Block *, const Block *);209using Comparison = bool (*)(const Block *, const Block *);
@@ -2073,9 +2091,7 @@ public:
2073 2091 
2074 static size_t round_size(size_t size)2092 static size_t round_size(size_t size)
2075 {2093 {
2076- constexpr size_t kPadSize = 32;2094+ size += AddPadSize();
2077- size += kPadSize;
2078-
2079 if (size < kMinBlockSize) {2095 if (size < kMinBlockSize) {
2080 return kMinBlockSize;2096 return kMinBlockSize;
2081 } else {2097 } else {
@@ -3540,7 +3556,7 @@ public:
3540 if (size != 0) {3556 if (size != 0) {
3541 if (c10_npu::option::OptionsManager::CheckForceUncached()) {3557 if (c10_npu::option::OptionsManager::CheckForceUncached()) {
3542 deleteFunc = &uncached_delete;3558 deleteFunc = &uncached_delete;
3543- size_t alloc_size = size + 32;3559+ size_t alloc_size = size + AddPadSize();
3544 NPU_CHECK_ERROR(c10_npu::acl::AclrtMallocAlign32(&devPtr, alloc_size,3560 NPU_CHECK_ERROR(c10_npu::acl::AclrtMallocAlign32(&devPtr, alloc_size,
3545 aclrtMemMallocPolicy::ACL_MEM_MALLOC_HUGE_FIRST));3561 aclrtMemMallocPolicy::ACL_MEM_MALLOC_HUGE_FIRST));
3546 TORCH_NPU_MEMORY_LOGD("Without NPUCachingAllocator, malloc by "3562 TORCH_NPU_MEMORY_LOGD("Without NPUCachingAllocator, malloc by "
@@ -3570,7 +3586,7 @@ public:
3570 if (size != 0) {3586 if (size != 0) {
3571 if (c10_npu::option::OptionsManager::CheckForceUncached()) {3587 if (c10_npu::option::OptionsManager::CheckForceUncached()) {
3572 deleteFunc = &uncached_delete;3588 deleteFunc = &uncached_delete;
3573- size_t alloc_size = size + 32 + aligned;3589+ size_t alloc_size = size + AddPadSize() + aligned;
3574 NPU_CHECK_ERROR(c10_npu::acl::AclrtMallocAlign32(&realPtr, alloc_size,3590 NPU_CHECK_ERROR(c10_npu::acl::AclrtMallocAlign32(&realPtr, alloc_size,
3575 aclrtMemMallocPolicy::ACL_MEM_MALLOC_HUGE_FIRST));3591 aclrtMemMallocPolicy::ACL_MEM_MALLOC_HUGE_FIRST));
3576 TORCH_NPU_MEMORY_LOGD("Without NPUCachingAllocator, malloc by AclrtMallocAlign32: size=%zu", alloc_size);3592 TORCH_NPU_MEMORY_LOGD("Without NPUCachingAllocator, malloc by AclrtMallocAlign32: size=%zu", alloc_size);