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