Skip to content
7 changes: 4 additions & 3 deletions extension/module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ else()
endif()
target_link_libraries(
extension_module PRIVATE executorch_core extension_data_loader
extension_flat_tensor
extension_flat_tensor extension_named_data_map
)
target_include_directories(
extension_module PUBLIC ${_common_include_directories}
Expand All @@ -42,8 +42,9 @@ target_compile_options(
# after cleaning up CMake targets.
add_library(extension_module_static STATIC ${_extension_module__srcs})
target_link_libraries(
extension_module_static PRIVATE executorch_core extension_data_loader
extension_flat_tensor
extension_module_static
PRIVATE executorch_core extension_data_loader extension_flat_tensor
extension_named_data_map
)
target_include_directories(
extension_module_static PUBLIC ${_common_include_directories}
Expand Down
26 changes: 15 additions & 11 deletions extension/module/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <executorch/extension/data_loader/mmap_data_loader.h>
#include <executorch/extension/flat_tensor/flat_tensor_data_map.h>
#include <executorch/extension/memory_allocator/malloc_memory_allocator.h>
#include <executorch/extension/named_data_map/merged_data_map.h>
#include <executorch/runtime/platform/runtime.h>

/**
Expand Down Expand Up @@ -155,24 +156,27 @@ runtime::Error Module::load(const Program::Verification verification) {
data_loader_ = ET_UNWRAP(make_data_loader(file_path_, load_mode_));
}
if (data_files_.size() > 0) {
ET_CHECK_OR_RETURN_ERROR(
data_files_.size() == 1,
NotImplemented,
"Multiple named data map paths are not supported yet.");
for (const auto& data_file : data_files_) {
data_map_loaders_.push_back(
ET_UNWRAP(make_data_loader(data_file, load_mode_)));
}
}

if (data_map_loaders_.size() > 0) {
ET_CHECK_OR_RETURN_ERROR(
data_map_loaders_.size() == 1 && merged_data_map_ == nullptr,
NotImplemented,
"Multiple named data map loaders are not supported yet.");
// TODO(lfq): support multiple named data map loaders.
merged_data_map_ =
ET_UNWRAP_UNIQUE(FlatTensorDataMap::load(data_map_loaders_[0].get()));
for (auto i = 0; i < data_map_loaders_.size(); ++i) {
named_data_maps_.push_back(ET_UNWRAP_UNIQUE(
FlatTensorDataMap::load(data_map_loaders_[i].get())));
}

// 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());
}
merged_data_map_ = ET_UNWRAP_UNIQUE(
MergedDataMap::load(runtime::Span<const NamedDataMap*>(
raw_data_maps.data(), raw_data_maps.size())));
}

auto program =
Expand Down
1 change: 1 addition & 0 deletions extension/module/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def define_common_targets():
"//executorch/extension/data_loader:file_data_loader",
"//executorch/extension/data_loader:mmap_data_loader",
"//executorch/extension/flat_tensor:flat_tensor_data_map" + aten_suffix,
"//executorch/extension/named_data_map:merged_data_map" + aten_suffix,
],
exported_deps = [
"//executorch/runtime/executor:program_no_prim_ops" + aten_suffix,
Expand Down
21 changes: 15 additions & 6 deletions extension/module/test/module_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ class ModuleTest : public ::testing::Test {
model_path_ = std::getenv("ET_MODULE_ADD_PATH");
add_mul_path_ = std::getenv("ET_MODULE_ADD_MUL_PROGRAM_PATH");
add_mul_data_path_ = std::getenv("ET_MODULE_ADD_MUL_DATA_PATH");
linear_path_ = std::getenv("ET_MODULE_LINEAR_PROGRAM_PATH");
linear_data_path_ = std::getenv("ET_MODULE_LINEAR_DATA_PATH");
}

static inline std::string model_path_;
static inline std::string add_mul_path_;
static inline std::string add_mul_data_path_;
static inline std::string linear_path_;
static inline std::string linear_data_path_;
};

TEST_F(ModuleTest, TestLoad) {
Expand Down Expand Up @@ -532,16 +536,21 @@ TEST_F(ModuleTest, TestPTD) {
}

TEST_F(ModuleTest, TestPTD_Multiple) {
std::vector<std::string> data_files = {add_mul_data_path_};
Module module(add_mul_path_, data_files);

ASSERT_EQ(module.load_method("forward"), Error::Ok);
std::vector<std::string> data_files = {add_mul_data_path_, linear_data_path_};

// Create module with add mul.
Module module_add_mul(add_mul_path_, data_files);
ASSERT_EQ(module_add_mul.load_method("forward"), Error::Ok);
auto tensor = make_tensor_ptr({2, 2}, {2.f, 3.f, 4.f, 2.f});
ASSERT_EQ(module.forward(tensor).error(), Error::Ok);
ASSERT_EQ(module_add_mul.forward(tensor).error(), Error::Ok);

// Confirm that the data_file is not std::move'd away.
ASSERT_EQ(std::strcmp(data_files[0].c_str(), add_mul_data_path_.c_str()), 0);
ASSERT_EQ(std::strcmp(data_files[1].c_str(), linear_data_path_.c_str()), 0);

// TODO(lfq): add test when merge capability is supported.
// Create module with linear.
Module module_linear(linear_path_, data_files);
ASSERT_EQ(module_linear.load_method("forward"), Error::Ok);
auto tensor2 = make_tensor_ptr({3}, {2.f, 3.f, 4.f});
ASSERT_EQ(module_linear.forward(tensor2).error(), Error::Ok);
}
2 changes: 2 additions & 0 deletions extension/module/test/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def define_common_targets(is_fbcode=False):
"ET_MODULE_ADD_PATH": "$(location fbcode//executorch/test/models:exported_programs[ModuleAdd.pte])",
"ET_MODULE_ADD_MUL_PROGRAM_PATH": "$(location fbcode//executorch/test/models:exported_program_and_data[ModuleAddMul.pte])",
"ET_MODULE_ADD_MUL_DATA_PATH": "$(location fbcode//executorch/test/models:exported_program_and_data[ModuleAddMul.ptd])",
"ET_MODULE_LINEAR_PROGRAM_PATH": "$(location fbcode//executorch/test/models:exported_program_and_data[ModuleLinear.pte])",
"ET_MODULE_LINEAR_DATA_PATH": "$(location fbcode//executorch/test/models:exported_program_and_data[ModuleLinear.ptd])",
"ET_MODULE_SHARED_STATE": "$(location fbcode//executorch/test/models:exported_programs[ModuleSharedState.pte])",
}

Expand Down
Loading