forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RangeFactories.cpp
151 lines (130 loc) · 5.02 KB
/
RangeFactories.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
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
#include <ATen/NativeFunctions.h>
#include <ATen/AccumulateType.h>
#include <ATen/Parallel.h>
#include <ATen/Dispatch.h>
#include <cmath>
#include <limits>
namespace at { namespace native {
Tensor& linspace_cpu_out(Tensor& result, Scalar start, Scalar end, int64_t steps) {
AT_CHECK(steps >= 0, "number of steps must be non-negative");
if (result.numel() != steps) {
result.resize_({steps});
}
Tensor r = result.is_contiguous() ? result : result.contiguous();
if (steps == 0) {
// skip
} else if (steps == 1) {
r.fill_(start);
} else {
AT_DISPATCH_FLOATING_TYPES(r.type(), "linspace", [&]() {
scalar_t scalar_start = start.to<scalar_t>();
scalar_t scalar_end = end.to<scalar_t>();
scalar_t *data_ptr = r.data<scalar_t>();
scalar_t step = (scalar_end - scalar_start) / static_cast<scalar_t>(steps - 1);
at::parallel_for(0, steps, internal::GRAIN_SIZE, [&](int64_t p_begin, int64_t p_end) {
scalar_t is = static_cast<scalar_t>(p_begin);
for (int64_t i = p_begin; i < p_end; ++i, ++is) {
data_ptr[i] = scalar_start + step*is;
}
});
});
}
if (!result.is_contiguous()) {
result.copy_(r);
}
return result;
}
Tensor& logspace_cpu_out(Tensor& result, Scalar start, Scalar end, int64_t steps) {
AT_CHECK(steps >= 0, "number of steps must be non-negative");
if (result.numel() != steps) {
result.resize_({steps});
}
Tensor r = result.is_contiguous() ? result : result.contiguous();
if (steps == 0) {
// skip
} else if (steps == 1) {
r.fill_(std::pow(10.0, start.to<double>()));
} else {
AT_DISPATCH_FLOATING_TYPES(r.type(), "logspace", [&]() {
scalar_t base10 = 10;
scalar_t scalar_start = start.to<scalar_t>();
scalar_t scalar_end = end.to<scalar_t>();
scalar_t *data_ptr = r.data<scalar_t>();
scalar_t step = (scalar_end - scalar_start) / static_cast<scalar_t>(steps - 1);
at::parallel_for(0, steps, internal::GRAIN_SIZE, [&](int64_t p_begin, int64_t p_end) {
scalar_t is = static_cast<scalar_t>(p_begin);
for (int64_t i = p_begin; i < p_end; ++i, ++is) {
data_ptr[i]= std::pow(base10, scalar_start + step*is);
}
});
});
}
if (!result.is_contiguous()) {
result.copy_(r);
}
return result;
}
Tensor& range_cpu_out(Tensor& result, Scalar start, Scalar end, Scalar step) {
AT_DISPATCH_ALL_TYPES(result.type(), "range", [&]() {
using accscalar_t = at::acc_type<scalar_t, false>;
auto xstart = start.to<accscalar_t>();
auto xend = end.to<accscalar_t>();
auto xstep = step.to<accscalar_t>();
AT_CHECK(xstep > 0 || xstep < 0, "step must be nonzero");
AT_CHECK(std::isfinite(static_cast<double>(xstart)) &&
std::isfinite(static_cast<double>(xend)),
"unsupported range: ", xstart, " -> ", xend);
AT_CHECK(((xstep > 0) && (xend >= xstart)) || ((xstep < 0) && (xend <= xstart)),
"upper bound and larger bound inconsistent with step sign");
int64_t size = static_cast<int64_t>(((xend - xstart) / xstep) + 1);
if (result.numel() != size) {
result.resize_({size});
}
Tensor r = result.is_contiguous() ? result : result.contiguous();
scalar_t *data_ptr = r.data<scalar_t>();
at::parallel_for(0, size, internal::GRAIN_SIZE, [&](int64_t p_begin, int64_t p_end) {
scalar_t is = p_begin;
for (int64_t i = p_begin; i < p_end; ++i, ++is) {
data_ptr[i] = xstart + is * xstep;
}
});
if (!result.is_contiguous()) {
result.copy_(r);
}
});
return result;
}
Tensor& arange_cpu_out(Tensor& result, Scalar start, Scalar end, Scalar step) {
AT_DISPATCH_ALL_TYPES(result.type(), "arange", [&]() {
using accscalar_t = at::acc_type<scalar_t, false>;
auto xstart = start.to<accscalar_t>();
auto xend = end.to<accscalar_t>();
auto xstep = step.to<accscalar_t>();
AT_CHECK(xstep > 0 || xstep < 0, "step must be nonzero");
AT_CHECK(std::isfinite(static_cast<double>(xstart)) &&
std::isfinite(static_cast<double>(xend)),
"unsupported range: ", xstart, " -> ", xend);
AT_CHECK(((xstep > 0) && (xend >= xstart)) || ((xstep < 0) && (xend <= xstart)),
"upper bound and larger bound inconsistent with step sign");
double size_d = std::ceil(static_cast<double>(xend - xstart) / xstep);
AT_CHECK(size_d >= 0 && size_d <= static_cast<double>(std::numeric_limits<int64_t>::max()),
"invalid size, possible overflow?");
int64_t size = static_cast<int64_t>(size_d);
if (result.numel() != size) {
result.resize_({size});
}
Tensor r = result.is_contiguous() ? result : result.contiguous();
scalar_t *data_ptr = r.data<scalar_t>();
at::parallel_for(0, size, internal::GRAIN_SIZE, [&](int64_t p_begin, int64_t p_end) {
scalar_t is = p_begin;
for (int64_t i = p_begin; i < p_end; ++i, ++is) {
data_ptr[i] = xstart + is * xstep;
}
});
if (!result.is_contiguous()) {
result.copy_(r);
}
});
return result;
}
}} // namespace at::native