-
Notifications
You must be signed in to change notification settings - Fork 536
[ExecuTorch][#10375] Add extension.BundledModule
to Wrap extension.Module
with Bundled Program Logic
#10449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
facebook-github-bot
merged 16 commits into
gh/zhenyan-zhang-meta/5/base
from
gh/zhenyan-zhang-meta/5/head
May 7, 2025
Merged
[ExecuTorch][#10375] Add extension.BundledModule
to Wrap extension.Module
with Bundled Program Logic
#10449
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5feb8d5
[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap `extension…
f6ba0bd
Update on "[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap…
7fdd05d
Update on "[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap…
d8b016c
Update on "[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap…
zhenyan-zhang-meta ec2c131
Update on "[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap…
zhenyan-zhang-meta 2a71451
Update on "[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap…
zhenyan-zhang-meta 5b765c5
Update on "[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap…
zhenyan-zhang-meta e2c53bd
Update on "[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap…
zhenyan-zhang-meta 053d845
Update on "[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap…
zhenyan-zhang-meta f819120
Update on "[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap…
zhenyan-zhang-meta df3cb35
Update on "[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap…
zhenyan-zhang-meta 395fea4
Update on "[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap…
zhenyan-zhang-meta 5a3ff8c
Update on "[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap…
zhenyan-zhang-meta 7761ae4
Update on "[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap…
zhenyan-zhang-meta ce01aa2
Update on "[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap…
zhenyan-zhang-meta 54cf39f
Update on "[ExecuTorch][#10375] Add `extension.BundledModule` to Wrap…
zhenyan-zhang-meta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include <executorch/extension/module/bundled_module.h> | ||
|
||
#include <executorch/devtools/bundled_program/bundled_program.h> | ||
#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> | ||
|
||
namespace executorch { | ||
namespace extension { | ||
|
||
namespace { | ||
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(); | ||
return std::make_unique<BufferDataLoader>(program->data(), program->size()); | ||
} | ||
} // namespace | ||
|
||
BundledModule::BundledModule( | ||
const void* bundled_program_ptr, | ||
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( | ||
program_data_loader(bundled_program_ptr), | ||
std::move(memory_allocator), | ||
std::move(temp_allocator), | ||
std::move(event_tracer), | ||
std::move(data_map_loader)), | ||
bundled_program_ptr_(bundled_program_ptr) {} | ||
|
||
runtime::Result<std::unique_ptr<BundledModule>> BundledModule::from_file( | ||
const std::string& file_path, | ||
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) { | ||
auto data_loader_result = FileDataLoader::from(file_path.c_str()); | ||
if (!data_loader_result.ok()) { | ||
return data_loader_result.error(); | ||
} | ||
|
||
auto file_size_result = data_loader_result->size(); | ||
if (!file_size_result.ok()) { | ||
return file_size_result.error(); | ||
} | ||
|
||
size_t file_size = file_size_result.get(); | ||
auto file_data = std::make_unique<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(), | ||
std::move(memory_allocator), | ||
std::move(temp_allocator), | ||
std::move(event_tracer), | ||
std::move(data_map_loader)); | ||
|
||
bm->is_loaded_from_file_ = true; | ||
|
||
return bm; | ||
} | ||
|
||
runtime::Result<std::vector<runtime::EValue>> BundledModule::execute( | ||
const std::string& method_name, | ||
const size_t testset_idx) { | ||
ET_CHECK_OK_OR_RETURN_ERROR(load_method(method_name)); | ||
auto& method = methods_.at(method_name).method; | ||
|
||
ET_CHECK_OK_OR_RETURN_ERROR( | ||
executorch::BUNDLED_PROGRAM_NAMESPACE::load_bundled_input( | ||
*method, bundled_program_ptr_, testset_idx)); | ||
ET_CHECK_OK_OR_RETURN_ERROR(method->execute()); | ||
|
||
const auto outputs_size = method->outputs_size(); | ||
std::vector<runtime::EValue> outputs(outputs_size); | ||
ET_CHECK_OK_OR_RETURN_ERROR( | ||
method->get_outputs(outputs.data(), outputs_size)); | ||
|
||
return outputs; | ||
} | ||
|
||
runtime::Error BundledModule::verify_method_outputs( | ||
const std::string& method_name, | ||
const size_t testset_idx, | ||
double rtol, | ||
double atol) { | ||
ET_CHECK_OK_OR_RETURN_ERROR(load_method(method_name)); | ||
auto& method = methods_.at(method_name).method; | ||
return executorch::BUNDLED_PROGRAM_NAMESPACE::verify_method_outputs( | ||
*method, bundled_program_ptr_, testset_idx, rtol, atol); | ||
} | ||
|
||
} // namespace extension | ||
} // namespace executorch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <executorch/extension/module/module.h> | ||
|
||
namespace executorch { | ||
namespace extension { | ||
|
||
/** | ||
* A facade class for loading bundled programs and executing methods within | ||
* them. | ||
*/ | ||
class BundledModule : public Module { | ||
public: | ||
/** | ||
* Constructs an instance with the bundled program buffer pointer. | ||
* | ||
* This constructor reads the program from bundled program buffer to load the | ||
* module with data loader. The bundled program pointer is preserved so that | ||
* the portion outside of program is accessible. | ||
* | ||
* @param[in] bundled_program_ptr A DataLoader used for loading program data. | ||
* @param[in] memory_allocator A MemoryAllocator used for memory management. | ||
* @param[in] temp_allocator A MemoryAllocator to use when allocating | ||
* temporary data during kernel or delegate execution. | ||
* @param[in] event_tracer A EventTracer used for tracking and logging events. | ||
* @param[in] data_map_loader A DataLoader used for loading external weights. | ||
*/ | ||
explicit BundledModule( | ||
const void* bundled_program_ptr, | ||
std::unique_ptr<runtime::MemoryAllocator> memory_allocator = nullptr, | ||
std::unique_ptr<runtime::MemoryAllocator> temp_allocator = nullptr, | ||
std::unique_ptr<runtime::EventTracer> event_tracer = nullptr, | ||
std::unique_ptr<runtime::DataLoader> data_map_loader = nullptr); | ||
|
||
// Disallow copying | ||
BundledModule(const BundledModule&) = delete; | ||
BundledModule& operator=(const BundledModule&) = delete; | ||
// 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 | ||
* specified memory locking behavior. | ||
* | ||
* @param[in] file_path The path to the ExecuTorch bundled program file to | ||
* load. | ||
* @param[in] memory_allocator A MemoryAllocator used for memory management. | ||
* @param[in] temp_allocator A MemoryAllocator to use when allocating | ||
* temporary data during kernel or delegate execution. | ||
* @param[in] event_tracer A EventTracer used for tracking and logging events. | ||
* @param[in] data_map_loader A DataLoader used for loading external weights. | ||
*/ | ||
ET_NODISCARD static runtime::Result<std::unique_ptr<BundledModule>> from_file( | ||
const std::string& file_path, | ||
std::unique_ptr<runtime::MemoryAllocator> memory_allocator = nullptr, | ||
std::unique_ptr<runtime::MemoryAllocator> temp_allocator = nullptr, | ||
std::unique_ptr<runtime::EventTracer> event_tracer = nullptr, | ||
std::unique_ptr<runtime::DataLoader> data_map_loader = nullptr); | ||
|
||
using Module::execute; | ||
|
||
/** | ||
* Execute a specific method with the input value at the given `testset_idx` | ||
* from the bundle to the method. Loads the program and method before | ||
* executing if needed. | ||
* | ||
* This function is a wrapper of `load_bundled_input` in `bundled_program`. | ||
* | ||
* @param[in] method_name The name of the method to execute. | ||
* @param[in] testset_idx The index of the input value to be passed to the | ||
* method. | ||
* | ||
* @returns Return Error::Ok on a successful load, or the error happens during | ||
* execution. | ||
*/ | ||
ET_NODISCARD | ||
runtime::Result<std::vector<runtime::EValue>> execute( | ||
const std::string& method_name, | ||
const size_t testset_idx); | ||
|
||
/** | ||
* Verify the output of a specific method with the expected output from the | ||
* program bundle at the given `testset_idx`. | ||
* | ||
* This function is a wrapper of `verify_method_outputs` in `bundled_program`. | ||
* | ||
* @param[in] method_name The name of the method to extract outputs from. | ||
* @param[in] testset_idx The index of expected output needs to be compared. | ||
* @param[in] rtol Relative tolerance used for data comparsion. | ||
* @param[in] atol Absolute tolerance used for data comparsion. | ||
* | ||
* @returns Return Error::Ok if two outputs match, or the error happens during | ||
* execution. | ||
*/ | ||
ET_NODISCARD | ||
runtime::Error verify_method_outputs( | ||
const std::string& method_name, | ||
const size_t testset_idx, | ||
double rtol = 1e-5, | ||
double atol = 1e-8); | ||
|
||
private: | ||
const void* bundled_program_ptr_; | ||
bool is_loaded_from_file_ = false; | ||
}; | ||
|
||
} // namespace extension | ||
} // namespace executorch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great if we can follow the
extension.module
's pattern, which uses constructor instead of factory function, and leverageis_loaded
function for sanity check.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we can follow up in another PR to iterate on it. Will land this diff as discussed and write up potential improvements in #10375.