1010
1111#include < executorch/runtime/core/exec_aten/exec_aten.h>
1212#include < executorch/runtime/core/exec_aten/util/scalar_type_util.h>
13+ #include < executorch/runtime/core/result.h>
1314#include < executorch/runtime/core/span.h>
1415
1516namespace executorch {
1617namespace runtime {
1718
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-
3219/* *
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.
3821 */
39- class TensorLayout {
22+ class ET_EXPERIMENTAL TensorLayout final {
4023 public:
41- TensorLayout (
42- executorch::aten::ScalarType scalar_type,
43- Span<const int32_t > sizes,
44- Span<const uint8_t > dim_order)
45- : sizes_(sizes),
46- dim_order_ (dim_order),
47- scalar_type_(scalar_type),
48- nbytes_(calculate_nbytes(sizes_, scalar_type_)) {}
24+ TensorLayout () = delete ;
4925
50- TensorLayout (const TensorLayout&) = default;
51- TensorLayout (TensorLayout&&) = default;
52- TensorLayout& operator =(const TensorLayout&) = default ;
53- TensorLayout& operator =(TensorLayout&& other) = default ;
54- ~TensorLayout () = default ;
26+ /* * Creates a TensorLayout from the given parameters.
27+ *
28+ * @param[in] sizes The sizes of the tensor. Note: the span passed here must
29+ * outlive the TensorLayout and all copies of it.
30+ * @param[in] dim_order The dim order of the tensor. Note: the span passed
31+ * here must outlive the TensorLayout and all copies of it.
32+ * @param[in] scalar_type The scalar type of the tensor.
33+ * @return A Result containing the TensorLayout on success, or an error.
34+ */
35+ static executorch::runtime::Result<TensorLayout> create (
36+ Span<const int32_t > sizes,
37+ Span<const uint8_t > dim_order,
38+ executorch::aten::ScalarType scalar_type);
5539
56- // / Returns the sizes of the tensor.
40+ /* *
41+ * Returns the sizes of the tensor.
42+ *
43+ * NOTE: The TensorLayout must outlive the spans returned here.
44+ */
5745 Span<const int32_t > sizes () const {
5846 return sizes_;
5947 }
6048
61- // / Returns the dim order of the tensor.
49+ /* *
50+ * Returns the dim order of the tensor.
51+ *
52+ * NOTE: The TensorLayout must outlive the spans returned here.
53+ */
6254 Span<const uint8_t > dim_order () const {
6355 return dim_order_;
6456 }
@@ -74,6 +66,15 @@ class TensorLayout {
7466 }
7567
7668 private:
69+ TensorLayout (
70+ Span<const int32_t > sizes,
71+ Span<const uint8_t > dim_order,
72+ executorch::aten::ScalarType scalar_type,
73+ size_t nbytes)
74+ : sizes_(sizes),
75+ dim_order_ (dim_order),
76+ scalar_type_(scalar_type),
77+ nbytes_(nbytes) {}
7778 // / The sizes of the tensor.
7879 Span<const int32_t > sizes_;
7980
0 commit comments