Skip to content

[CoreML Backend] Handle missing data types. #3134

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

Merged
merged 1 commit into from
Apr 22, 2024
Merged
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
10 changes: 10 additions & 0 deletions backends/apple/coreml/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ if(NOT EXECUTORCH_ROOT)
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
endif()

option(COREML_BUILD_EXECUTOR_RUNNER "Build CoreML executor runner." OFF)

# inmemoryfs sources
set(INMEMORYFS_SOURCES
runtime/inmemoryfs/inmemory_filesystem.cpp
Expand Down Expand Up @@ -181,6 +183,14 @@ target_link_libraries(coremldelegate
${SQLITE_LIBRARY}
)

if(COREML_BUILD_EXECUTOR_RUNNER)
target_link_libraries(coremldelegate
PRIVATE
portable_ops_lib
portable_kernels
)
endif()

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ - (NSUInteger)_compact:(NSUInteger)sizeInBytes error:(NSError * __autoreleasing
}

if (_estimatedSizeInBytes <= sizeInBytes) {
return YES;
return _estimatedSizeInBytes;
}

std::error_code ec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ __attribute__((objc_subclassing_restricted)) @interface ETCoreMLDefaultModelExec
/// The model.
@property (readonly, strong, nonatomic) ETCoreMLModel* model;

/// If set to `YES` then output backing are ignored.
@property (readwrite, atomic) BOOL ignoreOutputBackings;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ - (instancetype)initWithModel:(ETCoreMLModel *)model {
loggingOptions:(const executorchcoreml::ModelLoggingOptions& __unused)loggingOptions
eventLogger:(const executorchcoreml::ModelEventLogger* _Nullable __unused)eventLogger
error:(NSError * __autoreleasing *)error {
if (self.ignoreOutputBackings) {
predictionOptions.outputBackings = @{};
}
id<MLFeatureProvider> outputs = [self.model.mlModel predictionFromFeatures:inputs
options:predictionOptions
error:error];
Expand Down
36 changes: 24 additions & 12 deletions backends/apple/coreml/runtime/delegate/ETCoreMLLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#import <Foundation/Foundation.h>

#import <executorch/runtime/platform/log.h>
#import <os/log.h>

NS_ASSUME_NONNULL_BEGIN
Expand Down Expand Up @@ -48,7 +49,11 @@ typedef NS_ERROR_ENUM(ETCoreMLErrorDomain, ETCoreMLError) {

/// Record the error with `os_log_error` and fills `*errorOut` with `NSError`.
#define ETCoreMLLogErrorAndSetNSError(errorOut, errorCode, formatString, ...) \
os_log_error(ETCoreMLErrorUtils.loggingChannel, formatString, ##__VA_ARGS__); \
if (ET_LOG_ENABLED) { \
ET_LOG(Error, "%s", [NSString stringWithFormat:@formatString, ##__VA_ARGS__].UTF8String); \
} else { \
os_log_error(ETCoreMLErrorUtils.loggingChannel, formatString, ##__VA_ARGS__); \
} \
if (errorOut) { \
*errorOut = \
[NSError errorWithDomain:ETCoreMLErrorDomain \
Expand All @@ -58,24 +63,31 @@ typedef NS_ERROR_ENUM(ETCoreMLErrorDomain, ETCoreMLError) {
}]; \
}

/// Record the error and its underlying error with `os_log_error` and fills
/// `*errorOut` with NSError.
/// Record the error and its underlying error with `os_log_error` and fills `*errorOut` with `NSError`.
#define ETCoreMLLogUnderlyingErrorAndSetNSError(errorOut, errorCode, underlyingNSError, formatString, ...) \
os_log_error(ETCoreMLErrorUtils.loggingChannel, \
formatString ", with underlying error= %@.", \
##__VA_ARGS__, \
(underlyingNSError).localizedDescription); \
if (ET_LOG_ENABLED) { \
ET_LOG(Error, "%s", [NSString stringWithFormat:@formatString, ##__VA_ARGS__].UTF8String); \
} else { \
os_log_error(ETCoreMLErrorUtils.loggingChannel, \
formatString ", with underlying error= %@.", \
##__VA_ARGS__, \
(underlyingNSError).localizedDescription); \
} \
if (errorOut) { \
*errorOut = [ETCoreMLErrorUtils errorWithCode:errorCode \
underlyingError:underlyingNSError \
format:@formatString, ##__VA_ARGS__]; \
}

#define ETCoreMLLogError(error, formatString, ...) \
os_log_error(ETCoreMLErrorUtils.loggingChannel, \
formatString ", with error= %@.", \
##__VA_ARGS__, \
(error).localizedDescription);
#define ETCoreMLLogError(error, formatString, ...) \
if (ET_LOG_ENABLED) { \
ET_LOG(Error, "%s", [NSString stringWithFormat:@formatString, ##__VA_ARGS__].UTF8String); \
} else { \
os_log_error(ETCoreMLErrorUtils.loggingChannel, \
formatString ", with error= %@.", \
##__VA_ARGS__, \
(error).localizedDescription); \
}


#pragma clang diagnostic pop
Expand Down
13 changes: 12 additions & 1 deletion backends/apple/coreml/runtime/delegate/ETCoreMLModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
// Please refer to the license found in the LICENSE file in the root directory of the source tree.

#import <CoreML/CoreML.h>
#import <vector>

NS_ASSUME_NONNULL_BEGIN

@class ETCoreMLAsset;

namespace executorchcoreml {
class MultiArray;
}

/// Represents a ML model, the class is a thin wrapper over `MLModel` with additional properties.
@interface ETCoreMLModel : NSObject
__attribute__((objc_subclassing_restricted)) @interface ETCoreMLModel : NSObject

- (instancetype)init NS_UNAVAILABLE;

Expand All @@ -31,6 +36,12 @@ NS_ASSUME_NONNULL_BEGIN
orderedOutputNames:(NSOrderedSet<NSString*>*)orderedOutputNames
error:(NSError* __autoreleasing*)error NS_DESIGNATED_INITIALIZER;

- (nullable NSArray<MLMultiArray*>*)prepareInputs:(const std::vector<executorchcoreml::MultiArray>&)inputs
error:(NSError* __autoreleasing*)error;

- (nullable NSArray<MLMultiArray*>*)prepareOutputBackings:(const std::vector<executorchcoreml::MultiArray>&)outputs
error:(NSError* __autoreleasing*)error;

/// The underlying MLModel.
@property (strong, readonly, nonatomic) MLModel* mlModel;

Expand Down
Loading
Loading