@@ -161,10 +161,31 @@ void setup_output_storage(
161161inline std::unique_ptr<Module> load_module_from_buffer (
162162 const void * ptr,
163163 size_t ptr_len,
164+ std::optional<const std::string>& data_map_path,
164165 std::unique_ptr<runtime::EventTracer> event_tracer,
165166 Program::Verification program_verification) {
166167 EXECUTORCH_SCOPE_PROF (" load_module_from_buffer" );
167168 auto loader = std::make_unique<BufferDataLoader>(ptr, ptr_len);
169+
170+ if (data_map_path.has_value ()) {
171+ Result<MmapDataLoader> data_map_loader_res = MmapDataLoader::from (
172+ data_map_path->c_str (),
173+ MmapDataLoader::MlockConfig::UseMlockIgnoreErrors);
174+ THROW_IF_ERROR (
175+ data_map_loader_res.error (),
176+ " Failed to create MmapDataLoader from file %s, error: 0x:%" PRIx32,
177+ data_map_path->c_str (),
178+ static_cast <uint32_t >(data_map_loader_res.error ()));
179+ auto data_map_loader =
180+ std::make_unique<MmapDataLoader>(std::move (data_map_loader_res.get ()));
181+ return std::make_unique<Module>(
182+ std::move (loader),
183+ nullptr , // memory_allocator
184+ nullptr , // temp_allocator
185+ std::move (event_tracer), // event_tracer
186+ std::move (data_map_loader)); // data_map_loader
187+ }
188+
168189 return std::make_unique<Module>(
169190 std::move (loader),
170191 nullptr , // memory_allocator
@@ -504,6 +525,7 @@ struct PyMethodMeta final {
504525struct PyModule final {
505526 explicit PyModule (
506527 const py::bytes& buffer,
528+ std::optional<const std::string>& data_path,
507529 bool enable_etdump,
508530 size_t debug_buffer_size = 0 ,
509531 Program::Verification program_verification =
@@ -512,12 +534,14 @@ struct PyModule final {
512534 module_(load_module_from_buffer(
513535 buffer.cast<std::string_view>().data(),
514536 py::len(buffer),
537+ data_path,
515538 setup_event_tracer(enable_etdump, debug_buffer_size),
516539 program_verification)) {}
517540
518541 explicit PyModule (
519542 const void * ptr,
520543 size_t ptr_len,
544+ std::optional<const std::string>& data_path,
521545 bool enable_etdump,
522546 size_t debug_buffer_size = 0 ,
523547 Program::Verification program_verification =
@@ -526,6 +550,7 @@ struct PyModule final {
526550 module_(load_module_from_buffer(
527551 ptr,
528552 ptr_len,
553+ data_path,
529554 setup_event_tracer (enable_etdump, debug_buffer_size),
530555 program_verification)) {}
531556
@@ -551,12 +576,13 @@ struct PyModule final {
551576 // Module is only valid as long as the python buffer is alive.
552577 static std::unique_ptr<PyModule> load_from_buffer (
553578 const py::bytes& buffer,
579+ std::optional<const std::string>& data_path,
554580 bool enable_etdump,
555581 size_t debug_buffer_size = 0 ,
556582 Program::Verification program_verification =
557583 Program::Verification::InternalConsistency) {
558584 return std::make_unique<PyModule>(
559- buffer, enable_etdump, debug_buffer_size, program_verification);
585+ buffer, data_path, enable_etdump, debug_buffer_size, program_verification);
560586 }
561587
562588 static std::unique_ptr<PyModule> load_from_file (
@@ -576,11 +602,13 @@ struct PyModule final {
576602
577603 static std::unique_ptr<PyModule> load_from_bundled_program (
578604 PyBundledModule& m,
605+ std::optional<const std::string>& data_path,
579606 bool enable_etdump,
580607 size_t debug_buffer_size = 0 ) {
581608 return std::make_unique<PyModule>(
582609 m.get_program_ptr (),
583610 m.get_program_len (),
611+ data_path,
584612 enable_etdump,
585613 debug_buffer_size);
586614 }
@@ -1390,6 +1418,7 @@ PYBIND11_MODULE(EXECUTORCH_PYTHON_MODULE_NAME, m) {
13901418 " _load_for_executorch_from_buffer" ,
13911419 &PyModule::load_from_buffer,
13921420 py::arg (" buffer" ),
1421+ py::arg (" data_path" ) = std::nullopt ,
13931422 py::arg (" enable_etdump" ) = false ,
13941423 py::arg (" debug_buffer_size" ) = 0 ,
13951424 py::arg (" program_verification" ) =
@@ -1399,6 +1428,7 @@ PYBIND11_MODULE(EXECUTORCH_PYTHON_MODULE_NAME, m) {
13991428 " _load_for_executorch_from_bundled_program" ,
14001429 &PyModule::load_from_bundled_program,
14011430 py::arg (" ptr" ),
1431+ py::arg (" data_path" ) = std::nullopt ,
14021432 py::arg (" enable_etdump" ) = false ,
14031433 py::arg (" debug_buffer_size" ) = 0 ,
14041434 call_guard);
0 commit comments