Skip to content

Commit ae84721

Browse files
cymbalrushfacebook-github-bot
authored andcommitted
Integrate sdk (#2314)
Summary: The PR adds support for logging profiling information and intermediate outputs. The required files are only included if CoreML delegate is built with`EXECUTORCH_BUILD_SDK` option. - Profiling: `ETCoreMLModelProfiler` adds profiling support, it uses the `MLComputePlan` API that's currently in beta but unlikely to change. The caveat is that it only works for macOS >= 14.4, iOS >= 17.4. - Debugging: `ETCoreMLModelDebugger` adds support for getting intermediate tensors. The model spec is updated to include additional intermediate outputs, compiled, and then executed. It has a dependency on `protobuf` as it modifies the specification of the model. The logging part doesn't work yet, it would require ExecuTorch API (`log_tensor_delegate`). - CLI: Added cli file for parsing CoreML delegate metadata. - AOT: AOT integration is remaining but most likely it would be done in `coremltools`. `coremltools` will add a `json` file to `mlpackage` that would contain the operation path to debug handle mapping. ![Screenshot 2024-03-07 at 11 04 00 PM](https://github.com/pytorch/executorch/assets/709448/62bbc623-cd53-42cd-bec7-f7afca227f79) Pull Request resolved: #2314 Reviewed By: cccclai Differential Revision: D54754857 Pulled By: shoumikhin fbshipit-source-id: 493da4bfb8f9a4d60d68a219cfed63178d731add
1 parent 39c93aa commit ae84721

File tree

108 files changed

+4328
-362
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+4328
-362
lines changed

backends/apple/coreml/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Mac OS X
22
*.DS_Store
33

4-
cmake-out/
4+
*-cmake-out/
55

66

77
# Xcode
@@ -18,6 +18,7 @@ runtime/runner/build/
1818
runtime/libraries/
1919
runtime/inmemoryfs/build/
2020
runtime/test/models/
21+
runtime/sdk/format/
2122
runtime/include/executorch/
2223
third-party/
2324

backends/apple/coreml/CMakeLists.txt

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ set(DELEGATE_SOURCES
3838
runtime/delegate/coreml_backend_delegate.mm
3939
runtime/delegate/ETCoreMLAsset.mm
4040
runtime/delegate/ETCoreMLAssetManager.mm
41+
runtime/delegate/ETCoreMLDefaultModelExecutor.mm
42+
runtime/delegate/ETCoreMLModelLoader.mm
43+
runtime/delegate/ETCoreMLModelCompiler.mm
4144
runtime/delegate/ETCoreMLLogging.mm
4245
runtime/delegate/ETCoreMLModel.mm
4346
runtime/delegate/ETCoreMLModelManager.mm
@@ -54,6 +57,57 @@ set(UTIL_SOURCES
5457
runtime/util/objc_json_serde.mm
5558
)
5659

60+
# sdk sources
61+
set(SDK_SOURCES
62+
runtime/sdk/ETCoreMLModelAnalyzer.mm
63+
runtime/sdk/ETCoreMLModelStructurePath.mm
64+
runtime/sdk/ETCoreMLOperationProfilingInfo.mm
65+
runtime/sdk/ETCoreMLModelDebugger.mm
66+
runtime/sdk/ETCoreMLModelProfiler.mm
67+
runtime/sdk/ETCoreMLPair.mm
68+
runtime/sdk/model_package_info.mm
69+
runtime/sdk/model_event_logger_impl.mm
70+
runtime/sdk/program_path.mm
71+
)
72+
73+
# protobuf sources
74+
set(PROTOBUF_SOURCES
75+
runtime/sdk/format/ArrayFeatureExtractor.pb.cc
76+
runtime/sdk/format/AudioFeaturePrint.pb.cc
77+
runtime/sdk/format/BayesianProbitRegressor.pb.cc
78+
runtime/sdk/format/CategoricalMapping.pb.cc
79+
runtime/sdk/format/ClassConfidenceThresholding.pb.cc
80+
runtime/sdk/format/CustomModel.pb.cc
81+
runtime/sdk/format/DataStructures.pb.cc
82+
runtime/sdk/format/DictVectorizer.pb.cc
83+
runtime/sdk/format/FeatureTypes.pb.cc
84+
runtime/sdk/format/FeatureVectorizer.pb.cc
85+
runtime/sdk/format/Gazetteer.pb.cc
86+
runtime/sdk/format/GLMClassifier.pb.cc
87+
runtime/sdk/format/GLMRegressor.pb.cc
88+
runtime/sdk/format/Identity.pb.cc
89+
runtime/sdk/format/Imputer.pb.cc
90+
runtime/sdk/format/ItemSimilarityRecommender.pb.cc
91+
runtime/sdk/format/LinkedModel.pb.cc
92+
runtime/sdk/format/MIL.pb.cc
93+
runtime/sdk/format/Model.pb.cc
94+
runtime/sdk/format/NearestNeighbors.pb.cc
95+
runtime/sdk/format/NeuralNetwork.pb.cc
96+
runtime/sdk/format/NonMaximumSuppression.pb.cc
97+
runtime/sdk/format/Normalizer.pb.cc
98+
runtime/sdk/format/NonMaximumSuppression.pb.cc
99+
runtime/sdk/format/OneHotEncoder.pb.cc
100+
runtime/sdk/format/Parameters.pb.cc
101+
runtime/sdk/format/Scaler.pb.cc
102+
runtime/sdk/format/SoundAnalysisPreprocessing.pb.cc
103+
runtime/sdk/format/SVM.pb.cc
104+
runtime/sdk/format/TextClassifier.pb.cc
105+
runtime/sdk/format/TreeEnsemble.pb.cc
106+
runtime/sdk/format/VisionFeaturePrint.pb.cc
107+
runtime/sdk/format/WordEmbedding.pb.cc
108+
runtime/sdk/format/WordTagger.pb.cc
109+
)
110+
57111
# Define the delegate library
58112
add_library(coremldelegate)
59113
target_sources(
@@ -63,6 +117,7 @@ target_sources(
63117
${DELEGATE_SOURCES}
64118
${UTIL_SOURCES}
65119
)
120+
66121
target_include_directories(
67122
coremldelegate PRIVATE
68123
${CMAKE_CURRENT_SOURCE_DIR}/runtime/include
@@ -87,7 +142,30 @@ target_include_directories(
87142
coremldelegate PRIVATE
88143
${EXECUTORCH_ROOT}/..
89144
)
90-
target_compile_options(executorch PUBLIC -DET_EVENT_TRACER_ENABLED)
145+
target_link_libraries(
146+
coremldelegate PRIVATE
147+
executorch
148+
)
149+
150+
if(EXECUTORCH_BUILD_SDK)
151+
target_sources(
152+
coremldelegate PRIVATE
153+
${SDK_SOURCES}
154+
${PROTOBUF_SOURCES}
155+
)
156+
target_include_directories(
157+
coremldelegate PRIVATE
158+
${CMAKE_CURRENT_SOURCE_DIR}/runtime/sdk
159+
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/src
160+
)
161+
add_subdirectory(
162+
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/cmake
163+
)
164+
target_link_libraries(
165+
coremldelegate PRIVATE
166+
libprotobuf-lite
167+
)
168+
endif()
91169

92170
find_library(ACCELERATE_FRAMEWORK Accelerate)
93171
find_library(COREML_FRAMEWORK CoreML)
@@ -105,7 +183,14 @@ target_link_libraries(coremldelegate
105183

106184
target_compile_options(coremldelegate PRIVATE "-fobjc-arc")
107185
target_compile_options(coremldelegate PRIVATE "-fno-exceptions")
186+
187+
if(EXECUTORCH_BUILD_SDK)
188+
target_compile_options(executorch PUBLIC -DET_EVENT_TRACER_ENABLED)
189+
target_compile_options(coremldelegate PRIVATE "-frtti")
190+
target_compile_options(libprotobuf-lite PRIVATE "-frtti")
191+
else()
108192
target_compile_options(coremldelegate PRIVATE "-fno-rtti")
193+
endif()
109194

110195
set(
111196
TARGET coremldelegate

backends/apple/coreml/runtime/delegate/ETCoreMLAsset.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// ETCoreMLAsset.h
33
//
4-
// Copyright © 2023 Apple Inc. All rights reserved.
4+
// Copyright © 2024 Apple Inc. All rights reserved.
55
//
66
// Please refer to the license found in the LICENSE file in the root directory of the source tree.
77

@@ -11,7 +11,7 @@
1111

1212
NS_ASSUME_NONNULL_BEGIN
1313

14-
/// Represents an asset on the filesystem. An asset is merely a directory with a unique identifier.
14+
/// Represents an asset on the filesystem.
1515
@interface ETCoreMLAsset : NSObject
1616

1717
- (instancetype)init NS_UNAVAILABLE;
@@ -21,7 +21,7 @@ NS_ASSUME_NONNULL_BEGIN
2121
/// Constructs an `ETCoreMLAsset` instance from `ModelAsset`
2222
///
2323
/// @param backingAsset The cpp asset.
24-
- (instancetype)initWithBackingAsset:(const executorchcoreml::Asset&)backingAsset NS_DESIGNATED_INITIALIZER;
24+
- (instancetype)initWithBackingAsset:(executorchcoreml::Asset)backingAsset NS_DESIGNATED_INITIALIZER;
2525

2626
/// The unique identifier.
2727
@property (copy, readonly, nonatomic) NSString* identifier;
@@ -51,7 +51,7 @@ NS_ASSUME_NONNULL_BEGIN
5151
/// @retval `YES` is the advisory read to all the files succeeded otherwise`NO`.
5252
- (BOOL)prewarmAndReturnError:(NSError* __autoreleasing*)error;
5353

54-
/// Closes all the opened file handles by the `keepAliveAndReturnError` call.
54+
/// Closes all the file handles opened by the `keepAliveAndReturnError` call.
5555
- (void)close;
5656

5757
@end

backends/apple/coreml/runtime/delegate/ETCoreMLAsset.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// ETCoreMLAsset.mm
33
//
4-
// Copyright © 2023 Apple Inc. All rights reserved.
4+
// Copyright © 2024 Apple Inc. All rights reserved.
55
//
66
// Please refer to the license found in the LICENSE file in the root directory of the source tree.
77

@@ -66,14 +66,14 @@ @implementation ETCoreMLAsset {
6666
os_unfair_lock _lock;
6767
}
6868

69-
- (instancetype)initWithBackingAsset:(const executorchcoreml::Asset&)backingAsset {
69+
- (instancetype)initWithBackingAsset:(executorchcoreml::Asset)backingAsset {
7070
self = [super init];
7171
if (self) {
72-
_backingAsset = backingAsset;
7372
_isValid = static_cast<BOOL>(is_asset_valid(backingAsset));
7473
_identifier = @(backingAsset.identifier.c_str());
7574
_contentURL = [NSURL fileURLWithPath:@(backingAsset.path.c_str())];
7675
_totalSizeInBytes = backingAsset.total_size_in_bytes();
76+
_backingAsset = std::move(backingAsset);
7777
}
7878

7979
return self;

backends/apple/coreml/runtime/delegate/ETCoreMLAssetManager.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// ETCoreMLAssetManager.h
33
//
4-
// Copyright © 2023 Apple Inc. All rights reserved.
4+
// Copyright © 2024 Apple Inc. All rights reserved.
55
//
66
// Please refer to the license found in the LICENSE file in the root directory of the source tree.
77

@@ -117,6 +117,9 @@ NS_ASSUME_NONNULL_BEGIN
117117
/// contents are deleted asynchronously.
118118
@property (copy, readonly, nonatomic) NSURL* trashDirectoryURL;
119119

120+
/// The file manager.
121+
@property (strong, readonly, nonatomic) NSFileManager* fileManager;
122+
120123
@end
121124

122125
NS_ASSUME_NONNULL_END

backends/apple/coreml/runtime/delegate/ETCoreMLAssetManager.mm

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
//
22
// ETCoreMLAssetManager.mm
33
//
4-
// Copyright © 2023 Apple Inc. All rights reserved.
4+
// Copyright © 2024 Apple Inc. All rights reserved.
55
//
66
// Please refer to the license found in the LICENSE file in the root directory of the source tree.
77

88
#import "ETCoreMLAssetManager.h"
9-
10-
#import <iostream>
11-
#import <sstream>
12-
9+
#import <ETCoreMLAsset.h>
10+
#import <ETCoreMLLogging.h>
1311
#import <database.hpp>
12+
#import <iostream>
1413
#import <json_key_value_store.hpp>
1514
#import <serde_json.h>
16-
17-
#import <ETCoreMLAsset.h>
18-
#import <ETCoreMLLogging.h>
15+
#import <sstream>
1916

2017
namespace {
2118

@@ -264,7 +261,6 @@ @interface ETCoreMLAssetManager () <NSFileManagerDelegate> {
264261

265262
@property (assign, readwrite, atomic) NSInteger estimatedSizeInBytes;
266263
@property (copy, readonly, nonatomic) NSURL *assetsDirectoryURL;
267-
@property (strong, readonly, nonatomic) NSFileManager *fileManager;
268264
@property (strong, readonly, nonatomic) dispatch_queue_t syncQueue;
269265
@property (strong, readonly, nonatomic) dispatch_queue_t trashQueue;
270266
@property (strong, readonly, nonatomic) NSMapTable<NSString *, ETCoreMLAsset *> *assetsInUseMap;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// ETCoreMLComputeUnits.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+
/// An enum representing the compute options on the system.
9+
typedef NS_OPTIONS(NSUInteger, ETCoreMLComputeUnits) {
10+
ETCoreMLComputeUnitUnknown = 0, // Unknown compute unit.
11+
ETCoreMLComputeUnitCPU = 1 << 0, // Represents the CPU compute unit.
12+
ETCoreMLComputeUnitGPU = 1 << 1, // Represents the GPU compute unit.
13+
ETCoreMLComputeUnitNeuralEngine = 1 << 2 // Represents the NeuralEngine compute unit.
14+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// ETCoreMLDefaultModelExecutor.h
3+
// executorchcoreml_tests
4+
//
5+
// Created by Gyan Sinha on 2/25/24.
6+
//
7+
8+
#import <CoreML/CoreML.h>
9+
10+
#import <ETCoreMLModelExecutor.h>
11+
12+
@class ETCoreMLModel;
13+
14+
NS_ASSUME_NONNULL_BEGIN
15+
/// The default model executor, the executor ignores logging options.
16+
__attribute__((objc_subclassing_restricted)) @interface ETCoreMLDefaultModelExecutor : NSObject<ETCoreMLModelExecutor>
17+
18+
+ (instancetype)new NS_UNAVAILABLE;
19+
20+
- (instancetype)init NS_UNAVAILABLE;
21+
22+
/// Constructs an `ETCoreMLDefaultModelExecutor` from the given model.
23+
///
24+
/// @param model The model.
25+
- (instancetype)initWithModel:(ETCoreMLModel*)model NS_DESIGNATED_INITIALIZER;
26+
27+
/// The model.
28+
@property (readonly, strong, nonatomic) ETCoreMLModel* model;
29+
30+
@end
31+
32+
NS_ASSUME_NONNULL_END
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// ETCoreMLDefaultModelExecutor.m
3+
// executorchcoreml_tests
4+
//
5+
// Created by Gyan Sinha on 2/25/24.
6+
//
7+
8+
#import <ETCoreMLAsset.h>
9+
#import <ETCoreMLDefaultModelExecutor.h>
10+
#import <ETCoreMLLogging.h>
11+
#import <ETCoreMLModel.h>
12+
13+
@implementation ETCoreMLDefaultModelExecutor
14+
15+
- (instancetype)initWithModel:(ETCoreMLModel *)model {
16+
self = [super init];
17+
if (self) {
18+
_model = model;
19+
}
20+
21+
return self;
22+
}
23+
24+
- (nullable NSArray<MLMultiArray *> *)executeModelWithInputs:(id<MLFeatureProvider>)inputs
25+
predictionOptions:(MLPredictionOptions *)predictionOptions
26+
loggingOptions:(const executorchcoreml::ModelLoggingOptions& __unused)loggingOptions
27+
eventLogger:(const executorchcoreml::ModelEventLogger* _Nullable __unused)eventLogger
28+
error:(NSError * __autoreleasing *)error {
29+
id<MLFeatureProvider> outputs = [self.model.mlModel predictionFromFeatures:inputs
30+
options:predictionOptions
31+
error:error];
32+
if (!outputs) {
33+
return nil;
34+
}
35+
36+
NSOrderedSet<NSString*>* orderedOutputNames = self.model.orderedOutputNames;
37+
NSMutableArray<MLMultiArray *> *result = [NSMutableArray arrayWithCapacity:orderedOutputNames.count];
38+
for (NSString *outputName in orderedOutputNames) {
39+
MLFeatureValue *featureValue = [outputs featureValueForName:outputName];
40+
if (!featureValue.multiArrayValue) {
41+
ETCoreMLLogErrorAndSetNSError(error,
42+
ETCoreMLErrorBrokenModel,
43+
"%@: Model is broken, expected multiarray for output=%@.",
44+
NSStringFromClass(self.class),
45+
outputName);
46+
return nil;
47+
}
48+
49+
[result addObject:featureValue.multiArrayValue];
50+
}
51+
52+
return result;
53+
}
54+
55+
@end

backends/apple/coreml/runtime/delegate/ETCoreMLLogging.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// ETCoreMLLogging.h
33
//
4-
// Copyright © 2023 Apple Inc. All rights reserved.
4+
// Copyright © 2024 Apple Inc. All rights reserved.
55
//
66
// Please refer to the license found in the LICENSE file in the root directory of the source tree.
77

@@ -22,7 +22,8 @@ typedef NS_ERROR_ENUM(ETCoreMLErrorDomain, ETCoreMLError) {
2222
ETCoreMLErrorBrokenModel, // CoreML model doesn't match the input and output specification.
2323
ETCoreMLErrorCompilationFailed, // CoreML model failed to compile.
2424
ETCoreMLErrorModelSaveFailed, // Failed to save CoreML model to disk.
25-
ETCoreMLErrorModelCacheCreationFailed // Failed to create model cache.
25+
ETCoreMLErrorModelCacheCreationFailed, // Failed to create model cache.
26+
ETCoreMLErrorInternalError, // Internal error.
2627
};
2728

2829
@interface ETCoreMLErrorUtils : NSObject

backends/apple/coreml/runtime/delegate/ETCoreMLLogging.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// ETCoreMLLogging.mm
33
//
4-
// Copyright © 2023 Apple Inc. All rights reserved.
4+
// Copyright © 2024 Apple Inc. All rights reserved.
55
//
66
// Please refer to the license found in the LICENSE file in the root directory of the source tree.
77

backends/apple/coreml/runtime/delegate/ETCoreMLModel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// ETCoreMLModel.h
33
//
4-
// Copyright © 2023 Apple Inc. All rights reserved.
4+
// Copyright © 2024 Apple Inc. All rights reserved.
55
//
66
// Please refer to the license found in the LICENSE file in the root directory of the source tree.
77

0 commit comments

Comments
 (0)