mirror of
https://github.com/ggerganov/whisper.cpp.git
synced 2024-12-30 09:18:51 +00:00
5e47e223bd
* coreml : use Core ML encoder inference * coreml : simlpify whisper_encode + log messages * whisper : resolve rebase conflicts * coreml : add scripts for CoreML model generation * bench-all : recognize COREML flag
202 lines
8.6 KiB
Objective-C
202 lines
8.6 KiB
Objective-C
//
|
|
// whisper-decoder-impl.m
|
|
//
|
|
// This file was automatically generated and should not be edited.
|
|
//
|
|
|
|
#if !__has_feature(objc_arc)
|
|
#error This file must be compiled with automatic reference counting enabled (-fobjc-arc)
|
|
#endif
|
|
|
|
#import "whisper-decoder-impl.h"
|
|
|
|
@implementation whisper_decoder_implInput
|
|
|
|
- (instancetype)initWithToken_data:(MLMultiArray *)token_data audio_data:(MLMultiArray *)audio_data {
|
|
self = [super init];
|
|
if (self) {
|
|
_token_data = token_data;
|
|
_audio_data = audio_data;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (NSSet<NSString *> *)featureNames {
|
|
return [NSSet setWithArray:@[@"token_data", @"audio_data"]];
|
|
}
|
|
|
|
- (nullable MLFeatureValue *)featureValueForName:(NSString *)featureName {
|
|
if ([featureName isEqualToString:@"token_data"]) {
|
|
return [MLFeatureValue featureValueWithMultiArray:self.token_data];
|
|
}
|
|
if ([featureName isEqualToString:@"audio_data"]) {
|
|
return [MLFeatureValue featureValueWithMultiArray:self.audio_data];
|
|
}
|
|
return nil;
|
|
}
|
|
|
|
@end
|
|
|
|
@implementation whisper_decoder_implOutput
|
|
|
|
- (instancetype)initWithVar_1346:(MLMultiArray *)var_1346 {
|
|
self = [super init];
|
|
if (self) {
|
|
_var_1346 = var_1346;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (NSSet<NSString *> *)featureNames {
|
|
return [NSSet setWithArray:@[@"var_1346"]];
|
|
}
|
|
|
|
- (nullable MLFeatureValue *)featureValueForName:(NSString *)featureName {
|
|
if ([featureName isEqualToString:@"var_1346"]) {
|
|
return [MLFeatureValue featureValueWithMultiArray:self.var_1346];
|
|
}
|
|
return nil;
|
|
}
|
|
|
|
@end
|
|
|
|
@implementation whisper_decoder_impl
|
|
|
|
|
|
/**
|
|
URL of the underlying .mlmodelc directory.
|
|
*/
|
|
+ (nullable NSURL *)URLOfModelInThisBundle {
|
|
NSString *assetPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"whisper_decoder_impl" ofType:@"mlmodelc"];
|
|
if (nil == assetPath) { os_log_error(OS_LOG_DEFAULT, "Could not load whisper-decoder-impl.mlmodelc in the bundle resource"); return nil; }
|
|
return [NSURL fileURLWithPath:assetPath];
|
|
}
|
|
|
|
|
|
/**
|
|
Initialize whisper_decoder_impl instance from an existing MLModel object.
|
|
|
|
Usually the application does not use this initializer unless it makes a subclass of whisper_decoder_impl.
|
|
Such application may want to use `-[MLModel initWithContentsOfURL:configuration:error:]` and `+URLOfModelInThisBundle` to create a MLModel object to pass-in.
|
|
*/
|
|
- (instancetype)initWithMLModel:(MLModel *)model {
|
|
self = [super init];
|
|
if (!self) { return nil; }
|
|
_model = model;
|
|
if (_model == nil) { return nil; }
|
|
return self;
|
|
}
|
|
|
|
|
|
/**
|
|
Initialize whisper_decoder_impl instance with the model in this bundle.
|
|
*/
|
|
- (nullable instancetype)init {
|
|
return [self initWithContentsOfURL:(NSURL * _Nonnull)self.class.URLOfModelInThisBundle error:nil];
|
|
}
|
|
|
|
|
|
/**
|
|
Initialize whisper_decoder_impl instance with the model in this bundle.
|
|
|
|
@param configuration The model configuration object
|
|
@param error If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors, pass in NULL.
|
|
*/
|
|
- (nullable instancetype)initWithConfiguration:(MLModelConfiguration *)configuration error:(NSError * _Nullable __autoreleasing * _Nullable)error {
|
|
return [self initWithContentsOfURL:(NSURL * _Nonnull)self.class.URLOfModelInThisBundle configuration:configuration error:error];
|
|
}
|
|
|
|
|
|
/**
|
|
Initialize whisper_decoder_impl instance from the model URL.
|
|
|
|
@param modelURL URL to the .mlmodelc directory for whisper_decoder_impl.
|
|
@param error If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors, pass in NULL.
|
|
*/
|
|
- (nullable instancetype)initWithContentsOfURL:(NSURL *)modelURL error:(NSError * _Nullable __autoreleasing * _Nullable)error {
|
|
MLModel *model = [MLModel modelWithContentsOfURL:modelURL error:error];
|
|
if (model == nil) { return nil; }
|
|
return [self initWithMLModel:model];
|
|
}
|
|
|
|
|
|
/**
|
|
Initialize whisper_decoder_impl instance from the model URL.
|
|
|
|
@param modelURL URL to the .mlmodelc directory for whisper_decoder_impl.
|
|
@param configuration The model configuration object
|
|
@param error If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors, pass in NULL.
|
|
*/
|
|
- (nullable instancetype)initWithContentsOfURL:(NSURL *)modelURL configuration:(MLModelConfiguration *)configuration error:(NSError * _Nullable __autoreleasing * _Nullable)error {
|
|
MLModel *model = [MLModel modelWithContentsOfURL:modelURL configuration:configuration error:error];
|
|
if (model == nil) { return nil; }
|
|
return [self initWithMLModel:model];
|
|
}
|
|
|
|
|
|
/**
|
|
Construct whisper_decoder_impl instance asynchronously with configuration.
|
|
Model loading may take time when the model content is not immediately available (e.g. encrypted model). Use this factory method especially when the caller is on the main thread.
|
|
|
|
@param configuration The model configuration
|
|
@param handler When the model load completes successfully or unsuccessfully, the completion handler is invoked with a valid whisper_decoder_impl instance or NSError object.
|
|
*/
|
|
+ (void)loadWithConfiguration:(MLModelConfiguration *)configuration completionHandler:(void (^)(whisper_decoder_impl * _Nullable model, NSError * _Nullable error))handler {
|
|
[self loadContentsOfURL:(NSURL * _Nonnull)[self URLOfModelInThisBundle]
|
|
configuration:configuration
|
|
completionHandler:handler];
|
|
}
|
|
|
|
|
|
/**
|
|
Construct whisper_decoder_impl instance asynchronously with URL of .mlmodelc directory and optional configuration.
|
|
|
|
Model loading may take time when the model content is not immediately available (e.g. encrypted model). Use this factory method especially when the caller is on the main thread.
|
|
|
|
@param modelURL The model URL.
|
|
@param configuration The model configuration
|
|
@param handler When the model load completes successfully or unsuccessfully, the completion handler is invoked with a valid whisper_decoder_impl instance or NSError object.
|
|
*/
|
|
+ (void)loadContentsOfURL:(NSURL *)modelURL configuration:(MLModelConfiguration *)configuration completionHandler:(void (^)(whisper_decoder_impl * _Nullable model, NSError * _Nullable error))handler {
|
|
[MLModel loadContentsOfURL:modelURL
|
|
configuration:configuration
|
|
completionHandler:^(MLModel *model, NSError *error) {
|
|
if (model != nil) {
|
|
whisper_decoder_impl *typedModel = [[whisper_decoder_impl alloc] initWithMLModel:model];
|
|
handler(typedModel, nil);
|
|
} else {
|
|
handler(nil, error);
|
|
}
|
|
}];
|
|
}
|
|
|
|
- (nullable whisper_decoder_implOutput *)predictionFromFeatures:(whisper_decoder_implInput *)input error:(NSError * _Nullable __autoreleasing * _Nullable)error {
|
|
return [self predictionFromFeatures:input options:[[MLPredictionOptions alloc] init] error:error];
|
|
}
|
|
|
|
- (nullable whisper_decoder_implOutput *)predictionFromFeatures:(whisper_decoder_implInput *)input options:(MLPredictionOptions *)options error:(NSError * _Nullable __autoreleasing * _Nullable)error {
|
|
id<MLFeatureProvider> outFeatures = [self.model predictionFromFeatures:input options:options error:error];
|
|
if (!outFeatures) { return nil; }
|
|
return [[whisper_decoder_implOutput alloc] initWithVar_1346:(MLMultiArray *)[outFeatures featureValueForName:@"var_1346"].multiArrayValue];
|
|
}
|
|
|
|
- (nullable whisper_decoder_implOutput *)predictionFromToken_data:(MLMultiArray *)token_data audio_data:(MLMultiArray *)audio_data error:(NSError * _Nullable __autoreleasing * _Nullable)error {
|
|
whisper_decoder_implInput *input_ = [[whisper_decoder_implInput alloc] initWithToken_data:token_data audio_data:audio_data];
|
|
return [self predictionFromFeatures:input_ error:error];
|
|
}
|
|
|
|
- (nullable NSArray<whisper_decoder_implOutput *> *)predictionsFromInputs:(NSArray<whisper_decoder_implInput*> *)inputArray options:(MLPredictionOptions *)options error:(NSError * _Nullable __autoreleasing * _Nullable)error {
|
|
id<MLBatchProvider> inBatch = [[MLArrayBatchProvider alloc] initWithFeatureProviderArray:inputArray];
|
|
id<MLBatchProvider> outBatch = [self.model predictionsFromBatch:inBatch options:options error:error];
|
|
if (!outBatch) { return nil; }
|
|
NSMutableArray<whisper_decoder_implOutput*> *results = [NSMutableArray arrayWithCapacity:(NSUInteger)outBatch.count];
|
|
for (NSInteger i = 0; i < outBatch.count; i++) {
|
|
id<MLFeatureProvider> resultProvider = [outBatch featuresAtIndex:i];
|
|
whisper_decoder_implOutput * result = [[whisper_decoder_implOutput alloc] initWithVar_1346:(MLMultiArray *)[resultProvider featureValueForName:@"var_1346"].multiArrayValue];
|
|
[results addObject:result];
|
|
}
|
|
return results;
|
|
}
|
|
|
|
@end
|