forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor_ptr_maker.h
More file actions
687 lines (647 loc) · 26.3 KB
/
tensor_ptr_maker.h
File metadata and controls
687 lines (647 loc) · 26.3 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/*
* 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 <executorch/extension/tensor/tensor_ptr.h>
namespace executorch {
namespace extension {
/**
* A helper class for creating TensorPtr instances from raw data and tensor
* properties. Note that the TensorPtr created by this class does not own the
* data, so the data must outlive the TensorPtr.
*
* TensorPtrMaker provides a fluent interface for specifying various tensor
* properties, such as type, sizes, data pointer, dimension order, strides, and
* shape dynamism. The final tensor is created by invoking make_tensor_ptr() or
* by converting TensorPtrMaker to TensorPtr.
*/
class TensorPtrMaker final {
public:
// This class may have non-copyable members in the future.
TensorPtrMaker(const TensorPtrMaker&) = delete;
TensorPtrMaker& operator=(const TensorPtrMaker&) = delete;
// But it is movable.
TensorPtrMaker(TensorPtrMaker&&) = default;
TensorPtrMaker& operator=(TensorPtrMaker&&) = default;
/**
* Sets the scalar type of the tensor elements.
*
* @param type The scalar type (e.g., float, int, bool).
* @return Rvalue to this TensorPtrMaker for method chaining.
*/
TensorPtrMaker&& type(executorch::aten::ScalarType type) {
type_ = type;
return std::move(*this);
}
/**
* Sets the order of dimensions in memory.
*
* @param dim_order A vector specifying the dimension order.
* @return Rvalue to this TensorPtrMaker for method chaining.
*/
TensorPtrMaker&& dim_order(
std::vector<executorch::aten::DimOrderType> dim_order) {
dim_order_ = std::move(dim_order);
return std::move(*this);
}
/**
* Sets the strides for each dimension of the tensor.
*
* @param strides A vector specifying the stride for each dimension.
* @return Rvalue to this TensorPtrMaker for method chaining.
*/
TensorPtrMaker&& strides(std::vector<executorch::aten::StridesType> strides) {
strides_ = std::move(strides);
return std::move(*this);
}
/**
* Sets the shape dynamism of the tensor.
*
* @param dynamism Specifies whether the tensor's shape is static, dynamic, or
* bounded.
* @return Rvalue to this TensorPtrMaker for method chaining.
*/
TensorPtrMaker&& dynamism(executorch::aten::TensorShapeDynamism dynamism) {
dynamism_ = dynamism;
return std::move(*this);
}
/**
* Sets a custom deleter function to manage the lifetime of the data buffer.
*
* @param deleter A function that will be called to delete the data buffer
* when the Tensor object managed by the TensorPtr is destroyed. Explicitly
* consuming an rvalue to avoid unnecessary copies when the deleter is a
* lambda that has captured some state.
* @return Rvalue to this TensorPtrMaker for method chaining.
*/
TensorPtrMaker&& deleter(std::function<void(void*)>&& deleter) {
deleter_ = std::move(deleter);
return std::move(*this);
}
/**
* Creates and returns a TensorPtr instance using the properties set in this
* TensorPtrMaker.
*
* @return A TensorPtr instance that manages the newly created Tensor.
*/
TensorPtr make_tensor_ptr() && {
return ::executorch::extension::make_tensor_ptr(
std::move(sizes_),
data_,
std::move(dim_order_),
std::move(strides_),
type_,
dynamism_,
std::move(deleter_));
}
/**
* Implicit conversion operator to create a TensorPtr.
*
* @return A TensorPtr instance that manages the newly created Tensor.
*/
operator TensorPtr() && {
return std::move(*this).make_tensor_ptr();
}
private:
TensorPtrMaker(
void* data,
std::vector<executorch::aten::SizesType> sizes,
executorch::aten::ScalarType type)
: sizes_(std::move(sizes)), data_(data), type_(type) {}
private:
// The following properties are required to create a Tensor.
friend TensorPtrMaker for_blob(
void* data,
std::vector<executorch::aten::SizesType> sizes,
executorch::aten::ScalarType type);
private:
std::vector<executorch::aten::SizesType> sizes_;
std::vector<executorch::aten::StridesType> strides_;
std::vector<executorch::aten::DimOrderType> dim_order_;
std::function<void(void*)> deleter_ = nullptr;
void* data_ = nullptr;
executorch::aten::ScalarType type_ = executorch::aten::ScalarType::Float;
executorch::aten::TensorShapeDynamism dynamism_ =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND;
};
/**
* Creates a TensorPtrMaker instance for building a TensorPtr from a raw data
* pointer and tensor sizes.
*
* The TensorPtrMaker returned by this function allows for further customization
* of the tensor's properties, such as data type, dimension order, strides, and
* shape dynamism, before finalizing the TensorPtr creation.
*
* @param data A pointer to the raw data to be used by the tensor. It must
* outlive the TensorPtr created by this function.
* @param sizes A vector specifying the size of each dimension.
* @param type The scalar type of the tensor elements.
* @return A TensorPtrMaker instance for creating a TensorPtr.
*/
inline TensorPtrMaker for_blob(
void* data,
std::vector<executorch::aten::SizesType> sizes,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Float) {
return TensorPtrMaker(data, std::move(sizes), type);
}
/**
* Creates a TensorPtr from a raw data pointer and tensor sizes, with an
* optional dynamism setting.
*
* This function provides a convenient way to create a tensor from existing
* data, with the option to specify whether the tensor's shape is static or
* dynamic.
*
* @param data A pointer to the raw data used by the tensor. The data must
* outlive the TensorPtr created by this function.
* @param sizes A vector specifying the size of each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr from_blob(
void* data,
std::vector<executorch::aten::SizesType> sizes,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Float,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return for_blob(data, std::move(sizes), type)
.dynamism(dynamism)
.make_tensor_ptr();
}
/**
* Creates a TensorPtr from a raw data pointer, tensor sizes, and strides, with
* an optional dynamism setting.
*
* This function allows for the creation of a tensor from existing data, with
* the option to specify custom strides for each dimension and whether the
* tensor’s shape is static, dynamic, or bounded.
*
* @param data A pointer to the raw data used by the tensor. The data must
* outlive the TensorPtr created by this function.
* @param sizes A vector specifying the size of each dimension.
* @param strides A vector specifying the stride for each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static, dynamic, or
* bounded.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr from_blob(
void* data,
std::vector<executorch::aten::SizesType> sizes,
std::vector<executorch::aten::StridesType> strides,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Float,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return for_blob(data, std::move(sizes), type)
.strides(std::move(strides))
.dynamism(dynamism)
.make_tensor_ptr();
}
/**
* Creates a TensorPtr from a raw data pointer and tensor sizes, with an
* optional dynamism setting.
*
* This function is a convenient way to create a tensor from existing data, with
* the option to specify whether the tensor's shape is static, dynamic, or
* bounded.
*
* @param data A pointer to the raw data to be used by the tensor. It must
* outlive the TensorPtr created by this function.
* @param sizes A vector specifying the size of each dimension.
* @param type The scalar type of the tensor elements.
* @param deleter A function to delete the data when it's no longer needed.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance that manages the newly created Tensor.
*/
inline TensorPtr from_blob(
void* data,
std::vector<executorch::aten::SizesType> sizes,
executorch::aten::ScalarType type,
std::function<void(void*)>&& deleter,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return for_blob(data, std::move(sizes), type)
.deleter(std::move(deleter))
.dynamism(dynamism)
.make_tensor_ptr();
}
/**
* Creates a TensorPtr from a raw data pointer, tensor sizes, and strides, with
* an optional dynamism setting.
*
* This function allows for the creation of a tensor from existing data, with
* the option to specify custom strides for each dimension and whether the
* tensor's shape is static, dynamic, or bounded.
*
* @param data A pointer to the raw data to be used by the tensor. It must
* outlive the TensorPtr created by this function.
* @param sizes A vector specifying the size of each dimension.
* @param strides A vector specifying the stride for each dimension.
* @param type The scalar type of the tensor elements.
* @param deleter A function to delete the data when it's no longer needed.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance that manages the newly created Tensor.
*/
inline TensorPtr from_blob(
void* data,
std::vector<executorch::aten::SizesType> sizes,
std::vector<executorch::aten::StridesType> strides,
executorch::aten::ScalarType type,
std::function<void(void*)>&& deleter,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return for_blob(data, std::move(sizes), type)
.strides(std::move(strides))
.deleter(std::move(deleter))
.dynamism(dynamism)
.make_tensor_ptr();
}
/**
* Creates a TensorPtr with the specified sizes, strides, and properties.
*
* This function allocates memory for the tensor elements but does not
* initialize them with any specific values. The tensor is created with the
* specified strides.
*
* @param sizes A vector specifying the size of each dimension.
* @param strides A vector specifying the stride for each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
TensorPtr empty_strided(
std::vector<executorch::aten::SizesType> sizes,
std::vector<executorch::aten::StridesType> strides,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Float,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND);
/**
* Creates an empty TensorPtr with the same size and properties as the given
* tensor.
*
* This function allocates memory for the tensor elements but does not
* initialize them with any specific values.
*
* @param other A reference to another tensor, whose size and properties are
* used.
* @param type The scalar type of the tensor elements. If not provided, the
* scalar type of the other tensor is used.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr empty_like(
const TensorPtr& other,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Undefined,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
if (type == executorch::aten::ScalarType::Undefined) {
type = other->scalar_type();
}
return empty_strided(
{other->sizes().begin(), other->sizes().end()},
{other->strides().begin(), other->strides().end()},
type,
dynamism);
}
/**
* Creates an empty TensorPtr with the specified sizes and properties.
*
* This function allocates memory for the tensor elements but does not
* initialize them with any specific values.
*
* @param sizes A vector specifying the size of each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr empty(
std::vector<executorch::aten::SizesType> sizes,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Float,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return empty_strided(std::move(sizes), {}, type, dynamism);
}
/**
* Creates a TensorPtr filled with the specified value.
*
* @param sizes A vector specifying the size of each dimension.
* @param strides A vector specifying the stride for each dimension.
* @param fill_value The value to fill the tensor with.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
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::ScalarType::Float,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND);
/**
* Creates a TensorPtr filled with the specified value, with the same size and
* properties as another tensor.
*
* @param other A reference to another tensor, whose size and properties will be
* used.
* @param fill_value The value to fill the tensor with.
* @param type The scalar type of the tensor elements. If not specified, the
* scalar type of the other tensor is used.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr full_like(
const TensorPtr& other,
executorch::aten::Scalar fill_value,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Undefined,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
if (type == executorch::aten::ScalarType::Undefined) {
type = other->scalar_type();
}
return full_strided(
{other->sizes().begin(), other->sizes().end()},
{other->strides().begin(), other->strides().end()},
fill_value,
type,
dynamism);
}
/**
* Creates a TensorPtr filled with the specified value.
*
* @param sizes A vector specifying the size of each dimension.
* @param fill_value The value used to fill the tensor.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr full(
std::vector<executorch::aten::SizesType> sizes,
executorch::aten::Scalar fill_value,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Float,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return full_strided(std::move(sizes), {}, fill_value, type, dynamism);
}
/**
* Creates a TensorPtr holding a scalar value.
*
* @param value The scalar value for the tensor.
* @param type The scalar type of the tensor elements.
* @return A TensorPtr instance managing the newly created scalar Tensor.
*/
inline TensorPtr scalar_tensor(
executorch::aten::Scalar value,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Float) {
return full({}, value, type);
}
/**
* Creates a TensorPtr filled with ones, with the same size and properties as
* another tensor.
*
* @param other A reference to another tensor, whose size and properties are
* used.
* @param type The scalar type of the tensor elements. If not provided, the
* scalar type of the other tensor is used.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr ones_like(
const TensorPtr& other,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Undefined,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return full_like(other, 1, type, dynamism);
}
/**
* Creates a TensorPtr filled with ones.
*
* @param sizes A vector specifying the size of each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr ones(
std::vector<executorch::aten::SizesType> sizes,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Float,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return full(std::move(sizes), 1, type, dynamism);
}
/**
* Creates a TensorPtr filled with zeros, with the same size and properties as
* another tensor.
*
* @param other A reference to another tensor, whose size and properties will be
* used.
* @param type The scalar type of the tensor elements. If not specified, the
* scalar type of the `other` tensor is used.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr zeros_like(
const TensorPtr& other,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Undefined,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return full_like(other, 0, type, dynamism);
}
/**
* Creates a TensorPtr filled with zeros.
*
* @param sizes A vector specifying the size of each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr zeros(
std::vector<executorch::aten::SizesType> sizes,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Float,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return full(std::move(sizes), 0, type, dynamism);
}
/**
* Creates a TensorPtr filled with random values between 0 and 1.
*
* @param sizes A vector specifying the size of each dimension.
* @param strides A vector specifying the stride for each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
**/
TensorPtr rand_strided(
std::vector<executorch::aten::SizesType> sizes,
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 filled with random values between 0 and 1.
*
* @param other A reference to another tensor, whose size and properties will be
* used.
* @param type The scalar type of the tensor elements. If not specified, the
* scalar type of the other tensor is used.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr rand_like(
const TensorPtr& other,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Undefined,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
if (type == executorch::aten::ScalarType::Undefined) {
type = other->scalar_type();
}
return rand_strided(
{other->sizes().begin(), other->sizes().end()},
{other->strides().begin(), other->strides().end()},
type,
dynamism);
}
/**
* Creates a TensorPtr filled with random values between 0 and 1.
*
* @param sizes A vector specifying the size of each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr rand(
std::vector<executorch::aten::SizesType> sizes,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Float,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return rand_strided(std::move(sizes), {}, type, dynamism);
}
/**
* Creates a TensorPtr filled with random values from a normal distribution.
*
* @param sizes A vector specifying the size of each dimension.
* @param strides A vector specifying the stride for each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
TensorPtr randn_strided(
std::vector<executorch::aten::SizesType> sizes,
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 filled with random values from a normal distribution.
*
* @param other A reference to another tensor, whose size and properties will be
* used.
* @param type The scalar type of the tensor elements. If not specified, the
* scalar type of the other tensor is used.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr randn_like(
const TensorPtr& other,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Undefined,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
if (type == executorch::aten::ScalarType::Undefined) {
type = other->scalar_type();
}
return randn_strided(
{other->sizes().begin(), other->sizes().end()},
{other->strides().begin(), other->strides().end()},
type,
dynamism);
}
/**
* Creates a TensorPtr filled with random values from a normal distribution.
*
* @param sizes A vector specifying the size of each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr randn(
std::vector<executorch::aten::SizesType> sizes,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Float,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return randn_strided(std::move(sizes), {}, type, dynamism);
}
/**
* Creates a TensorPtr filled with random integer values in the given range.
*
* @param low The lower bound (inclusive) of the random values.
* @param high The upper bound (exclusive) of the random values.
* @param sizes A vector specifying the size of each dimension.
* @param strides A vector specifying the stride for each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
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::ScalarType::Int,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND);
/**
* Creates a TensorPtr filled with random integer values in the given range.
*
* @param other A reference to another tensor, whose size and properties will be
* used.
* @param low The lower bound (inclusive) of the random values.
* @param high The upper bound (exclusive) of the random values.
* @param type The scalar type of the tensor elements. If not specified, the
* scalar type of the other tensor is used.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr randint_like(
const TensorPtr& other,
int64_t low,
int64_t high,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Undefined,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
if (type == executorch::aten::ScalarType::Undefined) {
type = other->scalar_type();
}
return randint_strided(
low,
high,
{other->sizes().begin(), other->sizes().end()},
{other->strides().begin(), other->strides().end()},
type,
dynamism);
}
/**
* Creates a TensorPtr filled with random integer values within the specified
* range.
*
* @param low The inclusive lower bound of the random values.
* @param high The exclusive upper bound of the random values.
* @param sizes A vector specifying the size of each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr randint(
int64_t low,
int64_t high,
std::vector<executorch::aten::SizesType> sizes,
executorch::aten::ScalarType type = executorch::aten::ScalarType::Int,
executorch::aten::TensorShapeDynamism dynamism =
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return randint_strided(low, high, std::move(sizes), {}, type, dynamism);
}
} // namespace extension
} // namespace executorch