|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <executorch/extension/module/bundled_module.h> |
| 10 | + |
| 11 | +#include <executorch/devtools/bundled_program/bundled_program.h> |
| 12 | +#include <executorch/devtools/bundled_program/schema/bundled_program_schema_generated.h> |
| 13 | +#include <executorch/extension/data_loader/buffer_data_loader.h> |
| 14 | +#include <executorch/extension/data_loader/file_data_loader.h> |
| 15 | + |
| 16 | +namespace executorch { |
| 17 | +namespace extension { |
| 18 | + |
| 19 | +namespace { |
| 20 | +std::unique_ptr<BufferDataLoader> program_data_loader( |
| 21 | + const void* bundled_program_ptr) { |
| 22 | + auto bundled_program = |
| 23 | + bundled_program_flatbuffer::GetBundledProgram(bundled_program_ptr); |
| 24 | + // the program inside the bundled program |
| 25 | + auto program = bundled_program->program(); |
| 26 | + return std::make_unique<BufferDataLoader>(program->data(), program->size()); |
| 27 | +} |
| 28 | +} // namespace |
| 29 | + |
| 30 | +BundledModule::BundledModule( |
| 31 | + const void* bundled_program_ptr, |
| 32 | + std::unique_ptr<runtime::MemoryAllocator> memory_allocator, |
| 33 | + std::unique_ptr<runtime::MemoryAllocator> temp_allocator, |
| 34 | + std::unique_ptr<runtime::EventTracer> event_tracer, |
| 35 | + std::unique_ptr<runtime::DataLoader> data_map_loader) |
| 36 | + : Module( |
| 37 | + program_data_loader(bundled_program_ptr), |
| 38 | + std::move(memory_allocator), |
| 39 | + std::move(temp_allocator), |
| 40 | + std::move(event_tracer), |
| 41 | + std::move(data_map_loader)), |
| 42 | + bundled_program_ptr_(bundled_program_ptr) {} |
| 43 | + |
| 44 | +runtime::Result<std::unique_ptr<BundledModule>> BundledModule::from_file( |
| 45 | + const std::string& file_path, |
| 46 | + std::unique_ptr<runtime::MemoryAllocator> memory_allocator, |
| 47 | + std::unique_ptr<runtime::MemoryAllocator> temp_allocator, |
| 48 | + std::unique_ptr<runtime::EventTracer> event_tracer, |
| 49 | + std::unique_ptr<runtime::DataLoader> data_map_loader) { |
| 50 | + auto data_loader_result = FileDataLoader::from(file_path.c_str()); |
| 51 | + if (!data_loader_result.ok()) { |
| 52 | + return data_loader_result.error(); |
| 53 | + } |
| 54 | + |
| 55 | + auto file_size_result = data_loader_result->size(); |
| 56 | + if (!file_size_result.ok()) { |
| 57 | + return file_size_result.error(); |
| 58 | + } |
| 59 | + |
| 60 | + size_t file_size = file_size_result.get(); |
| 61 | + auto file_data = std::make_unique<uint8_t[]>(file_size); |
| 62 | + auto buffer_result = |
| 63 | + data_loader_result->load_into(0, file_size, {}, file_data.get()); |
| 64 | + if (buffer_result != runtime::Error::Ok) { |
| 65 | + return buffer_result; |
| 66 | + } |
| 67 | + |
| 68 | + // Pass ownership of the data to BundledModule |
| 69 | + auto bm = std::make_unique<BundledModule>( |
| 70 | + file_data.release(), |
| 71 | + std::move(memory_allocator), |
| 72 | + std::move(temp_allocator), |
| 73 | + std::move(event_tracer), |
| 74 | + std::move(data_map_loader)); |
| 75 | + |
| 76 | + bm->is_loaded_from_file_ = true; |
| 77 | + |
| 78 | + return bm; |
| 79 | +} |
| 80 | + |
| 81 | +runtime::Result<std::vector<runtime::EValue>> BundledModule::execute( |
| 82 | + const std::string& method_name, |
| 83 | + const size_t testset_idx) { |
| 84 | + ET_CHECK_OK_OR_RETURN_ERROR(load_method(method_name)); |
| 85 | + auto& method = methods_.at(method_name).method; |
| 86 | + |
| 87 | + ET_CHECK_OK_OR_RETURN_ERROR( |
| 88 | + executorch::BUNDLED_PROGRAM_NAMESPACE::load_bundled_input( |
| 89 | + *method, bundled_program_ptr_, testset_idx)); |
| 90 | + ET_CHECK_OK_OR_RETURN_ERROR(method->execute()); |
| 91 | + |
| 92 | + const auto outputs_size = method->outputs_size(); |
| 93 | + std::vector<runtime::EValue> outputs(outputs_size); |
| 94 | + ET_CHECK_OK_OR_RETURN_ERROR( |
| 95 | + method->get_outputs(outputs.data(), outputs_size)); |
| 96 | + |
| 97 | + return outputs; |
| 98 | +} |
| 99 | + |
| 100 | +runtime::Error BundledModule::verify_method_outputs( |
| 101 | + const std::string& method_name, |
| 102 | + const size_t testset_idx, |
| 103 | + double rtol, |
| 104 | + double atol) { |
| 105 | + ET_CHECK_OK_OR_RETURN_ERROR(load_method(method_name)); |
| 106 | + auto& method = methods_.at(method_name).method; |
| 107 | + return executorch::BUNDLED_PROGRAM_NAMESPACE::verify_method_outputs( |
| 108 | + *method, bundled_program_ptr_, testset_idx, rtol, atol); |
| 109 | +} |
| 110 | + |
| 111 | +} // namespace extension |
| 112 | +} // namespace executorch |
0 commit comments