|
| 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/result.h> |
| 13 | +#include <executorch/runtime/core/span.h> |
| 14 | +#include <executorch/runtime/core/tag.h> |
| 15 | + |
| 16 | +// Forward declare flatbuffer types. This is a public header and must not |
| 17 | +// include the generated flatbuffer header. |
| 18 | +namespace executorch_flatbuffer { |
| 19 | +struct ExecutionPlan; |
| 20 | +} // namespace executorch_flatbuffer |
| 21 | + |
| 22 | +namespace torch { |
| 23 | +namespace executor { |
| 24 | + |
| 25 | +/// Metadata about a specific Tensor input/output of an Executorch Program |
| 26 | +/// The program used to create the MethodMeta object that created this |
| 27 | +/// TensorInfo must outlive this TensorInfo. |
| 28 | +class TensorInfo final { |
| 29 | + public: |
| 30 | + TensorInfo() noexcept = delete; |
| 31 | + TensorInfo(const TensorInfo&) noexcept = default; |
| 32 | + TensorInfo(TensorInfo&&) noexcept = default; |
| 33 | + TensorInfo& operator=(const TensorInfo&) noexcept = default; |
| 34 | + TensorInfo& operator=(TensorInfo&& other) noexcept = default; |
| 35 | + ~TensorInfo() noexcept = default; |
| 36 | + |
| 37 | + /** |
| 38 | + * Get the sizes of the input/output. |
| 39 | + * |
| 40 | + * @returns The sizes of the tensor. |
| 41 | + */ |
| 42 | + const Span<const int32_t>& sizes() const noexcept; |
| 43 | + |
| 44 | + /** |
| 45 | + * Get the dim order of the input/output. |
| 46 | + * |
| 47 | + * @returns The dim order of the tensor. |
| 48 | + */ |
| 49 | + const Span<const uint8_t>& dim_order() const noexcept; |
| 50 | + |
| 51 | + /** |
| 52 | + * Get the scalar type of the input/output. |
| 53 | + * |
| 54 | + * @returns The scalar type of the tensor. |
| 55 | + */ |
| 56 | + exec_aten::ScalarType scalar_type() const noexcept; |
| 57 | + |
| 58 | + /** |
| 59 | + * The size of the input/output in bytes. |
| 60 | + * |
| 61 | + * @returns The size of the tensor in bytes. |
| 62 | + */ |
| 63 | + size_t nbytes() const noexcept; |
| 64 | + |
| 65 | + private: |
| 66 | + // Let MethodMeta create IOMeta. |
| 67 | + friend class MethodMeta; |
| 68 | + |
| 69 | + TensorInfo( |
| 70 | + const Span<const int32_t>& sizes, |
| 71 | + const Span<const uint8_t>& dim_order, |
| 72 | + exec_aten::ScalarType scalar_type) noexcept; |
| 73 | + |
| 74 | + /// Sizes of the tensor. |
| 75 | + Span<const int32_t> sizes_; |
| 76 | + |
| 77 | + /// Dim order of the tensor. |
| 78 | + Span<const uint8_t> dim_order_; |
| 79 | + |
| 80 | + /// Scalar type of the tensor. |
| 81 | + exec_aten::ScalarType scalar_type_; |
| 82 | +}; |
| 83 | + |
| 84 | +/** |
| 85 | + * Manages metadata about a method in an Executorch program. |
| 86 | +
|
| 87 | + * The program used to create a MethodMeta object must outlive the MethodMeta. |
| 88 | + * Separate from Method so that this information can be accessed without paying |
| 89 | + * the initialization cost of loading the full Method. |
| 90 | + */ |
| 91 | +class MethodMeta final { |
| 92 | + public: |
| 93 | + MethodMeta() noexcept = delete; |
| 94 | + MethodMeta(const MethodMeta&) noexcept = default; |
| 95 | + MethodMeta(MethodMeta&&) noexcept = default; |
| 96 | + MethodMeta& operator=(const MethodMeta&) noexcept = default; |
| 97 | + MethodMeta& operator=(MethodMeta&& other) noexcept = default; |
| 98 | + ~MethodMeta() noexcept = default; |
| 99 | + |
| 100 | + /** |
| 101 | + * Get the name of this method. |
| 102 | + * |
| 103 | + * @returns The method name. |
| 104 | + */ |
| 105 | + const char* name() const noexcept; |
| 106 | + |
| 107 | + /** |
| 108 | + * Get the number of inputs to this method. |
| 109 | + * |
| 110 | + * @returns The number of inputs. |
| 111 | + */ |
| 112 | + size_t num_inputs() const noexcept; |
| 113 | + |
| 114 | + /** |
| 115 | + * Get the tag of the specified input. |
| 116 | + * |
| 117 | + * @param[in] index The index of the input to look up. |
| 118 | + * @returns The tag of input, can only be [Tensor, Int, Bool, Double, String]. |
| 119 | + */ |
| 120 | + Result<Tag> input_tag(size_t index) const noexcept; |
| 121 | + |
| 122 | + /** |
| 123 | + * Get metadata about the specified input. |
| 124 | + * |
| 125 | + * @param[in] index The index of the input to look up. |
| 126 | + * @returns The metadata on success, or an error on failure. Only valid for |
| 127 | + * tag::Tensor |
| 128 | + */ |
| 129 | + Result<TensorInfo> input_tensor_meta(size_t index) const noexcept; |
| 130 | + |
| 131 | + /** |
| 132 | + * Get the number of outputs to this method. |
| 133 | + * |
| 134 | + * @returns The number of outputs. |
| 135 | + */ |
| 136 | + size_t num_outputs() const noexcept; |
| 137 | + |
| 138 | + /** |
| 139 | + * Get the tag of the specified output. |
| 140 | + * |
| 141 | + * @param[in] index The index of the output to look up. |
| 142 | + * @returns The tag of output, can only be [Tensor, Int, Bool, Double, |
| 143 | + * String]. |
| 144 | + */ |
| 145 | + Result<Tag> output_tag(size_t index) const noexcept; |
| 146 | + |
| 147 | + /** |
| 148 | + * Get metadata about the specified output. |
| 149 | + * |
| 150 | + * @param[in] index The index of the output to look up. |
| 151 | + * @returns The metadata on success, or an error on failure. Only valid for |
| 152 | + * tag::Tensor |
| 153 | + */ |
| 154 | + Result<TensorInfo> output_tensor_meta(size_t index) const noexcept; |
| 155 | + |
| 156 | + /** |
| 157 | + * Get the number of non-constant buffers this method requires. |
| 158 | + * |
| 159 | + * @returns The number of non-constant buffers. |
| 160 | + */ |
| 161 | + size_t num_non_const_buffers() const noexcept; |
| 162 | + |
| 163 | + /** |
| 164 | + * Get the size of the specified non-constant buffer. |
| 165 | + * |
| 166 | + * @param[in] index The index of the buffer to look up. |
| 167 | + * @returns The size in bytes on success, or an error on failure. |
| 168 | + */ |
| 169 | + Result<int64_t> non_const_buffer_size(size_t index) const noexcept; |
| 170 | + |
| 171 | + private: |
| 172 | + // Let Program create MethodMeta. |
| 173 | + friend class Program; |
| 174 | + |
| 175 | + explicit MethodMeta( |
| 176 | + const executorch_flatbuffer::ExecutionPlan* s_plan) noexcept; |
| 177 | + |
| 178 | + /// Source of truth for method information |
| 179 | + const executorch_flatbuffer::ExecutionPlan* s_plan_; |
| 180 | +}; |
| 181 | + |
| 182 | +} // namespace executor |
| 183 | +} // namespace torch |
0 commit comments