Skip to content

Commit 4da9d66

Browse files
cccclaifacebook-github-bot
authored andcommitted
Update ExecutorchModule to use data loader (#485)
Summary: Pull Request resolved: #485 Update to construct program from data loader instead of raw string, as those APIs are fully deprecated and cleaned up. Reviewed By: dbort Differential Revision: D49580893 fbshipit-source-id: 8684cda39cbd56f1956a6ba614b7c6d0f7208626
1 parent d515135 commit 4da9d66

File tree

1 file changed

+15
-32
lines changed
  • examples/ios_demo_apps/ExecutorchMobileNet/ExecutorchMobileNet/ExecutorchMobileNet

1 file changed

+15
-32
lines changed

examples/ios_demo_apps/ExecutorchMobileNet/ExecutorchMobileNet/ExecutorchMobileNet/ExecutorchModule.mm

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include <stdio.h>
1616
#include <cstddef>
1717
#include <string>
18-
//
19-
#include <executorch/extension/data_loader/file_data_loader.h>
18+
19+
#include <executorch/extension/data_loader/shared_ptr_data_loader.h>
2020
#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h>
2121
#include <executorch/runtime/executor/method.h>
2222
#include <executorch/runtime/executor/program.h>
@@ -33,7 +33,8 @@
3333
#include <string>
3434

3535
using namespace torch::executor;
36-
using torch::executor::util::FileDataLoader;
36+
37+
using torch::executor::util::SharedPtrDataLoader;
3738
using torch::executor::testing::TensorFactory;
3839

3940
static constexpr size_t kRuntimeMemorySize = 64021120;
@@ -120,37 +121,21 @@ - (nullable instancetype)initWithFileAtPath:(NSString*)filePath {
120121
- (char*)segmentImage:(void*)imageBuffer
121122
withWidth:(int)width
122123
withHeight:(int)height {
123-
printf("running segmentImage...");
124124
float* floatData = static_cast<float*>(imageBuffer);
125-
for (int i = 0; i < 10; i++) {
126-
printf("float Data first item: %f: ", floatData[i]);
127-
}
128-
129-
printf("last element: %f: ", floatData[1 * 3 * 224 * 224 - 1]);
130125

131126
runtime_init();
132127
Error status;
133128

134129
TensorFactory<ScalarType::Float> tensor_inputs;
135-
// const std::vector<int32_t> sizes = {1};
136-
// EValue evalue_inputs(tensor_inputs.make(sizes, /*data=*/{0.2}));
137-
138130
const std::vector<int32_t> sizes = {1, 3, 224, 224};
139131
std::vector<float> floatVector(floatData, floatData + 1 * 3 * 224 * 224);
140132
EValue evalue_inputs(tensor_inputs.make(sizes, /*data=*/floatVector));
141133

142-
// NSString *filePath = [[NSBundle mainBundle]
143-
// pathForResource:@"lowered_sin" ofType:@"ff"]; executorch_module_name =
144-
// filePath.UTF8String;
145134
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"mv2_softmax"
146135
ofType:@"pte"];
147-
// NSString *filePath = [[NSBundle mainBundle] pathForResource:@"mv3"
148-
// ofType:@"ff"]; NSString *filePath = [[NSBundle mainBundle]
149-
// pathForResource:@"lowered_sin" ofType:@"ff"];
150136
NSLog(@" filePath: %@", filePath);
151137
std::string log_message = "Start logging...\n";
152138
ET_LOG(Info, "Hello world.");
153-
NSString* objcString = @(log_message.c_str());
154139

155140
MemoryAllocator const_allocator{MemoryAllocator(0, nullptr)};
156141
const_allocator.enable_profiling("const allocator");
@@ -191,40 +176,38 @@ - (char*)segmentImage:(void*)imageBuffer
191176
// Allocate memory
192177
std::shared_ptr<char> file_data = std::shared_ptr<char>(
193178
new char[file_length + 1], std::default_delete<char[]>());
179+
194180
if (!file_data) {
195181
ET_LOG(Error, "Unable to allocate memory to read file %s\n", file_name);
196182
fclose(file);
197183
}
198184
ET_LOG(Info, "Allocate memory Finish.");
199-
//
200-
// // Read file contents into buffer
185+
201186
fread(file_data.get(), file_length, 1, file);
202187
ET_LOG(Info, "Load file Finish.");
203-
//
204-
const void* program_data = file_data.get();
205188

206-
const auto program = torch::executor::Program(program_data);
189+
// TODO(chenlai): use FileDataLoader or MmapDataLoader to load model
190+
SharedPtrDataLoader data_loader(file_data, file_length);
191+
Result<Program> program = Program::load(&data_loader);
207192

208-
if (!program.is_valid()) {
209-
ET_LOG(Info, "Failed to parse model file %s", file_name);
193+
if (!program.ok()) {
194+
ET_LOG(Error, "Failed to parse model file %s", file_name);
195+
return nil;
210196
}
211197

212-
// Use the first method in the program.
213198
const char* method_name = nullptr;
214199
{
215-
const auto method_name_result = program.get_method_name(0);
200+
const auto method_name_result = program->get_method_name(0);
216201
ET_CHECK_MSG(method_name_result.ok(), "Program has no methods");
217202
method_name = *method_name_result;
218203
}
219-
ET_LOG(Info, "Loading method %s", method_name);
220-
log_message = log_message + "Loading method " + method_name + "\n";
204+
ET_LOG(Info, "Using method %s", method_name);
221205

222-
Result<Method> method = program.load_method(method_name, &memory_manager);
206+
Result<Method> method = program->load_method(method_name, &memory_manager);
223207

224208
ET_CHECK(method.ok());
225209
ET_LOG(Info, "Method loaded.");
226210
method->set_input(evalue_inputs, 0);
227-
// auto inputs = torch::executor::util::PrepareInputTensors(*method);
228211

229212
ET_LOG(Info, "Inputs prepared.");
230213

0 commit comments

Comments
 (0)