From 8483ea67accbe3f74f0ae1cec986cb1308e72b45 Mon Sep 17 00:00:00 2001 From: Christian Privitelli Date: Mon, 16 Oct 2023 15:13:21 +1100 Subject: [PATCH 1/2] support metal, update whisper --- Package.swift | 51 ++++++++++++++++++++----- Sources/whisper_cpp/ggml-alloc.c | 1 + Sources/whisper_cpp/ggml-alloc.h | 1 + Sources/whisper_cpp/ggml-metal.h | 1 + Sources/whisper_metal/ggml-alloc.h | 1 + Sources/whisper_metal/ggml-metal.h | 1 + Sources/whisper_metal/ggml-metal.m | 1 + Sources/whisper_metal/ggml.h | 1 + Sources/whisper_metal/include/whisper.h | 1 + whisper.cpp | 2 +- 10 files changed, 51 insertions(+), 10 deletions(-) create mode 120000 Sources/whisper_cpp/ggml-alloc.c create mode 120000 Sources/whisper_cpp/ggml-alloc.h create mode 120000 Sources/whisper_cpp/ggml-metal.h create mode 120000 Sources/whisper_metal/ggml-alloc.h create mode 120000 Sources/whisper_metal/ggml-metal.h create mode 120000 Sources/whisper_metal/ggml-metal.m create mode 120000 Sources/whisper_metal/ggml.h create mode 120000 Sources/whisper_metal/include/whisper.h diff --git a/Package.swift b/Package.swift index 7626d7a..76cccd6 100644 --- a/Package.swift +++ b/Package.swift @@ -14,15 +14,48 @@ let package = Package( .library(name: "SwiftWhisper", targets: ["SwiftWhisper"]) ], targets: [ - .target(name: "SwiftWhisper", dependencies: [.target(name: "whisper_cpp")]), - .target(name: "whisper_cpp", - exclude: exclude, - cSettings: [ - .define("GGML_USE_ACCELERATE", .when(platforms: [.macOS, .macCatalyst, .iOS])), - .define("WHISPER_USE_COREML", .when(platforms: [.macOS, .macCatalyst, .iOS])), - .define("WHISPER_COREML_ALLOW_FALLBACK", .when(platforms: [.macOS, .macCatalyst, .iOS])) - ]), - .testTarget(name: "WhisperTests", dependencies: [.target(name: "SwiftWhisper")], resources: [.copy("TestResources/")]) + .target( + name: "SwiftWhisper", + dependencies: ["whisper_cpp", "whisper_cpp_metal"] + ), + .target( + name: "whisper_cpp_metal", + path: "Sources/whisper_metal", + sources: ["ggml-metal.m"], + resources: [.process("../Resources/ggml-metal.metal")], + publicHeadersPath: "include", + cSettings: [ + .unsafeFlags(["-fno-objc-arc"]) + ] + ), + .target( + name: "whisper_cpp", + dependencies: [.target(name: "whisper_cpp_metal")], + path: "Sources/whisper_cpp", + sources: [ + "ggml.c", + "ggml-alloc.c", + "coreml/whisper-encoder-impl.m", + "coreml/whisper-encoder.mm", + "whisper.cpp", + ], + publicHeadersPath: "include", + cSettings: [ + .unsafeFlags(["-Wno-shorten-64-to-32"]), + .define("GGML_USE_ACCELERATE", .when(platforms: [.macOS, .macCatalyst, .iOS])), + .define("WHISPER_USE_COREML", .when(platforms: [.macOS, .macCatalyst, .iOS])), + .define("WHISPER_COREML_ALLOW_FALLBACK", .when(platforms: [.macOS, .macCatalyst, .iOS])), + .define("GGML_USE_METAL", .when(platforms: [.macOS, .macCatalyst, .iOS])) + ], + linkerSettings: [ + .linkedFramework("Accelerate"), + ] + ), + .testTarget( + name: "WhisperTests", + dependencies: ["SwiftWhisper"], + resources: [.copy("TestResources/")] + ) ], cxxLanguageStandard: CXXLanguageStandard.cxx11 ) diff --git a/Sources/whisper_cpp/ggml-alloc.c b/Sources/whisper_cpp/ggml-alloc.c new file mode 120000 index 0000000..66cf21d --- /dev/null +++ b/Sources/whisper_cpp/ggml-alloc.c @@ -0,0 +1 @@ +../../whisper.cpp/ggml-alloc.c \ No newline at end of file diff --git a/Sources/whisper_cpp/ggml-alloc.h b/Sources/whisper_cpp/ggml-alloc.h new file mode 120000 index 0000000..971fb43 --- /dev/null +++ b/Sources/whisper_cpp/ggml-alloc.h @@ -0,0 +1 @@ +../../whisper.cpp/ggml-alloc.h \ No newline at end of file diff --git a/Sources/whisper_cpp/ggml-metal.h b/Sources/whisper_cpp/ggml-metal.h new file mode 120000 index 0000000..a0f7916 --- /dev/null +++ b/Sources/whisper_cpp/ggml-metal.h @@ -0,0 +1 @@ +../whisper_metal/ggml-metal.h \ No newline at end of file diff --git a/Sources/whisper_metal/ggml-alloc.h b/Sources/whisper_metal/ggml-alloc.h new file mode 120000 index 0000000..13216cf --- /dev/null +++ b/Sources/whisper_metal/ggml-alloc.h @@ -0,0 +1 @@ +../whisper_cpp/ggml-alloc.h \ No newline at end of file diff --git a/Sources/whisper_metal/ggml-metal.h b/Sources/whisper_metal/ggml-metal.h new file mode 120000 index 0000000..6c5d541 --- /dev/null +++ b/Sources/whisper_metal/ggml-metal.h @@ -0,0 +1 @@ +../../whisper.cpp/ggml-metal.h \ No newline at end of file diff --git a/Sources/whisper_metal/ggml-metal.m b/Sources/whisper_metal/ggml-metal.m new file mode 120000 index 0000000..0b08eee --- /dev/null +++ b/Sources/whisper_metal/ggml-metal.m @@ -0,0 +1 @@ +../../whisper.cpp/ggml-metal.m \ No newline at end of file diff --git a/Sources/whisper_metal/ggml.h b/Sources/whisper_metal/ggml.h new file mode 120000 index 0000000..57eb840 --- /dev/null +++ b/Sources/whisper_metal/ggml.h @@ -0,0 +1 @@ +../whisper_cpp/ggml.h \ No newline at end of file diff --git a/Sources/whisper_metal/include/whisper.h b/Sources/whisper_metal/include/whisper.h new file mode 120000 index 0000000..5769596 --- /dev/null +++ b/Sources/whisper_metal/include/whisper.h @@ -0,0 +1 @@ +../../whisper_cpp/include/whisper.h \ No newline at end of file diff --git a/whisper.cpp b/whisper.cpp index 95b02d7..54c978c 160000 --- a/whisper.cpp +++ b/whisper.cpp @@ -1 +1 @@ -Subproject commit 95b02d76b04d18e4ce37ed8353a1f0797f1717ea +Subproject commit 54c978c3a31dac51bb6fab06d5ac5b11b336ae30 From 727e21160d7a8b30c75883d685074999180439e8 Mon Sep 17 00:00:00 2001 From: Christian Privitelli Date: Mon, 16 Oct 2023 15:45:57 +1100 Subject: [PATCH 2/2] update directory name, remove old code --- Package.swift | 3 +-- Sources/whisper_cpp/ggml-metal.h | 2 +- Sources/{whisper_metal => whisper_cpp_metal}/ggml-alloc.h | 0 Sources/{whisper_metal => whisper_cpp_metal}/ggml-metal.h | 0 Sources/{whisper_metal => whisper_cpp_metal}/ggml-metal.m | 0 Sources/{whisper_metal => whisper_cpp_metal}/ggml.h | 0 Sources/{whisper_metal => whisper_cpp_metal}/include/whisper.h | 0 7 files changed, 2 insertions(+), 3 deletions(-) rename Sources/{whisper_metal => whisper_cpp_metal}/ggml-alloc.h (100%) rename Sources/{whisper_metal => whisper_cpp_metal}/ggml-metal.h (100%) rename Sources/{whisper_metal => whisper_cpp_metal}/ggml-metal.m (100%) rename Sources/{whisper_metal => whisper_cpp_metal}/ggml.h (100%) rename Sources/{whisper_metal => whisper_cpp_metal}/include/whisper.h (100%) diff --git a/Package.swift b/Package.swift index 76cccd6..d878088 100644 --- a/Package.swift +++ b/Package.swift @@ -20,9 +20,8 @@ let package = Package( ), .target( name: "whisper_cpp_metal", - path: "Sources/whisper_metal", + path: "Sources/whisper_cpp_metal", sources: ["ggml-metal.m"], - resources: [.process("../Resources/ggml-metal.metal")], publicHeadersPath: "include", cSettings: [ .unsafeFlags(["-fno-objc-arc"]) diff --git a/Sources/whisper_cpp/ggml-metal.h b/Sources/whisper_cpp/ggml-metal.h index a0f7916..d00bdb6 120000 --- a/Sources/whisper_cpp/ggml-metal.h +++ b/Sources/whisper_cpp/ggml-metal.h @@ -1 +1 @@ -../whisper_metal/ggml-metal.h \ No newline at end of file +../whisper_cpp_metal/ggml-metal.h \ No newline at end of file diff --git a/Sources/whisper_metal/ggml-alloc.h b/Sources/whisper_cpp_metal/ggml-alloc.h similarity index 100% rename from Sources/whisper_metal/ggml-alloc.h rename to Sources/whisper_cpp_metal/ggml-alloc.h diff --git a/Sources/whisper_metal/ggml-metal.h b/Sources/whisper_cpp_metal/ggml-metal.h similarity index 100% rename from Sources/whisper_metal/ggml-metal.h rename to Sources/whisper_cpp_metal/ggml-metal.h diff --git a/Sources/whisper_metal/ggml-metal.m b/Sources/whisper_cpp_metal/ggml-metal.m similarity index 100% rename from Sources/whisper_metal/ggml-metal.m rename to Sources/whisper_cpp_metal/ggml-metal.m diff --git a/Sources/whisper_metal/ggml.h b/Sources/whisper_cpp_metal/ggml.h similarity index 100% rename from Sources/whisper_metal/ggml.h rename to Sources/whisper_cpp_metal/ggml.h diff --git a/Sources/whisper_metal/include/whisper.h b/Sources/whisper_cpp_metal/include/whisper.h similarity index 100% rename from Sources/whisper_metal/include/whisper.h rename to Sources/whisper_cpp_metal/include/whisper.h