xref: /aosp_15_r20/external/executorch/runtime/backend/backend_init_context.h (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #pragma once
10 #include <executorch/runtime/core/memory_allocator.h>
11 
12 namespace executorch {
13 namespace runtime {
14 
15 /**
16  * BackendInitContext will be used to inject runtime info for to initialize
17  * delegate.
18  */
19 class BackendInitContext final {
20  public:
21   explicit BackendInitContext(
22       MemoryAllocator* runtime_allocator,
23       const char* method_name = nullptr)
runtime_allocator_(runtime_allocator)24       : runtime_allocator_(runtime_allocator), method_name_(method_name) {}
25 
26   /** Get the runtime allocator passed from Method. It's the same runtime
27    * executor used by the standard executor runtime and the life span is the
28    * same as the model.
29    */
get_runtime_allocator()30   MemoryAllocator* get_runtime_allocator() {
31     return runtime_allocator_;
32   }
33 
34   /** Get the loaded method name from ExecuTorch runtime. Usually it's
35    * "forward", however, if there are multiple methods in the .pte file, it can
36    * be different. One example is that we may have prefill and decode methods in
37    * the same .pte file. In this case, when client loads "prefill" method, the
38    * `get_method_name` function will return "prefill", when client loads
39    * "decode" method, the `get_method_name` function will return "decode".
40    */
get_method_name()41   const char* get_method_name() const {
42     return method_name_;
43   }
44 
45  private:
46   MemoryAllocator* runtime_allocator_ = nullptr;
47   const char* method_name_ = nullptr;
48 };
49 
50 } // namespace runtime
51 } // namespace executorch
52 
53 namespace torch {
54 namespace executor {
55 // TODO(T197294990): Remove these deprecated aliases once all users have moved
56 // to the new `::executorch` namespaces.
57 using ::executorch::runtime::BackendInitContext;
58 } // namespace executor
59 } // namespace torch
60