Skip to content

Commit 28b3232

Browse files
committed
coreml : add scripts for CoreML model generation
1 parent 73cd216 commit 28b3232

10 files changed

+830
-79
lines changed

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
*.o
22
*.a
3-
*.mlmodel
4-
*.mlmodelc
53
.cache/
4+
.coreml/
65
.test/
76
.vs/
87
.vscode/
@@ -37,4 +36,6 @@ examples/whisper.objc/whisper.objc.xcodeproj/project.xcworkspace/xcuserdata
3736

3837
extra/bench-gg.txt
3938

40-
*.mlmodel*
39+
models/*.mlmodel
40+
models/*.mlmodelc
41+
models/*.mlpackage

coreml/whisper-decoder-impl.h

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
//
2+
// whisper-decoder-impl.h
3+
//
4+
// This file was automatically generated and should not be edited.
5+
//
6+
7+
#import <Foundation/Foundation.h>
8+
#import <CoreML/CoreML.h>
9+
#include <stdint.h>
10+
#include <os/log.h>
11+
12+
NS_ASSUME_NONNULL_BEGIN
13+
14+
15+
/// Model Prediction Input Type
16+
API_AVAILABLE(macos(12.0), ios(15.0), watchos(8.0), tvos(15.0)) __attribute__((visibility("hidden")))
17+
@interface whisper_decoder_implInput : NSObject<MLFeatureProvider>
18+
19+
/// token_data as 1 by 1 matrix of 32-bit integers
20+
@property (readwrite, nonatomic, strong) MLMultiArray * token_data;
21+
22+
/// audio_data as 1 × 384 × 1 × 1500 4-dimensional array of floats
23+
@property (readwrite, nonatomic, strong) MLMultiArray * audio_data;
24+
- (instancetype)init NS_UNAVAILABLE;
25+
- (instancetype)initWithToken_data:(MLMultiArray *)token_data audio_data:(MLMultiArray *)audio_data NS_DESIGNATED_INITIALIZER;
26+
27+
@end
28+
29+
30+
/// Model Prediction Output Type
31+
API_AVAILABLE(macos(12.0), ios(15.0), watchos(8.0), tvos(15.0)) __attribute__((visibility("hidden")))
32+
@interface whisper_decoder_implOutput : NSObject<MLFeatureProvider>
33+
34+
/// var_1346 as multidimensional array of floats
35+
@property (readwrite, nonatomic, strong) MLMultiArray * var_1346;
36+
- (instancetype)init NS_UNAVAILABLE;
37+
- (instancetype)initWithVar_1346:(MLMultiArray *)var_1346 NS_DESIGNATED_INITIALIZER;
38+
39+
@end
40+
41+
42+
/// Class for model loading and prediction
43+
API_AVAILABLE(macos(12.0), ios(15.0), watchos(8.0), tvos(15.0)) __attribute__((visibility("hidden")))
44+
@interface whisper_decoder_impl : NSObject
45+
@property (readonly, nonatomic, nullable) MLModel * model;
46+
47+
/**
48+
URL of the underlying .mlmodelc directory.
49+
*/
50+
+ (nullable NSURL *)URLOfModelInThisBundle;
51+
52+
/**
53+
Initialize whisper_decoder_impl instance from an existing MLModel object.
54+
55+
Usually the application does not use this initializer unless it makes a subclass of whisper_decoder_impl.
56+
Such application may want to use `-[MLModel initWithContentsOfURL:configuration:error:]` and `+URLOfModelInThisBundle` to create a MLModel object to pass-in.
57+
*/
58+
- (instancetype)initWithMLModel:(MLModel *)model NS_DESIGNATED_INITIALIZER;
59+
60+
/**
61+
Initialize whisper_decoder_impl instance with the model in this bundle.
62+
*/
63+
- (nullable instancetype)init;
64+
65+
/**
66+
Initialize whisper_decoder_impl instance with the model in this bundle.
67+
68+
@param configuration The model configuration object
69+
@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.
70+
*/
71+
- (nullable instancetype)initWithConfiguration:(MLModelConfiguration *)configuration error:(NSError * _Nullable __autoreleasing * _Nullable)error;
72+
73+
/**
74+
Initialize whisper_decoder_impl instance from the model URL.
75+
76+
@param modelURL URL to the .mlmodelc directory for whisper_decoder_impl.
77+
@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.
78+
*/
79+
- (nullable instancetype)initWithContentsOfURL:(NSURL *)modelURL error:(NSError * _Nullable __autoreleasing * _Nullable)error;
80+
81+
/**
82+
Initialize whisper_decoder_impl instance from the model URL.
83+
84+
@param modelURL URL to the .mlmodelc directory for whisper_decoder_impl.
85+
@param configuration The model configuration object
86+
@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.
87+
*/
88+
- (nullable instancetype)initWithContentsOfURL:(NSURL *)modelURL configuration:(MLModelConfiguration *)configuration error:(NSError * _Nullable __autoreleasing * _Nullable)error;
89+
90+
/**
91+
Construct whisper_decoder_impl instance asynchronously with configuration.
92+
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.
93+
94+
@param configuration The model configuration
95+
@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.
96+
*/
97+
+ (void)loadWithConfiguration:(MLModelConfiguration *)configuration completionHandler:(void (^)(whisper_decoder_impl * _Nullable model, NSError * _Nullable error))handler;
98+
99+
/**
100+
Construct whisper_decoder_impl instance asynchronously with URL of .mlmodelc directory and optional configuration.
101+
102+
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.
103+
104+
@param modelURL The model URL.
105+
@param configuration The model configuration
106+
@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.
107+
*/
108+
+ (void)loadContentsOfURL:(NSURL *)modelURL configuration:(MLModelConfiguration *)configuration completionHandler:(void (^)(whisper_decoder_impl * _Nullable model, NSError * _Nullable error))handler;
109+
110+
/**
111+
Make a prediction using the standard interface
112+
@param input an instance of whisper_decoder_implInput to predict from
113+
@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.
114+
@return the prediction as whisper_decoder_implOutput
115+
*/
116+
- (nullable whisper_decoder_implOutput *)predictionFromFeatures:(whisper_decoder_implInput *)input error:(NSError * _Nullable __autoreleasing * _Nullable)error;
117+
118+
/**
119+
Make a prediction using the standard interface
120+
@param input an instance of whisper_decoder_implInput to predict from
121+
@param options prediction options
122+
@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.
123+
@return the prediction as whisper_decoder_implOutput
124+
*/
125+
- (nullable whisper_decoder_implOutput *)predictionFromFeatures:(whisper_decoder_implInput *)input options:(MLPredictionOptions *)options error:(NSError * _Nullable __autoreleasing * _Nullable)error;
126+
127+
/**
128+
Make a prediction using the convenience interface
129+
@param token_data as 1 by 1 matrix of 32-bit integers:
130+
@param audio_data as 1 × 384 × 1 × 1500 4-dimensional array of floats:
131+
@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.
132+
@return the prediction as whisper_decoder_implOutput
133+
*/
134+
- (nullable whisper_decoder_implOutput *)predictionFromToken_data:(MLMultiArray *)token_data audio_data:(MLMultiArray *)audio_data error:(NSError * _Nullable __autoreleasing * _Nullable)error;
135+
136+
/**
137+
Batch prediction
138+
@param inputArray array of whisper_decoder_implInput instances to obtain predictions from
139+
@param options prediction options
140+
@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.
141+
@return the predictions as NSArray<whisper_decoder_implOutput *>
142+
*/
143+
- (nullable NSArray<whisper_decoder_implOutput *> *)predictionsFromInputs:(NSArray<whisper_decoder_implInput*> *)inputArray options:(MLPredictionOptions *)options error:(NSError * _Nullable __autoreleasing * _Nullable)error;
144+
@end
145+
146+
NS_ASSUME_NONNULL_END

