forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TensorConversions.cpp
83 lines (72 loc) · 3.02 KB
/
TensorConversions.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <ATen/ATen.h>
#include <ATen/NativeFunctions.h>
#include <c10/util/Optional.h>
#include <c10/core/impl/DeviceGuardImplInterface.h>
namespace at {
namespace native {
// Take a Device that may not have device_index set (i.e., having it as -1
// representing the current device) and return the corresponding Device
// according to the actual device at the time of this function call. No-op
// if the device_index is set.
static inline Device ensure_has_index(Device device) {
if (device.is_cpu() || device.has_index()) {
return device;
}
const c10::impl::DeviceGuardImplInterface* impl = c10::impl::getDeviceGuardImpl(device.type());
return impl->getDevice();
}
static inline Tensor to_impl(const Tensor& self, const TensorOptions& options, bool non_blocking) {
return self.type().toBackend(options.backend()).toScalarType(typeMetaToScalarType(options.dtype()))
.copy(self, non_blocking, options.device());
}
Tensor to(const Tensor& self, const TensorOptions& options, bool non_blocking, bool copy) {
AT_CHECK(options.requires_grad_opt() == c10::nullopt,
"to(options) expects unset requires_grad flag, but got "
"options.requires_grad set as ", options.requires_grad());
const auto & layout_opt = options.layout_opt();
AT_CHECK(!layout_opt || self.layout() == layout_opt.value(),
"to(options) doesn't support converting to a different layout, "
"but got self.layout being ", self.layout(),
" and options.layout set as ", options.layout());
auto device_opt = options.device_opt();
if (device_opt) {
device_opt = ensure_has_index(device_opt.value());
}
const auto & dtype_opt = options.dtype_opt();
if ((!device_opt || self.device() == device_opt.value()) &&
(!dtype_opt || self.dtype() == dtype_opt.value()) && !copy) {
return self;
}
auto specified_options = self.options();
if (device_opt) {
specified_options = specified_options.device(device_opt.value());
}
if (dtype_opt) {
specified_options = specified_options.dtype(dtype_opt.value());
}
return to_impl(self, specified_options, non_blocking);
}
Tensor to(const Tensor& self, Device device, ScalarType dtype, bool non_blocking, bool copy) {
device = ensure_has_index(device);
if (self.device() == device && self.dtype() == dtype && !copy) {
return self;
}
return to_impl(self, self.options().device(device).dtype(dtype), non_blocking);
}
Tensor to(const Tensor& self, ScalarType dtype, bool non_blocking, bool copy) {
if (self.dtype() == dtype && !copy) {
return self;
}
return to_impl(self, self.options().dtype(dtype), non_blocking);
}
Tensor to(const Tensor& self, const Tensor& other, bool non_blocking, bool copy) {
auto self_options = self.options();
auto options = other.options();
// Tensor.options() always have everything filled so we are happy and don't
// even need to fill in device index.
if (self_options == options && !copy) {
return self;
}
return to_impl(self, options, non_blocking);
}
}} // namespace at::native