28
28
#include < executorch/runtime/platform/log.h>
29
29
#include < executorch/runtime/platform/profiler.h>
30
30
#include < executorch/runtime/platform/runtime.h>
31
+ #include < executorch/schema/bundled_program_schema_generated.h>
31
32
#include < executorch/util/bundled_program_verification.h>
32
33
#include < executorch/util/util.h>
33
34
@@ -36,19 +37,14 @@ static constexpr size_t kBundledAllocatorPoolSize = 16 * 1024U;
36
37
static uint8_t bundled_allocator_pool[kBundledAllocatorPoolSize ];
37
38
38
39
DEFINE_string (
39
- model_path ,
40
- " model.pte " ,
40
+ bundled_program_path ,
41
+ " model_bundled.bp " ,
41
42
" Model serialized in flatbuffer format." );
42
43
DEFINE_string (
43
44
prof_result_path,
44
45
" prof_result.bin" ,
45
46
" ExecuTorch profiler output path." );
46
47
47
- DEFINE_bool (
48
- bundled_program,
49
- false ,
50
- " True for running bundled program, false for executorch_flatbuffer::program" );
51
-
52
48
DEFINE_int32 (
53
49
testset_idx,
54
50
0 ,
@@ -74,8 +70,8 @@ int main(int argc, char** argv) {
74
70
// Create a loader to get the data of the program file. There are other
75
71
// DataLoaders that use mmap() or point to data that's already in memory, and
76
72
// users can create their own DataLoaders to load from arbitrary sources.
77
- const char * model_path = FLAGS_model_path .c_str ();
78
- Result<FileDataLoader> loader = FileDataLoader::from (model_path );
73
+ const char * bundled_program_path = FLAGS_bundled_program_path .c_str ();
74
+ Result<FileDataLoader> loader = FileDataLoader::from (bundled_program_path );
79
75
ET_CHECK_MSG (
80
76
loader.ok (), " FileDataLoader::from() failed: 0x%" PRIx32, loader.error ());
81
77
@@ -84,9 +80,16 @@ int main(int argc, char** argv) {
84
80
ET_CHECK_MSG (
85
81
file_data.ok (),
86
82
" Could not load contents of file '%s': 0x%x" ,
87
- model_path ,
83
+ bundled_program_path ,
88
84
(unsigned int )file_data.error ());
89
85
86
+ // Check whether the file is a bundled program.
87
+ ET_CHECK_MSG (
88
+ executorch_flatbuffer::BundledProgramBufferHasIdentifier (
89
+ file_data->data ()),
90
+ " The file '%s' is not a bundled program." ,
91
+ bundled_program_path);
92
+
90
93
// Find the offset to the embedded Program.
91
94
const void * program_data;
92
95
size_t program_data_len;
@@ -98,7 +101,7 @@ int main(int argc, char** argv) {
98
101
ET_CHECK_MSG (
99
102
status == Error::Ok,
100
103
" GetProgramData() failed on file '%s': 0x%x" ,
101
- model_path ,
104
+ bundled_program_path ,
102
105
(unsigned int )status);
103
106
104
107
auto buffer_data_loader =
@@ -108,10 +111,10 @@ int main(int argc, char** argv) {
108
111
// between multiple execution invocations across multiple threads.
109
112
Result<Program> program = Program::load (&buffer_data_loader);
110
113
if (!program.ok ()) {
111
- ET_LOG (Error, " Failed to parse model file %s" , model_path );
114
+ ET_LOG (Error, " Failed to parse model file %s" , bundled_program_path );
112
115
return 1 ;
113
116
}
114
- ET_LOG (Info, " Model file %s is loaded." , model_path );
117
+ ET_LOG (Info, " Model file %s is loaded." , bundled_program_path );
115
118
116
119
// Use the first method in the program.
117
120
const char * method_name = nullptr ;
@@ -198,22 +201,18 @@ int main(int argc, char** argv) {
198
201
MemoryAllocator bundled_input_allocator{
199
202
MemoryAllocator (kBundledAllocatorPoolSize , bundled_allocator_pool)};
200
203
exec_aten::ArrayRef<void *> inputs;
201
- if (FLAGS_bundled_program) {
202
- // Use the inputs embedded in the bundled program.
203
- status = torch::executor::util::LoadBundledInput (
204
- *method,
205
- file_data->data (),
206
- &bundled_input_allocator,
207
- 0 , // Using the 0th indexed program
208
- FLAGS_testset_idx);
209
- ET_CHECK_MSG (
210
- status == Error::Ok,
211
- " LoadBundledInput failed with status 0x%" PRIx32,
212
- status);
213
- } else {
214
- // Use ones-initialized inputs.
215
- inputs = torch::executor::util::PrepareInputTensors (*method);
216
- }
204
+ // Use the inputs embedded in the bundled program.
205
+ status = torch::executor::util::LoadBundledInput (
206
+ *method,
207
+ file_data->data (),
208
+ &bundled_input_allocator,
209
+ method_name,
210
+ FLAGS_testset_idx);
211
+ ET_CHECK_MSG (
212
+ status == Error::Ok,
213
+ " LoadBundledInput failed with status 0x%" PRIx32,
214
+ status);
215
+
217
216
ET_LOG (Info, " Inputs prepared." );
218
217
219
218
// Run the model.
@@ -249,24 +248,21 @@ int main(int argc, char** argv) {
249
248
fclose (ptr);
250
249
}
251
250
252
- // Handle the outputs.
253
- if (FLAGS_bundled_program) {
254
- status = torch::executor::util::VerifyResultWithBundledExpectedOutput (
255
- *method,
256
- file_data->data (),
257
- &bundled_input_allocator,
258
- 0 ,
259
- FLAGS_testset_idx,
260
- 1e-5 , // rtol
261
- 1e-8 // atol
262
- );
263
- ET_CHECK_MSG (
264
- status == Error::Ok,
265
- " Bundle verification failed with status 0x%" PRIx32,
266
- status);
267
- ET_LOG (Info, " Model verified successfully." );
268
- } else {
269
- torch::executor::util::FreeInputs (inputs);
270
- }
251
+ // Verify the outputs.
252
+ status = torch::executor::util::VerifyResultWithBundledExpectedOutput (
253
+ *method,
254
+ file_data->data (),
255
+ &bundled_input_allocator,
256
+ method_name,
257
+ FLAGS_testset_idx,
258
+ 1e-5 , // rtol
259
+ 1e-8 // atol
260
+ );
261
+ ET_CHECK_MSG (
262
+ status == Error::Ok,
263
+ " Bundle verification failed with status 0x%" PRIx32,
264
+ status);
265
+ ET_LOG (Info, " Model verified successfully." );
266
+
271
267
return 0 ;
272
268
}
0 commit comments