Skip to content

delete load_from_buffer shared ptr api from pybindings module #97

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion exir/backend/test/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def forward(self, x):
def test_backend_with_compiler_out_of_range(self, extract_segments: bool):
with self.assertRaisesRegex(
RuntimeError,
"initializing executor for method forward failed with error 0x:12",
"loading method forward failed with error 0x12",
):
self.run_model_in_unsupported_backend(extract_segments=extract_segments)

Expand Down
2 changes: 1 addition & 1 deletion exir/backend/test/test_backends_lifted.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def forward(self, x):
def test_backend_with_compiler_out_of_range(self, extract_segments: bool):
with self.assertRaisesRegex(
RuntimeError,
"initializing executor for method forward failed with error 0x:12",
"loading method forward failed with error 0x12",
):
self.run_model_in_unsupported_backend(extract_segments=extract_segments)

Expand Down
58 changes: 21 additions & 37 deletions extension/pybindings/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,23 @@ class Module final {
loader_.get(), Program::Verification::InternalConsistency);
THROW_IF_ERROR(
program.error(),
"Failed to deserialize program: 0x%" PRIx32,
"loading program failed with error: 0x%" PRIx32,
program.error());
program_ = std::make_unique<Program>(std::move(program.get()));
for (size_t i = 0; i < program_->num_methods(); ++i) {
auto name = program_->get_method_name(i);
auto name = program_->get_method_name(i).get();
// It's safe to use the same memory manager for all modules because
// we can guarantee that only one will be executing at a time.
// Everything in this module runs on a single thread.
auto executor =
std::make_unique<Executor>(program_.get(), memory_manager);
auto status = executor->init_execution_plan(name.get());
Result<Method> method = program_->load_method(name, memory_manager);
THROW_IF_ERROR(
status,
"initializing executor for method %s failed with error 0x:%" PRIx32,
name.get(),
status);
methods_.insert({std::string(name.get()), std::move(executor)});
method.error(),
"loading method %s failed with error 0x%" PRIx32,
name,
method.error());
methods_.insert(
{std::string(name),
std::make_unique<Method>(std::move(method.get()))});
}
}

Expand All @@ -114,21 +114,17 @@ class Module final {
return run_method("forward", std::forward<Types>(args)...);
}

void set_delete_memory(std::shared_ptr<char> mem_to_delete) {
mem_to_delete_ = mem_to_delete;
}

private:
run_method_return_type run_method_internal(
const std::string& method_name,
run_method_inputs_type args) {
auto& plan = methods_[method_name]->execution_plan();
auto& method = methods_[method_name];
exec_aten::ArrayRef<EValue> input_evalue_list(args.data(), args.size());

Error set_inputs_status = plan.set_inputs(input_evalue_list);
Error set_inputs_status = method->set_inputs(input_evalue_list);
THROW_IF_ERROR(
set_inputs_status,
"plan.set_inputs() for method '%s' failed with error 0x%" PRIx32,
"method->set_inputs() for method '%s' failed with error 0x%" PRIx32,
method_name.c_str(),
set_inputs_status);

Expand All @@ -145,42 +141,30 @@ class Module final {
c10::impl::ExcludeDispatchKeyGuard no_autograd(
c10::autograd_dispatch_keyset);
#endif
Error execute_status = plan.execute();
Error execute_status = method->execute();
THROW_IF_ERROR(
execute_status,
"execution_plan().execute() failed with error 0x%" PRIx32,
"method->execute() failed with error 0x%" PRIx32,
execute_status);
// process outputs
std::vector<EValue> result(plan.outputs_size());
std::vector<EValue> result(method->outputs_size());

Error get_outputs_status =
plan.get_outputs(result.data(), plan.outputs_size());
method->get_outputs(result.data(), method->outputs_size());
THROW_IF_ERROR(
get_outputs_status,
"plan.get_outputs() for method '%s' failed with error 0x%" PRIx32,
"method->get_outputs() for method '%s' failed with error 0x%" PRIx32,
method_name.c_str(),
get_outputs_status);

return result;
}

std::shared_ptr<char> mem_to_delete_; // loader_ may point to this.
std::shared_ptr<DataLoader> loader_; // program_ points to this.
std::unique_ptr<const Program> program_; // executor_ points to this.
std::unordered_map<std::string, std::unique_ptr<Executor>> methods_;
std::unique_ptr<DataLoader> loader_; // program_ points to this.
std::unique_ptr<const Program> program_; // methods_ entries points to this.
std::unordered_map<std::string, std::unique_ptr<Method>> methods_;
};

inline std::unique_ptr<Module> load_from_buffer(
std::shared_ptr<char> ptr,
size_t ptr_len,
MemoryManager* memory_manager) {
EXECUTORCH_SCOPE_PROF("load_from_buffer");
auto loader = std::make_unique<BufferDataLoader>(ptr.get(), ptr_len);
auto m = std::make_unique<Module>(std::move(loader), memory_manager);
m->set_delete_memory(std::move(ptr));
return m;
}

inline std::unique_ptr<Module> load_from_buffer(
const void* ptr,
size_t ptr_len,
Expand Down