Spaces:
Running
xcframework : add support for CoreML to ios/macOS (#2912)
Browse files* 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.
- build-xcframework.sh +18 -2
|
@@ -108,7 +108,7 @@ setup_framework_structure() {
|
|
| 108 |
fi
|
| 109 |
|
| 110 |
# Copy all required headers (common for all platforms)
|
| 111 |
-
cp include/whisper.h
|
| 112 |
cp ggml/include/ggml.h ${header_path}
|
| 113 |
cp ggml/include/ggml-alloc.h ${header_path}
|
| 114 |
cp ggml/include/ggml-backend.h ${header_path}
|
|
@@ -245,9 +245,16 @@ combine_static_libraries() {
|
|
| 245 |
"${base_dir}/${build_dir}/ggml/src/ggml-metal/${release_dir}/libggml-metal.a"
|
| 246 |
"${base_dir}/${build_dir}/ggml/src/ggml-blas/${release_dir}/libggml-blas.a"
|
| 247 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
# Create temporary directory for processing
|
| 250 |
local temp_dir="${base_dir}/${build_dir}/temp"
|
|
|
|
| 251 |
mkdir -p "${temp_dir}"
|
| 252 |
|
| 253 |
# Since we have multiple architectures libtool will find object files that do not
|
|
@@ -259,6 +266,7 @@ combine_static_libraries() {
|
|
| 259 |
local archs=""
|
| 260 |
local min_version_flag=""
|
| 261 |
local install_name=""
|
|
|
|
| 262 |
|
| 263 |
case "$platform" in
|
| 264 |
"ios")
|
|
@@ -272,12 +280,14 @@ combine_static_libraries() {
|
|
| 272 |
min_version_flag="-mios-version-min=${IOS_MIN_OS_VERSION}"
|
| 273 |
fi
|
| 274 |
install_name="@rpath/whisper.framework/whisper"
|
|
|
|
| 275 |
;;
|
| 276 |
"macos")
|
| 277 |
sdk="macosx"
|
| 278 |
archs="arm64 x86_64"
|
| 279 |
min_version_flag="-mmacosx-version-min=${MACOS_MIN_OS_VERSION}"
|
| 280 |
install_name="@rpath/whisper.framework/Versions/Current/whisper"
|
|
|
|
| 281 |
;;
|
| 282 |
"visionos")
|
| 283 |
if [[ "$is_simulator" == "true" ]]; then
|
|
@@ -319,7 +329,7 @@ combine_static_libraries() {
|
|
| 319 |
$arch_flags \
|
| 320 |
$min_version_flag \
|
| 321 |
-Wl,-force_load,"${temp_dir}/combined.a" \
|
| 322 |
-
|
| 323 |
-install_name "$install_name" \
|
| 324 |
-o "${base_dir}/${output_lib}"
|
| 325 |
|
|
@@ -399,6 +409,8 @@ cmake -B build-ios-sim -G Xcode \
|
|
| 399 |
-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphonesimulator \
|
| 400 |
-DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \
|
| 401 |
-DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \
|
|
|
|
|
|
|
| 402 |
-S .
|
| 403 |
cmake --build build-ios-sim --config Release -- -quiet
|
| 404 |
|
|
@@ -411,6 +423,8 @@ cmake -B build-ios-device -G Xcode \
|
|
| 411 |
-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphoneos \
|
| 412 |
-DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \
|
| 413 |
-DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \
|
|
|
|
|
|
|
| 414 |
-S .
|
| 415 |
cmake --build build-ios-device --config Release -- -quiet
|
| 416 |
|
|
@@ -421,6 +435,8 @@ cmake -B build-macos -G Xcode \
|
|
| 421 |
-DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
|
| 422 |
-DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \
|
| 423 |
-DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \
|
|
|
|
|
|
|
| 424 |
-S .
|
| 425 |
cmake --build build-macos --config Release -- -quiet
|
| 426 |
|
|
|
|
| 108 |
fi
|
| 109 |
|
| 110 |
# Copy all required headers (common for all platforms)
|
| 111 |
+
cp include/whisper.h ${header_path}
|
| 112 |
cp ggml/include/ggml.h ${header_path}
|
| 113 |
cp ggml/include/ggml-alloc.h ${header_path}
|
| 114 |
cp ggml/include/ggml-backend.h ${header_path}
|
|
|
|
| 245 |
"${base_dir}/${build_dir}/ggml/src/ggml-metal/${release_dir}/libggml-metal.a"
|
| 246 |
"${base_dir}/${build_dir}/ggml/src/ggml-blas/${release_dir}/libggml-blas.a"
|
| 247 |
)
|
| 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
|
| 254 |
|
| 255 |
# Create temporary directory for processing
|
| 256 |
local temp_dir="${base_dir}/${build_dir}/temp"
|
| 257 |
+
echo "Creating temporary directory: ${temp_dir}"
|
| 258 |
mkdir -p "${temp_dir}"
|
| 259 |
|
| 260 |
# Since we have multiple architectures libtool will find object files that do not
|
|
|
|
| 266 |
local archs=""
|
| 267 |
local min_version_flag=""
|
| 268 |
local install_name=""
|
| 269 |
+
local frameworks="-framework Foundation -framework Metal -framework Accelerate"
|
| 270 |
|
| 271 |
case "$platform" in
|
| 272 |
"ios")
|
|
|
|
| 280 |
min_version_flag="-mios-version-min=${IOS_MIN_OS_VERSION}"
|
| 281 |
fi
|
| 282 |
install_name="@rpath/whisper.framework/whisper"
|
| 283 |
+
frameworks+=" -framework CoreML"
|
| 284 |
;;
|
| 285 |
"macos")
|
| 286 |
sdk="macosx"
|
| 287 |
archs="arm64 x86_64"
|
| 288 |
min_version_flag="-mmacosx-version-min=${MACOS_MIN_OS_VERSION}"
|
| 289 |
install_name="@rpath/whisper.framework/Versions/Current/whisper"
|
| 290 |
+
frameworks+=" -framework CoreML"
|
| 291 |
;;
|
| 292 |
"visionos")
|
| 293 |
if [[ "$is_simulator" == "true" ]]; then
|
|
|
|
| 329 |
$arch_flags \
|
| 330 |
$min_version_flag \
|
| 331 |
-Wl,-force_load,"${temp_dir}/combined.a" \
|
| 332 |
+
$frameworks \
|
| 333 |
-install_name "$install_name" \
|
| 334 |
-o "${base_dir}/${output_lib}"
|
| 335 |
|
|
|
|
| 409 |
-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphonesimulator \
|
| 410 |
-DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \
|
| 411 |
-DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \
|
| 412 |
+
-DWHISPER_COREML="ON" \
|
| 413 |
+
-DWHISPER_COREML_ALLOW_FALLBACK="ON" \
|
| 414 |
-S .
|
| 415 |
cmake --build build-ios-sim --config Release -- -quiet
|
| 416 |
|
|
|
|
| 423 |
-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphoneos \
|
| 424 |
-DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \
|
| 425 |
-DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \
|
| 426 |
+
-DWHISPER_COREML="ON" \
|
| 427 |
+
-DWHISPER_COREML_ALLOW_FALLBACK="ON" \
|
| 428 |
-S .
|
| 429 |
cmake --build build-ios-device --config Release -- -quiet
|
| 430 |
|
|
|
|
| 435 |
-DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
|
| 436 |
-DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \
|
| 437 |
-DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \
|
| 438 |
+
-DWHISPER_COREML="ON" \
|
| 439 |
+
-DWHISPER_COREML_ALLOW_FALLBACK="ON" \
|
| 440 |
-S .
|
| 441 |
cmake --build build-macos --config Release -- -quiet
|
| 442 |
|