Skip to content

Commit 94fb1ea

Browse files
committed
integrate #6622, will drop it once merged
1 parent c7c0211 commit 94fb1ea

File tree

6 files changed

+44
-17
lines changed

6 files changed

+44
-17
lines changed

backends/qualcomm/runtime/QnnExecuTorchBackend.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Result<DelegateHandle*> QnnExecuTorchBackend::init(
7171
"Fail to allocate tensor");
7272
}
7373
}
74+
method_name_ = context.get_method_name();
7475
return qnn_manager;
7576
}
7677

@@ -79,13 +80,10 @@ Error QnnExecuTorchBackend::execute(
7980
DelegateHandle* handle,
8081
EValue** args) const {
8182
QnnManager* qnn_manager = static_cast<QnnManager*>(handle);
82-
// change this after BackendExecutionContext change lands
83-
auto graph_name = qnn_manager->GetGraphNames()[0];
84-
8583
std::vector<std::shared_ptr<TensorWrapper>> input_tensors =
86-
qnn_manager->GetGraphInputs(graph_name);
84+
qnn_manager->GetGraphInputs(method_name_);
8785
std::vector<std::shared_ptr<TensorWrapper>> output_tensors =
88-
qnn_manager->GetGraphOutputs(graph_name);
86+
qnn_manager->GetGraphOutputs(method_name_);
8987
std::vector<Qnn_Tensor_t> input_tensor_structs;
9088
std::vector<Qnn_Tensor_t> output_tensor_structs;
9189

@@ -118,14 +116,14 @@ Error QnnExecuTorchBackend::execute(
118116

119117
ET_CHECK_OR_RETURN_ERROR(
120118
qnn_manager->Execute(
121-
graph_name,
119+
method_name_,
122120
input_tensor_structs,
123121
output_tensor_structs,
124122
context.event_tracer()) == Error::Ok,
125123
Internal,
126124
"Fail to execute graph");
127125
ET_CHECK_OR_RETURN_ERROR(
128-
qnn_manager->ProfileExecuteData(graph_name, context.event_tracer()) ==
126+
qnn_manager->ProfileExecuteData(method_name_, context.event_tracer()) ==
129127
Error::Ok,
130128
Internal,
131129
"Fail to profile graph");

backends/qualcomm/runtime/QnnExecuTorchBackend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class QnnExecuTorchBackend final
3434
void destroy(executorch::runtime::DelegateHandle* handle) const override;
3535

3636
bool is_available() const override;
37+
38+
private:
39+
mutable std::string method_name_;
3740
};
3841

3942
} // namespace qnn

runtime/backend/backend_init_context.h

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

2426
/** Get the runtime allocator passed from Method. It's the same runtime
2527
* executor used by the standard executor runtime and the life span is the
@@ -29,8 +31,13 @@ class BackendInitContext final {
2931
return runtime_allocator_;
3032
}
3133

34+
const char* get_method_name() {
35+
return method_name_;
36+
}
37+
3238
private:
3339
MemoryAllocator* runtime_allocator_ = nullptr;
40+
const char* method_name_ = nullptr;
3441
};
3542

3643
} // 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: 24 additions & 7 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

@@ -83,8 +83,7 @@ class StubBackend final : public BackendInterface {
8383
FreeableBuffer* processed,
8484
ArrayRef<CompileSpec> compile_specs) const override {
8585
if (init_fn_) {
86-
return init_fn_.value()(
87-
processed, compile_specs, context.get_runtime_allocator());
86+
return init_fn_.value()(processed, compile_specs, context);
8887
}
8988
// Return a benign value otherwise.
9089
return nullptr;
@@ -351,7 +350,7 @@ TEST_P(BackendIntegrationTest, FreeingProcessedBufferSucceeds) {
351350
StubBackend::singleton().install_init(
352351
[&](FreeableBuffer* processed,
353352
ET_UNUSED ArrayRef<CompileSpec> compile_specs,
354-
ET_UNUSED MemoryAllocator* runtime_allocator)
353+
ET_UNUSED BackendInitContext backend_init_context)
355354
-> Result<DelegateHandle*> {
356355
init_called = true;
357356
processed_data = processed->data();
@@ -395,7 +394,7 @@ TEST_P(BackendIntegrationTest, EndToEndTestWithProcessedAsHandle) {
395394
StubBackend::singleton().install_init(
396395
[&](FreeableBuffer* processed,
397396
ET_UNUSED ArrayRef<CompileSpec> compile_specs,
398-
ET_UNUSED MemoryAllocator* runtime_allocator)
397+
ET_UNUSED BackendInitContext backend_init_context)
399398
-> Result<DelegateHandle*> {
400399
init_processed = processed;
401400
return processed;
@@ -492,7 +491,7 @@ TEST_P(BackendIntegrationTest, SegmentInfoIsPassedIntoDataLoader) {
492491
StubBackend::singleton().install_init(
493492
[&](FreeableBuffer* processed,
494493
ET_UNUSED ArrayRef<CompileSpec> compile_specs,
495-
ET_UNUSED MemoryAllocator* runtime_allocator)
494+
ET_UNUSED BackendInitContext backend_init_context)
496495
-> Result<DelegateHandle*> {
497496
processed_data = processed->data();
498497
processed->Free();
@@ -528,6 +527,24 @@ TEST_P(BackendIntegrationTest, SegmentInfoIsPassedIntoDataLoader) {
528527
EXPECT_EQ(backend_load_was_called, using_segments());
529528
}
530529

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

0 commit comments

Comments
 (0)