xref: /aosp_15_r20/external/executorch/backends/apple/coreml/runtime/include/coreml_backend/delegate.h (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 //
2 // delegate.h
3 //
4 // Copyright © 2023 Apple Inc. All rights reserved.
5 //
6 // Please refer to the license found in the LICENSE file in the root directory of the 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 <memory>
15 
16 namespace executorchcoreml {
17 class BackendDelegate;
18 }
19 
20 namespace executorch {
21 namespace backends {
22 namespace coreml {
23 
24 class CoreMLBackendDelegate final : public ::executorch::runtime::BackendInterface {
25 public:
26     CoreMLBackendDelegate() noexcept;
27     ~CoreMLBackendDelegate() = default;
28 
29     /// Loads a CoreML model from AOT blob.
30     ///
31     /// @param context An init context specific to the CoreML backend.
32     /// @param processed The AOT blob that was produced by a call to CoreML's
33     /// backend `preprocess` method.
34     /// @param compileSpecs The exact same compiler specification that was used to
35     /// produce `processed`.
36     /// @retval On success, an opaque handle representing the loaded model
37     /// otherwise  an`Error` case.
38     executorch::runtime::Result<executorch::runtime::DelegateHandle*>
39     init(executorch::runtime::BackendInitContext& context,
40          executorch::runtime::FreeableBuffer* processed,
41          executorch::runtime::ArrayRef<executorch::runtime::CompileSpec> compileSpecs) const override;
42 
43     /// Executes the loaded model.
44     ///
45     /// @param context An execution context specific to the CoreML backend.
46     /// @param handle The handle returned by an earlier call to `init`.
47     /// @param args The models inputs and outputs.
48     /// @retval On success, `Error::Ok` otherwise any other `Error` case.
49     executorch::runtime::Error execute(executorch::runtime::BackendExecutionContext& context,
50                                        executorch::runtime::DelegateHandle* handle,
51                                        executorch::runtime::EValue** args) const override;
52 
53     /// Returns `true` if the delegate is available otherwise `false`.
54     bool is_available() const override;
55 
56     /// Unloads the loaded CoreML model with the  specified handle.
57     ///
58     /// @param handle The handle returned by an earlier call to `init`.
59     void destroy(executorch::runtime::DelegateHandle* handle) const override;
60 
61     /// Returns the registered `CoreMLBackendDelegate` instance.
62     static CoreMLBackendDelegate* get_registered_delegate() noexcept;
63 
64     /// Purges the models cache.
65     ///
66     /// The cached models are moved to a temporary directory and are
67     /// asynchronously deleted.
68     bool purge_models_cache() const noexcept;
69 
70 private:
71     std::shared_ptr<executorchcoreml::BackendDelegate> impl_;
72 };
73 
74 } // namespace coreml
75 } // namespace backends
76 } // namespace executorch
77