8000 Fix AllocatorConfig potential SIO issue (#159629) · pytorch/pytorch@56d19a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 56d19a5

Browse files
guangyeypytorchmergebot
authored andcommitted
Fix AllocatorConfig potential SIO issue (#159629)
# Motivation As @ScottTodd identified in this [comment](#150312 (comment)), using STL containers like `std::string` and `std::unordered_set` at static init time can cause static initialization order issues. This PR is based on and modified from his original PR: #159607. I’m stacking this PR here to help facilitate the landing and validation process. Co-authored-by: @ScottTodd Pull Request resolved: #159629 Approved by: https://github.com/ScottTodd, https://github.com/albanD
1 parent b6c5338 commit 56d19a5

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

c10/core/AllocatorConfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ void AcceleratorAllocatorConfig::parseArgs(const std::string& env) {
224224
// check if the key is unrecognized.
225225
if (device_config_parser_hook_) {
226226
TORCH_CHECK(
227-
keys_.find(key) != keys_.end(),
227+
getKeys().find(key) != getKeys().end(),
228228
"Unrecognized key '",
229229
key,
230230
"' in Accelerator allocator config.");

c10/core/AllocatorConfig.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,24 @@ class C10_API AcceleratorAllocatorConfig {
220220
return instance().last_allocator_settings_;
221221
}
222222

223+
// Use `Construct On First Use Idiom` to avoid `Static Initialization Order`
224+
// issue.
225+
static std::unordered_set<std::string>& getMutableKeys() {
226+
static std::unordered_set<std::string> keys{
227+
"max_split_size_mb",
228+
"max_non_split_rounding_mb",
229+
"garbage_collection_threshold",
230+
"roundup_power2_divisions",
231+
"expandable_segments",
232+
"pinned_use_background_threads"};
233+
return keys;
234+
}
235+
223236
// Returns the set of valid keys for the allocator configuration.
224237
// This set is used to validate the presence and correctness of keys in
225238
// device-specific configuration parsers.
226239
static const std::unordered_set<std::string>& getKeys() {
227-
return keys_;
240+
return getMutableKeys();
228241
}
229242

230243
// Registers a device-specific configuration parser hook and its key. This
@@ -238,9 +251,10 @@ class C10_API AcceleratorAllocatorConfig {
238251
std::function<void(const std::string&)>&& hook,
239252
const std::unordered_set<std::string>& keys) {
240253
device_config_parser_hook_ = std::move(hook);
254+
auto& mutable_keys = getMutableKeys();
241255
for (auto& key : keys) {
242256
TORCH_CHECK(
243-
keys_.insert(key).second,
257+
mutable_keys.insert(key).second,
244258
"Duplicated key '",
245259
key,
246260
"' found in device-specific configuration parser hook registration");
@@ -326,17 +340,6 @@ class C10_API AcceleratorAllocatorConfig {
326340
// their own environment configuration extensions.
327341
inline static std::function<void(const std::string&)>
328342
device_config_parser_hook_{nullptr};
329-
330-
// A set of valid configuration keys, including both common and
331-
// device-specific options. This set is used to validate the presence and
332-
// correctness of keys during parsing.
333-
inline static std::unordered_set<std::string> keys_{
334-
"max_split_size_mb",
335-
"max_non_split_rounding_mb",
336-
"garbage_collection_threshold",
337-
"roundup_power2_divisions",
338-
"expandable_segments",
339-
"pinned_use_background_threads"};
340343
};
341344

342345
C10_API inline void setAllocatorSettings(const std::string& env) {

0 commit comments

Comments
 (0)
0