-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathtensor_ptr_maker.cpp
190 lines (174 loc) · 5.72 KB
/
tensor_ptr_maker.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
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/extension/tensor/tensor_ptr_maker.h>
#include <random>
namespace executorch {
namespace extension {
namespace {
template <
typename INT_T,
typename std::enable_if<
std::is_integral<INT_T>::value && !std::is_same<INT_T, bool>::value,
bool>::type = true>
bool extract_scalar(executorch::aten::Scalar scalar, INT_T* out_val) {
if (!scalar.isIntegral(/*includeBool=*/false)) {
return false;
}
int64_t val = scalar.to<int64_t>();
if (val < std::numeric_limits<INT_T>::lowest() ||
val > std::numeric_limits<INT_T>::max()) {
return false;
}
*out_val = static_cast<INT_T>(val);
return true;
}
template <
typename FLOAT_T,
typename std::enable_if<
std::is_floating_point_v<FLOAT_T> ||
std::is_same_v<FLOAT_T, executorch::aten::BFloat16> ||
std::is_same_v<FLOAT_T, executorch::aten::Half>,
bool>::type = true>
bool extract_scalar(executorch::aten::Scalar scalar, FLOAT_T* out_val) {
double val;
if (scalar.isFloatingPoint()) {
val = scalar.to<double>();
if (std::isfinite(val) &&
(val < std::numeric_limits<FLOAT_T>::lowest() ||
val > std::numeric_limits<FLOAT_T>::max())) {
return false;
}
} else if (scalar.isIntegral(/*includeBool=*/false)) {
val = static_cast<double>(scalar.to<int64_t>());
} else {
return false;
}
*out_val = static_cast<FLOAT_T>(val);
return true;
}
template <
typename BOOL_T,
typename std::enable_if<std::is_same<BOOL_T, bool>::value, bool>::type =
true>
bool extract_scalar(executorch::aten::Scalar scalar, BOOL_T* out_val) {
if (scalar.isIntegral(/*includeBool=*/false)) {
*out_val = static_cast<bool>(scalar.to<int64_t>());
return true;
}
if (scalar.isBoolean()) {
*out_val = scalar.to<bool>();
return true;
}
return false;
}
#define ET_EXTRACT_SCALAR(scalar, out_val) \
ET_CHECK_MSG( \
extract_scalar(scalar, &out_val), \
#scalar " could not be extracted: wrong type or out of range");
template <typename Distribution>
TensorPtr random_strided(
std::vector<executorch::aten::SizesType> sizes,
std::vector<executorch::aten::StridesType> strides,
executorch::aten::ScalarType type,
executorch::aten::TensorShapeDynamism dynamism,
Distribution&& distribution) {
auto tensor =
empty_strided(std::move(sizes), std::move(strides), type, dynamism);
std::default_random_engine gen{std::random_device{}()};
ET_SWITCH_REALHBBF16_TYPES(type, nullptr, "random_strided", CTYPE, [&] {
std::generate_n(tensor->mutable_data_ptr<CTYPE>(), tensor->numel(), [&]() {
return static_cast<CTYPE>(distribution(gen));
});
});
return tensor;
}
} // namespace
TensorPtr empty_strided(
std::vector<executorch::aten::SizesType> sizes,
std::vector<executorch::aten::StridesType> strides,
executorch::aten::ScalarType type,
executorch::aten::TensorShapeDynamism dynamism) {
std::vector<uint8_t> data(
executorch::aten::compute_numel(sizes.data(), sizes.size()) *
executorch::aten::elementSize(type));
return make_tensor_ptr(
std::move(sizes),
std::move(data),
{},
std::move(strides),
type,
dynamism);
}
TensorPtr full_strided(
std::vector<executorch::aten::SizesType> sizes,
std::vector<executorch::aten::StridesType> strides,
executorch::aten::Scalar fill_value,
executorch::aten::ScalarType type,
executorch::aten::TensorShapeDynamism dynamism) {
auto tensor =
empty_strided(std::move(sizes), std::move(strides), type, dynamism);
ET_SWITCH_REALHBBF16_TYPES(type, nullptr, "full_strided", CTYPE, [&] {
CTYPE value;
ET_EXTRACT_SCALAR(fill_value, value);
std::fill(
tensor->mutable_data_ptr<CTYPE>(),
tensor->mutable_data_ptr<CTYPE>() + tensor->numel(),
value);
});
return tensor;
}
TensorPtr rand_strided(
std::vector<executorch::aten::SizesType> sizes,
std::vector<executorch::aten::StridesType> strides,
executorch::aten::ScalarType type,
executorch::aten::TensorShapeDynamism dynamism) {
auto upper_bound = 1.0f;
// Adjusts the upper bound to prevent rounding to 1.0 when converting to
// lower-precision types.
if (type == executorch::aten::ScalarType::Half) {
upper_bound -=
float(std::numeric_limits<executorch::aten::Half>::epsilon()) / 2;
} else if (type == executorch::aten::ScalarType::BFloat16) {
upper_bound -=
float(std::numeric_limits<executorch::aten::BFloat16>::epsilon()) / 2;
}
return random_strided(
std::move(sizes),
std::move(strides),
type,
dynamism,
std::uniform_real_distribution<float>(0.0f, upper_bound));
}
TensorPtr randn_strided(
std::vector<executorch::aten::SizesType> sizes,
std::vector<executorch::aten::StridesType> strides,
executorch::aten::ScalarType type,
executorch::aten::TensorShapeDynamism dynamism) {
return random_strided(
std::move(sizes),
std::move(strides),
type,
dynamism,
std::normal_distribution<float>(0.0f, 1.0f));
}
TensorPtr randint_strided(
int64_t low,
int64_t high,
std::vector<executorch::aten::SizesType> sizes,
std::vector<executorch::aten::StridesType> strides,
executorch::aten::ScalarType type,
executorch::aten::TensorShapeDynamism dynamism) {
return random_strided(
std::move(sizes),
std::move(strides),
type,
dynamism,
std::uniform_int_distribution<int64_t>(low, high - 1));
}
} // namespace extension
} // namespace executorch