Skip to content

Commit 73fbb74

Browse files
pytorchbotdbort
andauthored
Migrate examples/apple/... away from the torch:: namespace (#5486)
Migrate examples/apple/... away from the torch:: namespace (#5405) Summary: Migrate examples/apple/... to use the new `executorch::` namespace. Pull Request resolved: #5405 Test Plan: Build the examples using the instructions at - https://github.com/pytorch/executorch/blob/main/examples/apple/coreml/README.md - https://github.com/pytorch/executorch/blob/main/examples/apple/mps/README.md Reviewed By: malfet, shoumikhin Differential Revision: D62793645 Pulled By: dbort fbshipit-source-id: b74b95b8ae87eeae40e57b195351c841c4a90885 (cherry picked from commit 5c3be4a) Co-authored-by: Dave Bort <[email protected]>
1 parent dc43193 commit 73fbb74

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

examples/apple/coreml/executor_runner/main.mm

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,25 @@ static inline id check_class(id obj, Class cls) {
2424

2525
#define SAFE_CAST(Object, Type) ((Type *)check_class(Object, [Type class]))
2626

27-
using namespace torch::executor;
28-
using torch::executor::util::FileDataLoader;
27+
using executorch::etdump::ETDumpGen;
28+
using executorch::etdump::ETDumpResult;
29+
using executorch::extension::FileDataLoader;
30+
using executorch::runtime::DataLoader;
31+
using executorch::runtime::EValue;
32+
using executorch::runtime::Error;
33+
using executorch::runtime::EventTracer;
34+
using executorch::runtime::EventTracerDebugLogLevel;
35+
using executorch::runtime::FreeableBuffer;
36+
using executorch::runtime::HierarchicalAllocator;
37+
using executorch::runtime::MemoryAllocator;
38+
using executorch::runtime::MemoryManager;
39+
using executorch::runtime::Method;
40+
using executorch::runtime::MethodMeta;
41+
using executorch::runtime::Program;
42+
using executorch::runtime::Result;
43+
using executorch::runtime::Span;
44+
using executorch::runtime::TensorInfo;
45+
using torch::executor::CoreMLBackendDelegate;
2946

3047
static constexpr size_t kRuntimeMemorySize = 16 * 1024U * 1024U; // 16 MB
3148

@@ -294,7 +311,7 @@ bool is_model_analysis_enabled(const Args& args) {
294311
}
295312

296313
void dump_etdump_gen(ETDumpGen *etdump_gen, const Buffer& debug_buffer, const Args& args) {
297-
etdump_result result = (etdump_gen != nullptr) ? etdump_gen->get_etdump_data() : etdump_result{.buf = nullptr, .size = 0};
314+
ETDumpResult result = (etdump_gen != nullptr) ? etdump_gen->get_etdump_data() : ETDumpResult{.buf = nullptr, .size = 0};
298315
if (result.size == 0) {
299316
return;
300317
}
@@ -316,7 +333,7 @@ void dump_etdump_gen(ETDumpGen *etdump_gen, const Buffer& debug_buffer, const Ar
316333

317334
int main(int argc, char * argv[]) {
318335
@autoreleasepool {
319-
runtime_init();
336+
executorch::runtime::runtime_init();
320337

321338
auto args = parse_command_line_args([[NSProcessInfo processInfo] arguments]);
322339
if (args.purge_models_cache) {

examples/apple/mps/executor_runner/mps_executor_runner.mm

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,26 @@
9797
262144, // 256 KB
9898
"Size of the debug buffer in bytes to allocate for intermediate outputs and program outputs logging.");
9999

100-
using namespace torch::executor;
101-
using torch::executor::util::FileDataLoader;
100+
using executorch::etdump::ETDumpGen;
101+
using executorch::etdump::ETDumpResult;
102+
using executorch::extension::BufferCleanup;
103+
using executorch::extension::BufferDataLoader;
104+
using executorch::extension::FileDataLoader;
105+
using executorch::runtime::DataLoader;
106+
using executorch::runtime::EValue;
107+
using executorch::runtime::Error;
108+
using executorch::runtime::EventTracerDebugLogLevel;
109+
using executorch::runtime::FreeableBuffer;
110+
using executorch::runtime::HierarchicalAllocator;
111+
using executorch::runtime::MemoryAllocator;
112+
using executorch::runtime::MemoryManager;
113+
using executorch::runtime::Method;
114+
using executorch::runtime::MethodMeta;
115+
using executorch::runtime::Program;
116+
using executorch::runtime::Result;
117+
using executorch::runtime::Span;
118+
119+
namespace bundled_program = executorch::bundled_program;
102120

103121
int main(int argc, char** argv) {
104122
{
@@ -113,7 +131,7 @@ int main(int argc, char** argv) {
113131
return 1;
114132
}
115133

116-
runtime_init();
134+
executorch::runtime::runtime_init();
117135

118136
gflags::ParseCommandLineFlags(&argc, &argv, true);
119137
if (argc != 1) {
@@ -144,20 +162,20 @@ int main(int argc, char** argv) {
144162
// Find the offset to the embedded Program.
145163
const void* program_data;
146164
size_t program_data_len;
147-
Error status = torch::executor::bundled_program::GetProgramData(
165+
Error status = bundled_program::get_program_data(
148166
const_cast<void*>(file_data->data()),
149167
file_data->size(),
150168
&program_data,
151169
&program_data_len);
152170
ET_CHECK_MSG(
153171
status == Error::Ok,
154-
"GetProgramData() failed on file '%s': 0x%x",
172+
"get_program_data() failed on file '%s': 0x%x",
155173
model_path,
156174
(unsigned int)status);
157175

158176
// Wrap the buffer in a DataLoader.
159177
auto buffer_data_loader =
160-
util::BufferDataLoader(program_data, program_data_len);
178+
BufferDataLoader(program_data, program_data_len);
161179

162180
// Parse the program file. This is immutable, and can also be reused between
163181
// multiple execution invocations across multiple threads.
@@ -239,7 +257,7 @@ HierarchicalAllocator planned_memory(
239257
// be used by a single thread at at time, but it can be reused.
240258
//
241259

242-
torch::executor::ETDumpGen etdump_gen = torch::executor::ETDumpGen();
260+
ETDumpGen etdump_gen;
243261
Result<Method> method =
244262
program->load_method(method_name, &memory_manager, &etdump_gen);
245263
ET_CHECK_MSG(
@@ -263,11 +281,11 @@ HierarchicalAllocator planned_memory(
263281
}
264282

265283
// Prepare the inputs.
266-
std::unique_ptr<util::BufferCleanup> inputs;
284+
std::unique_ptr<BufferCleanup> inputs;
267285
if (FLAGS_bundled_program) {
268286
ET_LOG(Info, "Loading bundled program...");
269287
// Use the inputs embedded in the bundled program.
270-
status = torch::executor::bundled_program::LoadBundledInput(
288+
status = bundled_program::load_bundled_input(
271289
*method,
272290
file_data->data(),
273291
FLAGS_testset_idx);
@@ -278,11 +296,11 @@ HierarchicalAllocator planned_memory(
278296
} else {
279297
ET_LOG(Info, "Loading non-bundled program...\n");
280298
// Use ones-initialized inputs.
281-
auto inputs_result = torch::executor::util::prepare_input_tensors(*method);
299+
auto inputs_result = executorch::extension::prepare_input_tensors(*method);
282300
if (inputs_result.ok()) {
283301
// Will free the inputs when destroyed.
284302
inputs =
285-
std::make_unique<util::BufferCleanup>(std::move(inputs_result.get()));
303+
std::make_unique<BufferCleanup>(std::move(inputs_result.get()));
286304
}
287305
}
288306
ET_LOG(Info, "Inputs prepared.");
@@ -322,14 +340,14 @@ HierarchicalAllocator planned_memory(
322340
status = method->get_outputs(outputs.data(), outputs.size());
323341
ET_CHECK(status == Error::Ok);
324342
// Print the first and last 100 elements of long lists of scalars.
325-
std::cout << torch::executor::util::evalue_edge_items(100);
343+
std::cout << executorch::extension::evalue_edge_items(100);
326344
for (int i = 0; i < outputs.size(); ++i) {
327345
std::cout << "Output " << i << ": " << outputs[i] << std::endl;
328346
}
329347

330348
// Dump the etdump data containing profiling/debugging data to the specified
331349
// file.
332-
etdump_result result = etdump_gen.get_etdump_data();
350+
ETDumpResult result = etdump_gen.get_etdump_data();
333351
if (result.buf != nullptr && result.size > 0) {
334352
FILE* f = fopen(FLAGS_etdump_path.c_str(), "w+");
335353
fwrite((uint8_t*)result.buf, 1, result.size, f);
@@ -362,7 +380,7 @@ HierarchicalAllocator planned_memory(
362380
atol = 1e-01;
363381
rtol = 1e-01;
364382
}
365-
status = torch::executor::bundled_program::VerifyResultWithBundledExpectedOutput(
383+
status = bundled_program::verify_method_outputs(
366384
*method,
367385
file_data->data(),
368386
FLAGS_testset_idx,

0 commit comments

Comments
 (0)