-
Notifications
You must be signed in to change notification settings - Fork 419
/
Copy pathprogram.h
264 lines (230 loc) · 8.43 KB
/
program.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* 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 <cinttypes>
#include <cstdint>
#include <executorch/runtime/core/data_loader.h>
#include <executorch/runtime/core/error.h>
#include <executorch/runtime/core/event_tracer.h>
#include <executorch/runtime/core/freeable_buffer.h>
#include <executorch/runtime/core/result.h>
#include <executorch/runtime/executor/memory_manager.h>
#include <executorch/runtime/executor/method.h>
#include <executorch/runtime/executor/method_meta.h>
#include <executorch/runtime/platform/compiler.h>
// Forward declare flatbuffer types. This is a public header and must not
// include the generated flatbuffer header.
namespace executorch_flatbuffer {
struct Program;
} // namespace executorch_flatbuffer
namespace torch {
namespace executor {
namespace testing {
// Provides test access to private Program methods.
class ProgramTestFriend;
} // namespace testing
/**
* A deserialized ExecuTorch program binary.
*/
class Program final {
public:
/**
* Types of validation that the Program can do before parsing the data.
*/
enum class Verification : uint8_t {
/**
* Do minimal verification of the data, ensuring that the header appears
* correct.
*
* Has minimal runtime overhead.
*/
Minimal,
/**
* Do full verification of the data, ensuring that internal pointers are
* self-consistent and that the data has not been truncated or obviously
* corrupted. May not catch all types of corruption, but should guard
* against illegal memory operations during parsing.
*
* Will have higher runtime overhead, scaling with the complexity of the
* proram data.
*/
InternalConsistency,
};
/**
* Loads a Program from the provided loader. The Program will hold a pointer
* to the loader, which must outlive the returned Program instance.
*
* @param[in] loader The source to load program data from. The Program will
* hold a pointer to this loader, which must outlive the returned Program
* instance.
* @param[in] verification The type of verification to do before returning
* success.
*/
__ET_NODISCARD static Result<Program> load(
DataLoader* loader,
Verification verification = Verification::Minimal);
/// DEPRECATED: Use the lowercase `load()` instead.
__ET_DEPRECATED __ET_NODISCARD static Result<Program> Load(
DataLoader* loader,
Verification verification = Verification::Minimal) {
return load(loader, verification);
}
// Movable, to be compatible with Result.
Program(Program&&) noexcept = default;
~Program() = default;
/**
* Get the constant buffer inside Program with index buffer_idx.
* @param[in] buffer_idx the index of the buffer in the constant_buffer.
* @param[in] nbytes the number of bytes to read from the buffer.
* @return The buffer with corresponding index.
*/
Result<const void*> get_constant_buffer_data(size_t buffer_idx, size_t nbytes)
const;
/**
* Returns the number of methods in the program.
*/
size_t num_methods() const;
/**
* Returns the name of the method at particular index.
*
* @param[in] method_index The index of the method name to retrieve. Must be
* less than the value returned by `num_methods()`.
*
* @returns The name of the requested method. The pointer is owned by the
* Program, and has the same lifetime as the Program.
*/
Result<const char*> get_method_name(size_t method_index) const;
/**
* Loads the named method and prepares it for execution.
*
* @param[in] method_name The name of the method to load.
* @param[in] memory_manager The allocators to use during initialization and
* execution of the loaded method.
* @param[in] event_tracer The event tracer to use for this method run.
*
* @returns The loaded method on success, or an error on failure.
*/
Result<Method> load_method(
const char* method_name,
MemoryManager* memory_manager,
EventTracer* event_tracer = nullptr) const;
/**
* Gathers metadata for the named method.
*
* @param[in] method_name The name of the method to get metadata for.
*/
Result<MethodMeta> method_meta(const char* method_name) const;
/**
* DEPRECATED: Get the pytree encoding string for the output. Deprecated as
* this functionality will eventually move out of the core program into a
* higher level structure, but that does not exist at this time.
* @param[in] method_name The name of the method to get the encoding for.
*
* @return The pytree encoding string for the output
*/
__ET_DEPRECATED Result<const char*> get_output_flattening_encoding(
const char* method_name = "forward") const;
/**
* Describes the presence of an ExecuTorch program header.
*/
enum HeaderStatus {
/**
* An ExecuTorch program header is present, and its version is compatible
* with this version of the runtime.
*/
CompatibleVersion,
/**
* An ExecuTorch program header is present, but its version is not
* compatible with this version of the runtime.
*/
IncompatibleVersion,
/**
* An ExecuTorch program header is not present.
*/
NotPresent,
/**
* The data provided was too short to find the program header.
*/
ShortData,
};
/**
* The minimum number of bytes necessary for calls to `check_header`.
*/
static constexpr size_t kMinHeadBytes = 64;
/**
* Looks for an ExecuTorch program header in the provided data.
*
* @param[in] data The data from the beginning of a file that might contain
* an ExecuTorch program.
* @param[in] size The size of `data` in bytes. Must be >= `kMinHeadBytes`.
*
* @returns A value describing the presence of a header in the data.
*/
static HeaderStatus check_header(const void* data, size_t size);
private:
// Let some classes call these private methods.
friend class BackendDelegate;
friend class Executor;
friend class Method;
friend class testing::ProgramTestFriend;
const executorch_flatbuffer::Program* get_internal_program() const {
return internal_program_;
}
// Used by Method to look up entries in the delegate data table.
Error get_backend_delegate_data(
size_t index,
const void** out_data,
size_t* out_size) const;
/**
* Loads a segment by index.
*
* @param[in] SegmentInfo Struct containing an index to load from the
* Program.segments list. The other fields of the struct, such as
* `segment_type` and `descriptor`, need to also be correct.
*
* @returns The data as a FreeableBuffer, if the index is valid.
* @retval Error::NotFound The program does not contain any segments or the
* index is out of range.
* @returns Other errors depending on the implementation of
* DataLoader: The Program.segment table is inconsistent, or the
* data cannot be accessed.
*/
__ET_NODISCARD Result<FreeableBuffer> LoadSegment(
const DataLoader::SegmentInfo& segment_info) const;
private:
Program(
DataLoader* loader,
size_t segment_base_offset,
FreeableBuffer&& program_data,
const executorch_flatbuffer::Program* internal_program,
FreeableBuffer&& constant_segment_data)
: program_data_(std::move(program_data)),
// Don't need the loader if there are no segments.
loader_(segment_base_offset > 0 ? loader : nullptr),
internal_program_(internal_program),
segment_base_offset_(segment_base_offset),
constant_segment_data_(std::move(constant_segment_data)) {}
// Not copyable or assignable.
Program(const Program& rhs) = delete;
Program& operator=(Program&& rhs) noexcept = delete;
Program& operator=(const Program& rhs) = delete;
/// The serialized program data. Tensors will point directly into this buffer.
FreeableBuffer program_data_;
/// Used to load segment data. Null if there are no segments.
DataLoader* loader_;
/// The flatbuffer representation of the program. Must not be exposed to
/// users.
const executorch_flatbuffer::Program* internal_program_;
/// The offset to the first segment, in bytes. If zero, no segments should
/// be present in internal_program_.
size_t segment_base_offset_;
/// Constant segment data.
FreeableBuffer constant_segment_data_;
};
} // namespace executor
} // namespace torch