coreml : backport CoreML features to macos < 14 (#3255)

This commit is contained in:
glaszig 2025-06-24 04:24:27 -03:00 committed by GitHub
parent 9c47902308
commit 0083335ba0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 49 additions and 0 deletions

View File

@ -58,6 +58,7 @@ if (WHISPER_COREML)
set(TARGET whisper.coreml) set(TARGET whisper.coreml)
add_library(${TARGET} add_library(${TARGET}
coreml/whisper-compat.m
coreml/whisper-encoder.h coreml/whisper-encoder.h
coreml/whisper-encoder.mm coreml/whisper-encoder.mm
coreml/whisper-encoder-impl.h coreml/whisper-encoder-impl.h
@ -76,6 +77,7 @@ if (WHISPER_COREML)
COMPILE_FLAGS "-fobjc-arc" COMPILE_FLAGS "-fobjc-arc"
XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC YES XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC YES
) )
set_target_properties(${TARGET} PROPERTIES FOLDER "libs") set_target_properties(${TARGET} PROPERTIES FOLDER "libs")
endif() endif()

View File

@ -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

View File

@ -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

View File

@ -8,6 +8,7 @@
#error This file must be compiled with automatic reference counting enabled (-fobjc-arc) #error This file must be compiled with automatic reference counting enabled (-fobjc-arc)
#endif #endif
#import "whisper-compat.h"
#import "whisper-decoder-impl.h" #import "whisper-decoder-impl.h"
@implementation whisper_decoder_implInput @implementation whisper_decoder_implInput

View File

@ -8,6 +8,7 @@
#error This file must be compiled with automatic reference counting enabled (-fobjc-arc) #error This file must be compiled with automatic reference counting enabled (-fobjc-arc)
#endif #endif
#import "whisper-compat.h"
#import "whisper-encoder-impl.h" #import "whisper-encoder-impl.h"
@implementation whisper_encoder_implInput @implementation whisper_encoder_implInput