|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 12 | +#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> |
| 13 | +#include <executorch/runtime/core/span.h> |
| 14 | + |
| 15 | +namespace executorch { |
| 16 | +namespace runtime { |
| 17 | + |
| 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 | +/** |
| 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. |
| 38 | + */ |
| 39 | +class TensorLayout { |
| 40 | + 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_)) {} |
| 49 | + |
| 50 | + TensorLayout(const TensorLayout&) = default; |
| 51 | + TensorLayout(TensorLayout&&) = default; |
| 52 | + TensorLayout& operator=(const TensorLayout&) = default; |
| 53 | + TensorLayout& operator=(TensorLayout&& other) = default; |
| 54 | + ~TensorLayout() = default; |
| 55 | + |
| 56 | + /// Returns the sizes of the tensor. |
| 57 | + Span<const int32_t> sizes() const { |
| 58 | + return sizes_; |
| 59 | + } |
| 60 | + |
| 61 | + /// Returns the dim order of the tensor. |
| 62 | + Span<const uint8_t> dim_order() const { |
| 63 | + return dim_order_; |
| 64 | + } |
| 65 | + |
| 66 | + /// Returns the scalar type of the tensor. |
| 67 | + executorch::aten::ScalarType scalar_type() const { |
| 68 | + return scalar_type_; |
| 69 | + } |
| 70 | + |
| 71 | + /// Returns the size of the tensor in bytes. |
| 72 | + size_t nbytes() const { |
| 73 | + return nbytes_; |
| 74 | + } |
| 75 | + |
| 76 | + private: |
| 77 | + /// The sizes of the tensor. |
| 78 | + Span<const int32_t> sizes_; |
| 79 | + |
| 80 | + /// The dim order of the tensor. |
| 81 | + Span<const uint8_t> dim_order_; |
| 82 | + |
| 83 | + /// The scalar type of the tensor. |
| 84 | + executorch::aten::ScalarType scalar_type_; |
| 85 | + |
| 86 | + /// The size in bytes of the tensor. |
| 87 | + size_t nbytes_; |
| 88 | +}; |
| 89 | + |
| 90 | +} // namespace runtime |
| 91 | +} // namespace executorch |
0 commit comments