Skip to content

Commit 9a56129

Browse files
Give backends the method's metadata at init (#24220) (#24220)
Summary: `BackendInitContext::method_meta()` gives a delegate's `init` the `MethodMeta` of the method being loaded, or null when the context is built without one. `Method::init` passes its own; the view is borrowed and valid only during `init`. With it, a delegate can check the method around it, such as its delegates, its instruction count and whether its inputs and outputs are memory planned, before it accepts options that depend on them. The Core ML delegate uses this later in the stack to accept IOSurface load options only for a method that is just its own call on unplanned inputs and outputs. The context carries the metadata instead of a `has_verified_single_delegate_io()` flag computed by the runtime, so each delegate applies its own rule. Differential Revision: D127716377
1 parent f7f6926 commit 9a56129

6 files changed

Lines changed: 85 additions & 4 deletions

File tree

‎runtime/backend/backend_init_context.h‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
namespace executorch {
2727
namespace ET_RUNTIME_NAMESPACE {
28+
29+
class MethodMeta;
30+
2831
/**
2932
* BackendInitContext will be used to inject runtime info for to initialize
3033
* delegate.
@@ -36,7 +39,8 @@ class BackendInitContext final {
3639
EventTracer* event_tracer = nullptr,
3740
const char* method_name = nullptr,
3841
const NamedDataMap* named_data_map = nullptr,
39-
Span<const BackendOption> runtime_specs = {})
42+
Span<const BackendOption> runtime_specs = {},
43+
const MethodMeta* method_meta = nullptr)
4044
: runtime_allocator_(runtime_allocator),
4145
#ifdef ET_EVENT_TRACER_ENABLED
4246
event_tracer_(event_tracer),
@@ -45,7 +49,8 @@ class BackendInitContext final {
4549
#endif
4650
method_name_(method_name),
4751
named_data_map_(named_data_map),
48-
runtime_specs_(runtime_specs) {
52+
runtime_specs_(runtime_specs),
53+
method_meta_(method_meta) {
4954
}
5055

5156
/** Get the runtime allocator passed from Method. It's the same runtime
@@ -94,6 +99,16 @@ class BackendInitContext final {
9499
return runtime_specs_;
95100
}
96101

102+
/**
103+
* Returns metadata for the enclosing method, or nullptr if not supplied.
104+
* The runtime provides a borrowed view valid during backend initialization.
105+
* Other callers must keep the view and its backing program alive for the
106+
* lifetime of this context.
107+
*/
108+
const MethodMeta* method_meta() const {
109+
return method_meta_;
110+
}
111+
97112
/**
98113
* Get a runtime spec value by key and type.
99114
*
@@ -135,6 +150,7 @@ class BackendInitContext final {
135150
const char* method_name_ = nullptr;
136151
const NamedDataMap* named_data_map_ = nullptr;
137152
Span<const BackendOption> runtime_specs_;
153+
const MethodMeta* method_meta_ = nullptr;
138154
};
139155

140156
} // namespace ET_RUNTIME_NAMESPACE

‎runtime/backend/test/backend_init_context_test.cpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88

99
#include <executorch/runtime/backend/backend_init_context.h>
1010
#include <executorch/runtime/backend/options.h>
11+
#include <executorch/runtime/executor/method_meta.h>
1112
#include <executorch/runtime/platform/runtime.h>
13+
#include <executorch/schema/program_generated.h>
14+
15+
#include <cstdint>
16+
#include <vector>
1217

1318
#include <gtest/gtest.h>
1419

@@ -17,6 +22,7 @@ using executorch::runtime::BackendInitContext;
1722
using executorch::runtime::BackendOption;
1823
using executorch::runtime::BackendOptions;
1924
using executorch::runtime::Error;
25+
using executorch::runtime::MethodMeta;
2026
using executorch::runtime::Span;
2127

2228
class BackendInitContextTest : public ::testing::Test {
@@ -32,6 +38,32 @@ TEST_F(BackendInitContextTest, DefaultConstructorNoRuntimeSpecs) {
3238

3339
auto specs = context.runtime_specs();
3440
EXPECT_EQ(specs.size(), 0);
41+
EXPECT_EQ(context.method_meta(), nullptr);
42+
}
43+
44+
TEST_F(BackendInitContextTest, ConstructorPreservesMethodMeta) {
45+
namespace fb = executorch_flatbuffer;
46+
flatbuffers::FlatBufferBuilder builder;
47+
const auto plan = fb::CreateExecutionPlan(
48+
builder,
49+
builder.CreateString("forward"),
50+
/*container_meta_type=*/0,
51+
builder.CreateVector(std::vector<flatbuffers::Offset<fb::EValue>>{}),
52+
builder.CreateVector(std::vector<int32_t>{}),
53+
builder.CreateVector(std::vector<int32_t>{}),
54+
/*chains=*/0,
55+
/*operators=*/0,
56+
builder.CreateVector(
57+
std::vector<flatbuffers::Offset<fb::BackendDelegate>>{}));
58+
builder.Finish(plan);
59+
const auto meta = MethodMeta::from_serialized_execution_plan(
60+
builder.GetBufferPointer(), builder.GetSize());
61+
ASSERT_TRUE(meta.ok());
62+
63+
BackendInitContext context(
64+
nullptr, nullptr, "forward", nullptr, {}, &meta.get());
65+
66+
EXPECT_EQ(context.method_meta(), &meta.get());
3567
}
3668

