From 0083335ba0e9d6becbe0958903b0a27fc2ebaeed Mon Sep 17 00:00:00 2001 From: glaszig Date: Tue, 24 Jun 2025 04:24:27 -0300 Subject: [PATCH] coreml : backport CoreML features to macos < 14 (#3255) --- src/CMakeLists.txt | 2 ++ src/coreml/whisper-compat.h | 10 +++++++++ src/coreml/whisper-compat.m | 35 +++++++++++++++++++++++++++++++ src/coreml/whisper-decoder-impl.m | 1 + src/coreml/whisper-encoder-impl.m | 1 + 5 files changed, 49 insertions(+) create mode 100644 src/coreml/whisper-compat.h create mode 100644 src/coreml/whisper-compat.m diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a091e66a..2eae0c66 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -58,6 +58,7 @@ if (WHISPER_COREML) set(TARGET whisper.coreml) add_library(${TARGET} + coreml/whisper-compat.m coreml/whisper-encoder.h coreml/whisper-encoder.mm coreml/whisper-encoder-impl.h @@ -76,6 +77,7 @@ if (WHISPER_COREML) COMPILE_FLAGS "-fobjc-arc" XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC YES ) + set_target_properties(${TARGET} PROPERTIES FOLDER "libs") endif() diff --git a/src/coreml/whisper-compat.h b/src/coreml/whisper-compat.h new file mode 100644 index 00000000..feacb9e8 --- /dev/null +++ b/src/coreml/whisper-compat.h @@ -0,0 +1,10 @@ +#import + +@interface MLModel (Compat) +- (void) predictionFromFeatures:(id) input + completionHandler:(void (^)(id output, NSError * error)) completionHandler; + +- (void) predictionFromFeatures:(id) input + options:(MLPredictionOptions *) options + completionHandler:(void (^)(id output, NSError * error)) completionHandler; +@end diff --git a/src/coreml/whisper-compat.m b/src/coreml/whisper-compat.m new file mode 100644 index 00000000..690414ca --- /dev/null +++ b/src/coreml/whisper-compat.m @@ -0,0 +1,35 @@ +#import "whisper-compat.h" +#import + +@implementation MLModel (Compat) + +#if !defined(MAC_OS_X_VERSION_14_00) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_14_00 + +- (void) predictionFromFeatures:(id) input + completionHandler:(void (^)(id output, NSError * error)) completionHandler { + [NSOperationQueue.new addOperationWithBlock:^{ + NSError *error = nil; + id prediction = [self predictionFromFeatures:input error:&error]; + + [NSOperationQueue.mainQueue addOperationWithBlock:^{ + completionHandler(prediction, error); + }]; + }]; +} + +- (void) predictionFromFeatures:(id) input + options:(MLPredictionOptions *) options + completionHandler:(void (^)(id output, NSError * error)) completionHandler { + [NSOperationQueue.new addOperationWithBlock:^{ + NSError *error = nil; + id prediction = [self predictionFromFeatures:input options:options error:&error]; + + [NSOperationQueue.mainQueue addOperationWithBlock:^{ + completionHandler(prediction, error); + }]; + }]; +} + +#endif + +@end diff --git a/src/coreml/whisper-decoder-impl.m b/src/coreml/whisper-decoder-impl.m index 732992e1..de1fd1ea 100644 --- a/src/coreml/whisper-decoder-impl.m +++ b/src/coreml/whisper-decoder-impl.m @@ -8,6 +8,7 @@ #error This file must be compiled with automatic reference counting enabled (-fobjc-arc) #endif +#import "whisper-compat.h" #import "whisper-decoder-impl.h" @implementation whisper_decoder_implInput diff --git a/src/coreml/whisper-encoder-impl.m b/src/coreml/whisper-encoder-impl.m index 2ed9dc61..0ddda3d3 100644 --- a/src/coreml/whisper-encoder-impl.m +++ b/src/coreml/whisper-encoder-impl.m @@ -8,6 +8,7 @@ #error This file must be compiled with automatic reference counting enabled (-fobjc-arc) #endif +#import "whisper-compat.h" #import "whisper-encoder-impl.h" @implementation whisper_encoder_implInput