Skip to content

Commit 5300117

Browse files
jhen0409ggerganov
andauthored
whisper.objc : enable Core ML in example & fix segmentation fault (#910)
* coreml : update endcoder header import path * coreml : force objc_arc in whisper-encoder.mm * whisper.objc : create coreml/ group link * whisper.objc : add coreml model link * whisper.objc : update readme * coreml : use -fobjc-arc for coreml/whisper-encoder.mm * ci: create dummy .mlmodelc for pass ios build * whisper.objc : update readme --------- Co-authored-by: Georgi Gerganov <[email protected]>
1 parent 70af52a commit 5300117

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,9 @@ jobs:
333333
uses: actions/checkout@v1
334334

335335
- name: Configure
336-
run: cp models/for-tests-ggml-base.en.bin models/ggml-base.en.bin
336+
run: |
337+
cp models/for-tests-ggml-base.en.bin models/ggml-base.en.bin
338+
mkdir models/ggml-base.en-encoder.mlmodelc
337339
338340
- name: Build objc example
339341
run: xcodebuild -project examples/whisper.objc/whisper.objc.xcodeproj -scheme whisper.objc -configuration ${{ matrix.build }} -sdk iphonesimulator build

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ ifndef WHISPER_COREML
240240
WHISPER_OBJ += whisper.o
241241
else
242242
whisper-encoder.o: coreml/whisper-encoder.mm coreml/whisper-encoder.h
243-
$(CXX) -O3 -I . -c coreml/whisper-encoder.mm -o whisper-encoder.o
243+
$(CXX) -O3 -I . -fobjc-arc -c coreml/whisper-encoder.mm -o whisper-encoder.o
244244

245245
whisper-encoder-impl.o: coreml/whisper-encoder-impl.m coreml/whisper-encoder-impl.h
246246
$(CXX) -O3 -I . -fobjc-arc -c coreml/whisper-encoder-impl.m -o whisper-encoder-impl.o

coreml/whisper-encoder.mm

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
#import "coreml/whisper-encoder.h"
2-
#import "coreml/whisper-encoder-impl.h"
1+
#if !__has_feature(objc_arc)
2+
#error This file must be compiled with automatic reference counting enabled (-fobjc-arc)
3+
#endif
4+
5+
#import "whisper-encoder.h"
6+
#import "whisper-encoder-impl.h"
37

48
#import <CoreML/CoreML.h>
59

@@ -52,8 +56,6 @@ void whisper_coreml_encode(
5256
whisper_encoder_implOutput * outCoreML = [(__bridge id) ctx->data predictionFromLogmel_data:inMultiArray error:nil];
5357

5458
memcpy(out, outCoreML.output.dataPointer, outCoreML.output.count * sizeof(float));
55-
56-
[inMultiArray release];
5759
}
5860

5961
#if __cplusplus

examples/whisper.objc/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,24 @@ https://user-images.githubusercontent.com/1991296/204126266-ce4177c6-6eca-4bd9-b
1414
```java
1515
git clone https://github.com/ggerganov/whisper.cpp
1616
open whisper.cpp/examples/whisper.objc/whisper.objc.xcodeproj/
17+
18+
// If you don't want to convert a Core ML model, you can skip this step by create dummy model
19+
mkdir models/ggml-base.en-encoder.mlmodelc
1720
```
1821

1922
Make sure to build the project in `Release`:
2023

2124
<img width="947" alt="image" src="https://user-images.githubusercontent.com/1991296/197382607-9e1e6d1b-79fa-496f-9d16-b71dc1535701.png">
2225

23-
Also, don't forget to add the `-DGGML_USE_ACCELERATE` compiler flag in Build Phases.
26+
Also, don't forget to add the `-DGGML_USE_ACCELERATE` compiler flag for `ggml.c` in Build Phases.
2427
This can significantly improve the performance of the transcription:
2528

2629
<img width="1072" alt="image" src="https://user-images.githubusercontent.com/1991296/208511239-8d7cdbd1-aa48-41b5-becd-ca288d53cc07.png">
2730

31+
If you want to enable Core ML support, you can add the `-DWHISPER_USE_COREML -DWHISPER_COREML_ALLOW_FALLBACK` compiler flag for `whisper.cpp` in Build Phases:
32+
33+
<img width="1072" alt="image" src="https://github.com/ggerganov/whisper.cpp/assets/3001525/103e8f57-6eb6-490d-a60c-f6cf6c319324">
34+
35+
Then follow the [`Core ML support` section of readme](../../README.md#core-ml-support) for convert the model.
36+
2837
In this project, it also added `-O3 -DNDEBUG` to `Other C Flags`, but adding flags to app proj is not ideal in real world (applies to all C/C++ files), consider splitting xcodeproj in workspace in your own project.

examples/whisper.objc/whisper.objc.xcodeproj/project.pbxproj

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
18627C8629052BE000BD2A04 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 18627C8529052BE000BD2A04 /* Assets.xcassets */; };
1515
18627C8929052BE000BD2A04 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 18627C8729052BE000BD2A04 /* LaunchScreen.storyboard */; };
1616
18627C8C29052BE000BD2A04 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 18627C8B29052BE000BD2A04 /* main.m */; };
17-
18627C9429052C4900BD2A04 /* whisper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 18627C9329052C4900BD2A04 /* whisper.cpp */; };
17+
18627C9429052C4900BD2A04 /* whisper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 18627C9329052C4900BD2A04 /* whisper.cpp */; settings = {COMPILER_FLAGS = "-DWHISPER_USE_COREML -DWHISPER_COREML_ALLOW_FALLBACK"; }; };
1818
18627C9629052C5800BD2A04 /* ggml.c in Sources */ = {isa = PBXBuildFile; fileRef = 18627C9529052C5800BD2A04 /* ggml.c */; settings = {COMPILER_FLAGS = "-DGGML_USE_ACCELERATE"; }; };
1919
18627C9B29052CFF00BD2A04 /* ggml-base.en.bin in Resources */ = {isa = PBXBuildFile; fileRef = 18627C9A29052CFF00BD2A04 /* ggml-base.en.bin */; };
20+
7FE3424B2A0C3FA20015A058 /* whisper-encoder-impl.m in Sources */ = {isa = PBXBuildFile; fileRef = 7FE342452A0C3FA20015A058 /* whisper-encoder-impl.m */; };
21+
7FE3424C2A0C3FA20015A058 /* whisper-encoder.mm in Sources */ = {isa = PBXBuildFile; fileRef = 7FE342472A0C3FA20015A058 /* whisper-encoder.mm */; };
22+
7FE3424D2A0C3FA20015A058 /* whisper-decoder-impl.m in Sources */ = {isa = PBXBuildFile; fileRef = 7FE3424A2A0C3FA20015A058 /* whisper-decoder-impl.m */; };
23+
7FE3424F2A0C418A0015A058 /* ggml-base.en-encoder.mlmodelc in Resources */ = {isa = PBXBuildFile; fileRef = 7FE3424E2A0C418A0015A058 /* ggml-base.en-encoder.mlmodelc */; };
2024
/* End PBXBuildFile section */
2125

