xref: /aosp_15_r20/external/executorch/backends/apple/coreml/runtime/delegate/ETCoreMLModel.h (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 //
2 // ETCoreMLModel.h
3 //
4 // Copyright © 2024 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 #import <CoreML/CoreML.h>
9 #import <vector>
10 
11 NS_ASSUME_NONNULL_BEGIN
12 
13 @class ETCoreMLAsset;
14 
15 namespace executorchcoreml {
16 class MultiArray;
17 }
18 
19 /// Represents a ML model, the class is a thin wrapper over `MLModel` with additional properties.
20 __attribute__((objc_subclassing_restricted))
21 @interface ETCoreMLModel : NSObject
22 
23 - (instancetype)init NS_UNAVAILABLE;
24 
25 + (instancetype)new NS_UNAVAILABLE;
26 
27 /// Constructs an `ETCoreMLModel` instance.
28 ///
29 /// @param asset The asset from which the model is loaded.
30 /// @param configuration The model configuration.
31 /// @param orderedInputNames   The ordered input names of the model.
32 /// @param orderedOutputNames   The ordered output names of the model.
33 /// @param error   On failure, error is filled with the failure information.
34 - (nullable instancetype)initWithAsset:(ETCoreMLAsset*)asset
35                          configuration:(MLModelConfiguration*)configuration
36                      orderedInputNames:(NSOrderedSet<NSString*>*)orderedInputNames
37                     orderedOutputNames:(NSOrderedSet<NSString*>*)orderedOutputNames
38                                  error:(NSError* __autoreleasing*)error NS_DESIGNATED_INITIALIZER;
39 
40 /// The underlying MLModel.
41 @property (strong, readonly, nonatomic) MLModel* mlModel;
42 
43 /// The model state.
44 @property (strong, readonly, nonatomic, nullable) id state;
45 
46 /// The asset from which the model is loaded.
47 @property (strong, readonly, nonatomic) ETCoreMLAsset* asset;
48 
49 /// The asset identifier.
50 @property (strong, readonly, nonatomic) NSString* identifier;
51 
52 /// The ordered input names of the model.
53 @property (copy, readonly, nonatomic) NSOrderedSet<NSString*>* orderedInputNames;
54 
55 /// The ordered output names of the model.
56 @property (copy, readonly, nonatomic) NSOrderedSet<NSString*>* orderedOutputNames;
57 
58 
59 - (nullable id<MLFeatureProvider>)predictionFromFeatures:(id<MLFeatureProvider>)input
60                                                  options:(MLPredictionOptions*)options
61                                                    error:(NSError* __autoreleasing*)error;
62 
63 - (nullable NSArray<MLMultiArray*>*)prepareInputs:(const std::vector<executorchcoreml::MultiArray>&)inputs
64                                             error:(NSError* __autoreleasing*)error;
65 
66 - (nullable NSArray<MLMultiArray*>*)prepareOutputBackings:(const std::vector<executorchcoreml::MultiArray>&)outputs
67                                                     error:(NSError* __autoreleasing*)error;
68 
69 - (BOOL)prewarmAndReturnError:(NSError* __autoreleasing*)error;
70 
71 @end
72 
73 NS_ASSUME_NONNULL_END
74