Skip to content

Commit ca75449

Browse files
authored
xcframework : add support for CoreML to ios/macOS (#2912)
* xcframework : add support for CoreML to ios/macOS This commit add support for compiling whisper with CoreML support for iOS and macOS. The motivation for this change is it will allow users to use a Core ML model or fall back to a ggml model if Core ML is not available. With the updated xcframework, I was able to run the whisper.objc example and successfully load a Core ML model: ```console whisper_init_state: loading Core ML model from '/Users/danbev/Library/Developer/CoreSimulator/Devices/25E8C27D-0253-4281-AF17-C3F2A4D1D8F4/data/Containers/Bundle/Application/B81F6FF0-BF1A-40DF-AC2A-3908EC4BCC9A/whisper.objc.app/ggml-base.en-encoder.mlmodelc' whisper_init_state: first run on a device may take a while ... whisper_init_state: Core ML model loaded ``` * squash! xcframework : add support for CoreML to ios/macOS Fix grammar in output message.
1 parent 80dad86 commit ca75449

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

build-xcframework.sh

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ setup_framework_structure() {
108108
fi
109109

110110
# Copy all required headers (common for all platforms)
111-
cp include/whisper.h ${header_path}
111+
cp include/whisper.h ${header_path}
112112
cp ggml/include/ggml.h ${header_path}
113113
cp ggml/include/ggml-alloc.h ${header_path}
114114
cp ggml/include/ggml-backend.h ${header_path}
@@ -245,9 +245,16 @@ combine_static_libraries() {
245245
"${base_dir}/${build_dir}/ggml/src/ggml-metal/${release_dir}/libggml-metal.a"
246246
"${base_dir}/${build_dir}/ggml/src/ggml-blas/${release_dir}/libggml-blas.a"
247247
)
248+
if [[ "$platform" == "macos" || "$platform" == "ios" ]]; then
249+
echo "Adding libwhisper.coreml library to the build."
250+
libs+=(
251+
"${base_dir}/${build_dir}/src/${release_dir}/libwhisper.coreml.a"
252+
)
253+
fi
248254

249255
# Create temporary directory for processing
250256
local temp_dir="${base_dir}/${build_dir}/temp"
257+
echo "Creating temporary directory: ${temp_dir}"
251258
mkdir -p "${temp_dir}"
252259

253260
# Since we have multiple architectures libtool will find object files that do not
@@ -259,6 +266,7 @@ combine_static_libraries() {
259266
local archs=""
260267
local min_version_flag=""
261268
local install_name=""
269+
local frameworks="-framework Foundation -framework Metal -framework Accelerate"
262270

263271
case "$platform" in
264272
"ios")
@@ -272,12 +280,14 @@ combine_static_libraries() {
272280
min_version_flag="-mios-version-min=${IOS_MIN_OS_VERSION}"
273281
fi
274282
install_name="@rpath/whisper.framework/whisper"
283+
frameworks+=" -framework CoreML"
275284
;;
276285
"macos")
277286
sdk="macosx"
278287
archs="arm64 x86_64"
279288
min_version_flag="-mmacosx-version-min=${MACOS_MIN_OS_VERSION}"
280289
install_name="@rpath/whisper.framework/Versions/Current/whisper"
290+
frameworks+=" -framework CoreML"
281291
;;
282292
"visionos")
283293
if [[ "$is_simulator" == "true" ]]; then
@@ -319,7 +329,7 @@ combine_static_libraries() {
319329
$arch_flags \
320330
$min_version_flag \
321331
-Wl,-force_load,"${temp_dir}/combined.a" \
322-
-framework Foundation -framework Metal -framework Accelerate \
332+
$frameworks \
323333
-install_name "$install_name" \
324334
-o "${base_dir}/${output_lib}"
325335

@@ -399,6 +409,8 @@ cmake -B build-ios-sim -G Xcode \
399409
-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphonesimulator \
400410
-DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \
401411
-DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \
412+
-DWHISPER_COREML="ON" \
413+
-DWHISPER_COREML_ALLOW_FALLBACK="ON" \
402414
-S .
403415
cmake --build build-ios-sim --config Release -- -quiet
404416

@@ -411,6 +423,8 @@ cmake -B build-ios-device -G Xcode \
411423
-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphoneos \
412424
-DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \
413425
-DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \
426+
-DWHISPER_COREML="ON" \
427+
-DWHISPER_COREML_ALLOW_FALLBACK="ON" \
414428
-S .
415429
cmake --build build-ios-device --config Release -- -quiet
416430

@@ -421,6 +435,8 @@ cmake -B build-macos -G Xcode \
421435
-DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
422436
-DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \
423437
-DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \
438+
-DWHISPER_COREML="ON" \
439+
-DWHISPER_COREML_ALLOW_FALLBACK="ON" \
424440
-S .
425441
cmake --build build-macos --config Release -- -quiet
426442

0 commit comments

Comments
 (0)