-
Notifications
You must be signed in to change notification settings - Fork 527
/
Copy pathtensor_ptr.h
378 lines (359 loc) · 15.2 KB
/
tensor_ptr.h
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* 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.
*/
#pragma once
#include <algorithm>
#include <functional>
#include <memory>
#include <vector>
#include <executorch/runtime/core/error.h>
#include <executorch/runtime/core/exec_aten/exec_aten.h>
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
namespace executorch {
namespace extension {
/**
* A smart pointer type for managing the lifecycle of a Tensor.
*/
using TensorPtr = std::shared_ptr<executorch::aten::Tensor>;
/**
* Creates a TensorPtr that manages a Tensor with the specified properties.
*
* @param sizes A vector specifying the size of each dimension.
* @param data A pointer to the data buffer.
* @param dim_order A vector specifying the order of dimensions.
* @param strides A vector specifying the strides of the tensor.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies the mutability of the tensor's shape.
* @param deleter A custom deleter function for managing the lifetime of the
* data buffer. If provided, this deleter will be called when the managed Tensor
* object is destroyed.
* @return A TensorPtr that manages the newly created Tensor.
*/
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,
const executorch::aten::ScalarType type =
executorch::aten::ScalarType::Float,
const executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND,
std::function<void(void*)> deleter = nullptr);
/**
* Creates a TensorPtr that manages a Tensor with the specified properties.
*
* @param sizes A vector specifying the size of each dimension.
* @param data A pointer to the data buffer.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies the mutability of the tensor's shape.
* @param deleter A custom deleter function for managing the lifetime of the
* data buffer. If provided, this deleter will be called when the managed Tensor
* object is destroyed.
* @return A TensorPtr that manages the newly created Tensor.
*/
inline TensorPtr make_tensor_ptr(
std::vector<executorch::aten::SizesType> sizes,
void* data,
const executorch::aten::ScalarType type =
executorch::aten::ScalarType::Float,
const executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND,
std::function<void(void*)> deleter = nullptr) {
return make_tensor_ptr(
std::move(sizes), data, {}, {}, type, dynamism, std::move(deleter));
}
/**
* Creates a TensorPtr that manages a Tensor with the specified properties.
*
* This template overload is specialized for cases where the tensor data is
* provided as a vector. The scalar type is automatically deduced from the
* vector's data type. If the specified `type` differs from the deduced type of
* the vector's elements, and casting is allowed, the data will be cast to the
* specified `type`. This allows for flexible creation of tensors with data
* vectors of one type and a different scalar type.
*
* @tparam T The C++ type of the tensor elements, deduced from the vector.
* @param sizes A vector specifying the size of each dimension.
* @param data A vector containing the tensor's data.
* @param dim_order A vector specifying the order of dimensions.
* @param strides A vector specifying the strides of each dimension.
* @param type The scalar type of the tensor elements. If it differs from the
* deduced type, the data will be cast to this type if allowed.
* @param dynamism Specifies the mutability of the tensor's shape.
* @return A TensorPtr that manages the newly created TensorImpl.
*/
template <
typename T = float,
executorch::aten::ScalarType deduced_type =
runtime::CppTypeToScalarType<T>::value>
inline TensorPtr make_tensor_ptr(
std::vector<executorch::aten::SizesType> sizes,
std::vector<T> data,
std::vector<executorch::aten::DimOrderType> dim_order = {},
std::vector<executorch::aten::StridesType> strides = {},
executorch::aten::ScalarType type = deduced_type,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
if (type != deduced_type) {
ET_CHECK_MSG(
runtime::canCast(deduced_type, type),
"Cannot cast deduced type to specified type.");
std::vector<uint8_t> casted_data(data.size() * runtime::elementSize(type));
ET_SWITCH_REALHBBF16_TYPES(type, nullptr, "make_tensor_ptr", CTYPE, [&] {
std::transform(
data.begin(),
data.end(),
reinterpret_cast<CTYPE*>(casted_data.data()),
[](const T& val) { return static_cast<CTYPE>(val); });
});
const auto raw_data_ptr = casted_data.data();
auto data_ptr =
std::make_shared<std::vector<uint8_t>>(std::move(casted_data));
return make_tensor_ptr(
std::move(sizes),
raw_data_ptr,
std::move(dim_order),
std::move(strides),
type,
dynamism,
[data_ptr = std::move(data_ptr)](void*) {});
}
const auto raw_data_ptr = data.data();
auto data_ptr = std::make_shared<std::vector<T>>(std::move(data));
return make_tensor_ptr(
std::move(sizes),
raw_data_ptr,
std::move(dim_order),
std::move(strides),
type,
dynamism,
[data_ptr = std::move(data_ptr)](void*) {});
}
/**
* Creates a TensorPtr that manages a Tensor with the specified properties.
*
* This template overload is specialized for cases where the tensor data is
* provided as a vector. The scalar type is automatically deduced from the
* vector's data type. If the specified `type` differs from the deduced type of
* the vector's elements, and casting is allowed, the data will be cast to the
* specified `type`. This allows for flexible creation of tensors with data
* vectors of one type and a different scalar type.
*
* @tparam T The C++ type of the tensor elements, deduced from the vector.
* @param data A vector containing the tensor's data.
* @param type The scalar type of the tensor elements. If it differs from the
* deduced type, the data will be cast to this type if allowed.
* @param dynamism Specifies the mutability of the tensor's shape.
* @return A TensorPtr that manages the newly created TensorImpl.
*/
template <
typename T = float,
executorch::aten::ScalarType deduced_type =
runtime::CppTypeToScalarType<T>::value>
inline TensorPtr make_tensor_ptr(
std::vector<T> data,
executorch::aten::ScalarType type = deduced_type,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
std::vector<executorch::aten::SizesType> sizes{
executorch::aten::SizesType(data.size())};
return make_tensor_ptr(
std::move(sizes), std::move(data), {0}, {1}, type, dynamism);
}
/**
* Creates a TensorPtr that manages a Tensor with the specified properties.
*
* This template overload is specialized for cases where the tensor data is
* provided as an initializer list. The scalar type is automatically deduced
* from the initializer list's data type. If the specified `type` differs from
* the deduced type of the initializer list's elements, and casting is allowed,
* the data will be cast to the specified `type`. This allows for flexible
* creation of tensors with data vectors of one type and a different scalar
* type.
*
* @tparam T The C++ type of the tensor elements, deduced from the initializer
* list.
* @param sizes A vector specifying the size of each dimension.
* @param list An initializer list containing the tensor's data.
* @param dim_order A vector specifying the order of dimensions.
* @param strides A vector specifying the strides of each dimension.
* @param type The scalar type of the tensor elements. If it differs from the
* deduced type, the data will be cast to this type if allowed.
* @param dynamism Specifies the mutability of the tensor's shape.
* @return A TensorPtr that manages the newly created TensorImpl.
*/
template <
typename T = float,
executorch::aten::ScalarType deduced_type =
runtime::CppTypeToScalarType<T>::value>
inline TensorPtr make_tensor_ptr(
std::vector<executorch::aten::SizesType> sizes,
std::initializer_list<T> list,
std::vector<executorch::aten::DimOrderType> dim_order = {},
std::vector<executorch::aten::StridesType> strides = {},
executorch::aten::ScalarType type = deduced_type,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return make_tensor_ptr(
std::move(sizes),
std::vector<T>(std::move(list)),
std::move(dim_order),
std::move(strides),
type,
dynamism);
}
/**
* Creates a TensorPtr that manages a Tensor with the specified properties.
*
* This template overload allows creating a Tensor from an initializer list
* of data. The scalar type is automatically deduced from the type of the
* initializer list's elements. If the specified `type` differs from
* the deduced type of the initializer list's elements, and casting is allowed,
* the data will be cast to the specified `type`. This allows for flexible
* creation of tensors with data vectors of one type and a different scalar
* type.
*
* @tparam T The C++ type of the tensor elements, deduced from the initializer
* list.
* @param list An initializer list containing the tensor's data.
* @param type The scalar type of the tensor elements. If it differs from the
* deduced type, the data will be cast to this type if allowed.
* @param dynamism Specifies the mutability of the tensor's shape.
* @return A TensorPtr that manages the newly created TensorImpl.
*/
template <
typename T = float,
executorch::aten::ScalarType deduced_type =
runtime::CppTypeToScalarType<T>::value>
inline TensorPtr make_tensor_ptr(
std::initializer_list<T> list,
executorch::aten::ScalarType type = deduced_type,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
std::vector<executorch::aten::SizesType> sizes{
executorch::aten::SizesType(list.size())};
return make_tensor_ptr(
std::move(sizes), std::move(list), {0}, {1}, type, dynamism);
}
/**
* Creates a TensorPtr that manages a Tensor with a single scalar value.
*
* @tparam T The C++ type of the scalar value.
* @param value The scalar value to be used for the Tensor.
* @return A TensorPtr that manages the newly created TensorImpl.
*/
template <typename T>
inline TensorPtr make_tensor_ptr(T value) {
return make_tensor_ptr({}, std::vector<T>{value});
}
/**
* Creates a TensorPtr that manages a Tensor with the specified properties.
*
* This overload accepts a raw memory buffer stored in a std::vector<uint8_t>
* and a scalar type to interpret the data. The vector is managed, and the
* memory's lifetime is tied to the TensorImpl.
*
* @param sizes A vector specifying the size of each dimension.
* @param data A vector containing the raw memory for the tensor's data.
* @param dim_order A vector specifying the order of dimensions.
* @param strides A vector specifying the strides of each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies the mutability of the tensor's shape.
* @return A TensorPtr managing the newly created Tensor.
*/
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::ScalarType::Float,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND);
/**
* Creates a TensorPtr that manages a Tensor with the specified properties.
*
* This overload accepts a raw memory buffer stored in a std::vector<uint8_t>
* and a scalar type to interpret the data. The vector is managed, and the
* memory's lifetime is tied to the TensorImpl.
*
* @param sizes A vector specifying the size of each dimension.
* @param data A vector containing the raw memory for the tensor's data.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies the mutability of the tensor's shape.
* @return A TensorPtr managing the newly created Tensor.
*/
inline TensorPtr make_tensor_ptr(
std::vector<executorch::aten::SizesType> sizes,
std::vector<uint8_t> data,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Float,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return make_tensor_ptr(
std::move(sizes), std::move(data), {}, {}, type, dynamism);
}
/**
* Creates a TensorPtr to manage a new Tensor with the same properties
* as the given Tensor, sharing the same data without owning it.
*
* @param tensor The Tensor whose properties are used to create a new TensorPtr.
* @return A new TensorPtr managing a Tensor with the same properties as the
* original.
*/
inline TensorPtr make_tensor_ptr(const executorch::aten::Tensor& tensor) {
return make_tensor_ptr(
std::vector<executorch::aten::SizesType>(
tensor.sizes().begin(), tensor.sizes().end()),
tensor.mutable_data_ptr(),
#ifndef USE_ATEN_LIB
std::vector<executorch::aten::DimOrderType>(
tensor.dim_order().begin(), tensor.dim_order().end()),
std::vector<executorch::aten::StridesType>(
tensor.strides().begin(), tensor.strides().end()),
tensor.scalar_type(),
tensor.shape_dynamism()
#else // USE_ATEN_LIB
{},
std::vector<executorch::aten::StridesType>(
tensor.strides().begin(), tensor.strides().end()),
tensor.scalar_type()
#endif // USE_ATEN_LIB
);
}
/**
* Creates a TensorPtr that manages a new Tensor with the same properties
* as the given Tensor, but with a copy of the data owned by the returned
* TensorPtr, or nullptr if the original data is null.
*
* @param tensor The Tensor to clone.
* @return A new TensorPtr that manages a Tensor with the same properties as the
* original but with copied data.
*/
TensorPtr clone_tensor_ptr(const executorch::aten::Tensor& tensor);
/**
* Creates a new TensorPtr by cloning the given TensorPtr, copying the
* underlying data.
*
* @param tensor The TensorPtr to clone.
* @return A new TensorPtr that manages a Tensor with the same properties as the
* original but with copied data.
*/
inline TensorPtr clone_tensor_ptr(const TensorPtr& tensor) {
return clone_tensor_ptr(*tensor);
}
/**
* Resizes the Tensor managed by the provided TensorPtr to the new sizes.
*
* @param tensor A TensorPtr managing the Tensor to resize.
* @param sizes A vector representing the new sizes for each dimension.
* @return Error::Ok on success, or an appropriate error code on failure.
*/
ET_NODISCARD
runtime::Error resize_tensor_ptr(
TensorPtr& tensor,
const std::vector<executorch::aten::SizesType>& sizes);
} // namespace extension
} // namespace executorch