forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVitals.cpp
59 lines (50 loc) · 1.32 KB
/
Vitals.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <ATen/core/Vitals.h>
#include <cstdlib>
namespace at {
namespace vitals {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
APIVitals VitalsAPI;
TorchVitalAttr& TorchVital::create(const std::string& attr) {
if (!torchVitalEnabled()) {
static TorchVitalAttr disabled;
return disabled;
}
auto iter = attrs.find(attr);
if (iter == attrs.end()) {
auto r = attrs.emplace(std::make_pair(attr, TorchVitalAttr()));
return r.first->second;
}
return iter->second;
}
bool torchVitalEnabled() {
// If this is a performance hit, make `enabled` variable static
// and return `const bool&` instead
bool enabled = []() {
auto e = getenv("TORCH_VITAL");
if (e != nullptr) {
return strlen(e) > 0;
}
return false;
}();
return enabled;
}
bool APIVitals::setVital(
const std::string& vital_name,
const std::string& attr_name,
const std::string& value) {
if (!torchVitalEnabled()) {
return false;
}
auto iter = name_map_.find(vital_name);
TorchVital *vital = nullptr;
if (iter == name_map_.end()) {
auto r = name_map_.emplace(std::make_pair(vital_name, TorchVital(vital_name)));
vital = &r.first->second;
} else {
vital = &iter->second;
}
vital->create(attr_name) << value;
return true;
}
} // namespace at
} // namespace vitals