forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TensorConversions.cpp
51 lines (43 loc) · 1.5 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
#include "ATen/ATen.h"
#include "ATen/NativeFunctions.h"
namespace at {
namespace native {
static void ensure_has_index(Device* device) {
if (!device->is_cuda() || device->has_index()) {
return;
}
device->set_index(at::current_device());
}
static Tensor to_impl(const Tensor& self, const TensorOptions& options, bool non_blocking) {
return self.type().toBackend(options.backend()).toScalarType(options.dtype())
.copy(self, non_blocking, options.device());
}
Tensor to(const Tensor& self, Device device, ScalarType dtype, bool non_blocking, bool copy) {
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, Device device, bool non_blocking, bool copy) {
ensure_has_index(&device);
if (self.device() == device && !copy) {
return self;
}
return to_impl(self, self.options().device(device), non_blocking);
}
Tensor to(const Tensor& self, const Tensor& other, bool non_blocking, bool copy) {
auto self_options = self.options();
auto options = other.options();
if (self_options == options && !copy) {
return self;
}
return to_impl(self, options, non_blocking);
}
}} // namespace at::native