Skip to content

Commit a7e2a01

Browse files
cccclaifacebook-github-bot
authored andcommitted
Expose mehod name as part of backend init context
Differential Revision: D65386597
1 parent 97a4600 commit a7e2a01

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

runtime/backend/backend_init_context.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ namespace runtime {
1818
*/
1919
class BackendInitContext final {
2020
public:
21-
explicit BackendInitContext(MemoryAllocator* runtime_allocator)
22-
: runtime_allocator_(runtime_allocator) {}
21+
explicit BackendInitContext(MemoryAllocator* runtime_allocator, const char* method_name)
22+
: runtime_allocator_(runtime_allocator), method_name_(method_name) {}
2323

2424
/** Get the runtime allocator passed from Method. It's the same runtime
2525
* executor used by the standard executor runtime and the life span is the
@@ -28,9 +28,14 @@ class BackendInitContext final {
2828
MemoryAllocator* get_runtime_allocator() {
2929
return runtime_allocator_;
3030
}
31+
32+
const char* get_method_name() {
33+
return method_name_;
34+
}
3135

3236
private:
3337
MemoryAllocator* runtime_allocator_ = nullptr;
38+
const char* method_name_ = nullptr;
3439
};
3540

3641
} // namespace runtime

runtime/executor/method.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ Error Method::init(executorch_flatbuffer::ExecutionPlan* s_plan) {
598598
init_state_ =
599599
InitializationState::InitializationFailed; // Until proven otherwise
600600
serialization_plan_ = s_plan;
601+
method_name_ = s_plan->name()->str().c_str();
601602
auto method_allocator = memory_manager_->method_allocator();
602603

603604
{
@@ -626,7 +627,7 @@ Error Method::init(executorch_flatbuffer::ExecutionPlan* s_plan) {
626627

627628
for (size_t i = 0; i < n_delegate; ++i) {
628629
const auto& delegate = *delegates->Get(i);
629-
BackendInitContext backend_init_context(method_allocator);
630+
BackendInitContext backend_init_context(method_allocator, method_name_);
630631
Error err = BackendDelegate::Init(
631632
delegate, program_, backend_init_context, &delegates_[i]);
632633
if (err != Error::Ok) {

runtime/executor/method.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ class Method final {
328328

329329
size_t n_chains_;
330330
Chain* chains_;
331+
const char* method_name_;
331332

332333
InitializationState init_state_;
333334

runtime/executor/test/backend_integration_test.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class StubBackend final : public BackendInterface {
5555
using InitFn = std::function<Result<DelegateHandle*>(
5656
FreeableBuffer*,
5757
ArrayRef<CompileSpec>,
58-
MemoryAllocator*)>;
58+
BackendInitContext)>;
5959
using ExecuteFn = std::function<Error(DelegateHandle*, EValue**)>;
6060
using DestroyFn = std::function<void(DelegateHandle*)>;
6161

@@ -84,7 +84,7 @@ class StubBackend final : public BackendInterface {
8484
ArrayRef<CompileSpec> compile_specs) const override {
8585
if (init_fn_) {
8686
return init_fn_.value()(
87-
processed, compile_specs, context.get_runtime_allocator());
87+
processed, compile_specs, context);
8888
}
8989
// Return a benign value otherwise.
9090
return nullptr;
@@ -351,7 +351,7 @@ TEST_P(BackendIntegrationTest, FreeingProcessedBufferSucceeds) {
351351
StubBackend::singleton().install_init(
352352
[&](FreeableBuffer* processed,
353353
ET_UNUSED ArrayRef<CompileSpec> compile_specs,
354-
ET_UNUSED MemoryAllocator* runtime_allocator)
354+
ET_UNUSED BackendInitContext backend_init_context)
355355
-> Result<DelegateHandle*> {
356356
init_called = true;
357357
processed_data = processed->data();
@@ -395,7 +395,7 @@ TEST_P(BackendIntegrationTest, EndToEndTestWithProcessedAsHandle) {
395395
StubBackend::singleton().install_init(
396396
[&](FreeableBuffer* processed,
397397
ET_UNUSED ArrayRef<CompileSpec> compile_specs,
398-
ET_UNUSED MemoryAllocator* runtime_allocator)
398+
ET_UNUSED BackendInitContext backend_init_context)
399399
-> Result<DelegateHandle*> {
400400
init_processed = processed;
401401
return processed;
@@ -492,7 +492,7 @@ TEST_P(BackendIntegrationTest, SegmentInfoIsPassedIntoDataLoader) {
492492
StubBackend::singleton().install_init(
493493
[&](FreeableBuffer* processed,
494494
ET_UNUSED ArrayRef<CompileSpec> compile_specs,
495-
ET_UNUSED MemoryAllocator* runtime_allocator)
495+
ET_UNUSED BackendInitContext backend_init_context)
496496
-> Result<DelegateHandle*> {
497497
processed_data = processed->data();
498498
processed->Free();
@@ -528,6 +528,25 @@ TEST_P(BackendIntegrationTest, SegmentInfoIsPassedIntoDataLoader) {
528528
EXPECT_EQ(backend_load_was_called, using_segments());
529529
}
530530

531+
TEST_P(BackendIntegrationTest, GetMethodNameSuccess) {
532+
Result<FileDataLoader> loader = FileDataLoader::from(program_path());
533+
ASSERT_EQ(loader.error(), Error::Ok);
534+
const void* processed_data = nullptr;
535+
StubBackend::singleton().install_init(
536+
[&](FreeableBuffer* processed,
537+
ET_UNUSED ArrayRef<CompileSpec> compile_specs,
538+
ET_UNUSED BackendInitContext backend_init_context)
539+
-> Result<DelegateHandle*> {
540+
auto method_name = backend_init_context.get_method_name();
541+
EXPECT_EQ(method_name, "forward");
542+
processed_data = processed->data();
543+
return nullptr;
544+
});
545+
Result<Program> program = Program::load(&loader.get());
546+
ASSERT_EQ(program.error(), Error::Ok);
547+
548+
}
549+
531550
// TODO: Add more tests for the runtime-to-backend interface. E.g.:
532551
// - Errors during init() or execute() result in runtime init/execution failures
533552
// - Correct values are passed to init()/execute()
@@ -606,7 +625,7 @@ TEST_P(DelegateDataAlignmentTest, ExpectedDataAlignment) {
606625
StubBackend::singleton().install_init(
607626
[&](FreeableBuffer* processed,
608627
ET_UNUSED ArrayRef<CompileSpec> compile_specs,
609-
ET_UNUSED MemoryAllocator* runtime_allocator)
628+
ET_UNUSED BackendInitContext backend_init_context)
610629
-> Result<DelegateHandle*> {
611630
processed_data = processed->data();
612631
return nullptr;

0 commit comments

Comments
 (0)