]> git.djapps.eu Git - pkg/ggml/sources/whisper.cpp/commitdiff
coreml : backport CoreML features to macos < 14 (#3255)
authorglaszig <redacted>
Tue, 24 Jun 2025 07:24:27 +0000 (04:24 -0300)
committerGitHub <redacted>
Tue, 24 Jun 2025 07:24:27 +0000 (09:24 +0200)
src/CMakeLists.txt
src/coreml/whisper-compat.h [new file with mode: 0644]
src/coreml/whisper-compat.m [new file with mode: 0644]
src/coreml/whisper-decoder-impl.m
src/coreml/whisper-encoder-impl.m

index a091e66a25f24774da16256da17ab747179a2f29..2eae0c66c78f36354f9620dc3416fa62298f7d6d 100644 (file)
@@ -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 (file)
index 0000000..feacb9e
--- /dev/null
@@ -0,0 +1,10 @@
+#import <CoreML/CoreML.h>
+
+@interface MLModel (Compat)
+- (void) predictionFromFeatures:(id<MLFeatureProvider>) input
+              completionHandler:(void (^)(id<MLFeatureProvider> output, NSError * error)) completionHandler;
+
+- (void) predictionFromFeatures:(id<MLFeatureProvider>) input
+                        options:(MLPredictionOptions *) options
+              completionHandler:(void (^)(id<MLFeatureProvider> output, NSError * error)) completionHandler;
+@end
diff --git a/src/coreml/whisper-compat.m b/src/coreml/whisper-compat.m
new file mode 100644 (file)
index 0000000..690414c
--- /dev/null
@@ -0,0 +1,35 @@
+#import "whisper-compat.h"
+#import <Foundation/Foundation.h>
+
+@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<MLFeatureProvider>) input
+              completionHandler:(void (^)(id<MLFeatureProvider> output, NSError * error)) completionHandler {
+    [NSOperationQueue.new addOperationWithBlock:^{
+        NSError *error = nil;
+        id<MLFeatureProvider> prediction = [self predictionFromFeatures:input error:&error];
+
+        [NSOperationQueue.mainQueue addOperationWithBlock:^{
+            completionHandler(prediction, error);
+        }];
+    }];
+}
+
+- (void) predictionFromFeatures:(id<MLFeatureProvider>) input
+                        options:(MLPredictionOptions *) options
+              completionHandler:(void (^)(id<MLFeatureProvider> output, NSError * error)) completionHandler {
+    [NSOperationQueue.new addOperationWithBlock:^{
+        NSError *error = nil;
+        id<MLFeatureProvider> prediction = [self predictionFromFeatures:input options:options error:&error];
+
+        [NSOperationQueue.mainQueue addOperationWithBlock:^{
+            completionHandler(prediction, error);
+        }];
+    }];
+}
+
+#endif
+
+@end
index 732992e193c68b6428d1c4dc182ba56af1122159..de1fd1ea0b7076757e3a07c543b4f2eabff27654 100644 (file)
@@ -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
index 2ed9dc6135135adf016cf25134451da5b89bb4b0..0ddda3d3ceec8eb44bc5718e84850e8951a20e40 100644 (file)
@@ -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