Skip to content

Expose mehod name as part of backend init context #6622

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
merged 1 commit into from
Nov 13, 2024
Merged
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
15 changes: 13 additions & 2 deletions runtime/backend/backend_execution_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ class BackendExecutionContext final {
public:
BackendExecutionContext(
EventTracer* event_tracer = nullptr,
MemoryAllocator* temp_allocator = nullptr)
: event_tracer_(event_tracer), temp_allocator_(temp_allocator) {}
MemoryAllocator* temp_allocator = nullptr,
const char* method_name = nullptr)
: event_tracer_(event_tracer),
temp_allocator_(temp_allocator),
method_name_(method_name) {}

/**
* Returns a pointer to an instance of EventTracer to do profiling/debugging
Expand Down Expand Up @@ -52,9 +55,17 @@ class BackendExecutionContext final {
return temp_allocator_;
}

/**
* Get the name of the executing method from the ExecuTorch runtime.
*/
const char* get_method_name() const {
return method_name_;
}

private:
EventTracer* event_tracer_ = nullptr;
MemoryAllocator* temp_allocator_ = nullptr;
const char* method_name_ = nullptr;
};

} // namespace runtime
Expand Down
18 changes: 16 additions & 2 deletions runtime/backend/backend_init_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ namespace runtime {
*/
class BackendInitContext final {
public:
explicit BackendInitContext(MemoryAllocator* runtime_allocator)
: runtime_allocator_(runtime_allocator) {}
explicit BackendInitContext(
MemoryAllocator* runtime_allocator,
const char* method_name = nullptr)
: runtime_allocator_(runtime_allocator), method_name_(method_name) {}

/** Get the runtime allocator passed from Method. It's the same runtime
* executor used by the standard executor runtime and the life span is the
Expand All @@ -29,8 +31,20 @@ class BackendInitContext final {
return runtime_allocator_;
}

/** Get the loaded method name from ExecuTorch runtime. Usually it's
* "forward", however, if there are multiple methods in the .pte file, it can
* be different. One example is that we may have prefill and decode methods in
* the same .pte file. In this case, when client loads "prefill" method, the
* `get_method_name` function will return "prefill", when client loads
* "decode" method, the `get_method_name` function will return "decode".
*/
const char* get_method_name() const {
return method_name_;
}

private:
MemoryAllocator* runtime_allocator_ = nullptr;
const char* method_name_ = nullptr;
};

} // namespace runtime
Expand Down
9 changes: 6 additions & 3 deletions runtime/executor/method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,9 @@ Error Method::init(executorch_flatbuffer::ExecutionPlan* s_plan) {

for (size_t i = 0; i < n_delegate; ++i) {
const auto& delegate = *delegates->Get(i);
BackendInitContext backend_init_context(method_allocator);
BackendInitContext backend_init_context(
method_allocator,
/*method_name=*/serialization_plan_->name()->c_str());
Error err = BackendDelegate::Init(
delegate, program_, backend_init_context, &delegates_[i]);
if (err != Error::Ok) {
Expand Down Expand Up @@ -1097,8 +1099,9 @@ Error Method::execute_instruction() {
n_delegate_,
step_state_.instr_idx);
BackendExecutionContext backend_execution_context(
/*event_tracer*/ event_tracer_,
/*temp_allocator*/ temp_allocator_);
/*event_tracer=*/event_tracer_,
/*temp_allocator=*/temp_allocator_,
/*method_name=*/serialization_plan_->name()->c_str());
err = delegates_[delegate_idx].Execute(
backend_execution_context,
chain.argument_lists_[step_state_.instr_idx].data());
Expand Down
44 changes: 43 additions & 1 deletion runtime/executor/test/backend_integration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class StubBackend final : public BackendInterface {
}

Error execute(
ET_UNUSED BackendExecutionContext& context,
BackendExecutionContext& context,
DelegateHandle* handle,
EValue** args) const override {
if (execute_fn_) {
Expand Down Expand Up @@ -530,6 +530,48 @@ TEST_P(BackendIntegrationTest, SegmentInfoIsPassedIntoDataLoader) {
EXPECT_EQ(backend_load_was_called, using_segments());
}

TEST_P(BackendIntegrationTest, GetMethodNameDuringInitSuccess) {
Result<FileDataLoader> loader = FileDataLoader::from(program_path());
ASSERT_EQ(loader.error(), Error::Ok);
const void* processed_data = nullptr;
StubBackend::singleton().install_init(
[&](FreeableBuffer* processed,
ET_UNUSED ArrayRef<CompileSpec> compile_specs,
ET_UNUSED BackendInitContext& backend_init_context)
-> Result<DelegateHandle*> {
auto method_name = backend_init_context.get_method_name();
// Ensure that we can get the method name during init via context
EXPECT_STREQ(method_name, "forward");
processed_data = processed->data();
return nullptr;
});
Result<Program> program = Program::load(&loader.get());
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
Result<Method> method = program->load_method("forward", &mmm.get());
EXPECT_TRUE(method.ok());
ASSERT_EQ(program.error(), Error::Ok);
}

TEST_P(BackendIntegrationTest, GetMethodNameDuringExecuteSuccess) {
Result<FileDataLoader> loader = FileDataLoader::from(program_path());
ASSERT_EQ(loader.error(), Error::Ok);
StubBackend::singleton().install_execute(
[&](BackendExecutionContext& backend_execution_context,
ET_UNUSED DelegateHandle* handle,
ET_UNUSED EValue** args) -> Error {
// Ensure that we can get the method name during execution via context
auto method_name = backend_execution_context.get_method_name();
EXPECT_STREQ(method_name, "forward");
return Error::Ok;
});
Result<Program> program = Program::load(&loader.get());
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
Result<Method> method = program->load_method("forward", &mmm.get());
EXPECT_TRUE(method.ok());
Error err = method->execute();
ASSERT_EQ(err, Error::Ok);
}

// TODO: Add more tests for the runtime-to-backend interface. E.g.:
// - Errors during init() or execute() result in runtime init/execution failures
// - Correct values are passed to init()/execute()
Expand Down
Loading