forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceAccelerator.cpp
More file actions
236 lines (209 loc) · 9.08 KB
/
DeviceAccelerator.cpp
File metadata and controls
236 lines (209 loc) · 9.08 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <c10/core/AllocatorConfig.h>
#include <torch/csrc/DeviceAccelerator.h>
#include <torch/csrc/Exceptions.h>
#include <torch/csrc/utils/device_lazy_init.h>
namespace torch::accelerator {
void initModule(PyObject* module) {
auto m = py::handle(module).cast<py::module>();
m.def("_accelerator_getAccelerator", []() -> std::optional<c10::Device> {
// If no accelerator was available at compile time, return None.
auto acc = at::getAccelerator(false);
if (acc.has_value()) {
return acc.value();
} else {
return std::nullopt;
}
});
m.def("_accelerator_setDeviceIndex", [](c10::DeviceIndex device_index) {
// If device index is negative, no-op
if (device_index < 0) {
return;
}
const auto device_type = at::accelerator::getAccelerator(true).value();
torch::utils::maybe_initialize_device(device_type);
at::accelerator::setDeviceIndex(device_index);
});
m.def("_accelerator_getDeviceIndex", []() {
const auto device_type = at::accelerator::getAccelerator(true).value();
torch::utils::maybe_initialize_device(device_type);
return at::accelerator::getDeviceIndex();
});
m.def("_accelerator_getDeviceCapability", [](c10::DeviceIndex device_index) {
const auto device_type = at::accelerator::getAccelerator(true).value();
torch::utils::maybe_initialize_device(device_type);
auto caps = at::accelerator::getDeviceCapability(device_index);
py::dict dict;
py::set dtype_set;
caps.forEachSupportedScalarType([&](c10::ScalarType dtype) {
THPDtype* thp_dtype = torch::getTHPDtype(dtype);
py::object dtype_obj =
py::reinterpret_borrow<py::object>((PyObject*)thp_dtype);
dtype_set.add(dtype_obj);
});
dict["supported_dtypes"] = dtype_set;
return dict;
});
m.def("_accelerator_setStream", [](c10::Stream stream) {
const auto device_type = at::accelerator::getAccelerator(true).value();
torch::utils::maybe_initialize_device(device_type);
// Set the current device to the device of stream
if (at::accelerator::getDeviceIndex() != stream.device_index()) {
at::accelerator::setDeviceIndex(stream.device_index());
}
at::accelerator::setCurrentStream(stream);
});
m.def("_accelerator_getStream", [](c10::DeviceIndex device_index) {
const auto device_type = at::accelerator::getAccelerator(true).value();
torch::utils::maybe_initialize_device(device_type);
return at::accelerator::getCurrentStream(device_index);
});
m.def("_accelerator_synchronizeDevice", [](c10::DeviceIndex device_index) {
const auto device_type = at::accelerator::getAccelerator(true).value();
if (torch::utils::is_device_lazy_init_supported(device_type) &&
!torch::utils::is_device_initialized(device_type)) {
return;
}
torch::utils::maybe_initialize_device(device_type);
{
py::gil_scoped_release no_gil;
at::accelerator::synchronizeDevice(device_index);
}
});
m.def("_accelerator_exchangeDevice", [](c10::DeviceIndex device_index) {
const auto device_type = at::accelerator::getAccelerator(true).value();
torch::utils::maybe_initialize_device(device_type);
return at::accelerator::exchangeDevice(device_index);
});
m.def("_accelerator_maybeExchangeDevice", [](c10::DeviceIndex device_index) {
const auto device_type = at::accelerator::getAccelerator(true).value();
torch::utils::maybe_initialize_device(device_type);
return at::accelerator::maybeExchangeDevice(device_index);
});
m.def("_accelerator_isAllocatorInitialized", []() {
const auto device_type = at::accelerator::getAccelerator(true).value();
return at::getDeviceAllocator(device_type)->initialized();
});
m.def("_accelerator_emptyCache", []() { at::accelerator::emptyCache(); });
m.def("_accelerator_getDeviceStats", [](c10::DeviceIndex device_index) {
using c10::CachingAllocator::Stat;
using c10::CachingAllocator::StatArray;
using c10::CachingAllocator::StatType;
using c10::CachingDeviceAllocator::DeviceStats;
const auto stats = at::accelerator::getDeviceStats(device_index);
const auto stat_to_dict = [](const Stat& stat) -> py::dict {
py::dict dict;
dict["current"] = stat.current;
dict["peak"] = stat.peak;
dict["allocated"] = stat.allocated;
dict["freed"] = stat.freed;
return dict;
};
const auto stat_array_to_dict = [=](const StatArray& stats) -> py::dict {
const std::array<const char*, static_cast<size_t>(StatType::NUM_TYPES)>
kStatTypeNames = {"all", "small_pool", "large_pool"};
py::dict dict;
for (const auto i : c10::irange(kStatTypeNames.size())) {
dict[kStatTypeNames[i]] = stat_to_dict(stats[i]);
}
return dict;
};
py::dict result;
result["num_alloc_retries"] = stats.num_alloc_retries;
result["num_ooms"] = stats.num_ooms;
result["max_split_size"] = stats.max_split_size;
result["num_sync_all_streams"] = stats.num_sync_all_streams;
result["num_device_alloc"] = stats.num_device_alloc;
result["num_device_free"] = stats.num_device_free;
result["allocated_bytes"] = stat_array_to_dict(stats.allocated_bytes);
result["reserved_bytes"] = stat_array_to_dict(stats.reserved_bytes);
result["active_bytes"] = stat_array_to_dict(stats.active_bytes);
result["requested_bytes"] = stat_array_to_dict(stats.requested_bytes);
result["allocation"] = stat_array_to_dict(stats.allocation);
result["segment"] = stat_array_to_dict(stats.segment);
result["active"] = stat_array_to_dict(stats.active);
result["inactive_split"] = stat_array_to_dict(stats.inactive_split);
result["inactive_split_bytes"] =
stat_array_to_dict(stats.inactive_split_bytes);
result["oversize_allocations"] = stat_to_dict(stats.oversize_allocations);
result["oversize_segments"] = stat_to_dict(stats.oversize_segments);
return result;
});
m.def(
"_accelerator_resetAccumulatedStats", [](c10::DeviceIndex device_index) {
at::accelerator::resetAccumulatedStats(device_index);
});
m.def("_accelerator_resetPeakStats", [](c10::DeviceIndex device_index) {
at::accelerator::resetPeakStats(device_index);
});
m.def("_accelerator_getMemoryInfo", [](c10::DeviceIndex device_index) {
const auto device_type = at::accelerator::getAccelerator(true).value();
torch::utils::maybe_initialize_device(device_type);
py::gil_scoped_release no_gil;
return at::accelerator::getMemoryInfo(device_index);
});
m.def("_accelerator_getAllocatorSettings", []() {
return c10::CachingAllocator::getAllocatorSettings();
});
m.def("_accelerator_setAllocatorSettings", [](std::string env) {
c10::CachingAllocator::setAllocatorSettings(env);
});
// Accelerator Graph class binding
py::class_<at::accelerator::Graph, std::shared_ptr<at::accelerator::Graph>>(
m, "_acceleratorGraph")
.def(py::init<bool>(), py::arg("keep_graph") = false)
.def(
"capture_begin",
[](at::accelerator::Graph& self,
std::optional<c10::MempoolId_t> pool_opt,
const std::string& capture_error_mode) {
c10::MempoolId_t pool = pool_opt.has_value()
? pool_opt.value()
: c10::MempoolId_t{0, 0};
at::GraphCaptureMode capture_mode = at::GraphCaptureMode::Default;
if (capture_error_mode == "default") {
capture_mode = at::GraphCaptureMode::Default;
} else if (capture_error_mode == "global") {
capture_mode = at::GraphCaptureMode::Global;
} else if (capture_error_mode == "thread_local") {
capture_mode = at::GraphCaptureMode::ThreadLocal;
} else if (capture_error_mode == "relaxed") {
capture_mode = at::GraphCaptureMode::Relaxed;
} else {
TORCH_CHECK(
false,
"Unknown capture error mode. Expected `default`, `global`, `thread_local`, or `relaxed`, got ",
capture_error_mode);
}
return self.capture_begin(pool, capture_mode);
},
py::arg("pool") = std::nullopt,
py::arg("capture_error_mode") = "default",
py::call_guard<py::gil_scoped_release>())
.def(
"capture_end",
torch::wrap_pybind_function_no_gil(
&at::accelerator::Graph::capture_end))
.def(
"instantiate",
torch::wrap_pybind_function_no_gil(
&at::accelerator::Graph::instantiate))
.def(
"replay",
torch::wrap_pybind_function_no_gil(&at::accelerator::Graph::replay))
.def(
"reset",
torch::wrap_pybind_function_no_gil(&at::accelerator::Graph::reset))
.def(
"pool",
torch::wrap_pybind_function_no_gil(&at::accelerator::Graph::pool))
.def(
"enable_debug_mode",
torch::wrap_pybind_function_no_gil(
&::at::accelerator::Graph::enable_debug_mode))
.def(
"debug_dump",
torch::wrap_pybind_function_no_gil(
&::at::accelerator::Graph::debug_dump),
py::arg("path"));
}
} // namespace torch::accelerator