coreml/whisper-decoder-impl.m

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
//
2+
// whisper-decoder-impl.m
3+
//
4+
// This file was automatically generated and should not be edited.
5+
//
6+
7+
#if !__has_feature(objc_arc)
8+
#error This file must be compiled with automatic reference counting enabled (-fobjc-arc)
9+
#endif
10+
11+
#import "whisper-decoder-impl.h"
12+
13+
@implementation whisper_decoder_implInput
14+
15+
- (instancetype)initWithToken_data:(MLMultiArray *)token_data audio_data:(MLMultiArray *)audio_data {
16+
self = [super init];
17+
if (self) {
18+
_token_data = token_data;
19+
_audio_data = audio_data;
20+
}
21+
return self;
22+
}
23+
24+
- (NSSet<NSString *> *)featureNames {
25+
return [NSSet setWithArray:@[@"token_data", @"audio_data"]];
26+
}
27+
28+
- (nullable MLFeatureValue *)featureValueForName:(NSString *)featureName {
29+
if ([featureName isEqualToString:@"token_data"]) {
30+
return [MLFeatureValue featureValueWithMultiArray:self.token_data];
31+
}
32+
if ([featureName isEqualToString:@"audio_data"]) {
33+
return [MLFeatureValue featureValueWithMultiArray:self.audio_data];
34+
}
35+
return nil;
36+
}
37+
38+
@end
39+
40+
@implementation whisper_decoder_implOutput
41+
42+
- (instancetype)initWithVar_1346:(MLMultiArray *)var_1346 {
43+
self = [super init];
44+
if (self) {
45+
_var_1346 = var_1346;
46+
}
47+
return self;
48+
}
49+
50+
- (NSSet<NSString *> *)featureNames {
51+
return [NSSet setWithArray:@[@"var_1346"]];
52+
}
53+
54+
- (nullable MLFeatureValue *)featureValueForName:(NSString *)featureName {
55+
if ([featureName isEqualToString:@"var_1346"]) {
56+
return [MLFeatureValue featureValueWithMultiArray:self.var_1346];
57+
}
58+
return nil;
59+
}
60+
61+
@end
62+
63+
@implementation whisper_decoder_impl
64+
65+
66+
/**
67+
URL of the underlying .mlmodelc directory.
68+
*/
69+
+ (nullable NSURL *)URLOfModelInThisBundle {
70+
NSString *assetPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"whisper_decoder_impl" ofType:@"mlmodelc"];
71+
if (nil == assetPath) { os_log_error(OS_LOG_DEFAULT, "Could not load whisper-decoder-impl.mlmodelc in the bundle resource"); return nil; }
72+
return [NSURL fileURLWithPath:assetPath];
73+
}
74+
75+
76+
/**
77+
Initialize whisper_decoder_impl instance from an existing MLModel object.
78+
79+
Usually the application does not use this initializer unless it makes a subclass of whisper_decoder_impl.
80+
Such application may want to use `-[MLModel initWithContentsOfURL:configuration:error:]` and `+URLOfModelInThisBundle` to create a MLModel object to pass-in.
81+
*/
82+
- (instancetype)initWithMLModel:(MLModel *)model {
83+
self = [super init];
84+
if (!self) { return nil; }
85+
_model = model;
86+
if (_model == nil) { return nil; }
87+
return self;
88+
}
89+
90+
91+
/**
92+
Initialize whisper_decoder_impl instance with the model in this bundle.
93+
*/
94+
- (nullable instancetype)init {
95+
return [self initWithContentsOfURL:(NSURL * _Nonnull)self.class.URLOfModelInThisBundle error:nil];
96+
}
97+
98+
99+
/**
100+
Initialize whisper_decoder_impl instance with the model in this bundle.
101+
102+
@param configuration The model configuration object
103+
@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.
104+
*/
105+
- (nullable instancetype)initWithConfiguration:(MLModelConfiguration *)configuration error:(NSError * _Nullable __autoreleasing * _Nullable)error {
106+
return [self initWithContentsOfURL:(NSURL * _Nonnull)self.class.URLOfModelInThisBundle configuration:configuration error:error];
107+
}
108+
109+
110+
/**
111+
Initialize whisper_decoder_impl instance from the model URL.
112+
113+
@param modelURL URL to the .mlmodelc directory for whisper_decoder_impl.
114+
@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.
115+
*/
116+
- (nullable instancetype)initWithContentsOfURL:(NSURL *)modelURL error:(NSError * _Nullable __autoreleasing * _Nullable)error {
117+
MLModel *model = [MLModel modelWithContentsOfURL:modelURL error:error];
118+
if (model == nil) { return nil; }
119+
return [self initWithMLModel:model];
120+
}
121+
122+
123+
/**
124+
Initialize whisper_decoder_impl instance from the model URL.
125+
126+
@param modelURL URL to the .mlmodelc directory for whisper_decoder_impl.
127+
@param configuration The model configuration object
128+
@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.
129+
*/
130+
- (nullable instancetype)initWithContentsOfURL:(NSURL *)modelURL configuration:(MLModelConfiguration *)configuration error:(NSError * _Nullable __autoreleasing * _Nullable)error {
131+
MLModel *model = [MLModel modelWithContentsOfURL:modelURL configuration:configuration error:error];
132+
if (model == nil) { return nil; }
133+
return [self initWithMLModel:model];
134+
}
135+
136+
137+
/**
138+
Construct whisper_decoder_impl instance asynchronously with configuration.
139+
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.
140+
141+
@param configuration The model configuration
142+
@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.
143+
*/
144+
+ (void)loadWithConfiguration:(MLModelConfiguration *)configuration completionHandler:(void (^)(whisper_decoder_impl * _Nullable model, NSError * _Nullable error))handler {
145+
[self loadContentsOfURL:(NSURL * _Nonnull)[self URLOfModelInThisBundle]
146+
configuration:configuration
147+
completionHandler:handler];
148+
}
149+
150+
151+
/**
152+
Construct whisper_decoder_impl instance asynchronously with URL of .mlmodelc directory and optional configuration.
153+
154+
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.
155+
156+
@param modelURL The model URL.
157+
@param configuration The model configuration
158+
@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.
159+
*/
160+
+ (void)loadContentsOfURL:(NSURL *)modelURL configuration:(MLModelConfiguration *)configuration completionHandler:(void (^)(whisper_decoder_impl * _Nullable model, NSError * _Nullable error))handler {
161+
[MLModel loadContentsOfURL:modelURL
162+
configuration:configuration
163+
completionHandler:^(MLModel *model, NSError *error) {
164+
if (model != nil) {
165+
whisper_decoder_impl *typedModel = [[whisper_decoder_impl alloc] initWithMLModel:model];
166+
handler(typedModel, nil);
167+
} else {
168+
handler(nil, error);
169+
}
170+
}];
171+
}
172+
173+
- (nullable whisper_decoder_implOutput *)predictionFromFeatures:(whisper_decoder_implInput *)input error:(NSError * _Nullable __autoreleasing * _Nullable)error {
174+
return [self predictionFromFeatures:input options:[[MLPredictionOptions alloc] init] error:error];
175+
}
176+
177+
- (nullable whisper_decoder_implOutput *)predictionFromFeatures:(whisper_decoder_implInput *)input options:(MLPredictionOptions *)options error:(NSError * _Nullable __autoreleasing * _Nullable)error {
178+
id<MLFeatureProvider> outFeatures = [self.model predictionFromFeatures:input options:options error:error];
179+
if (!outFeatures) { return nil; }
180+
return [[whisper_decoder_implOutput alloc] initWithVar_1346:(MLMultiArray *)[outFeatures featureValueForName:@"var_1346"].multiArrayValue];
181+
}
182+
183+
- (nullable whisper_decoder_implOutput *)predictionFromToken_data:(MLMultiArray *)token_data audio_data:(MLMultiArray *)audio_data error:(NSError * _Nullable __autoreleasing * _Nullable)error {
184+
whisper_decoder_implInput *input_ = [[whisper_decoder_implInput alloc] initWithToken_data:token_data audio_data:audio_data];
185+
return [self predictionFromFeatures:input_ error:error];
186+
}
187+
188+
- (nullable NSArray<whisper_decoder_implOutput *> *)predictionsFromInputs:(NSArray<whisper_decoder_implInput*> *)inputArray options:(MLPredictionOptions *)options error:(NSError * _Nullable __autoreleasing * _Nullable)error {
189+
id<MLBatchProvider> inBatch = [[MLArrayBatchProvider alloc] initWithFeatureProviderArray:inputArray];
190+
id<MLBatchProvider> outBatch = [self.model predictionsFromBatch:inBatch options:options error:error];
191+
if (!outBatch) { return nil; }
192+
NSMutableArray<whisper_decoder_implOutput*> *results = [NSMutableArray arrayWithCapacity:(NSUInteger)outBatch.count];
193+
for (NSInteger i = 0; i < outBatch.count; i++) {
194+
id<MLFeatureProvider> resultProvider = [outBatch featuresAtIndex:i];
195+
whisper_decoder_implOutput * result = [[whisper_decoder_implOutput alloc] initWithVar_1346:(MLMultiArray *)[resultProvider featureValueForName:@"var_1346"].multiArrayValue];
196+
[results addObject:result];
197+
}
198+
return results;
199+
}
200+
201+
@end

0 commit comments

Comments
 (0)