Skip to content

Commit 3c1ba4d

Browse files
scottsfacebook-github-bot
authored andcommitted
[torchcodec] delay device map init to runtime and fix targets
Summary: We were running into the Static Initialization Order Fiasco: https://en.cppreference.com/w/cpp/language/siof We avoid this problem by delaying the initialization of the device map until runtime. Differential Revision: D72722867
1 parent 82750c8 commit 3c1ba4d

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/torchcodec/_core/DeviceInterface.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
namespace facebook::torchcodec {
1212

1313
namespace {
14+
using DeviceInterfaceMap = std::map<torch::DeviceType, CreateDeviceInterfaceFn>;
1415
std::mutex g_interface_mutex;
15-
std::map<torch::DeviceType, CreateDeviceInterfaceFn> g_interface_map;
16+
std::unique_ptr<DeviceInterfaceMap> g_interface_map;
1617

1718
std::string getDeviceType(const std::string& device) {
1819
size_t pos = device.find(':');
@@ -28,11 +29,18 @@ bool registerDeviceInterface(
2829
torch::DeviceType deviceType,
2930
CreateDeviceInterfaceFn createInterface) {
3031
std::scoped_lock lock(g_interface_mutex);
32+
if (!g_interface_map) {
33+
// We delay this initialization until runtime to avoid the Static
34+
// Initialization Order Fiasco:
35+
//
36+
// https://en.cppreference.com/w/cpp/language/siof
37+
g_interface_map = std::make_unique<DeviceInterfaceMap>();
38+
}
3139
TORCH_CHECK(
32-
g_interface_map.find(deviceType) == g_interface_map.end(),
40+
g_interface_map->find(deviceType) == g_interface_map->end(),
3341
"Device interface already registered for ",
3442
deviceType);
35-
g_interface_map.insert({deviceType, createInterface});
43+
g_interface_map->insert({deviceType, createInterface});
3644
return true;
3745
}
3846

@@ -45,14 +53,14 @@ torch::Device createTorchDevice(const std::string device) {
4553
std::scoped_lock lock(g_interface_mutex);
4654
std::string deviceType = getDeviceType(device);
4755
auto deviceInterface = std::find_if(
48-
g_interface_map.begin(),
49-
g_interface_map.end(),
56+
g_interface_map->begin(),
57+
g_interface_map->end(),
5058
[&](const std::pair<torch::DeviceType, CreateDeviceInterfaceFn>& arg) {
5159
return device.rfind(
5260
torch::DeviceTypeName(arg.first, /*lcase*/ true), 0) == 0;
5361
});
5462
TORCH_CHECK(
55-
deviceInterface != g_interface_map.end(), "Unsupported device: ", device);
63+
deviceInterface != g_interface_map->end(), "Unsupported device: ", device);
5664

5765
return torch::Device(device);
5866
}
@@ -67,11 +75,11 @@ std::unique_ptr<DeviceInterface> createDeviceInterface(
6775

6876
std::scoped_lock lock(g_interface_mutex);
6977
TORCH_CHECK(
70-
g_interface_map.find(deviceType) != g_interface_map.end(),
78+
g_interface_map->find(deviceType) != g_interface_map->end(),
7179
"Unsupported device: ",
7280
device);
7381

74-
return std::unique_ptr<DeviceInterface>(g_interface_map[deviceType](device));
82+
return std::unique_ptr<DeviceInterface>((*g_interface_map)[deviceType](device));
7583
}
7684

7785
} // namespace facebook::torchcodec

0 commit comments

Comments
 (0)