Skip to content

Commit 8321e6e

Browse files
committed
Use NDM in backend init context
Add NDM to backend init context, so backends have access to it at init time. Add to backend init context instead of as an argument to backend.init, as it's easier to add/deprecate an arg there rather than in the init/execute interface. TODO: add a merge/union method to named_data_map.h interface. If there is an external file (or multiple), and/or named_segments in the PTE, these should be merged into one. Differential Revision: [D70279622](https://our.internmc.facebook.com/intern/diff/D70279622/) ghstack-source-id: 269086384 Pull Request resolved: #8944
1 parent fe6f16b commit 8321e6e

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

runtime/backend/backend_init_context.h

+14-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#pragma once
1010
#include <executorch/runtime/core/memory_allocator.h>
11+
#include <executorch/runtime/core/named_data_map.h>
1112

1213
namespace executorch {
1314
namespace runtime {
@@ -21,8 +22,11 @@ class BackendInitContext final {
2122
explicit BackendInitContext(
2223
MemoryAllocator* runtime_allocator,
2324
EventTracer* event_tracer = nullptr,
24-
const char* method_name = nullptr)
25-
: runtime_allocator_(runtime_allocator), method_name_(method_name) {}
25+
const char* method_name = nullptr,
26+
const NamedDataMap* named_data_map = nullptr)
27+
: runtime_allocator_(runtime_allocator),
28+
method_name_(method_name),
29+
named_data_map_(named_data_map) {}
2630

2731
/** Get the runtime allocator passed from Method. It's the same runtime
2832
* executor used by the standard executor runtime and the life span is the
@@ -52,10 +56,18 @@ class BackendInitContext final {
5256
return method_name_;
5357
}
5458

59+
/** Get the named data map from ExecuTorch runtime.
60+
* This provides a way for backends to retrieve data blobs by key.
61+
*/
62+
const NamedDataMap* get_named_data_map() const {
63+
return named_data_map_;
64+
}
65+
5566
private:
5667
MemoryAllocator* runtime_allocator_ = nullptr;
5768
EventTracer* event_tracer_ = nullptr;
5869
const char* method_name_ = nullptr;
70+
const NamedDataMap* named_data_map_ = nullptr;
5971
};
6072

6173
} // namespace runtime

runtime/backend/interface.h

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <executorch/runtime/core/evalue.h>
1818
#include <executorch/runtime/core/freeable_buffer.h>
1919
#include <executorch/runtime/core/memory_allocator.h>
20+
#include <executorch/runtime/core/named_data_map.h>
2021
#include <executorch/runtime/core/result.h>
2122
#include <executorch/runtime/platform/compiler.h>
2223

runtime/backend/targets.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ def define_common_targets():
2929
"//executorch/runtime/core:evalue" + aten_suffix,
3030
"//executorch/runtime/core:event_tracer" + aten_suffix,
3131
"//executorch/runtime/core:memory_allocator",
32+
"//executorch/runtime/core:named_data_map",
3233
],
3334
)

runtime/executor/method.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,14 @@ Error Method::init(
798798
return Error::MemoryAllocationFailed;
799799
}
800800

801+
// Get NamedDataMap, if it exists.
802+
const NamedDataMap* pte_data_map = nullptr;
803+
Result<const NamedDataMap*> pte_data_map_res =
804+
program_->get_named_data_map();
805+
if (pte_data_map_res.ok()) {
806+
pte_data_map = pte_data_map_res.get();
807+
}
808+
801809
// n_delegate_ counts the number of successfully-initialized delegates for
802810
// ~Method() to clean up, and is incremented at the bottom of the loop. This
803811
// makes it safe for errors to return without updating any state.
@@ -808,7 +816,8 @@ Error Method::init(
808816
BackendInitContext backend_init_context(
809817
method_allocator,
810818
/*event_tracer=*/event_tracer_,
811-
/*method_name=*/serialization_plan_->name()->c_str());
819+
/*method_name=*/serialization_plan_->name()->c_str(),
820+
/*named_data_map=*/pte_data_map);
812821
Error err = BackendDelegate::Init(
813822
delegate, program_, backend_init_context, &delegates_[i]);
814823
if (err != Error::Ok) {

0 commit comments

Comments
 (0)