-
Notifications
You must be signed in to change notification settings - Fork 473
/
Copy pathmethod_meta.h
237 lines (201 loc) · 6.13 KB
/
method_meta.h
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
/*
* 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/runtime/core/exec_aten/exec_aten.h>
#include <executorch/runtime/core/result.h>
#include <executorch/runtime/core/span.h>
#include <executorch/runtime/core/tag.h>
// Forward declare flatbuffer types. This is a public header and must not
// include the generated flatbuffer header.
namespace executorch_flatbuffer {
struct ExecutionPlan;
} // namespace executorch_flatbuffer
namespace executorch {
namespace runtime {
/**
* Metadata about a specific tensor of an ExecuTorch Program.
*
* The program used to create the MethodMeta object that created this
* TensorInfo must outlive this TensorInfo.
*/
class TensorInfo final {
public:
TensorInfo() = delete;
TensorInfo(const TensorInfo&) = default;
TensorInfo(TensorInfo&&) = default;
TensorInfo& operator=(const TensorInfo&) = default;
TensorInfo& operator=(TensorInfo&& other) = default;
~TensorInfo() = default;
/**
* Returns the sizes of the tensor.
*/
Span<const int32_t> sizes() const;
/**
* Returns the dim order of the tensor.
*/
Span<const uint8_t> dim_order() const;
/**
* Returns the scalar type of the input/output.
*/
executorch::aten::ScalarType scalar_type() const;
/**
* Returns whether the tensor's memory was planned during export.
*/
bool is_memory_planned() const;
/**
* Returns the size of the tensor in bytes.
*/
size_t nbytes() const;
private:
// Let MethodMeta create TensorInfo.
friend class MethodMeta;
TensorInfo(
Span<const int32_t> sizes,
Span<const uint8_t> dim_order,
executorch::aten::ScalarType scalar_type,
const bool is_memory_planned);
/**
* The sizes of the tensor.
*
* NOTE: References data from the Program, so the Program must outlive the
* TensorInfo.
*/
Span<const int32_t> sizes_;
/**
* The dim order of the tensor.
*
* NOTE: References data from the Program, so the Program must outlive the
* TensorInfo.
*/
Span<const uint8_t> dim_order_;
/// The scalar type of the tensor.
executorch::aten::ScalarType scalar_type_;
/// Whether the tensor's memory was planned during export.
bool is_memory_planned_;
/// The size in bytes of the tensor.
size_t nbytes_;
};
/**
* Describes a a method in an ExecuTorch program.
*
* The program used to create a MethodMeta object must outlive the MethodMeta.
* It is separate from Method so that this information can be accessed without
* paying the initialization cost of loading the full Method.
*/
class MethodMeta final {
public:
MethodMeta() = delete;
MethodMeta(const MethodMeta&) = default;
MethodMeta(MethodMeta&&) = default;
MethodMeta& operator=(const MethodMeta&) = default;
MethodMeta& operator=(MethodMeta&& other) = default;
~MethodMeta() = default;
/**
* Get the name of this method.
*
* @returns The method name.
*/
const char* name() const;
/**
* Get the number of inputs to this method.
*
* @returns The number of inputs.
*/
size_t num_inputs() const;
/**
* Get the tag of the specified input.
*
* @param[in] index The index of the input to look up.
* @returns The tag of input, can only be [Tensor, Int, Bool, Double, String].
*/
Result<Tag> input_tag(size_t index) const;
/**
* Get metadata about the specified input.
*
* @param[in] index The index of the input to look up.
* @returns The metadata on success, or an error on failure. Only valid for
* tag::Tensor
*/
Result<TensorInfo> input_tensor_meta(size_t index) const;
/**
* Get the number of outputs to this method.
*
* @returns The number of outputs.
*/
size_t num_outputs() const;
/**
* Get the tag of the specified output.
*
* @param[in] index The index of the output to look up.
* @returns The tag of output, can only be [Tensor, Int, Bool, Double,
* String].
*/
Result<Tag> output_tag(size_t index) const;
/**
* Get metadata about the specified output.
*
* @param[in] index The index of the output to look up.
* @returns The metadata on success, or an error on failure. Only valid for
* tag::Tensor
*/
Result<TensorInfo> output_tensor_meta(size_t index) const;
/**
* Get the number of memory-planned buffers this method requires.
*
* @returns The number of memory-planned buffers.
*/
size_t num_memory_planned_buffers() const;
/**
* Get the size in bytes of the specified memory-planned buffer.
*
* @param[in] index The index of the buffer to look up.
* @returns The size in bytes on success, or an error on failure.
*/
Result<int64_t> memory_planned_buffer_size(size_t index) const;
/**
* Check to see if a backend is used in this method.
*
* @param[in] backend_name The name of the backend to search for.
* @returns true if a backend is used in this method, otherwise false.
*/
bool uses_backend(const char* backend_name) const;
/**
* Get the number of instructions in this method.
*
* @returns The number of instructions.
*/
ET_EXPERIMENTAL size_t num_instructions() const;
/**
* DEPRECATED: Use num_memory_planned_buffers() instead.
*/
ET_DEPRECATED size_t num_non_const_buffers() const {
return num_memory_planned_buffers();
}
/**
* DEPRECATED: Use memory_planned_buffer_size() instead.
*/
Result<int64_t> non_const_buffer_size(size_t index) const {
return memory_planned_buffer_size(index);
}
private:
// Let Program create MethodMeta.
friend class Program;
explicit MethodMeta(const executorch_flatbuffer::ExecutionPlan* s_plan);
/// Source of truth for method information
const executorch_flatbuffer::ExecutionPlan* s_plan_;
};
} // namespace runtime
} // namespace executorch
namespace torch {
namespace executor {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using ::executorch::runtime::MethodMeta;
using ::executorch::runtime::TensorInfo;
} // namespace executor
} // namespace torch