jhenhong ggerganov commited on
Commit
b60f765
·
unverified ·
1 Parent(s): 8aa7146

whisper.objc : enable Core ML in example & fix segmentation fault (#910)

Browse files

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

.github/workflows/build.yml CHANGED
@@ -333,7 +333,9 @@ jobs:
333
  uses: actions/checkout@v1
334
 
335
  - name: Configure
336
- run: cp models/for-tests-ggml-base.en.bin models/ggml-base.en.bin
 
 
337
 
338
  - name: Build objc example
339
  run: xcodebuild -project examples/whisper.objc/whisper.objc.xcodeproj -scheme whisper.objc -configuration ${{ matrix.build }} -sdk iphonesimulator build
 
333
  uses: actions/checkout@v1
334
 
335
  - name: Configure
336
+ run: |
337
+ cp models/for-tests-ggml-base.en.bin models/ggml-base.en.bin
338
+ mkdir models/ggml-base.en-encoder.mlmodelc
339
 
340
  - name: Build objc example
341
  run: xcodebuild -project examples/whisper.objc/whisper.objc.xcodeproj -scheme whisper.objc -configuration ${{ matrix.build }} -sdk iphonesimulator build
Makefile CHANGED
@@ -240,7 +240,7 @@ ifndef WHISPER_COREML
240
  WHISPER_OBJ += whisper.o
241
  else
242
  whisper-encoder.o: coreml/whisper-encoder.mm coreml/whisper-encoder.h
243
- $(CXX) -O3 -I . -c coreml/whisper-encoder.mm -o whisper-encoder.o
244
 
245
  whisper-encoder-impl.o: coreml/whisper-encoder-impl.m coreml/whisper-encoder-impl.h
246
  $(CXX) -O3 -I . -fobjc-arc -c coreml/whisper-encoder-impl.m -o whisper-encoder-impl.o
 
240
  WHISPER_OBJ += whisper.o
241
  else
242
  whisper-encoder.o: coreml/whisper-encoder.mm coreml/whisper-encoder.h
243
+ $(CXX) -O3 -I . -fobjc-arc -c coreml/whisper-encoder.mm -o whisper-encoder.o
244
 
245
  whisper-encoder-impl.o: coreml/whisper-encoder-impl.m coreml/whisper-encoder-impl.h
246
  $(CXX) -O3 -I . -fobjc-arc -c coreml/whisper-encoder-impl.m -o whisper-encoder-impl.o
coreml/whisper-encoder.mm CHANGED
@@ -1,5 +1,9 @@
1
- #import "coreml/whisper-encoder.h"
2
- #import "coreml/whisper-encoder-impl.h"
 
 
 
 
3
 
4
  #import <CoreML/CoreML.h>
5
 
@@ -52,8 +56,6 @@ void whisper_coreml_encode(
52
  whisper_encoder_implOutput * outCoreML = [(__bridge id) ctx->data predictionFromLogmel_data:inMultiArray error:nil];
53
 
54
  memcpy(out, outCoreML.output.dataPointer, outCoreML.output.count * sizeof(float));
55
-
56
- [inMultiArray release];
57
  }
58
 
59
  #if __cplusplus
 
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"
7
 
8
  #import <CoreML/CoreML.h>
9
 
 
56
  whisper_encoder_implOutput * outCoreML = [(__bridge id) ctx->data predictionFromLogmel_data:inMultiArray error:nil];
57
 
58
  memcpy(out, outCoreML.output.dataPointer, outCoreML.output.count * sizeof(float));
 
 
59
  }
60
 
61
  #if __cplusplus
examples/whisper.objc/README.md CHANGED
@@ -14,15 +14,24 @@ https://user-images.githubusercontent.com/1991296/204126266-ce4177c6-6eca-4bd9-b
14
  ```java
15
  git clone https://github.com/ggerganov/whisper.cpp
16
  open whisper.cpp/examples/whisper.objc/whisper.objc.xcodeproj/
 
 
 
17
  ```
18
 
19
  Make sure to build the project in `Release`:
20
 
21
  <img width="947" alt="image" src="https://user-images.githubusercontent.com/1991296/197382607-9e1e6d1b-79fa-496f-9d16-b71dc1535701.png">
22
 
23
- Also, don't forget to add the `-DGGML_USE_ACCELERATE` compiler flag in Build Phases.
24
  This can significantly improve the performance of the transcription:
