Skip to content

Integrate sdk #2314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backends/apple/coreml/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Mac OS X
*.DS_Store

cmake-out/
*-cmake-out/


# Xcode
Expand All @@ -18,6 +18,7 @@ runtime/runner/build/
runtime/libraries/
runtime/inmemoryfs/build/
runtime/test/models/
runtime/sdk/format/
runtime/include/executorch/
third-party/

Expand Down
87 changes: 86 additions & 1 deletion backends/apple/coreml/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ set(DELEGATE_SOURCES
runtime/delegate/coreml_backend_delegate.mm
runtime/delegate/ETCoreMLAsset.mm
runtime/delegate/ETCoreMLAssetManager.mm
runtime/delegate/ETCoreMLDefaultModelExecutor.mm
runtime/delegate/ETCoreMLModelLoader.mm
runtime/delegate/ETCoreMLModelCompiler.mm
runtime/delegate/ETCoreMLLogging.mm
runtime/delegate/ETCoreMLModel.mm
runtime/delegate/ETCoreMLModelManager.mm
Expand All @@ -54,6 +57,57 @@ set(UTIL_SOURCES
runtime/util/objc_json_serde.mm
)

# sdk sources
set(SDK_SOURCES
runtime/sdk/ETCoreMLModelAnalyzer.mm
runtime/sdk/ETCoreMLModelStructurePath.mm
runtime/sdk/ETCoreMLOperationProfilingInfo.mm
runtime/sdk/ETCoreMLModelDebugger.mm
runtime/sdk/ETCoreMLModelProfiler.mm
runtime/sdk/ETCoreMLPair.mm
runtime/sdk/model_package_info.mm
runtime/sdk/model_event_logger_impl.mm
runtime/sdk/program_path.mm
)

# protobuf sources
set(PROTOBUF_SOURCES
runtime/sdk/format/ArrayFeatureExtractor.pb.cc
runtime/sdk/format/AudioFeaturePrint.pb.cc
runtime/sdk/format/BayesianProbitRegressor.pb.cc
runtime/sdk/format/CategoricalMapping.pb.cc
runtime/sdk/format/ClassConfidenceThresholding.pb.cc
runtime/sdk/format/CustomModel.pb.cc
runtime/sdk/format/DataStructures.pb.cc
runtime/sdk/format/DictVectorizer.pb.cc
runtime/sdk/format/FeatureTypes.pb.cc
runtime/sdk/format/FeatureVectorizer.pb.cc
runtime/sdk/format/Gazetteer.pb.cc
runtime/sdk/format/GLMClassifier.pb.cc
runtime/sdk/format/GLMRegressor.pb.cc
runtime/sdk/format/Identity.pb.cc
runtime/sdk/format/Imputer.pb.cc
runtime/sdk/format/ItemSimilarityRecommender.pb.cc
runtime/sdk/format/LinkedModel.pb.cc
runtime/sdk/format/MIL.pb.cc
runtime/sdk/format/Model.pb.cc
runtime/sdk/format/NearestNeighbors.pb.cc
runtime/sdk/format/NeuralNetwork.pb.cc
runtime/sdk/format/NonMaximumSuppression.pb.cc
runtime/sdk/format/Normalizer.pb.cc
runtime/sdk/format/NonMaximumSuppression.pb.cc
runtime/sdk/format/OneHotEncoder.pb.cc
runtime/sdk/format/Parameters.pb.cc
runtime/sdk/format/Scaler.pb.cc
runtime/sdk/format/SoundAnalysisPreprocessing.pb.cc
runtime/sdk/format/SVM.pb.cc
runtime/sdk/format/TextClassifier.pb.cc
runtime/sdk/format/TreeEnsemble.pb.cc
runtime/sdk/format/VisionFeaturePrint.pb.cc
runtime/sdk/format/WordEmbedding.pb.cc
runtime/sdk/format/WordTagger.pb.cc
)

# Define the delegate library
add_library(coremldelegate)
target_sources(
Expand All @@ -63,6 +117,7 @@ target_sources(
${DELEGATE_SOURCES}
${UTIL_SOURCES}
)

target_include_directories(
coremldelegate PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/runtime/include
Expand All @@ -87,7 +142,30 @@ target_include_directories(
coremldelegate PRIVATE
${EXECUTORCH_ROOT}/..
)
target_compile_options(executorch PUBLIC -DET_EVENT_TRACER_ENABLED)
target_link_libraries(
coremldelegate PRIVATE
executorch
)

if(EXECUTORCH_BUILD_SDK)
target_sources(
coremldelegate PRIVATE
${SDK_SOURCES}
${PROTOBUF_SOURCES}
)
target_include_directories(
coremldelegate PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/runtime/sdk
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/src
)
add_subdirectory(
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/cmake
)
target_link_libraries(
coremldelegate PRIVATE
libprotobuf-lite
)
endif()

find_library(ACCELERATE_FRAMEWORK Accelerate)
find_library(COREML_FRAMEWORK CoreML)
Expand All @@ -105,7 +183,14 @@ target_link_libraries(coremldelegate

target_compile_options(coremldelegate PRIVATE "-fobjc-arc")
target_compile_options(coremldelegate PRIVATE "-fno-exceptions")

if(EXECUTORCH_BUILD_SDK)
target_compile_options(executorch PUBLIC -DET_EVENT_TRACER_ENABLED)
target_compile_options(coremldelegate PRIVATE "-frtti")
target_compile_options(libprotobuf-lite PRIVATE "-frtti")
else()
target_compile_options(coremldelegate PRIVATE "-fno-rtti")
endif()

set(
TARGET coremldelegate
Expand Down
8 changes: 4 additions & 4 deletions backends/apple/coreml/runtime/delegate/ETCoreMLAsset.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// ETCoreMLAsset.h
//
// Copyright © 2023 Apple Inc. All rights reserved.
// Copyright © 2024 Apple Inc. All rights reserved.
//
// Please refer to the license found in the LICENSE file in the root directory of the source tree.

Expand All @@ -11,7 +11,7 @@

NS_ASSUME_NONNULL_BEGIN

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

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

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

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

@end
Expand Down
6 changes: 3 additions & 3 deletions backends/apple/coreml/runtime/delegate/ETCoreMLAsset.mm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// ETCoreMLAsset.mm
//
// Copyright © 2023 Apple Inc. All rights reserved.
// Copyright © 2024 Apple Inc. All rights reserved.
//
// Please refer to the license found in the LICENSE file in the root directory of the source tree.

Expand Down Expand Up @@ -66,14 +66,14 @@ @implementation ETCoreMLAsset {
os_unfair_lock _lock;
}

- (instancetype)initWithBackingAsset:(const executorchcoreml::Asset&)backingAsset {
- (instancetype)initWithBackingAsset:(executorchcoreml::Asset)backingAsset {
self = [super init];
if (self) {
_backingAsset = backingAsset;
_isValid = static_cast<BOOL>(is_asset_valid(backingAsset));
_identifier = @(backingAsset.identifier.c_str());
_contentURL = [NSURL fileURLWithPath:@(backingAsset.path.c_str())];
_totalSizeInBytes = backingAsset.total_size_in_bytes();
_backingAsset = std::move(backingAsset);
}

return self;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// ETCoreMLAssetManager.h
//
// Copyright © 2023 Apple Inc. All rights reserved.
// Copyright © 2024 Apple Inc. All rights reserved.
//
// Please refer to the license found in the LICENSE file in the root directory of the source tree.

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

/// The file manager.
@property (strong, readonly, nonatomic) NSFileManager* fileManager;

@end

NS_ASSUME_NONNULL_END
14 changes: 5 additions & 9 deletions backends/apple/coreml/runtime/delegate/ETCoreMLAssetManager.mm
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
//
// ETCoreMLAssetManager.mm
//
// Copyright © 2023 Apple Inc. All rights reserved.
// Copyright © 2024 Apple Inc. All rights reserved.
//
// Please refer to the license found in the LICENSE file in the root directory of the source tree.

#import "ETCoreMLAssetManager.h"

#import <iostream>
#import <sstream>

#import <ETCoreMLAsset.h>
#import <ETCoreMLLogging.h>
#import <database.hpp>
#import <iostream>
#import <json_key_value_store.hpp>
#import <serde_json.h>

#import <ETCoreMLAsset.h>
#import <ETCoreMLLogging.h>
#import <sstream>

namespace {

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

@property (assign, readwrite, atomic) NSInteger estimatedSizeInBytes;
@property (copy, readonly, nonatomic) NSURL *assetsDirectoryURL;
@property (strong, readonly, nonatomic) NSFileManager *fileManager;
@property (strong, readonly, nonatomic) dispatch_queue_t syncQueue;
@property (strong, readonly, nonatomic) dispatch_queue_t trashQueue;
@property (strong, readonly, nonatomic) NSMapTable<NSString *, ETCoreMLAsset *> *assetsInUseMap;
Expand Down
14 changes: 14 additions & 0 deletions backends/apple/coreml/runtime/delegate/ETCoreMLComputeUnits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// ETCoreMLComputeUnits.h
//
// Copyright © 2024 Apple Inc. All rights reserved.
//
// Please refer to the license found in the LICENSE file in the root directory of the source tree.

/// An enum representing the compute options on the system.
typedef NS_OPTIONS(NSUInteger, ETCoreMLComputeUnits) {
ETCoreMLComputeUnitUnknown = 0, // Unknown compute unit.
ETCoreMLComputeUnitCPU = 1 << 0, // Represents the CPU compute unit.
ETCoreMLComputeUnitGPU = 1 << 1, // Represents the GPU compute unit.
ETCoreMLComputeUnitNeuralEngine = 1 << 2 // Represents the NeuralEngine compute unit.
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// ETCoreMLDefaultModelExecutor.h
// executorchcoreml_tests
//
// Created by Gyan Sinha on 2/25/24.
//

#import <CoreML/CoreML.h>

#import <ETCoreMLModelExecutor.h>

@class ETCoreMLModel;

NS_ASSUME_NONNULL_BEGIN
/// The default model executor, the executor ignores logging options.
__attribute__((objc_subclassing_restricted)) @interface ETCoreMLDefaultModelExecutor : NSObject<ETCoreMLModelExecutor>

+ (instancetype)new NS_UNAVAILABLE;

- (instancetype)init NS_UNAVAILABLE;

/// Constructs an `ETCoreMLDefaultModelExecutor` from the given model.
///
/// @param model The model.
- (instancetype)initWithModel:(ETCoreMLModel*)model NS_DESIGNATED_INITIALIZER;

/// The model.
@property (readonly, strong, nonatomic) ETCoreMLModel* model;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// ETCoreMLDefaultModelExecutor.m
// executorchcoreml_tests
//
// Created by Gyan Sinha on 2/25/24.
//

#import <ETCoreMLAsset.h>
#import <ETCoreMLDefaultModelExecutor.h>
#import <ETCoreMLLogging.h>
#import <ETCoreMLModel.h>

@implementation ETCoreMLDefaultModelExecutor

- (instancetype)initWithModel:(ETCoreMLModel *)model {
self = [super init];
if (self) {
_model = model;
}

return self;
}

- (nullable NSArray<MLMultiArray *> *)executeModelWithInputs:(id<MLFeatureProvider>)inputs
predictionOptions:(MLPredictionOptions *)predictionOptions
loggingOptions:(const executorchcoreml::ModelLoggingOptions& __unused)loggingOptions
eventLogger:(const executorchcoreml::ModelEventLogger* _Nullable __unused)eventLogger
error:(NSError * __autoreleasing *)error {
id<MLFeatureProvider> outputs = [self.model.mlModel predictionFromFeatures:inputs
options:predictionOptions
error:error];
if (!outputs) {
return nil;
}

NSOrderedSet<NSString*>* orderedOutputNames = self.model.orderedOutputNames;
NSMutableArray<MLMultiArray *> *result = [NSMutableArray arrayWithCapacity:orderedOutputNames.count];
for (NSString *outputName in orderedOutputNames) {
MLFeatureValue *featureValue = [outputs featureValueForName:outputName];
if (!featureValue.multiArrayValue) {
ETCoreMLLogErrorAndSetNSError(error,
ETCoreMLErrorBrokenModel,
"%@: Model is broken, expected multiarray for output=%@.",
NSStringFromClass(self.class),
outputName);
return nil;
}

[result addObject:featureValue.multiArrayValue];
}

return result;
}

@end
5 changes: 3 additions & 2 deletions backends/apple/coreml/runtime/delegate/ETCoreMLLogging.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// ETCoreMLLogging.h
//
// Copyright © 2023 Apple Inc. All rights reserved.
// Copyright © 2024 Apple Inc. All rights reserved.
//
// Please refer to the license found in the LICENSE file in the root directory of the source tree.

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

@interface ETCoreMLErrorUtils : NSObject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// ETCoreMLLogging.mm
//
// Copyright © 2023 Apple Inc. All rights reserved.
// Copyright © 2024 Apple Inc. All rights reserved.
//
// Please refer to the license found in the LICENSE file in the root directory of the source tree.

Expand Down
2 changes: 1 addition & 1 deletion backends/apple/coreml/runtime/delegate/ETCoreMLModel.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// ETCoreMLModel.h
//
// Copyright © 2023 Apple Inc. All rights reserved.
// Copyright © 2024 Apple Inc. All rights reserved.
//
// Please refer to the license found in the LICENSE file in the root directory of the source tree.

Expand Down
Loading