diff --git a/.github/scripts/release-smoke.sh b/.github/scripts/release-smoke.sh new file mode 100755 index 0000000..95d347a --- /dev/null +++ b/.github/scripts/release-smoke.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# Smoke-test an installed release tree (cmake --install output): every binary +# resolves its libraries through the install rpath and starts. Needs no model. +# release-smoke.sh STAGE_DIR +# Runs on the host or inside a clean container (sh only, no bash). +set -eu +stage=$1 +cd / # not the build tree: libraries must come from the rpath, not the cwd + +if command -v ldd >/dev/null 2>&1; then + for f in "$stage"/bin/* "$stage"/lib/*.so; do + if ldd "$f" 2>/dev/null | grep -q "not found"; then + echo "missing libraries for $f:"; ldd "$f"; exit 1 + fi + done +elif command -v otool >/dev/null 2>&1; then + for f in "$stage"/bin/*; do otool -L "$f"; done +fi + +"$stage/bin/tts-cli" --version +if [ -x "$stage/bin/tts-server" ]; then "$stage/bin/tts-server" --version; fi +# codec-cli has no --version; its usage text proves it loaded. +"$stage/bin/codec-cli" 2>&1 | grep -q usage +echo "smoke ok: $stage" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8d353a1..1047435 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,13 +22,28 @@ jobs: - name: Install dependencies run: | sudo apt-get update - sudo apt-get install -y cmake build-essential pkg-config - + sudo apt-get install -y cmake build-essential pkg-config ninja-build ccache + + # Both ggml copies (codec.cpp's and the llama.cpp backbone's) compile + # through ccache once it is installed. A PR reads its own and main's caches. + - name: ccache + uses: actions/cache@v4 + with: + path: ${{ github.workspace }}/.ccache + key: ccache-ci-${{ github.run_id }} + restore-keys: ccache-ci- + - name: Configure CMake - run: cmake -B build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} - + run: cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} + - name: Build - run: cmake --build build --config ${{env.BUILD_TYPE}} -j$(nproc) + env: + CCACHE_DIR: ${{ github.workspace }}/.ccache + CCACHE_MAXSIZE: 400M + run: | + export CMAKE_BUILD_PARALLEL_LEVEL=$(nproc) # also caps the nested backbone build + cmake --build build + ccache -s - name: Test CLI exists run: | diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index efd1c9c..9edc498 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -48,7 +48,7 @@ jobs: - name: Install system dependencies run: | sudo apt-get update - sudo apt-get install -y cmake build-essential pkg-config python3-pip ffmpeg sox + sudo apt-get install -y cmake build-essential pkg-config python3-pip ffmpeg sox ninja-build ccache - name: Set up Python uses: actions/setup-python@v5 @@ -62,10 +62,22 @@ jobs: python3 -m pip install --upgrade pip python3 -m pip install -r requirements-e2e.txt + - name: ccache + uses: actions/cache@v4 + with: + path: ${{ github.workspace }}/.ccache + key: ccache-e2e-${{ github.run_id }} + restore-keys: ccache-e2e- + + # The codec smokes drive codec-cli and libcodec only: no llama.cpp backbone. - name: Build C++ + env: + CCACHE_DIR: ${{ github.workspace }}/.ccache + CCACHE_MAXSIZE: 200M run: | - cmake -B build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} - cmake --build build --config ${{env.BUILD_TYPE}} -j$(nproc) + cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCODEC_TTS_BACKBONE=OFF + cmake --build build --target codec-cli codec_shared -j$(nproc) + ccache -s - name: Verify CLI run: | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..7164ff2 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,249 @@ +name: Release + +# Prebuilt libraries + tools (codec-cli, tts-cli, tts-server). +# - push a tag v -> build every package, then a DRAFT GitHub release +# (publish it by hand after a look) +# - workflow_dispatch, or a PR that touches this recipe +# -> build every package as workflow artifacts only +# Cutting a release: bump `project(codec VERSION ...)` in CMakeLists.txt and +# `__version__` in python/codec_cpp/__init__.py, merge, push the tag. +# +# Budget: standard GitHub-hosted runners only (free on a public repo), never on +# a plain push. No CUDA / ROCm / SYCL packages: Vulkan covers those GPUs, and +# their builds are the slow, large ones. +# Windows: clang-cl against the MSVC runtime (/MD); the VC++ runtime DLLs ship +# app-local in bin/, so a clean machine needs no redistributable installed. +# +# CPU baseline: one fixed ISA for both ggml copies (codec.cpp's and the +# llama.cpp backbone's; SetupTtsBackbone.cmake forwards every GGML_* option): +# x86-64 AVX2 (Haswell / Zen 1 and newer), arm64 armv8.2-a+fp16+dotprod. +# OpenMP off, so the binaries need no libgomp. + +on: + push: + tags: [ 'v*' ] + workflow_dispatch: + pull_request: + paths: [ '.github/workflows/release.yml', '.github/scripts/release-smoke.sh' ] + +concurrency: + group: release-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +env: + CMAKE_COMMON: -G Ninja -DCMAKE_BUILD_TYPE=Release -DGGML_NATIVE=OFF -DGGML_OPENMP=OFF -DCMAKE_INSTALL_PREFIX=stage + VULKAN_SDK_TAG: vulkan-sdk-1.4.363.0 # Vulkan-Headers / SPIRV-Headers for the Android cross build + VULKAN_SDK_WIN: 1.4.313.2 # LunarG SDK for the Windows Vulkan package + +jobs: + version: + runs-on: ubuntu-24.04 + outputs: + name: ${{ steps.v.outputs.name }} + steps: + - uses: actions/checkout@v4 + - id: v + name: Version name (tags must match CMakeLists.txt and the Python package) + run: | + cmake_v=$(sed -nE 's/^project\(codec VERSION ([0-9.]+).*/\1/p' CMakeLists.txt) + py_v=$(sed -nE 's/^__version__ = "([^"]+)"/\1/p' python/codec_cpp/__init__.py) + if [ "${GITHUB_REF_TYPE}" = tag ]; then + tag_v=${GITHUB_REF_NAME#v} + if [ "$tag_v" != "$cmake_v" ] || [ "$tag_v" != "$py_v" ]; then + echo "::error::tag $GITHUB_REF_NAME, CMakeLists.txt $cmake_v, python $py_v: bump both before tagging" + exit 1 + fi + echo "name=$GITHUB_REF_NAME" >> "$GITHUB_OUTPUT" + else + echo "name=v$cmake_v-${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT" + fi + + build: + needs: version + timeout-minutes: 60 # longest job (Android) takes ~16 min; a stuck runner stops here + defaults: + run: + shell: bash + env: + # Both ggml copies compile through ccache once it is on PATH (ggml's + # GGML_CCACHE). A PR run reads its own earlier caches and main's. + CCACHE_DIR: ${{ github.workspace }}/.ccache + CCACHE_MAXSIZE: 500M + strategy: + fail-fast: false + matrix: + include: + - name: linux-x64-cpu + os: ubuntu-22.04 # glibc 2.35 + defines: -DGGML_AVX=ON -DGGML_AVX2=ON -DGGML_FMA=ON -DGGML_F16C=ON -DGGML_BMI2=ON -DGGML_AVX512=OFF + smoke_images: archlinux:latest ubuntu:22.04 + - name: linux-x64-vulkan + os: ubuntu-22.04 + defines: -DGGML_AVX=ON -DGGML_AVX2=ON -DGGML_FMA=ON -DGGML_F16C=ON -DGGML_BMI2=ON -DGGML_AVX512=OFF -DGGML_VULKAN=ON + smoke_images: ubuntu:22.04 + smoke_prep: apt-get update -qq && apt-get install -y -qq libvulkan1 >/dev/null && + - name: linux-arm64-cpu + os: ubuntu-22.04-arm + defines: -DGGML_CPU_ARM_ARCH=armv8.2-a+fp16+dotprod + smoke_images: ubuntu:22.04 debian:12 + - name: macos-arm64-metal + os: macos-15 + defines: -DGGML_CPU_ARM_ARCH=armv8.2-a+fp16+dotprod -DGGML_METAL=ON -DGGML_METAL_EMBED_LIBRARY=ON -DCMAKE_OSX_DEPLOYMENT_TARGET=13.3 + - name: android-arm64-vulkan + os: ubuntu-24.04 + android: true + defines: -DGGML_CPU_ARM_ARCH=armv8.2-a+fp16+dotprod -DGGML_VULKAN=ON -DCODEC_TTS_SERVER=OFF + - name: windows-x64-cpu + os: windows-2022 + defines: -DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl -DGGML_AVX=ON -DGGML_AVX2=ON -DGGML_FMA=ON -DGGML_F16C=ON -DGGML_BMI2=ON -DGGML_AVX512=OFF + - name: windows-x64-vulkan + os: windows-2022 + defines: -DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl -DGGML_AVX=ON -DGGML_AVX2=ON -DGGML_FMA=ON -DGGML_F16C=ON -DGGML_BMI2=ON -DGGML_AVX512=OFF -DGGML_VULKAN=ON + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + fetch-depth: 1 + + - name: ccache + uses: actions/cache@v4 + with: + path: ${{ github.workspace }}/.ccache + key: ccache-release-${{ matrix.name }}-${{ github.run_id }} + restore-keys: ccache-release-${{ matrix.name }}- + + - name: Dependencies (Linux) + if: runner.os == 'Linux' && !matrix.android + run: | + if [[ "${{ matrix.name }}" == *vulkan ]]; then + wget -qO - https://packages.lunarg.com/lunarg-signing-key-pub.asc | sudo apt-key add - + sudo wget -qO /etc/apt/sources.list.d/lunarg-vulkan-jammy.list https://packages.lunarg.com/vulkan/lunarg-vulkan-jammy.list + sudo apt-get update -y + sudo apt-get install -y ninja-build ccache vulkan-sdk + else + sudo apt-get update -y + sudo apt-get install -y ninja-build ccache + fi + + - name: Dependencies (macOS) + if: runner.os == 'macOS' + run: brew install ninja ccache + + - name: Dependencies (Android) + if: matrix.android + run: | + sudo apt-get update -y + sudo apt-get install -y ninja-build ccache glslc + # The NDK ships libvulkan but not vulkan.hpp / SPIRV-Headers. SPIRV-Headers + # installs into the Vulkan-Headers tree: ggml includes spirv.hpp but takes + # its include path from Vulkan only. + vk="$RUNNER_TEMP/Vulkan-Headers" + git clone -q --depth 1 -b "$VULKAN_SDK_TAG" https://github.com/KhronosGroup/Vulkan-Headers "$vk" + git clone -q --depth 1 -b "$VULKAN_SDK_TAG" https://github.com/KhronosGroup/SPIRV-Headers "$RUNNER_TEMP/SPIRV-Headers" + cmake -S "$RUNNER_TEMP/SPIRV-Headers" -B "$RUNNER_TEMP/spirv-build" -DSPIRV_HEADERS_ENABLE_TESTS=OFF \ + -DCMAKE_INSTALL_PREFIX="$vk" + cmake --install "$RUNNER_TEMP/spirv-build" + echo "ANDROID_DEFINES=-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_LATEST_HOME/build/cmake/android.toolchain.cmake \ + -DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-28 \ + -DVulkan_INCLUDE_DIR=$vk/include -DVulkan_GLSLC_EXECUTABLE=$(command -v glslc) \ + -DSPIRV-Headers_DIR=$vk/share/cmake/SPIRV-Headers" >> "$GITHUB_ENV" + + - name: MSVC environment (Windows) + if: runner.os == 'Windows' + shell: pwsh + # vcvars64 into the job environment, so the bash steps below build with + # the MSVC SDK, its Ninja and the LLVM clang-cl on the runner. + run: | + $vs = & "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -latest -property installationPath + cmd /c "`"$vs\VC\Auxiliary\Build\vcvars64.bat`" >nul && set" | ForEach-Object { + if ($_ -match '^([^=]+)=(.*)$') { "$($matches[1])=$($matches[2])" | Out-File $env:GITHUB_ENV -Append -Encoding utf8 } + } + + - name: Dependencies (Windows) + if: runner.os == 'Windows' + run: choco install -y --no-progress ccache + + - name: Dependencies (Windows Vulkan) + if: runner.os == 'Windows' && endsWith(matrix.name, 'vulkan') + shell: pwsh + run: | + curl.exe -sSLo "$env:RUNNER_TEMP\vulkansdk.exe" "https://sdk.lunarg.com/sdk/download/$env:VULKAN_SDK_WIN/windows/vulkansdk-windows-X64-$env:VULKAN_SDK_WIN.exe" + & "$env:RUNNER_TEMP\vulkansdk.exe" --accept-licenses --default-answer --confirm-command install | Out-Null + Add-Content $env:GITHUB_ENV "VULKAN_SDK=C:\VulkanSDK\$env:VULKAN_SDK_WIN" + Add-Content $env:GITHUB_PATH "C:\VulkanSDK\$env:VULKAN_SDK_WIN\bin" + + - name: Configure + run: cmake -S . -B build $CMAKE_COMMON ${{ matrix.defines }} ${ANDROID_DEFINES:-} + + - name: Build + # The env var also caps the nested llama.cpp backbone build. + run: | + export CMAKE_BUILD_PARALLEL_LEVEL=${NUMBER_OF_PROCESSORS:-$(getconf _NPROCESSORS_ONLN)} + cmake --build build + ccache -s + + - name: Install + run: cmake --install build --strip + + - name: VC++ runtime, app-local (Windows) + if: runner.os == 'Windows' + shell: pwsh + run: Copy-Item "$env:VCToolsRedistDir\x64\Microsoft.VC*.CRT\*.dll" stage\bin\ + + - name: Smoke test (host) + if: ${{ !matrix.android }} + run: sh .github/scripts/release-smoke.sh "$PWD/stage" + + - name: Smoke test (clean containers) + if: matrix.smoke_images + run: | + for img in ${{ matrix.smoke_images }}; do + echo "::group::$img" + docker run --rm -v "$PWD/stage:/opt/codec:ro" -v "$PWD/.github/scripts:/s:ro" "$img" \ + sh -c '${{ matrix.smoke_prep }} sh /s/release-smoke.sh /opt/codec' + echo "::endgroup::" + done + + - name: Package + run: | + pkg="codec-${{ needs.version.outputs.name }}-bin-${{ matrix.name }}" + mv stage "$pkg" + cp LICENSE README.md "$pkg/" + if [ "$RUNNER_OS" = Windows ]; then + 7z a -bd "$pkg.zip" "$pkg" >/dev/null + echo "PKG=$pkg.zip" >> "$GITHUB_ENV" + else + tar -czf "$pkg.tar.gz" "$pkg" + echo "PKG=$pkg.tar.gz" >> "$GITHUB_ENV" + fi + + - uses: actions/upload-artifact@v4 + with: + name: ${{ matrix.name }} + path: ${{ env.PKG }} + retention-days: 7 + compression-level: 0 # already compressed + + release: + needs: [ version, build ] + if: github.ref_type == 'tag' + runs-on: ubuntu-24.04 + permissions: + contents: write + steps: + - uses: actions/download-artifact@v4 + with: + path: dist + merge-multiple: true + - name: Draft release + env: + GH_TOKEN: ${{ github.token }} + run: | + cd dist + sha256sum *.tar.gz *.zip > SHA256SUMS + gh release create "$GITHUB_REF_NAME" --repo "$GITHUB_REPOSITORY" --draft \ + --title "$GITHUB_REF_NAME" --generate-notes *.tar.gz *.zip SHA256SUMS diff --git a/CMakeLists.txt b/CMakeLists.txt index 15a4d07..9f4cb69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,25 @@ cmake_minimum_required(VERSION 3.16) -project(codec C CXX) +project(codec VERSION 0.1.0 LANGUAGES C CXX) + +# Release tags are v; the commit lets a build from any +# checkout say what it is (`tts-cli --version`). +set(CODEC_BUILD_COMMIT "unknown") +find_package(Git QUIET) +if(GIT_FOUND) + execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE _codec_commit OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) + if(_codec_commit) + set(CODEC_BUILD_COMMIT "${_codec_commit}") + endif() +endif() set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) +if(MSVC) + add_compile_definitions(_USE_MATH_DEFINES) # M_PI, as glibc / libc++ give it +endif() # GGML Backend Options (pass-through to ggml subproject) # Goal: allow building with any ggml-native accelerator backend on other machines. @@ -150,6 +166,13 @@ target_include_directories(codec_audio_lm_objs add_library(codec STATIC $) add_library(codec_shared SHARED $ $) set_target_properties(codec_shared PROPERTIES OUTPUT_NAME codec) +if(WIN32) + # codec.dll's import library is codec.lib; the static one needs another + # name. No export macros in the headers: export every symbol, as the + # default visibility does elsewhere. + set_target_properties(codec PROPERTIES OUTPUT_NAME codec_static) + set_target_properties(codec_shared PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) +endif() target_include_directories(codec_objs PUBLIC @@ -254,6 +277,8 @@ if(CODEC_BUILD_TTS_CLI) tools/cli/main.cpp ) target_link_libraries(tts-cli PRIVATE codec_common codec_utils) + target_compile_definitions(tts-cli PRIVATE + CODEC_VERSION="${PROJECT_VERSION}" CODEC_BUILD_COMMIT="${CODEC_BUILD_COMMIT}") # Optional isolated llama.cpp backbone → enables the full `synthesize` # reference loop. The loop itself lives in codec_tts_runner (common/ @@ -270,11 +295,11 @@ if(CODEC_BUILD_TTS_CLI) target_include_directories(codec_tts_runner PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/common PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${TTS_BACKBONE_INCLUDE_DIRS}) - target_link_libraries(codec_tts_runner PUBLIC codec_common PRIVATE "${TTS_BACKBONE_SO}") + target_link_libraries(codec_tts_runner PUBLIC codec_common PRIVATE "${TTS_BACKBONE_LIB}") add_dependencies(tts-cli ttsbackbone) target_compile_definitions(tts-cli PRIVATE TTS_CLI_HAVE_BACKBONE=1) - target_link_libraries(tts-cli PRIVATE codec_tts_runner "${TTS_BACKBONE_SO}") + target_link_libraries(tts-cli PRIVATE codec_tts_runner "${TTS_BACKBONE_LIB}") set_target_properties(tts-cli PROPERTIES BUILD_RPATH "${CMAKE_BINARY_DIR}/ttsbackbone;${CMAKE_BINARY_DIR}/ggml/src") @@ -291,7 +316,9 @@ if(CODEC_BUILD_TTS_CLI) tools/server/audio_encode.cpp ${_ll_vendor}/cpp-httplib/httplib.cpp) target_include_directories(tts-server PRIVATE ${_ll_vendor} ${_ll_vendor}/cpp-httplib) - target_link_libraries(tts-server PRIVATE codec_tts_runner "${TTS_BACKBONE_SO}" Threads::Threads) + target_link_libraries(tts-server PRIVATE codec_tts_runner "${TTS_BACKBONE_LIB}" Threads::Threads) + target_compile_definitions(tts-server PRIVATE + CODEC_VERSION="${PROJECT_VERSION}" CODEC_BUILD_COMMIT="${CODEC_BUILD_COMMIT}") add_dependencies(tts-server ttsbackbone) set_target_properties(tts-server PROPERTIES BUILD_RPATH "${CMAKE_BINARY_DIR}/ttsbackbone;${CMAKE_BINARY_DIR}/ggml/src") @@ -355,18 +382,30 @@ if(NOT SKBUILD) install(TARGETS codec_shared LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} # codec.lib, the DLL's import library RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) install(FILES include/codec.h include/codec_speaker.h include/codec_lm.h include/codec_audio_lm.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) - foreach(_tool tts-cli tts-server) + foreach(_tool codec-cli tts-cli tts-server) if(TARGET ${_tool}) set_target_properties(${_tool} PROPERTIES INSTALL_RPATH "${_codec_origin}/${_codec_bin_to_lib}") + if(ANDROID) + # CMake's Android platform drops every RPATH; bionic honours a + # $ORIGIN DT_RUNPATH since API 24, so link it in directly. + target_link_options(${_tool} PRIVATE "LINKER:-rpath,\$ORIGIN/${_codec_bin_to_lib}") + endif() install(TARGETS ${_tool} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) endif() endforeach() if(TARGET ttsbackbone) - install(FILES "${TTS_BACKBONE_SO}" DESTINATION ${CMAKE_INSTALL_LIBDIR}) + # Windows has no rpath: DLLs load from the executable's directory, where + # ggml and libcodec install theirs too (RUNTIME → bin). + if(WIN32) + install(FILES "${TTS_BACKBONE_SO}" DESTINATION ${CMAKE_INSTALL_BINDIR}) + else() + install(FILES "${TTS_BACKBONE_SO}" DESTINATION ${CMAKE_INSTALL_LIBDIR}) + endif() endif() endif() # Wheels: the bundled llama.cpp backbone goes next to libcodec in the package diff --git a/README.md b/README.md index 7e55f2a..ffaa159 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,15 @@ bundled llama.cpp) plus a codec GGUF (run by codec.cpp). Ready-made GGUFs are on [Hugging Face](https://huggingface.co/BricksDisplay); each model card lists which two files to download. Supported models: [docs/model_status.md](docs/model_status.md#tts-tts-cli-synthesize). +Prebuilt packages for Linux (x64 CPU / Vulkan, arm64), Windows (x64 CPU / +Vulkan), macOS (arm64, Metal) and Android (arm64, Vulkan) are attached to each +[release](https://github.com/mybigday/codec.cpp/releases): unpack and run +`bin/tts-cli` (libraries are found in `lib/`; on Windows every DLL sits next to +`tts-cli.exe`). They need an AVX2 CPU on x86-64 and armv8.2-a with dotprod on +arm64 (e.g. not a Raspberry Pi 4); Linux packages need glibc 2.35 or newer, +Vulkan packages the system Vulkan loader (part of the GPU driver). Other +platforms, older CPUs and CUDA / ROCm / SYCL build from source. + Build (clone with `--recursive`: it fetches the llama.cpp that runs the text-side model; codec.cpp adds the architectures upstream llama.cpp lacks): @@ -69,9 +78,14 @@ text-side model; codec.cpp adds the architectures upstream llama.cpp lacks): git clone --recursive https://github.com/mybigday/codec.cpp cd codec.cpp cmake -S . -B build -DCMAKE_BUILD_TYPE=Release # GPU: add -DGGML_CUDA=ON / -DGGML_VULKAN=ON / -DGGML_METAL=ON -cmake --build build -j +cmake --build build -j 4 ``` +Give `-j` a number: a bare `-j` starts every compile at once and exhausts +memory. Each job takes up to about 1 GB; four jobs fit in 8 GB. With Ninja +(`-G Ninja`), also set `CMAKE_BUILD_PARALLEL_LEVEL=4`: the bundled llama.cpp +builds as a nested project that otherwise uses every core. + Synthesize: ```bash @@ -179,81 +193,81 @@ when streaming: with `0`, each chunk re-decodes everything generated so far. ### CUDA (NVIDIA) ```bash cmake -B build -DGGML_CUDA=ON -cmake --build build -j +cmake --build build -j 4 codec-cli --model model.gguf --codes in.npy --out out.wav --use-gpu ``` ### Vulkan (Cross-platform) ```bash cmake -B build -DGGML_VULKAN=ON -cmake --build build -j +cmake --build build -j 4 ``` ### Metal (macOS) ```bash cmake -B build -DGGML_METAL=ON -cmake --build build -j +cmake --build build -j 4 ``` ### SYCL (Intel GPUs) ```bash cmake -B build -DGGML_SYCL=ON -cmake --build build -j +cmake --build build -j 4 ``` ### OpenCL ```bash cmake -B build -DGGML_OPENCL=ON -cmake --build build -j +cmake --build build -j 4 ``` ### CANN (Ascend) ```bash cmake -B build -DGGML_CANN=ON -cmake --build build -j +cmake --build build -j 4 ``` ### HIP/ROCm (AMD GPUs) ```bash cmake -B build -DGGML_HIP=ON -cmake --build build -j +cmake --build build -j 4 ``` ### MUSA ```bash cmake -B build -DGGML_MUSA=ON -cmake --build build -j +cmake --build build -j 4 ``` ### WebGPU ```bash cmake -B build -DGGML_WEBGPU=ON -cmake --build build -j +cmake --build build -j 4 ``` ### zDNN ```bash cmake -B build -DGGML_ZDNN=ON -cmake --build build -j +cmake --build build -j 4 ``` ### VirtGPU ```bash cmake -B build -DGGML_VIRTGPU=ON -cmake --build build -j +cmake --build build -j 4 ``` ### Multiple backends (fallback chain) ```bash cmake -B build -DGGML_CUDA=ON -DGGML_VULKAN=ON -cmake --build build -j +cmake --build build -j 4 # Runtime auto-selects: CUDA > Vulkan > CPU ``` ### CPU-only (default) ```bash cmake -B build -cmake --build build -j +cmake --build build -j 4 codec-cli --model model.gguf --codes in.npy --out out.wav ``` diff --git a/cmake/SetupTtsBackbone.cmake b/cmake/SetupTtsBackbone.cmake index b6a397a..6e95bc0 100644 --- a/cmake/SetupTtsBackbone.cmake +++ b/cmake/SetupTtsBackbone.cmake @@ -24,6 +24,15 @@ include(ExternalProject) set(_ll_dir "${CMAKE_CURRENT_SOURCE_DIR}/common/third-party/llama.cpp") set(TTS_BACKBONE_SO "${CMAKE_BINARY_DIR}/ttsbackbone/${CMAKE_SHARED_LIBRARY_PREFIX}ttsbackbone${CMAKE_SHARED_LIBRARY_SUFFIX}") +# What consumers link: the import library on Windows, the library elsewhere. +if(WIN32) + set(TTS_BACKBONE_LIB + "${CMAKE_BINARY_DIR}/ttsbackbone/${CMAKE_IMPORT_LIBRARY_PREFIX}ttsbackbone${CMAKE_IMPORT_LIBRARY_SUFFIX}") +else() + set(TTS_BACKBONE_LIB "${TTS_BACKBONE_SO}") +endif() +set(_ll_byproducts "${TTS_BACKBONE_SO}" "${TTS_BACKBONE_LIB}") +list(REMOVE_DUPLICATES _ll_byproducts) # Every GGML_* cache option, at the value codec.cpp's ggml sees. Left out: # the ones the isolation fixes (static, no backend DL), install paths and @@ -55,7 +64,7 @@ foreach(_v CMAKE_TOOLCHAIN_FILE CMAKE_C_COMPILER CMAKE_CXX_COMPILER ANDROID_ABI ANDROID_PLATFORM ANDROID_NDK ANDROID_STL CMAKE_CUDA_COMPILER CMAKE_CUDA_HOST_COMPILER CMAKE_CUDA_ARCHITECTURES CMAKE_HIP_COMPILER CMAKE_HIP_ARCHITECTURES AMDGPU_TARGETS - Vulkan_INCLUDE_DIR Vulkan_LIBRARY Vulkan_GLSLC_EXECUTABLE) + Vulkan_INCLUDE_DIR Vulkan_LIBRARY Vulkan_GLSLC_EXECUTABLE SPIRV-Headers_DIR) if(DEFINED ${_v} AND NOT "${${_v}}" STREQUAL "") string(REPLACE ";" "|" _val "${${_v}}") list(APPEND _ll_args "-D${_v}=${_val}") @@ -85,11 +94,17 @@ ExternalProject_Add(ttsbackbone BUILD_COMMAND ${CMAKE_COMMAND} --build --target ttsbackbone --config Release # Rebuild on every build: the patched sources change outside ExternalProject's stamps. BUILD_ALWAYS ON - BUILD_BYPRODUCTS "${TTS_BACKBONE_SO}" + BUILD_BYPRODUCTS ${_ll_byproducts} INSTALL_COMMAND "" UPDATE_COMMAND "" ) +# Build it after codec.cpp's own libraries, not beside them: the nested build +# runs its own parallelism (Ninja: every core, whatever the outer -j), so side +# by side the two compile loads add up and a 16 GB machine runs out of memory. +# One after the other takes about as long, both being CPU-bound. +add_dependencies(ttsbackbone codec_shared) + # Exported for the tts-cli target. `common` is added so the runner can # include common.h / sampling.h (the common_sampler API). set(TTS_BACKBONE_INCLUDE_DIRS diff --git a/cmake/ttsbackbone/CMakeLists.txt b/cmake/ttsbackbone/CMakeLists.txt index 89c03c0..1f0465c 100644 --- a/cmake/ttsbackbone/CMakeLists.txt +++ b/cmake/ttsbackbone/CMakeLists.txt @@ -13,6 +13,13 @@ set(TTSBACKBONE_OUTPUT_DIR "${CMAKE_BINARY_DIR}" CACHE PATH "where libttsbackbon add_subdirectory("${LLAMA_DIR}" llama EXCLUDE_FROM_ALL) +# ggml-vulkan.cpp at -O3: LLVM's register coalescer takes ~14 minutes on it +# (NDK clang 21, 822 of 880 s); -O2 compiles it in under a minute. It is +# host-side dispatch code, so the GPU kernels run as fast either way. +if(TARGET ggml-vulkan AND NOT MSVC) + target_compile_options(ggml-vulkan PRIVATE -O2) +endif() + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/ttsbackbone.c" "") add_library(ttsbackbone SHARED "${CMAKE_CURRENT_BINARY_DIR}/ttsbackbone.c") set_target_properties(ttsbackbone PROPERTIES @@ -23,7 +30,24 @@ target_link_libraries(ttsbackbone PRIVATE llama llama-common) # Nothing in the wrapper references llama / common, so their archives go in # whole; ggml's objects are pulled by their references (static backend # registry, GGML_BACKEND_DL=OFF). -if(APPLE) +if(WIN32) + # A PE image binds each import to a named DLL, so the two ggml copies + # cannot clash; the export list is a .def file taken from the llama / + # common objects (llama.cpp built static has no dllexport). + set_target_properties(ttsbackbone PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${TTSBACKBONE_OUTPUT_DIR}" + ARCHIVE_OUTPUT_DIRECTORY "${TTSBACKBONE_OUTPUT_DIR}") + set(_objs "${CMAKE_CURRENT_BINARY_DIR}/ttsbackbone.objs") + set(_def "${CMAKE_CURRENT_BINARY_DIR}/ttsbackbone.def") + file(GENERATE OUTPUT "${_objs}" + CONTENT "$;$,\n>\n") + add_custom_command(TARGET ttsbackbone PRE_LINK + COMMAND ${CMAKE_COMMAND} -DOBJS=${_objs} -DDEF=${_def} + -P "${CMAKE_CURRENT_SOURCE_DIR}/windows-def.cmake" + VERBATIM) + target_link_options(ttsbackbone PRIVATE + "/WHOLEARCHIVE:$" "/WHOLEARCHIVE:$" "/DEF:${_def}") +elseif(APPLE) target_link_options(ttsbackbone PRIVATE "LINKER:-force_load,$" "LINKER:-force_load,$") diff --git a/cmake/ttsbackbone/windows-def.cmake b/cmake/ttsbackbone/windows-def.cmake new file mode 100644 index 0000000..ed3df7c --- /dev/null +++ b/cmake/ttsbackbone/windows-def.cmake @@ -0,0 +1,19 @@ +# ttsbackbone.def: the llama_* C API and the common_sampler C++ API, as the +# version script / exported-symbols list do on ELF / Mach-O. +# cmake -DOBJS= -DDEF= -P windows-def.cmake +# CMake's own WINDOWS_EXPORT_ALL_SYMBOLS generator lists every external symbol +# (`__create_def`: native COFF reader on Windows, llvm-nm elsewhere); keep ours. +execute_process(COMMAND ${CMAKE_COMMAND} -E __create_def "${DEF}.all" "${OBJS}" + RESULT_VARIABLE _rc) +if(NOT _rc EQUAL 0) + message(FATAL_ERROR "listing the ttsbackbone exports failed") +endif() +file(STRINGS "${DEF}.all" _lines) +set(_out "EXPORTS\n") +foreach(_l IN LISTS _lines) + # C names are bare (llama_x); MSVC-mangled C++ names start with ?name@. + if(_l MATCHES "^[ \t]*(llama_|\\?common_sampler)") + string(APPEND _out "${_l}\n") + endif() +endforeach() +file(WRITE "${DEF}" "${_out}") diff --git a/python/README.md b/python/README.md index 5e2cc5e..690a354 100644 --- a/python/README.md +++ b/python/README.md @@ -26,7 +26,7 @@ fetches submodules). ggml is built with `GGML_NATIVE` on, so the wheel is tuned to the machine that builds it. **From a source checkout, with no install.** Build the library the usual way -(`cmake --build build -j`) and put `python/` on `sys.path`. The loader finds +(`cmake --build build -j 4`) and put `python/` on `sys.path`. The loader finds `build/libcodec.so` on its own. This is how `tests/e2e` runs. The loader looks for the library in this order: diff --git a/tools/cli/main.cpp b/tools/cli/main.cpp index 3ddc2f3..87f0a43 100644 --- a/tools/cli/main.cpp +++ b/tools/cli/main.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -478,6 +479,10 @@ int cmd_synthesize(args a) { // note: args by value so we can mutate text/ref_t } // namespace int main(int argc, char ** argv) { + if (argc == 2 && std::strcmp(argv[1], "--version") == 0) { + std::printf("%s %s (%s)\n", argv[0], CODEC_VERSION, CODEC_BUILD_COMMIT); + return 0; + } args a; if (!parse_args(argc, argv, &a)) { print_usage(argv[0]); return 1; } diff --git a/tools/server/main.cpp b/tools/server/main.cpp index 528bad1..076ba41 100644 --- a/tools/server/main.cpp +++ b/tools/server/main.cpp @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -308,9 +309,19 @@ class speech_stream { } // namespace int main(int argc, char ** argv) { + if (argc == 2 && std::strcmp(argv[1], "--version") == 0) { + std::printf("%s %s (%s)\n", argv[0], CODEC_VERSION, CODEC_BUILD_COMMIT); + return 0; + } args a; if (!parse_args(argc, argv, &a)) { print_usage(argv[0]); return 1; } - std::setvbuf(stdout, nullptr, _IOLBF, 0); // runner progress lines show up as they happen in a log file + // Runner progress lines show up as they happen in a log file. The MSVC CRT + // has no line buffering (_IOLBF is full buffering) and aborts on size 0. +#ifdef _WIN32 + std::setvbuf(stdout, nullptr, _IONBF, 0); +#else + std::setvbuf(stdout, nullptr, _IOLBF, 0); +#endif if (a.segment_bytes <= 0 && a.stream_chunk_ms > 0) { std::fprintf(stderr, "warning: --segment-bytes 0 with frame streaming re-decodes the whole growing clip " "every chunk; long inputs get slow\n");