25
 
26
  <img width="1072" alt="image" src="https://user-images.githubusercontent.com/1991296/208511239-8d7cdbd1-aa48-41b5-becd-ca288d53cc07.png">
27
 
 
 
 
 
 
 
28
  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.
 
14
  ```java
15
  git clone https://github.com/ggerganov/whisper.cpp
16
  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
20
  ```
21
 
22
  Make sure to build the project in `Release`:
23
 
24
  <img width="947" alt="image" src="https://user-images.githubusercontent.com/1991296/197382607-9e1e6d1b-79fa-496f-9d16-b71dc1535701.png">
25
 
26
+ Also, don't forget to add the `-DGGML_USE_ACCELERATE` compiler flag for `ggml.c` in Build Phases.
27
  This can significantly improve the performance of the transcription:
28
 
29
  <img width="1072" alt="image" src="https://user-images.githubusercontent.com/1991296/208511239-8d7cdbd1-aa48-41b5-becd-ca288d53cc07.png">
30
 
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
+
37
  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 CHANGED
@@ -14,9 +14,13 @@
14
  18627C8629052BE000BD2A04 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 18627C8529052BE000BD2A04 /* Assets.xcassets */; };
15
  18627C8929052BE000BD2A04 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 18627C8729052BE000BD2A04 /* LaunchScreen.storyboard */; };
16
  18627C8C29052BE000BD2A04 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 18627C8B29052BE000BD2A04 /* main.m */; };
17
- 18627C9429052C4900BD2A04 /* whisper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 18627C9329052C4900BD2A04 /* whisper.cpp */; };
18
  18627C9629052C5800BD2A04 /* ggml.c in Sources */ = {isa = PBXBuildFile; fileRef = 18627C9529052C5800BD2A04 /* ggml.c */; settings = {COMPILER_FLAGS = "-DGGML_USE_ACCELERATE"; }; };
19
  18627C9B29052CFF00BD2A04 /* ggml-base.en.bin in Resources */ = {isa = PBXBuildFile; fileRef = 18627C9A29052CFF00BD2A04 /* ggml-base.en.bin */; };
 
 
 
 
20
  /* End PBXBuildFile section */
21
 
22
  /* Begin PBXFileReference section */
@@ -37,6 +41,13 @@
37
  18627C9529052C5800BD2A04 /* ggml.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ggml.c; path = ../../../ggml.c; sourceTree = "<group>"; };
38
  18627C9729052C6600BD2A04 /* ggml.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ggml.h; path = ../../../ggml.h; sourceTree = "<group>"; };
39
  18627C9A29052CFF00BD2A04 /* ggml-base.en.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = "ggml-base.en.bin"; path = "../../../models/ggml-base.en.bin"; sourceTree = "<group>"; };
 
 
 
 
 
 
 
40
  /* End PBXFileReference section */
41
 
42
  /* Begin PBXFrameworksBuildPhase section */
@@ -69,6 +80,8 @@
69
  18627C7829052BDF00BD2A04 /* whisper.objc */ = {
70
  isa = PBXGroup;
71
  children = (
 
 
72
  18627C9A29052CFF00BD2A04 /* ggml-base.en.bin */,
73
  18627C9729052C6600BD2A04 /* ggml.h */,
74
  18627C9529052C5800BD2A04 /* ggml.c */,
@@ -89,6 +102,20 @@
89
  path = whisper.objc;
90
  sourceTree = "<group>";
91
  };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  /* End PBXGroup section */
93
 
94
  /* Begin PBXNativeTarget section */
@@ -147,6 +174,7 @@
147
  buildActionMask = 2147483647;
148
  files = (
149
  18627C8929052BE000BD2A04 /* LaunchScreen.storyboard in Resources */,
 
150
  18627C8629052BE000BD2A04 /* Assets.xcassets in Resources */,
151
  18627C8429052BDF00BD2A04 /* Main.storyboard in Resources */,
152
  18627C9B29052CFF00BD2A04 /* ggml-base.en.bin in Resources */,
@@ -161,11 +189,14 @@
161
  buildActionMask = 2147483647;
162
  files = (
163
  18627C8129052BDF00BD2A04 /* ViewController.m in Sources */,
 
164
  18627C9429052C4900BD2A04 /* whisper.cpp in Sources */,
165
  18627C9629052C5800BD2A04 /* ggml.c in Sources */,
166
  18627C7B29052BDF00BD2A04 /* AppDelegate.m in Sources */,
 
167
  18627C8C29052BE000BD2A04 /* main.m in Sources */,
168
  18627C7E29052BDF00BD2A04 /* SceneDelegate.m in Sources */,
 
169
  );
170
  runOnlyForDeploymentPostprocessing = 0;
171
  };
 
14
  18627C8629052BE000BD2A04 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 18627C8529052BE000BD2A04 /* Assets.xcassets */; };