2226
/* Begin PBXFileReference section */
@@ -37,6 +41,13 @@
3741
18627C9529052C5800BD2A04 /* ggml.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ggml.c; path = ../../../ggml.c; sourceTree = "<group>"; };
3842
18627C9729052C6600BD2A04 /* ggml.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ggml.h; path = ../../../ggml.h; sourceTree = "<group>"; };
3943
18627C9A29052CFF00BD2A04 /* ggml-base.en.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = "ggml-base.en.bin"; path = "../../../models/ggml-base.en.bin"; sourceTree = "<group>"; };
44+
7FE342452A0C3FA20015A058 /* whisper-encoder-impl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "whisper-encoder-impl.m"; sourceTree = "<group>"; };
45+
7FE342462A0C3FA20015A058 /* whisper-encoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "whisper-encoder.h"; sourceTree = "<group>"; };
46+
7FE342472A0C3FA20015A058 /* whisper-encoder.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "whisper-encoder.mm"; sourceTree = "<group>"; };
47+
7FE342482A0C3FA20015A058 /* whisper-decoder-impl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "whisper-decoder-impl.h"; sourceTree = "<group>"; };
48+
7FE342492A0C3FA20015A058 /* whisper-encoder-impl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "whisper-encoder-impl.h"; sourceTree = "<group>"; };
49+
7FE3424A2A0C3FA20015A058 /* whisper-decoder-impl.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "whisper-decoder-impl.m"; sourceTree = "<group>"; };
50+
7FE3424E2A0C418A0015A058 /* ggml-base.en-encoder.mlmodelc */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = "ggml-base.en-encoder.mlmodelc"; path = "../../../models/ggml-base.en-encoder.mlmodelc"; sourceTree = "<group>"; };
4051
/* End PBXFileReference section */
4152

