From 455e0789573dfe162f649d50c925c36e07711842 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Thu, 20 Mar 2025 15:28:31 +0100 Subject: [PATCH 1/2] 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 ``` --- build-xcframework.sh | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/build-xcframework.sh b/build-xcframework.sh index c4b71a88ca2..35eecaa267b 100755 --- a/build-xcframework.sh +++ b/build-xcframework.sh @@ -108,7 +108,7 @@ setup_framework_structure() { fi # Copy all required headers (common for all platforms) - cp include/whisper.h ${header_path} + cp include/whisper.h ${header_path} cp ggml/include/ggml.h ${header_path} cp ggml/include/ggml-alloc.h ${header_path} cp ggml/include/ggml-backend.h ${header_path} @@ -245,9 +245,16 @@ combine_static_libraries() { "${base_dir}/${build_dir}/ggml/src/ggml-metal/${release_dir}/libggml-metal.a" "${base_dir}/${build_dir}/ggml/src/ggml-blas/${release_dir}/libggml-blas.a" ) + if [[ "$platform" == "macos" || "$platform" == "ios" ]]; then + echo "Adding libwhisper.coreml library the build." + libs+=( + "${base_dir}/${build_dir}/src/${release_dir}/libwhisper.coreml.a" + ) + fi # Create temporary directory for processing local temp_dir="${base_dir}/${build_dir}/temp" + echo "Creating temporary directory: ${temp_dir}" mkdir -p "${temp_dir}" # Since we have multiple architectures libtool will find object files that do not @@ -259,6 +266,7 @@ combine_static_libraries() { local archs="" local min_version_flag="" local install_name="" + local frameworks="-framework Foundation -framework Metal -framework Accelerate" case "$platform" in "ios") @@ -272,12 +280,14 @@ combine_static_libraries() { min_version_flag="-mios-version-min=${IOS_MIN_OS_VERSION}" fi install_name="@rpath/whisper.framework/whisper" + frameworks+=" -framework CoreML" ;; "macos") sdk="macosx" archs="arm64 x86_64" min_version_flag="-mmacosx-version-min=${MACOS_MIN_OS_VERSION}" install_name="@rpath/whisper.framework/Versions/Current/whisper" + frameworks+=" -framework CoreML" ;; "visionos") if [[ "$is_simulator" == "true" ]]; then @@ -319,7 +329,7 @@ combine_static_libraries() { $arch_flags \ $min_version_flag \ -Wl,-force_load,"${temp_dir}/combined.a" \ - -framework Foundation -framework Metal -framework Accelerate \ + $frameworks \ -install_name "$install_name" \ -o "${base_dir}/${output_lib}" @@ -399,6 +409,8 @@ cmake -B build-ios-sim -G Xcode \ -DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphonesimulator \ -DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \ -DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \ + -DWHISPER_COREML="ON" \ + -DWHISPER_COREML_ALLOW_FALLBACK="ON" \ -S . cmake --build build-ios-sim --config Release -- -quiet @@ -411,6 +423,8 @@ cmake -B build-ios-device -G Xcode \ -DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphoneos \ -DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \ -DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \ + -DWHISPER_COREML="ON" \ + -DWHISPER_COREML_ALLOW_FALLBACK="ON" \ -S . cmake --build build-ios-device --config Release -- -quiet @@ -421,6 +435,8 @@ cmake -B build-macos -G Xcode \ -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \ -DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \ -DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \ + -DWHISPER_COREML="ON" \ + -DWHISPER_COREML_ALLOW_FALLBACK="ON" \ -S . cmake --build build-macos --config Release -- -quiet From 21b65a53f1c04fd3bed61c8e9745e125c6067da9 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Thu, 20 Mar 2025 16:38:01 +0100 Subject: [PATCH 2/2] squash! xcframework : add support for CoreML to ios/macOS Fix grammar in output message. --- build-xcframework.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build-xcframework.sh b/build-xcframework.sh index 35eecaa267b..95644b6df7b 100755 --- a/build-xcframework.sh +++ b/build-xcframework.sh @@ -246,7 +246,7 @@ combine_static_libraries() { "${base_dir}/${build_dir}/ggml/src/ggml-blas/${release_dir}/libggml-blas.a" ) if [[ "$platform" == "macos" || "$platform" == "ios" ]]; then - echo "Adding libwhisper.coreml library the build." + echo "Adding libwhisper.coreml library to the build." libs+=( "${base_dir}/${build_dir}/src/${release_dir}/libwhisper.coreml.a" )