15
  18627C8929052BE000BD2A04 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 18627C8729052BE000BD2A04 /* LaunchScreen.storyboard */; };
16
  18627C8C29052BE000BD2A04 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 18627C8B29052BE000BD2A04 /* main.m */; };
17
+ 18627C9429052C4900BD2A04 /* whisper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 18627C9329052C4900BD2A04 /* whisper.cpp */; settings = {COMPILER_FLAGS = "-DWHISPER_USE_COREML -DWHISPER_COREML_ALLOW_FALLBACK"; }; };
18
  18627C9629052C5800BD2A04 /* ggml.c in Sources */ = {isa = PBXBuildFile; fileRef = 18627C9529052C5800BD2A04 /* ggml.c */; settings = {COMPILER_FLAGS = "-DGGML_USE_ACCELERATE"; }; };
19
  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 */; };
24
  /* End PBXBuildFile section */
25
 
26
  /* Begin PBXFileReference section */
 
41
  18627C9529052C5800BD2A04 /* ggml.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ggml.c; path = ../../../ggml.c; sourceTree = "<group>"; };
42
  18627C9729052C6600BD2A04 /* ggml.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ggml.h; path = ../../../ggml.h; sourceTree = "<group>"; };
43
  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>"; };
51
  /* End PBXFileReference section */
52
 
53
  /* Begin PBXFrameworksBuildPhase section */
 
80
  18627C7829052BDF00BD2A04 /* whisper.objc */ = {
81
  isa = PBXGroup;
82
  children = (
83
+ 7FE3424E2A0C418A0015A058 /* ggml-base.en-encoder.mlmodelc */,
84
+ 7FE342442A0C3FA20015A058 /* coreml */,
85
  18627C9A29052CFF00BD2A04 /* ggml-base.en.bin */,
86
  18627C9729052C6600BD2A04 /* ggml.h */,
87
  18627C9529052C5800BD2A04 /* ggml.c */,
 
102
  path = whisper.objc;
103
  sourceTree = "<group>";
104
  };
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
+ };
119
  /* End PBXGroup section */
120
 
121
  /* Begin PBXNativeTarget section */
 
174
  buildActionMask = 2147483647;
175
  files = (
176
  18627C8929052BE000BD2A04 /* LaunchScreen.storyboard in Resources */,
177
+ 7FE3424F2A0C418A0015A058 /* ggml-base.en-encoder.mlmodelc in Resources */,
178
  18627C8629052BE000BD2A04 /* Assets.xcassets in Resources */,
179
  18627C8429052BDF00BD2A04 /* Main.storyboard in Resources */,
180
  18627C9B29052CFF00BD2A04 /* ggml-base.en.bin in Resources */,
 
189
  buildActionMask = 2147483647;
190
  files = (
191
  18627C8129052BDF00BD2A04 /* ViewController.m in Sources */,
192
+ 7FE3424C2A0C3FA20015A058 /* whisper-encoder.mm in Sources */,
193
  18627C9429052C4900BD2A04 /* whisper.cpp in Sources */,
194
  18627C9629052C5800BD2A04 /* ggml.c in Sources */,
195
  18627C7B29052BDF00BD2A04 /* AppDelegate.m in Sources */,
196
+ 7FE3424D2A0C3FA20015A058 /* whisper-decoder-impl.m in Sources */,
197
  18627C8C29052BE000BD2A04 /* main.m in Sources */,
198
  18627C7E29052BDF00BD2A04 /* SceneDelegate.m in Sources */,
199
+ 7FE3424B2A0C3FA20015A058 /* whisper-encoder-impl.m in Sources */,
200
  );
201
  runOnlyForDeploymentPostprocessing = 0;
202
  };