Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions extension/module/bundled_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@
#include <executorch/devtools/bundled_program/schema/bundled_program_schema_generated.h>
#include <executorch/extension/data_loader/buffer_data_loader.h>
#include <executorch/extension/data_loader/file_data_loader.h>
#include <executorch/extension/data_loader/shared_ptr_data_loader.h>

namespace executorch {
namespace extension {

namespace {
const flatbuffers::Vector<uint8_t>* embedded_program(
const void* bundled_program_ptr) {
return bundled_program_flatbuffer::GetBundledProgram(bundled_program_ptr)
->program();
}

std::unique_ptr<BufferDataLoader> program_data_loader(
const void* bundled_program_ptr) {
auto bundled_program =
bundled_program_flatbuffer::GetBundledProgram(bundled_program_ptr);
// the program inside the bundled program
auto program = bundled_program->program();
auto program = embedded_program(bundled_program_ptr);
return std::make_unique<BufferDataLoader>(program->data(), program->size());
}
} // namespace
Expand All @@ -35,11 +39,26 @@ BundledModule::BundledModule(
std::unique_ptr<runtime::MemoryAllocator> temp_allocator,
std::unique_ptr<runtime::EventTracer> event_tracer,
std::unique_ptr<runtime::DataLoader> data_map_loader)
: Module(
: BundledModule(
bundled_program_ptr,
program_data_loader(bundled_program_ptr),
std::move(memory_allocator),
std::move(temp_allocator),
std::move(event_tracer),
std::move(data_map_loader)) {}

BundledModule::BundledModule(
const void* bundled_program_ptr,
std::unique_ptr<runtime::DataLoader> program_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)
: Module(
std::move(program_loader),
std::move(memory_allocator),
std::move(temp_allocator),
std::move(event_tracer),
std::move(data_map_loader)),
bundled_program_ptr_(bundled_program_ptr) {}

Expand All @@ -60,22 +79,27 @@ runtime::Result<std::unique_ptr<BundledModule>> BundledModule::from_file(
}

size_t file_size = file_size_result.get();
auto file_data = std::make_unique<uint8_t[]>(file_size);
std::shared_ptr<uint8_t[]> file_data(new uint8_t[file_size]);
auto buffer_result =
data_loader_result->load_into(0, file_size, {}, file_data.get());
if (buffer_result != runtime::Error::Ok) {
return buffer_result;
}

// Pass ownership of the data to BundledModule
auto bm = std::make_unique<BundledModule>(
file_data.release(),
// The loader shares the file data, so a Program it loads can outlive this
// module.
auto program = embedded_program(file_data.get());
auto program_loader = std::make_unique<SharedPtrDataLoader>(
std::shared_ptr<void>(file_data, const_cast<uint8_t*>(program->data())),
program->size());
std::unique_ptr<BundledModule> bm(new BundledModule(
file_data.get(),
std::move(program_loader),
std::move(memory_allocator),
std::move(temp_allocator),
std::move(event_tracer),
std::move(data_map_loader));

bm->is_loaded_from_file_ = true;
std::move(data_map_loader)));
bm->file_data_ = std::move(file_data);

return bm;
}
Expand Down
16 changes: 9 additions & 7 deletions extension/module/bundled_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ class BundledModule : public Module {
// Disallow copying
BundledModule(BundledModule&&) = delete;
BundledModule& operator=(BundledModule&&) = delete;
// Default destructor
~BundledModule() {
if (is_loaded_from_file_) {
delete[] static_cast<const uint8_t*>(bundled_program_ptr_);
}
}

/**
* Constructs an instance by loading a bundled program from a file with
Expand Down Expand Up @@ -125,8 +119,16 @@ class BundledModule : public Module {
double atol = 1e-8);

private:
BundledModule(
const void* bundled_program_ptr,
std::unique_ptr<runtime::DataLoader> program_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);

const void* bundled_program_ptr_;
bool is_loaded_from_file_ = false;
std::shared_ptr<uint8_t[]> file_data_;
};

} // namespace ET_BUNDLED_MODULE_NAMESPACE
Expand Down
98 changes: 58 additions & 40 deletions extension/module/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,55 +333,73 @@ runtime::Error Module::load_internal(
ptn_ = std::move(*ptn_load_result);
return runtime::Error::Ok;
}
if (data_files_.size() > 0) {
for (const auto& data_file : data_files_) {
auto data_map_loader_result = make_data_loader(data_file, load_mode_);
if (!data_map_loader_result.ok()) {
return data_map_loader_result.error();
}
data_map_loaders_.push_back(std::move(*data_map_loader_result));
}
}

if (data_map_loaders_.size() > 0) {
for (auto i = 0; i < data_map_loaders_.size(); ++i) {
auto res_flat_tensor =
FlatTensorDataMap::load(data_map_loaders_[i].get());
if (!res_flat_tensor.ok()) {
return res_flat_tensor.error();
}
named_data_maps_.push_back(
std::make_unique<
std::remove_reference_t<decltype(*res_flat_tensor)>>(
std::move(*res_flat_tensor)));
}

// Extract raw pointers from unique_ptrs to pass to MergedDataMap::load()
std::vector<const NamedDataMap*> raw_data_maps;
raw_data_maps.reserve(named_data_maps_.size());
for (const auto& data_map : named_data_maps_) {
raw_data_maps.push_back(data_map.get());
}
auto res_merged = MergedDataMap::load(runtime::Span<const NamedDataMap*>(
raw_data_maps.data(), raw_data_maps.size()));
if (!res_merged.ok()) {
return res_merged.error();
}
merged_data_map_ =
std::make_unique<std::remove_reference_t<decltype(*res_merged)>>(
std::move(*res_merged));
}

ET_CHECK_OK_OR_RETURN_ERROR(load_data_maps());
auto res_program = Program::load(data_loader_.get(), verification);
if (!res_program.ok()) {
return res_program.error();
}
auto program =
std::make_unique<std::remove_reference_t<decltype(*res_program)>>(
std::move(*res_program));
// The program reads from its loader, so it keeps the loader alive. The
// loader goes with the program, not with the last weak pointer to it.
std::shared_ptr<runtime::DataLoader> data_loader = std::move(data_loader_);
program_ = std::shared_ptr<Program>(
program.release(), [](Program* pointer) { delete pointer; });
program.release(),
[data_loader = std::move(data_loader)](Program* pointer) mutable {
delete pointer;
data_loader.reset();
});
} else if (program_) {
// A Module made from a shared program still loads its own data maps.
ET_CHECK_OK_OR_RETURN_ERROR(load_data_maps());
}
return runtime::Error::Ok;
}

runtime::Error Module::load_data_maps() {
if (merged_data_map_ || (data_files_.empty() && data_map_loaders_.empty())) {
return runtime::Error::Ok;
}
// Open every file before keeping any, so a failed open can be retried.
std::vector<std::unique_ptr<runtime::DataLoader>> file_loaders;
file_loaders.reserve(data_files_.size());
for (const auto& data_file : data_files_) {
auto data_map_loader_result = make_data_loader(data_file, load_mode_);
if (!data_map_loader_result.ok()) {
return data_map_loader_result.error();
}
file_loaders.push_back(std::move(*data_map_loader_result));
}
for (auto& file_loader : file_loaders) {
data_map_loaders_.push_back(std::move(file_loader));
}
data_files_.clear();
named_data_maps_.clear();
for (const auto& data_map_loader : data_map_loaders_) {
auto res_flat_tensor = FlatTensorDataMap::load(data_map_loader.get());
if (!res_flat_tensor.ok()) {
return res_flat_tensor.error();
}
named_data_maps_.push_back(
std::make_unique<std::remove_reference_t<decltype(*res_flat_tensor)>>(
std::move(*res_flat_tensor)));
}

// Extract raw pointers from unique_ptrs to pass to MergedDataMap::load()
std::vector<const NamedDataMap*> raw_data_maps;
raw_data_maps.reserve(named_data_maps_.size());
for (const auto& data_map : named_data_maps_) {
raw_data_maps.push_back(data_map.get());
}
auto res_merged = MergedDataMap::load(runtime::Span<const NamedDataMap*>(
raw_data_maps.data(), raw_data_maps.size()));
if (!res_merged.ok()) {
return res_merged.error();
}
merged_data_map_ =
std::make_unique<std::remove_reference_t<decltype(*res_merged)>>(
std::move(*res_merged));
return runtime::Error::Ok;
}

Expand Down
2 changes: 2 additions & 0 deletions extension/module/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,8 @@ class Module {
LoadBackendOptionsMap backend_options_map_;
bool share_memory_arenas_;

ET_NODISCARD runtime::Error load_data_maps();

ET_NODISCARD runtime::Error load_internal(
const Program::Verification verification,
bool has_backend_options = false);
Expand Down
1 change: 1 addition & 0 deletions extension/module/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def define_common_targets():
deps = [
"//executorch/extension/data_loader:buffer_data_loader",
"//executorch/extension/data_loader:file_data_loader",
"//executorch/extension/data_loader:shared_ptr_data_loader",
"//executorch/devtools/bundled_program:runtime" + aten_suffix,
"//executorch/devtools/bundled_program/schema:bundled_program_schema_fbs",
],
Expand Down
22 changes: 22 additions & 0 deletions extension/module/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,25 @@ add_dependencies(extension_module_test generated_module_test_files)
set_property(TEST extension_module_test PROPERTY ENVIRONMENT ${test_env})

set_property(TEST extension_module_test PROPERTY ENVIRONMENT "${test_env}")

if(TARGET bundled_program)
et_cxx_test(
extension_bundled_module_test
SOURCES
bundled_module_test.cpp
${EXECUTORCH_ROOT}/extension/module/bundled_module.cpp
EXTRA_LIBS
bundled_program
extension_data_loader
extension_module_static
extension_tensor
portable_kernels
portable_ops_lib
)
add_dependencies(extension_bundled_module_test generated_module_test_files)
set_property(
TEST extension_bundled_module_test
PROPERTY ENVIRONMENT "RESOURCES_PATH=${CMAKE_CURRENT_SOURCE_DIR}/resources"
"ET_MODULE_PTE_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModuleAdd.pte"
)
endif()
14 changes: 14 additions & 0 deletions extension/module/test/bundled_module_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,17 @@ TEST_F(BundledModuleTest, TestVerifyInvalidIdx) {
bundled_module->verify_method_outputs("forward", /*testset_idx=*/10000);
EXPECT_EQ(status, Error::InvalidArgument);
}

TEST_F(BundledModuleTest, TestProgramOutlivesModuleFromFile) {
std::shared_ptr<executorch::ET_RUNTIME_NAMESPACE::Program> program;
{
auto bundled_module = BundledModule::from_file(bpte_path_.c_str());
ASSERT_EQ(bundled_module.error(), Error::Ok);
ASSERT_EQ(bundled_module.get()->load(), Error::Ok);
program = bundled_module.get()->program();
}
ASSERT_NE(program, nullptr);
auto method_name = program->get_method_name(0);
ASSERT_EQ(method_name.error(), Error::Ok);
EXPECT_STREQ(method_name.get(), "forward");
}
Loading
Loading