4253
/* Begin PBXFrameworksBuildPhase section */
@@ -69,6 +80,8 @@
6980
18627C7829052BDF00BD2A04 /* whisper.objc */ = {
7081
isa = PBXGroup;
7182
children = (
83+
7FE3424E2A0C418A0015A058 /* ggml-base.en-encoder.mlmodelc */,
84+
7FE342442A0C3FA20015A058 /* coreml */,
7285
18627C9A29052CFF00BD2A04 /* ggml-base.en.bin */,
7386
18627C9729052C6600BD2A04 /* ggml.h */,
7487
18627C9529052C5800BD2A04 /* ggml.c */,
@@ -89,6 +102,20 @@
89102
path = whisper.objc;
90103
sourceTree = "<group>";
91104
};
105+
7FE342442A0C3FA20015A058 /* coreml */ = {
106+
isa = PBXGroup;
107+
children = (
108+
7FE342452A0C3FA20015A058 /* whisper-encoder-impl.m */,
109+
7FE342462A0C3FA20015A058 /* whisper-encoder.h */,
110+
7FE342472A0C3FA20015A058 /* whisper-encoder.mm */,
111+
7FE342482A0C3FA20015A058 /* whisper-decoder-impl.h */,
112+
7FE342492A0C3FA20015A058 /* whisper-encoder-impl.h */,
113+
7FE3424A2A0C3FA20015A058 /* whisper-decoder-impl.m */,
114+
);
115+
name = coreml;
116+
path = ../../../coreml;
117+
sourceTree = "<group>";
118+
};
92119
/* End PBXGroup section */
93120

94121
/* Begin PBXNativeTarget section */
@@ -147,6 +174,7 @@
147174
buildActionMask = 2147483647;
148175
files = (
149176
18627C8929052BE000BD2A04 /* LaunchScreen.storyboard in Resources */,
177+
7FE3424F2A0C418A0015A058 /* ggml-base.en-encoder.mlmodelc in Resources */,
150178
18627C8629052BE000BD2A04 /* Assets.xcassets in Resources */,
151179
18627C8429052BDF00BD2A04 /* Main.storyboard in Resources */,
152180
18627C9B29052CFF00BD2A04 /* ggml-base.en.bin in Resources */,
@@ -161,11 +189,14 @@
161189
buildActionMask = 2147483647;
162190
files = (
163191
18627C8129052BDF00BD2A04 /* ViewController.m in Sources */,
192+
7FE3424C2A0C3FA20015A058 /* whisper-encoder.mm in Sources */,
164193
18627C9429052C4900BD2A04 /* whisper.cpp in Sources */,
165194
18627C9629052C5800BD2A04 /* ggml.c in Sources */,
166195
18627C7B29052BDF00BD2A04 /* AppDelegate.m in Sources */,
196+
7FE3424D2A0C3FA20015A058 /* whisper-decoder-impl.m in Sources */,
167197
18627C8C29052BE000BD2A04 /* main.m in Sources */,
168198
18627C7E29052BDF00BD2A04 /* SceneDelegate.m in Sources */,
199+
7FE3424B2A0C3FA20015A058 /* whisper-encoder-impl.m in Sources */,
169200
);
170201
runOnlyForDeploymentPostprocessing = 0;
171202
};

0 commit comments

Comments
 (0)