xref: /aosp_15_r20/external/executorch/backends/xnnpack/runtime/XNNExecutor.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 
11 #include <executorch/backends/xnnpack/runtime/XNNStatus.h>
12 #include <executorch/backends/xnnpack/runtime/profiling/XNNProfiler.h>
13 #include <executorch/runtime/backend/interface.h>
14 #include <executorch/runtime/core/error.h>
15 #include <executorch/runtime/core/exec_aten/util/tensor_util.h>
16 
17 #include <xnnpack.h>
18 #include <map>
19 #include <memory>
20 #include <vector>
21 
22 namespace executorch {
23 namespace backends {
24 namespace xnnpack {
25 namespace delegate {
26 
27 class XNNExecutor {
28  private:
29   std::unique_ptr<xnn_runtime, decltype(&xnn_delete_runtime)> runtime_{
30       nullptr,
31       &xnn_delete_runtime};
32 
33   profiling::XNNProfiler profiler_;
34   std::vector<uint32_t> input_ids_;
35   std::vector<uint32_t> output_ids_;
36   std::vector<xnn_external_value> externals_;
37 
38  public:
39   XNNExecutor() = default;
40 
getNumInputs()41   inline size_t getNumInputs() {
42     return input_ids_.size();
43   }
44 
getNumOutputs()45   inline size_t getNumOutputs() {
46     return output_ids_.size();
47   }
48 
49   /**
50    * Initialize the XNNExecutor with a given runtime and input/output ids.
51    * The input/output ids are expected to be sorted in order of their
52    * flatbuffer id_outs
53    */
54   ET_NODISCARD executorch::runtime::Error initialize(
55       xnn_runtime_t runtime,
56       std::vector<uint32_t>&& input_ids,
57       std::vector<uint32_t>&& output_ids);
58 
59   /**
60    * Prepares the arguments for runtime graph execution.
61    * args is an array of EValues that will be passed into the runtime.
62    * input shapes will be propagated through the runtime, and perform
63    * any additional memory planning as needed
64    */
65   ET_NODISCARD executorch::runtime::Error prepare_args(
66       executorch::runtime::EValue** args);
67 
68   /**
69    * Executes the graph using the args prepared at prepare_args().
70    */
71   ET_NODISCARD executorch::runtime::Error forward(
72       executorch::runtime::BackendExecutionContext& context);
73 
74   /**
75    * Prepares the outputs to be returned by the delegate
76    *
77    * Performs any post processing of outputs like tensor resizing
78    */
79   ET_NODISCARD executorch::runtime::Error resize_outputs(
80       executorch::runtime::EValue** args) const;
81 
82   friend class XNNCompiler;
83 };
84 
85 } // namespace delegate
86 } // namespace xnnpack
87 } // namespace backends
88 } // namespace executorch
89