forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vitals.h
94 lines (73 loc) · 2.38 KB
/
Vitals.h
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once
#include <ostream>
#include <sstream>
#include <unordered_map>
#include <c10/core/impl/LocalDispatchKeySet.h>
namespace at::vitals {
TORCH_API bool torchVitalEnabled();
struct TORCH_API TorchVitalAttr {
// always initialized to empty
std::string value = "";
template <typename T>
TorchVitalAttr& operator<<(const T& t) {
if (torchVitalEnabled()) {
std::stringstream ss;
ss << t;
value += ss.str();
}
return *this;
}
template <typename T>
void write(const T& t, bool force) {
if (force || torchVitalEnabled()) {
std::stringstream ss;
ss << t;
value = ss.str();
}
}
};
struct TORCH_API TorchVital {
std::string name;
std::unordered_map<std::string, TorchVitalAttr> attrs;
explicit TorchVital(std::string n) : name(std::move(n)) {}
TorchVital(const TorchVital&) = default;
TorchVital(TorchVital&&) = default;
TorchVital& operator=(const TorchVital&) = default;
TorchVital& operator=(TorchVital&&) = default;
TorchVital() = delete;
TorchVitalAttr& create(const std::string& attr);
TorchVitalAttr& create(const std::string& attr, bool force);
friend std::ostream& operator<<(std::ostream& os, const TorchVital& dt);
~TorchVital();
};
std::ostream& operator<<(std::ostream& os, TorchVital const& tv);
// A way to access vitals by string names instead of by global reference.
// This enables access to vitals from the PythonAPI.
class TORCH_API APIVitals {
public:
bool vitals_enabled;
// Set any vital sign that was added to the map.
bool setVital(
const std::string& vital_name,
const std::string& attr_name,
const std::string& value,
bool force = false);
std::string readVitals();
APIVitals();
// Ensure this stays a singleton
APIVitals(APIVitals const& other) = delete;
APIVitals(APIVitals&& other) = delete;
APIVitals& operator=(const APIVitals&) = delete;
APIVitals& operator=(APIVitals&&) = delete;
~APIVitals() = default;
private:
std::unordered_map<std::string, TorchVital> name_map_;
};
extern TORCH_API APIVitals VitalsAPI;
} // namespace at::vitals
#define TORCH_VITAL_DECLARE(name) \
TORCH_API at::vitals::TorchVital TorchVital_##name;
#define TORCH_VITAL_DEFINE(name) \
TORCH_API at::vitals::TorchVital TorchVital_##name(#name);
#define TORCH_VITAL_BASE(name) TorchVital_##name
#define TORCH_VITAL(name, attr) TORCH_VITAL_BASE(name).create(#attr)