forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fill.cpp
114 lines (93 loc) · 3.35 KB
/
Fill.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
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
// Functions that fill Tensors with constants.
#include <ATen/ATen.h>
#include <ATen/Dispatch.h>
#include <ATen/native/TensorIterator.h>
#include <ATen/native/Fill.h>
namespace at {
namespace native {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ fill ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
namespace {
template <typename scalar_t>
inline void fill_fast(Tensor& self, Scalar value_scalar) {
auto value = value_scalar.to<scalar_t>();
scalar_t * dptr = static_cast<scalar_t *>(self.data_ptr());
*dptr = value;
}
} // namspace
Tensor& fill_out(Tensor& self, Scalar value) {
if (self.is_quantized()) {
at::Tensor out = at::ones(self.sizes()).to(kFloat) * value;
out = out.to(self.device());
// Trust the `copy_` to handle the quantization and the boundary chacks.
self.copy_(out);
return self;
}
// When filling a number to 1-element CPU tensor, we want to skip
// everything but manipulate data ptr directly.
// Ideally this fast pass should be implemented in TensorIterator,
// but we also want to skip compute_types which in not avoidable
// in TensorIterator for now.
if (self.device() == at::kCPU && self.numel() == 1 && !self.is_complex() && !value.isComplex()) {
AT_DISPATCH_ALL_TYPES_AND3(kHalf, kBool, kBFloat16, self.scalar_type(), "fill_out", [&]() {
fill_fast<scalar_t>(self, value);});
return self;
}
auto iter = TensorIteratorConfig()
.set_check_mem_overlap(false) // Fill is idempotent, so overlap is okay
.check_all_same_dtype(false)
.add_output(self)
.resize_outputs(false)
.build();
fill_stub(iter.device_type(), iter, value);
return self;
}
Tensor& fill_(Tensor& self, Scalar value) {
return fill_out(self, value);
}
Tensor& fill_(Tensor& self, const Tensor& value) {
TORCH_CHECK(value.dim() == 0, "fill_ only supports 0-dimension value tensor but got tensor with ", value.dim(), " dimensions.");
return fill_out(self, value.item());
}
DEFINE_DISPATCH(fill_stub);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ fill_diagonal ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tensor& fill_diagonal_(Tensor& self, Scalar fill_value, bool wrap) {
int64_t nDims = self.dim();
TORCH_CHECK(nDims >= 2, "dimensions must larger than 1");
int64_t height = self.size(0);
int64_t width = self.size(1);
if (nDims > 2) {
int64_t dim1 = height;
for (int64_t i = 1; i < nDims; i++) {
if (self.size(i) != dim1) {
AT_ERROR("all dimensions of input must be of equal length");
}
}
}
int64_t storage_offset = self.storage_offset();
std::vector<int64_t> sizes;
std::vector<int64_t> strides;
int64_t size = std::min(height, width);
int64_t stride = 0;
for (int64_t i = 0; i < nDims; i++) {
stride += self.stride(i);
}
strides.push_back(stride);
sizes.push_back(size);
auto main_diag = self.as_strided(sizes, strides, storage_offset);
main_diag.fill_(fill_value);
if (wrap && nDims == 2 && height > width + 1) {
std::vector<int64_t> wrap_sizes;
int64_t step = width + 1;
int64_t wrap_size = ((self.numel() + step - 1) / step) - size;
wrap_sizes.push_back(wrap_size);
int64_t offset = self.stride(0) * (width + 1);
auto wrap_diag = self.as_strided(wrap_sizes, strides, storage_offset + offset);
wrap_diag.fill_(fill_value);
}
return self;
}
Tensor& zero_(Tensor &self) {
return self.fill_(0);
}
} // namespace native
} // namespace at