-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathaten_xla_bridge.cpp
More file actions
549 lines (486 loc) · 17.5 KB
/
Copy pathaten_xla_bridge.cpp
File metadata and controls
549 lines (486 loc) · 17.5 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#include "torch_xla/csrc/aten_xla_bridge.h"
#include <map>
#include <string>
#include <vector>
#include <ATen/FunctionalTensorWrapper.h>
#include <torch/csrc/lazy/core/tensor_util.h>
#include "absl/log/absl_check.h"
#include "absl/strings/str_cat.h"
#include "torch_xla/csrc/device.h"
#include "torch_xla/csrc/runtime/debug_macros.h"
#include "torch_xla/csrc/runtime/runtime.h"
#include "torch_xla/csrc/status.h"
#include "torch_xla/csrc/tensor_impl.h"
#include "torch_xla/csrc/torch_util.h"
#include "torch_xla/csrc/xla_graph_executor.h"
namespace torch_xla {
namespace bridge {
namespace {
thread_local absl::optional<torch::lazy::BackendDevice> g_current_device;
class AtenXlaDeviceMapper {
public:
static AtenXlaDeviceMapper* Get();
size_t GetDeviceOrdinal(const torch::lazy::BackendDevice& device) const {
auto it = devices_ordinals_.find(device);
XLA_CHECK(it != devices_ordinals_.end()) << device;
return it->second;
}
const torch::lazy::BackendDevice& GetDeviceFromOrdinal(size_t ordinal) const {
return devices_.at(ordinal);
}
std::vector<torch::lazy::BackendDevice> GetAllDevices() const {
return devices_;
}
void SetVirtualDevice() {
for (auto& device : GetAllDevices()) {
if (static_cast<XlaDeviceType>(device.type()) == XlaDeviceType::SPMD) {
return;
}
}
devices_.emplace_back(GetVirtualDevice());
devices_ordinals_[devices_.back()] = 0;
}
private:
AtenXlaDeviceMapper() {
if (UseVirtualDevice()) {
devices_.emplace_back(GetVirtualDevice());
devices_ordinals_[devices_.back()] = 0;
} else {
XLA_ASSIGN_OR_THROW(
runtime::ComputationClient * absl_nonnull const client,
runtime::GetComputationClient());
for (auto& device_str : client->GetLocalDevices()) {
devices_.emplace_back(ParseDeviceString(device_str));
devices_ordinals_[devices_.back()] = devices_.size() - 1;
}
}
}
std::vector<torch::lazy::BackendDevice> devices_;
std::map<torch::lazy::BackendDevice, size_t> devices_ordinals_;
};
AtenXlaDeviceMapper* AtenXlaDeviceMapper::Get() {
static AtenXlaDeviceMapper* device_mapper = new AtenXlaDeviceMapper();
return device_mapper;
}
static absl::StatusOr<XLATensorImpl * absl_nonnull> GetXlaTensorImpl(
const at::Tensor& tensor) {
auto inner_tensor = torch::lazy::maybe_unwrap_functional(tensor);
XLATensorImpl* impl =
dynamic_cast<XLATensorImpl*>(inner_tensor.unsafeGetTensorImpl());
if (impl == nullptr) {
auto error_message =
absl::StrCat("Failed retrieving the inner XLATensorImpl* from ",
tensor.toString(), ". ",
"It's likely that `tensor` is not an actual XLA tensor, "
"i.e. it wasn't created inside PyTorch/XLA.");
return XLA_ERROR_WITH_LOCATION(absl::InvalidArgumentError(error_message));
}
return impl;
}
} // namespace
XLATensorPtr TryGetXlaTensor(const at::Tensor& tensor) {
return GetXlaTensor(tensor).value_or(XLATensorPtr{});
}
absl::StatusOr<absl_nonnull XLATensorPtr> GetXlaTensor(
const at::Tensor& tensor) {
if (tensor.defined() &&
at::functionalization::impl::isFunctionalTensor(tensor)) {
// To make sure we have the most updated version of tensor.
at::functionalization::impl::sync(tensor);
}
XLA_ASSIGN_OR_RETURN(
XLATensorImpl * impl, GetXlaTensorImpl(tensor),
absl::StrCat("Expected XLA tensor. Got: ", tensor.toString()));
return impl->tensor();
}
absl::StatusOr<std::vector<absl_nonnull XLATensorPtr>> GetXlaTensors(
const at::ITensorListRef& tensors) {
std::vector<absl_nonnull XLATensorPtr> xla_tensors;
xla_tensors.reserve(tensors.size());
std::size_t index = 0;
for (const auto& tensor : tensors) {
XLA_ASSIGN_OR_RETURN(
XLATensorPtr ptr, bridge::GetXlaTensor(tensor),
absl::StrCat("Expected all tensors in the given list to be XLA "
"tensors. Element at index ",
index, " is not an XLA tensor. Got: ", tensor.toString()));
xla_tensors.push_back(std::move(ptr));
index += 1;
}
return xla_tensors;
}
absl::StatusOr<absl_nonnull XLATensorPtr> GetInputXlaTensor(
const at::Tensor& tensor, const std::string_view param) {
XLA_ASSIGN_OR_RETURN(
XLATensorPtr ptr, GetXlaTensor(tensor),
absl::StrCat("Expected input tensor (", param,
") to be an actual XLA tensor. Got: ", tensor.toString(),
". Consider moving it (", param, ") to XLA."));
return ptr;
}
bool IsXlaTensor(const at::Tensor& tensor) {
return GetXlaTensorImpl(tensor).ok();
}
absl::Status ReplaceXlaTensor(const at::Tensor& tensor,
XLATensorPtr new_xla_tensor) {
XLA_ASSIGN_OR_RETURN(XLATensorImpl * impl, GetXlaTensorImpl(tensor),
"Failed replacing the XLA tensor in the given tensor.");
impl->set_tensor(std::move(new_xla_tensor));
return absl::OkStatus();
}
absl::Status ReplaceXlaTensor(const std::vector<at::Tensor>& tensors,
const std::vector<XLATensorPtr> new_xla_tensors) {
ABSL_CHECK(tensors.size() == new_xla_tensors.size())
<< "Expected the size of the list of tensors (" << tensors.size()
<< ") to match the size of the list of XLATensorPtr ("
<< new_xla_tensors.size() << ")";
for (size_t i = 0; i < tensors.size(); ++i) {
XLA_RETURN_IF_ERROR(
ReplaceXlaTensor(tensors[i], new_xla_tensors[i]),
absl::StrCat(
"Failed replacing the XLA tensor at index ", i,
". The reason being that that tensor is not an XLA tensor."));
}
return absl::OkStatus();
}
torch_xla::XLATensorPtr GetXlaTensorOrCreateForWrappedNumber(
const at::Tensor& tensor, const torch::lazy::BackendDevice& device) {
if (tensor.unsafeGetTensorImpl()->is_wrapped_number() ||
(tensor.dim() == 0 && tensor.numel() == 1)) {
return torch_xla::bridge::GetOrCreateXlaTensor(tensor, device);
} else {
XLA_ASSIGN_OR_THROW(XLATensorPtr xla_tensor,
torch_xla::bridge::GetXlaTensor(tensor));
return xla_tensor;
}
}
XLATensorPtr GetOrCreateXlaTensor(const at::Tensor& tensor,
const torch::lazy::BackendDevice& device) {
if (!tensor.defined()) {
return XLATensorPtr();
}
auto inner_tensor = torch::lazy::maybe_unwrap_functional(tensor);
if (!inner_tensor.defined()) {
return XLATensorPtr();
}
auto xtensor = GetXlaTensor(tensor);
if (xtensor.ok()) {
return xtensor.value();
}
XLA_ASSIGN_OR_THROW(XLATensorPtr xla_tensor,
XLATensor::Create(inner_tensor, device));
return xla_tensor;
}
XLATensorPtr GetOrCreateXlaTensor(const std::optional<at::Tensor>& tensor,
const torch::lazy::BackendDevice& device) {
if (!tensor.has_value()) {
return XLATensorPtr();
}
return GetOrCreateXlaTensor(*tensor, device);
}
std::vector<XLATensorPtr> GetOrCreateXlaTensors(
absl::Span<const at::Tensor> tensors,
const torch::lazy::BackendDevice& device) {
std::vector<XLATensorPtr> xla_tensors;
for (const at::Tensor& tensor : tensors) {
xla_tensors.push_back(bridge::GetOrCreateXlaTensor(tensor, device));
}
return xla_tensors;
}
std::vector<at::Tensor> XlaCreateTensorList(const at::ITensorListRef& tensors) {
std::vector<at::Tensor> aten_xla_tensors(tensors.size());
std::vector<XLATensorPtr> xla_tensors;
// We need to separate out the defined tensors first, GetXlaTensor() doesn't
// work with undefined tensors.
std::vector<bool> to_translate(tensors.size());
size_t ix = 0;
for (const auto& tensor : tensors) {
if (!tensor.defined()) {
continue;
}
auto inner_tensor = torch::lazy::maybe_unwrap_functional(tensor);
if (!inner_tensor.defined()) {
continue;
}
auto xtensor_status = GetXlaTensor(tensor);
if (xtensor_status.ok()) {
to_translate[ix] = true;
xla_tensors.push_back(xtensor_status.value());
} else {
aten_xla_tensors[ix] = tensor;
}
++ix;
}
auto defined_aten_xla_tensors =
XLAGraphExecutor::Get()->GetTensors(&xla_tensors);
// Insert undefined tensors into the result, back into the original undefined
// positions.
for (size_t i = 0, defined_pos = 0; i < tensors.size(); ++i) {
if (to_translate[i]) {
auto tensor = defined_aten_xla_tensors[defined_pos++];
XLA_CHECK(!at::functionalization::impl::isFunctionalTensor(tensor))
<< "Expected non-functional tensor!";
// This function is responsible for returning CPU tensors.
// So we do not want to wrap the outputs into FunctionalTensorWrappers.
aten_xla_tensors[i] = tensor;
}
}
return aten_xla_tensors;
}
std::vector<std::optional<at::Tensor>> XlaCreateOptTensorList(
const std::vector<std::optional<at::Tensor>>& tensors) {
std::vector<std::optional<at::Tensor>> opt_aten_xla_tensors(tensors.size());
std::vector<at::Tensor> materialized_tensors;
std::vector<bool> to_translate(tensors.size());
for (size_t i = 0; i < tensors.size(); ++i) {
auto tensor = tensors[i];
if (tensor.has_value()) {
to_translate[i] = true;
materialized_tensors.push_back(*tensor);
}
}
auto aten_materialzied_tensors = XlaCreateTensorList(materialized_tensors);
for (size_t i = 0, defined_pos = 0; i < tensors.size(); ++i) {
if (to_translate[i]) {
opt_aten_xla_tensors[i] =
std::move(aten_materialzied_tensors[defined_pos++]);
}
}
return opt_aten_xla_tensors;
}
void XlaUpdateTensors(absl::Span<const at::Tensor> dest_xla_tensors,
absl::Span<const at::Tensor> source_cpu_tensors,
absl::Span<const size_t> indices) {
for (auto index : indices) {
at::Tensor dest = dest_xla_tensors.at(index);
at::Tensor source = source_cpu_tensors.at(index);
auto dest_impl_status = GetXlaTensorImpl(dest);
if (dest_impl_status.ok()) {
auto dest_impl = std::move(dest_impl_status).value();
auto xla_source_status = GetXlaTensor(source);
if (xla_source_status.ok()) {
dest_impl->tensor()->UpdateFromTensorOut(xla_source_status.value());
} else {
dest_impl->tensor()->UpdateFromTensorOut(source);
}
dest_impl->force_refresh_sizes();
} else {
dest.resize_as_(source).copy_(source);
}
}
}
std::optional<torch::lazy::BackendDevice> GetXlaDevice(
const at::Tensor& tensor) {
auto xtensor_status = GetXlaTensor(tensor);
if (!xtensor_status.ok()) {
return std::nullopt;
}
return xtensor_status.value()->GetDevice();
}
std::optional<torch::lazy::BackendDevice> GetXlaDevice(
const std::optional<at::Tensor>& tensor) {
if (!tensor.has_value()) {
return std::nullopt;
}
return GetXlaDevice(*tensor);
}
std::optional<torch::lazy::BackendDevice> GetXlaDevice(
const at::TensorList& tensors) {
for (const auto& tensor : tensors) {
auto device = GetXlaDevice(tensor);
if (device) {
return device;
}
}
return std::nullopt;
}
std::optional<torch::lazy::BackendDevice> GetXlaDevice(
const std::vector<at::Tensor>& tensors) {
for (const auto& tensor : tensors) {
auto device = GetXlaDevice(tensor);
if (device) {
return device;
}
}
return std::nullopt;
}
std::optional<torch::lazy::BackendDevice> GetXlaDevice(
const at::TensorOptions& tensor_options) {
if (!tensor_options.has_device()) {
return std::nullopt;
}
return GetXlaDevice(tensor_options.device());
}
std::optional<torch::lazy::BackendDevice> GetXlaDevice(
const c10::Device& device) {
if (device.type() != at::kXLA) {
return std::nullopt;
}
return AtenDeviceToXlaDevice(device);
}
std::optional<torch::lazy::BackendDevice> GetXlaDevice(
const std::optional<c10::Device>& device) {
if (!device) {
return std::nullopt;
}
return GetXlaDevice(*device);
}
std::vector<torch::lazy::BackendDevice> GetBackendDevices() {
return AtenXlaDeviceMapper::Get()->GetAllDevices();
}
torch::lazy::BackendDevice AtenDeviceToXlaDevice(const c10::Device& device) {
XLA_CHECK_EQ(device.type(), at::kXLA) << device;
int ordinal = device.has_index() ? device.index() : -1;
if (ordinal < 0) {
c10::Device current_device = GetCurrentAtenDevice();
if (current_device.has_index()) {
ordinal = current_device.index();
}
}
if (ordinal < 0) {
return GetCurrentDevice();
}
return AtenXlaDeviceMapper::Get()->GetDeviceFromOrdinal(ordinal);
}
c10::Device XlaDeviceToAtenDevice(const torch::lazy::BackendDevice& device) {
// TODO(yeounoh) until we expose SPMD virtual device to the frontend, this
// will just be `XLA:0`.
if (device.type() == (int8_t)XlaDeviceType::SPMD) {
return c10::Device(at::kXLA, (size_t)0);
}
return c10::Device(at::kXLA,
AtenXlaDeviceMapper::Get()->GetDeviceOrdinal(device));
}
std::string ToXlaString(const c10::Device& device) {
return absl::StrCat("xla:", device.index());
}
static absl::StatusOr<torch::lazy::BackendDevice * absl_nonnull>
InitializeDefaultBackendDevice() {
std::string spec;
if (UseVirtualDevice()) {
spec = "SPMD:0";
} else {
XLA_ASSIGN_OR_RETURN(runtime::ComputationClient * absl_nonnull const client,
runtime::GetComputationClient());
spec = client->GetDefaultDevice();
}
ABSL_CHECK(!spec.empty());
XLA_ASSIGN_OR_RETURN(torch::lazy::BackendDevice device,
SafeParseDeviceString(spec));
return new torch::lazy::BackendDevice(device);
}
const absl::StatusOr<torch::lazy::BackendDevice * absl_nonnull>&
GetDefaultDevice() {
static absl::StatusOr<torch::lazy::BackendDevice* absl_nonnull>&
default_backend_device =
*new absl::StatusOr<torch::lazy::BackendDevice * absl_nonnull>(
InitializeDefaultBackendDevice());
return default_backend_device;
}
c10::Device AtenDefaultDevice() {
XLA_ASSIGN_OR_THROW(const torch::lazy::BackendDevice* default_device,
bridge::GetDefaultDevice());
return XlaDeviceToAtenDevice(*default_device);
}
torch::lazy::BackendDevice GetCurrentDevice() {
if (!g_current_device) {
XLA_ASSIGN_OR_THROW(const torch::lazy::BackendDevice* default_device,
bridge::GetDefaultDevice());
g_current_device = *default_device;
}
return *g_current_device;
}
c10::Device GetCurrentAtenDevice() {
return XlaDeviceToAtenDevice(GetCurrentDevice());
}
c10::Device SetCurrentDevice(const c10::Device& device) {
torch::lazy::BackendDevice prev_device =
SetCurrentDevice(AtenDeviceToXlaDevice(device));
return XlaDeviceToAtenDevice(prev_device);
}
torch::lazy::BackendDevice SetCurrentDevice(
const torch::lazy::BackendDevice& device) {
torch::lazy::BackendDevice current = GetCurrentDevice();
g_current_device = device;
TF_VLOG(2) << "New current device: " << device;
return current;
}
at::Tensor XlaToAtenTensor(XLATensorPtr xla_tensor,
const at::TensorOptions& tensor_options) {
if (tensor_options.has_device()) {
XLA_CHECK_NE(tensor_options.device().type(), at::kXLA);
}
at::Tensor tensor = xla_tensor->ToTensor(/*detached=*/false);
// We need to copy the tensor since it is cached within the XLATensor, and
// returning it directly might expose it to in place changes. Which there was
// COW option :)
return tensor.to(tensor_options, /*non_blocking=*/false, /*copy=*/true);
}
at::Tensor AtenFromXlaTensor(XLATensorPtr xla_tensor,
bool skip_functionalization) {
if (xla_tensor) {
auto out =
at::Tensor(c10::make_intrusive<XLATensorImpl>(std::move(xla_tensor)));
// See Note [Lazy Tensor Functionalization]
if (skip_functionalization ||
c10::impl::tls_local_dispatch_key_set().excluded_.has(
c10::DispatchKey::Functionalize)) {
// Invariant: if the functionalization key is in the exclude set, then
// we're expected to return an ordinary tensor, which will be "lifted"
// into a functional wrapper later.
return out;
} else {
auto wrapped = MaybeWrapTensorToFunctional(out);
return wrapped;
}
} else {
return at::Tensor();
}
}
std::vector<at::Tensor> AtenFromXlaTensors(
absl::Span<const XLATensorPtr> xla_tensors) {
std::vector<at::Tensor> tensors;
tensors.reserve(xla_tensors.size());
for (auto& tensor : xla_tensors) {
tensors.emplace_back(AtenFromXlaTensor(tensor));
}
return tensors;
}
at::Tensor CreateXlaTensor(
at::Tensor tensor,
const std::optional<torch::lazy::BackendDevice>& device) {
if (tensor.defined() && device) {
XLA_ASSIGN_OR_THROW(XLATensorPtr xla_tensor,
XLATensor::Create(std::move(tensor), *device));
tensor = AtenFromXlaTensor(xla_tensor);
}
return tensor;
}
std::vector<at::Tensor> CreateXlaTensors(
const std::vector<at::Tensor>& tensors,
const std::optional<torch::lazy::BackendDevice>& device) {
std::vector<at::Tensor> xtensors;
for (auto& tensor : tensors) {
xtensors.push_back(CreateXlaTensor(tensor, device));
}
return xtensors;
}
const at::Tensor& GetRootBase(const at::Tensor& tensor) {
auto xla_tensor_status = GetXlaTensor(tensor);
if (!xla_tensor_status.ok()) {
return tensor;
}
auto xla_tensor = std::move(xla_tensor_status).value();
if (!xla_tensor->Base().defined()) {
return tensor;
}
return GetRootBase(xla_tensor->Base());
}
XLATensorPtr SetBaseTensor(XLATensorPtr tensor, const at::Tensor& base) {
XLA_CHECK(base.device().is_xla())
<< "base tensor on unexpected device: " << base.device();
tensor->SetBase(GetRootBase(base));
return tensor;
}
} // namespace bridge
} // namespace torch_xla