Skip to content

Commit c114585

Browse files
guangyeypytorchmergebot
authored andcommitted
Deprecate overleap functions in CUDAAllocatorConfig, use AcceleratorAllocatorConfig instead (pytorch#156165)
Pull Request resolved: pytorch#156165 Approved by: https://github.com/albanD ghstack dependencies: pytorch#159629, pytorch#150312
1 parent ae1a706 commit c114585

File tree

5 files changed

+44
-32
lines changed

5 files changed

+44
-32
lines changed

aten/src/ATen/cuda/CachingHostAllocator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ struct CUDACachingHostAllocatorImpl
162162
}
163163

164164
bool pinned_use_background_threads() override {
165-
return c10::cuda::CUDACachingAllocator::CUDAAllocatorConfig::
165+
return c10::CachingAllocator::AcceleratorAllocatorConfig::
166166
pinned_use_background_threads();
167167
}
168168

c10/cuda/CUDAAllocatorConfig.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <c10/core/AllocatorConfig.h>
44
#include <c10/cuda/CUDAException.h>
55
#include <c10/cuda/CUDAMacros.h>
6+
#include <c10/util/Deprecated.h>
67
#include <c10/util/Exception.h>
78
#include <c10/util/env.h>
89

@@ -17,9 +18,13 @@ enum class Expandable_Segments_Handle_Type : int {
1718
// Environment config parser
1819
class C10_CUDA_API CUDAAllocatorConfig {
1920
public:
21+
C10_DEPRECATED_MESSAGE(
22+
"c10::cuda::CUDACachingAllocator::CUDAAllocatorConfig::max_split_size() is deprecated. Please use c10::CachingAllocator::AcceleratorAllocatorConfig::max_split_size() instead.")
2023
static size_t max_split_size() {
2124
return c10::CachingAllocator::AcceleratorAllocatorConfig::max_split_size();
2225
}
26+
C10_DEPRECATED_MESSAGE(
27+
"c10::cuda::CUDACachingAllocator::CUDAAllocatorConfig::garbage_collection_threshold() is deprecated. Please use c10::CachingAllocator::AcceleratorAllocatorConfig::garbage_collection_threshold() instead.")
2328
static double garbage_collection_threshold() {
2429
return c10::CachingAllocator::AcceleratorAllocatorConfig::
2530
garbage_collection_threshold();
@@ -60,6 +65,8 @@ class C10_CUDA_API CUDAAllocatorConfig {
6065
return instance().m_pinned_num_register_threads;
6166
}
6267

68+
C10_DEPRECATED_MESSAGE(
69+
"c10::cuda::CUDACachingAllocator::CUDAAllocatorConfig::pinned_use_background_threads() is deprecated. Please use c10::CachingAllocator::AcceleratorAllocatorConfig::pinned_use_background_threads() instead.")
6370
static bool pinned_use_background_threads() {
6471
return c10::CachingAllocator::AcceleratorAllocatorConfig::
6572
pinned_use_background_threads();
@@ -72,25 +79,29 @@ class C10_CUDA_API CUDAAllocatorConfig {
7279
return 128;
7380
}
7481

75-
// This is used to round-up allocation size to nearest power of 2 divisions.
76-
// More description below in function roundup_power2_next_division
77-
// As an example, if we want 4 divisions between 2's power, this can be done
78-
// using env variable: PYTORCH_CUDA_ALLOC_CONF=roundup_power2_divisions:4
82+
C10_DEPRECATED_MESSAGE(
83+
"c10::cuda::CUDACachingAllocator::CUDAAllocatorConfig::roundup_power2_divisions() is deprecated. Please use c10::CachingAllocator::AcceleratorAllocatorConfig::roundup_power2_divisions() instead.")
7984
static size_t roundup_power2_divisions(size_t size) {
8085
return c10::CachingAllocator::AcceleratorAllocatorConfig::
8186
roundup_power2_divisions(size);
8287
}
8388

89+
C10_DEPRECATED_MESSAGE(
90+
"c10::cuda::CUDACachingAllocator::CUDAAllocatorConfig::roundup_power2_divisions() is deprecated. Please use c10::CachingAllocator::AcceleratorAllocatorConfig::roundup_power2_divisions() instead.")
8491
static std::vector<size_t> roundup_power2_divisions() {
8592
return c10::CachingAllocator::AcceleratorAllocatorConfig::
8693
roundup_power2_divisions();
8794
}
8895

96+
C10_DEPRECATED_MESSAGE(
97+
"c10::cuda::CUDACachingAllocator::CUDAAllocatorConfig::max_non_split_rounding_size() is deprecated. Please use c10::CachingAllocator::AcceleratorAllocatorConfig::max_non_split_rounding_size() instead.")
8998
static size_t max_non_split_rounding_size() {
9099
return c10::CachingAllocator::AcceleratorAllocatorConfig::
91100
max_non_split_rounding_size();
92101
}
93102

103+
C10_DEPRECATED_MESSAGE(
104+
"c10::cuda::CUDACachingAllocator::CUDAAllocatorConfig::last_allocator_settings() is deprecated. Please use c10::CachingAllocator::AcceleratorAllocatorConfig::last_allocator_settings() instead.")
94105
static std::string last_allocator_settings() {
95106
return c10::CachingAllocator::getAllocatorSettings();
96107
}

c10/cuda/CUDACachingAllocator.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,7 @@ class DeviceCachingAllocator {
12181218
DeviceCachingAllocator()
12191219
: large_blocks(/*small=*/false), small_blocks(/*small=*/true) {
12201220
stats.max_split_size =
1221-
static_cast<int64_t>(CUDAAllocatorConfig::max_split_size());
1221+
static_cast<int64_t>(AcceleratorAllocatorConfig::max_split_size());
12221222
context_recorder_.store(nullptr);
12231223
}
12241224

@@ -1343,7 +1343,8 @@ class DeviceCachingAllocator {
13431343
// Do garbage collection if the flag is set.
13441344
if (C10_UNLIKELY(
13451345
set_fraction &&
1346-
CUDAAllocatorConfig::garbage_collection_threshold() > 0.0)) {
1346+
AcceleratorAllocatorConfig::garbage_collection_threshold() >
1347+
0.0)) {
13471348
garbage_collect_cached_blocks(context);
13481349
}
13491350
// Attempt allocate
@@ -1595,7 +1596,7 @@ class DeviceCachingAllocator {
15951596
stats.active_bytes[stat_type].increase(block->size);
15961597
stats.requested_bytes[stat_type].increase(block->requested_size);
15971598
});
1598-
if (block->size >= CUDAAllocatorConfig::max_split_size())
1599+
if (block->size >= AcceleratorAllocatorConfig::max_split_size())
15991600
stats.oversize_allocations.increase(1);
16001601

16011602
auto allocated_bytes_gauge =
@@ -1646,7 +1647,7 @@ class DeviceCachingAllocator {
16461647
block->pool->owner_MempoolId(),
16471648
context ? context : block->context_when_allocated);
16481649

1649-
if (block->size >= CUDAAllocatorConfig::max_split_size())
1650+
if (block->size >= AcceleratorAllocatorConfig::max_split_size())
16501651
stats.oversize_allocations.decrease(1);
16511652

16521653
if (!block->stream_uses.empty()) {
@@ -2195,7 +2196,8 @@ class DeviceCachingAllocator {
21952196
if (size < kMinBlockSize) {
21962197
return kMinBlockSize;
21972198
} else {
2198-
auto divisions = CUDAAllocatorConfig::roundup_power2_divisions(size);
2199+
auto divisions =
2200+
AcceleratorAllocatorConfig::roundup_power2_divisions(size);
21992201
if (divisions > 1 && size > (kMinBlockSize * divisions)) {
22002202
return roundup_power2_next_division(size, divisions);
22012203
} else {
@@ -2674,7 +2676,7 @@ class DeviceCachingAllocator {
26742676
if (block->pool->is_small || CUDAAllocatorConfig::expandable_segments()) {
26752677
return remaining >= kMinBlockSize;
26762678
} else {
2677-
return (size < CUDAAllocatorConfig::max_split_size()) &&
2679+
return (size < AcceleratorAllocatorConfig::max_split_size()) &&
26782680
(remaining > kSmallSize);
26792681
}
26802682
}
@@ -2694,7 +2696,7 @@ class DeviceCachingAllocator {
26942696

26952697
if (C10_UNLIKELY(
26962698
set_fraction &&
2697-
CUDAAllocatorConfig::garbage_collection_threshold() > 0.0)) {
2699+
AcceleratorAllocatorConfig::garbage_collection_threshold() > 0.0)) {
26982700
// Track block reuse interval only when garbage collection is enabled.
26992701
++pool.get_free_blocks_call_count;
27002702
}
@@ -2736,13 +2738,13 @@ class DeviceCachingAllocator {
27362738
}
27372739

27382740
// Do not return an oversized block for a large request
2739-
if ((p.size() < CUDAAllocatorConfig::max_split_size()) &&
2740-
((*it)->size >= CUDAAllocatorConfig::max_split_size()))
2741+
if ((p.size() < AcceleratorAllocatorConfig::max_split_size()) &&
2742+
((*it)->size >= AcceleratorAllocatorConfig::max_split_size()))
27412743
return false;
27422744
// Allow oversized block size to be rounded up but within a limit
2743-
if ((p.size() >= CUDAAllocatorConfig::max_split_size()) &&
2745+
if ((p.size() >= AcceleratorAllocatorConfig::max_split_size()) &&
27442746
((*it)->size >=
2745-
p.size() + CUDAAllocatorConfig::max_non_split_rounding_size()))
2747+
p.size() + AcceleratorAllocatorConfig::max_non_split_rounding_size()))
27462748
return false;
27472749
p.block = *it;
27482750
pool.blocks.erase(it);
@@ -2765,7 +2767,7 @@ class DeviceCachingAllocator {
27652767
// therefore should be of less overheads.
27662768

27672769
size_t gc_threshold = static_cast<size_t>(
2768-
CUDAAllocatorConfig::garbage_collection_threshold() *
2770+
AcceleratorAllocatorConfig::garbage_collection_threshold() *
27692771
static_cast<double>(allowed_memory_maximum));
27702772
// No need to trigger GC yet
27712773
if (total_allocated_memory <= gc_threshold) {
@@ -2913,7 +2915,7 @@ class DeviceCachingAllocator {
29132915
stats.segment[stat_type].increase(1);
29142916
stats.reserved_bytes[stat_type].increase(size);
29152917
});
2916-
if (size >= CUDAAllocatorConfig::max_split_size())
2918+
if (size >= AcceleratorAllocatorConfig::max_split_size())
29172919
stats.oversize_segments.increase(1);
29182920
auto reserved_bytes_gauge =
29192921
STATIC_GAUGE(pytorch.CUDACachingAllocator.reserved_bytes);
@@ -2942,16 +2944,16 @@ class DeviceCachingAllocator {
29422944
bool release_available_cached_blocks(
29432945
const AllocParams& p,
29442946
const std::shared_ptr<GatheredContext>& context) {
2945-
if (CUDAAllocatorConfig::max_split_size() ==
2947+
if (AcceleratorAllocatorConfig::max_split_size() ==
29462948
std::numeric_limits<size_t>::max())
29472949
return false;
29482950
BlockPool& pool = *p.pool;
29492951

29502952
// because of std::unique_ptr, block cannot be trivially copied
29512953
// Use constructor for search key.
29522954
Block key(p.search_key.device, p.search_key.stream, p.search_key.size);
2953-
key.size = (key.size < CUDAAllocatorConfig::max_split_size())
2954-
? CUDAAllocatorConfig::max_split_size()
2955+
key.size = (key.size < AcceleratorAllocatorConfig::max_split_size())
2956+
? AcceleratorAllocatorConfig::max_split_size()
29552957
: key.size;
29562958
auto it = pool.blocks.lower_bound(&key);
29572959
if (it == pool.blocks.end() || (*it)->stream != p.stream() ||
@@ -2964,7 +2966,7 @@ class DeviceCachingAllocator {
29642966
--it; // Back up one item. Now on the largest block for the correct
29652967
// stream
29662968
while ((totalReleased < key.size) &&
2967-
((*it)->size >= CUDAAllocatorConfig::max_split_size()) &&
2969+
((*it)->size >= AcceleratorAllocatorConfig::max_split_size()) &&
29682970
((*it)->stream == p.stream())) {
29692971
auto cur = it;
29702972
bool is_first = cur == pool.blocks.begin();
@@ -3089,7 +3091,7 @@ class DeviceCachingAllocator {
30893091
stats.reserved_bytes[static_cast<int64_t>(StatType::AGGREGATE)]
30903092
.current);
30913093

3092-
if (block->size >= CUDAAllocatorConfig::max_split_size())
3094+
if (block->size >= AcceleratorAllocatorConfig::max_split_size())
30933095
stats.oversize_segments.decrease(1);
30943096
pool->blocks.erase(block);
30953097
delete block;
@@ -3716,18 +3718,19 @@ class NativeCachingAllocator : public CUDAAllocator {
37163718

37173719
auto& md = result.config_metadata;
37183720
md.garbage_collection_threshold =
3719-
CUDAAllocatorConfig::garbage_collection_threshold();
3720-
md.max_split_size = CUDAAllocatorConfig::max_split_size();
3721+
AcceleratorAllocatorConfig::garbage_collection_threshold();
3722+
md.max_split_size = AcceleratorAllocatorConfig::max_split_size();
37213723
md.pinned_num_register_threads =
37223724
CUDAAllocatorConfig::pinned_num_register_threads();
37233725
md.expandable_segments = CUDAAllocatorConfig::expandable_segments();
37243726
md.release_lock_on_malloc =
37253727
CUDAAllocatorConfig::release_lock_on_cudamalloc();
37263728
md.pinned_use_host_register =
37273729
CUDAAllocatorConfig::pinned_use_cuda_host_register();
3728-
md.last_allocator_settings = CUDAAllocatorConfig::last_allocator_settings();
3730+
md.last_allocator_settings =
3731+
AcceleratorAllocatorConfig::last_allocator_settings();
37293732
md.roundup_power2_divisions =
3730-
CUDAAllocatorConfig::roundup_power2_divisions();
3733+
AcceleratorAllocatorConfig::roundup_power2_divisions();
37313734

37323735
return result;
37333736
}

c10/xpu/XPUCachingAllocator.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <c10/core/AllocatorConfig.h>
12
#include <c10/util/flat_hash_map.h>
23
#include <c10/util/irange.h>
34
#include <c10/xpu/XPUCachingAllocator.h>
@@ -20,8 +21,6 @@ constexpr size_t kMinBlockSize = 512;
2021
constexpr size_t kSmallSize = 1048576;
2122
// "small" allocations are packed in 2 MiB blocks
2223
constexpr size_t kSmallBuffer = 2097152;
23-
// "large" allocations may be packed in 20 MiB blocks
24-
constexpr size_t kLargeBuffer = 20971520;
2524
// allocations between 1 and 10 MiB may use kLargeBuffer
2625
constexpr size_t kMinLargeAlloc = 10485760;
2726
// round up large allocations to 2 MiB

torch/csrc/cuda/Module.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#include <ATen/cuda/detail/CUDAHooks.h>
2121
#include <ATen/cuda/jiterator.h>
2222
#include <ATen/cuda/tunable/Tunable.h>
23+
#include <c10/core/AllocatorConfig.h>
2324
#include <c10/core/StorageImpl.h>
24-
#include <c10/cuda/CUDAAllocatorConfig.h>
2525
#include <c10/cuda/CUDACachingAllocator.h>
2626
#include <c10/cuda/CUDAFunctions.h>
2727
#include <ATen/cuda/CUDAGraphsUtils.cuh>
@@ -426,8 +426,7 @@ PyObject* THCPModule_cudaCachingAllocator_set_allocator_settings(
426426
PyObject* _unused,
427427
PyObject* env) {
428428
HANDLE_TH_ERRORS
429-
c10::cuda::CUDACachingAllocator::setAllocatorSettings(
430-
THPUtils_unpackString(env));
429+
c10::CachingAllocator::setAllocatorSettings(THPUtils_unpackString(env));
431430
Py_RETURN_NONE;
432431
END_HANDLE_TH_ERRORS
433432
}

0 commit comments

Comments
 (0)