Skip to content

Commit 5d0eb73

Browse files
committed
Use merged data map in module
Differential Revision: [D83799869](https://our.internmc.facebook.com/intern/diff/D83799869/) ghstack-source-id: 313776962 Pull Request resolved: #14767
1 parent 7819f9f commit 5d0eb73

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

extension/module/module.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <executorch/extension/data_loader/mmap_data_loader.h>
1313
#include <executorch/extension/flat_tensor/flat_tensor_data_map.h>
1414
#include <executorch/extension/memory_allocator/malloc_memory_allocator.h>
15+
#include <executorch/extension/named_data_map/merged_data_map.h>
1516
#include <executorch/runtime/platform/runtime.h>
1617

1718
/**
@@ -155,24 +156,25 @@ runtime::Error Module::load(const Program::Verification verification) {
155156
data_loader_ = ET_UNWRAP(make_data_loader(file_path_, load_mode_));
156157
}
157158
if (data_files_.size() > 0) {
158-
ET_CHECK_OR_RETURN_ERROR(
159-
data_files_.size() == 1,
160-
NotImplemented,
161-
"Multiple named data map paths are not supported yet.");
162159
for (const auto& data_file : data_files_) {
163160
data_map_loaders_.push_back(
164161
ET_UNWRAP(make_data_loader(data_file, load_mode_)));
165162
}
166163
}
167164

168165
if (data_map_loaders_.size() > 0) {
169-
ET_CHECK_OR_RETURN_ERROR(
170-
data_map_loaders_.size() == 1 && merged_data_map_ == nullptr,
171-
NotImplemented,
172-
"Multiple named data map loaders are not supported yet.");
173-
// TODO(lfq): support multiple named data map loaders.
174-
merged_data_map_ =
175-
ET_UNWRAP_UNIQUE(FlatTensorDataMap::load(data_map_loaders_[0].get()));
166+
for (auto i = 0; i < data_map_loaders_.size(); ++i) {
167+
named_data_maps_.push_back(ET_UNWRAP_UNIQUE(
168+
FlatTensorDataMap::load(data_map_loaders_[i].get())));
169+
}
170+
171+
// Extract raw pointers from unique_ptrs to pass to MergedDataMap::load()
172+
std::vector<const NamedDataMap*> raw_data_maps;
173+
raw_data_maps.reserve(named_data_maps_.size());
174+
for (const auto& data_map : named_data_maps_) {
175+
raw_data_maps.push_back(data_map.get());
176+
}
177+
merged_data_map_ = ET_UNWRAP_UNIQUE(MergedDataMap::load(raw_data_maps));
176178
}
177179

178180
auto program =

extension/module/targets.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def define_common_targets():
2626
"//executorch/extension/data_loader:file_data_loader",
2727
"//executorch/extension/data_loader:mmap_data_loader",
2828
"//executorch/extension/flat_tensor:flat_tensor_data_map" + aten_suffix,
29+
"//executorch/extension/named_data_map:merged_data_map" + aten_suffix,
2930
],
3031
exported_deps = [
3132
"//executorch/runtime/executor:program_no_prim_ops" + aten_suffix,

extension/module/test/module_test.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ class ModuleTest : public ::testing::Test {
2626
model_path_ = std::getenv("ET_MODULE_ADD_PATH");
2727
add_mul_path_ = std::getenv("ET_MODULE_ADD_MUL_PROGRAM_PATH");
2828
add_mul_data_path_ = std::getenv("ET_MODULE_ADD_MUL_DATA_PATH");
29+
linear_path_ = std::getenv("ET_MODULE_LINEAR_PROGRAM_PATH");
30+
linear_data_path_ = std::getenv("ET_MODULE_LINEAR_DATA_PATH");
2931
}
3032

3133
static inline std::string model_path_;
3234
static inline std::string add_mul_path_;
3335
static inline std::string add_mul_data_path_;
36+
static inline std::string linear_path_;
37+
static inline std::string linear_data_path_;
3438
};
3539

3640
TEST_F(ModuleTest, TestLoad) {
@@ -532,16 +536,21 @@ TEST_F(ModuleTest, TestPTD) {
532536
}
533537

534538
TEST_F(ModuleTest, TestPTD_Multiple) {
535-
std::vector<std::string> data_files = {add_mul_data_path_};
536-
Module module(add_mul_path_, data_files);
537-
538-
ASSERT_EQ(module.load_method("forward"), Error::Ok);
539-
539+
std::vector<std::string> data_files = {add_mul_data_path_, linear_data_path_};
540+
541+
// Create module with add mul.
542+
Module module_add_mul(add_mul_path_, data_files);
543+
ASSERT_EQ(module_add_mul.load_method("forward"), Error::Ok);
540544
auto tensor = make_tensor_ptr({2, 2}, {2.f, 3.f, 4.f, 2.f});
541-
ASSERT_EQ(module.forward(tensor).error(), Error::Ok);
545+
ASSERT_EQ(module_add_mul.forward(tensor).error(), Error::Ok);
542546

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

546-
// TODO(lfq): add test when merge capability is supported.
551+
// Create module with linear.
552+
Module module_linear(linear_path_, data_files);
553+
ASSERT_EQ(module_linear.load_method("forward"), Error::Ok);
554+
auto tensor2 = make_tensor_ptr({3}, {2.f, 3.f, 4.f});
555+
ASSERT_EQ(module_linear.forward(tensor2).error(), Error::Ok);
547556
}

extension/module/test/targets.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def define_common_targets(is_fbcode=False):
1919
"ET_MODULE_ADD_PATH": "$(location fbcode//executorch/test/models:exported_programs[ModuleAdd.pte])",
2020
"ET_MODULE_ADD_MUL_PROGRAM_PATH": "$(location fbcode//executorch/test/models:exported_program_and_data[ModuleAddMul.pte])",
2121
"ET_MODULE_ADD_MUL_DATA_PATH": "$(location fbcode//executorch/test/models:exported_program_and_data[ModuleAddMul.ptd])",
22+
"ET_MODULE_LINEAR_PROGRAM_PATH": "$(location fbcode//executorch/test/models:exported_program_and_data[ModuleLinear.pte])",
23+
"ET_MODULE_LINEAR_DATA_PATH": "$(location fbcode//executorch/test/models:exported_program_and_data[ModuleLinear.ptd])",
2224
"ET_MODULE_SHARED_STATE": "$(location fbcode//executorch/test/models:exported_programs[ModuleSharedState.pte])",
2325
}
2426

0 commit comments

Comments
 (0)