forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor_ptr.cpp
More file actions
252 lines (234 loc) · 8.28 KB
/
tensor_ptr.cpp
File metadata and controls
252 lines (234 loc) · 8.28 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* 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.h>
#include <numeric>
#include <executorch/runtime/core/exec_aten/util/tensor_util.h>
namespace executorch {
namespace extension {
namespace {
#ifndef USE_ATEN_LIB
/**
* A structure that consolidates the metadata (sizes, dim_order, strides) and
* the data buffer associated with a Tensor. Since Tensor does not own
* the memory for these metadata arrays or the data itself, this structure
* ensures that they are managed together and have the same lifetime as the
* Tensor. When the Tensor is destroyed, the Storage structure ensures
* proper cleanup of the associated metadata and data if needed.
*/
struct Storage final {
executorch::aten::TensorImpl tensor_impl;
executorch::aten::Tensor tensor;
std::vector<executorch::aten::SizesType> sizes;
std::vector<executorch::aten::DimOrderType> dim_order;
std::vector<executorch::aten::StridesType> strides;
std::function<void(void*)> deleter;
Storage(
executorch::aten::TensorImpl&& tensor_impl,
std::vector<executorch::aten::SizesType>&& sizes,
std::vector<executorch::aten::DimOrderType>&& dim_order,
std::vector<executorch::aten::StridesType>&& strides,
std::function<void(void*)>&& deleter)
: tensor_impl(std::move(tensor_impl)),
tensor(&this->tensor_impl),
sizes(std::move(sizes)),
dim_order(std::move(dim_order)),
strides(std::move(strides)),
deleter(std::move(deleter)) {}
~Storage() {
if (deleter) {
deleter(tensor_impl.mutable_data());
}
}
};
#endif // USE_ATEN_LIB
} // namespace
TensorPtr make_tensor_ptr(
std::vector<executorch::aten::SizesType> sizes,
void* data,
std::vector<executorch::aten::DimOrderType> dim_order,
std::vector<executorch::aten::StridesType> strides,
executorch::aten::ScalarType type,
executorch::aten::TensorShapeDynamism dynamism,
std::function<void(void*)> deleter) {
const auto dim = sizes.size();
ET_CHECK_MSG(
dim_order.empty() || dim_order.size() == dim,
"dim_order size must match sizes or be empty.");
ET_CHECK_MSG(
strides.empty() || strides.size() == dim,
"strides size must match sizes or be empty.");
if (dim_order.empty()) {
dim_order.resize(dim);
std::iota(dim_order.begin(), dim_order.end(), 0);
if (!strides.empty()) {
std::sort(dim_order.begin(), dim_order.end(), [&](size_t a, size_t b) {
return strides[a] > strides[b];
});
}
}
std::vector<executorch::aten::StridesType> computed_strides(dim);
auto error = runtime::dim_order_to_stride(
sizes.data(), dim_order.data(), dim, computed_strides.data());
ET_CHECK_MSG(error == runtime::Error::Ok, "Failed to compute strides.");
if (!strides.empty()) {
for (size_t i = 0; i < dim; i++) {
ET_CHECK_MSG(
strides[i] == computed_strides[i] || sizes[i] == 1,
"invalid strides for dim %zu: %" ET_PRI_SIZES_AND_STRIDES
"!= %" ET_PRI_SIZES_AND_STRIDES
" while its size is %" ET_PRI_SIZES_AND_STRIDES " != 1",
i,
strides[i],
computed_strides[i],
sizes[i]);
}
}
strides = std::move(computed_strides);
#ifndef USE_ATEN_LIB
executorch::aten::TensorImpl tensor_impl(
type,
dim,
sizes.data(),
data,
dim_order.data(),
strides.data(),
dim > 0 ? dynamism : executorch::aten::TensorShapeDynamism::STATIC);
auto storage = std::make_shared<Storage>(
std::move(tensor_impl),
std::move(sizes),
std::move(dim_order),
std::move(strides),
std::move(deleter));
const auto tensor_ptr = &storage->tensor;
return std::shared_ptr<executorch::aten::Tensor>(
std::move(storage), tensor_ptr);
#else
auto options = c10::TensorOptions()
.dtype(c10::scalarTypeToTypeMeta(type))
.device(c10::kCPU);
auto storage = c10::Storage(
c10::Storage::use_byte_size_t(),
at::detail::computeStorageNbytes(
sizes, strides, options.dtype().itemsize()),
c10::InefficientStdFunctionContext::makeDataPtr(
data, std::move(deleter), options.device()),
nullptr,
false);
auto tensor_impl = c10::make_intrusive<executorch::aten::TensorImpl>(
std::move(storage),
c10::DispatchKeySet(c10::DispatchKey::CPU),
options.dtype());
tensor_impl->set_sizes_and_strides(sizes, strides);
return std::make_shared<executorch::aten::Tensor>(std::move(tensor_impl));
#endif // USE_ATEN_LIB
}
TensorPtr make_tensor_ptr(
std::vector<executorch::aten::SizesType> sizes,
std::vector<uint8_t> data,
std::vector<executorch::aten::DimOrderType> dim_order,
std::vector<executorch::aten::StridesType> strides,
executorch::aten::ScalarType type,
executorch::aten::TensorShapeDynamism dynamism) {
ET_CHECK_MSG(
data.size() ==
executorch::aten::compute_numel(sizes.data(), sizes.size()) *
executorch::aten::elementSize(type),
"Data size does not match tensor size.");
auto data_ptr = data.data();
return make_tensor_ptr(
std::move(sizes),
data_ptr,
std::move(dim_order),
std::move(strides),
type,
dynamism,
// Data is moved into the deleter and is destroyed together with Storage.
[data = std::move(data)](void*) {});
}
TensorPtr clone_tensor_ptr(
const executorch::aten::Tensor& tensor,
executorch::aten::ScalarType type) {
std::vector<executorch::aten::SizesType> sizes(
tensor.sizes().begin(), tensor.sizes().end());
std::vector<executorch::aten::DimOrderType> dim_order{
#ifndef USE_ATEN_LIB
tensor.dim_order().begin(), tensor.dim_order().end()
#endif // USE_ATEN_LIB
};
std::vector<executorch::aten::StridesType> strides(
tensor.strides().begin(), tensor.strides().end());
auto dynamism = executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND;
#ifndef USE_ATEN_LIB
dynamism = tensor.shape_dynamism();
#endif // USE_ATEN_LIB
const auto* tensor_data = tensor.const_data_ptr();
if (!tensor_data) {
return make_tensor_ptr(
std::move(sizes),
nullptr,
std::move(dim_order),
std::move(strides),
type,
dynamism);
}
const auto tensor_type = tensor.scalar_type();
if (tensor_type == type) {
return make_tensor_ptr(
std::move(sizes),
std::vector<uint8_t>(
(uint8_t*)tensor_data, (uint8_t*)tensor_data + tensor.nbytes()),
std::move(dim_order),
std::move(strides),
tensor_type,
dynamism);
}
ET_CHECK_MSG(
runtime::canCast(tensor_type, type),
"Cannot cast tensor type to desired type.");
const auto tensor_numel = static_cast<size_t>(tensor.numel());
std::vector<uint8_t> data(tensor_numel * aten::elementSize(type));
// Create a minimal context for error handling in ET_SWITCH
struct {
[[noreturn]] void fail(torch::executor::Error /* error */) {
ET_CHECK_MSG(false, "Unsupported dtype in clone_tensor_ptr");
}
} ctx;
ET_SWITCH_REALHBBF16_AND_UINT_TYPES(
tensor_type, ctx, "clone_tensor_ptr_from", CTYPE_FROM, [&] {
const CTYPE_FROM* tensor_data_ptr =
static_cast<const CTYPE_FROM*>(tensor_data);
ET_SWITCH_REALHBBF16_AND_UINT_TYPES(
type, ctx, "clone_tensor_ptr_to", CTYPE_TO, [&] {
CTYPE_TO* data_ptr = reinterpret_cast<CTYPE_TO*>(data.data());
std::transform(
tensor_data_ptr,
tensor_data_ptr + tensor_numel,
data_ptr,
[](const CTYPE_FROM& val) {
return static_cast<CTYPE_TO>(val);
});
});
});
return make_tensor_ptr(
std::move(sizes),
std::move(data),
std::move(dim_order),
std::move(strides),
type,
dynamism);
}
runtime::Error resize_tensor_ptr(
TensorPtr& tensor,
const std::vector<executorch::aten::SizesType>& sizes) {
return ET_RUNTIME_NAMESPACE::resize_tensor(
*tensor,
executorch::aten::ArrayRef<executorch::aten::SizesType>(
sizes.data(), sizes.size()));
}
} // namespace extension
} // namespace executorch