3769
// Test constructor with runtime specs

‎runtime/backend/test/targets.bzl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def define_common_targets():
3131
deps = [
3232
"//executorch/runtime/core:core",
3333
"//executorch/runtime/backend:interface",
34+
"//executorch/runtime/executor:program",
35+
"//executorch/schema:program",
3436
],
3537
)
3638

‎runtime/executor/method.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,7 @@ Error Method::init(
10191019
// makes it safe for errors to return without updating any state.
10201020
n_delegate_ = 0;
10211021

1022+
const auto method_meta = this->method_meta();
10221023
for (size_t i = 0; i < n_delegate; ++i) {
10231024
const auto& delegate = *delegates->Get(i);
10241025

@@ -1035,7 +1036,8 @@ Error Method::init(
10351036
/*event_tracer=*/event_tracer_,
10361037
/*method_name=*/serialization_plan_->name()->c_str(),
10371038
/*named_data_map=*/named_data_map,
1038-
/*runtime_specs=*/delegate_runtime_specs);
1039+
/*runtime_specs=*/delegate_runtime_specs,
1040+
/*method_meta=*/&method_meta);
10391041
Error err = BackendDelegate::Init(
10401042
delegate, program_, backend_init_context, &delegates_[i]);
10411043
if (err != Error::Ok) {

‎runtime/executor/test/backend_integration_test.cpp‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,34 @@ TEST_P(BackendIntegrationTest, GetMethodNameDuringInitSuccess) {
610610
ASSERT_EQ(program.error(), Error::Ok);
611611
}
612612

613+
TEST_P(BackendIntegrationTest, MethodMetaPassedToBackendInitContext) {
614+
Result<FileDataLoader> loader = FileDataLoader::from(program_path());
615+
ASSERT_EQ(loader.error(), Error::Ok);
616+
Result<Program> program = Program::load(&loader.get());
617+
ASSERT_EQ(program.error(), Error::Ok);
618+
619+
bool init_called = false;
620+
StubBackend::singleton().install_init(
621+
[&](FreeableBuffer* processed,
622+
ET_UNUSED ArrayRef<CompileSpec> compile_specs,
623+
BackendInitContext& context) -> Result<DelegateHandle*> {
624+
init_called = true;
625+
const auto* meta = context.method_meta();
626+
EXPECT_NE(meta, nullptr);
627+
if (meta != nullptr) {
628+
EXPECT_STREQ(meta->name(), "forward");
629+
EXPECT_TRUE(meta->uses_backend(StubBackend::kName));
630+
}
631+
processed->Free();
632+
return nullptr;
633+
});
634+
635+
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
636+
Result<Method> method = program->load_method("forward", &mmm.get());
637+
EXPECT_TRUE(method.ok());
638+
EXPECT_TRUE(init_called);
639+
}
640+
613641
TEST_P(BackendIntegrationTest, GetMethodNameDuringExecuteSuccess) {
614642
Result<FileDataLoader> loader = FileDataLoader::from(program_path());
615643
ASSERT_EQ(loader.error(), Error::Ok);

‎schema/targets.bzl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ def define_common_targets():
8080
"//executorch/runtime/executor/...",
8181
"//executorch/backends/native/extension/module/...",
8282
# Tests have a set up which uses raw flatbuffer.
83-
# TODO will refactor these setup steps into
83+
# TODO will refactor these setup steps into
8484
# testing utils in runtime/executor/... path
8585
"//executorch/backends/xnnpack/test/...",
86+
"//executorch/runtime/backend/test/...",
8687
],
8788
exported_headers = {
8889
OUTPUT_PROGRAM_HEADER: ":{}[{}]".format(PROGRAM_GEN_RULE_NAME, OUTPUT_PROGRAM_HEADER),

0 commit comments

Comments
 (0)