From 81a9db52fd57ad962de6af216b336b6b231d11f4 Mon Sep 17 00:00:00 2001 From: Claudio Costa Date: Tue, 29 Oct 2024 15:37:43 -0600 Subject: [PATCH] [MM-59980] Upgrade to whisper.cpp v1.7.1 (#33) * Avoid AV512 CPU extensions * Upgrade to whisper.cpp v1.7.1 * Go is not really needed in CI * Fix condition * Update opus * Fix expression --- .github/workflows/ci.yml | 24 ++++----------------- Makefile | 8 +++---- build/Dockerfile | 15 ++++++++----- build/build.sh | 4 ++-- build/lint.sh | 4 ++-- build/prepare_deps.sh | 2 ++ build/run_tests.sh | 4 ++-- build/whisper.patch | 21 ++++++++++++++++++ cmd/transcriber/apis/whisper.cpp/context.go | 2 +- 9 files changed, 48 insertions(+), 36 deletions(-) create mode 100644 build/whisper.patch diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 41ed052..2480700 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,12 +2,9 @@ on: [push] name: CI jobs: lint: - env: - GOPATH: ${{ github.workspace }} - defaults: run: - working-directory: ${{ env.GOPATH }}/src/github.com/${{ github.repository }} + working-directory: ${{ github.workspace }}/src/github.com/${{ github.repository }} runs-on: ubuntu-latest @@ -15,23 +12,15 @@ jobs: - name: Checkout Code uses: actions/checkout@v4 with: - path: ${{ env.GOPATH }}/src/github.com/${{ github.repository }} - - name: Install Go - uses: actions/setup-go@v5 - with: - go-version-file: ${{ env.GOPATH }}/src/github.com/${{ github.repository }}/go.mod - cache-dependency-path: ${{ env.GOPATH }}/src/github.com/${{ github.repository }}/go.sum + path: ${{ github.workspace }}/src/github.com/${{ github.repository }} - name: Lint code run: | make go-lint test: - env: - GOPATH: ${{ github.workspace }} - defaults: run: - working-directory: ${{ env.GOPATH }}/src/github.com/${{ github.repository }} + working-directory: ${{ github.workspace }}/src/github.com/${{ github.repository }} runs-on: ubuntu-latest @@ -39,12 +28,7 @@ jobs: - name: Checkout Code uses: actions/checkout@v4 with: - path: ${{ env.GOPATH }}/src/github.com/${{ github.repository }} - - name: Install Go - uses: actions/setup-go@v5 - with: - go-version-file: ${{ env.GOPATH }}/src/github.com/${{ github.repository }}/go.mod - cache-dependency-path: ${{ env.GOPATH }}/src/github.com/${{ github.repository }}/go.sum + path: ${{ github.workspace }}/src/github.com/${{ github.repository }} - name: Execute Tests run: | go mod download diff --git a/Makefile b/Makefile index 6b4f78f..c68df35 100644 --- a/Makefile +++ b/Makefile @@ -45,12 +45,12 @@ endif ## CGO dependencies # Whisper.cpp -WHISPER_VERSION ?= "1.6.2" -WHISPER_SHA ?= "da7988072022acc3cfa61b370b3c51baad017f1900c3dc4e68cb276499f66894" +WHISPER_VERSION ?= "1.7.1" +WHISPER_SHA ?= "97f19a32212f2f215e538ee37a16ff547aaebc54817bd8072034e02466ce6d55" WHISPER_MODELS ?= "tiny base small" # Opus -OPUS_VERSION ?= "1.4" -OPUS_SHA ?= "c9b32b4253be5ae63d1ff16eea06b94b5f0f2951b7a02aceef58e3a3ce49c51f" +OPUS_VERSION ?= "1.5.2" +OPUS_SHA ?= "65c1d2f78b9f2fb20082c38cbe47c951ad5839345876e46941612ee87f9a7ce1" # ONNX Runtime ONNX_VERSION ?= "1.18.1" # Azure Speech SDK diff --git a/build/Dockerfile b/build/Dockerfile index aaf47f2..6ae29f7 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -2,15 +2,15 @@ # A multi stage build, with golang used as a builder # and debian:sid-slim as runner ARG GO_VERSION -FROM --platform=$TARGETPLATFORM debian:bookworm-20240612-slim as base +FROM --platform=$TARGETPLATFORM debian:bookworm-20240612-slim AS base # Setup system dependencies WORKDIR /workdir -# Install TLS certificates +# Install TLS certificates and libgomp # hadolint ignore=DL3008 RUN apt-get update && \ - apt-get install --no-install-recommends -y ca-certificates && \ + apt-get install --no-install-recommends -y ca-certificates libgomp1 && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* @@ -18,7 +18,12 @@ RUN apt-get update && \ RUN groupadd -r calls && useradd -mr -g calls calls ARG GO_VERSION -FROM --platform=$TARGETPLATFORM golang:${GO_VERSION}-bookworm as builder +FROM --platform=$TARGETPLATFORM golang:${GO_VERSION}-bookworm AS builder +# Install patch +# hadolint ignore=DL3008 +RUN apt-get update && \ + apt-get install --no-install-recommends -y patch + ARG TARGETOS ARG TARGETARCH ARG GO_BUILD_PLATFORMS=${TARGETOS}-${TARGETARCH} @@ -37,7 +42,7 @@ ARG AZURE_SDK_SHA # We first copy just the build directory so that we can properly cache dependencies which can take a while to download. COPY ./build /src/build WORKDIR /src -RUN /bin/bash ./build/prepare_deps.sh ${OPUS_VERSION} ${OPUS_SHA} ${WHISPER_VERSION} ${WHISPER_SHA} "${WHISPER_MODELS}" ${ONNX_VERSION} ${TARGETARCH} ${AZURE_SDK_VERSION} ${AZURE_SDK_SHA} +RUN /bin/bash ./build/prepare_deps.sh ${OPUS_VERSION} ${OPUS_SHA} ${WHISPER_VERSION} ${WHISPER_SHA} "${WHISPER_MODELS}" ${ONNX_VERSION} ${TARGETARCH} ${AZURE_SDK_VERSION} ${AZURE_SDK_SHA} "true" COPY . /src RUN /bin/bash ./build/build.sh ${OPUS_VERSION} ${OPUS_SHA} ${WHISPER_VERSION} ${WHISPER_SHA} "${WHISPER_MODELS}" ${ONNX_VERSION} ${TARGETARCH} ${AZURE_SDK_VERSION} ${AZURE_SDK_SHA} diff --git a/build/build.sh b/build/build.sh index f067c4f..25efff0 100644 --- a/build/build.sh +++ b/build/build.sh @@ -12,9 +12,9 @@ AZURE_SDK_VERSION=$8 AZURE_SDK_SHA=$9 OPUS_INCLUDE_PATH="/tmp/opus-${OPUS_VERSION}/include" -WHISPER_INCLUDE_PATH="/tmp/whisper.cpp-${WHISPER_VERSION}" +WHISPER_INCLUDE_PATH="/tmp/whisper.cpp-${WHISPER_VERSION}/include:/tmp/whisper.cpp-${WHISPER_VERSION}/ggml/include" OPUS_LIBRARY_PATH="/tmp/opus-${OPUS_VERSION}/.libs" -WHISPER_LIBRARY_PATH=${WHISPER_INCLUDE_PATH} +WHISPER_LIBRARY_PATH="/tmp/whisper.cpp-${WHISPER_VERSION}" ONNX_INCLUDE_PATH="/tmp/onnxruntime-linux-${ONNX_VERSION}/include" ONNX_LIBRARY_PATH="/tmp/onnxruntime-linux-${ONNX_VERSION}/lib" AZURE_SDK_INCLUDE_PATH="/tmp/SpeechSDK-Linux-${AZURE_SDK_VERSION}/include/c_api" diff --git a/build/lint.sh b/build/lint.sh index 326ce75..6dbc16d 100644 --- a/build/lint.sh +++ b/build/lint.sh @@ -11,9 +11,9 @@ AZURE_SDK_VERSION=$7 AZURE_SDK_SHA=$8 OPUS_INCLUDE_PATH="/tmp/opus-${OPUS_VERSION}/include" -WHISPER_INCLUDE_PATH="/tmp/whisper.cpp-${WHISPER_VERSION}" +WHISPER_INCLUDE_PATH="/tmp/whisper.cpp-${WHISPER_VERSION}/include:/tmp/whisper.cpp-${WHISPER_VERSION}/ggml/include" OPUS_LIBRARY_PATH="/tmp/opus-${OPUS_VERSION}/.libs" -WHISPER_LIBRARY_PATH=${WHISPER_INCLUDE_PATH} +WHISPER_LIBRARY_PATH="/tmp/whisper.cpp-${WHISPER_VERSION}" ONNX_INCLUDE_PATH="/tmp/onnxruntime-linux-${ONNX_VERSION}/include" ONNX_LIBRARY_PATH="/tmp/onnxruntime-linux-${ONNX_VERSION}/lib" AZURE_SDK_INCLUDE_PATH="/tmp/SpeechSDK-Linux-${AZURE_SDK_VERSION}/include/c_api" diff --git a/build/prepare_deps.sh b/build/prepare_deps.sh index 8fb06e2..3cc2439 100644 --- a/build/prepare_deps.sh +++ b/build/prepare_deps.sh @@ -10,6 +10,7 @@ ONNX_VERSION=$6 TARGET_ARCH=$7 AZURE_SDK_VERSION=$8 AZURE_SDK_SHA=$9 +IS_BUILD=${10} ONNX_ARCH=x64 ONNX_SHA=a0994512ec1e1debc00c18bfc7a5f16249f6ebd6a6128ff2034464cc380ea211 if [ "$TARGET_ARCH" == "arm64" ]; then @@ -35,6 +36,7 @@ wget https://github.com/ggerganov/whisper.cpp/archive/refs/tags/v${WHISPER_VERSI echo "${WHISPER_SHA} v${WHISPER_VERSION}.tar.gz" | sha256sum --check && \ tar xf v${WHISPER_VERSION}.tar.gz && \ cd whisper.cpp-${WHISPER_VERSION} && \ +([[ "$TARGET_ARCH" == "amd64" ]] && [[ "$IS_BUILD" == "true" ]] && echo "Patching Whisper.CPP Makefile" && patch -p1 Makefile /src/build/whisper.patch) || true && \ for model in ${MODELS}; do ./models/download-ggml-model.sh "${model}"; done && \ make -j4 libwhisper.a UNAME_M=${UNAME_M} && \ cd /tmp && \ diff --git a/build/run_tests.sh b/build/run_tests.sh index db0fc0a..f43c723 100644 --- a/build/run_tests.sh +++ b/build/run_tests.sh @@ -12,9 +12,9 @@ AZURE_SDK_VERSION=$8 AZURE_SDK_SHA=$9 OPUS_INCLUDE_PATH="/tmp/opus-${OPUS_VERSION}/include" -WHISPER_INCLUDE_PATH="/tmp/whisper.cpp-${WHISPER_VERSION}" +WHISPER_INCLUDE_PATH="/tmp/whisper.cpp-${WHISPER_VERSION}/include:/tmp/whisper.cpp-${WHISPER_VERSION}/ggml/include" OPUS_LIBRARY_PATH="/tmp/opus-${OPUS_VERSION}/.libs" -WHISPER_LIBRARY_PATH=${WHISPER_INCLUDE_PATH} +WHISPER_LIBRARY_PATH="/tmp/whisper.cpp-${WHISPER_VERSION}" ONNX_INCLUDE_PATH="/tmp/onnxruntime-linux-${ONNX_VERSION}/include" ONNX_LIBRARY_PATH="/tmp/onnxruntime-linux-${ONNX_VERSION}/lib" AZURE_SDK_INCLUDE_PATH="/tmp/SpeechSDK-Linux-${AZURE_SDK_VERSION}/include/c_api" diff --git a/build/whisper.patch b/build/whisper.patch new file mode 100644 index 0000000..7e5372e --- /dev/null +++ b/build/whisper.patch @@ -0,0 +1,21 @@ +diff --git a/Makefile b/Makefile +index 32b7cbb..9ac28a4 100644 +--- a/Makefile ++++ b/Makefile +@@ -361,12 +361,12 @@ ifndef RISCV + + ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64)) + # Use all CPU extensions that are available: +- MK_CFLAGS += -march=native -mtune=native +- HOST_CXXFLAGS += -march=native -mtune=native ++ #MK_CFLAGS += -march=native -mtune=native ++ #HOST_CXXFLAGS += -march=native -mtune=native + + # Usage AVX-only +- #MK_CFLAGS += -mfma -mf16c -mavx +- #MK_CXXFLAGS += -mfma -mf16c -mavx ++ MK_CFLAGS += -mfma -mf16c -mavx ++ MK_CXXFLAGS += -mfma -mf16c -mavx + + # Usage SSSE3-only (Not is SSE3!) + #MK_CFLAGS += -mssse3 diff --git a/cmd/transcriber/apis/whisper.cpp/context.go b/cmd/transcriber/apis/whisper.cpp/context.go index 9f0e17c..fcbe580 100644 --- a/cmd/transcriber/apis/whisper.cpp/context.go +++ b/cmd/transcriber/apis/whisper.cpp/context.go @@ -1,6 +1,6 @@ package whisper -// #cgo linux LDFLAGS: -l:libwhisper.a -lm -lstdc++ +// #cgo linux LDFLAGS: -l:libwhisper.a -lm -lstdc++ -fopenmp // #cgo darwin LDFLAGS: -lwhisper -lstdc++ -framework Accelerate // #include // #include