Skip to content

Commit a7632fb

Browse files
GregoryComerfacebook-github-bot
authored andcommitted
Add unique method ID
Summary: Add an auto-incremented identifier field to the Method class and pass into BackendInitContext. This allows backends to disambiguated between methods with the same name (two models with a "forward" method, for example). Differential Revision: D78697798
1 parent 413dee4 commit a7632fb

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

runtime/backend/backend_init_context.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class BackendInitContext final {
2121
public:
2222
explicit BackendInitContext(
2323
MemoryAllocator* runtime_allocator,
24+
size_t method_id,
2425
EventTracer* event_tracer = nullptr,
2526
const char* method_name = nullptr,
2627
const NamedDataMap* named_data_map = nullptr)
@@ -31,6 +32,7 @@ class BackendInitContext final {
3132
event_tracer_(nullptr),
3233
#endif
3334
method_name_(method_name),
35+
method_id_(method_id),
3436
named_data_map_(named_data_map) {
3537
}
3638

@@ -61,6 +63,14 @@ class BackendInitContext final {
6163
const char* get_method_name() const {
6264
return method_name_;
6365
}
66+
67+
/** Get the method identifier of the loaded method. This corresponds to the
68+
* id() method on the Method object, and can be used to disambiguate between
69+
* different methods with the same name.
70+
*/
71+
const size_t method_id() const {
72+
return method_id_;
73+
}
6474

6575
/** Get the named data map from ExecuTorch runtime.
6676
* This provides a way for backends to retrieve data blobs by key.
@@ -73,6 +83,7 @@ class BackendInitContext final {
7383
MemoryAllocator* runtime_allocator_ = nullptr;
7484
EventTracer* event_tracer_ = nullptr;
7585
const char* method_name_ = nullptr;
86+
size_t method_id_ = 0;
7687
const NamedDataMap* named_data_map_ = nullptr;
7788
};
7889

runtime/executor/method.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ Result<bool> parse_cond_value(const EValue& cond_value) {
293293

294294
} // namespace
295295

296+
// Initialize static method ID counter.
297+
size_t Method::next_id_ = 0;
298+
296299
Result<size_t> Method::get_num_external_constants() {
297300
auto flatbuffer_values = serialization_plan_->values();
298301
size_t n_value = flatbuffer_values->size();
@@ -842,6 +845,7 @@ Error Method::init(
842845
const auto& delegate = *delegates->Get(i);
843846
BackendInitContext backend_init_context(
844847
method_allocator,
848+
/*method_id=*/id(),
845849
/*event_tracer=*/event_tracer_,
846850
/*method_name=*/serialization_plan_->name()->c_str(),
847851
/*named_data_map=*/named_data_map);

runtime/executor/method.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class Method final {
6565
* and leaves `rhs` in an uninitialized state.
6666
*/
6767
Method(Method&& rhs) noexcept
68-
: step_state_(rhs.step_state_),
68+
: id_(rhs.id_),
69+
step_state_(rhs.step_state_),
6970
program_(rhs.program_),
7071
memory_manager_(rhs.memory_manager_),
7172
temp_allocator_(rhs.temp_allocator_),
@@ -103,6 +104,18 @@ class Method final {
103104
rhs.n_chains_ = 0;
104105
rhs.chains_ = nullptr;
105106
}
107+
108+
/**
109+
* Retrieve the unique identifier for this method. This is guaranteed to be
110+
* unique among all methods loaded by the instance of the runtime. Note that
111+
* this value is only meaningful in the context of a specific instance of the
112+
* executorch runtime and will vary run to run.
113+
*
114+
* @returns The unique identifier for this method instance.
115+
*/
116+
size_t id() const {
117+
return id_;
118+
}
106119

107120
/**
108121
* Sets the internal input value to be equivalent to the to the provided
@@ -306,7 +319,8 @@ class Method final {
306319
MemoryManager* memory_manager,
307320
EventTracer* event_tracer,
308321
MemoryAllocator* temp_allocator)
309-
: step_state_(),
322+
: id_(next_id_++),
323+
step_state_(),
310324
program_(program),
311325
memory_manager_(memory_manager),
312326
temp_allocator_(temp_allocator),
@@ -352,7 +366,10 @@ class Method final {
352366

353367
// Executes a single instruction using the state in step_state_
354368
ET_NODISCARD Error execute_instruction();
369+
370+
static size_t next_id_;
355371

372+
size_t id_;
356373
StepState step_state_;
357374
const Program* program_;
358375
MemoryManager* memory_manager_;

runtime/executor/test/method_test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,27 @@ TEST_F(MethodTest, MethodGetAttributeTest) {
365365
EXPECT_EQ(res->const_data_ptr<int32_t>()[0], 1);
366366
}
367367

368+
TEST_F(MethodTest, MethodIdTest) {
369+
// Verify that methods are assigned unique ids. Load three methods and verify
370+
// that they have different ids.
371+
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
372+
373+
Result<Method> add_method = programs_["add"]->load_method("forward", &mmm.get());
374+
ASSERT_EQ(add_method.error(), Error::Ok);
375+
376+
Result<Method> add_mul_method =
377+
programs_["add_mul"]->load_method("forward", &mmm.get());
378+
ASSERT_EQ(add_mul_method.error(), Error::Ok);
379+
380+
Result<Method> stateful_method =
381+
programs_["stateful"]->load_method("forward", &mmm.get());
382+
ASSERT_EQ(stateful_method.error(), Error::Ok);
383+
384+
EXPECT_NE(add_method->id(), add_mul_method->id());
385+
EXPECT_NE(add_method->id(), stateful_method->id());
386+
EXPECT_NE(add_mul_method->id(), stateful_method->id());
387+
}
388+
368389
/*
369390
* TODO(T161163608): Test is disabled due to a resize bug in tensor_index_out of
370391
* the portable op lib

0 commit comments

Comments
 (0)