|
10 | 10 |
|
11 | 11 | #include <executorch/runtime/core/exec_aten/exec_aten.h> |
12 | 12 | #include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> |
| 13 | +#include <executorch/runtime/core/result.h> |
13 | 14 | #include <executorch/runtime/core/span.h> |
14 | 15 |
|
15 | 16 | namespace executorch { |
16 | 17 | namespace runtime { |
17 | 18 |
|
18 | | -namespace { |
19 | | -size_t calculate_nbytes( |
20 | | - const Span<const int32_t>& sizes, |
21 | | - const exec_aten::ScalarType& scalar_type) { |
22 | | - ssize_t n = 1; |
23 | | - for (ssize_t i = 0; i < sizes.size(); i++) { |
24 | | - ET_CHECK(sizes[i] >= 0); |
25 | | - n *= sizes[i]; |
26 | | - } |
27 | | - // Use the full namespace to disambiguate from c10::elementSize. |
28 | | - return n * executorch::runtime::elementSize(scalar_type); |
29 | | -} |
30 | | -} // namespace |
31 | | - |
32 | 19 | /** |
33 | | - * Metadata describing the layout of external tensors (tensors that are not |
34 | | - stored in the PTE file). |
35 | | - * |
36 | | - * The NamedDataMap used to create the TensorLayout must outlive the |
37 | | - TensorLayout. |
| 20 | + * Describes the layout of a tensor. |
38 | 21 | */ |
39 | | -class TensorLayout { |
| 22 | +class ET_EXPERIMENTAL TensorLayout final { |
40 | 23 | public: |
41 | 24 | TensorLayout( |
42 | | - executorch::aten::ScalarType scalar_type, |
43 | 25 | Span<const int32_t> sizes, |
44 | | - Span<const uint8_t> dim_order) |
| 26 | + Span<const uint8_t> dim_order, |
| 27 | + executorch::aten::ScalarType scalar_type, |
| 28 | + size_t nbytes) |
45 | 29 | : sizes_(sizes), |
46 | 30 | dim_order_(dim_order), |
47 | 31 | scalar_type_(scalar_type), |
48 | | - nbytes_(calculate_nbytes(sizes_, scalar_type_)) {} |
| 32 | + nbytes_(nbytes) {} |
49 | 33 |
|
50 | | - TensorLayout(const TensorLayout&) = default; |
51 | | - TensorLayout(TensorLayout&&) = default; |
52 | | - TensorLayout& operator=(const TensorLayout&) = default; |
53 | | - TensorLayout& operator=(TensorLayout&& other) = default; |
54 | | - ~TensorLayout() = default; |
| 34 | + /** Creates a TensorLayout from the given parameters. |
| 35 | + * |
| 36 | + * @param[in] sizes The sizes of the tensor. Note: the span passed here must |
| 37 | + * outlive the TensorLayout and all copies of it. |
| 38 | + * @param[in] dim_order The dim order of the tensor. Note: the span passed |
| 39 | + * here must outlive the TensorLayout and all copies of it. |
| 40 | + * @param[in] scalar_type The scalar type of the tensor. |
| 41 | + * @return A Result containing the TensorLayout on success, or an error. |
| 42 | + */ |
| 43 | + static executorch::runtime::Result<TensorLayout> create( |
| 44 | + Span<const int32_t> sizes, |
| 45 | + Span<const uint8_t> dim_order, |
| 46 | + executorch::aten::ScalarType scalar_type); |
55 | 47 |
|
56 | | - /// Returns the sizes of the tensor. |
| 48 | + /** |
| 49 | + * Returns the sizes of the tensor. |
| 50 | + * |
| 51 | + * NOTE: The TensorLayout must outlive the spans returned here. |
| 52 | + */ |
57 | 53 | Span<const int32_t> sizes() const { |
58 | 54 | return sizes_; |
59 | 55 | } |
60 | 56 |
|
61 | | - /// Returns the dim order of the tensor. |
| 57 | + /** |
| 58 | + * Returns the dim order of the tensor. |
| 59 | + * |
| 60 | + * NOTE: The TensorLayout must outlive the spans returned here. |
| 61 | + */ |
62 | 62 | Span<const uint8_t> dim_order() const { |
63 | 63 | return dim_order_; |
64 | 64 | } |
|
0 commit comments