From dd20fe18eec761a4e532fbc3ba929ec5d0d8e31d Mon Sep 17 00:00:00 2001 From: star9029 Date: Mon, 22 May 2023 14:23:49 +0800 Subject: [PATCH] add whisper.cpp (#2103) * add whisper.cpp * use v1.4.2 * add link test * try patch * disable plat --- .../w/whisper.cpp/patches/1.4.2/fix.patch | 83 +++++++++++++++++++ packages/w/whisper.cpp/xmake.lua | 57 +++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 packages/w/whisper.cpp/patches/1.4.2/fix.patch create mode 100644 packages/w/whisper.cpp/xmake.lua diff --git a/packages/w/whisper.cpp/patches/1.4.2/fix.patch b/packages/w/whisper.cpp/patches/1.4.2/fix.patch new file mode 100644 index 000000000..31cdd865d --- /dev/null +++ b/packages/w/whisper.cpp/patches/1.4.2/fix.patch @@ -0,0 +1,83 @@ +diff --git a/CMakeLists.txt b/CMakeLists.txt +index a6cbcbc..aa7bf4a 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -217,38 +217,40 @@ endif() + + message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}") + +-if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64") +- message(STATUS "ARM detected") +-else() +- message(STATUS "x86 detected") +- if (MSVC) +- if(NOT WHISPER_NO_AVX2) +- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX2") +- set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:AVX2") +- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX2") ++if (NOT ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "") ++ if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64") ++ message(STATUS "ARM detected") ++ else() ++ message(STATUS "x86 detected") ++ if (MSVC) ++ if(NOT WHISPER_NO_AVX2) ++ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX2") ++ set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:AVX2") ++ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX2") ++ else() ++ if(NOT WHISPER_NO_AVX) ++ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX") ++ set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:AVX") ++ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX") ++ endif() ++ endif() ++ else() ++ if (EMSCRIPTEN) ++ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread") ++ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") + else() + if(NOT WHISPER_NO_AVX) +- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX") +- set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:AVX") +- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX") ++ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx") ++ endif() ++ if(NOT WHISPER_NO_AVX2) ++ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx2") ++ endif() ++ if(NOT WHISPER_NO_FMA) ++ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfma") ++ endif() ++ if(NOT WHISPER_NO_F16C) ++ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mf16c") + endif() +- endif() +- else() +- if (EMSCRIPTEN) +- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread") +- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") +- else() +- if(NOT WHISPER_NO_AVX) +- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx") +- endif() +- if(NOT WHISPER_NO_AVX2) +- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx2") +- endif() +- if(NOT WHISPER_NO_FMA) +- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfma") +- endif() +- if(NOT WHISPER_NO_F16C) +- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mf16c") + endif() + endif() + endif() +@@ -352,7 +354,7 @@ set_target_properties(${TARGET} PROPERTIES PUBLIC_HEADER "whisper.h") + + install(TARGETS ${TARGET} + LIBRARY DESTINATION lib +- ARCHIVE DESTINATION lib/static ++ ARCHIVE DESTINATION lib + RUNTIME DESTINATION bin + PUBLIC_HEADER DESTINATION include + ) diff --git a/packages/w/whisper.cpp/xmake.lua b/packages/w/whisper.cpp/xmake.lua new file mode 100644 index 000000000..703bc8dbc --- /dev/null +++ b/packages/w/whisper.cpp/xmake.lua @@ -0,0 +1,57 @@ +package("whisper.cpp") + + set_homepage("https://github.com/ggerganov/whisper.cpp") + set_description("Port of OpenAI's Whisper model in C/C++") + + set_urls("https://github.com/ggerganov/whisper.cpp/archive/refs/tags/v$(version).tar.gz", + "https://github.com/ggerganov/whisper.cpp.git") + + add_versions("1.4.2", "1b988dcc77fca55f188dbc4e472f971a80854c1d44309cf3eaab9d5677f175e1") + + add_patches("1.4.2", path.join(os.scriptdir(), "patches", "1.4.2", "fix.patch"), "1330bdbb769aad37f0de6998ac9b0107423ec62385bbfb0a89a98c226daace48") + + add_configs("avx", { description = "Enable AVX", default = true, type = "boolean"}) + add_configs("avx2", { description = "Enable AVX2", default = true, type = "boolean"}) + add_configs("fma", { description = "Enable FMA", default = true, type = "boolean"}) + add_configs("f16c", { description = "Enable F16c", default = true, type = "boolean"}) + if is_plat("macosx") then + add_configs("accelerate", { description = "Enable Accelerate framework", default = true, type = "boolean"}) + add_configs("coreml", { description = "Enable Core ML framework", default = false, type = "boolean"}) + add_configs("coreml_allow_fallback", { description = "Allow non-CoreML fallback", default = false, type = "boolean"}) + else + add_configs("openblas", { description = "Support for OpenBLAS", default = false, type = "boolean", readonly = true}) + add_configs("cublas", { description = "Support for cuBLAS", default = false, type = "boolean", readonly = true}) + add_configs("clblast", { description = "use CLBlast", default = false, type = "boolean", readonly = true}) + end + add_configs("perf", { description = "Enable perf timings", default = false, type = "boolean"}) + + add_deps("cmake") + + on_install("windows", "linux", "mingw", "msys", "android", "wasm", function (package) + local configs = {"-DWHISPER_BUILD_TESTS=OFF", "-DWHISPER_BUILD_EXAMPLES=OFF"} + table.insert(configs, "-DWHISPER_NO_AVX=" .. (package:config("avx") and "OFF" or "ON")) + table.insert(configs, "-DWHISPER_NO_AVX2=" .. (package:config("avx2") and "OFF" or "ON")) + table.insert(configs, "-DWHISPER_NO_FMA=" .. (package:config("fma") and "OFF" or "ON")) + table.insert(configs, "-DWHISPER_NO_F16C=" .. (package:config("f16c") and "OFF" or "ON")) + if package:is_plat("macosx") then + table.insert(configs, "-DWHISPER_NO_ACCELERATE=" .. (package:config("accelerate") and "OFF" or "ON")) + table.insert(configs, "-DWHISPER_COREML=" .. (package:config("coreml") and "ON" or "OFF")) + table.insert(configs, "-DWHISPER_COREML_ALLOW_FALLBACK=" .. (package:config("coreml_allow_fallback") and "ON" or "OFF")) + else + table.insert(configs, "-DWHISPER_OPENBLAS=" .. (package:config("openblas") and "ON" or "OFF")) + table.insert(configs, "-DWHISPER_CUBLAS=" .. (package:config("cublas") and "ON" or "OFF")) + table.insert(configs, "-DWHISPER_CLBLAST=" .. (package:config("clblast") and "ON" or "OFF")) + end + table.insert(configs, "-DWHISPER_PERF=" .. (package:config("perf") and "ON" or "OFF")) + table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release")) + table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF")) + import("package.tools.cmake").install(package, configs) + end) + + on_test(function (package) + assert(package:check_cxxsnippets({test = [[ + void test() { + whisper_context* ctx = whisper_init_from_file("ggml-base.en.bin"); + } + ]]}, {includes = {"whisper.h"}})) + end)