Skip to content

Commit e9c5851

Browse files
cccclaifacebook-github-bot
authored andcommitted
Add backend init context to backend.init (#276)
Summary: Pull Request resolved: #276 Create `BackendInitContext` to wrap the runtime allocator. We may inject `EventTracer` to `BackendInitContext` to profile init time later Reviewed By: dbort Differential Revision: D48872105 fbshipit-source-id: 5022cec0b3dbf63562a6397d71417474eae10a37
1 parent 925cd1b commit e9c5851

File tree

9 files changed

+44
-31
lines changed

9 files changed

+44
-31
lines changed

backends/qnnpack/QNNPackBackend.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@ class QnnpackBackend final : public PyTorchBackendInterface {
112112
}
113113

114114
Result<DelegateHandle*> init(
115+
BackendInitContext& context,
115116
FreeableBuffer* processed,
116-
ArrayRef<CompileSpec> compile_specs,
117-
MemoryAllocator* runtime_allocator) const override {
117+
ArrayRef<CompileSpec> compile_specs) const override {
118+
MemoryAllocator* runtime_allocator = context.get_runtime_allocator();
118119
auto dynamic_linear = fb_qnnpack::GetQNNDynamicLinear(processed->data());
119120
auto bias = dynamic_linear->bias();
120121

backends/vulkan/VulkanBackend.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ class VulkanBackend final : public PyTorchBackendInterface {
245245
}
246246

247247
Result<DelegateHandle*> init(
248+
BackendInitContext& context,
248249
FreeableBuffer* processed,
249-
ArrayRef<CompileSpec>,
250-
MemoryAllocator* runtime_allocator) const override {
250+
ArrayRef<CompileSpec>) const override {
251251
ET_CHECK_OR_RETURN_ERROR(
252252
at::vulkan::delegate::VkGraphBufferHasIdentifier(processed->data()),
253253
DelegateInvalidCompatibility,
@@ -257,7 +257,7 @@ class VulkanBackend final : public PyTorchBackendInterface {
257257

258258
at::native::vulkan::ComputeGraph* compute_graph =
259259
ET_ALLOCATE_INSTANCE_OR_RETURN_ERROR(
260-
runtime_allocator, at::native::vulkan::ComputeGraph);
260+
context.get_runtime_allocator(), at::native::vulkan::ComputeGraph);
261261

262262
new (compute_graph) at::native::vulkan::ComputeGraph(generate_config());
263263

backends/xnnpack/runtime/XNNPACKBackend.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ class XnnpackBackend final : public PyTorchBackendInterface {
2525
}
2626

2727
Result<DelegateHandle*> init(
28+
BackendInitContext& context,
2829
FreeableBuffer* processed,
29-
ArrayRef<CompileSpec> compile_specs,
30-
MemoryAllocator* runtime_allocator) const override {
30+
ArrayRef<CompileSpec> compile_specs) const override {
3131
auto executor = ET_ALLOCATE_INSTANCE_OR_RETURN_ERROR(
32-
runtime_allocator, xnnpack::delegate::XNNExecutor);
32+
context.get_runtime_allocator(), xnnpack::delegate::XNNExecutor);
3333

3434
// Executor has been allocated but not constructed, ensure that runtime_ is
3535
// nullptr by constructing it in place here. NOTE: Since we use placement
@@ -38,7 +38,10 @@ class XnnpackBackend final : public PyTorchBackendInterface {
3838
new (executor) xnnpack::delegate::XNNExecutor;
3939

4040
Error err = xnnpack::delegate::XNNCompiler::compileModel(
41-
processed->data(), processed->size(), executor, runtime_allocator);
41+
processed->data(),
42+
processed->size(),
43+
executor,
44+
context.get_runtime_allocator());
4245
if (err != Error::Ok) {
4346
ET_LOG(Error, "XNNCompiler::compleModel failed: 0x%x", (unsigned int)err);
4447
}

exir/backend/test/demos/rpc/ExecutorBackend.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ class ExecutorBackend final : public PyTorchBackendInterface {
4444
}
4545

4646
Result<DelegateHandle*> init(
47+
BackendInitContext& context,
4748
FreeableBuffer* processed,
48-
__ET_UNUSED ArrayRef<CompileSpec> compile_specs,
49-
MemoryAllocator* runtime_allocator) const override {
49+
__ET_UNUSED ArrayRef<CompileSpec> compile_specs) const override {
5050
// `processed` contains an executorch program. Wrap it in a DataLoader that
5151
// will return the data directly without copying it.
52+
MemoryAllocator* runtime_allocator = context.get_runtime_allocator();
5253
auto loader = ET_ALLOCATE_INSTANCE_OR_RETURN_ERROR(
5354
runtime_allocator, util::BufferDataLoader);
5455
new (loader) util::BufferDataLoader(processed->data(), processed->size());

runtime/backend/backend_registry.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <cstring>
1212

1313
#include <executorch/runtime/backend/backend_execution_context.h>
14+
#include <executorch/runtime/backend/backend_init_context.h>
1415
#include <executorch/runtime/core/array_ref.h>
1516
#include <executorch/runtime/core/error.h>
1617
#include <executorch/runtime/core/evalue.h>
@@ -75,9 +76,9 @@ class PyTorchBackendInterface {
7576
* @returns On error, a value other than Error:Ok.
7677
*/
7778
__ET_NODISCARD virtual Result<DelegateHandle*> init(
79+
BackendInitContext& context,
7880
FreeableBuffer* processed,
79-
ArrayRef<CompileSpec> compile_specs,
80-
MemoryAllocator* memory_allocator) const = 0;
81+
ArrayRef<CompileSpec> compile_specs) const = 0;
8182

8283
/**
8384
* Responsible for executing the given method’s handle, as it was produced

runtime/executor/method.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class BackendDelegate final {
5252
static Error Init(
5353
const executorch_flatbuffer::BackendDelegate& delegate,
5454
const Program* program,
55-
MemoryAllocator* runtime_allocator,
55+
BackendInitContext& backend_init_context,
5656
BackendDelegate* out) {
5757
// Look up the backend.
5858
const char* backend_id = delegate.id()->c_str();
@@ -78,7 +78,7 @@ class BackendDelegate final {
7878
// Parse compilation specs from program
7979
CompileSpec* compile_specs;
8080
Error err = PopulateCompileSpecs(
81-
delegate.compile_specs(), runtime_allocator, &compile_specs);
81+
delegate.compile_specs(), backend_init_context, &compile_specs);
8282
if (err != Error::Ok) {
8383
ET_LOG(Error, "Failed to get compile specs for backend %s", backend_id);
8484
return err;
@@ -93,9 +93,9 @@ class BackendDelegate final {
9393

9494
// Initialize the delegate.
9595
Result<DelegateHandle*> handle = backend->init(
96+
backend_init_context,
9697
&out->segment_,
97-
ArrayRef<CompileSpec>(compile_specs, num_compile_specs),
98-
runtime_allocator);
98+
ArrayRef<CompileSpec>(compile_specs, num_compile_specs));
9999
if (!handle.ok()) {
100100
ET_LOG(
101101
Error,
@@ -135,12 +135,14 @@ class BackendDelegate final {
135135
static Error PopulateCompileSpecs(
136136
const flatbuffers::Vector<flatbuffers::Offset<
137137
executorch_flatbuffer::CompileSpec>>* compile_specs_in_program,
138-
torch::executor::MemoryAllocator* runtime_allocator,
138+
BackendInitContext& backend_init_context,
139139
CompileSpec** out_spec) {
140140
auto number_of_compile_specs = compile_specs_in_program->size();
141141

142142
CompileSpec* compile_specs_list = ET_ALLOCATE_LIST_OR_RETURN_ERROR(
143-
runtime_allocator, CompileSpec, number_of_compile_specs);
143+
backend_init_context.get_runtime_allocator(),
144+
CompileSpec,
145+
number_of_compile_specs);
144146

145147
// Initialize the spec list for each method spec
146148
for (size_t j = 0; j < number_of_compile_specs; j++) {
@@ -537,14 +539,15 @@ Error Method::init(executorch_flatbuffer::ExecutionPlan* s_plan) {
537539

538540
for (size_t i = 0; i < n_delegate; ++i) {
539541
const auto& delegate = *delegates->Get(i);
542+
BackendInitContext backend_init_context(runtime_allocator);
540543
Error err = BackendDelegate::Init(
541-
delegate, program_, runtime_allocator, &delegates_[i]);
544+
delegate, program_, backend_init_context, &delegates_[i]);
542545
if (err != Error::Ok) {
543546
return err;
544547
}
545548
// ~Method() will try to clean up n_delegate_ entries in the delegates_
546-
// array. Only increment this once we know the entry is valid, so that we
547-
// don't try to clean up an uninitialized entry.
549+
// array. Only increment this once we know the entry is valid, so that
550+
// we don't try to clean up an uninitialized entry.
548551
n_delegate_ = i + 1;
549552
}
550553
}
@@ -1035,8 +1038,8 @@ Error Method::execute() {
10351038
NotSupported,
10361039
"Cannot execute until method has been initialized.");
10371040

1038-
// Chains are executed sequentially today, but future async designs may branch
1039-
// and run many in parallel or out of order.
1041+
// Chains are executed sequentially today, but future async designs may
1042+
// branch and run many in parallel or out of order.
10401043
for (step_state_.chain_idx = 0; step_state_.chain_idx < n_chains_;
10411044
++step_state_.chain_idx) {
10421045
Chain& chain = chains_[step_state_.chain_idx];

runtime/executor/test/backend_integration_test.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using namespace ::testing;
3131
using exec_aten::ArrayRef;
3232
using torch::executor::BackendExecutionContext;
33+
using torch::executor::BackendInitContext;
3334
using torch::executor::CompileSpec;
3435
using torch::executor::DataLoader;
3536
using torch::executor::DelegateHandle;
@@ -78,11 +79,12 @@ class StubBackend final : public PyTorchBackendInterface {
7879
}
7980

8081
Result<DelegateHandle*> init(
82+
BackendInitContext& context,
8183
FreeableBuffer* processed,
82-
ArrayRef<CompileSpec> compile_specs,
83-
MemoryAllocator* runtime_allocator) const override {
84+
ArrayRef<CompileSpec> compile_specs) const override {
8485
if (init_fn_) {
85-
return init_fn_.value()(processed, compile_specs, runtime_allocator);
86+
return init_fn_.value()(
87+
processed, compile_specs, context.get_runtime_allocator());
8688
}
8789
// Return a benign value otherwise.
8890
return nullptr;

runtime/executor/test/test_backend_compiler_lib.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ class BackendWithCompiler final : public PyTorchBackendInterface {
9494
}
9595

9696
Result<DelegateHandle*> init(
97+
BackendInitContext& context,
9798
FreeableBuffer* processed,
98-
ArrayRef<CompileSpec> compile_specs,
99-
MemoryAllocator* runtime_allocator) const override {
99+
ArrayRef<CompileSpec> compile_specs) const override {
100+
MemoryAllocator* runtime_allocator = context.get_runtime_allocator();
100101
int shape = *(int*)(compile_specs.at(0).value.buffer);
101102
ET_CHECK_OR_RETURN_ERROR(
102103
shape <= max_shape,

runtime/executor/test/test_backend_with_delegate_mapping.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ class BackendWithDelegateMapping final : public PyTorchBackendInterface {
7777
}
7878

7979
Result<DelegateHandle*> init(
80+
BackendInitContext& context,
8081
FreeableBuffer* processed,
81-
ArrayRef<CompileSpec> compile_specs,
82-
MemoryAllocator* runtime_allocator) const override {
82+
ArrayRef<CompileSpec> compile_specs) const override {
83+
MemoryAllocator* runtime_allocator = context.get_runtime_allocator();
8384
(void)compile_specs;
8485
const char* kSignLiteral = "#";
8586
// The first number is the number of total instruction

0 commit comments

Comments
 (0)