xref: /aosp_15_r20/external/executorch/backends/qualcomm/runtime/QnnExecuTorchBackend.h (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Qualcomm Innovation Center, Inc.
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 #pragma once
9 
10 #include <executorch/runtime/backend/interface.h>
11 #include <executorch/runtime/core/error.h>
12 #include <executorch/runtime/core/evalue.h>
13 
14 #include <mutex>
15 #include <unordered_map>
16 
17 namespace executorch {
18 namespace backends {
19 namespace qnn {
20 
21 class QnnExecuTorchBackend final
22     : public ::executorch::runtime::BackendInterface {
23  public:
~QnnExecuTorchBackend()24   ~QnnExecuTorchBackend(){};
25 
26   executorch::runtime::Result<executorch::runtime::DelegateHandle*> init(
27       executorch::runtime::BackendInitContext& context,
28       executorch::runtime::FreeableBuffer* processed,
29       executorch::runtime::ArrayRef<executorch::runtime::CompileSpec>
30           compile_specs) const override;
31 
32   executorch::runtime::Error execute(
33       ET_UNUSED executorch::runtime::BackendExecutionContext& context,
34       executorch::runtime::DelegateHandle* handle,
35       executorch::runtime::EValue** args) const override;
36 
37   void destroy(executorch::runtime::DelegateHandle* handle) const override;
38 
39   bool is_available() const override;
40 
41  private:
42   void add_cached_delegate(
43       const std::string& signature,
44       executorch::runtime::DelegateHandle* handle) const;
45   void erase_cached_delegate(executorch::runtime::DelegateHandle* handle) const;
46 
47   mutable std::mutex mutex_;
48   mutable std::unordered_map<std::string, executorch::runtime::DelegateHandle*>
49       delegate_map_;
50   mutable std::unordered_map<executorch::runtime::DelegateHandle*, std::string>
51       delegate_map_rev_;
52 };
53 
54 } // namespace qnn
55 } // namespace backends
56 } // namespace executorch
57