forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule.h
510 lines (466 loc) · 17.4 KB
/
module.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* 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 <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <executorch/runtime/executor/program.h>
namespace executorch {
namespace extension {
using ET_RUNTIME_NAMESPACE::Method;
using ET_RUNTIME_NAMESPACE::MethodMeta;
using ET_RUNTIME_NAMESPACE::NamedDataMap;
using ET_RUNTIME_NAMESPACE::Program;
/**
* A facade class for loading programs and executing methods within them.
*/
class Module {
public:
/**
* Enum to define loading behavior.
*/
enum class LoadMode {
/// Load the whole file as a buffer.
File,
/// Use mmap to load pages into memory.
Mmap,
/// Use memory locking and handle errors.
MmapUseMlock,
/// Use memory locking and ignore errors.
MmapUseMlockIgnoreErrors,
};
/**
* Constructs an instance by loading a program from a file with specified
* memory locking behavior.
*
* @param[in] file_path The path to the ExecuTorch program file to load.
* @param[in] load_mode The loading mode to use.
* @param[in] event_tracer A EventTracer used for tracking and logging events.
*/
explicit Module(
const std::string& file_path,
const LoadMode load_mode = LoadMode::MmapUseMlock,
std::unique_ptr<runtime::EventTracer> event_tracer = nullptr);
/**
* Constructs an instance by loading a program from a file with specified
* memory locking behavior.
*
* @param[in] file_path The path to the ExecuTorch program file to load.
* @param[in] data_map_path The path to a .ptd file
* @param[in] load_mode The loading mode to use.
* @param[in] event_tracer A EventTracer used for tracking and logging events.
*/
explicit Module(
const std::string& file_path,
const std::string& data_map_path,
const LoadMode load_mode = LoadMode::MmapUseMlock,
std::unique_ptr<runtime::EventTracer> event_tracer = nullptr);
/**
* Constructs an instance with the provided data loader and memory allocator.
*
* @param[in] data_loader A DataLoader used for loading program data.
* @param[in] memory_allocator A MemoryAllocator used for memory management.
* @param[in] temp_allocator A MemoryAllocator to use when allocating
* temporary data during kernel or delegate execution.
* @param[in] event_tracer A EventTracer used for tracking and logging events.
* @param[in] data_map_loader A DataLoader used for loading external weights.
*/
explicit Module(
std::unique_ptr<runtime::DataLoader> data_loader,
std::unique_ptr<runtime::MemoryAllocator> memory_allocator = nullptr,
std::unique_ptr<runtime::MemoryAllocator> temp_allocator = nullptr,
std::unique_ptr<runtime::EventTracer> event_tracer = nullptr,
std::unique_ptr<runtime::DataLoader> data_map_loader = nullptr);
/**
* Constructs an instance using an existing shared program.
*
* @param[in] program The shared program to use. It's required the data loader
* the program uses is valid for the lifetime of the program.
* @param[in] memory_allocator A MemoryAllocator used for memory management.
* @param[in] temp_allocator A MemoryAllocator to use when allocating
* temporary data.
* @param[in] event_tracer A EventTracer used for tracking and logging events.
* @param[in] data_map_loader A DataLoader used for loading external weights.
*/
explicit Module(
std::shared_ptr<Program> program,
std::unique_ptr<runtime::MemoryAllocator> memory_allocator = nullptr,
std::unique_ptr<runtime::MemoryAllocator> temp_allocator = nullptr,
std::unique_ptr<runtime::EventTracer> event_tracer = nullptr,
std::unique_ptr<runtime::DataLoader> data_map_loader = nullptr);
Module(const Module&) = delete;
Module& operator=(const Module&) = delete;
Module(Module&&) = delete;
Module& operator=(Module&&) = delete;
/**
* Loads the program if needed.
*
* @param[in] verification The type of verification to do before returning
* success.
*
* @returns An Error to indicate success or failure of the loading process.
*/
ET_NODISCARD
runtime::Error load(
const Program::Verification verification =
Program::Verification::Minimal);
/**
* Checks if the program is loaded.
*
* @returns true if the program is loaded, false otherwise.
*/
inline bool is_loaded() const {
return program_ != nullptr;
}
/**
* Get the program. The data loader used by the program is guaranteed to be
* valid for the lifetime of the program.
*
* @returns Shared pointer to the program or nullptr if it's not yet loaded.
*/
inline std::shared_ptr<Program> program() const {
return program_;
}
/**
* Get the number of methods available in the loaded program.
*
* @returns A Result object containing either the number of methods available
* or an error to indicate failure.
*/
runtime::Result<size_t> num_methods();
/**
* Get a list of method names available in the loaded program.
* Loads the program and method if needed.
*
* @returns A set of strings containing the names of the methods, or an error
* if the program or method failed to load.
*/
runtime::Result<std::unordered_set<std::string>> method_names();
/**
* Load a specific method from the program and set up memory management if
* needed. The loaded method is cached to reuse the next time it's executed.
*
* @param[in] method_name The name of the method to load.
* @param[in] planned_memory The memory-planned buffers to use for mutable
* tensor data when executing a method.
* @param[in] event_tracer Per-method event tracer to profile/trace methods
* individually. When not given, the event tracer passed to the Module
* constructor is used. Otherwise, this per-method event tracer takes
* precedence.
*
* @returns An Error to indicate success or failure.
*/
ET_NODISCARD
runtime::Error load_method(
const std::string& method_name,
runtime::HierarchicalAllocator* planned_memory = nullptr,
torch::executor::EventTracer* event_tracer = nullptr);
ET_DEPRECATED ET_NODISCARD runtime::Error inline load_method(
const std::string& method_name,
torch::executor::EventTracer* event_tracer) {
return load_method(method_name, nullptr, event_tracer);
}
/**
* Load the 'forward' method from the program and set up memory management if
* needed. The loaded method is cached to reuse the next time it's executed.
*
* @param[in] planned_memory The memory-planned buffers to use for mutable
* tensor data when executing the 'forward' method.
* @param[in] event_tracer An event tracer used for tracking and logging
* events.
*
* @returns An Error to indicate success or failure.
*/
ET_NODISCARD inline runtime::Error load_forward(
runtime::HierarchicalAllocator* planned_memory = nullptr,
torch::executor::EventTracer* event_tracer = nullptr) {
return load_method("forward", planned_memory, event_tracer);
}
ET_DEPRECATED ET_NODISCARD inline runtime::Error load_forward(
torch::executor::EventTracer* event_tracer) {
return load_forward(nullptr, event_tracer);
}
/**
* Checks if a specific method is loaded.
*
* @param[in] method_name The name of the method to check.
*
* @returns true if the method specified by method_name is loaded, false
* otherwise.
*/
inline bool is_method_loaded(const std::string& method_name) const {
return methods_.count(method_name);
}
/**
* Get a method metadata struct by method name.
* Loads the program and method if needed.
*
* @param[in] method_name The name of the method to get the metadata for.
*
* @returns A method metadata, or an error if the program or method failed to
* load.
*/
runtime::Result<MethodMeta> method_meta(const std::string& method_name);
/**
* Execute a specific method with the given input values and retrieve the
* output values. Loads the program and method before executing if needed.
*
* @param[in] method_name The name of the method to execute.
* @param[in] input_values A vector of input values to be passed to the
* method.
*
* @returns A Result object containing either a vector of output values
* from the method or an error to indicate failure.
*/
ET_NODISCARD
runtime::Result<std::vector<runtime::EValue>> execute(
const std::string& method_name,
const std::vector<runtime::EValue>& input_values);
/**
* Execute a specific method with a single input value.
* Loads the program and method before executing if needed.
*
* @param[in] method_name The name of the method to execute.
* @param[in] input_value A value to be passed to the method.
*
* @returns A Result object containing either a vector of output values
* from the method or an error to indicate failure.
*/
ET_NODISCARD inline runtime::Result<std::vector<runtime::EValue>> execute(
const std::string& method_name,
const runtime::EValue& input_value) {
return execute(method_name, std::vector<runtime::EValue>{input_value});
}
/**
* Execute a specific method without any input values.
* Loads the program and method before executing if needed.
*
* @param[in] method_name The name of the method to execute.
*
* @returns A Result object containing either a vector of output values
* from the method or an error to indicate failure.
*/
ET_NODISCARD inline runtime::Result<std::vector<runtime::EValue>> execute(
const std::string& method_name) {
return execute(method_name, std::vector<runtime::EValue>{});
}
/**
* Retrieve the output value of a specific method with the given input values.
* Loads the program and method before execution if needed.
*
* @param[in] method_name The name of the method to execute.
* @param[in] input_values A vector of input values to be passed to the
* method.
*
* @returns A Result object containing either the first output value from the
* method or an error to indicate failure.
*/
ET_NODISCARD inline runtime::Result<runtime::EValue> get(
const std::string& method_name,
const std::vector<runtime::EValue>& input_values) {
auto result = ET_UNWRAP(execute(method_name, input_values));
if (result.empty()) {
return runtime::Error::InvalidArgument;
}
return result[0];
}
/**
* Retrieve the output value of a specific method with a single input value.
* Loads the program and method before execution if needed.
*
* @param[in] method_name The name of the method to execute.
* @param[in] input_value A value to be passed to the method.
*
* @returns A Result object containing either the first output value from the
* method or an error to indicate failure.
*/
ET_NODISCARD inline runtime::Result<runtime::EValue> get(
const std::string& method_name,
const runtime::EValue& input_value) {
return get(method_name, std::vector<runtime::EValue>{input_value});
}
/**
* Retrieve the output value of a specific method without any input values.
* Loads the program and method before execution if needed.
*
* @param[in] method_name The name of the method to execute.
*
* @returns A Result object containing either the first output value from the
* method or an error to indicate failure.
*/
ET_NODISCARD inline runtime::Result<runtime::EValue> get(
const std::string& method_name) {
return get(method_name, std::vector<runtime::EValue>{});
}
/**
* Execute the 'forward' method with the given input values and retrieve the
* output values. Loads the program and method before executing if needed.
*
* @param[in] input_values A vector of input values for the 'forward' method.
*
* @returns A Result object containing either a vector of output values
* from the 'forward' method or an error to indicate failure.
*/
ET_NODISCARD inline runtime::Result<std::vector<runtime::EValue>> forward(
const std::vector<runtime::EValue>& input_values) {
return execute("forward", input_values);
}
/**
* Execute the 'forward' method with a single value.
* Loads the program and method before executing if needed.
*
* @param[in] input_value A value for the 'forward' method.
*
* @returns A Result object containing either a vector of output values
* from the 'forward' method or an error to indicate failure.
*/
ET_NODISCARD inline runtime::Result<std::vector<runtime::EValue>> forward(
const runtime::EValue& input_value) {
return forward(std::vector<runtime::EValue>{input_value});
}
/**
* Execute the 'forward' method without any input values.
* Loads the program and method before executing if needed.
*
* @returns A Result object containing either a vector of output values
* from the 'forward' method or an error to indicate failure.
*/
ET_NODISCARD inline runtime::Result<std::vector<runtime::EValue>> forward() {
return forward(std::vector<runtime::EValue>{});
}
/**
* Sets a single input value for a specific method.
*
* @param[in] method_name The name of the method.
* @param[in] input_value The EValue to set as the method input.
* @param[in] input_index Zero-based index of the input to set.
*
* @returns An Error to indicate success or failure.
*/
ET_NODISCARD
runtime::Error set_input(
const std::string& method_name,
const runtime::EValue& input_value,
size_t input_index);
/**
* Sets a single input value for the "forward" method.
*
* @param[in] input_value The EValue to set as the method input.
* @param[in] input_index Zero-based index of the input to set.
*
* @returns An Error to indicate success or failure.
*/
ET_NODISCARD
inline runtime::Error set_input(
const runtime::EValue& input_value,
size_t input_index) {
return set_input("forward", input_value, input_index);
}
/**
* Sets all input values for a specific method.
*
* @param[in] method_name The name of the method.
* @param[in] input_values A vector of EValues to set as the method inputs.
*
* @returns An Error to indicate success or failure.
*/
ET_NODISCARD
runtime::Error set_inputs(
const std::string& method_name,
const std::vector<runtime::EValue>& input_values);
/**
* Sets all input values for the "forward" method.
*
* @param[in] input_values A vector of EValues to set as the method inputs.
*
* @returns An Error to indicate success or failure.
*/
ET_NODISCARD
inline runtime::Error set_inputs(
const std::vector<runtime::EValue>& input_values) {
return set_inputs("forward", input_values);
}
/**
* Sets the output tensor for a specific method.
*
* @param[in] method_name The name of the method.
* @param[in] output_value The EValue containing the Tensor to set as the
* method output.
* @param[in] output_index Zero-based index of the output to set.
*
* @returns An Error to indicate success or failure.
*
* @note Only Tensor outputs are currently supported for setting.
*/
ET_NODISCARD
runtime::Error set_output(
const std::string& method_name,
runtime::EValue output_value,
size_t output_index = 0);
/**
* Sets the output tensor for the "forward" method.
*
* @param[in] output_value The EValue containing the Tensor to set as the
* method output.
* @param[in] output_index Zero-based index of the output to set.
*
* @returns An Error to indicate success or failure.
*
* @note Only Tensor outputs are currently supported for setting.
*/
ET_NODISCARD
inline runtime::Error set_output(
runtime::EValue output_value,
size_t output_index = 0) {
return set_output("forward", std::move(output_value), output_index);
}
/**
* Retrieves the EventTracer instance being used by the Module.
* EventTracer is used for tracking and logging events during the execution
* of methods.
*
* @returns A pointer to the EventTracer instance. Returns nullptr if no
* EventTracer is set.
*/
inline runtime::EventTracer* event_tracer() const {
return event_tracer_.get();
}
private:
struct MethodHolder {
std::vector<std::vector<uint8_t>> planned_buffers;
std::vector<runtime::Span<uint8_t>> planned_spans;
std::unique_ptr<runtime::HierarchicalAllocator> planned_memory;
std::unique_ptr<runtime::MemoryManager> memory_manager;
std::unique_ptr<Method> method;
std::vector<runtime::EValue> inputs;
};
std::string file_path_;
std::string data_map_path_;
LoadMode load_mode_{LoadMode::MmapUseMlock};
std::shared_ptr<Program> program_;
std::unique_ptr<runtime::DataLoader> data_loader_;
std::unique_ptr<runtime::MemoryAllocator> memory_allocator_;
std::unique_ptr<runtime::MemoryAllocator> temp_allocator_;
std::unique_ptr<runtime::EventTracer> event_tracer_;
std::unique_ptr<runtime::DataLoader> data_map_loader_;
std::unique_ptr<NamedDataMap> data_map_;
protected:
std::unordered_map<std::string, MethodHolder> methods_;
friend class ExecuTorchJni;
};
} // namespace extension
} // namespace executorch
namespace torch {
namespace executor {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using ::executorch::extension::Module;
} // namespace executor
} // namespace torch